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 */
811 static __inline__
int
812 media_active(const struct media_info
* mi
)
814 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
817 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
820 static __inline__
int
821 media_full_duplex(const struct media_info
* mi
)
823 return ((mi
->mi_active
& IFM_FDX
) != 0);
826 static __inline__
int
827 media_speed(const struct media_info
* mi
)
829 return (link_speed(mi
->mi_active
));
832 static struct media_info
833 interface_media_info(struct ifnet
* ifp
)
835 struct ifmediareq ifmr
;
836 struct media_info mi
;
838 bzero(&mi
, sizeof(mi
));
839 bzero(&ifmr
, sizeof(ifmr
));
840 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
841 if (ifmr
.ifm_count
!= 0) {
842 mi
.mi_status
= ifmr
.ifm_status
;
843 mi
.mi_active
= ifmr
.ifm_active
;
850 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
855 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
856 * currently expects it that way
858 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
859 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
860 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
861 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
867 static bond_globals_ref
868 bond_globals_create(lacp_system_priority sys_pri
,
873 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
| M_ZERO
);
877 TAILQ_INIT(&b
->ifbond_list
);
879 b
->system_priority
= sys_pri
;
884 bond_globals_init(void)
890 bond_assert_lock_not_held();
892 if (g_bond
!= NULL
) {
897 * use en0's ethernet address as the system identifier, and if it's not
898 * there, use en1 .. en3
901 for (i
= 0; i
< 4; i
++) {
902 char ifname
[IFNAMSIZ
+1];
903 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
904 ifp
= ifunit(ifname
);
911 b
= bond_globals_create(0x8000, (lacp_system_ref
)IF_LLADDR(ifp
));
914 if (g_bond
!= NULL
) {
931 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
932 const struct ether_header
* eh_p
,
933 u_int16_t vlan_tag
, bpf_packet_func func
)
935 struct ether_vlan_header
* vlh_p
;
938 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
942 /* populate a new mbuf containing the vlan ethernet header */
943 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
944 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
945 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
946 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
947 vlh_p
->evl_tag
= htons(vlan_tag
);
948 vlh_p
->evl_proto
= eh_p
->ether_type
;
956 static __inline__
void
957 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
958 bpf_packet_func func
)
961 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
962 const struct ether_header
* eh_p
;
963 eh_p
= mtod(m
, const struct ether_header
*);
964 m
->m_data
+= ETHER_HDR_LEN
;
965 m
->m_len
-= ETHER_HDR_LEN
;
966 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
967 m
->m_data
-= ETHER_HDR_LEN
;
968 m
->m_len
+= ETHER_HDR_LEN
;
976 static __inline__
void
977 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
978 bpf_packet_func func
)
981 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
982 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
984 /* restore the header */
985 m
->m_data
-= ETHER_HDR_LEN
;
986 m
->m_len
+= ETHER_HDR_LEN
;
988 m
->m_data
+= ETHER_HDR_LEN
;
989 m
->m_len
-= ETHER_HDR_LEN
;
996 * Function: bond_setmulti
998 * Enable multicast reception on "our" interface by enabling multicasts on
999 * each of the member ports.
1002 bond_setmulti(struct ifnet
* ifp
)
1010 ifb
= ifnet_softc(ifp
);
1011 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1012 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1017 ifbond_wait(ifb
, "bond_setmulti");
1019 if (ifbond_flags_if_detaching(ifb
)) {
1020 /* someone destroyed the bond while we were waiting */
1026 /* ifbond_wait() let's us safely walk the list without holding the lock */
1027 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1028 struct ifnet
* port_ifp
= p
->po_ifp
;
1030 error
= multicast_list_program(&p
->po_multicast
,
1033 printf("bond_setmulti(%s): "
1034 "multicast_list_program(%s%d) failed, %d\n",
1035 ifb
->ifb_name
, ifnet_name(port_ifp
),
1036 ifnet_unit(port_ifp
), error
);
1042 ifbond_signal(ifb
, "bond_setmulti");
1044 ifbond_release(ifb
);
1049 bond_clone_attach(void)
1053 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1060 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1063 struct ifmultiaddr
* ifma
= NULL
;
1064 struct sockaddr_dl sdl
;
1066 bond_assert_lock_not_held();
1068 bzero(&sdl
, sizeof(sdl
));
1069 sdl
.sdl_len
= sizeof(sdl
);
1070 sdl
.sdl_family
= AF_LINK
;
1071 sdl
.sdl_type
= IFT_ETHER
;
1073 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1074 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1075 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1077 ifb
->ifb_ifma_slow_proto
= ifma
;
1083 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1088 struct ifnet_init_eparams bond_init
;
1090 error
= bond_globals_init();
1095 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
| M_ZERO
);
1101 TAILQ_INIT(&ifb
->ifb_port_list
);
1102 TAILQ_INIT(&ifb
->ifb_lag_list
);
1103 ifb
->ifb_key
= unit
+ 1;
1105 /* use the interface name as the unique id for ifp recycle */
1106 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1107 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1108 ifbond_release(ifb
);
1112 bzero(&bond_init
, sizeof(bond_init
));
1113 bond_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1114 bond_init
.len
= sizeof (bond_init
);
1115 bond_init
.flags
= IFNET_INIT_LEGACY
;
1116 bond_init
.uniqueid
= ifb
->ifb_name
;
1117 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1118 bond_init
.name
= ifc
->ifc_name
;
1119 bond_init
.unit
= unit
;
1120 bond_init
.family
= IFNET_FAMILY_BOND
;
1121 bond_init
.type
= IFT_IEEE8023ADLAG
;
1122 bond_init
.output
= bond_output
;
1123 bond_init
.demux
= ether_demux
;
1124 bond_init
.add_proto
= ether_add_proto
;
1125 bond_init
.del_proto
= ether_del_proto
;
1126 bond_init
.check_multi
= ether_check_multi
;
1127 bond_init
.framer_extended
= ether_frameout_extended
;
1128 bond_init
.ioctl
= bond_ioctl
;
1129 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1130 bond_init
.detach
= bond_if_free
;
1131 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1132 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1133 bond_init
.softc
= ifb
;
1134 error
= ifnet_allocate_extended(&bond_init
, &ifp
);
1137 ifbond_release(ifb
);
1142 ifnet_set_offload(ifp
, 0);
1143 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1144 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1145 ifnet_set_baudrate(ifp
, 0);
1146 ifnet_set_mtu(ifp
, 0);
1148 error
= ifnet_attach(ifp
, NULL
);
1151 ifbond_release(ifb
);
1154 error
= ifbond_add_slow_proto_multicast(ifb
);
1156 printf("bond_clone_create(%s): "
1157 "failed to add slow_proto multicast, %d\n",
1158 ifb
->ifb_name
, error
);
1161 /* attach as ethernet */
1162 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1165 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1172 bond_remove_all_interfaces(ifbond_ref ifb
)
1176 bond_assert_lock_held();
1179 * do this in reverse order to avoid re-programming the mac address
1180 * as each head interface is removed
1182 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1183 bond_remove_interface(ifb
, p
->po_ifp
);
1189 bond_remove(ifbond_ref ifb
)
1191 bond_assert_lock_held();
1192 ifbond_flags_set_if_detaching(ifb
);
1193 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1194 bond_remove_all_interfaces(ifb
);
1199 bond_if_detach(struct ifnet
* ifp
)
1203 error
= ifnet_detach(ifp
);
1205 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1206 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1213 bond_clone_destroy(struct ifnet
* ifp
)
1218 ifb
= ifnet_softc(ifp
);
1219 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1223 if (ifbond_flags_if_detaching(ifb
)) {
1229 bond_if_detach(ifp
);
1234 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1239 ifb
= ifnet_softc(ifp
);
1240 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1245 case BPF_TAP_DISABLE
:
1246 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1250 ifb
->ifb_bpf_input
= func
;
1253 case BPF_TAP_OUTPUT
:
1254 ifb
->ifb_bpf_output
= func
;
1257 case BPF_TAP_INPUT_OUTPUT
:
1258 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1268 ether_header_hash(struct ether_header
* eh_p
)
1272 /* get 32-bits from destination ether and ether type */
1273 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1275 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1279 static struct mbuf
*
1280 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1285 while (*offset
>= len
) {
1296 #if BYTE_ORDER == BIG_ENDIAN
1297 static __inline__
uint32_t
1298 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1300 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1301 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1303 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1304 static __inline__
uint32_t
1305 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1307 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1308 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1310 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1313 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1315 struct mbuf
* current
;
1316 u_char
* current_data
;
1321 current
= S_mbuf_skip_to_offset(m
, &offset
);
1322 if (current
== NULL
) {
1325 current_data
= mtod(current
, u_char
*) + offset
;
1326 space_current
= current
->m_len
- offset
;
1327 if (space_current
>= (int)sizeof(uint32_t)) {
1328 *val
= *((uint32_t *)current_data
);
1331 next
= current
->m_next
;
1332 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1335 next_data
= mtod(next
, u_char
*);
1336 switch (space_current
) {
1338 *val
= make_uint32(current_data
[0], next_data
[0],
1339 next_data
[1], next_data
[2]);
1342 *val
= make_uint32(current_data
[0], current_data
[1],
1343 next_data
[0], next_data
[1]);
1346 *val
= make_uint32(current_data
[0], current_data
[1],
1347 current_data
[2], next_data
[0]);
1353 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1354 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1357 ip_header_hash(struct mbuf
* m
)
1360 struct in_addr ip_dst
;
1361 struct in_addr ip_src
;
1364 struct mbuf
* orig_m
= m
;
1366 /* find the IP protocol field relative to the start of the packet */
1367 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1368 m
= S_mbuf_skip_to_offset(m
, &offset
);
1369 if (m
== NULL
|| m
->m_len
< 1) {
1372 data
= mtod(m
, u_char
*) + offset
;
1375 /* find the IP src relative to the IP protocol */
1376 if ((m
->m_len
- offset
)
1377 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1378 /* this should be the normal case */
1379 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1380 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1383 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1384 (uint32_t *)&ip_src
.s_addr
)) {
1387 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1388 (uint32_t *)&ip_dst
.s_addr
)) {
1392 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1395 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1398 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1400 ipv6_header_hash(struct mbuf
* m
)
1405 struct mbuf
* orig_m
= m
;
1409 /* find the IP protocol field relative to the start of the packet */
1410 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1411 m
= S_mbuf_skip_to_offset(m
, &offset
);
1413 goto bad_ipv6_packet
;
1415 data
= mtod(m
, u_char
*) + offset
;
1417 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1418 /* this should be the normal case */
1419 for (i
= 0, scan
= (uint32_t *)data
;
1420 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1426 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1428 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1429 (uint32_t *)&tmp
)) {
1430 goto bad_ipv6_packet
;
1435 return (ntohl(val
));
1438 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1442 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1444 bpf_packet_func bpf_func
;
1447 struct ifnet
* port_ifp
= NULL
;
1449 struct flowadv adv
= { FADV_SUCCESS
};
1454 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1458 if (m
->m_pkthdr
.pkt_flowid
!= 0) {
1459 h
= m
->m_pkthdr
.pkt_flowid
;
1462 struct ether_header
* eh_p
;
1464 eh_p
= mtod(m
, struct ether_header
*);
1465 switch (ntohs(eh_p
->ether_type
)) {
1467 h
= ip_header_hash(m
);
1469 case ETHERTYPE_IPV6
:
1470 h
= ipv6_header_hash(m
);
1473 h
= ether_header_hash(eh_p
);
1478 ifb
= ifnet_softc(ifp
);
1479 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1480 || ifb
->ifb_distributing_count
== 0) {
1483 h
%= ifb
->ifb_distributing_count
;
1484 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1485 bpf_func
= ifb
->ifb_bpf_output
;
1488 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1489 (void)ifnet_stat_increment_out(ifp
, 1,
1490 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1493 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1495 bond_bpf_output(ifp
, m
, bpf_func
);
1497 err
= dlil_output(port_ifp
, PF_BOND
, m
, NULL
, NULL
, 1, &adv
);
1500 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1502 } else if (adv
.code
== FADV_SUSPENDED
) {
1516 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1519 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1520 if (p
->po_ifp
== port_ifp
) {
1528 bond_lookup_port(struct ifnet
* port_ifp
)
1533 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1534 port
= ifbond_lookup_port(ifb
, port_ifp
);
1543 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1545 struct ifnet
* bond_ifp
= NULL
;
1551 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1554 p
= bond_lookup_port(port_ifp
);
1558 if (p
->po_enabled
== 0) {
1562 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1565 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1566 if (ifbond_selection(ifb
)) {
1567 event_code
= (ifb
->ifb_active_lag
== NULL
)
1570 /* XXX need to take a reference on bond_ifp */
1571 bond_ifp
= ifb
->ifb_ifp
;
1572 ifb
->ifb_last_link_event
= event_code
;
1575 event_code
= (ifb
->ifb_active_lag
== NULL
)
1578 if (event_code
!= ifb
->ifb_last_link_event
) {
1579 if (g_bond
->verbose
) {
1580 timestamp_printf("%s: (receive) generating LINK event\n",
1583 bond_ifp
= ifb
->ifb_ifp
;
1584 ifb
->ifb_last_link_event
= event_code
;
1590 if (bond_ifp
!= NULL
) {
1591 interface_link_event(bond_ifp
, event_code
);
1598 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1600 la_marker_pdu_ref marker_p
;
1603 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1604 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1608 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1612 p
= bond_lookup_port(port_ifp
);
1613 if (p
== NULL
|| p
->po_enabled
== 0
1614 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1618 /* echo back the same packet as a marker response */
1619 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1620 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1630 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1631 char * frame_header
)
1633 bpf_packet_func bpf_func
;
1634 const struct ether_header
* eh_p
;
1639 eh_p
= (const struct ether_header
*)frame_header
;
1640 if ((m
->m_flags
& M_MCAST
) != 0
1641 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1642 sizeof(eh_p
->ether_dhost
)) == 0
1643 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1644 u_char subtype
= *mtod(m
, u_char
*);
1646 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1647 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1652 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1653 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1658 bond_receive_lacpdu(m
, port_ifp
);
1661 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1664 /* restore the ethernet header pointer in the mbuf */
1665 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1666 m
->m_data
-= ETHER_HDR_LEN
;
1667 m
->m_len
+= ETHER_HDR_LEN
;
1668 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1669 if (m
->m_pkthdr
.len
< min_size
) {
1674 if (m
->m_len
< min_size
) {
1675 m
= m_pullup(m
, min_size
);
1680 /* send to marker responder */
1681 bond_receive_la_marker_pdu(m
, port_ifp
);
1684 else if (subtype
== 0
1685 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1686 /* invalid subtype, discard the frame */
1692 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1695 p
= bond_lookup_port(port_ifp
);
1696 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1700 /* make the packet appear as if it arrived on the bonded interface */
1703 bpf_func
= ifb
->ifb_bpf_input
;
1706 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1707 (void)ifnet_stat_increment_in(ifp
, 1,
1708 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1709 + ETHER_VLAN_ENCAP_LEN
), 0);
1712 (void)ifnet_stat_increment_in(ifp
, 1,
1713 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1715 m
->m_pkthdr
.rcvif
= ifp
;
1716 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1717 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1718 dlil_input_packet_list(ifp
, m
);
1727 static __inline__
const char *
1728 bondport_get_name(bondport_ref p
)
1730 return (p
->po_name
);
1733 static __inline__
int
1734 bondport_get_index(bondport_ref p
)
1736 return (ifnet_index(p
->po_ifp
));
1740 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1742 struct ether_header
* eh_p
;
1745 /* packet_buffer_allocate leaves room for ethernet header */
1746 eh_p
= mtod(buf
, struct ether_header
*);
1747 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1748 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1749 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1750 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1752 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1753 bondport_get_name(p
), error
);
1759 bondport_timer_process_func(devtimer_ref timer
,
1760 devtimer_process_func_event event
)
1765 case devtimer_process_func_event_lock
:
1767 devtimer_retain(timer
);
1769 case devtimer_process_func_event_unlock
:
1770 if (devtimer_valid(timer
)) {
1771 /* as long as the devtimer is valid, we can look at arg0 */
1773 struct ifnet
* bond_ifp
= NULL
;
1775 p
= (bondport_ref
)devtimer_arg0(timer
);
1776 if (ifbond_selection(p
->po_bond
)) {
1777 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1780 /* XXX need to take a reference on bond_ifp */
1781 bond_ifp
= p
->po_bond
->ifb_ifp
;
1782 p
->po_bond
->ifb_last_link_event
= event_code
;
1785 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1788 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1789 if (g_bond
->verbose
) {
1790 timestamp_printf("%s: (timer) generating LINK event\n",
1791 p
->po_bond
->ifb_name
);
1793 bond_ifp
= p
->po_bond
->ifb_ifp
;
1794 p
->po_bond
->ifb_last_link_event
= event_code
;
1797 devtimer_release(timer
);
1799 if (bond_ifp
!= NULL
) {
1800 interface_link_event(bond_ifp
, event_code
);
1804 /* timer is going away */
1805 devtimer_release(timer
);
1815 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1816 int active
, int short_timeout
, int * ret_error
)
1819 bondport_ref p
= NULL
;
1820 lacp_actor_partner_state s
;
1823 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
| M_ZERO
);
1825 *ret_error
= ENOMEM
;
1828 multicast_list_init(&p
->po_multicast
);
1829 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1830 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1831 >= sizeof(p
->po_name
)) {
1832 printf("if_bond: name too large\n");
1833 *ret_error
= EINVAL
;
1836 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1838 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1839 bondport_get_name(p
), error
);
1842 /* remember the current interface MTU so it can be restored */
1843 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1844 p
->po_ifp
= port_ifp
;
1845 p
->po_media_info
= interface_media_info(port_ifp
);
1846 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1847 if (p
->po_current_while_timer
== NULL
) {
1848 *ret_error
= ENOMEM
;
1851 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1852 if (p
->po_periodic_timer
== NULL
) {
1853 *ret_error
= ENOMEM
;
1856 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1857 if (p
->po_wait_while_timer
== NULL
) {
1858 *ret_error
= ENOMEM
;
1861 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1862 if (p
->po_transmit_timer
== NULL
) {
1863 *ret_error
= ENOMEM
;
1866 p
->po_receive_state
= ReceiveState_none
;
1867 p
->po_mux_state
= MuxState_none
;
1868 p
->po_priority
= priority
;
1870 s
= lacp_actor_partner_state_set_aggregatable(s
);
1871 if (short_timeout
) {
1872 s
= lacp_actor_partner_state_set_short_timeout(s
);
1875 s
= lacp_actor_partner_state_set_active_lacp(s
);
1877 p
->po_actor_state
= s
;
1886 bondport_start(bondport_ref p
)
1888 bondport_receive_machine(p
, LAEventStart
, NULL
);
1889 bondport_mux_machine(p
, LAEventStart
, NULL
);
1890 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1891 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1896 * Function: bondport_invalidate_timers
1898 * Invalidate all of the timers for the bondport.
1901 bondport_invalidate_timers(bondport_ref p
)
1903 devtimer_invalidate(p
->po_current_while_timer
);
1904 devtimer_invalidate(p
->po_periodic_timer
);
1905 devtimer_invalidate(p
->po_wait_while_timer
);
1906 devtimer_invalidate(p
->po_transmit_timer
);
1910 * Function: bondport_cancel_timers
1912 * Cancel all of the timers for the bondport.
1915 bondport_cancel_timers(bondport_ref p
)
1917 devtimer_cancel(p
->po_current_while_timer
);
1918 devtimer_cancel(p
->po_periodic_timer
);
1919 devtimer_cancel(p
->po_wait_while_timer
);
1920 devtimer_cancel(p
->po_transmit_timer
);
1924 bondport_free(bondport_ref p
)
1926 multicast_list_remove(&p
->po_multicast
);
1927 devtimer_release(p
->po_current_while_timer
);
1928 devtimer_release(p
->po_periodic_timer
);
1929 devtimer_release(p
->po_wait_while_timer
);
1930 devtimer_release(p
->po_transmit_timer
);
1935 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1936 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1937 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1938 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1940 static __inline__
int
1941 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1943 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1944 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1948 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1955 bondport_ref
* new_array
= NULL
;
1956 bondport_ref
* old_array
= NULL
;
1960 /* pre-allocate space for new port */
1961 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1966 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1967 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1970 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1973 /* make sure this interface can handle our current MTU */
1974 devmtu
= bond_device_mtu(ifp
, ifb
);
1976 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
1978 printf("if_bond: interface %s doesn't support mtu %d",
1979 bondport_get_name(p
), devmtu
);
1984 /* make sure ifb doesn't get de-allocated while we wait */
1987 /* wait for other add or remove to complete */
1988 ifbond_wait(ifb
, "bond_add_interface");
1990 if (ifbond_flags_if_detaching(ifb
)) {
1991 /* someone destroyed the bond while we were waiting */
1995 if (bond_lookup_port(port_ifp
) != NULL
) {
1996 /* port is already part of a bond */
2000 ifnet_lock_exclusive(port_ifp
);
2001 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2002 /* interface already has VLAN's, or is part of bond */
2003 ifnet_lock_done(port_ifp
);
2008 /* mark the interface busy */
2009 /* can't use ifnet_set_eflags because that takes the lock */
2010 port_ifp
->if_eflags
|= IFEF_BOND
;
2011 ifnet_lock_done(port_ifp
);
2013 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2014 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2015 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2016 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2020 ifnet_offload_t ifp_offload
;
2021 ifnet_offload_t port_ifp_offload
;
2023 ifp_offload
= ifnet_offload(ifp
);
2024 port_ifp_offload
= ifnet_offload(port_ifp
);
2025 if (ifp_offload
!= port_ifp_offload
) {
2026 ifnet_offload_t offload
;
2028 offload
= ifp_offload
& port_ifp_offload
;
2029 printf("bond_add_interface(%s, %s) "
2030 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2031 ifb
->ifb_name
, bondport_get_name(p
),
2032 ifp_offload
, port_ifp_offload
, offload
);
2035 * if the bond has VLAN's, we can't simply change the hwassist
2036 * field behind its back: this needs work
2038 ifnet_set_offload(ifp
, offload
);
2043 /* remember the port's ethernet address so it can be restored */
2044 ether_addr_copy(&p
->po_saved_addr
, IF_LLADDR(port_ifp
));
2046 /* add it to the list of ports */
2047 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2048 ifb
->ifb_port_count
++;
2050 /* set the default MTU */
2051 if (ifnet_mtu(ifp
) == 0) {
2052 ifnet_set_mtu(ifp
, ETHERMTU
);
2057 /* first port added to bond determines bond's ethernet address */
2059 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(port_ifp
), ETHER_ADDR_LEN
,
2063 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2065 /* allocate a larger distributing array */
2066 new_array
= (bondport_ref
*)
2067 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2068 if (new_array
== NULL
) {
2073 /* attach our BOND "protocol" to the interface */
2074 error
= bond_attach_protocol(port_ifp
);
2078 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2080 /* set the interface MTU */
2081 devmtu
= bond_device_mtu(ifp
, ifb
);
2082 error
= siocsifmtu(port_ifp
, devmtu
);
2084 printf("bond_add_interface(%s, %s):"
2085 " SIOCSIFMTU %d failed %d\n",
2086 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2089 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2091 /* program the port with our multicast addresses */
2092 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2094 printf("bond_add_interface(%s, %s):"
2095 " multicast_list_program failed %d\n",
2096 ifb
->ifb_name
, bondport_get_name(p
), error
);
2100 /* mark the interface up */
2101 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2103 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2105 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2106 ifb
->ifb_name
, bondport_get_name(p
), error
);
2110 /* re-program the port's ethernet address */
2111 error
= if_siflladdr(port_ifp
,
2112 (const struct ether_addr
*)IF_LLADDR(ifp
));
2114 /* port doesn't support setting the link address */
2115 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2116 ifb
->ifb_name
, bondport_get_name(p
), error
);
2119 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2123 /* no failures past this point */
2126 /* copy the contents of the existing distributing array */
2127 if (ifb
->ifb_distributing_count
) {
2128 bcopy(ifb
->ifb_distributing_array
, new_array
,
2129 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2131 old_array
= ifb
->ifb_distributing_array
;
2132 ifb
->ifb_distributing_array
= new_array
;
2134 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2137 /* check if we need to generate a link status event */
2138 if (ifbond_selection(ifb
)) {
2139 event_code
= (ifb
->ifb_active_lag
== NULL
)
2142 ifb
->ifb_last_link_event
= event_code
;
2146 /* are we adding the first distributing interface? */
2147 if (media_active(&p
->po_media_info
)) {
2148 if (ifb
->ifb_distributing_count
== 0) {
2149 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2151 bondport_enable_distributing(p
);
2154 bondport_disable_distributing(p
);
2157 /* clear the busy state, and wakeup anyone waiting */
2158 ifbond_signal(ifb
, "bond_add_interface");
2160 if (event_code
!= 0) {
2161 interface_link_event(ifp
, event_code
);
2163 if (old_array
!= NULL
) {
2164 FREE(old_array
, M_BOND
);
2169 bond_assert_lock_not_held();
2171 /* if this was the first port to be added, clear our address */
2173 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2176 if (new_array
!= NULL
) {
2177 FREE(new_array
, M_BOND
);
2179 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2182 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2184 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2185 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2188 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2189 (void)bond_detach_protocol(port_ifp
);
2191 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2194 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2196 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2197 ifb
->ifb_name
, bondport_get_name(p
),
2198 p
->po_devmtu
.ifdm_current
, error1
);
2202 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2203 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2204 ifb
->ifb_port_count
--;
2206 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2207 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2208 ifb
->ifb_altmtu
= 0;
2209 ifnet_set_mtu(ifp
, 0);
2210 ifnet_set_offload(ifp
, 0);
2214 ifbond_signal(ifb
, "bond_add_interface");
2216 ifbond_release(ifb
);
2222 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2227 bondport_ref head_port
;
2230 int new_link_address
= FALSE
;
2232 lacp_actor_partner_state s
;
2233 int was_distributing
;
2235 bond_assert_lock_held();
2238 ifbond_wait(ifb
, "bond_remove_interface");
2240 p
= ifbond_lookup_port(ifb
, port_ifp
);
2243 /* it got removed by another thread */
2247 /* de-select it and remove it from the lists */
2248 was_distributing
= bondport_flags_distributing(p
);
2249 bondport_disable_distributing(p
);
2250 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2251 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2252 active_lag
= bondport_remove_from_LAG(p
);
2253 /* invalidate timers here while holding the bond_lock */
2254 bondport_invalidate_timers(p
);
2256 /* announce that we're Individual now */
2257 s
= p
->po_actor_state
;
2258 s
= lacp_actor_partner_state_set_individual(s
);
2259 s
= lacp_actor_partner_state_set_not_collecting(s
);
2260 s
= lacp_actor_partner_state_set_not_distributing(s
);
2261 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2262 p
->po_actor_state
= s
;
2263 bondport_flags_set_ntt(p
);
2266 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2267 ifb
->ifb_port_count
--;
2270 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2271 if (head_port
== NULL
) {
2272 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2273 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2276 ifnet_set_offload(ifp
, 0);
2277 ifnet_set_mtu(ifp
, 0);
2278 ifb
->ifb_altmtu
= 0;
2279 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2280 && bcmp(&p
->po_saved_addr
, IF_LLADDR(ifp
),
2281 ETHER_ADDR_LEN
) == 0) {
2282 new_link_address
= TRUE
;
2284 /* check if we need to generate a link status event */
2285 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2286 if (ifbond_selection(ifb
) || active_lag
) {
2287 event_code
= (ifb
->ifb_active_lag
== NULL
)
2290 ifb
->ifb_last_link_event
= event_code
;
2292 bondport_transmit_machine(p
, LAEventStart
,
2293 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2296 /* are we removing the last distributing interface? */
2297 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2298 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2305 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2307 else if (new_link_address
) {
2308 struct ifnet
* scan_ifp
;
2309 bondport_ref scan_port
;
2311 /* ifbond_wait() allows port list traversal without holding the lock */
2313 /* this port gave the bond its ethernet address, switch to new one */
2314 ifnet_set_lladdr_and_type(ifp
,
2315 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2318 /* re-program each port with the new link address */
2319 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2320 scan_ifp
= scan_port
->po_ifp
;
2322 error
= if_siflladdr(scan_ifp
,
2323 (const struct ether_addr
*) IF_LLADDR(ifp
));
2325 printf("bond_remove_interface(%s, %s): "
2326 "if_siflladdr (%s) failed %d\n",
2327 ifb
->ifb_name
, bondport_get_name(p
),
2328 bondport_get_name(scan_port
), error
);
2333 /* restore the port's ethernet address */
2334 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2336 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2337 ifb
->ifb_name
, bondport_get_name(p
), error
);
2340 /* restore the port's MTU */
2341 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2343 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2344 ifb
->ifb_name
, bondport_get_name(p
),
2345 p
->po_devmtu
.ifdm_current
, error
);
2348 /* remove the bond "protocol" */
2349 bond_detach_protocol(port_ifp
);
2351 /* generate link event */
2352 if (event_code
!= 0) {
2353 interface_link_event(ifp
, event_code
);
2358 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2359 /* release this bondport's reference to the ifbond */
2360 ifbond_release(ifb
);
2363 ifbond_signal(ifb
, "bond_remove_interface");
2364 ifbond_release(ifb
);
2369 bond_set_lacp_mode(ifbond_ref ifb
)
2373 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2374 bondport_disable_distributing(p
);
2381 bond_set_static_mode(ifbond_ref ifb
)
2384 lacp_actor_partner_state s
;
2386 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2387 bondport_disable_distributing(p
);
2388 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2389 (void)bondport_remove_from_LAG(p
);
2390 bondport_cancel_timers(p
);
2392 /* announce that we're Individual now */
2393 s
= p
->po_actor_state
;
2394 s
= lacp_actor_partner_state_set_individual(s
);
2395 s
= lacp_actor_partner_state_set_not_collecting(s
);
2396 s
= lacp_actor_partner_state_set_not_distributing(s
);
2397 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2398 p
->po_actor_state
= s
;
2399 bondport_flags_set_ntt(p
);
2400 bondport_transmit_machine(p
, LAEventStart
,
2401 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2403 p
->po_actor_state
= 0;
2404 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2406 if (media_active(&p
->po_media_info
)) {
2407 bondport_enable_distributing(p
);
2410 bondport_disable_distributing(p
);
2417 bond_set_mode(struct ifnet
* ifp
, int mode
)
2424 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2425 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2427 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2429 if (ifb
->ifb_mode
== mode
) {
2435 ifbond_wait(ifb
, "bond_set_mode");
2437 /* verify (again) that the mode is actually different */
2438 if (ifb
->ifb_mode
== mode
) {
2443 ifb
->ifb_mode
= mode
;
2444 if (mode
== IF_BOND_MODE_LACP
) {
2445 bond_set_lacp_mode(ifb
);
2447 /* check if we need to generate a link status event */
2448 if (ifbond_selection(ifb
)) {
2449 event_code
= (ifb
->ifb_active_lag
== NULL
)
2454 bond_set_static_mode(ifb
);
2455 event_code
= (ifb
->ifb_distributing_count
== 0)
2459 ifb
->ifb_last_link_event
= event_code
;
2462 ifbond_signal(ifb
, "bond_set_mode");
2464 ifbond_release(ifb
);
2466 if (event_code
!= 0) {
2467 interface_link_event(ifp
, event_code
);
2473 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2478 struct if_bond_status_req
* ibsr
;
2479 struct if_bond_status ibs
;
2482 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2483 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2486 ibsr
->ibsr_key
= ifb
->ifb_key
;
2487 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2488 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2489 dst
= proc_is64bit(current_proc())
2490 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2491 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2492 if (dst
== USER_ADDR_NULL
) {
2493 /* just want to know how many there are */
2496 if (ibsr
->ibsr_count
< 0) {
2499 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2500 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2501 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2502 struct if_bond_partner_state
* ibps_p
;
2503 partner_state_ref ps
;
2508 bzero(&ibs
, sizeof(ibs
));
2509 strlcpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2510 ibs
.ibs_port_priority
= port
->po_priority
;
2511 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2512 ibs
.ibs_state
= port
->po_actor_state
;
2513 ibs
.ibs_selected_state
= port
->po_selected
;
2514 ps
= &port
->po_partner_state
;
2515 ibps_p
= &ibs
.ibs_partner_state
;
2516 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2517 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2518 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2519 ibps_p
->ibps_port
= ps
->ps_port
;
2520 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2521 ibps_p
->ibps_state
= ps
->ps_state
;
2524 /* fake the selected information */
2525 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2526 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2528 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2538 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2541 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2547 bond_set_promisc(__unused
struct ifnet
*ifp
)
2554 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2560 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2561 mtu_min
= IF_MINMTU
;
2563 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2564 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2566 if (devmtu_p
->ifdm_min
> mtu_min
) {
2567 mtu_min
= devmtu_p
->ifdm_min
;
2569 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2570 mtu_max
= devmtu_p
->ifdm_max
;
2579 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2584 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2585 error
= siocsifmtu(p
->po_ifp
, mtu
);
2587 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2588 ifb
->ifb_name
, bondport_get_name(p
), error
);
2596 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2606 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2607 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2608 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2612 ifbond_wait(ifb
, "bond_set_mtu");
2615 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2619 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2620 if (mtu
> mtu_max
) {
2624 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2625 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2630 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2633 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2635 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2636 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2637 if (new_max
!= old_max
) {
2638 /* we can safely walk the list of port without the lock held */
2640 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2642 /* try our best to back out of it */
2643 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2649 ifb
->ifb_altmtu
= mtu
;
2652 ifnet_set_mtu(ifp
, mtu
);
2657 ifbond_signal(ifb
, "bond_set_mtu");
2658 ifbond_release(ifb
);
2666 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2669 struct if_bond_req ibr
;
2670 struct ifaddr
* ifa
;
2673 struct ifmediareq
*ifmr
;
2674 struct ifnet
* port_ifp
= NULL
;
2675 user_addr_t user_addr
;
2677 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2678 return (EOPNOTSUPP
);
2680 ifr
= (struct ifreq
*)data
;
2681 ifa
= (struct ifaddr
*)data
;
2685 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2688 case SIOCGIFMEDIA32
:
2689 case SIOCGIFMEDIA64
:
2691 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2692 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2694 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2696 ifmr
= (struct ifmediareq
*)data
;
2697 ifmr
->ifm_current
= IFM_ETHER
;
2699 ifmr
->ifm_status
= IFM_AVALID
;
2700 ifmr
->ifm_active
= IFM_ETHER
;
2701 ifmr
->ifm_count
= 1;
2702 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2703 if (ifb
->ifb_active_lag
!= NULL
) {
2704 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2705 ifmr
->ifm_status
|= IFM_ACTIVE
;
2708 else if (ifb
->ifb_distributing_count
> 0) {
2710 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2711 ifmr
->ifm_status
|= IFM_ACTIVE
;
2714 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2715 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2716 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2717 if (user_addr
!= USER_ADDR_NULL
) {
2718 error
= copyout(&ifmr
->ifm_current
,
2725 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2731 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2732 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2734 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2737 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2738 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2739 &ifr
->ifr_devmtu
.ifdm_max
);
2745 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2746 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2748 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2751 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2756 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2760 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2764 user_addr
= proc_is64bit(current_proc())
2765 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2766 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2770 switch (ibr
.ibr_op
) {
2771 case IF_BOND_OP_ADD_INTERFACE
:
2772 case IF_BOND_OP_REMOVE_INTERFACE
:
2773 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2774 if (port_ifp
== NULL
) {
2778 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2779 error
= EPROTONOSUPPORT
;
2783 case IF_BOND_OP_SET_VERBOSE
:
2784 case IF_BOND_OP_SET_MODE
:
2793 switch (ibr
.ibr_op
) {
2794 case IF_BOND_OP_ADD_INTERFACE
:
2795 error
= bond_add_interface(ifp
, port_ifp
);
2797 case IF_BOND_OP_REMOVE_INTERFACE
:
2799 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2800 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2802 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2804 error
= bond_remove_interface(ifb
, port_ifp
);
2807 case IF_BOND_OP_SET_VERBOSE
:
2809 if (g_bond
== NULL
) {
2814 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2817 case IF_BOND_OP_SET_MODE
:
2818 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2819 case IF_BOND_MODE_LACP
:
2820 case IF_BOND_MODE_STATIC
:
2829 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2832 break; /* SIOCSIFBOND */
2835 user_addr
= proc_is64bit(current_proc())
2836 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2837 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2841 switch (ibr
.ibr_op
) {
2842 case IF_BOND_OP_GET_STATUS
:
2852 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2853 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2855 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2857 switch (ibr
.ibr_op
) {
2858 case IF_BOND_OP_GET_STATUS
:
2859 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2863 break; /* SIOCGIFBOND */
2870 /* enable/disable promiscuous mode */
2872 error
= bond_set_promisc(ifp
);
2878 error
= bond_setmulti(ifp
);
2887 bond_if_free(struct ifnet
* ifp
)
2895 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2900 ifbond_release(ifb
);
2907 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
2909 struct ifnet
* bond_ifp
= NULL
;
2911 int old_distributing_count
;
2913 struct media_info media_info
= { 0, 0};
2915 switch (event_code
) {
2916 case KEV_DL_IF_DETACHED
:
2918 case KEV_DL_LINK_OFF
:
2919 case KEV_DL_LINK_ON
:
2920 media_info
= interface_media_info(port_ifp
);
2926 p
= bond_lookup_port(port_ifp
);
2932 old_distributing_count
= ifb
->ifb_distributing_count
;
2933 switch (event_code
) {
2934 case KEV_DL_IF_DETACHED
:
2935 bond_remove_interface(ifb
, p
->po_ifp
);
2937 case KEV_DL_LINK_OFF
:
2938 case KEV_DL_LINK_ON
:
2939 p
->po_media_info
= media_info
;
2940 if (p
->po_enabled
) {
2941 bondport_link_status_changed(p
);
2945 /* generate a link-event */
2946 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2947 if (ifbond_selection(ifb
)) {
2948 event_code
= (ifb
->ifb_active_lag
== NULL
)
2951 /* XXX need to take a reference on bond_ifp */
2952 bond_ifp
= ifb
->ifb_ifp
;
2953 ifb
->ifb_last_link_event
= event_code
;
2956 event_code
= (ifb
->ifb_active_lag
== NULL
)
2959 if (event_code
!= ifb
->ifb_last_link_event
) {
2960 if (g_bond
->verbose
) {
2961 timestamp_printf("%s: (event) generating LINK event\n",
2964 bond_ifp
= ifb
->ifb_ifp
;
2965 ifb
->ifb_last_link_event
= event_code
;
2971 * if the distributing array membership changed from 0 <-> !0
2972 * generate a link event
2974 if (old_distributing_count
== 0
2975 && ifb
->ifb_distributing_count
!= 0) {
2976 event_code
= KEV_DL_LINK_ON
;
2978 else if (old_distributing_count
!= 0
2979 && ifb
->ifb_distributing_count
== 0) {
2980 event_code
= KEV_DL_LINK_OFF
;
2982 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
2983 bond_ifp
= ifb
->ifb_ifp
;
2984 ifb
->ifb_last_link_event
= event_code
;
2989 if (bond_ifp
!= NULL
) {
2990 interface_link_event(bond_ifp
, event_code
);
2996 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
2997 const struct kev_msg
* event
)
3001 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3002 || event
->kev_class
!= KEV_NETWORK_CLASS
3003 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3006 event_code
= event
->event_code
;
3007 switch (event_code
) {
3008 case KEV_DL_LINK_OFF
:
3009 case KEV_DL_LINK_ON
:
3010 /* we only care about link status changes */
3011 bond_handle_event(port_ifp
, event_code
);
3020 bond_detached(ifnet_t port_ifp
, __unused protocol_family_t protocol
)
3022 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3027 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3030 struct kern_event_msg header
;
3032 char if_name
[IFNAMSIZ
];
3035 bzero(&event
, sizeof(event
));
3036 event
.header
.total_size
= sizeof(event
);
3037 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3038 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3039 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3040 event
.header
.event_code
= event_code
;
3041 event
.header
.event_data
[0] = ifnet_family(ifp
);
3042 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3043 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3044 ifnet_event(ifp
, &event
.header
);
3049 * Function: bond_attach_protocol
3051 * Attach a DLIL protocol to the interface.
3053 * The ethernet demux special cases to always return PF_BOND if the
3054 * interface is bonded. That means we receive all traffic from that
3055 * interface without passing any of the traffic to any other attached
3059 bond_attach_protocol(struct ifnet
*ifp
)
3062 struct ifnet_attach_proto_param reg
;
3064 bzero(®
, sizeof(reg
));
3065 reg
.input
= bond_input
;
3066 reg
.event
= bond_event
;
3067 reg
.detached
= bond_detached
;
3069 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3071 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3072 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3078 * Function: bond_detach_protocol
3080 * Detach our DLIL protocol from an interface
3083 bond_detach_protocol(struct ifnet
*ifp
)
3087 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3089 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3090 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3096 * DLIL interface family functions
3098 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3099 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3100 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3101 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3102 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3103 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3105 __private_extern__
int
3106 bond_family_init(void)
3110 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3114 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3119 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3121 ether_detach_inet6
);
3123 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3128 error
= bond_clone_attach();
3130 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3145 ** LACP ifbond_list routines
3148 ifbond_list_find_moved_port(bondport_ref rx_port
,
3149 const lacp_actor_partner_tlv_ref atlv
)
3153 partner_state_ref ps
;
3156 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3157 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3160 /* no point in comparing against ourselves */
3163 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3164 /* it's not clear that we should be checking this */
3167 ps
= &p
->po_partner_state
;
3168 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3171 ps_li
= &ps
->ps_lag_info
;
3172 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3173 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3174 sizeof(ps_li
->li_system
)) == 0) {
3175 if (g_bond
->verbose
) {
3176 timestamp_printf("System " EA_FORMAT
3177 " Port 0x%x moved from %s to %s\n",
3178 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3179 bondport_get_name(p
),
3180 bondport_get_name(rx_port
));
3190 ** LACP ifbond, LAG routines
3194 ifbond_selection(ifbond_ref bond
)
3196 int all_ports_ready
= 0;
3197 int active_media
= 0;
3199 int lag_changed
= 0;
3203 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3204 if (lag
!= bond
->ifb_active_lag
) {
3205 if (bond
->ifb_active_lag
!= NULL
) {
3206 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3207 bond
->ifb_active_lag
= NULL
;
3209 bond
->ifb_active_lag
= lag
;
3211 ifbond_activate_LAG(bond
, lag
, active_media
);
3215 else if (lag
!= NULL
) {
3216 if (lag
->lag_active_media
!= active_media
) {
3217 if (g_bond
->verbose
) {
3218 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3219 link_speed(lag
->lag_active_media
),
3220 link_speed(active_media
));
3222 ifbond_deactivate_LAG(bond
, lag
);
3223 ifbond_activate_LAG(bond
, lag
, active_media
);
3228 port_speed
= link_speed(active_media
);
3229 all_ports_ready
= ifbond_all_ports_ready(bond
);
3231 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3232 if (lag
!= NULL
&& p
->po_lag
== lag
3233 && media_speed(&p
->po_media_info
) == port_speed
3234 && (p
->po_mux_state
== MuxState_DETACHED
3235 || p
->po_selected
== SelectedState_SELECTED
3236 || p
->po_selected
== SelectedState_STANDBY
)
3237 && bondport_aggregatable(p
)) {
3238 if (bond
->ifb_max_active
> 0) {
3239 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3240 if (p
->po_selected
== SelectedState_STANDBY
3241 || p
->po_selected
== SelectedState_UNSELECTED
) {
3242 bondport_set_selected(p
, SelectedState_SELECTED
);
3245 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3246 bondport_set_selected(p
, SelectedState_STANDBY
);
3250 bondport_set_selected(p
, SelectedState_SELECTED
);
3253 if (bondport_flags_selected_changed(p
)) {
3254 bondport_flags_clear_selected_changed(p
);
3255 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3258 && bondport_flags_ready(p
)
3259 && p
->po_mux_state
== MuxState_WAITING
) {
3260 bondport_mux_machine(p
, LAEventReady
, NULL
);
3262 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3264 return (lag_changed
);
3268 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3270 int best_active
= 0;
3271 LAG_ref best_lag
= NULL
;
3276 if (bond
->ifb_active_lag
!= NULL
) {
3277 best_lag
= bond
->ifb_active_lag
;
3278 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3279 if (bond
->ifb_max_active
> 0
3280 && best_count
> bond
->ifb_max_active
) {
3281 best_count
= bond
->ifb_max_active
;
3283 best_speed
= link_speed(best_active
);
3285 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3290 if (lag
== bond
->ifb_active_lag
) {
3291 /* we've already computed it */
3294 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3298 if (bond
->ifb_max_active
> 0
3299 && count
> bond
->ifb_max_active
) {
3300 /* if there's a limit, don't count extra links */
3301 count
= bond
->ifb_max_active
;
3303 speed
= link_speed(active
);
3304 if ((count
* speed
) > (best_count
* best_speed
)) {
3307 best_active
= active
;
3311 if (best_count
== 0) {
3314 *active_media
= best_active
;
3319 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3323 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3324 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3330 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3335 if (bond
->ifb_max_active
> 0) {
3336 need
= bond
->ifb_max_active
;
3338 lag
->lag_active_media
= active_media
;
3339 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3340 if (bondport_aggregatable(p
) == 0) {
3341 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3343 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3344 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3346 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3347 if (bond
->ifb_max_active
> 0) {
3349 bondport_set_selected(p
, SelectedState_SELECTED
);
3353 bondport_set_selected(p
, SelectedState_STANDBY
);
3357 bondport_set_selected(p
, SelectedState_SELECTED
);
3361 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3369 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3371 LAG_ref lag
= bond
->ifb_active_lag
;
3373 bond
->ifb_max_active
= max_active
;
3374 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3377 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3381 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3382 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3383 if (p
->po_selected
== SelectedState_SELECTED
) {
3384 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3386 if (remove_count
== 0) {
3397 ifbond_all_ports_ready(ifbond_ref bond
)
3402 if (bond
->ifb_active_lag
== NULL
) {
3405 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3406 if (p
->po_mux_state
== MuxState_WAITING
3407 && p
->po_selected
== SelectedState_SELECTED
) {
3408 if (bondport_flags_ready(p
) == 0) {
3412 /* note that there was at least one ready port */
3419 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3423 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3424 if (this_port
== p
) {
3427 if (bondport_flags_mux_attached(p
) == 0) {
3435 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3439 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3440 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3441 sizeof(lag
->lag_info
)) == 0) {
3449 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3459 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3460 if (bondport_aggregatable(p
)) {
3463 this_speed
= media_speed(&p
->po_media_info
);
3464 if (this_speed
== 0) {
3467 if (this_speed
> speed
) {
3468 active
= p
->po_media_info
.mi_active
;
3472 else if (this_speed
== speed
) {
3477 *active_media
= active
;
3483 ** LACP bondport routines
3486 bondport_link_status_changed(bondport_ref p
)
3488 ifbond_ref bond
= p
->po_bond
;
3490 if (g_bond
->verbose
) {
3491 if (media_active(&p
->po_media_info
)) {
3492 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3493 bondport_get_name(p
),
3494 media_speed(&p
->po_media_info
),
3495 media_full_duplex(&p
->po_media_info
)
3499 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3502 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3503 if (media_active(&p
->po_media_info
)
3504 && bond
->ifb_active_lag
!= NULL
3505 && p
->po_lag
== bond
->ifb_active_lag
3506 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3507 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3508 if (g_bond
->verbose
) {
3509 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3510 bondport_get_name(p
),
3511 media_speed(&p
->po_media_info
),
3512 link_speed(p
->po_lag
->lag_active_media
));
3514 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3517 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3518 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3519 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3522 if (media_active(&p
->po_media_info
)) {
3523 bondport_enable_distributing(p
);
3526 bondport_disable_distributing(p
);
3533 bondport_aggregatable(bondport_ref p
)
3535 partner_state_ref ps
= &p
->po_partner_state
;
3537 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3538 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3539 /* we and/or our partner are individual */
3542 if (p
->po_lag
== NULL
) {
3545 switch (p
->po_receive_state
) {
3547 if (g_bond
->verbose
) {
3548 timestamp_printf("[%s] Port is not selectable\n",
3549 bondport_get_name(p
));
3552 case ReceiveState_CURRENT
:
3553 case ReceiveState_EXPIRED
:
3560 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3562 LAG_info_ref lag_li
;
3563 partner_state_ref ps
;
3566 ps
= &p
->po_partner_state
;
3567 ps_li
= &ps
->ps_lag_info
;
3568 lag_li
= &lag
->lag_info
;
3569 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3570 && ps_li
->li_key
== lag_li
->li_key
3571 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3572 sizeof(lag_li
->li_system
))
3580 bondport_remove_from_LAG(bondport_ref p
)
3583 ifbond_ref bond
= p
->po_bond
;
3584 LAG_ref lag
= p
->po_lag
;
3589 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3590 if (g_bond
->verbose
) {
3591 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3593 bondport_get_name(p
),
3594 lag
->lag_info
.li_system_priority
,
3595 EA_LIST(&lag
->lag_info
.li_system
),
3596 lag
->lag_info
.li_key
);
3599 lag
->lag_port_count
--;
3600 if (lag
->lag_port_count
> 0) {
3601 return (bond
->ifb_active_lag
== lag
);
3603 if (g_bond
->verbose
) {
3604 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3607 lag
->lag_info
.li_system_priority
,
3608 EA_LIST(&lag
->lag_info
.li_system
),
3609 lag
->lag_info
.li_key
);
3611 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3612 if (bond
->ifb_active_lag
== lag
) {
3613 bond
->ifb_active_lag
= NULL
;
3617 return (active_lag
);
3621 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3623 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3625 lag
->lag_port_count
++;
3626 if (g_bond
->verbose
) {
3627 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3628 bondport_get_name(p
),
3629 lag
->lag_info
.li_system_priority
,
3630 EA_LIST(&lag
->lag_info
.li_system
),
3631 lag
->lag_info
.li_key
);
3637 bondport_assign_to_LAG(bondport_ref p
)
3639 ifbond_ref bond
= p
->po_bond
;
3642 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3643 bondport_remove_from_LAG(p
);
3648 if (bondport_matches_LAG(p
, lag
)) {
3652 bondport_remove_from_LAG(p
);
3654 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3656 bondport_add_to_LAG(p
, lag
);
3659 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3660 TAILQ_INIT(&lag
->lag_port_list
);
3661 lag
->lag_port_count
= 0;
3662 lag
->lag_selected_port_count
= 0;
3663 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3664 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3665 if (g_bond
->verbose
) {
3666 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3669 lag
->lag_info
.li_system_priority
,
3670 EA_LIST(&lag
->lag_info
.li_system
),
3671 lag
->lag_info
.li_key
);
3673 bondport_add_to_LAG(p
, lag
);
3678 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3680 bondport_ref moved_port
;
3683 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3684 &in_lacpdu_p
->la_actor_tlv
);
3685 if (moved_port
!= NULL
) {
3686 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3688 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3689 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3690 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3695 bondport_set_selected(bondport_ref p
, SelectedState s
)
3697 if (s
!= p
->po_selected
) {
3698 ifbond_ref bond
= p
->po_bond
;
3699 LAG_ref lag
= p
->po_lag
;
3701 bondport_flags_set_selected_changed(p
);
3702 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3703 if (p
->po_selected
== SelectedState_SELECTED
) {
3704 lag
->lag_selected_port_count
--;
3706 else if (s
== SelectedState_SELECTED
) {
3707 lag
->lag_selected_port_count
++;
3709 if (g_bond
->verbose
) {
3710 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3711 bondport_get_name(p
),
3712 SelectedStateString(s
),
3713 SelectedStateString(p
->po_selected
));
3726 bondport_UpdateDefaultSelected(bondport_ref p
)
3728 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3733 bondport_RecordDefault(bondport_ref p
)
3735 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3737 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3738 bondport_assign_to_LAG(p
);
3743 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3745 lacp_actor_partner_tlv_ref actor
;
3746 partner_state_ref ps
;
3749 /* compare the PDU's Actor information to our Partner state */
3750 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3751 ps
= &p
->po_partner_state
;
3752 ps_li
= &ps
->ps_lag_info
;
3753 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3754 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3755 != ps
->ps_port_priority
)
3756 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3757 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3758 != ps_li
->li_system_priority
)
3759 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3760 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3761 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3762 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3763 if (g_bond
->verbose
) {
3764 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3765 bondport_get_name(p
));
3772 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3774 lacp_actor_partner_tlv_ref actor
;
3775 ifbond_ref bond
= p
->po_bond
;
3776 int lacp_maintain
= 0;
3777 partner_state_ref ps
;
3778 lacp_actor_partner_tlv_ref partner
;
3781 /* copy the PDU's Actor information into our Partner state */
3782 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3783 ps
= &p
->po_partner_state
;
3784 ps_li
= &ps
->ps_lag_info
;
3785 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3786 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3787 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3788 ps_li
->li_system_priority
3789 = lacp_actor_partner_tlv_get_system_priority(actor
);
3790 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3791 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3793 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3795 /* compare the PDU's Partner information to our own information */
3796 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3798 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3799 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3800 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3801 if (g_bond
->verbose
) {
3802 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3803 bondport_get_name(p
));
3807 if ((lacp_actor_partner_tlv_get_port(partner
)
3808 == bondport_get_index(p
))
3809 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3810 && bcmp(partner
->lap_system
, &g_bond
->system
,
3811 sizeof(g_bond
->system
)) == 0
3812 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3813 == g_bond
->system_priority
)
3814 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3815 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3816 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3817 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3819 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3820 if (g_bond
->verbose
) {
3821 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3822 bondport_get_name(p
));
3825 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3826 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3828 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3829 if (g_bond
->verbose
) {
3830 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3831 bondport_get_name(p
));
3834 bondport_assign_to_LAG(p
);
3838 static __inline__ lacp_actor_partner_state
3839 updateNTTBits(lacp_actor_partner_state s
)
3841 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3842 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3843 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3844 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3848 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3850 ifbond_ref bond
= p
->po_bond
;
3851 lacp_actor_partner_tlv_ref partner
;
3853 /* compare the PDU's Actor information to our Partner state */
3854 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3855 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3856 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3857 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3858 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3859 != g_bond
->system_priority
)
3860 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3861 || (updateNTTBits(partner
->lap_state
)
3862 != updateNTTBits(p
->po_actor_state
))) {
3863 bondport_flags_set_ntt(p
);
3864 if (g_bond
->verbose
) {
3865 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3866 bondport_get_name(p
));
3873 bondport_AttachMuxToAggregator(bondport_ref p
)
3875 if (bondport_flags_mux_attached(p
) == 0) {
3876 if (g_bond
->verbose
) {
3877 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3878 bondport_get_name(p
));
3880 bondport_flags_set_mux_attached(p
);
3886 bondport_DetachMuxFromAggregator(bondport_ref p
)
3888 if (bondport_flags_mux_attached(p
)) {
3889 if (g_bond
->verbose
) {
3890 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3891 bondport_get_name(p
));
3893 bondport_flags_clear_mux_attached(p
);
3899 bondport_enable_distributing(bondport_ref p
)
3901 if (bondport_flags_distributing(p
) == 0) {
3902 ifbond_ref bond
= p
->po_bond
;
3904 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3905 if (g_bond
->verbose
) {
3906 timestamp_printf("[%s] Distribution Enabled\n",
3907 bondport_get_name(p
));
3909 bondport_flags_set_distributing(p
);
3915 bondport_disable_distributing(bondport_ref p
)
3917 if (bondport_flags_distributing(p
)) {
3918 bondport_ref
* array
;
3924 array
= bond
->ifb_distributing_array
;
3925 count
= bond
->ifb_distributing_count
;
3926 for (i
= 0; i
< count
; i
++) {
3927 if (array
[i
] == p
) {
3930 for (j
= i
; j
< (count
- 1); j
++) {
3931 array
[j
] = array
[j
+ 1];
3936 bond
->ifb_distributing_count
--;
3937 if (g_bond
->verbose
) {
3938 timestamp_printf("[%s] Distribution Disabled\n",
3939 bondport_get_name(p
));
3941 bondport_flags_clear_distributing(p
);
3947 ** Receive machine functions
3950 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3953 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3956 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3959 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3962 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3965 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3969 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
3972 switch (p
->po_receive_state
) {
3973 case ReceiveState_none
:
3974 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
3976 case ReceiveState_INITIALIZE
:
3977 bondport_receive_machine_initialize(p
, event
, event_data
);
3979 case ReceiveState_PORT_DISABLED
:
3980 bondport_receive_machine_port_disabled(p
, event
, event_data
);
3982 case ReceiveState_EXPIRED
:
3983 bondport_receive_machine_expired(p
, event
, event_data
);
3985 case ReceiveState_LACP_DISABLED
:
3986 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
3988 case ReceiveState_DEFAULTED
:
3989 bondport_receive_machine_defaulted(p
, event
, event_data
);
3991 case ReceiveState_CURRENT
:
3992 bondport_receive_machine_current(p
, event
, event_data
);
4001 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4006 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4007 bondport_receive_machine_current(p
, event
, event_data
);
4010 case LAEventMediaChange
:
4011 if (media_active(&p
->po_media_info
)) {
4012 switch (p
->po_receive_state
) {
4013 case ReceiveState_PORT_DISABLED
:
4014 case ReceiveState_LACP_DISABLED
:
4015 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4022 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4026 bondport_receive_machine_event(p
, event
, event_data
);
4033 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4034 __unused
void * event_data
)
4038 devtimer_cancel(p
->po_current_while_timer
);
4039 if (g_bond
->verbose
) {
4040 timestamp_printf("[%s] Receive INITIALIZE\n",
4041 bondport_get_name(p
));
4043 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4044 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4045 bondport_RecordDefault(p
);
4047 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4048 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4057 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4058 __unused
void * event_data
)
4060 partner_state_ref ps
;
4064 devtimer_cancel(p
->po_current_while_timer
);
4065 if (g_bond
->verbose
) {
4066 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4067 bondport_get_name(p
));
4069 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4070 ps
= &p
->po_partner_state
;
4071 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4073 case LAEventMediaChange
:
4074 if (media_active(&p
->po_media_info
)) {
4075 if (media_full_duplex(&p
->po_media_info
)) {
4076 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4079 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4082 else if (p
->po_selected
== SelectedState_SELECTED
) {
4085 if (g_bond
->verbose
) {
4086 timestamp_printf("[%s] Receive PORT_DISABLED: "
4087 "link timer started\n",
4088 bondport_get_name(p
));
4092 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4093 (devtimer_timeout_func
)
4094 bondport_receive_machine_port_disabled
,
4095 (void *)LAEventTimeout
, NULL
);
4097 else if (p
->po_selected
== SelectedState_STANDBY
) {
4098 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4101 case LAEventTimeout
:
4102 if (p
->po_selected
== SelectedState_SELECTED
) {
4103 if (g_bond
->verbose
) {
4104 timestamp_printf("[%s] Receive PORT_DISABLED: "
4105 "link timer completed, marking UNSELECTED\n",
4106 bondport_get_name(p
));
4108 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4111 case LAEventPortMoved
:
4112 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4121 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4122 __unused
void * event_data
)
4124 lacp_actor_partner_state s
;
4129 devtimer_cancel(p
->po_current_while_timer
);
4130 if (g_bond
->verbose
) {
4131 timestamp_printf("[%s] Receive EXPIRED\n",
4132 bondport_get_name(p
));
4134 p
->po_receive_state
= ReceiveState_EXPIRED
;
4135 s
= p
->po_partner_state
.ps_state
;
4136 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4137 s
= lacp_actor_partner_state_set_short_timeout(s
);
4138 p
->po_partner_state
.ps_state
= s
;
4140 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4141 /* start current_while timer */
4142 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4144 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4145 (devtimer_timeout_func
)
4146 bondport_receive_machine_expired
,
4147 (void *)LAEventTimeout
, NULL
);
4150 case LAEventTimeout
:
4151 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4160 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4161 __unused
void * event_data
)
4163 partner_state_ref ps
;
4166 devtimer_cancel(p
->po_current_while_timer
);
4167 if (g_bond
->verbose
) {
4168 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4169 bondport_get_name(p
));
4171 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4172 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4173 bondport_RecordDefault(p
);
4174 ps
= &p
->po_partner_state
;
4175 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4177 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4186 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4187 __unused
void * event_data
)
4191 devtimer_cancel(p
->po_current_while_timer
);
4192 if (g_bond
->verbose
) {
4193 timestamp_printf("[%s] Receive DEFAULTED\n",
4194 bondport_get_name(p
));
4196 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4197 bondport_UpdateDefaultSelected(p
);
4198 bondport_RecordDefault(p
);
4200 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4209 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4212 partner_state_ref ps
;
4217 devtimer_cancel(p
->po_current_while_timer
);
4218 if (g_bond
->verbose
) {
4219 timestamp_printf("[%s] Receive CURRENT\n",
4220 bondport_get_name(p
));
4222 p
->po_receive_state
= ReceiveState_CURRENT
;
4223 bondport_UpdateSelected(p
, event_data
);
4224 bondport_UpdateNTT(p
, event_data
);
4225 bondport_RecordPDU(p
, event_data
);
4227 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4228 bondport_assign_to_LAG(p
);
4229 /* start current_while timer */
4230 ps
= &p
->po_partner_state
;
4231 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4232 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4235 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4238 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4239 (devtimer_timeout_func
)
4240 bondport_receive_machine_current
,
4241 (void *)LAEventTimeout
, NULL
);
4243 case LAEventTimeout
:
4244 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4253 ** Periodic Transmission machine
4257 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4258 __unused
void * event_data
)
4261 partner_state_ref ps
;
4266 if (g_bond
->verbose
) {
4267 timestamp_printf("[%s] periodic_transmit Start\n",
4268 bondport_get_name(p
));
4271 case LAEventMediaChange
:
4272 devtimer_cancel(p
->po_periodic_timer
);
4273 p
->po_periodic_interval
= 0;
4274 if (media_active(&p
->po_media_info
) == 0
4275 || media_full_duplex(&p
->po_media_info
) == 0) {
4279 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4280 ps
= &p
->po_partner_state
;
4281 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4282 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4284 devtimer_cancel(p
->po_periodic_timer
);
4285 p
->po_periodic_interval
= 0;
4288 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4289 interval
= LACP_FAST_PERIODIC_TIME
;
4292 interval
= LACP_SLOW_PERIODIC_TIME
;
4294 if (p
->po_periodic_interval
!= interval
) {
4295 if (interval
== LACP_FAST_PERIODIC_TIME
4296 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4297 if (g_bond
->verbose
) {
4298 timestamp_printf("[%s] periodic_transmit:"
4299 " Need To Transmit\n",
4300 bondport_get_name(p
));
4302 bondport_flags_set_ntt(p
);
4304 p
->po_periodic_interval
= interval
;
4306 tv
.tv_sec
= interval
;
4307 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4308 (devtimer_timeout_func
)
4309 bondport_periodic_transmit_machine
,
4310 (void *)LAEventTimeout
, NULL
);
4311 if (g_bond
->verbose
) {
4312 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4313 bondport_get_name(p
),
4314 p
->po_periodic_interval
);
4318 case LAEventTimeout
:
4319 bondport_flags_set_ntt(p
);
4320 tv
.tv_sec
= p
->po_periodic_interval
;
4322 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4323 bondport_periodic_transmit_machine
,
4324 (void *)LAEventTimeout
, NULL
);
4325 if (g_bond
->verbose
> 1) {
4326 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4327 bondport_get_name(p
), p
->po_periodic_interval
);
4340 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4341 __darwin_time_t
* next_secs
)
4343 if (p
->po_last_transmit_secs
!= current_secs
) {
4344 p
->po_last_transmit_secs
= current_secs
;
4345 p
->po_n_transmit
= 0;
4347 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4351 if (next_secs
!= NULL
) {
4352 *next_secs
= current_secs
+ 1;
4358 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4361 lacp_actor_partner_tlv_ref aptlv
;
4362 lacp_collector_tlv_ref ctlv
;
4363 struct timeval next_tick_time
= {0, 0};
4364 lacpdu_ref out_lacpdu_p
;
4365 packet_buffer_ref pkt
;
4366 partner_state_ref ps
;
4370 case LAEventTimeout
:
4372 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4375 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4376 /* we're going away, transmit the packet no matter what */
4378 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4379 &next_tick_time
.tv_sec
) == 0) {
4380 if (devtimer_enabled(p
->po_transmit_timer
)) {
4381 if (g_bond
->verbose
> 0) {
4382 timestamp_printf("[%s] Transmit Timer Already Set\n",
4383 bondport_get_name(p
));
4387 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4388 (devtimer_timeout_func
)
4389 bondport_transmit_machine
,
4390 (void *)LAEventTimeout
, NULL
);
4391 if (g_bond
->verbose
> 0) {
4392 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4393 bondport_get_name(p
),
4394 (int)next_tick_time
.tv_sec
);
4399 if (g_bond
->verbose
> 0) {
4400 if (event
== LAEventTimeout
) {
4401 timestamp_printf("[%s] Transmit Timer Complete\n",
4402 bondport_get_name(p
));
4405 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4407 printf("[%s] Transmit: failed to allocate packet buffer\n",
4408 bondport_get_name(p
));
4411 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4412 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4413 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4414 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4417 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4418 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4419 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4420 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4421 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4422 g_bond
->system_priority
);
4423 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4424 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4425 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4426 aptlv
->lap_state
= p
->po_actor_state
;
4429 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4430 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4431 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4432 ps
= &p
->po_partner_state
;
4433 ps_li
= &ps
->ps_lag_info
;
4434 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4435 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4436 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4437 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4438 ps_li
->li_system_priority
);
4439 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4440 aptlv
->lap_state
= ps
->ps_state
;
4443 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4444 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4445 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4447 bondport_slow_proto_transmit(p
, pkt
);
4448 bondport_flags_clear_ntt(p
);
4449 if (g_bond
->verbose
> 0) {
4450 timestamp_printf("[%s] Transmit Packet %d\n",
4451 bondport_get_name(p
), p
->po_n_transmit
);
4461 ** Mux machine functions
4465 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4468 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4471 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4475 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4479 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4481 switch (p
->po_mux_state
) {
4483 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4485 case MuxState_DETACHED
:
4486 bondport_mux_machine_detached(p
, event
, event_data
);
4488 case MuxState_WAITING
:
4489 bondport_mux_machine_waiting(p
, event
, event_data
);
4491 case MuxState_ATTACHED
:
4492 bondport_mux_machine_attached(p
, event
, event_data
);
4494 case MuxState_COLLECTING_DISTRIBUTING
:
4495 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4504 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4505 __unused
void * event_data
)
4507 lacp_actor_partner_state s
;
4511 devtimer_cancel(p
->po_wait_while_timer
);
4512 if (g_bond
->verbose
) {
4513 timestamp_printf("[%s] Mux DETACHED\n",
4514 bondport_get_name(p
));
4516 p
->po_mux_state
= MuxState_DETACHED
;
4517 bondport_flags_clear_ready(p
);
4518 bondport_DetachMuxFromAggregator(p
);
4519 bondport_disable_distributing(p
);
4520 s
= p
->po_actor_state
;
4521 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4522 s
= lacp_actor_partner_state_set_not_collecting(s
);
4523 s
= lacp_actor_partner_state_set_not_distributing(s
);
4524 p
->po_actor_state
= s
;
4525 bondport_flags_set_ntt(p
);
4527 case LAEventSelectedChange
:
4529 case LAEventMediaChange
:
4530 if (p
->po_selected
== SelectedState_SELECTED
4531 || p
->po_selected
== SelectedState_STANDBY
) {
4532 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4542 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4543 __unused
void * event_data
)
4549 devtimer_cancel(p
->po_wait_while_timer
);
4550 if (g_bond
->verbose
) {
4551 timestamp_printf("[%s] Mux WAITING\n",
4552 bondport_get_name(p
));
4554 p
->po_mux_state
= MuxState_WAITING
;
4557 case LAEventSelectedChange
:
4558 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4559 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4562 if (p
->po_selected
== SelectedState_STANDBY
) {
4563 devtimer_cancel(p
->po_wait_while_timer
);
4564 /* wait until state changes to SELECTED */
4565 if (g_bond
->verbose
) {
4566 timestamp_printf("[%s] Mux WAITING: Standby\n",
4567 bondport_get_name(p
));
4571 if (bondport_flags_ready(p
)) {
4572 if (g_bond
->verbose
) {
4573 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4574 bondport_get_name(p
));
4578 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4579 if (g_bond
->verbose
) {
4580 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4581 bondport_get_name(p
));
4585 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4586 devtimer_cancel(p
->po_wait_while_timer
);
4587 if (g_bond
->verbose
) {
4588 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4589 bondport_get_name(p
));
4591 bondport_flags_set_ready(p
);
4594 if (g_bond
->verbose
) {
4595 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4596 bondport_get_name(p
));
4598 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4600 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4601 (devtimer_timeout_func
)
4602 bondport_mux_machine_waiting
,
4603 (void *)LAEventTimeout
, NULL
);
4605 case LAEventTimeout
:
4606 if (g_bond
->verbose
) {
4607 timestamp_printf("[%s] Mux WAITING: Ready\n",
4608 bondport_get_name(p
));
4610 bondport_flags_set_ready(p
);
4614 if (bondport_flags_ready(p
)){
4615 if (g_bond
->verbose
) {
4616 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4617 bondport_get_name(p
));
4619 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4628 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4629 __unused
void * event_data
)
4631 lacp_actor_partner_state s
;
4635 devtimer_cancel(p
->po_wait_while_timer
);
4636 if (g_bond
->verbose
) {
4637 timestamp_printf("[%s] Mux ATTACHED\n",
4638 bondport_get_name(p
));
4640 p
->po_mux_state
= MuxState_ATTACHED
;
4641 bondport_AttachMuxToAggregator(p
);
4642 s
= p
->po_actor_state
;
4643 s
= lacp_actor_partner_state_set_in_sync(s
);
4644 s
= lacp_actor_partner_state_set_not_collecting(s
);
4645 s
= lacp_actor_partner_state_set_not_distributing(s
);
4646 bondport_disable_distributing(p
);
4647 p
->po_actor_state
= s
;
4648 bondport_flags_set_ntt(p
);
4651 switch (p
->po_selected
) {
4652 case SelectedState_SELECTED
:
4653 s
= p
->po_partner_state
.ps_state
;
4654 if (lacp_actor_partner_state_in_sync(s
)) {
4655 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4660 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4669 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4671 __unused
void * event_data
)
4673 lacp_actor_partner_state s
;
4677 devtimer_cancel(p
->po_wait_while_timer
);
4678 if (g_bond
->verbose
) {
4679 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4680 bondport_get_name(p
));
4682 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4683 bondport_enable_distributing(p
);
4684 s
= p
->po_actor_state
;
4685 s
= lacp_actor_partner_state_set_collecting(s
);
4686 s
= lacp_actor_partner_state_set_distributing(s
);
4687 p
->po_actor_state
= s
;
4688 bondport_flags_set_ntt(p
);
4691 s
= p
->po_partner_state
.ps_state
;
4692 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4693 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4696 switch (p
->po_selected
) {
4697 case SelectedState_UNSELECTED
:
4698 case SelectedState_STANDBY
:
4699 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);