2 * Copyright (c) 2004-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * - bond/failover interface
32 * - implements IEEE 802.3ad Link Aggregation
36 * Modification History:
38 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/kern_event.h>
54 #include <net/ethernet.h>
56 #include <net/kpi_interface.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_ether.h>
60 #include <net/if_types.h>
61 #include <net/if_bond_var.h>
62 #include <net/ieee8023ad.h>
66 #include <net/devtimer.h>
67 #include <net/if_vlan_var.h>
68 #include <net/kpi_protocol.h>
70 #include <kern/locks.h>
71 #include <libkern/OSAtomic.h>
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
79 #include <net/if_media.h>
80 #include <net/multicast_list.h>
82 static struct ether_addr slow_proto_multicast
= {
83 IEEE8023AD_SLOW_PROTO_MULTICAST
86 #define BOND_MAXUNIT 128
87 #define BONDNAME "bond"
88 #define M_BOND M_DEVBUF
90 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
91 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
92 #define EA_LIST(ea) EA_CH(ea,0),EA_CH(ea,1),EA_CH(ea,2),EA_CH(ea,3),EA_CH(ea,4),EA_CH(ea,5)
94 #define timestamp_printf printf
99 static __inline__ lck_grp_t
*
100 my_lck_grp_alloc_init(const char * grp_name
)
103 lck_grp_attr_t
* grp_attrs
;
105 grp_attrs
= lck_grp_attr_alloc_init();
106 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
107 lck_grp_attr_free(grp_attrs
);
111 static __inline__ lck_mtx_t
*
112 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
114 lck_attr_t
* lck_attrs
;
117 lck_attrs
= lck_attr_alloc_init();
118 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
119 lck_attr_free(lck_attrs
);
123 static lck_mtx_t
* bond_lck_mtx
;
125 static __inline__
void
128 lck_grp_t
* bond_lck_grp
;
130 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
131 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
134 static __inline__
void
135 bond_assert_lock_held(void)
137 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
141 static __inline__
void
142 bond_assert_lock_not_held(void)
144 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
148 static __inline__
void
151 lck_mtx_lock(bond_lck_mtx
);
155 static __inline__
void
158 lck_mtx_unlock(bond_lck_mtx
);
163 ** bond structures, types
167 lacp_system li_system
;
168 lacp_system_priority li_system_priority
;
171 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
174 TAILQ_HEAD(port_list
, bondport_s
);
176 TAILQ_HEAD(ifbond_list
, ifbond_s
);
178 TAILQ_HEAD(lag_list
, LAG_s
);
180 typedef struct ifbond_s ifbond
, * ifbond_ref
;
181 typedef struct bondport_s bondport
, * bondport_ref
;
184 TAILQ_ENTRY(LAG_s
) lag_list
;
185 struct port_list lag_port_list
;
186 short lag_port_count
;
187 short lag_selected_port_count
;
188 int lag_active_media
;
191 typedef struct LAG_s LAG
, * LAG_ref
;
193 typedef struct partner_state_s
{
194 LAG_info ps_lag_info
;
196 lacp_port_priority ps_port_priority
;
197 lacp_actor_partner_state ps_state
;
198 } partner_state
, * partner_state_ref
;
201 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
203 SInt32 ifb_retain_count
;
204 char ifb_name
[IFNAMSIZ
];
205 struct ifnet
* ifb_ifp
;
206 bpf_packet_func ifb_bpf_input
;
207 bpf_packet_func ifb_bpf_output
;
209 struct port_list ifb_port_list
;
210 short ifb_port_count
;
211 struct lag_list ifb_lag_list
;
213 short ifb_max_active
; /* 0 == unlimited */
214 LAG_ref ifb_active_lag
;
215 struct ifmultiaddr
* ifb_ifma_slow_proto
;
216 bondport_ref
* ifb_distributing_array
;
217 int ifb_distributing_count
;
218 int ifb_last_link_event
;
219 int ifb_mode
; /* LACP, STATIC */
228 ReceiveState_none
= 0,
229 ReceiveState_INITIALIZE
= 1,
230 ReceiveState_PORT_DISABLED
= 2,
231 ReceiveState_EXPIRED
= 3,
232 ReceiveState_LACP_DISABLED
= 4,
233 ReceiveState_DEFAULTED
= 5,
234 ReceiveState_CURRENT
= 6,
237 typedef u_char ReceiveState
;
240 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
241 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
242 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
244 typedef u_char SelectedState
;
246 static __inline__
const char *
247 SelectedStateString(SelectedState s
)
249 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
251 if (s
<= SelectedState_STANDBY
) {
254 return ("<unknown>");
259 MuxState_DETACHED
= 1,
260 MuxState_WAITING
= 2,
261 MuxState_ATTACHED
= 3,
262 MuxState_COLLECTING_DISTRIBUTING
= 4,
265 typedef u_char MuxState
;
268 TAILQ_ENTRY(bondport_s
) po_port_list
;
270 struct multicast_list po_multicast
;
271 struct ifnet
* po_ifp
;
272 struct ether_addr po_saved_addr
;
274 char po_name
[IFNAMSIZ
];
275 struct ifdevmtu po_devmtu
;
278 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
279 devtimer_ref po_current_while_timer
;
280 devtimer_ref po_periodic_timer
;
281 devtimer_ref po_wait_while_timer
;
282 devtimer_ref po_transmit_timer
;
283 partner_state po_partner_state
;
284 lacp_port_priority po_priority
;
285 lacp_actor_partner_state po_actor_state
;
287 u_char po_periodic_interval
;
288 u_char po_n_transmit
;
289 ReceiveState po_receive_state
;
290 MuxState po_mux_state
;
291 SelectedState po_selected
;
292 int32_t po_last_transmit_secs
;
293 struct media_info po_media_info
;
297 #define IFBF_PROMISC 0x1 /* promiscuous mode */
298 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
299 #define IFBF_LLADDR 0x4 /* specific link address requested */
300 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
302 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
305 static __inline__
int
306 ifbond_flags_promisc(ifbond_ref ifb
)
308 return ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0);
311 static __inline__
void
312 ifbond_flags_set_promisc(ifbond_ref ifb
)
314 ifb
->ifb_flags
|= IFBF_PROMISC
;
318 static __inline__
void
319 ifbond_flags_clear_promisc(ifbond_ref ifb
)
321 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
325 static __inline__
int
326 ifbond_flags_if_detaching(ifbond_ref ifb
)
328 return ((ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0);
331 static __inline__
void
332 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
334 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
338 static __inline__
int
339 ifbond_flags_lladdr(ifbond_ref ifb
)
341 return ((ifb
->ifb_flags
& IFBF_LLADDR
) != 0);
344 static __inline__
void
345 ifbond_flags_set_lladdr(ifbond_ref ifb
)
347 ifb
->ifb_flags
|= IFBF_LLADDR
;
351 static __inline__
void
352 ifbond_flags_clear_lladdr(ifbond_ref ifb
)
354 ifb
->ifb_flags
&= ~IFBF_LLADDR
;
358 static __inline__
int
359 ifbond_flags_change_in_progress(ifbond_ref ifb
)
361 return ((ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0);
364 static __inline__
void
365 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
367 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
371 static __inline__
void
372 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
374 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
379 * bondport_ref->po_flags bits
381 #define BONDPORT_FLAGS_NTT 0x01
382 #define BONDPORT_FLAGS_READY 0x02
383 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
384 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
385 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
386 #define BONDPORT_FLAGS_UNUSED2 0x20
387 #define BONDPORT_FLAGS_UNUSED3 0x40
388 #define BONDPORT_FLAGS_UNUSED4 0x80
390 static __inline__
void
391 bondport_flags_set_ntt(bondport_ref p
)
393 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
397 static __inline__
void
398 bondport_flags_clear_ntt(bondport_ref p
)
400 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
404 static __inline__
int
405 bondport_flags_ntt(bondport_ref p
)
407 return ((p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0);
410 static __inline__
void
411 bondport_flags_set_ready(bondport_ref p
)
413 p
->po_flags
|= BONDPORT_FLAGS_READY
;
417 static __inline__
void
418 bondport_flags_clear_ready(bondport_ref p
)
420 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
424 static __inline__
int
425 bondport_flags_ready(bondport_ref p
)
427 return ((p
->po_flags
& BONDPORT_FLAGS_READY
) != 0);
430 static __inline__
void
431 bondport_flags_set_selected_changed(bondport_ref p
)
433 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
437 static __inline__
void
438 bondport_flags_clear_selected_changed(bondport_ref p
)
440 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
444 static __inline__
int
445 bondport_flags_selected_changed(bondport_ref p
)
447 return ((p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0);
450 static __inline__
void
451 bondport_flags_set_mux_attached(bondport_ref p
)
453 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
457 static __inline__
void
458 bondport_flags_clear_mux_attached(bondport_ref p
)
460 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
464 static __inline__
int
465 bondport_flags_mux_attached(bondport_ref p
)
467 return ((p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0);
470 static __inline__
void
471 bondport_flags_set_distributing(bondport_ref p
)
473 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
477 static __inline__
void
478 bondport_flags_clear_distributing(bondport_ref p
)
480 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
484 static __inline__
int
485 bondport_flags_distributing(bondport_ref p
)
487 return ((p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0);
490 typedef struct bond_globals_s
{
491 struct ifbond_list ifbond_list
;
493 lacp_system_priority system_priority
;
495 } * bond_globals_ref
;
497 static bond_globals_ref g_bond
;
500 ** packet_buffer routines
501 ** - thin wrapper for mbuf
504 typedef struct mbuf
* packet_buffer_ref
;
506 static packet_buffer_ref
507 packet_buffer_allocate(int length
)
512 /* leave room for ethernet header */
513 size
= length
+ sizeof(struct ether_header
);
514 if (size
> (int)MHLEN
) {
515 if (size
> (int)MCLBYTES
) {
516 printf("bond: packet_buffer_allocate size %d > max %u\n",
520 m
= m_getcl(M_WAITOK
, MT_DATA
, M_PKTHDR
);
522 m
= m_gethdr(M_WAITOK
, MT_DATA
);
528 m
->m_pkthdr
.len
= size
;
533 packet_buffer_byteptr(packet_buffer_ref buf
)
535 return (buf
->m_data
+ sizeof(struct ether_header
));
543 LAEventSelectedChange
,
552 bondport_receive_machine(bondport_ref p
, LAEvent event
,
555 ** Periodic Transmission machine
558 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
564 #define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
567 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
574 bondport_mux_machine(bondport_ref p
, LAEvent event
,
581 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
584 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
587 ifbond_all_ports_ready(ifbond_ref bond
);
590 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
593 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
596 ifbond_selection(ifbond_ref bond
);
604 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
607 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
610 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
611 int active
, int short_timeout
, int * error
);
613 bondport_start(bondport_ref p
);
616 bondport_free(bondport_ref p
);
619 bondport_aggregatable(bondport_ref p
);
622 bondport_remove_from_LAG(bondport_ref p
);
625 bondport_set_selected(bondport_ref p
, SelectedState s
);
628 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
631 bondport_link_status_changed(bondport_ref p
);
634 bondport_enable_distributing(bondport_ref p
);
637 bondport_disable_distributing(bondport_ref p
);
639 static __inline__
int
640 bondport_collecting(bondport_ref p
)
642 if (p
->po_bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
643 return (lacp_actor_partner_state_collecting(p
->po_actor_state
));
649 ** bond interface/dlil specific routines
651 static int bond_clone_create(struct if_clone
*, u_int32_t
, void *);
652 static int bond_clone_destroy(struct ifnet
*);
653 static int bond_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t m
,
655 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
656 static int bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * addr
);
657 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
658 bpf_packet_func func
);
659 static int bond_attach_protocol(struct ifnet
*ifp
);
660 static int bond_detach_protocol(struct ifnet
*ifp
);
661 static int bond_setmulti(struct ifnet
*ifp
);
662 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
663 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
664 static void bond_if_free(struct ifnet
* ifp
);
666 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
671 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
674 siocsifmtu(struct ifnet
* ifp
, int mtu
)
678 bzero(&ifr
, sizeof(ifr
));
680 return (ifnet_ioctl(ifp
, 0, SIOCSIFMTU
, &ifr
));
684 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
689 bzero(&ifr
, sizeof(ifr
));
690 error
= ifnet_ioctl(ifp
, 0, SIOCGIFDEVMTU
, &ifr
);
692 *ifdm_p
= ifr
.ifr_devmtu
;
697 static __inline__
void
698 ether_addr_copy(void * dest
, const void * source
)
700 bcopy(source
, dest
, ETHER_ADDR_LEN
);
704 static __inline__
void
705 ifbond_retain(ifbond_ref ifb
)
707 OSIncrementAtomic(&ifb
->ifb_retain_count
);
710 static __inline__
void
711 ifbond_release(ifbond_ref ifb
)
713 UInt32 old_retain_count
;
715 old_retain_count
= OSDecrementAtomic(&ifb
->ifb_retain_count
);
716 switch (old_retain_count
) {
718 panic("ifbond_release: retain count is 0\n");
721 if (g_bond
->verbose
) {
722 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
724 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
725 if (g_bond
->verbose
) {
726 printf("ifbond_release(%s) removing multicast\n",
729 (void) if_delmulti_anon(ifb
->ifb_ifma_slow_proto
->ifma_ifp
,
730 ifb
->ifb_ifma_slow_proto
->ifma_addr
);
731 IFMA_REMREF(ifb
->ifb_ifma_slow_proto
);
733 if (ifb
->ifb_distributing_array
!= NULL
) {
734 FREE(ifb
->ifb_distributing_array
, M_BOND
);
745 * Function: ifbond_wait
747 * Allows a single thread to gain exclusive access to the ifbond
748 * data structure. Some operations take a long time to complete,
749 * and some have side-effects that we can't predict. Holding the
750 * bond_lock() across such operations is not possible.
753 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
754 * complete. Simply holding the bond_lock() would freeze all other
755 * data structure accesses during that time.
756 * 2) When we attach our protocol to the interface, a dlil event is
757 * generated and invokes our bond_event() function. bond_event()
758 * needs to take the bond_lock(), but we're already holding it, so
759 * we're deadlocked against ourselves.
761 * Before calling, you must be holding the bond_lock and have taken
762 * a reference on the ifbond_ref.
765 ifbond_wait(ifbond_ref ifb
, const char * msg
)
769 /* other add/remove in progress */
770 while (ifbond_flags_change_in_progress(ifb
)) {
771 if (g_bond
->verbose
) {
772 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
775 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
777 /* prevent other bond list remove/add from taking place */
778 ifbond_flags_set_change_in_progress(ifb
);
779 if (g_bond
->verbose
&& waited
) {
780 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
786 * Function: ifbond_signal
788 * Allows the thread that previously invoked ifbond_wait() to
789 * give up exclusive access to the ifbond data structure, and wake up
790 * any other threads waiting to access
792 * Before calling, you must be holding the bond_lock and have taken
793 * a reference on the ifbond_ref.
796 ifbond_signal(ifbond_ref ifb
, const char * msg
)
798 ifbond_flags_clear_change_in_progress(ifb
);
799 wakeup((caddr_t
)ifb
);
800 if (g_bond
->verbose
) {
801 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
811 link_speed(int active
)
813 switch (IFM_SUBTYPE(active
)) {
834 /* assume that new defined types are going to be at least 10GigE */
841 static __inline__
int
842 media_active(const struct media_info
* mi
)
844 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
847 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
850 static __inline__
int
851 media_full_duplex(const struct media_info
* mi
)
853 return ((mi
->mi_active
& IFM_FDX
) != 0);
856 static __inline__
int
857 media_speed(const struct media_info
* mi
)
859 return (link_speed(mi
->mi_active
));
862 static struct media_info
863 interface_media_info(struct ifnet
* ifp
)
865 struct ifmediareq ifmr
;
866 struct media_info mi
;
868 bzero(&mi
, sizeof(mi
));
869 bzero(&ifmr
, sizeof(ifmr
));
870 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
871 if (ifmr
.ifm_count
!= 0) {
872 mi
.mi_status
= ifmr
.ifm_status
;
873 mi
.mi_active
= ifmr
.ifm_active
;
880 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
885 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
886 * currently expects it that way
888 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
889 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
890 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
891 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
897 static bond_globals_ref
898 bond_globals_create(lacp_system_priority sys_pri
,
903 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
);
907 bzero(b
, sizeof(*b
));
908 TAILQ_INIT(&b
->ifbond_list
);
910 b
->system_priority
= sys_pri
;
915 bond_globals_init(void)
921 bond_assert_lock_not_held();
923 if (g_bond
!= NULL
) {
928 * use en0's ethernet address as the system identifier, and if it's not
929 * there, use en1 .. en3
932 for (i
= 0; i
< 4; i
++) {
933 char ifname
[IFNAMSIZ
+1];
934 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
935 ifp
= ifunit(ifname
);
942 b
= bond_globals_create(0x8000, (lacp_system_ref
)ifnet_lladdr(ifp
));
945 if (g_bond
!= NULL
) {
962 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
963 const struct ether_header
* eh_p
,
964 u_int16_t vlan_tag
, bpf_packet_func func
)
966 struct ether_vlan_header
* vlh_p
;
969 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
973 /* populate a new mbuf containing the vlan ethernet header */
974 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
975 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
976 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
977 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
978 vlh_p
->evl_tag
= htons(vlan_tag
);
979 vlh_p
->evl_proto
= eh_p
->ether_type
;
987 static __inline__
void
988 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
989 bpf_packet_func func
)
992 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
993 const struct ether_header
* eh_p
;
994 eh_p
= mtod(m
, const struct ether_header
*);
995 m
->m_data
+= ETHER_HDR_LEN
;
996 m
->m_len
-= ETHER_HDR_LEN
;
997 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
998 m
->m_data
-= ETHER_HDR_LEN
;
999 m
->m_len
+= ETHER_HDR_LEN
;
1007 static __inline__
void
1008 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
1009 bpf_packet_func func
)
1012 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1013 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1015 /* restore the header */
1016 m
->m_data
-= ETHER_HDR_LEN
;
1017 m
->m_len
+= ETHER_HDR_LEN
;
1019 m
->m_data
+= ETHER_HDR_LEN
;
1020 m
->m_len
-= ETHER_HDR_LEN
;
1027 * Function: bond_setmulti
1029 * Enable multicast reception on "our" interface by enabling multicasts on
1030 * each of the member ports.
1033 bond_setmulti(struct ifnet
* ifp
)
1041 ifb
= ifnet_softc(ifp
);
1042 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1043 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1048 ifbond_wait(ifb
, "bond_setmulti");
1050 if (ifbond_flags_if_detaching(ifb
)) {
1051 /* someone destroyed the bond while we were waiting */
1057 /* ifbond_wait() let's us safely walk the list without holding the lock */
1058 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1059 struct ifnet
* port_ifp
= p
->po_ifp
;
1061 error
= multicast_list_program(&p
->po_multicast
,
1064 printf("bond_setmulti(%s): "
1065 "multicast_list_program(%s%d) failed, %d\n",
1066 ifb
->ifb_name
, ifnet_name(port_ifp
),
1067 ifnet_unit(port_ifp
), error
);
1073 ifbond_signal(ifb
, "bond_setmulti");
1075 ifbond_release(ifb
);
1080 bond_clone_attach(void)
1084 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1091 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1094 struct ifmultiaddr
* ifma
= NULL
;
1095 struct sockaddr_dl sdl
;
1097 bond_assert_lock_not_held();
1099 bzero(&sdl
, sizeof(sdl
));
1100 sdl
.sdl_len
= sizeof(sdl
);
1101 sdl
.sdl_family
= AF_LINK
;
1102 sdl
.sdl_type
= IFT_ETHER
;
1104 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1105 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1106 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1108 ifb
->ifb_ifma_slow_proto
= ifma
;
1114 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1119 struct ifnet_init_params bond_init
;
1121 error
= bond_globals_init();
1126 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
);
1130 bzero(ifb
, sizeof(*ifb
));
1133 TAILQ_INIT(&ifb
->ifb_port_list
);
1134 TAILQ_INIT(&ifb
->ifb_lag_list
);
1135 ifb
->ifb_key
= unit
+ 1;
1137 /* use the interface name as the unique id for ifp recycle */
1138 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1139 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1140 ifbond_release(ifb
);
1144 bzero(&bond_init
, sizeof(bond_init
));
1145 bond_init
.uniqueid
= ifb
->ifb_name
;
1146 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1147 bond_init
.name
= ifc
->ifc_name
;
1148 bond_init
.unit
= unit
;
1149 bond_init
.family
= IFNET_FAMILY_BOND
;
1150 bond_init
.type
= IFT_IEEE8023ADLAG
;
1151 bond_init
.output
= bond_output
;
1152 bond_init
.demux
= ether_demux
;
1153 bond_init
.add_proto
= ether_add_proto
;
1154 bond_init
.del_proto
= ether_del_proto
;
1155 bond_init
.check_multi
= ether_check_multi
;
1156 bond_init
.framer
= ether_frameout
;
1157 bond_init
.ioctl
= bond_ioctl
;
1158 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1159 bond_init
.detach
= bond_if_free
;
1160 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1161 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1162 bond_init
.softc
= ifb
;
1163 error
= ifnet_allocate(&bond_init
, &ifp
);
1166 ifbond_release(ifb
);
1171 ifnet_set_offload(ifp
, 0);
1172 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1173 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1174 ifnet_set_baudrate(ifp
, 0);
1175 ifnet_set_mtu(ifp
, 0);
1177 error
= ifnet_attach(ifp
, NULL
);
1180 ifbond_release(ifb
);
1183 error
= ifbond_add_slow_proto_multicast(ifb
);
1185 printf("bond_clone_create(%s): "
1186 "failed to add slow_proto multicast, %d\n",
1187 ifb
->ifb_name
, error
);
1190 /* attach as ethernet */
1191 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1194 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1201 bond_remove_all_interfaces(ifbond_ref ifb
)
1205 bond_assert_lock_held();
1208 * do this in reverse order to avoid re-programming the mac address
1209 * as each head interface is removed
1211 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1212 bond_remove_interface(ifb
, p
->po_ifp
);
1218 bond_remove(ifbond_ref ifb
)
1220 bond_assert_lock_held();
1221 ifbond_flags_set_if_detaching(ifb
);
1222 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1223 bond_remove_all_interfaces(ifb
);
1228 bond_if_detach(struct ifnet
* ifp
)
1232 error
= ifnet_detach(ifp
);
1234 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1235 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1242 bond_clone_destroy(struct ifnet
* ifp
)
1247 ifb
= ifnet_softc(ifp
);
1248 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1252 if (ifbond_flags_if_detaching(ifb
)) {
1258 bond_if_detach(ifp
);
1263 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1268 ifb
= ifnet_softc(ifp
);
1269 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1274 case BPF_TAP_DISABLE
:
1275 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1279 ifb
->ifb_bpf_input
= func
;
1282 case BPF_TAP_OUTPUT
:
1283 ifb
->ifb_bpf_output
= func
;
1286 case BPF_TAP_INPUT_OUTPUT
:
1287 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1297 ether_header_hash(struct ether_header
* eh_p
)
1301 /* get 32-bits from destination ether and ether type */
1302 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1304 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1308 static struct mbuf
*
1309 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1314 while (*offset
>= len
) {
1325 #if BYTE_ORDER == BIG_ENDIAN
1326 static __inline__
uint32_t
1327 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1329 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1330 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1332 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1333 static __inline__
uint32_t
1334 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1336 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1337 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1339 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1342 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1344 struct mbuf
* current
;
1345 u_char
* current_data
;
1350 current
= S_mbuf_skip_to_offset(m
, &offset
);
1351 if (current
== NULL
) {
1354 current_data
= mtod(current
, u_char
*) + offset
;
1355 space_current
= current
->m_len
- offset
;
1356 if (space_current
>= (int)sizeof(uint32_t)) {
1357 *val
= *((uint32_t *)current_data
);
1360 next
= current
->m_next
;
1361 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1364 next_data
= mtod(next
, u_char
*);
1365 switch (space_current
) {
1367 *val
= make_uint32(current_data
[0], next_data
[0],
1368 next_data
[1], next_data
[2]);
1371 *val
= make_uint32(current_data
[0], current_data
[1],
1372 next_data
[0], next_data
[1]);
1375 *val
= make_uint32(current_data
[0], current_data
[1],
1376 current_data
[2], next_data
[0]);
1382 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1383 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1386 ip_header_hash(struct mbuf
* m
)
1389 struct in_addr ip_dst
;
1390 struct in_addr ip_src
;
1393 struct mbuf
* orig_m
= m
;
1395 /* find the IP protocol field relative to the start of the packet */
1396 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1397 m
= S_mbuf_skip_to_offset(m
, &offset
);
1398 if (m
== NULL
|| m
->m_len
< 1) {
1401 data
= mtod(m
, u_char
*) + offset
;
1404 /* find the IP src relative to the IP protocol */
1405 if ((m
->m_len
- offset
)
1406 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1407 /* this should be the normal case */
1408 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1409 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1412 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1413 (uint32_t *)&ip_src
.s_addr
)) {
1416 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1417 (uint32_t *)&ip_dst
.s_addr
)) {
1421 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1424 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1427 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1429 ipv6_header_hash(struct mbuf
* m
)
1434 struct mbuf
* orig_m
= m
;
1438 /* find the IP protocol field relative to the start of the packet */
1439 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1440 m
= S_mbuf_skip_to_offset(m
, &offset
);
1442 goto bad_ipv6_packet
;
1444 data
= mtod(m
, u_char
*) + offset
;
1446 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1447 /* this should be the normal case */
1448 for (i
= 0, scan
= (uint32_t *)data
;
1449 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1455 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1457 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1458 (uint32_t *)&tmp
)) {
1459 goto bad_ipv6_packet
;
1464 return (ntohl(val
));
1467 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1471 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1473 bpf_packet_func bpf_func
;
1476 struct ifnet
* port_ifp
= NULL
;
1478 struct flowadv adv
= { FADV_SUCCESS
};
1483 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1487 if (m
->m_pkthdr
.socket_id
!= 0) {
1488 h
= m
->m_pkthdr
.socket_id
;
1491 struct ether_header
* eh_p
;
1493 eh_p
= mtod(m
, struct ether_header
*);
1494 switch (ntohs(eh_p
->ether_type
)) {
1496 h
= ip_header_hash(m
);
1498 case ETHERTYPE_IPV6
:
1499 h
= ipv6_header_hash(m
);
1502 h
= ether_header_hash(eh_p
);
1507 ifb
= ifnet_softc(ifp
);
1508 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1509 || ifb
->ifb_distributing_count
== 0) {
1512 h
%= ifb
->ifb_distributing_count
;
1513 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1514 bpf_func
= ifb
->ifb_bpf_output
;
1517 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1518 (void)ifnet_stat_increment_out(ifp
, 1,
1519 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1522 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1524 bond_bpf_output(ifp
, m
, bpf_func
);
1526 err
= dlil_output(port_ifp
, PF_BOND
, m
, NULL
, NULL
, 1, &adv
);
1529 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1531 } else if (adv
.code
== FADV_SUSPENDED
) {
1545 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1548 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1549 if (p
->po_ifp
== port_ifp
) {
1557 bond_lookup_port(struct ifnet
* port_ifp
)
1562 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1563 port
= ifbond_lookup_port(ifb
, port_ifp
);
1572 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1574 struct ifnet
* bond_ifp
= NULL
;
1580 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1583 p
= bond_lookup_port(port_ifp
);
1587 if (p
->po_enabled
== 0) {
1591 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1594 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1595 if (ifbond_selection(ifb
)) {
1596 event_code
= (ifb
->ifb_active_lag
== NULL
)
1599 /* XXX need to take a reference on bond_ifp */
1600 bond_ifp
= ifb
->ifb_ifp
;
1601 ifb
->ifb_last_link_event
= event_code
;
1604 event_code
= (ifb
->ifb_active_lag
== NULL
)
1607 if (event_code
!= ifb
->ifb_last_link_event
) {
1608 if (g_bond
->verbose
) {
1609 timestamp_printf("%s: (receive) generating LINK event\n",
1612 bond_ifp
= ifb
->ifb_ifp
;
1613 ifb
->ifb_last_link_event
= event_code
;
1619 if (bond_ifp
!= NULL
) {
1620 interface_link_event(bond_ifp
, event_code
);
1627 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1629 la_marker_pdu_ref marker_p
;
1632 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1633 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1637 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1641 p
= bond_lookup_port(port_ifp
);
1642 if (p
== NULL
|| p
->po_enabled
== 0
1643 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1647 /* echo back the same packet as a marker response */
1648 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1649 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1659 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1660 char * frame_header
)
1662 bpf_packet_func bpf_func
;
1663 const struct ether_header
* eh_p
;
1668 eh_p
= (const struct ether_header
*)frame_header
;
1669 if ((m
->m_flags
& M_MCAST
) != 0
1670 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1671 sizeof(eh_p
->ether_dhost
)) == 0
1672 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1673 u_char subtype
= *mtod(m
, u_char
*);
1675 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1676 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1681 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1682 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1687 bond_receive_lacpdu(m
, port_ifp
);
1690 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1693 /* restore the ethernet header pointer in the mbuf */
1694 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1695 m
->m_data
-= ETHER_HDR_LEN
;
1696 m
->m_len
+= ETHER_HDR_LEN
;
1697 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1698 if (m
->m_pkthdr
.len
< min_size
) {
1703 if (m
->m_len
< min_size
) {
1704 m
= m_pullup(m
, min_size
);
1709 /* send to marker responder */
1710 bond_receive_la_marker_pdu(m
, port_ifp
);
1713 else if (subtype
== 0
1714 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1715 /* invalid subtype, discard the frame */
1721 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1724 p
= bond_lookup_port(port_ifp
);
1725 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1729 /* make the packet appear as if it arrived on the bonded interface */
1732 bpf_func
= ifb
->ifb_bpf_input
;
1735 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1736 (void)ifnet_stat_increment_in(ifp
, 1,
1737 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1738 + ETHER_VLAN_ENCAP_LEN
), 0);
1741 (void)ifnet_stat_increment_in(ifp
, 1,
1742 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1744 m
->m_pkthdr
.rcvif
= ifp
;
1745 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1746 m
->m_pkthdr
.header
= frame_header
;
1747 dlil_input_packet_list(ifp
, m
);
1756 static __inline__
const char *
1757 bondport_get_name(bondport_ref p
)
1759 return (p
->po_name
);
1762 static __inline__
int
1763 bondport_get_index(bondport_ref p
)
1765 return (ifnet_index(p
->po_ifp
));
1769 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1771 struct ether_header
* eh_p
;
1774 /* packet_buffer_allocate leaves room for ethernet header */
1775 eh_p
= mtod(buf
, struct ether_header
*);
1776 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1777 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1778 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1779 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1781 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1782 bondport_get_name(p
), error
);
1788 bondport_timer_process_func(devtimer_ref timer
,
1789 devtimer_process_func_event event
)
1794 case devtimer_process_func_event_lock
:
1796 devtimer_retain(timer
);
1798 case devtimer_process_func_event_unlock
:
1799 if (devtimer_valid(timer
)) {
1800 /* as long as the devtimer is valid, we can look at arg0 */
1802 struct ifnet
* bond_ifp
= NULL
;
1804 p
= (bondport_ref
)devtimer_arg0(timer
);
1805 if (ifbond_selection(p
->po_bond
)) {
1806 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1809 /* XXX need to take a reference on bond_ifp */
1810 bond_ifp
= p
->po_bond
->ifb_ifp
;
1811 p
->po_bond
->ifb_last_link_event
= event_code
;
1814 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1817 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1818 if (g_bond
->verbose
) {
1819 timestamp_printf("%s: (timer) generating LINK event\n",
1820 p
->po_bond
->ifb_name
);
1822 bond_ifp
= p
->po_bond
->ifb_ifp
;
1823 p
->po_bond
->ifb_last_link_event
= event_code
;
1826 devtimer_release(timer
);
1828 if (bond_ifp
!= NULL
) {
1829 interface_link_event(bond_ifp
, event_code
);
1833 /* timer is going away */
1834 devtimer_release(timer
);
1844 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1845 int active
, int short_timeout
, int * ret_error
)
1848 bondport_ref p
= NULL
;
1849 lacp_actor_partner_state s
;
1852 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
);
1854 *ret_error
= ENOMEM
;
1857 bzero(p
, sizeof(*p
));
1858 multicast_list_init(&p
->po_multicast
);
1859 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1860 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1861 >= sizeof(p
->po_name
)) {
1862 printf("if_bond: name too large\n");
1863 *ret_error
= EINVAL
;
1866 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1868 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1869 bondport_get_name(p
), error
);
1872 /* remember the current interface MTU so it can be restored */
1873 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1874 p
->po_ifp
= port_ifp
;
1875 p
->po_media_info
= interface_media_info(port_ifp
);
1876 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1877 if (p
->po_current_while_timer
== NULL
) {
1878 *ret_error
= ENOMEM
;
1881 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1882 if (p
->po_periodic_timer
== NULL
) {
1883 *ret_error
= ENOMEM
;
1886 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1887 if (p
->po_wait_while_timer
== NULL
) {
1888 *ret_error
= ENOMEM
;
1891 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1892 if (p
->po_transmit_timer
== NULL
) {
1893 *ret_error
= ENOMEM
;
1896 p
->po_receive_state
= ReceiveState_none
;
1897 p
->po_mux_state
= MuxState_none
;
1898 p
->po_priority
= priority
;
1900 s
= lacp_actor_partner_state_set_aggregatable(s
);
1901 if (short_timeout
) {
1902 s
= lacp_actor_partner_state_set_short_timeout(s
);
1905 s
= lacp_actor_partner_state_set_active_lacp(s
);
1907 p
->po_actor_state
= s
;
1916 bondport_start(bondport_ref p
)
1918 bondport_receive_machine(p
, LAEventStart
, NULL
);
1919 bondport_mux_machine(p
, LAEventStart
, NULL
);
1920 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1921 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1926 * Function: bondport_invalidate_timers
1928 * Invalidate all of the timers for the bondport.
1931 bondport_invalidate_timers(bondport_ref p
)
1933 devtimer_invalidate(p
->po_current_while_timer
);
1934 devtimer_invalidate(p
->po_periodic_timer
);
1935 devtimer_invalidate(p
->po_wait_while_timer
);
1936 devtimer_invalidate(p
->po_transmit_timer
);
1940 * Function: bondport_cancel_timers
1942 * Cancel all of the timers for the bondport.
1945 bondport_cancel_timers(bondport_ref p
)
1947 devtimer_cancel(p
->po_current_while_timer
);
1948 devtimer_cancel(p
->po_periodic_timer
);
1949 devtimer_cancel(p
->po_wait_while_timer
);
1950 devtimer_cancel(p
->po_transmit_timer
);
1954 bondport_free(bondport_ref p
)
1956 multicast_list_remove(&p
->po_multicast
);
1957 devtimer_release(p
->po_current_while_timer
);
1958 devtimer_release(p
->po_periodic_timer
);
1959 devtimer_release(p
->po_wait_while_timer
);
1960 devtimer_release(p
->po_transmit_timer
);
1965 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1966 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1967 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1968 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1970 static __inline__
int
1971 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1973 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1974 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1978 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1985 bondport_ref
* new_array
= NULL
;
1986 bondport_ref
* old_array
= NULL
;
1990 /* pre-allocate space for new port */
1991 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1996 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1997 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2000 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
2003 /* make sure this interface can handle our current MTU */
2004 devmtu
= bond_device_mtu(ifp
, ifb
);
2006 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
2008 printf("if_bond: interface %s doesn't support mtu %d",
2009 bondport_get_name(p
), devmtu
);
2014 /* make sure ifb doesn't get de-allocated while we wait */
2017 /* wait for other add or remove to complete */
2018 ifbond_wait(ifb
, "bond_add_interface");
2020 if (ifbond_flags_if_detaching(ifb
)) {
2021 /* someone destroyed the bond while we were waiting */
2025 if (bond_lookup_port(port_ifp
) != NULL
) {
2026 /* port is already part of a bond */
2030 ifnet_lock_exclusive(port_ifp
);
2031 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2032 /* interface already has VLAN's, or is part of bond */
2033 ifnet_lock_done(port_ifp
);
2038 /* mark the interface busy */
2039 /* can't use ifnet_set_eflags because that takes the lock */
2040 port_ifp
->if_eflags
|= IFEF_BOND
;
2041 ifnet_lock_done(port_ifp
);
2043 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2044 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2045 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2046 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2050 ifnet_offload_t ifp_offload
;
2051 ifnet_offload_t port_ifp_offload
;
2053 ifp_offload
= ifnet_offload(ifp
);
2054 port_ifp_offload
= ifnet_offload(port_ifp
);
2055 if (ifp_offload
!= port_ifp_offload
) {
2056 ifnet_offload_t offload
;
2058 offload
= ifp_offload
& port_ifp_offload
;
2059 printf("bond_add_interface(%s, %s) "
2060 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2061 ifb
->ifb_name
, bondport_get_name(p
),
2062 ifp_offload
, port_ifp_offload
, offload
);
2065 * if the bond has VLAN's, we can't simply change the hwassist
2066 * field behind its back: this needs work
2068 ifnet_set_offload(ifp
, offload
);
2073 /* remember the port's ethernet address so it can be restored */
2074 ether_addr_copy(&p
->po_saved_addr
, ifnet_lladdr(port_ifp
));
2076 /* add it to the list of ports */
2077 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2078 ifb
->ifb_port_count
++;
2080 /* set the default MTU */
2081 if (ifnet_mtu(ifp
) == 0) {
2082 ifnet_set_mtu(ifp
, ETHERMTU
);
2087 /* first port added to bond determines bond's ethernet address */
2089 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(port_ifp
), ETHER_ADDR_LEN
,
2093 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2095 /* allocate a larger distributing array */
2096 new_array
= (bondport_ref
*)
2097 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2098 if (new_array
== NULL
) {
2103 /* attach our BOND "protocol" to the interface */
2104 error
= bond_attach_protocol(port_ifp
);
2108 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2110 /* set the interface MTU */
2111 devmtu
= bond_device_mtu(ifp
, ifb
);
2112 error
= siocsifmtu(port_ifp
, devmtu
);
2114 printf("bond_add_interface(%s, %s):"
2115 " SIOCSIFMTU %d failed %d\n",
2116 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2119 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2121 /* program the port with our multicast addresses */
2122 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2124 printf("bond_add_interface(%s, %s):"
2125 " multicast_list_program failed %d\n",
2126 ifb
->ifb_name
, bondport_get_name(p
), error
);
2130 /* mark the interface up */
2131 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2133 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2135 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2136 ifb
->ifb_name
, bondport_get_name(p
), error
);
2140 /* re-program the port's ethernet address */
2141 error
= if_siflladdr(port_ifp
,
2142 (const struct ether_addr
*)ifnet_lladdr(ifp
));
2144 /* port doesn't support setting the link address */
2145 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2146 ifb
->ifb_name
, bondport_get_name(p
), error
);
2149 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2153 /* no failures past this point */
2156 /* copy the contents of the existing distributing array */
2157 if (ifb
->ifb_distributing_count
) {
2158 bcopy(ifb
->ifb_distributing_array
, new_array
,
2159 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2161 old_array
= ifb
->ifb_distributing_array
;
2162 ifb
->ifb_distributing_array
= new_array
;
2164 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2167 /* check if we need to generate a link status event */
2168 if (ifbond_selection(ifb
)) {
2169 event_code
= (ifb
->ifb_active_lag
== NULL
)
2172 ifb
->ifb_last_link_event
= event_code
;
2176 /* are we adding the first distributing interface? */
2177 if (media_active(&p
->po_media_info
)) {
2178 if (ifb
->ifb_distributing_count
== 0) {
2179 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2181 bondport_enable_distributing(p
);
2184 bondport_disable_distributing(p
);
2187 /* clear the busy state, and wakeup anyone waiting */
2188 ifbond_signal(ifb
, "bond_add_interface");
2190 if (event_code
!= 0) {
2191 interface_link_event(ifp
, event_code
);
2193 if (old_array
!= NULL
) {
2194 FREE(old_array
, M_BOND
);
2199 bond_assert_lock_not_held();
2201 /* if this was the first port to be added, clear our address */
2203 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2206 if (new_array
!= NULL
) {
2207 FREE(new_array
, M_BOND
);
2209 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2212 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2214 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2215 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2218 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2219 (void)bond_detach_protocol(port_ifp
);
2221 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2224 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2226 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2227 ifb
->ifb_name
, bondport_get_name(p
),
2228 p
->po_devmtu
.ifdm_current
, error1
);
2232 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2233 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2234 ifb
->ifb_port_count
--;
2236 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2237 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2238 ifb
->ifb_altmtu
= 0;
2239 ifnet_set_mtu(ifp
, 0);
2240 ifnet_set_offload(ifp
, 0);
2244 ifbond_signal(ifb
, "bond_add_interface");
2246 ifbond_release(ifb
);
2252 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2257 bondport_ref head_port
;
2260 int new_link_address
= FALSE
;
2262 lacp_actor_partner_state s
;
2263 int was_distributing
;
2265 bond_assert_lock_held();
2268 ifbond_wait(ifb
, "bond_remove_interface");
2270 p
= ifbond_lookup_port(ifb
, port_ifp
);
2273 /* it got removed by another thread */
2277 /* de-select it and remove it from the lists */
2278 was_distributing
= bondport_flags_distributing(p
);
2279 bondport_disable_distributing(p
);
2280 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2281 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2282 active_lag
= bondport_remove_from_LAG(p
);
2283 /* invalidate timers here while holding the bond_lock */
2284 bondport_invalidate_timers(p
);
2286 /* announce that we're Individual now */
2287 s
= p
->po_actor_state
;
2288 s
= lacp_actor_partner_state_set_individual(s
);
2289 s
= lacp_actor_partner_state_set_not_collecting(s
);
2290 s
= lacp_actor_partner_state_set_not_distributing(s
);
2291 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2292 p
->po_actor_state
= s
;
2293 bondport_flags_set_ntt(p
);
2296 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2297 ifb
->ifb_port_count
--;
2300 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2301 if (head_port
== NULL
) {
2302 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2303 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2306 ifnet_set_offload(ifp
, 0);
2307 ifnet_set_mtu(ifp
, 0);
2308 ifb
->ifb_altmtu
= 0;
2309 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2310 && bcmp(&p
->po_saved_addr
, ifnet_lladdr(ifp
),
2311 ETHER_ADDR_LEN
) == 0) {
2312 new_link_address
= TRUE
;
2314 /* check if we need to generate a link status event */
2315 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2316 if (ifbond_selection(ifb
) || active_lag
) {
2317 event_code
= (ifb
->ifb_active_lag
== NULL
)
2320 ifb
->ifb_last_link_event
= event_code
;
2322 bondport_transmit_machine(p
, LAEventStart
,
2323 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2326 /* are we removing the last distributing interface? */
2327 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2328 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2335 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2337 else if (new_link_address
) {
2338 struct ifnet
* scan_ifp
;
2339 bondport_ref scan_port
;
2341 /* ifbond_wait() allows port list traversal without holding the lock */
2343 /* this port gave the bond its ethernet address, switch to new one */
2344 ifnet_set_lladdr_and_type(ifp
,
2345 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2348 /* re-program each port with the new link address */
2349 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2350 scan_ifp
= scan_port
->po_ifp
;
2352 error
= if_siflladdr(scan_ifp
,
2353 (const struct ether_addr
*) ifnet_lladdr(ifp
));
2355 printf("bond_remove_interface(%s, %s): "
2356 "if_siflladdr (%s) failed %d\n",
2357 ifb
->ifb_name
, bondport_get_name(p
),
2358 bondport_get_name(scan_port
), error
);
2363 /* restore the port's ethernet address */
2364 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2366 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2367 ifb
->ifb_name
, bondport_get_name(p
), error
);
2370 /* restore the port's MTU */
2371 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2373 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2374 ifb
->ifb_name
, bondport_get_name(p
),
2375 p
->po_devmtu
.ifdm_current
, error
);
2378 /* remove the bond "protocol" */
2379 bond_detach_protocol(port_ifp
);
2381 /* generate link event */
2382 if (event_code
!= 0) {
2383 interface_link_event(ifp
, event_code
);
2388 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2389 /* release this bondport's reference to the ifbond */
2390 ifbond_release(ifb
);
2393 ifbond_signal(ifb
, "bond_remove_interface");
2394 ifbond_release(ifb
);
2399 bond_set_lacp_mode(ifbond_ref ifb
)
2403 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2404 bondport_disable_distributing(p
);
2411 bond_set_static_mode(ifbond_ref ifb
)
2414 lacp_actor_partner_state s
;
2416 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2417 bondport_disable_distributing(p
);
2418 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2419 (void)bondport_remove_from_LAG(p
);
2420 bondport_cancel_timers(p
);
2422 /* announce that we're Individual now */
2423 s
= p
->po_actor_state
;
2424 s
= lacp_actor_partner_state_set_individual(s
);
2425 s
= lacp_actor_partner_state_set_not_collecting(s
);
2426 s
= lacp_actor_partner_state_set_not_distributing(s
);
2427 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2428 p
->po_actor_state
= s
;
2429 bondport_flags_set_ntt(p
);
2430 bondport_transmit_machine(p
, LAEventStart
,
2431 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2433 p
->po_actor_state
= 0;
2434 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2436 if (media_active(&p
->po_media_info
)) {
2437 bondport_enable_distributing(p
);
2440 bondport_disable_distributing(p
);
2447 bond_set_mode(struct ifnet
* ifp
, int mode
)
2454 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2455 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2457 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2459 if (ifb
->ifb_mode
== mode
) {
2465 ifbond_wait(ifb
, "bond_set_mode");
2467 /* verify (again) that the mode is actually different */
2468 if (ifb
->ifb_mode
== mode
) {
2473 ifb
->ifb_mode
= mode
;
2474 if (mode
== IF_BOND_MODE_LACP
) {
2475 bond_set_lacp_mode(ifb
);
2477 /* check if we need to generate a link status event */
2478 if (ifbond_selection(ifb
)) {
2479 event_code
= (ifb
->ifb_active_lag
== NULL
)
2484 bond_set_static_mode(ifb
);
2485 event_code
= (ifb
->ifb_distributing_count
== 0)
2489 ifb
->ifb_last_link_event
= event_code
;
2492 ifbond_signal(ifb
, "bond_set_mode");
2494 ifbond_release(ifb
);
2496 if (event_code
!= 0) {
2497 interface_link_event(ifp
, event_code
);
2503 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2508 struct if_bond_status_req
* ibsr
;
2509 struct if_bond_status ibs
;
2512 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2513 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2516 ibsr
->ibsr_key
= ifb
->ifb_key
;
2517 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2518 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2519 dst
= proc_is64bit(current_proc())
2520 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2521 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2522 if (dst
== USER_ADDR_NULL
) {
2523 /* just want to know how many there are */
2526 if (ibsr
->ibsr_count
< 0) {
2529 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2530 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2531 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2532 struct if_bond_partner_state
* ibps_p
;
2533 partner_state_ref ps
;
2538 bzero(&ibs
, sizeof(ibs
));
2539 strncpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2540 ibs
.ibs_port_priority
= port
->po_priority
;
2541 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2542 ibs
.ibs_state
= port
->po_actor_state
;
2543 ibs
.ibs_selected_state
= port
->po_selected
;
2544 ps
= &port
->po_partner_state
;
2545 ibps_p
= &ibs
.ibs_partner_state
;
2546 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2547 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2548 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2549 ibps_p
->ibps_port
= ps
->ps_port
;
2550 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2551 ibps_p
->ibps_state
= ps
->ps_state
;
2554 /* fake the selected information */
2555 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2556 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2558 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2568 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2571 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2577 bond_set_promisc(__unused
struct ifnet
*ifp
)
2584 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2590 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2591 mtu_min
= IF_MINMTU
;
2593 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2594 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2596 if (devmtu_p
->ifdm_min
> mtu_min
) {
2597 mtu_min
= devmtu_p
->ifdm_min
;
2599 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2600 mtu_max
= devmtu_p
->ifdm_max
;
2609 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2614 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2615 error
= siocsifmtu(p
->po_ifp
, mtu
);
2617 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2618 ifb
->ifb_name
, bondport_get_name(p
), error
);
2626 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2636 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2637 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2638 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2642 ifbond_wait(ifb
, "bond_set_mtu");
2645 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2649 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2650 if (mtu
> mtu_max
) {
2654 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2655 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2660 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2663 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2665 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2666 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2667 if (new_max
!= old_max
) {
2668 /* we can safely walk the list of port without the lock held */
2670 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2672 /* try our best to back out of it */
2673 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2679 ifb
->ifb_altmtu
= mtu
;
2682 ifnet_set_mtu(ifp
, mtu
);
2687 ifbond_signal(ifb
, "bond_set_mtu");
2688 ifbond_release(ifb
);
2696 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2699 struct if_bond_req ibr
;
2700 struct ifaddr
* ifa
;
2703 struct ifmediareq
*ifmr
;
2704 struct ifnet
* port_ifp
= NULL
;
2705 user_addr_t user_addr
;
2707 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2708 return (EOPNOTSUPP
);
2710 ifr
= (struct ifreq
*)data
;
2711 ifa
= (struct ifaddr
*)data
;
2715 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2718 case SIOCGIFMEDIA32
:
2719 case SIOCGIFMEDIA64
:
2721 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2722 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2724 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2726 ifmr
= (struct ifmediareq
*)data
;
2727 ifmr
->ifm_current
= IFM_ETHER
;
2729 ifmr
->ifm_status
= IFM_AVALID
;
2730 ifmr
->ifm_active
= IFM_ETHER
;
2731 ifmr
->ifm_count
= 1;
2732 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2733 if (ifb
->ifb_active_lag
!= NULL
) {
2734 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2735 ifmr
->ifm_status
|= IFM_ACTIVE
;
2738 else if (ifb
->ifb_distributing_count
> 0) {
2740 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2741 ifmr
->ifm_status
|= IFM_ACTIVE
;
2744 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2745 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2746 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2747 if (user_addr
!= USER_ADDR_NULL
) {
2748 error
= copyout(&ifmr
->ifm_current
,
2755 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2761 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2762 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2764 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2767 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2768 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2769 &ifr
->ifr_devmtu
.ifdm_max
);
2775 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2776 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2778 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2781 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2786 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2790 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2794 user_addr
= proc_is64bit(current_proc())
2795 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2796 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2800 switch (ibr
.ibr_op
) {
2801 case IF_BOND_OP_ADD_INTERFACE
:
2802 case IF_BOND_OP_REMOVE_INTERFACE
:
2803 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2804 if (port_ifp
== NULL
) {
2808 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2809 error
= EPROTONOSUPPORT
;
2813 case IF_BOND_OP_SET_VERBOSE
:
2814 case IF_BOND_OP_SET_MODE
:
2823 switch (ibr
.ibr_op
) {
2824 case IF_BOND_OP_ADD_INTERFACE
:
2825 error
= bond_add_interface(ifp
, port_ifp
);
2827 case IF_BOND_OP_REMOVE_INTERFACE
:
2829 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2830 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2832 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2834 error
= bond_remove_interface(ifb
, port_ifp
);
2837 case IF_BOND_OP_SET_VERBOSE
:
2839 if (g_bond
== NULL
) {
2844 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2847 case IF_BOND_OP_SET_MODE
:
2848 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2849 case IF_BOND_MODE_LACP
:
2850 case IF_BOND_MODE_STATIC
:
2859 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2862 break; /* SIOCSIFBOND */
2865 user_addr
= proc_is64bit(current_proc())
2866 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2867 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2871 switch (ibr
.ibr_op
) {
2872 case IF_BOND_OP_GET_STATUS
:
2882 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2883 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2885 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2887 switch (ibr
.ibr_op
) {
2888 case IF_BOND_OP_GET_STATUS
:
2889 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2893 break; /* SIOCGIFBOND */
2900 /* enable/disable promiscuous mode */
2902 error
= bond_set_promisc(ifp
);
2908 error
= bond_setmulti(ifp
);
2917 bond_if_free(struct ifnet
* ifp
)
2925 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2930 ifbond_release(ifb
);
2937 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
2939 struct ifnet
* bond_ifp
= NULL
;
2941 int old_distributing_count
;
2943 struct media_info media_info
= { 0, 0};
2945 switch (event_code
) {
2946 case KEV_DL_IF_DETACHED
:
2948 case KEV_DL_LINK_OFF
:
2949 case KEV_DL_LINK_ON
:
2950 media_info
= interface_media_info(port_ifp
);
2956 p
= bond_lookup_port(port_ifp
);
2962 old_distributing_count
= ifb
->ifb_distributing_count
;
2963 switch (event_code
) {
2964 case KEV_DL_IF_DETACHED
:
2965 bond_remove_interface(ifb
, p
->po_ifp
);
2967 case KEV_DL_LINK_OFF
:
2968 case KEV_DL_LINK_ON
:
2969 p
->po_media_info
= media_info
;
2970 if (p
->po_enabled
) {
2971 bondport_link_status_changed(p
);
2975 /* generate a link-event */
2976 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2977 if (ifbond_selection(ifb
)) {
2978 event_code
= (ifb
->ifb_active_lag
== NULL
)
2981 /* XXX need to take a reference on bond_ifp */
2982 bond_ifp
= ifb
->ifb_ifp
;
2983 ifb
->ifb_last_link_event
= event_code
;
2986 event_code
= (ifb
->ifb_active_lag
== NULL
)
2989 if (event_code
!= ifb
->ifb_last_link_event
) {
2990 if (g_bond
->verbose
) {
2991 timestamp_printf("%s: (event) generating LINK event\n",
2994 bond_ifp
= ifb
->ifb_ifp
;
2995 ifb
->ifb_last_link_event
= event_code
;
3001 * if the distributing array membership changed from 0 <-> !0
3002 * generate a link event
3004 if (old_distributing_count
== 0
3005 && ifb
->ifb_distributing_count
!= 0) {
3006 event_code
= KEV_DL_LINK_ON
;
3008 else if (old_distributing_count
!= 0
3009 && ifb
->ifb_distributing_count
== 0) {
3010 event_code
= KEV_DL_LINK_OFF
;
3012 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
3013 bond_ifp
= ifb
->ifb_ifp
;
3014 ifb
->ifb_last_link_event
= event_code
;
3019 if (bond_ifp
!= NULL
) {
3020 interface_link_event(bond_ifp
, event_code
);
3026 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
3027 const struct kev_msg
* event
)
3031 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3032 || event
->kev_class
!= KEV_NETWORK_CLASS
3033 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3036 event_code
= event
->event_code
;
3037 switch (event_code
) {
3038 case KEV_DL_LINK_OFF
:
3039 case KEV_DL_LINK_ON
:
3040 /* we only care about link status changes */
3041 bond_handle_event(port_ifp
, event_code
);
3050 bond_detached(ifnet_t port_ifp
, __unused protocol_family_t protocol
)
3052 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3057 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3060 struct kern_event_msg header
;
3062 char if_name
[IFNAMSIZ
];
3065 bzero(&event
, sizeof(event
));
3066 event
.header
.total_size
= sizeof(event
);
3067 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3068 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3069 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3070 event
.header
.event_code
= event_code
;
3071 event
.header
.event_data
[0] = ifnet_family(ifp
);
3072 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3073 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3074 ifnet_event(ifp
, &event
.header
);
3079 * Function: bond_attach_protocol
3081 * Attach a DLIL protocol to the interface.
3083 * The ethernet demux special cases to always return PF_BOND if the
3084 * interface is bonded. That means we receive all traffic from that
3085 * interface without passing any of the traffic to any other attached
3089 bond_attach_protocol(struct ifnet
*ifp
)
3092 struct ifnet_attach_proto_param reg
;
3094 bzero(®
, sizeof(reg
));
3095 reg
.input
= bond_input
;
3096 reg
.event
= bond_event
;
3097 reg
.detached
= bond_detached
;
3099 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3101 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3102 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3108 * Function: bond_detach_protocol
3110 * Detach our DLIL protocol from an interface
3113 bond_detach_protocol(struct ifnet
*ifp
)
3117 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3119 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3120 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3126 * DLIL interface family functions
3128 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3129 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3130 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3131 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3132 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3133 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3135 __private_extern__
int
3136 bond_family_init(void)
3140 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3144 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3149 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3151 ether_detach_inet6
);
3153 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3159 error
= proto_register_plumber(PF_APPLETALK
, APPLE_IF_FAM_BOND
,
3163 printf("bond: proto_register_plumber failed for AppleTalk error=%d\n",
3168 error
= bond_clone_attach();
3170 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3185 ** LACP ifbond_list routines
3188 ifbond_list_find_moved_port(bondport_ref rx_port
,
3189 const lacp_actor_partner_tlv_ref atlv
)
3193 partner_state_ref ps
;
3196 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3197 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3200 /* no point in comparing against ourselves */
3203 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3204 /* it's not clear that we should be checking this */
3207 ps
= &p
->po_partner_state
;
3208 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3211 ps_li
= &ps
->ps_lag_info
;
3212 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3213 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3214 sizeof(ps_li
->li_system
)) == 0) {
3215 if (g_bond
->verbose
) {
3216 timestamp_printf("System " EA_FORMAT
3217 " Port 0x%x moved from %s to %s\n",
3218 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3219 bondport_get_name(p
),
3220 bondport_get_name(rx_port
));
3230 ** LACP ifbond, LAG routines
3234 ifbond_selection(ifbond_ref bond
)
3236 int all_ports_ready
= 0;
3237 int active_media
= 0;
3239 int lag_changed
= 0;
3243 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3244 if (lag
!= bond
->ifb_active_lag
) {
3245 if (bond
->ifb_active_lag
!= NULL
) {
3246 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3247 bond
->ifb_active_lag
= NULL
;
3249 bond
->ifb_active_lag
= lag
;
3251 ifbond_activate_LAG(bond
, lag
, active_media
);
3255 else if (lag
!= NULL
) {
3256 if (lag
->lag_active_media
!= active_media
) {
3257 if (g_bond
->verbose
) {
3258 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3259 link_speed(lag
->lag_active_media
),
3260 link_speed(active_media
));
3262 ifbond_deactivate_LAG(bond
, lag
);
3263 ifbond_activate_LAG(bond
, lag
, active_media
);
3268 port_speed
= link_speed(active_media
);
3269 all_ports_ready
= ifbond_all_ports_ready(bond
);
3271 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3272 if (lag
!= NULL
&& p
->po_lag
== lag
3273 && media_speed(&p
->po_media_info
) == port_speed
3274 && (p
->po_mux_state
== MuxState_DETACHED
3275 || p
->po_selected
== SelectedState_SELECTED
3276 || p
->po_selected
== SelectedState_STANDBY
)
3277 && bondport_aggregatable(p
)) {
3278 if (bond
->ifb_max_active
> 0) {
3279 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3280 if (p
->po_selected
== SelectedState_STANDBY
3281 || p
->po_selected
== SelectedState_UNSELECTED
) {
3282 bondport_set_selected(p
, SelectedState_SELECTED
);
3285 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3286 bondport_set_selected(p
, SelectedState_STANDBY
);
3290 bondport_set_selected(p
, SelectedState_SELECTED
);
3293 if (bondport_flags_selected_changed(p
)) {
3294 bondport_flags_clear_selected_changed(p
);
3295 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3298 && bondport_flags_ready(p
)
3299 && p
->po_mux_state
== MuxState_WAITING
) {
3300 bondport_mux_machine(p
, LAEventReady
, NULL
);
3302 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3304 return (lag_changed
);
3308 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3310 int best_active
= 0;
3311 LAG_ref best_lag
= NULL
;
3316 if (bond
->ifb_active_lag
!= NULL
) {
3317 best_lag
= bond
->ifb_active_lag
;
3318 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3319 if (bond
->ifb_max_active
> 0
3320 && best_count
> bond
->ifb_max_active
) {
3321 best_count
= bond
->ifb_max_active
;
3323 best_speed
= link_speed(best_active
);
3325 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3330 if (lag
== bond
->ifb_active_lag
) {
3331 /* we've already computed it */
3334 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3338 if (bond
->ifb_max_active
> 0
3339 && count
> bond
->ifb_max_active
) {
3340 /* if there's a limit, don't count extra links */
3341 count
= bond
->ifb_max_active
;
3343 speed
= link_speed(active
);
3344 if ((count
* speed
) > (best_count
* best_speed
)) {
3347 best_active
= active
;
3351 if (best_count
== 0) {
3354 *active_media
= best_active
;
3359 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3363 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3364 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3370 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3375 if (bond
->ifb_max_active
> 0) {
3376 need
= bond
->ifb_max_active
;
3378 lag
->lag_active_media
= active_media
;
3379 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3380 if (bondport_aggregatable(p
) == 0) {
3381 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3383 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3384 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3386 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3387 if (bond
->ifb_max_active
> 0) {
3389 bondport_set_selected(p
, SelectedState_SELECTED
);
3393 bondport_set_selected(p
, SelectedState_STANDBY
);
3397 bondport_set_selected(p
, SelectedState_SELECTED
);
3401 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3409 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3411 LAG_ref lag
= bond
->ifb_active_lag
;
3413 bond
->ifb_max_active
= max_active
;
3414 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3417 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3421 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3422 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3423 if (p
->po_selected
== SelectedState_SELECTED
) {
3424 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3426 if (remove_count
== 0) {
3437 ifbond_all_ports_ready(ifbond_ref bond
)
3442 if (bond
->ifb_active_lag
== NULL
) {
3445 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3446 if (p
->po_mux_state
== MuxState_WAITING
3447 && p
->po_selected
== SelectedState_SELECTED
) {
3448 if (bondport_flags_ready(p
) == 0) {
3452 /* note that there was at least one ready port */
3459 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3463 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3464 if (this_port
== p
) {
3467 if (bondport_flags_mux_attached(p
) == 0) {
3475 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3479 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3480 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3481 sizeof(lag
->lag_info
)) == 0) {
3489 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3499 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3500 if (bondport_aggregatable(p
)) {
3503 this_speed
= media_speed(&p
->po_media_info
);
3504 if (this_speed
== 0) {
3507 if (this_speed
> speed
) {
3508 active
= p
->po_media_info
.mi_active
;
3512 else if (this_speed
== speed
) {
3517 *active_media
= active
;
3523 ** LACP bondport routines
3526 bondport_link_status_changed(bondport_ref p
)
3528 ifbond_ref bond
= p
->po_bond
;
3530 if (g_bond
->verbose
) {
3531 if (media_active(&p
->po_media_info
)) {
3532 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3533 bondport_get_name(p
),
3534 media_speed(&p
->po_media_info
),
3535 media_full_duplex(&p
->po_media_info
)
3539 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3542 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3543 if (media_active(&p
->po_media_info
)
3544 && bond
->ifb_active_lag
!= NULL
3545 && p
->po_lag
== bond
->ifb_active_lag
3546 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3547 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3548 if (g_bond
->verbose
) {
3549 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3550 bondport_get_name(p
),
3551 media_speed(&p
->po_media_info
),
3552 link_speed(p
->po_lag
->lag_active_media
));
3554 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3557 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3558 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3559 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3562 if (media_active(&p
->po_media_info
)) {
3563 bondport_enable_distributing(p
);
3566 bondport_disable_distributing(p
);
3573 bondport_aggregatable(bondport_ref p
)
3575 partner_state_ref ps
= &p
->po_partner_state
;
3577 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3578 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3579 /* we and/or our partner are individual */
3582 if (p
->po_lag
== NULL
) {
3585 switch (p
->po_receive_state
) {
3587 if (g_bond
->verbose
) {
3588 timestamp_printf("[%s] Port is not selectable\n",
3589 bondport_get_name(p
));
3592 case ReceiveState_CURRENT
:
3593 case ReceiveState_EXPIRED
:
3600 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3602 LAG_info_ref lag_li
;
3603 partner_state_ref ps
;
3606 ps
= &p
->po_partner_state
;
3607 ps_li
= &ps
->ps_lag_info
;
3608 lag_li
= &lag
->lag_info
;
3609 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3610 && ps_li
->li_key
== lag_li
->li_key
3611 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3612 sizeof(lag_li
->li_system
))
3620 bondport_remove_from_LAG(bondport_ref p
)
3623 ifbond_ref bond
= p
->po_bond
;
3624 LAG_ref lag
= p
->po_lag
;
3629 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3630 if (g_bond
->verbose
) {
3631 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3633 bondport_get_name(p
),
3634 lag
->lag_info
.li_system_priority
,
3635 EA_LIST(&lag
->lag_info
.li_system
),
3636 lag
->lag_info
.li_key
);
3639 lag
->lag_port_count
--;
3640 if (lag
->lag_port_count
> 0) {
3641 return (bond
->ifb_active_lag
== lag
);
3643 if (g_bond
->verbose
) {
3644 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3647 lag
->lag_info
.li_system_priority
,
3648 EA_LIST(&lag
->lag_info
.li_system
),
3649 lag
->lag_info
.li_key
);
3651 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3652 if (bond
->ifb_active_lag
== lag
) {
3653 bond
->ifb_active_lag
= NULL
;
3657 return (active_lag
);
3661 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3663 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3665 lag
->lag_port_count
++;
3666 if (g_bond
->verbose
) {
3667 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3668 bondport_get_name(p
),
3669 lag
->lag_info
.li_system_priority
,
3670 EA_LIST(&lag
->lag_info
.li_system
),
3671 lag
->lag_info
.li_key
);
3677 bondport_assign_to_LAG(bondport_ref p
)
3679 ifbond_ref bond
= p
->po_bond
;
3682 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3683 bondport_remove_from_LAG(p
);
3688 if (bondport_matches_LAG(p
, lag
)) {
3692 bondport_remove_from_LAG(p
);
3694 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3696 bondport_add_to_LAG(p
, lag
);
3699 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3700 TAILQ_INIT(&lag
->lag_port_list
);
3701 lag
->lag_port_count
= 0;
3702 lag
->lag_selected_port_count
= 0;
3703 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3704 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3705 if (g_bond
->verbose
) {
3706 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3709 lag
->lag_info
.li_system_priority
,
3710 EA_LIST(&lag
->lag_info
.li_system
),
3711 lag
->lag_info
.li_key
);
3713 bondport_add_to_LAG(p
, lag
);
3718 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3720 bondport_ref moved_port
;
3723 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3724 &in_lacpdu_p
->la_actor_tlv
);
3725 if (moved_port
!= NULL
) {
3726 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3728 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3729 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3730 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3735 bondport_set_selected(bondport_ref p
, SelectedState s
)
3737 if (s
!= p
->po_selected
) {
3738 ifbond_ref bond
= p
->po_bond
;
3739 LAG_ref lag
= p
->po_lag
;
3741 bondport_flags_set_selected_changed(p
);
3742 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3743 if (p
->po_selected
== SelectedState_SELECTED
) {
3744 lag
->lag_selected_port_count
--;
3746 else if (s
== SelectedState_SELECTED
) {
3747 lag
->lag_selected_port_count
++;
3749 if (g_bond
->verbose
) {
3750 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3751 bondport_get_name(p
),
3752 SelectedStateString(s
),
3753 SelectedStateString(p
->po_selected
));
3766 bondport_UpdateDefaultSelected(bondport_ref p
)
3768 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3773 bondport_RecordDefault(bondport_ref p
)
3775 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3777 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3778 bondport_assign_to_LAG(p
);
3783 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3785 lacp_actor_partner_tlv_ref actor
;
3786 partner_state_ref ps
;
3789 /* compare the PDU's Actor information to our Partner state */
3790 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3791 ps
= &p
->po_partner_state
;
3792 ps_li
= &ps
->ps_lag_info
;
3793 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3794 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3795 != ps
->ps_port_priority
)
3796 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3797 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3798 != ps_li
->li_system_priority
)
3799 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3800 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3801 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3802 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3803 if (g_bond
->verbose
) {
3804 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3805 bondport_get_name(p
));
3812 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3814 lacp_actor_partner_tlv_ref actor
;
3815 ifbond_ref bond
= p
->po_bond
;
3816 int lacp_maintain
= 0;
3817 partner_state_ref ps
;
3818 lacp_actor_partner_tlv_ref partner
;
3821 /* copy the PDU's Actor information into our Partner state */
3822 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3823 ps
= &p
->po_partner_state
;
3824 ps_li
= &ps
->ps_lag_info
;
3825 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3826 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3827 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3828 ps_li
->li_system_priority
3829 = lacp_actor_partner_tlv_get_system_priority(actor
);
3830 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3831 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3833 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3835 /* compare the PDU's Partner information to our own information */
3836 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3838 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3839 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3840 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3841 if (g_bond
->verbose
) {
3842 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3843 bondport_get_name(p
));
3847 if ((lacp_actor_partner_tlv_get_port(partner
)
3848 == bondport_get_index(p
))
3849 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3850 && bcmp(partner
->lap_system
, &g_bond
->system
,
3851 sizeof(g_bond
->system
)) == 0
3852 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3853 == g_bond
->system_priority
)
3854 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3855 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3856 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3857 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3859 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3860 if (g_bond
->verbose
) {
3861 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3862 bondport_get_name(p
));
3865 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3866 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3868 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3869 if (g_bond
->verbose
) {
3870 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3871 bondport_get_name(p
));
3874 bondport_assign_to_LAG(p
);
3878 static __inline__ lacp_actor_partner_state
3879 updateNTTBits(lacp_actor_partner_state s
)
3881 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3882 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3883 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3884 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3888 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3890 ifbond_ref bond
= p
->po_bond
;
3891 lacp_actor_partner_tlv_ref partner
;
3893 /* compare the PDU's Actor information to our Partner state */
3894 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3895 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3896 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3897 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3898 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3899 != g_bond
->system_priority
)
3900 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3901 || (updateNTTBits(partner
->lap_state
)
3902 != updateNTTBits(p
->po_actor_state
))) {
3903 bondport_flags_set_ntt(p
);
3904 if (g_bond
->verbose
) {
3905 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3906 bondport_get_name(p
));
3913 bondport_AttachMuxToAggregator(bondport_ref p
)
3915 if (bondport_flags_mux_attached(p
) == 0) {
3916 if (g_bond
->verbose
) {
3917 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3918 bondport_get_name(p
));
3920 bondport_flags_set_mux_attached(p
);
3926 bondport_DetachMuxFromAggregator(bondport_ref p
)
3928 if (bondport_flags_mux_attached(p
)) {
3929 if (g_bond
->verbose
) {
3930 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3931 bondport_get_name(p
));
3933 bondport_flags_clear_mux_attached(p
);
3939 bondport_enable_distributing(bondport_ref p
)
3941 if (bondport_flags_distributing(p
) == 0) {
3942 ifbond_ref bond
= p
->po_bond
;
3944 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3945 if (g_bond
->verbose
) {
3946 timestamp_printf("[%s] Distribution Enabled\n",
3947 bondport_get_name(p
));
3949 bondport_flags_set_distributing(p
);
3955 bondport_disable_distributing(bondport_ref p
)
3957 if (bondport_flags_distributing(p
)) {
3958 bondport_ref
* array
;
3964 array
= bond
->ifb_distributing_array
;
3965 count
= bond
->ifb_distributing_count
;
3966 for (i
= 0; i
< count
; i
++) {
3967 if (array
[i
] == p
) {
3970 for (j
= i
; j
< (count
- 1); j
++) {
3971 array
[j
] = array
[j
+ 1];
3976 bond
->ifb_distributing_count
--;
3977 if (g_bond
->verbose
) {
3978 timestamp_printf("[%s] Distribution Disabled\n",
3979 bondport_get_name(p
));
3981 bondport_flags_clear_distributing(p
);
3987 ** Receive machine functions
3990 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3993 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3996 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3999 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4002 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4005 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4009 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
4012 switch (p
->po_receive_state
) {
4013 case ReceiveState_none
:
4014 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4016 case ReceiveState_INITIALIZE
:
4017 bondport_receive_machine_initialize(p
, event
, event_data
);
4019 case ReceiveState_PORT_DISABLED
:
4020 bondport_receive_machine_port_disabled(p
, event
, event_data
);
4022 case ReceiveState_EXPIRED
:
4023 bondport_receive_machine_expired(p
, event
, event_data
);
4025 case ReceiveState_LACP_DISABLED
:
4026 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
4028 case ReceiveState_DEFAULTED
:
4029 bondport_receive_machine_defaulted(p
, event
, event_data
);
4031 case ReceiveState_CURRENT
:
4032 bondport_receive_machine_current(p
, event
, event_data
);
4041 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4046 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4047 bondport_receive_machine_current(p
, event
, event_data
);
4050 case LAEventMediaChange
:
4051 if (media_active(&p
->po_media_info
)) {
4052 switch (p
->po_receive_state
) {
4053 case ReceiveState_PORT_DISABLED
:
4054 case ReceiveState_LACP_DISABLED
:
4055 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4062 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4066 bondport_receive_machine_event(p
, event
, event_data
);
4073 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4074 __unused
void * event_data
)
4078 devtimer_cancel(p
->po_current_while_timer
);
4079 if (g_bond
->verbose
) {
4080 timestamp_printf("[%s] Receive INITIALIZE\n",
4081 bondport_get_name(p
));
4083 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4084 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4085 bondport_RecordDefault(p
);
4087 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4088 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4097 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4098 __unused
void * event_data
)
4100 partner_state_ref ps
;
4104 devtimer_cancel(p
->po_current_while_timer
);
4105 if (g_bond
->verbose
) {
4106 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4107 bondport_get_name(p
));
4109 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4110 ps
= &p
->po_partner_state
;
4111 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4113 case LAEventMediaChange
:
4114 if (media_active(&p
->po_media_info
)) {
4115 if (media_full_duplex(&p
->po_media_info
)) {
4116 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4119 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4122 else if (p
->po_selected
== SelectedState_SELECTED
) {
4125 if (g_bond
->verbose
) {
4126 timestamp_printf("[%s] Receive PORT_DISABLED: "
4127 "link timer started\n",
4128 bondport_get_name(p
));
4132 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4133 (devtimer_timeout_func
)
4134 bondport_receive_machine_port_disabled
,
4135 (void *)LAEventTimeout
, NULL
);
4137 else if (p
->po_selected
== SelectedState_STANDBY
) {
4138 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4141 case LAEventTimeout
:
4142 if (p
->po_selected
== SelectedState_SELECTED
) {
4143 if (g_bond
->verbose
) {
4144 timestamp_printf("[%s] Receive PORT_DISABLED: "
4145 "link timer completed, marking UNSELECTED\n",
4146 bondport_get_name(p
));
4148 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4151 case LAEventPortMoved
:
4152 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4161 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4162 __unused
void * event_data
)
4164 lacp_actor_partner_state s
;
4169 devtimer_cancel(p
->po_current_while_timer
);
4170 if (g_bond
->verbose
) {
4171 timestamp_printf("[%s] Receive EXPIRED\n",
4172 bondport_get_name(p
));
4174 p
->po_receive_state
= ReceiveState_EXPIRED
;
4175 s
= p
->po_partner_state
.ps_state
;
4176 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4177 s
= lacp_actor_partner_state_set_short_timeout(s
);
4178 p
->po_partner_state
.ps_state
= s
;
4180 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4181 /* start current_while timer */
4182 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4184 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4185 (devtimer_timeout_func
)
4186 bondport_receive_machine_expired
,
4187 (void *)LAEventTimeout
, NULL
);
4190 case LAEventTimeout
:
4191 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4200 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4201 __unused
void * event_data
)
4203 partner_state_ref ps
;
4206 devtimer_cancel(p
->po_current_while_timer
);
4207 if (g_bond
->verbose
) {
4208 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4209 bondport_get_name(p
));
4211 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4212 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4213 bondport_RecordDefault(p
);
4214 ps
= &p
->po_partner_state
;
4215 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4217 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4226 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4227 __unused
void * event_data
)
4231 devtimer_cancel(p
->po_current_while_timer
);
4232 if (g_bond
->verbose
) {
4233 timestamp_printf("[%s] Receive DEFAULTED\n",
4234 bondport_get_name(p
));
4236 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4237 bondport_UpdateDefaultSelected(p
);
4238 bondport_RecordDefault(p
);
4240 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4249 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4252 partner_state_ref ps
;
4257 devtimer_cancel(p
->po_current_while_timer
);
4258 if (g_bond
->verbose
) {
4259 timestamp_printf("[%s] Receive CURRENT\n",
4260 bondport_get_name(p
));
4262 p
->po_receive_state
= ReceiveState_CURRENT
;
4263 bondport_UpdateSelected(p
, event_data
);
4264 bondport_UpdateNTT(p
, event_data
);
4265 bondport_RecordPDU(p
, event_data
);
4267 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4268 bondport_assign_to_LAG(p
);
4269 /* start current_while timer */
4270 ps
= &p
->po_partner_state
;
4271 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4272 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4275 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4278 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4279 (devtimer_timeout_func
)
4280 bondport_receive_machine_current
,
4281 (void *)LAEventTimeout
, NULL
);
4283 case LAEventTimeout
:
4284 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4293 ** Periodic Transmission machine
4297 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4298 __unused
void * event_data
)
4301 partner_state_ref ps
;
4306 if (g_bond
->verbose
) {
4307 timestamp_printf("[%s] periodic_transmit Start\n",
4308 bondport_get_name(p
));
4311 case LAEventMediaChange
:
4312 devtimer_cancel(p
->po_periodic_timer
);
4313 p
->po_periodic_interval
= 0;
4314 if (media_active(&p
->po_media_info
) == 0
4315 || media_full_duplex(&p
->po_media_info
) == 0) {
4319 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4320 ps
= &p
->po_partner_state
;
4321 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4322 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4324 devtimer_cancel(p
->po_periodic_timer
);
4325 p
->po_periodic_interval
= 0;
4328 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4329 interval
= LACP_FAST_PERIODIC_TIME
;
4332 interval
= LACP_SLOW_PERIODIC_TIME
;
4334 if (p
->po_periodic_interval
!= interval
) {
4335 if (interval
== LACP_FAST_PERIODIC_TIME
4336 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4337 if (g_bond
->verbose
) {
4338 timestamp_printf("[%s] periodic_transmit:"
4339 " Need To Transmit\n",
4340 bondport_get_name(p
));
4342 bondport_flags_set_ntt(p
);
4344 p
->po_periodic_interval
= interval
;
4346 tv
.tv_sec
= interval
;
4347 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4348 (devtimer_timeout_func
)
4349 bondport_periodic_transmit_machine
,
4350 (void *)LAEventTimeout
, NULL
);
4351 if (g_bond
->verbose
) {
4352 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4353 bondport_get_name(p
),
4354 p
->po_periodic_interval
);
4358 case LAEventTimeout
:
4359 bondport_flags_set_ntt(p
);
4360 tv
.tv_sec
= p
->po_periodic_interval
;
4362 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4363 bondport_periodic_transmit_machine
,
4364 (void *)LAEventTimeout
, NULL
);
4365 if (g_bond
->verbose
> 1) {
4366 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4367 bondport_get_name(p
), p
->po_periodic_interval
);
4380 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4381 __darwin_time_t
* next_secs
)
4383 if (p
->po_last_transmit_secs
!= current_secs
) {
4384 p
->po_last_transmit_secs
= current_secs
;
4385 p
->po_n_transmit
= 0;
4387 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4391 if (next_secs
!= NULL
) {
4392 *next_secs
= current_secs
+ 1;
4398 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4401 lacp_actor_partner_tlv_ref aptlv
;
4402 lacp_collector_tlv_ref ctlv
;
4403 struct timeval next_tick_time
= {0, 0};
4404 lacpdu_ref out_lacpdu_p
;
4405 packet_buffer_ref pkt
;
4406 partner_state_ref ps
;
4410 case LAEventTimeout
:
4412 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4415 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4416 /* we're going away, transmit the packet no matter what */
4418 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4419 &next_tick_time
.tv_sec
) == 0) {
4420 if (devtimer_enabled(p
->po_transmit_timer
)) {
4421 if (g_bond
->verbose
> 0) {
4422 timestamp_printf("[%s] Transmit Timer Already Set\n",
4423 bondport_get_name(p
));
4427 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4428 (devtimer_timeout_func
)
4429 bondport_transmit_machine
,
4430 (void *)LAEventTimeout
, NULL
);
4431 if (g_bond
->verbose
> 0) {
4432 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4433 bondport_get_name(p
),
4434 (int)next_tick_time
.tv_sec
);
4439 if (g_bond
->verbose
> 0) {
4440 if (event
== LAEventTimeout
) {
4441 timestamp_printf("[%s] Transmit Timer Complete\n",
4442 bondport_get_name(p
));
4445 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4447 printf("[%s] Transmit: failed to allocate packet buffer\n",
4448 bondport_get_name(p
));
4451 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4452 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4453 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4454 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4457 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4458 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4459 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4460 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4461 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4462 g_bond
->system_priority
);
4463 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4464 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4465 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4466 aptlv
->lap_state
= p
->po_actor_state
;
4469 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4470 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4471 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4472 ps
= &p
->po_partner_state
;
4473 ps_li
= &ps
->ps_lag_info
;
4474 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4475 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4476 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4477 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4478 ps_li
->li_system_priority
);
4479 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4480 aptlv
->lap_state
= ps
->ps_state
;
4483 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4484 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4485 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4487 bondport_slow_proto_transmit(p
, pkt
);
4488 bondport_flags_clear_ntt(p
);
4489 if (g_bond
->verbose
> 0) {
4490 timestamp_printf("[%s] Transmit Packet %d\n",
4491 bondport_get_name(p
), p
->po_n_transmit
);
4501 ** Mux machine functions
4505 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4508 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4511 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4515 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4519 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4521 switch (p
->po_mux_state
) {
4523 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4525 case MuxState_DETACHED
:
4526 bondport_mux_machine_detached(p
, event
, event_data
);
4528 case MuxState_WAITING
:
4529 bondport_mux_machine_waiting(p
, event
, event_data
);
4531 case MuxState_ATTACHED
:
4532 bondport_mux_machine_attached(p
, event
, event_data
);
4534 case MuxState_COLLECTING_DISTRIBUTING
:
4535 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4544 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4545 __unused
void * event_data
)
4547 lacp_actor_partner_state s
;
4551 devtimer_cancel(p
->po_wait_while_timer
);
4552 if (g_bond
->verbose
) {
4553 timestamp_printf("[%s] Mux DETACHED\n",
4554 bondport_get_name(p
));
4556 p
->po_mux_state
= MuxState_DETACHED
;
4557 bondport_flags_clear_ready(p
);
4558 bondport_DetachMuxFromAggregator(p
);
4559 bondport_disable_distributing(p
);
4560 s
= p
->po_actor_state
;
4561 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4562 s
= lacp_actor_partner_state_set_not_collecting(s
);
4563 s
= lacp_actor_partner_state_set_not_distributing(s
);
4564 p
->po_actor_state
= s
;
4565 bondport_flags_set_ntt(p
);
4567 case LAEventSelectedChange
:
4569 case LAEventMediaChange
:
4570 if (p
->po_selected
== SelectedState_SELECTED
4571 || p
->po_selected
== SelectedState_STANDBY
) {
4572 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4582 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4583 __unused
void * event_data
)
4589 devtimer_cancel(p
->po_wait_while_timer
);
4590 if (g_bond
->verbose
) {
4591 timestamp_printf("[%s] Mux WAITING\n",
4592 bondport_get_name(p
));
4594 p
->po_mux_state
= MuxState_WAITING
;
4597 case LAEventSelectedChange
:
4598 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4599 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4602 if (p
->po_selected
== SelectedState_STANDBY
) {
4603 devtimer_cancel(p
->po_wait_while_timer
);
4604 /* wait until state changes to SELECTED */
4605 if (g_bond
->verbose
) {
4606 timestamp_printf("[%s] Mux WAITING: Standby\n",
4607 bondport_get_name(p
));
4611 if (bondport_flags_ready(p
)) {
4612 if (g_bond
->verbose
) {
4613 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4614 bondport_get_name(p
));
4618 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4619 if (g_bond
->verbose
) {
4620 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4621 bondport_get_name(p
));
4625 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4626 devtimer_cancel(p
->po_wait_while_timer
);
4627 if (g_bond
->verbose
) {
4628 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4629 bondport_get_name(p
));
4631 bondport_flags_set_ready(p
);
4634 if (g_bond
->verbose
) {
4635 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4636 bondport_get_name(p
));
4638 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4640 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4641 (devtimer_timeout_func
)
4642 bondport_mux_machine_waiting
,
4643 (void *)LAEventTimeout
, NULL
);
4645 case LAEventTimeout
:
4646 if (g_bond
->verbose
) {
4647 timestamp_printf("[%s] Mux WAITING: Ready\n",
4648 bondport_get_name(p
));
4650 bondport_flags_set_ready(p
);
4654 if (bondport_flags_ready(p
)){
4655 if (g_bond
->verbose
) {
4656 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4657 bondport_get_name(p
));
4659 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4668 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4669 __unused
void * event_data
)
4671 lacp_actor_partner_state s
;
4675 devtimer_cancel(p
->po_wait_while_timer
);
4676 if (g_bond
->verbose
) {
4677 timestamp_printf("[%s] Mux ATTACHED\n",
4678 bondport_get_name(p
));
4680 p
->po_mux_state
= MuxState_ATTACHED
;
4681 bondport_AttachMuxToAggregator(p
);
4682 s
= p
->po_actor_state
;
4683 s
= lacp_actor_partner_state_set_in_sync(s
);
4684 s
= lacp_actor_partner_state_set_not_collecting(s
);
4685 s
= lacp_actor_partner_state_set_not_distributing(s
);
4686 bondport_disable_distributing(p
);
4687 p
->po_actor_state
= s
;
4688 bondport_flags_set_ntt(p
);
4691 switch (p
->po_selected
) {
4692 case SelectedState_SELECTED
:
4693 s
= p
->po_partner_state
.ps_state
;
4694 if (lacp_actor_partner_state_in_sync(s
)) {
4695 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4700 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4709 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4711 __unused
void * event_data
)
4713 lacp_actor_partner_state s
;
4717 devtimer_cancel(p
->po_wait_while_timer
);
4718 if (g_bond
->verbose
) {
4719 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4720 bondport_get_name(p
));
4722 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4723 bondport_enable_distributing(p
);
4724 s
= p
->po_actor_state
;
4725 s
= lacp_actor_partner_state_set_collecting(s
);
4726 s
= lacp_actor_partner_state_set_distributing(s
);
4727 p
->po_actor_state
= s
;
4728 bondport_flags_set_ntt(p
);
4731 s
= p
->po_partner_state
.ps_state
;
4732 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4733 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4736 switch (p
->po_selected
) {
4737 case SelectedState_UNSELECTED
:
4738 case SelectedState_STANDBY
:
4739 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);