2 * Copyright (c) 2004-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * - bond/failover interface
32 * - implements IEEE 802.3ad Link Aggregation
36 * Modification History:
38 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/kern_event.h>
54 #include <net/ethernet.h>
56 #include <net/kpi_interface.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_ether.h>
60 #include <net/if_types.h>
61 #include <net/if_bond_var.h>
62 #include <net/ieee8023ad.h>
66 #include <net/devtimer.h>
67 #include <net/if_vlan_var.h>
68 #include <net/kpi_protocol.h>
70 #include <kern/locks.h>
71 #include <libkern/OSAtomic.h>
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
79 #include <net/if_media.h>
80 #include <net/multicast_list.h>
82 static struct ether_addr slow_proto_multicast
= {
83 IEEE8023AD_SLOW_PROTO_MULTICAST
86 #define BOND_MAXUNIT 128
87 #define BONDNAME "bond"
88 #define M_BOND M_DEVBUF
90 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
91 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
92 #define EA_LIST(ea) EA_CH(ea,0),EA_CH(ea,1),EA_CH(ea,2),EA_CH(ea,3),EA_CH(ea,4),EA_CH(ea,5)
94 #define timestamp_printf printf
99 static __inline__ lck_grp_t
*
100 my_lck_grp_alloc_init(const char * grp_name
)
103 lck_grp_attr_t
* grp_attrs
;
105 grp_attrs
= lck_grp_attr_alloc_init();
106 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
107 lck_grp_attr_free(grp_attrs
);
111 static __inline__ lck_mtx_t
*
112 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
114 lck_attr_t
* lck_attrs
;
117 lck_attrs
= lck_attr_alloc_init();
118 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
119 lck_attr_free(lck_attrs
);
123 static lck_mtx_t
* bond_lck_mtx
;
125 static __inline__
void
128 lck_grp_t
* bond_lck_grp
;
130 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
131 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
134 static __inline__
void
135 bond_assert_lock_held(void)
137 LCK_MTX_ASSERT(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
141 static __inline__
void
142 bond_assert_lock_not_held(void)
144 LCK_MTX_ASSERT(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
148 static __inline__
void
151 lck_mtx_lock(bond_lck_mtx
);
155 static __inline__
void
158 lck_mtx_unlock(bond_lck_mtx
);
163 ** bond structures, types
167 lacp_system li_system
;
168 lacp_system_priority li_system_priority
;
171 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
174 TAILQ_HEAD(port_list
, bondport_s
);
176 TAILQ_HEAD(ifbond_list
, ifbond_s
);
178 TAILQ_HEAD(lag_list
, LAG_s
);
180 typedef struct ifbond_s ifbond
, * ifbond_ref
;
181 typedef struct bondport_s bondport
, * bondport_ref
;
184 TAILQ_ENTRY(LAG_s
) lag_list
;
185 struct port_list lag_port_list
;
186 short lag_port_count
;
187 short lag_selected_port_count
;
188 int lag_active_media
;
191 typedef struct LAG_s LAG
, * LAG_ref
;
193 typedef struct partner_state_s
{
194 LAG_info ps_lag_info
;
196 lacp_port_priority ps_port_priority
;
197 lacp_actor_partner_state ps_state
;
198 } partner_state
, * partner_state_ref
;
201 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
203 SInt32 ifb_retain_count
;
204 char ifb_name
[IFNAMSIZ
];
205 struct ifnet
* ifb_ifp
;
206 bpf_packet_func ifb_bpf_input
;
207 bpf_packet_func ifb_bpf_output
;
209 struct port_list ifb_port_list
;
210 short ifb_port_count
;
211 struct lag_list ifb_lag_list
;
213 short ifb_max_active
; /* 0 == unlimited */
214 LAG_ref ifb_active_lag
;
215 struct ifmultiaddr
* ifb_ifma_slow_proto
;
216 bondport_ref
* ifb_distributing_array
;
217 int ifb_distributing_count
;
218 int ifb_last_link_event
;
219 int ifb_mode
; /* LACP, STATIC */
228 ReceiveState_none
= 0,
229 ReceiveState_INITIALIZE
= 1,
230 ReceiveState_PORT_DISABLED
= 2,
231 ReceiveState_EXPIRED
= 3,
232 ReceiveState_LACP_DISABLED
= 4,
233 ReceiveState_DEFAULTED
= 5,
234 ReceiveState_CURRENT
= 6,
237 typedef u_char ReceiveState
;
240 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
241 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
242 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
244 typedef u_char SelectedState
;
246 static __inline__
const char *
247 SelectedStateString(SelectedState s
)
249 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
251 if (s
<= SelectedState_STANDBY
) {
254 return ("<unknown>");
259 MuxState_DETACHED
= 1,
260 MuxState_WAITING
= 2,
261 MuxState_ATTACHED
= 3,
262 MuxState_COLLECTING_DISTRIBUTING
= 4,
265 typedef u_char MuxState
;
268 TAILQ_ENTRY(bondport_s
) po_port_list
;
270 struct multicast_list po_multicast
;
271 struct ifnet
* po_ifp
;
272 struct ether_addr po_saved_addr
;
274 char po_name
[IFNAMSIZ
];
275 struct ifdevmtu po_devmtu
;
278 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
279 devtimer_ref po_current_while_timer
;
280 devtimer_ref po_periodic_timer
;
281 devtimer_ref po_wait_while_timer
;
282 devtimer_ref po_transmit_timer
;
283 partner_state po_partner_state
;
284 lacp_port_priority po_priority
;
285 lacp_actor_partner_state po_actor_state
;
287 u_char po_periodic_interval
;
288 u_char po_n_transmit
;
289 ReceiveState po_receive_state
;
290 MuxState po_mux_state
;
291 SelectedState po_selected
;
292 int32_t po_last_transmit_secs
;
293 struct media_info po_media_info
;
297 #define IFBF_PROMISC 0x1 /* promiscuous mode */
298 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
299 #define IFBF_LLADDR 0x4 /* specific link address requested */
300 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
302 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
305 static __inline__
int
306 ifbond_flags_if_detaching(ifbond_ref ifb
)
308 return ((ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0);
311 static __inline__
void
312 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
314 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
318 static __inline__
int
319 ifbond_flags_lladdr(ifbond_ref ifb
)
321 return ((ifb
->ifb_flags
& IFBF_LLADDR
) != 0);
324 static __inline__
int
325 ifbond_flags_change_in_progress(ifbond_ref ifb
)
327 return ((ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0);
330 static __inline__
void
331 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
333 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
337 static __inline__
void
338 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
340 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
345 * bondport_ref->po_flags bits
347 #define BONDPORT_FLAGS_NTT 0x01
348 #define BONDPORT_FLAGS_READY 0x02
349 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
350 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
351 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
352 #define BONDPORT_FLAGS_UNUSED2 0x20
353 #define BONDPORT_FLAGS_UNUSED3 0x40
354 #define BONDPORT_FLAGS_UNUSED4 0x80
356 static __inline__
void
357 bondport_flags_set_ntt(bondport_ref p
)
359 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
363 static __inline__
void
364 bondport_flags_clear_ntt(bondport_ref p
)
366 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
370 static __inline__
int
371 bondport_flags_ntt(bondport_ref p
)
373 return ((p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0);
376 static __inline__
void
377 bondport_flags_set_ready(bondport_ref p
)
379 p
->po_flags
|= BONDPORT_FLAGS_READY
;
383 static __inline__
void
384 bondport_flags_clear_ready(bondport_ref p
)
386 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
390 static __inline__
int
391 bondport_flags_ready(bondport_ref p
)
393 return ((p
->po_flags
& BONDPORT_FLAGS_READY
) != 0);
396 static __inline__
void
397 bondport_flags_set_selected_changed(bondport_ref p
)
399 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
403 static __inline__
void
404 bondport_flags_clear_selected_changed(bondport_ref p
)
406 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
410 static __inline__
int
411 bondport_flags_selected_changed(bondport_ref p
)
413 return ((p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0);
416 static __inline__
void
417 bondport_flags_set_mux_attached(bondport_ref p
)
419 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
423 static __inline__
void
424 bondport_flags_clear_mux_attached(bondport_ref p
)
426 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
430 static __inline__
int
431 bondport_flags_mux_attached(bondport_ref p
)
433 return ((p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0);
436 static __inline__
void
437 bondport_flags_set_distributing(bondport_ref p
)
439 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
443 static __inline__
void
444 bondport_flags_clear_distributing(bondport_ref p
)
446 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
450 static __inline__
int
451 bondport_flags_distributing(bondport_ref p
)
453 return ((p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0);
456 typedef struct bond_globals_s
{
457 struct ifbond_list ifbond_list
;
459 lacp_system_priority system_priority
;
461 } * bond_globals_ref
;
463 static bond_globals_ref g_bond
;
466 ** packet_buffer routines
467 ** - thin wrapper for mbuf
470 typedef struct mbuf
* packet_buffer_ref
;
472 static packet_buffer_ref
473 packet_buffer_allocate(int length
)
478 /* leave room for ethernet header */
479 size
= length
+ sizeof(struct ether_header
);
480 if (size
> (int)MHLEN
) {
481 if (size
> (int)MCLBYTES
) {
482 printf("bond: packet_buffer_allocate size %d > max %u\n",
486 m
= m_getcl(M_WAITOK
, MT_DATA
, M_PKTHDR
);
488 m
= m_gethdr(M_WAITOK
, MT_DATA
);
494 m
->m_pkthdr
.len
= size
;
499 packet_buffer_byteptr(packet_buffer_ref buf
)
501 return (buf
->m_data
+ sizeof(struct ether_header
));
509 LAEventSelectedChange
,
518 bondport_receive_machine(bondport_ref p
, LAEvent event
,
521 ** Periodic Transmission machine
524 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
530 #define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
533 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
540 bondport_mux_machine(bondport_ref p
, LAEvent event
,
547 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
550 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
553 ifbond_all_ports_ready(ifbond_ref bond
);
556 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
559 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
562 ifbond_selection(ifbond_ref bond
);
570 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
573 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
576 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
577 int active
, int short_timeout
, int * error
);
579 bondport_start(bondport_ref p
);
582 bondport_free(bondport_ref p
);
585 bondport_aggregatable(bondport_ref p
);
588 bondport_remove_from_LAG(bondport_ref p
);
591 bondport_set_selected(bondport_ref p
, SelectedState s
);
594 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
597 bondport_link_status_changed(bondport_ref p
);
600 bondport_enable_distributing(bondport_ref p
);
603 bondport_disable_distributing(bondport_ref p
);
605 static __inline__
int
606 bondport_collecting(bondport_ref p
)
608 if (p
->po_bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
609 return (lacp_actor_partner_state_collecting(p
->po_actor_state
));
615 ** bond interface/dlil specific routines
617 static int bond_clone_create(struct if_clone
*, u_int32_t
, void *);
618 static int bond_clone_destroy(struct ifnet
*);
619 static int bond_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t m
,
621 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
622 static int bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * addr
);
623 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
624 bpf_packet_func func
);
625 static int bond_attach_protocol(struct ifnet
*ifp
);
626 static int bond_detach_protocol(struct ifnet
*ifp
);
627 static int bond_setmulti(struct ifnet
*ifp
);
628 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
629 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
630 static void bond_if_free(struct ifnet
* ifp
);
632 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
637 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
640 siocsifmtu(struct ifnet
* ifp
, int mtu
)
644 bzero(&ifr
, sizeof(ifr
));
646 return (ifnet_ioctl(ifp
, 0, SIOCSIFMTU
, &ifr
));
650 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
655 bzero(&ifr
, sizeof(ifr
));
656 error
= ifnet_ioctl(ifp
, 0, SIOCGIFDEVMTU
, &ifr
);
658 *ifdm_p
= ifr
.ifr_devmtu
;
663 static __inline__
void
664 ether_addr_copy(void * dest
, const void * source
)
666 bcopy(source
, dest
, ETHER_ADDR_LEN
);
670 static __inline__
void
671 ifbond_retain(ifbond_ref ifb
)
673 OSIncrementAtomic(&ifb
->ifb_retain_count
);
676 static __inline__
void
677 ifbond_release(ifbond_ref ifb
)
679 UInt32 old_retain_count
;
681 old_retain_count
= OSDecrementAtomic(&ifb
->ifb_retain_count
);
682 switch (old_retain_count
) {
684 panic("ifbond_release: retain count is 0\n");
687 if (g_bond
->verbose
) {
688 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
690 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
691 if (g_bond
->verbose
) {
692 printf("ifbond_release(%s) removing multicast\n",
695 (void) if_delmulti_anon(ifb
->ifb_ifma_slow_proto
->ifma_ifp
,
696 ifb
->ifb_ifma_slow_proto
->ifma_addr
);
697 IFMA_REMREF(ifb
->ifb_ifma_slow_proto
);
699 if (ifb
->ifb_distributing_array
!= NULL
) {
700 FREE(ifb
->ifb_distributing_array
, M_BOND
);
711 * Function: ifbond_wait
713 * Allows a single thread to gain exclusive access to the ifbond
714 * data structure. Some operations take a long time to complete,
715 * and some have side-effects that we can't predict. Holding the
716 * bond_lock() across such operations is not possible.
719 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
720 * complete. Simply holding the bond_lock() would freeze all other
721 * data structure accesses during that time.
722 * 2) When we attach our protocol to the interface, a dlil event is
723 * generated and invokes our bond_event() function. bond_event()
724 * needs to take the bond_lock(), but we're already holding it, so
725 * we're deadlocked against ourselves.
727 * Before calling, you must be holding the bond_lock and have taken
728 * a reference on the ifbond_ref.
731 ifbond_wait(ifbond_ref ifb
, const char * msg
)
735 /* other add/remove in progress */
736 while (ifbond_flags_change_in_progress(ifb
)) {
737 if (g_bond
->verbose
) {
738 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
741 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
743 /* prevent other bond list remove/add from taking place */
744 ifbond_flags_set_change_in_progress(ifb
);
745 if (g_bond
->verbose
&& waited
) {
746 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
752 * Function: ifbond_signal
754 * Allows the thread that previously invoked ifbond_wait() to
755 * give up exclusive access to the ifbond data structure, and wake up
756 * any other threads waiting to access
758 * Before calling, you must be holding the bond_lock and have taken
759 * a reference on the ifbond_ref.
762 ifbond_signal(ifbond_ref ifb
, const char * msg
)
764 ifbond_flags_clear_change_in_progress(ifb
);
765 wakeup((caddr_t
)ifb
);
766 if (g_bond
->verbose
) {
767 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
777 link_speed(int active
)
779 switch (IFM_SUBTYPE(active
)) {
800 /* assume that new defined types are going to be at least 10GigE */
811 static __inline__
int
812 media_active(const struct media_info
* mi
)
814 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
817 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
820 static __inline__
int
821 media_full_duplex(const struct media_info
* mi
)
823 return ((mi
->mi_active
& IFM_FDX
) != 0);
826 static __inline__
int
827 media_speed(const struct media_info
* mi
)
829 return (link_speed(mi
->mi_active
));
832 static struct media_info
833 interface_media_info(struct ifnet
* ifp
)
835 struct ifmediareq ifmr
;
836 struct media_info mi
;
838 bzero(&mi
, sizeof(mi
));
839 bzero(&ifmr
, sizeof(ifmr
));
840 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
841 if (ifmr
.ifm_count
!= 0) {
842 mi
.mi_status
= ifmr
.ifm_status
;
843 mi
.mi_active
= ifmr
.ifm_active
;
850 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
855 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
856 * currently expects it that way
858 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
859 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
860 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
861 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
867 static bond_globals_ref
868 bond_globals_create(lacp_system_priority sys_pri
,
873 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
| M_ZERO
);
877 TAILQ_INIT(&b
->ifbond_list
);
879 b
->system_priority
= sys_pri
;
884 bond_globals_init(void)
890 bond_assert_lock_not_held();
892 if (g_bond
!= NULL
) {
897 * use en0's ethernet address as the system identifier, and if it's not
898 * there, use en1 .. en3
901 for (i
= 0; i
< 4; i
++) {
902 char ifname
[IFNAMSIZ
+1];
903 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
904 ifp
= ifunit(ifname
);
911 b
= bond_globals_create(0x8000, (lacp_system_ref
)IF_LLADDR(ifp
));
914 if (g_bond
!= NULL
) {
931 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
932 const struct ether_header
* eh_p
,
933 u_int16_t vlan_tag
, bpf_packet_func func
)
935 struct ether_vlan_header
* vlh_p
;
938 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
942 /* populate a new mbuf containing the vlan ethernet header */
943 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
944 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
945 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
946 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
947 vlh_p
->evl_tag
= htons(vlan_tag
);
948 vlh_p
->evl_proto
= eh_p
->ether_type
;
956 static __inline__
void
957 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
958 bpf_packet_func func
)
961 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
962 const struct ether_header
* eh_p
;
963 eh_p
= mtod(m
, const struct ether_header
*);
964 m
->m_data
+= ETHER_HDR_LEN
;
965 m
->m_len
-= ETHER_HDR_LEN
;
966 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
967 m
->m_data
-= ETHER_HDR_LEN
;
968 m
->m_len
+= ETHER_HDR_LEN
;
976 static __inline__
void
977 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
978 bpf_packet_func func
)
981 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
982 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
984 /* restore the header */
985 m
->m_data
-= ETHER_HDR_LEN
;
986 m
->m_len
+= ETHER_HDR_LEN
;
988 m
->m_data
+= ETHER_HDR_LEN
;
989 m
->m_len
-= ETHER_HDR_LEN
;
996 * Function: bond_setmulti
998 * Enable multicast reception on "our" interface by enabling multicasts on
999 * each of the member ports.
1002 bond_setmulti(struct ifnet
* ifp
)
1010 ifb
= ifnet_softc(ifp
);
1011 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1012 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1017 ifbond_wait(ifb
, "bond_setmulti");
1019 if (ifbond_flags_if_detaching(ifb
)) {
1020 /* someone destroyed the bond while we were waiting */
1026 /* ifbond_wait() let's us safely walk the list without holding the lock */
1027 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1028 struct ifnet
* port_ifp
= p
->po_ifp
;
1030 error
= multicast_list_program(&p
->po_multicast
,
1033 printf("bond_setmulti(%s): "
1034 "multicast_list_program(%s%d) failed, %d\n",
1035 ifb
->ifb_name
, ifnet_name(port_ifp
),
1036 ifnet_unit(port_ifp
), error
);
1042 ifbond_signal(ifb
, "bond_setmulti");
1044 ifbond_release(ifb
);
1049 bond_clone_attach(void)
1053 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1060 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1063 struct ifmultiaddr
* ifma
= NULL
;
1064 struct sockaddr_dl sdl
;
1066 bond_assert_lock_not_held();
1068 bzero(&sdl
, sizeof(sdl
));
1069 sdl
.sdl_len
= sizeof(sdl
);
1070 sdl
.sdl_family
= AF_LINK
;
1071 sdl
.sdl_type
= IFT_ETHER
;
1073 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1074 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1075 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1077 ifb
->ifb_ifma_slow_proto
= ifma
;
1083 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1088 struct ifnet_init_eparams bond_init
;
1090 error
= bond_globals_init();
1095 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
| M_ZERO
);
1101 TAILQ_INIT(&ifb
->ifb_port_list
);
1102 TAILQ_INIT(&ifb
->ifb_lag_list
);
1103 ifb
->ifb_key
= unit
+ 1;
1105 /* use the interface name as the unique id for ifp recycle */
1106 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1107 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1108 ifbond_release(ifb
);
1112 bzero(&bond_init
, sizeof(bond_init
));
1113 bond_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1114 bond_init
.len
= sizeof (bond_init
);
1115 bond_init
.flags
= IFNET_INIT_LEGACY
;
1116 bond_init
.uniqueid
= ifb
->ifb_name
;
1117 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1118 bond_init
.name
= ifc
->ifc_name
;
1119 bond_init
.unit
= unit
;
1120 bond_init
.family
= IFNET_FAMILY_BOND
;
1121 bond_init
.type
= IFT_IEEE8023ADLAG
;
1122 bond_init
.output
= bond_output
;
1123 bond_init
.demux
= ether_demux
;
1124 bond_init
.add_proto
= ether_add_proto
;
1125 bond_init
.del_proto
= ether_del_proto
;
1126 bond_init
.check_multi
= ether_check_multi
;
1127 bond_init
.framer_extended
= ether_frameout_extended
;
1128 bond_init
.ioctl
= bond_ioctl
;
1129 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1130 bond_init
.detach
= bond_if_free
;
1131 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1132 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1133 bond_init
.softc
= ifb
;
1134 error
= ifnet_allocate_extended(&bond_init
, &ifp
);
1137 ifbond_release(ifb
);
1142 ifnet_set_offload(ifp
, 0);
1143 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1144 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1145 ifnet_set_baudrate(ifp
, 0);
1146 ifnet_set_mtu(ifp
, 0);
1148 error
= ifnet_attach(ifp
, NULL
);
1151 ifbond_release(ifb
);
1154 error
= ifbond_add_slow_proto_multicast(ifb
);
1156 printf("bond_clone_create(%s): "
1157 "failed to add slow_proto multicast, %d\n",
1158 ifb
->ifb_name
, error
);
1161 /* attach as ethernet */
1162 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1165 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1172 bond_remove_all_interfaces(ifbond_ref ifb
)
1176 bond_assert_lock_held();
1179 * do this in reverse order to avoid re-programming the mac address
1180 * as each head interface is removed
1182 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1183 bond_remove_interface(ifb
, p
->po_ifp
);
1189 bond_remove(ifbond_ref ifb
)
1191 bond_assert_lock_held();
1192 ifbond_flags_set_if_detaching(ifb
);
1193 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1194 bond_remove_all_interfaces(ifb
);
1199 bond_if_detach(struct ifnet
* ifp
)
1203 error
= ifnet_detach(ifp
);
1205 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1206 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1213 bond_clone_destroy(struct ifnet
* ifp
)
1218 ifb
= ifnet_softc(ifp
);
1219 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1223 if (ifbond_flags_if_detaching(ifb
)) {
1229 bond_if_detach(ifp
);
1234 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1239 ifb
= ifnet_softc(ifp
);
1240 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1245 case BPF_TAP_DISABLE
:
1246 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1250 ifb
->ifb_bpf_input
= func
;
1253 case BPF_TAP_OUTPUT
:
1254 ifb
->ifb_bpf_output
= func
;
1257 case BPF_TAP_INPUT_OUTPUT
:
1258 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1268 ether_header_hash(struct ether_header
* eh_p
)
1272 /* get 32-bits from destination ether and ether type */
1273 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1275 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1279 static struct mbuf
*
1280 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1285 while (*offset
>= len
) {
1296 #if BYTE_ORDER == BIG_ENDIAN
1297 static __inline__
uint32_t
1298 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1300 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1301 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1303 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1304 static __inline__
uint32_t
1305 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1307 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1308 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1310 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1313 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1315 struct mbuf
* current
;
1316 u_char
* current_data
;
1321 current
= S_mbuf_skip_to_offset(m
, &offset
);
1322 if (current
== NULL
) {
1325 current_data
= mtod(current
, u_char
*) + offset
;
1326 space_current
= current
->m_len
- offset
;
1327 if (space_current
>= (int)sizeof(uint32_t)) {
1328 *val
= *((uint32_t *)current_data
);
1331 next
= current
->m_next
;
1332 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1335 next_data
= mtod(next
, u_char
*);
1336 switch (space_current
) {
1338 *val
= make_uint32(current_data
[0], next_data
[0],
1339 next_data
[1], next_data
[2]);
1342 *val
= make_uint32(current_data
[0], current_data
[1],
1343 next_data
[0], next_data
[1]);
1346 *val
= make_uint32(current_data
[0], current_data
[1],
1347 current_data
[2], next_data
[0]);
1353 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1354 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1357 ip_header_hash(struct mbuf
* m
)
1360 struct in_addr ip_dst
;
1361 struct in_addr ip_src
;
1364 struct mbuf
* orig_m
= m
;
1366 /* find the IP protocol field relative to the start of the packet */
1367 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1368 m
= S_mbuf_skip_to_offset(m
, &offset
);
1369 if (m
== NULL
|| m
->m_len
< 1) {
1372 data
= mtod(m
, u_char
*) + offset
;
1375 /* find the IP src relative to the IP protocol */
1376 if ((m
->m_len
- offset
)
1377 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1378 /* this should be the normal case */
1379 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1380 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1383 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1384 (uint32_t *)&ip_src
.s_addr
)) {
1387 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1388 (uint32_t *)&ip_dst
.s_addr
)) {
1392 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1395 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1398 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1400 ipv6_header_hash(struct mbuf
* m
)
1405 struct mbuf
* orig_m
= m
;
1409 /* find the IP protocol field relative to the start of the packet */
1410 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1411 m
= S_mbuf_skip_to_offset(m
, &offset
);
1413 goto bad_ipv6_packet
;
1415 data
= mtod(m
, u_char
*) + offset
;
1417 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1418 /* this should be the normal case */
1419 for (i
= 0, scan
= (uint32_t *)data
;
1420 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1426 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1428 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1429 (uint32_t *)&tmp
)) {
1430 goto bad_ipv6_packet
;
1435 return (ntohl(val
));
1438 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1442 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1444 bpf_packet_func bpf_func
;
1447 struct ifnet
* port_ifp
= NULL
;
1449 struct flowadv adv
= { FADV_SUCCESS
};
1454 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1458 if (m
->m_pkthdr
.pkt_flowid
!= 0) {
1459 h
= m
->m_pkthdr
.pkt_flowid
;
1462 struct ether_header
* eh_p
;
1464 eh_p
= mtod(m
, struct ether_header
*);
1465 switch (ntohs(eh_p
->ether_type
)) {
1467 h
= ip_header_hash(m
);
1469 case ETHERTYPE_IPV6
:
1470 h
= ipv6_header_hash(m
);
1473 h
= ether_header_hash(eh_p
);
1478 ifb
= ifnet_softc(ifp
);
1479 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1480 || ifb
->ifb_distributing_count
== 0) {
1483 h
%= ifb
->ifb_distributing_count
;
1484 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1485 bpf_func
= ifb
->ifb_bpf_output
;
1488 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1489 (void)ifnet_stat_increment_out(ifp
, 1,
1490 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1493 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1495 bond_bpf_output(ifp
, m
, bpf_func
);
1497 err
= dlil_output(port_ifp
, PF_BOND
, m
, NULL
, NULL
, 1, &adv
);
1500 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1502 } else if (adv
.code
== FADV_SUSPENDED
) {
1516 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1519 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1520 if (p
->po_ifp
== port_ifp
) {
1528 bond_lookup_port(struct ifnet
* port_ifp
)
1533 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1534 port
= ifbond_lookup_port(ifb
, port_ifp
);
1543 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1545 struct ifnet
* bond_ifp
= NULL
;
1551 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1554 p
= bond_lookup_port(port_ifp
);
1558 if (p
->po_enabled
== 0) {
1562 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1565 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1566 if (ifbond_selection(ifb
)) {
1567 event_code
= (ifb
->ifb_active_lag
== NULL
)
1570 /* XXX need to take a reference on bond_ifp */
1571 bond_ifp
= ifb
->ifb_ifp
;
1572 ifb
->ifb_last_link_event
= event_code
;
1575 event_code
= (ifb
->ifb_active_lag
== NULL
)
1578 if (event_code
!= ifb
->ifb_last_link_event
) {
1579 if (g_bond
->verbose
) {
1580 timestamp_printf("%s: (receive) generating LINK event\n",
1583 bond_ifp
= ifb
->ifb_ifp
;
1584 ifb
->ifb_last_link_event
= event_code
;
1590 if (bond_ifp
!= NULL
) {
1591 interface_link_event(bond_ifp
, event_code
);
1598 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1600 la_marker_pdu_ref marker_p
;
1603 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1604 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1608 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1612 p
= bond_lookup_port(port_ifp
);
1613 if (p
== NULL
|| p
->po_enabled
== 0
1614 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1618 /* echo back the same packet as a marker response */
1619 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1620 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1630 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1631 char * frame_header
)
1633 bpf_packet_func bpf_func
;
1634 const struct ether_header
* eh_p
;
1639 eh_p
= (const struct ether_header
*)frame_header
;
1640 if ((m
->m_flags
& M_MCAST
) != 0
1641 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1642 sizeof(eh_p
->ether_dhost
)) == 0
1643 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1644 u_char subtype
= *mtod(m
, u_char
*);
1646 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1647 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1652 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1653 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1658 bond_receive_lacpdu(m
, port_ifp
);
1661 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1664 /* restore the ethernet header pointer in the mbuf */
1665 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1666 m
->m_data
-= ETHER_HDR_LEN
;
1667 m
->m_len
+= ETHER_HDR_LEN
;
1668 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1669 if (m
->m_pkthdr
.len
< min_size
) {
1674 if (m
->m_len
< min_size
) {
1675 m
= m_pullup(m
, min_size
);
1680 /* send to marker responder */
1681 bond_receive_la_marker_pdu(m
, port_ifp
);
1684 else if (subtype
== 0
1685 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1686 /* invalid subtype, discard the frame */
1692 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1695 p
= bond_lookup_port(port_ifp
);
1696 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1700 /* make the packet appear as if it arrived on the bonded interface */
1703 bpf_func
= ifb
->ifb_bpf_input
;
1706 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1707 (void)ifnet_stat_increment_in(ifp
, 1,
1708 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1709 + ETHER_VLAN_ENCAP_LEN
), 0);
1712 (void)ifnet_stat_increment_in(ifp
, 1,
1713 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1715 m
->m_pkthdr
.rcvif
= ifp
;
1716 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1717 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1718 dlil_input_packet_list(ifp
, m
);
1727 static __inline__
const char *
1728 bondport_get_name(bondport_ref p
)
1730 return (p
->po_name
);
1733 static __inline__
int
1734 bondport_get_index(bondport_ref p
)
1736 return (ifnet_index(p
->po_ifp
));
1740 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1742 struct ether_header
* eh_p
;
1745 /* packet_buffer_allocate leaves room for ethernet header */
1746 eh_p
= mtod(buf
, struct ether_header
*);
1747 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1748 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1749 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1750 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1752 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1753 bondport_get_name(p
), error
);
1759 bondport_timer_process_func(devtimer_ref timer
,
1760 devtimer_process_func_event event
)
1765 case devtimer_process_func_event_lock
:
1767 devtimer_retain(timer
);
1769 case devtimer_process_func_event_unlock
:
1770 if (devtimer_valid(timer
)) {
1771 /* as long as the devtimer is valid, we can look at arg0 */
1773 struct ifnet
* bond_ifp
= NULL
;
1775 p
= (bondport_ref
)devtimer_arg0(timer
);
1776 if (ifbond_selection(p
->po_bond
)) {
1777 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1780 /* XXX need to take a reference on bond_ifp */
1781 bond_ifp
= p
->po_bond
->ifb_ifp
;
1782 p
->po_bond
->ifb_last_link_event
= event_code
;
1785 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1788 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1789 if (g_bond
->verbose
) {
1790 timestamp_printf("%s: (timer) generating LINK event\n",
1791 p
->po_bond
->ifb_name
);
1793 bond_ifp
= p
->po_bond
->ifb_ifp
;
1794 p
->po_bond
->ifb_last_link_event
= event_code
;
1797 devtimer_release(timer
);
1799 if (bond_ifp
!= NULL
) {
1800 interface_link_event(bond_ifp
, event_code
);
1804 /* timer is going away */
1805 devtimer_release(timer
);
1815 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1816 int active
, int short_timeout
, int * ret_error
)
1819 bondport_ref p
= NULL
;
1820 lacp_actor_partner_state s
;
1823 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
| M_ZERO
);
1825 *ret_error
= ENOMEM
;
1828 multicast_list_init(&p
->po_multicast
);
1829 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1830 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1831 >= sizeof(p
->po_name
)) {
1832 printf("if_bond: name too large\n");
1833 *ret_error
= EINVAL
;
1836 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1838 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1839 bondport_get_name(p
), error
);
1842 /* remember the current interface MTU so it can be restored */
1843 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1844 p
->po_ifp
= port_ifp
;
1845 p
->po_media_info
= interface_media_info(port_ifp
);
1846 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1847 if (p
->po_current_while_timer
== NULL
) {
1848 *ret_error
= ENOMEM
;
1851 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1852 if (p
->po_periodic_timer
== NULL
) {
1853 *ret_error
= ENOMEM
;
1856 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1857 if (p
->po_wait_while_timer
== NULL
) {
1858 *ret_error
= ENOMEM
;
1861 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1862 if (p
->po_transmit_timer
== NULL
) {
1863 *ret_error
= ENOMEM
;
1866 p
->po_receive_state
= ReceiveState_none
;
1867 p
->po_mux_state
= MuxState_none
;
1868 p
->po_priority
= priority
;
1870 s
= lacp_actor_partner_state_set_aggregatable(s
);
1871 if (short_timeout
) {
1872 s
= lacp_actor_partner_state_set_short_timeout(s
);
1875 s
= lacp_actor_partner_state_set_active_lacp(s
);
1877 p
->po_actor_state
= s
;
1886 bondport_start(bondport_ref p
)
1888 bondport_receive_machine(p
, LAEventStart
, NULL
);
1889 bondport_mux_machine(p
, LAEventStart
, NULL
);
1890 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1891 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1896 * Function: bondport_invalidate_timers
1898 * Invalidate all of the timers for the bondport.
1901 bondport_invalidate_timers(bondport_ref p
)
1903 devtimer_invalidate(p
->po_current_while_timer
);
1904 devtimer_invalidate(p
->po_periodic_timer
);
1905 devtimer_invalidate(p
->po_wait_while_timer
);
1906 devtimer_invalidate(p
->po_transmit_timer
);
1910 * Function: bondport_cancel_timers
1912 * Cancel all of the timers for the bondport.
1915 bondport_cancel_timers(bondport_ref p
)
1917 devtimer_cancel(p
->po_current_while_timer
);
1918 devtimer_cancel(p
->po_periodic_timer
);
1919 devtimer_cancel(p
->po_wait_while_timer
);
1920 devtimer_cancel(p
->po_transmit_timer
);
1924 bondport_free(bondport_ref p
)
1926 multicast_list_remove(&p
->po_multicast
);
1927 devtimer_release(p
->po_current_while_timer
);
1928 devtimer_release(p
->po_periodic_timer
);
1929 devtimer_release(p
->po_wait_while_timer
);
1930 devtimer_release(p
->po_transmit_timer
);
1935 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1936 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1937 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1938 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1940 static __inline__
int
1941 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1943 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1944 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1948 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1955 bondport_ref
* new_array
= NULL
;
1956 bondport_ref
* old_array
= NULL
;
1960 if (IFNET_IS_INTCOPROC(port_ifp
)) {
1964 /* pre-allocate space for new port */
1965 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1970 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1971 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1974 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1977 /* make sure this interface can handle our current MTU */
1978 devmtu
= bond_device_mtu(ifp
, ifb
);
1980 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
1982 printf("if_bond: interface %s doesn't support mtu %d",
1983 bondport_get_name(p
), devmtu
);
1988 /* make sure ifb doesn't get de-allocated while we wait */
1991 /* wait for other add or remove to complete */
1992 ifbond_wait(ifb
, "bond_add_interface");
1994 if (ifbond_flags_if_detaching(ifb
)) {
1995 /* someone destroyed the bond while we were waiting */
1999 if (bond_lookup_port(port_ifp
) != NULL
) {
2000 /* port is already part of a bond */
2004 ifnet_lock_exclusive(port_ifp
);
2005 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2006 /* interface already has VLAN's, or is part of bond */
2007 ifnet_lock_done(port_ifp
);
2012 /* mark the interface busy */
2013 /* can't use ifnet_set_eflags because that takes the lock */
2014 port_ifp
->if_eflags
|= IFEF_BOND
;
2015 ifnet_lock_done(port_ifp
);
2017 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2018 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2019 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2020 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2024 ifnet_offload_t ifp_offload
;
2025 ifnet_offload_t port_ifp_offload
;
2027 ifp_offload
= ifnet_offload(ifp
);
2028 port_ifp_offload
= ifnet_offload(port_ifp
);
2029 if (ifp_offload
!= port_ifp_offload
) {
2030 ifnet_offload_t offload
;
2032 offload
= ifp_offload
& port_ifp_offload
;
2033 printf("bond_add_interface(%s, %s) "
2034 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2035 ifb
->ifb_name
, bondport_get_name(p
),
2036 ifp_offload
, port_ifp_offload
, offload
);
2039 * if the bond has VLAN's, we can't simply change the hwassist
2040 * field behind its back: this needs work
2042 ifnet_set_offload(ifp
, offload
);
2047 /* remember the port's ethernet address so it can be restored */
2048 ether_addr_copy(&p
->po_saved_addr
, IF_LLADDR(port_ifp
));
2050 /* add it to the list of ports */
2051 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2052 ifb
->ifb_port_count
++;
2054 /* set the default MTU */
2055 if (ifnet_mtu(ifp
) == 0) {
2056 ifnet_set_mtu(ifp
, ETHERMTU
);
2061 /* first port added to bond determines bond's ethernet address */
2063 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(port_ifp
), ETHER_ADDR_LEN
,
2067 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2069 /* allocate a larger distributing array */
2070 new_array
= (bondport_ref
*)
2071 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2072 if (new_array
== NULL
) {
2077 /* attach our BOND "protocol" to the interface */
2078 error
= bond_attach_protocol(port_ifp
);
2082 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2084 /* set the interface MTU */
2085 devmtu
= bond_device_mtu(ifp
, ifb
);
2086 error
= siocsifmtu(port_ifp
, devmtu
);
2088 printf("bond_add_interface(%s, %s):"
2089 " SIOCSIFMTU %d failed %d\n",
2090 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2093 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2095 /* program the port with our multicast addresses */
2096 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2098 printf("bond_add_interface(%s, %s):"
2099 " multicast_list_program failed %d\n",
2100 ifb
->ifb_name
, bondport_get_name(p
), error
);
2104 /* mark the interface up */
2105 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2107 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2109 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2110 ifb
->ifb_name
, bondport_get_name(p
), error
);
2114 /* re-program the port's ethernet address */
2115 error
= if_siflladdr(port_ifp
,
2116 (const struct ether_addr
*)IF_LLADDR(ifp
));
2118 /* port doesn't support setting the link address */
2119 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2120 ifb
->ifb_name
, bondport_get_name(p
), error
);
2123 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2127 /* no failures past this point */
2130 /* copy the contents of the existing distributing array */
2131 if (ifb
->ifb_distributing_count
) {
2132 bcopy(ifb
->ifb_distributing_array
, new_array
,
2133 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2135 old_array
= ifb
->ifb_distributing_array
;
2136 ifb
->ifb_distributing_array
= new_array
;
2138 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2141 /* check if we need to generate a link status event */
2142 if (ifbond_selection(ifb
)) {
2143 event_code
= (ifb
->ifb_active_lag
== NULL
)
2146 ifb
->ifb_last_link_event
= event_code
;
2150 /* are we adding the first distributing interface? */
2151 if (media_active(&p
->po_media_info
)) {
2152 if (ifb
->ifb_distributing_count
== 0) {
2153 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2155 bondport_enable_distributing(p
);
2158 bondport_disable_distributing(p
);
2161 /* clear the busy state, and wakeup anyone waiting */
2162 ifbond_signal(ifb
, "bond_add_interface");
2164 if (event_code
!= 0) {
2165 interface_link_event(ifp
, event_code
);
2167 if (old_array
!= NULL
) {
2168 FREE(old_array
, M_BOND
);
2173 bond_assert_lock_not_held();
2175 /* if this was the first port to be added, clear our address */
2177 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2180 if (new_array
!= NULL
) {
2181 FREE(new_array
, M_BOND
);
2183 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2186 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2188 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2189 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2192 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2193 (void)bond_detach_protocol(port_ifp
);
2195 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2198 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2200 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2201 ifb
->ifb_name
, bondport_get_name(p
),
2202 p
->po_devmtu
.ifdm_current
, error1
);
2206 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2207 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2208 ifb
->ifb_port_count
--;
2210 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2211 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2212 ifb
->ifb_altmtu
= 0;
2213 ifnet_set_mtu(ifp
, 0);
2214 ifnet_set_offload(ifp
, 0);
2218 ifbond_signal(ifb
, "bond_add_interface");
2220 ifbond_release(ifb
);
2226 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2231 bondport_ref head_port
;
2234 int new_link_address
= FALSE
;
2236 lacp_actor_partner_state s
;
2237 int was_distributing
;
2239 bond_assert_lock_held();
2242 ifbond_wait(ifb
, "bond_remove_interface");
2244 p
= ifbond_lookup_port(ifb
, port_ifp
);
2247 /* it got removed by another thread */
2251 /* de-select it and remove it from the lists */
2252 was_distributing
= bondport_flags_distributing(p
);
2253 bondport_disable_distributing(p
);
2254 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2255 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2256 active_lag
= bondport_remove_from_LAG(p
);
2257 /* invalidate timers here while holding the bond_lock */
2258 bondport_invalidate_timers(p
);
2260 /* announce that we're Individual now */
2261 s
= p
->po_actor_state
;
2262 s
= lacp_actor_partner_state_set_individual(s
);
2263 s
= lacp_actor_partner_state_set_not_collecting(s
);
2264 s
= lacp_actor_partner_state_set_not_distributing(s
);
2265 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2266 p
->po_actor_state
= s
;
2267 bondport_flags_set_ntt(p
);
2270 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2271 ifb
->ifb_port_count
--;
2274 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2275 if (head_port
== NULL
) {
2276 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2277 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2280 ifnet_set_offload(ifp
, 0);
2281 ifnet_set_mtu(ifp
, 0);
2282 ifb
->ifb_altmtu
= 0;
2283 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2284 && bcmp(&p
->po_saved_addr
, IF_LLADDR(ifp
),
2285 ETHER_ADDR_LEN
) == 0) {
2286 new_link_address
= TRUE
;
2288 /* check if we need to generate a link status event */
2289 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2290 if (ifbond_selection(ifb
) || active_lag
) {
2291 event_code
= (ifb
->ifb_active_lag
== NULL
)
2294 ifb
->ifb_last_link_event
= event_code
;
2296 bondport_transmit_machine(p
, LAEventStart
,
2297 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2300 /* are we removing the last distributing interface? */
2301 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2302 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2309 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2311 else if (new_link_address
) {
2312 struct ifnet
* scan_ifp
;
2313 bondport_ref scan_port
;
2315 /* ifbond_wait() allows port list traversal without holding the lock */
2317 /* this port gave the bond its ethernet address, switch to new one */
2318 ifnet_set_lladdr_and_type(ifp
,
2319 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2322 /* re-program each port with the new link address */
2323 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2324 scan_ifp
= scan_port
->po_ifp
;
2326 error
= if_siflladdr(scan_ifp
,
2327 (const struct ether_addr
*) IF_LLADDR(ifp
));
2329 printf("bond_remove_interface(%s, %s): "
2330 "if_siflladdr (%s) failed %d\n",
2331 ifb
->ifb_name
, bondport_get_name(p
),
2332 bondport_get_name(scan_port
), error
);
2337 /* restore the port's ethernet address */
2338 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2340 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2341 ifb
->ifb_name
, bondport_get_name(p
), error
);
2344 /* restore the port's MTU */
2345 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2347 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2348 ifb
->ifb_name
, bondport_get_name(p
),
2349 p
->po_devmtu
.ifdm_current
, error
);
2352 /* remove the bond "protocol" */
2353 bond_detach_protocol(port_ifp
);
2355 /* generate link event */
2356 if (event_code
!= 0) {
2357 interface_link_event(ifp
, event_code
);
2362 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2363 /* release this bondport's reference to the ifbond */
2364 ifbond_release(ifb
);
2367 ifbond_signal(ifb
, "bond_remove_interface");
2368 ifbond_release(ifb
);
2373 bond_set_lacp_mode(ifbond_ref ifb
)
2377 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2378 bondport_disable_distributing(p
);
2385 bond_set_static_mode(ifbond_ref ifb
)
2388 lacp_actor_partner_state s
;
2390 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2391 bondport_disable_distributing(p
);
2392 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2393 (void)bondport_remove_from_LAG(p
);
2394 bondport_cancel_timers(p
);
2396 /* announce that we're Individual now */
2397 s
= p
->po_actor_state
;
2398 s
= lacp_actor_partner_state_set_individual(s
);
2399 s
= lacp_actor_partner_state_set_not_collecting(s
);
2400 s
= lacp_actor_partner_state_set_not_distributing(s
);
2401 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2402 p
->po_actor_state
= s
;
2403 bondport_flags_set_ntt(p
);
2404 bondport_transmit_machine(p
, LAEventStart
,
2405 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2407 p
->po_actor_state
= 0;
2408 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2410 if (media_active(&p
->po_media_info
)) {
2411 bondport_enable_distributing(p
);
2414 bondport_disable_distributing(p
);
2421 bond_set_mode(struct ifnet
* ifp
, int mode
)
2428 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2429 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2431 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2433 if (ifb
->ifb_mode
== mode
) {
2439 ifbond_wait(ifb
, "bond_set_mode");
2441 /* verify (again) that the mode is actually different */
2442 if (ifb
->ifb_mode
== mode
) {
2447 ifb
->ifb_mode
= mode
;
2448 if (mode
== IF_BOND_MODE_LACP
) {
2449 bond_set_lacp_mode(ifb
);
2451 /* check if we need to generate a link status event */
2452 if (ifbond_selection(ifb
)) {
2453 event_code
= (ifb
->ifb_active_lag
== NULL
)
2458 bond_set_static_mode(ifb
);
2459 event_code
= (ifb
->ifb_distributing_count
== 0)
2463 ifb
->ifb_last_link_event
= event_code
;
2466 ifbond_signal(ifb
, "bond_set_mode");
2468 ifbond_release(ifb
);
2470 if (event_code
!= 0) {
2471 interface_link_event(ifp
, event_code
);
2477 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2482 struct if_bond_status_req
* ibsr
;
2483 struct if_bond_status ibs
;
2486 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2487 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2490 ibsr
->ibsr_key
= ifb
->ifb_key
;
2491 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2492 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2493 dst
= proc_is64bit(current_proc())
2494 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2495 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2496 if (dst
== USER_ADDR_NULL
) {
2497 /* just want to know how many there are */
2500 if (ibsr
->ibsr_count
< 0) {
2503 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2504 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2505 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2506 struct if_bond_partner_state
* ibps_p
;
2507 partner_state_ref ps
;
2512 bzero(&ibs
, sizeof(ibs
));
2513 strlcpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2514 ibs
.ibs_port_priority
= port
->po_priority
;
2515 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2516 ibs
.ibs_state
= port
->po_actor_state
;
2517 ibs
.ibs_selected_state
= port
->po_selected
;
2518 ps
= &port
->po_partner_state
;
2519 ibps_p
= &ibs
.ibs_partner_state
;
2520 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2521 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2522 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2523 ibps_p
->ibps_port
= ps
->ps_port
;
2524 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2525 ibps_p
->ibps_state
= ps
->ps_state
;
2528 /* fake the selected information */
2529 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2530 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2532 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2542 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2545 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2551 bond_set_promisc(__unused
struct ifnet
*ifp
)
2558 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2564 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2565 mtu_min
= IF_MINMTU
;
2567 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2568 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2570 if (devmtu_p
->ifdm_min
> mtu_min
) {
2571 mtu_min
= devmtu_p
->ifdm_min
;
2573 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2574 mtu_max
= devmtu_p
->ifdm_max
;
2583 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2588 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2589 error
= siocsifmtu(p
->po_ifp
, mtu
);
2591 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2592 ifb
->ifb_name
, bondport_get_name(p
), error
);
2600 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2610 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2611 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2612 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2616 ifbond_wait(ifb
, "bond_set_mtu");
2619 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2623 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2624 if (mtu
> mtu_max
) {
2628 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2629 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2634 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2637 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2639 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2640 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2641 if (new_max
!= old_max
) {
2642 /* we can safely walk the list of port without the lock held */
2644 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2646 /* try our best to back out of it */
2647 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2653 ifb
->ifb_altmtu
= mtu
;
2656 ifnet_set_mtu(ifp
, mtu
);
2661 ifbond_signal(ifb
, "bond_set_mtu");
2662 ifbond_release(ifb
);
2670 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2673 struct if_bond_req ibr
;
2674 struct ifaddr
* ifa
;
2677 struct ifmediareq
*ifmr
;
2678 struct ifnet
* port_ifp
= NULL
;
2679 user_addr_t user_addr
;
2681 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2682 return (EOPNOTSUPP
);
2684 ifr
= (struct ifreq
*)data
;
2685 ifa
= (struct ifaddr
*)data
;
2689 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2692 case SIOCGIFMEDIA32
:
2693 case SIOCGIFMEDIA64
:
2695 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2696 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2698 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2700 ifmr
= (struct ifmediareq
*)data
;
2701 ifmr
->ifm_current
= IFM_ETHER
;
2703 ifmr
->ifm_status
= IFM_AVALID
;
2704 ifmr
->ifm_active
= IFM_ETHER
;
2705 ifmr
->ifm_count
= 1;
2706 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2707 if (ifb
->ifb_active_lag
!= NULL
) {
2708 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2709 ifmr
->ifm_status
|= IFM_ACTIVE
;
2712 else if (ifb
->ifb_distributing_count
> 0) {
2714 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2715 ifmr
->ifm_status
|= IFM_ACTIVE
;
2718 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2719 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2720 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2721 if (user_addr
!= USER_ADDR_NULL
) {
2722 error
= copyout(&ifmr
->ifm_current
,
2729 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2735 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2736 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2738 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2741 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2742 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2743 &ifr
->ifr_devmtu
.ifdm_max
);
2749 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2750 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2752 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2755 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2760 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2764 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2768 user_addr
= proc_is64bit(current_proc())
2769 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2770 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2774 switch (ibr
.ibr_op
) {
2775 case IF_BOND_OP_ADD_INTERFACE
:
2776 case IF_BOND_OP_REMOVE_INTERFACE
:
2777 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2778 if (port_ifp
== NULL
) {
2782 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2783 error
= EPROTONOSUPPORT
;
2787 case IF_BOND_OP_SET_VERBOSE
:
2788 case IF_BOND_OP_SET_MODE
:
2797 switch (ibr
.ibr_op
) {
2798 case IF_BOND_OP_ADD_INTERFACE
:
2799 error
= bond_add_interface(ifp
, port_ifp
);
2801 case IF_BOND_OP_REMOVE_INTERFACE
:
2803 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2804 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2806 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2808 error
= bond_remove_interface(ifb
, port_ifp
);
2811 case IF_BOND_OP_SET_VERBOSE
:
2813 if (g_bond
== NULL
) {
2818 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2821 case IF_BOND_OP_SET_MODE
:
2822 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2823 case IF_BOND_MODE_LACP
:
2824 case IF_BOND_MODE_STATIC
:
2833 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2836 break; /* SIOCSIFBOND */
2839 user_addr
= proc_is64bit(current_proc())
2840 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2841 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2845 switch (ibr
.ibr_op
) {
2846 case IF_BOND_OP_GET_STATUS
:
2856 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2857 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2859 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2861 switch (ibr
.ibr_op
) {
2862 case IF_BOND_OP_GET_STATUS
:
2863 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2867 break; /* SIOCGIFBOND */
2874 /* enable/disable promiscuous mode */
2876 error
= bond_set_promisc(ifp
);
2882 error
= bond_setmulti(ifp
);
2891 bond_if_free(struct ifnet
* ifp
)
2899 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2904 ifbond_release(ifb
);
2911 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
2913 struct ifnet
* bond_ifp
= NULL
;
2915 int old_distributing_count
;
2917 struct media_info media_info
= { 0, 0};
2919 switch (event_code
) {
2920 case KEV_DL_IF_DETACHED
:
2922 case KEV_DL_LINK_OFF
:
2923 case KEV_DL_LINK_ON
:
2924 media_info
= interface_media_info(port_ifp
);
2930 p
= bond_lookup_port(port_ifp
);
2936 old_distributing_count
= ifb
->ifb_distributing_count
;
2937 switch (event_code
) {
2938 case KEV_DL_IF_DETACHED
:
2939 bond_remove_interface(ifb
, p
->po_ifp
);
2941 case KEV_DL_LINK_OFF
:
2942 case KEV_DL_LINK_ON
:
2943 p
->po_media_info
= media_info
;
2944 if (p
->po_enabled
) {
2945 bondport_link_status_changed(p
);
2949 /* generate a link-event */
2950 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2951 if (ifbond_selection(ifb
)) {
2952 event_code
= (ifb
->ifb_active_lag
== NULL
)
2955 /* XXX need to take a reference on bond_ifp */
2956 bond_ifp
= ifb
->ifb_ifp
;
2957 ifb
->ifb_last_link_event
= event_code
;
2960 event_code
= (ifb
->ifb_active_lag
== NULL
)
2963 if (event_code
!= ifb
->ifb_last_link_event
) {
2964 if (g_bond
->verbose
) {
2965 timestamp_printf("%s: (event) generating LINK event\n",
2968 bond_ifp
= ifb
->ifb_ifp
;
2969 ifb
->ifb_last_link_event
= event_code
;
2975 * if the distributing array membership changed from 0 <-> !0
2976 * generate a link event
2978 if (old_distributing_count
== 0
2979 && ifb
->ifb_distributing_count
!= 0) {
2980 event_code
= KEV_DL_LINK_ON
;
2982 else if (old_distributing_count
!= 0
2983 && ifb
->ifb_distributing_count
== 0) {
2984 event_code
= KEV_DL_LINK_OFF
;
2986 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
2987 bond_ifp
= ifb
->ifb_ifp
;
2988 ifb
->ifb_last_link_event
= event_code
;
2993 if (bond_ifp
!= NULL
) {
2994 interface_link_event(bond_ifp
, event_code
);
3000 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
3001 const struct kev_msg
* event
)
3005 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3006 || event
->kev_class
!= KEV_NETWORK_CLASS
3007 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3010 event_code
= event
->event_code
;
3011 switch (event_code
) {
3012 case KEV_DL_LINK_OFF
:
3013 case KEV_DL_LINK_ON
:
3014 /* we only care about link status changes */
3015 bond_handle_event(port_ifp
, event_code
);
3024 bond_detached(ifnet_t port_ifp
, __unused protocol_family_t protocol
)
3026 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3031 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3034 struct kern_event_msg header
;
3036 char if_name
[IFNAMSIZ
];
3039 bzero(&event
, sizeof(event
));
3040 event
.header
.total_size
= sizeof(event
);
3041 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3042 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3043 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3044 event
.header
.event_code
= event_code
;
3045 event
.header
.event_data
[0] = ifnet_family(ifp
);
3046 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3047 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3048 ifnet_event(ifp
, &event
.header
);
3053 * Function: bond_attach_protocol
3055 * Attach a DLIL protocol to the interface.
3057 * The ethernet demux special cases to always return PF_BOND if the
3058 * interface is bonded. That means we receive all traffic from that
3059 * interface without passing any of the traffic to any other attached
3063 bond_attach_protocol(struct ifnet
*ifp
)
3066 struct ifnet_attach_proto_param reg
;
3068 bzero(®
, sizeof(reg
));
3069 reg
.input
= bond_input
;
3070 reg
.event
= bond_event
;
3071 reg
.detached
= bond_detached
;
3073 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3075 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3076 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3082 * Function: bond_detach_protocol
3084 * Detach our DLIL protocol from an interface
3087 bond_detach_protocol(struct ifnet
*ifp
)
3091 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3093 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3094 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3100 * DLIL interface family functions
3102 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3103 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3104 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3105 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3106 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3107 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3109 __private_extern__
int
3110 bond_family_init(void)
3114 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3118 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3123 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3125 ether_detach_inet6
);
3127 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3132 error
= bond_clone_attach();
3134 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3149 ** LACP ifbond_list routines
3152 ifbond_list_find_moved_port(bondport_ref rx_port
,
3153 const lacp_actor_partner_tlv_ref atlv
)
3157 partner_state_ref ps
;
3160 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3161 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3164 /* no point in comparing against ourselves */
3167 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3168 /* it's not clear that we should be checking this */
3171 ps
= &p
->po_partner_state
;
3172 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3175 ps_li
= &ps
->ps_lag_info
;
3176 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3177 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3178 sizeof(ps_li
->li_system
)) == 0) {
3179 if (g_bond
->verbose
) {
3180 timestamp_printf("System " EA_FORMAT
3181 " Port 0x%x moved from %s to %s\n",
3182 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3183 bondport_get_name(p
),
3184 bondport_get_name(rx_port
));
3194 ** LACP ifbond, LAG routines
3198 ifbond_selection(ifbond_ref bond
)
3200 int all_ports_ready
= 0;
3201 int active_media
= 0;
3203 int lag_changed
= 0;
3207 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3208 if (lag
!= bond
->ifb_active_lag
) {
3209 if (bond
->ifb_active_lag
!= NULL
) {
3210 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3211 bond
->ifb_active_lag
= NULL
;
3213 bond
->ifb_active_lag
= lag
;
3215 ifbond_activate_LAG(bond
, lag
, active_media
);
3219 else if (lag
!= NULL
) {
3220 if (lag
->lag_active_media
!= active_media
) {
3221 if (g_bond
->verbose
) {
3222 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3223 link_speed(lag
->lag_active_media
),
3224 link_speed(active_media
));
3226 ifbond_deactivate_LAG(bond
, lag
);
3227 ifbond_activate_LAG(bond
, lag
, active_media
);
3232 port_speed
= link_speed(active_media
);
3233 all_ports_ready
= ifbond_all_ports_ready(bond
);
3235 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3236 if (lag
!= NULL
&& p
->po_lag
== lag
3237 && media_speed(&p
->po_media_info
) == port_speed
3238 && (p
->po_mux_state
== MuxState_DETACHED
3239 || p
->po_selected
== SelectedState_SELECTED
3240 || p
->po_selected
== SelectedState_STANDBY
)
3241 && bondport_aggregatable(p
)) {
3242 if (bond
->ifb_max_active
> 0) {
3243 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3244 if (p
->po_selected
== SelectedState_STANDBY
3245 || p
->po_selected
== SelectedState_UNSELECTED
) {
3246 bondport_set_selected(p
, SelectedState_SELECTED
);
3249 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3250 bondport_set_selected(p
, SelectedState_STANDBY
);
3254 bondport_set_selected(p
, SelectedState_SELECTED
);
3257 if (bondport_flags_selected_changed(p
)) {
3258 bondport_flags_clear_selected_changed(p
);
3259 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3262 && bondport_flags_ready(p
)
3263 && p
->po_mux_state
== MuxState_WAITING
) {
3264 bondport_mux_machine(p
, LAEventReady
, NULL
);
3266 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3268 return (lag_changed
);
3272 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3274 int best_active
= 0;
3275 LAG_ref best_lag
= NULL
;
3280 if (bond
->ifb_active_lag
!= NULL
) {
3281 best_lag
= bond
->ifb_active_lag
;
3282 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3283 if (bond
->ifb_max_active
> 0
3284 && best_count
> bond
->ifb_max_active
) {
3285 best_count
= bond
->ifb_max_active
;
3287 best_speed
= link_speed(best_active
);
3289 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3294 if (lag
== bond
->ifb_active_lag
) {
3295 /* we've already computed it */
3298 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3302 if (bond
->ifb_max_active
> 0
3303 && count
> bond
->ifb_max_active
) {
3304 /* if there's a limit, don't count extra links */
3305 count
= bond
->ifb_max_active
;
3307 speed
= link_speed(active
);
3308 if ((count
* speed
) > (best_count
* best_speed
)) {
3311 best_active
= active
;
3315 if (best_count
== 0) {
3318 *active_media
= best_active
;
3323 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3327 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3328 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3334 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3339 if (bond
->ifb_max_active
> 0) {
3340 need
= bond
->ifb_max_active
;
3342 lag
->lag_active_media
= active_media
;
3343 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3344 if (bondport_aggregatable(p
) == 0) {
3345 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3347 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3348 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3350 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3351 if (bond
->ifb_max_active
> 0) {
3353 bondport_set_selected(p
, SelectedState_SELECTED
);
3357 bondport_set_selected(p
, SelectedState_STANDBY
);
3361 bondport_set_selected(p
, SelectedState_SELECTED
);
3365 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3373 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3375 LAG_ref lag
= bond
->ifb_active_lag
;
3377 bond
->ifb_max_active
= max_active
;
3378 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3381 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3385 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3386 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3387 if (p
->po_selected
== SelectedState_SELECTED
) {
3388 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3390 if (remove_count
== 0) {
3401 ifbond_all_ports_ready(ifbond_ref bond
)
3406 if (bond
->ifb_active_lag
== NULL
) {
3409 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3410 if (p
->po_mux_state
== MuxState_WAITING
3411 && p
->po_selected
== SelectedState_SELECTED
) {
3412 if (bondport_flags_ready(p
) == 0) {
3416 /* note that there was at least one ready port */
3423 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3427 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3428 if (this_port
== p
) {
3431 if (bondport_flags_mux_attached(p
) == 0) {
3439 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3443 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3444 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3445 sizeof(lag
->lag_info
)) == 0) {
3453 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3463 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3464 if (bondport_aggregatable(p
)) {
3467 this_speed
= media_speed(&p
->po_media_info
);
3468 if (this_speed
== 0) {
3471 if (this_speed
> speed
) {
3472 active
= p
->po_media_info
.mi_active
;
3476 else if (this_speed
== speed
) {
3481 *active_media
= active
;
3487 ** LACP bondport routines
3490 bondport_link_status_changed(bondport_ref p
)
3492 ifbond_ref bond
= p
->po_bond
;
3494 if (g_bond
->verbose
) {
3495 if (media_active(&p
->po_media_info
)) {
3496 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3497 bondport_get_name(p
),
3498 media_speed(&p
->po_media_info
),
3499 media_full_duplex(&p
->po_media_info
)
3503 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3506 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3507 if (media_active(&p
->po_media_info
)
3508 && bond
->ifb_active_lag
!= NULL
3509 && p
->po_lag
== bond
->ifb_active_lag
3510 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3511 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3512 if (g_bond
->verbose
) {
3513 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3514 bondport_get_name(p
),
3515 media_speed(&p
->po_media_info
),
3516 link_speed(p
->po_lag
->lag_active_media
));
3518 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3521 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3522 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3523 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3526 if (media_active(&p
->po_media_info
)) {
3527 bondport_enable_distributing(p
);
3530 bondport_disable_distributing(p
);
3537 bondport_aggregatable(bondport_ref p
)
3539 partner_state_ref ps
= &p
->po_partner_state
;
3541 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3542 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3543 /* we and/or our partner are individual */
3546 if (p
->po_lag
== NULL
) {
3549 switch (p
->po_receive_state
) {
3551 if (g_bond
->verbose
) {
3552 timestamp_printf("[%s] Port is not selectable\n",
3553 bondport_get_name(p
));
3556 case ReceiveState_CURRENT
:
3557 case ReceiveState_EXPIRED
:
3564 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3566 LAG_info_ref lag_li
;
3567 partner_state_ref ps
;
3570 ps
= &p
->po_partner_state
;
3571 ps_li
= &ps
->ps_lag_info
;
3572 lag_li
= &lag
->lag_info
;
3573 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3574 && ps_li
->li_key
== lag_li
->li_key
3575 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3576 sizeof(lag_li
->li_system
))
3584 bondport_remove_from_LAG(bondport_ref p
)
3587 ifbond_ref bond
= p
->po_bond
;
3588 LAG_ref lag
= p
->po_lag
;
3593 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3594 if (g_bond
->verbose
) {
3595 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3597 bondport_get_name(p
),
3598 lag
->lag_info
.li_system_priority
,
3599 EA_LIST(&lag
->lag_info
.li_system
),
3600 lag
->lag_info
.li_key
);
3603 lag
->lag_port_count
--;
3604 if (lag
->lag_port_count
> 0) {
3605 return (bond
->ifb_active_lag
== lag
);
3607 if (g_bond
->verbose
) {
3608 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3611 lag
->lag_info
.li_system_priority
,
3612 EA_LIST(&lag
->lag_info
.li_system
),
3613 lag
->lag_info
.li_key
);
3615 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3616 if (bond
->ifb_active_lag
== lag
) {
3617 bond
->ifb_active_lag
= NULL
;
3621 return (active_lag
);
3625 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3627 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3629 lag
->lag_port_count
++;
3630 if (g_bond
->verbose
) {
3631 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3632 bondport_get_name(p
),
3633 lag
->lag_info
.li_system_priority
,
3634 EA_LIST(&lag
->lag_info
.li_system
),
3635 lag
->lag_info
.li_key
);
3641 bondport_assign_to_LAG(bondport_ref p
)
3643 ifbond_ref bond
= p
->po_bond
;
3646 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3647 bondport_remove_from_LAG(p
);
3652 if (bondport_matches_LAG(p
, lag
)) {
3656 bondport_remove_from_LAG(p
);
3658 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3660 bondport_add_to_LAG(p
, lag
);
3663 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3664 TAILQ_INIT(&lag
->lag_port_list
);
3665 lag
->lag_port_count
= 0;
3666 lag
->lag_selected_port_count
= 0;
3667 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3668 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3669 if (g_bond
->verbose
) {
3670 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3673 lag
->lag_info
.li_system_priority
,
3674 EA_LIST(&lag
->lag_info
.li_system
),
3675 lag
->lag_info
.li_key
);
3677 bondport_add_to_LAG(p
, lag
);
3682 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3684 bondport_ref moved_port
;
3687 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3688 &in_lacpdu_p
->la_actor_tlv
);
3689 if (moved_port
!= NULL
) {
3690 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3692 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3693 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3694 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3699 bondport_set_selected(bondport_ref p
, SelectedState s
)
3701 if (s
!= p
->po_selected
) {
3702 ifbond_ref bond
= p
->po_bond
;
3703 LAG_ref lag
= p
->po_lag
;
3705 bondport_flags_set_selected_changed(p
);
3706 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3707 if (p
->po_selected
== SelectedState_SELECTED
) {
3708 lag
->lag_selected_port_count
--;
3710 else if (s
== SelectedState_SELECTED
) {
3711 lag
->lag_selected_port_count
++;
3713 if (g_bond
->verbose
) {
3714 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3715 bondport_get_name(p
),
3716 SelectedStateString(s
),
3717 SelectedStateString(p
->po_selected
));
3730 bondport_UpdateDefaultSelected(bondport_ref p
)
3732 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3737 bondport_RecordDefault(bondport_ref p
)
3739 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3741 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3742 bondport_assign_to_LAG(p
);
3747 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3749 lacp_actor_partner_tlv_ref actor
;
3750 partner_state_ref ps
;
3753 /* compare the PDU's Actor information to our Partner state */
3754 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3755 ps
= &p
->po_partner_state
;
3756 ps_li
= &ps
->ps_lag_info
;
3757 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3758 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3759 != ps
->ps_port_priority
)
3760 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3761 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3762 != ps_li
->li_system_priority
)
3763 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3764 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3765 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3766 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3767 if (g_bond
->verbose
) {
3768 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3769 bondport_get_name(p
));
3776 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3778 lacp_actor_partner_tlv_ref actor
;
3779 ifbond_ref bond
= p
->po_bond
;
3780 int lacp_maintain
= 0;
3781 partner_state_ref ps
;
3782 lacp_actor_partner_tlv_ref partner
;
3785 /* copy the PDU's Actor information into our Partner state */
3786 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3787 ps
= &p
->po_partner_state
;
3788 ps_li
= &ps
->ps_lag_info
;
3789 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3790 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3791 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3792 ps_li
->li_system_priority
3793 = lacp_actor_partner_tlv_get_system_priority(actor
);
3794 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3795 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3797 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3799 /* compare the PDU's Partner information to our own information */
3800 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3802 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3803 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3804 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3805 if (g_bond
->verbose
) {
3806 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3807 bondport_get_name(p
));
3811 if ((lacp_actor_partner_tlv_get_port(partner
)
3812 == bondport_get_index(p
))
3813 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3814 && bcmp(partner
->lap_system
, &g_bond
->system
,
3815 sizeof(g_bond
->system
)) == 0
3816 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3817 == g_bond
->system_priority
)
3818 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3819 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3820 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3821 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3823 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3824 if (g_bond
->verbose
) {
3825 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3826 bondport_get_name(p
));
3829 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3830 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3832 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3833 if (g_bond
->verbose
) {
3834 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3835 bondport_get_name(p
));
3838 bondport_assign_to_LAG(p
);
3842 static __inline__ lacp_actor_partner_state
3843 updateNTTBits(lacp_actor_partner_state s
)
3845 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3846 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3847 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3848 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3852 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3854 ifbond_ref bond
= p
->po_bond
;
3855 lacp_actor_partner_tlv_ref partner
;
3857 /* compare the PDU's Actor information to our Partner state */
3858 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3859 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3860 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3861 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3862 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3863 != g_bond
->system_priority
)
3864 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3865 || (updateNTTBits(partner
->lap_state
)
3866 != updateNTTBits(p
->po_actor_state
))) {
3867 bondport_flags_set_ntt(p
);
3868 if (g_bond
->verbose
) {
3869 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3870 bondport_get_name(p
));
3877 bondport_AttachMuxToAggregator(bondport_ref p
)
3879 if (bondport_flags_mux_attached(p
) == 0) {
3880 if (g_bond
->verbose
) {
3881 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3882 bondport_get_name(p
));
3884 bondport_flags_set_mux_attached(p
);
3890 bondport_DetachMuxFromAggregator(bondport_ref p
)
3892 if (bondport_flags_mux_attached(p
)) {
3893 if (g_bond
->verbose
) {
3894 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3895 bondport_get_name(p
));
3897 bondport_flags_clear_mux_attached(p
);
3903 bondport_enable_distributing(bondport_ref p
)
3905 if (bondport_flags_distributing(p
) == 0) {
3906 ifbond_ref bond
= p
->po_bond
;
3908 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3909 if (g_bond
->verbose
) {
3910 timestamp_printf("[%s] Distribution Enabled\n",
3911 bondport_get_name(p
));
3913 bondport_flags_set_distributing(p
);
3919 bondport_disable_distributing(bondport_ref p
)
3921 if (bondport_flags_distributing(p
)) {
3922 bondport_ref
* array
;
3928 array
= bond
->ifb_distributing_array
;
3929 count
= bond
->ifb_distributing_count
;
3930 for (i
= 0; i
< count
; i
++) {
3931 if (array
[i
] == p
) {
3934 for (j
= i
; j
< (count
- 1); j
++) {
3935 array
[j
] = array
[j
+ 1];
3940 bond
->ifb_distributing_count
--;
3941 if (g_bond
->verbose
) {
3942 timestamp_printf("[%s] Distribution Disabled\n",
3943 bondport_get_name(p
));
3945 bondport_flags_clear_distributing(p
);
3951 ** Receive machine functions
3954 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3957 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3960 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3963 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3966 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3969 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3973 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
3976 switch (p
->po_receive_state
) {
3977 case ReceiveState_none
:
3978 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
3980 case ReceiveState_INITIALIZE
:
3981 bondport_receive_machine_initialize(p
, event
, event_data
);
3983 case ReceiveState_PORT_DISABLED
:
3984 bondport_receive_machine_port_disabled(p
, event
, event_data
);
3986 case ReceiveState_EXPIRED
:
3987 bondport_receive_machine_expired(p
, event
, event_data
);
3989 case ReceiveState_LACP_DISABLED
:
3990 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
3992 case ReceiveState_DEFAULTED
:
3993 bondport_receive_machine_defaulted(p
, event
, event_data
);
3995 case ReceiveState_CURRENT
:
3996 bondport_receive_machine_current(p
, event
, event_data
);
4005 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4010 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4011 bondport_receive_machine_current(p
, event
, event_data
);
4014 case LAEventMediaChange
:
4015 if (media_active(&p
->po_media_info
)) {
4016 switch (p
->po_receive_state
) {
4017 case ReceiveState_PORT_DISABLED
:
4018 case ReceiveState_LACP_DISABLED
:
4019 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4026 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4030 bondport_receive_machine_event(p
, event
, event_data
);
4037 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4038 __unused
void * event_data
)
4042 devtimer_cancel(p
->po_current_while_timer
);
4043 if (g_bond
->verbose
) {
4044 timestamp_printf("[%s] Receive INITIALIZE\n",
4045 bondport_get_name(p
));
4047 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4048 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4049 bondport_RecordDefault(p
);
4051 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4052 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4061 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4062 __unused
void * event_data
)
4064 partner_state_ref ps
;
4068 devtimer_cancel(p
->po_current_while_timer
);
4069 if (g_bond
->verbose
) {
4070 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4071 bondport_get_name(p
));
4073 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4074 ps
= &p
->po_partner_state
;
4075 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4077 case LAEventMediaChange
:
4078 if (media_active(&p
->po_media_info
)) {
4079 if (media_full_duplex(&p
->po_media_info
)) {
4080 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4083 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4086 else if (p
->po_selected
== SelectedState_SELECTED
) {
4089 if (g_bond
->verbose
) {
4090 timestamp_printf("[%s] Receive PORT_DISABLED: "
4091 "link timer started\n",
4092 bondport_get_name(p
));
4096 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4097 (devtimer_timeout_func
)
4098 bondport_receive_machine_port_disabled
,
4099 (void *)LAEventTimeout
, NULL
);
4101 else if (p
->po_selected
== SelectedState_STANDBY
) {
4102 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4105 case LAEventTimeout
:
4106 if (p
->po_selected
== SelectedState_SELECTED
) {
4107 if (g_bond
->verbose
) {
4108 timestamp_printf("[%s] Receive PORT_DISABLED: "
4109 "link timer completed, marking UNSELECTED\n",
4110 bondport_get_name(p
));
4112 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4115 case LAEventPortMoved
:
4116 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4125 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4126 __unused
void * event_data
)
4128 lacp_actor_partner_state s
;
4133 devtimer_cancel(p
->po_current_while_timer
);
4134 if (g_bond
->verbose
) {
4135 timestamp_printf("[%s] Receive EXPIRED\n",
4136 bondport_get_name(p
));
4138 p
->po_receive_state
= ReceiveState_EXPIRED
;
4139 s
= p
->po_partner_state
.ps_state
;
4140 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4141 s
= lacp_actor_partner_state_set_short_timeout(s
);
4142 p
->po_partner_state
.ps_state
= s
;
4144 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4145 /* start current_while timer */
4146 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4148 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4149 (devtimer_timeout_func
)
4150 bondport_receive_machine_expired
,
4151 (void *)LAEventTimeout
, NULL
);
4154 case LAEventTimeout
:
4155 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4164 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4165 __unused
void * event_data
)
4167 partner_state_ref ps
;
4170 devtimer_cancel(p
->po_current_while_timer
);
4171 if (g_bond
->verbose
) {
4172 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4173 bondport_get_name(p
));
4175 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4176 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4177 bondport_RecordDefault(p
);
4178 ps
= &p
->po_partner_state
;
4179 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4181 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4190 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4191 __unused
void * event_data
)
4195 devtimer_cancel(p
->po_current_while_timer
);
4196 if (g_bond
->verbose
) {
4197 timestamp_printf("[%s] Receive DEFAULTED\n",
4198 bondport_get_name(p
));
4200 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4201 bondport_UpdateDefaultSelected(p
);
4202 bondport_RecordDefault(p
);
4204 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4213 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4216 partner_state_ref ps
;
4221 devtimer_cancel(p
->po_current_while_timer
);
4222 if (g_bond
->verbose
) {
4223 timestamp_printf("[%s] Receive CURRENT\n",
4224 bondport_get_name(p
));
4226 p
->po_receive_state
= ReceiveState_CURRENT
;
4227 bondport_UpdateSelected(p
, event_data
);
4228 bondport_UpdateNTT(p
, event_data
);
4229 bondport_RecordPDU(p
, event_data
);
4231 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4232 bondport_assign_to_LAG(p
);
4233 /* start current_while timer */
4234 ps
= &p
->po_partner_state
;
4235 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4236 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4239 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4242 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4243 (devtimer_timeout_func
)
4244 bondport_receive_machine_current
,
4245 (void *)LAEventTimeout
, NULL
);
4247 case LAEventTimeout
:
4248 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4257 ** Periodic Transmission machine
4261 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4262 __unused
void * event_data
)
4265 partner_state_ref ps
;
4270 if (g_bond
->verbose
) {
4271 timestamp_printf("[%s] periodic_transmit Start\n",
4272 bondport_get_name(p
));
4275 case LAEventMediaChange
:
4276 devtimer_cancel(p
->po_periodic_timer
);
4277 p
->po_periodic_interval
= 0;
4278 if (media_active(&p
->po_media_info
) == 0
4279 || media_full_duplex(&p
->po_media_info
) == 0) {
4283 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4284 ps
= &p
->po_partner_state
;
4285 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4286 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4288 devtimer_cancel(p
->po_periodic_timer
);
4289 p
->po_periodic_interval
= 0;
4292 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4293 interval
= LACP_FAST_PERIODIC_TIME
;
4296 interval
= LACP_SLOW_PERIODIC_TIME
;
4298 if (p
->po_periodic_interval
!= interval
) {
4299 if (interval
== LACP_FAST_PERIODIC_TIME
4300 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4301 if (g_bond
->verbose
) {
4302 timestamp_printf("[%s] periodic_transmit:"
4303 " Need To Transmit\n",
4304 bondport_get_name(p
));
4306 bondport_flags_set_ntt(p
);
4308 p
->po_periodic_interval
= interval
;
4310 tv
.tv_sec
= interval
;
4311 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4312 (devtimer_timeout_func
)
4313 bondport_periodic_transmit_machine
,
4314 (void *)LAEventTimeout
, NULL
);
4315 if (g_bond
->verbose
) {
4316 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4317 bondport_get_name(p
),
4318 p
->po_periodic_interval
);
4322 case LAEventTimeout
:
4323 bondport_flags_set_ntt(p
);
4324 tv
.tv_sec
= p
->po_periodic_interval
;
4326 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4327 bondport_periodic_transmit_machine
,
4328 (void *)LAEventTimeout
, NULL
);
4329 if (g_bond
->verbose
> 1) {
4330 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4331 bondport_get_name(p
), p
->po_periodic_interval
);
4344 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4345 __darwin_time_t
* next_secs
)
4347 if (p
->po_last_transmit_secs
!= current_secs
) {
4348 p
->po_last_transmit_secs
= current_secs
;
4349 p
->po_n_transmit
= 0;
4351 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4355 if (next_secs
!= NULL
) {
4356 *next_secs
= current_secs
+ 1;
4362 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4365 lacp_actor_partner_tlv_ref aptlv
;
4366 lacp_collector_tlv_ref ctlv
;
4367 struct timeval next_tick_time
= {0, 0};
4368 lacpdu_ref out_lacpdu_p
;
4369 packet_buffer_ref pkt
;
4370 partner_state_ref ps
;
4374 case LAEventTimeout
:
4376 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4379 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4380 /* we're going away, transmit the packet no matter what */
4382 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4383 &next_tick_time
.tv_sec
) == 0) {
4384 if (devtimer_enabled(p
->po_transmit_timer
)) {
4385 if (g_bond
->verbose
> 0) {
4386 timestamp_printf("[%s] Transmit Timer Already Set\n",
4387 bondport_get_name(p
));
4391 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4392 (devtimer_timeout_func
)
4393 bondport_transmit_machine
,
4394 (void *)LAEventTimeout
, NULL
);
4395 if (g_bond
->verbose
> 0) {
4396 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4397 bondport_get_name(p
),
4398 (int)next_tick_time
.tv_sec
);
4403 if (g_bond
->verbose
> 0) {
4404 if (event
== LAEventTimeout
) {
4405 timestamp_printf("[%s] Transmit Timer Complete\n",
4406 bondport_get_name(p
));
4409 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4411 printf("[%s] Transmit: failed to allocate packet buffer\n",
4412 bondport_get_name(p
));
4415 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4416 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4417 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4418 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4421 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4422 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4423 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4424 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4425 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4426 g_bond
->system_priority
);
4427 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4428 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4429 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4430 aptlv
->lap_state
= p
->po_actor_state
;
4433 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4434 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4435 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4436 ps
= &p
->po_partner_state
;
4437 ps_li
= &ps
->ps_lag_info
;
4438 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4439 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4440 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4441 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4442 ps_li
->li_system_priority
);
4443 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4444 aptlv
->lap_state
= ps
->ps_state
;
4447 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4448 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4449 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4451 bondport_slow_proto_transmit(p
, pkt
);
4452 bondport_flags_clear_ntt(p
);
4453 if (g_bond
->verbose
> 0) {
4454 timestamp_printf("[%s] Transmit Packet %d\n",
4455 bondport_get_name(p
), p
->po_n_transmit
);
4465 ** Mux machine functions
4469 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4472 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4475 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4479 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4483 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4485 switch (p
->po_mux_state
) {
4487 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4489 case MuxState_DETACHED
:
4490 bondport_mux_machine_detached(p
, event
, event_data
);
4492 case MuxState_WAITING
:
4493 bondport_mux_machine_waiting(p
, event
, event_data
);
4495 case MuxState_ATTACHED
:
4496 bondport_mux_machine_attached(p
, event
, event_data
);
4498 case MuxState_COLLECTING_DISTRIBUTING
:
4499 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4508 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4509 __unused
void * event_data
)
4511 lacp_actor_partner_state s
;
4515 devtimer_cancel(p
->po_wait_while_timer
);
4516 if (g_bond
->verbose
) {
4517 timestamp_printf("[%s] Mux DETACHED\n",
4518 bondport_get_name(p
));
4520 p
->po_mux_state
= MuxState_DETACHED
;
4521 bondport_flags_clear_ready(p
);
4522 bondport_DetachMuxFromAggregator(p
);
4523 bondport_disable_distributing(p
);
4524 s
= p
->po_actor_state
;
4525 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4526 s
= lacp_actor_partner_state_set_not_collecting(s
);
4527 s
= lacp_actor_partner_state_set_not_distributing(s
);
4528 p
->po_actor_state
= s
;
4529 bondport_flags_set_ntt(p
);
4531 case LAEventSelectedChange
:
4533 case LAEventMediaChange
:
4534 if (p
->po_selected
== SelectedState_SELECTED
4535 || p
->po_selected
== SelectedState_STANDBY
) {
4536 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4546 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4547 __unused
void * event_data
)
4553 devtimer_cancel(p
->po_wait_while_timer
);
4554 if (g_bond
->verbose
) {
4555 timestamp_printf("[%s] Mux WAITING\n",
4556 bondport_get_name(p
));
4558 p
->po_mux_state
= MuxState_WAITING
;
4561 case LAEventSelectedChange
:
4562 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4563 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4566 if (p
->po_selected
== SelectedState_STANDBY
) {
4567 devtimer_cancel(p
->po_wait_while_timer
);
4568 /* wait until state changes to SELECTED */
4569 if (g_bond
->verbose
) {
4570 timestamp_printf("[%s] Mux WAITING: Standby\n",
4571 bondport_get_name(p
));
4575 if (bondport_flags_ready(p
)) {
4576 if (g_bond
->verbose
) {
4577 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4578 bondport_get_name(p
));
4582 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4583 if (g_bond
->verbose
) {
4584 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4585 bondport_get_name(p
));
4589 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4590 devtimer_cancel(p
->po_wait_while_timer
);
4591 if (g_bond
->verbose
) {
4592 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4593 bondport_get_name(p
));
4595 bondport_flags_set_ready(p
);
4598 if (g_bond
->verbose
) {
4599 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4600 bondport_get_name(p
));
4602 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4604 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4605 (devtimer_timeout_func
)
4606 bondport_mux_machine_waiting
,
4607 (void *)LAEventTimeout
, NULL
);
4609 case LAEventTimeout
:
4610 if (g_bond
->verbose
) {
4611 timestamp_printf("[%s] Mux WAITING: Ready\n",
4612 bondport_get_name(p
));
4614 bondport_flags_set_ready(p
);
4618 if (bondport_flags_ready(p
)){
4619 if (g_bond
->verbose
) {
4620 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4621 bondport_get_name(p
));
4623 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4632 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4633 __unused
void * event_data
)
4635 lacp_actor_partner_state s
;
4639 devtimer_cancel(p
->po_wait_while_timer
);
4640 if (g_bond
->verbose
) {
4641 timestamp_printf("[%s] Mux ATTACHED\n",
4642 bondport_get_name(p
));
4644 p
->po_mux_state
= MuxState_ATTACHED
;
4645 bondport_AttachMuxToAggregator(p
);
4646 s
= p
->po_actor_state
;
4647 s
= lacp_actor_partner_state_set_in_sync(s
);
4648 s
= lacp_actor_partner_state_set_not_collecting(s
);
4649 s
= lacp_actor_partner_state_set_not_distributing(s
);
4650 bondport_disable_distributing(p
);
4651 p
->po_actor_state
= s
;
4652 bondport_flags_set_ntt(p
);
4655 switch (p
->po_selected
) {
4656 case SelectedState_SELECTED
:
4657 s
= p
->po_partner_state
.ps_state
;
4658 if (lacp_actor_partner_state_in_sync(s
)) {
4659 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4664 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4673 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4675 __unused
void * event_data
)
4677 lacp_actor_partner_state s
;
4681 devtimer_cancel(p
->po_wait_while_timer
);
4682 if (g_bond
->verbose
) {
4683 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4684 bondport_get_name(p
));
4686 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4687 bondport_enable_distributing(p
);
4688 s
= p
->po_actor_state
;
4689 s
= lacp_actor_partner_state_set_collecting(s
);
4690 s
= lacp_actor_partner_state_set_distributing(s
);
4691 p
->po_actor_state
= s
;
4692 bondport_flags_set_ntt(p
);
4695 s
= p
->po_partner_state
.ps_state
;
4696 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4697 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4700 switch (p
->po_selected
) {
4701 case SelectedState_UNSELECTED
:
4702 case SelectedState_STANDBY
:
4703 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);