2 * Copyright (c) 2004-2008 Apple Computer, 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 extern void dlil_input_packet_list(struct ifnet
*, struct mbuf
*);
84 static struct ether_addr slow_proto_multicast
= {
85 IEEE8023AD_SLOW_PROTO_MULTICAST
88 #define BOND_MAXUNIT 128
89 #define BONDNAME "bond"
90 #define M_BOND M_DEVBUF
92 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
93 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
94 #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)
96 #define timestamp_printf printf
101 static __inline__ lck_grp_t
*
102 my_lck_grp_alloc_init(const char * grp_name
)
105 lck_grp_attr_t
* grp_attrs
;
107 grp_attrs
= lck_grp_attr_alloc_init();
108 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
109 lck_grp_attr_free(grp_attrs
);
113 static __inline__ lck_mtx_t
*
114 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
116 lck_attr_t
* lck_attrs
;
119 lck_attrs
= lck_attr_alloc_init();
120 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
121 lck_attr_free(lck_attrs
);
125 static lck_mtx_t
* bond_lck_mtx
;
127 static __inline__
void
130 lck_grp_t
* bond_lck_grp
;
132 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
133 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
136 static __inline__
void
137 bond_assert_lock_held(void)
139 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
143 static __inline__
void
144 bond_assert_lock_not_held(void)
146 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
150 static __inline__
void
153 lck_mtx_lock(bond_lck_mtx
);
157 static __inline__
void
160 lck_mtx_unlock(bond_lck_mtx
);
165 ** bond structures, types
169 lacp_system li_system
;
170 lacp_system_priority li_system_priority
;
173 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
176 TAILQ_HEAD(port_list
, bondport_s
);
178 TAILQ_HEAD(ifbond_list
, ifbond_s
);
180 TAILQ_HEAD(lag_list
, LAG_s
);
182 typedef struct ifbond_s ifbond
, * ifbond_ref
;
183 typedef struct bondport_s bondport
, * bondport_ref
;
186 TAILQ_ENTRY(LAG_s
) lag_list
;
187 struct port_list lag_port_list
;
188 short lag_port_count
;
189 short lag_selected_port_count
;
190 int lag_active_media
;
193 typedef struct LAG_s LAG
, * LAG_ref
;
195 typedef struct partner_state_s
{
196 LAG_info ps_lag_info
;
198 lacp_port_priority ps_port_priority
;
199 lacp_actor_partner_state ps_state
;
200 } partner_state
, * partner_state_ref
;
203 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
205 SInt32 ifb_retain_count
;
206 char ifb_name
[IFNAMSIZ
];
207 struct ifnet
* ifb_ifp
;
208 bpf_packet_func ifb_bpf_input
;
209 bpf_packet_func ifb_bpf_output
;
211 struct port_list ifb_port_list
;
212 short ifb_port_count
;
213 struct lag_list ifb_lag_list
;
215 short ifb_max_active
; /* 0 == unlimited */
216 LAG_ref ifb_active_lag
;
217 struct ifmultiaddr
* ifb_ifma_slow_proto
;
218 bondport_ref
* ifb_distributing_array
;
219 int ifb_distributing_count
;
220 int ifb_last_link_event
;
221 int ifb_mode
; /* LACP, STATIC */
230 ReceiveState_none
= 0,
231 ReceiveState_INITIALIZE
= 1,
232 ReceiveState_PORT_DISABLED
= 2,
233 ReceiveState_EXPIRED
= 3,
234 ReceiveState_LACP_DISABLED
= 4,
235 ReceiveState_DEFAULTED
= 5,
236 ReceiveState_CURRENT
= 6,
239 typedef u_char ReceiveState
;
242 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
243 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
244 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
246 typedef u_char SelectedState
;
248 static __inline__
const char *
249 SelectedStateString(SelectedState s
)
251 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
253 if (s
<= SelectedState_STANDBY
) {
256 return ("<unknown>");
261 MuxState_DETACHED
= 1,
262 MuxState_WAITING
= 2,
263 MuxState_ATTACHED
= 3,
264 MuxState_COLLECTING_DISTRIBUTING
= 4,
267 typedef u_char MuxState
;
270 TAILQ_ENTRY(bondport_s
) po_port_list
;
272 struct multicast_list po_multicast
;
273 struct ifnet
* po_ifp
;
274 struct ether_addr po_saved_addr
;
276 char po_name
[IFNAMSIZ
];
277 struct ifdevmtu po_devmtu
;
280 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
281 devtimer_ref po_current_while_timer
;
282 devtimer_ref po_periodic_timer
;
283 devtimer_ref po_wait_while_timer
;
284 devtimer_ref po_transmit_timer
;
285 partner_state po_partner_state
;
286 lacp_port_priority po_priority
;
287 lacp_actor_partner_state po_actor_state
;
289 u_char po_periodic_interval
;
290 u_char po_n_transmit
;
291 ReceiveState po_receive_state
;
292 MuxState po_mux_state
;
293 SelectedState po_selected
;
294 int32_t po_last_transmit_secs
;
295 struct media_info po_media_info
;
299 #define IFBF_PROMISC 0x1 /* promiscuous mode */
300 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
301 #define IFBF_LLADDR 0x4 /* specific link address requested */
302 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
304 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
307 static __inline__
int
308 ifbond_flags_promisc(ifbond_ref ifb
)
310 return ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0);
313 static __inline__
void
314 ifbond_flags_set_promisc(ifbond_ref ifb
)
316 ifb
->ifb_flags
|= IFBF_PROMISC
;
320 static __inline__
void
321 ifbond_flags_clear_promisc(ifbond_ref ifb
)
323 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
327 static __inline__
int
328 ifbond_flags_if_detaching(ifbond_ref ifb
)
330 return ((ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0);
333 static __inline__
void
334 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
336 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
340 static __inline__
int
341 ifbond_flags_lladdr(ifbond_ref ifb
)
343 return ((ifb
->ifb_flags
& IFBF_LLADDR
) != 0);
346 static __inline__
void
347 ifbond_flags_set_lladdr(ifbond_ref ifb
)
349 ifb
->ifb_flags
|= IFBF_LLADDR
;
353 static __inline__
void
354 ifbond_flags_clear_lladdr(ifbond_ref ifb
)
356 ifb
->ifb_flags
&= ~IFBF_LLADDR
;
360 static __inline__
int
361 ifbond_flags_change_in_progress(ifbond_ref ifb
)
363 return ((ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0);
366 static __inline__
void
367 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
369 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
373 static __inline__
void
374 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
376 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
381 * bondport_ref->po_flags bits
383 #define BONDPORT_FLAGS_NTT 0x01
384 #define BONDPORT_FLAGS_READY 0x02
385 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
386 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
387 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
388 #define BONDPORT_FLAGS_UNUSED2 0x20
389 #define BONDPORT_FLAGS_UNUSED3 0x40
390 #define BONDPORT_FLAGS_UNUSED4 0x80
392 static __inline__
void
393 bondport_flags_set_ntt(bondport_ref p
)
395 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
399 static __inline__
void
400 bondport_flags_clear_ntt(bondport_ref p
)
402 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
406 static __inline__
int
407 bondport_flags_ntt(bondport_ref p
)
409 return ((p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0);
412 static __inline__
void
413 bondport_flags_set_ready(bondport_ref p
)
415 p
->po_flags
|= BONDPORT_FLAGS_READY
;
419 static __inline__
void
420 bondport_flags_clear_ready(bondport_ref p
)
422 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
426 static __inline__
int
427 bondport_flags_ready(bondport_ref p
)
429 return ((p
->po_flags
& BONDPORT_FLAGS_READY
) != 0);
432 static __inline__
void
433 bondport_flags_set_selected_changed(bondport_ref p
)
435 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
439 static __inline__
void
440 bondport_flags_clear_selected_changed(bondport_ref p
)
442 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
446 static __inline__
int
447 bondport_flags_selected_changed(bondport_ref p
)
449 return ((p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0);
452 static __inline__
void
453 bondport_flags_set_mux_attached(bondport_ref p
)
455 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
459 static __inline__
void
460 bondport_flags_clear_mux_attached(bondport_ref p
)
462 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
466 static __inline__
int
467 bondport_flags_mux_attached(bondport_ref p
)
469 return ((p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0);
472 static __inline__
void
473 bondport_flags_set_distributing(bondport_ref p
)
475 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
479 static __inline__
void
480 bondport_flags_clear_distributing(bondport_ref p
)
482 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
486 static __inline__
int
487 bondport_flags_distributing(bondport_ref p
)
489 return ((p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0);
492 typedef struct bond_globals_s
{
493 struct ifbond_list ifbond_list
;
495 lacp_system_priority system_priority
;
497 } * bond_globals_ref
;
499 static bond_globals_ref g_bond
;
502 ** packet_buffer routines
503 ** - thin wrapper for mbuf
506 typedef struct mbuf
* packet_buffer_ref
;
508 static packet_buffer_ref
509 packet_buffer_allocate(int length
)
514 /* leave room for ethernet header */
515 size
= length
+ sizeof(struct ether_header
);
516 if (size
> (int)MHLEN
) {
517 /* XXX doesn't handle large payloads */
518 printf("bond: packet_buffer_allocate size %d > max %u\n", size
, MHLEN
);
521 m
= m_gethdr(M_WAITOK
, MT_DATA
);
526 m
->m_pkthdr
.len
= size
;
531 packet_buffer_byteptr(packet_buffer_ref buf
)
533 return (buf
->m_data
+ sizeof(struct ether_header
));
541 LAEventSelectedChange
,
550 bondport_receive_machine(bondport_ref p
, LAEvent event
,
553 ** Periodic Transmission machine
556 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
562 #define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
565 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
572 bondport_mux_machine(bondport_ref p
, LAEvent event
,
579 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
582 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
585 ifbond_all_ports_ready(ifbond_ref bond
);
588 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
591 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
594 ifbond_selection(ifbond_ref bond
);
602 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
605 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
608 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
609 int active
, int short_timeout
, int * error
);
611 bondport_start(bondport_ref p
);
614 bondport_free(bondport_ref p
);
617 bondport_aggregatable(bondport_ref p
);
620 bondport_remove_from_LAG(bondport_ref p
);
623 bondport_set_selected(bondport_ref p
, SelectedState s
);
626 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
629 bondport_link_status_changed(bondport_ref p
);
632 bondport_enable_distributing(bondport_ref p
);
635 bondport_disable_distributing(bondport_ref p
);
637 static __inline__
int
638 bondport_collecting(bondport_ref p
)
640 if (p
->po_bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
641 return (lacp_actor_partner_state_collecting(p
->po_actor_state
));
647 ** bond interface/dlil specific routines
649 static int bond_clone_create(struct if_clone
*, int);
650 static void bond_clone_destroy(struct ifnet
*);
651 static int bond_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t m
,
653 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
654 static int bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * addr
);
655 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
656 bpf_packet_func func
);
657 static int bond_attach_protocol(struct ifnet
*ifp
);
658 static int bond_detach_protocol(struct ifnet
*ifp
);
659 static int bond_setmulti(struct ifnet
*ifp
);
660 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
661 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
662 static void bond_if_free(struct ifnet
* ifp
);
664 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
669 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
672 siocsifmtu(struct ifnet
* ifp
, int mtu
)
676 bzero(&ifr
, sizeof(ifr
));
678 return (ifnet_ioctl(ifp
, 0, SIOCSIFMTU
, &ifr
));
682 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
687 bzero(&ifr
, sizeof(ifr
));
688 error
= ifnet_ioctl(ifp
, 0, SIOCGIFDEVMTU
, &ifr
);
690 *ifdm_p
= ifr
.ifr_devmtu
;
695 static __inline__
void
696 ether_addr_copy(void * dest
, const void * source
)
698 bcopy(source
, dest
, ETHER_ADDR_LEN
);
702 static __inline__
void
703 ifbond_retain(ifbond_ref ifb
)
705 OSIncrementAtomic(&ifb
->ifb_retain_count
);
708 static __inline__
void
709 ifbond_release(ifbond_ref ifb
)
711 UInt32 old_retain_count
;
713 old_retain_count
= OSDecrementAtomic(&ifb
->ifb_retain_count
);
714 switch (old_retain_count
) {
716 panic("ifbond_release: retain count is 0\n");
719 if (g_bond
->verbose
) {
720 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
722 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
723 if (g_bond
->verbose
) {
724 printf("ifbond_release(%s) removing multicast\n",
727 (void)if_delmultiaddr(ifb
->ifb_ifma_slow_proto
, 0);
728 ifma_release(ifb
->ifb_ifma_slow_proto
);
730 if (ifb
->ifb_distributing_array
!= NULL
) {
731 FREE(ifb
->ifb_distributing_array
, M_BOND
);
742 * Function: ifbond_wait
744 * Allows a single thread to gain exclusive access to the ifbond
745 * data structure. Some operations take a long time to complete,
746 * and some have side-effects that we can't predict. Holding the
747 * bond_lock() across such operations is not possible.
750 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
751 * complete. Simply holding the bond_lock() would freeze all other
752 * data structure accesses during that time.
753 * 2) When we attach our protocol to the interface, a dlil event is
754 * generated and invokes our bond_event() function. bond_event()
755 * needs to take the bond_lock(), but we're already holding it, so
756 * we're deadlocked against ourselves.
758 * Before calling, you must be holding the bond_lock and have taken
759 * a reference on the ifbond_ref.
762 ifbond_wait(ifbond_ref ifb
, const char * msg
)
766 /* other add/remove in progress */
767 while (ifbond_flags_change_in_progress(ifb
)) {
768 if (g_bond
->verbose
) {
769 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
772 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
774 /* prevent other bond list remove/add from taking place */
775 ifbond_flags_set_change_in_progress(ifb
);
776 if (g_bond
->verbose
&& waited
) {
777 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
783 * Function: ifbond_signal
785 * Allows the thread that previously invoked ifbond_wait() to
786 * give up exclusive access to the ifbond data structure, and wake up
787 * any other threads waiting to access
789 * Before calling, you must be holding the bond_lock and have taken
790 * a reference on the ifbond_ref.
793 ifbond_signal(ifbond_ref ifb
, const char * msg
)
795 ifbond_flags_clear_change_in_progress(ifb
);
796 wakeup((caddr_t
)ifb
);
797 if (g_bond
->verbose
) {
798 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
808 link_speed(int active
)
810 switch (IFM_SUBTYPE(active
)) {
831 /* assume that new defined types are going to be at least 10GigE */
838 static __inline__
int
839 media_active(const struct media_info
* mi
)
841 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
844 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
847 static __inline__
int
848 media_full_duplex(const struct media_info
* mi
)
850 return ((mi
->mi_active
& IFM_FDX
) != 0);
853 static __inline__
int
854 media_speed(const struct media_info
* mi
)
856 return (link_speed(mi
->mi_active
));
859 static struct media_info
860 interface_media_info(struct ifnet
* ifp
)
862 struct ifmediareq ifmr
;
863 struct media_info mi
;
865 bzero(&mi
, sizeof(mi
));
866 bzero(&ifmr
, sizeof(ifmr
));
867 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
868 if (ifmr
.ifm_count
!= 0) {
869 mi
.mi_status
= ifmr
.ifm_status
;
870 mi
.mi_active
= ifmr
.ifm_active
;
877 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
882 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
883 * currently expects it that way
885 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
886 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
887 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
889 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", ifnet_name(ifp
),
892 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
898 static bond_globals_ref
899 bond_globals_create(lacp_system_priority sys_pri
,
904 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
);
908 bzero(b
, sizeof(*b
));
909 TAILQ_INIT(&b
->ifbond_list
);
911 b
->system_priority
= sys_pri
;
919 bond_globals_init(void)
925 bond_assert_lock_not_held();
927 if (g_bond
!= NULL
) {
932 * use en0's ethernet address as the system identifier, and if it's not
933 * there, use en1 .. en3
936 for (i
= 0; i
< 4; i
++) {
937 char ifname
[IFNAMSIZ
+1];
938 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
939 /* XXX ifunit() needs to return a reference on the ifp */
940 ifp
= ifunit(ifname
);
947 b
= bond_globals_create(0x8000, (lacp_system_ref
)ifnet_lladdr(ifp
));
950 if (g_bond
!= NULL
) {
967 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
968 const struct ether_header
* eh_p
,
969 u_int16_t vlan_tag
, bpf_packet_func func
)
971 struct ether_vlan_header
* vlh_p
;
974 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
978 /* populate a new mbuf containing the vlan ethernet header */
979 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
980 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
981 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
982 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
983 vlh_p
->evl_tag
= htons(vlan_tag
);
984 vlh_p
->evl_proto
= eh_p
->ether_type
;
992 static __inline__
void
993 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
994 bpf_packet_func func
)
997 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
998 const struct ether_header
* eh_p
;
999 eh_p
= mtod(m
, const struct ether_header
*);
1000 m
->m_data
+= ETHER_HDR_LEN
;
1001 m
->m_len
-= ETHER_HDR_LEN
;
1002 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1003 m
->m_data
-= ETHER_HDR_LEN
;
1004 m
->m_len
+= ETHER_HDR_LEN
;
1012 static __inline__
void
1013 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
1014 bpf_packet_func func
)
1017 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1018 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1020 /* restore the header */
1021 m
->m_data
-= ETHER_HDR_LEN
;
1022 m
->m_len
+= ETHER_HDR_LEN
;
1024 m
->m_data
+= ETHER_HDR_LEN
;
1025 m
->m_len
-= ETHER_HDR_LEN
;
1032 * Function: bond_setmulti
1034 * Enable multicast reception on "our" interface by enabling multicasts on
1035 * each of the member ports.
1038 bond_setmulti(struct ifnet
* ifp
)
1046 ifb
= ifnet_softc(ifp
);
1047 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1048 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1053 ifbond_wait(ifb
, "bond_setmulti");
1055 if (ifbond_flags_if_detaching(ifb
)) {
1056 /* someone destroyed the bond while we were waiting */
1062 /* ifbond_wait() let's us safely walk the list without holding the lock */
1063 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1064 struct ifnet
* port_ifp
= p
->po_ifp
;
1066 error
= multicast_list_program(&p
->po_multicast
,
1069 printf("bond_setmulti(%s): "
1070 "multicast_list_program(%s%d) failed, %d\n",
1071 ifb
->ifb_name
, ifnet_name(port_ifp
),
1072 ifnet_unit(port_ifp
), error
);
1078 ifbond_signal(ifb
, "bond_setmulti");
1080 ifbond_release(ifb
);
1085 bond_clone_attach(void)
1089 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1096 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1099 struct ifmultiaddr
* ifma
= NULL
;
1100 struct sockaddr_dl sdl
;
1102 bond_assert_lock_not_held();
1104 bzero(&sdl
, sizeof(sdl
));
1105 sdl
.sdl_len
= sizeof(sdl
);
1106 sdl
.sdl_family
= AF_LINK
;
1107 sdl
.sdl_type
= IFT_ETHER
;
1109 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1110 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1111 error
= if_addmulti(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
,
1114 ifb
->ifb_ifma_slow_proto
= ifma
;
1120 bond_clone_create(struct if_clone
* ifc
, int unit
)
1125 struct ifnet_init_params bond_init
;
1127 error
= bond_globals_init();
1132 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
);
1136 bzero(ifb
, sizeof(*ifb
));
1139 TAILQ_INIT(&ifb
->ifb_port_list
);
1140 TAILQ_INIT(&ifb
->ifb_lag_list
);
1141 ifb
->ifb_key
= unit
+ 1;
1143 /* use the interface name as the unique id for ifp recycle */
1144 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1145 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1146 ifbond_release(ifb
);
1150 bzero(&bond_init
, sizeof(bond_init
));
1151 bond_init
.uniqueid
= ifb
->ifb_name
;
1152 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1153 bond_init
.name
= ifc
->ifc_name
;
1154 bond_init
.unit
= unit
;
1155 bond_init
.family
= IFNET_FAMILY_BOND
;
1156 bond_init
.type
= IFT_IEEE8023ADLAG
;
1157 bond_init
.output
= bond_output
;
1158 bond_init
.demux
= ether_demux
;
1159 bond_init
.add_proto
= ether_add_proto
;
1160 bond_init
.del_proto
= ether_del_proto
;
1161 bond_init
.check_multi
= ether_check_multi
;
1162 bond_init
.framer
= ether_frameout
;
1163 bond_init
.ioctl
= bond_ioctl
;
1164 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1165 bond_init
.detach
= bond_if_free
;
1166 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1167 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1168 bond_init
.softc
= ifb
;
1169 error
= ifnet_allocate(&bond_init
, &ifp
);
1172 ifbond_release(ifb
);
1177 ifnet_set_offload(ifp
, 0);
1178 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1179 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1180 ifnet_set_baudrate(ifp
, 0);
1181 ifnet_set_mtu(ifp
, 0);
1183 error
= ifnet_attach(ifp
, NULL
);
1186 ifbond_release(ifb
);
1189 error
= ifbond_add_slow_proto_multicast(ifb
);
1191 printf("bond_clone_create(%s): "
1192 "failed to add slow_proto multicast, %d\n",
1193 ifb
->ifb_name
, error
);
1196 /* attach as ethernet */
1197 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1200 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1207 bond_remove_all_interfaces(ifbond_ref ifb
)
1211 bond_assert_lock_held();
1214 * do this in reverse order to avoid re-programming the mac address
1215 * as each head interface is removed
1217 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1218 bond_remove_interface(ifb
, p
->po_ifp
);
1224 bond_remove(ifbond_ref ifb
)
1226 bond_assert_lock_held();
1227 ifbond_flags_set_if_detaching(ifb
);
1228 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1229 bond_remove_all_interfaces(ifb
);
1234 bond_if_detach(struct ifnet
* ifp
)
1238 error
= ifnet_detach(ifp
);
1240 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1241 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1248 bond_clone_destroy(struct ifnet
* ifp
)
1253 ifb
= ifnet_softc(ifp
);
1254 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1258 if (ifbond_flags_if_detaching(ifb
)) {
1264 bond_if_detach(ifp
);
1269 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1274 ifb
= ifnet_softc(ifp
);
1275 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1280 case BPF_TAP_DISABLE
:
1281 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1285 ifb
->ifb_bpf_input
= func
;
1288 case BPF_TAP_OUTPUT
:
1289 ifb
->ifb_bpf_output
= func
;
1292 case BPF_TAP_INPUT_OUTPUT
:
1293 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1303 ether_header_hash(struct ether_header
* eh_p
)
1307 /* get 32-bits from destination ether and ether type */
1308 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1310 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1314 static struct mbuf
*
1315 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1320 while (*offset
>= len
) {
1331 #if BYTE_ORDER == BIG_ENDIAN
1332 static __inline__
uint32_t
1333 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1335 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1336 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1338 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1339 static __inline__
uint32_t
1340 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1342 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1343 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1345 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1348 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1350 struct mbuf
* current
;
1351 u_char
* current_data
;
1356 current
= S_mbuf_skip_to_offset(m
, &offset
);
1357 if (current
== NULL
) {
1360 current_data
= mtod(current
, u_char
*) + offset
;
1361 space_current
= current
->m_len
- offset
;
1362 if (space_current
>= (int)sizeof(uint32_t)) {
1363 *val
= *((uint32_t *)current_data
);
1366 next
= current
->m_next
;
1367 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1370 next_data
= mtod(next
, u_char
*);
1371 switch (space_current
) {
1373 *val
= make_uint32(current_data
[0], next_data
[0],
1374 next_data
[1], next_data
[2]);
1377 *val
= make_uint32(current_data
[0], current_data
[1],
1378 next_data
[0], next_data
[1]);
1381 *val
= make_uint32(current_data
[0], current_data
[1],
1382 current_data
[2], next_data
[0]);
1388 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1389 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1392 ip_header_hash(struct mbuf
* m
)
1395 struct in_addr ip_dst
;
1396 struct in_addr ip_src
;
1399 struct mbuf
* orig_m
= m
;
1401 /* find the IP protocol field relative to the start of the packet */
1402 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1403 m
= S_mbuf_skip_to_offset(m
, &offset
);
1404 if (m
== NULL
|| m
->m_len
< 1) {
1407 data
= mtod(m
, u_char
*) + offset
;
1410 /* find the IP src relative to the IP protocol */
1411 if ((m
->m_len
- offset
)
1412 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1413 /* this should be the normal case */
1414 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1415 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1418 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1419 (uint32_t *)&ip_src
.s_addr
)) {
1422 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1423 (uint32_t *)&ip_dst
.s_addr
)) {
1427 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1430 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1433 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1435 ipv6_header_hash(struct mbuf
* m
)
1440 struct mbuf
* orig_m
= m
;
1444 /* find the IP protocol field relative to the start of the packet */
1445 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1446 m
= S_mbuf_skip_to_offset(m
, &offset
);
1448 goto bad_ipv6_packet
;
1450 data
= mtod(m
, u_char
*) + offset
;
1452 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1453 /* this should be the normal case */
1454 for (i
= 0, scan
= (uint32_t *)data
;
1455 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1461 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1463 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1464 (uint32_t *)&tmp
)) {
1465 goto bad_ipv6_packet
;
1470 return (ntohl(val
));
1473 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1477 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1479 bpf_packet_func bpf_func
;
1482 struct ifnet
* port_ifp
= NULL
;
1487 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1491 if (m
->m_pkthdr
.socket_id
!= 0) {
1492 h
= m
->m_pkthdr
.socket_id
;
1495 struct ether_header
* eh_p
;
1497 eh_p
= mtod(m
, struct ether_header
*);
1498 switch (ntohs(eh_p
->ether_type
)) {
1500 h
= ip_header_hash(m
);
1502 case ETHERTYPE_IPV6
:
1503 h
= ipv6_header_hash(m
);
1506 h
= ether_header_hash(eh_p
);
1511 ifb
= ifnet_softc(ifp
);
1512 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1513 || ifb
->ifb_distributing_count
== 0) {
1516 h
%= ifb
->ifb_distributing_count
;
1517 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1518 bpf_func
= ifb
->ifb_bpf_output
;
1521 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1522 (void)ifnet_stat_increment_out(ifp
, 1,
1523 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1526 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1528 bond_bpf_output(ifp
, m
, bpf_func
);
1530 return (ifnet_output_raw(port_ifp
, PF_BOND
, m
));
1539 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1542 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1543 if (p
->po_ifp
== port_ifp
) {
1551 bond_lookup_port(struct ifnet
* port_ifp
)
1556 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1557 port
= ifbond_lookup_port(ifb
, port_ifp
);
1566 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1568 struct ifnet
* bond_ifp
= NULL
;
1574 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1577 p
= bond_lookup_port(port_ifp
);
1581 if (p
->po_enabled
== 0) {
1585 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1588 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1589 if (ifbond_selection(ifb
)) {
1590 event_code
= (ifb
->ifb_active_lag
== NULL
)
1593 /* XXX need to take a reference on bond_ifp */
1594 bond_ifp
= ifb
->ifb_ifp
;
1595 ifb
->ifb_last_link_event
= event_code
;
1598 event_code
= (ifb
->ifb_active_lag
== NULL
)
1601 if (event_code
!= ifb
->ifb_last_link_event
) {
1602 if (g_bond
->verbose
) {
1603 timestamp_printf("%s: (receive) generating LINK event\n",
1606 bond_ifp
= ifb
->ifb_ifp
;
1607 ifb
->ifb_last_link_event
= event_code
;
1613 if (bond_ifp
!= NULL
) {
1614 interface_link_event(bond_ifp
, event_code
);
1621 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1623 la_marker_pdu_ref marker_p
;
1626 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1627 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1631 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1635 p
= bond_lookup_port(port_ifp
);
1636 if (p
== NULL
|| p
->po_enabled
== 0
1637 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1641 /* echo back the same packet as a marker response */
1642 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1643 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1653 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1654 char * frame_header
)
1656 bpf_packet_func bpf_func
;
1657 const struct ether_header
* eh_p
;
1662 eh_p
= (const struct ether_header
*)frame_header
;
1663 if ((m
->m_flags
& M_MCAST
) != 0
1664 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1665 sizeof(eh_p
->ether_dhost
)) == 0
1666 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1667 u_char subtype
= *mtod(m
, u_char
*);
1669 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1670 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1675 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1676 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1681 bond_receive_lacpdu(m
, port_ifp
);
1684 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1687 /* restore the ethernet header pointer in the mbuf */
1688 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1689 m
->m_data
-= ETHER_HDR_LEN
;
1690 m
->m_len
+= ETHER_HDR_LEN
;
1691 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1692 if (m
->m_pkthdr
.len
< min_size
) {
1697 if (m
->m_len
< min_size
) {
1698 m
= m_pullup(m
, min_size
);
1703 /* send to marker responder */
1704 bond_receive_la_marker_pdu(m
, port_ifp
);
1707 else if (subtype
== 0
1708 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1709 /* invalid subtype, discard the frame */
1715 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1718 p
= bond_lookup_port(port_ifp
);
1719 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1723 /* make the packet appear as if it arrived on the bonded interface */
1726 bpf_func
= ifb
->ifb_bpf_input
;
1729 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1730 (void)ifnet_stat_increment_in(ifp
, 1,
1731 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1732 + ETHER_VLAN_ENCAP_LEN
), 0);
1735 (void)ifnet_stat_increment_in(ifp
, 1,
1736 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1738 m
->m_pkthdr
.rcvif
= ifp
;
1739 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1740 m
->m_pkthdr
.header
= frame_header
;
1741 dlil_input_packet_list(ifp
, m
);
1750 static __inline__
const char *
1751 bondport_get_name(bondport_ref p
)
1753 return (p
->po_name
);
1756 static __inline__
int
1757 bondport_get_index(bondport_ref p
)
1759 return (ifnet_index(p
->po_ifp
));
1763 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1765 struct ether_header
* eh_p
;
1768 /* packet_buffer_allocate leaves room for ethernet header */
1769 eh_p
= mtod(buf
, struct ether_header
*);
1770 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1771 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1772 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1773 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1775 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1776 bondport_get_name(p
), error
);
1782 bondport_timer_process_func(devtimer_ref timer
,
1783 devtimer_process_func_event event
)
1788 case devtimer_process_func_event_lock
:
1790 devtimer_retain(timer
);
1792 case devtimer_process_func_event_unlock
:
1793 if (devtimer_valid(timer
)) {
1794 /* as long as the devtimer is valid, we can look at arg0 */
1796 struct ifnet
* bond_ifp
= NULL
;
1798 p
= (bondport_ref
)devtimer_arg0(timer
);
1799 if (ifbond_selection(p
->po_bond
)) {
1800 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1803 /* XXX need to take a reference on bond_ifp */
1804 bond_ifp
= p
->po_bond
->ifb_ifp
;
1805 p
->po_bond
->ifb_last_link_event
= event_code
;
1808 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1811 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1812 if (g_bond
->verbose
) {
1813 timestamp_printf("%s: (timer) generating LINK event\n",
1814 p
->po_bond
->ifb_name
);
1816 bond_ifp
= p
->po_bond
->ifb_ifp
;
1817 p
->po_bond
->ifb_last_link_event
= event_code
;
1820 devtimer_release(timer
);
1822 if (bond_ifp
!= NULL
) {
1823 interface_link_event(bond_ifp
, event_code
);
1827 /* timer is going away */
1828 devtimer_release(timer
);
1838 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1839 int active
, int short_timeout
, int * ret_error
)
1842 bondport_ref p
= NULL
;
1843 lacp_actor_partner_state s
;
1846 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
);
1848 *ret_error
= ENOMEM
;
1851 bzero(p
, sizeof(*p
));
1852 multicast_list_init(&p
->po_multicast
);
1853 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1854 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1855 >= sizeof(p
->po_name
)) {
1856 printf("if_bond: name too large\n");
1857 *ret_error
= EINVAL
;
1860 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1862 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1863 bondport_get_name(p
), error
);
1866 /* remember the current interface MTU so it can be restored */
1867 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1868 p
->po_ifp
= port_ifp
;
1869 p
->po_media_info
= interface_media_info(port_ifp
);
1870 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1871 if (p
->po_current_while_timer
== NULL
) {
1872 *ret_error
= ENOMEM
;
1875 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1876 if (p
->po_periodic_timer
== NULL
) {
1877 *ret_error
= ENOMEM
;
1880 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1881 if (p
->po_wait_while_timer
== NULL
) {
1882 *ret_error
= ENOMEM
;
1885 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1886 if (p
->po_transmit_timer
== NULL
) {
1887 *ret_error
= ENOMEM
;
1890 p
->po_receive_state
= ReceiveState_none
;
1891 p
->po_mux_state
= MuxState_none
;
1892 p
->po_priority
= priority
;
1894 s
= lacp_actor_partner_state_set_aggregatable(s
);
1895 if (short_timeout
) {
1896 s
= lacp_actor_partner_state_set_short_timeout(s
);
1899 s
= lacp_actor_partner_state_set_active_lacp(s
);
1901 p
->po_actor_state
= s
;
1910 bondport_start(bondport_ref p
)
1912 bondport_receive_machine(p
, LAEventStart
, NULL
);
1913 bondport_mux_machine(p
, LAEventStart
, NULL
);
1914 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1915 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1920 * Function: bondport_invalidate_timers
1922 * Invalidate all of the timers for the bondport.
1925 bondport_invalidate_timers(bondport_ref p
)
1927 devtimer_invalidate(p
->po_current_while_timer
);
1928 devtimer_invalidate(p
->po_periodic_timer
);
1929 devtimer_invalidate(p
->po_wait_while_timer
);
1930 devtimer_invalidate(p
->po_transmit_timer
);
1934 * Function: bondport_cancel_timers
1936 * Cancel all of the timers for the bondport.
1939 bondport_cancel_timers(bondport_ref p
)
1941 devtimer_cancel(p
->po_current_while_timer
);
1942 devtimer_cancel(p
->po_periodic_timer
);
1943 devtimer_cancel(p
->po_wait_while_timer
);
1944 devtimer_cancel(p
->po_transmit_timer
);
1948 bondport_free(bondport_ref p
)
1950 multicast_list_remove(&p
->po_multicast
);
1951 devtimer_release(p
->po_current_while_timer
);
1952 devtimer_release(p
->po_periodic_timer
);
1953 devtimer_release(p
->po_wait_while_timer
);
1954 devtimer_release(p
->po_transmit_timer
);
1959 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1960 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1961 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1962 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1964 static __inline__
int
1965 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1967 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1968 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1972 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1979 bondport_ref
* new_array
= NULL
;
1980 bondport_ref
* old_array
= NULL
;
1984 /* pre-allocate space for new port */
1985 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1990 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1991 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1994 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1997 /* make sure this interface can handle our current MTU */
1998 devmtu
= bond_device_mtu(ifp
, ifb
);
2000 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
2002 printf("if_bond: interface %s doesn't support mtu %d",
2003 bondport_get_name(p
), devmtu
);
2008 /* make sure ifb doesn't get de-allocated while we wait */
2011 /* wait for other add or remove to complete */
2012 ifbond_wait(ifb
, "bond_add_interface");
2014 if (ifbond_flags_if_detaching(ifb
)) {
2015 /* someone destroyed the bond while we were waiting */
2019 if (bond_lookup_port(port_ifp
) != NULL
) {
2020 /* port is already part of a bond */
2024 ifnet_lock_exclusive(port_ifp
);
2025 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2026 /* interface already has VLAN's, or is part of bond */
2027 ifnet_lock_done(port_ifp
);
2032 /* mark the interface busy */
2033 /* can't use ifnet_set_eflags because that takes the lock */
2034 port_ifp
->if_eflags
|= IFEF_BOND
;
2035 ifnet_lock_done(port_ifp
);
2037 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2038 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2039 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2040 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2044 ifnet_offload_t ifp_offload
;
2045 ifnet_offload_t port_ifp_offload
;
2047 ifp_offload
= ifnet_offload(ifp
);
2048 port_ifp_offload
= ifnet_offload(port_ifp
);
2049 if (ifp_offload
!= port_ifp_offload
) {
2050 ifnet_offload_t offload
;
2052 offload
= ifp_offload
& port_ifp_offload
;
2053 printf("bond_add_interface(%s, %s) "
2054 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2055 ifb
->ifb_name
, bondport_get_name(p
),
2056 ifp_offload
, port_ifp_offload
, offload
);
2059 * if the bond has VLAN's, we can't simply change the hwassist
2060 * field behind its back: this needs work
2062 ifnet_set_offload(ifp
, offload
);
2067 /* remember the port's ethernet address so it can be restored */
2068 ether_addr_copy(&p
->po_saved_addr
, ifnet_lladdr(port_ifp
));
2070 /* add it to the list of ports */
2071 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2072 ifb
->ifb_port_count
++;
2074 /* set the default MTU */
2075 if (ifnet_mtu(ifp
) == 0) {
2076 ifnet_set_mtu(ifp
, ETHERMTU
);
2081 /* first port added to bond determines bond's ethernet address */
2083 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(port_ifp
), ETHER_ADDR_LEN
,
2087 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2089 /* allocate a larger distributing array */
2090 new_array
= (bondport_ref
*)
2091 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2092 if (new_array
== NULL
) {
2097 /* attach our BOND "protocol" to the interface */
2098 error
= bond_attach_protocol(port_ifp
);
2102 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2104 /* set the interface MTU */
2105 devmtu
= bond_device_mtu(ifp
, ifb
);
2106 error
= siocsifmtu(port_ifp
, devmtu
);
2108 printf("bond_add_interface(%s, %s):"
2109 " SIOCSIFMTU %d failed %d\n",
2110 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2113 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2115 /* program the port with our multicast addresses */
2116 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2118 printf("bond_add_interface(%s, %s):"
2119 " multicast_list_program failed %d\n",
2120 ifb
->ifb_name
, bondport_get_name(p
), error
);
2124 /* mark the interface up */
2125 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2127 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2129 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2130 ifb
->ifb_name
, bondport_get_name(p
), error
);
2134 /* re-program the port's ethernet address */
2135 error
= if_siflladdr(port_ifp
,
2136 (const struct ether_addr
*)ifnet_lladdr(ifp
));
2138 /* port doesn't support setting the link address */
2139 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2140 ifb
->ifb_name
, bondport_get_name(p
), error
);
2143 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2147 /* no failures past this point */
2150 /* copy the contents of the existing distributing array */
2151 if (ifb
->ifb_distributing_count
) {
2152 bcopy(ifb
->ifb_distributing_array
, new_array
,
2153 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2155 old_array
= ifb
->ifb_distributing_array
;
2156 ifb
->ifb_distributing_array
= new_array
;
2158 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2161 /* check if we need to generate a link status event */
2162 if (ifbond_selection(ifb
)) {
2163 event_code
= (ifb
->ifb_active_lag
== NULL
)
2166 ifb
->ifb_last_link_event
= event_code
;
2170 /* are we adding the first distributing interface? */
2171 if (media_active(&p
->po_media_info
)) {
2172 if (ifb
->ifb_distributing_count
== 0) {
2173 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2175 bondport_enable_distributing(p
);
2178 bondport_disable_distributing(p
);
2181 /* clear the busy state, and wakeup anyone waiting */
2182 ifbond_signal(ifb
, "bond_add_interface");
2184 if (event_code
!= 0) {
2185 interface_link_event(ifp
, event_code
);
2187 if (old_array
!= NULL
) {
2188 FREE(old_array
, M_BOND
);
2193 bond_assert_lock_not_held();
2195 /* if this was the first port to be added, clear our address */
2197 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2200 if (new_array
!= NULL
) {
2201 FREE(new_array
, M_BOND
);
2203 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2206 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2208 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2209 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2212 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2213 (void)bond_detach_protocol(port_ifp
);
2215 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2218 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2220 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2221 ifb
->ifb_name
, bondport_get_name(p
),
2222 p
->po_devmtu
.ifdm_current
, error1
);
2226 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2227 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2228 ifb
->ifb_port_count
--;
2230 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2231 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2232 ifb
->ifb_altmtu
= 0;
2233 ifnet_set_mtu(ifp
, 0);
2234 ifnet_set_offload(ifp
, 0);
2238 ifbond_signal(ifb
, "bond_add_interface");
2240 ifbond_release(ifb
);
2246 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2251 bondport_ref head_port
;
2254 int new_link_address
= FALSE
;
2256 lacp_actor_partner_state s
;
2257 int was_distributing
;
2259 bond_assert_lock_held();
2262 ifbond_wait(ifb
, "bond_remove_interface");
2264 p
= ifbond_lookup_port(ifb
, port_ifp
);
2267 /* it got removed by another thread */
2271 /* de-select it and remove it from the lists */
2272 was_distributing
= bondport_flags_distributing(p
);
2273 bondport_disable_distributing(p
);
2274 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2275 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2276 active_lag
= bondport_remove_from_LAG(p
);
2277 /* invalidate timers here while holding the bond_lock */
2278 bondport_invalidate_timers(p
);
2280 /* announce that we're Individual now */
2281 s
= p
->po_actor_state
;
2282 s
= lacp_actor_partner_state_set_individual(s
);
2283 s
= lacp_actor_partner_state_set_not_collecting(s
);
2284 s
= lacp_actor_partner_state_set_not_distributing(s
);
2285 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2286 p
->po_actor_state
= s
;
2287 bondport_flags_set_ntt(p
);
2290 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2291 ifb
->ifb_port_count
--;
2294 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2295 if (head_port
== NULL
) {
2296 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2297 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2300 ifnet_set_offload(ifp
, 0);
2301 ifnet_set_mtu(ifp
, 0);
2302 ifb
->ifb_altmtu
= 0;
2303 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2304 && bcmp(&p
->po_saved_addr
, ifnet_lladdr(ifp
),
2305 ETHER_ADDR_LEN
) == 0) {
2306 new_link_address
= TRUE
;
2308 /* check if we need to generate a link status event */
2309 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2310 if (ifbond_selection(ifb
) || active_lag
) {
2311 event_code
= (ifb
->ifb_active_lag
== NULL
)
2314 ifb
->ifb_last_link_event
= event_code
;
2316 bondport_transmit_machine(p
, LAEventStart
,
2317 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2320 /* are we removing the last distributing interface? */
2321 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2322 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2329 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2331 else if (new_link_address
) {
2332 struct ifnet
* scan_ifp
;
2333 bondport_ref scan_port
;
2335 /* ifbond_wait() allows port list traversal without holding the lock */
2337 /* this port gave the bond its ethernet address, switch to new one */
2338 ifnet_set_lladdr_and_type(ifp
,
2339 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2342 /* re-program each port with the new link address */
2343 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2344 scan_ifp
= scan_port
->po_ifp
;
2346 error
= if_siflladdr(scan_ifp
,
2347 (const struct ether_addr
*) ifnet_lladdr(ifp
));
2349 printf("bond_remove_interface(%s, %s): "
2350 "if_siflladdr (%s) failed %d\n",
2351 ifb
->ifb_name
, bondport_get_name(p
),
2352 bondport_get_name(scan_port
), error
);
2357 /* restore the port's ethernet address */
2358 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2360 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2361 ifb
->ifb_name
, bondport_get_name(p
), error
);
2364 /* restore the port's MTU */
2365 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2367 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2368 ifb
->ifb_name
, bondport_get_name(p
),
2369 p
->po_devmtu
.ifdm_current
, error
);
2372 /* remove the bond "protocol" */
2373 bond_detach_protocol(port_ifp
);
2375 /* generate link event */
2376 if (event_code
!= 0) {
2377 interface_link_event(ifp
, event_code
);
2382 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2383 /* release this bondport's reference to the ifbond */
2384 ifbond_release(ifb
);
2387 ifbond_signal(ifb
, "bond_remove_interface");
2388 ifbond_release(ifb
);
2393 bond_set_lacp_mode(ifbond_ref ifb
)
2397 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2398 bondport_disable_distributing(p
);
2405 bond_set_static_mode(ifbond_ref ifb
)
2408 lacp_actor_partner_state s
;
2410 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2411 bondport_disable_distributing(p
);
2412 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2413 (void)bondport_remove_from_LAG(p
);
2414 bondport_cancel_timers(p
);
2416 /* announce that we're Individual now */
2417 s
= p
->po_actor_state
;
2418 s
= lacp_actor_partner_state_set_individual(s
);
2419 s
= lacp_actor_partner_state_set_not_collecting(s
);
2420 s
= lacp_actor_partner_state_set_not_distributing(s
);
2421 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2422 p
->po_actor_state
= s
;
2423 bondport_flags_set_ntt(p
);
2424 bondport_transmit_machine(p
, LAEventStart
,
2425 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2427 p
->po_actor_state
= 0;
2428 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2430 if (media_active(&p
->po_media_info
)) {
2431 bondport_enable_distributing(p
);
2434 bondport_disable_distributing(p
);
2441 bond_set_mode(struct ifnet
* ifp
, int mode
)
2448 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2449 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2451 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2453 if (ifb
->ifb_mode
== mode
) {
2459 ifbond_wait(ifb
, "bond_set_mode");
2461 /* verify (again) that the mode is actually different */
2462 if (ifb
->ifb_mode
== mode
) {
2467 ifb
->ifb_mode
= mode
;
2468 if (mode
== IF_BOND_MODE_LACP
) {
2469 bond_set_lacp_mode(ifb
);
2471 /* check if we need to generate a link status event */
2472 if (ifbond_selection(ifb
)) {
2473 event_code
= (ifb
->ifb_active_lag
== NULL
)
2478 bond_set_static_mode(ifb
);
2479 event_code
= (ifb
->ifb_distributing_count
== 0)
2483 ifb
->ifb_last_link_event
= event_code
;
2486 ifbond_signal(ifb
, "bond_set_mode");
2488 ifbond_release(ifb
);
2490 if (event_code
!= 0) {
2491 interface_link_event(ifp
, event_code
);
2497 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2502 struct if_bond_status_req
* ibsr
;
2503 struct if_bond_status ibs
;
2506 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2507 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2510 ibsr
->ibsr_key
= ifb
->ifb_key
;
2511 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2512 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2513 dst
= proc_is64bit(current_proc())
2514 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2515 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2516 if (dst
== USER_ADDR_NULL
) {
2517 /* just want to know how many there are */
2520 if (ibsr
->ibsr_count
< 0) {
2523 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2524 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2525 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2526 struct if_bond_partner_state
* ibps_p
;
2527 partner_state_ref ps
;
2532 bzero(&ibs
, sizeof(ibs
));
2533 strncpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2534 ibs
.ibs_port_priority
= port
->po_priority
;
2535 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2536 ibs
.ibs_state
= port
->po_actor_state
;
2537 ibs
.ibs_selected_state
= port
->po_selected
;
2538 ps
= &port
->po_partner_state
;
2539 ibps_p
= &ibs
.ibs_partner_state
;
2540 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2541 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2542 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2543 ibps_p
->ibps_port
= ps
->ps_port
;
2544 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2545 ibps_p
->ibps_state
= ps
->ps_state
;
2548 /* fake the selected information */
2549 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2550 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2552 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2562 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2565 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2571 bond_set_promisc(__unused
struct ifnet
*ifp
)
2575 ifbond_ref ifb
= ifnet_softc(ifp
);
2578 if ((ifnet_flags(ifp
) & IFF_PROMISC
) != 0) {
2579 if ((ifb
->ifb_flags
& IFBF_PROMISC
) == 0) {
2580 error
= ifnet_set_promiscuous(ifb
->ifb_p
, 1);
2582 ifb
->ifb_flags
|= IFBF_PROMISC
;
2585 if ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0) {
2586 error
= ifnet_set_promiscuous(ifb
->ifb_p
, 0);
2588 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
2596 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2602 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2603 mtu_min
= IF_MINMTU
;
2605 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2606 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2608 if (devmtu_p
->ifdm_min
> mtu_min
) {
2609 mtu_min
= devmtu_p
->ifdm_min
;
2611 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2612 mtu_max
= devmtu_p
->ifdm_max
;
2621 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2626 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2627 error
= siocsifmtu(p
->po_ifp
, mtu
);
2629 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2630 ifb
->ifb_name
, bondport_get_name(p
), error
);
2638 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2648 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2649 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2650 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2654 ifbond_wait(ifb
, "bond_set_mtu");
2657 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2661 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2662 if (mtu
> mtu_max
) {
2666 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2667 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2672 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2675 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2677 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2678 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2679 if (new_max
!= old_max
) {
2680 /* we can safely walk the list of port without the lock held */
2682 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2684 /* try our best to back out of it */
2685 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2691 ifb
->ifb_altmtu
= mtu
;
2694 ifnet_set_mtu(ifp
, mtu
);
2699 ifbond_signal(ifb
, "bond_set_mtu");
2700 ifbond_release(ifb
);
2708 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2711 struct if_bond_req ibr
;
2712 struct ifaddr
* ifa
;
2715 struct ifmediareq
*ifmr
;
2716 struct ifnet
* port_ifp
= NULL
;
2717 user_addr_t user_addr
;
2719 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2720 return (EOPNOTSUPP
);
2722 ifr
= (struct ifreq
*)data
;
2723 ifa
= (struct ifaddr
*)data
;
2727 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2730 case SIOCGIFMEDIA32
:
2731 case SIOCGIFMEDIA64
:
2733 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2734 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2736 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2738 ifmr
= (struct ifmediareq
*)data
;
2739 ifmr
->ifm_current
= IFM_ETHER
;
2741 ifmr
->ifm_status
= IFM_AVALID
;
2742 ifmr
->ifm_active
= IFM_ETHER
;
2743 ifmr
->ifm_count
= 1;
2744 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2745 if (ifb
->ifb_active_lag
!= NULL
) {
2746 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2747 ifmr
->ifm_status
|= IFM_ACTIVE
;
2750 else if (ifb
->ifb_distributing_count
> 0) {
2752 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2753 ifmr
->ifm_status
|= IFM_ACTIVE
;
2756 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2757 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2758 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2759 if (user_addr
!= USER_ADDR_NULL
) {
2760 error
= copyout(&ifmr
->ifm_current
,
2767 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2773 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2774 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2776 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2779 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2780 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2781 &ifr
->ifr_devmtu
.ifdm_max
);
2787 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2788 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2790 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2793 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2798 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2802 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2806 user_addr
= proc_is64bit(current_proc())
2807 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2808 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2812 switch (ibr
.ibr_op
) {
2813 case IF_BOND_OP_ADD_INTERFACE
:
2814 case IF_BOND_OP_REMOVE_INTERFACE
:
2815 /* XXX ifunit() needs to return a reference on the ifp */
2816 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2817 if (port_ifp
== NULL
) {
2821 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2822 error
= EPROTONOSUPPORT
;
2826 case IF_BOND_OP_SET_VERBOSE
:
2827 case IF_BOND_OP_SET_MODE
:
2836 switch (ibr
.ibr_op
) {
2837 case IF_BOND_OP_ADD_INTERFACE
:
2838 error
= bond_add_interface(ifp
, port_ifp
);
2840 case IF_BOND_OP_REMOVE_INTERFACE
:
2842 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2843 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2845 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2847 error
= bond_remove_interface(ifb
, port_ifp
);
2850 case IF_BOND_OP_SET_VERBOSE
:
2852 if (g_bond
== NULL
) {
2857 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2860 case IF_BOND_OP_SET_MODE
:
2861 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2862 case IF_BOND_MODE_LACP
:
2863 case IF_BOND_MODE_STATIC
:
2872 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2875 break; /* SIOCSIFBOND */
2878 user_addr
= proc_is64bit(current_proc())
2879 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2880 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2884 switch (ibr
.ibr_op
) {
2885 case IF_BOND_OP_GET_STATUS
:
2895 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2896 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2898 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2900 switch (ibr
.ibr_op
) {
2901 case IF_BOND_OP_GET_STATUS
:
2902 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2906 break; /* SIOCGIFBOND */
2913 /* enable/disable promiscuous mode */
2915 error
= bond_set_promisc(ifp
);
2921 error
= bond_setmulti(ifp
);
2930 bond_if_free(struct ifnet
* ifp
)
2938 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2943 ifbond_release(ifb
);
2950 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
2951 const struct kev_msg
* event
)
2953 struct ifnet
* bond_ifp
= NULL
;
2956 int old_distributing_count
;
2958 struct media_info media_info
= { 0, 0};
2960 if (event
->vendor_code
!= KEV_VENDOR_APPLE
2961 || event
->kev_class
!= KEV_NETWORK_CLASS
2962 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
2965 switch (event
->event_code
) {
2966 case KEV_DL_IF_DETACHING
:
2968 case KEV_DL_LINK_OFF
:
2969 case KEV_DL_LINK_ON
:
2970 media_info
= interface_media_info(port_ifp
);
2976 p
= bond_lookup_port(port_ifp
);
2982 old_distributing_count
= ifb
->ifb_distributing_count
;
2983 switch (event
->event_code
) {
2984 case KEV_DL_IF_DETACHING
:
2985 bond_remove_interface(ifb
, p
->po_ifp
);
2987 case KEV_DL_LINK_OFF
:
2988 case KEV_DL_LINK_ON
:
2989 p
->po_media_info
= media_info
;
2990 if (p
->po_enabled
) {
2991 bondport_link_status_changed(p
);
2995 /* generate a link-event */
2996 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2997 if (ifbond_selection(ifb
)) {
2998 event_code
= (ifb
->ifb_active_lag
== NULL
)
3001 /* XXX need to take a reference on bond_ifp */
3002 bond_ifp
= ifb
->ifb_ifp
;
3003 ifb
->ifb_last_link_event
= event_code
;
3006 event_code
= (ifb
->ifb_active_lag
== NULL
)
3009 if (event_code
!= ifb
->ifb_last_link_event
) {
3010 if (g_bond
->verbose
) {
3011 timestamp_printf("%s: (event) generating LINK event\n",
3014 bond_ifp
= ifb
->ifb_ifp
;
3015 ifb
->ifb_last_link_event
= event_code
;
3021 * if the distributing array membership changed from 0 <-> !0
3022 * generate a link event
3024 if (old_distributing_count
== 0
3025 && ifb
->ifb_distributing_count
!= 0) {
3026 event_code
= KEV_DL_LINK_ON
;
3028 else if (old_distributing_count
!= 0
3029 && ifb
->ifb_distributing_count
== 0) {
3030 event_code
= KEV_DL_LINK_OFF
;
3032 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
3033 bond_ifp
= ifb
->ifb_ifp
;
3034 ifb
->ifb_last_link_event
= event_code
;
3039 if (bond_ifp
!= NULL
) {
3040 interface_link_event(bond_ifp
, event_code
);
3046 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3049 struct kern_event_msg header
;
3051 char if_name
[IFNAMSIZ
];
3054 event
.header
.total_size
= sizeof(event
);
3055 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3056 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3057 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3058 event
.header
.event_code
= event_code
;
3059 event
.header
.event_data
[0] = ifnet_family(ifp
);
3060 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3061 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3062 ifnet_event(ifp
, &event
.header
);
3067 * Function: bond_attach_protocol
3069 * Attach a DLIL protocol to the interface.
3071 * The ethernet demux special cases to always return PF_BOND if the
3072 * interface is bonded. That means we receive all traffic from that
3073 * interface without passing any of the traffic to any other attached
3077 bond_attach_protocol(struct ifnet
*ifp
)
3080 struct ifnet_attach_proto_param reg
;
3082 bzero(®
, sizeof(reg
));
3083 reg
.input
= bond_input
;
3084 reg
.event
= bond_event
;
3086 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3088 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3089 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3095 * Function: bond_detach_protocol
3097 * Detach our DLIL protocol from an interface
3100 bond_detach_protocol(struct ifnet
*ifp
)
3104 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3106 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3107 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3113 * DLIL interface family functions
3115 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3116 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3117 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3118 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3119 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3120 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3122 __private_extern__
int
3123 bond_family_init(void)
3127 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3131 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3136 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3138 ether_detach_inet6
);
3140 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3146 error
= proto_register_plumber(PF_APPLETALK
, APPLE_IF_FAM_BOND
,
3150 printf("bond: proto_register_plumber failed for AppleTalk error=%d\n",
3155 error
= bond_clone_attach();
3157 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3172 ** LACP ifbond_list routines
3175 ifbond_list_find_moved_port(bondport_ref rx_port
,
3176 const lacp_actor_partner_tlv_ref atlv
)
3180 partner_state_ref ps
;
3183 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3184 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3187 /* no point in comparing against ourselves */
3190 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3191 /* it's not clear that we should be checking this */
3194 ps
= &p
->po_partner_state
;
3195 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3198 ps_li
= &ps
->ps_lag_info
;
3199 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3200 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3201 sizeof(ps_li
->li_system
)) == 0) {
3202 if (g_bond
->verbose
) {
3203 timestamp_printf("System " EA_FORMAT
3204 " Port 0x%x moved from %s to %s\n",
3205 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3206 bondport_get_name(p
),
3207 bondport_get_name(rx_port
));
3217 ** LACP ifbond, LAG routines
3221 ifbond_selection(ifbond_ref bond
)
3223 int all_ports_ready
= 0;
3224 int active_media
= 0;
3226 int lag_changed
= 0;
3230 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3231 if (lag
!= bond
->ifb_active_lag
) {
3232 if (bond
->ifb_active_lag
!= NULL
) {
3233 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3234 bond
->ifb_active_lag
= NULL
;
3236 bond
->ifb_active_lag
= lag
;
3238 ifbond_activate_LAG(bond
, lag
, active_media
);
3242 else if (lag
!= NULL
) {
3243 if (lag
->lag_active_media
!= active_media
) {
3244 if (g_bond
->verbose
) {
3245 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3246 link_speed(lag
->lag_active_media
),
3247 link_speed(active_media
));
3249 ifbond_deactivate_LAG(bond
, lag
);
3250 ifbond_activate_LAG(bond
, lag
, active_media
);
3255 port_speed
= link_speed(active_media
);
3256 all_ports_ready
= ifbond_all_ports_ready(bond
);
3258 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3259 if (lag
!= NULL
&& p
->po_lag
== lag
3260 && media_speed(&p
->po_media_info
) == port_speed
3261 && (p
->po_mux_state
== MuxState_DETACHED
3262 || p
->po_selected
== SelectedState_SELECTED
3263 || p
->po_selected
== SelectedState_STANDBY
)
3264 && bondport_aggregatable(p
)) {
3265 if (bond
->ifb_max_active
> 0) {
3266 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3267 if (p
->po_selected
== SelectedState_STANDBY
3268 || p
->po_selected
== SelectedState_UNSELECTED
) {
3269 bondport_set_selected(p
, SelectedState_SELECTED
);
3272 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3273 bondport_set_selected(p
, SelectedState_STANDBY
);
3277 bondport_set_selected(p
, SelectedState_SELECTED
);
3280 if (bondport_flags_selected_changed(p
)) {
3281 bondport_flags_clear_selected_changed(p
);
3282 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3285 && bondport_flags_ready(p
)
3286 && p
->po_mux_state
== MuxState_WAITING
) {
3287 bondport_mux_machine(p
, LAEventReady
, NULL
);
3289 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3291 return (lag_changed
);
3295 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3297 int best_active
= 0;
3298 LAG_ref best_lag
= NULL
;
3303 if (bond
->ifb_active_lag
!= NULL
) {
3304 best_lag
= bond
->ifb_active_lag
;
3305 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3306 if (bond
->ifb_max_active
> 0
3307 && best_count
> bond
->ifb_max_active
) {
3308 best_count
= bond
->ifb_max_active
;
3310 best_speed
= link_speed(best_active
);
3312 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3317 if (lag
== bond
->ifb_active_lag
) {
3318 /* we've already computed it */
3321 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3325 if (bond
->ifb_max_active
> 0
3326 && count
> bond
->ifb_max_active
) {
3327 /* if there's a limit, don't count extra links */
3328 count
= bond
->ifb_max_active
;
3330 speed
= link_speed(active
);
3331 if ((count
* speed
) > (best_count
* best_speed
)) {
3334 best_active
= active
;
3338 if (best_count
== 0) {
3341 *active_media
= best_active
;
3346 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3350 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3351 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3357 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3362 if (bond
->ifb_max_active
> 0) {
3363 need
= bond
->ifb_max_active
;
3365 lag
->lag_active_media
= active_media
;
3366 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3367 if (bondport_aggregatable(p
) == 0) {
3368 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3370 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3371 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3373 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3374 if (bond
->ifb_max_active
> 0) {
3376 bondport_set_selected(p
, SelectedState_SELECTED
);
3380 bondport_set_selected(p
, SelectedState_STANDBY
);
3384 bondport_set_selected(p
, SelectedState_SELECTED
);
3388 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3396 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3398 LAG_ref lag
= bond
->ifb_active_lag
;
3400 bond
->ifb_max_active
= max_active
;
3401 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3404 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3408 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3409 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3410 if (p
->po_selected
== SelectedState_SELECTED
) {
3411 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3413 if (remove_count
== 0) {
3424 ifbond_all_ports_ready(ifbond_ref bond
)
3429 if (bond
->ifb_active_lag
== NULL
) {
3432 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3433 if (p
->po_mux_state
== MuxState_WAITING
3434 && p
->po_selected
== SelectedState_SELECTED
) {
3435 if (bondport_flags_ready(p
) == 0) {
3439 /* note that there was at least one ready port */
3446 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3450 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3451 if (this_port
== p
) {
3454 if (bondport_flags_mux_attached(p
) == 0) {
3462 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3466 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3467 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3468 sizeof(lag
->lag_info
)) == 0) {
3476 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3486 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3487 if (bondport_aggregatable(p
)) {
3490 this_speed
= media_speed(&p
->po_media_info
);
3491 if (this_speed
== 0) {
3494 if (this_speed
> speed
) {
3495 active
= p
->po_media_info
.mi_active
;
3499 else if (this_speed
== speed
) {
3504 *active_media
= active
;
3510 ** LACP bondport routines
3513 bondport_link_status_changed(bondport_ref p
)
3515 ifbond_ref bond
= p
->po_bond
;
3517 if (g_bond
->verbose
) {
3518 if (media_active(&p
->po_media_info
)) {
3519 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3520 bondport_get_name(p
),
3521 media_speed(&p
->po_media_info
),
3522 media_full_duplex(&p
->po_media_info
)
3526 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3529 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3530 if (media_active(&p
->po_media_info
)
3531 && bond
->ifb_active_lag
!= NULL
3532 && p
->po_lag
== bond
->ifb_active_lag
3533 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3534 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3535 if (g_bond
->verbose
) {
3536 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3537 bondport_get_name(p
),
3538 media_speed(&p
->po_media_info
),
3539 link_speed(p
->po_lag
->lag_active_media
));
3541 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3544 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3545 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3546 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3549 if (media_active(&p
->po_media_info
)) {
3550 bondport_enable_distributing(p
);
3553 bondport_disable_distributing(p
);
3560 bondport_aggregatable(bondport_ref p
)
3562 partner_state_ref ps
= &p
->po_partner_state
;
3564 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3565 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3566 /* we and/or our partner are individual */
3569 if (p
->po_lag
== NULL
) {
3572 switch (p
->po_receive_state
) {
3574 if (g_bond
->verbose
) {
3575 timestamp_printf("[%s] Port is not selectable\n",
3576 bondport_get_name(p
));
3579 case ReceiveState_CURRENT
:
3580 case ReceiveState_EXPIRED
:
3587 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3589 LAG_info_ref lag_li
;
3590 partner_state_ref ps
;
3593 ps
= &p
->po_partner_state
;
3594 ps_li
= &ps
->ps_lag_info
;
3595 lag_li
= &lag
->lag_info
;
3596 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3597 && ps_li
->li_key
== lag_li
->li_key
3598 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3599 sizeof(lag_li
->li_system
))
3607 bondport_remove_from_LAG(bondport_ref p
)
3610 ifbond_ref bond
= p
->po_bond
;
3611 LAG_ref lag
= p
->po_lag
;
3616 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3617 if (g_bond
->verbose
) {
3618 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3620 bondport_get_name(p
),
3621 lag
->lag_info
.li_system_priority
,
3622 EA_LIST(&lag
->lag_info
.li_system
),
3623 lag
->lag_info
.li_key
);
3626 lag
->lag_port_count
--;
3627 if (lag
->lag_port_count
> 0) {
3628 return (bond
->ifb_active_lag
== lag
);
3630 if (g_bond
->verbose
) {
3631 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3634 lag
->lag_info
.li_system_priority
,
3635 EA_LIST(&lag
->lag_info
.li_system
),
3636 lag
->lag_info
.li_key
);
3638 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3639 if (bond
->ifb_active_lag
== lag
) {
3640 bond
->ifb_active_lag
= NULL
;
3644 return (active_lag
);
3648 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3650 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3652 lag
->lag_port_count
++;
3653 if (g_bond
->verbose
) {
3654 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3655 bondport_get_name(p
),
3656 lag
->lag_info
.li_system_priority
,
3657 EA_LIST(&lag
->lag_info
.li_system
),
3658 lag
->lag_info
.li_key
);
3664 bondport_assign_to_LAG(bondport_ref p
)
3666 ifbond_ref bond
= p
->po_bond
;
3669 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3670 bondport_remove_from_LAG(p
);
3675 if (bondport_matches_LAG(p
, lag
)) {
3679 bondport_remove_from_LAG(p
);
3681 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3683 bondport_add_to_LAG(p
, lag
);
3686 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3687 TAILQ_INIT(&lag
->lag_port_list
);
3688 lag
->lag_port_count
= 0;
3689 lag
->lag_selected_port_count
= 0;
3690 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3691 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3692 if (g_bond
->verbose
) {
3693 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3696 lag
->lag_info
.li_system_priority
,
3697 EA_LIST(&lag
->lag_info
.li_system
),
3698 lag
->lag_info
.li_key
);
3700 bondport_add_to_LAG(p
, lag
);
3705 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3707 bondport_ref moved_port
;
3710 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3711 &in_lacpdu_p
->la_actor_tlv
);
3712 if (moved_port
!= NULL
) {
3713 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3715 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3716 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3717 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3722 bondport_set_selected(bondport_ref p
, SelectedState s
)
3724 if (s
!= p
->po_selected
) {
3725 ifbond_ref bond
= p
->po_bond
;
3726 LAG_ref lag
= p
->po_lag
;
3728 bondport_flags_set_selected_changed(p
);
3729 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3730 if (p
->po_selected
== SelectedState_SELECTED
) {
3731 lag
->lag_selected_port_count
--;
3733 else if (s
== SelectedState_SELECTED
) {
3734 lag
->lag_selected_port_count
++;
3736 if (g_bond
->verbose
) {
3737 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3738 bondport_get_name(p
),
3739 SelectedStateString(s
),
3740 SelectedStateString(p
->po_selected
));
3753 bondport_UpdateDefaultSelected(bondport_ref p
)
3755 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3760 bondport_RecordDefault(bondport_ref p
)
3762 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3764 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3765 bondport_assign_to_LAG(p
);
3770 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3772 lacp_actor_partner_tlv_ref actor
;
3773 partner_state_ref ps
;
3776 /* compare the PDU's Actor information to our Partner state */
3777 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3778 ps
= &p
->po_partner_state
;
3779 ps_li
= &ps
->ps_lag_info
;
3780 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3781 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3782 != ps
->ps_port_priority
)
3783 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3784 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3785 != ps_li
->li_system_priority
)
3786 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3787 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3788 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3789 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3790 if (g_bond
->verbose
) {
3791 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3792 bondport_get_name(p
));
3799 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3801 lacp_actor_partner_tlv_ref actor
;
3802 ifbond_ref bond
= p
->po_bond
;
3803 int lacp_maintain
= 0;
3804 partner_state_ref ps
;
3805 lacp_actor_partner_tlv_ref partner
;
3808 /* copy the PDU's Actor information into our Partner state */
3809 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3810 ps
= &p
->po_partner_state
;
3811 ps_li
= &ps
->ps_lag_info
;
3812 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3813 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3814 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3815 ps_li
->li_system_priority
3816 = lacp_actor_partner_tlv_get_system_priority(actor
);
3817 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3818 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3820 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3822 /* compare the PDU's Partner information to our own information */
3823 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3825 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3826 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3827 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3828 if (g_bond
->verbose
) {
3829 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3830 bondport_get_name(p
));
3834 if ((lacp_actor_partner_tlv_get_port(partner
)
3835 == bondport_get_index(p
))
3836 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3837 && bcmp(partner
->lap_system
, &g_bond
->system
,
3838 sizeof(g_bond
->system
)) == 0
3839 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3840 == g_bond
->system_priority
)
3841 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3842 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3843 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3844 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3846 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3847 if (g_bond
->verbose
) {
3848 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3849 bondport_get_name(p
));
3852 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3853 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3855 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3856 if (g_bond
->verbose
) {
3857 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3858 bondport_get_name(p
));
3861 bondport_assign_to_LAG(p
);
3865 static __inline__ lacp_actor_partner_state
3866 updateNTTBits(lacp_actor_partner_state s
)
3868 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3869 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3870 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3871 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3875 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3877 ifbond_ref bond
= p
->po_bond
;
3878 lacp_actor_partner_tlv_ref partner
;
3880 /* compare the PDU's Actor information to our Partner state */
3881 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3882 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3883 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3884 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3885 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3886 != g_bond
->system_priority
)
3887 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3888 || (updateNTTBits(partner
->lap_state
)
3889 != updateNTTBits(p
->po_actor_state
))) {
3890 bondport_flags_set_ntt(p
);
3891 if (g_bond
->verbose
) {
3892 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3893 bondport_get_name(p
));
3900 bondport_AttachMuxToAggregator(bondport_ref p
)
3902 if (bondport_flags_mux_attached(p
) == 0) {
3903 if (g_bond
->verbose
) {
3904 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3905 bondport_get_name(p
));
3907 bondport_flags_set_mux_attached(p
);
3913 bondport_DetachMuxFromAggregator(bondport_ref p
)
3915 if (bondport_flags_mux_attached(p
)) {
3916 if (g_bond
->verbose
) {
3917 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3918 bondport_get_name(p
));
3920 bondport_flags_clear_mux_attached(p
);
3926 bondport_enable_distributing(bondport_ref p
)
3928 if (bondport_flags_distributing(p
) == 0) {
3929 ifbond_ref bond
= p
->po_bond
;
3931 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3932 if (g_bond
->verbose
) {
3933 timestamp_printf("[%s] Distribution Enabled\n",
3934 bondport_get_name(p
));
3936 bondport_flags_set_distributing(p
);
3942 bondport_disable_distributing(bondport_ref p
)
3944 if (bondport_flags_distributing(p
)) {
3945 bondport_ref
* array
;
3951 array
= bond
->ifb_distributing_array
;
3952 count
= bond
->ifb_distributing_count
;
3953 for (i
= 0; i
< count
; i
++) {
3954 if (array
[i
] == p
) {
3957 for (j
= i
; j
< (count
- 1); j
++) {
3958 array
[j
] = array
[j
+ 1];
3963 bond
->ifb_distributing_count
--;
3964 if (g_bond
->verbose
) {
3965 timestamp_printf("[%s] Distribution Disabled\n",
3966 bondport_get_name(p
));
3968 bondport_flags_clear_distributing(p
);
3974 ** Receive machine functions
3977 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3980 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3983 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3986 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3989 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3992 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3996 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
3999 switch (p
->po_receive_state
) {
4000 case ReceiveState_none
:
4001 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4003 case ReceiveState_INITIALIZE
:
4004 bondport_receive_machine_initialize(p
, event
, event_data
);
4006 case ReceiveState_PORT_DISABLED
:
4007 bondport_receive_machine_port_disabled(p
, event
, event_data
);
4009 case ReceiveState_EXPIRED
:
4010 bondport_receive_machine_expired(p
, event
, event_data
);
4012 case ReceiveState_LACP_DISABLED
:
4013 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
4015 case ReceiveState_DEFAULTED
:
4016 bondport_receive_machine_defaulted(p
, event
, event_data
);
4018 case ReceiveState_CURRENT
:
4019 bondport_receive_machine_current(p
, event
, event_data
);
4028 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4033 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4034 bondport_receive_machine_current(p
, event
, event_data
);
4037 case LAEventMediaChange
:
4038 if (media_active(&p
->po_media_info
)) {
4039 switch (p
->po_receive_state
) {
4040 case ReceiveState_PORT_DISABLED
:
4041 case ReceiveState_LACP_DISABLED
:
4042 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4049 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4053 bondport_receive_machine_event(p
, event
, event_data
);
4060 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4061 __unused
void * event_data
)
4065 devtimer_cancel(p
->po_current_while_timer
);
4066 if (g_bond
->verbose
) {
4067 timestamp_printf("[%s] Receive INITIALIZE\n",
4068 bondport_get_name(p
));
4070 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4071 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4072 bondport_RecordDefault(p
);
4074 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4075 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4084 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4085 __unused
void * event_data
)
4087 partner_state_ref ps
;
4091 devtimer_cancel(p
->po_current_while_timer
);
4092 if (g_bond
->verbose
) {
4093 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4094 bondport_get_name(p
));
4096 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4097 ps
= &p
->po_partner_state
;
4098 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4100 case LAEventMediaChange
:
4101 if (media_active(&p
->po_media_info
)) {
4102 if (media_full_duplex(&p
->po_media_info
)) {
4103 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4106 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4109 else if (p
->po_selected
== SelectedState_SELECTED
) {
4112 if (g_bond
->verbose
) {
4113 timestamp_printf("[%s] Receive PORT_DISABLED: "
4114 "link timer started\n",
4115 bondport_get_name(p
));
4119 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4120 (devtimer_timeout_func
)
4121 bondport_receive_machine_port_disabled
,
4122 (void *)LAEventTimeout
, NULL
);
4124 else if (p
->po_selected
== SelectedState_STANDBY
) {
4125 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4128 case LAEventTimeout
:
4129 if (p
->po_selected
== SelectedState_SELECTED
) {
4130 if (g_bond
->verbose
) {
4131 timestamp_printf("[%s] Receive PORT_DISABLED: "
4132 "link timer completed, marking UNSELECTED\n",
4133 bondport_get_name(p
));
4135 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4138 case LAEventPortMoved
:
4139 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4148 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4149 __unused
void * event_data
)
4151 lacp_actor_partner_state s
;
4156 devtimer_cancel(p
->po_current_while_timer
);
4157 if (g_bond
->verbose
) {
4158 timestamp_printf("[%s] Receive EXPIRED\n",
4159 bondport_get_name(p
));
4161 p
->po_receive_state
= ReceiveState_EXPIRED
;
4162 s
= p
->po_partner_state
.ps_state
;
4163 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4164 s
= lacp_actor_partner_state_set_short_timeout(s
);
4165 p
->po_partner_state
.ps_state
= s
;
4167 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4168 /* start current_while timer */
4169 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4171 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4172 (devtimer_timeout_func
)
4173 bondport_receive_machine_expired
,
4174 (void *)LAEventTimeout
, NULL
);
4177 case LAEventTimeout
:
4178 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4187 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4188 __unused
void * event_data
)
4190 partner_state_ref ps
;
4193 devtimer_cancel(p
->po_current_while_timer
);
4194 if (g_bond
->verbose
) {
4195 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4196 bondport_get_name(p
));
4198 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4199 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4200 bondport_RecordDefault(p
);
4201 ps
= &p
->po_partner_state
;
4202 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4204 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4213 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4214 __unused
void * event_data
)
4218 devtimer_cancel(p
->po_current_while_timer
);
4219 if (g_bond
->verbose
) {
4220 timestamp_printf("[%s] Receive DEFAULTED\n",
4221 bondport_get_name(p
));
4223 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4224 bondport_UpdateDefaultSelected(p
);
4225 bondport_RecordDefault(p
);
4227 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4236 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4239 partner_state_ref ps
;
4244 devtimer_cancel(p
->po_current_while_timer
);
4245 if (g_bond
->verbose
) {
4246 timestamp_printf("[%s] Receive CURRENT\n",
4247 bondport_get_name(p
));
4249 p
->po_receive_state
= ReceiveState_CURRENT
;
4250 bondport_UpdateSelected(p
, event_data
);
4251 bondport_UpdateNTT(p
, event_data
);
4252 bondport_RecordPDU(p
, event_data
);
4254 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4255 bondport_assign_to_LAG(p
);
4256 /* start current_while timer */
4257 ps
= &p
->po_partner_state
;
4258 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4259 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4262 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4265 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4266 (devtimer_timeout_func
)
4267 bondport_receive_machine_current
,
4268 (void *)LAEventTimeout
, NULL
);
4270 case LAEventTimeout
:
4271 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4280 ** Periodic Transmission machine
4284 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4285 __unused
void * event_data
)
4288 partner_state_ref ps
;
4293 if (g_bond
->verbose
) {
4294 timestamp_printf("[%s] periodic_transmit Start\n",
4295 bondport_get_name(p
));
4298 case LAEventMediaChange
:
4299 devtimer_cancel(p
->po_periodic_timer
);
4300 p
->po_periodic_interval
= 0;
4301 if (media_active(&p
->po_media_info
) == 0
4302 || media_full_duplex(&p
->po_media_info
) == 0) {
4306 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4307 ps
= &p
->po_partner_state
;
4308 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4309 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4311 devtimer_cancel(p
->po_periodic_timer
);
4312 p
->po_periodic_interval
= 0;
4315 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4316 interval
= LACP_FAST_PERIODIC_TIME
;
4319 interval
= LACP_SLOW_PERIODIC_TIME
;
4321 if (p
->po_periodic_interval
!= interval
) {
4322 if (interval
== LACP_FAST_PERIODIC_TIME
4323 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4324 if (g_bond
->verbose
) {
4325 timestamp_printf("[%s] periodic_transmit:"
4326 " Need To Transmit\n",
4327 bondport_get_name(p
));
4329 bondport_flags_set_ntt(p
);
4331 p
->po_periodic_interval
= interval
;
4333 tv
.tv_sec
= interval
;
4334 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4335 (devtimer_timeout_func
)
4336 bondport_periodic_transmit_machine
,
4337 (void *)LAEventTimeout
, NULL
);
4338 if (g_bond
->verbose
) {
4339 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4340 bondport_get_name(p
),
4341 p
->po_periodic_interval
);
4345 case LAEventTimeout
:
4346 bondport_flags_set_ntt(p
);
4347 tv
.tv_sec
= p
->po_periodic_interval
;
4349 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4350 bondport_periodic_transmit_machine
,
4351 (void *)LAEventTimeout
, NULL
);
4352 if (g_bond
->verbose
> 1) {
4353 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4354 bondport_get_name(p
), p
->po_periodic_interval
);
4367 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4368 __darwin_time_t
* next_secs
)
4370 if (p
->po_last_transmit_secs
!= current_secs
) {
4371 p
->po_last_transmit_secs
= current_secs
;
4372 p
->po_n_transmit
= 0;
4374 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4378 if (next_secs
!= NULL
) {
4379 *next_secs
= current_secs
+ 1;
4385 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4388 lacp_actor_partner_tlv_ref aptlv
;
4389 lacp_collector_tlv_ref ctlv
;
4390 struct timeval next_tick_time
= {0, 0};
4391 lacpdu_ref out_lacpdu_p
;
4392 packet_buffer_ref pkt
;
4393 partner_state_ref ps
;
4397 case LAEventTimeout
:
4399 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4402 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4403 /* we're going away, transmit the packet no matter what */
4405 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4406 &next_tick_time
.tv_sec
) == 0) {
4407 if (devtimer_enabled(p
->po_transmit_timer
)) {
4408 if (g_bond
->verbose
> 0) {
4409 timestamp_printf("[%s] Transmit Timer Already Set\n",
4410 bondport_get_name(p
));
4414 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4415 (devtimer_timeout_func
)
4416 bondport_transmit_machine
,
4417 (void *)LAEventTimeout
, NULL
);
4418 if (g_bond
->verbose
> 0) {
4419 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4420 bondport_get_name(p
),
4421 (int)next_tick_time
.tv_sec
);
4426 if (g_bond
->verbose
> 0) {
4427 if (event
== LAEventTimeout
) {
4428 timestamp_printf("[%s] Transmit Timer Complete\n",
4429 bondport_get_name(p
));
4432 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4434 printf("[%s] Transmit: failed to allocate packet buffer\n",
4435 bondport_get_name(p
));
4438 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4439 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4440 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4441 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4444 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4445 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4446 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4447 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4448 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4449 g_bond
->system_priority
);
4450 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4451 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4452 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4453 aptlv
->lap_state
= p
->po_actor_state
;
4456 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4457 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4458 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4459 ps
= &p
->po_partner_state
;
4460 ps_li
= &ps
->ps_lag_info
;
4461 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4462 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4463 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4464 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4465 ps_li
->li_system_priority
);
4466 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4467 aptlv
->lap_state
= ps
->ps_state
;
4470 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4471 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4472 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4474 bondport_slow_proto_transmit(p
, pkt
);
4475 bondport_flags_clear_ntt(p
);
4476 if (g_bond
->verbose
> 0) {
4477 timestamp_printf("[%s] Transmit Packet %d\n",
4478 bondport_get_name(p
), p
->po_n_transmit
);
4488 ** Mux machine functions
4492 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4495 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4498 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4502 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4506 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4508 switch (p
->po_mux_state
) {
4510 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4512 case MuxState_DETACHED
:
4513 bondport_mux_machine_detached(p
, event
, event_data
);
4515 case MuxState_WAITING
:
4516 bondport_mux_machine_waiting(p
, event
, event_data
);
4518 case MuxState_ATTACHED
:
4519 bondport_mux_machine_attached(p
, event
, event_data
);
4521 case MuxState_COLLECTING_DISTRIBUTING
:
4522 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4531 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4532 __unused
void * event_data
)
4534 lacp_actor_partner_state s
;
4538 devtimer_cancel(p
->po_wait_while_timer
);
4539 if (g_bond
->verbose
) {
4540 timestamp_printf("[%s] Mux DETACHED\n",
4541 bondport_get_name(p
));
4543 p
->po_mux_state
= MuxState_DETACHED
;
4544 bondport_flags_clear_ready(p
);
4545 bondport_DetachMuxFromAggregator(p
);
4546 bondport_disable_distributing(p
);
4547 s
= p
->po_actor_state
;
4548 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4549 s
= lacp_actor_partner_state_set_not_collecting(s
);
4550 s
= lacp_actor_partner_state_set_not_distributing(s
);
4551 p
->po_actor_state
= s
;
4552 bondport_flags_set_ntt(p
);
4554 case LAEventSelectedChange
:
4556 case LAEventMediaChange
:
4557 if (p
->po_selected
== SelectedState_SELECTED
4558 || p
->po_selected
== SelectedState_STANDBY
) {
4559 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4569 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4570 __unused
void * event_data
)
4576 devtimer_cancel(p
->po_wait_while_timer
);
4577 if (g_bond
->verbose
) {
4578 timestamp_printf("[%s] Mux WAITING\n",
4579 bondport_get_name(p
));
4581 p
->po_mux_state
= MuxState_WAITING
;
4584 case LAEventSelectedChange
:
4585 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4586 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4589 if (p
->po_selected
== SelectedState_STANDBY
) {
4590 devtimer_cancel(p
->po_wait_while_timer
);
4591 /* wait until state changes to SELECTED */
4592 if (g_bond
->verbose
) {
4593 timestamp_printf("[%s] Mux WAITING: Standby\n",
4594 bondport_get_name(p
));
4598 if (bondport_flags_ready(p
)) {
4599 if (g_bond
->verbose
) {
4600 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4601 bondport_get_name(p
));
4605 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4606 if (g_bond
->verbose
) {
4607 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4608 bondport_get_name(p
));
4612 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4613 devtimer_cancel(p
->po_wait_while_timer
);
4614 if (g_bond
->verbose
) {
4615 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4616 bondport_get_name(p
));
4618 bondport_flags_set_ready(p
);
4621 if (g_bond
->verbose
) {
4622 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4623 bondport_get_name(p
));
4625 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4627 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4628 (devtimer_timeout_func
)
4629 bondport_mux_machine_waiting
,
4630 (void *)LAEventTimeout
, NULL
);
4632 case LAEventTimeout
:
4633 if (g_bond
->verbose
) {
4634 timestamp_printf("[%s] Mux WAITING: Ready\n",
4635 bondport_get_name(p
));
4637 bondport_flags_set_ready(p
);
4641 if (bondport_flags_ready(p
)){
4642 if (g_bond
->verbose
) {
4643 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4644 bondport_get_name(p
));
4646 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4655 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4656 __unused
void * event_data
)
4658 lacp_actor_partner_state s
;
4662 devtimer_cancel(p
->po_wait_while_timer
);
4663 if (g_bond
->verbose
) {
4664 timestamp_printf("[%s] Mux ATTACHED\n",
4665 bondport_get_name(p
));
4667 p
->po_mux_state
= MuxState_ATTACHED
;
4668 bondport_AttachMuxToAggregator(p
);
4669 s
= p
->po_actor_state
;
4670 s
= lacp_actor_partner_state_set_in_sync(s
);
4671 s
= lacp_actor_partner_state_set_not_collecting(s
);
4672 s
= lacp_actor_partner_state_set_not_distributing(s
);
4673 bondport_disable_distributing(p
);
4674 p
->po_actor_state
= s
;
4675 bondport_flags_set_ntt(p
);
4678 switch (p
->po_selected
) {
4679 case SelectedState_SELECTED
:
4680 s
= p
->po_partner_state
.ps_state
;
4681 if (lacp_actor_partner_state_in_sync(s
)) {
4682 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4687 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4696 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4698 __unused
void * event_data
)
4700 lacp_actor_partner_state s
;
4704 devtimer_cancel(p
->po_wait_while_timer
);
4705 if (g_bond
->verbose
) {
4706 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4707 bondport_get_name(p
));
4709 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4710 bondport_enable_distributing(p
);
4711 s
= p
->po_actor_state
;
4712 s
= lacp_actor_partner_state_set_collecting(s
);
4713 s
= lacp_actor_partner_state_set_distributing(s
);
4714 p
->po_actor_state
= s
;
4715 bondport_flags_set_ntt(p
);
4718 s
= p
->po_partner_state
.ps_state
;
4719 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4720 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4723 switch (p
->po_selected
) {
4724 case SelectedState_UNSELECTED
:
4725 case SelectedState_STANDBY
:
4726 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);