2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 * - bond/failover interface
34 * - implements IEEE 802.3ad Link Aggregation
38 * Modification History:
40 * April 29, 2004 Dieter Siegmund (dieter@apple.com)
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/kern_event.h>
56 #include <net/ethernet.h>
58 #include <net/kpi_interface.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_types.h>
63 #include <net/if_bond_var.h>
64 #include <net/ieee8023ad.h>
68 #include <net/devtimer.h>
69 #include <net/if_vlan_var.h>
71 #include <kern/locks.h>
72 #include <libkern/OSAtomic.h>
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
80 #include <net/if_media.h>
81 #include <net/multicast_list.h>
83 extern int dlil_input_packet(struct ifnet
*, struct mbuf
*, char *);
85 static struct ether_addr slow_proto_multicast
= {
86 IEEE8023AD_SLOW_PROTO_MULTICAST
89 #define BOND_MAXUNIT 128
90 #define BONDNAME "bond"
91 #define M_BOND M_DEVBUF
93 #define EA_FORMAT "%x:%x:%x:%x:%x:%x"
94 #define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
95 #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)
97 #define timestamp_printf printf
102 static __inline__ lck_grp_t
*
103 my_lck_grp_alloc_init(const char * grp_name
)
106 lck_grp_attr_t
* grp_attrs
;
108 grp_attrs
= lck_grp_attr_alloc_init();
109 grp
= lck_grp_alloc_init(grp_name
, grp_attrs
);
110 lck_grp_attr_free(grp_attrs
);
114 static __inline__ lck_mtx_t
*
115 my_lck_mtx_alloc_init(lck_grp_t
* lck_grp
)
117 lck_attr_t
* lck_attrs
;
120 lck_attrs
= lck_attr_alloc_init();
121 lck_mtx
= lck_mtx_alloc_init(lck_grp
, lck_attrs
);
122 lck_attr_free(lck_attrs
);
126 static lck_mtx_t
* bond_lck_mtx
;
128 static __inline__
void
131 lck_grp_t
* bond_lck_grp
;
133 bond_lck_grp
= my_lck_grp_alloc_init("if_bond");
134 bond_lck_mtx
= my_lck_mtx_alloc_init(bond_lck_grp
);
137 static __inline__
void
138 bond_assert_lock_held(void)
140 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_OWNED
);
144 static __inline__
void
145 bond_assert_lock_not_held(void)
147 lck_mtx_assert(bond_lck_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
151 static __inline__
void
154 lck_mtx_lock(bond_lck_mtx
);
158 static __inline__
void
161 lck_mtx_unlock(bond_lck_mtx
);
166 ** bond structures, types
170 lacp_system li_system
;
171 lacp_system_priority li_system_priority
;
174 typedef struct LAG_info_s LAG_info
, * LAG_info_ref
;
177 TAILQ_HEAD(port_list
, bondport_s
);
179 TAILQ_HEAD(ifbond_list
, ifbond_s
);
181 TAILQ_HEAD(lag_list
, LAG_s
);
183 typedef struct ifbond_s ifbond
, * ifbond_ref
;
184 typedef struct bondport_s bondport
, * bondport_ref
;
187 TAILQ_ENTRY(LAG_s
) lag_list
;
188 struct port_list lag_port_list
;
189 short lag_port_count
;
190 short lag_selected_port_count
;
191 int lag_active_media
;
194 typedef struct LAG_s LAG
, * LAG_ref
;
196 typedef struct partner_state_s
{
197 LAG_info ps_lag_info
;
199 lacp_port_priority ps_port_priority
;
200 lacp_actor_partner_state ps_state
;
201 } partner_state
, * partner_state_ref
;
204 TAILQ_ENTRY(ifbond_s
) ifb_bond_list
;
206 UInt32 ifb_retain_count
;
207 char ifb_name
[IFNAMSIZ
];
208 struct ifnet
* ifb_ifp
;
209 bpf_packet_func ifb_bpf_input
;
210 bpf_packet_func ifb_bpf_output
;
212 struct port_list ifb_port_list
;
213 short ifb_port_count
;
214 struct lag_list ifb_lag_list
;
216 short ifb_max_active
; /* 0 == unlimited */
217 LAG_ref ifb_active_lag
;
218 struct ifmultiaddr
* ifb_ifma_slow_proto
;
219 bondport_ref
* ifb_distributing_array
;
220 int ifb_distributing_count
;
229 ReceiveState_none
= 0,
230 ReceiveState_INITIALIZE
= 1,
231 ReceiveState_PORT_DISABLED
= 2,
232 ReceiveState_EXPIRED
= 3,
233 ReceiveState_LACP_DISABLED
= 4,
234 ReceiveState_DEFAULTED
= 5,
235 ReceiveState_CURRENT
= 6,
238 typedef u_char ReceiveState
;
241 SelectedState_UNSELECTED
= IF_BOND_STATUS_SELECTED_STATE_UNSELECTED
,
242 SelectedState_SELECTED
= IF_BOND_STATUS_SELECTED_STATE_SELECTED
,
243 SelectedState_STANDBY
= IF_BOND_STATUS_SELECTED_STATE_STANDBY
245 typedef u_char SelectedState
;
247 static __inline__
const char *
248 SelectedStateString(SelectedState s
)
250 static const char * names
[] = { "UNSELECTED", "SELECTED", "STANDBY" };
252 if (s
<= SelectedState_STANDBY
) {
255 return ("<unknown>");
260 MuxState_DETACHED
= 1,
261 MuxState_WAITING
= 2,
262 MuxState_ATTACHED
= 3,
263 MuxState_COLLECTING_DISTRIBUTING
= 4,
266 typedef u_char MuxState
;
269 TAILQ_ENTRY(bondport_s
) po_port_list
;
271 struct multicast_list po_multicast
;
272 struct ifnet
* po_ifp
;
273 struct ether_addr po_saved_addr
;
275 char po_name
[IFNAMSIZ
];
276 struct ifdevmtu po_devmtu
;
279 TAILQ_ENTRY(bondport_s
) po_lag_port_list
;
280 devtimer_ref po_current_while_timer
;
281 devtimer_ref po_periodic_timer
;
282 devtimer_ref po_wait_while_timer
;
283 devtimer_ref po_transmit_timer
;
284 partner_state po_partner_state
;
285 lacp_port_priority po_priority
;
286 lacp_actor_partner_state po_actor_state
;
288 u_char po_periodic_interval
;
289 u_char po_n_transmit
;
290 ReceiveState po_receive_state
;
291 MuxState po_mux_state
;
292 SelectedState po_selected
;
293 int32_t po_last_transmit_secs
;
294 struct media_info po_media_info
;
298 #define IFBF_PROMISC 0x1 /* promiscuous mode */
299 #define IFBF_IF_DETACHING 0x2 /* interface is detaching */
300 #define IFBF_LLADDR 0x4 /* specific link address requested */
301 #define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
303 static int bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
,
306 static __inline__
int
307 ifbond_flags_promisc(ifbond_ref ifb
)
309 return ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0);
312 static __inline__
void
313 ifbond_flags_set_promisc(ifbond_ref ifb
)
315 ifb
->ifb_flags
|= IFBF_PROMISC
;
319 static __inline__
void
320 ifbond_flags_clear_promisc(ifbond_ref ifb
)
322 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
326 static __inline__
int
327 ifbond_flags_if_detaching(ifbond_ref ifb
)
329 return ((ifb
->ifb_flags
& IFBF_IF_DETACHING
) != 0);
332 static __inline__
void
333 ifbond_flags_set_if_detaching(ifbond_ref ifb
)
335 ifb
->ifb_flags
|= IFBF_IF_DETACHING
;
339 static __inline__
int
340 ifbond_flags_lladdr(ifbond_ref ifb
)
342 return ((ifb
->ifb_flags
& IFBF_LLADDR
) != 0);
345 static __inline__
void
346 ifbond_flags_set_lladdr(ifbond_ref ifb
)
348 ifb
->ifb_flags
|= IFBF_LLADDR
;
352 static __inline__
void
353 ifbond_flags_clear_lladdr(ifbond_ref ifb
)
355 ifb
->ifb_flags
&= ~IFBF_LLADDR
;
359 static __inline__
int
360 ifbond_flags_change_in_progress(ifbond_ref ifb
)
362 return ((ifb
->ifb_flags
& IFBF_CHANGE_IN_PROGRESS
) != 0);
365 static __inline__
void
366 ifbond_flags_set_change_in_progress(ifbond_ref ifb
)
368 ifb
->ifb_flags
|= IFBF_CHANGE_IN_PROGRESS
;
372 static __inline__
void
373 ifbond_flags_clear_change_in_progress(ifbond_ref ifb
)
375 ifb
->ifb_flags
&= ~IFBF_CHANGE_IN_PROGRESS
;
380 * bondport_ref->po_flags bits
382 #define BONDPORT_FLAGS_NTT 0x01
383 #define BONDPORT_FLAGS_READY 0x02
384 #define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
385 #define BONDPORT_FLAGS_MUX_ATTACHED 0x08
386 #define BONDPORT_FLAGS_DISTRIBUTING 0x10
387 #define BONDPORT_FLAGS_UNUSED2 0x20
388 #define BONDPORT_FLAGS_UNUSED3 0x40
389 #define BONDPORT_FLAGS_UNUSED4 0x80
391 static __inline__
void
392 bondport_flags_set_ntt(bondport_ref p
)
394 p
->po_flags
|= BONDPORT_FLAGS_NTT
;
398 static __inline__
void
399 bondport_flags_clear_ntt(bondport_ref p
)
401 p
->po_flags
&= ~BONDPORT_FLAGS_NTT
;
405 static __inline__
int
406 bondport_flags_ntt(bondport_ref p
)
408 return ((p
->po_flags
& BONDPORT_FLAGS_NTT
) != 0);
411 static __inline__
void
412 bondport_flags_set_ready(bondport_ref p
)
414 p
->po_flags
|= BONDPORT_FLAGS_READY
;
418 static __inline__
void
419 bondport_flags_clear_ready(bondport_ref p
)
421 p
->po_flags
&= ~BONDPORT_FLAGS_READY
;
425 static __inline__
int
426 bondport_flags_ready(bondport_ref p
)
428 return ((p
->po_flags
& BONDPORT_FLAGS_READY
) != 0);
431 static __inline__
void
432 bondport_flags_set_selected_changed(bondport_ref p
)
434 p
->po_flags
|= BONDPORT_FLAGS_SELECTED_CHANGED
;
438 static __inline__
void
439 bondport_flags_clear_selected_changed(bondport_ref p
)
441 p
->po_flags
&= ~BONDPORT_FLAGS_SELECTED_CHANGED
;
445 static __inline__
int
446 bondport_flags_selected_changed(bondport_ref p
)
448 return ((p
->po_flags
& BONDPORT_FLAGS_SELECTED_CHANGED
) != 0);
451 static __inline__
void
452 bondport_flags_set_mux_attached(bondport_ref p
)
454 p
->po_flags
|= BONDPORT_FLAGS_MUX_ATTACHED
;
458 static __inline__
void
459 bondport_flags_clear_mux_attached(bondport_ref p
)
461 p
->po_flags
&= ~BONDPORT_FLAGS_MUX_ATTACHED
;
465 static __inline__
int
466 bondport_flags_mux_attached(bondport_ref p
)
468 return ((p
->po_flags
& BONDPORT_FLAGS_MUX_ATTACHED
) != 0);
471 static __inline__
void
472 bondport_flags_set_distributing(bondport_ref p
)
474 p
->po_flags
|= BONDPORT_FLAGS_DISTRIBUTING
;
478 static __inline__
void
479 bondport_flags_clear_distributing(bondport_ref p
)
481 p
->po_flags
&= ~BONDPORT_FLAGS_DISTRIBUTING
;
485 static __inline__
int
486 bondport_flags_distributing(bondport_ref p
)
488 return ((p
->po_flags
& BONDPORT_FLAGS_DISTRIBUTING
) != 0);
491 typedef struct bond_globals_s
{
492 struct ifbond_list ifbond_list
;
494 lacp_system_priority system_priority
;
496 } * bond_globals_ref
;
498 static bond_globals_ref g_bond
;
501 ** packet_buffer routines
502 ** - thin wrapper for mbuf
505 typedef struct mbuf
* packet_buffer_ref
;
507 static packet_buffer_ref
508 packet_buffer_allocate(int length
)
513 /* leave room for ethernet header */
514 size
= length
+ sizeof(struct ether_header
);
515 if (size
> (int)MHLEN
) {
516 /* XXX doesn't handle large payloads */
517 printf("bond: packet_buffer_allocate size %d > max %d\n", size
, MHLEN
);
520 m
= m_gethdr(M_WAITOK
, MT_DATA
);
525 m
->m_pkthdr
.len
= size
;
530 packet_buffer_byteptr(packet_buffer_ref buf
)
532 return (buf
->m_data
+ sizeof(struct ether_header
));
540 LAEventSelectedChange
,
549 bondport_receive_machine(bondport_ref p
, LAEvent event
,
552 ** Periodic Transmission machine
555 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
562 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
569 bondport_mux_machine(bondport_ref p
, LAEvent event
,
576 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
);
579 ifbond_deactivate_LAG(ifbond_ref bond
, LAG_ref lag
);
582 ifbond_all_ports_ready(ifbond_ref bond
);
585 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
);
588 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
);
591 ifbond_selection(ifbond_ref bond
);
599 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
);
602 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
);
605 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
606 int active
, int short_timeout
, int * error
);
608 bondport_start(bondport_ref p
);
611 bondport_free(bondport_ref p
);
614 bondport_aggregatable(bondport_ref p
);
617 bondport_remove_from_LAG(bondport_ref p
);
620 bondport_set_selected(bondport_ref p
, SelectedState s
);
623 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
);
626 bondport_link_status_changed(bondport_ref p
);
629 bondport_enable_distributing(bondport_ref p
);
632 bondport_disable_distributing(bondport_ref p
);
634 static __inline__
int
635 bondport_collecting(bondport_ref p
)
637 return (lacp_actor_partner_state_collecting(p
->po_actor_state
));
641 ** bond interface/dlil specific routines
643 static int bond_clone_create(struct if_clone
*, int);
644 static void bond_clone_destroy(struct ifnet
*);
645 static int bond_input(struct mbuf
*m
, char *frame_header
, struct ifnet
*ifp
,
646 u_long protocol_family
, int sync_ok
);
647 static int bond_output(struct ifnet
*ifp
, struct mbuf
*m
);
648 static int bond_ioctl(struct ifnet
*ifp
, u_int32_t cmd
, void * addr
);
649 static int bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
,
650 bpf_packet_func func
);
651 static int bond_attach_protocol(struct ifnet
*ifp
);
652 static int bond_detach_protocol(struct ifnet
*ifp
);
653 static int bond_setmulti(struct ifnet
*ifp
);
654 static int bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
);
655 static int bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
);
656 static void bond_if_free(struct ifnet
* ifp
);
658 static struct if_clone bond_cloner
= IF_CLONE_INITIALIZER(BONDNAME
,
663 static void interface_link_event(struct ifnet
* ifp
, u_long event_code
);
666 siocsifmtu(struct ifnet
* ifp
, int mtu
)
670 bzero(&ifr
, sizeof(ifr
));
672 return (dlil_ioctl(0, ifp
, SIOCSIFMTU
, (caddr_t
)&ifr
));
676 siocgifdevmtu(struct ifnet
* ifp
, struct ifdevmtu
* ifdm_p
)
681 bzero(&ifr
, sizeof(ifr
));
682 error
= dlil_ioctl(0, ifp
, SIOCGIFDEVMTU
, (caddr_t
)&ifr
);
684 *ifdm_p
= ifr
.ifr_devmtu
;
689 static __inline__
void
690 ether_addr_copy(void * dest
, const void * source
)
692 bcopy(source
, dest
, ETHER_ADDR_LEN
);
696 static __inline__
void
697 ifbond_retain(ifbond_ref ifb
)
699 OSIncrementAtomic(&ifb
->ifb_retain_count
);
702 static __inline__
void
703 ifbond_release(ifbond_ref ifb
)
705 UInt32 old_retain_count
;
707 old_retain_count
= OSDecrementAtomic(&ifb
->ifb_retain_count
);
708 switch (old_retain_count
) {
710 panic("ifbond_release: retain count is 0\n");
713 if (g_bond
->verbose
) {
714 printf("ifbond_release(%s)\n", ifb
->ifb_name
);
716 if (ifb
->ifb_ifma_slow_proto
!= NULL
) {
717 if (g_bond
->verbose
) {
718 printf("ifbond_release(%s) removing multicast\n",
721 (void)if_delmultiaddr(ifb
->ifb_ifma_slow_proto
, 0);
722 ifma_release(ifb
->ifb_ifma_slow_proto
);
724 if (ifb
->ifb_distributing_array
!= NULL
) {
725 FREE(ifb
->ifb_distributing_array
, M_BOND
);
736 * Function: ifbond_wait
738 * Allows a single thread to gain exclusive access to the ifbond
739 * data structure. Some operations take a long time to complete,
740 * and some have side-effects that we can't predict. Holding the
741 * bond_lock() across such operations is not possible.
744 * 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
745 * complete. Simply holding the bond_lock() would freeze all other
746 * data structure accesses during that time.
747 * 2) When we attach our protocol to the interface, a dlil event is
748 * generated and invokes our bond_event() function. bond_event()
749 * needs to take the bond_lock(), but we're already holding it, so
750 * we're deadlocked against ourselves.
752 * Before calling, you must be holding the bond_lock and have taken
753 * a reference on the ifbond_ref.
756 ifbond_wait(ifbond_ref ifb
, const char * msg
)
760 /* other add/remove in progress */
761 while (ifbond_flags_change_in_progress(ifb
)) {
762 if (g_bond
->verbose
) {
763 printf("%s: %s msleep\n", ifb
->ifb_name
, msg
);
766 (void)msleep(ifb
, bond_lck_mtx
, PZERO
, msg
, 0);
768 /* prevent other bond list remove/add from taking place */
769 ifbond_flags_set_change_in_progress(ifb
);
770 if (g_bond
->verbose
&& waited
) {
771 printf("%s: %s woke up\n", ifb
->ifb_name
, msg
);
777 * Function: ifbond_signal
779 * Allows the thread that previously invoked ifbond_wait() to
780 * give up exclusive access to the ifbond data structure, and wake up
781 * any other threads waiting to access
783 * Before calling, you must be holding the bond_lock and have taken
784 * a reference on the ifbond_ref.
787 ifbond_signal(ifbond_ref ifb
, const char * msg
)
789 ifbond_flags_clear_change_in_progress(ifb
);
790 wakeup((caddr_t
)ifb
);
791 if (g_bond
->verbose
) {
792 printf("%s: %s wakeup\n", ifb
->ifb_name
, msg
);
802 link_speed(int active
)
804 switch (IFM_SUBTYPE(active
)) {
825 /* assume that new defined types are going to be at least 10GigE */
832 static __inline__
int
833 media_active(const struct media_info
* mi
)
835 if ((mi
->mi_status
& IFM_AVALID
) == 0) {
838 return ((mi
->mi_status
& IFM_ACTIVE
) != 0);
841 static __inline__
int
842 media_full_duplex(const struct media_info
* mi
)
844 return ((mi
->mi_active
& IFM_FDX
) != 0);
847 static __inline__
int
848 media_speed(const struct media_info
* mi
)
850 return (link_speed(mi
->mi_active
));
853 static struct media_info
854 interface_media_info(struct ifnet
* ifp
)
856 struct ifmediareq ifmr
;
857 struct media_info mi
;
859 bzero(&mi
, sizeof(mi
));
860 bzero(&ifmr
, sizeof(ifmr
));
861 if (dlil_ioctl(0, ifp
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == 0) {
862 if (ifmr
.ifm_count
!= 0) {
863 mi
.mi_status
= ifmr
.ifm_status
;
864 mi
.mi_active
= ifmr
.ifm_active
;
871 ** interface utility functions
873 static __inline__
struct ifaddr
*
874 ifindex_get_ifaddr(int i
)
876 if (i
> if_index
|| i
== 0) {
879 return (ifnet_addrs
[i
- 1]);
882 static __inline__
struct ifaddr
*
883 ifp_get_ifaddr(struct ifnet
* ifp
)
885 return (ifindex_get_ifaddr(ifp
->if_index
));
888 static __inline__
struct sockaddr_dl
*
889 ifp_get_sdl(struct ifnet
* ifp
)
893 ifa
= ifp_get_ifaddr(ifp
);
894 return ((struct sockaddr_dl
*)(ifa
->ifa_addr
));
898 if_siflladdr(struct ifnet
* ifp
, const struct ether_addr
* ea_p
)
903 * XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
904 * currently expects it that way
906 ifr
.ifr_addr
.sa_family
= AF_UNSPEC
;
907 ifr
.ifr_addr
.sa_len
= ETHER_ADDR_LEN
;
908 ether_addr_copy(ifr
.ifr_addr
.sa_data
, ea_p
);
910 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", ifp
->if_name
,
913 return (dlil_ioctl(0, ifp
, SIOCSIFLLADDR
, (caddr_t
)&ifr
));
919 static bond_globals_ref
920 bond_globals_create(lacp_system_priority sys_pri
,
925 b
= _MALLOC(sizeof(*b
), M_BOND
, M_WAITOK
);
929 bzero(b
, sizeof(*b
));
930 TAILQ_INIT(&b
->ifbond_list
);
932 b
->system_priority
= sys_pri
;
940 bond_globals_init(void)
946 bond_assert_lock_not_held();
948 if (g_bond
!= NULL
) {
953 * use en0's ethernet address as the system identifier, and if it's not
954 * there, use en1 .. en3
957 for (i
= 0; i
< 4; i
++) {
958 char ifname
[IFNAMSIZ
+1];
959 snprintf(ifname
, sizeof(ifname
), "en%d", i
);
960 /* XXX ifunit() needs to return a reference on the ifp */
961 ifp
= ifunit(ifname
);
968 b
= bond_globals_create(0x8000,
969 (lacp_system_ref
)LLADDR(ifp_get_sdl(ifp
)));
972 if (g_bond
!= NULL
) {
989 bond_bpf_vlan(struct ifnet
* ifp
, struct mbuf
* m
,
990 const struct ether_header
* eh_p
,
991 u_int16_t vlan_tag
, bpf_packet_func func
)
993 struct ether_vlan_header
* vlh_p
;
996 vl_m
= m_get(M_DONTWAIT
, MT_DATA
);
1000 /* populate a new mbuf containing the vlan ethernet header */
1001 vl_m
->m_len
= ETHER_HDR_LEN
+ ETHER_VLAN_ENCAP_LEN
;
1002 vlh_p
= mtod(vl_m
, struct ether_vlan_header
*);
1003 bcopy(eh_p
, vlh_p
, offsetof(struct ether_header
, ether_type
));
1004 vlh_p
->evl_encap_proto
= htons(ETHERTYPE_VLAN
);
1005 vlh_p
->evl_tag
= htons(vlan_tag
);
1006 vlh_p
->evl_proto
= eh_p
->ether_type
;
1009 vl_m
->m_next
= NULL
;
1014 static __inline__
void
1015 bond_bpf_output(struct ifnet
* ifp
, struct mbuf
* m
,
1016 bpf_packet_func func
)
1019 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1020 const struct ether_header
* eh_p
;
1021 eh_p
= mtod(m
, const struct ether_header
*);
1022 m
->m_data
+= ETHER_HDR_LEN
;
1023 m
->m_len
-= ETHER_HDR_LEN
;
1024 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1025 m
->m_data
-= ETHER_HDR_LEN
;
1026 m
->m_len
+= ETHER_HDR_LEN
;
1034 static __inline__
void
1035 bond_bpf_input(ifnet_t ifp
, mbuf_t m
, const struct ether_header
* eh_p
,
1036 bpf_packet_func func
)
1039 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1040 bond_bpf_vlan(ifp
, m
, eh_p
, m
->m_pkthdr
.vlan_tag
, func
);
1042 /* restore the header */
1043 m
->m_data
-= ETHER_HDR_LEN
;
1044 m
->m_len
+= ETHER_HDR_LEN
;
1046 m
->m_data
+= ETHER_HDR_LEN
;
1047 m
->m_len
-= ETHER_HDR_LEN
;
1054 * Function: bond_setmulti
1056 * Enable multicast reception on "our" interface by enabling multicasts on
1057 * each of the member ports.
1060 bond_setmulti(struct ifnet
* ifp
)
1068 ifb
= ifp
->if_private
;
1069 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1070 || TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
1075 ifbond_wait(ifb
, "bond_setmulti");
1077 if (ifbond_flags_if_detaching(ifb
)) {
1078 /* someone destroyed the bond while we were waiting */
1084 /* ifbond_wait() let's us safely walk the list without holding the lock */
1085 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1086 struct ifnet
* port_ifp
= p
->po_ifp
;
1088 error
= multicast_list_program(&p
->po_multicast
,
1091 printf("bond_setmulti(%s): "
1092 "multicast_list_program(%s%d) failed, %d\n",
1093 ifb
->ifb_name
, port_ifp
->if_name
,
1094 port_ifp
->if_unit
, error
);
1100 ifbond_release(ifb
);
1101 ifbond_signal(ifb
, "bond_setmulti");
1107 bond_clone_attach(void)
1109 if_clone_attach(&bond_cloner
);
1115 ifbond_add_slow_proto_multicast(ifbond_ref ifb
)
1118 struct ifmultiaddr
* ifma
= NULL
;
1119 struct sockaddr_dl sdl
;
1121 bond_assert_lock_not_held();
1123 bzero(&sdl
, sizeof(sdl
));
1124 sdl
.sdl_len
= sizeof(sdl
);
1125 sdl
.sdl_family
= AF_LINK
;
1126 sdl
.sdl_type
= IFT_ETHER
;
1128 sdl
.sdl_alen
= sizeof(slow_proto_multicast
);
1129 bcopy(&slow_proto_multicast
, sdl
.sdl_data
, sizeof(slow_proto_multicast
));
1130 error
= if_addmulti(ifb
->ifb_ifp
, (struct sockaddr
*)&sdl
,
1133 ifb
->ifb_ifma_slow_proto
= ifma
;
1139 bond_clone_create(struct if_clone
* ifc
, int unit
)
1145 error
= bond_globals_init();
1150 ifb
= _MALLOC(sizeof(ifbond
), M_BOND
, M_WAITOK
);
1154 bzero(ifb
, sizeof(*ifb
));
1157 TAILQ_INIT(&ifb
->ifb_port_list
);
1158 TAILQ_INIT(&ifb
->ifb_lag_list
);
1159 ifb
->ifb_key
= unit
+ 1;
1161 /* use the interface name as the unique id for ifp recycle */
1162 if ((u_long
)snprintf(ifb
->ifb_name
, sizeof(ifb
->ifb_name
), "%s%d",
1163 ifc
->ifc_name
, unit
) >= sizeof(ifb
->ifb_name
)) {
1164 ifbond_release(ifb
);
1167 error
= dlil_if_acquire(APPLE_IF_FAM_BOND
,
1169 strlen(ifb
->ifb_name
),
1172 ifbond_release(ifb
);
1176 ifp
->if_name
= ifc
->ifc_name
;
1177 ifp
->if_unit
= unit
;
1178 ifp
->if_family
= APPLE_IF_FAM_BOND
;
1179 ifp
->if_private
= NULL
;
1180 ifp
->if_ioctl
= bond_ioctl
;
1181 ifp
->if_set_bpf_tap
= bond_set_bpf_tap
;
1182 ifp
->if_free
= bond_if_free
;
1183 ifp
->if_output
= bond_output
;
1184 ifp
->if_hwassist
= 0;
1185 ifp
->if_addrlen
= ETHER_ADDR_LEN
;
1186 ifp
->if_baudrate
= 0;
1187 ifp
->if_type
= IFT_IEEE8023ADLAG
;
1188 ifp
->if_flags
= IFF_BROADCAST
| IFF_MULTICAST
| IFF_SIMPLEX
;
1191 /* XXX ethernet specific */
1192 ifp
->if_broadcast
.length
= ETHER_ADDR_LEN
;
1193 bcopy(etherbroadcastaddr
, ifp
->if_broadcast
.u
.buffer
, ETHER_ADDR_LEN
);
1195 error
= dlil_if_attach(ifp
);
1197 dlil_if_release(ifp
);
1198 ifbond_release(ifb
);
1201 error
= ifbond_add_slow_proto_multicast(ifb
);
1203 printf("bond_clone_create(%s): "
1204 "failed to add slow_proto multicast, %d\n",
1205 ifb
->ifb_name
, error
);
1208 /* attach as ethernet */
1209 bpfattach(ifp
, DLT_EN10MB
, sizeof(struct ether_header
));
1212 ifp
->if_private
= ifb
;
1213 TAILQ_INSERT_HEAD(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1220 bond_remove_all_interfaces(ifbond_ref ifb
)
1224 bond_assert_lock_held();
1227 * do this in reverse order to avoid re-programming the mac address
1228 * as each head interface is removed
1230 while ((p
= TAILQ_LAST(&ifb
->ifb_port_list
, port_list
)) != NULL
) {
1231 bond_remove_interface(ifb
, p
->po_ifp
);
1237 bond_remove(ifbond_ref ifb
)
1239 bond_assert_lock_held();
1240 ifbond_flags_set_if_detaching(ifb
);
1241 TAILQ_REMOVE(&g_bond
->ifbond_list
, ifb
, ifb_bond_list
);
1242 bond_remove_all_interfaces(ifb
);
1247 bond_if_detach(struct ifnet
* ifp
)
1251 error
= dlil_if_detach(ifp
);
1252 if (error
!= DLIL_WAIT_FOR_FREE
) {
1254 printf("bond_if_detach %s%d: dlil_if_detach failed, %d\n",
1255 ifp
->if_name
, ifp
->if_unit
, error
);
1263 bond_clone_destroy(struct ifnet
* ifp
)
1268 ifb
= ifp
->if_private
;
1269 if (ifb
== NULL
|| ifp
->if_type
!= IFT_IEEE8023ADLAG
) {
1273 if (ifbond_flags_if_detaching(ifb
)) {
1279 bond_if_detach(ifp
);
1284 bond_set_bpf_tap(struct ifnet
* ifp
, bpf_tap_mode mode
, bpf_packet_func func
)
1289 ifb
= ifp
->if_private
;
1290 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1295 case BPF_TAP_DISABLE
:
1296 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= NULL
;
1300 ifb
->ifb_bpf_input
= func
;
1303 case BPF_TAP_OUTPUT
:
1304 ifb
->ifb_bpf_output
= func
;
1307 case BPF_TAP_INPUT_OUTPUT
:
1308 ifb
->ifb_bpf_input
= ifb
->ifb_bpf_output
= func
;
1318 ether_header_hash(struct ether_header
* eh_p
)
1322 /* get 32-bits from destination ether and ether type */
1323 h
= (*((uint16_t *)&eh_p
->ether_dhost
[4]) << 16)
1325 h
^= *((uint32_t *)&eh_p
->ether_dhost
[0]);
1329 static struct mbuf
*
1330 S_mbuf_skip_to_offset(struct mbuf
* m
, long * offset
)
1335 while (*offset
>= len
) {
1346 #if BYTE_ORDER == BIG_ENDIAN
1347 static __inline__
uint32_t
1348 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1350 return (((uint32_t)c0
<< 24) | ((uint32_t)c1
<< 16)
1351 | ((uint32_t)c2
<< 8) | (uint32_t)c3
);
1353 #else /* BYTE_ORDER == LITTLE_ENDIAN */
1354 static __inline__
uint32_t
1355 make_uint32(u_char c0
, u_char c1
, u_char c2
, u_char c3
)
1357 return (((uint32_t)c3
<< 24) | ((uint32_t)c2
<< 16)
1358 | ((uint32_t)c1
<< 8) | (uint32_t)c0
);
1360 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
1363 S_mbuf_copy_uint32(struct mbuf
* m
, long offset
, uint32_t * val
)
1365 struct mbuf
* current
;
1366 u_char
* current_data
;
1371 current
= S_mbuf_skip_to_offset(m
, &offset
);
1372 if (current
== NULL
) {
1375 current_data
= mtod(current
, u_char
*) + offset
;
1376 space_current
= current
->m_len
- offset
;
1377 if (space_current
>= (int)sizeof(uint32_t)) {
1378 *val
= *((uint32_t *)current_data
);
1381 next
= current
->m_next
;
1382 if (next
== NULL
|| (next
->m_len
+ space_current
) < (int)sizeof(uint32_t)) {
1385 next_data
= mtod(next
, u_char
*);
1386 switch (space_current
) {
1388 *val
= make_uint32(current_data
[0], next_data
[0],
1389 next_data
[1], next_data
[2]);
1392 *val
= make_uint32(current_data
[0], current_data
[1],
1393 next_data
[0], next_data
[1]);
1396 *val
= make_uint32(current_data
[0], current_data
[1],
1397 current_data
[2], next_data
[0]);
1403 #define IP_SRC_OFFSET (offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p))
1404 #define IP_DST_OFFSET (offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p))
1407 ip_header_hash(struct mbuf
* m
)
1410 struct in_addr ip_dst
;
1411 struct in_addr ip_src
;
1414 struct mbuf
* orig_m
= m
;
1416 /* find the IP protocol field relative to the start of the packet */
1417 offset
= offsetof(struct ip
, ip_p
) + sizeof(struct ether_header
);
1418 m
= S_mbuf_skip_to_offset(m
, &offset
);
1419 if (m
== NULL
|| m
->m_len
< 1) {
1422 data
= mtod(m
, u_char
*) + offset
;
1425 /* find the IP src relative to the IP protocol */
1426 if ((m
->m_len
- offset
)
1427 >= (int)(IP_SRC_OFFSET
+ sizeof(struct in_addr
) * 2)) {
1428 /* this should be the normal case */
1429 ip_src
= *(struct in_addr
*)(data
+ IP_SRC_OFFSET
);
1430 ip_dst
= *(struct in_addr
*)(data
+ IP_DST_OFFSET
);
1433 if (S_mbuf_copy_uint32(m
, offset
+ IP_SRC_OFFSET
,
1434 (uint32_t *)&ip_src
.s_addr
)) {
1437 if (S_mbuf_copy_uint32(m
, offset
+ IP_DST_OFFSET
,
1438 (uint32_t *)&ip_dst
.s_addr
)) {
1442 return (ntohl(ip_dst
.s_addr
) ^ ntohl(ip_src
.s_addr
) ^ ((uint32_t)ip_p
));
1445 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1448 #define IP6_ADDRS_LEN (sizeof(struct in6_addr) * 2)
1450 ipv6_header_hash(struct mbuf
* m
)
1455 struct mbuf
* orig_m
= m
;
1459 /* find the IP protocol field relative to the start of the packet */
1460 offset
= offsetof(struct ip6_hdr
, ip6_src
) + sizeof(struct ether_header
);
1461 m
= S_mbuf_skip_to_offset(m
, &offset
);
1463 goto bad_ipv6_packet
;
1465 data
= mtod(m
, u_char
*) + offset
;
1467 if ((m
->m_len
- offset
) >= (int)IP6_ADDRS_LEN
) {
1468 /* this should be the normal case */
1469 for (i
= 0, scan
= (uint32_t *)data
;
1470 i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t));
1476 for (i
= 0; i
< (int)(IP6_ADDRS_LEN
/ sizeof(uint32_t)); i
++) {
1478 if (S_mbuf_copy_uint32(m
, offset
+ i
* sizeof(uint32_t),
1479 (uint32_t *)&tmp
)) {
1480 goto bad_ipv6_packet
;
1485 return (ntohl(val
));
1488 return (ether_header_hash(mtod(orig_m
, struct ether_header
*)));
1492 bond_output(struct ifnet
* ifp
, struct mbuf
* m
)
1494 bpf_packet_func bpf_func
;
1497 struct ifnet
* port_ifp
= NULL
;
1502 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1506 if (m
->m_pkthdr
.socket_id
!= 0) {
1507 h
= m
->m_pkthdr
.socket_id
;
1510 struct ether_header
* eh_p
;
1512 eh_p
= mtod(m
, struct ether_header
*);
1513 switch (ntohs(eh_p
->ether_type
)) {
1515 h
= ip_header_hash(m
);
1517 case ETHERTYPE_IPV6
:
1518 h
= ipv6_header_hash(m
);
1521 h
= ether_header_hash(eh_p
);
1526 ifb
= ifp
->if_private
;
1527 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)
1528 || ifb
->ifb_distributing_count
== 0) {
1531 h
%= ifb
->ifb_distributing_count
;
1532 port_ifp
= ifb
->ifb_distributing_array
[h
]->po_ifp
;
1533 bpf_func
= ifb
->ifb_bpf_output
;
1536 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1537 (void)ifnet_stat_increment_out(ifp
, 1,
1538 m
->m_pkthdr
.len
+ ETHER_VLAN_ENCAP_LEN
,
1541 (void)ifnet_stat_increment_out(ifp
, 1, m
->m_pkthdr
.len
, 0);
1543 bond_bpf_output(ifp
, m
, bpf_func
);
1545 return (dlil_output(port_ifp
, 0, m
, NULL
, NULL
, 1));
1554 ifbond_lookup_port(ifbond_ref ifb
, struct ifnet
* port_ifp
)
1557 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
1558 if (p
->po_ifp
== port_ifp
) {
1566 bond_lookup_port(struct ifnet
* port_ifp
)
1571 TAILQ_FOREACH(ifb
, &g_bond
->ifbond_list
, ifb_bond_list
) {
1572 port
= ifbond_lookup_port(ifb
, port_ifp
);
1581 bond_receive_lacpdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1583 struct ifnet
* bond_ifp
= NULL
;
1588 if ((port_ifp
->if_eflags
& IFEF_BOND
) == 0) {
1591 p
= bond_lookup_port(port_ifp
);
1595 if (p
->po_enabled
== 0) {
1598 bondport_receive_lacpdu(p
, (lacpdu_ref
)m
->m_data
);
1599 if (ifbond_selection(p
->po_bond
)) {
1600 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1603 /* XXX need to take a reference on bond_ifp */
1604 bond_ifp
= p
->po_bond
->ifb_ifp
;
1609 if (bond_ifp
!= NULL
) {
1610 interface_link_event(bond_ifp
, event_code
);
1617 bond_receive_la_marker_pdu(struct mbuf
* m
, struct ifnet
* port_ifp
)
1619 la_marker_pdu_ref marker_p
;
1622 marker_p
= (la_marker_pdu_ref
)(m
->m_data
+ ETHER_HDR_LEN
);
1623 if (marker_p
->lm_marker_tlv_type
!= LA_MARKER_TLV_TYPE_MARKER
) {
1627 if ((port_ifp
->if_eflags
& IFEF_BOND
) == 0) {
1631 p
= bond_lookup_port(port_ifp
);
1632 if (p
== NULL
|| p
->po_enabled
== 0) {
1636 /* echo back the same packet as a marker response */
1637 marker_p
->lm_marker_tlv_type
= LA_MARKER_TLV_TYPE_MARKER_RESPONSE
;
1638 bondport_slow_proto_transmit(p
, (packet_buffer_ref
)m
);
1648 bond_input(struct mbuf
* m
, char * frame_header
, struct ifnet
* port_ifp
,
1649 __unused u_long protocol_family
, __unused
int sync_ok
)
1651 bpf_packet_func bpf_func
;
1652 const struct ether_header
* eh_p
;
1657 eh_p
= (const struct ether_header
*)frame_header
;
1658 if ((m
->m_flags
& M_MCAST
) != 0
1659 && bcmp(eh_p
->ether_dhost
, &slow_proto_multicast
,
1660 sizeof(eh_p
->ether_dhost
)) == 0
1661 && ntohs(eh_p
->ether_type
) == IEEE8023AD_SLOW_PROTO_ETHERTYPE
) {
1662 u_char subtype
= *mtod(m
, u_char
*);
1664 if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
) {
1665 if (m
->m_pkthdr
.len
< (int)offsetof(lacpdu
, la_reserved
)) {
1670 if (m
->m_len
< (int)offsetof(lacpdu
, la_reserved
)) {
1671 m
= m_pullup(m
, offsetof(lacpdu
, la_reserved
));
1676 bond_receive_lacpdu(m
, port_ifp
);
1679 else if (subtype
== IEEE8023AD_SLOW_PROTO_SUBTYPE_LA_MARKER_PROTOCOL
) {
1682 /* restore the ethernet header pointer in the mbuf */
1683 m
->m_pkthdr
.len
+= ETHER_HDR_LEN
;
1684 m
->m_data
-= ETHER_HDR_LEN
;
1685 m
->m_len
+= ETHER_HDR_LEN
;
1686 min_size
= ETHER_HDR_LEN
+ offsetof(la_marker_pdu
, lm_reserved
);
1687 if (m
->m_pkthdr
.len
< min_size
) {
1692 if (m
->m_len
< min_size
) {
1693 m
= m_pullup(m
, min_size
);
1698 /* send to marker responder */
1699 bond_receive_la_marker_pdu(m
, port_ifp
);
1702 else if (subtype
== 0
1703 || subtype
> IEEE8023AD_SLOW_PROTO_SUBTYPE_RESERVED_END
) {
1704 /* invalid subtype, discard the frame */
1710 if ((port_ifp
->if_eflags
& IFEF_BOND
) == 0) {
1713 p
= bond_lookup_port(port_ifp
);
1714 if (p
== NULL
|| bondport_collecting(p
) == 0) {
1718 /* make the packet appear as if it arrived on the bonded interface */
1721 bpf_func
= ifb
->ifb_bpf_input
;
1724 if (m
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) {
1725 (void)ifnet_stat_increment_in(ifp
, 1,
1726 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
1727 + ETHER_VLAN_ENCAP_LEN
), 0);
1730 (void)ifnet_stat_increment_in(ifp
, 1,
1731 (m
->m_pkthdr
.len
+ ETHER_HDR_LEN
), 0);
1733 m
->m_pkthdr
.rcvif
= ifp
;
1734 bond_bpf_input(ifp
, m
, eh_p
, bpf_func
);
1735 dlil_input_packet(ifp
, m
, frame_header
);
1744 static __inline__
const char *
1745 bondport_get_name(bondport_ref p
)
1747 return (p
->po_name
);
1750 static __inline__
int
1751 bondport_get_index(bondport_ref p
)
1753 return (p
->po_ifp
->if_index
);
1757 bondport_slow_proto_transmit(bondport_ref p
, packet_buffer_ref buf
)
1759 struct ether_header
* eh_p
;
1762 /* packet_buffer_allocate leaves room for ethernet header */
1763 eh_p
= mtod(buf
, struct ether_header
*);
1764 bcopy(&slow_proto_multicast
, &eh_p
->ether_dhost
, sizeof(eh_p
->ether_dhost
));
1765 bcopy(&p
->po_saved_addr
, eh_p
->ether_shost
, sizeof(eh_p
->ether_shost
));
1766 eh_p
->ether_type
= htons(IEEE8023AD_SLOW_PROTO_ETHERTYPE
);
1767 error
= dlil_output(p
->po_ifp
, 0, buf
, NULL
, NULL
, 1);
1769 printf("bondport_slow_proto_transmit(%s) failed %d\n",
1770 bondport_get_name(p
), error
);
1776 bondport_timer_process_func(devtimer_ref timer
,
1777 devtimer_process_func_event event
)
1782 case devtimer_process_func_event_lock
:
1784 devtimer_retain(timer
);
1786 case devtimer_process_func_event_unlock
:
1787 if (devtimer_valid(timer
)) {
1788 /* as long as the devtimer is valid, we can look at arg0 */
1790 struct ifnet
* bond_ifp
= NULL
;
1792 p
= (bondport_ref
)devtimer_arg0(timer
);
1793 if (ifbond_selection(p
->po_bond
)) {
1794 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
1797 /* XXX need to take a reference on bond_ifp */
1798 bond_ifp
= p
->po_bond
->ifb_ifp
;
1800 devtimer_release(timer
);
1802 if (bond_ifp
!= NULL
) {
1803 interface_link_event(bond_ifp
, event_code
);
1807 /* timer is going away */
1808 devtimer_release(timer
);
1818 bondport_create(struct ifnet
* port_ifp
, lacp_port_priority priority
,
1819 int active
, int short_timeout
, int * ret_error
)
1822 bondport_ref p
= NULL
;
1823 lacp_actor_partner_state s
;
1826 p
= _MALLOC(sizeof(*p
), M_BOND
, M_WAITOK
);
1828 *ret_error
= ENOMEM
;
1831 bzero(p
, sizeof(*p
));
1832 multicast_list_init(&p
->po_multicast
);
1833 if ((u_long
)snprintf(p
->po_name
, sizeof(p
->po_name
), "%s%d",
1834 port_ifp
->if_name
, port_ifp
->if_unit
)
1835 >= sizeof(p
->po_name
)) {
1836 printf("if_bond: name too large\n");
1837 *ret_error
= EINVAL
;
1840 error
= siocgifdevmtu(port_ifp
, &p
->po_devmtu
);
1842 printf("if_bond: SIOCGIFDEVMTU %s failed, %d\n",
1843 bondport_get_name(p
), error
);
1846 /* remember the current interface MTU so it can be restored */
1847 p
->po_devmtu
.ifdm_current
= port_ifp
->if_mtu
;
1848 p
->po_ifp
= port_ifp
;
1849 p
->po_media_info
= interface_media_info(port_ifp
);
1850 p
->po_current_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1851 if (p
->po_current_while_timer
== NULL
) {
1852 *ret_error
= ENOMEM
;
1855 p
->po_periodic_timer
= devtimer_create(bondport_timer_process_func
, p
);
1856 if (p
->po_periodic_timer
== NULL
) {
1857 *ret_error
= ENOMEM
;
1860 p
->po_wait_while_timer
= devtimer_create(bondport_timer_process_func
, p
);
1861 if (p
->po_wait_while_timer
== NULL
) {
1862 *ret_error
= ENOMEM
;
1865 p
->po_transmit_timer
= devtimer_create(bondport_timer_process_func
, p
);
1866 if (p
->po_transmit_timer
== NULL
) {
1867 *ret_error
= ENOMEM
;
1870 p
->po_receive_state
= ReceiveState_none
;
1871 p
->po_mux_state
= MuxState_none
;
1872 p
->po_priority
= priority
;
1874 s
= lacp_actor_partner_state_set_aggregatable(s
);
1875 if (short_timeout
) {
1876 s
= lacp_actor_partner_state_set_short_timeout(s
);
1879 s
= lacp_actor_partner_state_set_active_lacp(s
);
1881 p
->po_actor_state
= s
;
1890 bondport_start(bondport_ref p
)
1892 bondport_receive_machine(p
, LAEventStart
, NULL
);
1893 bondport_mux_machine(p
, LAEventStart
, NULL
);
1894 bondport_periodic_transmit_machine(p
, LAEventStart
, NULL
);
1895 bondport_transmit_machine(p
, LAEventStart
, NULL
);
1900 * Function: bondport_invalidate_timers
1902 * Invalidate all of the timers for the bondport.
1905 bondport_invalidate_timers(bondport_ref p
)
1907 devtimer_invalidate(p
->po_current_while_timer
);
1908 devtimer_invalidate(p
->po_periodic_timer
);
1909 devtimer_invalidate(p
->po_wait_while_timer
);
1910 devtimer_invalidate(p
->po_transmit_timer
);
1914 bondport_free(bondport_ref p
)
1916 multicast_list_remove(&p
->po_multicast
);
1917 devtimer_release(p
->po_current_while_timer
);
1918 devtimer_release(p
->po_periodic_timer
);
1919 devtimer_release(p
->po_wait_while_timer
);
1920 devtimer_release(p
->po_transmit_timer
);
1925 #define BOND_ADD_PROGRESS_IN_LIST 0x1
1926 #define BOND_ADD_PROGRESS_PROTO_ATTACHED 0x2
1927 #define BOND_ADD_PROGRESS_LLADDR_SET 0x4
1928 #define BOND_ADD_PROGRESS_MTU_SET 0x8
1930 static __inline__
int
1931 bond_device_mtu(struct ifnet
* ifp
, ifbond_ref ifb
)
1933 return (((int)ifp
->if_mtu
> ifb
->ifb_altmtu
)
1934 ? (int)ifp
->if_mtu
: ifb
->ifb_altmtu
);
1938 bond_add_interface(struct ifnet
* ifp
, struct ifnet
* port_ifp
)
1944 struct sockaddr_dl
* ifb_sdl
;
1945 bondport_ref
* new_array
= NULL
;
1946 bondport_ref
* old_array
= NULL
;
1948 struct sockaddr_dl
* port_sdl
;
1951 /* pre-allocate space for new port */
1952 p
= bondport_create(port_ifp
, 0x8000, 1, 0, &error
);
1957 ifb
= (ifbond_ref
)ifp
->if_private
;
1958 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
1961 return ((ifb
== NULL
? EOPNOTSUPP
: EBUSY
));
1964 /* make sure this interface can handle our current MTU */
1965 devmtu
= bond_device_mtu(ifp
, ifb
);
1967 && (devmtu
> p
->po_devmtu
.ifdm_max
|| devmtu
< p
->po_devmtu
.ifdm_min
)) {
1969 printf("if_bond: interface %s doesn't support mtu %d",
1970 bondport_get_name(p
), devmtu
);
1975 /* make sure ifb doesn't get de-allocated while we wait */
1978 /* wait for other add or remove to complete */
1979 ifbond_wait(ifb
, "bond_add_interface");
1981 if (ifbond_flags_if_detaching(ifb
)) {
1982 /* someone destroyed the bond while we were waiting */
1986 if (bond_lookup_port(port_ifp
) != NULL
) {
1987 /* port is already part of a bond */
1991 ifnet_lock_exclusive(port_ifp
);
1992 if ((port_ifp
->if_eflags
& (IFEF_VLAN
| IFEF_BOND
)) != 0) {
1993 /* interface already has VLAN's, or is part of bond */
1994 ifnet_lock_done(port_ifp
);
1999 /* mark the interface busy */
2000 port_ifp
->if_eflags
|= IFEF_BOND
;
2001 ifnet_lock_done(port_ifp
);
2003 port_sdl
= ifp_get_sdl(port_ifp
);
2004 ifb_sdl
= ifp_get_sdl(ifp
);
2006 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2007 ifp
->if_hwassist
= port_ifp
->if_hwassist
;
2008 ifp
->if_flags
|= IFF_RUNNING
;
2009 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2010 /* first port added to bond determines bond's ethernet address */
2011 ether_addr_copy(LLADDR(ifb_sdl
), LLADDR(port_sdl
));
2012 ifb_sdl
->sdl_type
= IFT_ETHER
;
2013 ifb_sdl
->sdl_alen
= ETHER_ADDR_LEN
;
2016 if (ifp
->if_hwassist
!= port_ifp
->if_hwassist
) {
2017 printf("bond_add_interface(%s, %s) "
2018 "hwassist values don't match 0x%x != 0x%x\n",
2019 ifb
->ifb_name
, bondport_get_name(p
),
2020 ifp
->if_hwassist
, port_ifp
->if_hwassist
);
2023 * if the bond has VLAN's, we can't simply change the hwassist
2024 * field behind its back: this needs work
2026 ifp
->if_hwassist
= 0;
2031 /* remember the port's ethernet address so it can be restored */
2032 ether_addr_copy(&p
->po_saved_addr
, LLADDR(port_sdl
));
2034 /* add it to the list of ports */
2035 TAILQ_INSERT_TAIL(&ifb
->ifb_port_list
, p
, po_port_list
);
2036 ifb
->ifb_port_count
++;
2038 /* set the default MTU */
2039 if (ifp
->if_mtu
== 0) {
2040 ifp
->if_mtu
= ETHERMTU
;
2043 progress
|= BOND_ADD_PROGRESS_IN_LIST
;
2045 /* allocate a larger distributing array */
2046 new_array
= (bondport_ref
*)
2047 _MALLOC(sizeof(*new_array
) * ifb
->ifb_port_count
, M_BOND
, M_WAITOK
);
2048 if (new_array
== NULL
) {
2053 /* attach our BOND "protocol" to the interface */
2054 error
= bond_attach_protocol(port_ifp
);
2058 progress
|= BOND_ADD_PROGRESS_PROTO_ATTACHED
;
2060 /* set the interface MTU */
2061 devmtu
= bond_device_mtu(ifp
, ifb
);
2062 error
= siocsifmtu(port_ifp
, devmtu
);
2064 printf("bond_add_interface(%s, %s):"
2065 " SIOCSIFMTU %d failed %d\n",
2066 ifb
->ifb_name
, bondport_get_name(p
), devmtu
, error
);
2069 progress
|= BOND_ADD_PROGRESS_MTU_SET
;
2071 /* program the port with our multicast addresses */
2072 error
= multicast_list_program(&p
->po_multicast
, ifp
, port_ifp
);
2074 printf("bond_add_interface(%s, %s):"
2075 " multicast_list_program failed %d\n",
2076 ifb
->ifb_name
, bondport_get_name(p
), error
);
2080 /* mark the interface up */
2081 ifnet_set_flags(port_ifp
, IFF_UP
, IFF_UP
);
2083 error
= dlil_ioctl(0, port_ifp
, SIOCSIFFLAGS
, (caddr_t
)NULL
);
2085 printf("bond_add_interface(%s, %s): SIOCSIFFLAGS failed %d\n",
2086 ifb
->ifb_name
, bondport_get_name(p
), error
);
2090 /* re-program the port's ethernet address */
2091 error
= if_siflladdr(port_ifp
,
2092 (const struct ether_addr
*)LLADDR(ifb_sdl
));
2094 /* port doesn't support setting the link address */
2095 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2096 ifb
->ifb_name
, bondport_get_name(p
), error
);
2099 progress
|= BOND_ADD_PROGRESS_LLADDR_SET
;
2103 /* no failures past this point */
2106 /* copy the contents of the existing distributing array */
2107 if (ifb
->ifb_distributing_count
) {
2108 bcopy(ifb
->ifb_distributing_array
, new_array
,
2109 sizeof(*new_array
) * ifb
->ifb_distributing_count
);
2111 old_array
= ifb
->ifb_distributing_array
;
2112 ifb
->ifb_distributing_array
= new_array
;
2114 /* clear the busy state, and wakeup anyone waiting */
2115 ifbond_signal(ifb
, "bond_add_interface");
2118 /* check if we need to generate a link status event */
2119 if (ifbond_selection(ifb
)) {
2120 event_code
= (ifb
->ifb_active_lag
== NULL
)
2125 if (event_code
!= 0) {
2126 interface_link_event(ifp
, event_code
);
2128 if (old_array
!= NULL
) {
2129 FREE(old_array
, M_BOND
);
2134 bond_assert_lock_not_held();
2136 if (new_array
!= NULL
) {
2137 FREE(new_array
, M_BOND
);
2139 if ((progress
& BOND_ADD_PROGRESS_LLADDR_SET
) != 0) {
2142 error1
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2144 printf("bond_add_interface(%s, %s): if_siflladdr failed %d\n",
2145 ifb
->ifb_name
, bondport_get_name(p
), error1
);
2148 if ((progress
& BOND_ADD_PROGRESS_PROTO_ATTACHED
) != 0) {
2149 (void)bond_detach_protocol(port_ifp
);
2151 if ((progress
& BOND_ADD_PROGRESS_MTU_SET
) != 0) {
2154 error1
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2156 printf("bond_add_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2157 ifb
->ifb_name
, bondport_get_name(p
), p
->po_devmtu
.ifdm_current
,
2162 if ((progress
& BOND_ADD_PROGRESS_IN_LIST
) != 0) {
2163 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2164 ifb
->ifb_port_count
--;
2166 ifnet_lock_exclusive(port_ifp
);
2167 port_ifp
->if_eflags
&= ~IFEF_BOND
;
2168 ifnet_lock_done(port_ifp
);
2169 if (TAILQ_EMPTY(&ifb
->ifb_port_list
)) {
2170 ifb
->ifb_altmtu
= 0;
2172 ifp
->if_hwassist
= 0;
2173 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2174 bzero(LLADDR(ifb_sdl
), ETHER_ADDR_LEN
);
2175 ifb_sdl
->sdl_type
= IFT_IEEE8023ADLAG
;
2176 ifb_sdl
->sdl_alen
= 0;
2181 ifbond_release(ifb
);
2182 ifbond_signal(ifb
, "bond_add_interface");
2189 bond_remove_interface(ifbond_ref ifb
, struct ifnet
* port_ifp
)
2194 bondport_ref head_port
;
2195 struct sockaddr_dl
* ifb_sdl
;
2197 int new_link_address
= 0;
2199 lacp_actor_partner_state s
;
2201 bond_assert_lock_held();
2204 ifbond_wait(ifb
, "bond_remove_interface");
2206 p
= ifbond_lookup_port(ifb
, port_ifp
);
2209 /* it got removed by another thread */
2213 /* de-select it and remove it from the lists */
2214 bondport_disable_distributing(p
);
2215 bondport_set_selected(p
, SelectedState_UNSELECTED
);
2216 active_lag
= bondport_remove_from_LAG(p
);
2217 TAILQ_REMOVE(&ifb
->ifb_port_list
, p
, po_port_list
);
2218 ifb
->ifb_port_count
--;
2220 /* invalidate timers here while holding the bond_lock */
2221 bondport_invalidate_timers(p
);
2223 /* announce that we're Individual now */
2224 s
= p
->po_actor_state
;
2225 s
= lacp_actor_partner_state_set_individual(s
);
2226 s
= lacp_actor_partner_state_set_not_collecting(s
);
2227 s
= lacp_actor_partner_state_set_not_distributing(s
);
2228 s
= lacp_actor_partner_state_set_out_of_sync(s
);
2229 p
->po_actor_state
= s
;
2230 bondport_flags_set_ntt(p
);
2233 ifb_sdl
= ifp_get_sdl(ifp
);
2234 head_port
= TAILQ_FIRST(&ifb
->ifb_port_list
);
2235 if (head_port
== NULL
) {
2236 ifp
->if_flags
&= ~IFF_RUNNING
;
2237 if (ifbond_flags_lladdr(ifb
) == FALSE
) {
2238 ifb_sdl
->sdl_type
= IFT_IEEE8023ADLAG
;
2239 ifb_sdl
->sdl_alen
= 0;
2240 bzero(LLADDR(ifb_sdl
), ETHER_ADDR_LEN
);
2242 ifp
->if_hwassist
= 0;
2244 ifb
->ifb_altmtu
= 0;
2245 } else if (ifbond_flags_lladdr(ifb
) == FALSE
2246 && bcmp(&p
->po_saved_addr
, LLADDR(ifb_sdl
),
2247 ETHER_ADDR_LEN
) == 0) {
2248 /* this port gave the bond its ethernet address, switch to new one */
2249 ether_addr_copy(LLADDR(ifb_sdl
), &head_port
->po_saved_addr
);
2250 ifb_sdl
->sdl_type
= IFT_ETHER
;
2251 ifb_sdl
->sdl_alen
= ETHER_ADDR_LEN
;
2252 new_link_address
= 1;
2254 /* check if we need to generate a link status event */
2255 if (ifbond_selection(ifb
) || active_lag
) {
2256 event_code
= (ifb
->ifb_active_lag
== NULL
)
2262 bondport_transmit_machine(p
, LAEventStart
, (void *)1);
2264 if (new_link_address
) {
2265 struct ifnet
* scan_ifp
;
2266 bondport_ref scan_port
;
2268 /* ifbond_wait() allows port list traversal without holding the lock */
2270 /* re-program each port with the new link address */
2271 TAILQ_FOREACH(scan_port
, &ifb
->ifb_port_list
, po_port_list
) {
2272 scan_ifp
= scan_port
->po_ifp
;
2274 error
= if_siflladdr(scan_ifp
,
2275 (const struct ether_addr
*) LLADDR(ifb_sdl
));
2277 printf("bond_remove_interface(%s, %s): "
2278 "if_siflladdr (%s) failed %d\n",
2279 ifb
->ifb_name
, bondport_get_name(p
),
2280 bondport_get_name(scan_port
), error
);
2285 /* restore the port's ethernet address */
2286 error
= if_siflladdr(port_ifp
, &p
->po_saved_addr
);
2288 printf("bond_remove_interface(%s, %s): if_siflladdr failed %d\n",
2289 ifb
->ifb_name
, bondport_get_name(p
), error
);
2292 /* restore the port's MTU */
2293 error
= siocsifmtu(port_ifp
, p
->po_devmtu
.ifdm_current
);
2295 printf("bond_remove_interface(%s, %s): SIOCSIFMTU %d failed %d\n",
2296 ifb
->ifb_name
, bondport_get_name(p
),
2297 p
->po_devmtu
.ifdm_current
, error
);
2300 /* remove the bond "protocol" */
2301 bond_detach_protocol(port_ifp
);
2303 /* generate link event */
2304 if (event_code
!= 0) {
2305 interface_link_event(ifp
, event_code
);
2309 ifbond_release(ifb
);
2311 ifnet_lock_exclusive(port_ifp
);
2312 port_ifp
->if_eflags
&= ~IFEF_BOND
;
2313 ifnet_lock_done(port_ifp
);
2316 ifbond_signal(ifb
, "bond_remove_interface");
2317 ifbond_release(ifb
); /* a second release for the second reference */
2322 bond_get_status(ifbond_ref ifb
, struct if_bond_req
* ibr_p
, user_addr_t datap
)
2327 struct if_bond_status_req
* ibsr
;
2328 struct if_bond_status ibs
;
2331 ibsr
= &(ibr_p
->ibr_ibru
.ibru_status
);
2332 if (ibsr
->ibsr_version
!= IF_BOND_STATUS_REQ_VERSION
) {
2335 ibsr
->ibsr_key
= ifb
->ifb_key
;
2336 ibsr
->ibsr_total
= ifb
->ifb_port_count
;
2337 dst
= proc_is64bit(current_proc())
2338 ? ibsr
->ibsr_ibsru
.ibsru_buffer64
2339 : CAST_USER_ADDR_T(ibsr
->ibsr_ibsru
.ibsru_buffer32
);
2340 if (dst
== USER_ADDR_NULL
) {
2341 /* just want to know how many there are */
2344 if (ibsr
->ibsr_count
< 0) {
2347 count
= (ifb
->ifb_port_count
< ibsr
->ibsr_count
)
2348 ? ifb
->ifb_port_count
: ibsr
->ibsr_count
;
2349 TAILQ_FOREACH(port
, &ifb
->ifb_port_list
, po_port_list
) {
2350 struct if_bond_partner_state
* ibps_p
;
2351 partner_state_ref ps
;
2356 bzero(&ibs
, sizeof(ibs
));
2357 strncpy(ibs
.ibs_if_name
, port
->po_name
, sizeof(ibs
.ibs_if_name
));
2358 ibs
.ibs_port_priority
= port
->po_priority
;
2359 ibs
.ibs_state
= port
->po_actor_state
;
2360 ibs
.ibs_selected_state
= port
->po_selected
;
2361 ps
= &port
->po_partner_state
;
2362 ibps_p
= &ibs
.ibs_partner_state
;
2363 ibps_p
->ibps_system
= ps
->ps_lag_info
.li_system
;
2364 ibps_p
->ibps_system_priority
= ps
->ps_lag_info
.li_system_priority
;
2365 ibps_p
->ibps_key
= ps
->ps_lag_info
.li_key
;
2366 ibps_p
->ibps_port
= ps
->ps_port
;
2367 ibps_p
->ibps_port_priority
= ps
->ps_port_priority
;
2368 ibps_p
->ibps_state
= ps
->ps_state
;
2369 error
= copyout(&ibs
, dst
, sizeof(ibs
));
2379 error
= copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2382 (void)copyout(ibr_p
, datap
, sizeof(*ibr_p
));
2388 bond_set_promisc(__unused
struct ifnet
*ifp
)
2392 ifbond_ref ifb
= ifp
->if_private
;
2395 if ((ifp
->if_flags
& IFF_PROMISC
) != 0) {
2396 if ((ifb
->ifb_flags
& IFBF_PROMISC
) == 0) {
2397 error
= ifnet_set_promiscuous(ifb
->ifb_p
, 1);
2399 ifb
->ifb_flags
|= IFBF_PROMISC
;
2402 if ((ifb
->ifb_flags
& IFBF_PROMISC
) != 0) {
2403 error
= ifnet_set_promiscuous(ifb
->ifb_p
, 0);
2405 ifb
->ifb_flags
&= ~IFBF_PROMISC
;
2413 bond_get_mtu_values(ifbond_ref ifb
, int * ret_min
, int * ret_max
)
2419 if (TAILQ_FIRST(&ifb
->ifb_port_list
) != NULL
) {
2420 mtu_min
= IF_MINMTU
;
2422 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2423 struct ifdevmtu
* devmtu_p
= &p
->po_devmtu
;
2425 if (devmtu_p
->ifdm_min
> mtu_min
) {
2426 mtu_min
= devmtu_p
->ifdm_min
;
2428 if (mtu_max
== 0 || devmtu_p
->ifdm_max
< mtu_max
) {
2429 mtu_max
= devmtu_p
->ifdm_max
;
2438 bond_set_mtu_on_ports(ifbond_ref ifb
, int mtu
)
2443 TAILQ_FOREACH(p
, &ifb
->ifb_port_list
, po_port_list
) {
2444 error
= siocsifmtu(p
->po_ifp
, mtu
);
2446 printf("if_bond(%s): SIOCSIFMTU %s failed, %d\n",
2447 ifb
->ifb_name
, bondport_get_name(p
), error
);
2455 bond_set_mtu(struct ifnet
* ifp
, int mtu
, int isdevmtu
)
2465 ifb
= (ifbond_ref
)ifp
->if_private
;
2466 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2467 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2471 ifbond_wait(ifb
, "bond_set_mtu");
2474 if (ifp
->if_private
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2478 bond_get_mtu_values(ifb
, &mtu_min
, &mtu_max
);
2479 if (mtu
> mtu_max
) {
2483 if (mtu
< mtu_min
&& (isdevmtu
== 0 || mtu
!= 0)) {
2484 /* allow SIOCSIFALTMTU to set the mtu to 0 */
2489 new_max
= (mtu
> (int)ifp
->if_mtu
) ? mtu
: (int)ifp
->if_mtu
;
2492 new_max
= (mtu
> ifb
->ifb_altmtu
) ? mtu
: ifb
->ifb_altmtu
;
2494 old_max
= ((int)ifp
->if_mtu
> ifb
->ifb_altmtu
)
2495 ? (int)ifp
->if_mtu
: ifb
->ifb_altmtu
;
2496 if (new_max
!= old_max
) {
2497 /* we can safely walk the list of port without the lock held */
2499 error
= bond_set_mtu_on_ports(ifb
, new_max
);
2501 /* try our best to back out of it */
2502 (void)bond_set_mtu_on_ports(ifb
, old_max
);
2508 ifb
->ifb_altmtu
= mtu
;
2516 ifbond_signal(ifb
, "bond_set_mtu");
2517 ifbond_release(ifb
);
2525 bond_ioctl(struct ifnet
*ifp
, u_int32_t cmd
, void * data
)
2528 struct if_bond_req ibr
;
2529 struct ifaddr
* ifa
;
2532 struct ifmediareq64
*ifmr
;
2533 struct ifnet
* port_ifp
= NULL
;
2534 user_addr_t user_addr
;
2536 if (ifp
->if_type
!= IFT_IEEE8023ADLAG
) {
2537 return (EOPNOTSUPP
);
2539 ifr
= (struct ifreq
*)data
;
2540 ifa
= (struct ifaddr
*)data
;
2544 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
2547 case SIOCGIFMEDIA64
:
2550 ifb
= (ifbond_ref
)ifp
->if_private
;
2551 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2553 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2555 ifmr
= (struct ifmediareq64
*)data
;
2556 ifmr
->ifm_current
= IFM_ETHER
;
2558 ifmr
->ifm_status
= IFM_AVALID
;
2559 ifmr
->ifm_active
= IFM_ETHER
;
2560 ifmr
->ifm_count
= 1;
2561 if (ifb
->ifb_active_lag
!= NULL
) {
2562 ifmr
->ifm_active
= ifb
->ifb_active_lag
->lag_active_media
;
2563 ifmr
->ifm_status
|= IFM_ACTIVE
;
2566 user_addr
= (cmd
== SIOCGIFMEDIA64
)
2567 ? ifmr
->ifm_ifmu
.ifmu_ulist64
2568 : CAST_USER_ADDR_T(ifmr
->ifm_ifmu
.ifmu_ulist32
);
2569 if (user_addr
!= USER_ADDR_NULL
) {
2570 error
= copyout(&ifmr
->ifm_current
,
2577 /* XXX send the SIFMEDIA to all children? Or force autoselect? */
2583 ifb
= (ifbond_ref
)ifp
->if_private
;
2584 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2586 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2589 ifr
->ifr_devmtu
.ifdm_current
= bond_device_mtu(ifp
, ifb
);
2590 bond_get_mtu_values(ifb
, &ifr
->ifr_devmtu
.ifdm_min
,
2591 &ifr
->ifr_devmtu
.ifdm_max
);
2597 ifb
= (ifbond_ref
)ifp
->if_private
;
2598 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2600 error
= (ifb
== NULL
) ? EOPNOTSUPP
: EBUSY
;
2603 ifr
->ifr_mtu
= ifb
->ifb_altmtu
;
2608 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 1);
2612 error
= bond_set_mtu(ifp
, ifr
->ifr_mtu
, 0);
2616 user_addr
= proc_is64bit(current_proc())
2617 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2618 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2622 switch (ibr
.ibr_op
) {
2623 case IF_BOND_OP_ADD_INTERFACE
:
2624 case IF_BOND_OP_REMOVE_INTERFACE
:
2625 /* XXX ifunit() needs to return a reference on the ifp */
2626 port_ifp
= ifunit(ibr
.ibr_ibru
.ibru_if_name
);
2627 if (port_ifp
== NULL
) {
2631 if (port_ifp
->if_type
!= IFT_ETHER
) {
2632 error
= EPROTONOSUPPORT
;
2636 case IF_BOND_OP_SET_VERBOSE
:
2645 switch (ibr
.ibr_op
) {
2646 case IF_BOND_OP_ADD_INTERFACE
:
2647 error
= bond_add_interface(ifp
, port_ifp
);
2649 case IF_BOND_OP_REMOVE_INTERFACE
:
2651 ifb
= (ifbond_ref
)ifp
->if_private
;
2652 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2654 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2656 error
= bond_remove_interface(ifb
, port_ifp
);
2659 case IF_BOND_OP_SET_VERBOSE
:
2661 if (g_bond
== NULL
) {
2666 g_bond
->verbose
= ibr
.ibr_ibru
.ibru_int_val
;
2673 user_addr
= proc_is64bit(current_proc())
2674 ? ifr
->ifr_data64
: CAST_USER_ADDR_T(ifr
->ifr_data
);
2675 error
= copyin(user_addr
, &ibr
, sizeof(ibr
));
2679 switch (ibr
.ibr_op
) {
2680 case IF_BOND_OP_GET_STATUS
:
2690 ifb
= (ifbond_ref
)ifp
->if_private
;
2691 if (ifb
== NULL
|| ifbond_flags_if_detaching(ifb
)) {
2693 return (ifb
== NULL
? EOPNOTSUPP
: EBUSY
);
2695 switch (ibr
.ibr_op
) {
2696 case IF_BOND_OP_GET_STATUS
:
2697 error
= bond_get_status(ifb
, &ibr
, user_addr
);
2708 /* enable/disable promiscuous mode */
2710 error
= bond_set_promisc(ifp
);
2716 error
= bond_setmulti(ifp
);
2725 bond_if_free(struct ifnet
* ifp
)
2733 ifb
= (ifbond_ref
)ifp
->if_private
;
2738 ifp
->if_private
= NULL
;
2739 ifbond_release(ifb
);
2741 dlil_if_release(ifp
);
2746 bond_event(struct ifnet
* port_ifp
, struct kev_msg
* event
)
2748 struct ifnet
* bond_ifp
= NULL
;
2751 struct media_info media_info
;
2753 if (event
->vendor_code
!= KEV_VENDOR_APPLE
2754 || event
->kev_class
!= KEV_NETWORK_CLASS
2755 || event
->kev_subclass
!= KEV_DL_SUBCLASS
) {
2758 switch (event
->event_code
) {
2759 case KEV_DL_IF_DETACHING
:
2761 case KEV_DL_LINK_OFF
:
2762 case KEV_DL_LINK_ON
:
2763 media_info
= interface_media_info(port_ifp
);
2769 p
= bond_lookup_port(port_ifp
);
2774 switch (event
->event_code
) {
2775 case KEV_DL_IF_DETACHING
:
2776 bond_remove_interface(p
->po_bond
, p
->po_ifp
);
2778 case KEV_DL_LINK_OFF
:
2779 case KEV_DL_LINK_ON
:
2780 p
->po_media_info
= media_info
;
2781 if (p
->po_enabled
) {
2782 bondport_link_status_changed(p
);
2786 /* generate a link-event */
2787 if (ifbond_selection(p
->po_bond
)) {
2788 event_code
= (p
->po_bond
->ifb_active_lag
== NULL
)
2791 /* XXX need to take a reference on bond_ifp */
2792 bond_ifp
= p
->po_bond
->ifb_ifp
;
2795 if (bond_ifp
!= NULL
) {
2796 interface_link_event(bond_ifp
, event_code
);
2802 interface_link_event(struct ifnet
* ifp
, u_long event_code
)
2805 struct kern_event_msg header
;
2807 char if_name
[IFNAMSIZ
];
2810 event
.header
.total_size
= sizeof(event
);
2811 event
.header
.vendor_code
= KEV_VENDOR_APPLE
;
2812 event
.header
.kev_class
= KEV_NETWORK_CLASS
;
2813 event
.header
.kev_subclass
= KEV_DL_SUBCLASS
;
2814 event
.header
.event_code
= event_code
;
2815 event
.header
.event_data
[0] = ifp
->if_family
;
2816 event
.unit
= (u_long
) ifp
->if_unit
;
2817 strncpy(event
.if_name
, ifp
->if_name
, IFNAMSIZ
);
2818 dlil_event(ifp
, &event
.header
);
2823 * Function: bond_attach_protocol
2825 * Attach a DLIL protocol to the interface.
2827 * The ethernet demux special cases to always return PF_BOND if the
2828 * interface is bonded. That means we receive all traffic from that
2829 * interface without passing any of the traffic to any other attached
2833 bond_attach_protocol(struct ifnet
*ifp
)
2836 struct dlil_proto_reg_str reg
;
2838 bzero(®
, sizeof(reg
));
2839 TAILQ_INIT(®
.demux_desc_head
);
2840 reg
.interface_family
= ifp
->if_family
;
2841 reg
.unit_number
= ifp
->if_unit
;
2842 reg
.input
= bond_input
;
2843 reg
.event
= bond_event
;
2844 reg
.protocol_family
= PF_BOND
;
2846 error
= dlil_attach_protocol(®
);
2848 printf("bond over %s%d: dlil_attach_protocol failed, %d\n",
2849 ifp
->if_name
, ifp
->if_unit
, error
);
2855 * Function: bond_detach_protocol
2857 * Detach our DLIL protocol from an interface
2860 bond_detach_protocol(struct ifnet
*ifp
)
2864 error
= dlil_detach_protocol(ifp
, PF_BOND
);
2866 printf("bond over %s%d: dlil_detach_protocol failed, %d\n",
2867 ifp
->if_name
, ifp
->if_unit
, error
);
2873 * DLIL interface family functions
2875 extern int ether_add_if(struct ifnet
*ifp
);
2876 extern int ether_del_if(struct ifnet
*ifp
);
2877 extern int ether_init_if(struct ifnet
*ifp
);
2878 extern int ether_add_proto_old(struct ifnet
*ifp
, u_long protocol_family
,
2879 struct ddesc_head_str
*desc_head
);
2881 extern int ether_attach_inet(struct ifnet
*ifp
, u_long protocol_family
);
2882 extern int ether_detach_inet(struct ifnet
*ifp
, u_long protocol_family
);
2883 extern int ether_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
2884 extern int ether_detach_inet6(struct ifnet
*ifp
, u_long protocol_family
);
2886 __private_extern__
int
2887 bond_family_init(void)
2890 struct dlil_ifmod_reg_str ifmod_reg
;
2892 bzero(&ifmod_reg
, sizeof(ifmod_reg
));
2893 ifmod_reg
.add_if
= ether_add_if
;
2894 ifmod_reg
.del_if
= ether_del_if
;
2895 ifmod_reg
.init_if
= NULL
;
2896 ifmod_reg
.add_proto
= ether_add_proto_old
;
2897 ifmod_reg
.del_proto
= ether_del_proto
;
2898 ifmod_reg
.ifmod_ioctl
= ether_ioctl
;
2899 ifmod_reg
.shutdown
= NULL
;
2901 if (dlil_reg_if_modules(APPLE_IF_FAM_BOND
, &ifmod_reg
)) {
2902 printf("WARNING: bond_family_init -- "
2903 "Can't register if family modules\n");
2908 error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_BOND
,
2912 printf("bond: dlil_reg_proto_module failed for AF_INET6 error=%d\n",
2917 error
= dlil_reg_proto_module(PF_INET6
, APPLE_IF_FAM_BOND
,
2919 ether_detach_inet6
);
2921 printf("bond: dlil_reg_proto_module failed for AF_INET6 error=%d\n",
2925 bond_clone_attach();
2937 ** LACP ifbond_list routines
2940 ifbond_list_find_moved_port(bondport_ref rx_port
,
2941 const lacp_actor_partner_tlv_ref atlv
)
2945 partner_state_ref ps
;
2948 TAILQ_FOREACH(bond
, &g_bond
->ifbond_list
, ifb_bond_list
) {
2949 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
2952 /* no point in comparing against ourselves */
2955 if (p
->po_receive_state
!= ReceiveState_PORT_DISABLED
) {
2956 /* it's not clear that we should be checking this */
2959 ps
= &p
->po_partner_state
;
2960 if (lacp_actor_partner_state_defaulted(ps
->ps_state
)) {
2963 ps_li
= &ps
->ps_lag_info
;
2964 if (ps
->ps_port
== lacp_actor_partner_tlv_get_port(atlv
)
2965 && bcmp(&ps_li
->li_system
, atlv
->lap_system
,
2966 sizeof(ps_li
->li_system
)) == 0) {
2967 if (g_bond
->verbose
) {
2968 timestamp_printf("System " EA_FORMAT
2969 " Port 0x%x moved from %s to %s\n",
2970 EA_LIST(&ps_li
->li_system
), ps
->ps_port
,
2971 bondport_get_name(p
),
2972 bondport_get_name(rx_port
));
2982 ** LACP ifbond, LAG routines
2986 ifbond_selection(ifbond_ref bond
)
2988 int all_ports_ready
= 0;
2989 int active_media
= 0;
2991 int lag_changed
= 0;
2995 lag
= ifbond_find_best_LAG(bond
, &active_media
);
2996 if (lag
!= bond
->ifb_active_lag
) {
2997 if (bond
->ifb_active_lag
!= NULL
) {
2998 ifbond_deactivate_LAG(bond
, bond
->ifb_active_lag
);
2999 bond
->ifb_active_lag
= NULL
;
3001 bond
->ifb_active_lag
= lag
;
3003 ifbond_activate_LAG(bond
, lag
, active_media
);
3007 else if (lag
!= NULL
) {
3008 if (lag
->lag_active_media
!= active_media
) {
3009 if (g_bond
->verbose
) {
3010 timestamp_printf("LAG PORT SPEED CHANGED from %d to %d\n",
3011 link_speed(lag
->lag_active_media
),
3012 link_speed(active_media
));
3014 ifbond_deactivate_LAG(bond
, lag
);
3015 ifbond_activate_LAG(bond
, lag
, active_media
);
3020 port_speed
= link_speed(active_media
);
3021 all_ports_ready
= ifbond_all_ports_ready(bond
);
3023 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3024 if (lag
!= NULL
&& p
->po_lag
== lag
3025 && media_speed(&p
->po_media_info
) == port_speed
3026 && (p
->po_mux_state
== MuxState_DETACHED
3027 || p
->po_selected
== SelectedState_SELECTED
3028 || p
->po_selected
== SelectedState_STANDBY
)
3029 && bondport_aggregatable(p
)) {
3030 if (bond
->ifb_max_active
> 0) {
3031 if (lag
->lag_selected_port_count
< bond
->ifb_max_active
) {
3032 if (p
->po_selected
== SelectedState_STANDBY
3033 || p
->po_selected
== SelectedState_UNSELECTED
) {
3034 bondport_set_selected(p
, SelectedState_SELECTED
);
3037 else if (p
->po_selected
== SelectedState_UNSELECTED
) {
3038 bondport_set_selected(p
, SelectedState_STANDBY
);
3042 bondport_set_selected(p
, SelectedState_SELECTED
);
3045 if (bondport_flags_selected_changed(p
)) {
3046 bondport_flags_clear_selected_changed(p
);
3047 bondport_mux_machine(p
, LAEventSelectedChange
, NULL
);
3050 && bondport_flags_ready(p
)
3051 && p
->po_mux_state
== MuxState_WAITING
) {
3052 bondport_mux_machine(p
, LAEventReady
, NULL
);
3054 bondport_transmit_machine(p
, LAEventStart
, NULL
);
3056 return (lag_changed
);
3060 ifbond_find_best_LAG(ifbond_ref bond
, int * active_media
)
3062 int best_active
= 0;
3063 LAG_ref best_lag
= NULL
;
3068 if (bond
->ifb_active_lag
!= NULL
) {
3069 best_lag
= bond
->ifb_active_lag
;
3070 best_count
= LAG_get_aggregatable_port_count(best_lag
, &best_active
);
3071 if (bond
->ifb_max_active
> 0
3072 && best_count
> bond
->ifb_max_active
) {
3073 best_count
= bond
->ifb_max_active
;
3075 best_speed
= link_speed(best_active
);
3077 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3082 if (lag
== bond
->ifb_active_lag
) {
3083 /* we've already computed it */
3086 count
= LAG_get_aggregatable_port_count(lag
, &active
);
3090 if (bond
->ifb_max_active
> 0
3091 && count
> bond
->ifb_max_active
) {
3092 /* if there's a limit, don't count extra links */
3093 count
= bond
->ifb_max_active
;
3095 speed
= link_speed(active
);
3096 if ((count
* speed
) > (best_count
* best_speed
)) {
3099 best_active
= active
;
3103 if (best_count
== 0) {
3106 *active_media
= best_active
;
3111 ifbond_deactivate_LAG(__unused ifbond_ref bond
, LAG_ref lag
)
3115 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3116 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3122 ifbond_activate_LAG(ifbond_ref bond
, LAG_ref lag
, int active_media
)
3127 if (bond
->ifb_max_active
> 0) {
3128 need
= bond
->ifb_max_active
;
3130 lag
->lag_active_media
= active_media
;
3131 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3132 if (bondport_aggregatable(p
) == 0) {
3133 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3135 else if (media_speed(&p
->po_media_info
) != link_speed(active_media
)) {
3136 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3138 else if (p
->po_mux_state
== MuxState_DETACHED
) {
3139 if (bond
->ifb_max_active
> 0) {
3141 bondport_set_selected(p
, SelectedState_SELECTED
);
3145 bondport_set_selected(p
, SelectedState_STANDBY
);
3149 bondport_set_selected(p
, SelectedState_SELECTED
);
3153 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3161 ifbond_set_max_active(ifbond_ref bond
, int max_active
)
3163 LAG_ref lag
= bond
->ifb_active_lag
;
3165 bond
->ifb_max_active
= max_active
;
3166 if (bond
->ifb_max_active
<= 0 || lag
== NULL
) {
3169 if (lag
->lag_selected_port_count
> bond
->ifb_max_active
) {
3173 remove_count
= lag
->lag_selected_port_count
- bond
->ifb_max_active
;
3174 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3175 if (p
->po_selected
== SelectedState_SELECTED
) {
3176 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3178 if (remove_count
== 0) {
3189 ifbond_all_ports_ready(ifbond_ref bond
)
3194 if (bond
->ifb_active_lag
== NULL
) {
3197 TAILQ_FOREACH(p
, &bond
->ifb_active_lag
->lag_port_list
, po_lag_port_list
) {
3198 if (p
->po_mux_state
== MuxState_WAITING
3199 && p
->po_selected
== SelectedState_SELECTED
) {
3200 if (bondport_flags_ready(p
) == 0) {
3204 /* note that there was at least one ready port */
3211 ifbond_all_ports_attached(ifbond_ref bond
, bondport_ref this_port
)
3215 TAILQ_FOREACH(p
, &bond
->ifb_port_list
, po_port_list
) {
3216 if (this_port
== p
) {
3219 if (bondport_flags_mux_attached(p
) == 0) {
3227 ifbond_get_LAG_matching_port(ifbond_ref bond
, bondport_ref p
)
3231 TAILQ_FOREACH(lag
, &bond
->ifb_lag_list
, lag_list
) {
3232 if (bcmp(&lag
->lag_info
, &p
->po_partner_state
.ps_lag_info
,
3233 sizeof(lag
->lag_info
)) == 0) {
3241 LAG_get_aggregatable_port_count(LAG_ref lag
, int * active_media
)
3251 TAILQ_FOREACH(p
, &lag
->lag_port_list
, po_lag_port_list
) {
3252 if (bondport_aggregatable(p
)) {
3255 this_speed
= media_speed(&p
->po_media_info
);
3256 if (this_speed
== 0) {
3259 if (this_speed
> speed
) {
3260 active
= p
->po_media_info
.mi_active
;
3264 else if (this_speed
== speed
) {
3269 *active_media
= active
;
3275 ** LACP bondport routines
3278 bondport_link_status_changed(bondport_ref p
)
3280 ifbond_ref bond
= p
->po_bond
;
3282 if (g_bond
->verbose
) {
3283 if (media_active(&p
->po_media_info
)) {
3284 timestamp_printf("[%s] Link UP %d Mbit/s %s duplex\n",
3285 bondport_get_name(p
),
3286 media_speed(&p
->po_media_info
),
3287 media_full_duplex(&p
->po_media_info
)
3291 timestamp_printf("[%s] Link DOWN\n", bondport_get_name(p
));
3294 if (media_active(&p
->po_media_info
)
3295 && bond
->ifb_active_lag
!= NULL
3296 && p
->po_lag
== bond
->ifb_active_lag
3297 && p
->po_selected
!= SelectedState_UNSELECTED
) {
3298 if (media_speed(&p
->po_media_info
) != p
->po_lag
->lag_active_media
) {
3299 if (g_bond
->verbose
) {
3300 timestamp_printf("[%s] Port speed %d differs from LAG %d\n",
3301 bondport_get_name(p
),
3302 media_speed(&p
->po_media_info
),
3303 link_speed(p
->po_lag
->lag_active_media
));
3305 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3308 bondport_receive_machine(p
, LAEventMediaChange
, NULL
);
3309 bondport_mux_machine(p
, LAEventMediaChange
, NULL
);
3310 bondport_periodic_transmit_machine(p
, LAEventMediaChange
, NULL
);
3316 bondport_aggregatable(bondport_ref p
)
3318 partner_state_ref ps
= &p
->po_partner_state
;
3320 if (lacp_actor_partner_state_aggregatable(p
->po_actor_state
) == 0
3321 || lacp_actor_partner_state_aggregatable(ps
->ps_state
) == 0) {
3322 /* we and/or our partner are individual */
3325 if (p
->po_lag
== NULL
) {
3328 switch (p
->po_receive_state
) {
3330 if (g_bond
->verbose
) {
3331 timestamp_printf("[%s] Port is not selectable\n",
3332 bondport_get_name(p
));
3335 case ReceiveState_CURRENT
:
3336 case ReceiveState_EXPIRED
:
3343 bondport_matches_LAG(bondport_ref p
, LAG_ref lag
)
3345 LAG_info_ref lag_li
;
3346 partner_state_ref ps
;
3349 ps
= &p
->po_partner_state
;
3350 ps_li
= &ps
->ps_lag_info
;
3351 lag_li
= &lag
->lag_info
;
3352 if (ps_li
->li_system_priority
== lag_li
->li_system_priority
3353 && ps_li
->li_key
== lag_li
->li_key
3354 && (bcmp(&ps_li
->li_system
, &lag_li
->li_system
,
3355 sizeof(lag_li
->li_system
))
3363 bondport_remove_from_LAG(bondport_ref p
)
3366 ifbond_ref bond
= p
->po_bond
;
3367 LAG_ref lag
= p
->po_lag
;
3372 TAILQ_REMOVE(&lag
->lag_port_list
, p
, po_lag_port_list
);
3373 if (g_bond
->verbose
) {
3374 timestamp_printf("[%s] Removed from LAG (0x%04x," EA_FORMAT
3376 bondport_get_name(p
),
3377 lag
->lag_info
.li_system_priority
,
3378 EA_LIST(&lag
->lag_info
.li_system
),
3379 lag
->lag_info
.li_key
);
3382 lag
->lag_port_count
--;
3383 if (lag
->lag_port_count
> 0) {
3384 return (bond
->ifb_active_lag
== lag
);
3386 if (g_bond
->verbose
) {
3387 timestamp_printf("Key 0x%04x: LAG Released (%04x," EA_FORMAT
3390 lag
->lag_info
.li_system_priority
,
3391 EA_LIST(&lag
->lag_info
.li_system
),
3392 lag
->lag_info
.li_key
);
3394 TAILQ_REMOVE(&bond
->ifb_lag_list
, lag
, lag_list
);
3395 if (bond
->ifb_active_lag
== lag
) {
3396 bond
->ifb_active_lag
= NULL
;
3400 return (active_lag
);
3404 bondport_add_to_LAG(bondport_ref p
, LAG_ref lag
)
3406 TAILQ_INSERT_TAIL(&lag
->lag_port_list
, p
, po_lag_port_list
);
3408 lag
->lag_port_count
++;
3409 if (g_bond
->verbose
) {
3410 timestamp_printf("[%s] Added to LAG (0x%04x," EA_FORMAT
"0x%04x)\n",
3411 bondport_get_name(p
),
3412 lag
->lag_info
.li_system_priority
,
3413 EA_LIST(&lag
->lag_info
.li_system
),
3414 lag
->lag_info
.li_key
);
3420 bondport_assign_to_LAG(bondport_ref p
)
3422 ifbond_ref bond
= p
->po_bond
;
3425 if (lacp_actor_partner_state_defaulted(p
->po_actor_state
)) {
3426 bondport_remove_from_LAG(p
);
3431 if (bondport_matches_LAG(p
, lag
)) {
3435 bondport_remove_from_LAG(p
);
3437 lag
= ifbond_get_LAG_matching_port(bond
, p
);
3439 bondport_add_to_LAG(p
, lag
);
3442 lag
= (LAG_ref
)_MALLOC(sizeof(*lag
), M_BOND
, M_WAITOK
);
3443 TAILQ_INIT(&lag
->lag_port_list
);
3444 lag
->lag_port_count
= 0;
3445 lag
->lag_selected_port_count
= 0;
3446 lag
->lag_info
= p
->po_partner_state
.ps_lag_info
;
3447 TAILQ_INSERT_TAIL(&bond
->ifb_lag_list
, lag
, lag_list
);
3448 if (g_bond
->verbose
) {
3449 timestamp_printf("Key 0x%04x: LAG Created (0x%04x," EA_FORMAT
3452 lag
->lag_info
.li_system_priority
,
3453 EA_LIST(&lag
->lag_info
.li_system
),
3454 lag
->lag_info
.li_key
);
3456 bondport_add_to_LAG(p
, lag
);
3461 bondport_receive_lacpdu(bondport_ref p
, lacpdu_ref in_lacpdu_p
)
3463 bondport_ref moved_port
;
3466 = ifbond_list_find_moved_port(p
, (const lacp_actor_partner_tlv_ref
)
3467 &in_lacpdu_p
->la_actor_tlv
);
3468 if (moved_port
!= NULL
) {
3469 bondport_receive_machine(moved_port
, LAEventPortMoved
, NULL
);
3471 bondport_receive_machine(p
, LAEventPacket
, in_lacpdu_p
);
3472 bondport_mux_machine(p
, LAEventPacket
, in_lacpdu_p
);
3473 bondport_periodic_transmit_machine(p
, LAEventPacket
, in_lacpdu_p
);
3478 bondport_set_selected(bondport_ref p
, SelectedState s
)
3480 if (s
!= p
->po_selected
) {
3481 ifbond_ref bond
= p
->po_bond
;
3482 LAG_ref lag
= p
->po_lag
;
3484 bondport_flags_set_selected_changed(p
);
3485 if (lag
!= NULL
&& bond
->ifb_active_lag
== lag
) {
3486 if (p
->po_selected
== SelectedState_SELECTED
) {
3487 lag
->lag_selected_port_count
--;
3489 else if (s
== SelectedState_SELECTED
) {
3490 lag
->lag_selected_port_count
++;
3492 if (g_bond
->verbose
) {
3493 timestamp_printf("[%s] SetSelected: %s (was %s)\n",
3494 bondport_get_name(p
),
3495 SelectedStateString(s
),
3496 SelectedStateString(p
->po_selected
));
3509 bondport_UpdateDefaultSelected(bondport_ref p
)
3511 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3516 bondport_RecordDefault(bondport_ref p
)
3518 bzero(&p
->po_partner_state
, sizeof(p
->po_partner_state
));
3520 = lacp_actor_partner_state_set_defaulted(p
->po_actor_state
);
3521 bondport_assign_to_LAG(p
);
3526 bondport_UpdateSelected(bondport_ref p
, lacpdu_ref lacpdu_p
)
3528 lacp_actor_partner_tlv_ref actor
;
3529 partner_state_ref ps
;
3532 /* compare the PDU's Actor information to our Partner state */
3533 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3534 ps
= &p
->po_partner_state
;
3535 ps_li
= &ps
->ps_lag_info
;
3536 if (lacp_actor_partner_tlv_get_port(actor
) != ps
->ps_port
3537 || (lacp_actor_partner_tlv_get_port_priority(actor
)
3538 != ps
->ps_port_priority
)
3539 || bcmp(actor
->lap_system
, &ps_li
->li_system
, sizeof(ps_li
->li_system
))
3540 || (lacp_actor_partner_tlv_get_system_priority(actor
)
3541 != ps_li
->li_system_priority
)
3542 || (lacp_actor_partner_tlv_get_key(actor
) != ps_li
->li_key
)
3543 || (lacp_actor_partner_state_aggregatable(actor
->lap_state
)
3544 != lacp_actor_partner_state_aggregatable(ps
->ps_state
))) {
3545 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3546 if (g_bond
->verbose
) {
3547 timestamp_printf("[%s] updateSelected UNSELECTED\n",
3548 bondport_get_name(p
));
3555 bondport_RecordPDU(bondport_ref p
, lacpdu_ref lacpdu_p
)
3557 lacp_actor_partner_tlv_ref actor
;
3558 ifbond_ref bond
= p
->po_bond
;
3559 int lacp_maintain
= 0;
3560 partner_state_ref ps
;
3561 lacp_actor_partner_tlv_ref partner
;
3564 /* copy the PDU's Actor information into our Partner state */
3565 actor
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_actor_tlv
;
3566 ps
= &p
->po_partner_state
;
3567 ps_li
= &ps
->ps_lag_info
;
3568 ps
->ps_port
= lacp_actor_partner_tlv_get_port(actor
);
3569 ps
->ps_port_priority
= lacp_actor_partner_tlv_get_port_priority(actor
);
3570 ps_li
->li_system
= *((lacp_system_ref
)actor
->lap_system
);
3571 ps_li
->li_system_priority
3572 = lacp_actor_partner_tlv_get_system_priority(actor
);
3573 ps_li
->li_key
= lacp_actor_partner_tlv_get_key(actor
);
3574 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(actor
->lap_state
);
3576 = lacp_actor_partner_state_set_not_defaulted(p
->po_actor_state
);
3578 /* compare the PDU's Partner information to our own information */
3579 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3581 if (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
3582 || (lacp_actor_partner_state_active_lacp(p
->po_actor_state
)
3583 && lacp_actor_partner_state_active_lacp(partner
->lap_state
))) {
3584 if (g_bond
->verbose
) {
3585 timestamp_printf("[%s] recordPDU: LACP will maintain\n",
3586 bondport_get_name(p
));
3590 if ((lacp_actor_partner_tlv_get_port(partner
)
3591 == bondport_get_index(p
))
3592 && lacp_actor_partner_tlv_get_port_priority(partner
) == p
->po_priority
3593 && bcmp(partner
->lap_system
, &g_bond
->system
,
3594 sizeof(g_bond
->system
)) == 0
3595 && (lacp_actor_partner_tlv_get_system_priority(partner
)
3596 == g_bond
->system_priority
)
3597 && lacp_actor_partner_tlv_get_key(partner
) == bond
->ifb_key
3598 && (lacp_actor_partner_state_aggregatable(partner
->lap_state
)
3599 == lacp_actor_partner_state_aggregatable(p
->po_actor_state
))
3600 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3602 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3603 if (g_bond
->verbose
) {
3604 timestamp_printf("[%s] recordPDU: LACP partner in sync\n",
3605 bondport_get_name(p
));
3608 else if (lacp_actor_partner_state_aggregatable(actor
->lap_state
) == 0
3609 && lacp_actor_partner_state_in_sync(actor
->lap_state
)
3611 ps
->ps_state
= lacp_actor_partner_state_set_in_sync(ps
->ps_state
);
3612 if (g_bond
->verbose
) {
3613 timestamp_printf("[%s] recordPDU: LACP partner in sync (ind)\n",
3614 bondport_get_name(p
));
3617 bondport_assign_to_LAG(p
);
3621 static __inline__ lacp_actor_partner_state
3622 updateNTTBits(lacp_actor_partner_state s
)
3624 return (s
& (LACP_ACTOR_PARTNER_STATE_LACP_ACTIVITY
3625 | LACP_ACTOR_PARTNER_STATE_LACP_TIMEOUT
3626 | LACP_ACTOR_PARTNER_STATE_AGGREGATION
3627 | LACP_ACTOR_PARTNER_STATE_SYNCHRONIZATION
));
3631 bondport_UpdateNTT(bondport_ref p
, lacpdu_ref lacpdu_p
)
3633 ifbond_ref bond
= p
->po_bond
;
3634 lacp_actor_partner_tlv_ref partner
;
3636 /* compare the PDU's Actor information to our Partner state */
3637 partner
= (lacp_actor_partner_tlv_ref
)lacpdu_p
->la_partner_tlv
;
3638 if ((lacp_actor_partner_tlv_get_port(partner
) != bondport_get_index(p
))
3639 || lacp_actor_partner_tlv_get_port_priority(partner
) != p
->po_priority
3640 || bcmp(partner
->lap_system
, &g_bond
->system
, sizeof(g_bond
->system
))
3641 || (lacp_actor_partner_tlv_get_system_priority(partner
)
3642 != g_bond
->system_priority
)
3643 || lacp_actor_partner_tlv_get_key(partner
) != bond
->ifb_key
3644 || (updateNTTBits(partner
->lap_state
)
3645 != updateNTTBits(p
->po_actor_state
))) {
3646 bondport_flags_set_ntt(p
);
3647 if (g_bond
->verbose
) {
3648 timestamp_printf("[%s] updateNTT: Need To Transmit\n",
3649 bondport_get_name(p
));
3656 bondport_AttachMuxToAggregator(bondport_ref p
)
3658 if (bondport_flags_mux_attached(p
) == 0) {
3659 if (g_bond
->verbose
) {
3660 timestamp_printf("[%s] Attached Mux To Aggregator\n",
3661 bondport_get_name(p
));
3663 bondport_flags_set_mux_attached(p
);
3669 bondport_DetachMuxFromAggregator(bondport_ref p
)
3671 if (bondport_flags_mux_attached(p
)) {
3672 if (g_bond
->verbose
) {
3673 timestamp_printf("[%s] Detached Mux From Aggregator\n",
3674 bondport_get_name(p
));
3676 bondport_flags_clear_mux_attached(p
);
3682 bondport_enable_distributing(bondport_ref p
)
3684 if (bondport_flags_distributing(p
) == 0) {
3685 ifbond_ref bond
= p
->po_bond
;
3687 bond
->ifb_distributing_array
[bond
->ifb_distributing_count
++] = p
;
3688 if (g_bond
->verbose
) {
3689 timestamp_printf("[%s] Distribution Enabled\n",
3690 bondport_get_name(p
));
3692 bondport_flags_set_distributing(p
);
3698 bondport_disable_distributing(bondport_ref p
)
3700 if (bondport_flags_distributing(p
)) {
3701 bondport_ref
* array
;
3707 array
= bond
->ifb_distributing_array
;
3708 count
= bond
->ifb_distributing_count
;
3709 for (i
= 0; i
< count
; i
++) {
3710 if (array
[i
] == p
) {
3713 for (j
= i
; j
< (count
- 1); j
++) {
3714 array
[j
] = array
[j
+ 1];
3719 bond
->ifb_distributing_count
--;
3720 if (g_bond
->verbose
) {
3721 timestamp_printf("[%s] Distribution Disabled\n",
3722 bondport_get_name(p
));
3724 bondport_flags_clear_distributing(p
);
3730 ** Receive machine functions
3733 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3736 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3739 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3742 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3745 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3748 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3752 bondport_receive_machine_event(bondport_ref p
, LAEvent event
,
3755 switch (p
->po_receive_state
) {
3756 case ReceiveState_none
:
3757 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
3759 case ReceiveState_INITIALIZE
:
3760 bondport_receive_machine_initialize(p
, event
, event_data
);
3762 case ReceiveState_PORT_DISABLED
:
3763 bondport_receive_machine_port_disabled(p
, event
, event_data
);
3765 case ReceiveState_EXPIRED
:
3766 bondport_receive_machine_expired(p
, event
, event_data
);
3768 case ReceiveState_LACP_DISABLED
:
3769 bondport_receive_machine_lacp_disabled(p
, event
, event_data
);
3771 case ReceiveState_DEFAULTED
:
3772 bondport_receive_machine_defaulted(p
, event
, event_data
);
3774 case ReceiveState_CURRENT
:
3775 bondport_receive_machine_current(p
, event
, event_data
);
3784 bondport_receive_machine(bondport_ref p
, LAEvent event
,
3789 if (p
->po_receive_state
!= ReceiveState_LACP_DISABLED
) {
3790 bondport_receive_machine_current(p
, event
, event_data
);
3793 case LAEventMediaChange
:
3794 if (media_active(&p
->po_media_info
)) {
3795 switch (p
->po_receive_state
) {
3796 case ReceiveState_PORT_DISABLED
:
3797 case ReceiveState_LACP_DISABLED
:
3798 bondport_receive_machine_port_disabled(p
, LAEventMediaChange
, NULL
);
3805 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
3809 bondport_receive_machine_event(p
, event
, event_data
);
3816 bondport_receive_machine_initialize(bondport_ref p
, LAEvent event
,
3817 __unused
void * event_data
)
3821 devtimer_cancel(p
->po_current_while_timer
);
3822 if (g_bond
->verbose
) {
3823 timestamp_printf("[%s] Receive INITIALIZE\n",
3824 bondport_get_name(p
));
3826 p
->po_receive_state
= ReceiveState_INITIALIZE
;
3827 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3828 bondport_RecordDefault(p
);
3830 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
3831 bondport_receive_machine_port_disabled(p
, LAEventStart
, NULL
);
3840 bondport_receive_machine_port_disabled(bondport_ref p
, LAEvent event
,
3841 __unused
void * event_data
)
3843 partner_state_ref ps
;
3847 devtimer_cancel(p
->po_current_while_timer
);
3848 if (g_bond
->verbose
) {
3849 timestamp_printf("[%s] Receive PORT_DISABLED\n",
3850 bondport_get_name(p
));
3852 p
->po_receive_state
= ReceiveState_PORT_DISABLED
;
3853 ps
= &p
->po_partner_state
;
3854 ps
->ps_state
= lacp_actor_partner_state_set_out_of_sync(ps
->ps_state
);
3856 case LAEventMediaChange
:
3857 if (media_active(&p
->po_media_info
)) {
3858 if (media_full_duplex(&p
->po_media_info
)) {
3859 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
3862 bondport_receive_machine_lacp_disabled(p
, LAEventStart
, NULL
);
3865 else if (p
->po_selected
== SelectedState_SELECTED
) {
3868 if (g_bond
->verbose
) {
3869 timestamp_printf("[%s] Receive PORT_DISABLED: "
3870 "link timer started\n",
3871 bondport_get_name(p
));
3875 devtimer_set_relative(p
->po_current_while_timer
, tv
,
3876 (devtimer_timeout_func
)
3877 bondport_receive_machine_port_disabled
,
3878 (void *)LAEventTimeout
, NULL
);
3880 else if (p
->po_selected
== SelectedState_STANDBY
) {
3881 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3884 case LAEventTimeout
:
3885 if (p
->po_selected
== SelectedState_SELECTED
) {
3886 if (g_bond
->verbose
) {
3887 timestamp_printf("[%s] Receive PORT_DISABLED: "
3888 "link timer completed, marking UNSELECTED\n",
3889 bondport_get_name(p
));
3891 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3894 case LAEventPortMoved
:
3895 bondport_receive_machine_initialize(p
, LAEventStart
, NULL
);
3904 bondport_receive_machine_expired(bondport_ref p
, LAEvent event
,
3905 __unused
void * event_data
)
3907 lacp_actor_partner_state s
;
3912 devtimer_cancel(p
->po_current_while_timer
);
3913 if (g_bond
->verbose
) {
3914 timestamp_printf("[%s] Receive EXPIRED\n",
3915 bondport_get_name(p
));
3917 p
->po_receive_state
= ReceiveState_EXPIRED
;
3918 s
= p
->po_partner_state
.ps_state
;
3919 s
= lacp_actor_partner_state_set_out_of_sync(s
);
3920 s
= lacp_actor_partner_state_set_short_timeout(s
);
3921 p
->po_partner_state
.ps_state
= s
;
3923 = lacp_actor_partner_state_set_expired(p
->po_actor_state
);
3924 /* start current_while timer */
3925 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
3927 devtimer_set_relative(p
->po_current_while_timer
, tv
,
3928 (devtimer_timeout_func
)
3929 bondport_receive_machine_expired
,
3930 (void *)LAEventTimeout
, NULL
);
3933 case LAEventTimeout
:
3934 bondport_receive_machine_defaulted(p
, LAEventStart
, NULL
);
3943 bondport_receive_machine_lacp_disabled(bondport_ref p
, LAEvent event
,
3944 __unused
void * event_data
)
3946 partner_state_ref ps
;
3949 devtimer_cancel(p
->po_current_while_timer
);
3950 if (g_bond
->verbose
) {
3951 timestamp_printf("[%s] Receive LACP_DISABLED\n",
3952 bondport_get_name(p
));
3954 p
->po_receive_state
= ReceiveState_LACP_DISABLED
;
3955 bondport_set_selected(p
, SelectedState_UNSELECTED
);
3956 bondport_RecordDefault(p
);
3957 ps
= &p
->po_partner_state
;
3958 ps
->ps_state
= lacp_actor_partner_state_set_individual(ps
->ps_state
);
3960 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
3969 bondport_receive_machine_defaulted(bondport_ref p
, LAEvent event
,
3970 __unused
void * event_data
)
3974 devtimer_cancel(p
->po_current_while_timer
);
3975 if (g_bond
->verbose
) {
3976 timestamp_printf("[%s] Receive DEFAULTED\n",
3977 bondport_get_name(p
));
3979 p
->po_receive_state
= ReceiveState_DEFAULTED
;
3980 bondport_UpdateDefaultSelected(p
);
3981 bondport_RecordDefault(p
);
3983 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
3992 bondport_receive_machine_current(bondport_ref p
, LAEvent event
,
3995 partner_state_ref ps
;
4000 devtimer_cancel(p
->po_current_while_timer
);
4001 if (g_bond
->verbose
) {
4002 timestamp_printf("[%s] Receive CURRENT\n",
4003 bondport_get_name(p
));
4005 p
->po_receive_state
= ReceiveState_CURRENT
;
4006 bondport_UpdateSelected(p
, event_data
);
4007 bondport_UpdateNTT(p
, event_data
);
4008 bondport_RecordPDU(p
, event_data
);
4010 = lacp_actor_partner_state_set_not_expired(p
->po_actor_state
);
4011 bondport_assign_to_LAG(p
);
4012 /* start current_while timer */
4013 ps
= &p
->po_partner_state
;
4014 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4015 tv
.tv_sec
= LACP_SHORT_TIMEOUT_TIME
;
4018 tv
.tv_sec
= LACP_LONG_TIMEOUT_TIME
;
4021 devtimer_set_relative(p
->po_current_while_timer
, tv
,
4022 (devtimer_timeout_func
)
4023 bondport_receive_machine_current
,
4024 (void *)LAEventTimeout
, NULL
);
4026 case LAEventTimeout
:
4027 bondport_receive_machine_expired(p
, LAEventStart
, NULL
);
4036 ** Periodic Transmission machine
4040 bondport_periodic_transmit_machine(bondport_ref p
, LAEvent event
,
4041 __unused
void * event_data
)
4044 partner_state_ref ps
;
4049 if (g_bond
->verbose
) {
4050 timestamp_printf("[%s] periodic_transmit Start\n",
4051 bondport_get_name(p
));
4054 case LAEventMediaChange
:
4055 devtimer_cancel(p
->po_periodic_timer
);
4056 p
->po_periodic_interval
= 0;
4057 if (media_active(&p
->po_media_info
) == 0
4058 || media_full_duplex(&p
->po_media_info
) == 0) {
4062 /* Neither Partner nor Actor are LACP Active, no periodic tx */
4063 ps
= &p
->po_partner_state
;
4064 if (lacp_actor_partner_state_active_lacp(p
->po_actor_state
) == 0
4065 && (lacp_actor_partner_state_active_lacp(ps
->ps_state
)
4067 devtimer_cancel(p
->po_periodic_timer
);
4068 p
->po_periodic_interval
= 0;
4071 if (lacp_actor_partner_state_short_timeout(ps
->ps_state
)) {
4072 interval
= LACP_FAST_PERIODIC_TIME
;
4075 interval
= LACP_SLOW_PERIODIC_TIME
;
4077 if (p
->po_periodic_interval
!= interval
) {
4078 if (interval
== LACP_FAST_PERIODIC_TIME
4079 && p
->po_periodic_interval
== LACP_SLOW_PERIODIC_TIME
) {
4080 if (g_bond
->verbose
) {
4081 timestamp_printf("[%s] periodic_transmit:"
4082 " Need To Transmit\n",
4083 bondport_get_name(p
));
4085 bondport_flags_set_ntt(p
);
4087 p
->po_periodic_interval
= interval
;
4089 tv
.tv_sec
= interval
;
4090 devtimer_set_relative(p
->po_periodic_timer
, tv
,
4091 (devtimer_timeout_func
)
4092 bondport_periodic_transmit_machine
,
4093 (void *)LAEventTimeout
, NULL
);
4094 if (g_bond
->verbose
) {
4095 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4096 bondport_get_name(p
),
4097 p
->po_periodic_interval
);
4101 case LAEventTimeout
:
4102 bondport_flags_set_ntt(p
);
4103 tv
.tv_sec
= p
->po_periodic_interval
;
4105 devtimer_set_relative(p
->po_periodic_timer
, tv
, (devtimer_timeout_func
)
4106 bondport_periodic_transmit_machine
,
4107 (void *)LAEventTimeout
, NULL
);
4108 if (g_bond
->verbose
> 1) {
4109 timestamp_printf("[%s] Periodic Transmission Timer: %d secs\n",
4110 bondport_get_name(p
), p
->po_periodic_interval
);
4123 bondport_can_transmit(bondport_ref p
, int32_t current_secs
,
4126 if (p
->po_last_transmit_secs
!= current_secs
) {
4127 p
->po_last_transmit_secs
= current_secs
;
4128 p
->po_n_transmit
= 0;
4130 if (p
->po_n_transmit
< LACP_PACKET_RATE
) {
4134 if (next_secs
!= NULL
) {
4135 *next_secs
= current_secs
+ 1;
4141 bondport_transmit_machine(bondport_ref p
, LAEvent event
,
4144 lacp_actor_partner_tlv_ref aptlv
;
4145 lacp_collector_tlv_ref ctlv
;
4146 struct timeval next_tick_time
= {0, 0};
4147 lacpdu_ref out_lacpdu_p
;
4148 packet_buffer_ref pkt
;
4149 partner_state_ref ps
;
4153 case LAEventTimeout
:
4155 if (p
->po_periodic_interval
== 0 || bondport_flags_ntt(p
) == 0) {
4158 if (event_data
!= NULL
) {
4159 /* we're going away, transmit the packet no matter what */
4161 else if (bondport_can_transmit(p
, devtimer_current_secs(),
4162 &next_tick_time
.tv_sec
) == 0) {
4163 if (devtimer_enabled(p
->po_transmit_timer
)) {
4164 if (g_bond
->verbose
> 0) {
4165 timestamp_printf("[%s] Transmit Timer Already Set\n",
4166 bondport_get_name(p
));
4170 devtimer_set_absolute(p
->po_transmit_timer
, next_tick_time
,
4171 (devtimer_timeout_func
)
4172 bondport_transmit_machine
,
4173 (void *)LAEventTimeout
, NULL
);
4174 if (g_bond
->verbose
> 0) {
4175 timestamp_printf("[%s] Transmit Timer Deadline %d secs\n",
4176 bondport_get_name(p
),
4177 next_tick_time
.tv_sec
);
4182 if (g_bond
->verbose
> 0) {
4183 if (event
== LAEventTimeout
) {
4184 timestamp_printf("[%s] Transmit Timer Complete\n",
4185 bondport_get_name(p
));
4188 pkt
= packet_buffer_allocate(sizeof(*out_lacpdu_p
));
4190 printf("[%s] Transmit: failed to allocate packet buffer\n",
4191 bondport_get_name(p
));
4194 out_lacpdu_p
= (lacpdu_ref
)packet_buffer_byteptr(pkt
);
4195 bzero(out_lacpdu_p
, sizeof(*out_lacpdu_p
));
4196 out_lacpdu_p
->la_subtype
= IEEE8023AD_SLOW_PROTO_SUBTYPE_LACP
;
4197 out_lacpdu_p
->la_version
= LACPDU_VERSION_1
;
4200 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_actor_tlv
;
4201 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_ACTOR
;
4202 aptlv
->lap_length
= LACPDU_ACTOR_TLV_LENGTH
;
4203 *((lacp_system_ref
)aptlv
->lap_system
) = g_bond
->system
;
4204 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4205 g_bond
->system_priority
);
4206 lacp_actor_partner_tlv_set_port_priority(aptlv
, p
->po_priority
);
4207 lacp_actor_partner_tlv_set_port(aptlv
, bondport_get_index(p
));
4208 lacp_actor_partner_tlv_set_key(aptlv
, p
->po_bond
->ifb_key
);
4209 aptlv
->lap_state
= p
->po_actor_state
;
4212 aptlv
= (lacp_actor_partner_tlv_ref
)out_lacpdu_p
->la_partner_tlv
;
4213 aptlv
->lap_tlv_type
= LACPDU_TLV_TYPE_PARTNER
;
4214 aptlv
->lap_length
= LACPDU_PARTNER_TLV_LENGTH
;
4215 ps
= &p
->po_partner_state
;
4216 ps_li
= &ps
->ps_lag_info
;
4217 lacp_actor_partner_tlv_set_port(aptlv
, ps
->ps_port
);
4218 lacp_actor_partner_tlv_set_port_priority(aptlv
, ps
->ps_port_priority
);
4219 *((lacp_system_ref
)aptlv
->lap_system
) = ps_li
->li_system
;
4220 lacp_actor_partner_tlv_set_system_priority(aptlv
,
4221 ps_li
->li_system_priority
);
4222 lacp_actor_partner_tlv_set_key(aptlv
, ps_li
->li_key
);
4223 aptlv
->lap_state
= ps
->ps_state
;
4226 ctlv
= (lacp_collector_tlv_ref
)out_lacpdu_p
->la_collector_tlv
;
4227 ctlv
->lac_tlv_type
= LACPDU_TLV_TYPE_COLLECTOR
;
4228 ctlv
->lac_length
= LACPDU_COLLECTOR_TLV_LENGTH
;
4230 bondport_slow_proto_transmit(p
, pkt
);
4231 bondport_flags_clear_ntt(p
);
4232 if (g_bond
->verbose
> 0) {
4233 timestamp_printf("[%s] Transmit Packet %d\n",
4234 bondport_get_name(p
), p
->po_n_transmit
);
4244 ** Mux machine functions
4248 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4251 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4254 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4258 bondport_mux_machine_collecting_distributing(bondport_ref p
, LAEvent event
,
4262 bondport_mux_machine(bondport_ref p
, LAEvent event
, void * event_data
)
4264 switch (p
->po_mux_state
) {
4266 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4268 case MuxState_DETACHED
:
4269 bondport_mux_machine_detached(p
, event
, event_data
);
4271 case MuxState_WAITING
:
4272 bondport_mux_machine_waiting(p
, event
, event_data
);
4274 case MuxState_ATTACHED
:
4275 bondport_mux_machine_attached(p
, event
, event_data
);
4277 case MuxState_COLLECTING_DISTRIBUTING
:
4278 bondport_mux_machine_collecting_distributing(p
, event
, event_data
);
4287 bondport_mux_machine_detached(bondport_ref p
, LAEvent event
,
4288 __unused
void * event_data
)
4290 lacp_actor_partner_state s
;
4294 devtimer_cancel(p
->po_wait_while_timer
);
4295 if (g_bond
->verbose
) {
4296 timestamp_printf("[%s] Mux DETACHED\n",
4297 bondport_get_name(p
));
4299 p
->po_mux_state
= MuxState_DETACHED
;
4300 bondport_flags_clear_ready(p
);
4301 bondport_DetachMuxFromAggregator(p
);
4302 bondport_disable_distributing(p
);
4303 s
= p
->po_actor_state
;
4304 s
= lacp_actor_partner_state_set_out_of_sync(s
);
4305 s
= lacp_actor_partner_state_set_not_collecting(s
);
4306 s
= lacp_actor_partner_state_set_not_distributing(s
);
4307 p
->po_actor_state
= s
;
4308 bondport_flags_set_ntt(p
);
4310 case LAEventSelectedChange
:
4312 case LAEventMediaChange
:
4313 if (p
->po_selected
== SelectedState_SELECTED
4314 || p
->po_selected
== SelectedState_STANDBY
) {
4315 bondport_mux_machine_waiting(p
, LAEventStart
, NULL
);
4325 bondport_mux_machine_waiting(bondport_ref p
, LAEvent event
,
4326 __unused
void * event_data
)
4332 devtimer_cancel(p
->po_wait_while_timer
);
4333 if (g_bond
->verbose
) {
4334 timestamp_printf("[%s] Mux WAITING\n",
4335 bondport_get_name(p
));
4337 p
->po_mux_state
= MuxState_WAITING
;
4340 case LAEventSelectedChange
:
4341 if (p
->po_selected
== SelectedState_UNSELECTED
) {
4342 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4345 if (p
->po_selected
== SelectedState_STANDBY
) {
4346 devtimer_cancel(p
->po_wait_while_timer
);
4347 /* wait until state changes to SELECTED */
4348 if (g_bond
->verbose
) {
4349 timestamp_printf("[%s] Mux WAITING: Standby\n",
4350 bondport_get_name(p
));
4354 if (bondport_flags_ready(p
)) {
4355 if (g_bond
->verbose
) {
4356 timestamp_printf("[%s] Mux WAITING: Port is already ready\n",
4357 bondport_get_name(p
));
4361 if (devtimer_enabled(p
->po_wait_while_timer
)) {
4362 if (g_bond
->verbose
) {
4363 timestamp_printf("[%s] Mux WAITING: Timer already set\n",
4364 bondport_get_name(p
));
4368 if (ifbond_all_ports_attached(p
->po_bond
, p
)) {
4369 devtimer_cancel(p
->po_wait_while_timer
);
4370 if (g_bond
->verbose
) {
4371 timestamp_printf("[%s] Mux WAITING: No waiting\n",
4372 bondport_get_name(p
));
4374 bondport_flags_set_ready(p
);
4377 if (g_bond
->verbose
) {
4378 timestamp_printf("[%s] Mux WAITING: 2 seconds\n",
4379 bondport_get_name(p
));
4381 tv
.tv_sec
= LACP_AGGREGATE_WAIT_TIME
;
4383 devtimer_set_relative(p
->po_wait_while_timer
, tv
,
4384 (devtimer_timeout_func
)
4385 bondport_mux_machine_waiting
,
4386 (void *)LAEventTimeout
, NULL
);
4388 case LAEventTimeout
:
4389 if (g_bond
->verbose
) {
4390 timestamp_printf("[%s] Mux WAITING: Ready\n",
4391 bondport_get_name(p
));
4393 bondport_flags_set_ready(p
);
4397 if (bondport_flags_ready(p
)){
4398 if (g_bond
->verbose
) {
4399 timestamp_printf("[%s] Mux WAITING: All Ports Ready\n",
4400 bondport_get_name(p
));
4402 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4411 bondport_mux_machine_attached(bondport_ref p
, LAEvent event
,
4412 __unused
void * event_data
)
4414 lacp_actor_partner_state s
;
4418 devtimer_cancel(p
->po_wait_while_timer
);
4419 if (g_bond
->verbose
) {
4420 timestamp_printf("[%s] Mux ATTACHED\n",
4421 bondport_get_name(p
));
4423 p
->po_mux_state
= MuxState_ATTACHED
;
4424 bondport_AttachMuxToAggregator(p
);
4425 s
= p
->po_actor_state
;
4426 s
= lacp_actor_partner_state_set_in_sync(s
);
4427 s
= lacp_actor_partner_state_set_not_collecting(s
);
4428 s
= lacp_actor_partner_state_set_not_distributing(s
);
4429 bondport_disable_distributing(p
);
4430 p
->po_actor_state
= s
;
4431 bondport_flags_set_ntt(p
);
4434 switch (p
->po_selected
) {
4435 case SelectedState_SELECTED
:
4436 s
= p
->po_partner_state
.ps_state
;
4437 if (lacp_actor_partner_state_in_sync(s
)) {
4438 bondport_mux_machine_collecting_distributing(p
, LAEventStart
,
4443 bondport_mux_machine_detached(p
, LAEventStart
, NULL
);
4452 bondport_mux_machine_collecting_distributing(bondport_ref p
,
4454 __unused
void * event_data
)
4456 lacp_actor_partner_state s
;
4460 devtimer_cancel(p
->po_wait_while_timer
);
4461 if (g_bond
->verbose
) {
4462 timestamp_printf("[%s] Mux COLLECTING_DISTRIBUTING\n",
4463 bondport_get_name(p
));
4465 p
->po_mux_state
= MuxState_COLLECTING_DISTRIBUTING
;
4466 bondport_enable_distributing(p
);
4467 s
= p
->po_actor_state
;
4468 s
= lacp_actor_partner_state_set_collecting(s
);
4469 s
= lacp_actor_partner_state_set_distributing(s
);
4470 p
->po_actor_state
= s
;
4471 bondport_flags_set_ntt(p
);
4474 s
= p
->po_partner_state
.ps_state
;
4475 if (lacp_actor_partner_state_in_sync(s
) == 0) {
4476 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);
4479 switch (p
->po_selected
) {
4480 case SelectedState_UNSELECTED
:
4481 case SelectedState_STANDBY
:
4482 bondport_mux_machine_attached(p
, LAEventStart
, NULL
);