2 * iplink_bridge_slave.c Bridge slave device support
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <linux/if_link.h>
16 #include <linux/if_bridge.h>
20 #include "ip_common.h"
22 static void print_explain(FILE *f
)
25 "Usage: ... bridge_slave [ fdb_flush ]\n"
27 " [ priority PRIO ]\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"
48 static void explain(void)
50 print_explain(stderr
);
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",
61 static const char *fwd_mask_tbl
[16] = {
67 static void print_portstate(FILE *f
, __u8 state
)
69 if (state
<= BR_STATE_BLOCKING
)
70 print_string(PRINT_ANY
,
75 print_int(PRINT_ANY
, "state_index", "state (%d) ", state
);
78 static void _print_onoff(FILE *f
, char *json_flag
, char *flag
, __u8 val
)
80 if (is_json_context())
81 print_bool(PRINT_JSON
, flag
, NULL
, val
);
83 fprintf(f
, "%s %s ", flag
, val
? "on" : "off");
86 static void _print_timer(FILE *f
, const char *attr
, struct rtattr
*timer
)
90 __jiffies_to_tv(&tv
, rta_getattr_u64(timer
));
91 if (is_json_context()) {
92 json_writer_t
*jw
= get_json_writer();
95 jsonw_printf(jw
, "%i.%.2i",
96 (int)tv
.tv_sec
, (int)tv
.tv_usec
/ 10000);
98 fprintf(f
, "%s %4i.%.2i ", attr
, (int)tv
.tv_sec
,
99 (int)tv
.tv_usec
/ 10000);
103 static void _bitmask2str(__u16 bitmask
, char *dst
, size_t dst_size
,
108 for (i
= 0, len
= 0; bitmask
; i
++, bitmask
>>= 1) {
111 len
+= snprintf(dst
+ len
, dst_size
- len
, "%s,",
114 len
+= snprintf(dst
+ len
, dst_size
- len
, "0x%x,",
120 snprintf(dst
, dst_size
, "0x0");
125 static void bridge_slave_print_opt(struct link_util
*lu
, FILE *f
,
131 if (tb
[IFLA_BRPORT_STATE
])
132 print_portstate(f
, rta_getattr_u8(tb
[IFLA_BRPORT_STATE
]));
134 if (tb
[IFLA_BRPORT_PRIORITY
])
138 rta_getattr_u16(tb
[IFLA_BRPORT_PRIORITY
]));
140 if (tb
[IFLA_BRPORT_COST
])
144 rta_getattr_u32(tb
[IFLA_BRPORT_COST
]));
146 if (tb
[IFLA_BRPORT_MODE
])
147 _print_onoff(f
, "mode", "hairpin",
148 rta_getattr_u8(tb
[IFLA_BRPORT_MODE
]));
150 if (tb
[IFLA_BRPORT_GUARD
])
151 _print_onoff(f
, "guard", "guard",
152 rta_getattr_u8(tb
[IFLA_BRPORT_GUARD
]));
154 if (tb
[IFLA_BRPORT_PROTECT
])
155 _print_onoff(f
, "protect", "root_block",
156 rta_getattr_u8(tb
[IFLA_BRPORT_PROTECT
]));
158 if (tb
[IFLA_BRPORT_FAST_LEAVE
])
159 _print_onoff(f
, "fast_leave", "fastleave",
160 rta_getattr_u8(tb
[IFLA_BRPORT_FAST_LEAVE
]));
162 if (tb
[IFLA_BRPORT_LEARNING
])
163 _print_onoff(f
, "learning", "learning",
164 rta_getattr_u8(tb
[IFLA_BRPORT_LEARNING
]));
166 if (tb
[IFLA_BRPORT_UNICAST_FLOOD
])
167 _print_onoff(f
, "unicast_flood", "flood",
168 rta_getattr_u8(tb
[IFLA_BRPORT_UNICAST_FLOOD
]));
170 if (tb
[IFLA_BRPORT_ID
])
171 print_0xhex(PRINT_ANY
, "id", "port_id 0x%x ",
172 rta_getattr_u16(tb
[IFLA_BRPORT_ID
]));
174 if (tb
[IFLA_BRPORT_NO
])
175 print_0xhex(PRINT_ANY
, "no", "port_no 0x%x ",
176 rta_getattr_u16(tb
[IFLA_BRPORT_NO
]));
178 if (tb
[IFLA_BRPORT_DESIGNATED_PORT
])
179 print_uint(PRINT_ANY
,
181 "designated_port %u ",
182 rta_getattr_u16(tb
[IFLA_BRPORT_DESIGNATED_PORT
]));
184 if (tb
[IFLA_BRPORT_DESIGNATED_COST
])
185 print_uint(PRINT_ANY
,
187 "designated_cost %u ",
188 rta_getattr_u16(tb
[IFLA_BRPORT_DESIGNATED_COST
]));
190 if (tb
[IFLA_BRPORT_BRIDGE_ID
]) {
193 br_dump_bridge_id(RTA_DATA(tb
[IFLA_BRPORT_BRIDGE_ID
]),
194 bridge_id
, sizeof(bridge_id
));
195 print_string(PRINT_ANY
,
197 "designated_bridge %s ",
201 if (tb
[IFLA_BRPORT_ROOT_ID
]) {
204 br_dump_bridge_id(RTA_DATA(tb
[IFLA_BRPORT_ROOT_ID
]),
205 root_id
, sizeof(root_id
));
206 print_string(PRINT_ANY
,
208 "designated_root %s ", root_id
);
211 if (tb
[IFLA_BRPORT_HOLD_TIMER
])
212 _print_timer(f
, "hold_timer", tb
[IFLA_BRPORT_HOLD_TIMER
]);
214 if (tb
[IFLA_BRPORT_MESSAGE_AGE_TIMER
])
215 _print_timer(f
, "message_age_timer",
216 tb
[IFLA_BRPORT_MESSAGE_AGE_TIMER
]);
218 if (tb
[IFLA_BRPORT_FORWARD_DELAY_TIMER
])
219 _print_timer(f
, "forward_delay_timer",
220 tb
[IFLA_BRPORT_FORWARD_DELAY_TIMER
]);
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
]));
228 if (tb
[IFLA_BRPORT_CONFIG_PENDING
])
229 print_uint(PRINT_ANY
,
231 "config_pending %u ",
232 rta_getattr_u8(tb
[IFLA_BRPORT_CONFIG_PENDING
]));
234 if (tb
[IFLA_BRPORT_PROXYARP
])
235 _print_onoff(f
, "proxyarp", "proxy_arp",
236 rta_getattr_u8(tb
[IFLA_BRPORT_PROXYARP
]));
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
]));
242 if (tb
[IFLA_BRPORT_MULTICAST_ROUTER
])
243 print_uint(PRINT_ANY
,
246 rta_getattr_u8(tb
[IFLA_BRPORT_MULTICAST_ROUTER
]));
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
,
253 "mcast_fast_leave %s ",
254 rta_getattr_u8(tb
[IFLA_BRPORT_FAST_LEAVE
]) ? "on" : "off");
256 if (tb
[IFLA_BRPORT_MCAST_FLOOD
])
257 _print_onoff(f
, "mcast_flood", "mcast_flood",
258 rta_getattr_u8(tb
[IFLA_BRPORT_MCAST_FLOOD
]));
260 if (tb
[IFLA_BRPORT_NEIGH_SUPPRESS
])
261 _print_onoff(f
, "neigh_suppress", "neigh_suppress",
262 rta_getattr_u8(tb
[IFLA_BRPORT_NEIGH_SUPPRESS
]));
264 if (tb
[IFLA_BRPORT_GROUP_FWD_MASK
]) {
268 fwd_mask
= rta_getattr_u16(tb
[IFLA_BRPORT_GROUP_FWD_MASK
]);
269 print_0xhex(PRINT_ANY
, "group_fwd_mask",
270 "group_fwd_mask 0x%x ", 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
);
276 if (tb
[IFLA_BRPORT_VLAN_TUNNEL
])
277 _print_onoff(f
, "vlan_tunnel", "vlan_tunnel",
278 rta_getattr_u8(tb
[IFLA_BRPORT_VLAN_TUNNEL
]));
280 if (tb
[IFLA_BRPORT_ISOLATED
])
281 _print_onoff(f
, "isolated", "isolated",
282 rta_getattr_u8(tb
[IFLA_BRPORT_ISOLATED
]));
284 if (tb
[IFLA_BRPORT_BACKUP_PORT
]) {
285 int backup_p
= rta_getattr_u32(tb
[IFLA_BRPORT_BACKUP_PORT
]);
287 print_string(PRINT_ANY
, "backup_port", "backup_port %s ",
288 ll_index_to_name(backup_p
));
292 static void bridge_slave_parse_on_off(char *arg_name
, char *arg_val
,
293 struct nlmsghdr
*n
, int type
)
297 if (strcmp(arg_val
, "on") == 0)
299 else if (strcmp(arg_val
, "off") == 0)
302 invarg("should be \"on\" or \"off\"", arg_name
);
304 addattr8(n
, 1024, type
, val
);
307 static int bridge_slave_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
315 if (matches(*argv
, "fdb_flush") == 0) {
316 addattr(n
, 1024, IFLA_BRPORT_FLUSH
);
317 } else if (matches(*argv
, "state") == 0) {
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) {
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) {
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) {
334 bridge_slave_parse_on_off("hairpin", *argv
, n
,
336 } else if (matches(*argv
, "guard") == 0) {
338 bridge_slave_parse_on_off("guard", *argv
, n
,
340 } else if (matches(*argv
, "root_block") == 0) {
342 bridge_slave_parse_on_off("root_block", *argv
, n
,
343 IFLA_BRPORT_PROTECT
);
344 } else if (matches(*argv
, "fastleave") == 0) {
346 bridge_slave_parse_on_off("fastleave", *argv
, n
,
347 IFLA_BRPORT_FAST_LEAVE
);
348 } else if (matches(*argv
, "learning") == 0) {
350 bridge_slave_parse_on_off("learning", *argv
, n
,
351 IFLA_BRPORT_LEARNING
);
352 } else if (matches(*argv
, "flood") == 0) {
354 bridge_slave_parse_on_off("flood", *argv
, n
,
355 IFLA_BRPORT_UNICAST_FLOOD
);
356 } else if (matches(*argv
, "mcast_flood") == 0) {
358 bridge_slave_parse_on_off("mcast_flood", *argv
, n
,
359 IFLA_BRPORT_MCAST_FLOOD
);
360 } else if (matches(*argv
, "proxy_arp") == 0) {
362 bridge_slave_parse_on_off("proxy_arp", *argv
, n
,
363 IFLA_BRPORT_PROXYARP
);
364 } else if (matches(*argv
, "proxy_arp_wifi") == 0) {
366 bridge_slave_parse_on_off("proxy_arp_wifi", *argv
, n
,
367 IFLA_BRPORT_PROXYARP_WIFI
);
368 } else if (matches(*argv
, "mcast_router") == 0) {
372 if (get_u8(&mcast_router
, *argv
, 0))
373 invarg("invalid mcast_router", *argv
);
374 addattr8(n
, 1024, IFLA_BRPORT_MULTICAST_ROUTER
,
376 } else if (matches(*argv
, "mcast_fast_leave") == 0) {
378 bridge_slave_parse_on_off("mcast_fast_leave", *argv
, n
,
379 IFLA_BRPORT_FAST_LEAVE
);
380 } else if (matches(*argv
, "neigh_suppress") == 0) {
382 bridge_slave_parse_on_off("neigh_suppress", *argv
, n
,
383 IFLA_BRPORT_NEIGH_SUPPRESS
);
384 } else if (matches(*argv
, "group_fwd_mask") == 0) {
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) {
393 bridge_slave_parse_on_off("vlan_tunnel", *argv
, n
,
394 IFLA_BRPORT_VLAN_TUNNEL
);
395 } else if (matches(*argv
, "isolated") == 0) {
397 bridge_slave_parse_on_off("isolated", *argv
, n
,
398 IFLA_BRPORT_ISOLATED
);
399 } else if (matches(*argv
, "backup_port") == 0) {
403 ifindex
= ll_name_to_index(*argv
);
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) {
413 fprintf(stderr
, "bridge_slave: unknown option \"%s\"?\n",
424 static void bridge_slave_print_help(struct link_util
*lu
, int argc
, char **argv
,
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
,