2 * Copyright (c) 2004-2019 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>
53 #include <net/ethernet.h>
55 #include <net/kpi_interface.h>
56 #include <net/kpi_interfacefilter.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>
69 #include <sys/protosw.h>
70 #include <kern/locks.h>
71 #include <kern/zalloc.h>
72 #include <os/refcnt.h>
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
80 #include <net/if_media.h>
81 #include <net/multicast_list.h>
83 SYSCTL_DECL(_net_link
);
84 SYSCTL_NODE(_net_link
, OID_AUTO
, bond
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
87 static int if_bond_debug
= 0;
88 SYSCTL_INT(_net_link_bond
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
89 &if_bond_debug
, 0, "Bond interface debug logs");
91 static struct ether_addr slow_proto_multicast
= {
92 .octet
= IEEE8023AD_SLOW_PROTO_MULTICAST
95 typedef struct ifbond_s ifbond
, * ifbond_ref
;
96 typedef struct bondport_s bondport
, * bondport_ref
;
98 #define BOND_MAXUNIT 128
99 #define BOND_ZONE_MAX_ELEM MIN(IFNETS_MAX, BOND_MAXUNIT)
100 #define BONDNAME "bond"
102 #define M_BOND M_DEVBUF
104 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
105 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
106 #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)
108 #define timestamp_printf printf
113 static __inline__ lck_grp_t
*
114 my_lck_grp_alloc_init(const char * grp_name
)
117 lck_grp_attr_t
* grp_attrs
;
119 grp_attrs
= lck_grp_attr_alloc_init();
120 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
121 lck_grp_attr_free(grp_attrs
);
125 static __inline__ lck_mtx_t
*
126 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
128 lck_attr_t
* lck_attrs
;
131 lck_attrs
= lck_attr_alloc_init();
132 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
133 lck_attr_free(lck_attrs
);
137 static lck_mtx_t
* bond_lck_mtx
;
139 static __inline__
void
142 lck_grp_t
* bond_lck_grp
;
144 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
145 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
148 static __inline__
void
149 bond_assert_lock_held(void)
151 LCK_MTX_ASSERT(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
155 static __inline__
void
156 bond_assert_lock_not_held(void)
158 LCK_MTX_ASSERT(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
162 static __inline__
void
165 lck_mtx_lock(bond_lck_mtx
);
169 static __inline__
void
172 lck_mtx_unlock(bond_lck_mtx
);
177 ** bond structures, types
181 lacp_system li_system
;
182 lacp_system_priority li_system_priority
;
185 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
188 TAILQ_HEAD(port_list
, bondport_s
);
190 TAILQ_HEAD(ifbond_list
, ifbond_s
);
192 TAILQ_HEAD(lag_list
, LAG_s
);
194 typedef struct ifbond_s ifbond
, * ifbond_ref
;
195 typedef struct bondport_s bondport
, * bondport_ref
;
198 TAILQ_ENTRY(LAG_s
) lag_list
;
199 struct port_list lag_port_list
;
200 short lag_port_count
;
201 short lag_selected_port_count
;
202 int lag_active_media
;
205 typedef struct LAG_s LAG
, * LAG_ref
;
207 typedef struct partner_state_s
{
208 LAG_info ps_lag_info
;
210 lacp_port_priority ps_port_priority
;
211 lacp_actor_partner_state ps_state
;
212 } partner_state
, * partner_state_ref
;
215 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
217 struct os_refcnt ifb_retain_count
;
218 char ifb_name
[IFNAMSIZ
];
219 struct ifnet
* ifb_ifp
;
220 bpf_packet_func ifb_bpf_input
;
221 bpf_packet_func ifb_bpf_output
;
223 struct port_list ifb_port_list
;
224 short ifb_port_count
;
225 struct lag_list ifb_lag_list
;
227 short ifb_max_active
;/* 0 == unlimited */
228 LAG_ref ifb_active_lag
;
229 struct ifmultiaddr
* ifb_ifma_slow_proto
;
230 bondport_ref
* ifb_distributing_array
;
231 int ifb_distributing_count
;
232 int ifb_last_link_event
;
233 int ifb_mode
;/* LACP, STATIC */
242 ReceiveState_none
= 0,
243 ReceiveState_INITIALIZE
= 1,
244 ReceiveState_PORT_DISABLED
= 2,
245 ReceiveState_EXPIRED
= 3,
246 ReceiveState_LACP_DISABLED
= 4,
247 ReceiveState_DEFAULTED
= 5,
248 ReceiveState_CURRENT
= 6,
251 typedef u_char ReceiveState
;
254 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
255 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
256 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
258 typedef u_char SelectedState
;
260 static __inline__
const char *
261 SelectedStateString(SelectedState s
)
263 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
265 if (s
<= SelectedState_STANDBY
) {
273 MuxState_DETACHED
= 1,
274 MuxState_WAITING
= 2,
275 MuxState_ATTACHED
= 3,
276 MuxState_COLLECTING_DISTRIBUTING
= 4,
279 typedef u_char MuxState
;
281 #define PORT_CONTROL_FLAGS_IN_LIST 0x01
282 #define PORT_CONTROL_FLAGS_PROTO_ATTACHED 0x02
283 #define PORT_CONTROL_FLAGS_FILTER_ATTACHED 0x04
284 #define PORT_CONTROL_FLAGS_LLADDR_SET 0x08
285 #define PORT_CONTROL_FLAGS_MTU_SET 0x10
286 #define PORT_CONTROL_FLAGS_PROMISCUOUS_SET 0x20
289 TAILQ_ENTRY(bondport_s
) po_port_list
;
291 struct multicast_list po_multicast
;
292 struct ifnet
* po_ifp
;
293 struct ether_addr po_saved_addr
;
295 char po_name
[IFNAMSIZ
];
296 struct ifdevmtu po_devmtu
;
297 uint32_t po_control_flags
;
298 interface_filter_t po_filter
;
301 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
302 devtimer_ref po_current_while_timer
;
303 devtimer_ref po_periodic_timer
;
304 devtimer_ref po_wait_while_timer
;
305 devtimer_ref po_transmit_timer
;
306 partner_state po_partner_state
;
307 lacp_port_priority po_priority
;
308 lacp_actor_partner_state po_actor_state
;
310 u_char po_periodic_interval
;
311 u_char po_n_transmit
;
312 ReceiveState po_receive_state
;
313 MuxState po_mux_state
;
314 SelectedState po_selected
;
315 int32_t po_last_transmit_secs
;
316 struct media_info po_media_info
;
317 uint64_t po_force_link_event_time
;
321 #define IFBF_PROMISC 0x1 /* promiscuous mode */
322 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
323 #define IFBF_LLADDR 0x4 /* specific link address requested */
324 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
326 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
329 static __inline__
int
330 ifbond_flags_if_detaching(ifbond_ref ifb
)
332 return (ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0;
335 static __inline__
void
336 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
338 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
342 static __inline__
int
343 ifbond_flags_lladdr(ifbond_ref ifb
)
345 return (ifb
->ifb_flags
& IFBF_LLADDR
) != 0;
348 static __inline__
int
349 ifbond_flags_change_in_progress(ifbond_ref ifb
)
351 return (ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0;
354 static __inline__
void
355 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
357 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
361 static __inline__
void
362 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
364 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
369 * bondport_ref->po_flags bits
371 #define BONDPORT_FLAGS_NTT 0x01
372 #define BONDPORT_FLAGS_READY 0x02
373 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
374 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
375 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
376 #define BONDPORT_FLAGS_UNUSED2 0x20
377 #define BONDPORT_FLAGS_UNUSED3 0x40
378 #define BONDPORT_FLAGS_UNUSED4 0x80
380 static __inline__
void
381 bondport_flags_set_ntt(bondport_ref p
)
383 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
387 static __inline__
void
388 bondport_flags_clear_ntt(bondport_ref p
)
390 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
394 static __inline__
int
395 bondport_flags_ntt(bondport_ref p
)
397 return (p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0;
400 static __inline__
void
401 bondport_flags_set_ready(bondport_ref p
)
403 p
->po_flags
|= BONDPORT_FLAGS_READY
;
407 static __inline__
void
408 bondport_flags_clear_ready(bondport_ref p
)
410 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
414 static __inline__
int
415 bondport_flags_ready(bondport_ref p
)
417 return (p
->po_flags
& BONDPORT_FLAGS_READY
) != 0;
420 static __inline__
void
421 bondport_flags_set_selected_changed(bondport_ref p
)
423 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
427 static __inline__
void
428 bondport_flags_clear_selected_changed(bondport_ref p
)
430 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
434 static __inline__
int
435 bondport_flags_selected_changed(bondport_ref p
)
437 return (p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0;
440 static __inline__
void
441 bondport_flags_set_mux_attached(bondport_ref p
)
443 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
447 static __inline__
void
448 bondport_flags_clear_mux_attached(bondport_ref p
)
450 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
454 static __inline__
int
455 bondport_flags_mux_attached(bondport_ref p
)
457 return (p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0;
460 static __inline__
void
461 bondport_flags_set_distributing(bondport_ref p
)
463 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
467 static __inline__
void
468 bondport_flags_clear_distributing(bondport_ref p
)
470 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
474 static __inline__
int
475 bondport_flags_distributing(bondport_ref p
)
477 return (p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0;
480 typedef struct bond_globals_s
{
481 struct ifbond_list ifbond_list
;
483 lacp_system_priority system_priority
;
484 } * bond_globals_ref
;
486 static bond_globals_ref g_bond
;
489 ** packet_buffer routines
490 ** - thin wrapper for mbuf
493 typedef struct mbuf
* packet_buffer_ref
;
495 static packet_buffer_ref
496 packet_buffer_allocate(int length
)
501 /* leave room for ethernet header */
502 size
= length
+ sizeof(struct ether_header
);
503 if (size
> (int)MHLEN
) {
504 if (size
> (int)MCLBYTES
) {
505 printf("bond: packet_buffer_allocate size %d > max %u\n",
509 m
= m_getcl(M_WAITOK
, MT_DATA
, M_PKTHDR
);
511 m
= m_gethdr(M_WAITOK
, MT_DATA
);
517 m
->m_pkthdr
.len
= size
;
522 packet_buffer_byteptr(packet_buffer_ref buf
)
524 return buf
->m_data
+ sizeof(struct ether_header
);
532 LAEventSelectedChange
,
541 bondport_receive_machine(bondport_ref p
, LAEvent event
,
544 ** Periodic Transmission machine
547 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
553 #define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
556 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
563 bondport_mux_machine(bondport_ref p
, LAEvent event
,
570 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
573 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
576 ifbond_all_ports_ready(ifbond_ref bond
);
579 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
582 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
585 ifbond_selection(ifbond_ref bond
);
588 bond_handle_event(struct ifnet
* port_ifp
, int event_code
);
595 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
598 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
601 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
602 int active
, int short_timeout
, int * error
);
604 bondport_start(bondport_ref p
);
607 bondport_free(bondport_ref p
);
610 bondport_aggregatable(bondport_ref p
);
613 bondport_remove_from_LAG(bondport_ref p
);
616 bondport_set_selected(bondport_ref p
, SelectedState s
);
619 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
622 bondport_link_status_changed(bondport_ref p
);
625 bondport_enable_distributing(bondport_ref p
);
628 bondport_disable_distributing(bondport_ref p
);
630 static __inline__
int
631 bondport_collecting(bondport_ref p
)
633 if (p
->po_bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
634 return lacp_actor_partner_state_collecting(p
->po_actor_state
);
640 ** bond interface/dlil specific routines
642 static int bond_clone_create(struct if_clone
*, u_int32_t
, void *);
643 static int bond_clone_destroy(struct ifnet
*);
644 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
645 static int bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * addr
);
646 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
647 bpf_packet_func func
);
648 static int bond_attach_protocol(struct ifnet
*ifp
);
649 static int bond_detach_protocol(struct ifnet
*ifp
);
650 static errno_t
bond_iff_input(void *cookie
, ifnet_t ifp
,
651 protocol_family_t protocol
, mbuf_t
*data
, char **frame_ptr
);
652 static int bond_attach_filter(struct ifnet
*ifp
, interface_filter_t
* filter_p
);
653 static int bond_setmulti(struct ifnet
*ifp
);
654 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
655 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
656 static void bond_if_free(struct ifnet
* ifp
);
657 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
659 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
668 siocsifmtu(struct ifnet
* ifp
, int mtu
)
672 bzero(&ifr
, sizeof(ifr
));
674 return ifnet_ioctl(ifp
, 0, SIOCSIFMTU
, &ifr
);
678 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
683 bzero(&ifr
, sizeof(ifr
));
684 error
= ifnet_ioctl(ifp
, 0, SIOCGIFDEVMTU
, &ifr
);
686 *ifdm_p
= ifr
.ifr_devmtu
;
691 static __inline__
void
692 ether_addr_copy(void * dest
, const void * source
)
694 bcopy(source
, dest
, ETHER_ADDR_LEN
);
698 static __inline__
void
699 ifbond_retain(ifbond_ref ifb
)
701 os_ref_retain(&ifb
->ifb_retain_count
);
704 static __inline__
void
705 ifbond_release(ifbond_ref ifb
)
707 if (os_ref_release(&ifb
->ifb_retain_count
) != 0) {
712 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
714 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
716 printf("ifbond_release(%s) removing multicast\n",
719 (void) if_delmulti_anon(ifb
->ifb_ifma_slow_proto
->ifma_ifp
,
720 ifb
->ifb_ifma_slow_proto
->ifma_addr
);
721 IFMA_REMREF(ifb
->ifb_ifma_slow_proto
);
723 if (ifb
->ifb_distributing_array
!= NULL
) {
724 FREE(ifb
->ifb_distributing_array
, M_BOND
);
726 if_clone_softc_deallocate(&bond_cloner
, ifb
);
730 * Function: ifbond_wait
732 * Allows a single thread to gain exclusive access to the ifbond
733 * data structure. Some operations take a long time to complete,
734 * and some have side-effects that we can't predict. Holding the
735 * bond_lock() across such operations is not possible.
738 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
739 * complete. Simply holding the bond_lock() would freeze all other
740 * data structure accesses during that time.
741 * 2) When we attach our protocol to the interface, a dlil event is
742 * generated and invokes our bond_event() function. bond_event()
743 * needs to take the bond_lock(), but we're already holding it, so
744 * we're deadlocked against ourselves.
746 * Before calling, you must be holding the bond_lock and have taken
747 * a reference on the ifbond_ref.
750 ifbond_wait(ifbond_ref ifb
, const char * msg
)
754 /* other add/remove in progress */
755 while (ifbond_flags_change_in_progress(ifb
)) {
757 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
760 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
762 /* prevent other bond list remove/add from taking place */
763 ifbond_flags_set_change_in_progress(ifb
);
764 if (if_bond_debug
&& waited
) {
765 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
771 * Function: ifbond_signal
773 * Allows the thread that previously invoked ifbond_wait() to
774 * give up exclusive access to the ifbond data structure, and wake up
775 * any other threads waiting to access
777 * Before calling, you must be holding the bond_lock and have taken
778 * a reference on the ifbond_ref.
781 ifbond_signal(ifbond_ref ifb
, const char * msg
)
783 ifbond_flags_clear_change_in_progress(ifb
);
784 wakeup((caddr_t
)ifb
);
786 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
796 link_speed(int active
)
798 switch (IFM_SUBTYPE(active
)) {
819 case IFM_1000_CX_SGMII
:
825 /* assume that new defined types are going to be at least 10GigE */
864 static __inline__
int
865 media_active(const struct media_info
* mi
)
867 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
870 return (mi
->mi_status
& IFM_ACTIVE
) != 0;
873 static __inline__
int
874 media_full_duplex(const struct media_info
* mi
)
876 return (mi
->mi_active
& IFM_FDX
) != 0;
879 static __inline__
int
880 media_type_unknown(const struct media_info
* mi
)
884 switch (IFM_SUBTYPE(mi
->mi_active
)) {
897 static __inline__
int
898 media_ok(const struct media_info
* mi
)
900 return media_full_duplex(mi
) || media_type_unknown(mi
);
903 static __inline__
int
904 media_speed(const struct media_info
* mi
)
906 return link_speed(mi
->mi_active
);
909 static struct media_info
910 interface_media_info(struct ifnet
* ifp
)
912 struct ifmediareq ifmr
;
913 struct media_info mi
;
915 bzero(&mi
, sizeof(mi
));
916 bzero(&ifmr
, sizeof(ifmr
));
917 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
918 if (ifmr
.ifm_count
!= 0) {
919 mi
.mi_status
= ifmr
.ifm_status
;
920 mi
.mi_active
= ifmr
.ifm_active
;
927 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
932 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
933 * currently expects it that way
935 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
936 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
937 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
938 return ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
);
944 static bond_globals_ref
945 bond_globals_create(lacp_system_priority sys_pri
,
950 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
| M_ZERO
);
954 TAILQ_INIT(&b
->ifbond_list
);
956 b
->system_priority
= sys_pri
;
961 bond_globals_init(void)
967 bond_assert_lock_not_held();
969 if (g_bond
!= NULL
) {
974 * use en0's ethernet address as the system identifier, and if it's not
975 * there, use en1 .. en3
978 for (i
= 0; i
< 4; i
++) {
979 char ifname
[IFNAMSIZ
+ 1];
980 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
981 ifp
= ifunit(ifname
);
988 b
= bond_globals_create(0x8000, (lacp_system_ref
)IF_LLADDR(ifp
));
991 if (g_bond
!= NULL
) {
1008 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
1009 const struct ether_header
* eh_p
,
1010 u_int16_t vlan_tag
, bpf_packet_func func
)
1012 struct ether_vlan_header
* vlh_p
;
1015 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
1019 /* populate a new mbuf containing the vlan ethernet header */
1020 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
1021 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
1022 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
1023 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1024 vlh_p
->evl_tag
= htons(vlan_tag
);
1025 vlh_p
->evl_proto
= eh_p
->ether_type
;
1028 vl_m
->m_next
= NULL
;
1033 static __inline__
void
1034 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
1035 bpf_packet_func func
)
1038 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1039 const struct ether_header
* eh_p
;
1040 eh_p
= mtod(m
, const struct ether_header
*);
1041 m
->m_data
+= ETHER_HDR_LEN
;
1042 m
->m_len
-= ETHER_HDR_LEN
;
1043 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1044 m
->m_data
-= ETHER_HDR_LEN
;
1045 m
->m_len
+= ETHER_HDR_LEN
;
1053 static __inline__
void
1054 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
1055 bpf_packet_func func
)
1058 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1059 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1061 /* restore the header */
1062 m
->m_data
-= ETHER_HDR_LEN
;
1063 m
->m_len
+= ETHER_HDR_LEN
;
1065 m
->m_data
+= ETHER_HDR_LEN
;
1066 m
->m_len
-= ETHER_HDR_LEN
;
1073 * Function: bond_setmulti
1075 * Enable multicast reception on "our" interface by enabling multicasts on
1076 * each of the member ports.
1079 bond_setmulti(struct ifnet
* ifp
)
1087 ifb
= ifnet_softc(ifp
);
1088 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1089 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1094 ifbond_wait(ifb
, "bond_setmulti");
1096 if (ifbond_flags_if_detaching(ifb
)) {
1097 /* someone destroyed the bond while we were waiting */
1103 /* ifbond_wait() let's us safely walk the list without holding the lock */
1104 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1105 struct ifnet
* port_ifp
= p
->po_ifp
;
1107 error
= multicast_list_program(&p
->po_multicast
,
1110 printf("bond_setmulti(%s): "
1111 "multicast_list_program(%s%d) failed, %d\n",
1112 ifb
->ifb_name
, ifnet_name(port_ifp
),
1113 ifnet_unit(port_ifp
), error
);
1119 ifbond_signal(ifb
, __func__
);
1121 ifbond_release(ifb
);
1126 bond_clone_attach(void)
1130 if ((error
= if_clone_attach(&bond_cloner
)) != 0) {
1138 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1141 struct ifmultiaddr
* ifma
= NULL
;
1142 struct sockaddr_dl sdl
;
1144 bond_assert_lock_not_held();
1146 bzero(&sdl
, sizeof(sdl
));
1147 sdl
.sdl_len
= sizeof(sdl
);
1148 sdl
.sdl_family
= AF_LINK
;
1149 sdl
.sdl_type
= IFT_ETHER
;
1151 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1152 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1153 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1155 ifb
->ifb_ifma_slow_proto
= ifma
;
1161 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1166 struct ifnet_init_eparams bond_init
;
1168 error
= bond_globals_init();
1173 ifb
= if_clone_softc_allocate(&bond_cloner
);
1178 os_ref_init(&ifb
->ifb_retain_count
, NULL
);
1179 TAILQ_INIT(&ifb
->ifb_port_list
);
1180 TAILQ_INIT(&ifb
->ifb_lag_list
);
1181 ifb
->ifb_key
= unit
+ 1;
1183 /* use the interface name as the unique id for ifp recycle */
1184 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1185 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1186 ifbond_release(ifb
);
1190 bzero(&bond_init
, sizeof(bond_init
));
1191 bond_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1192 bond_init
.len
= sizeof(bond_init
);
1193 bond_init
.flags
= IFNET_INIT_LEGACY
;
1194 bond_init
.uniqueid
= ifb
->ifb_name
;
1195 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1196 bond_init
.name
= ifc
->ifc_name
;
1197 bond_init
.unit
= unit
;
1198 bond_init
.family
= IFNET_FAMILY_BOND
;
1199 bond_init
.type
= IFT_IEEE8023ADLAG
;
1200 bond_init
.output
= bond_output
;
1201 bond_init
.demux
= ether_demux
;
1202 bond_init
.add_proto
= ether_add_proto
;
1203 bond_init
.del_proto
= ether_del_proto
;
1204 bond_init
.check_multi
= ether_check_multi
;
1205 bond_init
.framer_extended
= ether_frameout_extended
;
1206 bond_init
.ioctl
= bond_ioctl
;
1207 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1208 bond_init
.detach
= bond_if_free
;
1209 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1210 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1211 bond_init
.softc
= ifb
;
1212 error
= ifnet_allocate_extended(&bond_init
, &ifp
);
1215 ifbond_release(ifb
);
1220 ifnet_set_offload(ifp
, 0);
1221 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1222 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1223 ifnet_set_mtu(ifp
, ETHERMTU
);
1225 error
= ifnet_attach(ifp
, NULL
);
1228 ifbond_release(ifb
);
1231 error
= ifbond_add_slow_proto_multicast(ifb
);
1233 printf("bond_clone_create(%s): "
1234 "failed to add slow_proto multicast, %d\n",
1235 ifb
->ifb_name
, error
);
1238 /* attach as ethernet */
1239 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1242 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1249 bond_remove_all_interfaces(ifbond_ref ifb
)
1253 bond_assert_lock_held();
1256 * do this in reverse order to avoid re-programming the mac address
1257 * as each head interface is removed
1259 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1260 bond_remove_interface(ifb
, p
->po_ifp
);
1266 bond_remove(ifbond_ref ifb
)
1268 bond_assert_lock_held();
1269 ifbond_flags_set_if_detaching(ifb
);
1270 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1271 bond_remove_all_interfaces(ifb
);
1276 bond_if_detach(struct ifnet
* ifp
)
1280 error
= ifnet_detach(ifp
);
1282 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1283 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1290 bond_clone_destroy(struct ifnet
* ifp
)
1295 ifb
= ifnet_softc(ifp
);
1296 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1300 if (ifbond_flags_if_detaching(ifb
)) {
1306 bond_if_detach(ifp
);
1311 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1316 ifb
= ifnet_softc(ifp
);
1317 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1322 case BPF_TAP_DISABLE
:
1323 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1327 ifb
->ifb_bpf_input
= func
;
1330 case BPF_TAP_OUTPUT
:
1331 ifb
->ifb_bpf_output
= func
;
1334 case BPF_TAP_INPUT_OUTPUT
:
1335 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1345 ether_header_hash(struct ether_header
* eh_p
)
1349 /* get 32-bits from destination ether and ether type */
1350 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1352 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1356 static struct mbuf
*
1357 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1362 while (*offset
>= len
) {
1373 #if BYTE_ORDER == BIG_ENDIAN
1374 static __inline__
uint32_t
1375 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1377 return ((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1378 | ((uint32_t)c2
<< 8) | (uint32_t)c3
;
1380 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1381 static __inline__
uint32_t
1382 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1384 return ((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1385 | ((uint32_t)c1
<< 8) | (uint32_t)c0
;
1387 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1390 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1392 struct mbuf
* current
;
1393 u_char
* current_data
;
1398 current
= S_mbuf_skip_to_offset(m
, &offset
);
1399 if (current
== NULL
) {
1402 current_data
= mtod(current
, u_char
*) + offset
;
1403 space_current
= current
->m_len
- offset
;
1404 if (space_current
>= (int)sizeof(uint32_t)) {
1405 *val
= *((uint32_t *)current_data
);
1408 next
= current
->m_next
;
1409 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1412 next_data
= mtod(next
, u_char
*);
1413 switch (space_current
) {
1415 *val
= make_uint32(current_data
[0], next_data
[0],
1416 next_data
[1], next_data
[2]);
1419 *val
= make_uint32(current_data
[0], current_data
[1],
1420 next_data
[0], next_data
[1]);
1423 *val
= make_uint32(current_data
[0], current_data
[1],
1424 current_data
[2], next_data
[0]);
1430 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1431 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1434 ip_header_hash(struct mbuf
* m
)
1437 struct in_addr ip_dst
;
1438 struct in_addr ip_src
;
1441 struct mbuf
* orig_m
= m
;
1443 /* find the IP protocol field relative to the start of the packet */
1444 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1445 m
= S_mbuf_skip_to_offset(m
, &offset
);
1446 if (m
== NULL
|| m
->m_len
< 1) {
1449 data
= mtod(m
, u_char
*) + offset
;
1452 /* find the IP src relative to the IP protocol */
1453 if ((m
->m_len
- offset
)
1454 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1455 /* this should be the normal case */
1456 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1457 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1459 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1460 (uint32_t *)&ip_src
.s_addr
)) {
1463 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1464 (uint32_t *)&ip_dst
.s_addr
)) {
1468 return ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
);
1471 return ether_header_hash(mtod(orig_m
, struct ether_header
*));
1474 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1476 ipv6_header_hash(struct mbuf
* m
)
1481 struct mbuf
* orig_m
= m
;
1485 /* find the IP protocol field relative to the start of the packet */
1486 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1487 m
= S_mbuf_skip_to_offset(m
, &offset
);
1489 goto bad_ipv6_packet
;
1491 data
= mtod(m
, u_char
*) + offset
;
1493 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1494 /* this should be the normal case */
1495 for (i
= 0, scan
= (uint32_t *)data
;
1496 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1501 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1503 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1504 (uint32_t *)&tmp
)) {
1505 goto bad_ipv6_packet
;
1513 return ether_header_hash(mtod(orig_m
, struct ether_header
*));
1517 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1519 bpf_packet_func bpf_func
;
1522 struct ifnet
* port_ifp
= NULL
;
1524 struct flowadv adv
= { .code
= FADV_SUCCESS
};
1529 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1533 if (m
->m_pkthdr
.pkt_flowid
!= 0) {
1534 h
= m
->m_pkthdr
.pkt_flowid
;
1536 struct ether_header
* eh_p
;
1538 eh_p
= mtod(m
, struct ether_header
*);
1539 switch (ntohs(eh_p
->ether_type
)) {
1541 h
= ip_header_hash(m
);
1543 case ETHERTYPE_IPV6
:
1544 h
= ipv6_header_hash(m
);
1547 h
= ether_header_hash(eh_p
);
1552 ifb
= ifnet_softc(ifp
);
1553 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1554 || ifb
->ifb_distributing_count
== 0) {
1557 h
%= ifb
->ifb_distributing_count
;
1558 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1559 bpf_func
= ifb
->ifb_bpf_output
;
1562 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1563 (void)ifnet_stat_increment_out(ifp
, 1,
1564 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1567 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1569 bond_bpf_output(ifp
, m
, bpf_func
);
1571 err
= dlil_output(port_ifp
, PF_BOND
, m
, NULL
, NULL
, 1, &adv
);
1574 if (adv
.code
== FADV_FLOW_CONTROLLED
) {
1576 } else if (adv
.code
== FADV_SUSPENDED
) {
1590 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1593 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1594 if (p
->po_ifp
== port_ifp
) {
1602 bond_lookup_port(struct ifnet
* port_ifp
)
1607 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1608 port
= ifbond_lookup_port(ifb
, port_ifp
);
1617 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1619 struct ifnet
* bond_ifp
= NULL
;
1622 bool need_link_update
= false;
1626 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1629 p
= bond_lookup_port(port_ifp
);
1633 if (p
->po_enabled
== 0) {
1637 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1641 * Work-around for rdar://problem/51372042
1642 * Sometimes, the link comes up but the driver doesn't report the
1643 * negotiated medium at that time. When we receive an LACPDU packet,
1644 * and the medium is unknown, force a link status check. Don't force
1645 * the link status check more often than _FORCE_LINK_EVENT_INTERVAL
1648 #define _FORCE_LINK_EVENT_INTERVAL 1
1649 if (media_type_unknown(&p
->po_media_info
)) {
1650 uint64_t now
= net_uptime();
1652 if ((now
- p
->po_force_link_event_time
) >=
1653 _FORCE_LINK_EVENT_INTERVAL
) {
1654 need_link_update
= true;
1655 p
->po_force_link_event_time
= now
;
1658 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1659 if (ifbond_selection(ifb
)) {
1660 event_code
= (ifb
->ifb_active_lag
== NULL
)
1663 /* XXX need to take a reference on bond_ifp */
1664 bond_ifp
= ifb
->ifb_ifp
;
1665 ifb
->ifb_last_link_event
= event_code
;
1667 event_code
= (ifb
->ifb_active_lag
== NULL
)
1670 if (event_code
!= ifb
->ifb_last_link_event
) {
1671 if (if_bond_debug
) {
1672 timestamp_printf("%s: (receive) generating LINK event\n",
1675 bond_ifp
= ifb
->ifb_ifp
;
1676 ifb
->ifb_last_link_event
= event_code
;
1682 if (bond_ifp
!= NULL
) {
1683 interface_link_event(bond_ifp
, event_code
);
1686 if (need_link_update
) {
1687 if (if_bond_debug
!= 0) {
1688 printf("bond: simulating link status changed event");
1690 bond_handle_event(port_ifp
, KEV_DL_LINK_ON
);
1696 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1698 la_marker_pdu_ref marker_p
;
1701 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1702 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1706 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1710 p
= bond_lookup_port(port_ifp
);
1711 if (p
== NULL
|| p
->po_enabled
== 0
1712 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1716 /* echo back the same packet as a marker response */
1717 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1718 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1728 bond_input(ifnet_t port_ifp
, mbuf_t m
, char *frame_header
)
1730 bpf_packet_func bpf_func
;
1731 const struct ether_header
* eh_p
;
1736 eh_p
= (const struct ether_header
*)frame_header
;
1737 if ((m
->m_flags
& M_MCAST
) != 0
1738 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1739 sizeof(eh_p
->ether_dhost
)) == 0
1740 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1741 u_char subtype
= *mtod(m
, u_char
*);
1743 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1744 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1749 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1750 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1755 bond_receive_lacpdu(m
, port_ifp
);
1757 } else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1760 /* restore the ethernet header pointer in the mbuf */
1761 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1762 m
->m_data
-= ETHER_HDR_LEN
;
1763 m
->m_len
+= ETHER_HDR_LEN
;
1764 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1765 if (m
->m_pkthdr
.len
< min_size
) {
1770 if (m
->m_len
< min_size
) {
1771 m
= m_pullup(m
, min_size
);
1776 /* send to marker responder */
1777 bond_receive_la_marker_pdu(m
, port_ifp
);
1779 } else if (subtype
== 0
1780 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1781 /* invalid subtype, discard the frame */
1787 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1790 p
= bond_lookup_port(port_ifp
);
1791 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1797 bpf_func
= ifb
->ifb_bpf_input
;
1801 * Need to clear the promiscous flags otherwise it will be
1802 * dropped by DLIL after processing filters
1804 if ((mbuf_flags(m
) & MBUF_PROMISC
)) {
1805 mbuf_setflags_mask(m
, 0, MBUF_PROMISC
);
1808 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1809 (void)ifnet_stat_increment_in(ifp
, 1,
1810 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1811 + ETHER_VLAN_ENCAP_LEN
), 0);
1813 (void)ifnet_stat_increment_in(ifp
, 1,
1814 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1817 /* make the packet appear as if it arrived on the bonded interface */
1818 m
->m_pkthdr
.rcvif
= ifp
;
1819 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1820 m
->m_pkthdr
.pkt_hdr
= frame_header
;
1821 dlil_input_packet_list(ifp
, m
);
1831 bond_iff_input(void *cookie
, ifnet_t port_ifp
, protocol_family_t protocol
,
1832 mbuf_t
*data
, char **frame_header_ptr
)
1834 #pragma unused(cookie)
1835 #pragma unused(protocol)
1837 char * frame_header
= *frame_header_ptr
;
1839 bond_input(port_ifp
, m
, frame_header
);
1843 static __inline__
const char *
1844 bondport_get_name(bondport_ref p
)
1849 static __inline__
int
1850 bondport_get_index(bondport_ref p
)
1852 return ifnet_index(p
->po_ifp
);
1856 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1858 struct ether_header
* eh_p
;
1861 /* packet_buffer_allocate leaves room for ethernet header */
1862 eh_p
= mtod(buf
, struct ether_header
*);
1863 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1864 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1865 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1866 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1868 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1869 bondport_get_name(p
), error
);
1875 bondport_timer_process_func(devtimer_ref timer
,
1876 devtimer_process_func_event event
)
1881 case devtimer_process_func_event_lock
:
1883 devtimer_retain(timer
);
1885 case devtimer_process_func_event_unlock
:
1886 if (devtimer_valid(timer
)) {
1887 /* as long as the devtimer is valid, we can look at arg0 */
1889 struct ifnet
* bond_ifp
= NULL
;
1891 p
= (bondport_ref
)devtimer_arg0(timer
);
1892 if (ifbond_selection(p
->po_bond
)) {
1893 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1896 /* XXX need to take a reference on bond_ifp */
1897 bond_ifp
= p
->po_bond
->ifb_ifp
;
1898 p
->po_bond
->ifb_last_link_event
= event_code
;
1900 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1903 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1904 if (if_bond_debug
) {
1905 timestamp_printf("%s: (timer) generating LINK event\n",
1906 p
->po_bond
->ifb_name
);
1908 bond_ifp
= p
->po_bond
->ifb_ifp
;
1909 p
->po_bond
->ifb_last_link_event
= event_code
;
1912 devtimer_release(timer
);
1914 if (bond_ifp
!= NULL
) {
1915 interface_link_event(bond_ifp
, event_code
);
1918 /* timer is going away */
1919 devtimer_release(timer
);
1929 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1930 int active
, int short_timeout
, int * ret_error
)
1933 bondport_ref p
= NULL
;
1934 lacp_actor_partner_state s
;
1937 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
| M_ZERO
);
1939 *ret_error
= ENOMEM
;
1942 multicast_list_init(&p
->po_multicast
);
1943 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1944 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1945 >= sizeof(p
->po_name
)) {
1946 printf("if_bond: name too large\n");
1947 *ret_error
= EINVAL
;
1950 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1952 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1953 bondport_get_name(p
), error
);
1956 /* remember the current interface MTU so it can be restored */
1957 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1958 p
->po_ifp
= port_ifp
;
1959 p
->po_media_info
= interface_media_info(port_ifp
);
1960 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1961 if (p
->po_current_while_timer
== NULL
) {
1962 *ret_error
= ENOMEM
;
1965 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1966 if (p
->po_periodic_timer
== NULL
) {
1967 *ret_error
= ENOMEM
;
1970 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1971 if (p
->po_wait_while_timer
== NULL
) {
1972 *ret_error
= ENOMEM
;
1975 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1976 if (p
->po_transmit_timer
== NULL
) {
1977 *ret_error
= ENOMEM
;
1980 p
->po_receive_state
= ReceiveState_none
;
1981 p
->po_mux_state
= MuxState_none
;
1982 p
->po_priority
= priority
;
1984 s
= lacp_actor_partner_state_set_aggregatable(s
);
1985 if (short_timeout
) {
1986 s
= lacp_actor_partner_state_set_short_timeout(s
);
1989 s
= lacp_actor_partner_state_set_active_lacp(s
);
1991 p
->po_actor_state
= s
;
2000 bondport_start(bondport_ref p
)
2002 bondport_receive_machine(p
, LAEventStart
, NULL
);
2003 bondport_mux_machine(p
, LAEventStart
, NULL
);
2004 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
2005 bondport_transmit_machine(p
, LAEventStart
, NULL
);
2010 * Function: bondport_invalidate_timers
2012 * Invalidate all of the timers for the bondport.
2015 bondport_invalidate_timers(bondport_ref p
)
2017 devtimer_invalidate(p
->po_current_while_timer
);
2018 devtimer_invalidate(p
->po_periodic_timer
);
2019 devtimer_invalidate(p
->po_wait_while_timer
);
2020 devtimer_invalidate(p
->po_transmit_timer
);
2024 * Function: bondport_cancel_timers
2026 * Cancel all of the timers for the bondport.
2029 bondport_cancel_timers(bondport_ref p
)
2031 devtimer_cancel(p
->po_current_while_timer
);
2032 devtimer_cancel(p
->po_periodic_timer
);
2033 devtimer_cancel(p
->po_wait_while_timer
);
2034 devtimer_cancel(p
->po_transmit_timer
);
2038 bondport_free(bondport_ref p
)
2040 multicast_list_remove(&p
->po_multicast
);
2041 devtimer_release(p
->po_current_while_timer
);
2042 devtimer_release(p
->po_periodic_timer
);
2043 devtimer_release(p
->po_wait_while_timer
);
2044 devtimer_release(p
->po_transmit_timer
);
2049 static __inline__
int
2050 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
2052 return ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2053 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2057 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
2059 uint32_t control_flags
= 0;
2063 interface_filter_t filter
= NULL
;
2066 bondport_ref
* new_array
= NULL
;
2067 bondport_ref
* old_array
= NULL
;
2070 if (IFNET_IS_INTCOPROC(port_ifp
)) {
2074 /* pre-allocate space for new port */
2075 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
2080 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2081 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2084 return ifb
== NULL
? EOPNOTSUPP
: EBUSY
;
2087 /* make sure this interface can handle our current MTU */
2088 devmtu
= bond_device_mtu(ifp
, ifb
);
2090 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
2092 printf("if_bond: interface %s doesn't support mtu %d",
2093 bondport_get_name(p
), devmtu
);
2098 /* make sure ifb doesn't get de-allocated while we wait */
2101 /* wait for other add or remove to complete */
2102 ifbond_wait(ifb
, __func__
);
2104 if (ifbond_flags_if_detaching(ifb
)) {
2105 /* someone destroyed the bond while we were waiting */
2109 if (bond_lookup_port(port_ifp
) != NULL
) {
2110 /* port is already part of a bond */
2114 ifnet_lock_exclusive(port_ifp
);
2115 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2116 /* interface already has VLAN's, or is part of bond */
2117 ifnet_lock_done(port_ifp
);
2122 /* mark the interface busy */
2123 /* can't use ifnet_set_eflags because that takes the lock */
2124 port_ifp
->if_eflags
|= IFEF_BOND
;
2125 ifnet_lock_done(port_ifp
);
2127 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2128 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2129 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2130 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2134 ifnet_offload_t ifp_offload
;
2135 ifnet_offload_t port_ifp_offload
;
2137 ifp_offload
= ifnet_offload(ifp
);
2138 port_ifp_offload
= ifnet_offload(port_ifp
);
2139 if (ifp_offload
!= port_ifp_offload
) {
2140 ifnet_offload_t offload
;
2142 offload
= ifp_offload
& port_ifp_offload
;
2143 printf("%s(%s, %s) "
2144 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2146 ifb
->ifb_name
, bondport_get_name(p
),
2147 ifp_offload
, port_ifp_offload
, offload
);
2150 * if the bond has VLAN's, we can't simply change the hwassist
2151 * field behind its back: this needs work
2153 ifnet_set_offload(ifp
, offload
);
2158 /* remember the port's ethernet address so it can be restored */
2159 ether_addr_copy(&p
->po_saved_addr
, IF_LLADDR(port_ifp
));
2161 /* add it to the list of ports */
2162 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2163 ifb
->ifb_port_count
++;
2168 /* first port added to bond determines bond's ethernet address */
2170 ifnet_set_lladdr_and_type(ifp
, IF_LLADDR(port_ifp
), ETHER_ADDR_LEN
,
2174 control_flags
|= PORT_CONTROL_FLAGS_IN_LIST
;
2176 /* allocate a larger distributing array */
2177 new_array
= (bondport_ref
*)
2178 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2179 if (new_array
== NULL
) {
2184 /* attach our BOND "protocol" to the interface */
2185 error
= bond_attach_protocol(port_ifp
);
2189 control_flags
|= PORT_CONTROL_FLAGS_PROTO_ATTACHED
;
2191 /* attach our BOND interface filter */
2192 error
= bond_attach_filter(port_ifp
, &filter
);
2196 control_flags
|= PORT_CONTROL_FLAGS_FILTER_ATTACHED
;
2198 /* set the interface MTU */
2199 devmtu
= bond_device_mtu(ifp
, ifb
);
2200 error
= siocsifmtu(port_ifp
, devmtu
);
2202 printf("%s(%s, %s):"
2203 " SIOCSIFMTU %d failed %d\n",
2205 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2208 control_flags
|= PORT_CONTROL_FLAGS_MTU_SET
;
2210 /* program the port with our multicast addresses */
2211 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2213 printf("%s(%s, %s): multicast_list_program failed %d\n",
2215 ifb
->ifb_name
, bondport_get_name(p
), error
);
2219 /* mark the interface up */
2220 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2222 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2224 printf("%s(%s, %s): SIOCSIFFLAGS failed %d\n",
2226 ifb
->ifb_name
, bondport_get_name(p
), error
);
2230 /* re-program the port's ethernet address */
2231 error
= if_siflladdr(port_ifp
,
2232 (const struct ether_addr
*)IF_LLADDR(ifp
));
2234 if (memcmp(IF_LLADDR(ifp
), IF_LLADDR(port_ifp
), ETHER_ADDR_LEN
)
2236 /* it lied, it really doesn't support setting lladdr */
2241 /* port doesn't support setting the link address */
2242 printf("%s(%s, %s): if_siflladdr failed %d\n",
2244 ifb
->ifb_name
, bondport_get_name(p
), error
);
2245 error
= ifnet_set_promiscuous(port_ifp
, 1);
2247 /* port doesn't support setting promiscuous mode */
2248 printf("%s(%s, %s): set promiscuous failed %d\n",
2250 ifb
->ifb_name
, bondport_get_name(p
), error
);
2253 control_flags
|= PORT_CONTROL_FLAGS_PROMISCUOUS_SET
;
2255 control_flags
|= PORT_CONTROL_FLAGS_LLADDR_SET
;
2260 /* no failures past this point */
2262 p
->po_control_flags
= control_flags
;
2264 /* copy the contents of the existing distributing array */
2265 if (ifb
->ifb_distributing_count
) {
2266 bcopy(ifb
->ifb_distributing_array
, new_array
,
2267 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2269 old_array
= ifb
->ifb_distributing_array
;
2270 ifb
->ifb_distributing_array
= new_array
;
2272 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2275 /* check if we need to generate a link status event */
2276 if (ifbond_selection(ifb
)) {
2277 event_code
= (ifb
->ifb_active_lag
== NULL
)
2280 ifb
->ifb_last_link_event
= event_code
;
2283 /* are we adding the first distributing interface? */
2284 if (media_active(&p
->po_media_info
)) {
2285 if (ifb
->ifb_distributing_count
== 0) {
2286 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2288 bondport_enable_distributing(p
);
2290 bondport_disable_distributing(p
);
2293 p
->po_filter
= filter
;
2295 /* clear the busy state, and wakeup anyone waiting */
2296 ifbond_signal(ifb
, __func__
);
2298 if (event_code
!= 0) {
2299 interface_link_event(ifp
, event_code
);
2301 if (old_array
!= NULL
) {
2302 FREE(old_array
, M_BOND
);
2307 bond_assert_lock_not_held();
2309 /* if this was the first port to be added, clear our address */
2311 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2314 if (new_array
!= NULL
) {
2315 FREE(new_array
, M_BOND
);
2317 if ((control_flags
& PORT_CONTROL_FLAGS_LLADDR_SET
) != 0) {
2320 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2322 printf("%s(%s, %s): if_siflladdr restore failed %d\n",
2324 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2327 if ((control_flags
& PORT_CONTROL_FLAGS_PROMISCUOUS_SET
) != 0) {
2330 error1
= ifnet_set_promiscuous(port_ifp
, 0);
2332 printf("%s(%s, %s): promiscous mode disable failed %d\n",
2334 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2337 if ((control_flags
& PORT_CONTROL_FLAGS_PROTO_ATTACHED
) != 0) {
2338 (void)bond_detach_protocol(port_ifp
);
2340 if ((control_flags
& PORT_CONTROL_FLAGS_FILTER_ATTACHED
) != 0) {
2341 iflt_detach(filter
);
2343 if ((control_flags
& PORT_CONTROL_FLAGS_MTU_SET
) != 0) {
2346 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2348 printf("%s(%s, %s): SIOCSIFMTU %d failed %d\n",
2350 ifb
->ifb_name
, bondport_get_name(p
),
2351 p
->po_devmtu
.ifdm_current
, error1
);
2355 if ((control_flags
& PORT_CONTROL_FLAGS_IN_LIST
) != 0) {
2356 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2357 ifb
->ifb_port_count
--;
2359 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2360 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2361 ifb
->ifb_altmtu
= 0;
2362 ifnet_set_mtu(ifp
, ETHERMTU
);
2363 ifnet_set_offload(ifp
, 0);
2367 ifbond_signal(ifb
, __func__
);
2369 ifbond_release(ifb
);
2375 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2380 bondport_ref head_port
;
2382 interface_filter_t filter
;
2384 int new_link_address
= FALSE
;
2386 lacp_actor_partner_state s
;
2387 int was_distributing
;
2389 bond_assert_lock_held();
2392 ifbond_wait(ifb
, "bond_remove_interface");
2394 p
= ifbond_lookup_port(ifb
, port_ifp
);
2397 /* it got removed by another thread */
2401 /* de-select it and remove it from the lists */
2402 was_distributing
= bondport_flags_distributing(p
);
2403 bondport_disable_distributing(p
);
2404 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2405 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2406 active_lag
= bondport_remove_from_LAG(p
);
2407 /* invalidate timers here while holding the bond_lock */
2408 bondport_invalidate_timers(p
);
2410 /* announce that we're Individual now */
2411 s
= p
->po_actor_state
;
2412 s
= lacp_actor_partner_state_set_individual(s
);
2413 s
= lacp_actor_partner_state_set_not_collecting(s
);
2414 s
= lacp_actor_partner_state_set_not_distributing(s
);
2415 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2416 p
->po_actor_state
= s
;
2417 bondport_flags_set_ntt(p
);
2420 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2421 ifb
->ifb_port_count
--;
2424 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2425 if (head_port
== NULL
) {
2426 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2427 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2430 ifnet_set_offload(ifp
, 0);
2431 ifnet_set_mtu(ifp
, ETHERMTU
);
2432 ifb
->ifb_altmtu
= 0;
2433 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2434 && bcmp(&p
->po_saved_addr
, IF_LLADDR(ifp
),
2435 ETHER_ADDR_LEN
) == 0) {
2436 new_link_address
= TRUE
;
2438 /* check if we need to generate a link status event */
2439 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2440 if (ifbond_selection(ifb
) || active_lag
) {
2441 event_code
= (ifb
->ifb_active_lag
== NULL
)
2444 ifb
->ifb_last_link_event
= event_code
;
2446 bondport_transmit_machine(p
, LAEventStart
,
2447 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2449 /* are we removing the last distributing interface? */
2450 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2451 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2454 filter
= p
->po_filter
;
2458 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2459 } else if (new_link_address
) {
2460 struct ifnet
* scan_ifp
;
2461 bondport_ref scan_port
;
2463 /* ifbond_wait() allows port list traversal without holding the lock */
2465 /* this port gave the bond its ethernet address, switch to new one */
2466 ifnet_set_lladdr_and_type(ifp
,
2467 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2470 /* re-program each port with the new link address */
2471 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2472 scan_ifp
= scan_port
->po_ifp
;
2474 if ((scan_port
->po_control_flags
&
2475 PORT_CONTROL_FLAGS_LLADDR_SET
) == 0) {
2476 /* port doesn't support setting lladdr */
2479 error
= if_siflladdr(scan_ifp
,
2480 (const struct ether_addr
*) IF_LLADDR(ifp
));
2482 printf("%s(%s, %s): "
2483 "if_siflladdr (%s) failed %d\n",
2485 ifb
->ifb_name
, bondport_get_name(p
),
2486 bondport_get_name(scan_port
), error
);
2491 /* restore the port's ethernet address */
2492 if ((p
->po_control_flags
& PORT_CONTROL_FLAGS_LLADDR_SET
) != 0) {
2493 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2495 printf("%s(%s, %s): if_siflladdr failed %d\n",
2497 ifb
->ifb_name
, bondport_get_name(p
), error
);
2501 /* disable promiscous mode (if we enabled it) */
2502 if ((p
->po_control_flags
& PORT_CONTROL_FLAGS_PROMISCUOUS_SET
) != 0) {
2503 error
= ifnet_set_promiscuous(port_ifp
, 0);
2505 printf("%s(%s, %s): disable promiscuous failed %d\n",
2507 ifb
->ifb_name
, bondport_get_name(p
), error
);
2511 /* restore the port's MTU */
2512 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2514 printf("%s(%s, %s): SIOCSIFMTU %d failed %d\n",
2516 ifb
->ifb_name
, bondport_get_name(p
),
2517 p
->po_devmtu
.ifdm_current
, error
);
2520 /* remove the bond "protocol" */
2521 bond_detach_protocol(port_ifp
);
2523 /* detach the filter */
2524 if (filter
!= NULL
) {
2525 iflt_detach(filter
);
2528 /* generate link event */
2529 if (event_code
!= 0) {
2530 interface_link_event(ifp
, event_code
);
2535 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2536 /* release this bondport's reference to the ifbond */
2537 ifbond_release(ifb
);
2540 ifbond_signal(ifb
, __func__
);
2541 ifbond_release(ifb
);
2546 bond_set_lacp_mode(ifbond_ref ifb
)
2550 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2551 bondport_disable_distributing(p
);
2558 bond_set_static_mode(ifbond_ref ifb
)
2561 lacp_actor_partner_state s
;
2563 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2564 bondport_disable_distributing(p
);
2565 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2566 (void)bondport_remove_from_LAG(p
);
2567 bondport_cancel_timers(p
);
2569 /* announce that we're Individual now */
2570 s
= p
->po_actor_state
;
2571 s
= lacp_actor_partner_state_set_individual(s
);
2572 s
= lacp_actor_partner_state_set_not_collecting(s
);
2573 s
= lacp_actor_partner_state_set_not_distributing(s
);
2574 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2575 p
->po_actor_state
= s
;
2576 bondport_flags_set_ntt(p
);
2577 bondport_transmit_machine(p
, LAEventStart
,
2578 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2580 p
->po_actor_state
= 0;
2581 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2583 if (media_active(&p
->po_media_info
)) {
2584 bondport_enable_distributing(p
);
2586 bondport_disable_distributing(p
);
2593 bond_set_mode(struct ifnet
* ifp
, int mode
)
2600 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2601 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2603 return (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2605 if (ifb
->ifb_mode
== mode
) {
2611 ifbond_wait(ifb
, "bond_set_mode");
2613 /* verify (again) that the mode is actually different */
2614 if (ifb
->ifb_mode
== mode
) {
2619 ifb
->ifb_mode
= mode
;
2620 if (mode
== IF_BOND_MODE_LACP
) {
2621 bond_set_lacp_mode(ifb
);
2623 /* check if we need to generate a link status event */
2624 if (ifbond_selection(ifb
)) {
2625 event_code
= (ifb
->ifb_active_lag
== NULL
)
2630 bond_set_static_mode(ifb
);
2631 event_code
= (ifb
->ifb_distributing_count
== 0)
2635 ifb
->ifb_last_link_event
= event_code
;
2638 ifbond_signal(ifb
, __func__
);
2640 ifbond_release(ifb
);
2642 if (event_code
!= 0) {
2643 interface_link_event(ifp
, event_code
);
2649 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2654 struct if_bond_status_req
* ibsr
;
2655 struct if_bond_status ibs
;
2658 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2659 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2662 ibsr
->ibsr_key
= ifb
->ifb_key
;
2663 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2664 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2665 dst
= proc_is64bit(current_proc())
2666 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2667 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2668 if (dst
== USER_ADDR_NULL
) {
2669 /* just want to know how many there are */
2672 if (ibsr
->ibsr_count
< 0) {
2675 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2676 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2677 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2678 struct if_bond_partner_state
* ibps_p
;
2679 partner_state_ref ps
;
2684 bzero(&ibs
, sizeof(ibs
));
2685 strlcpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2686 ibs
.ibs_port_priority
= port
->po_priority
;
2687 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2688 ibs
.ibs_state
= port
->po_actor_state
;
2689 ibs
.ibs_selected_state
= port
->po_selected
;
2690 ps
= &port
->po_partner_state
;
2691 ibps_p
= &ibs
.ibs_partner_state
;
2692 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2693 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2694 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2695 ibps_p
->ibps_port
= ps
->ps_port
;
2696 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2697 ibps_p
->ibps_state
= ps
->ps_state
;
2699 /* fake the selected information */
2700 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2701 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2703 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2713 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2715 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2721 bond_set_promisc(__unused
struct ifnet
*ifp
)
2728 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2734 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2735 mtu_min
= IF_MINMTU
;
2737 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2738 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2740 if (devmtu_p
->ifdm_min
> mtu_min
) {
2741 mtu_min
= devmtu_p
->ifdm_min
;
2743 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2744 mtu_max
= devmtu_p
->ifdm_max
;
2753 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2758 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2759 error
= siocsifmtu(p
->po_ifp
, mtu
);
2761 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2762 ifb
->ifb_name
, bondport_get_name(p
), error
);
2770 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2780 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2781 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2782 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2786 ifbond_wait(ifb
, "bond_set_mtu");
2789 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2793 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2794 if (mtu
> mtu_max
) {
2798 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2799 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2804 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2806 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2808 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2809 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2810 if (new_max
!= old_max
) {
2811 /* we can safely walk the list of port without the lock held */
2813 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2815 /* try our best to back out of it */
2816 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2822 ifb
->ifb_altmtu
= mtu
;
2824 ifnet_set_mtu(ifp
, mtu
);
2829 ifbond_signal(ifb
, __func__
);
2830 ifbond_release(ifb
);
2838 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2841 struct if_bond_req ibr
;
2842 struct ifaddr
* ifa
;
2845 struct ifmediareq
*ifmr
;
2846 struct ifnet
* port_ifp
= NULL
;
2847 user_addr_t user_addr
;
2849 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2852 ifr
= (struct ifreq
*)data
;
2853 ifa
= (struct ifaddr
*)data
;
2857 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2860 case SIOCGIFMEDIA32
:
2861 case SIOCGIFMEDIA64
:
2863 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2864 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2866 return ifb
== NULL
? EOPNOTSUPP
: EBUSY
;
2868 ifmr
= (struct ifmediareq
*)data
;
2869 ifmr
->ifm_current
= IFM_ETHER
;
2871 ifmr
->ifm_status
= IFM_AVALID
;
2872 ifmr
->ifm_active
= IFM_ETHER
;
2873 ifmr
->ifm_count
= 1;
2874 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2875 if (ifb
->ifb_active_lag
!= NULL
) {
2876 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2877 ifmr
->ifm_status
|= IFM_ACTIVE
;
2879 } else if (ifb
->ifb_distributing_count
> 0) {
2881 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2882 ifmr
->ifm_status
|= IFM_ACTIVE
;
2885 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2886 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2887 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2888 if (user_addr
!= USER_ADDR_NULL
) {
2889 error
= copyout(&ifmr
->ifm_current
,
2896 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2902 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2903 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2905 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2908 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2909 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2910 &ifr
->ifr_devmtu
.ifdm_max
);
2916 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2917 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2919 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2922 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2927 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2931 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2935 user_addr
= proc_is64bit(current_proc())
2936 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2937 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2941 switch (ibr
.ibr_op
) {
2942 case IF_BOND_OP_ADD_INTERFACE
:
2943 case IF_BOND_OP_REMOVE_INTERFACE
:
2944 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2945 if (port_ifp
== NULL
) {
2949 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2950 error
= EPROTONOSUPPORT
;
2954 case IF_BOND_OP_SET_VERBOSE
:
2955 case IF_BOND_OP_SET_MODE
:
2964 switch (ibr
.ibr_op
) {
2965 case IF_BOND_OP_ADD_INTERFACE
:
2966 error
= bond_add_interface(ifp
, port_ifp
);
2968 case IF_BOND_OP_REMOVE_INTERFACE
:
2970 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2971 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2973 return ifb
== NULL
? EOPNOTSUPP
: EBUSY
;
2975 error
= bond_remove_interface(ifb
, port_ifp
);
2978 case IF_BOND_OP_SET_VERBOSE
:
2980 if_bond_debug
= ibr
.ibr_ibru
.ibru_int_val
;
2983 case IF_BOND_OP_SET_MODE
:
2984 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2985 case IF_BOND_MODE_LACP
:
2986 case IF_BOND_MODE_STATIC
:
2995 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2998 break; /* SIOCSIFBOND */
3001 user_addr
= proc_is64bit(current_proc())
3002 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
3003 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
3007 switch (ibr
.ibr_op
) {
3008 case IF_BOND_OP_GET_STATUS
:
3018 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
3019 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
3021 return ifb
== NULL
? EOPNOTSUPP
: EBUSY
;
3023 switch (ibr
.ibr_op
) {
3024 case IF_BOND_OP_GET_STATUS
:
3025 error
= bond_get_status(ifb
, &ibr
, user_addr
);
3029 break; /* SIOCGIFBOND */
3036 /* enable/disable promiscuous mode */
3038 error
= bond_set_promisc(ifp
);
3044 error
= bond_setmulti(ifp
);
3053 bond_if_free(struct ifnet
* ifp
)
3061 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
3066 ifbond_release(ifb
);
3073 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
3075 struct ifnet
* bond_ifp
= NULL
;
3077 int old_distributing_count
;
3079 struct media_info media_info
= { .mi_active
= 0, .mi_status
= 0 };
3081 switch (event_code
) {
3082 case KEV_DL_IF_DETACHED
:
3083 case KEV_DL_IF_DETACHING
:
3085 case KEV_DL_LINK_OFF
:
3086 case KEV_DL_LINK_ON
:
3087 media_info
= interface_media_info(port_ifp
);
3093 p
= bond_lookup_port(port_ifp
);
3099 old_distributing_count
= ifb
->ifb_distributing_count
;
3100 switch (event_code
) {
3101 case KEV_DL_IF_DETACHED
:
3102 case KEV_DL_IF_DETACHING
:
3103 bond_remove_interface(ifb
, p
->po_ifp
);
3105 case KEV_DL_LINK_OFF
:
3106 case KEV_DL_LINK_ON
:
3107 p
->po_media_info
= media_info
;
3108 if (p
->po_enabled
) {
3109 bondport_link_status_changed(p
);
3113 /* generate a link-event */
3114 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
3115 if (ifbond_selection(ifb
)) {
3116 event_code
= (ifb
->ifb_active_lag
== NULL
)
3119 /* XXX need to take a reference on bond_ifp */
3120 bond_ifp
= ifb
->ifb_ifp
;
3121 ifb
->ifb_last_link_event
= event_code
;
3123 event_code
= (ifb
->ifb_active_lag
== NULL
)
3126 if (event_code
!= ifb
->ifb_last_link_event
) {
3127 if (if_bond_debug
) {
3128 timestamp_printf("%s: (event) generating LINK event\n",
3131 bond_ifp
= ifb
->ifb_ifp
;
3132 ifb
->ifb_last_link_event
= event_code
;
3137 * if the distributing array membership changed from 0 <-> !0
3138 * generate a link event
3140 if (old_distributing_count
== 0
3141 && ifb
->ifb_distributing_count
!= 0) {
3142 event_code
= KEV_DL_LINK_ON
;
3143 } else if (old_distributing_count
!= 0
3144 && ifb
->ifb_distributing_count
== 0) {
3145 event_code
= KEV_DL_LINK_OFF
;
3147 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
3148 bond_ifp
= ifb
->ifb_ifp
;
3149 ifb
->ifb_last_link_event
= event_code
;
3154 if (bond_ifp
!= NULL
) {
3155 interface_link_event(bond_ifp
, event_code
);
3161 bond_iff_event(__unused
void *cookie
, ifnet_t port_ifp
,
3162 __unused protocol_family_t protocol
,
3163 const struct kev_msg
*event
)
3167 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3168 || event
->kev_class
!= KEV_NETWORK_CLASS
3169 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3172 event_code
= event
->event_code
;
3173 switch (event_code
) {
3174 case KEV_DL_LINK_OFF
:
3175 case KEV_DL_LINK_ON
:
3176 case KEV_DL_IF_DETACHING
:
3177 case KEV_DL_IF_DETACHED
:
3178 bond_handle_event(port_ifp
, event_code
);
3187 bond_iff_detached(__unused
void *cookie
, ifnet_t port_ifp
)
3189 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3194 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3197 struct kern_event_msg header
;
3199 char if_name
[IFNAMSIZ
];
3202 bzero(&event
, sizeof(event
));
3203 event
.header
.total_size
= sizeof(event
);
3204 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3205 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3206 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3207 event
.header
.event_code
= event_code
;
3208 event
.header
.event_data
[0] = ifnet_family(ifp
);
3209 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3210 strlcpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3211 ifnet_event(ifp
, &event
.header
);
3216 bond_proto_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t packet
,
3219 #pragma unused(protocol, packet, header)
3220 if (if_bond_debug
!= 0) {
3221 printf("%s: unexpected packet from %s\n", __func__
,
3229 * Function: bond_attach_protocol
3231 * Attach a DLIL protocol to the interface.
3233 * The ethernet demux special cases to always return PF_BOND if the
3234 * interface is bonded. That means we receive all traffic from that
3235 * interface without passing any of the traffic to any other attached
3239 bond_attach_protocol(struct ifnet
*ifp
)
3242 struct ifnet_attach_proto_param reg
;
3244 bzero(®
, sizeof(reg
));
3245 reg
.input
= bond_proto_input
;
3247 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3249 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3250 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3256 * Function: bond_detach_protocol
3258 * Detach our DLIL protocol from an interface
3261 bond_detach_protocol(struct ifnet
*ifp
)
3265 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3267 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3268 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3274 * Function: bond_attach_filter
3276 * Attach our DLIL interface filter.
3279 bond_attach_filter(struct ifnet
*ifp
, interface_filter_t
* filter_p
)
3282 struct iff_filter iff
;
3285 * install an interface filter
3287 memset(&iff
, 0, sizeof(struct iff_filter
));
3288 iff
.iff_name
= "com.apple.kernel.bsd.net.if_bond";
3289 iff
.iff_input
= bond_iff_input
;
3290 iff
.iff_event
= bond_iff_event
;
3291 iff
.iff_detached
= bond_iff_detached
;
3292 error
= iflt_attach_internal(ifp
, &iff
, filter_p
);
3294 printf("%s: iflt_attach_internal failed %d\n", __func__
, error
);
3301 * DLIL interface family functions
3303 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3304 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3305 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3306 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3307 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3308 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3310 __private_extern__
int
3311 bond_family_init(void)
3315 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3319 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3324 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3326 ether_detach_inet6
);
3328 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3333 error
= bond_clone_attach();
3335 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3350 ** LACP ifbond_list routines
3353 ifbond_list_find_moved_port(bondport_ref rx_port
,
3354 const lacp_actor_partner_tlv_ref atlv
)
3358 partner_state_ref ps
;
3361 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3362 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3364 /* no point in comparing against ourselves */
3367 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3368 /* it's not clear that we should be checking this */
3371 ps
= &p
->po_partner_state
;
3372 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3375 ps_li
= &ps
->ps_lag_info
;
3376 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3377 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3378 sizeof(ps_li
->li_system
)) == 0) {
3379 if (if_bond_debug
) {
3380 timestamp_printf("System " EA_FORMAT
3381 " Port 0x%x moved from %s to %s\n",
3382 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3383 bondport_get_name(p
),
3384 bondport_get_name(rx_port
));
3394 ** LACP ifbond, LAG routines
3398 ifbond_selection(ifbond_ref bond
)
3400 int all_ports_ready
= 0;
3401 int active_media
= 0;
3403 int lag_changed
= 0;
3407 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3408 if (lag
!= bond
->ifb_active_lag
) {
3409 if (bond
->ifb_active_lag
!= NULL
) {
3410 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3411 bond
->ifb_active_lag
= NULL
;
3413 bond
->ifb_active_lag
= lag
;
3415 ifbond_activate_LAG(bond
, lag
, active_media
);
3418 } else if (lag
!= NULL
) {
3419 if (lag
->lag_active_media
!= active_media
) {
3420 if (if_bond_debug
) {
3421 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3422 link_speed(lag
->lag_active_media
),
3423 link_speed(active_media
));
3425 ifbond_deactivate_LAG(bond
, lag
);
3426 ifbond_activate_LAG(bond
, lag
, active_media
);
3431 port_speed
= link_speed(active_media
);
3432 all_ports_ready
= ifbond_all_ports_ready(bond
);
3434 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3435 if (lag
!= NULL
&& p
->po_lag
== lag
3436 && media_speed(&p
->po_media_info
) == port_speed
3437 && (p
->po_mux_state
== MuxState_DETACHED
3438 || p
->po_selected
== SelectedState_SELECTED
3439 || p
->po_selected
== SelectedState_STANDBY
)
3440 && bondport_aggregatable(p
)) {
3441 if (bond
->ifb_max_active
> 0) {
3442 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3443 if (p
->po_selected
== SelectedState_STANDBY
3444 || p
->po_selected
== SelectedState_UNSELECTED
) {
3445 bondport_set_selected(p
, SelectedState_SELECTED
);
3447 } else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3448 bondport_set_selected(p
, SelectedState_STANDBY
);
3451 bondport_set_selected(p
, SelectedState_SELECTED
);
3454 if (bondport_flags_selected_changed(p
)) {
3455 bondport_flags_clear_selected_changed(p
);
3456 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3459 && bondport_flags_ready(p
)
3460 && p
->po_mux_state
== MuxState_WAITING
) {
3461 bondport_mux_machine(p
, LAEventReady
, NULL
);
3463 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3469 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3471 int best_active
= 0;
3472 LAG_ref best_lag
= NULL
;
3477 if (bond
->ifb_active_lag
!= NULL
) {
3478 best_lag
= bond
->ifb_active_lag
;
3479 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3480 if (bond
->ifb_max_active
> 0
3481 && best_count
> bond
->ifb_max_active
) {
3482 best_count
= bond
->ifb_max_active
;
3484 best_speed
= link_speed(best_active
);
3486 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3491 if (lag
== bond
->ifb_active_lag
) {
3492 /* we've already computed it */
3495 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3499 if (bond
->ifb_max_active
> 0
3500 && count
> bond
->ifb_max_active
) {
3501 /* if there's a limit, don't count extra links */
3502 count
= bond
->ifb_max_active
;
3504 speed
= link_speed(active
);
3505 if ((count
* speed
) > (best_count
* best_speed
)) {
3508 best_active
= active
;
3512 if (best_count
== 0) {
3515 *active_media
= best_active
;
3520 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3524 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3525 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3531 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3536 if (bond
->ifb_max_active
> 0) {
3537 need
= bond
->ifb_max_active
;
3539 lag
->lag_active_media
= active_media
;
3540 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3541 if (bondport_aggregatable(p
) == 0) {
3542 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3543 } else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3544 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3545 } else if (p
->po_mux_state
== MuxState_DETACHED
) {
3546 if (bond
->ifb_max_active
> 0) {
3548 bondport_set_selected(p
, SelectedState_SELECTED
);
3551 bondport_set_selected(p
, SelectedState_STANDBY
);
3554 bondport_set_selected(p
, SelectedState_SELECTED
);
3557 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3565 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3567 LAG_ref lag
= bond
->ifb_active_lag
;
3569 bond
->ifb_max_active
= max_active
;
3570 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3573 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3577 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3578 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3579 if (p
->po_selected
== SelectedState_SELECTED
) {
3580 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3582 if (remove_count
== 0) {
3593 ifbond_all_ports_ready(ifbond_ref bond
)
3598 if (bond
->ifb_active_lag
== NULL
) {
3601 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3602 if (p
->po_mux_state
== MuxState_WAITING
3603 && p
->po_selected
== SelectedState_SELECTED
) {
3604 if (bondport_flags_ready(p
) == 0) {
3608 /* note that there was at least one ready port */
3615 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3619 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3620 if (this_port
== p
) {
3623 if (bondport_flags_mux_attached(p
) == 0) {
3631 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3635 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3636 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3637 sizeof(lag
->lag_info
)) == 0) {
3645 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3655 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3656 if (bondport_aggregatable(p
)) {
3659 this_speed
= media_speed(&p
->po_media_info
);
3660 if (this_speed
== 0) {
3663 if (this_speed
> speed
) {
3664 active
= p
->po_media_info
.mi_active
;
3667 } else if (this_speed
== speed
) {
3672 *active_media
= active
;
3678 ** LACP bondport routines
3681 bondport_link_status_changed(bondport_ref p
)
3683 ifbond_ref bond
= p
->po_bond
;
3685 if (if_bond_debug
) {
3686 if (media_active(&p
->po_media_info
)) {
3687 const char * duplex_string
;
3689 if (media_full_duplex(&p
->po_media_info
)) {
3690 duplex_string
= "full";
3691 } else if (media_type_unknown(&p
->po_media_info
)) {
3692 duplex_string
= "unknown";
3694 duplex_string
= "half";
3696 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3697 bondport_get_name(p
),
3698 media_speed(&p
->po_media_info
),
3701 timestamp_printf("[%s] Link DOWN\n",
3702 bondport_get_name(p
));
3705 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3706 if (media_active(&p
->po_media_info
)
3707 && bond
->ifb_active_lag
!= NULL
3708 && p
->po_lag
== bond
->ifb_active_lag
3709 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3710 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3711 if (if_bond_debug
) {
3712 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3713 bondport_get_name(p
),
3714 media_speed(&p
->po_media_info
),
3715 link_speed(p
->po_lag
->lag_active_media
));
3717 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3720 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3721 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3722 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3724 if (media_active(&p
->po_media_info
)) {
3725 bondport_enable_distributing(p
);
3727 bondport_disable_distributing(p
);
3734 bondport_aggregatable(bondport_ref p
)
3736 partner_state_ref ps
= &p
->po_partner_state
;
3738 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3739 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3740 /* we and/or our partner are individual */
3743 if (p
->po_lag
== NULL
) {
3746 switch (p
->po_receive_state
) {
3748 if (if_bond_debug
) {
3749 timestamp_printf("[%s] Port is not selectable\n",
3750 bondport_get_name(p
));
3753 case ReceiveState_CURRENT
:
3754 case ReceiveState_EXPIRED
:
3761 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3763 LAG_info_ref lag_li
;
3764 partner_state_ref ps
;
3767 ps
= &p
->po_partner_state
;
3768 ps_li
= &ps
->ps_lag_info
;
3769 lag_li
= &lag
->lag_info
;
3770 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3771 && ps_li
->li_key
== lag_li
->li_key
3772 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3773 sizeof(lag_li
->li_system
))
3781 bondport_remove_from_LAG(bondport_ref p
)
3784 ifbond_ref bond
= p
->po_bond
;
3785 LAG_ref lag
= p
->po_lag
;
3790 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3791 if (if_bond_debug
) {
3792 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3794 bondport_get_name(p
),
3795 lag
->lag_info
.li_system_priority
,
3796 EA_LIST(&lag
->lag_info
.li_system
),
3797 lag
->lag_info
.li_key
);
3800 lag
->lag_port_count
--;
3801 if (lag
->lag_port_count
> 0) {
3802 return bond
->ifb_active_lag
== lag
;
3804 if (if_bond_debug
) {
3805 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3808 lag
->lag_info
.li_system_priority
,
3809 EA_LIST(&lag
->lag_info
.li_system
),
3810 lag
->lag_info
.li_key
);
3812 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3813 if (bond
->ifb_active_lag
== lag
) {
3814 bond
->ifb_active_lag
= NULL
;
3822 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3824 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3826 lag
->lag_port_count
++;
3827 if (if_bond_debug
) {
3828 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3829 bondport_get_name(p
),
3830 lag
->lag_info
.li_system_priority
,
3831 EA_LIST(&lag
->lag_info
.li_system
),
3832 lag
->lag_info
.li_key
);
3838 bondport_assign_to_LAG(bondport_ref p
)
3840 ifbond_ref bond
= p
->po_bond
;
3843 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3844 bondport_remove_from_LAG(p
);
3849 if (bondport_matches_LAG(p
, lag
)) {
3853 bondport_remove_from_LAG(p
);
3855 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3857 bondport_add_to_LAG(p
, lag
);
3860 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3861 TAILQ_INIT(&lag
->lag_port_list
);
3862 lag
->lag_port_count
= 0;
3863 lag
->lag_selected_port_count
= 0;
3864 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3865 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3866 if (if_bond_debug
) {
3867 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3870 lag
->lag_info
.li_system_priority
,
3871 EA_LIST(&lag
->lag_info
.li_system
),
3872 lag
->lag_info
.li_key
);
3874 bondport_add_to_LAG(p
, lag
);
3879 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3881 bondport_ref moved_port
;
3884 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3885 &in_lacpdu_p
->la_actor_tlv
);
3886 if (moved_port
!= NULL
) {
3887 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3889 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3890 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3891 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3896 bondport_set_selected(bondport_ref p
, SelectedState s
)
3898 if (s
!= p
->po_selected
) {
3899 ifbond_ref bond
= p
->po_bond
;
3900 LAG_ref lag
= p
->po_lag
;
3902 bondport_flags_set_selected_changed(p
);
3903 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3904 if (p
->po_selected
== SelectedState_SELECTED
) {
3905 lag
->lag_selected_port_count
--;
3906 } else if (s
== SelectedState_SELECTED
) {
3907 lag
->lag_selected_port_count
++;
3909 if (if_bond_debug
) {
3910 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3911 bondport_get_name(p
),
3912 SelectedStateString(s
),
3913 SelectedStateString(p
->po_selected
));
3926 bondport_UpdateDefaultSelected(bondport_ref p
)
3928 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3933 bondport_RecordDefault(bondport_ref p
)
3935 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3937 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3938 bondport_assign_to_LAG(p
);
3943 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3945 lacp_actor_partner_tlv_ref actor
;
3946 partner_state_ref ps
;
3949 /* compare the PDU's Actor information to our Partner state */
3950 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3951 ps
= &p
->po_partner_state
;
3952 ps_li
= &ps
->ps_lag_info
;
3953 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3954 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3955 != ps
->ps_port_priority
)
3956 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3957 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3958 != ps_li
->li_system_priority
)
3959 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3960 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3961 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3962 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3963 if (if_bond_debug
) {
3964 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3965 bondport_get_name(p
));
3972 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3974 lacp_actor_partner_tlv_ref actor
;
3975 ifbond_ref bond
= p
->po_bond
;
3976 int lacp_maintain
= 0;
3977 partner_state_ref ps
;
3978 lacp_actor_partner_tlv_ref partner
;
3981 /* copy the PDU's Actor information into our Partner state */
3982 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3983 ps
= &p
->po_partner_state
;
3984 ps_li
= &ps
->ps_lag_info
;
3985 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3986 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3987 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3988 ps_li
->li_system_priority
3989 = lacp_actor_partner_tlv_get_system_priority(actor
);
3990 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3991 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3993 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3995 /* compare the PDU's Partner information to our own information */
3996 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3998 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3999 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
4000 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
4001 if (if_bond_debug
) {
4002 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
4003 bondport_get_name(p
));
4007 if ((lacp_actor_partner_tlv_get_port(partner
)
4008 == bondport_get_index(p
))
4009 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
4010 && bcmp(partner
->lap_system
, &g_bond
->system
,
4011 sizeof(g_bond
->system
)) == 0
4012 && (lacp_actor_partner_tlv_get_system_priority(partner
)
4013 == g_bond
->system_priority
)
4014 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
4015 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
4016 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
4017 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
4019 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
4020 if (if_bond_debug
) {
4021 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
4022 bondport_get_name(p
));
4024 } else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
4025 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
4027 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
4028 if (if_bond_debug
) {
4029 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
4030 bondport_get_name(p
));
4033 bondport_assign_to_LAG(p
);
4037 static __inline__ lacp_actor_partner_state
4038 updateNTTBits(lacp_actor_partner_state s
)
4040 return s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
4041 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
4042 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
4043 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
);
4047 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
4049 ifbond_ref bond
= p
->po_bond
;
4050 lacp_actor_partner_tlv_ref partner
;
4052 /* compare the PDU's Actor information to our Partner state */
4053 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
4054 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
4055 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
4056 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
4057 || (lacp_actor_partner_tlv_get_system_priority(partner
)
4058 != g_bond
->system_priority
)
4059 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
4060 || (updateNTTBits(partner
->lap_state
)
4061 != updateNTTBits(p
->po_actor_state
))) {
4062 bondport_flags_set_ntt(p
);
4063 if (if_bond_debug
) {
4064 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
4065 bondport_get_name(p
));
4072 bondport_AttachMuxToAggregator(bondport_ref p
)
4074 if (bondport_flags_mux_attached(p
) == 0) {
4075 if (if_bond_debug
) {
4076 timestamp_printf("[%s] Attached Mux To Aggregator\n",
4077 bondport_get_name(p
));
4079 bondport_flags_set_mux_attached(p
);
4085 bondport_DetachMuxFromAggregator(bondport_ref p
)
4087 if (bondport_flags_mux_attached(p
)) {
4088 if (if_bond_debug
) {
4089 timestamp_printf("[%s] Detached Mux From Aggregator\n",
4090 bondport_get_name(p
));
4092 bondport_flags_clear_mux_attached(p
);
4098 bondport_enable_distributing(bondport_ref p
)
4100 if (bondport_flags_distributing(p
) == 0) {
4101 ifbond_ref bond
= p
->po_bond
;
4103 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
4104 if (if_bond_debug
) {
4105 timestamp_printf("[%s] Distribution Enabled\n",
4106 bondport_get_name(p
));
4108 bondport_flags_set_distributing(p
);
4114 bondport_disable_distributing(bondport_ref p
)
4116 if (bondport_flags_distributing(p
)) {
4117 bondport_ref
* array
;
4123 array
= bond
->ifb_distributing_array
;
4124 count
= bond
->ifb_distributing_count
;
4125 for (i
= 0; i
< count
; i
++) {
4126 if (array
[i
] == p
) {
4129 for (j
= i
; j
< (count
- 1); j
++) {
4130 array
[j
] = array
[j
+ 1];
4135 bond
->ifb_distributing_count
--;
4136 if (if_bond_debug
) {
4137 timestamp_printf("[%s] Distribution Disabled\n",
4138 bondport_get_name(p
));
4140 bondport_flags_clear_distributing(p
);
4146 ** Receive machine functions
4149 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4152 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4155 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4158 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4161 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4164 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4168 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
4171 switch (p
->po_receive_state
) {
4172 case ReceiveState_none
:
4173 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4175 case ReceiveState_INITIALIZE
:
4176 bondport_receive_machine_initialize(p
, event
, event_data
);
4178 case ReceiveState_PORT_DISABLED
:
4179 bondport_receive_machine_port_disabled(p
, event
, event_data
);
4181 case ReceiveState_EXPIRED
:
4182 bondport_receive_machine_expired(p
, event
, event_data
);
4184 case ReceiveState_LACP_DISABLED
:
4185 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
4187 case ReceiveState_DEFAULTED
:
4188 bondport_receive_machine_defaulted(p
, event
, event_data
);
4190 case ReceiveState_CURRENT
:
4191 bondport_receive_machine_current(p
, event
, event_data
);
4200 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4205 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4206 bondport_receive_machine_current(p
, event
, event_data
);
4209 case LAEventMediaChange
:
4210 if (media_active(&p
->po_media_info
)) {
4211 switch (p
->po_receive_state
) {
4212 case ReceiveState_PORT_DISABLED
:
4213 case ReceiveState_LACP_DISABLED
:
4214 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4220 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4224 bondport_receive_machine_event(p
, event
, event_data
);
4231 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4232 __unused
void * event_data
)
4236 devtimer_cancel(p
->po_current_while_timer
);
4237 if (if_bond_debug
) {
4238 timestamp_printf("[%s] Receive INITIALIZE\n",
4239 bondport_get_name(p
));
4241 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4242 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4243 bondport_RecordDefault(p
);
4245 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4246 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4255 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4256 __unused
void * event_data
)
4258 partner_state_ref ps
;
4262 devtimer_cancel(p
->po_current_while_timer
);
4263 if (if_bond_debug
) {
4264 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4265 bondport_get_name(p
));
4267 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4268 ps
= &p
->po_partner_state
;
4269 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4271 case LAEventMediaChange
:
4272 if (media_active(&p
->po_media_info
)) {
4273 if (media_ok(&p
->po_media_info
)) {
4274 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4276 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4278 } else if (p
->po_selected
== SelectedState_SELECTED
) {
4281 if (if_bond_debug
) {
4282 timestamp_printf("[%s] Receive PORT_DISABLED: "
4283 "link timer started\n",
4284 bondport_get_name(p
));
4288 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4289 (devtimer_timeout_func
)
4290 bondport_receive_machine_port_disabled
,
4291 (void *)LAEventTimeout
, NULL
);
4292 } else if (p
->po_selected
== SelectedState_STANDBY
) {
4293 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4296 case LAEventTimeout
:
4297 if (p
->po_selected
== SelectedState_SELECTED
) {
4298 if (if_bond_debug
) {
4299 timestamp_printf("[%s] Receive PORT_DISABLED: "
4300 "link timer completed, marking UNSELECTED\n",
4301 bondport_get_name(p
));
4303 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4306 case LAEventPortMoved
:
4307 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4316 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4317 __unused
void * event_data
)
4319 lacp_actor_partner_state s
;
4324 devtimer_cancel(p
->po_current_while_timer
);
4325 if (if_bond_debug
) {
4326 timestamp_printf("[%s] Receive EXPIRED\n",
4327 bondport_get_name(p
));
4329 p
->po_receive_state
= ReceiveState_EXPIRED
;
4330 s
= p
->po_partner_state
.ps_state
;
4331 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4332 s
= lacp_actor_partner_state_set_short_timeout(s
);
4333 p
->po_partner_state
.ps_state
= s
;
4335 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4336 /* start current_while timer */
4337 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4339 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4340 (devtimer_timeout_func
)
4341 bondport_receive_machine_expired
,
4342 (void *)LAEventTimeout
, NULL
);
4345 case LAEventTimeout
:
4346 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4355 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4356 __unused
void * event_data
)
4358 partner_state_ref ps
;
4361 devtimer_cancel(p
->po_current_while_timer
);
4362 if (if_bond_debug
) {
4363 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4364 bondport_get_name(p
));
4366 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4367 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4368 bondport_RecordDefault(p
);
4369 ps
= &p
->po_partner_state
;
4370 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4372 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4381 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4382 __unused
void * event_data
)
4386 devtimer_cancel(p
->po_current_while_timer
);
4387 if (if_bond_debug
) {
4388 timestamp_printf("[%s] Receive DEFAULTED\n",
4389 bondport_get_name(p
));
4391 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4392 bondport_UpdateDefaultSelected(p
);
4393 bondport_RecordDefault(p
);
4395 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4404 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4407 partner_state_ref ps
;
4412 devtimer_cancel(p
->po_current_while_timer
);
4413 if (if_bond_debug
) {
4414 timestamp_printf("[%s] Receive CURRENT\n",
4415 bondport_get_name(p
));
4417 p
->po_receive_state
= ReceiveState_CURRENT
;
4418 bondport_UpdateSelected(p
, event_data
);
4419 bondport_UpdateNTT(p
, event_data
);
4420 bondport_RecordPDU(p
, event_data
);
4422 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4423 bondport_assign_to_LAG(p
);
4424 /* start current_while timer */
4425 ps
= &p
->po_partner_state
;
4426 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4427 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4429 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4432 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4433 (devtimer_timeout_func
)
4434 bondport_receive_machine_current
,
4435 (void *)LAEventTimeout
, NULL
);
4437 case LAEventTimeout
:
4438 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4447 ** Periodic Transmission machine
4451 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4452 __unused
void * event_data
)
4455 partner_state_ref ps
;
4460 if (if_bond_debug
) {
4461 timestamp_printf("[%s] periodic_transmit Start\n",
4462 bondport_get_name(p
));
4465 case LAEventMediaChange
:
4466 devtimer_cancel(p
->po_periodic_timer
);
4467 p
->po_periodic_interval
= 0;
4468 if (media_active(&p
->po_media_info
) == 0
4469 || media_ok(&p
->po_media_info
) == 0) {
4473 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4474 ps
= &p
->po_partner_state
;
4475 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4476 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4478 devtimer_cancel(p
->po_periodic_timer
);
4479 p
->po_periodic_interval
= 0;
4482 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4483 interval
= LACP_FAST_PERIODIC_TIME
;
4485 interval
= LACP_SLOW_PERIODIC_TIME
;
4487 if (p
->po_periodic_interval
!= interval
) {
4488 if (interval
== LACP_FAST_PERIODIC_TIME
4489 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4490 if (if_bond_debug
) {
4491 timestamp_printf("[%s] periodic_transmit:"
4492 " Need To Transmit\n",
4493 bondport_get_name(p
));
4495 bondport_flags_set_ntt(p
);
4497 p
->po_periodic_interval
= interval
;
4499 tv
.tv_sec
= interval
;
4500 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4501 (devtimer_timeout_func
)
4502 bondport_periodic_transmit_machine
,
4503 (void *)LAEventTimeout
, NULL
);
4504 if (if_bond_debug
) {
4505 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4506 bondport_get_name(p
),
4507 p
->po_periodic_interval
);
4511 case LAEventTimeout
:
4512 bondport_flags_set_ntt(p
);
4513 tv
.tv_sec
= p
->po_periodic_interval
;
4515 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4516 bondport_periodic_transmit_machine
,
4517 (void *)LAEventTimeout
, NULL
);
4518 if (if_bond_debug
> 1) {
4519 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4520 bondport_get_name(p
), p
->po_periodic_interval
);
4533 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4534 __darwin_time_t
* next_secs
)
4536 if (p
->po_last_transmit_secs
!= current_secs
) {
4537 p
->po_last_transmit_secs
= current_secs
;
4538 p
->po_n_transmit
= 0;
4540 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4544 if (next_secs
!= NULL
) {
4545 *next_secs
= current_secs
+ 1;
4551 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4554 lacp_actor_partner_tlv_ref aptlv
;
4555 lacp_collector_tlv_ref ctlv
;
4556 struct timeval next_tick_time
= {.tv_sec
= 0, .tv_usec
= 0};
4557 lacpdu_ref out_lacpdu_p
;
4558 packet_buffer_ref pkt
;
4559 partner_state_ref ps
;
4563 case LAEventTimeout
:
4565 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4568 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4569 /* we're going away, transmit the packet no matter what */
4570 } else if (bondport_can_transmit(p
, devtimer_current_secs(),
4571 &next_tick_time
.tv_sec
) == 0) {
4572 if (devtimer_enabled(p
->po_transmit_timer
)) {
4573 if (if_bond_debug
> 0) {
4574 timestamp_printf("[%s] Transmit Timer Already Set\n",
4575 bondport_get_name(p
));
4578 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4579 (devtimer_timeout_func
)
4580 bondport_transmit_machine
,
4581 (void *)LAEventTimeout
, NULL
);
4582 if (if_bond_debug
> 0) {
4583 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4584 bondport_get_name(p
),
4585 (int)next_tick_time
.tv_sec
);
4590 if (if_bond_debug
> 0) {
4591 if (event
== LAEventTimeout
) {
4592 timestamp_printf("[%s] Transmit Timer Complete\n",
4593 bondport_get_name(p
));
4596 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4598 printf("[%s] Transmit: failed to allocate packet buffer\n",
4599 bondport_get_name(p
));
4602 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4603 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4604 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4605 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4608 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4609 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4610 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4611 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4612 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4613 g_bond
->system_priority
);
4614 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4615 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4616 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4617 aptlv
->lap_state
= p
->po_actor_state
;
4620 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4621 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4622 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4623 ps
= &p
->po_partner_state
;
4624 ps_li
= &ps
->ps_lag_info
;
4625 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4626 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4627 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4628 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4629 ps_li
->li_system_priority
);
4630 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4631 aptlv
->lap_state
= ps
->ps_state
;
4634 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4635 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4636 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4638 bondport_slow_proto_transmit(p
, pkt
);
4639 bondport_flags_clear_ntt(p
);
4640 if (if_bond_debug
> 0) {
4641 timestamp_printf("[%s] Transmit Packet %d\n",
4642 bondport_get_name(p
), p
->po_n_transmit
);
4652 ** Mux machine functions
4656 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4659 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4662 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4666 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4670 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4672 switch (p
->po_mux_state
) {
4674 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4676 case MuxState_DETACHED
:
4677 bondport_mux_machine_detached(p
, event
, event_data
);
4679 case MuxState_WAITING
:
4680 bondport_mux_machine_waiting(p
, event
, event_data
);
4682 case MuxState_ATTACHED
:
4683 bondport_mux_machine_attached(p
, event
, event_data
);
4685 case MuxState_COLLECTING_DISTRIBUTING
:
4686 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4695 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4696 __unused
void * event_data
)
4698 lacp_actor_partner_state s
;
4702 devtimer_cancel(p
->po_wait_while_timer
);
4703 if (if_bond_debug
) {
4704 timestamp_printf("[%s] Mux DETACHED\n",
4705 bondport_get_name(p
));
4707 p
->po_mux_state
= MuxState_DETACHED
;
4708 bondport_flags_clear_ready(p
);
4709 bondport_DetachMuxFromAggregator(p
);
4710 bondport_disable_distributing(p
);
4711 s
= p
->po_actor_state
;
4712 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4713 s
= lacp_actor_partner_state_set_not_collecting(s
);
4714 s
= lacp_actor_partner_state_set_not_distributing(s
);
4715 p
->po_actor_state
= s
;
4716 bondport_flags_set_ntt(p
);
4718 case LAEventSelectedChange
:
4720 case LAEventMediaChange
:
4721 if (p
->po_selected
== SelectedState_SELECTED
4722 || p
->po_selected
== SelectedState_STANDBY
) {
4723 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4733 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4734 __unused
void * event_data
)
4740 devtimer_cancel(p
->po_wait_while_timer
);
4741 if (if_bond_debug
) {
4742 timestamp_printf("[%s] Mux WAITING\n",
4743 bondport_get_name(p
));
4745 p
->po_mux_state
= MuxState_WAITING
;
4748 case LAEventSelectedChange
:
4749 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4750 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4753 if (p
->po_selected
== SelectedState_STANDBY
) {
4754 devtimer_cancel(p
->po_wait_while_timer
);
4755 /* wait until state changes to SELECTED */
4756 if (if_bond_debug
) {
4757 timestamp_printf("[%s] Mux WAITING: Standby\n",
4758 bondport_get_name(p
));
4762 if (bondport_flags_ready(p
)) {
4763 if (if_bond_debug
) {
4764 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4765 bondport_get_name(p
));
4769 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4770 if (if_bond_debug
) {
4771 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4772 bondport_get_name(p
));
4776 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4777 devtimer_cancel(p
->po_wait_while_timer
);
4778 if (if_bond_debug
) {
4779 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4780 bondport_get_name(p
));
4782 bondport_flags_set_ready(p
);
4785 if (if_bond_debug
) {
4786 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4787 bondport_get_name(p
));
4789 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4791 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4792 (devtimer_timeout_func
)
4793 bondport_mux_machine_waiting
,
4794 (void *)LAEventTimeout
, NULL
);
4796 case LAEventTimeout
:
4797 if (if_bond_debug
) {
4798 timestamp_printf("[%s] Mux WAITING: Ready\n",
4799 bondport_get_name(p
));
4801 bondport_flags_set_ready(p
);
4805 if (bondport_flags_ready(p
)) {
4806 if (if_bond_debug
) {
4807 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4808 bondport_get_name(p
));
4810 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4819 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4820 __unused
void * event_data
)
4822 lacp_actor_partner_state s
;
4826 devtimer_cancel(p
->po_wait_while_timer
);
4827 if (if_bond_debug
) {
4828 timestamp_printf("[%s] Mux ATTACHED\n",
4829 bondport_get_name(p
));
4831 p
->po_mux_state
= MuxState_ATTACHED
;
4832 bondport_AttachMuxToAggregator(p
);
4833 s
= p
->po_actor_state
;
4834 s
= lacp_actor_partner_state_set_in_sync(s
);
4835 s
= lacp_actor_partner_state_set_not_collecting(s
);
4836 s
= lacp_actor_partner_state_set_not_distributing(s
);
4837 bondport_disable_distributing(p
);
4838 p
->po_actor_state
= s
;
4839 bondport_flags_set_ntt(p
);
4842 switch (p
->po_selected
) {
4843 case SelectedState_SELECTED
:
4844 s
= p
->po_partner_state
.ps_state
;
4845 if (lacp_actor_partner_state_in_sync(s
)) {
4846 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4851 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4860 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4862 __unused
void * event_data
)
4864 lacp_actor_partner_state s
;
4868 devtimer_cancel(p
->po_wait_while_timer
);
4869 if (if_bond_debug
) {
4870 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4871 bondport_get_name(p
));
4873 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4874 bondport_enable_distributing(p
);
4875 s
= p
->po_actor_state
;
4876 s
= lacp_actor_partner_state_set_collecting(s
);
4877 s
= lacp_actor_partner_state_set_distributing(s
);
4878 p
->po_actor_state
= s
;
4879 bondport_flags_set_ntt(p
);
4882 s
= p
->po_partner_state
.ps_state
;
4883 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4884 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4887 switch (p
->po_selected
) {
4888 case SelectedState_UNSELECTED
:
4889 case SelectedState_STANDBY
:
4890 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);