/
aggr_grp.c
3378 lines (2915 loc) · 87.3 KB
/
aggr_grp.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 Joyent, Inc.
*/
/*
* IEEE 802.3ad Link Aggregation -- Link Aggregation Groups.
*
* An instance of the structure aggr_grp_t is allocated for each
* link aggregation group. When created, aggr_grp_t objects are
* entered into the aggr_grp_hash hash table maintained by the modhash
* module. The hash key is the linkid associated with the link
* aggregation group.
*
* Each aggregation contains a set of ports. The port is represented
* by the aggr_port_t structure. A port consists of a single MAC
* client which has exclusive (MCIS_EXCLUSIVE) use of the underlying
* MAC. This client is used by the aggr to send and receive LACP
* traffic. Each port client takes on the same MAC unicast address --
* the address of the aggregation itself (taken from the first port by
* default).
*
* The MAC client that hangs off each aggr port is not your typical
* MAC client. Not only does it have exclusive control of the MAC, but
* it also has no Tx or Rx SRSes. An SRS is designed to queue and
* fanout traffic among L4 protocols; but the aggr is an intermediary,
* not a consumer. Instead of using SRSes, the aggr puts the
* underlying hardware rings into passthru mode and ships packets up
* via a direct call to aggr_recv_cb(). This allows aggr to enforce
* LACP while passing all other traffic up to clients of the aggr.
*
* Pseudo Rx Groups and Rings
* --------------------------
*
* It is imperative for client performance that the aggr provide as
* many MAC groups as possible. In order to use the underlying HW
* resources, aggr creates pseudo groups to aggregate the underlying
* HW groups. Every HW group gets mapped to a pseudo group; and every
* HW ring in that group gets mapped to a pseudo ring. The pseudo
* group at index 0 combines all the HW groups at index 0 from each
* port, etc. The aggr's MAC then creates normal MAC groups and rings
* out of these pseudo groups and rings to present to the aggr's
* clients. To the clients, the aggr's groups and rings are absolutely
* no different than a NIC's groups or rings.
*
* Pseudo Tx Rings
* ---------------
*
* The underlying ports (NICs) in an aggregation can have Tx rings. To
* enhance aggr's performance, these Tx rings are made available to
* the aggr layer as pseudo Tx rings. The concept of pseudo rings are
* not new. They are already present and implemented on the Rx side.
* The same concept is extended to the Tx side where each Tx ring of
* an underlying port is reflected in aggr as a pseudo Tx ring. Thus
* each pseudo Tx ring will map to a specific hardware Tx ring. Even
* in the case of a NIC that does not have a Tx ring, a pseudo Tx ring
* is given to the aggregation layer.
*
* With this change, the outgoing stack depth looks much better:
*
* mac_tx() -> mac_tx_aggr_mode() -> mac_tx_soft_ring_process() ->
* mac_tx_send() -> aggr_ring_rx() -> <driver>_ring_tx()
*
* Two new modes are introduced to mac_tx() to handle aggr pseudo Tx rings:
* SRS_TX_AGGR and SRS_TX_BW_AGGR.
*
* In SRS_TX_AGGR mode, mac_tx_aggr_mode() routine is called. This routine
* invokes an aggr function, aggr_find_tx_ring(), to find a (pseudo) Tx
* ring belonging to a port on which the packet has to be sent.
* aggr_find_tx_ring() first finds the outgoing port based on L2/L3/L4
* policy and then uses the fanout_hint passed to it to pick a Tx ring from
* the selected port.
*
* In SRS_TX_BW_AGGR mode, mac_tx_bw_mode() function is called where
* bandwidth limit is applied first on the outgoing packet and the packets
* allowed to go out would call mac_tx_aggr_mode() to send the packet on a
* particular Tx ring.
*/
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/disp.h>
#include <sys/list.h>
#include <sys/ksynch.h>
#include <sys/kmem.h>
#include <sys/stream.h>
#include <sys/modctl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/atomic.h>
#include <sys/stat.h>
#include <sys/modhash.h>
#include <sys/id_space.h>
#include <sys/strsun.h>
#include <sys/cred.h>
#include <sys/dlpi.h>
#include <sys/zone.h>
#include <sys/mac_provider.h>
#include <sys/dls.h>
#include <sys/vlan.h>
#include <sys/aggr.h>
#include <sys/aggr_impl.h>
static int aggr_m_start(void *);
static void aggr_m_stop(void *);
static int aggr_m_promisc(void *, boolean_t);
static int aggr_m_multicst(void *, boolean_t, const uint8_t *);
static int aggr_m_unicst(void *, const uint8_t *);
static int aggr_m_stat(void *, uint_t, uint64_t *);
static void aggr_m_ioctl(void *, queue_t *, mblk_t *);
static boolean_t aggr_m_capab_get(void *, mac_capab_t, void *);
static int aggr_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
const void *);
static void aggr_m_propinfo(void *, const char *, mac_prop_id_t,
mac_prop_info_handle_t);
static aggr_port_t *aggr_grp_port_lookup(aggr_grp_t *, datalink_id_t);
static int aggr_grp_rem_port(aggr_grp_t *, aggr_port_t *, boolean_t *,
boolean_t *);
static void aggr_grp_capab_set(aggr_grp_t *);
static boolean_t aggr_grp_capab_check(aggr_grp_t *, aggr_port_t *);
static uint_t aggr_grp_max_sdu(aggr_grp_t *);
static uint32_t aggr_grp_max_margin(aggr_grp_t *);
static boolean_t aggr_grp_sdu_check(aggr_grp_t *, aggr_port_t *);
static boolean_t aggr_grp_margin_check(aggr_grp_t *, aggr_port_t *);
static int aggr_add_pseudo_rx_group(aggr_port_t *, aggr_pseudo_rx_group_t *);
static void aggr_rem_pseudo_rx_group(aggr_port_t *, aggr_pseudo_rx_group_t *);
static int aggr_pseudo_disable_intr(mac_intr_handle_t);
static int aggr_pseudo_enable_intr(mac_intr_handle_t);
static int aggr_pseudo_start_rx_ring(mac_ring_driver_t, uint64_t);
static void aggr_pseudo_stop_rx_ring(mac_ring_driver_t);
static int aggr_addmac(void *, const uint8_t *);
static int aggr_remmac(void *, const uint8_t *);
static int aggr_addvlan(mac_group_driver_t, uint16_t);
static int aggr_remvlan(mac_group_driver_t, uint16_t);
static mblk_t *aggr_rx_poll(void *, int);
static void aggr_fill_ring(void *, mac_ring_type_t, const int,
const int, mac_ring_info_t *, mac_ring_handle_t);
static void aggr_fill_group(void *, mac_ring_type_t, const int,
mac_group_info_t *, mac_group_handle_t);
static kmem_cache_t *aggr_grp_cache;
static mod_hash_t *aggr_grp_hash;
static krwlock_t aggr_grp_lock;
static uint_t aggr_grp_cnt;
static id_space_t *key_ids;
#define GRP_HASHSZ 64
#define GRP_HASH_KEY(linkid) ((mod_hash_key_t)(uintptr_t)linkid)
#define AGGR_PORT_NAME_DELIMIT '-'
static uchar_t aggr_zero_mac[] = {0, 0, 0, 0, 0, 0};
#define AGGR_M_CALLBACK_FLAGS \
(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_PROPINFO)
static mac_callbacks_t aggr_m_callbacks = {
AGGR_M_CALLBACK_FLAGS,
aggr_m_stat,
aggr_m_start,
aggr_m_stop,
aggr_m_promisc,
aggr_m_multicst,
NULL,
NULL,
NULL,
aggr_m_ioctl,
aggr_m_capab_get,
NULL,
NULL,
aggr_m_setprop,
NULL,
aggr_m_propinfo
};
/*ARGSUSED*/
static int
aggr_grp_constructor(void *buf, void *arg, int kmflag)
{
aggr_grp_t *grp = buf;
bzero(grp, sizeof (*grp));
mutex_init(&grp->lg_lacp_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&grp->lg_lacp_cv, NULL, CV_DEFAULT, NULL);
rw_init(&grp->lg_tx_lock, NULL, RW_DRIVER, NULL);
mutex_init(&grp->lg_port_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&grp->lg_port_cv, NULL, CV_DEFAULT, NULL);
mutex_init(&grp->lg_tx_flowctl_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&grp->lg_tx_flowctl_cv, NULL, CV_DEFAULT, NULL);
grp->lg_link_state = LINK_STATE_UNKNOWN;
return (0);
}
/*ARGSUSED*/
static void
aggr_grp_destructor(void *buf, void *arg)
{
aggr_grp_t *grp = buf;
if (grp->lg_tx_ports != NULL) {
kmem_free(grp->lg_tx_ports,
grp->lg_tx_ports_size * sizeof (aggr_port_t *));
}
mutex_destroy(&grp->lg_lacp_lock);
cv_destroy(&grp->lg_lacp_cv);
mutex_destroy(&grp->lg_port_lock);
cv_destroy(&grp->lg_port_cv);
rw_destroy(&grp->lg_tx_lock);
mutex_destroy(&grp->lg_tx_flowctl_lock);
cv_destroy(&grp->lg_tx_flowctl_cv);
}
void
aggr_grp_init(void)
{
aggr_grp_cache = kmem_cache_create("aggr_grp_cache",
sizeof (aggr_grp_t), 0, aggr_grp_constructor,
aggr_grp_destructor, NULL, NULL, NULL, 0);
aggr_grp_hash = mod_hash_create_idhash("aggr_grp_hash",
GRP_HASHSZ, mod_hash_null_valdtor);
rw_init(&aggr_grp_lock, NULL, RW_DEFAULT, NULL);
aggr_grp_cnt = 0;
/*
* Allocate an id space to manage key values (when key is not
* specified). The range of the id space will be from
* (AGGR_MAX_KEY + 1) to UINT16_MAX, because the LACP protocol
* uses a 16-bit key.
*/
key_ids = id_space_create("aggr_key_ids", AGGR_MAX_KEY + 1, UINT16_MAX);
ASSERT(key_ids != NULL);
}
void
aggr_grp_fini(void)
{
id_space_destroy(key_ids);
rw_destroy(&aggr_grp_lock);
mod_hash_destroy_idhash(aggr_grp_hash);
kmem_cache_destroy(aggr_grp_cache);
}
uint_t
aggr_grp_count(void)
{
uint_t count;
rw_enter(&aggr_grp_lock, RW_READER);
count = aggr_grp_cnt;
rw_exit(&aggr_grp_lock);
return (count);
}
/*
* Since both aggr_port_notify_cb() and aggr_port_timer_thread() functions
* requires the mac perimeter, this function holds a reference of the aggr
* and aggr won't call mac_unregister() until this reference drops to 0.
*/
void
aggr_grp_port_hold(aggr_port_t *port)
{
aggr_grp_t *grp = port->lp_grp;
AGGR_PORT_REFHOLD(port);
mutex_enter(&grp->lg_port_lock);
grp->lg_port_ref++;
mutex_exit(&grp->lg_port_lock);
}
/*
* Release the reference of the grp and inform aggr_grp_delete() calling
* mac_unregister() is now safe.
*/
void
aggr_grp_port_rele(aggr_port_t *port)
{
aggr_grp_t *grp = port->lp_grp;
mutex_enter(&grp->lg_port_lock);
if (--grp->lg_port_ref == 0)
cv_signal(&grp->lg_port_cv);
mutex_exit(&grp->lg_port_lock);
AGGR_PORT_REFRELE(port);
}
/*
* Wait for the port's lacp timer thread and the port's notification callback
* to exit.
*/
void
aggr_grp_port_wait(aggr_grp_t *grp)
{
mutex_enter(&grp->lg_port_lock);
if (grp->lg_port_ref != 0)
cv_wait(&grp->lg_port_cv, &grp->lg_port_lock);
mutex_exit(&grp->lg_port_lock);
}
/*
* Attach a port to a link aggregation group.
*
* A port is attached to a link aggregation group once its speed
* and link state have been verified.
*
* Returns B_TRUE if the group link state or speed has changed. If
* it's the case, the caller must notify the MAC layer via a call
* to mac_link().
*/
boolean_t
aggr_grp_attach_port(aggr_grp_t *grp, aggr_port_t *port)
{
boolean_t link_state_changed = B_FALSE;
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
ASSERT(MAC_PERIM_HELD(port->lp_mh));
if (port->lp_state == AGGR_PORT_STATE_ATTACHED)
return (B_FALSE);
/*
* Validate the MAC port link speed and update the group
* link speed if needed.
*/
if (port->lp_ifspeed == 0 ||
port->lp_link_state != LINK_STATE_UP ||
port->lp_link_duplex != LINK_DUPLEX_FULL) {
/*
* Can't attach a MAC port with unknown link speed,
* down link, or not in full duplex mode.
*/
return (B_FALSE);
}
mutex_enter(&grp->lg_stat_lock);
if (grp->lg_ifspeed == 0) {
/*
* The group inherits the speed of the first link being
* attached.
*/
grp->lg_ifspeed = port->lp_ifspeed;
link_state_changed = B_TRUE;
} else if (grp->lg_ifspeed != port->lp_ifspeed) {
/*
* The link speed of the MAC port must be the same as
* the group link speed, as per 802.3ad. Since it is
* not, the attach is cancelled.
*/
mutex_exit(&grp->lg_stat_lock);
return (B_FALSE);
}
mutex_exit(&grp->lg_stat_lock);
grp->lg_nattached_ports++;
/*
* Update the group link state.
*/
if (grp->lg_link_state != LINK_STATE_UP) {
grp->lg_link_state = LINK_STATE_UP;
mutex_enter(&grp->lg_stat_lock);
grp->lg_link_duplex = LINK_DUPLEX_FULL;
mutex_exit(&grp->lg_stat_lock);
link_state_changed = B_TRUE;
}
/*
* Update port's state.
*/
port->lp_state = AGGR_PORT_STATE_ATTACHED;
aggr_grp_multicst_port(port, B_TRUE);
/*
* The port client doesn't have an Rx SRS; instead of calling
* mac_rx_set() we set the client's flow callback directly.
* This datapath is used only when the port's driver doesn't
* support MAC_CAPAB_RINGS. Drivers with ring support will
* deliver traffic to the aggr via ring passthru.
*/
mac_client_set_flow_cb(port->lp_mch, aggr_recv_cb, port);
/*
* If LACP is OFF, the port can be used to send data as soon
* as its link is up and verified to be compatible with the
* aggregation.
*
* If LACP is active or passive, notify the LACP subsystem, which
* will enable sending on the port following the LACP protocol.
*/
if (grp->lg_lacp_mode == AGGR_LACP_OFF)
aggr_send_port_enable(port);
else
aggr_lacp_port_attached(port);
return (link_state_changed);
}
boolean_t
aggr_grp_detach_port(aggr_grp_t *grp, aggr_port_t *port)
{
boolean_t link_state_changed = B_FALSE;
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
ASSERT(MAC_PERIM_HELD(port->lp_mh));
/* update state */
if (port->lp_state != AGGR_PORT_STATE_ATTACHED)
return (B_FALSE);
mac_client_clear_flow_cb(port->lp_mch);
aggr_grp_multicst_port(port, B_FALSE);
if (grp->lg_lacp_mode == AGGR_LACP_OFF)
aggr_send_port_disable(port);
else
aggr_lacp_port_detached(port);
port->lp_state = AGGR_PORT_STATE_STANDBY;
grp->lg_nattached_ports--;
if (grp->lg_nattached_ports == 0) {
/* the last attached MAC port of the group is being detached */
grp->lg_link_state = LINK_STATE_DOWN;
mutex_enter(&grp->lg_stat_lock);
grp->lg_ifspeed = 0;
grp->lg_link_duplex = LINK_DUPLEX_UNKNOWN;
mutex_exit(&grp->lg_stat_lock);
link_state_changed = B_TRUE;
}
return (link_state_changed);
}
/*
* Update the MAC addresses of the constituent ports of the specified
* group. This function is invoked:
* - after creating a new aggregation group.
* - after adding new ports to an aggregation group.
* - after removing a port from a group when the MAC address of
* that port was used for the MAC address of the group.
* - after the MAC address of a port changed when the MAC address
* of that port was used for the MAC address of the group.
*
* Return true if the link state of the aggregation changed, for example
* as a result of a failure changing the MAC address of one of the
* constituent ports.
*/
boolean_t
aggr_grp_update_ports_mac(aggr_grp_t *grp)
{
aggr_port_t *cport;
boolean_t link_state_changed = B_FALSE;
mac_perim_handle_t mph;
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
for (cport = grp->lg_ports; cport != NULL;
cport = cport->lp_next) {
mac_perim_enter_by_mh(cport->lp_mh, &mph);
if (aggr_port_unicst(cport) != 0) {
if (aggr_grp_detach_port(grp, cport))
link_state_changed = B_TRUE;
} else {
/*
* If a port was detached because of a previous
* failure changing the MAC address, the port is
* reattached when it successfully changes the MAC
* address now, and this might cause the link state
* of the aggregation to change.
*/
if (aggr_grp_attach_port(grp, cport))
link_state_changed = B_TRUE;
}
mac_perim_exit(mph);
}
return (link_state_changed);
}
/*
* Invoked when the MAC address of a port has changed. If the port's
* MAC address was used for the group MAC address, set mac_addr_changedp
* to B_TRUE to indicate to the caller that it should send a MAC_NOTE_UNICST
* notification. If the link state changes due to detach/attach of
* the constituent port, set link_state_changedp to B_TRUE to indicate
* to the caller that it should send a MAC_NOTE_LINK notification. In both
* cases, it is the responsibility of the caller to invoke notification
* functions after releasing the the port lock.
*/
void
aggr_grp_port_mac_changed(aggr_grp_t *grp, aggr_port_t *port,
boolean_t *mac_addr_changedp, boolean_t *link_state_changedp)
{
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
ASSERT(MAC_PERIM_HELD(port->lp_mh));
ASSERT(mac_addr_changedp != NULL);
ASSERT(link_state_changedp != NULL);
*mac_addr_changedp = B_FALSE;
*link_state_changedp = B_FALSE;
if (grp->lg_addr_fixed) {
/*
* The group is using a fixed MAC address or an automatic
* MAC address has not been set.
*/
return;
}
if (grp->lg_mac_addr_port == port) {
/*
* The MAC address of the port was assigned to the group
* MAC address. Update the group MAC address.
*/
bcopy(port->lp_addr, grp->lg_addr, ETHERADDRL);
*mac_addr_changedp = B_TRUE;
} else {
/*
* Update the actual port MAC address to the MAC address
* of the group.
*/
if (aggr_port_unicst(port) != 0) {
*link_state_changedp = aggr_grp_detach_port(grp, port);
} else {
/*
* If a port was detached because of a previous
* failure changing the MAC address, the port is
* reattached when it successfully changes the MAC
* address now, and this might cause the link state
* of the aggregation to change.
*/
*link_state_changedp = aggr_grp_attach_port(grp, port);
}
}
}
/*
* Add a port to a link aggregation group.
*/
static int
aggr_grp_add_port(aggr_grp_t *grp, datalink_id_t port_linkid, boolean_t force,
aggr_port_t **pp)
{
aggr_port_t *port, **cport;
mac_perim_handle_t mph;
zoneid_t port_zoneid = ALL_ZONES;
int err;
/* The port must be in the same zone as the aggregation. */
if (zone_check_datalink(&port_zoneid, port_linkid) != 0)
port_zoneid = GLOBAL_ZONEID;
if (grp->lg_zoneid != port_zoneid)
return (EBUSY);
/*
* If we are creating the aggr, then there is no MAC handle
* and thus no perimeter to hold. If we are adding a port to
* an existing aggr, then the perimiter of the aggr's MAC must
* be held.
*/
ASSERT(grp->lg_mh == NULL || MAC_PERIM_HELD(grp->lg_mh));
err = aggr_port_create(grp, port_linkid, force, &port);
if (err != 0)
return (err);
mac_perim_enter_by_mh(port->lp_mh, &mph);
/* Add the new port to the end of the list. */
cport = &grp->lg_ports;
while (*cport != NULL)
cport = &((*cport)->lp_next);
*cport = port;
/*
* Back reference to the group it is member of. A port always
* holds a reference to its group to ensure that the back
* reference is always valid.
*/
port->lp_grp = grp;
AGGR_GRP_REFHOLD(grp);
grp->lg_nports++;
aggr_lacp_init_port(port);
mac_perim_exit(mph);
if (pp != NULL)
*pp = port;
return (0);
}
/*
* This is called in response to either our LACP state machine or a MAC
* notification that the link has gone down via aggr_send_port_disable(). At
* this point, we may need to update our default ring. To that end, we go
* through the set of ports (underlying datalinks in an aggregation) that are
* currently enabled to transmit data. If all our links have been disabled for
* transmit, then we don't do anything.
*
* Note, because we only have a single TX group, we don't have to worry about
* the rings moving between groups and the chance that mac will reassign it
* unless someone removes a port, at which point, we play it safe and call this
* again.
*/
void
aggr_grp_update_default(aggr_grp_t *grp)
{
aggr_port_t *port;
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
rw_enter(&grp->lg_tx_lock, RW_WRITER);
if (grp->lg_ntx_ports == 0) {
rw_exit(&grp->lg_tx_lock);
return;
}
port = grp->lg_tx_ports[0];
ASSERT(port->lp_tx_ring_cnt > 0);
mac_hwring_set_default(grp->lg_mh, port->lp_pseudo_tx_rings[0]);
rw_exit(&grp->lg_tx_lock);
}
/*
* Add a pseudo RX ring for the given HW ring handle.
*/
static int
aggr_add_pseudo_rx_ring(aggr_port_t *port,
aggr_pseudo_rx_group_t *rx_grp, mac_ring_handle_t hw_rh)
{
aggr_pseudo_rx_ring_t *ring;
int err;
int j;
for (j = 0; j < MAX_RINGS_PER_GROUP; j++) {
ring = rx_grp->arg_rings + j;
if (!(ring->arr_flags & MAC_PSEUDO_RING_INUSE))
break;
}
/*
* No slot for this new RX ring.
*/
if (j == MAX_RINGS_PER_GROUP)
return (EIO);
ring->arr_flags |= MAC_PSEUDO_RING_INUSE;
ring->arr_hw_rh = hw_rh;
ring->arr_port = port;
ring->arr_grp = rx_grp;
rx_grp->arg_ring_cnt++;
/*
* The group is already registered, dynamically add a new ring to the
* mac group.
*/
if ((err = mac_group_add_ring(rx_grp->arg_gh, j)) != 0) {
ring->arr_flags &= ~MAC_PSEUDO_RING_INUSE;
ring->arr_hw_rh = NULL;
ring->arr_port = NULL;
ring->arr_grp = NULL;
rx_grp->arg_ring_cnt--;
} else {
/*
* This must run after the MAC is registered.
*/
ASSERT3P(ring->arr_rh, !=, NULL);
mac_hwring_set_passthru(hw_rh, (mac_rx_t)aggr_recv_cb,
(void *)port, (mac_resource_handle_t)ring);
}
return (err);
}
/*
* Remove the pseudo RX ring of the given HW ring handle.
*/
static void
aggr_rem_pseudo_rx_ring(aggr_pseudo_rx_group_t *rx_grp, mac_ring_handle_t hw_rh)
{
for (uint_t j = 0; j < MAX_RINGS_PER_GROUP; j++) {
aggr_pseudo_rx_ring_t *ring = rx_grp->arg_rings + j;
if (!(ring->arr_flags & MAC_PSEUDO_RING_INUSE) ||
ring->arr_hw_rh != hw_rh) {
continue;
}
mac_group_rem_ring(rx_grp->arg_gh, ring->arr_rh);
ring->arr_flags &= ~MAC_PSEUDO_RING_INUSE;
ring->arr_hw_rh = NULL;
ring->arr_port = NULL;
ring->arr_grp = NULL;
rx_grp->arg_ring_cnt--;
mac_hwring_clear_passthru(hw_rh);
break;
}
}
/*
* Create pseudo rings over the HW rings of the port.
*
* o Create a pseudo ring in rx_grp per HW ring in the port's HW group.
*
* o Program existing unicast filters on the pseudo group into the HW group.
*
* o Program existing VLAN filters on the pseudo group into the HW group.
*/
static int
aggr_add_pseudo_rx_group(aggr_port_t *port, aggr_pseudo_rx_group_t *rx_grp)
{
mac_ring_handle_t hw_rh[MAX_RINGS_PER_GROUP];
aggr_unicst_addr_t *addr, *a;
mac_perim_handle_t pmph;
aggr_vlan_t *avp;
uint_t hw_rh_cnt, i;
int err = 0;
uint_t g_idx = rx_grp->arg_index;
ASSERT(MAC_PERIM_HELD(port->lp_grp->lg_mh));
ASSERT3U(g_idx, <, MAX_GROUPS_PER_PORT);
mac_perim_enter_by_mh(port->lp_mh, &pmph);
i = 0;
addr = NULL;
/*
* This function must be called after the aggr registers its
* MAC and its Rx groups have been initialized.
*/
ASSERT(rx_grp->arg_gh != NULL);
/*
* Get the list of the underlying HW rings.
*/
hw_rh_cnt = mac_hwrings_idx_get(port->lp_mh, g_idx,
&port->lp_hwghs[g_idx], hw_rh, MAC_RING_TYPE_RX);
/*
* Add existing VLAN and unicast address filters to the port.
*/
for (avp = list_head(&rx_grp->arg_vlans); avp != NULL;
avp = list_next(&rx_grp->arg_vlans, avp)) {
if ((err = aggr_port_addvlan(port, g_idx, avp->av_vid)) != 0)
goto err;
}
for (addr = rx_grp->arg_macaddr; addr != NULL; addr = addr->aua_next) {
if ((err = aggr_port_addmac(port, g_idx, addr->aua_addr)) != 0)
goto err;
}
for (i = 0; i < hw_rh_cnt; i++) {
err = aggr_add_pseudo_rx_ring(port, rx_grp, hw_rh[i]);
if (err != 0)
goto err;
}
mac_perim_exit(pmph);
return (0);
err:
ASSERT(err != 0);
for (uint_t j = 0; j < i; j++)
aggr_rem_pseudo_rx_ring(rx_grp, hw_rh[j]);
for (a = rx_grp->arg_macaddr; a != addr; a = a->aua_next)
aggr_port_remmac(port, g_idx, a->aua_addr);
if (avp != NULL)
avp = list_prev(&rx_grp->arg_vlans, avp);
for (; avp != NULL; avp = list_prev(&rx_grp->arg_vlans, avp)) {
int err2;
if ((err2 = aggr_port_remvlan(port, g_idx, avp->av_vid)) != 0) {
cmn_err(CE_WARN, "Failed to remove VLAN %u from port %s"
": errno %d.", avp->av_vid,
mac_client_name(port->lp_mch), err2);
}
}
port->lp_hwghs[g_idx] = NULL;
mac_perim_exit(pmph);
return (err);
}
/*
* Destroy the pseudo rings mapping to this port and remove all VLAN
* and unicast filters from this port. Even if there are no underlying
* HW rings we must still remove the unicast filters to take the port
* out of promisc mode.
*/
static void
aggr_rem_pseudo_rx_group(aggr_port_t *port, aggr_pseudo_rx_group_t *rx_grp)
{
mac_ring_handle_t hw_rh[MAX_RINGS_PER_GROUP];
aggr_unicst_addr_t *addr;
mac_perim_handle_t pmph;
uint_t hw_rh_cnt;
uint_t g_idx = rx_grp->arg_index;
ASSERT(MAC_PERIM_HELD(port->lp_grp->lg_mh));
ASSERT3U(g_idx, <, MAX_GROUPS_PER_PORT);
ASSERT3P(rx_grp->arg_gh, !=, NULL);
mac_perim_enter_by_mh(port->lp_mh, &pmph);
hw_rh_cnt = mac_hwrings_idx_get(port->lp_mh, g_idx, NULL, hw_rh,
MAC_RING_TYPE_RX);
for (uint_t i = 0; i < hw_rh_cnt; i++)
aggr_rem_pseudo_rx_ring(rx_grp, hw_rh[i]);
for (addr = rx_grp->arg_macaddr; addr != NULL; addr = addr->aua_next)
aggr_port_remmac(port, g_idx, addr->aua_addr);
for (aggr_vlan_t *avp = list_head(&rx_grp->arg_vlans); avp != NULL;
avp = list_next(&rx_grp->arg_vlans, avp)) {
int err;
if ((err = aggr_port_remvlan(port, g_idx, avp->av_vid)) != 0) {
cmn_err(CE_WARN, "Failed to remove VLAN %u from port %s"
": errno %d.", avp->av_vid,
mac_client_name(port->lp_mch), err);
}
}
port->lp_hwghs[g_idx] = NULL;
mac_perim_exit(pmph);
}
/*
* Add a pseudo TX ring for the given HW ring handle.
*/
static int
aggr_add_pseudo_tx_ring(aggr_port_t *port,
aggr_pseudo_tx_group_t *tx_grp, mac_ring_handle_t hw_rh,
mac_ring_handle_t *pseudo_rh)
{
aggr_pseudo_tx_ring_t *ring;
int err;
int i;
ASSERT(MAC_PERIM_HELD(port->lp_mh));
for (i = 0; i < MAX_RINGS_PER_GROUP; i++) {
ring = tx_grp->atg_rings + i;
if (!(ring->atr_flags & MAC_PSEUDO_RING_INUSE))
break;
}
/*
* No slot for this new TX ring.
*/
if (i == MAX_RINGS_PER_GROUP)
return (EIO);
/*
* The following 4 statements needs to be done before
* calling mac_group_add_ring(). Otherwise it will
* result in an assertion failure in mac_init_ring().
*/
ring->atr_flags |= MAC_PSEUDO_RING_INUSE;
ring->atr_hw_rh = hw_rh;
ring->atr_port = port;
tx_grp->atg_ring_cnt++;
/*
* The TX side has no concept of ring groups unlike RX groups.
* There is just a single group which stores all the TX rings.
* This group will be used to store aggr's pseudo TX rings.
*/
if ((err = mac_group_add_ring(tx_grp->atg_gh, i)) != 0) {
ring->atr_flags &= ~MAC_PSEUDO_RING_INUSE;
ring->atr_hw_rh = NULL;
ring->atr_port = NULL;
tx_grp->atg_ring_cnt--;
} else {
*pseudo_rh = mac_find_ring(tx_grp->atg_gh, i);
if (hw_rh != NULL) {
mac_hwring_setup(hw_rh, (mac_resource_handle_t)ring,
mac_find_ring(tx_grp->atg_gh, i));
}
}
return (err);
}
/*
* Remove the pseudo TX ring of the given HW ring handle.
*/
static void
aggr_rem_pseudo_tx_ring(aggr_pseudo_tx_group_t *tx_grp,
mac_ring_handle_t pseudo_hw_rh)
{
aggr_pseudo_tx_ring_t *ring;
int i;
for (i = 0; i < MAX_RINGS_PER_GROUP; i++) {
ring = tx_grp->atg_rings + i;
if (ring->atr_rh != pseudo_hw_rh)
continue;
ASSERT(ring->atr_flags & MAC_PSEUDO_RING_INUSE);
mac_group_rem_ring(tx_grp->atg_gh, pseudo_hw_rh);
ring->atr_flags &= ~MAC_PSEUDO_RING_INUSE;
mac_hwring_teardown(ring->atr_hw_rh);
ring->atr_hw_rh = NULL;
ring->atr_port = NULL;
tx_grp->atg_ring_cnt--;
break;
}
}
/*
* This function is called to create pseudo rings over hardware rings of
* the underlying device. There is a 1:1 mapping between the pseudo TX
* rings of the aggr and the hardware rings of the underlying port.
*/
static int
aggr_add_pseudo_tx_group(aggr_port_t *port, aggr_pseudo_tx_group_t *tx_grp)
{
aggr_grp_t *grp = port->lp_grp;
mac_ring_handle_t hw_rh[MAX_RINGS_PER_GROUP], pseudo_rh;
mac_perim_handle_t pmph;
int hw_rh_cnt, i = 0, j;
int err = 0;
ASSERT(MAC_PERIM_HELD(grp->lg_mh));
mac_perim_enter_by_mh(port->lp_mh, &pmph);
/*
* Get the list the the underlying HW rings.
*/
hw_rh_cnt = mac_hwrings_get(port->lp_mch, NULL, hw_rh,
MAC_RING_TYPE_TX);
/*
* Even if the underlying NIC does not have TX rings, we
* still make a psuedo TX ring for that NIC with NULL as
* the ring handle.
*/
if (hw_rh_cnt == 0)
port->lp_tx_ring_cnt = 1;
else
port->lp_tx_ring_cnt = hw_rh_cnt;
port->lp_tx_rings = kmem_zalloc((sizeof (mac_ring_handle_t *) *
port->lp_tx_ring_cnt), KM_SLEEP);
port->lp_pseudo_tx_rings = kmem_zalloc((sizeof (mac_ring_handle_t *) *
port->lp_tx_ring_cnt), KM_SLEEP);
if (hw_rh_cnt == 0) {
if ((err = aggr_add_pseudo_tx_ring(port, tx_grp,
NULL, &pseudo_rh)) == 0) {
port->lp_tx_rings[0] = NULL;
port->lp_pseudo_tx_rings[0] = pseudo_rh;
}
} else {
for (i = 0; err == 0 && i < hw_rh_cnt; i++) {
err = aggr_add_pseudo_tx_ring(port,
tx_grp, hw_rh[i], &pseudo_rh);
if (err != 0)
break;
port->lp_tx_rings[i] = hw_rh[i];
port->lp_pseudo_tx_rings[i] = pseudo_rh;
}
}
if (err != 0) {
if (hw_rh_cnt != 0) {
for (j = 0; j < i; j++) {
aggr_rem_pseudo_tx_ring(tx_grp,
port->lp_pseudo_tx_rings[j]);
}
}