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
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* iplink.c "ip link".
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <linux/mpls.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"
#include "namespace.h"
#ifndef GSO_MAX_SEGS
#define GSO_MAX_SEGS 65535
#endif
static void usage(void) __attribute__((noreturn));
void iplink_types_usage(void)
{
/* Remember to add new entry here if new type is added. */
fprintf(stderr,
"TYPE := { amt | bareudp | bond | bond_slave | bridge | bridge_slave |\n"
" dsa | dummy | erspan | geneve | gre | gretap | gtp | ifb |\n"
" ip6erspan | ip6gre | ip6gretap | ip6tnl |\n"
" ipip | ipoib | ipvlan | ipvtap |\n"
" macsec | macvlan | macvtap | netdevsim |\n"
" netkit | nlmon | rmnet | sit | team | team_slave |\n"
" vcan | veth | vlan | vrf | vti | vxcan | vxlan | wwan |\n"
" xfrm | virt_wifi }\n");
}
void iplink_usage(void)
{
fprintf(stderr,
"Usage: ip link add [link DEV | parentdev NAME] [ name ] NAME\n"
" [ txqueuelen PACKETS ]\n"
" [ address LLADDR ]\n"
" [ broadcast LLADDR ]\n"
" [ mtu MTU ] [index IDX ]\n"
" [ numtxqueues QUEUE_COUNT ]\n"
" [ numrxqueues QUEUE_COUNT ]\n"
" [ netns { PID | NETNSNAME | NETNSFILE } ]\n"
" type TYPE [ ARGS ]\n"
"\n"
" ip link delete { DEVICE | dev DEVICE | group DEVGROUP } type TYPE [ ARGS ]\n"
"\n"
" ip link set { DEVICE | dev DEVICE | group DEVGROUP }\n"
" [ { up | down } ]\n"
" [ type TYPE ARGS ]\n");
fprintf(stderr,
" [ arp { on | off } ]\n"
" [ dynamic { on | off } ]\n"
" [ multicast { on | off } ]\n"
" [ allmulticast { on | off } ]\n"
" [ promisc { on | off } ]\n"
" [ trailers { on | off } ]\n"
" [ carrier { on | off } ]\n"
" [ txqueuelen PACKETS ]\n"
" [ name NEWNAME ]\n"
" [ address LLADDR ]\n"
" [ broadcast LLADDR ]\n"
" [ mtu MTU ]\n"
" [ netns { PID | NETNSNAME | NETNSFILE } ]\n"
" [ link-netns NAME | link-netnsid ID ]\n"
" [ alias NAME ]\n"
" [ vf NUM [ mac LLADDR ]\n"
" [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]\n"
" [ rate TXRATE ]\n"
" [ max_tx_rate TXRATE ]\n"
" [ min_tx_rate TXRATE ]\n"
" [ spoofchk { on | off} ]\n"
" [ query_rss { on | off} ]\n"
" [ state { auto | enable | disable} ]\n"
" [ trust { on | off} ]\n"
" [ node_guid EUI64 ]\n"
" [ port_guid EUI64 ] ]\n"
" [ { xdp | xdpgeneric | xdpdrv | xdpoffload } { off |\n"
#ifdef HAVE_LIBBPF
" object FILE [ { section | program } NAME ] [ verbose ] |\n"
#else
" object FILE [ section NAME ] [ verbose ] |\n"
#endif
" pinned FILE } ]\n"
" [ master DEVICE ][ vrf NAME ]\n"
" [ nomaster ]\n"
" [ addrgenmode { eui64 | none | stable_secret | random } ]\n"
" [ protodown { on | off } ]\n"
" [ protodown_reason PREASON { on | off } ]\n"
" [ gso_max_size BYTES ] [ gso_ipv4_max_size BYTES ] [ gso_max_segs PACKETS ]\n"
" [ gro_max_size BYTES ] [ gro_ipv4_max_size BYTES ]\n"
"\n"
" ip link show [ DEVICE | group GROUP ] [up] [master DEV] [vrf NAME] [type TYPE]\n"
" [nomaster]\n"
"\n"
" ip link xstats type TYPE [ ARGS ]\n"
"\n"
" ip link afstats [ dev DEVICE ]\n"
" ip link property add dev DEVICE [ altname NAME .. ]\n"
" ip link property del dev DEVICE [ altname NAME .. ]\n");
fprintf(stderr,
"\n"
" ip link help [ TYPE ]\n"
"\n");
iplink_types_usage();
exit(-1);
}
static void usage(void)
{
iplink_usage();
}
static int on_off(const char *msg, const char *realval)
{
fprintf(stderr,
"Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n",
msg, realval);
return -1;
}
static void *BODY; /* cached dlopen(NULL) handle */
static struct link_util *linkutil_list;
struct link_util *get_link_kind(const char *id)
{
void *dlh;
char buf[256];
struct link_util *l;
for (l = linkutil_list; l; l = l->next)
if (strcmp(l->id, id) == 0)
return l;
snprintf(buf, sizeof(buf), "%s/link_%s.so", get_ip_lib_dir(), id);
dlh = dlopen(buf, RTLD_LAZY);
if (dlh == NULL) {
/* look in current binary, only open once */
dlh = BODY;
if (dlh == NULL) {
dlh = BODY = dlopen(NULL, RTLD_LAZY);
if (dlh == NULL)
return NULL;
}
}
snprintf(buf, sizeof(buf), "%s_link_util", id);
l = dlsym(dlh, buf);
if (l == NULL)
return NULL;
l->next = linkutil_list;
linkutil_list = l;
return l;
}
static int get_link_mode(const char *mode)
{
if (strcasecmp(mode, "default") == 0)
return IF_LINK_MODE_DEFAULT;
if (strcasecmp(mode, "dormant") == 0)
return IF_LINK_MODE_DORMANT;
return -1;
}
static int get_addr_gen_mode(const char *mode)
{
if (strcasecmp(mode, "eui64") == 0)
return IN6_ADDR_GEN_MODE_EUI64;
if (strcasecmp(mode, "none") == 0)
return IN6_ADDR_GEN_MODE_NONE;
if (strcasecmp(mode, "stable_secret") == 0)
return IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
if (strcasecmp(mode, "random") == 0)
return IN6_ADDR_GEN_MODE_RANDOM;
return -1;
}
static int nl_get_ll_addr_len(const char *ifname)
{
int len;
int dev_index = ll_name_to_index(ifname);
struct iplink_req req = {
.n = {
.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.nlmsg_type = RTM_GETLINK,
.nlmsg_flags = NLM_F_REQUEST
},
.i = {
.ifi_family = preferred_family,
.ifi_index = dev_index,
}
};
struct nlmsghdr *answer;
struct rtattr *tb[IFLA_MAX+1];
if (dev_index == 0)
return -1;
if (rtnl_talk(&rth, &req.n, &answer) < 0)
return -1;
len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
if (len < 0) {
free(answer);
return -1;
}
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)),
len, NLA_F_NESTED);
if (!tb[IFLA_ADDRESS]) {
free(answer);
return -1;
}
len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
free(answer);
return len;
}
static void iplink_parse_vf_vlan_info(int vf, int *argcp, char ***argvp,
struct ifla_vf_vlan_info *ivvip)
{
int argc = *argcp;
char **argv = *argvp;
unsigned int vci;
NEXT_ARG();
if (get_unsigned(&vci, *argv, 0) || vci > 4095)
invarg("Invalid \"vlan\" value\n", *argv);
ivvip->vlan = vci;
ivvip->vf = vf;
ivvip->qos = 0;
ivvip->vlan_proto = htons(ETH_P_8021Q);
if (NEXT_ARG_OK()) {
NEXT_ARG();
if (matches(*argv, "qos") == 0) {
NEXT_ARG();
if (get_unsigned(&ivvip->qos, *argv, 0))
invarg("Invalid \"qos\" value\n", *argv);
} else {
/* rewind arg */
PREV_ARG();
}
}
if (NEXT_ARG_OK()) {
NEXT_ARG();
if (matches(*argv, "proto") == 0) {
NEXT_ARG();
if (ll_proto_a2n(&ivvip->vlan_proto, *argv))
invarg("protocol is invalid\n", *argv);
if (ivvip->vlan_proto != htons(ETH_P_8021AD) &&
ivvip->vlan_proto != htons(ETH_P_8021Q)) {
SPRINT_BUF(b1);
SPRINT_BUF(b2);
char msg[64 + sizeof(b1) + sizeof(b2)];
sprintf(msg,
"Invalid \"vlan protocol\" value - supported %s, %s\n",
ll_proto_n2a(htons(ETH_P_8021Q),
b1, sizeof(b1)),
ll_proto_n2a(htons(ETH_P_8021AD),
b2, sizeof(b2)));
invarg(msg, *argv);
}
} else {
/* rewind arg */
PREV_ARG();
}
}
*argcp = argc;
*argvp = argv;
}
static int iplink_parse_vf(int vf, int *argcp, char ***argvp,
struct iplink_req *req, const char *dev)
{
char new_rate_api = 0, count = 0, override_legacy_rate = 0;
struct ifla_vf_rate tivt;
int len, argc = *argcp;
char **argv = *argvp;
struct rtattr *vfinfo;
int ret;
tivt.min_tx_rate = -1;
tivt.max_tx_rate = -1;
vfinfo = addattr_nest(&req->n, sizeof(*req), IFLA_VF_INFO);
while (NEXT_ARG_OK()) {
NEXT_ARG();
count++;
if (!matches(*argv, "max_tx_rate")) {
/* new API in use */
new_rate_api = 1;
/* override legacy rate */
override_legacy_rate = 1;
} else if (!matches(*argv, "min_tx_rate")) {
/* new API in use */
new_rate_api = 1;
}
}
while (count--) {
/* rewind arg */
PREV_ARG();
}
while (NEXT_ARG_OK()) {
NEXT_ARG();
if (matches(*argv, "mac") == 0) {
struct ifla_vf_mac ivm = { 0 };
int halen = nl_get_ll_addr_len(dev);
NEXT_ARG();
ivm.vf = vf;
len = ll_addr_a2n((char *)ivm.mac, 32, *argv);
if (len < 0)
return -1;
if (halen > 0 && len != halen) {
fprintf(stderr,
"Invalid address length %d - must be %d bytes\n",
len, halen);
return -1;
}
addattr_l(&req->n, sizeof(*req), IFLA_VF_MAC,
&ivm, sizeof(ivm));
} else if (matches(*argv, "vlan") == 0) {
struct ifla_vf_vlan_info ivvi;
iplink_parse_vf_vlan_info(vf, &argc, &argv, &ivvi);
/* support the old interface in case of older kernel*/
if (ivvi.vlan_proto == htons(ETH_P_8021Q)) {
struct ifla_vf_vlan ivv;
ivv.vf = ivvi.vf;
ivv.vlan = ivvi.vlan;
ivv.qos = ivvi.qos;
addattr_l(&req->n, sizeof(*req),
IFLA_VF_VLAN, &ivv, sizeof(ivv));
} else {
struct rtattr *vfvlanlist;
vfvlanlist = addattr_nest(&req->n, sizeof(*req),
IFLA_VF_VLAN_LIST);
addattr_l(&req->n, sizeof(*req),
IFLA_VF_VLAN_INFO, &ivvi,
sizeof(ivvi));
while (NEXT_ARG_OK()) {
NEXT_ARG();
if (matches(*argv, "vlan") != 0) {
PREV_ARG();
break;
}
iplink_parse_vf_vlan_info(vf, &argc,
&argv, &ivvi);
addattr_l(&req->n, sizeof(*req),
IFLA_VF_VLAN_INFO, &ivvi,
sizeof(ivvi));
}
addattr_nest_end(&req->n, vfvlanlist);
}
} else if (matches(*argv, "rate") == 0) {
struct ifla_vf_tx_rate ivt;
NEXT_ARG();
if (get_unsigned(&ivt.rate, *argv, 0))
invarg("Invalid \"rate\" value\n", *argv);
ivt.vf = vf;
if (!new_rate_api)
addattr_l(&req->n, sizeof(*req),
IFLA_VF_TX_RATE, &ivt, sizeof(ivt));
else if (!override_legacy_rate)
tivt.max_tx_rate = ivt.rate;
} else if (matches(*argv, "max_tx_rate") == 0) {
NEXT_ARG();
if (get_unsigned(&tivt.max_tx_rate, *argv, 0))
invarg("Invalid \"max tx rate\" value\n",
*argv);
tivt.vf = vf;
} else if (matches(*argv, "min_tx_rate") == 0) {
NEXT_ARG();
if (get_unsigned(&tivt.min_tx_rate, *argv, 0))
invarg("Invalid \"min tx rate\" value\n",
*argv);
tivt.vf = vf;
} else if (matches(*argv, "spoofchk") == 0) {
struct ifla_vf_spoofchk ivs;
NEXT_ARG();
ivs.setting = parse_on_off("spoofchk", *argv, &ret);
if (ret)
return ret;
ivs.vf = vf;
addattr_l(&req->n, sizeof(*req), IFLA_VF_SPOOFCHK,
&ivs, sizeof(ivs));
} else if (matches(*argv, "query_rss") == 0) {
struct ifla_vf_rss_query_en ivs;
NEXT_ARG();
ivs.setting = parse_on_off("query_rss", *argv, &ret);
if (ret)
return ret;
ivs.vf = vf;
addattr_l(&req->n, sizeof(*req), IFLA_VF_RSS_QUERY_EN,
&ivs, sizeof(ivs));
} else if (matches(*argv, "trust") == 0) {
struct ifla_vf_trust ivt;
NEXT_ARG();
ivt.setting = parse_on_off("trust", *argv, &ret);
if (ret)
return ret;
ivt.vf = vf;
addattr_l(&req->n, sizeof(*req), IFLA_VF_TRUST,
&ivt, sizeof(ivt));
} else if (matches(*argv, "state") == 0) {
struct ifla_vf_link_state ivl;
NEXT_ARG();
if (matches(*argv, "auto") == 0)
ivl.link_state = IFLA_VF_LINK_STATE_AUTO;
else if (matches(*argv, "enable") == 0)
ivl.link_state = IFLA_VF_LINK_STATE_ENABLE;
else if (matches(*argv, "disable") == 0)
ivl.link_state = IFLA_VF_LINK_STATE_DISABLE;
else
invarg("Invalid \"state\" value\n", *argv);
ivl.vf = vf;
addattr_l(&req->n, sizeof(*req), IFLA_VF_LINK_STATE,
&ivl, sizeof(ivl));
} else if (matches(*argv, "node_guid") == 0) {
struct ifla_vf_guid ivg;
NEXT_ARG();
ivg.vf = vf;
if (get_guid(&ivg.guid, *argv)) {
invarg("Invalid GUID format\n", *argv);
return -1;
}
addattr_l(&req->n, sizeof(*req), IFLA_VF_IB_NODE_GUID,
&ivg, sizeof(ivg));
} else if (matches(*argv, "port_guid") == 0) {
struct ifla_vf_guid ivg;
NEXT_ARG();
ivg.vf = vf;
if (get_guid(&ivg.guid, *argv)) {
invarg("Invalid GUID format\n", *argv);
return -1;
}
addattr_l(&req->n, sizeof(*req), IFLA_VF_IB_PORT_GUID,
&ivg, sizeof(ivg));
} else {
/* rewind arg */
PREV_ARG();
break;
}
}
if (new_rate_api) {
int tmin, tmax;
if (tivt.min_tx_rate == -1 || tivt.max_tx_rate == -1) {
ipaddr_get_vf_rate(tivt.vf, &tmin, &tmax, dev);
if (tivt.min_tx_rate == -1)
tivt.min_tx_rate = tmin;
if (tivt.max_tx_rate == -1)
tivt.max_tx_rate = tmax;
}
if (tivt.max_tx_rate && tivt.min_tx_rate > tivt.max_tx_rate) {
fprintf(stderr,
"Invalid min_tx_rate %d - must be <= max_tx_rate %d\n",
tivt.min_tx_rate, tivt.max_tx_rate);
return -1;
}
addattr_l(&req->n, sizeof(*req), IFLA_VF_RATE, &tivt,
sizeof(tivt));
}
if (argc == *argcp)
incomplete_command();
addattr_nest_end(&req->n, vfinfo);
*argcp = argc;
*argvp = argv;
return 0;
}
int iplink_parse(int argc, char **argv, struct iplink_req *req, char **type)
{
bool move_netns = false;
char *name = NULL;
char *dev = NULL;
char *link = NULL;
int ret, len;
char abuf[32];
int qlen = -1;
int mtu = -1;
int netns = -1;
int vf = -1;
int numtxqueues = -1;
int numrxqueues = -1;
int link_netnsid = -1;
int index = 0;
int group = -1;
int addr_len = 0;
int err;
ret = argc;
while (argc > 0) {
if (strcmp(*argv, "up") == 0) {
req->i.ifi_change |= IFF_UP;
req->i.ifi_flags |= IFF_UP;
} else if (strcmp(*argv, "down") == 0) {
req->i.ifi_change |= IFF_UP;
req->i.ifi_flags &= ~IFF_UP;
} else if (strcmp(*argv, "name") == 0) {
NEXT_ARG();
if (name)
duparg("name", *argv);
if (check_ifname(*argv))
invarg("\"name\" not a valid ifname", *argv);
name = *argv;
if (!dev)
dev = name;
} else if (strcmp(*argv, "index") == 0) {
NEXT_ARG();
if (index)
duparg("index", *argv);
index = atoi(*argv);
if (index <= 0)
invarg("Invalid \"index\" value", *argv);
} else if (matches(*argv, "link") == 0) {
NEXT_ARG();
link = *argv;
} else if (matches(*argv, "address") == 0) {
NEXT_ARG();
addr_len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
if (addr_len < 0)
return -1;
addattr_l(&req->n, sizeof(*req),
IFLA_ADDRESS, abuf, addr_len);
} else if (matches(*argv, "broadcast") == 0 ||
strcmp(*argv, "brd") == 0) {
NEXT_ARG();
len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
if (len < 0)
return -1;
addattr_l(&req->n, sizeof(*req),
IFLA_BROADCAST, abuf, len);
} else if (matches(*argv, "txqueuelen") == 0 ||
strcmp(*argv, "qlen") == 0 ||
matches(*argv, "txqlen") == 0) {
NEXT_ARG();
if (qlen != -1)
duparg("txqueuelen", *argv);
if (get_integer(&qlen, *argv, 0))
invarg("Invalid \"txqueuelen\" value\n", *argv);
addattr_l(&req->n, sizeof(*req),
IFLA_TXQLEN, &qlen, 4);
} else if (strcmp(*argv, "mtu") == 0) {
NEXT_ARG();
if (mtu != -1)
duparg("mtu", *argv);
if (get_integer(&mtu, *argv, 0))
invarg("Invalid \"mtu\" value\n", *argv);
addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4);
} else if (strcmp(*argv, "xdpgeneric") == 0 ||
strcmp(*argv, "xdpdrv") == 0 ||
strcmp(*argv, "xdpoffload") == 0 ||
strcmp(*argv, "xdp") == 0) {
bool generic = strcmp(*argv, "xdpgeneric") == 0;
bool drv = strcmp(*argv, "xdpdrv") == 0;
bool offload = strcmp(*argv, "xdpoffload") == 0;
NEXT_ARG();
if (xdp_parse(&argc, &argv, req, dev,
generic, drv, offload))
exit(-1);
if (offload && name == dev)
dev = NULL;
} else if (strcmp(*argv, "netns") == 0) {
NEXT_ARG();
if (netns != -1)
duparg("netns", *argv);
netns = netns_get_fd(*argv);
if (netns >= 0)
addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD,
&netns, 4);
else if (get_integer(&netns, *argv, 0) == 0)
addattr_l(&req->n, sizeof(*req),
IFLA_NET_NS_PID, &netns, 4);
else
invarg("Invalid \"netns\" value\n", *argv);
move_netns = true;
} else if (strcmp(*argv, "multicast") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_MULTICAST;
if (strcmp(*argv, "on") == 0)
req->i.ifi_flags |= IFF_MULTICAST;
else if (strcmp(*argv, "off") == 0)
req->i.ifi_flags &= ~IFF_MULTICAST;
else
return on_off("multicast", *argv);
} else if (strcmp(*argv, "allmulticast") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_ALLMULTI;
if (strcmp(*argv, "on") == 0)
req->i.ifi_flags |= IFF_ALLMULTI;
else if (strcmp(*argv, "off") == 0)
req->i.ifi_flags &= ~IFF_ALLMULTI;
else
return on_off("allmulticast", *argv);
} else if (strcmp(*argv, "promisc") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_PROMISC;
if (strcmp(*argv, "on") == 0)
req->i.ifi_flags |= IFF_PROMISC;
else if (strcmp(*argv, "off") == 0)
req->i.ifi_flags &= ~IFF_PROMISC;
else
return on_off("promisc", *argv);
} else if (strcmp(*argv, "trailers") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_NOTRAILERS;
if (strcmp(*argv, "off") == 0)
req->i.ifi_flags |= IFF_NOTRAILERS;
else if (strcmp(*argv, "on") == 0)
req->i.ifi_flags &= ~IFF_NOTRAILERS;
else
return on_off("trailers", *argv);
} else if (strcmp(*argv, "arp") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_NOARP;
if (strcmp(*argv, "on") == 0)
req->i.ifi_flags &= ~IFF_NOARP;
else if (strcmp(*argv, "off") == 0)
req->i.ifi_flags |= IFF_NOARP;
else
return on_off("arp", *argv);
} else if (strcmp(*argv, "carrier") == 0) {
int carrier;
NEXT_ARG();
carrier = parse_on_off("carrier", *argv, &err);
if (err)
return err;
addattr8(&req->n, sizeof(*req), IFLA_CARRIER, carrier);
} else if (strcmp(*argv, "vf") == 0) {
struct rtattr *vflist;
NEXT_ARG();
if (get_integer(&vf, *argv, 0))
invarg("Invalid \"vf\" value\n", *argv);
vflist = addattr_nest(&req->n, sizeof(*req),
IFLA_VFINFO_LIST);
if (!dev)
missarg("dev");
len = iplink_parse_vf(vf, &argc, &argv, req, dev);
if (len < 0)
return -1;
addattr_nest_end(&req->n, vflist);
if (name == dev)
dev = NULL;
} else if (matches(*argv, "master") == 0) {
int ifindex;
NEXT_ARG();
ifindex = ll_name_to_index(*argv);
if (!ifindex)
invarg("Device does not exist\n", *argv);
addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
&ifindex, 4);
} else if (strcmp(*argv, "vrf") == 0) {
int ifindex;
NEXT_ARG();
ifindex = ll_name_to_index(*argv);
if (!ifindex)
invarg("Not a valid VRF name\n", *argv);
if (!name_is_vrf(*argv))
invarg("Not a valid VRF name\n", *argv);
addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
&ifindex, sizeof(ifindex));
} else if (matches(*argv, "nomaster") == 0) {
int ifindex = 0;
addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
&ifindex, 4);
} else if (matches(*argv, "dynamic") == 0) {
NEXT_ARG();
req->i.ifi_change |= IFF_DYNAMIC;
if (strcmp(*argv, "on") == 0)
req->i.ifi_flags |= IFF_DYNAMIC;
else if (strcmp(*argv, "off") == 0)
req->i.ifi_flags &= ~IFF_DYNAMIC;
else
return on_off("dynamic", *argv);
} else if (matches(*argv, "type") == 0) {
NEXT_ARG();
*type = *argv;
argc--; argv++;
break;
} else if (matches(*argv, "alias") == 0) {
NEXT_ARG();
len = strlen(*argv);
if (len >= IFALIASZ)
invarg("alias too long\n", *argv);
addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
*argv, len);
} else if (strcmp(*argv, "group") == 0) {
NEXT_ARG();
if (group != -1)
duparg("group", *argv);
if (rtnl_group_a2n(&group, *argv))
invarg("Invalid \"group\" value\n", *argv);
addattr32(&req->n, sizeof(*req), IFLA_GROUP, group);
} else if (strcmp(*argv, "mode") == 0) {
int mode;
NEXT_ARG();
mode = get_link_mode(*argv);
if (mode < 0)
invarg("Invalid link mode\n", *argv);
addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
} else if (strcmp(*argv, "state") == 0) {
int state;
NEXT_ARG();
state = get_operstate(*argv);
if (state < 0)
invarg("Invalid operstate\n", *argv);
addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
} else if (matches(*argv, "numtxqueues") == 0) {
NEXT_ARG();
if (numtxqueues != -1)
duparg("numtxqueues", *argv);
if (get_integer(&numtxqueues, *argv, 0))
invarg("Invalid \"numtxqueues\" value\n",
*argv);
addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
&numtxqueues, 4);
} else if (matches(*argv, "numrxqueues") == 0) {
NEXT_ARG();
if (numrxqueues != -1)
duparg("numrxqueues", *argv);
if (get_integer(&numrxqueues, *argv, 0))
invarg("Invalid \"numrxqueues\" value\n",
*argv);
addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
&numrxqueues, 4);
} else if (matches(*argv, "addrgenmode") == 0) {
struct rtattr *afs, *afs6;
int mode;
NEXT_ARG();
mode = get_addr_gen_mode(*argv);
if (mode < 0)
invarg("Invalid address generation mode\n",
*argv);
afs = addattr_nest(&req->n, sizeof(*req), IFLA_AF_SPEC);
afs6 = addattr_nest(&req->n, sizeof(*req), AF_INET6);
addattr8(&req->n, sizeof(*req),
IFLA_INET6_ADDR_GEN_MODE, mode);
addattr_nest_end(&req->n, afs6);
addattr_nest_end(&req->n, afs);
} else if (matches(*argv, "link-netns") == 0) {
NEXT_ARG();
if (link_netnsid != -1)
duparg("link-netns/link-netnsid", *argv);
link_netnsid = get_netnsid_from_name(*argv);
/* No nsid? Try to assign one. */
if (link_netnsid < 0)
set_netnsid_from_name(*argv, -1);
link_netnsid = get_netnsid_from_name(*argv);
if (link_netnsid < 0)
invarg("Invalid \"link-netns\" value\n",
*argv);
addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID,
link_netnsid);
} else if (matches(*argv, "link-netnsid") == 0) {
NEXT_ARG();
if (link_netnsid != -1)
duparg("link-netns/link-netnsid", *argv);
if (get_integer(&link_netnsid, *argv, 0))
invarg("Invalid \"link-netnsid\" value\n",
*argv);
addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID,
link_netnsid);
} else if (strcmp(*argv, "protodown") == 0) {
unsigned int proto_down;
NEXT_ARG();
proto_down = parse_on_off("protodown", *argv, &err);
if (err)
return err;
addattr8(&req->n, sizeof(*req), IFLA_PROTO_DOWN,
proto_down);
} else if (strcmp(*argv, "protodown_reason") == 0) {
struct rtattr *pr;
__u32 preason = 0, prvalue = 0, prmask = 0;
NEXT_ARG();
if (protodown_reason_a2n(&preason, *argv))
invarg("invalid protodown reason\n", *argv);
NEXT_ARG();
prmask = 1 << preason;
if (matches(*argv, "on") == 0)
prvalue |= prmask;
else if (matches(*argv, "off") == 0)
prvalue &= ~prmask;
else
return on_off("protodown_reason", *argv);
pr = addattr_nest(&req->n, sizeof(*req),
IFLA_PROTO_DOWN_REASON | NLA_F_NESTED);
addattr32(&req->n, sizeof(*req),
IFLA_PROTO_DOWN_REASON_MASK, prmask);
addattr32(&req->n, sizeof(*req),
IFLA_PROTO_DOWN_REASON_VALUE, prvalue);
addattr_nest_end(&req->n, pr);
} else if (strcmp(*argv, "gso_max_size") == 0) {
unsigned int max_size;
NEXT_ARG();
if (get_unsigned(&max_size, *argv, 0))
invarg("Invalid \"gso_max_size\" value\n",
*argv);
addattr32(&req->n, sizeof(*req),
IFLA_GSO_MAX_SIZE, max_size);
} else if (strcmp(*argv, "gso_max_segs") == 0) {
unsigned int max_segs;
NEXT_ARG();
if (get_unsigned(&max_segs, *argv, 0) ||
max_segs > GSO_MAX_SEGS)
invarg("Invalid \"gso_max_segs\" value\n",
*argv);
addattr32(&req->n, sizeof(*req),
IFLA_GSO_MAX_SEGS, max_segs);
} else if (strcmp(*argv, "gro_max_size") == 0) {
unsigned int max_size;
NEXT_ARG();
if (get_unsigned(&max_size, *argv, 0))
invarg("Invalid \"gro_max_size\" value\n",
*argv);
addattr32(&req->n, sizeof(*req),
IFLA_GRO_MAX_SIZE, max_size);
} else if (strcmp(*argv, "gso_ipv4_max_size") == 0) {
unsigned int max_size;
NEXT_ARG();
if (get_unsigned(&max_size, *argv, 0))
invarg("Invalid \"gso_ipv4_max_size\" value\n",
*argv);
addattr32(&req->n, sizeof(*req),
IFLA_GSO_IPV4_MAX_SIZE, max_size);
} else if (strcmp(*argv, "gro_ipv4_max_size") == 0) {
unsigned int max_size;
NEXT_ARG();
if (get_unsigned(&max_size, *argv, 0))
invarg("Invalid \"gro_ipv4_max_size\" value\n",
*argv);
addattr32(&req->n, sizeof(*req),
IFLA_GRO_IPV4_MAX_SIZE, max_size);
} else if (strcmp(*argv, "parentdev") == 0) {
NEXT_ARG();
addattr_l(&req->n, sizeof(*req), IFLA_PARENT_DEV_NAME,
*argv, strlen(*argv) + 1);
} else {
if (matches(*argv, "help") == 0)
usage();
if (strcmp(*argv, "dev") == 0)
NEXT_ARG();
if (dev != name)
duparg2("dev", *argv);
if (check_altifname(*argv))
invarg("\"dev\" not a valid ifname", *argv);
dev = *argv;
}
argc--; argv++;
}
ret -= argc;
/* Allow "ip link add dev" and "ip link add name" */
if (!name)
name = dev;
else if (!dev)
dev = name;
else if (!strcmp(name, dev))
name = dev;
if (dev && addr_len &&
!(req->n.nlmsg_flags & NLM_F_CREATE)) {
int halen = nl_get_ll_addr_len(dev);
if (halen >= 0 && halen != addr_len) {
fprintf(stderr,
"Invalid address length %d - must be %d bytes\n",
addr_len, halen);
return -1;
}
}
if (index &&
(!(req->n.nlmsg_flags & NLM_F_CREATE) &&
!move_netns)) {
fprintf(stderr,
"index can be used only when creating devices or when moving device to another netns.\n");
exit(-1);
}
if (group != -1) {
if (!dev) {
if (argc) {
fprintf(stderr,
"Garbage instead of arguments \"%s ...\". Try \"ip link help\".\n",
*argv);
exit(-1);
}
if (req->n.nlmsg_flags & NLM_F_CREATE) {
fprintf(stderr,
"group cannot be used when creating devices.\n");
exit(-1);
}
*type = NULL;
return ret;
}
}
if (!(req->n.nlmsg_flags & NLM_F_CREATE)) {
if (!dev) {
fprintf(stderr,
"Not enough information: \"dev\" argument is required.\n");
exit(-1);
}
req->i.ifi_index = ll_name_to_index(dev);
if (!req->i.ifi_index)
return nodev(dev);
/* Not renaming to the same name */
if (name == dev)
name = NULL;
if (index)
addattr32(&req->n, sizeof(*req), IFLA_NEW_IFINDEX, index);
} else {
if (name != dev) {
fprintf(stderr,
"both \"name\" and \"dev\" cannot be used when creating devices.\n");
exit(-1);
}
if (link) {
int ifindex;
ifindex = ll_name_to_index(link);
if (!ifindex)
return nodev(link);
addattr32(&req->n, sizeof(*req), IFLA_LINK, ifindex);
}
req->i.ifi_index = index;
}
if (name) {
addattr_l(&req->n, sizeof(*req),
IFLA_IFNAME, name, strlen(name) + 1);
}
return ret;
}
static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
{
char *type = NULL;
struct iplink_req req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.n.nlmsg_flags = NLM_F_REQUEST | flags,
.n.nlmsg_type = cmd,
.i.ifi_family = preferred_family,
};
int ret;
ret = iplink_parse(argc, argv, &req, &type);
if (ret < 0)
return ret;
if (type) {
struct link_util *lu;
struct rtattr *linkinfo;
char *ulinep = strchr(type, '_');
int iflatype;
linkinfo = addattr_nest(&req.n, sizeof(req), IFLA_LINKINFO);
addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type,
strlen(type));
lu = get_link_kind(type);
if (ulinep && !strcmp(ulinep, "_slave"))
iflatype = IFLA_INFO_SLAVE_DATA;
else
iflatype = IFLA_INFO_DATA;
argc -= ret;
argv += ret;
if (lu && lu->parse_opt && argc) {
struct rtattr *data;
data = addattr_nest(&req.n, sizeof(req), iflatype);
if (lu->parse_opt(lu, argc, argv, &req.n))
return -1;
addattr_nest_end(&req.n, data);
} else if (argc) {
if (matches(*argv, "help") == 0)
usage();
fprintf(stderr,
"Garbage instead of arguments \"%s ...\". Try \"ip link help\".\n",
*argv);
return -1;
}
addattr_nest_end(&req.n, linkinfo);
} else if (flags & NLM_F_CREATE) {
fprintf(stderr,
"Not enough information: \"type\" argument is required\n");
return -1;
}
if (echo_request)
ret = rtnl_echo_talk(&rth, &req.n, json, print_linkinfo);
else
ret = rtnl_talk(&rth, &req.n, NULL);
if (ret)
return -2;
/* remove device from cache; next use can refresh with new data */
ll_drop_by_index(req.i.ifi_index);
return 0;
}
int iplink_get(char *name, __u32 filt_mask)
{
struct iplink_req req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.n.nlmsg_flags = NLM_F_REQUEST,
.n.nlmsg_type = RTM_GETLINK,
.i.ifi_family = preferred_family,
};
struct nlmsghdr *answer;
if (name) {
addattr_l(&req.n, sizeof(req),
!check_ifname(name) ? IFLA_IFNAME : IFLA_ALT_IFNAME,
name, strlen(name) + 1);
}
if (!show_stats)
filt_mask |= RTEXT_FILTER_SKIP_STATS;
addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask);
if (rtnl_talk(&rth, &req.n, &answer) < 0)
return -2;
open_json_object(NULL);
print_linkinfo(answer, stdout);
close_json_object();
free(answer);
return 0;
}
void print_mpls_link_stats(FILE *fp, const struct mpls_link_stats *stats,
const char *indent)
{
unsigned int cols[] = {
strlen("*X: bytes"),
strlen("packets"),
strlen("errors"),
strlen("dropped"),
strlen("noroute"),
};
if (is_json_context()) {
/* RX stats */
open_json_object("rx");
print_u64(PRINT_JSON, "bytes", NULL, stats->rx_bytes);
print_u64(PRINT_JSON, "packets", NULL, stats->rx_packets);
print_u64(PRINT_JSON, "errors", NULL, stats->rx_errors);
print_u64(PRINT_JSON, "dropped", NULL, stats->rx_dropped);
print_u64(PRINT_JSON, "noroute", NULL, stats->rx_noroute);
close_json_object();
/* TX stats */
open_json_object("tx");
print_u64(PRINT_JSON, "bytes", NULL, stats->tx_bytes);
print_u64(PRINT_JSON, "packets", NULL, stats->tx_packets);
print_u64(PRINT_JSON, "errors", NULL, stats->tx_errors);
print_u64(PRINT_JSON, "dropped", NULL, stats->tx_dropped);
close_json_object();
} else {
size_columns(cols, ARRAY_SIZE(cols), stats->rx_bytes,
stats->rx_packets, stats->rx_errors,
stats->rx_dropped, stats->rx_noroute);
size_columns(cols, ARRAY_SIZE(cols), stats->tx_bytes,
stats->tx_packets, stats->tx_errors,
stats->tx_dropped, 0);
fprintf(fp, "%sRX: %*s %*s %*s %*s %*s%s", indent,
cols[0] - 4, "bytes", cols[1], "packets",
cols[2], "errors", cols[3], "dropped",
cols[4], "noroute", _SL_);
fprintf(fp, "%s", indent);
print_num(fp, cols[0], stats->rx_bytes);
print_num(fp, cols[1], stats->rx_packets);
print_num(fp, cols[2], stats->rx_errors);
print_num(fp, cols[3], stats->rx_dropped);
print_num(fp, cols[4], stats->rx_noroute);
print_nl();
fprintf(fp, "%sTX: %*s %*s %*s %*s%s", indent,
cols[0] - 4, "bytes", cols[1], "packets",
cols[2], "errors", cols[3], "dropped", _SL_);
fprintf(fp, "%s", indent);
print_num(fp, cols[0], stats->tx_bytes);
print_num(fp, cols[1], stats->tx_packets);
print_num(fp, cols[2], stats->tx_errors);
print_num(fp, cols[3], stats->tx_dropped);
}
}
static void print_mpls_stats(FILE *fp, struct rtattr *attr)
{
struct rtattr *mrtb[MPLS_STATS_MAX+1];
struct mpls_link_stats *stats;
parse_rtattr(mrtb, MPLS_STATS_MAX, RTA_DATA(attr),
RTA_PAYLOAD(attr));
if (!mrtb[MPLS_STATS_LINK])
return;
stats = RTA_DATA(mrtb[MPLS_STATS_LINK]);
print_string(PRINT_FP, NULL, " mpls:", NULL);
print_nl();
print_mpls_link_stats(fp, stats, " ");
print_string(PRINT_FP, NULL, "%s", "\n");
fflush(fp);
}
static void print_af_stats_attr(FILE *fp, int ifindex, struct rtattr *attr)
{
bool if_printed = false;
struct rtattr *i;
int rem;
rem = RTA_PAYLOAD(attr);
for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
if (preferred_family != AF_UNSPEC &&
i->rta_type != preferred_family)
continue;
if (!if_printed) {
print_uint(PRINT_ANY, "ifindex",
"%u:", ifindex);
print_color_string(PRINT_ANY, COLOR_IFNAME,
"ifname", "%s",
ll_index_to_name(ifindex));
print_nl();
if_printed = true;
}
switch (i->rta_type) {
case AF_MPLS:
print_mpls_stats(fp, i);
break;
default:
fprintf(stderr, " unknown af(%d)\n", i->rta_type);
break;
}
}
}
struct af_stats_ctx {
FILE *fp;
int ifindex;
};
static int print_af_stats(struct nlmsghdr *n, void *arg)
{
struct if_stats_msg *ifsm = NLMSG_DATA(n);
struct rtattr *tb[IFLA_STATS_MAX+1];
int len = n->nlmsg_len;
struct af_stats_ctx *ctx = arg;
FILE *fp = ctx->fp;
len -= NLMSG_LENGTH(sizeof(*ifsm));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
if (ctx->ifindex && ifsm->ifindex != ctx->ifindex)
return 0;
parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len);
if (tb[IFLA_STATS_AF_SPEC])
print_af_stats_attr(fp, ifsm->ifindex, tb[IFLA_STATS_AF_SPEC]);
fflush(fp);
return 0;
}
static int iplink_afstats(int argc, char **argv)
{
__u32 filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_AF_SPEC);
const char *filter_dev = NULL;
struct af_stats_ctx ctx = {
.fp = stdout,
.ifindex = 0,
};
while (argc > 0) {
if (strcmp(*argv, "dev") == 0) {
NEXT_ARG();
if (filter_dev)
duparg2("dev", *argv);
filter_dev = *argv;
} else if (matches(*argv, "help") == 0) {
usage();
} else {
fprintf(stderr,
"Command \"%s\" is unknown, try \"ip link help\".\n",
*argv);
exit(-1);
}
argv++; argc--;
}
if (filter_dev) {
ctx.ifindex = ll_name_to_index(filter_dev);
if (ctx.ifindex <= 0) {
fprintf(stderr,
"Device \"%s\" does not exist.\n",
filter_dev);
return -1;
}
}
new_json_obj(json);
if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, filt_mask,
NULL, NULL) < 0) {
perror("Cannot send dump request");
delete_json_obj();
return 1;
}
if (rtnl_dump_filter(&rth, print_af_stats, &ctx) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();
return 0;
}
static int iplink_prop_mod(int argc, char **argv, struct iplink_req *req)
{
struct rtattr *proplist;
char *dev = NULL;
char *name;
proplist = addattr_nest(&req->n, sizeof(*req),
IFLA_PROP_LIST | NLA_F_NESTED);
while (argc > 0) {
if (matches(*argv, "altname") == 0) {
NEXT_ARG();
if (check_altifname(*argv))
invarg("not a valid altname", *argv);
name = *argv;
addattr_l(&req->n, sizeof(*req), IFLA_ALT_IFNAME,
name, strlen(name) + 1);
} else if (matches(*argv, "help") == 0) {
usage();
} else {
if (strcmp(*argv, "dev") == 0)
NEXT_ARG();
if (dev)
duparg2("dev", *argv);
if (check_altifname(*argv))
invarg("\"dev\" not a valid ifname", *argv);
dev = *argv;
}
argv++; argc--;
}
addattr_nest_end(&req->n, proplist);
if (!dev) {
fprintf(stderr, "Not enough of information: \"dev\" argument is required.\n");
exit(-1);
}
req->i.ifi_index = ll_name_to_index(dev);
if (!req->i.ifi_index)
return nodev(dev);
if (rtnl_talk(&rth, &req->n, NULL) < 0)
return -2;
return 0;
}
static int iplink_prop(int argc, char **argv)
{
struct iplink_req req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.n.nlmsg_flags = NLM_F_REQUEST,
.i.ifi_family = preferred_family,
};
if (argc <= 0) {
usage();
exit(-1);
}
if (matches(*argv, "add") == 0) {
req.n.nlmsg_flags |= NLM_F_EXCL | NLM_F_CREATE | NLM_F_APPEND;
req.n.nlmsg_type = RTM_NEWLINKPROP;
} else if (matches(*argv, "del") == 0) {
req.n.nlmsg_type = RTM_DELLINKPROP;
} else if (matches(*argv, "help") == 0) {
usage();
} else {
fprintf(stderr, "Operator required\n");
exit(-1);
}
return iplink_prop_mod(argc - 1, argv + 1, &req);
}
static void do_help(int argc, char **argv)
{
struct link_util *lu = NULL;
if (argc <= 0) {
usage();
return;
}
lu = get_link_kind(*argv);
if (lu && lu->print_help)
lu->print_help(lu, argc-1, argv+1, stdout);
else
usage();
}
int do_iplink(int argc, char **argv)
{
if (argc < 1)
return ipaddr_list_link(0, NULL);
if (matches(*argv, "add") == 0)
return iplink_modify(RTM_NEWLINK,
NLM_F_CREATE|NLM_F_EXCL,
argc-1, argv+1);
if (matches(*argv, "set") == 0 ||
matches(*argv, "change") == 0)
return iplink_modify(RTM_NEWLINK, 0,
argc-1, argv+1);
if (matches(*argv, "replace") == 0)
return iplink_modify(RTM_NEWLINK,
NLM_F_CREATE|NLM_F_REPLACE,
argc-1, argv+1);
if (matches(*argv, "delete") == 0)
return iplink_modify(RTM_DELLINK, 0,
argc-1, argv+1);
if (matches(*argv, "show") == 0 ||
matches(*argv, "lst") == 0 ||
matches(*argv, "list") == 0)
return ipaddr_list_link(argc-1, argv+1);
if (matches(*argv, "xstats") == 0)
return iplink_ifla_xstats(argc-1, argv+1);
if (matches(*argv, "afstats") == 0) {
iplink_afstats(argc-1, argv+1);
return 0;
}
if (matches(*argv, "property") == 0)
return iplink_prop(argc-1, argv+1);
if (matches(*argv, "help") == 0) {
do_help(argc-1, argv+1);
return 0;
}
fprintf(stderr, "Command \"%s\" is unknown, try \"ip link help\".\n",
*argv);
exit(-1);
}
|