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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
|
/*
* 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.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <strings.h>
#include <libintl.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <sys/dld.h>
#include <libdllink.h>
#include <libdlvlan.h>
#include <libdlaggr.h>
#include <libdladm_impl.h>
/*
* Link Aggregation Administration Library.
*
* This library is used by administration tools such as dladm(1M) to
* configure link aggregations.
*/
/* Limits on buffer size for LAIOC_INFO request */
#define MIN_INFO_SIZE (4*1024)
#define MAX_INFO_SIZE (128*1024)
static uchar_t zero_mac[] = {0, 0, 0, 0, 0, 0};
#define VALID_PORT_MAC(mac) \
(((mac) != NULL) && (bcmp(zero_mac, (mac), ETHERADDRL) != 0) && \
(!((mac)[0] & 0x01)))
#define PORT_DELIMITER ":"
typedef struct dladm_aggr_modify_attr {
uint32_t ld_policy;
boolean_t ld_mac_fixed;
uchar_t ld_mac[ETHERADDRL];
aggr_lacp_mode_t ld_lacp_mode;
aggr_lacp_timer_t ld_lacp_timer;
} dladm_aggr_modify_attr_t;
typedef struct policy_s {
char *pol_name;
uint32_t policy;
} policy_t;
static policy_t policies[] = {
{"L2", AGGR_POLICY_L2},
{"L3", AGGR_POLICY_L3},
{"L4", AGGR_POLICY_L4}};
#define NPOLICIES (sizeof (policies) / sizeof (policy_t))
typedef struct dladm_aggr_lacpmode_s {
char *mode_str;
aggr_lacp_mode_t mode_id;
} dladm_aggr_lacpmode_t;
static dladm_aggr_lacpmode_t lacp_modes[] = {
{"off", AGGR_LACP_OFF},
{"active", AGGR_LACP_ACTIVE},
{"passive", AGGR_LACP_PASSIVE}};
#define NLACP_MODES (sizeof (lacp_modes) / sizeof (dladm_aggr_lacpmode_t))
typedef struct dladm_aggr_lacptimer_s {
char *lt_str;
aggr_lacp_timer_t lt_id;
} dladm_aggr_lacptimer_t;
static dladm_aggr_lacptimer_t lacp_timers[] = {
{"short", AGGR_LACP_TIMER_SHORT},
{"long", AGGR_LACP_TIMER_LONG}};
#define NLACP_TIMERS (sizeof (lacp_timers) / sizeof (dladm_aggr_lacptimer_t))
typedef struct dladm_aggr_port_state {
char *state_str;
aggr_port_state_t state_id;
} dladm_aggr_port_state_t;
static dladm_aggr_port_state_t port_states[] = {
{"standby", AGGR_PORT_STATE_STANDBY },
{"attached", AGGR_PORT_STATE_ATTACHED }
};
#define NPORT_STATES \
(sizeof (port_states) / sizeof (dladm_aggr_port_state_t))
static dladm_status_t
write_port(dladm_handle_t handle, char *portstr, datalink_id_t portid,
size_t portstrsize)
{
char pname[MAXLINKNAMELEN + 1];
dladm_status_t status;
if ((status = dladm_datalink_id2info(handle, portid, NULL, NULL, NULL,
pname, sizeof (pname))) != DLADM_STATUS_OK)
return (status);
(void) strlcat(pname, PORT_DELIMITER, sizeof (pname));
if (strlcat(portstr, pname, portstrsize) >= portstrsize)
status = DLADM_STATUS_TOOSMALL;
return (status);
}
static dladm_status_t
read_port(dladm_handle_t handle, char **portstr, datalink_id_t *portid)
{
dladm_status_t status;
char *pname;
if ((pname = strtok(*portstr, PORT_DELIMITER)) == NULL)
return (DLADM_STATUS_REPOSITORYINVAL);
*portstr += (strlen(pname) + 1);
status = dladm_name2info(handle, pname, portid, NULL, NULL, NULL);
return (status);
}
static int
i_dladm_aggr_ioctl(dladm_handle_t handle, int cmd, void *ptr)
{
return (ioctl(dladm_dld_fd(handle), cmd, ptr));
}
/*
* Caller must free attr.lg_ports. The ptr pointer is advanced while convert
* the laioc_info_t to the dladm_aggr_grp_attr_t structure.
*/
static int
i_dladm_aggr_iocp2grpattr(void **ptr, dladm_aggr_grp_attr_t *attrp)
{
laioc_info_group_t *grp;
laioc_info_port_t *port;
int i;
void *where = (*ptr);
grp = (laioc_info_group_t *)where;
attrp->lg_linkid = grp->lg_linkid;
attrp->lg_key = grp->lg_key;
attrp->lg_nports = grp->lg_nports;
attrp->lg_policy = grp->lg_policy;
attrp->lg_lacp_mode = grp->lg_lacp_mode;
attrp->lg_lacp_timer = grp->lg_lacp_timer;
attrp->lg_force = grp->lg_force;
bcopy(grp->lg_mac, attrp->lg_mac, ETHERADDRL);
attrp->lg_mac_fixed = grp->lg_mac_fixed;
if ((attrp->lg_ports = malloc(grp->lg_nports *
sizeof (dladm_aggr_port_attr_t))) == NULL) {
errno = ENOMEM;
goto fail;
}
where = (grp + 1);
/*
* Go through each port that is part of the group.
*/
for (i = 0; i < grp->lg_nports; i++) {
port = (laioc_info_port_t *)where;
attrp->lg_ports[i].lp_linkid = port->lp_linkid;
bcopy(port->lp_mac, attrp->lg_ports[i].lp_mac, ETHERADDRL);
attrp->lg_ports[i].lp_state = port->lp_state;
attrp->lg_ports[i].lp_lacp_state = port->lp_lacp_state;
where = (port + 1);
}
*ptr = where;
return (0);
fail:
return (-1);
}
/*
* Get active configuration of a specific aggregation.
* Caller must free attrp->la_ports.
*/
static dladm_status_t
i_dladm_aggr_info_active(dladm_handle_t handle, datalink_id_t linkid,
dladm_aggr_grp_attr_t *attrp)
{
laioc_info_t *ioc;
int bufsize;
void *where;
dladm_status_t status = DLADM_STATUS_OK;
bufsize = MIN_INFO_SIZE;
ioc = (laioc_info_t *)calloc(1, bufsize);
if (ioc == NULL)
return (DLADM_STATUS_NOMEM);
ioc->li_group_linkid = linkid;
tryagain:
ioc->li_bufsize = bufsize;
if (i_dladm_aggr_ioctl(handle, LAIOC_INFO, ioc) != 0) {
if (errno == ENOSPC) {
/*
* The LAIOC_INFO call failed due to a short
* buffer. Reallocate the buffer and try again.
*/
bufsize *= 2;
if (bufsize <= MAX_INFO_SIZE) {
ioc = (laioc_info_t *)realloc(ioc, bufsize);
if (ioc != NULL) {
bzero(ioc, sizeof (bufsize));
goto tryagain;
}
}
}
status = dladm_errno2status(errno);
goto bail;
}
/*
* Go through each group returned by the aggregation driver.
*/
where = (char *)(ioc + 1);
if (i_dladm_aggr_iocp2grpattr(&where, attrp) != 0) {
status = dladm_errno2status(errno);
goto bail;
}
bail:
free(ioc);
return (status);
}
/*
* Get configuration information of a specific aggregation.
* Caller must free attrp->la_ports.
*/
static dladm_status_t
i_dladm_aggr_info_persist(dladm_handle_t handle, datalink_id_t linkid,
dladm_aggr_grp_attr_t *attrp)
{
dladm_conf_t conf;
uint32_t nports, i;
char *portstr = NULL, *next;
dladm_status_t status;
uint64_t u64;
int size;
char macstr[ETHERADDRL * 3];
attrp->lg_linkid = linkid;
if ((status = dladm_getsnap_conf(handle, linkid, &conf)) !=
DLADM_STATUS_OK)
return (status);
status = dladm_get_conf_field(handle, conf, FKEY, &u64, sizeof (u64));
if (status != DLADM_STATUS_OK)
goto done;
attrp->lg_key = (uint16_t)u64;
status = dladm_get_conf_field(handle, conf, FPOLICY, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
goto done;
attrp->lg_policy = (uint32_t)u64;
status = dladm_get_conf_field(handle, conf, FFIXMACADDR,
&attrp->lg_mac_fixed, sizeof (boolean_t));
if (status != DLADM_STATUS_OK)
goto done;
if (attrp->lg_mac_fixed) {
boolean_t fixed;
if ((status = dladm_get_conf_field(handle, conf, FMACADDR,
macstr, sizeof (macstr))) != DLADM_STATUS_OK) {
goto done;
}
if (!dladm_aggr_str2macaddr(macstr, &fixed, attrp->lg_mac)) {
status = DLADM_STATUS_REPOSITORYINVAL;
goto done;
}
}
status = dladm_get_conf_field(handle, conf, FFORCE, &attrp->lg_force,
sizeof (boolean_t));
if (status != DLADM_STATUS_OK)
goto done;
status = dladm_get_conf_field(handle, conf, FLACPMODE, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
goto done;
attrp->lg_lacp_mode = (aggr_lacp_mode_t)u64;
status = dladm_get_conf_field(handle, conf, FLACPTIMER, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
goto done;
attrp->lg_lacp_timer = (aggr_lacp_timer_t)u64;
status = dladm_get_conf_field(handle, conf, FNPORTS, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
goto done;
nports = (uint32_t)u64;
attrp->lg_nports = nports;
size = nports * (MAXLINKNAMELEN + 1) + 1;
if ((portstr = calloc(1, size)) == NULL) {
status = DLADM_STATUS_NOMEM;
goto done;
}
status = dladm_get_conf_field(handle, conf, FPORTS, portstr, size);
if (status != DLADM_STATUS_OK)
goto done;
if ((attrp->lg_ports = malloc(nports *
sizeof (dladm_aggr_port_attr_t))) == NULL) {
status = DLADM_STATUS_NOMEM;
goto done;
}
for (next = portstr, i = 0; i < nports; i++) {
if ((status = read_port(handle, &next,
&attrp->lg_ports[i].lp_linkid)) != DLADM_STATUS_OK)
free(attrp->lg_ports);
}
done:
free(portstr);
dladm_destroy_conf(handle, conf);
return (status);
}
dladm_status_t
dladm_aggr_info(dladm_handle_t handle, datalink_id_t linkid,
dladm_aggr_grp_attr_t *attrp, uint32_t flags)
{
assert(flags == DLADM_OPT_ACTIVE || flags == DLADM_OPT_PERSIST);
if (flags == DLADM_OPT_ACTIVE)
return (i_dladm_aggr_info_active(handle, linkid, attrp));
else
return (i_dladm_aggr_info_persist(handle, linkid, attrp));
}
/*
* Add or remove one or more ports to/from an existing link aggregation.
*/
static dladm_status_t
i_dladm_aggr_add_rmv(dladm_handle_t handle, datalink_id_t linkid,
uint32_t nports, dladm_aggr_port_attr_db_t *ports, uint32_t flags, int cmd)
{
char *orig_portstr = NULL, *portstr = NULL;
laioc_add_rem_t *iocp = NULL;
laioc_port_t *ioc_ports;
uint32_t orig_nports, result_nports, len, i, j;
dladm_conf_t conf;
datalink_class_t class;
dladm_status_t status = DLADM_STATUS_OK;
int size;
uint64_t u64;
uint32_t media;
if (nports == 0)
return (DLADM_STATUS_BADARG);
/*
* Sanity check - aggregations can only be created over Ethernet
* physical links and simnets.
*/
for (i = 0; i < nports; i++) {
if ((dladm_datalink_id2info(handle, ports[i].lp_linkid, NULL,
&class, &media, NULL, 0) != DLADM_STATUS_OK) ||
!((class == DATALINK_CLASS_PHYS) ||
(class == DATALINK_CLASS_SIMNET)) || (media != DL_ETHER)) {
return (DLADM_STATUS_BADARG);
}
}
/*
* First, update the persistent configuration if requested. We only
* need to update the FPORTS and FNPORTS fields of this aggregation.
* Note that FPORTS is a list of port linkids separated by
* PORT_DELIMITER (':').
*/
if (flags & DLADM_OPT_PERSIST) {
status = dladm_open_conf(handle, linkid, &conf);
if (status != DLADM_STATUS_OK)
return (status);
/*
* Get the original configuration of FNPORTS and FPORTS.
*/
status = dladm_get_conf_field(handle, conf, FNPORTS, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
goto destroyconf;
orig_nports = (uint32_t)u64;
/*
* At least one port needs to be in the aggregation.
*/
if ((cmd == LAIOC_REMOVE) && (orig_nports <= nports)) {
status = DLADM_STATUS_BADARG;
goto destroyconf;
}
size = orig_nports * (MAXLINKNAMELEN + 1) + 1;
if ((orig_portstr = calloc(1, size)) == NULL) {
status = dladm_errno2status(errno);
goto destroyconf;
}
status = dladm_get_conf_field(handle, conf, FPORTS,
orig_portstr, size);
if (status != DLADM_STATUS_OK)
goto destroyconf;
result_nports = (cmd == LAIOC_ADD) ? orig_nports + nports :
orig_nports;
size = result_nports * (MAXLINKNAMELEN + 1) + 1;
if ((portstr = calloc(1, size)) == NULL) {
status = dladm_errno2status(errno);
goto destroyconf;
}
/*
* get the new configuration and set to result_nports and
* portstr.
*/
if (cmd == LAIOC_ADD) {
(void) strlcpy(portstr, orig_portstr, size);
for (i = 0; i < nports; i++) {
status = write_port(handle, portstr,
ports[i].lp_linkid, size);
if (status != DLADM_STATUS_OK) {
free(portstr);
goto destroyconf;
}
}
} else {
char *next;
datalink_id_t portid;
uint32_t remove = 0;
for (next = orig_portstr, j = 0; j < orig_nports; j++) {
/*
* Read the portids from the old configuration
* one by one.
*/
status = read_port(handle, &next, &portid);
if (status != DLADM_STATUS_OK) {
free(portstr);
goto destroyconf;
}
/*
* See whether this port is in the removal
* list. If not, copy to the new config.
*/
for (i = 0; i < nports; i++) {
if (ports[i].lp_linkid == portid)
break;
}
if (i == nports) {
status = write_port(handle, portstr,
portid, size);
if (status != DLADM_STATUS_OK) {
free(portstr);
goto destroyconf;
}
} else {
remove++;
}
}
if (remove != nports) {
status = DLADM_STATUS_LINKINVAL;
free(portstr);
goto destroyconf;
}
result_nports -= nports;
}
u64 = result_nports;
if ((status = dladm_set_conf_field(handle, conf, FNPORTS,
DLADM_TYPE_UINT64, &u64)) != DLADM_STATUS_OK) {
free(portstr);
goto destroyconf;
}
status = dladm_set_conf_field(handle, conf, FPORTS,
DLADM_TYPE_STR, portstr);
free(portstr);
if (status != DLADM_STATUS_OK)
goto destroyconf;
/*
* Write the new configuration to the persistent repository.
*/
status = dladm_write_conf(handle, conf);
destroyconf:
dladm_destroy_conf(handle, conf);
if (status != DLADM_STATUS_OK) {
free(orig_portstr);
return (status);
}
}
/*
* If the caller only requested to update the persistent
* configuration, we are done.
*/
if (!(flags & DLADM_OPT_ACTIVE))
goto done;
/*
* Update the active configuration.
*/
len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
if ((iocp = malloc(len)) == NULL) {
status = DLADM_STATUS_NOMEM;
goto done;
}
iocp->la_linkid = linkid;
iocp->la_nports = nports;
if (cmd == LAIOC_ADD)
iocp->la_force = (flags & DLADM_OPT_FORCE);
ioc_ports = (laioc_port_t *)(iocp + 1);
for (i = 0; i < nports; i++)
ioc_ports[i].lp_linkid = ports[i].lp_linkid;
if (i_dladm_aggr_ioctl(handle, cmd, iocp) < 0)
status = dladm_errno2status(errno);
done:
free(iocp);
/*
* If the active configuration update fails, restore the old
* persistent configuration if we've changed that.
*/
if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
if (dladm_open_conf(handle, linkid, &conf) == DLADM_STATUS_OK) {
u64 = orig_nports;
if ((dladm_set_conf_field(handle, conf, FNPORTS,
DLADM_TYPE_UINT64, &u64) == DLADM_STATUS_OK) &&
(dladm_set_conf_field(handle, conf, FPORTS,
DLADM_TYPE_STR, orig_portstr) == DLADM_STATUS_OK)) {
(void) dladm_write_conf(handle, conf);
}
(void) dladm_destroy_conf(handle, conf);
}
}
free(orig_portstr);
return (status);
}
/*
* Send a modify command to the link aggregation driver.
*/
static dladm_status_t
i_dladm_aggr_modify_sys(dladm_handle_t handle, datalink_id_t linkid,
uint32_t mask, dladm_aggr_modify_attr_t *attr)
{
laioc_modify_t ioc;
ioc.lu_linkid = linkid;
ioc.lu_modify_mask = 0;
if (mask & DLADM_AGGR_MODIFY_POLICY)
ioc.lu_modify_mask |= LAIOC_MODIFY_POLICY;
if (mask & DLADM_AGGR_MODIFY_MAC)
ioc.lu_modify_mask |= LAIOC_MODIFY_MAC;
if (mask & DLADM_AGGR_MODIFY_LACP_MODE)
ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_MODE;
if (mask & DLADM_AGGR_MODIFY_LACP_TIMER)
ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_TIMER;
ioc.lu_policy = attr->ld_policy;
ioc.lu_mac_fixed = attr->ld_mac_fixed;
bcopy(attr->ld_mac, ioc.lu_mac, ETHERADDRL);
ioc.lu_lacp_mode = attr->ld_lacp_mode;
ioc.lu_lacp_timer = attr->ld_lacp_timer;
if (i_dladm_aggr_ioctl(handle, LAIOC_MODIFY, &ioc) < 0) {
if (errno == EINVAL)
return (DLADM_STATUS_MACADDRINVAL);
else
return (dladm_errno2status(errno));
} else {
return (DLADM_STATUS_OK);
}
}
/*
* Send a create command to the link aggregation driver.
*/
static dladm_status_t
i_dladm_aggr_create_sys(dladm_handle_t handle, datalink_id_t linkid,
uint16_t key, uint32_t nports, dladm_aggr_port_attr_db_t *ports,
uint32_t policy, boolean_t mac_addr_fixed, const uchar_t *mac_addr,
aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, boolean_t force)
{
int i, len;
laioc_create_t *iocp = NULL;
laioc_port_t *ioc_ports;
dladm_status_t status = DLADM_STATUS_OK;
len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
iocp = malloc(len);
if (iocp == NULL)
return (DLADM_STATUS_NOMEM);
iocp->lc_key = key;
iocp->lc_linkid = linkid;
iocp->lc_nports = nports;
iocp->lc_policy = policy;
iocp->lc_lacp_mode = lacp_mode;
iocp->lc_lacp_timer = lacp_timer;
ioc_ports = (laioc_port_t *)(iocp + 1);
iocp->lc_force = force;
for (i = 0; i < nports; i++)
ioc_ports[i].lp_linkid = ports[i].lp_linkid;
if (mac_addr_fixed && !VALID_PORT_MAC(mac_addr)) {
status = DLADM_STATUS_MACADDRINVAL;
goto done;
}
bcopy(mac_addr, iocp->lc_mac, ETHERADDRL);
iocp->lc_mac_fixed = mac_addr_fixed;
if (i_dladm_aggr_ioctl(handle, LAIOC_CREATE, iocp) < 0)
status = dladm_errno2status(errno);
done:
free(iocp);
return (status);
}
/*
* Invoked to bring up a link aggregation group.
*/
static int
i_dladm_aggr_up(dladm_handle_t handle, datalink_id_t linkid, void *arg)
{
dladm_status_t *statusp = (dladm_status_t *)arg;
dladm_aggr_grp_attr_t attr;
dladm_aggr_port_attr_db_t *ports = NULL;
uint16_t key = 0;
int i, j;
dladm_status_t status;
status = dladm_aggr_info(handle, linkid, &attr, DLADM_OPT_PERSIST);
if (status != DLADM_STATUS_OK) {
*statusp = status;
return (DLADM_WALK_CONTINUE);
}
if (attr.lg_key <= AGGR_MAX_KEY)
key = attr.lg_key;
ports = malloc(attr.lg_nports * sizeof (dladm_aggr_port_attr_db_t));
if (ports == NULL) {
status = DLADM_STATUS_NOMEM;
goto done;
}
/*
* Validate (and purge) each physical link associated with this
* aggregation, if the specific hardware has been removed during
* the system shutdown.
*/
for (i = 0, j = 0; i < attr.lg_nports; i++) {
datalink_id_t portid = attr.lg_ports[i].lp_linkid;
uint32_t flags;
dladm_status_t s;
s = dladm_datalink_id2info(handle, portid, &flags, NULL, NULL,
NULL, 0);
if (s != DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE))
continue;
ports[j++].lp_linkid = portid;
}
if (j == 0) {
/*
* All of the physical links are removed.
*/
status = DLADM_STATUS_BADARG;
goto done;
}
/*
* Create active aggregation.
*/
if ((status = i_dladm_aggr_create_sys(handle, linkid,
key, j, ports, attr.lg_policy, attr.lg_mac_fixed,
(const uchar_t *)attr.lg_mac, attr.lg_lacp_mode,
attr.lg_lacp_timer, attr.lg_force)) != DLADM_STATUS_OK) {
goto done;
}
if ((status = dladm_up_datalink_id(handle, linkid)) !=
DLADM_STATUS_OK) {
laioc_delete_t ioc;
ioc.ld_linkid = linkid;
(void) i_dladm_aggr_ioctl(handle, LAIOC_DELETE, &ioc);
}
done:
free(attr.lg_ports);
free(ports);
*statusp = status;
return (DLADM_WALK_CONTINUE);
}
/*
* Bring up one aggregation, or all persistent aggregations. In the latter
* case, the walk may terminate early if bringup of an aggregation fails.
*/
dladm_status_t
dladm_aggr_up(dladm_handle_t handle, datalink_id_t linkid)
{
dladm_status_t status;
if (linkid == DATALINK_ALL_LINKID) {
(void) dladm_walk_datalink_id(i_dladm_aggr_up, handle, &status,
DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
DLADM_OPT_PERSIST);
return (DLADM_STATUS_OK);
} else {
(void) i_dladm_aggr_up(handle, linkid, &status);
return (status);
}
}
/*
* Given a policy string, return a policy mask. Returns B_TRUE on
* success, or B_FALSE if an error occurred during parsing.
*/
boolean_t
dladm_aggr_str2policy(const char *str, uint32_t *policy)
{
int i;
policy_t *pol;
char *token = NULL;
char *lasts;
*policy = 0;
while ((token = strtok_r((token == NULL) ? (char *)str : NULL, ",",
&lasts)) != NULL) {
for (i = 0; i < NPOLICIES; i++) {
pol = &policies[i];
if (strcasecmp(token, pol->pol_name) == 0) {
*policy |= pol->policy;
break;
}
}
if (i == NPOLICIES)
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Given a policy mask, returns a printable string, or NULL if the
* policy mask is invalid. It is the responsibility of the caller to
* free the returned string after use.
*/
char *
dladm_aggr_policy2str(uint32_t policy, char *str)
{
int i, npolicies = 0;
policy_t *pol;
if (str == NULL)
return (NULL);
str[0] = '\0';
for (i = 0; i < NPOLICIES; i++) {
pol = &policies[i];
if ((policy & pol->policy) != 0) {
npolicies++;
if (npolicies > 1)
(void) strlcat(str, ",", DLADM_STRSIZE);
(void) strlcat(str, pol->pol_name, DLADM_STRSIZE);
}
}
return (str);
}
/*
* Given a MAC address string, return the MAC address in the mac_addr
* array. If the MAC address was not explicitly specified, i.e. is
* equal to 'auto', zero out mac-addr and set mac_fixed to B_TRUE.
* Return B_FALSE if a syntax error was encountered, B_FALSE otherwise.
*/
boolean_t
dladm_aggr_str2macaddr(const char *str, boolean_t *mac_fixed, uchar_t *mac_addr)
{
uchar_t *conv_str;
int mac_len;
*mac_fixed = (strcmp(str, "auto") != 0);
if (!*mac_fixed) {
bzero(mac_addr, ETHERADDRL);
return (B_TRUE);
}
conv_str = _link_aton(str, &mac_len);
if (conv_str == NULL)
return (B_FALSE);
if (mac_len != ETHERADDRL) {
free(conv_str);
return (B_FALSE);
}
if ((bcmp(zero_mac, conv_str, ETHERADDRL) == 0) ||
(conv_str[0] & 0x01)) {
free(conv_str);
return (B_FALSE);
}
bcopy(conv_str, mac_addr, ETHERADDRL);
free(conv_str);
return (B_TRUE);
}
/*
* Returns a string containing a printable representation of a MAC address.
*/
const char *
dladm_aggr_macaddr2str(const unsigned char *mac, char *buf)
{
static char unknown_mac[] = {0, 0, 0, 0, 0, 0};
if (buf == NULL)
return (NULL);
if (bcmp(unknown_mac, mac, ETHERADDRL) == 0)
(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
else
return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
return (buf);
}
/*
* Given a LACP mode string, find the corresponding LACP mode number. Returns
* B_TRUE if a match was found, B_FALSE otherwise.
*/
boolean_t
dladm_aggr_str2lacpmode(const char *str, aggr_lacp_mode_t *lacp_mode)
{
int i;
dladm_aggr_lacpmode_t *mode;
for (i = 0; i < NLACP_MODES; i++) {
mode = &lacp_modes[i];
if (strncasecmp(str, mode->mode_str,
strlen(mode->mode_str)) == 0) {
*lacp_mode = mode->mode_id;
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* Given a LACP mode number, returns a printable string, or NULL if the
* LACP mode number is invalid.
*/
const char *
dladm_aggr_lacpmode2str(aggr_lacp_mode_t mode_id, char *buf)
{
int i;
dladm_aggr_lacpmode_t *mode;
if (buf == NULL)
return (NULL);
for (i = 0; i < NLACP_MODES; i++) {
mode = &lacp_modes[i];
if (mode->mode_id == mode_id) {
(void) snprintf(buf, DLADM_STRSIZE, "%s",
mode->mode_str);
return (buf);
}
}
(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
return (buf);
}
/*
* Given a LACP timer string, find the corresponding LACP timer number. Returns
* B_TRUE if a match was found, B_FALSE otherwise.
*/
boolean_t
dladm_aggr_str2lacptimer(const char *str, aggr_lacp_timer_t *lacp_timer)
{
int i;
dladm_aggr_lacptimer_t *timer;
for (i = 0; i < NLACP_TIMERS; i++) {
timer = &lacp_timers[i];
if (strncasecmp(str, timer->lt_str,
strlen(timer->lt_str)) == 0) {
*lacp_timer = timer->lt_id;
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* Given a LACP timer, returns a printable string, or NULL if the
* LACP timer number is invalid.
*/
const char *
dladm_aggr_lacptimer2str(aggr_lacp_timer_t timer_id, char *buf)
{
int i;
dladm_aggr_lacptimer_t *timer;
if (buf == NULL)
return (NULL);
for (i = 0; i < NLACP_TIMERS; i++) {
timer = &lacp_timers[i];
if (timer->lt_id == timer_id) {
(void) snprintf(buf, DLADM_STRSIZE, "%s",
timer->lt_str);
return (buf);
}
}
(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
return (buf);
}
const char *
dladm_aggr_portstate2str(aggr_port_state_t state_id, char *buf)
{
int i;
dladm_aggr_port_state_t *state;
if (buf == NULL)
return (NULL);
for (i = 0; i < NPORT_STATES; i++) {
state = &port_states[i];
if (state->state_id == state_id) {
(void) snprintf(buf, DLADM_STRSIZE, "%s",
state->state_str);
return (buf);
}
}
(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
return (buf);
}
static dladm_status_t
dladm_aggr_persist_aggr_conf(dladm_handle_t handle, const char *link,
datalink_id_t linkid, uint16_t key, uint32_t nports,
dladm_aggr_port_attr_db_t *ports, uint32_t policy, boolean_t mac_addr_fixed,
const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
aggr_lacp_timer_t lacp_timer, boolean_t force)
{
dladm_conf_t conf;
char *portstr = NULL;
char macstr[ETHERADDRL * 3];
dladm_status_t status;
int i, size;
uint64_t u64;
if ((status = dladm_create_conf(handle, link, linkid,
DATALINK_CLASS_AGGR, DL_ETHER, &conf)) != DLADM_STATUS_OK) {
return (status);
}
u64 = key;
status = dladm_set_conf_field(handle, conf, FKEY, DLADM_TYPE_UINT64,
&u64);
if (status != DLADM_STATUS_OK)
goto done;
u64 = nports;
status = dladm_set_conf_field(handle, conf, FNPORTS, DLADM_TYPE_UINT64,
&u64);
if (status != DLADM_STATUS_OK)
goto done;
size = nports * MAXLINKNAMELEN + 1;
if ((portstr = calloc(1, size)) == NULL) {
status = DLADM_STATUS_NOMEM;
goto done;
}
for (i = 0; i < nports; i++) {
status = write_port(handle, portstr, ports[i].lp_linkid, size);
if (status != DLADM_STATUS_OK) {
free(portstr);
goto done;
}
}
status = dladm_set_conf_field(handle, conf, FPORTS, DLADM_TYPE_STR,
portstr);
free(portstr);
if (status != DLADM_STATUS_OK)
goto done;
u64 = policy;
status = dladm_set_conf_field(handle, conf, FPOLICY, DLADM_TYPE_UINT64,
&u64);
if (status != DLADM_STATUS_OK)
goto done;
status = dladm_set_conf_field(handle, conf, FFIXMACADDR,
DLADM_TYPE_BOOLEAN, &mac_addr_fixed);
if (status != DLADM_STATUS_OK)
goto done;
if (mac_addr_fixed) {
if (!VALID_PORT_MAC(mac_addr)) {
status = DLADM_STATUS_MACADDRINVAL;
goto done;
}
(void) dladm_aggr_macaddr2str(mac_addr, macstr);
status = dladm_set_conf_field(handle, conf, FMACADDR,
DLADM_TYPE_STR, macstr);
if (status != DLADM_STATUS_OK)
goto done;
}
status = dladm_set_conf_field(handle, conf, FFORCE, DLADM_TYPE_BOOLEAN,
&force);
if (status != DLADM_STATUS_OK)
goto done;
u64 = lacp_mode;
status = dladm_set_conf_field(handle, conf, FLACPMODE,
DLADM_TYPE_UINT64, &u64);
if (status != DLADM_STATUS_OK)
goto done;
u64 = lacp_timer;
status = dladm_set_conf_field(handle, conf, FLACPTIMER,
DLADM_TYPE_UINT64, &u64);
if (status != DLADM_STATUS_OK)
goto done;
/*
* Commit the link aggregation configuration.
*/
status = dladm_write_conf(handle, conf);
done:
dladm_destroy_conf(handle, conf);
return (status);
}
/*
* Create a new link aggregation group. Update the configuration
* file and bring it up.
*/
dladm_status_t
dladm_aggr_create(dladm_handle_t handle, const char *name, uint16_t key,
uint32_t nports, dladm_aggr_port_attr_db_t *ports, uint32_t policy,
boolean_t mac_addr_fixed, const uchar_t *mac_addr,
aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, uint32_t flags)
{
datalink_id_t linkid = DATALINK_INVALID_LINKID;
uint32_t media;
uint32_t i;
datalink_class_t class;
dladm_status_t status;
boolean_t force = (flags & DLADM_OPT_FORCE) ? B_TRUE : B_FALSE;
if (key != 0 && key > AGGR_MAX_KEY)
return (DLADM_STATUS_KEYINVAL);
if (nports == 0)
return (DLADM_STATUS_BADARG);
for (i = 0; i < nports; i++) {
if ((dladm_datalink_id2info(handle, ports[i].lp_linkid, NULL,
&class, &media, NULL, 0) != DLADM_STATUS_OK) ||
!((class == DATALINK_CLASS_PHYS || class ==
DATALINK_CLASS_SIMNET) && (media == DL_ETHER))) {
return (DLADM_STATUS_BADARG);
}
}
flags &= (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
if ((status = dladm_create_datalink_id(handle, name,
DATALINK_CLASS_AGGR, DL_ETHER, flags, &linkid)) !=
DLADM_STATUS_OK) {
goto fail;
}
if ((flags & DLADM_OPT_PERSIST) &&
(status = dladm_aggr_persist_aggr_conf(handle, name, linkid, key,
nports, ports, policy, mac_addr_fixed, mac_addr, lacp_mode,
lacp_timer, force)) != DLADM_STATUS_OK) {
goto fail;
}
if (!(flags & DLADM_OPT_ACTIVE))
return (DLADM_STATUS_OK);
status = i_dladm_aggr_create_sys(handle, linkid, key, nports, ports,
policy, mac_addr_fixed, mac_addr, lacp_mode, lacp_timer, force);
if (status != DLADM_STATUS_OK) {
if (flags & DLADM_OPT_PERSIST)
(void) dladm_remove_conf(handle, linkid);
goto fail;
}
return (DLADM_STATUS_OK);
fail:
if (linkid != DATALINK_INVALID_LINKID)
(void) dladm_destroy_datalink_id(handle, linkid, flags);
return (status);
}
static dladm_status_t
i_dladm_aggr_get_aggr_attr(dladm_handle_t handle, dladm_conf_t conf,
uint32_t mask, dladm_aggr_modify_attr_t *attrp)
{
dladm_status_t status = DLADM_STATUS_OK;
char macstr[ETHERADDRL * 3];
uint64_t u64;
if (mask & DLADM_AGGR_MODIFY_POLICY) {
status = dladm_get_conf_field(handle, conf, FPOLICY, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
return (status);
attrp->ld_policy = (uint32_t)u64;
}
if (mask & DLADM_AGGR_MODIFY_MAC) {
status = dladm_get_conf_field(handle, conf, FFIXMACADDR,
&attrp->ld_mac_fixed, sizeof (boolean_t));
if (status != DLADM_STATUS_OK)
return (status);
if (attrp->ld_mac_fixed) {
boolean_t fixed;
status = dladm_get_conf_field(handle, conf, FMACADDR,
macstr, sizeof (macstr));
if (status != DLADM_STATUS_OK)
return (status);
if (!dladm_aggr_str2macaddr(macstr, &fixed,
attrp->ld_mac)) {
return (DLADM_STATUS_REPOSITORYINVAL);
}
}
}
if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
status = dladm_get_conf_field(handle, conf, FLACPMODE, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
return (status);
attrp->ld_lacp_mode = (aggr_lacp_mode_t)u64;
}
if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
status = dladm_get_conf_field(handle, conf, FLACPTIMER, &u64,
sizeof (u64));
if (status != DLADM_STATUS_OK)
return (status);
attrp->ld_lacp_timer = (aggr_lacp_timer_t)u64;
}
return (status);
}
static dladm_status_t
i_dladm_aggr_set_aggr_attr(dladm_handle_t handle, dladm_conf_t conf,
uint32_t mask, dladm_aggr_modify_attr_t *attrp)
{
dladm_status_t status = DLADM_STATUS_OK;
char macstr[ETHERADDRL * 3];
uint64_t u64;
if (mask & DLADM_AGGR_MODIFY_POLICY) {
u64 = attrp->ld_policy;
status = dladm_set_conf_field(handle, conf, FPOLICY,
DLADM_TYPE_UINT64, &u64);
if (status != DLADM_STATUS_OK)
return (status);
}
if (mask & DLADM_AGGR_MODIFY_MAC) {
status = dladm_set_conf_field(handle, conf, FFIXMACADDR,
DLADM_TYPE_BOOLEAN, &attrp->ld_mac_fixed);
if (status != DLADM_STATUS_OK)
return (status);
if (attrp->ld_mac_fixed) {
(void) dladm_aggr_macaddr2str(attrp->ld_mac, macstr);
status = dladm_set_conf_field(handle, conf, FMACADDR,
DLADM_TYPE_STR, macstr);
if (status != DLADM_STATUS_OK)
return (status);
}
}
if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
u64 = attrp->ld_lacp_mode;
status = dladm_set_conf_field(handle, conf, FLACPMODE,
DLADM_TYPE_UINT64, &u64);
if (status != DLADM_STATUS_OK)
return (status);
}
if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
u64 = attrp->ld_lacp_timer;
status = dladm_set_conf_field(handle, conf, FLACPTIMER,
DLADM_TYPE_UINT64, &u64);
if (status != DLADM_STATUS_OK)
return (status);
}
return (status);
}
/*
* Modify the parameters of an existing link aggregation group. Update
* the configuration file and pass the changes to the kernel.
*/
dladm_status_t
dladm_aggr_modify(dladm_handle_t handle, datalink_id_t linkid,
uint32_t modify_mask, uint32_t policy, boolean_t mac_fixed,
const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
aggr_lacp_timer_t lacp_timer, uint32_t flags)
{
dladm_aggr_modify_attr_t new_attr, old_attr;
dladm_conf_t conf;
dladm_status_t status;
new_attr.ld_policy = policy;
new_attr.ld_mac_fixed = mac_fixed;
new_attr.ld_lacp_mode = lacp_mode;
new_attr.ld_lacp_timer = lacp_timer;
bcopy(mac_addr, new_attr.ld_mac, ETHERADDRL);
if (flags & DLADM_OPT_PERSIST) {
status = dladm_open_conf(handle, linkid, &conf);
if (status != DLADM_STATUS_OK)
return (status);
if ((status = i_dladm_aggr_get_aggr_attr(handle, conf,
modify_mask, &old_attr)) != DLADM_STATUS_OK) {
goto done;
}
if ((status = i_dladm_aggr_set_aggr_attr(handle, conf,
modify_mask, &new_attr)) != DLADM_STATUS_OK) {
goto done;
}
status = dladm_write_conf(handle, conf);
done:
dladm_destroy_conf(handle, conf);
if (status != DLADM_STATUS_OK)
return (status);
}
if (!(flags & DLADM_OPT_ACTIVE))
return (DLADM_STATUS_OK);
status = i_dladm_aggr_modify_sys(handle, linkid, modify_mask,
&new_attr);
if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
if (dladm_open_conf(handle, linkid, &conf) == DLADM_STATUS_OK) {
if (i_dladm_aggr_set_aggr_attr(handle, conf,
modify_mask, &old_attr) == DLADM_STATUS_OK) {
(void) dladm_write_conf(handle, conf);
}
dladm_destroy_conf(handle, conf);
}
}
return (status);
}
typedef struct aggr_held_arg_s {
datalink_id_t aggrid;
boolean_t isheld;
} aggr_held_arg_t;
static int
i_dladm_aggr_is_held(dladm_handle_t handle, datalink_id_t linkid, void *arg)
{
aggr_held_arg_t *aggr_held_arg = arg;
dladm_vlan_attr_t dva;
if (dladm_vlan_info(handle, linkid, &dva, DLADM_OPT_PERSIST) !=
DLADM_STATUS_OK)
return (DLADM_WALK_CONTINUE);
if (dva.dv_linkid == aggr_held_arg->aggrid) {
/*
* This VLAN is created over the given aggregation.
*/
aggr_held_arg->isheld = B_TRUE;
return (DLADM_WALK_TERMINATE);
}
return (DLADM_WALK_CONTINUE);
}
/*
* Delete a previously created link aggregation group. Either the name "aggr"
* or the "key" is specified.
*/
dladm_status_t
dladm_aggr_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
{
laioc_delete_t ioc;
datalink_class_t class;
dladm_status_t status;
if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL,
0) != DLADM_STATUS_OK) || (class != DATALINK_CLASS_AGGR)) {
return (DLADM_STATUS_BADARG);
}
if (flags & DLADM_OPT_ACTIVE) {
ioc.ld_linkid = linkid;
if ((i_dladm_aggr_ioctl(handle, LAIOC_DELETE, &ioc) < 0) &&
((errno != ENOENT) || !(flags & DLADM_OPT_PERSIST))) {
status = dladm_errno2status(errno);
return (status);
}
/*
* Delete ACTIVE linkprop first.
*/
(void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0,
DLADM_OPT_ACTIVE);
(void) dladm_destroy_datalink_id(handle, linkid,
DLADM_OPT_ACTIVE);
}
/*
* If we reach here, it means that the active aggregation has already
* been deleted, and there is no active VLANs holding this aggregation.
* Now we see whether there is any persistent VLANs holding this
* aggregation. If so, we fail the operation.
*/
if (flags & DLADM_OPT_PERSIST) {
aggr_held_arg_t arg;
arg.aggrid = linkid;
arg.isheld = B_FALSE;
(void) dladm_walk_datalink_id(i_dladm_aggr_is_held, handle,
&arg, DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE,
DLADM_OPT_PERSIST);
if (arg.isheld)
return (DLADM_STATUS_LINKBUSY);
(void) dladm_remove_conf(handle, linkid);
(void) dladm_destroy_datalink_id(handle, linkid,
DLADM_OPT_PERSIST);
}
return (DLADM_STATUS_OK);
}
/*
* Add one or more ports to an existing link aggregation.
*/
dladm_status_t
dladm_aggr_add(dladm_handle_t handle, datalink_id_t linkid, uint32_t nports,
dladm_aggr_port_attr_db_t *ports, uint32_t flags)
{
return (i_dladm_aggr_add_rmv(handle, linkid, nports, ports, flags,
LAIOC_ADD));
}
/*
* Remove one or more ports from an existing link aggregation.
*/
dladm_status_t
dladm_aggr_remove(dladm_handle_t handle, datalink_id_t linkid, uint32_t nports,
dladm_aggr_port_attr_db_t *ports, uint32_t flags)
{
return (i_dladm_aggr_add_rmv(handle, linkid, nports, ports, flags,
LAIOC_REMOVE));
}
typedef struct i_walk_key_state_s {
uint16_t key;
datalink_id_t linkid;
boolean_t found;
} i_walk_key_state_t;
static int
i_dladm_walk_key2linkid(dladm_handle_t handle, datalink_id_t linkid, void *arg)
{
dladm_conf_t conf;
uint16_t key;
dladm_status_t status;
i_walk_key_state_t *statep = (i_walk_key_state_t *)arg;
uint64_t u64;
if (dladm_getsnap_conf(handle, linkid, &conf) != DLADM_STATUS_OK)
return (DLADM_WALK_CONTINUE);
status = dladm_get_conf_field(handle, conf, FKEY, &u64, sizeof (u64));
key = (uint16_t)u64;
dladm_destroy_conf(handle, conf);
if ((status == DLADM_STATUS_OK) && (key == statep->key)) {
statep->found = B_TRUE;
statep->linkid = linkid;
return (DLADM_WALK_TERMINATE);
}
return (DLADM_WALK_CONTINUE);
}
dladm_status_t
dladm_key2linkid(dladm_handle_t handle, uint16_t key, datalink_id_t *linkidp,
uint32_t flags)
{
i_walk_key_state_t state;
if (key > AGGR_MAX_KEY)
return (DLADM_STATUS_NOTFOUND);
state.found = B_FALSE;
state.key = key;
(void) dladm_walk_datalink_id(i_dladm_walk_key2linkid, handle, &state,
DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags);
if (state.found == B_TRUE) {
*linkidp = state.linkid;
return (DLADM_STATUS_OK);
} else {
return (DLADM_STATUS_NOTFOUND);
}
}
|