2 * Copyright (c) 2004-2014 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 */
807 static __inline__
int
808 media_active(const struct media_info
* mi
)
810 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
813 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
816 static __inline__
int
817 media_full_duplex(const struct media_info
* mi
)
819 return ((mi
->mi_active
& IFM_FDX
) != 0);
822 static __inline__
int
823 media_speed(const struct media_info
* mi
)
825 return (link_speed(mi
->mi_active
));
828 static struct media_info
829 interface_media_info(struct ifnet
* ifp
)
831 struct ifmediareq ifmr
;
832 struct media_info mi
;
834 bzero(&mi
, sizeof(mi
));
835 bzero(&ifmr
, sizeof(ifmr
));
836 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
837 if (ifmr
.ifm_count
!= 0) {
838 mi
.mi_status
= ifmr
.ifm_status
;
839 mi
.mi_active
= ifmr
.ifm_active
;
846 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
851 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
852 * currently expects it that way
854 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
855 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
856 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
857 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
863 static bond_globals_ref
864 bond_globals_create(lacp_system_priority sys_pri
,
869 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
);
873 bzero(b
, sizeof(*b
));
874 TAILQ_INIT(&b
->ifbond_list
);
876 b
->system_priority
= sys_pri
;
881 bond_globals_init(void)
887 bond_assert_lock_not_held();
889 if (g_bond
!= NULL
) {
894 * use en0's ethernet address as the system identifier, and if it's not
895 * there, use en1 .. en3
898 for (i
= 0; i
< 4; i
++) {
899 char ifname
[IFNAMSIZ
+1];
900 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
901 ifp
= ifunit(ifname
);
908 b
= bond_globals_create(0x8000, (lacp_system_ref
)IF_LLADDR(ifp
));
911 if (g_bond
!= NULL
) {
928 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
929 const struct ether_header
* eh_p
,
930 u_int16_t vlan_tag
, bpf_packet_func func
)
932 struct ether_vlan_header
* vlh_p
;
935 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
939 /* populate a new mbuf containing the vlan ethernet header */
940 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
941 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
942 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
943 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
944 vlh_p
->evl_tag
= htons(vlan_tag
);
945 vlh_p
->evl_proto
= eh_p
->ether_type
;
953 static __inline__
void
954 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
955 bpf_packet_func func
)
958 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
959 const struct ether_header
* eh_p
;
960 eh_p
= mtod(m
, const struct ether_header
*);
961 m
->m_data
+= ETHER_HDR_LEN
;
962 m
->m_len
-= ETHER_HDR_LEN
;
963 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
964 m
->m_data
-= ETHER_HDR_LEN
;
965 m
->m_len
+= ETHER_HDR_LEN
;
973 static __inline__
void
974 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
975 bpf_packet_func func
)
978 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
979 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
981 /* restore the header */
982 m
->m_data
-= ETHER_HDR_LEN
;
983 m
->m_len
+= ETHER_HDR_LEN
;
985 m
->m_data
+= ETHER_HDR_LEN
;
986 m
->m_len
-= ETHER_HDR_LEN
;
993 * Function: bond_setmulti
995 * Enable multicast reception on "our" interface by enabling multicasts on
996 * each of the member ports.
999 bond_setmulti(struct ifnet
* ifp
)
1007 ifb
= ifnet_softc(ifp
);
1008 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1009 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1014 ifbond_wait(ifb
, "bond_setmulti");
1016 if (ifbond_flags_if_detaching(ifb
)) {
1017 /* someone destroyed the bond while we were waiting */
1023 /* ifbond_wait() let's us safely walk the list without holding the lock */
1024 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1025 struct ifnet
* port_ifp
= p
->po_ifp
;
1027 error
= multicast_list_program(&p
->po_multicast
,
1030 printf("bond_setmulti(%s): "
1031 "multicast_list_program(%s%d) failed, %d\n",
1032 ifb
->ifb_name
, ifnet_name(port_ifp
),
1033 ifnet_unit(port_ifp
), error
);
1039 ifbond_signal(ifb
, "bond_setmulti");
1041 ifbond_release(ifb
);
1046 bond_clone_attach(void)
1050 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1057 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1060 struct ifmultiaddr
* ifma
= NULL
;
1061 struct sockaddr_dl sdl
;
1063 bond_assert_lock_not_held();
1065 bzero(&sdl
, sizeof(sdl
));
1066 sdl
.sdl_len
= sizeof(sdl
);
1067 sdl
.sdl_family
= AF_LINK
;
1068 sdl
.sdl_type
= IFT_ETHER
;
1070 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1071 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1072 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1074 ifb
->ifb_ifma_slow_proto
= ifma
;
1080 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1085 struct ifnet_init_eparams bond_init
;
1087 error
= bond_globals_init();
1092 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
);
1096 bzero(ifb
, sizeof(*ifb
));
1099 TAILQ_INIT(&ifb
->ifb_port_list
);
1100 TAILQ_INIT(&ifb
->ifb_lag_list
);
1101 ifb
->ifb_key
= unit
+ 1;
1103 /* use the interface name as the unique id for ifp recycle */
1104 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1105 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1106 ifbond_release(ifb
);
1110 bzero(&bond_init
, sizeof(bond_init
));
1111 bond_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1112 bond_init
.len
= sizeof (bond_init
);
1113 bond_init
.flags
= IFNET_INIT_LEGACY
;
1114 bond_init
.uniqueid
= ifb
->ifb_name
;
1115 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1116 bond_init
.name
= ifc
->ifc_name
;
1117 bond_init
.unit
= unit
;
1118 bond_init
.family
= IFNET_FAMILY_BOND
;
1119 bond_init
.type
= IFT_IEEE8023ADLAG
;
1120 bond_init
.output
= bond_output
;
1121 bond_init
.demux
= ether_demux
;
1122 bond_init
.add_proto
= ether_add_proto
;
1123 bond_init
.del_proto
= ether_del_proto
;
1124 bond_init
.check_multi
= ether_check_multi
;
1125 bond_init
.framer_extended
= ether_frameout_extended
;
1126 bond_init
.ioctl
= bond_ioctl
;
1127 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1128 bond_init
.detach
= bond_if_free
;
1129 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1130 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1131 bond_init
.softc
= ifb
;
1132 error
= ifnet_allocate_extended(&bond_init
, &ifp
);
1135 ifbond_release(ifb
);
1140 ifnet_set_offload(ifp
, 0);
1141 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1142 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1143 ifnet_set_baudrate(ifp
, 0);
1144 ifnet_set_mtu(ifp
, 0);
1146 error
= ifnet_attach(ifp
, NULL
);
1149 ifbond_release(ifb
);
1152 error
= ifbond_add_slow_proto_multicast(ifb
);
1154 printf("bond_clone_create(%s): "
1155 "failed to add slow_proto multicast, %d\n",
1156 ifb
->ifb_name
, error
);
1159 /* attach as ethernet */
1160 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1163 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1170 bond_remove_all_interfaces(ifbond_ref ifb
)
1174 bond_assert_lock_held();
1177 * do this in reverse order to avoid re-programming the mac address
1178 * as each head interface is removed
1180 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1181 bond_remove_interface(ifb
, p
->po_ifp
);
1187 bond_remove(ifbond_ref ifb
)
1189 bond_assert_lock_held();
1190 ifbond_flags_set_if_detaching(ifb
);
1191 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1192 bond_remove_all_interfaces(ifb
);
1197 bond_if_detach(struct ifnet
* ifp
)
1201 error
= ifnet_detach(ifp
);
1203 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1204 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1211 bond_clone_destroy(struct ifnet
* ifp
)
1216 ifb
= ifnet_softc(ifp
);
1217 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1221 if (ifbond_flags_if_detaching(ifb
)) {
1227 bond_if_detach(ifp
);
1232 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1237 ifb
= ifnet_softc(ifp
);
1238 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1243 case BPF_TAP_DISABLE
:
1244 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1248 ifb
->ifb_bpf_input
= func
;
1251 case BPF_TAP_OUTPUT
:
1252 ifb
->ifb_bpf_output
= func
;
1255 case BPF_TAP_INPUT_OUTPUT
:
1256 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1266 ether_header_hash(struct ether_header
* eh_p
)
1270 /* get 32-bits from destination ether and ether type */
1271 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1273 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1277 static struct mbuf
*
1278 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1283 while (*offset
>= len
) {
1294 #if BYTE_ORDER == BIG_ENDIAN
1295 static __inline__
uint32_t
1296 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1298 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1299 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1301 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1302 static __inline__
uint32_t
1303 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1305 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1306 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1308 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1311 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1313 struct mbuf
* current
;
1314 u_char
* current_data
;
1319 current
= S_mbuf_skip_to_offset(m
, &offset
);
1320 if (current
== NULL
) {
1323 current_data
= mtod(current
, u_char
*) + offset
;
1324 space_current
= current
->m_len
- offset
;
1325 if (space_current
>= (int)sizeof(uint32_t)) {
1326 *val
= *((uint32_t *)current_data
);
1329 next
= current
->m_next
;
1330 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1333 next_data
= mtod(next
, u_char
*);
1334 switch (space_current
) {
1336 *val
= make_uint32(current_data
[0], next_data
[0],
1337 next_data
[1], next_data
[2]);
1340 *val
= make_uint32(current_data
[0], current_data
[1],
1341 next_data
[0], next_data
[1]);
1344 *val
= make_uint32(current_data
[0], current_data
[1],
1345 current_data
[2], next_data
[0]);
1351 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1352 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1355 ip_header_hash(struct mbuf
* m
)
1358 struct in_addr ip_dst
;
1359 struct in_addr ip_src
;
1362 struct mbuf
* orig_m
= m
;
1364 /* find the IP protocol field relative to the start of the packet */
1365 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1366 m
= S_mbuf_skip_to_offset(m
, &offset
);
1367 if (m
== NULL
|| m
->m_len
< 1) {
1370 data
= mtod(m
, u_char
*) + offset
;
1373 /* find the IP src relative to the IP protocol */
1374 if ((m
->m_len
- offset
)
1375 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1376 /* this should be the normal case */
1377 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1378 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1381 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1382 (uint32_t *)&ip_src
.s_addr
)) {
1385 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1386 (uint32_t *)&ip_dst
.s_addr
)) {
1390 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1393 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1396 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1398 ipv6_header_hash(struct mbuf
* m
)
1403 struct mbuf
* orig_m
= m
;
1407 /* find the IP protocol field relative to the start of the packet */
1408 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1409 m
= S_mbuf_skip_to_offset(m
, &offset
);
1411 goto bad_ipv6_packet
;
1413 data
= mtod(m
, u_char
*) + offset
;
1415 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1416 /* this should be the normal case */
1417 for (i
= 0, scan
= (uint32_t *)data
;
1418 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1424 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1426 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1427 (uint32_t *)&tmp
)) {
1428 goto bad_ipv6_packet
;
1433 return (ntohl(val
));
1436 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1440 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1442 bpf_packet_func bpf_func
;
1445 struct ifnet
* port_ifp
= NULL
;
1447 struct flowadv adv
= { FADV_SUCCESS
};
1452 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1456 if (m
->m_pkthdr
.pkt_flowid
!= 0) {
1457 h
= m
->m_pkthdr
.pkt_flowid
;
1460 struct ether_header
* eh_p
;
1462 eh_p
= mtod(m
, struct ether_header
*);
1463 switch (ntohs(eh_p
->ether_type
)) {
1465 h
= ip_header_hash(m
);
1467 case ETHERTYPE_IPV6
:
1468 h
= ipv6_header_hash(m
);
1471 h
= ether_header_hash(eh_p
);
1476 ifb
= ifnet_softc(ifp
);
1477 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1478 || ifb
->ifb_distributing_count
== 0) {
1481 h
%= ifb
->ifb_distributing_count
;
1482 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1483 bpf_func
= ifb
->ifb_bpf_output
;
1486 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1487 (void)ifnet_stat_increment_out(ifp
, 1,
1488 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1491 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1493 bond_bpf_output(ifp
, m
, bpf_func
);
1495 err
= dlil_output(port_ifp
, PF_BOND
, m
, NULL
, NULL
, 1, &adv
);
1498 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1500 } else if (adv
.code
== FADV_SUSPENDED
) {
1514 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1517 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1518 if (p
->po_ifp
== port_ifp
) {
1526 bond_lookup_port(struct ifnet
* port_ifp
)
1531 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1532 port
= ifbond_lookup_port(ifb
, port_ifp
);
1541 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1543 struct ifnet
* bond_ifp
= NULL
;
1549 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1552 p
= bond_lookup_port(port_ifp
);
1556 if (p
->po_enabled
== 0) {
1560 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1563 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1564 if (ifbond_selection(ifb
)) {
1565 event_code
= (ifb
->ifb_active_lag
== NULL
)
1568 /* XXX need to take a reference on bond_ifp */
1569 bond_ifp
= ifb
->ifb_ifp
;
1570 ifb
->ifb_last_link_event
= event_code
;
1573 event_code
= (ifb
->ifb_active_lag
== NULL
)
1576 if (event_code
!= ifb
->ifb_last_link_event
) {
1577 if (g_bond
->verbose
) {
1578 timestamp_printf("%s: (receive) generating LINK event\n",
1581 bond_ifp
= ifb
->ifb_ifp
;
1582 ifb
->ifb_last_link_event
= event_code
;
1588 if (bond_ifp
!= NULL
) {
1589 interface_link_event(bond_ifp
, event_code
);
1596 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1598 la_marker_pdu_ref marker_p
;
1601 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1602 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1606 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1610 p
= bond_lookup_port(port_ifp
);
1611 if (p
== NULL
|| p
->po_enabled
== 0
1612 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1616 /* echo back the same packet as a marker response */
1617 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1618 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1628 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1629 char * frame_header
)
1631 bpf_packet_func bpf_func
;
1632 const struct ether_header
* eh_p
;
1637 eh_p
= (const struct ether_header
*)frame_header
;
1638 if ((m
->m_flags
& M_MCAST
) != 0
1639 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1640 sizeof(eh_p
->ether_dhost
)) == 0
1641 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1642 u_char subtype
= *mtod(m
, u_char
*);
1644 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1645 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1650 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1651 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1656 bond_receive_lacpdu(m
, port_ifp
);
1659 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1662 /* restore the ethernet header pointer in the mbuf */
1663 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1664 m
->m_data
-= ETHER_HDR_LEN
;
1665 m
->m_len
+= ETHER_HDR_LEN
;
1666 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1667 if (m
->m_pkthdr
.len
< min_size
) {
1672 if (m
->m_len
< min_size
) {
1673 m
= m_pullup(m
, min_size
);
1678 /* send to marker responder */
1679 bond_receive_la_marker_pdu(m
, port_ifp
);
1682 else if (subtype
== 0
1683 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1684 /* invalid subtype, discard the frame */
1690 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1693 p
= bond_lookup_port(port_ifp
);
1694 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1698 /* make the packet appear as if it arrived on the bonded interface */
1701 bpf_func
= ifb
->ifb_bpf_input
;
1704 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1705 (void)ifnet_stat_increment_in(ifp
, 1,
1706 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1707 + ETHER_VLAN_ENCAP_LEN
), 0);
1710 (void)ifnet_stat_increment_in(ifp
, 1,
1711 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1713 m
->m_pkthdr
.rcvif
= ifp
;
1714 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1715 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1716 dlil_input_packet_list(ifp
, m
);
1725 static __inline__
const char *
1726 bondport_get_name(bondport_ref p
)
1728 return (p
->po_name
);
1731 static __inline__
int
1732 bondport_get_index(bondport_ref p
)
1734 return (ifnet_index(p
->po_ifp
));
1738 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1740 struct ether_header
* eh_p
;
1743 /* packet_buffer_allocate leaves room for ethernet header */
1744 eh_p
= mtod(buf
, struct ether_header
*);
1745 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1746 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1747 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1748 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1750 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1751 bondport_get_name(p
), error
);
1757 bondport_timer_process_func(devtimer_ref timer
,
1758 devtimer_process_func_event event
)
1763 case devtimer_process_func_event_lock
:
1765 devtimer_retain(timer
);
1767 case devtimer_process_func_event_unlock
:
1768 if (devtimer_valid(timer
)) {
1769 /* as long as the devtimer is valid, we can look at arg0 */
1771 struct ifnet
* bond_ifp
= NULL
;
1773 p
= (bondport_ref
)devtimer_arg0(timer
);
1774 if (ifbond_selection(p
->po_bond
)) {
1775 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1778 /* XXX need to take a reference on bond_ifp */
1779 bond_ifp
= p
->po_bond
->ifb_ifp
;
1780 p
->po_bond
->ifb_last_link_event
= event_code
;
1783 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1786 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1787 if (g_bond
->verbose
) {
1788 timestamp_printf("%s: (timer) generating LINK event\n",
1789 p
->po_bond
->ifb_name
);
1791 bond_ifp
= p
->po_bond
->ifb_ifp
;
1792 p
->po_bond
->ifb_last_link_event
= event_code
;
1795 devtimer_release(timer
);
1797 if (bond_ifp
!= NULL
) {
1798 interface_link_event(bond_ifp
, event_code
);
1802 /* timer is going away */
1803 devtimer_release(timer
);
1813 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1814 int active
, int short_timeout
, int * ret_error
)
1817 bondport_ref p
= NULL
;
1818 lacp_actor_partner_state s
;
1821 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
);
1823 *ret_error
= ENOMEM
;
1826 bzero(p
, sizeof(*p
));
1827 multicast_list_init(&p
->po_multicast
);
1828 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1829 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1830 >= sizeof(p
->po_name
)) {
1831 printf("if_bond: name too large\n");
1832 *ret_error
= EINVAL
;
1835 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1837 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1838 bondport_get_name(p
), error
);
1841 /* remember the current interface MTU so it can be restored */
1842 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1843 p
->po_ifp
= port_ifp
;
1844 p
->po_media_info
= interface_media_info(port_ifp
);
1845 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1846 if (p
->po_current_while_timer
== NULL
) {
1847 *ret_error
= ENOMEM
;
1850 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1851 if (p
->po_periodic_timer
== NULL
) {
1852 *ret_error
= ENOMEM
;
1855 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1856 if (p
->po_wait_while_timer
== NULL
) {
1857 *ret_error
= ENOMEM
;
1860 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1861 if (p
->po_transmit_timer
== NULL
) {
1862 *ret_error
= ENOMEM
;
1865 p
->po_receive_state
= ReceiveState_none
;
1866 p
->po_mux_state
= MuxState_none
;
1867 p
->po_priority
= priority
;
1869 s
= lacp_actor_partner_state_set_aggregatable(s
);
1870 if (short_timeout
) {
1871 s
= lacp_actor_partner_state_set_short_timeout(s
);
1874 s
= lacp_actor_partner_state_set_active_lacp(s
);
1876 p
->po_actor_state
= s
;
1885 bondport_start(bondport_ref p
)
1887 bondport_receive_machine(p
, LAEventStart
, NULL
);
1888 bondport_mux_machine(p
, LAEventStart
, NULL
);
1889 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1890 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1895 * Function: bondport_invalidate_timers
1897 * Invalidate all of the timers for the bondport.
1900 bondport_invalidate_timers(bondport_ref p
)
1902 devtimer_invalidate(p
->po_current_while_timer
);
1903 devtimer_invalidate(p
->po_periodic_timer
);
1904 devtimer_invalidate(p
->po_wait_while_timer
);
1905 devtimer_invalidate(p
->po_transmit_timer
);
1909 * Function: bondport_cancel_timers
1911 * Cancel all of the timers for the bondport.
1914 bondport_cancel_timers(bondport_ref p
)
1916 devtimer_cancel(p
->po_current_while_timer
);
1917 devtimer_cancel(p
->po_periodic_timer
);
1918 devtimer_cancel(p
->po_wait_while_timer
);
1919 devtimer_cancel(p
->po_transmit_timer
);
1923 bondport_free(bondport_ref p
)
1925 multicast_list_remove(&p
->po_multicast
);
1926 devtimer_release(p
->po_current_while_timer
);
1927 devtimer_release(p
->po_periodic_timer
);
1928 devtimer_release(p
->po_wait_while_timer
);
1929 devtimer_release(p
->po_transmit_timer
);
1934 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1935 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1936 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1937 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1939 static __inline__
int
1940 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1942 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1943 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1947 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1954 bondport_ref
* new_array
= NULL
;
1955 bondport_ref
* old_array
= NULL
;
1959 /* pre-allocate space for new port */
1960 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1965 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1966 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1969 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1972 /* make sure this interface can handle our current MTU */
1973 devmtu
= bond_device_mtu(ifp
, ifb
);
1975 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
1977 printf("if_bond: interface %s doesn't support mtu %d",
1978 bondport_get_name(p
), devmtu
);
1983 /* make sure ifb doesn't get de-allocated while we wait */
1986 /* wait for other add or remove to complete */
1987 ifbond_wait(ifb
, "bond_add_interface");
1989 if (ifbond_flags_if_detaching(ifb
)) {
1990 /* someone destroyed the bond while we were waiting */
1994 if (bond_lookup_port(port_ifp
) != NULL
) {
1995 /* port is already part of a bond */
1999 ifnet_lock_exclusive(port_ifp
);
2000 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2001 /* interface already has VLAN's, or is part of bond */
2002 ifnet_lock_done(port_ifp
);
2007 /* mark the interface busy */
2008 /* can't use ifnet_set_eflags because that takes the lock */
2009 port_ifp
->if_eflags
|= IFEF_BOND
;
2010 ifnet_lock_done(port_ifp
);
2012 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2013 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2014 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2015 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2019 ifnet_offload_t ifp_offload
;
2020 ifnet_offload_t port_ifp_offload
;
2022 ifp_offload
= ifnet_offload(ifp
);
2023 port_ifp_offload
= ifnet_offload(port_ifp
);
2024 if (ifp_offload
!= port_ifp_offload
) {
2025 ifnet_offload_t offload
;
2027 offload
= ifp_offload
& port_ifp_offload
;
2028 printf("bond_add_interface(%s, %s) "
2029 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2030 ifb
->ifb_name
, bondport_get_name(p
),
2031 ifp_offload
, port_ifp_offload
, offload
);
2034 * if the bond has VLAN's, we can't simply change the hwassist
2035 * field behind its back: this needs work
2037 ifnet_set_offload(ifp
, offload
);
2042 /* remember the port's ethernet address so it can be restored */
2043 ether_addr_copy(&p
->po_saved_addr
, IF_LLADDR(port_ifp
));
2045 /* add it to the list of ports */
2046 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2047 ifb
->ifb_port_count
++;
2049 /* set the default MTU */
2050 if (ifnet_mtu(ifp
) == 0) {
2051 ifnet_set_mtu(ifp
, ETHERMTU
);
2056 /* first port added to bond determines bond's ethernet address */
2058 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(port_ifp
), ETHER_ADDR_LEN
,
2062 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2064 /* allocate a larger distributing array */
2065 new_array
= (bondport_ref
*)
2066 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2067 if (new_array
== NULL
) {
2072 /* attach our BOND "protocol" to the interface */
2073 error
= bond_attach_protocol(port_ifp
);
2077 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2079 /* set the interface MTU */
2080 devmtu
= bond_device_mtu(ifp
, ifb
);
2081 error
= siocsifmtu(port_ifp
, devmtu
);
2083 printf("bond_add_interface(%s, %s):"
2084 " SIOCSIFMTU %d failed %d\n",
2085 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2088 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2090 /* program the port with our multicast addresses */
2091 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2093 printf("bond_add_interface(%s, %s):"
2094 " multicast_list_program failed %d\n",
2095 ifb
->ifb_name
, bondport_get_name(p
), error
);
2099 /* mark the interface up */
2100 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2102 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2104 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2105 ifb
->ifb_name
, bondport_get_name(p
), error
);
2109 /* re-program the port's ethernet address */
2110 error
= if_siflladdr(port_ifp
,
2111 (const struct ether_addr
*)IF_LLADDR(ifp
));
2113 /* port doesn't support setting the link address */
2114 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2115 ifb
->ifb_name
, bondport_get_name(p
), error
);
2118 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2122 /* no failures past this point */
2125 /* copy the contents of the existing distributing array */
2126 if (ifb
->ifb_distributing_count
) {
2127 bcopy(ifb
->ifb_distributing_array
, new_array
,
2128 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2130 old_array
= ifb
->ifb_distributing_array
;
2131 ifb
->ifb_distributing_array
= new_array
;
2133 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2136 /* check if we need to generate a link status event */
2137 if (ifbond_selection(ifb
)) {
2138 event_code
= (ifb
->ifb_active_lag
== NULL
)
2141 ifb
->ifb_last_link_event
= event_code
;
2145 /* are we adding the first distributing interface? */
2146 if (media_active(&p
->po_media_info
)) {
2147 if (ifb
->ifb_distributing_count
== 0) {
2148 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2150 bondport_enable_distributing(p
);
2153 bondport_disable_distributing(p
);
2156 /* clear the busy state, and wakeup anyone waiting */
2157 ifbond_signal(ifb
, "bond_add_interface");
2159 if (event_code
!= 0) {
2160 interface_link_event(ifp
, event_code
);
2162 if (old_array
!= NULL
) {
2163 FREE(old_array
, M_BOND
);
2168 bond_assert_lock_not_held();
2170 /* if this was the first port to be added, clear our address */
2172 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2175 if (new_array
!= NULL
) {
2176 FREE(new_array
, M_BOND
);
2178 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2181 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2183 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2184 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2187 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2188 (void)bond_detach_protocol(port_ifp
);
2190 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2193 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2195 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2196 ifb
->ifb_name
, bondport_get_name(p
),
2197 p
->po_devmtu
.ifdm_current
, error1
);
2201 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2202 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2203 ifb
->ifb_port_count
--;
2205 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2206 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2207 ifb
->ifb_altmtu
= 0;
2208 ifnet_set_mtu(ifp
, 0);
2209 ifnet_set_offload(ifp
, 0);
2213 ifbond_signal(ifb
, "bond_add_interface");
2215 ifbond_release(ifb
);
2221 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2226 bondport_ref head_port
;
2229 int new_link_address
= FALSE
;
2231 lacp_actor_partner_state s
;
2232 int was_distributing
;
2234 bond_assert_lock_held();
2237 ifbond_wait(ifb
, "bond_remove_interface");
2239 p
= ifbond_lookup_port(ifb
, port_ifp
);
2242 /* it got removed by another thread */
2246 /* de-select it and remove it from the lists */
2247 was_distributing
= bondport_flags_distributing(p
);
2248 bondport_disable_distributing(p
);
2249 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2250 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2251 active_lag
= bondport_remove_from_LAG(p
);
2252 /* invalidate timers here while holding the bond_lock */
2253 bondport_invalidate_timers(p
);
2255 /* announce that we're Individual now */
2256 s
= p
->po_actor_state
;
2257 s
= lacp_actor_partner_state_set_individual(s
);
2258 s
= lacp_actor_partner_state_set_not_collecting(s
);
2259 s
= lacp_actor_partner_state_set_not_distributing(s
);
2260 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2261 p
->po_actor_state
= s
;
2262 bondport_flags_set_ntt(p
);
2265 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2266 ifb
->ifb_port_count
--;
2269 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2270 if (head_port
== NULL
) {
2271 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2272 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2275 ifnet_set_offload(ifp
, 0);
2276 ifnet_set_mtu(ifp
, 0);
2277 ifb
->ifb_altmtu
= 0;
2278 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2279 && bcmp(&p
->po_saved_addr
, IF_LLADDR(ifp
),
2280 ETHER_ADDR_LEN
) == 0) {
2281 new_link_address
= TRUE
;
2283 /* check if we need to generate a link status event */
2284 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2285 if (ifbond_selection(ifb
) || active_lag
) {
2286 event_code
= (ifb
->ifb_active_lag
== NULL
)
2289 ifb
->ifb_last_link_event
= event_code
;
2291 bondport_transmit_machine(p
, LAEventStart
,
2292 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2295 /* are we removing the last distributing interface? */
2296 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2297 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2304 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2306 else if (new_link_address
) {
2307 struct ifnet
* scan_ifp
;
2308 bondport_ref scan_port
;
2310 /* ifbond_wait() allows port list traversal without holding the lock */
2312 /* this port gave the bond its ethernet address, switch to new one */
2313 ifnet_set_lladdr_and_type(ifp
,
2314 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2317 /* re-program each port with the new link address */
2318 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2319 scan_ifp
= scan_port
->po_ifp
;
2321 error
= if_siflladdr(scan_ifp
,
2322 (const struct ether_addr
*) IF_LLADDR(ifp
));
2324 printf("bond_remove_interface(%s, %s): "
2325 "if_siflladdr (%s) failed %d\n",
2326 ifb
->ifb_name
, bondport_get_name(p
),
2327 bondport_get_name(scan_port
), error
);
2332 /* restore the port's ethernet address */
2333 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2335 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2336 ifb
->ifb_name
, bondport_get_name(p
), error
);
2339 /* restore the port's MTU */
2340 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2342 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2343 ifb
->ifb_name
, bondport_get_name(p
),
2344 p
->po_devmtu
.ifdm_current
, error
);
2347 /* remove the bond "protocol" */
2348 bond_detach_protocol(port_ifp
);
2350 /* generate link event */
2351 if (event_code
!= 0) {
2352 interface_link_event(ifp
, event_code
);
2357 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2358 /* release this bondport's reference to the ifbond */
2359 ifbond_release(ifb
);
2362 ifbond_signal(ifb
, "bond_remove_interface");
2363 ifbond_release(ifb
);
2368 bond_set_lacp_mode(ifbond_ref ifb
)
2372 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2373 bondport_disable_distributing(p
);
2380 bond_set_static_mode(ifbond_ref ifb
)
2383 lacp_actor_partner_state s
;
2385 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2386 bondport_disable_distributing(p
);
2387 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2388 (void)bondport_remove_from_LAG(p
);
2389 bondport_cancel_timers(p
);
2391 /* announce that we're Individual now */
2392 s
= p
->po_actor_state
;
2393 s
= lacp_actor_partner_state_set_individual(s
);
2394 s
= lacp_actor_partner_state_set_not_collecting(s
);
2395 s
= lacp_actor_partner_state_set_not_distributing(s
);
2396 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2397 p
->po_actor_state
= s
;
2398 bondport_flags_set_ntt(p
);
2399 bondport_transmit_machine(p
, LAEventStart
,
2400 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2402 p
->po_actor_state
= 0;
2403 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2405 if (media_active(&p
->po_media_info
)) {
2406 bondport_enable_distributing(p
);
2409 bondport_disable_distributing(p
);
2416 bond_set_mode(struct ifnet
* ifp
, int mode
)
2423 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2424 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2426 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2428 if (ifb
->ifb_mode
== mode
) {
2434 ifbond_wait(ifb
, "bond_set_mode");
2436 /* verify (again) that the mode is actually different */
2437 if (ifb
->ifb_mode
== mode
) {
2442 ifb
->ifb_mode
= mode
;
2443 if (mode
== IF_BOND_MODE_LACP
) {
2444 bond_set_lacp_mode(ifb
);
2446 /* check if we need to generate a link status event */
2447 if (ifbond_selection(ifb
)) {
2448 event_code
= (ifb
->ifb_active_lag
== NULL
)
2453 bond_set_static_mode(ifb
);
2454 event_code
= (ifb
->ifb_distributing_count
== 0)
2458 ifb
->ifb_last_link_event
= event_code
;
2461 ifbond_signal(ifb
, "bond_set_mode");
2463 ifbond_release(ifb
);
2465 if (event_code
!= 0) {
2466 interface_link_event(ifp
, event_code
);
2472 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2477 struct if_bond_status_req
* ibsr
;
2478 struct if_bond_status ibs
;
2481 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2482 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2485 ibsr
->ibsr_key
= ifb
->ifb_key
;
2486 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2487 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2488 dst
= proc_is64bit(current_proc())
2489 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2490 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2491 if (dst
== USER_ADDR_NULL
) {
2492 /* just want to know how many there are */
2495 if (ibsr
->ibsr_count
< 0) {
2498 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2499 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2500 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2501 struct if_bond_partner_state
* ibps_p
;
2502 partner_state_ref ps
;
2507 bzero(&ibs
, sizeof(ibs
));
2508 strlcpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2509 ibs
.ibs_port_priority
= port
->po_priority
;
2510 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2511 ibs
.ibs_state
= port
->po_actor_state
;
2512 ibs
.ibs_selected_state
= port
->po_selected
;
2513 ps
= &port
->po_partner_state
;
2514 ibps_p
= &ibs
.ibs_partner_state
;
2515 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2516 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2517 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2518 ibps_p
->ibps_port
= ps
->ps_port
;
2519 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2520 ibps_p
->ibps_state
= ps
->ps_state
;
2523 /* fake the selected information */
2524 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2525 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2527 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2537 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2540 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2546 bond_set_promisc(__unused
struct ifnet
*ifp
)
2553 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2559 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2560 mtu_min
= IF_MINMTU
;
2562 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2563 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2565 if (devmtu_p
->ifdm_min
> mtu_min
) {
2566 mtu_min
= devmtu_p
->ifdm_min
;
2568 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2569 mtu_max
= devmtu_p
->ifdm_max
;
2578 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2583 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2584 error
= siocsifmtu(p
->po_ifp
, mtu
);
2586 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2587 ifb
->ifb_name
, bondport_get_name(p
), error
);
2595 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2605 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2606 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2607 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2611 ifbond_wait(ifb
, "bond_set_mtu");
2614 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2618 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2619 if (mtu
> mtu_max
) {
2623 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2624 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2629 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2632 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2634 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2635 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2636 if (new_max
!= old_max
) {
2637 /* we can safely walk the list of port without the lock held */
2639 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2641 /* try our best to back out of it */
2642 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2648 ifb
->ifb_altmtu
= mtu
;
2651 ifnet_set_mtu(ifp
, mtu
);
2656 ifbond_signal(ifb
, "bond_set_mtu");
2657 ifbond_release(ifb
);
2665 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2668 struct if_bond_req ibr
;
2669 struct ifaddr
* ifa
;
2672 struct ifmediareq
*ifmr
;
2673 struct ifnet
* port_ifp
= NULL
;
2674 user_addr_t user_addr
;
2676 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2677 return (EOPNOTSUPP
);
2679 ifr
= (struct ifreq
*)data
;
2680 ifa
= (struct ifaddr
*)data
;
2684 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2687 case SIOCGIFMEDIA32
:
2688 case SIOCGIFMEDIA64
:
2690 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2691 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2693 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2695 ifmr
= (struct ifmediareq
*)data
;
2696 ifmr
->ifm_current
= IFM_ETHER
;
2698 ifmr
->ifm_status
= IFM_AVALID
;
2699 ifmr
->ifm_active
= IFM_ETHER
;
2700 ifmr
->ifm_count
= 1;
2701 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2702 if (ifb
->ifb_active_lag
!= NULL
) {
2703 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2704 ifmr
->ifm_status
|= IFM_ACTIVE
;
2707 else if (ifb
->ifb_distributing_count
> 0) {
2709 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2710 ifmr
->ifm_status
|= IFM_ACTIVE
;
2713 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2714 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2715 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2716 if (user_addr
!= USER_ADDR_NULL
) {
2717 error
= copyout(&ifmr
->ifm_current
,
2724 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2730 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2731 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2733 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2736 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2737 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2738 &ifr
->ifr_devmtu
.ifdm_max
);
2744 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2745 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2747 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2750 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2755 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2759 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2763 user_addr
= proc_is64bit(current_proc())
2764 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2765 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2769 switch (ibr
.ibr_op
) {
2770 case IF_BOND_OP_ADD_INTERFACE
:
2771 case IF_BOND_OP_REMOVE_INTERFACE
:
2772 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2773 if (port_ifp
== NULL
) {
2777 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2778 error
= EPROTONOSUPPORT
;
2782 case IF_BOND_OP_SET_VERBOSE
:
2783 case IF_BOND_OP_SET_MODE
:
2792 switch (ibr
.ibr_op
) {
2793 case IF_BOND_OP_ADD_INTERFACE
:
2794 error
= bond_add_interface(ifp
, port_ifp
);
2796 case IF_BOND_OP_REMOVE_INTERFACE
:
2798 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2799 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2801 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2803 error
= bond_remove_interface(ifb
, port_ifp
);
2806 case IF_BOND_OP_SET_VERBOSE
:
2808 if (g_bond
== NULL
) {
2813 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2816 case IF_BOND_OP_SET_MODE
:
2817 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2818 case IF_BOND_MODE_LACP
:
2819 case IF_BOND_MODE_STATIC
:
2828 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2831 break; /* SIOCSIFBOND */
2834 user_addr
= proc_is64bit(current_proc())
2835 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2836 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2840 switch (ibr
.ibr_op
) {
2841 case IF_BOND_OP_GET_STATUS
:
2851 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2852 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2854 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2856 switch (ibr
.ibr_op
) {
2857 case IF_BOND_OP_GET_STATUS
:
2858 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2862 break; /* SIOCGIFBOND */
2869 /* enable/disable promiscuous mode */
2871 error
= bond_set_promisc(ifp
);
2877 error
= bond_setmulti(ifp
);
2886 bond_if_free(struct ifnet
* ifp
)
2894 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2899 ifbond_release(ifb
);
2906 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
2908 struct ifnet
* bond_ifp
= NULL
;
2910 int old_distributing_count
;
2912 struct media_info media_info
= { 0, 0};
2914 switch (event_code
) {
2915 case KEV_DL_IF_DETACHED
:
2917 case KEV_DL_LINK_OFF
:
2918 case KEV_DL_LINK_ON
:
2919 media_info
= interface_media_info(port_ifp
);
2925 p
= bond_lookup_port(port_ifp
);
2931 old_distributing_count
= ifb
->ifb_distributing_count
;
2932 switch (event_code
) {
2933 case KEV_DL_IF_DETACHED
:
2934 bond_remove_interface(ifb
, p
->po_ifp
);
2936 case KEV_DL_LINK_OFF
:
2937 case KEV_DL_LINK_ON
:
2938 p
->po_media_info
= media_info
;
2939 if (p
->po_enabled
) {
2940 bondport_link_status_changed(p
);
2944 /* generate a link-event */
2945 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2946 if (ifbond_selection(ifb
)) {
2947 event_code
= (ifb
->ifb_active_lag
== NULL
)
2950 /* XXX need to take a reference on bond_ifp */
2951 bond_ifp
= ifb
->ifb_ifp
;
2952 ifb
->ifb_last_link_event
= event_code
;
2955 event_code
= (ifb
->ifb_active_lag
== NULL
)
2958 if (event_code
!= ifb
->ifb_last_link_event
) {
2959 if (g_bond
->verbose
) {
2960 timestamp_printf("%s: (event) generating LINK event\n",
2963 bond_ifp
= ifb
->ifb_ifp
;
2964 ifb
->ifb_last_link_event
= event_code
;
2970 * if the distributing array membership changed from 0 <-> !0
2971 * generate a link event
2973 if (old_distributing_count
== 0
2974 && ifb
->ifb_distributing_count
!= 0) {
2975 event_code
= KEV_DL_LINK_ON
;
2977 else if (old_distributing_count
!= 0
2978 && ifb
->ifb_distributing_count
== 0) {
2979 event_code
= KEV_DL_LINK_OFF
;
2981 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
2982 bond_ifp
= ifb
->ifb_ifp
;
2983 ifb
->ifb_last_link_event
= event_code
;
2988 if (bond_ifp
!= NULL
) {
2989 interface_link_event(bond_ifp
, event_code
);
2995 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
2996 const struct kev_msg
* event
)
3000 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3001 || event
->kev_class
!= KEV_NETWORK_CLASS
3002 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3005 event_code
= event
->event_code
;
3006 switch (event_code
) {
3007 case KEV_DL_LINK_OFF
:
3008 case KEV_DL_LINK_ON
:
3009 /* we only care about link status changes */
3010 bond_handle_event(port_ifp
, event_code
);
3019 bond_detached(ifnet_t port_ifp
, __unused protocol_family_t protocol
)
3021 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3026 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3029 struct kern_event_msg header
;
3031 char if_name
[IFNAMSIZ
];
3034 bzero(&event
, sizeof(event
));
3035 event
.header
.total_size
= sizeof(event
);
3036 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3037 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3038 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3039 event
.header
.event_code
= event_code
;
3040 event
.header
.event_data
[0] = ifnet_family(ifp
);
3041 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3042 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3043 ifnet_event(ifp
, &event
.header
);
3048 * Function: bond_attach_protocol
3050 * Attach a DLIL protocol to the interface.
3052 * The ethernet demux special cases to always return PF_BOND if the
3053 * interface is bonded. That means we receive all traffic from that
3054 * interface without passing any of the traffic to any other attached
3058 bond_attach_protocol(struct ifnet
*ifp
)
3061 struct ifnet_attach_proto_param reg
;
3063 bzero(®
, sizeof(reg
));
3064 reg
.input
= bond_input
;
3065 reg
.event
= bond_event
;
3066 reg
.detached
= bond_detached
;
3068 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3070 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3071 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3077 * Function: bond_detach_protocol
3079 * Detach our DLIL protocol from an interface
3082 bond_detach_protocol(struct ifnet
*ifp
)
3086 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3088 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3089 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3095 * DLIL interface family functions
3097 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3098 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3099 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3100 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3101 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3102 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3104 __private_extern__
int
3105 bond_family_init(void)
3109 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3113 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3118 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3120 ether_detach_inet6
);
3122 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3127 error
= bond_clone_attach();
3129 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3144 ** LACP ifbond_list routines
3147 ifbond_list_find_moved_port(bondport_ref rx_port
,
3148 const lacp_actor_partner_tlv_ref atlv
)
3152 partner_state_ref ps
;
3155 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3156 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3159 /* no point in comparing against ourselves */
3162 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3163 /* it's not clear that we should be checking this */
3166 ps
= &p
->po_partner_state
;
3167 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3170 ps_li
= &ps
->ps_lag_info
;
3171 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3172 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3173 sizeof(ps_li
->li_system
)) == 0) {
3174 if (g_bond
->verbose
) {
3175 timestamp_printf("System " EA_FORMAT
3176 " Port 0x%x moved from %s to %s\n",
3177 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3178 bondport_get_name(p
),
3179 bondport_get_name(rx_port
));
3189 ** LACP ifbond, LAG routines
3193 ifbond_selection(ifbond_ref bond
)
3195 int all_ports_ready
= 0;
3196 int active_media
= 0;
3198 int lag_changed
= 0;
3202 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3203 if (lag
!= bond
->ifb_active_lag
) {
3204 if (bond
->ifb_active_lag
!= NULL
) {
3205 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3206 bond
->ifb_active_lag
= NULL
;
3208 bond
->ifb_active_lag
= lag
;
3210 ifbond_activate_LAG(bond
, lag
, active_media
);
3214 else if (lag
!= NULL
) {
3215 if (lag
->lag_active_media
!= active_media
) {
3216 if (g_bond
->verbose
) {
3217 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3218 link_speed(lag
->lag_active_media
),
3219 link_speed(active_media
));
3221 ifbond_deactivate_LAG(bond
, lag
);
3222 ifbond_activate_LAG(bond
, lag
, active_media
);
3227 port_speed
= link_speed(active_media
);
3228 all_ports_ready
= ifbond_all_ports_ready(bond
);
3230 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3231 if (lag
!= NULL
&& p
->po_lag
== lag
3232 && media_speed(&p
->po_media_info
) == port_speed
3233 && (p
->po_mux_state
== MuxState_DETACHED
3234 || p
->po_selected
== SelectedState_SELECTED
3235 || p
->po_selected
== SelectedState_STANDBY
)
3236 && bondport_aggregatable(p
)) {
3237 if (bond
->ifb_max_active
> 0) {
3238 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3239 if (p
->po_selected
== SelectedState_STANDBY
3240 || p
->po_selected
== SelectedState_UNSELECTED
) {
3241 bondport_set_selected(p
, SelectedState_SELECTED
);
3244 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3245 bondport_set_selected(p
, SelectedState_STANDBY
);
3249 bondport_set_selected(p
, SelectedState_SELECTED
);
3252 if (bondport_flags_selected_changed(p
)) {
3253 bondport_flags_clear_selected_changed(p
);
3254 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3257 && bondport_flags_ready(p
)
3258 && p
->po_mux_state
== MuxState_WAITING
) {
3259 bondport_mux_machine(p
, LAEventReady
, NULL
);
3261 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3263 return (lag_changed
);
3267 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3269 int best_active
= 0;
3270 LAG_ref best_lag
= NULL
;
3275 if (bond
->ifb_active_lag
!= NULL
) {
3276 best_lag
= bond
->ifb_active_lag
;
3277 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3278 if (bond
->ifb_max_active
> 0
3279 && best_count
> bond
->ifb_max_active
) {
3280 best_count
= bond
->ifb_max_active
;
3282 best_speed
= link_speed(best_active
);
3284 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3289 if (lag
== bond
->ifb_active_lag
) {
3290 /* we've already computed it */
3293 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3297 if (bond
->ifb_max_active
> 0
3298 && count
> bond
->ifb_max_active
) {
3299 /* if there's a limit, don't count extra links */
3300 count
= bond
->ifb_max_active
;
3302 speed
= link_speed(active
);
3303 if ((count
* speed
) > (best_count
* best_speed
)) {
3306 best_active
= active
;
3310 if (best_count
== 0) {
3313 *active_media
= best_active
;
3318 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3322 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3323 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3329 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3334 if (bond
->ifb_max_active
> 0) {
3335 need
= bond
->ifb_max_active
;
3337 lag
->lag_active_media
= active_media
;
3338 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3339 if (bondport_aggregatable(p
) == 0) {
3340 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3342 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3343 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3345 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3346 if (bond
->ifb_max_active
> 0) {
3348 bondport_set_selected(p
, SelectedState_SELECTED
);
3352 bondport_set_selected(p
, SelectedState_STANDBY
);
3356 bondport_set_selected(p
, SelectedState_SELECTED
);
3360 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3368 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3370 LAG_ref lag
= bond
->ifb_active_lag
;
3372 bond
->ifb_max_active
= max_active
;
3373 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3376 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3380 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3381 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3382 if (p
->po_selected
== SelectedState_SELECTED
) {
3383 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3385 if (remove_count
== 0) {
3396 ifbond_all_ports_ready(ifbond_ref bond
)
3401 if (bond
->ifb_active_lag
== NULL
) {
3404 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3405 if (p
->po_mux_state
== MuxState_WAITING
3406 && p
->po_selected
== SelectedState_SELECTED
) {
3407 if (bondport_flags_ready(p
) == 0) {
3411 /* note that there was at least one ready port */
3418 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3422 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3423 if (this_port
== p
) {
3426 if (bondport_flags_mux_attached(p
) == 0) {
3434 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3438 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3439 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3440 sizeof(lag
->lag_info
)) == 0) {
3448 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3458 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3459 if (bondport_aggregatable(p
)) {
3462 this_speed
= media_speed(&p
->po_media_info
);
3463 if (this_speed
== 0) {
3466 if (this_speed
> speed
) {
3467 active
= p
->po_media_info
.mi_active
;
3471 else if (this_speed
== speed
) {
3476 *active_media
= active
;
3482 ** LACP bondport routines
3485 bondport_link_status_changed(bondport_ref p
)
3487 ifbond_ref bond
= p
->po_bond
;
3489 if (g_bond
->verbose
) {
3490 if (media_active(&p
->po_media_info
)) {
3491 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3492 bondport_get_name(p
),
3493 media_speed(&p
->po_media_info
),
3494 media_full_duplex(&p
->po_media_info
)
3498 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3501 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3502 if (media_active(&p
->po_media_info
)
3503 && bond
->ifb_active_lag
!= NULL
3504 && p
->po_lag
== bond
->ifb_active_lag
3505 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3506 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3507 if (g_bond
->verbose
) {
3508 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3509 bondport_get_name(p
),
3510 media_speed(&p
->po_media_info
),
3511 link_speed(p
->po_lag
->lag_active_media
));
3513 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3516 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3517 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3518 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3521 if (media_active(&p
->po_media_info
)) {
3522 bondport_enable_distributing(p
);
3525 bondport_disable_distributing(p
);
3532 bondport_aggregatable(bondport_ref p
)
3534 partner_state_ref ps
= &p
->po_partner_state
;
3536 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3537 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3538 /* we and/or our partner are individual */
3541 if (p
->po_lag
== NULL
) {
3544 switch (p
->po_receive_state
) {
3546 if (g_bond
->verbose
) {
3547 timestamp_printf("[%s] Port is not selectable\n",
3548 bondport_get_name(p
));
3551 case ReceiveState_CURRENT
:
3552 case ReceiveState_EXPIRED
:
3559 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3561 LAG_info_ref lag_li
;
3562 partner_state_ref ps
;
3565 ps
= &p
->po_partner_state
;
3566 ps_li
= &ps
->ps_lag_info
;
3567 lag_li
= &lag
->lag_info
;
3568 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3569 && ps_li
->li_key
== lag_li
->li_key
3570 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3571 sizeof(lag_li
->li_system
))
3579 bondport_remove_from_LAG(bondport_ref p
)
3582 ifbond_ref bond
= p
->po_bond
;
3583 LAG_ref lag
= p
->po_lag
;
3588 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3589 if (g_bond
->verbose
) {
3590 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3592 bondport_get_name(p
),
3593 lag
->lag_info
.li_system_priority
,
3594 EA_LIST(&lag
->lag_info
.li_system
),
3595 lag
->lag_info
.li_key
);
3598 lag
->lag_port_count
--;
3599 if (lag
->lag_port_count
> 0) {
3600 return (bond
->ifb_active_lag
== lag
);
3602 if (g_bond
->verbose
) {
3603 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3606 lag
->lag_info
.li_system_priority
,
3607 EA_LIST(&lag
->lag_info
.li_system
),
3608 lag
->lag_info
.li_key
);
3610 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3611 if (bond
->ifb_active_lag
== lag
) {
3612 bond
->ifb_active_lag
= NULL
;
3616 return (active_lag
);
3620 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3622 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3624 lag
->lag_port_count
++;
3625 if (g_bond
->verbose
) {
3626 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3627 bondport_get_name(p
),
3628 lag
->lag_info
.li_system_priority
,
3629 EA_LIST(&lag
->lag_info
.li_system
),
3630 lag
->lag_info
.li_key
);
3636 bondport_assign_to_LAG(bondport_ref p
)
3638 ifbond_ref bond
= p
->po_bond
;
3641 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3642 bondport_remove_from_LAG(p
);
3647 if (bondport_matches_LAG(p
, lag
)) {
3651 bondport_remove_from_LAG(p
);
3653 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3655 bondport_add_to_LAG(p
, lag
);
3658 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3659 TAILQ_INIT(&lag
->lag_port_list
);
3660 lag
->lag_port_count
= 0;
3661 lag
->lag_selected_port_count
= 0;
3662 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3663 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3664 if (g_bond
->verbose
) {
3665 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3668 lag
->lag_info
.li_system_priority
,
3669 EA_LIST(&lag
->lag_info
.li_system
),
3670 lag
->lag_info
.li_key
);
3672 bondport_add_to_LAG(p
, lag
);
3677 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3679 bondport_ref moved_port
;
3682 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3683 &in_lacpdu_p
->la_actor_tlv
);
3684 if (moved_port
!= NULL
) {
3685 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3687 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3688 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3689 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3694 bondport_set_selected(bondport_ref p
, SelectedState s
)
3696 if (s
!= p
->po_selected
) {
3697 ifbond_ref bond
= p
->po_bond
;
3698 LAG_ref lag
= p
->po_lag
;
3700 bondport_flags_set_selected_changed(p
);
3701 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3702 if (p
->po_selected
== SelectedState_SELECTED
) {
3703 lag
->lag_selected_port_count
--;
3705 else if (s
== SelectedState_SELECTED
) {
3706 lag
->lag_selected_port_count
++;
3708 if (g_bond
->verbose
) {
3709 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3710 bondport_get_name(p
),
3711 SelectedStateString(s
),
3712 SelectedStateString(p
->po_selected
));
3725 bondport_UpdateDefaultSelected(bondport_ref p
)
3727 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3732 bondport_RecordDefault(bondport_ref p
)
3734 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3736 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3737 bondport_assign_to_LAG(p
);
3742 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3744 lacp_actor_partner_tlv_ref actor
;
3745 partner_state_ref ps
;
3748 /* compare the PDU's Actor information to our Partner state */
3749 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3750 ps
= &p
->po_partner_state
;
3751 ps_li
= &ps
->ps_lag_info
;
3752 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3753 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3754 != ps
->ps_port_priority
)
3755 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3756 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3757 != ps_li
->li_system_priority
)
3758 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3759 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3760 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3761 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3762 if (g_bond
->verbose
) {
3763 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3764 bondport_get_name(p
));
3771 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3773 lacp_actor_partner_tlv_ref actor
;
3774 ifbond_ref bond
= p
->po_bond
;
3775 int lacp_maintain
= 0;
3776 partner_state_ref ps
;
3777 lacp_actor_partner_tlv_ref partner
;
3780 /* copy the PDU's Actor information into our Partner state */
3781 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3782 ps
= &p
->po_partner_state
;
3783 ps_li
= &ps
->ps_lag_info
;
3784 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3785 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3786 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3787 ps_li
->li_system_priority
3788 = lacp_actor_partner_tlv_get_system_priority(actor
);
3789 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3790 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3792 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3794 /* compare the PDU's Partner information to our own information */
3795 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3797 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3798 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3799 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3800 if (g_bond
->verbose
) {
3801 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3802 bondport_get_name(p
));
3806 if ((lacp_actor_partner_tlv_get_port(partner
)
3807 == bondport_get_index(p
))
3808 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3809 && bcmp(partner
->lap_system
, &g_bond
->system
,
3810 sizeof(g_bond
->system
)) == 0
3811 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3812 == g_bond
->system_priority
)
3813 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3814 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3815 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3816 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3818 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3819 if (g_bond
->verbose
) {
3820 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3821 bondport_get_name(p
));
3824 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3825 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3827 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3828 if (g_bond
->verbose
) {
3829 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3830 bondport_get_name(p
));
3833 bondport_assign_to_LAG(p
);
3837 static __inline__ lacp_actor_partner_state
3838 updateNTTBits(lacp_actor_partner_state s
)
3840 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3841 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3842 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3843 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3847 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3849 ifbond_ref bond
= p
->po_bond
;
3850 lacp_actor_partner_tlv_ref partner
;
3852 /* compare the PDU's Actor information to our Partner state */
3853 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3854 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3855 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3856 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3857 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3858 != g_bond
->system_priority
)
3859 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3860 || (updateNTTBits(partner
->lap_state
)
3861 != updateNTTBits(p
->po_actor_state
))) {
3862 bondport_flags_set_ntt(p
);
3863 if (g_bond
->verbose
) {
3864 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3865 bondport_get_name(p
));
3872 bondport_AttachMuxToAggregator(bondport_ref p
)
3874 if (bondport_flags_mux_attached(p
) == 0) {
3875 if (g_bond
->verbose
) {
3876 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3877 bondport_get_name(p
));
3879 bondport_flags_set_mux_attached(p
);
3885 bondport_DetachMuxFromAggregator(bondport_ref p
)
3887 if (bondport_flags_mux_attached(p
)) {
3888 if (g_bond
->verbose
) {
3889 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3890 bondport_get_name(p
));
3892 bondport_flags_clear_mux_attached(p
);
3898 bondport_enable_distributing(bondport_ref p
)
3900 if (bondport_flags_distributing(p
) == 0) {
3901 ifbond_ref bond
= p
->po_bond
;
3903 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3904 if (g_bond
->verbose
) {
3905 timestamp_printf("[%s] Distribution Enabled\n",
3906 bondport_get_name(p
));
3908 bondport_flags_set_distributing(p
);
3914 bondport_disable_distributing(bondport_ref p
)
3916 if (bondport_flags_distributing(p
)) {
3917 bondport_ref
* array
;
3923 array
= bond
->ifb_distributing_array
;
3924 count
= bond
->ifb_distributing_count
;
3925 for (i
= 0; i
< count
; i
++) {
3926 if (array
[i
] == p
) {
3929 for (j
= i
; j
< (count
- 1); j
++) {
3930 array
[j
] = array
[j
+ 1];
3935 bond
->ifb_distributing_count
--;
3936 if (g_bond
->verbose
) {
3937 timestamp_printf("[%s] Distribution Disabled\n",
3938 bondport_get_name(p
));
3940 bondport_flags_clear_distributing(p
);
3946 ** Receive machine functions
3949 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3952 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3955 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3958 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3961 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3964 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3968 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
3971 switch (p
->po_receive_state
) {
3972 case ReceiveState_none
:
3973 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
3975 case ReceiveState_INITIALIZE
:
3976 bondport_receive_machine_initialize(p
, event
, event_data
);
3978 case ReceiveState_PORT_DISABLED
:
3979 bondport_receive_machine_port_disabled(p
, event
, event_data
);
3981 case ReceiveState_EXPIRED
:
3982 bondport_receive_machine_expired(p
, event
, event_data
);
3984 case ReceiveState_LACP_DISABLED
:
3985 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
3987 case ReceiveState_DEFAULTED
:
3988 bondport_receive_machine_defaulted(p
, event
, event_data
);
3990 case ReceiveState_CURRENT
:
3991 bondport_receive_machine_current(p
, event
, event_data
);
4000 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4005 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4006 bondport_receive_machine_current(p
, event
, event_data
);
4009 case LAEventMediaChange
:
4010 if (media_active(&p
->po_media_info
)) {
4011 switch (p
->po_receive_state
) {
4012 case ReceiveState_PORT_DISABLED
:
4013 case ReceiveState_LACP_DISABLED
:
4014 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4021 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4025 bondport_receive_machine_event(p
, event
, event_data
);
4032 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4033 __unused
void * event_data
)
4037 devtimer_cancel(p
->po_current_while_timer
);
4038 if (g_bond
->verbose
) {
4039 timestamp_printf("[%s] Receive INITIALIZE\n",
4040 bondport_get_name(p
));
4042 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4043 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4044 bondport_RecordDefault(p
);
4046 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4047 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4056 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4057 __unused
void * event_data
)
4059 partner_state_ref ps
;
4063 devtimer_cancel(p
->po_current_while_timer
);
4064 if (g_bond
->verbose
) {
4065 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4066 bondport_get_name(p
));
4068 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4069 ps
= &p
->po_partner_state
;
4070 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4072 case LAEventMediaChange
:
4073 if (media_active(&p
->po_media_info
)) {
4074 if (media_full_duplex(&p
->po_media_info
)) {
4075 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4078 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4081 else if (p
->po_selected
== SelectedState_SELECTED
) {
4084 if (g_bond
->verbose
) {
4085 timestamp_printf("[%s] Receive PORT_DISABLED: "
4086 "link timer started\n",
4087 bondport_get_name(p
));
4091 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4092 (devtimer_timeout_func
)
4093 bondport_receive_machine_port_disabled
,
4094 (void *)LAEventTimeout
, NULL
);
4096 else if (p
->po_selected
== SelectedState_STANDBY
) {
4097 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4100 case LAEventTimeout
:
4101 if (p
->po_selected
== SelectedState_SELECTED
) {
4102 if (g_bond
->verbose
) {
4103 timestamp_printf("[%s] Receive PORT_DISABLED: "
4104 "link timer completed, marking UNSELECTED\n",
4105 bondport_get_name(p
));
4107 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4110 case LAEventPortMoved
:
4111 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4120 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4121 __unused
void * event_data
)
4123 lacp_actor_partner_state s
;
4128 devtimer_cancel(p
->po_current_while_timer
);
4129 if (g_bond
->verbose
) {
4130 timestamp_printf("[%s] Receive EXPIRED\n",
4131 bondport_get_name(p
));
4133 p
->po_receive_state
= ReceiveState_EXPIRED
;
4134 s
= p
->po_partner_state
.ps_state
;
4135 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4136 s
= lacp_actor_partner_state_set_short_timeout(s
);
4137 p
->po_partner_state
.ps_state
= s
;
4139 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4140 /* start current_while timer */
4141 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4143 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4144 (devtimer_timeout_func
)
4145 bondport_receive_machine_expired
,
4146 (void *)LAEventTimeout
, NULL
);
4149 case LAEventTimeout
:
4150 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4159 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4160 __unused
void * event_data
)
4162 partner_state_ref ps
;
4165 devtimer_cancel(p
->po_current_while_timer
);
4166 if (g_bond
->verbose
) {
4167 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4168 bondport_get_name(p
));
4170 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4171 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4172 bondport_RecordDefault(p
);
4173 ps
= &p
->po_partner_state
;
4174 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4176 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4185 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4186 __unused
void * event_data
)
4190 devtimer_cancel(p
->po_current_while_timer
);
4191 if (g_bond
->verbose
) {
4192 timestamp_printf("[%s] Receive DEFAULTED\n",
4193 bondport_get_name(p
));
4195 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4196 bondport_UpdateDefaultSelected(p
);
4197 bondport_RecordDefault(p
);
4199 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4208 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4211 partner_state_ref ps
;
4216 devtimer_cancel(p
->po_current_while_timer
);
4217 if (g_bond
->verbose
) {
4218 timestamp_printf("[%s] Receive CURRENT\n",
4219 bondport_get_name(p
));
4221 p
->po_receive_state
= ReceiveState_CURRENT
;
4222 bondport_UpdateSelected(p
, event_data
);
4223 bondport_UpdateNTT(p
, event_data
);
4224 bondport_RecordPDU(p
, event_data
);
4226 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4227 bondport_assign_to_LAG(p
);
4228 /* start current_while timer */
4229 ps
= &p
->po_partner_state
;
4230 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4231 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4234 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4237 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4238 (devtimer_timeout_func
)
4239 bondport_receive_machine_current
,
4240 (void *)LAEventTimeout
, NULL
);
4242 case LAEventTimeout
:
4243 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4252 ** Periodic Transmission machine
4256 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4257 __unused
void * event_data
)
4260 partner_state_ref ps
;
4265 if (g_bond
->verbose
) {
4266 timestamp_printf("[%s] periodic_transmit Start\n",
4267 bondport_get_name(p
));
4270 case LAEventMediaChange
:
4271 devtimer_cancel(p
->po_periodic_timer
);
4272 p
->po_periodic_interval
= 0;
4273 if (media_active(&p
->po_media_info
) == 0
4274 || media_full_duplex(&p
->po_media_info
) == 0) {
4278 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4279 ps
= &p
->po_partner_state
;
4280 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4281 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4283 devtimer_cancel(p
->po_periodic_timer
);
4284 p
->po_periodic_interval
= 0;
4287 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4288 interval
= LACP_FAST_PERIODIC_TIME
;
4291 interval
= LACP_SLOW_PERIODIC_TIME
;
4293 if (p
->po_periodic_interval
!= interval
) {
4294 if (interval
== LACP_FAST_PERIODIC_TIME
4295 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4296 if (g_bond
->verbose
) {
4297 timestamp_printf("[%s] periodic_transmit:"
4298 " Need To Transmit\n",
4299 bondport_get_name(p
));
4301 bondport_flags_set_ntt(p
);
4303 p
->po_periodic_interval
= interval
;
4305 tv
.tv_sec
= interval
;
4306 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4307 (devtimer_timeout_func
)
4308 bondport_periodic_transmit_machine
,
4309 (void *)LAEventTimeout
, NULL
);
4310 if (g_bond
->verbose
) {
4311 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4312 bondport_get_name(p
),
4313 p
->po_periodic_interval
);
4317 case LAEventTimeout
:
4318 bondport_flags_set_ntt(p
);
4319 tv
.tv_sec
= p
->po_periodic_interval
;
4321 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4322 bondport_periodic_transmit_machine
,
4323 (void *)LAEventTimeout
, NULL
);
4324 if (g_bond
->verbose
> 1) {
4325 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4326 bondport_get_name(p
), p
->po_periodic_interval
);
4339 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4340 __darwin_time_t
* next_secs
)
4342 if (p
->po_last_transmit_secs
!= current_secs
) {
4343 p
->po_last_transmit_secs
= current_secs
;
4344 p
->po_n_transmit
= 0;
4346 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4350 if (next_secs
!= NULL
) {
4351 *next_secs
= current_secs
+ 1;
4357 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4360 lacp_actor_partner_tlv_ref aptlv
;
4361 lacp_collector_tlv_ref ctlv
;
4362 struct timeval next_tick_time
= {0, 0};
4363 lacpdu_ref out_lacpdu_p
;
4364 packet_buffer_ref pkt
;
4365 partner_state_ref ps
;
4369 case LAEventTimeout
:
4371 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4374 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4375 /* we're going away, transmit the packet no matter what */
4377 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4378 &next_tick_time
.tv_sec
) == 0) {
4379 if (devtimer_enabled(p
->po_transmit_timer
)) {
4380 if (g_bond
->verbose
> 0) {
4381 timestamp_printf("[%s] Transmit Timer Already Set\n",
4382 bondport_get_name(p
));
4386 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4387 (devtimer_timeout_func
)
4388 bondport_transmit_machine
,
4389 (void *)LAEventTimeout
, NULL
);
4390 if (g_bond
->verbose
> 0) {
4391 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4392 bondport_get_name(p
),
4393 (int)next_tick_time
.tv_sec
);
4398 if (g_bond
->verbose
> 0) {
4399 if (event
== LAEventTimeout
) {
4400 timestamp_printf("[%s] Transmit Timer Complete\n",
4401 bondport_get_name(p
));
4404 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4406 printf("[%s] Transmit: failed to allocate packet buffer\n",
4407 bondport_get_name(p
));
4410 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4411 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4412 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4413 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4416 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4417 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4418 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4419 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4420 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4421 g_bond
->system_priority
);
4422 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4423 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4424 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4425 aptlv
->lap_state
= p
->po_actor_state
;
4428 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4429 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4430 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4431 ps
= &p
->po_partner_state
;
4432 ps_li
= &ps
->ps_lag_info
;
4433 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4434 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4435 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4436 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4437 ps_li
->li_system_priority
);
4438 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4439 aptlv
->lap_state
= ps
->ps_state
;
4442 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4443 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4444 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4446 bondport_slow_proto_transmit(p
, pkt
);
4447 bondport_flags_clear_ntt(p
);
4448 if (g_bond
->verbose
> 0) {
4449 timestamp_printf("[%s] Transmit Packet %d\n",
4450 bondport_get_name(p
), p
->po_n_transmit
);
4460 ** Mux machine functions
4464 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4467 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4470 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4474 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4478 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4480 switch (p
->po_mux_state
) {
4482 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4484 case MuxState_DETACHED
:
4485 bondport_mux_machine_detached(p
, event
, event_data
);
4487 case MuxState_WAITING
:
4488 bondport_mux_machine_waiting(p
, event
, event_data
);
4490 case MuxState_ATTACHED
:
4491 bondport_mux_machine_attached(p
, event
, event_data
);
4493 case MuxState_COLLECTING_DISTRIBUTING
:
4494 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4503 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4504 __unused
void * event_data
)
4506 lacp_actor_partner_state s
;
4510 devtimer_cancel(p
->po_wait_while_timer
);
4511 if (g_bond
->verbose
) {
4512 timestamp_printf("[%s] Mux DETACHED\n",
4513 bondport_get_name(p
));
4515 p
->po_mux_state
= MuxState_DETACHED
;
4516 bondport_flags_clear_ready(p
);
4517 bondport_DetachMuxFromAggregator(p
);
4518 bondport_disable_distributing(p
);
4519 s
= p
->po_actor_state
;
4520 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4521 s
= lacp_actor_partner_state_set_not_collecting(s
);
4522 s
= lacp_actor_partner_state_set_not_distributing(s
);
4523 p
->po_actor_state
= s
;
4524 bondport_flags_set_ntt(p
);
4526 case LAEventSelectedChange
:
4528 case LAEventMediaChange
:
4529 if (p
->po_selected
== SelectedState_SELECTED
4530 || p
->po_selected
== SelectedState_STANDBY
) {
4531 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4541 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4542 __unused
void * event_data
)
4548 devtimer_cancel(p
->po_wait_while_timer
);
4549 if (g_bond
->verbose
) {
4550 timestamp_printf("[%s] Mux WAITING\n",
4551 bondport_get_name(p
));
4553 p
->po_mux_state
= MuxState_WAITING
;
4556 case LAEventSelectedChange
:
4557 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4558 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4561 if (p
->po_selected
== SelectedState_STANDBY
) {
4562 devtimer_cancel(p
->po_wait_while_timer
);
4563 /* wait until state changes to SELECTED */
4564 if (g_bond
->verbose
) {
4565 timestamp_printf("[%s] Mux WAITING: Standby\n",
4566 bondport_get_name(p
));
4570 if (bondport_flags_ready(p
)) {
4571 if (g_bond
->verbose
) {
4572 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4573 bondport_get_name(p
));
4577 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4578 if (g_bond
->verbose
) {
4579 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4580 bondport_get_name(p
));
4584 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4585 devtimer_cancel(p
->po_wait_while_timer
);
4586 if (g_bond
->verbose
) {
4587 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4588 bondport_get_name(p
));
4590 bondport_flags_set_ready(p
);
4593 if (g_bond
->verbose
) {
4594 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4595 bondport_get_name(p
));
4597 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4599 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4600 (devtimer_timeout_func
)
4601 bondport_mux_machine_waiting
,
4602 (void *)LAEventTimeout
, NULL
);
4604 case LAEventTimeout
:
4605 if (g_bond
->verbose
) {
4606 timestamp_printf("[%s] Mux WAITING: Ready\n",
4607 bondport_get_name(p
));
4609 bondport_flags_set_ready(p
);
4613 if (bondport_flags_ready(p
)){
4614 if (g_bond
->verbose
) {
4615 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4616 bondport_get_name(p
));
4618 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4627 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4628 __unused
void * event_data
)
4630 lacp_actor_partner_state s
;
4634 devtimer_cancel(p
->po_wait_while_timer
);
4635 if (g_bond
->verbose
) {
4636 timestamp_printf("[%s] Mux ATTACHED\n",
4637 bondport_get_name(p
));
4639 p
->po_mux_state
= MuxState_ATTACHED
;
4640 bondport_AttachMuxToAggregator(p
);
4641 s
= p
->po_actor_state
;
4642 s
= lacp_actor_partner_state_set_in_sync(s
);
4643 s
= lacp_actor_partner_state_set_not_collecting(s
);
4644 s
= lacp_actor_partner_state_set_not_distributing(s
);
4645 bondport_disable_distributing(p
);
4646 p
->po_actor_state
= s
;
4647 bondport_flags_set_ntt(p
);
4650 switch (p
->po_selected
) {
4651 case SelectedState_SELECTED
:
4652 s
= p
->po_partner_state
.ps_state
;
4653 if (lacp_actor_partner_state_in_sync(s
)) {
4654 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4659 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4668 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4670 __unused
void * event_data
)
4672 lacp_actor_partner_state s
;
4676 devtimer_cancel(p
->po_wait_while_timer
);
4677 if (g_bond
->verbose
) {
4678 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4679 bondport_get_name(p
));
4681 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4682 bondport_enable_distributing(p
);
4683 s
= p
->po_actor_state
;
4684 s
= lacp_actor_partner_state_set_collecting(s
);
4685 s
= lacp_actor_partner_state_set_distributing(s
);
4686 p
->po_actor_state
= s
;
4687 bondport_flags_set_ntt(p
);
4690 s
= p
->po_partner_state
.ps_state
;
4691 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4692 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4695 switch (p
->po_selected
) {
4696 case SelectedState_UNSELECTED
:
4697 case SelectedState_STANDBY
:
4698 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);