forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_bond.c
4707 lines (4277 loc) · 118 KB
/
if_bond.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004-2014 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* if_bond.c
* - bond/failover interface
* - implements IEEE 802.3ad Link Aggregation
*/
/*
* Modification History:
*
* April 29, 2004 Dieter Siegmund ([email protected])
* - created
*/
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/kern_event.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/kpi_interface.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_types.h>
#include <net/if_bond_var.h>
#include <net/ieee8023ad.h>
#include <net/lacp.h>
#include <net/dlil.h>
#include <sys/time.h>
#include <net/devtimer.h>
#include <net/if_vlan_var.h>
#include <net/kpi_protocol.h>
#include <kern/locks.h>
#include <libkern/OSAtomic.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <net/if_media.h>
#include <net/multicast_list.h>
static struct ether_addr slow_proto_multicast = {
IEEE8023AD_SLOW_PROTO_MULTICAST
};
#define BOND_MAXUNIT 128
#define BONDNAME "bond"
#define M_BOND M_DEVBUF
#define EA_FORMAT "%x:%x:%x:%x:%x:%x"
#define EA_CH(e, i) ((u_char)((u_char *)(e))[(i)])
#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)
#define timestamp_printf printf
/**
** bond locks
**/
static __inline__ lck_grp_t *
my_lck_grp_alloc_init(const char * grp_name)
{
lck_grp_t * grp;
lck_grp_attr_t * grp_attrs;
grp_attrs = lck_grp_attr_alloc_init();
grp = lck_grp_alloc_init(grp_name, grp_attrs);
lck_grp_attr_free(grp_attrs);
return (grp);
}
static __inline__ lck_mtx_t *
my_lck_mtx_alloc_init(lck_grp_t * lck_grp)
{
lck_attr_t * lck_attrs;
lck_mtx_t * lck_mtx;
lck_attrs = lck_attr_alloc_init();
lck_mtx = lck_mtx_alloc_init(lck_grp, lck_attrs);
lck_attr_free(lck_attrs);
return (lck_mtx);
}
static lck_mtx_t * bond_lck_mtx;
static __inline__ void
bond_lock_init(void)
{
lck_grp_t * bond_lck_grp;
bond_lck_grp = my_lck_grp_alloc_init("if_bond");
bond_lck_mtx = my_lck_mtx_alloc_init(bond_lck_grp);
}
static __inline__ void
bond_assert_lock_held(void)
{
LCK_MTX_ASSERT(bond_lck_mtx, LCK_MTX_ASSERT_OWNED);
return;
}
static __inline__ void
bond_assert_lock_not_held(void)
{
LCK_MTX_ASSERT(bond_lck_mtx, LCK_MTX_ASSERT_NOTOWNED);
return;
}
static __inline__ void
bond_lock(void)
{
lck_mtx_lock(bond_lck_mtx);
return;
}
static __inline__ void
bond_unlock(void)
{
lck_mtx_unlock(bond_lck_mtx);
return;
}
/**
** bond structures, types
**/
struct LAG_info_s {
lacp_system li_system;
lacp_system_priority li_system_priority;
lacp_key li_key;
};
typedef struct LAG_info_s LAG_info, * LAG_info_ref;
struct bondport_s;
TAILQ_HEAD(port_list, bondport_s);
struct ifbond_s;
TAILQ_HEAD(ifbond_list, ifbond_s);
struct LAG_s;
TAILQ_HEAD(lag_list, LAG_s);
typedef struct ifbond_s ifbond, * ifbond_ref;
typedef struct bondport_s bondport, * bondport_ref;
struct LAG_s {
TAILQ_ENTRY(LAG_s) lag_list;
struct port_list lag_port_list;
short lag_port_count;
short lag_selected_port_count;
int lag_active_media;
LAG_info lag_info;
};
typedef struct LAG_s LAG, * LAG_ref;
typedef struct partner_state_s {
LAG_info ps_lag_info;
lacp_port ps_port;
lacp_port_priority ps_port_priority;
lacp_actor_partner_state ps_state;
} partner_state, * partner_state_ref;
struct ifbond_s {
TAILQ_ENTRY(ifbond_s) ifb_bond_list;
int ifb_flags;
SInt32 ifb_retain_count;
char ifb_name[IFNAMSIZ];
struct ifnet * ifb_ifp;
bpf_packet_func ifb_bpf_input;
bpf_packet_func ifb_bpf_output;
int ifb_altmtu;
struct port_list ifb_port_list;
short ifb_port_count;
struct lag_list ifb_lag_list;
lacp_key ifb_key;
short ifb_max_active; /* 0 == unlimited */
LAG_ref ifb_active_lag;
struct ifmultiaddr * ifb_ifma_slow_proto;
bondport_ref * ifb_distributing_array;
int ifb_distributing_count;
int ifb_last_link_event;
int ifb_mode; /* LACP, STATIC */
};
struct media_info {
int mi_active;
int mi_status;
};
enum {
ReceiveState_none = 0,
ReceiveState_INITIALIZE = 1,
ReceiveState_PORT_DISABLED = 2,
ReceiveState_EXPIRED = 3,
ReceiveState_LACP_DISABLED = 4,
ReceiveState_DEFAULTED = 5,
ReceiveState_CURRENT = 6,
};
typedef u_char ReceiveState;
enum {
SelectedState_UNSELECTED = IF_BOND_STATUS_SELECTED_STATE_UNSELECTED,
SelectedState_SELECTED = IF_BOND_STATUS_SELECTED_STATE_SELECTED,
SelectedState_STANDBY = IF_BOND_STATUS_SELECTED_STATE_STANDBY
};
typedef u_char SelectedState;
static __inline__ const char *
SelectedStateString(SelectedState s)
{
static const char * names[] = { "UNSELECTED", "SELECTED", "STANDBY" };
if (s <= SelectedState_STANDBY) {
return (names[s]);
}
return ("<unknown>");
}
enum {
MuxState_none = 0,
MuxState_DETACHED = 1,
MuxState_WAITING = 2,
MuxState_ATTACHED = 3,
MuxState_COLLECTING_DISTRIBUTING = 4,
};
typedef u_char MuxState;
struct bondport_s {
TAILQ_ENTRY(bondport_s) po_port_list;
ifbond_ref po_bond;
struct multicast_list po_multicast;
struct ifnet * po_ifp;
struct ether_addr po_saved_addr;
int po_enabled;
char po_name[IFNAMSIZ];
struct ifdevmtu po_devmtu;
/* LACP */
TAILQ_ENTRY(bondport_s) po_lag_port_list;
devtimer_ref po_current_while_timer;
devtimer_ref po_periodic_timer;
devtimer_ref po_wait_while_timer;
devtimer_ref po_transmit_timer;
partner_state po_partner_state;
lacp_port_priority po_priority;
lacp_actor_partner_state po_actor_state;
u_char po_flags;
u_char po_periodic_interval;
u_char po_n_transmit;
ReceiveState po_receive_state;
MuxState po_mux_state;
SelectedState po_selected;
int32_t po_last_transmit_secs;
struct media_info po_media_info;
LAG_ref po_lag;
};
#define IFBF_PROMISC 0x1 /* promiscuous mode */
#define IFBF_IF_DETACHING 0x2 /* interface is detaching */
#define IFBF_LLADDR 0x4 /* specific link address requested */
#define IFBF_CHANGE_IN_PROGRESS 0x8 /* interface add/remove in progress */
static int bond_get_status(ifbond_ref ifb, struct if_bond_req * ibr_p,
user_addr_t datap);
static __inline__ int
ifbond_flags_if_detaching(ifbond_ref ifb)
{
return ((ifb->ifb_flags & IFBF_IF_DETACHING) != 0);
}
static __inline__ void
ifbond_flags_set_if_detaching(ifbond_ref ifb)
{
ifb->ifb_flags |= IFBF_IF_DETACHING;
return;
}
static __inline__ int
ifbond_flags_lladdr(ifbond_ref ifb)
{
return ((ifb->ifb_flags & IFBF_LLADDR) != 0);
}
static __inline__ int
ifbond_flags_change_in_progress(ifbond_ref ifb)
{
return ((ifb->ifb_flags & IFBF_CHANGE_IN_PROGRESS) != 0);
}
static __inline__ void
ifbond_flags_set_change_in_progress(ifbond_ref ifb)
{
ifb->ifb_flags |= IFBF_CHANGE_IN_PROGRESS;
return;
}
static __inline__ void
ifbond_flags_clear_change_in_progress(ifbond_ref ifb)
{
ifb->ifb_flags &= ~IFBF_CHANGE_IN_PROGRESS;
return;
}
/*
* bondport_ref->po_flags bits
*/
#define BONDPORT_FLAGS_NTT 0x01
#define BONDPORT_FLAGS_READY 0x02
#define BONDPORT_FLAGS_SELECTED_CHANGED 0x04
#define BONDPORT_FLAGS_MUX_ATTACHED 0x08
#define BONDPORT_FLAGS_DISTRIBUTING 0x10
#define BONDPORT_FLAGS_UNUSED2 0x20
#define BONDPORT_FLAGS_UNUSED3 0x40
#define BONDPORT_FLAGS_UNUSED4 0x80
static __inline__ void
bondport_flags_set_ntt(bondport_ref p)
{
p->po_flags |= BONDPORT_FLAGS_NTT;
return;
}
static __inline__ void
bondport_flags_clear_ntt(bondport_ref p)
{
p->po_flags &= ~BONDPORT_FLAGS_NTT;
return;
}
static __inline__ int
bondport_flags_ntt(bondport_ref p)
{
return ((p->po_flags & BONDPORT_FLAGS_NTT) != 0);
}
static __inline__ void
bondport_flags_set_ready(bondport_ref p)
{
p->po_flags |= BONDPORT_FLAGS_READY;
return;
}
static __inline__ void
bondport_flags_clear_ready(bondport_ref p)
{
p->po_flags &= ~BONDPORT_FLAGS_READY;
return;
}
static __inline__ int
bondport_flags_ready(bondport_ref p)
{
return ((p->po_flags & BONDPORT_FLAGS_READY) != 0);
}
static __inline__ void
bondport_flags_set_selected_changed(bondport_ref p)
{
p->po_flags |= BONDPORT_FLAGS_SELECTED_CHANGED;
return;
}
static __inline__ void
bondport_flags_clear_selected_changed(bondport_ref p)
{
p->po_flags &= ~BONDPORT_FLAGS_SELECTED_CHANGED;
return;
}
static __inline__ int
bondport_flags_selected_changed(bondport_ref p)
{
return ((p->po_flags & BONDPORT_FLAGS_SELECTED_CHANGED) != 0);
}
static __inline__ void
bondport_flags_set_mux_attached(bondport_ref p)
{
p->po_flags |= BONDPORT_FLAGS_MUX_ATTACHED;
return;
}
static __inline__ void
bondport_flags_clear_mux_attached(bondport_ref p)
{
p->po_flags &= ~BONDPORT_FLAGS_MUX_ATTACHED;
return;
}
static __inline__ int
bondport_flags_mux_attached(bondport_ref p)
{
return ((p->po_flags & BONDPORT_FLAGS_MUX_ATTACHED) != 0);
}
static __inline__ void
bondport_flags_set_distributing(bondport_ref p)
{
p->po_flags |= BONDPORT_FLAGS_DISTRIBUTING;
return;
}
static __inline__ void
bondport_flags_clear_distributing(bondport_ref p)
{
p->po_flags &= ~BONDPORT_FLAGS_DISTRIBUTING;
return;
}
static __inline__ int
bondport_flags_distributing(bondport_ref p)
{
return ((p->po_flags & BONDPORT_FLAGS_DISTRIBUTING) != 0);
}
typedef struct bond_globals_s {
struct ifbond_list ifbond_list;
lacp_system system;
lacp_system_priority system_priority;
int verbose;
} * bond_globals_ref;
static bond_globals_ref g_bond;
/**
** packet_buffer routines
** - thin wrapper for mbuf
**/
typedef struct mbuf * packet_buffer_ref;
static packet_buffer_ref
packet_buffer_allocate(int length)
{
packet_buffer_ref m;
int size;
/* leave room for ethernet header */
size = length + sizeof(struct ether_header);
if (size > (int)MHLEN) {
if (size > (int)MCLBYTES) {
printf("bond: packet_buffer_allocate size %d > max %u\n",
size, MCLBYTES);
return (NULL);
}
m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
} else {
m = m_gethdr(M_WAITOK, MT_DATA);
}
if (m == NULL) {
return (NULL);
}
m->m_len = size;
m->m_pkthdr.len = size;
return (m);
}
static void *
packet_buffer_byteptr(packet_buffer_ref buf)
{
return (buf->m_data + sizeof(struct ether_header));
}
typedef enum {
LAEventStart,
LAEventTimeout,
LAEventPacket,
LAEventMediaChange,
LAEventSelectedChange,
LAEventPortMoved,
LAEventReady
} LAEvent;
/**
** Receive machine
**/
static void
bondport_receive_machine(bondport_ref p, LAEvent event,
void * event_data);
/**
** Periodic Transmission machine
**/
static void
bondport_periodic_transmit_machine(bondport_ref p, LAEvent event,
void * event_data);
/**
** Transmit machine
**/
#define TRANSMIT_MACHINE_TX_IMMEDIATE ((void *)1)
static void
bondport_transmit_machine(bondport_ref p, LAEvent event,
void * event_data);
/**
** Mux machine
**/
static void
bondport_mux_machine(bondport_ref p, LAEvent event,
void * event_data);
/**
** bond, LAG
**/
static void
ifbond_activate_LAG(ifbond_ref bond, LAG_ref lag, int active_media);
static void
ifbond_deactivate_LAG(ifbond_ref bond, LAG_ref lag);
static int
ifbond_all_ports_ready(ifbond_ref bond);
static LAG_ref
ifbond_find_best_LAG(ifbond_ref bond, int * active_media);
static int
LAG_get_aggregatable_port_count(LAG_ref lag, int * active_media);
static int
ifbond_selection(ifbond_ref bond);
/**
** bondport
**/
static void
bondport_receive_lacpdu(bondport_ref p, lacpdu_ref in_lacpdu_p);
static void
bondport_slow_proto_transmit(bondport_ref p, packet_buffer_ref buf);
static bondport_ref
bondport_create(struct ifnet * port_ifp, lacp_port_priority priority,
int active, int short_timeout, int * error);
static void
bondport_start(bondport_ref p);
static void
bondport_free(bondport_ref p);
static int
bondport_aggregatable(bondport_ref p);
static int
bondport_remove_from_LAG(bondport_ref p);
static void
bondport_set_selected(bondport_ref p, SelectedState s);
static int
bondport_matches_LAG(bondport_ref p, LAG_ref lag);
static void
bondport_link_status_changed(bondport_ref p);
static void
bondport_enable_distributing(bondport_ref p);
static void
bondport_disable_distributing(bondport_ref p);
static __inline__ int
bondport_collecting(bondport_ref p)
{
if (p->po_bond->ifb_mode == IF_BOND_MODE_LACP) {
return (lacp_actor_partner_state_collecting(p->po_actor_state));
}
return (TRUE);
}
/**
** bond interface/dlil specific routines
**/
static int bond_clone_create(struct if_clone *, u_int32_t, void *);
static int bond_clone_destroy(struct ifnet *);
static int bond_input(ifnet_t ifp, protocol_family_t protocol, mbuf_t m,
char *frame_header);
static int bond_output(struct ifnet *ifp, struct mbuf *m);
static int bond_ioctl(struct ifnet *ifp, u_long cmd, void * addr);
static int bond_set_bpf_tap(struct ifnet * ifp, bpf_tap_mode mode,
bpf_packet_func func);
static int bond_attach_protocol(struct ifnet *ifp);
static int bond_detach_protocol(struct ifnet *ifp);
static int bond_setmulti(struct ifnet *ifp);
static int bond_add_interface(struct ifnet * ifp, struct ifnet * port_ifp);
static int bond_remove_interface(ifbond_ref ifb, struct ifnet * port_ifp);
static void bond_if_free(struct ifnet * ifp);
static struct if_clone bond_cloner = IF_CLONE_INITIALIZER(BONDNAME,
bond_clone_create,
bond_clone_destroy,
0,
BOND_MAXUNIT);
static void interface_link_event(struct ifnet * ifp, u_int32_t event_code);
static int
siocsifmtu(struct ifnet * ifp, int mtu)
{
struct ifreq ifr;
bzero(&ifr, sizeof(ifr));
ifr.ifr_mtu = mtu;
return (ifnet_ioctl(ifp, 0, SIOCSIFMTU, &ifr));
}
static int
siocgifdevmtu(struct ifnet * ifp, struct ifdevmtu * ifdm_p)
{
struct ifreq ifr;
int error;
bzero(&ifr, sizeof(ifr));
error = ifnet_ioctl(ifp, 0, SIOCGIFDEVMTU, &ifr);
if (error == 0) {
*ifdm_p = ifr.ifr_devmtu;
}
return (error);
}
static __inline__ void
ether_addr_copy(void * dest, const void * source)
{
bcopy(source, dest, ETHER_ADDR_LEN);
return;
}
static __inline__ void
ifbond_retain(ifbond_ref ifb)
{
OSIncrementAtomic(&ifb->ifb_retain_count);
}
static __inline__ void
ifbond_release(ifbond_ref ifb)
{
UInt32 old_retain_count;
old_retain_count = OSDecrementAtomic(&ifb->ifb_retain_count);
switch (old_retain_count) {
case 0:
panic("ifbond_release: retain count is 0\n");
break;
case 1:
if (g_bond->verbose) {
printf("ifbond_release(%s)\n", ifb->ifb_name);
}
if (ifb->ifb_ifma_slow_proto != NULL) {
if (g_bond->verbose) {
printf("ifbond_release(%s) removing multicast\n",
ifb->ifb_name);
}
(void) if_delmulti_anon(ifb->ifb_ifma_slow_proto->ifma_ifp,
ifb->ifb_ifma_slow_proto->ifma_addr);
IFMA_REMREF(ifb->ifb_ifma_slow_proto);
}
if (ifb->ifb_distributing_array != NULL) {
FREE(ifb->ifb_distributing_array, M_BOND);
}
FREE(ifb, M_BOND);
break;
default:
break;
}
return;
}
/*
* Function: ifbond_wait
* Purpose:
* Allows a single thread to gain exclusive access to the ifbond
* data structure. Some operations take a long time to complete,
* and some have side-effects that we can't predict. Holding the
* bond_lock() across such operations is not possible.
*
* For example:
* 1) The SIOCSIFLLADDR ioctl takes a long time (several seconds) to
* complete. Simply holding the bond_lock() would freeze all other
* data structure accesses during that time.
* 2) When we attach our protocol to the interface, a dlil event is
* generated and invokes our bond_event() function. bond_event()
* needs to take the bond_lock(), but we're already holding it, so
* we're deadlocked against ourselves.
* Notes:
* Before calling, you must be holding the bond_lock and have taken
* a reference on the ifbond_ref.
*/
static void
ifbond_wait(ifbond_ref ifb, const char * msg)
{
int waited = 0;
/* other add/remove in progress */
while (ifbond_flags_change_in_progress(ifb)) {
if (g_bond->verbose) {
printf("%s: %s msleep\n", ifb->ifb_name, msg);
}
waited = 1;
(void)msleep(ifb, bond_lck_mtx, PZERO, msg, 0);
}
/* prevent other bond list remove/add from taking place */
ifbond_flags_set_change_in_progress(ifb);
if (g_bond->verbose && waited) {
printf("%s: %s woke up\n", ifb->ifb_name, msg);
}
return;
}
/*
* Function: ifbond_signal
* Purpose:
* Allows the thread that previously invoked ifbond_wait() to
* give up exclusive access to the ifbond data structure, and wake up
* any other threads waiting to access
* Notes:
* Before calling, you must be holding the bond_lock and have taken
* a reference on the ifbond_ref.
*/
static void
ifbond_signal(ifbond_ref ifb, const char * msg)
{
ifbond_flags_clear_change_in_progress(ifb);
wakeup((caddr_t)ifb);
if (g_bond->verbose) {
printf("%s: %s wakeup\n", ifb->ifb_name, msg);
}
return;
}
/**
** Media information
**/
static int
link_speed(int active)
{
switch (IFM_SUBTYPE(active)) {
case IFM_10_T:
case IFM_10_2:
case IFM_10_5:
case IFM_10_STP:
case IFM_10_FL:
return (10);
case IFM_100_TX:
case IFM_100_FX:
case IFM_100_T4:
case IFM_100_VG:
case IFM_100_T2:
return (100);
case IFM_1000_SX:
case IFM_1000_LX:
case IFM_1000_CX:
case IFM_1000_TX:
return (1000);
case IFM_HPNA_1:
return (0);
default:
/* assume that new defined types are going to be at least 10GigE */
case IFM_10G_SR:
case IFM_10G_LR:
return (10000);
case IFM_2500_T:
return (2500);
case IFM_5000_T:
return (5000);
}
}
static __inline__ int
media_active(const struct media_info * mi)
{
if ((mi->mi_status & IFM_AVALID) == 0) {
return (1);
}
return ((mi->mi_status & IFM_ACTIVE) != 0);
}
static __inline__ int
media_full_duplex(const struct media_info * mi)
{
return ((mi->mi_active & IFM_FDX) != 0);
}
static __inline__ int
media_speed(const struct media_info * mi)
{
return (link_speed(mi->mi_active));
}
static struct media_info
interface_media_info(struct ifnet * ifp)
{
struct ifmediareq ifmr;
struct media_info mi;
bzero(&mi, sizeof(mi));
bzero(&ifmr, sizeof(ifmr));
if (ifnet_ioctl(ifp, 0, SIOCGIFMEDIA, &ifmr) == 0) {
if (ifmr.ifm_count != 0) {
mi.mi_status = ifmr.ifm_status;
mi.mi_active = ifmr.ifm_active;
}
}
return (mi);
}
static int
if_siflladdr(struct ifnet * ifp, const struct ether_addr * ea_p)
{
struct ifreq ifr;
/*
* XXX setting the sa_len to ETHER_ADDR_LEN is wrong, but the driver
* currently expects it that way
*/
ifr.ifr_addr.sa_family = AF_UNSPEC;
ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
ether_addr_copy(ifr.ifr_addr.sa_data, ea_p);
return (ifnet_ioctl(ifp, 0, SIOCSIFLLADDR, &ifr));
}
/**
** bond_globals
**/
static bond_globals_ref
bond_globals_create(lacp_system_priority sys_pri,
lacp_system_ref sys)
{
bond_globals_ref b;
b = _MALLOC(sizeof(*b), M_BOND, M_WAITOK | M_ZERO);
if (b == NULL) {
return (NULL);
}
TAILQ_INIT(&b->ifbond_list);
b->system = *sys;
b->system_priority = sys_pri;
return (b);
}
static int
bond_globals_init(void)
{
bond_globals_ref b;
int i;
struct ifnet * ifp;
bond_assert_lock_not_held();
if (g_bond != NULL) {
return (0);
}
/*
* use en0's ethernet address as the system identifier, and if it's not
* there, use en1 .. en3
*/
ifp = NULL;
for (i = 0; i < 4; i++) {
char ifname[IFNAMSIZ+1];
snprintf(ifname, sizeof(ifname), "en%d", i);
ifp = ifunit(ifname);
if (ifp != NULL) {
break;
}
}
b = NULL;
if (ifp != NULL) {
b = bond_globals_create(0x8000, (lacp_system_ref)IF_LLADDR(ifp));
}
bond_lock();
if (g_bond != NULL) {
bond_unlock();
_FREE(b, M_BOND);
return (0);
}
g_bond = b;
bond_unlock();
if (ifp == NULL) {
return (ENXIO);
}
if (b == NULL) {
return (ENOMEM);
}
return (0);
}
static void
bond_bpf_vlan(struct ifnet * ifp, struct mbuf * m,
const struct ether_header * eh_p,
u_int16_t vlan_tag, bpf_packet_func func)
{
struct ether_vlan_header * vlh_p;
struct mbuf * vl_m;
vl_m = m_get(M_DONTWAIT, MT_DATA);
if (vl_m == NULL) {
return;
}
/* populate a new mbuf containing the vlan ethernet header */
vl_m->m_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
vlh_p = mtod(vl_m, struct ether_vlan_header *);
bcopy(eh_p, vlh_p, offsetof(struct ether_header, ether_type));
vlh_p->evl_encap_proto = htons(ETHERTYPE_VLAN);
vlh_p->evl_tag = htons(vlan_tag);
vlh_p->evl_proto = eh_p->ether_type;
vl_m->m_next = m;
(*func)(ifp, vl_m);
vl_m->m_next = NULL;
m_free(vl_m);
return;
}
static __inline__ void
bond_bpf_output(struct ifnet * ifp, struct mbuf * m,
bpf_packet_func func)
{
if (func != NULL) {
if (m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) {
const struct ether_header * eh_p;
eh_p = mtod(m, const struct ether_header *);
m->m_data += ETHER_HDR_LEN;
m->m_len -= ETHER_HDR_LEN;
bond_bpf_vlan(ifp, m, eh_p, m->m_pkthdr.vlan_tag, func);
m->m_data -= ETHER_HDR_LEN;
m->m_len += ETHER_HDR_LEN;
} else {
(*func)(ifp, m);
}
}
return;
}
static __inline__ void
bond_bpf_input(ifnet_t ifp, mbuf_t m, const struct ether_header * eh_p,
bpf_packet_func func)
{
if (func != NULL) {
if (m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) {
bond_bpf_vlan(ifp, m, eh_p, m->m_pkthdr.vlan_tag, func);
} else {
/* restore the header */
m->m_data -= ETHER_HDR_LEN;
m->m_len += ETHER_HDR_LEN;
(*func)(ifp, m);
m->m_data += ETHER_HDR_LEN;
m->m_len -= ETHER_HDR_LEN;
}
}
return;
}
/*
* Function: bond_setmulti
* Purpose:
* Enable multicast reception on "our" interface by enabling multicasts on
* each of the member ports.
*/