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