]> git.proxmox.com Git - mirror_iproute2.git/blame - bridge/link.c
bridge: update man page for new color and json changes
[mirror_iproute2.git] / bridge / link.c
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
d04bc300
SH
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <time.h>
7#include <sys/socket.h>
8#include <sys/time.h>
9#include <netinet/in.h>
10#include <linux/if.h>
11#include <linux/if_bridge.h>
12#include <string.h>
64108901 13#include <stdbool.h>
d04bc300 14
f32e4977 15#include "json_print.h"
64108901 16#include "libnetlink.h"
d04bc300
SH
17#include "utils.h"
18#include "br_common.h"
19
9dca899b 20static unsigned int filter_index;
64108901 21
d04bc300
SH
22static const char *port_states[] = {
23 [BR_STATE_DISABLED] = "disabled",
24 [BR_STATE_LISTENING] = "listening",
25 [BR_STATE_LEARNING] = "learning",
26 [BR_STATE_FORWARDING] = "forwarding",
27 [BR_STATE_BLOCKING] = "blocking",
28};
29
f32e4977
SH
30static const char *hw_mode[] = {
31 "VEB", "VEPA"
32};
33
34static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
d04bc300 35{
f32e4977 36 open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
d04bc300 37 if (flags & IFF_UP && !(flags & IFF_RUNNING))
f32e4977
SH
38 print_string(PRINT_ANY, NULL,
39 flags ? "%s," : "%s", "NO-CARRIER");
d04bc300 40 flags &= ~IFF_RUNNING;
f32e4977
SH
41
42#define _PF(f) if (flags&IFF_##f) { \
43 flags &= ~IFF_##f ; \
44 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
d04bc300
SH
45 _PF(LOOPBACK);
46 _PF(BROADCAST);
47 _PF(POINTOPOINT);
48 _PF(MULTICAST);
49 _PF(NOARP);
50 _PF(ALLMULTI);
51 _PF(PROMISC);
52 _PF(MASTER);
53 _PF(SLAVE);
54 _PF(DEBUG);
55 _PF(DYNAMIC);
56 _PF(AUTOMEDIA);
57 _PF(PORTSEL);
58 _PF(NOTRAILERS);
59 _PF(UP);
60 _PF(LOWER_UP);
61 _PF(DORMANT);
62 _PF(ECHO);
63#undef _PF
df4b043f 64 if (flags)
f32e4977
SH
65 print_hex(PRINT_ANY, NULL, "%x", flags);
66 if (mdown)
67 print_string(PRINT_ANY, NULL, ",%s", "M-DOWN");
68 close_json_array(PRINT_ANY, "> ");
d04bc300
SH
69}
70
f32e4977
SH
71static void print_portstate(__u8 state)
72{
73 if (state <= BR_STATE_BLOCKING)
74 print_string(PRINT_ANY, "state",
75 "state %s ", port_states[state]);
76 else
77 print_uint(PRINT_ANY, "state",
78 "state (%d) ", state);
79}
b1b7ce0f 80
f32e4977 81static void print_onoff(FILE *fp, const char *flag, __u8 val)
d04bc300 82{
f32e4977
SH
83 if (is_json_context())
84 print_bool(PRINT_JSON, flag, NULL, val);
d04bc300 85 else
f32e4977 86 fprintf(fp, "%s %s ", flag, val ? "on" : "off");
d04bc300
SH
87}
88
f32e4977 89static void print_hwmode(__u16 mode)
b1b7ce0f 90{
f32e4977
SH
91 if (mode >= ARRAY_SIZE(hw_mode))
92 print_0xhex(PRINT_ANY, "hwmode",
93 "hwmode %#hx ", mode);
b1b7ce0f 94 else
f32e4977
SH
95 print_string(PRINT_ANY, "hwmode",
96 "hwmode %s ", hw_mode[mode]);
b1b7ce0f
VY
97}
98
f32e4977 99static void print_protinfo(FILE *fp, struct rtattr *attr)
b1b7ce0f 100{
f32e4977
SH
101 if (attr->rta_type & NLA_F_NESTED) {
102 struct rtattr *prtb[IFLA_BRPORT_MAX + 1];
103
104 parse_rtattr_nested(prtb, IFLA_BRPORT_MAX, attr);
105
106 if (prtb[IFLA_BRPORT_STATE])
107 print_portstate(rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
108
109 if (prtb[IFLA_BRPORT_PRIORITY])
110 print_uint(PRINT_ANY, "priority",
111 "priority %u ",
112 rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
113
114 if (prtb[IFLA_BRPORT_COST])
115 print_uint(PRINT_ANY, "cost",
116 "cost %u ",
117 rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
118
119 if (!show_details)
120 return;
121
122 if (!is_json_context())
123 fprintf(fp, "%s ", _SL_);
124
125 if (prtb[IFLA_BRPORT_MODE])
126 print_onoff(fp, "hairpin",
127 rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
128 if (prtb[IFLA_BRPORT_GUARD])
129 print_onoff(fp, "guard",
130 rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
131 if (prtb[IFLA_BRPORT_PROTECT])
132 print_onoff(fp, "root_block",
133 rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
134 if (prtb[IFLA_BRPORT_FAST_LEAVE])
135 print_onoff(fp, "fastleave",
136 rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
137 if (prtb[IFLA_BRPORT_LEARNING])
138 print_onoff(fp, "learning",
139 rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING]));
140 if (prtb[IFLA_BRPORT_LEARNING_SYNC])
141 print_onoff(fp, "learning_sync",
142 rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC]));
143 if (prtb[IFLA_BRPORT_UNICAST_FLOOD])
144 print_onoff(fp, "flood",
145 rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD]));
146 if (prtb[IFLA_BRPORT_MCAST_FLOOD])
147 print_onoff(fp, "mcast_flood",
148 rta_getattr_u8(prtb[IFLA_BRPORT_MCAST_FLOOD]));
149 if (prtb[IFLA_BRPORT_NEIGH_SUPPRESS])
150 print_onoff(fp, "neigh_suppress",
151 rta_getattr_u8(prtb[IFLA_BRPORT_NEIGH_SUPPRESS]));
152 if (prtb[IFLA_BRPORT_VLAN_TUNNEL])
153 print_onoff(fp, "vlan_tunnel",
154 rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL]));
155 } else
156 print_portstate(rta_getattr_u8(attr));
b1b7ce0f
VY
157}
158
f32e4977
SH
159
160/*
161 * This is reported by HW devices that have some bridging
162 * capabilities.
163 */
164static void print_af_spec(FILE *fp, struct rtattr *attr)
b1b7ce0f 165{
f32e4977
SH
166 struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
167
168 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, attr);
169
170 if (aftb[IFLA_BRIDGE_MODE])
171 print_hwmode(rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
172
173 if (!show_details)
174 return;
175
176 if (aftb[IFLA_BRIDGE_VLAN_INFO])
177 print_vlan_info(fp, aftb[IFLA_BRIDGE_VLAN_INFO]);
b1b7ce0f
VY
178}
179
d04bc300
SH
180int print_linkinfo(const struct sockaddr_nl *who,
181 struct nlmsghdr *n, void *arg)
182{
183 FILE *fp = arg;
d04bc300 184 struct ifinfomsg *ifi = NLMSG_DATA(n);
df4b043f 185 struct rtattr *tb[IFLA_MAX+1];
f32e4977 186 unsigned int m_flag = 0;
fcac9665
SP
187 int len = n->nlmsg_len;
188 const char *name;
d04bc300
SH
189
190 len -= NLMSG_LENGTH(sizeof(*ifi));
191 if (len < 0) {
192 fprintf(stderr, "Message too short!\n");
193 return -1;
df4b043f 194 }
d04bc300
SH
195
196 if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
197 return 0;
198
64108901
VY
199 if (filter_index && filter_index != ifi->ifi_index)
200 return 0;
201
b1b7ce0f 202 parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
d04bc300 203
fcac9665
SP
204 name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
205 if (!name)
d04bc300 206 return -1;
d04bc300 207
f32e4977 208 open_json_object(NULL);
d04bc300 209 if (n->nlmsg_type == RTM_DELLINK)
f32e4977 210 print_bool(PRINT_ANY, "deleted", "Deleted ", true);
d04bc300 211
f32e4977
SH
212 print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index);
213 m_flag = print_name_and_link("%s: ", COLOR_IFNAME, name, tb);
214 print_link_flags(fp, ifi->ifi_flags, m_flag);
d04bc300
SH
215
216 if (tb[IFLA_MTU])
f32e4977
SH
217 print_int(PRINT_ANY,
218 "mtu", "mtu %u ",
219 rta_getattr_u32(tb[IFLA_MTU]));
d04bc300 220
45d3a6ef
SP
221 if (tb[IFLA_MASTER]) {
222 int master = rta_getattr_u32(tb[IFLA_MASTER]);
223
f32e4977
SH
224 print_string(PRINT_ANY, "master", "master %s ",
225 ll_index_to_name(master));
45d3a6ef 226 }
d04bc300 227
f32e4977
SH
228 if (tb[IFLA_PROTINFO])
229 print_protinfo(fp, tb[IFLA_PROTINFO]);
d04bc300 230
f32e4977
SH
231 if (tb[IFLA_AF_SPEC])
232 print_af_spec(fp, tb[IFLA_AF_SPEC]);
d04bc300 233
f32e4977
SH
234 print_string(PRINT_FP, NULL, "%s", "\n");
235 close_json_object();
d04bc300
SH
236 fflush(fp);
237 return 0;
238}
64108901
VY
239
240static void usage(void)
241{
242 fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
243 fprintf(stderr, " [ guard {on | off} ]\n");
df4b043f 244 fprintf(stderr, " [ hairpin {on | off} ]\n");
64108901
VY
245 fprintf(stderr, " [ fastleave {on | off} ]\n");
246 fprintf(stderr, " [ root_block {on | off} ]\n");
f0f4ab60 247 fprintf(stderr, " [ learning {on | off} ]\n");
674bb438 248 fprintf(stderr, " [ learning_sync {on | off} ]\n");
f0f4ab60 249 fprintf(stderr, " [ flood {on | off} ]\n");
9208b4e7 250 fprintf(stderr, " [ mcast_flood {on | off} ]\n");
8cfde5c9
RP
251 fprintf(stderr, " [ neigh_suppress {on | off} ]\n");
252 fprintf(stderr, " [ vlan_tunnel {on | off} ]\n");
64108901 253 fprintf(stderr, " [ hwmode {vepa | veb} ]\n");
22a98f51 254 fprintf(stderr, " [ self ] [ master ]\n");
64108901
VY
255 fprintf(stderr, " bridge link show [dev DEV]\n");
256 exit(-1);
257}
258
259static bool on_off(char *arg, __s8 *attr, char *val)
260{
261 if (strcmp(val, "on") == 0)
262 *attr = 1;
263 else if (strcmp(val, "off") == 0)
264 *attr = 0;
265 else {
266 fprintf(stderr,
267 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
268 arg);
269 return false;
270 }
271
272 return true;
273}
274
275static int brlink_modify(int argc, char **argv)
276{
277 struct {
278 struct nlmsghdr n;
279 struct ifinfomsg ifm;
280 char buf[512];
d17b136f
PS
281 } req = {
282 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
283 .n.nlmsg_flags = NLM_F_REQUEST,
284 .n.nlmsg_type = RTM_SETLINK,
285 .ifm.ifi_family = PF_BRIDGE,
286 };
64108901 287 char *d = NULL;
41973a47 288 __s8 neigh_suppress = -1;
f0f4ab60 289 __s8 learning = -1;
674bb438 290 __s8 learning_sync = -1;
f0f4ab60 291 __s8 flood = -1;
8cfde5c9 292 __s8 vlan_tunnel = -1;
9208b4e7 293 __s8 mcast_flood = -1;
64108901
VY
294 __s8 hairpin = -1;
295 __s8 bpdu_guard = -1;
296 __s8 fast_leave = -1;
b1b7ce0f 297 __s8 root_block = -1;
64108901
VY
298 __u32 cost = 0;
299 __s16 priority = -1;
300 __s8 state = -1;
301 __s16 mode = -1;
6fdb4658 302 __u16 flags = 0;
64108901
VY
303 struct rtattr *nest;
304
64108901
VY
305 while (argc > 0) {
306 if (strcmp(*argv, "dev") == 0) {
307 NEXT_ARG();
308 d = *argv;
309 } else if (strcmp(*argv, "guard") == 0) {
310 NEXT_ARG();
311 if (!on_off("guard", &bpdu_guard, *argv))
42ecedd4 312 return -1;
64108901
VY
313 } else if (strcmp(*argv, "hairpin") == 0) {
314 NEXT_ARG();
315 if (!on_off("hairping", &hairpin, *argv))
42ecedd4 316 return -1;
64108901
VY
317 } else if (strcmp(*argv, "fastleave") == 0) {
318 NEXT_ARG();
319 if (!on_off("fastleave", &fast_leave, *argv))
42ecedd4 320 return -1;
b1b7ce0f
VY
321 } else if (strcmp(*argv, "root_block") == 0) {
322 NEXT_ARG();
323 if (!on_off("root_block", &root_block, *argv))
42ecedd4 324 return -1;
f0f4ab60
VY
325 } else if (strcmp(*argv, "learning") == 0) {
326 NEXT_ARG();
327 if (!on_off("learning", &learning, *argv))
42ecedd4 328 return -1;
674bb438
SF
329 } else if (strcmp(*argv, "learning_sync") == 0) {
330 NEXT_ARG();
331 if (!on_off("learning_sync", &learning_sync, *argv))
42ecedd4 332 return -1;
f0f4ab60
VY
333 } else if (strcmp(*argv, "flood") == 0) {
334 NEXT_ARG();
335 if (!on_off("flood", &flood, *argv))
42ecedd4 336 return -1;
9208b4e7
NA
337 } else if (strcmp(*argv, "mcast_flood") == 0) {
338 NEXT_ARG();
339 if (!on_off("mcast_flood", &mcast_flood, *argv))
340 return -1;
a40d0827 341 } else if (strcmp(*argv, "cost") == 0) {
64108901
VY
342 NEXT_ARG();
343 cost = atoi(*argv);
a40d0827 344 } else if (strcmp(*argv, "priority") == 0) {
64108901
VY
345 NEXT_ARG();
346 priority = atoi(*argv);
a40d0827 347 } else if (strcmp(*argv, "state") == 0) {
64108901 348 NEXT_ARG();
6b8c871d 349 char *endptr;
62000e51 350 size_t nstates = ARRAY_SIZE(port_states);
df4b043f 351
6b8c871d
AP
352 state = strtol(*argv, &endptr, 10);
353 if (!(**argv != '\0' && *endptr == '\0')) {
354 for (state = 0; state < nstates; state++)
355 if (strcmp(port_states[state], *argv) == 0)
356 break;
357 if (state == nstates) {
358 fprintf(stderr,
359 "Error: invalid STP port state\n");
42ecedd4 360 return -1;
6b8c871d
AP
361 }
362 }
a40d0827 363 } else if (strcmp(*argv, "hwmode") == 0) {
64108901 364 NEXT_ARG();
a40d0827 365 flags = BRIDGE_FLAGS_SELF;
64108901
VY
366 if (strcmp(*argv, "vepa") == 0)
367 mode = BRIDGE_MODE_VEPA;
368 else if (strcmp(*argv, "veb") == 0)
369 mode = BRIDGE_MODE_VEB;
370 else {
371 fprintf(stderr,
df4b043f 372 "Mode argument must be \"vepa\" or \"veb\".\n");
42ecedd4 373 return -1;
64108901 374 }
6fdb4658 375 } else if (strcmp(*argv, "self") == 0) {
22a98f51
RP
376 flags |= BRIDGE_FLAGS_SELF;
377 } else if (strcmp(*argv, "master") == 0) {
378 flags |= BRIDGE_FLAGS_MASTER;
41973a47
RP
379 } else if (strcmp(*argv, "neigh_suppress") == 0) {
380 NEXT_ARG();
381 if (!on_off("neigh_suppress", &neigh_suppress,
382 *argv))
383 return -1;
8cfde5c9
RP
384 } else if (strcmp(*argv, "vlan_tunnel") == 0) {
385 NEXT_ARG();
386 if (!on_off("vlan_tunnel", &vlan_tunnel,
387 *argv))
388 return -1;
64108901
VY
389 } else {
390 usage();
391 }
392 argc--; argv++;
393 }
394 if (d == NULL) {
395 fprintf(stderr, "Device is a required argument.\n");
42ecedd4 396 return -1;
64108901
VY
397 }
398
399
400 req.ifm.ifi_index = ll_name_to_index(d);
401 if (req.ifm.ifi_index == 0) {
402 fprintf(stderr, "Cannot find bridge device \"%s\"\n", d);
42ecedd4 403 return -1;
64108901
VY
404 }
405
406 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
407 * state.
408 */
409 nest = addattr_nest(&req.n, sizeof(req),
410 IFLA_PROTINFO | NLA_F_NESTED);
411 /* Flags first */
412 if (bpdu_guard >= 0)
413 addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard);
414 if (hairpin >= 0)
415 addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin);
416 if (fast_leave >= 0)
417 addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE,
418 fast_leave);
b1b7ce0f
VY
419 if (root_block >= 0)
420 addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block);
f0f4ab60
VY
421 if (flood >= 0)
422 addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood);
9208b4e7
NA
423 if (mcast_flood >= 0)
424 addattr8(&req.n, sizeof(req), IFLA_BRPORT_MCAST_FLOOD,
425 mcast_flood);
f0f4ab60
VY
426 if (learning >= 0)
427 addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning);
674bb438
SF
428 if (learning_sync >= 0)
429 addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING_SYNC,
430 learning_sync);
64108901
VY
431
432 if (cost > 0)
433 addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost);
434
435 if (priority >= 0)
436 addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority);
437
438 if (state >= 0)
439 addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state);
440
41973a47
RP
441 if (neigh_suppress != -1)
442 addattr8(&req.n, sizeof(req), IFLA_BRPORT_NEIGH_SUPPRESS,
443 neigh_suppress);
8cfde5c9
RP
444 if (vlan_tunnel != -1)
445 addattr8(&req.n, sizeof(req), IFLA_BRPORT_VLAN_TUNNEL,
446 vlan_tunnel);
41973a47 447
64108901
VY
448 addattr_nest_end(&req.n, nest);
449
6fdb4658
RP
450 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
451 * designates master or self operation and IFLA_BRIDGE_MODE
452 * for hw 'vepa' or 'veb' operation modes. The hwmodes are
453 * only valid in 'self' mode on some devices so far.
64108901 454 */
6fdb4658 455 if (mode >= 0 || flags > 0) {
64108901
VY
456 nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
457
6fdb4658
RP
458 if (flags > 0)
459 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags);
64108901
VY
460
461 if (mode >= 0)
462 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode);
463
464 addattr_nest_end(&req.n, nest);
465 }
466
86bf43c7 467 if (rtnl_talk(&rth, &req.n, NULL) < 0)
42ecedd4 468 return -1;
64108901
VY
469
470 return 0;
471}
472
473static int brlink_show(int argc, char **argv)
474{
475 char *filter_dev = NULL;
476
477 while (argc > 0) {
478 if (strcmp(*argv, "dev") == 0) {
479 NEXT_ARG();
480 if (filter_dev)
481 duparg("dev", *argv);
482 filter_dev = *argv;
483 }
484 argc--; argv++;
485 }
486
487 if (filter_dev) {
488 if ((filter_index = ll_name_to_index(filter_dev)) == 0) {
489 fprintf(stderr, "Cannot find device \"%s\"\n",
490 filter_dev);
491 return -1;
492 }
493 }
494
fab9a18a
RM
495 if (show_details) {
496 if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK,
497 (compress_vlans ?
498 RTEXT_FILTER_BRVLAN_COMPRESSED :
499 RTEXT_FILTER_BRVLAN)) < 0) {
500 perror("Cannon send dump request");
501 exit(1);
502 }
503 } else {
504 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) {
505 perror("Cannon send dump request");
506 exit(1);
507 }
64108901
VY
508 }
509
f32e4977 510 new_json_obj(json);
64108901
VY
511 if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
512 fprintf(stderr, "Dump terminated\n");
513 exit(1);
514 }
f32e4977
SH
515
516 delete_json_obj();
517 fflush(stdout);
64108901
VY
518 return 0;
519}
520
521int do_link(int argc, char **argv)
522{
523 ll_init_map(&rth);
524 if (argc > 0) {
525 if (matches(*argv, "set") == 0 ||
526 matches(*argv, "change") == 0)
527 return brlink_modify(argc-1, argv+1);
528 if (matches(*argv, "show") == 0 ||
529 matches(*argv, "lst") == 0 ||
530 matches(*argv, "list") == 0)
531 return brlink_show(argc-1, argv+1);
532 if (matches(*argv, "help") == 0)
533 usage();
534 } else
535 return brlink_show(0, NULL);
536
537 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv);
538 exit(-1);
539}