2 * Copyright (c) 2004-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * - bond/failover interface
32 * - implements IEEE 802.3ad Link Aggregation
36 * Modification History:
38 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/kern_event.h>
54 #include <net/ethernet.h>
56 #include <net/kpi_interface.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_ether.h>
60 #include <net/if_types.h>
61 #include <net/if_bond_var.h>
62 #include <net/ieee8023ad.h>
66 #include <net/devtimer.h>
67 #include <net/if_vlan_var.h>
68 #include <net/kpi_protocol.h>
70 #include <kern/locks.h>
71 #include <libkern/OSAtomic.h>
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
79 #include <net/if_media.h>
80 #include <net/multicast_list.h>
82 static struct ether_addr slow_proto_multicast
= {
83 IEEE8023AD_SLOW_PROTO_MULTICAST
86 #define BOND_MAXUNIT 128
87 #define BONDNAME "bond"
88 #define M_BOND M_DEVBUF
90 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
91 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
92 #define EA_LIST(ea) EA_CH(ea,0),EA_CH(ea,1),EA_CH(ea,2),EA_CH(ea,3),EA_CH(ea,4),EA_CH(ea,5)
94 #define timestamp_printf printf
99 static __inline__ lck_grp_t
*
100 my_lck_grp_alloc_init(const char * grp_name
)
103 lck_grp_attr_t
* grp_attrs
;
105 grp_attrs
= lck_grp_attr_alloc_init();
106 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
107 lck_grp_attr_free(grp_attrs
);
111 static __inline__ lck_mtx_t
*
112 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
114 lck_attr_t
* lck_attrs
;
117 lck_attrs
= lck_attr_alloc_init();
118 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
119 lck_attr_free(lck_attrs
);
123 static lck_mtx_t
* bond_lck_mtx
;
125 static __inline__
void
128 lck_grp_t
* bond_lck_grp
;
130 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
131 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
134 static __inline__
void
135 bond_assert_lock_held(void)
137 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
141 static __inline__
void
142 bond_assert_lock_not_held(void)
144 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
148 static __inline__
void
151 lck_mtx_lock(bond_lck_mtx
);
155 static __inline__
void
158 lck_mtx_unlock(bond_lck_mtx
);
163 ** bond structures, types
167 lacp_system li_system
;
168 lacp_system_priority li_system_priority
;
171 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
174 TAILQ_HEAD(port_list
, bondport_s
);
176 TAILQ_HEAD(ifbond_list
, ifbond_s
);
178 TAILQ_HEAD(lag_list
, LAG_s
);
180 typedef struct ifbond_s ifbond
, * ifbond_ref
;
181 typedef struct bondport_s bondport
, * bondport_ref
;
184 TAILQ_ENTRY(LAG_s
) lag_list
;
185 struct port_list lag_port_list
;
186 short lag_port_count
;
187 short lag_selected_port_count
;
188 int lag_active_media
;
191 typedef struct LAG_s LAG
, * LAG_ref
;
193 typedef struct partner_state_s
{
194 LAG_info ps_lag_info
;
196 lacp_port_priority ps_port_priority
;
197 lacp_actor_partner_state ps_state
;
198 } partner_state
, * partner_state_ref
;
201 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
203 SInt32 ifb_retain_count
;
204 char ifb_name
[IFNAMSIZ
];
205 struct ifnet
* ifb_ifp
;
206 bpf_packet_func ifb_bpf_input
;
207 bpf_packet_func ifb_bpf_output
;
209 struct port_list ifb_port_list
;
210 short ifb_port_count
;
211 struct lag_list ifb_lag_list
;
213 short ifb_max_active
; /* 0 == unlimited */
214 LAG_ref ifb_active_lag
;
215 struct ifmultiaddr
* ifb_ifma_slow_proto
;
216 bondport_ref
* ifb_distributing_array
;
217 int ifb_distributing_count
;
218 int ifb_last_link_event
;
219 int ifb_mode
; /* LACP, STATIC */
228 ReceiveState_none
= 0,
229 ReceiveState_INITIALIZE
= 1,
230 ReceiveState_PORT_DISABLED
= 2,
231 ReceiveState_EXPIRED
= 3,
232 ReceiveState_LACP_DISABLED
= 4,
233 ReceiveState_DEFAULTED
= 5,
234 ReceiveState_CURRENT
= 6,
237 typedef u_char ReceiveState
;
240 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
241 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
242 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
244 typedef u_char SelectedState
;
246 static __inline__
const char *
247 SelectedStateString(SelectedState s
)
249 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
251 if (s
<= SelectedState_STANDBY
) {
254 return ("<unknown>");
259 MuxState_DETACHED
= 1,
260 MuxState_WAITING
= 2,
261 MuxState_ATTACHED
= 3,
262 MuxState_COLLECTING_DISTRIBUTING
= 4,
265 typedef u_char MuxState
;
268 TAILQ_ENTRY(bondport_s
) po_port_list
;
270 struct multicast_list po_multicast
;
271 struct ifnet
* po_ifp
;
272 struct ether_addr po_saved_addr
;
274 char po_name
[IFNAMSIZ
];
275 struct ifdevmtu po_devmtu
;
278 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
279 devtimer_ref po_current_while_timer
;
280 devtimer_ref po_periodic_timer
;
281 devtimer_ref po_wait_while_timer
;
282 devtimer_ref po_transmit_timer
;
283 partner_state po_partner_state
;
284 lacp_port_priority po_priority
;
285 lacp_actor_partner_state po_actor_state
;
287 u_char po_periodic_interval
;
288 u_char po_n_transmit
;
289 ReceiveState po_receive_state
;
290 MuxState po_mux_state
;
291 SelectedState po_selected
;
292 int32_t po_last_transmit_secs
;
293 struct media_info po_media_info
;
297 #define IFBF_PROMISC 0x1 /* promiscuous mode */
298 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
299 #define IFBF_LLADDR 0x4 /* specific link address requested */
300 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
302 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
305 static __inline__
int
306 ifbond_flags_promisc(ifbond_ref ifb
)
308 return ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0);
311 static __inline__
void
312 ifbond_flags_set_promisc(ifbond_ref ifb
)
314 ifb
->ifb_flags
|= IFBF_PROMISC
;
318 static __inline__
void
319 ifbond_flags_clear_promisc(ifbond_ref ifb
)
321 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
325 static __inline__
int
326 ifbond_flags_if_detaching(ifbond_ref ifb
)
328 return ((ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0);
331 static __inline__
void
332 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
334 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
338 static __inline__
int
339 ifbond_flags_lladdr(ifbond_ref ifb
)
341 return ((ifb
->ifb_flags
& IFBF_LLADDR
) != 0);
344 static __inline__
void
345 ifbond_flags_set_lladdr(ifbond_ref ifb
)
347 ifb
->ifb_flags
|= IFBF_LLADDR
;
351 static __inline__
void
352 ifbond_flags_clear_lladdr(ifbond_ref ifb
)
354 ifb
->ifb_flags
&= ~IFBF_LLADDR
;
358 static __inline__
int
359 ifbond_flags_change_in_progress(ifbond_ref ifb
)
361 return ((ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0);
364 static __inline__
void
365 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
367 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
371 static __inline__
void
372 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
374 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
379 * bondport_ref->po_flags bits
381 #define BONDPORT_FLAGS_NTT 0x01
382 #define BONDPORT_FLAGS_READY 0x02
383 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
384 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
385 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
386 #define BONDPORT_FLAGS_UNUSED2 0x20
387 #define BONDPORT_FLAGS_UNUSED3 0x40
388 #define BONDPORT_FLAGS_UNUSED4 0x80
390 static __inline__
void
391 bondport_flags_set_ntt(bondport_ref p
)
393 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
397 static __inline__
void
398 bondport_flags_clear_ntt(bondport_ref p
)
400 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
404 static __inline__
int
405 bondport_flags_ntt(bondport_ref p
)
407 return ((p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0);
410 static __inline__
void
411 bondport_flags_set_ready(bondport_ref p
)
413 p
->po_flags
|= BONDPORT_FLAGS_READY
;
417 static __inline__
void
418 bondport_flags_clear_ready(bondport_ref p
)
420 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
424 static __inline__
int
425 bondport_flags_ready(bondport_ref p
)
427 return ((p
->po_flags
& BONDPORT_FLAGS_READY
) != 0);
430 static __inline__
void
431 bondport_flags_set_selected_changed(bondport_ref p
)
433 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
437 static __inline__
void
438 bondport_flags_clear_selected_changed(bondport_ref p
)
440 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
444 static __inline__
int
445 bondport_flags_selected_changed(bondport_ref p
)
447 return ((p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0);
450 static __inline__
void
451 bondport_flags_set_mux_attached(bondport_ref p
)
453 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
457 static __inline__
void
458 bondport_flags_clear_mux_attached(bondport_ref p
)
460 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
464 static __inline__
int
465 bondport_flags_mux_attached(bondport_ref p
)
467 return ((p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0);
470 static __inline__
void
471 bondport_flags_set_distributing(bondport_ref p
)
473 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
477 static __inline__
void
478 bondport_flags_clear_distributing(bondport_ref p
)
480 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
484 static __inline__
int
485 bondport_flags_distributing(bondport_ref p
)
487 return ((p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0);
490 typedef struct bond_globals_s
{
491 struct ifbond_list ifbond_list
;
493 lacp_system_priority system_priority
;
495 } * bond_globals_ref
;
497 static bond_globals_ref g_bond
;
500 ** packet_buffer routines
501 ** - thin wrapper for mbuf
504 typedef struct mbuf
* packet_buffer_ref
;
506 static packet_buffer_ref
507 packet_buffer_allocate(int length
)
512 /* leave room for ethernet header */
513 size
= length
+ sizeof(struct ether_header
);
514 if (size
> (int)MHLEN
) {
515 /* XXX doesn't handle large payloads */
516 printf("bond: packet_buffer_allocate size %d > max %u\n", size
, MHLEN
);
519 m
= m_gethdr(M_WAITOK
, MT_DATA
);
524 m
->m_pkthdr
.len
= size
;
529 packet_buffer_byteptr(packet_buffer_ref buf
)
531 return (buf
->m_data
+ sizeof(struct ether_header
));
539 LAEventSelectedChange
,
548 bondport_receive_machine(bondport_ref p
, LAEvent event
,
551 ** Periodic Transmission machine
554 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
560 #define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
563 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
570 bondport_mux_machine(bondport_ref p
, LAEvent event
,
577 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
580 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
583 ifbond_all_ports_ready(ifbond_ref bond
);
586 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
589 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
592 ifbond_selection(ifbond_ref bond
);
600 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
603 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
606 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
607 int active
, int short_timeout
, int * error
);
609 bondport_start(bondport_ref p
);
612 bondport_free(bondport_ref p
);
615 bondport_aggregatable(bondport_ref p
);
618 bondport_remove_from_LAG(bondport_ref p
);
621 bondport_set_selected(bondport_ref p
, SelectedState s
);
624 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
627 bondport_link_status_changed(bondport_ref p
);
630 bondport_enable_distributing(bondport_ref p
);
633 bondport_disable_distributing(bondport_ref p
);
635 static __inline__
int
636 bondport_collecting(bondport_ref p
)
638 if (p
->po_bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
639 return (lacp_actor_partner_state_collecting(p
->po_actor_state
));
645 ** bond interface/dlil specific routines
647 static int bond_clone_create(struct if_clone
*, u_int32_t
, void *);
648 static int bond_clone_destroy(struct ifnet
*);
649 static int bond_input(ifnet_t ifp
, protocol_family_t protocol
, mbuf_t m
,
651 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
652 static int bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * addr
);
653 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
654 bpf_packet_func func
);
655 static int bond_attach_protocol(struct ifnet
*ifp
);
656 static int bond_detach_protocol(struct ifnet
*ifp
);
657 static int bond_setmulti(struct ifnet
*ifp
);
658 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
659 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
660 static void bond_if_free(struct ifnet
* ifp
);
662 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
667 static void interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
);
670 siocsifmtu(struct ifnet
* ifp
, int mtu
)
674 bzero(&ifr
, sizeof(ifr
));
676 return (ifnet_ioctl(ifp
, 0, SIOCSIFMTU
, &ifr
));
680 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
685 bzero(&ifr
, sizeof(ifr
));
686 error
= ifnet_ioctl(ifp
, 0, SIOCGIFDEVMTU
, &ifr
);
688 *ifdm_p
= ifr
.ifr_devmtu
;
693 static __inline__
void
694 ether_addr_copy(void * dest
, const void * source
)
696 bcopy(source
, dest
, ETHER_ADDR_LEN
);
700 static __inline__
void
701 ifbond_retain(ifbond_ref ifb
)
703 OSIncrementAtomic(&ifb
->ifb_retain_count
);
706 static __inline__
void
707 ifbond_release(ifbond_ref ifb
)
709 UInt32 old_retain_count
;
711 old_retain_count
= OSDecrementAtomic(&ifb
->ifb_retain_count
);
712 switch (old_retain_count
) {
714 panic("ifbond_release: retain count is 0\n");
717 if (g_bond
->verbose
) {
718 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
720 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
721 if (g_bond
->verbose
) {
722 printf("ifbond_release(%s) removing multicast\n",
725 (void) if_delmulti_anon(ifb
->ifb_ifma_slow_proto
->ifma_ifp
,
726 ifb
->ifb_ifma_slow_proto
->ifma_addr
);
727 IFMA_REMREF(ifb
->ifb_ifma_slow_proto
);
729 if (ifb
->ifb_distributing_array
!= NULL
) {
730 FREE(ifb
->ifb_distributing_array
, M_BOND
);
741 * Function: ifbond_wait
743 * Allows a single thread to gain exclusive access to the ifbond
744 * data structure. Some operations take a long time to complete,
745 * and some have side-effects that we can't predict. Holding the
746 * bond_lock() across such operations is not possible.
749 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
750 * complete. Simply holding the bond_lock() would freeze all other
751 * data structure accesses during that time.
752 * 2) When we attach our protocol to the interface, a dlil event is
753 * generated and invokes our bond_event() function. bond_event()
754 * needs to take the bond_lock(), but we're already holding it, so
755 * we're deadlocked against ourselves.
757 * Before calling, you must be holding the bond_lock and have taken
758 * a reference on the ifbond_ref.
761 ifbond_wait(ifbond_ref ifb
, const char * msg
)
765 /* other add/remove in progress */
766 while (ifbond_flags_change_in_progress(ifb
)) {
767 if (g_bond
->verbose
) {
768 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
771 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
773 /* prevent other bond list remove/add from taking place */
774 ifbond_flags_set_change_in_progress(ifb
);
775 if (g_bond
->verbose
&& waited
) {
776 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
782 * Function: ifbond_signal
784 * Allows the thread that previously invoked ifbond_wait() to
785 * give up exclusive access to the ifbond data structure, and wake up
786 * any other threads waiting to access
788 * Before calling, you must be holding the bond_lock and have taken
789 * a reference on the ifbond_ref.
792 ifbond_signal(ifbond_ref ifb
, const char * msg
)
794 ifbond_flags_clear_change_in_progress(ifb
);
795 wakeup((caddr_t
)ifb
);
796 if (g_bond
->verbose
) {
797 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
807 link_speed(int active
)
809 switch (IFM_SUBTYPE(active
)) {
830 /* assume that new defined types are going to be at least 10GigE */
837 static __inline__
int
838 media_active(const struct media_info
* mi
)
840 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
843 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
846 static __inline__
int
847 media_full_duplex(const struct media_info
* mi
)
849 return ((mi
->mi_active
& IFM_FDX
) != 0);
852 static __inline__
int
853 media_speed(const struct media_info
* mi
)
855 return (link_speed(mi
->mi_active
));
858 static struct media_info
859 interface_media_info(struct ifnet
* ifp
)
861 struct ifmediareq ifmr
;
862 struct media_info mi
;
864 bzero(&mi
, sizeof(mi
));
865 bzero(&ifmr
, sizeof(ifmr
));
866 if (ifnet_ioctl(ifp
, 0, SIOCGIFMEDIA
, &ifmr
) == 0) {
867 if (ifmr
.ifm_count
!= 0) {
868 mi
.mi_status
= ifmr
.ifm_status
;
869 mi
.mi_active
= ifmr
.ifm_active
;
876 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
881 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
882 * currently expects it that way
884 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
885 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
886 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
887 return (ifnet_ioctl(ifp
, 0, SIOCSIFLLADDR
, &ifr
));
893 static bond_globals_ref
894 bond_globals_create(lacp_system_priority sys_pri
,
899 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
);
903 bzero(b
, sizeof(*b
));
904 TAILQ_INIT(&b
->ifbond_list
);
906 b
->system_priority
= sys_pri
;
911 bond_globals_init(void)
917 bond_assert_lock_not_held();
919 if (g_bond
!= NULL
) {
924 * use en0's ethernet address as the system identifier, and if it's not
925 * there, use en1 .. en3
928 for (i
= 0; i
< 4; i
++) {
929 char ifname
[IFNAMSIZ
+1];
930 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
931 ifp
= ifunit(ifname
);
938 b
= bond_globals_create(0x8000, (lacp_system_ref
)ifnet_lladdr(ifp
));
941 if (g_bond
!= NULL
) {
958 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
959 const struct ether_header
* eh_p
,
960 u_int16_t vlan_tag
, bpf_packet_func func
)
962 struct ether_vlan_header
* vlh_p
;
965 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
969 /* populate a new mbuf containing the vlan ethernet header */
970 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
971 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
972 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
973 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
974 vlh_p
->evl_tag
= htons(vlan_tag
);
975 vlh_p
->evl_proto
= eh_p
->ether_type
;
983 static __inline__
void
984 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
985 bpf_packet_func func
)
988 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
989 const struct ether_header
* eh_p
;
990 eh_p
= mtod(m
, const struct ether_header
*);
991 m
->m_data
+= ETHER_HDR_LEN
;
992 m
->m_len
-= ETHER_HDR_LEN
;
993 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
994 m
->m_data
-= ETHER_HDR_LEN
;
995 m
->m_len
+= ETHER_HDR_LEN
;
1003 static __inline__
void
1004 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
1005 bpf_packet_func func
)
1008 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1009 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1011 /* restore the header */
1012 m
->m_data
-= ETHER_HDR_LEN
;
1013 m
->m_len
+= ETHER_HDR_LEN
;
1015 m
->m_data
+= ETHER_HDR_LEN
;
1016 m
->m_len
-= ETHER_HDR_LEN
;
1023 * Function: bond_setmulti
1025 * Enable multicast reception on "our" interface by enabling multicasts on
1026 * each of the member ports.
1029 bond_setmulti(struct ifnet
* ifp
)
1037 ifb
= ifnet_softc(ifp
);
1038 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1039 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1044 ifbond_wait(ifb
, "bond_setmulti");
1046 if (ifbond_flags_if_detaching(ifb
)) {
1047 /* someone destroyed the bond while we were waiting */
1053 /* ifbond_wait() let's us safely walk the list without holding the lock */
1054 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1055 struct ifnet
* port_ifp
= p
->po_ifp
;
1057 error
= multicast_list_program(&p
->po_multicast
,
1060 printf("bond_setmulti(%s): "
1061 "multicast_list_program(%s%d) failed, %d\n",
1062 ifb
->ifb_name
, ifnet_name(port_ifp
),
1063 ifnet_unit(port_ifp
), error
);
1069 ifbond_signal(ifb
, "bond_setmulti");
1071 ifbond_release(ifb
);
1076 bond_clone_attach(void)
1080 if ((error
= if_clone_attach(&bond_cloner
)) != 0)
1087 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1090 struct ifmultiaddr
* ifma
= NULL
;
1091 struct sockaddr_dl sdl
;
1093 bond_assert_lock_not_held();
1095 bzero(&sdl
, sizeof(sdl
));
1096 sdl
.sdl_len
= sizeof(sdl
);
1097 sdl
.sdl_family
= AF_LINK
;
1098 sdl
.sdl_type
= IFT_ETHER
;
1100 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1101 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1102 error
= if_addmulti_anon(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
, &ifma
);
1104 ifb
->ifb_ifma_slow_proto
= ifma
;
1110 bond_clone_create(struct if_clone
* ifc
, u_int32_t unit
, __unused
void *params
)
1115 struct ifnet_init_params bond_init
;
1117 error
= bond_globals_init();
1122 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
);
1126 bzero(ifb
, sizeof(*ifb
));
1129 TAILQ_INIT(&ifb
->ifb_port_list
);
1130 TAILQ_INIT(&ifb
->ifb_lag_list
);
1131 ifb
->ifb_key
= unit
+ 1;
1133 /* use the interface name as the unique id for ifp recycle */
1134 if ((u_int32_t
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1135 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1136 ifbond_release(ifb
);
1140 bzero(&bond_init
, sizeof(bond_init
));
1141 bond_init
.uniqueid
= ifb
->ifb_name
;
1142 bond_init
.uniqueid_len
= strlen(ifb
->ifb_name
);
1143 bond_init
.name
= ifc
->ifc_name
;
1144 bond_init
.unit
= unit
;
1145 bond_init
.family
= IFNET_FAMILY_BOND
;
1146 bond_init
.type
= IFT_IEEE8023ADLAG
;
1147 bond_init
.output
= bond_output
;
1148 bond_init
.demux
= ether_demux
;
1149 bond_init
.add_proto
= ether_add_proto
;
1150 bond_init
.del_proto
= ether_del_proto
;
1151 bond_init
.check_multi
= ether_check_multi
;
1152 bond_init
.framer
= ether_frameout
;
1153 bond_init
.ioctl
= bond_ioctl
;
1154 bond_init
.set_bpf_tap
= bond_set_bpf_tap
;
1155 bond_init
.detach
= bond_if_free
;
1156 bond_init
.broadcast_addr
= etherbroadcastaddr
;
1157 bond_init
.broadcast_len
= ETHER_ADDR_LEN
;
1158 bond_init
.softc
= ifb
;
1159 error
= ifnet_allocate(&bond_init
, &ifp
);
1162 ifbond_release(ifb
);
1167 ifnet_set_offload(ifp
, 0);
1168 ifnet_set_addrlen(ifp
, ETHER_ADDR_LEN
); /* XXX ethernet specific */
1169 ifnet_set_flags(ifp
, IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
, 0xffff);
1170 ifnet_set_baudrate(ifp
, 0);
1171 ifnet_set_mtu(ifp
, 0);
1173 error
= ifnet_attach(ifp
, NULL
);
1176 ifbond_release(ifb
);
1179 error
= ifbond_add_slow_proto_multicast(ifb
);
1181 printf("bond_clone_create(%s): "
1182 "failed to add slow_proto multicast, %d\n",
1183 ifb
->ifb_name
, error
);
1186 /* attach as ethernet */
1187 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1190 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1197 bond_remove_all_interfaces(ifbond_ref ifb
)
1201 bond_assert_lock_held();
1204 * do this in reverse order to avoid re-programming the mac address
1205 * as each head interface is removed
1207 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1208 bond_remove_interface(ifb
, p
->po_ifp
);
1214 bond_remove(ifbond_ref ifb
)
1216 bond_assert_lock_held();
1217 ifbond_flags_set_if_detaching(ifb
);
1218 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1219 bond_remove_all_interfaces(ifb
);
1224 bond_if_detach(struct ifnet
* ifp
)
1228 error
= ifnet_detach(ifp
);
1230 printf("bond_if_detach %s%d: ifnet_detach failed, %d\n",
1231 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
1238 bond_clone_destroy(struct ifnet
* ifp
)
1243 ifb
= ifnet_softc(ifp
);
1244 if (ifb
== NULL
|| ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
1248 if (ifbond_flags_if_detaching(ifb
)) {
1254 bond_if_detach(ifp
);
1259 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1264 ifb
= ifnet_softc(ifp
);
1265 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1270 case BPF_TAP_DISABLE
:
1271 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1275 ifb
->ifb_bpf_input
= func
;
1278 case BPF_TAP_OUTPUT
:
1279 ifb
->ifb_bpf_output
= func
;
1282 case BPF_TAP_INPUT_OUTPUT
:
1283 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1293 ether_header_hash(struct ether_header
* eh_p
)
1297 /* get 32-bits from destination ether and ether type */
1298 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1300 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1304 static struct mbuf
*
1305 S_mbuf_skip_to_offset(struct mbuf
* m
, int32_t * offset
)
1310 while (*offset
>= len
) {
1321 #if BYTE_ORDER == BIG_ENDIAN
1322 static __inline__
uint32_t
1323 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1325 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1326 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1328 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1329 static __inline__
uint32_t
1330 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1332 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1333 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1335 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1338 S_mbuf_copy_uint32(struct mbuf
* m
, int32_t offset
, uint32_t * val
)
1340 struct mbuf
* current
;
1341 u_char
* current_data
;
1346 current
= S_mbuf_skip_to_offset(m
, &offset
);
1347 if (current
== NULL
) {
1350 current_data
= mtod(current
, u_char
*) + offset
;
1351 space_current
= current
->m_len
- offset
;
1352 if (space_current
>= (int)sizeof(uint32_t)) {
1353 *val
= *((uint32_t *)current_data
);
1356 next
= current
->m_next
;
1357 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1360 next_data
= mtod(next
, u_char
*);
1361 switch (space_current
) {
1363 *val
= make_uint32(current_data
[0], next_data
[0],
1364 next_data
[1], next_data
[2]);
1367 *val
= make_uint32(current_data
[0], current_data
[1],
1368 next_data
[0], next_data
[1]);
1371 *val
= make_uint32(current_data
[0], current_data
[1],
1372 current_data
[2], next_data
[0]);
1378 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1379 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1382 ip_header_hash(struct mbuf
* m
)
1385 struct in_addr ip_dst
;
1386 struct in_addr ip_src
;
1389 struct mbuf
* orig_m
= m
;
1391 /* find the IP protocol field relative to the start of the packet */
1392 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1393 m
= S_mbuf_skip_to_offset(m
, &offset
);
1394 if (m
== NULL
|| m
->m_len
< 1) {
1397 data
= mtod(m
, u_char
*) + offset
;
1400 /* find the IP src relative to the IP protocol */
1401 if ((m
->m_len
- offset
)
1402 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1403 /* this should be the normal case */
1404 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1405 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1408 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1409 (uint32_t *)&ip_src
.s_addr
)) {
1412 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1413 (uint32_t *)&ip_dst
.s_addr
)) {
1417 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1420 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1423 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1425 ipv6_header_hash(struct mbuf
* m
)
1430 struct mbuf
* orig_m
= m
;
1434 /* find the IP protocol field relative to the start of the packet */
1435 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1436 m
= S_mbuf_skip_to_offset(m
, &offset
);
1438 goto bad_ipv6_packet
;
1440 data
= mtod(m
, u_char
*) + offset
;
1442 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1443 /* this should be the normal case */
1444 for (i
= 0, scan
= (uint32_t *)data
;
1445 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1451 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1453 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1454 (uint32_t *)&tmp
)) {
1455 goto bad_ipv6_packet
;
1460 return (ntohl(val
));
1463 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1467 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1469 bpf_packet_func bpf_func
;
1472 struct ifnet
* port_ifp
= NULL
;
1477 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1481 if (m
->m_pkthdr
.socket_id
!= 0) {
1482 h
= m
->m_pkthdr
.socket_id
;
1485 struct ether_header
* eh_p
;
1487 eh_p
= mtod(m
, struct ether_header
*);
1488 switch (ntohs(eh_p
->ether_type
)) {
1490 h
= ip_header_hash(m
);
1492 case ETHERTYPE_IPV6
:
1493 h
= ipv6_header_hash(m
);
1496 h
= ether_header_hash(eh_p
);
1501 ifb
= ifnet_softc(ifp
);
1502 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1503 || ifb
->ifb_distributing_count
== 0) {
1506 h
%= ifb
->ifb_distributing_count
;
1507 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1508 bpf_func
= ifb
->ifb_bpf_output
;
1511 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1512 (void)ifnet_stat_increment_out(ifp
, 1,
1513 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1516 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1518 bond_bpf_output(ifp
, m
, bpf_func
);
1520 return (ifnet_output_raw(port_ifp
, PF_BOND
, m
));
1529 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1532 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1533 if (p
->po_ifp
== port_ifp
) {
1541 bond_lookup_port(struct ifnet
* port_ifp
)
1546 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1547 port
= ifbond_lookup_port(ifb
, port_ifp
);
1556 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1558 struct ifnet
* bond_ifp
= NULL
;
1564 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1567 p
= bond_lookup_port(port_ifp
);
1571 if (p
->po_enabled
== 0) {
1575 if (ifb
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1578 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1579 if (ifbond_selection(ifb
)) {
1580 event_code
= (ifb
->ifb_active_lag
== NULL
)
1583 /* XXX need to take a reference on bond_ifp */
1584 bond_ifp
= ifb
->ifb_ifp
;
1585 ifb
->ifb_last_link_event
= event_code
;
1588 event_code
= (ifb
->ifb_active_lag
== NULL
)
1591 if (event_code
!= ifb
->ifb_last_link_event
) {
1592 if (g_bond
->verbose
) {
1593 timestamp_printf("%s: (receive) generating LINK event\n",
1596 bond_ifp
= ifb
->ifb_ifp
;
1597 ifb
->ifb_last_link_event
= event_code
;
1603 if (bond_ifp
!= NULL
) {
1604 interface_link_event(bond_ifp
, event_code
);
1611 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1613 la_marker_pdu_ref marker_p
;
1616 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1617 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1621 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1625 p
= bond_lookup_port(port_ifp
);
1626 if (p
== NULL
|| p
->po_enabled
== 0
1627 || p
->po_bond
->ifb_mode
!= IF_BOND_MODE_LACP
) {
1631 /* echo back the same packet as a marker response */
1632 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1633 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1643 bond_input(ifnet_t port_ifp
, __unused protocol_family_t protocol
, mbuf_t m
,
1644 char * frame_header
)
1646 bpf_packet_func bpf_func
;
1647 const struct ether_header
* eh_p
;
1652 eh_p
= (const struct ether_header
*)frame_header
;
1653 if ((m
->m_flags
& M_MCAST
) != 0
1654 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1655 sizeof(eh_p
->ether_dhost
)) == 0
1656 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1657 u_char subtype
= *mtod(m
, u_char
*);
1659 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1660 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1665 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1666 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1671 bond_receive_lacpdu(m
, port_ifp
);
1674 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1677 /* restore the ethernet header pointer in the mbuf */
1678 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1679 m
->m_data
-= ETHER_HDR_LEN
;
1680 m
->m_len
+= ETHER_HDR_LEN
;
1681 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1682 if (m
->m_pkthdr
.len
< min_size
) {
1687 if (m
->m_len
< min_size
) {
1688 m
= m_pullup(m
, min_size
);
1693 /* send to marker responder */
1694 bond_receive_la_marker_pdu(m
, port_ifp
);
1697 else if (subtype
== 0
1698 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1699 /* invalid subtype, discard the frame */
1705 if ((ifnet_eflags(port_ifp
) & IFEF_BOND
) == 0) {
1708 p
= bond_lookup_port(port_ifp
);
1709 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1713 /* make the packet appear as if it arrived on the bonded interface */
1716 bpf_func
= ifb
->ifb_bpf_input
;
1719 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1720 (void)ifnet_stat_increment_in(ifp
, 1,
1721 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1722 + ETHER_VLAN_ENCAP_LEN
), 0);
1725 (void)ifnet_stat_increment_in(ifp
, 1,
1726 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1728 m
->m_pkthdr
.rcvif
= ifp
;
1729 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1730 m
->m_pkthdr
.header
= frame_header
;
1731 dlil_input_packet_list(ifp
, m
);
1740 static __inline__
const char *
1741 bondport_get_name(bondport_ref p
)
1743 return (p
->po_name
);
1746 static __inline__
int
1747 bondport_get_index(bondport_ref p
)
1749 return (ifnet_index(p
->po_ifp
));
1753 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1755 struct ether_header
* eh_p
;
1758 /* packet_buffer_allocate leaves room for ethernet header */
1759 eh_p
= mtod(buf
, struct ether_header
*);
1760 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1761 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1762 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1763 error
= ifnet_output_raw(p
->po_ifp
, PF_BOND
, buf
);
1765 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1766 bondport_get_name(p
), error
);
1772 bondport_timer_process_func(devtimer_ref timer
,
1773 devtimer_process_func_event event
)
1778 case devtimer_process_func_event_lock
:
1780 devtimer_retain(timer
);
1782 case devtimer_process_func_event_unlock
:
1783 if (devtimer_valid(timer
)) {
1784 /* as long as the devtimer is valid, we can look at arg0 */
1786 struct ifnet
* bond_ifp
= NULL
;
1788 p
= (bondport_ref
)devtimer_arg0(timer
);
1789 if (ifbond_selection(p
->po_bond
)) {
1790 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1793 /* XXX need to take a reference on bond_ifp */
1794 bond_ifp
= p
->po_bond
->ifb_ifp
;
1795 p
->po_bond
->ifb_last_link_event
= event_code
;
1798 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1801 if (event_code
!= p
->po_bond
->ifb_last_link_event
) {
1802 if (g_bond
->verbose
) {
1803 timestamp_printf("%s: (timer) generating LINK event\n",
1804 p
->po_bond
->ifb_name
);
1806 bond_ifp
= p
->po_bond
->ifb_ifp
;
1807 p
->po_bond
->ifb_last_link_event
= event_code
;
1810 devtimer_release(timer
);
1812 if (bond_ifp
!= NULL
) {
1813 interface_link_event(bond_ifp
, event_code
);
1817 /* timer is going away */
1818 devtimer_release(timer
);
1828 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1829 int active
, int short_timeout
, int * ret_error
)
1832 bondport_ref p
= NULL
;
1833 lacp_actor_partner_state s
;
1836 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
);
1838 *ret_error
= ENOMEM
;
1841 bzero(p
, sizeof(*p
));
1842 multicast_list_init(&p
->po_multicast
);
1843 if ((u_int32_t
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1844 ifnet_name(port_ifp
), ifnet_unit(port_ifp
))
1845 >= sizeof(p
->po_name
)) {
1846 printf("if_bond: name too large\n");
1847 *ret_error
= EINVAL
;
1850 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1852 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1853 bondport_get_name(p
), error
);
1856 /* remember the current interface MTU so it can be restored */
1857 p
->po_devmtu
.ifdm_current
= ifnet_mtu(port_ifp
);
1858 p
->po_ifp
= port_ifp
;
1859 p
->po_media_info
= interface_media_info(port_ifp
);
1860 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1861 if (p
->po_current_while_timer
== NULL
) {
1862 *ret_error
= ENOMEM
;
1865 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1866 if (p
->po_periodic_timer
== NULL
) {
1867 *ret_error
= ENOMEM
;
1870 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1871 if (p
->po_wait_while_timer
== NULL
) {
1872 *ret_error
= ENOMEM
;
1875 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1876 if (p
->po_transmit_timer
== NULL
) {
1877 *ret_error
= ENOMEM
;
1880 p
->po_receive_state
= ReceiveState_none
;
1881 p
->po_mux_state
= MuxState_none
;
1882 p
->po_priority
= priority
;
1884 s
= lacp_actor_partner_state_set_aggregatable(s
);
1885 if (short_timeout
) {
1886 s
= lacp_actor_partner_state_set_short_timeout(s
);
1889 s
= lacp_actor_partner_state_set_active_lacp(s
);
1891 p
->po_actor_state
= s
;
1900 bondport_start(bondport_ref p
)
1902 bondport_receive_machine(p
, LAEventStart
, NULL
);
1903 bondport_mux_machine(p
, LAEventStart
, NULL
);
1904 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1905 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1910 * Function: bondport_invalidate_timers
1912 * Invalidate all of the timers for the bondport.
1915 bondport_invalidate_timers(bondport_ref p
)
1917 devtimer_invalidate(p
->po_current_while_timer
);
1918 devtimer_invalidate(p
->po_periodic_timer
);
1919 devtimer_invalidate(p
->po_wait_while_timer
);
1920 devtimer_invalidate(p
->po_transmit_timer
);
1924 * Function: bondport_cancel_timers
1926 * Cancel all of the timers for the bondport.
1929 bondport_cancel_timers(bondport_ref p
)
1931 devtimer_cancel(p
->po_current_while_timer
);
1932 devtimer_cancel(p
->po_periodic_timer
);
1933 devtimer_cancel(p
->po_wait_while_timer
);
1934 devtimer_cancel(p
->po_transmit_timer
);
1938 bondport_free(bondport_ref p
)
1940 multicast_list_remove(&p
->po_multicast
);
1941 devtimer_release(p
->po_current_while_timer
);
1942 devtimer_release(p
->po_periodic_timer
);
1943 devtimer_release(p
->po_wait_while_timer
);
1944 devtimer_release(p
->po_transmit_timer
);
1949 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1950 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1951 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1952 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1954 static __inline__
int
1955 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1957 return (((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
1958 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
);
1962 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1969 bondport_ref
* new_array
= NULL
;
1970 bondport_ref
* old_array
= NULL
;
1974 /* pre-allocate space for new port */
1975 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1980 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
1981 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1984 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1987 /* make sure this interface can handle our current MTU */
1988 devmtu
= bond_device_mtu(ifp
, ifb
);
1990 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
1992 printf("if_bond: interface %s doesn't support mtu %d",
1993 bondport_get_name(p
), devmtu
);
1998 /* make sure ifb doesn't get de-allocated while we wait */
2001 /* wait for other add or remove to complete */
2002 ifbond_wait(ifb
, "bond_add_interface");
2004 if (ifbond_flags_if_detaching(ifb
)) {
2005 /* someone destroyed the bond while we were waiting */
2009 if (bond_lookup_port(port_ifp
) != NULL
) {
2010 /* port is already part of a bond */
2014 ifnet_lock_exclusive(port_ifp
);
2015 if ((ifnet_eflags(port_ifp
) & (IFEF_VLAN
| IFEF_BOND
)) != 0) {
2016 /* interface already has VLAN's, or is part of bond */
2017 ifnet_lock_done(port_ifp
);
2022 /* mark the interface busy */
2023 /* can't use ifnet_set_eflags because that takes the lock */
2024 port_ifp
->if_eflags
|= IFEF_BOND
;
2025 ifnet_lock_done(port_ifp
);
2027 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2028 ifnet_set_offload(ifp
, ifnet_offload(port_ifp
));
2029 ifnet_set_flags(ifp
, IFF_RUNNING
, IFF_RUNNING
);
2030 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2034 ifnet_offload_t ifp_offload
;
2035 ifnet_offload_t port_ifp_offload
;
2037 ifp_offload
= ifnet_offload(ifp
);
2038 port_ifp_offload
= ifnet_offload(port_ifp
);
2039 if (ifp_offload
!= port_ifp_offload
) {
2040 ifnet_offload_t offload
;
2042 offload
= ifp_offload
& port_ifp_offload
;
2043 printf("bond_add_interface(%s, %s) "
2044 "hwassist values don't match 0x%x != 0x%x, using 0x%x instead\n",
2045 ifb
->ifb_name
, bondport_get_name(p
),
2046 ifp_offload
, port_ifp_offload
, offload
);
2049 * if the bond has VLAN's, we can't simply change the hwassist
2050 * field behind its back: this needs work
2052 ifnet_set_offload(ifp
, offload
);
2057 /* remember the port's ethernet address so it can be restored */
2058 ether_addr_copy(&p
->po_saved_addr
, ifnet_lladdr(port_ifp
));
2060 /* add it to the list of ports */
2061 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2062 ifb
->ifb_port_count
++;
2064 /* set the default MTU */
2065 if (ifnet_mtu(ifp
) == 0) {
2066 ifnet_set_mtu(ifp
, ETHERMTU
);
2071 /* first port added to bond determines bond's ethernet address */
2073 ifnet_set_lladdr_and_type(ifp
, ifnet_lladdr(port_ifp
), ETHER_ADDR_LEN
,
2077 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2079 /* allocate a larger distributing array */
2080 new_array
= (bondport_ref
*)
2081 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2082 if (new_array
== NULL
) {
2087 /* attach our BOND "protocol" to the interface */
2088 error
= bond_attach_protocol(port_ifp
);
2092 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2094 /* set the interface MTU */
2095 devmtu
= bond_device_mtu(ifp
, ifb
);
2096 error
= siocsifmtu(port_ifp
, devmtu
);
2098 printf("bond_add_interface(%s, %s):"
2099 " SIOCSIFMTU %d failed %d\n",
2100 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2103 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2105 /* program the port with our multicast addresses */
2106 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2108 printf("bond_add_interface(%s, %s):"
2109 " multicast_list_program failed %d\n",
2110 ifb
->ifb_name
, bondport_get_name(p
), error
);
2114 /* mark the interface up */
2115 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2117 error
= ifnet_ioctl(port_ifp
, 0, SIOCSIFFLAGS
, NULL
);
2119 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2120 ifb
->ifb_name
, bondport_get_name(p
), error
);
2124 /* re-program the port's ethernet address */
2125 error
= if_siflladdr(port_ifp
,
2126 (const struct ether_addr
*)ifnet_lladdr(ifp
));
2128 /* port doesn't support setting the link address */
2129 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2130 ifb
->ifb_name
, bondport_get_name(p
), error
);
2133 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2137 /* no failures past this point */
2140 /* copy the contents of the existing distributing array */
2141 if (ifb
->ifb_distributing_count
) {
2142 bcopy(ifb
->ifb_distributing_array
, new_array
,
2143 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2145 old_array
= ifb
->ifb_distributing_array
;
2146 ifb
->ifb_distributing_array
= new_array
;
2148 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2151 /* check if we need to generate a link status event */
2152 if (ifbond_selection(ifb
)) {
2153 event_code
= (ifb
->ifb_active_lag
== NULL
)
2156 ifb
->ifb_last_link_event
= event_code
;
2160 /* are we adding the first distributing interface? */
2161 if (media_active(&p
->po_media_info
)) {
2162 if (ifb
->ifb_distributing_count
== 0) {
2163 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_ON
;
2165 bondport_enable_distributing(p
);
2168 bondport_disable_distributing(p
);
2171 /* clear the busy state, and wakeup anyone waiting */
2172 ifbond_signal(ifb
, "bond_add_interface");
2174 if (event_code
!= 0) {
2175 interface_link_event(ifp
, event_code
);
2177 if (old_array
!= NULL
) {
2178 FREE(old_array
, M_BOND
);
2183 bond_assert_lock_not_held();
2185 /* if this was the first port to be added, clear our address */
2187 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2190 if (new_array
!= NULL
) {
2191 FREE(new_array
, M_BOND
);
2193 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2196 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2198 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2199 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2202 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2203 (void)bond_detach_protocol(port_ifp
);
2205 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2208 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2210 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2211 ifb
->ifb_name
, bondport_get_name(p
),
2212 p
->po_devmtu
.ifdm_current
, error1
);
2216 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2217 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2218 ifb
->ifb_port_count
--;
2220 ifnet_set_eflags(ifp
, 0, IFEF_BOND
);
2221 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2222 ifb
->ifb_altmtu
= 0;
2223 ifnet_set_mtu(ifp
, 0);
2224 ifnet_set_offload(ifp
, 0);
2228 ifbond_signal(ifb
, "bond_add_interface");
2230 ifbond_release(ifb
);
2236 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2241 bondport_ref head_port
;
2244 int new_link_address
= FALSE
;
2246 lacp_actor_partner_state s
;
2247 int was_distributing
;
2249 bond_assert_lock_held();
2252 ifbond_wait(ifb
, "bond_remove_interface");
2254 p
= ifbond_lookup_port(ifb
, port_ifp
);
2257 /* it got removed by another thread */
2261 /* de-select it and remove it from the lists */
2262 was_distributing
= bondport_flags_distributing(p
);
2263 bondport_disable_distributing(p
);
2264 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2265 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2266 active_lag
= bondport_remove_from_LAG(p
);
2267 /* invalidate timers here while holding the bond_lock */
2268 bondport_invalidate_timers(p
);
2270 /* announce that we're Individual now */
2271 s
= p
->po_actor_state
;
2272 s
= lacp_actor_partner_state_set_individual(s
);
2273 s
= lacp_actor_partner_state_set_not_collecting(s
);
2274 s
= lacp_actor_partner_state_set_not_distributing(s
);
2275 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2276 p
->po_actor_state
= s
;
2277 bondport_flags_set_ntt(p
);
2280 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2281 ifb
->ifb_port_count
--;
2284 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2285 if (head_port
== NULL
) {
2286 ifnet_set_flags(ifp
, 0, IFF_RUNNING
);
2287 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2290 ifnet_set_offload(ifp
, 0);
2291 ifnet_set_mtu(ifp
, 0);
2292 ifb
->ifb_altmtu
= 0;
2293 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2294 && bcmp(&p
->po_saved_addr
, ifnet_lladdr(ifp
),
2295 ETHER_ADDR_LEN
) == 0) {
2296 new_link_address
= TRUE
;
2298 /* check if we need to generate a link status event */
2299 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2300 if (ifbond_selection(ifb
) || active_lag
) {
2301 event_code
= (ifb
->ifb_active_lag
== NULL
)
2304 ifb
->ifb_last_link_event
= event_code
;
2306 bondport_transmit_machine(p
, LAEventStart
,
2307 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2310 /* are we removing the last distributing interface? */
2311 if (was_distributing
&& ifb
->ifb_distributing_count
== 0) {
2312 ifb
->ifb_last_link_event
= event_code
= KEV_DL_LINK_OFF
;
2319 ifnet_set_lladdr_and_type(ifp
, NULL
, 0, IFT_IEEE8023ADLAG
);
2321 else if (new_link_address
) {
2322 struct ifnet
* scan_ifp
;
2323 bondport_ref scan_port
;
2325 /* ifbond_wait() allows port list traversal without holding the lock */
2327 /* this port gave the bond its ethernet address, switch to new one */
2328 ifnet_set_lladdr_and_type(ifp
,
2329 &head_port
->po_saved_addr
, ETHER_ADDR_LEN
,
2332 /* re-program each port with the new link address */
2333 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2334 scan_ifp
= scan_port
->po_ifp
;
2336 error
= if_siflladdr(scan_ifp
,
2337 (const struct ether_addr
*) ifnet_lladdr(ifp
));
2339 printf("bond_remove_interface(%s, %s): "
2340 "if_siflladdr (%s) failed %d\n",
2341 ifb
->ifb_name
, bondport_get_name(p
),
2342 bondport_get_name(scan_port
), error
);
2347 /* restore the port's ethernet address */
2348 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2350 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2351 ifb
->ifb_name
, bondport_get_name(p
), error
);
2354 /* restore the port's MTU */
2355 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2357 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2358 ifb
->ifb_name
, bondport_get_name(p
),
2359 p
->po_devmtu
.ifdm_current
, error
);
2362 /* remove the bond "protocol" */
2363 bond_detach_protocol(port_ifp
);
2365 /* generate link event */
2366 if (event_code
!= 0) {
2367 interface_link_event(ifp
, event_code
);
2372 ifnet_set_eflags(port_ifp
, 0, IFEF_BOND
);
2373 /* release this bondport's reference to the ifbond */
2374 ifbond_release(ifb
);
2377 ifbond_signal(ifb
, "bond_remove_interface");
2378 ifbond_release(ifb
);
2383 bond_set_lacp_mode(ifbond_ref ifb
)
2387 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2388 bondport_disable_distributing(p
);
2395 bond_set_static_mode(ifbond_ref ifb
)
2398 lacp_actor_partner_state s
;
2400 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2401 bondport_disable_distributing(p
);
2402 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2403 (void)bondport_remove_from_LAG(p
);
2404 bondport_cancel_timers(p
);
2406 /* announce that we're Individual now */
2407 s
= p
->po_actor_state
;
2408 s
= lacp_actor_partner_state_set_individual(s
);
2409 s
= lacp_actor_partner_state_set_not_collecting(s
);
2410 s
= lacp_actor_partner_state_set_not_distributing(s
);
2411 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2412 p
->po_actor_state
= s
;
2413 bondport_flags_set_ntt(p
);
2414 bondport_transmit_machine(p
, LAEventStart
,
2415 TRANSMIT_MACHINE_TX_IMMEDIATE
);
2417 p
->po_actor_state
= 0;
2418 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
2420 if (media_active(&p
->po_media_info
)) {
2421 bondport_enable_distributing(p
);
2424 bondport_disable_distributing(p
);
2431 bond_set_mode(struct ifnet
* ifp
, int mode
)
2438 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2439 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2441 return ((ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
);
2443 if (ifb
->ifb_mode
== mode
) {
2449 ifbond_wait(ifb
, "bond_set_mode");
2451 /* verify (again) that the mode is actually different */
2452 if (ifb
->ifb_mode
== mode
) {
2457 ifb
->ifb_mode
= mode
;
2458 if (mode
== IF_BOND_MODE_LACP
) {
2459 bond_set_lacp_mode(ifb
);
2461 /* check if we need to generate a link status event */
2462 if (ifbond_selection(ifb
)) {
2463 event_code
= (ifb
->ifb_active_lag
== NULL
)
2468 bond_set_static_mode(ifb
);
2469 event_code
= (ifb
->ifb_distributing_count
== 0)
2473 ifb
->ifb_last_link_event
= event_code
;
2476 ifbond_signal(ifb
, "bond_set_mode");
2478 ifbond_release(ifb
);
2480 if (event_code
!= 0) {
2481 interface_link_event(ifp
, event_code
);
2487 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2492 struct if_bond_status_req
* ibsr
;
2493 struct if_bond_status ibs
;
2496 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2497 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2500 ibsr
->ibsr_key
= ifb
->ifb_key
;
2501 ibsr
->ibsr_mode
= ifb
->ifb_mode
;
2502 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2503 dst
= proc_is64bit(current_proc())
2504 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2505 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer
);
2506 if (dst
== USER_ADDR_NULL
) {
2507 /* just want to know how many there are */
2510 if (ibsr
->ibsr_count
< 0) {
2513 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2514 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2515 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2516 struct if_bond_partner_state
* ibps_p
;
2517 partner_state_ref ps
;
2522 bzero(&ibs
, sizeof(ibs
));
2523 strncpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2524 ibs
.ibs_port_priority
= port
->po_priority
;
2525 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2526 ibs
.ibs_state
= port
->po_actor_state
;
2527 ibs
.ibs_selected_state
= port
->po_selected
;
2528 ps
= &port
->po_partner_state
;
2529 ibps_p
= &ibs
.ibs_partner_state
;
2530 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2531 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2532 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2533 ibps_p
->ibps_port
= ps
->ps_port
;
2534 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2535 ibps_p
->ibps_state
= ps
->ps_state
;
2538 /* fake the selected information */
2539 ibs
.ibs_selected_state
= bondport_flags_distributing(port
)
2540 ? SelectedState_SELECTED
: SelectedState_UNSELECTED
;
2542 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2552 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2555 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2561 bond_set_promisc(__unused
struct ifnet
*ifp
)
2565 * The benefit of doing this currently does not warrant
2566 * the added code complexity. Do nothing and return.
2572 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2578 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2579 mtu_min
= IF_MINMTU
;
2581 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2582 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2584 if (devmtu_p
->ifdm_min
> mtu_min
) {
2585 mtu_min
= devmtu_p
->ifdm_min
;
2587 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2588 mtu_max
= devmtu_p
->ifdm_max
;
2597 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2602 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2603 error
= siocsifmtu(p
->po_ifp
, mtu
);
2605 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2606 ifb
->ifb_name
, bondport_get_name(p
), error
);
2614 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2624 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2625 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2626 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2630 ifbond_wait(ifb
, "bond_set_mtu");
2633 if (ifnet_softc(ifp
) == NULL
|| ifbond_flags_if_detaching(ifb
)) {
2637 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2638 if (mtu
> mtu_max
) {
2642 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2643 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2648 new_max
= (mtu
> (int)ifnet_mtu(ifp
)) ? mtu
: (int)ifnet_mtu(ifp
);
2651 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2653 old_max
= ((int)ifnet_mtu(ifp
) > ifb
->ifb_altmtu
)
2654 ? (int)ifnet_mtu(ifp
) : ifb
->ifb_altmtu
;
2655 if (new_max
!= old_max
) {
2656 /* we can safely walk the list of port without the lock held */
2658 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2660 /* try our best to back out of it */
2661 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2667 ifb
->ifb_altmtu
= mtu
;
2670 ifnet_set_mtu(ifp
, mtu
);
2675 ifbond_signal(ifb
, "bond_set_mtu");
2676 ifbond_release(ifb
);
2684 bond_ioctl(struct ifnet
*ifp
, u_long cmd
, void * data
)
2687 struct if_bond_req ibr
;
2688 struct ifaddr
* ifa
;
2691 struct ifmediareq
*ifmr
;
2692 struct ifnet
* port_ifp
= NULL
;
2693 user_addr_t user_addr
;
2695 if (ifnet_type(ifp
) != IFT_IEEE8023ADLAG
) {
2696 return (EOPNOTSUPP
);
2698 ifr
= (struct ifreq
*)data
;
2699 ifa
= (struct ifaddr
*)data
;
2703 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2706 case SIOCGIFMEDIA32
:
2707 case SIOCGIFMEDIA64
:
2709 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2710 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2712 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2714 ifmr
= (struct ifmediareq
*)data
;
2715 ifmr
->ifm_current
= IFM_ETHER
;
2717 ifmr
->ifm_status
= IFM_AVALID
;
2718 ifmr
->ifm_active
= IFM_ETHER
;
2719 ifmr
->ifm_count
= 1;
2720 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2721 if (ifb
->ifb_active_lag
!= NULL
) {
2722 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2723 ifmr
->ifm_status
|= IFM_ACTIVE
;
2726 else if (ifb
->ifb_distributing_count
> 0) {
2728 = ifb
->ifb_distributing_array
[0]->po_media_info
.mi_active
;
2729 ifmr
->ifm_status
|= IFM_ACTIVE
;
2732 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2733 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2734 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2735 if (user_addr
!= USER_ADDR_NULL
) {
2736 error
= copyout(&ifmr
->ifm_current
,
2743 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2749 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2750 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2752 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2755 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2756 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2757 &ifr
->ifr_devmtu
.ifdm_max
);
2763 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2764 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2766 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2769 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2774 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2778 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2782 user_addr
= proc_is64bit(current_proc())
2783 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2784 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2788 switch (ibr
.ibr_op
) {
2789 case IF_BOND_OP_ADD_INTERFACE
:
2790 case IF_BOND_OP_REMOVE_INTERFACE
:
2791 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2792 if (port_ifp
== NULL
) {
2796 if (ifnet_type(port_ifp
) != IFT_ETHER
) {
2797 error
= EPROTONOSUPPORT
;
2801 case IF_BOND_OP_SET_VERBOSE
:
2802 case IF_BOND_OP_SET_MODE
:
2811 switch (ibr
.ibr_op
) {
2812 case IF_BOND_OP_ADD_INTERFACE
:
2813 error
= bond_add_interface(ifp
, port_ifp
);
2815 case IF_BOND_OP_REMOVE_INTERFACE
:
2817 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2818 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2820 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2822 error
= bond_remove_interface(ifb
, port_ifp
);
2825 case IF_BOND_OP_SET_VERBOSE
:
2827 if (g_bond
== NULL
) {
2832 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2835 case IF_BOND_OP_SET_MODE
:
2836 switch (ibr
.ibr_ibru
.ibru_int_val
) {
2837 case IF_BOND_MODE_LACP
:
2838 case IF_BOND_MODE_STATIC
:
2847 error
= bond_set_mode(ifp
, ibr
.ibr_ibru
.ibru_int_val
);
2850 break; /* SIOCSIFBOND */
2853 user_addr
= proc_is64bit(current_proc())
2854 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2855 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2859 switch (ibr
.ibr_op
) {
2860 case IF_BOND_OP_GET_STATUS
:
2870 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2871 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2873 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2875 switch (ibr
.ibr_op
) {
2876 case IF_BOND_OP_GET_STATUS
:
2877 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2881 break; /* SIOCGIFBOND */
2888 /* enable/disable promiscuous mode */
2890 error
= bond_set_promisc(ifp
);
2896 error
= bond_setmulti(ifp
);
2905 bond_if_free(struct ifnet
* ifp
)
2913 ifb
= (ifbond_ref
)ifnet_softc(ifp
);
2918 ifbond_release(ifb
);
2925 bond_handle_event(struct ifnet
* port_ifp
, int event_code
)
2927 struct ifnet
* bond_ifp
= NULL
;
2929 int old_distributing_count
;
2931 struct media_info media_info
= { 0, 0};
2933 switch (event_code
) {
2934 case KEV_DL_IF_DETACHED
:
2936 case KEV_DL_LINK_OFF
:
2937 case KEV_DL_LINK_ON
:
2938 media_info
= interface_media_info(port_ifp
);
2944 p
= bond_lookup_port(port_ifp
);
2950 old_distributing_count
= ifb
->ifb_distributing_count
;
2951 switch (event_code
) {
2952 case KEV_DL_IF_DETACHED
:
2953 bond_remove_interface(ifb
, p
->po_ifp
);
2955 case KEV_DL_LINK_OFF
:
2956 case KEV_DL_LINK_ON
:
2957 p
->po_media_info
= media_info
;
2958 if (p
->po_enabled
) {
2959 bondport_link_status_changed(p
);
2963 /* generate a link-event */
2964 if (ifb
->ifb_mode
== IF_BOND_MODE_LACP
) {
2965 if (ifbond_selection(ifb
)) {
2966 event_code
= (ifb
->ifb_active_lag
== NULL
)
2969 /* XXX need to take a reference on bond_ifp */
2970 bond_ifp
= ifb
->ifb_ifp
;
2971 ifb
->ifb_last_link_event
= event_code
;
2974 event_code
= (ifb
->ifb_active_lag
== NULL
)
2977 if (event_code
!= ifb
->ifb_last_link_event
) {
2978 if (g_bond
->verbose
) {
2979 timestamp_printf("%s: (event) generating LINK event\n",
2982 bond_ifp
= ifb
->ifb_ifp
;
2983 ifb
->ifb_last_link_event
= event_code
;
2989 * if the distributing array membership changed from 0 <-> !0
2990 * generate a link event
2992 if (old_distributing_count
== 0
2993 && ifb
->ifb_distributing_count
!= 0) {
2994 event_code
= KEV_DL_LINK_ON
;
2996 else if (old_distributing_count
!= 0
2997 && ifb
->ifb_distributing_count
== 0) {
2998 event_code
= KEV_DL_LINK_OFF
;
3000 if (event_code
!= 0 && event_code
!= ifb
->ifb_last_link_event
) {
3001 bond_ifp
= ifb
->ifb_ifp
;
3002 ifb
->ifb_last_link_event
= event_code
;
3007 if (bond_ifp
!= NULL
) {
3008 interface_link_event(bond_ifp
, event_code
);
3014 bond_event(struct ifnet
* port_ifp
, __unused protocol_family_t protocol
,
3015 const struct kev_msg
* event
)
3019 if (event
->vendor_code
!= KEV_VENDOR_APPLE
3020 || event
->kev_class
!= KEV_NETWORK_CLASS
3021 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
3024 event_code
= event
->event_code
;
3025 switch (event_code
) {
3026 case KEV_DL_LINK_OFF
:
3027 case KEV_DL_LINK_ON
:
3028 /* we only care about link status changes */
3029 bond_handle_event(port_ifp
, event_code
);
3038 bond_detached(ifnet_t port_ifp
, __unused protocol_family_t protocol
)
3040 bond_handle_event(port_ifp
, KEV_DL_IF_DETACHED
);
3045 interface_link_event(struct ifnet
* ifp
, u_int32_t event_code
)
3048 struct kern_event_msg header
;
3050 char if_name
[IFNAMSIZ
];
3053 bzero(&event
, sizeof(event
));
3054 event
.header
.total_size
= sizeof(event
);
3055 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
3056 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
3057 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
3058 event
.header
.event_code
= event_code
;
3059 event
.header
.event_data
[0] = ifnet_family(ifp
);
3060 event
.unit
= (u_int32_t
) ifnet_unit(ifp
);
3061 strncpy(event
.if_name
, ifnet_name(ifp
), IFNAMSIZ
);
3062 ifnet_event(ifp
, &event
.header
);
3067 * Function: bond_attach_protocol
3069 * Attach a DLIL protocol to the interface.
3071 * The ethernet demux special cases to always return PF_BOND if the
3072 * interface is bonded. That means we receive all traffic from that
3073 * interface without passing any of the traffic to any other attached
3077 bond_attach_protocol(struct ifnet
*ifp
)
3080 struct ifnet_attach_proto_param reg
;
3082 bzero(®
, sizeof(reg
));
3083 reg
.input
= bond_input
;
3084 reg
.event
= bond_event
;
3085 reg
.detached
= bond_detached
;
3087 error
= ifnet_attach_protocol(ifp
, PF_BOND
, ®
);
3089 printf("bond over %s%d: ifnet_attach_protocol failed, %d\n",
3090 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3096 * Function: bond_detach_protocol
3098 * Detach our DLIL protocol from an interface
3101 bond_detach_protocol(struct ifnet
*ifp
)
3105 error
= ifnet_detach_protocol(ifp
, PF_BOND
);
3107 printf("bond over %s%d: ifnet_detach_protocol failed, %d\n",
3108 ifnet_name(ifp
), ifnet_unit(ifp
), error
);
3114 * DLIL interface family functions
3116 extern int ether_attach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3117 extern void ether_detach_inet(ifnet_t ifp
, protocol_family_t protocol_family
);
3118 extern int ether_attach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3119 extern void ether_detach_inet6(ifnet_t ifp
, protocol_family_t protocol_family
);
3120 extern int ether_attach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3121 extern void ether_detach_at(ifnet_t ifp
, protocol_family_t protocol_family
);
3123 __private_extern__
int
3124 bond_family_init(void)
3128 error
= proto_register_plumber(PF_INET
, APPLE_IF_FAM_BOND
,
3132 printf("bond: proto_register_plumber failed for AF_INET error=%d\n",
3137 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_BOND
,
3139 ether_detach_inet6
);
3141 printf("bond: proto_register_plumber failed for AF_INET6 error=%d\n",
3147 error
= proto_register_plumber(PF_APPLETALK
, APPLE_IF_FAM_BOND
,
3151 printf("bond: proto_register_plumber failed for AppleTalk error=%d\n",
3156 error
= bond_clone_attach();
3158 printf("bond: proto_register_plumber failed bond_clone_attach error=%d\n",
3173 ** LACP ifbond_list routines
3176 ifbond_list_find_moved_port(bondport_ref rx_port
,
3177 const lacp_actor_partner_tlv_ref atlv
)
3181 partner_state_ref ps
;
3184 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
3185 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3188 /* no point in comparing against ourselves */
3191 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
3192 /* it's not clear that we should be checking this */
3195 ps
= &p
->po_partner_state
;
3196 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
3199 ps_li
= &ps
->ps_lag_info
;
3200 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
3201 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
3202 sizeof(ps_li
->li_system
)) == 0) {
3203 if (g_bond
->verbose
) {
3204 timestamp_printf("System " EA_FORMAT
3205 " Port 0x%x moved from %s to %s\n",
3206 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
3207 bondport_get_name(p
),
3208 bondport_get_name(rx_port
));
3218 ** LACP ifbond, LAG routines
3222 ifbond_selection(ifbond_ref bond
)
3224 int all_ports_ready
= 0;
3225 int active_media
= 0;
3227 int lag_changed
= 0;
3231 lag
= ifbond_find_best_LAG(bond
, &active_media
);
3232 if (lag
!= bond
->ifb_active_lag
) {
3233 if (bond
->ifb_active_lag
!= NULL
) {
3234 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
3235 bond
->ifb_active_lag
= NULL
;
3237 bond
->ifb_active_lag
= lag
;
3239 ifbond_activate_LAG(bond
, lag
, active_media
);
3243 else if (lag
!= NULL
) {
3244 if (lag
->lag_active_media
!= active_media
) {
3245 if (g_bond
->verbose
) {
3246 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3247 link_speed(lag
->lag_active_media
),
3248 link_speed(active_media
));
3250 ifbond_deactivate_LAG(bond
, lag
);
3251 ifbond_activate_LAG(bond
, lag
, active_media
);
3256 port_speed
= link_speed(active_media
);
3257 all_ports_ready
= ifbond_all_ports_ready(bond
);
3259 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3260 if (lag
!= NULL
&& p
->po_lag
== lag
3261 && media_speed(&p
->po_media_info
) == port_speed
3262 && (p
->po_mux_state
== MuxState_DETACHED
3263 || p
->po_selected
== SelectedState_SELECTED
3264 || p
->po_selected
== SelectedState_STANDBY
)
3265 && bondport_aggregatable(p
)) {
3266 if (bond
->ifb_max_active
> 0) {
3267 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3268 if (p
->po_selected
== SelectedState_STANDBY
3269 || p
->po_selected
== SelectedState_UNSELECTED
) {
3270 bondport_set_selected(p
, SelectedState_SELECTED
);
3273 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3274 bondport_set_selected(p
, SelectedState_STANDBY
);
3278 bondport_set_selected(p
, SelectedState_SELECTED
);
3281 if (bondport_flags_selected_changed(p
)) {
3282 bondport_flags_clear_selected_changed(p
);
3283 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3286 && bondport_flags_ready(p
)
3287 && p
->po_mux_state
== MuxState_WAITING
) {
3288 bondport_mux_machine(p
, LAEventReady
, NULL
);
3290 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3292 return (lag_changed
);
3296 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3298 int best_active
= 0;
3299 LAG_ref best_lag
= NULL
;
3304 if (bond
->ifb_active_lag
!= NULL
) {
3305 best_lag
= bond
->ifb_active_lag
;
3306 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3307 if (bond
->ifb_max_active
> 0
3308 && best_count
> bond
->ifb_max_active
) {
3309 best_count
= bond
->ifb_max_active
;
3311 best_speed
= link_speed(best_active
);
3313 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3318 if (lag
== bond
->ifb_active_lag
) {
3319 /* we've already computed it */
3322 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3326 if (bond
->ifb_max_active
> 0
3327 && count
> bond
->ifb_max_active
) {
3328 /* if there's a limit, don't count extra links */
3329 count
= bond
->ifb_max_active
;
3331 speed
= link_speed(active
);
3332 if ((count
* speed
) > (best_count
* best_speed
)) {
3335 best_active
= active
;
3339 if (best_count
== 0) {
3342 *active_media
= best_active
;
3347 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3351 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3352 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3358 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3363 if (bond
->ifb_max_active
> 0) {
3364 need
= bond
->ifb_max_active
;
3366 lag
->lag_active_media
= active_media
;
3367 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3368 if (bondport_aggregatable(p
) == 0) {
3369 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3371 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3372 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3374 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3375 if (bond
->ifb_max_active
> 0) {
3377 bondport_set_selected(p
, SelectedState_SELECTED
);
3381 bondport_set_selected(p
, SelectedState_STANDBY
);
3385 bondport_set_selected(p
, SelectedState_SELECTED
);
3389 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3397 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3399 LAG_ref lag
= bond
->ifb_active_lag
;
3401 bond
->ifb_max_active
= max_active
;
3402 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3405 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3409 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3410 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3411 if (p
->po_selected
== SelectedState_SELECTED
) {
3412 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3414 if (remove_count
== 0) {
3425 ifbond_all_ports_ready(ifbond_ref bond
)
3430 if (bond
->ifb_active_lag
== NULL
) {
3433 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3434 if (p
->po_mux_state
== MuxState_WAITING
3435 && p
->po_selected
== SelectedState_SELECTED
) {
3436 if (bondport_flags_ready(p
) == 0) {
3440 /* note that there was at least one ready port */
3447 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3451 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3452 if (this_port
== p
) {
3455 if (bondport_flags_mux_attached(p
) == 0) {
3463 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3467 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3468 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3469 sizeof(lag
->lag_info
)) == 0) {
3477 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3487 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3488 if (bondport_aggregatable(p
)) {
3491 this_speed
= media_speed(&p
->po_media_info
);
3492 if (this_speed
== 0) {
3495 if (this_speed
> speed
) {
3496 active
= p
->po_media_info
.mi_active
;
3500 else if (this_speed
== speed
) {
3505 *active_media
= active
;
3511 ** LACP bondport routines
3514 bondport_link_status_changed(bondport_ref p
)
3516 ifbond_ref bond
= p
->po_bond
;
3518 if (g_bond
->verbose
) {
3519 if (media_active(&p
->po_media_info
)) {
3520 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3521 bondport_get_name(p
),
3522 media_speed(&p
->po_media_info
),
3523 media_full_duplex(&p
->po_media_info
)
3527 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3530 if (bond
->ifb_mode
== IF_BOND_MODE_LACP
) {
3531 if (media_active(&p
->po_media_info
)
3532 && bond
->ifb_active_lag
!= NULL
3533 && p
->po_lag
== bond
->ifb_active_lag
3534 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3535 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3536 if (g_bond
->verbose
) {
3537 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3538 bondport_get_name(p
),
3539 media_speed(&p
->po_media_info
),
3540 link_speed(p
->po_lag
->lag_active_media
));
3542 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3545 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3546 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3547 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3550 if (media_active(&p
->po_media_info
)) {
3551 bondport_enable_distributing(p
);
3554 bondport_disable_distributing(p
);
3561 bondport_aggregatable(bondport_ref p
)
3563 partner_state_ref ps
= &p
->po_partner_state
;
3565 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3566 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3567 /* we and/or our partner are individual */
3570 if (p
->po_lag
== NULL
) {
3573 switch (p
->po_receive_state
) {
3575 if (g_bond
->verbose
) {
3576 timestamp_printf("[%s] Port is not selectable\n",
3577 bondport_get_name(p
));
3580 case ReceiveState_CURRENT
:
3581 case ReceiveState_EXPIRED
:
3588 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3590 LAG_info_ref lag_li
;
3591 partner_state_ref ps
;
3594 ps
= &p
->po_partner_state
;
3595 ps_li
= &ps
->ps_lag_info
;
3596 lag_li
= &lag
->lag_info
;
3597 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3598 && ps_li
->li_key
== lag_li
->li_key
3599 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3600 sizeof(lag_li
->li_system
))
3608 bondport_remove_from_LAG(bondport_ref p
)
3611 ifbond_ref bond
= p
->po_bond
;
3612 LAG_ref lag
= p
->po_lag
;
3617 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3618 if (g_bond
->verbose
) {
3619 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3621 bondport_get_name(p
),
3622 lag
->lag_info
.li_system_priority
,
3623 EA_LIST(&lag
->lag_info
.li_system
),
3624 lag
->lag_info
.li_key
);
3627 lag
->lag_port_count
--;
3628 if (lag
->lag_port_count
> 0) {
3629 return (bond
->ifb_active_lag
== lag
);
3631 if (g_bond
->verbose
) {
3632 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3635 lag
->lag_info
.li_system_priority
,
3636 EA_LIST(&lag
->lag_info
.li_system
),
3637 lag
->lag_info
.li_key
);
3639 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3640 if (bond
->ifb_active_lag
== lag
) {
3641 bond
->ifb_active_lag
= NULL
;
3645 return (active_lag
);
3649 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3651 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3653 lag
->lag_port_count
++;
3654 if (g_bond
->verbose
) {
3655 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3656 bondport_get_name(p
),
3657 lag
->lag_info
.li_system_priority
,
3658 EA_LIST(&lag
->lag_info
.li_system
),
3659 lag
->lag_info
.li_key
);
3665 bondport_assign_to_LAG(bondport_ref p
)
3667 ifbond_ref bond
= p
->po_bond
;
3670 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3671 bondport_remove_from_LAG(p
);
3676 if (bondport_matches_LAG(p
, lag
)) {
3680 bondport_remove_from_LAG(p
);
3682 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3684 bondport_add_to_LAG(p
, lag
);
3687 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3688 TAILQ_INIT(&lag
->lag_port_list
);
3689 lag
->lag_port_count
= 0;
3690 lag
->lag_selected_port_count
= 0;
3691 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3692 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3693 if (g_bond
->verbose
) {
3694 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3697 lag
->lag_info
.li_system_priority
,
3698 EA_LIST(&lag
->lag_info
.li_system
),
3699 lag
->lag_info
.li_key
);
3701 bondport_add_to_LAG(p
, lag
);
3706 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3708 bondport_ref moved_port
;
3711 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3712 &in_lacpdu_p
->la_actor_tlv
);
3713 if (moved_port
!= NULL
) {
3714 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3716 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3717 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3718 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3723 bondport_set_selected(bondport_ref p
, SelectedState s
)
3725 if (s
!= p
->po_selected
) {
3726 ifbond_ref bond
= p
->po_bond
;
3727 LAG_ref lag
= p
->po_lag
;
3729 bondport_flags_set_selected_changed(p
);
3730 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3731 if (p
->po_selected
== SelectedState_SELECTED
) {
3732 lag
->lag_selected_port_count
--;
3734 else if (s
== SelectedState_SELECTED
) {
3735 lag
->lag_selected_port_count
++;
3737 if (g_bond
->verbose
) {
3738 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3739 bondport_get_name(p
),
3740 SelectedStateString(s
),
3741 SelectedStateString(p
->po_selected
));
3754 bondport_UpdateDefaultSelected(bondport_ref p
)
3756 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3761 bondport_RecordDefault(bondport_ref p
)
3763 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3765 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3766 bondport_assign_to_LAG(p
);
3771 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3773 lacp_actor_partner_tlv_ref actor
;
3774 partner_state_ref ps
;
3777 /* compare the PDU's Actor information to our Partner state */
3778 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3779 ps
= &p
->po_partner_state
;
3780 ps_li
= &ps
->ps_lag_info
;
3781 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3782 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3783 != ps
->ps_port_priority
)
3784 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3785 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3786 != ps_li
->li_system_priority
)
3787 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3788 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3789 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3790 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3791 if (g_bond
->verbose
) {
3792 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3793 bondport_get_name(p
));
3800 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3802 lacp_actor_partner_tlv_ref actor
;
3803 ifbond_ref bond
= p
->po_bond
;
3804 int lacp_maintain
= 0;
3805 partner_state_ref ps
;
3806 lacp_actor_partner_tlv_ref partner
;
3809 /* copy the PDU's Actor information into our Partner state */
3810 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3811 ps
= &p
->po_partner_state
;
3812 ps_li
= &ps
->ps_lag_info
;
3813 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3814 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3815 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3816 ps_li
->li_system_priority
3817 = lacp_actor_partner_tlv_get_system_priority(actor
);
3818 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3819 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3821 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3823 /* compare the PDU's Partner information to our own information */
3824 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3826 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3827 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3828 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3829 if (g_bond
->verbose
) {
3830 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3831 bondport_get_name(p
));
3835 if ((lacp_actor_partner_tlv_get_port(partner
)
3836 == bondport_get_index(p
))
3837 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3838 && bcmp(partner
->lap_system
, &g_bond
->system
,
3839 sizeof(g_bond
->system
)) == 0
3840 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3841 == g_bond
->system_priority
)
3842 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3843 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3844 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3845 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3847 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3848 if (g_bond
->verbose
) {
3849 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3850 bondport_get_name(p
));
3853 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3854 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3856 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3857 if (g_bond
->verbose
) {
3858 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3859 bondport_get_name(p
));
3862 bondport_assign_to_LAG(p
);
3866 static __inline__ lacp_actor_partner_state
3867 updateNTTBits(lacp_actor_partner_state s
)
3869 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3870 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3871 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3872 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3876 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3878 ifbond_ref bond
= p
->po_bond
;
3879 lacp_actor_partner_tlv_ref partner
;
3881 /* compare the PDU's Actor information to our Partner state */
3882 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3883 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3884 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3885 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3886 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3887 != g_bond
->system_priority
)
3888 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3889 || (updateNTTBits(partner
->lap_state
)
3890 != updateNTTBits(p
->po_actor_state
))) {
3891 bondport_flags_set_ntt(p
);
3892 if (g_bond
->verbose
) {
3893 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3894 bondport_get_name(p
));
3901 bondport_AttachMuxToAggregator(bondport_ref p
)
3903 if (bondport_flags_mux_attached(p
) == 0) {
3904 if (g_bond
->verbose
) {
3905 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3906 bondport_get_name(p
));
3908 bondport_flags_set_mux_attached(p
);
3914 bondport_DetachMuxFromAggregator(bondport_ref p
)
3916 if (bondport_flags_mux_attached(p
)) {
3917 if (g_bond
->verbose
) {
3918 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3919 bondport_get_name(p
));
3921 bondport_flags_clear_mux_attached(p
);
3927 bondport_enable_distributing(bondport_ref p
)
3929 if (bondport_flags_distributing(p
) == 0) {
3930 ifbond_ref bond
= p
->po_bond
;
3932 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3933 if (g_bond
->verbose
) {
3934 timestamp_printf("[%s] Distribution Enabled\n",
3935 bondport_get_name(p
));
3937 bondport_flags_set_distributing(p
);
3943 bondport_disable_distributing(bondport_ref p
)
3945 if (bondport_flags_distributing(p
)) {
3946 bondport_ref
* array
;
3952 array
= bond
->ifb_distributing_array
;
3953 count
= bond
->ifb_distributing_count
;
3954 for (i
= 0; i
< count
; i
++) {
3955 if (array
[i
] == p
) {
3958 for (j
= i
; j
< (count
- 1); j
++) {
3959 array
[j
] = array
[j
+ 1];
3964 bond
->ifb_distributing_count
--;
3965 if (g_bond
->verbose
) {
3966 timestamp_printf("[%s] Distribution Disabled\n",
3967 bondport_get_name(p
));
3969 bondport_flags_clear_distributing(p
);
3975 ** Receive machine functions
3978 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3981 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3984 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3987 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3990 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3993 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3997 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
4000 switch (p
->po_receive_state
) {
4001 case ReceiveState_none
:
4002 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4004 case ReceiveState_INITIALIZE
:
4005 bondport_receive_machine_initialize(p
, event
, event_data
);
4007 case ReceiveState_PORT_DISABLED
:
4008 bondport_receive_machine_port_disabled(p
, event
, event_data
);
4010 case ReceiveState_EXPIRED
:
4011 bondport_receive_machine_expired(p
, event
, event_data
);
4013 case ReceiveState_LACP_DISABLED
:
4014 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
4016 case ReceiveState_DEFAULTED
:
4017 bondport_receive_machine_defaulted(p
, event
, event_data
);
4019 case ReceiveState_CURRENT
:
4020 bondport_receive_machine_current(p
, event
, event_data
);
4029 bondport_receive_machine(bondport_ref p
, LAEvent event
,
4034 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
4035 bondport_receive_machine_current(p
, event
, event_data
);
4038 case LAEventMediaChange
:
4039 if (media_active(&p
->po_media_info
)) {
4040 switch (p
->po_receive_state
) {
4041 case ReceiveState_PORT_DISABLED
:
4042 case ReceiveState_LACP_DISABLED
:
4043 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
4050 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4054 bondport_receive_machine_event(p
, event
, event_data
);
4061 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
4062 __unused
void * event_data
)
4066 devtimer_cancel(p
->po_current_while_timer
);
4067 if (g_bond
->verbose
) {
4068 timestamp_printf("[%s] Receive INITIALIZE\n",
4069 bondport_get_name(p
));
4071 p
->po_receive_state
= ReceiveState_INITIALIZE
;
4072 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4073 bondport_RecordDefault(p
);
4075 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4076 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
4085 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
4086 __unused
void * event_data
)
4088 partner_state_ref ps
;
4092 devtimer_cancel(p
->po_current_while_timer
);
4093 if (g_bond
->verbose
) {
4094 timestamp_printf("[%s] Receive PORT_DISABLED\n",
4095 bondport_get_name(p
));
4097 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
4098 ps
= &p
->po_partner_state
;
4099 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
4101 case LAEventMediaChange
:
4102 if (media_active(&p
->po_media_info
)) {
4103 if (media_full_duplex(&p
->po_media_info
)) {
4104 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4107 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
4110 else if (p
->po_selected
== SelectedState_SELECTED
) {
4113 if (g_bond
->verbose
) {
4114 timestamp_printf("[%s] Receive PORT_DISABLED: "
4115 "link timer started\n",
4116 bondport_get_name(p
));
4120 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4121 (devtimer_timeout_func
)
4122 bondport_receive_machine_port_disabled
,
4123 (void *)LAEventTimeout
, NULL
);
4125 else if (p
->po_selected
== SelectedState_STANDBY
) {
4126 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4129 case LAEventTimeout
:
4130 if (p
->po_selected
== SelectedState_SELECTED
) {
4131 if (g_bond
->verbose
) {
4132 timestamp_printf("[%s] Receive PORT_DISABLED: "
4133 "link timer completed, marking UNSELECTED\n",
4134 bondport_get_name(p
));
4136 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4139 case LAEventPortMoved
:
4140 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
4149 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
4150 __unused
void * event_data
)
4152 lacp_actor_partner_state s
;
4157 devtimer_cancel(p
->po_current_while_timer
);
4158 if (g_bond
->verbose
) {
4159 timestamp_printf("[%s] Receive EXPIRED\n",
4160 bondport_get_name(p
));
4162 p
->po_receive_state
= ReceiveState_EXPIRED
;
4163 s
= p
->po_partner_state
.ps_state
;
4164 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4165 s
= lacp_actor_partner_state_set_short_timeout(s
);
4166 p
->po_partner_state
.ps_state
= s
;
4168 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
4169 /* start current_while timer */
4170 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4172 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4173 (devtimer_timeout_func
)
4174 bondport_receive_machine_expired
,
4175 (void *)LAEventTimeout
, NULL
);
4178 case LAEventTimeout
:
4179 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
4188 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
4189 __unused
void * event_data
)
4191 partner_state_ref ps
;
4194 devtimer_cancel(p
->po_current_while_timer
);
4195 if (g_bond
->verbose
) {
4196 timestamp_printf("[%s] Receive LACP_DISABLED\n",
4197 bondport_get_name(p
));
4199 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
4200 bondport_set_selected(p
, SelectedState_UNSELECTED
);
4201 bondport_RecordDefault(p
);
4202 ps
= &p
->po_partner_state
;
4203 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
4205 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4214 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
4215 __unused
void * event_data
)
4219 devtimer_cancel(p
->po_current_while_timer
);
4220 if (g_bond
->verbose
) {
4221 timestamp_printf("[%s] Receive DEFAULTED\n",
4222 bondport_get_name(p
));
4224 p
->po_receive_state
= ReceiveState_DEFAULTED
;
4225 bondport_UpdateDefaultSelected(p
);
4226 bondport_RecordDefault(p
);
4228 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4237 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
4240 partner_state_ref ps
;
4245 devtimer_cancel(p
->po_current_while_timer
);
4246 if (g_bond
->verbose
) {
4247 timestamp_printf("[%s] Receive CURRENT\n",
4248 bondport_get_name(p
));
4250 p
->po_receive_state
= ReceiveState_CURRENT
;
4251 bondport_UpdateSelected(p
, event_data
);
4252 bondport_UpdateNTT(p
, event_data
);
4253 bondport_RecordPDU(p
, event_data
);
4255 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4256 bondport_assign_to_LAG(p
);
4257 /* start current_while timer */
4258 ps
= &p
->po_partner_state
;
4259 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4260 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4263 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4266 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4267 (devtimer_timeout_func
)
4268 bondport_receive_machine_current
,
4269 (void *)LAEventTimeout
, NULL
);
4271 case LAEventTimeout
:
4272 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4281 ** Periodic Transmission machine
4285 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4286 __unused
void * event_data
)
4289 partner_state_ref ps
;
4294 if (g_bond
->verbose
) {
4295 timestamp_printf("[%s] periodic_transmit Start\n",
4296 bondport_get_name(p
));
4299 case LAEventMediaChange
:
4300 devtimer_cancel(p
->po_periodic_timer
);
4301 p
->po_periodic_interval
= 0;
4302 if (media_active(&p
->po_media_info
) == 0
4303 || media_full_duplex(&p
->po_media_info
) == 0) {
4307 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4308 ps
= &p
->po_partner_state
;
4309 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4310 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4312 devtimer_cancel(p
->po_periodic_timer
);
4313 p
->po_periodic_interval
= 0;
4316 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4317 interval
= LACP_FAST_PERIODIC_TIME
;
4320 interval
= LACP_SLOW_PERIODIC_TIME
;
4322 if (p
->po_periodic_interval
!= interval
) {
4323 if (interval
== LACP_FAST_PERIODIC_TIME
4324 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4325 if (g_bond
->verbose
) {
4326 timestamp_printf("[%s] periodic_transmit:"
4327 " Need To Transmit\n",
4328 bondport_get_name(p
));
4330 bondport_flags_set_ntt(p
);
4332 p
->po_periodic_interval
= interval
;
4334 tv
.tv_sec
= interval
;
4335 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4336 (devtimer_timeout_func
)
4337 bondport_periodic_transmit_machine
,
4338 (void *)LAEventTimeout
, NULL
);
4339 if (g_bond
->verbose
) {
4340 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4341 bondport_get_name(p
),
4342 p
->po_periodic_interval
);
4346 case LAEventTimeout
:
4347 bondport_flags_set_ntt(p
);
4348 tv
.tv_sec
= p
->po_periodic_interval
;
4350 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4351 bondport_periodic_transmit_machine
,
4352 (void *)LAEventTimeout
, NULL
);
4353 if (g_bond
->verbose
> 1) {
4354 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4355 bondport_get_name(p
), p
->po_periodic_interval
);
4368 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4369 __darwin_time_t
* next_secs
)
4371 if (p
->po_last_transmit_secs
!= current_secs
) {
4372 p
->po_last_transmit_secs
= current_secs
;
4373 p
->po_n_transmit
= 0;
4375 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4379 if (next_secs
!= NULL
) {
4380 *next_secs
= current_secs
+ 1;
4386 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4389 lacp_actor_partner_tlv_ref aptlv
;
4390 lacp_collector_tlv_ref ctlv
;
4391 struct timeval next_tick_time
= {0, 0};
4392 lacpdu_ref out_lacpdu_p
;
4393 packet_buffer_ref pkt
;
4394 partner_state_ref ps
;
4398 case LAEventTimeout
:
4400 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4403 if (event_data
== TRANSMIT_MACHINE_TX_IMMEDIATE
) {
4404 /* we're going away, transmit the packet no matter what */
4406 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4407 &next_tick_time
.tv_sec
) == 0) {
4408 if (devtimer_enabled(p
->po_transmit_timer
)) {
4409 if (g_bond
->verbose
> 0) {
4410 timestamp_printf("[%s] Transmit Timer Already Set\n",
4411 bondport_get_name(p
));
4415 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4416 (devtimer_timeout_func
)
4417 bondport_transmit_machine
,
4418 (void *)LAEventTimeout
, NULL
);
4419 if (g_bond
->verbose
> 0) {
4420 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4421 bondport_get_name(p
),
4422 (int)next_tick_time
.tv_sec
);
4427 if (g_bond
->verbose
> 0) {
4428 if (event
== LAEventTimeout
) {
4429 timestamp_printf("[%s] Transmit Timer Complete\n",
4430 bondport_get_name(p
));
4433 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4435 printf("[%s] Transmit: failed to allocate packet buffer\n",
4436 bondport_get_name(p
));
4439 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4440 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4441 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4442 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4445 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4446 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4447 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4448 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4449 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4450 g_bond
->system_priority
);
4451 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4452 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4453 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4454 aptlv
->lap_state
= p
->po_actor_state
;
4457 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4458 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4459 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4460 ps
= &p
->po_partner_state
;
4461 ps_li
= &ps
->ps_lag_info
;
4462 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4463 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4464 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4465 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4466 ps_li
->li_system_priority
);
4467 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4468 aptlv
->lap_state
= ps
->ps_state
;
4471 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4472 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4473 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4475 bondport_slow_proto_transmit(p
, pkt
);
4476 bondport_flags_clear_ntt(p
);
4477 if (g_bond
->verbose
> 0) {
4478 timestamp_printf("[%s] Transmit Packet %d\n",
4479 bondport_get_name(p
), p
->po_n_transmit
);
4489 ** Mux machine functions
4493 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4496 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4499 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4503 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4507 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4509 switch (p
->po_mux_state
) {
4511 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4513 case MuxState_DETACHED
:
4514 bondport_mux_machine_detached(p
, event
, event_data
);
4516 case MuxState_WAITING
:
4517 bondport_mux_machine_waiting(p
, event
, event_data
);
4519 case MuxState_ATTACHED
:
4520 bondport_mux_machine_attached(p
, event
, event_data
);
4522 case MuxState_COLLECTING_DISTRIBUTING
:
4523 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4532 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4533 __unused
void * event_data
)
4535 lacp_actor_partner_state s
;
4539 devtimer_cancel(p
->po_wait_while_timer
);
4540 if (g_bond
->verbose
) {
4541 timestamp_printf("[%s] Mux DETACHED\n",
4542 bondport_get_name(p
));
4544 p
->po_mux_state
= MuxState_DETACHED
;
4545 bondport_flags_clear_ready(p
);
4546 bondport_DetachMuxFromAggregator(p
);
4547 bondport_disable_distributing(p
);
4548 s
= p
->po_actor_state
;
4549 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4550 s
= lacp_actor_partner_state_set_not_collecting(s
);
4551 s
= lacp_actor_partner_state_set_not_distributing(s
);
4552 p
->po_actor_state
= s
;
4553 bondport_flags_set_ntt(p
);
4555 case LAEventSelectedChange
:
4557 case LAEventMediaChange
:
4558 if (p
->po_selected
== SelectedState_SELECTED
4559 || p
->po_selected
== SelectedState_STANDBY
) {
4560 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4570 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4571 __unused
void * event_data
)
4577 devtimer_cancel(p
->po_wait_while_timer
);
4578 if (g_bond
->verbose
) {
4579 timestamp_printf("[%s] Mux WAITING\n",
4580 bondport_get_name(p
));
4582 p
->po_mux_state
= MuxState_WAITING
;
4585 case LAEventSelectedChange
:
4586 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4587 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4590 if (p
->po_selected
== SelectedState_STANDBY
) {
4591 devtimer_cancel(p
->po_wait_while_timer
);
4592 /* wait until state changes to SELECTED */
4593 if (g_bond
->verbose
) {
4594 timestamp_printf("[%s] Mux WAITING: Standby\n",
4595 bondport_get_name(p
));
4599 if (bondport_flags_ready(p
)) {
4600 if (g_bond
->verbose
) {
4601 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4602 bondport_get_name(p
));
4606 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4607 if (g_bond
->verbose
) {
4608 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4609 bondport_get_name(p
));
4613 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4614 devtimer_cancel(p
->po_wait_while_timer
);
4615 if (g_bond
->verbose
) {
4616 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4617 bondport_get_name(p
));
4619 bondport_flags_set_ready(p
);
4622 if (g_bond
->verbose
) {
4623 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4624 bondport_get_name(p
));
4626 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4628 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4629 (devtimer_timeout_func
)
4630 bondport_mux_machine_waiting
,
4631 (void *)LAEventTimeout
, NULL
);
4633 case LAEventTimeout
:
4634 if (g_bond
->verbose
) {
4635 timestamp_printf("[%s] Mux WAITING: Ready\n",
4636 bondport_get_name(p
));
4638 bondport_flags_set_ready(p
);
4642 if (bondport_flags_ready(p
)){
4643 if (g_bond
->verbose
) {
4644 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4645 bondport_get_name(p
));
4647 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4656 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4657 __unused
void * event_data
)
4659 lacp_actor_partner_state s
;
4663 devtimer_cancel(p
->po_wait_while_timer
);
4664 if (g_bond
->verbose
) {
4665 timestamp_printf("[%s] Mux ATTACHED\n",
4666 bondport_get_name(p
));
4668 p
->po_mux_state
= MuxState_ATTACHED
;
4669 bondport_AttachMuxToAggregator(p
);
4670 s
= p
->po_actor_state
;
4671 s
= lacp_actor_partner_state_set_in_sync(s
);
4672 s
= lacp_actor_partner_state_set_not_collecting(s
);
4673 s
= lacp_actor_partner_state_set_not_distributing(s
);
4674 bondport_disable_distributing(p
);
4675 p
->po_actor_state
= s
;
4676 bondport_flags_set_ntt(p
);
4679 switch (p
->po_selected
) {
4680 case SelectedState_SELECTED
:
4681 s
= p
->po_partner_state
.ps_state
;
4682 if (lacp_actor_partner_state_in_sync(s
)) {
4683 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4688 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4697 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4699 __unused
void * event_data
)
4701 lacp_actor_partner_state s
;
4705 devtimer_cancel(p
->po_wait_while_timer
);
4706 if (g_bond
->verbose
) {
4707 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4708 bondport_get_name(p
));
4710 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4711 bondport_enable_distributing(p
);
4712 s
= p
->po_actor_state
;
4713 s
= lacp_actor_partner_state_set_collecting(s
);
4714 s
= lacp_actor_partner_state_set_distributing(s
);
4715 p
->po_actor_state
= s
;
4716 bondport_flags_set_ntt(p
);
4719 s
= p
->po_partner_state
.ps_state
;
4720 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4721 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4724 switch (p
->po_selected
) {
4725 case SelectedState_UNSELECTED
:
4726 case SelectedState_STANDBY
:
4727 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);