]>
Commit | Line | Data |
---|---|---|
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 | 18 | static unsigned int filter_index, filter_vlan; |
8652eeb3 | 19 | static int show_vlan_tunnel_info = 0; |
9eff0e5c VY |
20 | |
21 | static 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 |
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 | ||
9eff0e5c VY |
125 | static 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 | 242 | static 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 | 255 | static 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 | ||
263 | static void close_vlan_port(void) | |
264 | { | |
265 | close_json_array(PRINT_JSON, NULL); | |
266 | close_json_object(); | |
d82a49ce RP |
267 | } |
268 | ||
c7c1a1ef | 269 | static 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 |
281 | static 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 | ||
cd554f2c | 350 | static int print_vlan_tunnel(struct nlmsghdr *n, void *arg) |
8652eeb3 RP |
351 | { |
352 | struct ifinfomsg *ifm = NLMSG_DATA(n); | |
353 | struct rtattr *tb[IFLA_MAX+1]; | |
354 | int len = n->nlmsg_len; | |
355 | FILE *fp = arg; | |
356 | ||
357 | if (n->nlmsg_type != RTM_NEWLINK) { | |
358 | fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n", | |
359 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
360 | return 0; | |
361 | } | |
362 | ||
363 | len -= NLMSG_LENGTH(sizeof(*ifm)); | |
364 | if (len < 0) { | |
365 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
366 | return -1; | |
367 | } | |
368 | ||
369 | if (ifm->ifi_family != AF_BRIDGE) | |
370 | return 0; | |
371 | ||
372 | if (filter_index && filter_index != ifm->ifi_index) | |
373 | return 0; | |
374 | ||
375 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len); | |
376 | ||
377 | /* if AF_SPEC isn't there, vlan table is not preset for this port */ | |
378 | if (!tb[IFLA_AF_SPEC]) { | |
c7c1a1ef SH |
379 | if (!filter_vlan && !is_json_context()) { |
380 | color_fprintf(fp, COLOR_IFNAME, "%s", | |
381 | ll_index_to_name(ifm->ifi_index)); | |
382 | fprintf(fp, "\tNone\n"); | |
383 | } | |
8652eeb3 RP |
384 | return 0; |
385 | } | |
386 | ||
387 | print_vlan_tunnel_info(fp, tb[IFLA_AF_SPEC], ifm->ifi_index); | |
388 | ||
389 | fflush(fp); | |
390 | return 0; | |
391 | } | |
392 | ||
cd554f2c | 393 | static int print_vlan(struct nlmsghdr *n, void *arg) |
9eff0e5c VY |
394 | { |
395 | FILE *fp = arg; | |
396 | struct ifinfomsg *ifm = NLMSG_DATA(n); | |
397 | int len = n->nlmsg_len; | |
df4b043f | 398 | struct rtattr *tb[IFLA_MAX+1]; |
9eff0e5c VY |
399 | |
400 | if (n->nlmsg_type != RTM_NEWLINK) { | |
401 | fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n", | |
402 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
403 | return 0; | |
404 | } | |
405 | ||
406 | len -= NLMSG_LENGTH(sizeof(*ifm)); | |
407 | if (len < 0) { | |
408 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
409 | return -1; | |
410 | } | |
411 | ||
412 | if (ifm->ifi_family != AF_BRIDGE) | |
413 | return 0; | |
414 | ||
415 | if (filter_index && filter_index != ifm->ifi_index) | |
416 | return 0; | |
417 | ||
418 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len); | |
419 | ||
420 | /* if AF_SPEC isn't there, vlan table is not preset for this port */ | |
421 | if (!tb[IFLA_AF_SPEC]) { | |
c7c1a1ef SH |
422 | if (!filter_vlan && !is_json_context()) { |
423 | color_fprintf(fp, COLOR_IFNAME, "%s", | |
424 | ll_index_to_name(ifm->ifi_index)); | |
425 | fprintf(fp, "\tNone\n"); | |
426 | } | |
9eff0e5c | 427 | return 0; |
9eff0e5c | 428 | } |
bcddcddd | 429 | |
0f362674 SH |
430 | print_vlan_info(tb[IFLA_AF_SPEC], ifm->ifi_index); |
431 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
d82a49ce | 432 | |
9eff0e5c VY |
433 | fflush(fp); |
434 | return 0; | |
435 | } | |
436 | ||
c7c1a1ef | 437 | static void print_vlan_flags(__u16 flags) |
7abf5de6 | 438 | { |
0f362674 SH |
439 | if (flags == 0) |
440 | return; | |
441 | ||
442 | open_json_array(PRINT_JSON, "flags"); | |
c7c1a1ef | 443 | if (flags & BRIDGE_VLAN_INFO_PVID) |
0f362674 | 444 | print_string(PRINT_ANY, NULL, " %s", "PVID"); |
7abf5de6 | 445 | |
c7c1a1ef | 446 | if (flags & BRIDGE_VLAN_INFO_UNTAGGED) |
0f362674 SH |
447 | print_string(PRINT_ANY, NULL, " %s", "Egress Untagged"); |
448 | close_json_array(PRINT_JSON, NULL); | |
c7c1a1ef | 449 | } |
7abf5de6 | 450 | |
c7c1a1ef SH |
451 | static void print_one_vlan_stats(const struct bridge_vlan_xstats *vstats) |
452 | { | |
453 | open_json_object(NULL); | |
454 | print_hu(PRINT_ANY, "vid", " %hu", vstats->vid); | |
455 | ||
456 | print_vlan_flags(vstats->flags); | |
457 | ||
458 | print_lluint(PRINT_ANY, "rx_bytes", | |
459 | "\n RX: %llu bytes", | |
460 | vstats->rx_bytes); | |
461 | print_lluint(PRINT_ANY, "rx_packets", " %llu packets\n", | |
462 | vstats->rx_packets); | |
463 | print_lluint(PRINT_ANY, "tx_bytes", | |
464 | " TX: %llu bytes", | |
465 | vstats->tx_bytes); | |
466 | print_lluint(PRINT_ANY, "tx_packets", " %llu packets", | |
467 | vstats->tx_packets); | |
468 | close_json_object(); | |
7abf5de6 NA |
469 | } |
470 | ||
c7c1a1ef | 471 | static void print_vlan_stats_attr(struct rtattr *attr, int ifindex) |
7abf5de6 NA |
472 | { |
473 | struct rtattr *brtb[LINK_XSTATS_TYPE_MAX+1]; | |
474 | struct rtattr *i, *list; | |
0f362674 | 475 | const char *ifname; |
7abf5de6 NA |
476 | int rem; |
477 | ||
478 | parse_rtattr(brtb, LINK_XSTATS_TYPE_MAX, RTA_DATA(attr), | |
479 | RTA_PAYLOAD(attr)); | |
480 | if (!brtb[LINK_XSTATS_TYPE_BRIDGE]) | |
481 | return; | |
482 | ||
483 | list = brtb[LINK_XSTATS_TYPE_BRIDGE]; | |
484 | rem = RTA_PAYLOAD(list); | |
c7c1a1ef | 485 | |
0f362674 | 486 | ifname = ll_index_to_name(ifindex); |
45fca4ed | 487 | open_vlan_port(ifindex); |
c7c1a1ef | 488 | |
0f362674 SH |
489 | print_color_string(PRINT_FP, COLOR_IFNAME, |
490 | NULL, "%-16s", ifname); | |
c7c1a1ef | 491 | |
7abf5de6 | 492 | for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { |
c7c1a1ef SH |
493 | const struct bridge_vlan_xstats *vstats = RTA_DATA(i); |
494 | ||
7abf5de6 NA |
495 | if (i->rta_type != BRIDGE_XSTATS_VLAN) |
496 | continue; | |
c7c1a1ef SH |
497 | |
498 | if (filter_vlan && filter_vlan != vstats->vid) | |
499 | continue; | |
500 | ||
501 | /* skip pure port entries, they'll be dumped via the slave stats call */ | |
502 | if ((vstats->flags & BRIDGE_VLAN_INFO_MASTER) && | |
503 | !(vstats->flags & BRIDGE_VLAN_INFO_BRENTRY)) | |
504 | continue; | |
505 | ||
506 | print_one_vlan_stats(vstats); | |
7abf5de6 | 507 | } |
45fca4ed | 508 | close_vlan_port(); |
7abf5de6 NA |
509 | } |
510 | ||
cd554f2c | 511 | static int print_vlan_stats(struct nlmsghdr *n, void *arg) |
7abf5de6 NA |
512 | { |
513 | struct if_stats_msg *ifsm = NLMSG_DATA(n); | |
514 | struct rtattr *tb[IFLA_STATS_MAX+1]; | |
515 | int len = n->nlmsg_len; | |
516 | FILE *fp = arg; | |
517 | ||
518 | len -= NLMSG_LENGTH(sizeof(*ifsm)); | |
519 | if (len < 0) { | |
520 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
521 | return -1; | |
522 | } | |
523 | ||
524 | if (filter_index && filter_index != ifsm->ifindex) | |
525 | return 0; | |
526 | ||
527 | parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len); | |
528 | ||
529 | /* We have to check if any of the two attrs are usable */ | |
530 | if (tb[IFLA_STATS_LINK_XSTATS]) | |
c7c1a1ef | 531 | print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS], |
7abf5de6 NA |
532 | ifsm->ifindex); |
533 | ||
534 | if (tb[IFLA_STATS_LINK_XSTATS_SLAVE]) | |
c7c1a1ef | 535 | print_vlan_stats_attr(tb[IFLA_STATS_LINK_XSTATS_SLAVE], |
7abf5de6 NA |
536 | ifsm->ifindex); |
537 | ||
538 | fflush(fp); | |
539 | return 0; | |
540 | } | |
541 | ||
9eff0e5c VY |
542 | static int vlan_show(int argc, char **argv) |
543 | { | |
544 | char *filter_dev = NULL; | |
8652eeb3 | 545 | int ret = 0; |
9eff0e5c VY |
546 | |
547 | while (argc > 0) { | |
548 | if (strcmp(*argv, "dev") == 0) { | |
549 | NEXT_ARG(); | |
550 | if (filter_dev) | |
551 | duparg("dev", *argv); | |
552 | filter_dev = *argv; | |
5a2d0201 NA |
553 | } else if (strcmp(*argv, "vid") == 0) { |
554 | NEXT_ARG(); | |
555 | if (filter_vlan) | |
556 | duparg("vid", *argv); | |
557 | filter_vlan = atoi(*argv); | |
9eff0e5c VY |
558 | } |
559 | argc--; argv++; | |
560 | } | |
561 | ||
562 | if (filter_dev) { | |
7a14358b | 563 | filter_index = ll_name_to_index(filter_dev); |
fe99adbc SP |
564 | if (!filter_index) |
565 | return nodev(filter_dev); | |
9eff0e5c VY |
566 | } |
567 | ||
c7c1a1ef SH |
568 | new_json_obj(json); |
569 | ||
7abf5de6 | 570 | if (!show_stats) { |
31ae2912 | 571 | if (rtnl_linkdump_req_filter(&rth, PF_BRIDGE, |
7abf5de6 | 572 | (compress_vlans ? |
01842eb5 SH |
573 | RTEXT_FILTER_BRVLAN_COMPRESSED : |
574 | RTEXT_FILTER_BRVLAN)) < 0) { | |
7abf5de6 NA |
575 | perror("Cannont send dump request"); |
576 | exit(1); | |
577 | } | |
01842eb5 | 578 | |
c7c1a1ef | 579 | if (!is_json_context()) { |
8652eeb3 RP |
580 | if (show_vlan_tunnel_info) |
581 | printf("port\tvlan ids\ttunnel id\n"); | |
582 | else | |
583 | printf("port\tvlan ids\n"); | |
7abf5de6 | 584 | } |
9eff0e5c | 585 | |
8652eeb3 RP |
586 | if (show_vlan_tunnel_info) |
587 | ret = rtnl_dump_filter(&rth, print_vlan_tunnel, | |
588 | stdout); | |
589 | else | |
590 | ret = rtnl_dump_filter(&rth, print_vlan, stdout); | |
8652eeb3 | 591 | if (ret < 0) { |
7abf5de6 | 592 | fprintf(stderr, "Dump ternminated\n"); |
d82a49ce RP |
593 | exit(1); |
594 | } | |
d82a49ce | 595 | } else { |
7abf5de6 | 596 | __u32 filt_mask; |
d82a49ce | 597 | |
7abf5de6 | 598 | filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS); |
56eeeda9 | 599 | if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, filt_mask) < 0) { |
7abf5de6 NA |
600 | perror("Cannont send dump request"); |
601 | exit(1); | |
602 | } | |
603 | ||
c7c1a1ef SH |
604 | if (!is_json_context()) |
605 | printf("%-16s vlan id\n", "port"); | |
606 | ||
7abf5de6 NA |
607 | if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { |
608 | fprintf(stderr, "Dump terminated\n"); | |
609 | exit(1); | |
610 | } | |
611 | ||
612 | filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE); | |
56eeeda9 | 613 | if (rtnl_statsdump_req_filter(&rth, AF_UNSPEC, filt_mask) < 0) { |
7abf5de6 NA |
614 | perror("Cannont send slave dump request"); |
615 | exit(1); | |
616 | } | |
617 | ||
618 | if (rtnl_dump_filter(&rth, print_vlan_stats, stdout) < 0) { | |
619 | fprintf(stderr, "Dump terminated\n"); | |
620 | exit(1); | |
621 | } | |
9eff0e5c VY |
622 | } |
623 | ||
c7c1a1ef SH |
624 | delete_json_obj(); |
625 | fflush(stdout); | |
9eff0e5c VY |
626 | return 0; |
627 | } | |
628 | ||
0f362674 | 629 | void print_vlan_info(struct rtattr *tb, int ifindex) |
b97c679c RM |
630 | { |
631 | struct rtattr *i, *list = tb; | |
632 | int rem = RTA_PAYLOAD(list); | |
633 | __u16 last_vid_start = 0; | |
b97c679c | 634 | |
0f362674 | 635 | open_vlan_port(ifindex); |
b97c679c RM |
636 | |
637 | for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
638 | struct bridge_vlan_info *vinfo; | |
639 | int vcheck_ret; | |
640 | ||
641 | if (i->rta_type != IFLA_BRIDGE_VLAN_INFO) | |
642 | continue; | |
643 | ||
644 | vinfo = RTA_DATA(i); | |
645 | ||
646 | if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END)) | |
647 | last_vid_start = vinfo->vid; | |
8652eeb3 | 648 | vcheck_ret = filter_vlan_check(vinfo->vid, vinfo->flags); |
b97c679c RM |
649 | if (vcheck_ret == -1) |
650 | break; | |
651 | else if (vcheck_ret == 0) | |
652 | continue; | |
653 | ||
c7c1a1ef SH |
654 | open_json_object(NULL); |
655 | print_range("vlan", last_vid_start, vinfo->vid); | |
b97c679c | 656 | |
c7c1a1ef SH |
657 | print_vlan_flags(vinfo->flags); |
658 | close_json_object(); | |
0f362674 | 659 | print_string(PRINT_FP, NULL, "%s", _SL_); |
b97c679c | 660 | } |
0f362674 | 661 | close_vlan_port(); |
b97c679c RM |
662 | } |
663 | ||
9eff0e5c VY |
664 | int do_vlan(int argc, char **argv) |
665 | { | |
666 | ll_init_map(&rth); | |
667 | ||
668 | if (argc > 0) { | |
669 | if (matches(*argv, "add") == 0) | |
670 | return vlan_modify(RTM_SETLINK, argc-1, argv+1); | |
671 | if (matches(*argv, "delete") == 0) | |
672 | return vlan_modify(RTM_DELLINK, argc-1, argv+1); | |
673 | if (matches(*argv, "show") == 0 || | |
674 | matches(*argv, "lst") == 0 || | |
675 | matches(*argv, "list") == 0) | |
676 | return vlan_show(argc-1, argv+1); | |
8652eeb3 RP |
677 | if (matches(*argv, "tunnelshow") == 0) { |
678 | show_vlan_tunnel_info = 1; | |
679 | return vlan_show(argc-1, argv+1); | |
680 | } | |
9eff0e5c VY |
681 | if (matches(*argv, "help") == 0) |
682 | usage(); | |
7abf5de6 | 683 | } else { |
9eff0e5c | 684 | return vlan_show(0, NULL); |
7abf5de6 | 685 | } |
9eff0e5c | 686 | |
296cee6f | 687 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv); |
9eff0e5c VY |
688 | exit(-1); |
689 | } |