]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: GPL-2.0 */ | |
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 | ||
13 | #include "json_print.h" | |
14 | #include "libnetlink.h" | |
15 | #include "br_common.h" | |
16 | #include "utils.h" | |
17 | ||
18 | static unsigned int filter_index, filter_vlan; | |
19 | static int show_vlan_tunnel_info = 0; | |
20 | ||
21 | static void usage(void) | |
22 | { | |
23 | fprintf(stderr, | |
24 | "Usage: bridge vlan { add | del } vid VLAN_ID dev DEV [ tunnel_info id TUNNEL_ID ]\n" | |
25 | " [ pvid ] [ untagged ]\n" | |
26 | " [ self ] [ master ]\n" | |
27 | " bridge vlan { show } [ dev DEV ] [ vid VLAN_ID ]\n" | |
28 | " bridge vlan { tunnelshow } [ dev DEV ] [ vid VLAN_ID ]\n"); | |
29 | exit(-1); | |
30 | } | |
31 | ||
32 | static 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 | ||
67 | static 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 | ||
82 | static 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 | ||
99 | static 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 | ||
125 | static int vlan_modify(int cmd, int argc, char **argv) | |
126 | { | |
127 | struct { | |
128 | struct nlmsghdr n; | |
129 | struct ifinfomsg ifm; | |
130 | char buf[1024]; | |
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 | }; | |
137 | char *d = NULL; | |
138 | short vid = -1; | |
139 | short vid_end = -1; | |
140 | struct rtattr *afspec; | |
141 | struct bridge_vlan_info vinfo = {}; | |
142 | bool tunnel_info_set = false; | |
143 | unsigned short flags = 0; | |
144 | __u32 tun_id_start = 0; | |
145 | __u32 tun_id_end = 0; | |
146 | ||
147 | while (argc > 0) { | |
148 | if (strcmp(*argv, "dev") == 0) { | |
149 | NEXT_ARG(); | |
150 | d = *argv; | |
151 | } else if (strcmp(*argv, "vid") == 0) { | |
152 | char *p; | |
153 | ||
154 | NEXT_ARG(); | |
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 | } | |
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; | |
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; | |
179 | } else { | |
180 | if (matches(*argv, "help") == 0) | |
181 | NEXT_ARG(); | |
182 | } | |
183 | argc--; argv++; | |
184 | } | |
185 | ||
186 | if (d == NULL || vid == -1) { | |
187 | fprintf(stderr, "Device and VLAN ID are required arguments.\n"); | |
188 | return -1; | |
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 | ||
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 | } | |
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 | ||
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); | |
226 | ||
227 | addattr_nest_end(&req.n, afspec); | |
228 | ||
229 | if (rtnl_talk(&rth, &req.n, NULL) < 0) | |
230 | return -1; | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
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 | */ | |
242 | static int filter_vlan_check(__u16 vid, __u16 flags) | |
243 | { | |
244 | /* if we're filtering we should stop on the first greater entry */ | |
245 | if (filter_vlan && vid > filter_vlan && | |
246 | !(flags & BRIDGE_VLAN_INFO_RANGE_END)) | |
247 | return -1; | |
248 | if ((flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) || | |
249 | vid < filter_vlan) | |
250 | return 0; | |
251 | ||
252 | return 1; | |
253 | } | |
254 | ||
255 | static void open_vlan_port(int ifi_index) | |
256 | { | |
257 | open_json_object(NULL); | |
258 | print_string(PRINT_ANY, "ifname", "%s", | |
259 | ll_index_to_name(ifi_index)); | |
260 | open_json_array(PRINT_JSON, "vlans"); | |
261 | } | |
262 | ||
263 | static void close_vlan_port(void) | |
264 | { | |
265 | close_json_array(PRINT_JSON, NULL); | |
266 | close_json_object(); | |
267 | } | |
268 | ||
269 | static void print_range(const char *name, __u16 start, __u16 id) | |
270 | { | |
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 | ||
279 | } | |
280 | ||
281 | static void print_vlan_tunnel_info(FILE *fp, struct rtattr *tb, int ifindex) | |
282 | { | |
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 | ||
288 | if (!filter_vlan) | |
289 | open_vlan_port(ifindex); | |
290 | ||
291 | open_json_array(PRINT_JSON, "tunnel"); | |
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 | } | |
323 | ||
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 | ||
333 | if (filter_vlan) | |
334 | open_vlan_port(ifindex); | |
335 | ||
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(); | |
340 | ||
341 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
342 | if (filter_vlan) | |
343 | close_vlan_port(); | |
344 | } | |
345 | ||
346 | if (!filter_vlan) | |
347 | close_vlan_port(); | |
348 | } | |
349 | ||
350 | static 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]) { | |
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 | } | |
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 | ||
395 | static 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; | |
402 | struct rtattr *tb[IFLA_MAX+1]; | |
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]) { | |
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 | } | |
431 | return 0; | |
432 | } | |
433 | ||
434 | print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index); | |
435 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
436 | ||
437 | fflush(fp); | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static void print_vlan_flags(__u16 flags) | |
442 | { | |
443 | if (flags == 0) | |
444 | return; | |
445 | ||
446 | open_json_array(PRINT_JSON, "flags"); | |
447 | if (flags & BRIDGE_VLAN_INFO_PVID) | |
448 | print_string(PRINT_ANY, NULL, " %s", "PVID"); | |
449 | ||
450 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) | |
451 | print_string(PRINT_ANY, NULL, " %s", "Egress Untagged"); | |
452 | close_json_array(PRINT_JSON, NULL); | |
453 | } | |
454 | ||
455 | static 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(); | |
473 | } | |
474 | ||
475 | static void print_vlan_stats_attr(struct rtattr *attr, int ifindex) | |
476 | { | |
477 | struct rtattr *brtb[LINK_XSTATS_TYPE_MAX+1]; | |
478 | struct rtattr *i, *list; | |
479 | const char *ifname; | |
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); | |
489 | ||
490 | ifname = ll_index_to_name(ifindex); | |
491 | open_json_object(ifname); | |
492 | ||
493 | print_color_string(PRINT_FP, COLOR_IFNAME, | |
494 | NULL, "%-16s", ifname); | |
495 | ||
496 | for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
497 | const struct bridge_vlan_xstats *vstats = RTA_DATA(i); | |
498 | ||
499 | if (i->rta_type != BRIDGE_XSTATS_VLAN) | |
500 | continue; | |
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); | |
511 | } | |
512 | close_json_object(); | |
513 | ||
514 | } | |
515 | ||
516 | static 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]) | |
538 | print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS], | |
539 | ifsm->ifindex); | |
540 | ||
541 | if (tb[IFLA_STATS_LINK_XSTATS_SLAVE]) | |
542 | print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS_SLAVE], | |
543 | ifsm->ifindex); | |
544 | ||
545 | fflush(fp); | |
546 | return 0; | |
547 | } | |
548 | ||
549 | static int vlan_show(int argc, char **argv) | |
550 | { | |
551 | char *filter_dev = NULL; | |
552 | int ret = 0; | |
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; | |
560 | } else if (strcmp(*argv, "vid") == 0) { | |
561 | NEXT_ARG(); | |
562 | if (filter_vlan) | |
563 | duparg("vid", *argv); | |
564 | filter_vlan = atoi(*argv); | |
565 | } | |
566 | argc--; argv++; | |
567 | } | |
568 | ||
569 | if (filter_dev) { | |
570 | filter_index = ll_name_to_index(filter_dev); | |
571 | if (!filter_index) | |
572 | return nodev(filter_dev); | |
573 | } | |
574 | ||
575 | new_json_obj(json); | |
576 | ||
577 | if (!show_stats) { | |
578 | if (rtnl_linkdump_req_filter(&rth, PF_BRIDGE, | |
579 | (compress_vlans ? | |
580 | RTEXT_FILTER_BRVLAN_COMPRESSED : | |
581 | RTEXT_FILTER_BRVLAN)) < 0) { | |
582 | perror("Cannont send dump request"); | |
583 | exit(1); | |
584 | } | |
585 | ||
586 | if (!is_json_context()) { | |
587 | if (show_vlan_tunnel_info) | |
588 | printf("port\tvlan ids\ttunnel id\n"); | |
589 | else | |
590 | printf("port\tvlan ids\n"); | |
591 | } | |
592 | ||
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); | |
598 | if (ret < 0) { | |
599 | fprintf(stderr, "Dump ternminated\n"); | |
600 | exit(1); | |
601 | } | |
602 | } else { | |
603 | __u32 filt_mask; | |
604 | ||
605 | filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS); | |
606 | if (rtnl_wilddump_stats_req_filter(&rth, AF_UNSPEC, | |
607 | RTM_GETSTATS, | |
608 | filt_mask) < 0) { | |
609 | perror("Cannont send dump request"); | |
610 | exit(1); | |
611 | } | |
612 | ||
613 | if (!is_json_context()) | |
614 | printf("%-16s vlan id\n", "port"); | |
615 | ||
616 | if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { | |
617 | fprintf(stderr, "Dump terminated\n"); | |
618 | exit(1); | |
619 | } | |
620 | ||
621 | filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE); | |
622 | if (rtnl_wilddump_stats_req_filter(&rth, AF_UNSPEC, | |
623 | RTM_GETSTATS, | |
624 | filt_mask) < 0) { | |
625 | perror("Cannont send slave dump request"); | |
626 | exit(1); | |
627 | } | |
628 | ||
629 | if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { | |
630 | fprintf(stderr, "Dump terminated\n"); | |
631 | exit(1); | |
632 | } | |
633 | } | |
634 | ||
635 | delete_json_obj(); | |
636 | fflush(stdout); | |
637 | return 0; | |
638 | } | |
639 | ||
640 | void print_vlan_info(struct rtattr *tb, int ifindex) | |
641 | { | |
642 | struct rtattr *i, *list = tb; | |
643 | int rem = RTA_PAYLOAD(list); | |
644 | __u16 last_vid_start = 0; | |
645 | ||
646 | open_vlan_port(ifindex); | |
647 | ||
648 | for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
649 | struct bridge_vlan_info *vinfo; | |
650 | int vcheck_ret; | |
651 | ||
652 | if (i->rta_type != IFLA_BRIDGE_VLAN_INFO) | |
653 | continue; | |
654 | ||
655 | vinfo = RTA_DATA(i); | |
656 | ||
657 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) | |
658 | last_vid_start = vinfo->vid; | |
659 | vcheck_ret = filter_vlan_check(vinfo->vid, vinfo->flags); | |
660 | if (vcheck_ret == -1) | |
661 | break; | |
662 | else if (vcheck_ret == 0) | |
663 | continue; | |
664 | ||
665 | open_json_object(NULL); | |
666 | print_range("vlan", last_vid_start, vinfo->vid); | |
667 | ||
668 | print_vlan_flags(vinfo->flags); | |
669 | close_json_object(); | |
670 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
671 | } | |
672 | close_vlan_port(); | |
673 | } | |
674 | ||
675 | int do_vlan(int argc, char **argv) | |
676 | { | |
677 | ll_init_map(&rth); | |
678 | ||
679 | if (argc > 0) { | |
680 | if (matches(*argv, "add") == 0) | |
681 | return vlan_modify(RTM_SETLINK, argc-1, argv+1); | |
682 | if (matches(*argv, "delete") == 0) | |
683 | return vlan_modify(RTM_DELLINK, argc-1, argv+1); | |
684 | if (matches(*argv, "show") == 0 || | |
685 | matches(*argv, "lst") == 0 || | |
686 | matches(*argv, "list") == 0) | |
687 | return vlan_show(argc-1, argv+1); | |
688 | if (matches(*argv, "tunnelshow") == 0) { | |
689 | show_vlan_tunnel_info = 1; | |
690 | return vlan_show(argc-1, argv+1); | |
691 | } | |
692 | if (matches(*argv, "help") == 0) | |
693 | usage(); | |
694 | } else { | |
695 | return vlan_show(0, NULL); | |
696 | } | |
697 | ||
698 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv); | |
699 | exit(-1); | |
700 | } |