]> git.proxmox.com Git - mirror_iproute2.git/blob - bridge/link.c
bridge: add oneline option
[mirror_iproute2.git] / bridge / link.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/socket.h>
7 #include <sys/time.h>
8 #include <netinet/in.h>
9 #include <linux/if.h>
10 #include <linux/if_bridge.h>
11 #include <string.h>
12 #include <stdbool.h>
13
14 #include "libnetlink.h"
15 #include "utils.h"
16 #include "br_common.h"
17
18 unsigned int filter_index;
19
20 static const char *port_states[] = {
21 [BR_STATE_DISABLED] = "disabled",
22 [BR_STATE_LISTENING] = "listening",
23 [BR_STATE_LEARNING] = "learning",
24 [BR_STATE_FORWARDING] = "forwarding",
25 [BR_STATE_BLOCKING] = "blocking",
26 };
27
28 extern char *if_indextoname (unsigned int __ifindex, char *__ifname);
29
30 static void print_link_flags(FILE *fp, unsigned flags)
31 {
32 fprintf(fp, "<");
33 if (flags & IFF_UP && !(flags & IFF_RUNNING))
34 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
35 flags &= ~IFF_RUNNING;
36 #define _PF(f) if (flags&IFF_##f) { \
37 flags &= ~IFF_##f ; \
38 fprintf(fp, #f "%s", flags ? "," : ""); }
39 _PF(LOOPBACK);
40 _PF(BROADCAST);
41 _PF(POINTOPOINT);
42 _PF(MULTICAST);
43 _PF(NOARP);
44 _PF(ALLMULTI);
45 _PF(PROMISC);
46 _PF(MASTER);
47 _PF(SLAVE);
48 _PF(DEBUG);
49 _PF(DYNAMIC);
50 _PF(AUTOMEDIA);
51 _PF(PORTSEL);
52 _PF(NOTRAILERS);
53 _PF(UP);
54 _PF(LOWER_UP);
55 _PF(DORMANT);
56 _PF(ECHO);
57 #undef _PF
58 if (flags)
59 fprintf(fp, "%x", flags);
60 fprintf(fp, "> ");
61 }
62
63 static const char *oper_states[] = {
64 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
65 "TESTING", "DORMANT", "UP"
66 };
67
68 static const char *hw_mode[] = {"VEB", "VEPA"};
69
70 static void print_operstate(FILE *f, __u8 state)
71 {
72 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
73 fprintf(f, "state %#x ", state);
74 else
75 fprintf(f, "state %s ", oper_states[state]);
76 }
77
78 static void print_portstate(FILE *f, __u8 state)
79 {
80 if (state <= BR_STATE_BLOCKING)
81 fprintf(f, "state %s ", port_states[state]);
82 else
83 fprintf(f, "state (%d) ", state);
84 }
85
86 static void print_onoff(FILE *f, char *flag, __u8 val)
87 {
88 fprintf(f, "%s %s ", flag, val ? "on" : "off");
89 }
90
91 static void print_hwmode(FILE *f, __u16 mode)
92 {
93 if (mode >= sizeof(hw_mode)/sizeof(hw_mode[0]))
94 fprintf(f, "hwmode %#hx ", mode);
95 else
96 fprintf(f, "hwmode %s ", hw_mode[mode]);
97 }
98
99 int print_linkinfo(const struct sockaddr_nl *who,
100 struct nlmsghdr *n, void *arg)
101 {
102 FILE *fp = arg;
103 int len = n->nlmsg_len;
104 struct ifinfomsg *ifi = NLMSG_DATA(n);
105 struct rtattr * tb[IFLA_MAX+1];
106 char b1[IFNAMSIZ];
107
108 len -= NLMSG_LENGTH(sizeof(*ifi));
109 if (len < 0) {
110 fprintf(stderr, "Message too short!\n");
111 return -1;
112 }
113
114 if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
115 return 0;
116
117 if (filter_index && filter_index != ifi->ifi_index)
118 return 0;
119
120 parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
121
122 if (tb[IFLA_IFNAME] == NULL) {
123 fprintf(stderr, "BUG: nil ifname\n");
124 return -1;
125 }
126
127 if (n->nlmsg_type == RTM_DELLINK)
128 fprintf(fp, "Deleted ");
129
130 fprintf(fp, "%d: %s ", ifi->ifi_index,
131 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
132
133 if (tb[IFLA_OPERSTATE])
134 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
135
136 if (tb[IFLA_LINK]) {
137 SPRINT_BUF(b1);
138 int iflink = rta_getattr_u32(tb[IFLA_LINK]);
139 if (iflink == 0)
140 fprintf(fp, "@NONE: ");
141 else
142 fprintf(fp, "@%s: ",
143 if_indextoname(iflink, b1));
144 } else
145 fprintf(fp, ": ");
146
147 print_link_flags(fp, ifi->ifi_flags);
148
149 if (tb[IFLA_MTU])
150 fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU]));
151
152 if (tb[IFLA_MASTER])
153 fprintf(fp, "master %s ",
154 if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1));
155
156 if (tb[IFLA_PROTINFO]) {
157 if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) {
158 struct rtattr *prtb[IFLA_BRPORT_MAX+1];
159
160 parse_rtattr_nested(prtb, IFLA_BRPORT_MAX,
161 tb[IFLA_PROTINFO]);
162
163 if (prtb[IFLA_BRPORT_STATE])
164 print_portstate(fp,
165 rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
166 if (prtb[IFLA_BRPORT_PRIORITY])
167 fprintf(fp, "priority %hu ",
168 rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
169 if (prtb[IFLA_BRPORT_COST])
170 fprintf(fp, "cost %u ",
171 rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
172
173 if (show_details) {
174 fprintf(fp, "%s ", _SL_);
175
176 if (prtb[IFLA_BRPORT_MODE])
177 print_onoff(fp, "hairpin",
178 rta_getattr_u8(prtb[IFLA_BRPORT_MODE]));
179 if (prtb[IFLA_BRPORT_GUARD])
180 print_onoff(fp, "guard",
181 rta_getattr_u8(prtb[IFLA_BRPORT_GUARD]));
182 if (prtb[IFLA_BRPORT_PROTECT])
183 print_onoff(fp, "root_block",
184 rta_getattr_u8(prtb[IFLA_BRPORT_PROTECT]));
185 if (prtb[IFLA_BRPORT_FAST_LEAVE])
186 print_onoff(fp, "fastleave",
187 rta_getattr_u8(prtb[IFLA_BRPORT_FAST_LEAVE]));
188 }
189 } else
190 print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO]));
191 }
192
193 if (tb[IFLA_AF_SPEC]) {
194 /* This is reported by HW devices that have some bridging
195 * capabilities.
196 */
197 struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
198
199 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
200
201 if (tb[IFLA_BRIDGE_MODE])
202 print_hwmode(fp, rta_getattr_u16(tb[IFLA_BRIDGE_MODE]));
203 }
204
205 fprintf(fp, "\n");
206 fflush(fp);
207 return 0;
208 }
209
210 static void usage(void)
211 {
212 fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
213 fprintf(stderr, " [ guard {on | off} ]\n");
214 fprintf(stderr, " [ hairpin {on | off} ] \n");
215 fprintf(stderr, " [ fastleave {on | off} ]\n");
216 fprintf(stderr, " [ root_block {on | off} ]\n");
217 fprintf(stderr, " [ hwmode {vepa | veb} ]\n");
218 fprintf(stderr, " bridge link show [dev DEV]\n");
219 exit(-1);
220 }
221
222 static bool on_off(char *arg, __s8 *attr, char *val)
223 {
224 if (strcmp(val, "on") == 0)
225 *attr = 1;
226 else if (strcmp(val, "off") == 0)
227 *attr = 0;
228 else {
229 fprintf(stderr,
230 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
231 arg);
232 return false;
233 }
234
235 return true;
236 }
237
238 static int brlink_modify(int argc, char **argv)
239 {
240 struct {
241 struct nlmsghdr n;
242 struct ifinfomsg ifm;
243 char buf[512];
244 } req;
245 char *d = NULL;
246 __s8 hairpin = -1;
247 __s8 bpdu_guard = -1;
248 __s8 fast_leave = -1;
249 __s8 root_block = -1;
250 __u32 cost = 0;
251 __s16 priority = -1;
252 __s8 state = -1;
253 __s16 mode = -1;
254 __u16 flags = BRIDGE_FLAGS_MASTER;
255 struct rtattr *nest;
256
257 memset(&req, 0, sizeof(req));
258
259 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
260 req.n.nlmsg_flags = NLM_F_REQUEST;
261 req.n.nlmsg_type = RTM_SETLINK;
262 req.ifm.ifi_family = PF_BRIDGE;
263
264 while (argc > 0) {
265 if (strcmp(*argv, "dev") == 0) {
266 NEXT_ARG();
267 d = *argv;
268 } else if (strcmp(*argv, "guard") == 0) {
269 NEXT_ARG();
270 if (!on_off("guard", &bpdu_guard, *argv))
271 exit(-1);
272 } else if (strcmp(*argv, "hairpin") == 0) {
273 NEXT_ARG();
274 if (!on_off("hairping", &hairpin, *argv))
275 exit(-1);
276 } else if (strcmp(*argv, "fastleave") == 0) {
277 NEXT_ARG();
278 if (!on_off("fastleave", &fast_leave, *argv))
279 exit(-1);
280 } else if (strcmp(*argv, "root_block") == 0) {
281 NEXT_ARG();
282 if (!on_off("root_block", &root_block, *argv))
283 exit(-1);
284 } else if (strcmp(*argv, "cost")) {
285 NEXT_ARG();
286 cost = atoi(*argv);
287 } else if (strcmp(*argv, "priority")) {
288 NEXT_ARG();
289 priority = atoi(*argv);
290 } else if (strcmp(*argv, "state")) {
291 NEXT_ARG();
292 state = atoi(*argv);
293 } else if (strcmp(*argv, "hwmode")) {
294 NEXT_ARG();
295 flags |= BRIDGE_FLAGS_SELF;
296 if (strcmp(*argv, "vepa") == 0)
297 mode = BRIDGE_MODE_VEPA;
298 else if (strcmp(*argv, "veb") == 0)
299 mode = BRIDGE_MODE_VEB;
300 else {
301 fprintf(stderr,
302 "Mode argument must be \"vepa\" or "
303 "\"veb\".\n");
304 exit(-1);
305 }
306 } else {
307 usage();
308 }
309 argc--; argv++;
310 }
311 if (d == NULL) {
312 fprintf(stderr, "Device is a required argument.\n");
313 exit(-1);
314 }
315
316
317 req.ifm.ifi_index = ll_name_to_index(d);
318 if (req.ifm.ifi_index == 0) {
319 fprintf(stderr, "Cannot find bridge device \"%s\"\n", d);
320 exit(-1);
321 }
322
323 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
324 * state.
325 */
326 nest = addattr_nest(&req.n, sizeof(req),
327 IFLA_PROTINFO | NLA_F_NESTED);
328 /* Flags first */
329 if (bpdu_guard >= 0)
330 addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard);
331 if (hairpin >= 0)
332 addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin);
333 if (fast_leave >= 0)
334 addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE,
335 fast_leave);
336 if (root_block >= 0)
337 addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block);
338
339 if (cost > 0)
340 addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost);
341
342 if (priority >= 0)
343 addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority);
344
345 if (state >= 0)
346 addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state);
347
348 addattr_nest_end(&req.n, nest);
349
350 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
351 * designates master or self operation as well as 'vepa' or 'veb'
352 * operation modes. These are only valid in 'self' mode on some
353 * devices so far. Thus we only need to include the flags attribute
354 * if we are setting the hw mode.
355 */
356 if (mode >= 0) {
357 nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC);
358
359 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags);
360
361 if (mode >= 0)
362 addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode);
363
364 addattr_nest_end(&req.n, nest);
365 }
366
367 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
368 exit(2);
369
370 return 0;
371 }
372
373 static int brlink_show(int argc, char **argv)
374 {
375 char *filter_dev = NULL;
376
377 while (argc > 0) {
378 if (strcmp(*argv, "dev") == 0) {
379 NEXT_ARG();
380 if (filter_dev)
381 duparg("dev", *argv);
382 filter_dev = *argv;
383 }
384 argc--; argv++;
385 }
386
387 if (filter_dev) {
388 if ((filter_index = ll_name_to_index(filter_dev)) == 0) {
389 fprintf(stderr, "Cannot find device \"%s\"\n",
390 filter_dev);
391 return -1;
392 }
393 }
394
395 if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) {
396 perror("Cannon send dump request");
397 exit(1);
398 }
399
400 if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
401 fprintf(stderr, "Dump terminated\n");
402 exit(1);
403 }
404 return 0;
405 }
406
407 int do_link(int argc, char **argv)
408 {
409 ll_init_map(&rth);
410 if (argc > 0) {
411 if (matches(*argv, "set") == 0 ||
412 matches(*argv, "change") == 0)
413 return brlink_modify(argc-1, argv+1);
414 if (matches(*argv, "show") == 0 ||
415 matches(*argv, "lst") == 0 ||
416 matches(*argv, "list") == 0)
417 return brlink_show(argc-1, argv+1);
418 if (matches(*argv, "help") == 0)
419 usage();
420 } else
421 return brlink_show(0, NULL);
422
423 fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv);
424 exit(-1);
425 }