blob: e704e290ce4194278762eb584d09287fec5c3ec5 [file] [log] [blame]
Jiri Pirko28d84b42014-09-28 16:33:29 -07001/*
2 * iplink_bridge.c Bridge 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 <stdlib.h>
14#include <string.h>
15#include <linux/if_link.h>
16
17#include "utils.h"
18#include "ip_common.h"
19
Zhang Shengju43367ef2015-08-12 06:03:23 +000020static void print_explain(FILE *f)
Jiri Pirko28d84b42014-09-28 16:33:29 -070021{
Zhang Shengju43367ef2015-08-12 06:03:23 +000022 fprintf(f,
Jiri Pirko28d84b42014-09-28 16:33:29 -070023 "Usage: ... bridge [ forward_delay FORWARD_DELAY ]\n"
24 " [ hello_time HELLO_TIME ]\n"
25 " [ max_age MAX_AGE ]\n"
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030026 " [ ageing_time AGEING_TIME ]\n"
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030027 " [ stp_state STP_STATE ]\n"
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030028 " [ priority PRIORITY ]\n"
Jiri Pirko28d84b42014-09-28 16:33:29 -070029 );
30}
31
Zhang Shengju43367ef2015-08-12 06:03:23 +000032static void explain(void)
33{
34 print_explain(stderr);
35}
36
Jiri Pirko28d84b42014-09-28 16:33:29 -070037static int bridge_parse_opt(struct link_util *lu, int argc, char **argv,
38 struct nlmsghdr *n)
39{
40 __u32 val;
41
42 while (argc > 0) {
43 if (matches(*argv, "forward_delay") == 0) {
44 NEXT_ARG();
45 if (get_u32(&val, *argv, 0)) {
46 invarg("invalid forward_delay", *argv);
47 return -1;
48 }
49 addattr32(n, 1024, IFLA_BR_FORWARD_DELAY, val);
50 } else if (matches(*argv, "hello_time") == 0) {
51 NEXT_ARG();
52 if (get_u32(&val, *argv, 0)) {
53 invarg("invalid hello_time", *argv);
54 return -1;
55 }
56 addattr32(n, 1024, IFLA_BR_HELLO_TIME, val);
57 } else if (matches(*argv, "max_age") == 0) {
58 NEXT_ARG();
59 if (get_u32(&val, *argv, 0)) {
60 invarg("invalid max_age", *argv);
61 return -1;
62 }
63 addattr32(n, 1024, IFLA_BR_MAX_AGE, val);
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030064 } else if (matches(*argv, "ageing_time") == 0) {
65 NEXT_ARG();
66 if (get_u32(&val, *argv, 0)) {
67 invarg("invalid ageing_time", *argv);
68 return -1;
69 }
70 addattr32(n, 1024, IFLA_BR_AGEING_TIME, val);
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030071 } else if (matches(*argv, "stp_state") == 0) {
72 NEXT_ARG();
73 if (get_u32(&val, *argv, 0)) {
74 invarg("invalid stp_state", *argv);
75 return -1;
76 }
77 addattr32(n, 1024, IFLA_BR_STP_STATE, val);
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030078 } else if (matches(*argv, "priority") == 0) {
79 __u16 prio;
80
81 NEXT_ARG();
82 if (get_u16(&prio, *argv, 0)) {
83 invarg("invalid priority", *argv);
84 return -1;
85 }
86 addattr16(n, 1024, IFLA_BR_PRIORITY, prio);
Jiri Pirko28d84b42014-09-28 16:33:29 -070087 } else if (matches(*argv, "help") == 0) {
88 explain();
89 return -1;
90 } else {
91 fprintf(stderr, "bridge: unknown command \"%s\"?\n", *argv);
92 explain();
93 return -1;
94 }
95 argc--, argv++;
96 }
97
98 return 0;
99}
100
101static void bridge_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
102{
103 if (!tb)
104 return;
105
106 if (tb[IFLA_BR_FORWARD_DELAY])
107 fprintf(f, "forward_delay %u ",
108 rta_getattr_u32(tb[IFLA_BR_FORWARD_DELAY]));
109
110 if (tb[IFLA_BR_HELLO_TIME])
111 fprintf(f, "hello_time %u ",
112 rta_getattr_u32(tb[IFLA_BR_HELLO_TIME]));
113
114 if (tb[IFLA_BR_MAX_AGE])
115 fprintf(f, "max_age %u ",
116 rta_getattr_u32(tb[IFLA_BR_MAX_AGE]));
Nikolay Aleksandrovfdba0512015-08-12 09:11:30 -0700117
118 if (tb[IFLA_BR_AGEING_TIME])
119 fprintf(f, "ageing_time %u ",
120 rta_getattr_u32(tb[IFLA_BR_AGEING_TIME]));
121
122 if (tb[IFLA_BR_STP_STATE])
123 fprintf(f, "stp_state %u ",
124 rta_getattr_u32(tb[IFLA_BR_STP_STATE]));
125
126 if (tb[IFLA_BR_PRIORITY])
127 fprintf(f, "priority %u ",
128 rta_getattr_u16(tb[IFLA_BR_PRIORITY]));
Jiri Pirko28d84b42014-09-28 16:33:29 -0700129}
130
Zhang Shengju43367ef2015-08-12 06:03:23 +0000131static void bridge_print_help(struct link_util *lu, int argc, char **argv,
132 FILE *f)
133{
134 print_explain(f);
135}
136
Jiri Pirko28d84b42014-09-28 16:33:29 -0700137struct link_util bridge_link_util = {
138 .id = "bridge",
139 .maxattr = IFLA_BR_MAX,
140 .parse_opt = bridge_parse_opt,
141 .print_opt = bridge_print_opt,
Zhang Shengju43367ef2015-08-12 06:03:23 +0000142 .print_help = bridge_print_help,
Jiri Pirko28d84b42014-09-28 16:33:29 -0700143};