Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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> |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 12 | #include <stdbool.h> |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 13 | |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 14 | #include "libnetlink.h" |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 15 | #include "utils.h" |
| 16 | #include "br_common.h" |
| 17 | |
Andreas Henriksson | 9dca899 | 2014-06-04 19:40:37 +0200 | [diff] [blame] | 18 | static unsigned int filter_index; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 19 | |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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[] = { |
Stephen Hemminger | 38df7ac | 2012-10-29 17:48:55 -0700 | [diff] [blame] | 64 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 65 | "TESTING", "DORMANT", "UP" |
| 66 | }; |
| 67 | |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 68 | static const char *hw_mode[] = {"VEB", "VEPA"}; |
| 69 | |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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 | |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 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 | |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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 | |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 117 | if (filter_index && filter_index != ifi->ifi_index) |
| 118 | return 0; |
| 119 | |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 120 | parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 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, |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 131 | tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>"); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 132 | |
Stephen Hemminger | 38df7ac | 2012-10-29 17:48:55 -0700 | [diff] [blame] | 133 | if (tb[IFLA_OPERSTATE]) |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 134 | print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); |
Stephen Hemminger | 38df7ac | 2012-10-29 17:48:55 -0700 | [diff] [blame] | 135 | |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 136 | if (tb[IFLA_LINK]) { |
| 137 | SPRINT_BUF(b1); |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 138 | int iflink = rta_getattr_u32(tb[IFLA_LINK]); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 139 | if (iflink == 0) |
| 140 | fprintf(fp, "@NONE: "); |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 141 | else |
Stephen Hemminger | 38df7ac | 2012-10-29 17:48:55 -0700 | [diff] [blame] | 142 | fprintf(fp, "@%s: ", |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 143 | if_indextoname(iflink, b1)); |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 144 | } else |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 145 | fprintf(fp, ": "); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 146 | |
| 147 | print_link_flags(fp, ifi->ifi_flags); |
| 148 | |
| 149 | if (tb[IFLA_MTU]) |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 150 | fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU])); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 151 | |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 152 | if (tb[IFLA_MASTER]) |
Stephen Hemminger | 38df7ac | 2012-10-29 17:48:55 -0700 | [diff] [blame] | 153 | fprintf(fp, "master %s ", |
Stephen Hemminger | 1465db1 | 2012-10-29 17:52:45 -0700 | [diff] [blame] | 154 | if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1)); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 155 | |
| 156 | if (tb[IFLA_PROTINFO]) { |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 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])); |
Stephen Hemminger | 4cd20da | 2013-03-16 10:18:50 -0700 | [diff] [blame] | 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])); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 188 | if (prtb[IFLA_BRPORT_LEARNING]) |
| 189 | print_onoff(fp, "learning", |
| 190 | rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING])); |
Scott Feldman | 674bb43 | 2014-12-29 12:20:07 -0800 | [diff] [blame] | 191 | if (prtb[IFLA_BRPORT_LEARNING_SYNC]) |
| 192 | print_onoff(fp, "learning_sync", |
| 193 | rta_getattr_u8(prtb[IFLA_BRPORT_LEARNING_SYNC])); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 194 | if (prtb[IFLA_BRPORT_UNICAST_FLOOD]) |
| 195 | print_onoff(fp, "flood", |
| 196 | rta_getattr_u8(prtb[IFLA_BRPORT_UNICAST_FLOOD])); |
Stephen Hemminger | 4cd20da | 2013-03-16 10:18:50 -0700 | [diff] [blame] | 197 | } |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 198 | } else |
| 199 | print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO])); |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 202 | if (tb[IFLA_AF_SPEC]) { |
| 203 | /* This is reported by HW devices that have some bridging |
| 204 | * capabilities. |
| 205 | */ |
| 206 | struct rtattr *aftb[IFLA_BRIDGE_MAX+1]; |
| 207 | |
| 208 | parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]); |
| 209 | |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 210 | if (aftb[IFLA_BRIDGE_MODE]) |
| 211 | print_hwmode(fp, rta_getattr_u16(aftb[IFLA_BRIDGE_MODE])); |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 212 | } |
Stephen Hemminger | d04bc30 | 2012-08-01 15:23:49 -0700 | [diff] [blame] | 213 | |
| 214 | fprintf(fp, "\n"); |
| 215 | fflush(fp); |
| 216 | return 0; |
| 217 | } |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 218 | |
| 219 | static void usage(void) |
| 220 | { |
| 221 | fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n"); |
| 222 | fprintf(stderr, " [ guard {on | off} ]\n"); |
| 223 | fprintf(stderr, " [ hairpin {on | off} ] \n"); |
| 224 | fprintf(stderr, " [ fastleave {on | off} ]\n"); |
| 225 | fprintf(stderr, " [ root_block {on | off} ]\n"); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 226 | fprintf(stderr, " [ learning {on | off} ]\n"); |
Scott Feldman | 674bb43 | 2014-12-29 12:20:07 -0800 | [diff] [blame] | 227 | fprintf(stderr, " [ learning_sync {on | off} ]\n"); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 228 | fprintf(stderr, " [ flood {on | off} ]\n"); |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 229 | fprintf(stderr, " [ hwmode {vepa | veb} ]\n"); |
| 230 | fprintf(stderr, " bridge link show [dev DEV]\n"); |
| 231 | exit(-1); |
| 232 | } |
| 233 | |
| 234 | static bool on_off(char *arg, __s8 *attr, char *val) |
| 235 | { |
| 236 | if (strcmp(val, "on") == 0) |
| 237 | *attr = 1; |
| 238 | else if (strcmp(val, "off") == 0) |
| 239 | *attr = 0; |
| 240 | else { |
| 241 | fprintf(stderr, |
| 242 | "Error: argument of \"%s\" must be \"on\" or \"off\"\n", |
| 243 | arg); |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | static int brlink_modify(int argc, char **argv) |
| 251 | { |
| 252 | struct { |
| 253 | struct nlmsghdr n; |
| 254 | struct ifinfomsg ifm; |
| 255 | char buf[512]; |
| 256 | } req; |
| 257 | char *d = NULL; |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 258 | __s8 learning = -1; |
Scott Feldman | 674bb43 | 2014-12-29 12:20:07 -0800 | [diff] [blame] | 259 | __s8 learning_sync = -1; |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 260 | __s8 flood = -1; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 261 | __s8 hairpin = -1; |
| 262 | __s8 bpdu_guard = -1; |
| 263 | __s8 fast_leave = -1; |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 264 | __s8 root_block = -1; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 265 | __u32 cost = 0; |
| 266 | __s16 priority = -1; |
| 267 | __s8 state = -1; |
| 268 | __s16 mode = -1; |
Roopa Prabhu | 6fdb465 | 2014-12-06 00:21:01 -0800 | [diff] [blame] | 269 | __u16 flags = 0; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 270 | struct rtattr *nest; |
| 271 | |
| 272 | memset(&req, 0, sizeof(req)); |
| 273 | |
| 274 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 275 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 276 | req.n.nlmsg_type = RTM_SETLINK; |
| 277 | req.ifm.ifi_family = PF_BRIDGE; |
| 278 | |
| 279 | while (argc > 0) { |
| 280 | if (strcmp(*argv, "dev") == 0) { |
| 281 | NEXT_ARG(); |
| 282 | d = *argv; |
| 283 | } else if (strcmp(*argv, "guard") == 0) { |
| 284 | NEXT_ARG(); |
| 285 | if (!on_off("guard", &bpdu_guard, *argv)) |
| 286 | exit(-1); |
| 287 | } else if (strcmp(*argv, "hairpin") == 0) { |
| 288 | NEXT_ARG(); |
| 289 | if (!on_off("hairping", &hairpin, *argv)) |
| 290 | exit(-1); |
| 291 | } else if (strcmp(*argv, "fastleave") == 0) { |
| 292 | NEXT_ARG(); |
| 293 | if (!on_off("fastleave", &fast_leave, *argv)) |
| 294 | exit(-1); |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 295 | } else if (strcmp(*argv, "root_block") == 0) { |
| 296 | NEXT_ARG(); |
| 297 | if (!on_off("root_block", &root_block, *argv)) |
| 298 | exit(-1); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 299 | } else if (strcmp(*argv, "learning") == 0) { |
| 300 | NEXT_ARG(); |
| 301 | if (!on_off("learning", &learning, *argv)) |
| 302 | exit(-1); |
Scott Feldman | 674bb43 | 2014-12-29 12:20:07 -0800 | [diff] [blame] | 303 | } else if (strcmp(*argv, "learning_sync") == 0) { |
| 304 | NEXT_ARG(); |
| 305 | if (!on_off("learning_sync", &learning_sync, *argv)) |
| 306 | exit(-1); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 307 | } else if (strcmp(*argv, "flood") == 0) { |
| 308 | NEXT_ARG(); |
| 309 | if (!on_off("flood", &flood, *argv)) |
| 310 | exit(-1); |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 311 | } else if (strcmp(*argv, "cost") == 0) { |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 312 | NEXT_ARG(); |
| 313 | cost = atoi(*argv); |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 314 | } else if (strcmp(*argv, "priority") == 0) { |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 315 | NEXT_ARG(); |
| 316 | priority = atoi(*argv); |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 317 | } else if (strcmp(*argv, "state") == 0) { |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 318 | NEXT_ARG(); |
| 319 | state = atoi(*argv); |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 320 | } else if (strcmp(*argv, "hwmode") == 0) { |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 321 | NEXT_ARG(); |
John Fastabend | a40d082 | 2013-05-29 06:20:53 +0000 | [diff] [blame] | 322 | flags = BRIDGE_FLAGS_SELF; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 323 | if (strcmp(*argv, "vepa") == 0) |
| 324 | mode = BRIDGE_MODE_VEPA; |
| 325 | else if (strcmp(*argv, "veb") == 0) |
| 326 | mode = BRIDGE_MODE_VEB; |
| 327 | else { |
| 328 | fprintf(stderr, |
| 329 | "Mode argument must be \"vepa\" or " |
| 330 | "\"veb\".\n"); |
| 331 | exit(-1); |
| 332 | } |
Roopa Prabhu | 6fdb465 | 2014-12-06 00:21:01 -0800 | [diff] [blame] | 333 | } else if (strcmp(*argv, "self") == 0) { |
| 334 | flags = BRIDGE_FLAGS_SELF; |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 335 | } else { |
| 336 | usage(); |
| 337 | } |
| 338 | argc--; argv++; |
| 339 | } |
| 340 | if (d == NULL) { |
| 341 | fprintf(stderr, "Device is a required argument.\n"); |
| 342 | exit(-1); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | req.ifm.ifi_index = ll_name_to_index(d); |
| 347 | if (req.ifm.ifi_index == 0) { |
| 348 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", d); |
| 349 | exit(-1); |
| 350 | } |
| 351 | |
| 352 | /* Nested PROTINFO attribute. Contains: port flags, cost, priority and |
| 353 | * state. |
| 354 | */ |
| 355 | nest = addattr_nest(&req.n, sizeof(req), |
| 356 | IFLA_PROTINFO | NLA_F_NESTED); |
| 357 | /* Flags first */ |
| 358 | if (bpdu_guard >= 0) |
| 359 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard); |
| 360 | if (hairpin >= 0) |
| 361 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin); |
| 362 | if (fast_leave >= 0) |
| 363 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE, |
| 364 | fast_leave); |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 365 | if (root_block >= 0) |
| 366 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_PROTECT, root_block); |
Vlad Yasevich | f0f4ab6 | 2014-05-21 09:53:43 -0400 | [diff] [blame] | 367 | if (flood >= 0) |
| 368 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_UNICAST_FLOOD, flood); |
| 369 | if (learning >= 0) |
| 370 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING, learning); |
Scott Feldman | 674bb43 | 2014-12-29 12:20:07 -0800 | [diff] [blame] | 371 | if (learning_sync >= 0) |
| 372 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_LEARNING_SYNC, |
| 373 | learning_sync); |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 374 | |
| 375 | if (cost > 0) |
| 376 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost); |
| 377 | |
| 378 | if (priority >= 0) |
| 379 | addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority); |
| 380 | |
| 381 | if (state >= 0) |
| 382 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state); |
| 383 | |
| 384 | addattr_nest_end(&req.n, nest); |
| 385 | |
Roopa Prabhu | 6fdb465 | 2014-12-06 00:21:01 -0800 | [diff] [blame] | 386 | /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that |
| 387 | * designates master or self operation and IFLA_BRIDGE_MODE |
| 388 | * for hw 'vepa' or 'veb' operation modes. The hwmodes are |
| 389 | * only valid in 'self' mode on some devices so far. |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 390 | */ |
Roopa Prabhu | 6fdb465 | 2014-12-06 00:21:01 -0800 | [diff] [blame] | 391 | if (mode >= 0 || flags > 0) { |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 392 | nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC); |
| 393 | |
Roopa Prabhu | 6fdb465 | 2014-12-06 00:21:01 -0800 | [diff] [blame] | 394 | if (flags > 0) |
| 395 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags); |
Vlad Yasevich | 6410890 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 396 | |
| 397 | if (mode >= 0) |
| 398 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode); |
| 399 | |
| 400 | addattr_nest_end(&req.n, nest); |
| 401 | } |
| 402 | |
| 403 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
| 404 | exit(2); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int brlink_show(int argc, char **argv) |
| 410 | { |
| 411 | char *filter_dev = NULL; |
| 412 | |
| 413 | while (argc > 0) { |
| 414 | if (strcmp(*argv, "dev") == 0) { |
| 415 | NEXT_ARG(); |
| 416 | if (filter_dev) |
| 417 | duparg("dev", *argv); |
| 418 | filter_dev = *argv; |
| 419 | } |
| 420 | argc--; argv++; |
| 421 | } |
| 422 | |
| 423 | if (filter_dev) { |
| 424 | if ((filter_index = ll_name_to_index(filter_dev)) == 0) { |
| 425 | fprintf(stderr, "Cannot find device \"%s\"\n", |
| 426 | filter_dev); |
| 427 | return -1; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) { |
| 432 | perror("Cannon send dump request"); |
| 433 | exit(1); |
| 434 | } |
| 435 | |
| 436 | if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) { |
| 437 | fprintf(stderr, "Dump terminated\n"); |
| 438 | exit(1); |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int do_link(int argc, char **argv) |
| 444 | { |
| 445 | ll_init_map(&rth); |
| 446 | if (argc > 0) { |
| 447 | if (matches(*argv, "set") == 0 || |
| 448 | matches(*argv, "change") == 0) |
| 449 | return brlink_modify(argc-1, argv+1); |
| 450 | if (matches(*argv, "show") == 0 || |
| 451 | matches(*argv, "lst") == 0 || |
| 452 | matches(*argv, "list") == 0) |
| 453 | return brlink_show(argc-1, argv+1); |
| 454 | if (matches(*argv, "help") == 0) |
| 455 | usage(); |
| 456 | } else |
| 457 | return brlink_show(0, NULL); |
| 458 | |
| 459 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv); |
| 460 | exit(-1); |
| 461 | } |