]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_bridge_slave.c
85e6b424315fd3fc7dbc6e568335c27a93c3ee7b
[mirror_iproute2.git] / ip / iplink_bridge_slave.c
1 /*
2 * iplink_bridge_slave.c Bridge slave device support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jiri Pirko <jiri@resnulli.us>
10 */
11
12 #include <stdio.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <linux/if_link.h>
16 #include <linux/if_bridge.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "ip_common.h"
21
22 static void print_explain(FILE *f)
23 {
24 fprintf(f,
25 "Usage: ... bridge_slave [ fdb_flush ]\n"
26 " [ state STATE ]\n"
27 " [ priority PRIO ]\n"
28 " [ cost COST ]\n"
29 " [ guard {on | off} ]\n"
30 " [ hairpin {on | off} ]\n"
31 " [ fastleave {on | off} ]\n"
32 " [ root_block {on | off} ]\n"
33 " [ learning {on | off} ]\n"
34 " [ flood {on | off} ]\n"
35 " [ proxy_arp {on | off} ]\n"
36 " [ proxy_arp_wifi {on | off} ]\n"
37 " [ mcast_router MULTICAST_ROUTER ]\n"
38 " [ mcast_fast_leave {on | off} ]\n"
39 " [ mcast_flood {on | off} ]\n"
40 " [ group_fwd_mask MASK ]\n"
41 " [ neigh_suppress {on | off} ]\n"
42 " [ vlan_tunnel {on | off} ]\n"
43 " [ isolated {on | off} ]\n"
44 " [ backup_port DEVICE ] [ nobackup_port ]\n"
45 );
46 }
47
48 static void explain(void)
49 {
50 print_explain(stderr);
51 }
52
53 static const char *port_states[] = {
54 [BR_STATE_DISABLED] = "disabled",
55 [BR_STATE_LISTENING] = "listening",
56 [BR_STATE_LEARNING] = "learning",
57 [BR_STATE_FORWARDING] = "forwarding",
58 [BR_STATE_BLOCKING] = "blocking",
59 };
60
61 static const char *fwd_mask_tbl[16] = {
62 [0] = "stp",
63 [2] = "lacp",
64 [14] = "lldp"
65 };
66
67 static void print_portstate(FILE *f, __u8 state)
68 {
69 if (state <= BR_STATE_BLOCKING)
70 print_string(PRINT_ANY,
71 "state",
72 "state %s ",
73 port_states[state]);
74 else
75 print_int(PRINT_ANY, "state_index", "state (%d) ", state);
76 }
77
78 static void _print_onoff(FILE *f, char *json_flag, char *flag, __u8 val)
79 {
80 if (is_json_context())
81 print_bool(PRINT_JSON, flag, NULL, val);
82 else
83 fprintf(f, "%s %s ", flag, val ? "on" : "off");
84 }
85
86 static void _print_timer(FILE *f, const char *attr, struct rtattr *timer)
87 {
88 struct timeval tv;
89
90 __jiffies_to_tv(&tv, rta_getattr_u64(timer));
91 if (is_json_context()) {
92 json_writer_t *jw = get_json_writer();
93
94 jsonw_name(jw, attr);
95 jsonw_printf(jw, "%i.%.2i",
96 (int)tv.tv_sec, (int)tv.tv_usec / 10000);
97 } else {
98 fprintf(f, "%s %4i.%.2i ", attr, (int)tv.tv_sec,
99 (int)tv.tv_usec / 10000);
100 }
101 }
102
103 static void _bitmask2str(__u16 bitmask, char *dst, size_t dst_size,
104 const char **tbl)
105 {
106 int len, i;
107
108 for (i = 0, len = 0; bitmask; i++, bitmask >>= 1) {
109 if (bitmask & 0x1) {
110 if (tbl[i])
111 len += snprintf(dst + len, dst_size - len, "%s,",
112 tbl[i]);
113 else
114 len += snprintf(dst + len, dst_size - len, "0x%x,",
115 (1 << i));
116 }
117 }
118
119 if (!len)
120 snprintf(dst, dst_size, "0x0");
121 else
122 dst[len - 1] = 0;
123 }
124
125 static void bridge_slave_print_opt(struct link_util *lu, FILE *f,
126 struct rtattr *tb[])
127 {
128 if (!tb)
129 return;
130
131 if (tb[IFLA_BRPORT_STATE])
132 print_portstate(f, rta_getattr_u8(tb[IFLA_BRPORT_STATE]));
133
134 if (tb[IFLA_BRPORT_PRIORITY])
135 print_int(PRINT_ANY,
136 "priority",
137 "priority %d ",
138 rta_getattr_u16(tb[IFLA_BRPORT_PRIORITY]));
139
140 if (tb[IFLA_BRPORT_COST])
141 print_int(PRINT_ANY,
142 "cost",
143 "cost %d ",
144 rta_getattr_u32(tb[IFLA_BRPORT_COST]));
145
146 if (tb[IFLA_BRPORT_MODE])
147 _print_onoff(f, "mode", "hairpin",
148 rta_getattr_u8(tb[IFLA_BRPORT_MODE]));
149
150 if (tb[IFLA_BRPORT_GUARD])
151 _print_onoff(f, "guard", "guard",
152 rta_getattr_u8(tb[IFLA_BRPORT_GUARD]));
153
154 if (tb[IFLA_BRPORT_PROTECT])
155 _print_onoff(f, "protect", "root_block",
156 rta_getattr_u8(tb[IFLA_BRPORT_PROTECT]));
157
158 if (tb[IFLA_BRPORT_FAST_LEAVE])
159 _print_onoff(f, "fast_leave", "fastleave",
160 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]));
161
162 if (tb[IFLA_BRPORT_LEARNING])
163 _print_onoff(f, "learning", "learning",
164 rta_getattr_u8(tb[IFLA_BRPORT_LEARNING]));
165
166 if (tb[IFLA_BRPORT_UNICAST_FLOOD])
167 _print_onoff(f, "unicast_flood", "flood",
168 rta_getattr_u8(tb[IFLA_BRPORT_UNICAST_FLOOD]));
169
170 if (tb[IFLA_BRPORT_ID])
171 print_0xhex(PRINT_ANY, "id", "port_id %#llx ",
172 rta_getattr_u16(tb[IFLA_BRPORT_ID]));
173
174 if (tb[IFLA_BRPORT_NO])
175 print_0xhex(PRINT_ANY, "no", "port_no %#llx ",
176 rta_getattr_u16(tb[IFLA_BRPORT_NO]));
177
178 if (tb[IFLA_BRPORT_DESIGNATED_PORT])
179 print_uint(PRINT_ANY,
180 "designated_port",
181 "designated_port %u ",
182 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_PORT]));
183
184 if (tb[IFLA_BRPORT_DESIGNATED_COST])
185 print_uint(PRINT_ANY,
186 "designated_cost",
187 "designated_cost %u ",
188 rta_getattr_u16(tb[IFLA_BRPORT_DESIGNATED_COST]));
189
190 if (tb[IFLA_BRPORT_BRIDGE_ID]) {
191 char bridge_id[32];
192
193 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_BRIDGE_ID]),
194 bridge_id, sizeof(bridge_id));
195 print_string(PRINT_ANY,
196 "bridge_id",
197 "designated_bridge %s ",
198 bridge_id);
199 }
200
201 if (tb[IFLA_BRPORT_ROOT_ID]) {
202 char root_id[32];
203
204 br_dump_bridge_id(RTA_DATA(tb[IFLA_BRPORT_ROOT_ID]),
205 root_id, sizeof(root_id));
206 print_string(PRINT_ANY,
207 "root_id",
208 "designated_root %s ", root_id);
209 }
210
211 if (tb[IFLA_BRPORT_HOLD_TIMER])
212 _print_timer(f, "hold_timer", tb[IFLA_BRPORT_HOLD_TIMER]);
213
214 if (tb[IFLA_BRPORT_MESSAGE_AGE_TIMER])
215 _print_timer(f, "message_age_timer",
216 tb[IFLA_BRPORT_MESSAGE_AGE_TIMER]);
217
218 if (tb[IFLA_BRPORT_FORWARD_DELAY_TIMER])
219 _print_timer(f, "forward_delay_timer",
220 tb[IFLA_BRPORT_FORWARD_DELAY_TIMER]);
221
222 if (tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK])
223 print_uint(PRINT_ANY,
224 "topology_change_ack",
225 "topology_change_ack %u ",
226 rta_getattr_u8(tb[IFLA_BRPORT_TOPOLOGY_CHANGE_ACK]));
227
228 if (tb[IFLA_BRPORT_CONFIG_PENDING])
229 print_uint(PRINT_ANY,
230 "config_pending",
231 "config_pending %u ",
232 rta_getattr_u8(tb[IFLA_BRPORT_CONFIG_PENDING]));
233
234 if (tb[IFLA_BRPORT_PROXYARP])
235 _print_onoff(f, "proxyarp", "proxy_arp",
236 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP]));
237
238 if (tb[IFLA_BRPORT_PROXYARP_WIFI])
239 _print_onoff(f, "proxyarp_wifi", "proxy_arp_wifi",
240 rta_getattr_u8(tb[IFLA_BRPORT_PROXYARP_WIFI]));
241
242 if (tb[IFLA_BRPORT_MULTICAST_ROUTER])
243 print_uint(PRINT_ANY,
244 "multicast_router",
245 "mcast_router %u ",
246 rta_getattr_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]));
247
248 if (tb[IFLA_BRPORT_FAST_LEAVE])
249 // not printing any json here because
250 // we already printed fast_leave before
251 print_string(PRINT_FP,
252 NULL,
253 "mcast_fast_leave %s ",
254 rta_getattr_u8(tb[IFLA_BRPORT_FAST_LEAVE]) ? "on" : "off");
255
256 if (tb[IFLA_BRPORT_MCAST_FLOOD])
257 _print_onoff(f, "mcast_flood", "mcast_flood",
258 rta_getattr_u8(tb[IFLA_BRPORT_MCAST_FLOOD]));
259
260 if (tb[IFLA_BRPORT_NEIGH_SUPPRESS])
261 _print_onoff(f, "neigh_suppress", "neigh_suppress",
262 rta_getattr_u8(tb[IFLA_BRPORT_NEIGH_SUPPRESS]));
263
264 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
265 char convbuf[256];
266 __u16 fwd_mask;
267
268 fwd_mask = rta_getattr_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
269 print_0xhex(PRINT_ANY, "group_fwd_mask",
270 "group_fwd_mask %#llx ", fwd_mask);
271 _bitmask2str(fwd_mask, convbuf, sizeof(convbuf), fwd_mask_tbl);
272 print_string(PRINT_ANY, "group_fwd_mask_str",
273 "group_fwd_mask_str %s ", convbuf);
274 }
275
276 if (tb[IFLA_BRPORT_VLAN_TUNNEL])
277 _print_onoff(f, "vlan_tunnel", "vlan_tunnel",
278 rta_getattr_u8(tb[IFLA_BRPORT_VLAN_TUNNEL]));
279
280 if (tb[IFLA_BRPORT_ISOLATED])
281 _print_onoff(f, "isolated", "isolated",
282 rta_getattr_u8(tb[IFLA_BRPORT_ISOLATED]));
283
284 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
285 int backup_p = rta_getattr_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
286
287 print_string(PRINT_ANY, "backup_port", "backup_port %s ",
288 ll_index_to_name(backup_p));
289 }
290 }
291
292 static void bridge_slave_parse_on_off(char *arg_name, char *arg_val,
293 struct nlmsghdr *n, int type)
294 {
295 __u8 val;
296
297 if (strcmp(arg_val, "on") == 0)
298 val = 1;
299 else if (strcmp(arg_val, "off") == 0)
300 val = 0;
301 else
302 invarg("should be \"on\" or \"off\"", arg_name);
303
304 addattr8(n, 1024, type, val);
305 }
306
307 static int bridge_slave_parse_opt(struct link_util *lu, int argc, char **argv,
308 struct nlmsghdr *n)
309 {
310 __u8 state;
311 __u16 priority;
312 __u32 cost;
313
314 while (argc > 0) {
315 if (matches(*argv, "fdb_flush") == 0) {
316 addattr(n, 1024, IFLA_BRPORT_FLUSH);
317 } else if (matches(*argv, "state") == 0) {
318 NEXT_ARG();
319 if (get_u8(&state, *argv, 0))
320 invarg("state is invalid", *argv);
321 addattr8(n, 1024, IFLA_BRPORT_STATE, state);
322 } else if (matches(*argv, "priority") == 0) {
323 NEXT_ARG();
324 if (get_u16(&priority, *argv, 0))
325 invarg("priority is invalid", *argv);
326 addattr16(n, 1024, IFLA_BRPORT_PRIORITY, priority);
327 } else if (matches(*argv, "cost") == 0) {
328 NEXT_ARG();
329 if (get_u32(&cost, *argv, 0))
330 invarg("cost is invalid", *argv);
331 addattr32(n, 1024, IFLA_BRPORT_COST, cost);
332 } else if (matches(*argv, "hairpin") == 0) {
333 NEXT_ARG();
334 bridge_slave_parse_on_off("hairpin", *argv, n,
335 IFLA_BRPORT_MODE);
336 } else if (matches(*argv, "guard") == 0) {
337 NEXT_ARG();
338 bridge_slave_parse_on_off("guard", *argv, n,
339 IFLA_BRPORT_GUARD);
340 } else if (matches(*argv, "root_block") == 0) {
341 NEXT_ARG();
342 bridge_slave_parse_on_off("root_block", *argv, n,
343 IFLA_BRPORT_PROTECT);
344 } else if (matches(*argv, "fastleave") == 0) {
345 NEXT_ARG();
346 bridge_slave_parse_on_off("fastleave", *argv, n,
347 IFLA_BRPORT_FAST_LEAVE);
348 } else if (matches(*argv, "learning") == 0) {
349 NEXT_ARG();
350 bridge_slave_parse_on_off("learning", *argv, n,
351 IFLA_BRPORT_LEARNING);
352 } else if (matches(*argv, "flood") == 0) {
353 NEXT_ARG();
354 bridge_slave_parse_on_off("flood", *argv, n,
355 IFLA_BRPORT_UNICAST_FLOOD);
356 } else if (matches(*argv, "mcast_flood") == 0) {
357 NEXT_ARG();
358 bridge_slave_parse_on_off("mcast_flood", *argv, n,
359 IFLA_BRPORT_MCAST_FLOOD);
360 } else if (matches(*argv, "proxy_arp") == 0) {
361 NEXT_ARG();
362 bridge_slave_parse_on_off("proxy_arp", *argv, n,
363 IFLA_BRPORT_PROXYARP);
364 } else if (matches(*argv, "proxy_arp_wifi") == 0) {
365 NEXT_ARG();
366 bridge_slave_parse_on_off("proxy_arp_wifi", *argv, n,
367 IFLA_BRPORT_PROXYARP_WIFI);
368 } else if (matches(*argv, "mcast_router") == 0) {
369 __u8 mcast_router;
370
371 NEXT_ARG();
372 if (get_u8(&mcast_router, *argv, 0))
373 invarg("invalid mcast_router", *argv);
374 addattr8(n, 1024, IFLA_BRPORT_MULTICAST_ROUTER,
375 mcast_router);
376 } else if (matches(*argv, "mcast_fast_leave") == 0) {
377 NEXT_ARG();
378 bridge_slave_parse_on_off("mcast_fast_leave", *argv, n,
379 IFLA_BRPORT_FAST_LEAVE);
380 } else if (matches(*argv, "neigh_suppress") == 0) {
381 NEXT_ARG();
382 bridge_slave_parse_on_off("neigh_suppress", *argv, n,
383 IFLA_BRPORT_NEIGH_SUPPRESS);
384 } else if (matches(*argv, "group_fwd_mask") == 0) {
385 __u16 mask;
386
387 NEXT_ARG();
388 if (get_u16(&mask, *argv, 0))
389 invarg("invalid group_fwd_mask", *argv);
390 addattr16(n, 1024, IFLA_BRPORT_GROUP_FWD_MASK, mask);
391 } else if (matches(*argv, "vlan_tunnel") == 0) {
392 NEXT_ARG();
393 bridge_slave_parse_on_off("vlan_tunnel", *argv, n,
394 IFLA_BRPORT_VLAN_TUNNEL);
395 } else if (matches(*argv, "isolated") == 0) {
396 NEXT_ARG();
397 bridge_slave_parse_on_off("isolated", *argv, n,
398 IFLA_BRPORT_ISOLATED);
399 } else if (matches(*argv, "backup_port") == 0) {
400 int ifindex;
401
402 NEXT_ARG();
403 ifindex = ll_name_to_index(*argv);
404 if (!ifindex)
405 invarg("Device does not exist\n", *argv);
406 addattr32(n, 1024, IFLA_BRPORT_BACKUP_PORT, ifindex);
407 } else if (matches(*argv, "nobackup_port") == 0) {
408 addattr32(n, 1024, IFLA_BRPORT_BACKUP_PORT, 0);
409 } else if (matches(*argv, "help") == 0) {
410 explain();
411 return -1;
412 } else {
413 fprintf(stderr, "bridge_slave: unknown option \"%s\"?\n",
414 *argv);
415 explain();
416 return -1;
417 }
418 argc--, argv++;
419 }
420
421 return 0;
422 }
423
424 static void bridge_slave_print_help(struct link_util *lu, int argc, char **argv,
425 FILE *f)
426 {
427 print_explain(f);
428 }
429
430 struct link_util bridge_slave_link_util = {
431 .id = "bridge_slave",
432 .maxattr = IFLA_BRPORT_MAX,
433 .print_opt = bridge_slave_print_opt,
434 .parse_opt = bridge_slave_parse_opt,
435 .print_help = bridge_slave_print_help,
436 .parse_ifla_xstats = bridge_parse_xstats,
437 .print_ifla_xstats = bridge_print_xstats,
438 };