]> git.proxmox.com Git - mirror_iproute2.git/blame - bridge/vlan.c
libnetlink: Rename rtnl_wilddump_stats_req_filter to rtnl_statsdump_req_filter
[mirror_iproute2.git] / bridge / vlan.c
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
9eff0e5c
VY
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <sys/socket.h>
7#include <net/if.h>
8#include <netinet/in.h>
9#include <linux/if_bridge.h>
10#include <linux/if_ether.h>
11#include <string.h>
12
c7c1a1ef 13#include "json_print.h"
9eff0e5c
VY
14#include "libnetlink.h"
15#include "br_common.h"
16#include "utils.h"
17
5a2d0201 18static unsigned int filter_index, filter_vlan;
8652eeb3 19static int show_vlan_tunnel_info = 0;
9eff0e5c
VY
20
21static void usage(void)
22{
bcddcddd 23 fprintf(stderr,
8652eeb3
RP
24 "Usage: bridge vlan { add | del } vid VLAN_ID dev DEV [ tunnel_info id TUNNEL_ID ]\n"
25 " [ pvid ] [ untagged ]\n"
bcddcddd 26 " [ self ] [ master ]\n"
8652eeb3
RP
27 " bridge vlan { show } [ dev DEV ] [ vid VLAN_ID ]\n"
28 " bridge vlan { tunnelshow } [ dev DEV ] [ vid VLAN_ID ]\n");
9eff0e5c
VY
29 exit(-1);
30}
31
8652eeb3
RP
32static int parse_tunnel_info(int *argcp, char ***argvp, __u32 *tun_id_start,
33 __u32 *tun_id_end)
34{
35 char **argv = *argvp;
36 int argc = *argcp;
37 char *t;
38
39 NEXT_ARG();
40 if (!matches(*argv, "id")) {
41 NEXT_ARG();
42 t = strchr(*argv, '-');
43 if (t) {
44 *t = '\0';
45 if (get_u32(tun_id_start, *argv, 0) ||
46 *tun_id_start >= 1u << 24)
47 invarg("invalid tun id", *argv);
48 if (get_u32(tun_id_end, t + 1, 0) ||
49 *tun_id_end >= 1u << 24)
50 invarg("invalid tun id", *argv);
51
52 } else {
53 if (get_u32(tun_id_start, *argv, 0) ||
54 *tun_id_start >= 1u << 24)
55 invarg("invalid tun id", *argv);
56 }
57 } else {
58 invarg("tunnel id expected", *argv);
59 }
60
61 *argcp = argc;
62 *argvp = argv;
63
64 return 0;
65}
66
67static int add_tunnel_info(struct nlmsghdr *n, int reqsize,
68 __u16 vid, __u32 tun_id, __u16 flags)
69{
70 struct rtattr *tinfo;
71
72 tinfo = addattr_nest(n, reqsize, IFLA_BRIDGE_VLAN_TUNNEL_INFO);
73 addattr32(n, reqsize, IFLA_BRIDGE_VLAN_TUNNEL_ID, tun_id);
74 addattr32(n, reqsize, IFLA_BRIDGE_VLAN_TUNNEL_VID, vid);
75 addattr32(n, reqsize, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS, flags);
76
77 addattr_nest_end(n, tinfo);
78
79 return 0;
80}
81
82static int add_tunnel_info_range(struct nlmsghdr *n, int reqsize,
83 __u16 vid_start, int16_t vid_end,
84 __u32 tun_id_start, __u32 tun_id_end)
85{
86 if (vid_end != -1 && (vid_end - vid_start) > 0) {
87 add_tunnel_info(n, reqsize, vid_start, tun_id_start,
88 BRIDGE_VLAN_INFO_RANGE_BEGIN);
89
90 add_tunnel_info(n, reqsize, vid_end, tun_id_end,
91 BRIDGE_VLAN_INFO_RANGE_END);
92 } else {
93 add_tunnel_info(n, reqsize, vid_start, tun_id_start, 0);
94 }
95
96 return 0;
97}
98
99static int add_vlan_info_range(struct nlmsghdr *n, int reqsize, __u16 vid_start,
100 int16_t vid_end, __u16 flags)
101{
102 struct bridge_vlan_info vinfo = {};
103
104 vinfo.flags = flags;
105 vinfo.vid = vid_start;
106 if (vid_end != -1) {
107 /* send vlan range start */
108 addattr_l(n, reqsize, IFLA_BRIDGE_VLAN_INFO, &vinfo,
109 sizeof(vinfo));
110 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
111
112 /* Now send the vlan range end */
113 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
114 vinfo.vid = vid_end;
115 addattr_l(n, reqsize, IFLA_BRIDGE_VLAN_INFO, &vinfo,
116 sizeof(vinfo));
117 } else {
118 addattr_l(n, reqsize, IFLA_BRIDGE_VLAN_INFO, &vinfo,
119 sizeof(vinfo));
120 }
121
122 return 0;
123}
124
9eff0e5c
VY
125static int vlan_modify(int cmd, int argc, char **argv)
126{
127 struct {
df4b043f
SH
128 struct nlmsghdr n;
129 struct ifinfomsg ifm;
130 char buf[1024];
d17b136f
PS
131 } req = {
132 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
133 .n.nlmsg_flags = NLM_F_REQUEST,
134 .n.nlmsg_type = cmd,
135 .ifm.ifi_family = PF_BRIDGE,
136 };
9eff0e5c
VY
137 char *d = NULL;
138 short vid = -1;
3ac0d36d 139 short vid_end = -1;
9eff0e5c 140 struct rtattr *afspec;
d17b136f 141 struct bridge_vlan_info vinfo = {};
8652eeb3 142 bool tunnel_info_set = false;
9eff0e5c 143 unsigned short flags = 0;
8652eeb3
RP
144 __u32 tun_id_start = 0;
145 __u32 tun_id_end = 0;
9eff0e5c 146
9eff0e5c
VY
147 while (argc > 0) {
148 if (strcmp(*argv, "dev") == 0) {
149 NEXT_ARG();
150 d = *argv;
151 } else if (strcmp(*argv, "vid") == 0) {
3ac0d36d 152 char *p;
df4b043f 153
9eff0e5c 154 NEXT_ARG();
3ac0d36d
RP
155 p = strchr(*argv, '-');
156 if (p) {
157 *p = '\0';
158 p++;
159 vid = atoi(*argv);
160 vid_end = atoi(p);
161 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
162 } else {
163 vid = atoi(*argv);
164 }
9eff0e5c
VY
165 } else if (strcmp(*argv, "self") == 0) {
166 flags |= BRIDGE_FLAGS_SELF;
167 } else if (strcmp(*argv, "master") == 0) {
168 flags |= BRIDGE_FLAGS_MASTER;
169 } else if (strcmp(*argv, "pvid") == 0) {
170 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
171 } else if (strcmp(*argv, "untagged") == 0) {
172 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
8652eeb3
RP
173 } else if (strcmp(*argv, "tunnel_info") == 0) {
174 if (parse_tunnel_info(&argc, &argv,
175 &tun_id_start,
176 &tun_id_end))
177 return -1;
178 tunnel_info_set = true;
9eff0e5c 179 } else {
bcddcddd 180 if (matches(*argv, "help") == 0)
9eff0e5c 181 NEXT_ARG();
9eff0e5c
VY
182 }
183 argc--; argv++;
184 }
185
186 if (d == NULL || vid == -1) {
187 fprintf(stderr, "Device and VLAN ID are required arguments.\n");
42ecedd4 188 return -1;
9eff0e5c
VY
189 }
190
191 req.ifm.ifi_index = ll_name_to_index(d);
192 if (req.ifm.ifi_index == 0) {
193 fprintf(stderr, "Cannot find bridge device \"%s\"\n", d);
194 return -1;
195 }
196
197 if (vid >= 4096) {
198 fprintf(stderr, "Invalid VLAN ID \"%hu\"\n", vid);
199 return -1;
200 }
201
3ac0d36d
RP
202 if (vinfo.flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
203 if (vid_end == -1 || vid_end >= 4096 || vid >= vid_end) {
204 fprintf(stderr, "Invalid VLAN range \"%hu-%hu\"\n",
205 vid, vid_end);
206 return -1;
207 }
208 if (vinfo.flags & BRIDGE_VLAN_INFO_PVID) {
209 fprintf(stderr,
210 "pvid cannot be configured for a vlan range\n");
211 return -1;
212 }
213 }
9eff0e5c
VY
214
215 afspec = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
216
217 if (flags)
218 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags);
219
8652eeb3
RP
220 if (tunnel_info_set)
221 add_tunnel_info_range(&req.n, sizeof(req), vid, vid_end,
222 tun_id_start, tun_id_end);
223 else
224 add_vlan_info_range(&req.n, sizeof(req), vid, vid_end,
225 vinfo.flags);
9eff0e5c
VY
226
227 addattr_nest_end(&req.n, afspec);
228
86bf43c7 229 if (rtnl_talk(&rth, &req.n, NULL) < 0)
42ecedd4 230 return -1;
9eff0e5c
VY
231
232 return 0;
233}
234
5a2d0201
NA
235/* In order to use this function for both filtering and non-filtering cases
236 * we need to make it a tristate:
237 * return -1 - if filtering we've gone over so don't continue
238 * return 0 - skip entry and continue (applies to range start or to entries
239 * which are less than filter_vlan)
240 * return 1 - print the entry and continue
241 */
8652eeb3 242static int filter_vlan_check(__u16 vid, __u16 flags)
5a2d0201
NA
243{
244 /* if we're filtering we should stop on the first greater entry */
8652eeb3
RP
245 if (filter_vlan && vid > filter_vlan &&
246 !(flags & BRIDGE_VLAN_INFO_RANGE_END))
5a2d0201 247 return -1;
8652eeb3
RP
248 if ((flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) ||
249 vid < filter_vlan)
5a2d0201
NA
250 return 0;
251
252 return 1;
253}
254
0f362674 255static void open_vlan_port(int ifi_index)
d82a49ce 256{
0f362674
SH
257 open_json_object(NULL);
258 print_string(PRINT_ANY, "ifname", "%s",
c7c1a1ef 259 ll_index_to_name(ifi_index));
0f362674
SH
260 open_json_array(PRINT_JSON, "vlans");
261}
262
263static void close_vlan_port(void)
264{
265 close_json_array(PRINT_JSON, NULL);
266 close_json_object();
d82a49ce
RP
267}
268
c7c1a1ef 269static void print_range(const char *name, __u16 start, __u16 id)
d82a49ce 270{
c7c1a1ef
SH
271 char end[64];
272
273 snprintf(end, sizeof(end), "%sEnd", name);
274
275 print_hu(PRINT_ANY, name, "\t %hu", start);
276 if (start != id)
277 print_hu(PRINT_ANY, end, "-%hu", id);
278
d82a49ce
RP
279}
280
8652eeb3
RP
281static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex)
282{
8652eeb3
RP
283 struct rtattr *i, *list = tb;
284 int rem = RTA_PAYLOAD(list);
285 __u16 last_vid_start = 0;
286 __u32 last_tunid_start = 0;
287
c7c1a1ef 288 if (!filter_vlan)
0f362674 289 open_vlan_port(ifindex);
8652eeb3 290
c7c1a1ef 291 open_json_array(PRINT_JSON, "tunnel");
8652eeb3
RP
292 for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
293 struct rtattr *ttb[IFLA_BRIDGE_VLAN_TUNNEL_MAX+1];
294 __u32 tunnel_id = 0;
295 __u16 tunnel_vid = 0;
296 __u16 tunnel_flags = 0;
297 int vcheck_ret;
298
299 if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO)
300 continue;
301
302 parse_rtattr(ttb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
303 RTA_DATA(i), RTA_PAYLOAD(i));
304
305 if (ttb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
306 tunnel_vid =
307 rta_getattr_u32(ttb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
308 else
309 continue;
310
311 if (ttb[IFLA_BRIDGE_VLAN_TUNNEL_ID])
312 tunnel_id =
313 rta_getattr_u32(ttb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
314
315 if (ttb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
316 tunnel_flags =
317 rta_getattr_u32(ttb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
318
319 if (!(tunnel_flags & BRIDGE_VLAN_INFO_RANGE_END)) {
320 last_vid_start = tunnel_vid;
321 last_tunid_start = tunnel_id;
322 }
c7c1a1ef 323
8652eeb3
RP
324 vcheck_ret = filter_vlan_check(tunnel_vid, tunnel_flags);
325 if (vcheck_ret == -1)
326 break;
327 else if (vcheck_ret == 0)
328 continue;
329
330 if (tunnel_flags & BRIDGE_VLAN_INFO_RANGE_BEGIN)
331 continue;
332
c7c1a1ef 333 if (filter_vlan)
0f362674 334 open_vlan_port(ifindex);
8652eeb3 335
c7c1a1ef
SH
336 open_json_object(NULL);
337 print_range("vlan", last_vid_start, tunnel_vid);
338 print_range("tunid", last_tunid_start, tunnel_id);
339 close_json_object();
8652eeb3 340
0f362674
SH
341 print_string(PRINT_FP, NULL, "%s", _SL_);
342 if (filter_vlan)
343 close_vlan_port();
8652eeb3 344 }
0f362674
SH
345
346 if (!filter_vlan)
347 close_vlan_port();
8652eeb3
RP
348}
349
350static int print_vlan_tunnel(const struct sockaddr_nl *who,
351 struct nlmsghdr *n,
352 void *arg)
353{
354 struct ifinfomsg *ifm = NLMSG_DATA(n);
355 struct rtattr *tb[IFLA_MAX+1];
356 int len = n->nlmsg_len;
357 FILE *fp = arg;
358
359 if (n->nlmsg_type != RTM_NEWLINK) {
360 fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n",
361 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
362 return 0;
363 }
364
365 len -= NLMSG_LENGTH(sizeof(*ifm));
366 if (len < 0) {
367 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
368 return -1;
369 }
370
371 if (ifm->ifi_family != AF_BRIDGE)
372 return 0;
373
374 if (filter_index && filter_index != ifm->ifi_index)
375 return 0;
376
377 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len);
378
379 /* if AF_SPEC isn't there, vlan table is not preset for this port */
380 if (!tb[IFLA_AF_SPEC]) {
c7c1a1ef
SH
381 if (!filter_vlan && !is_json_context()) {
382 color_fprintf(fp, COLOR_IFNAME, "%s",
383 ll_index_to_name(ifm->ifi_index));
384 fprintf(fp, "\tNone\n");
385 }
8652eeb3
RP
386 return 0;
387 }
388
389 print_vlan_tunnel_info(fp, tb[IFLA_AF_SPEC], ifm->ifi_index);
390
391 fflush(fp);
392 return 0;
393}
394
9eff0e5c
VY
395static int print_vlan(const struct sockaddr_nl *who,
396 struct nlmsghdr *n,
397 void *arg)
398{
399 FILE *fp = arg;
400 struct ifinfomsg *ifm = NLMSG_DATA(n);
401 int len = n->nlmsg_len;
df4b043f 402 struct rtattr *tb[IFLA_MAX+1];
9eff0e5c
VY
403
404 if (n->nlmsg_type != RTM_NEWLINK) {
405 fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n",
406 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
407 return 0;
408 }
409
410 len -= NLMSG_LENGTH(sizeof(*ifm));
411 if (len < 0) {
412 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
413 return -1;
414 }
415
416 if (ifm->ifi_family != AF_BRIDGE)
417 return 0;
418
419 if (filter_index && filter_index != ifm->ifi_index)
420 return 0;
421
422 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len);
423
424 /* if AF_SPEC isn't there, vlan table is not preset for this port */
425 if (!tb[IFLA_AF_SPEC]) {
c7c1a1ef
SH
426 if (!filter_vlan && !is_json_context()) {
427 color_fprintf(fp, COLOR_IFNAME, "%s",
428 ll_index_to_name(ifm->ifi_index));
429 fprintf(fp, "\tNone\n");
430 }
9eff0e5c 431 return 0;
9eff0e5c 432 }
bcddcddd 433
0f362674
SH
434 print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index);
435 print_string(PRINT_FP, NULL, "%s", _SL_);
d82a49ce 436
9eff0e5c
VY
437 fflush(fp);
438 return 0;
439}
440
c7c1a1ef 441static void print_vlan_flags(__u16 flags)
7abf5de6 442{
0f362674
SH
443 if (flags == 0)
444 return;
445
446 open_json_array(PRINT_JSON, "flags");
c7c1a1ef 447 if (flags & BRIDGE_VLAN_INFO_PVID)
0f362674 448 print_string(PRINT_ANY, NULL, " %s", "PVID");
7abf5de6 449
c7c1a1ef 450 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
0f362674
SH
451 print_string(PRINT_ANY, NULL, " %s", "Egress Untagged");
452 close_json_array(PRINT_JSON, NULL);
c7c1a1ef 453}
7abf5de6 454
c7c1a1ef
SH
455static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats)
456{
457 open_json_object(NULL);
458 print_hu(PRINT_ANY, "vid", " %hu", vstats->vid);
459
460 print_vlan_flags(vstats->flags);
461
462 print_lluint(PRINT_ANY, "rx_bytes",
463 "\n RX: %llu bytes",
464 vstats->rx_bytes);
465 print_lluint(PRINT_ANY, "rx_packets", " %llu packets\n",
466 vstats->rx_packets);
467 print_lluint(PRINT_ANY, "tx_bytes",
468 " TX: %llu bytes",
469 vstats->tx_bytes);
470 print_lluint(PRINT_ANY, "tx_packets", " %llu packets",
471 vstats->tx_packets);
472 close_json_object();
7abf5de6
NA
473}
474
c7c1a1ef 475static void print_vlan_stats_attr(struct rtattr *attr, int ifindex)
7abf5de6
NA
476{
477 struct rtattr *brtb[LINK_XSTATS_TYPE_MAX+1];
478 struct rtattr *i, *list;
0f362674 479 const char *ifname;
7abf5de6
NA
480 int rem;
481
482 parse_rtattr(brtb, LINK_XSTATS_TYPE_MAX, RTA_DATA(attr),
483 RTA_PAYLOAD(attr));
484 if (!brtb[LINK_XSTATS_TYPE_BRIDGE])
485 return;
486
487 list = brtb[LINK_XSTATS_TYPE_BRIDGE];
488 rem = RTA_PAYLOAD(list);
c7c1a1ef 489
0f362674
SH
490 ifname = ll_index_to_name(ifindex);
491 open_json_object(ifname);
c7c1a1ef 492
0f362674
SH
493 print_color_string(PRINT_FP, COLOR_IFNAME,
494 NULL, "%-16s", ifname);
c7c1a1ef 495
7abf5de6 496 for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
c7c1a1ef
SH
497 const struct bridge_vlan_xstats *vstats = RTA_DATA(i);
498
7abf5de6
NA
499 if (i->rta_type != BRIDGE_XSTATS_VLAN)
500 continue;
c7c1a1ef
SH
501
502 if (filter_vlan && filter_vlan != vstats->vid)
503 continue;
504
505 /* skip pure port entries, they'll be dumped via the slave stats call */
506 if ((vstats->flags & BRIDGE_VLAN_INFO_MASTER) &&
507 !(vstats->flags & BRIDGE_VLAN_INFO_BRENTRY))
508 continue;
509
510 print_one_vlan_stats(vstats);
7abf5de6 511 }
c7c1a1ef
SH
512 close_json_object();
513
7abf5de6
NA
514}
515
516static int print_vlan_stats(const struct sockaddr_nl *who,
517 struct nlmsghdr *n,
518 void *arg)
519{
520 struct if_stats_msg *ifsm = NLMSG_DATA(n);
521 struct rtattr *tb[IFLA_STATS_MAX+1];
522 int len = n->nlmsg_len;
523 FILE *fp = arg;
524
525 len -= NLMSG_LENGTH(sizeof(*ifsm));
526 if (len < 0) {
527 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
528 return -1;
529 }
530
531 if (filter_index && filter_index != ifsm->ifindex)
532 return 0;
533
534 parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len);
535
536 /* We have to check if any of the two attrs are usable */
537 if (tb[IFLA_STATS_LINK_XSTATS])
c7c1a1ef 538 print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS],
7abf5de6
NA
539 ifsm->ifindex);
540
541 if (tb[IFLA_STATS_LINK_XSTATS_SLAVE])
c7c1a1ef 542 print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS_SLAVE],
7abf5de6
NA
543 ifsm->ifindex);
544
545 fflush(fp);
546 return 0;
547}
548
9eff0e5c
VY
549static int vlan_show(int argc, char **argv)
550{
551 char *filter_dev = NULL;
8652eeb3 552 int ret = 0;
9eff0e5c
VY
553
554 while (argc > 0) {
555 if (strcmp(*argv, "dev") == 0) {
556 NEXT_ARG();
557 if (filter_dev)
558 duparg("dev", *argv);
559 filter_dev = *argv;
5a2d0201
NA
560 } else if (strcmp(*argv, "vid") == 0) {
561 NEXT_ARG();
562 if (filter_vlan)
563 duparg("vid", *argv);
564 filter_vlan = atoi(*argv);
9eff0e5c
VY
565 }
566 argc--; argv++;
567 }
568
569 if (filter_dev) {
7a14358b 570 filter_index = ll_name_to_index(filter_dev);
fe99adbc
SP
571 if (!filter_index)
572 return nodev(filter_dev);
9eff0e5c
VY
573 }
574
c7c1a1ef
SH
575 new_json_obj(json);
576
7abf5de6 577 if (!show_stats) {
31ae2912 578 if (rtnl_linkdump_req_filter(&rth, PF_BRIDGE,
7abf5de6 579 (compress_vlans ?
01842eb5
SH
580 RTEXT_FILTER_BRVLAN_COMPRESSED :
581 RTEXT_FILTER_BRVLAN)) < 0) {
7abf5de6
NA
582 perror("Cannont send dump request");
583 exit(1);
584 }
01842eb5 585
c7c1a1ef 586 if (!is_json_context()) {
8652eeb3
RP
587 if (show_vlan_tunnel_info)
588 printf("port\tvlan ids\ttunnel id\n");
589 else
590 printf("port\tvlan ids\n");
7abf5de6 591 }
9eff0e5c 592
8652eeb3
RP
593 if (show_vlan_tunnel_info)
594 ret = rtnl_dump_filter(&rth, print_vlan_tunnel,
595 stdout);
596 else
597 ret = rtnl_dump_filter(&rth, print_vlan, stdout);
8652eeb3 598 if (ret < 0) {
7abf5de6 599 fprintf(stderr, "Dump ternminated\n");
d82a49ce
RP
600 exit(1);
601 }
d82a49ce 602 } else {
7abf5de6 603 __u32 filt_mask;
d82a49ce 604
7abf5de6 605 filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS);
56eeeda9 606 if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, filt_mask) < 0) {
7abf5de6
NA
607 perror("Cannont send dump request");
608 exit(1);
609 }
610
c7c1a1ef
SH
611 if (!is_json_context())
612 printf("%-16s vlan id\n", "port");
613
7abf5de6
NA
614 if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) {
615 fprintf(stderr, "Dump terminated\n");
616 exit(1);
617 }
618
619 filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE);
56eeeda9 620 if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, filt_mask) < 0) {
7abf5de6
NA
621 perror("Cannont send slave dump request");
622 exit(1);
623 }
624
625 if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) {
626 fprintf(stderr, "Dump terminated\n");
627 exit(1);
628 }
9eff0e5c
VY
629 }
630
c7c1a1ef
SH
631 delete_json_obj();
632 fflush(stdout);
9eff0e5c
VY
633 return 0;
634}
635
0f362674 636void print_vlan_info(struct rtattr *tb, int ifindex)
b97c679c
RM
637{
638 struct rtattr *i, *list = tb;
639 int rem = RTA_PAYLOAD(list);
640 __u16 last_vid_start = 0;
b97c679c 641
0f362674 642 open_vlan_port(ifindex);
b97c679c
RM
643
644 for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
645 struct bridge_vlan_info *vinfo;
646 int vcheck_ret;
647
648 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
649 continue;
650
651 vinfo = RTA_DATA(i);
652
653 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
654 last_vid_start = vinfo->vid;
8652eeb3 655 vcheck_ret = filter_vlan_check(vinfo->vid, vinfo->flags);
b97c679c
RM
656 if (vcheck_ret == -1)
657 break;
658 else if (vcheck_ret == 0)
659 continue;
660
c7c1a1ef
SH
661 open_json_object(NULL);
662 print_range("vlan", last_vid_start, vinfo->vid);
b97c679c 663
c7c1a1ef
SH
664 print_vlan_flags(vinfo->flags);
665 close_json_object();
0f362674 666 print_string(PRINT_FP, NULL, "%s", _SL_);
b97c679c 667 }
0f362674 668 close_vlan_port();
b97c679c
RM
669}
670
9eff0e5c
VY
671int do_vlan(int argc, char **argv)
672{
673 ll_init_map(&rth);
674
675 if (argc > 0) {
676 if (matches(*argv, "add") == 0)
677 return vlan_modify(RTM_SETLINK, argc-1, argv+1);
678 if (matches(*argv, "delete") == 0)
679 return vlan_modify(RTM_DELLINK, argc-1, argv+1);
680 if (matches(*argv, "show") == 0 ||
681 matches(*argv, "lst") == 0 ||
682 matches(*argv, "list") == 0)
683 return vlan_show(argc-1, argv+1);
8652eeb3
RP
684 if (matches(*argv, "tunnelshow") == 0) {
685 show_vlan_tunnel_info = 1;
686 return vlan_show(argc-1, argv+1);
687 }
9eff0e5c
VY
688 if (matches(*argv, "help") == 0)
689 usage();
7abf5de6 690 } else {
9eff0e5c 691 return vlan_show(0, NULL);
7abf5de6 692 }
9eff0e5c 693
296cee6f 694 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv);
9eff0e5c
VY
695 exit(-1);
696}