blob: 61e4cdaba4c1af3e3dfcf1b7b8a32832237e754d [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();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000045 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070046 invarg("invalid forward_delay", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000047
Jiri Pirko28d84b42014-09-28 16:33:29 -070048 addattr32(n, 1024, IFLA_BR_FORWARD_DELAY, val);
49 } else if (matches(*argv, "hello_time") == 0) {
50 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000051 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070052 invarg("invalid hello_time", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000053
Jiri Pirko28d84b42014-09-28 16:33:29 -070054 addattr32(n, 1024, IFLA_BR_HELLO_TIME, val);
55 } else if (matches(*argv, "max_age") == 0) {
56 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000057 if (get_u32(&val, *argv, 0))
Jiri Pirko28d84b42014-09-28 16:33:29 -070058 invarg("invalid max_age", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000059
Jiri Pirko28d84b42014-09-28 16:33:29 -070060 addattr32(n, 1024, IFLA_BR_MAX_AGE, val);
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030061 } else if (matches(*argv, "ageing_time") == 0) {
62 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000063 if (get_u32(&val, *argv, 0))
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030064 invarg("invalid ageing_time", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000065
Nikolay Aleksandrov6c99fb62015-06-16 13:38:47 +030066 addattr32(n, 1024, IFLA_BR_AGEING_TIME, val);
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030067 } else if (matches(*argv, "stp_state") == 0) {
68 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000069 if (get_u32(&val, *argv, 0))
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030070 invarg("invalid stp_state", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000071
Nikolay Aleksandrovdab04962015-06-16 13:38:48 +030072 addattr32(n, 1024, IFLA_BR_STP_STATE, val);
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030073 } else if (matches(*argv, "priority") == 0) {
74 __u16 prio;
75
76 NEXT_ARG();
Zhang Shengju6a9ce302015-08-13 07:48:15 +000077 if (get_u16(&prio, *argv, 0))
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030078 invarg("invalid priority", *argv);
Zhang Shengju6a9ce302015-08-13 07:48:15 +000079
Nikolay Aleksandrovb0197a02015-06-16 13:38:49 +030080 addattr16(n, 1024, IFLA_BR_PRIORITY, prio);
Jiri Pirko28d84b42014-09-28 16:33:29 -070081 } else if (matches(*argv, "help") == 0) {
82 explain();
83 return -1;
84 } else {
85 fprintf(stderr, "bridge: unknown command \"%s\"?\n", *argv);
86 explain();
87 return -1;
88 }
89 argc--, argv++;
90 }
91
92 return 0;
93}
94
95static void bridge_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
96{
97 if (!tb)
98 return;
99
100 if (tb[IFLA_BR_FORWARD_DELAY])
101 fprintf(f, "forward_delay %u ",
102 rta_getattr_u32(tb[IFLA_BR_FORWARD_DELAY]));
103
104 if (tb[IFLA_BR_HELLO_TIME])
105 fprintf(f, "hello_time %u ",
106 rta_getattr_u32(tb[IFLA_BR_HELLO_TIME]));
107
108 if (tb[IFLA_BR_MAX_AGE])
109 fprintf(f, "max_age %u ",
110 rta_getattr_u32(tb[IFLA_BR_MAX_AGE]));
Nikolay Aleksandrovfdba0512015-08-12 09:11:30 -0700111
112 if (tb[IFLA_BR_AGEING_TIME])
113 fprintf(f, "ageing_time %u ",
114 rta_getattr_u32(tb[IFLA_BR_AGEING_TIME]));
115
116 if (tb[IFLA_BR_STP_STATE])
117 fprintf(f, "stp_state %u ",
118 rta_getattr_u32(tb[IFLA_BR_STP_STATE]));
119
120 if (tb[IFLA_BR_PRIORITY])
121 fprintf(f, "priority %u ",
122 rta_getattr_u16(tb[IFLA_BR_PRIORITY]));
Jiri Pirko28d84b42014-09-28 16:33:29 -0700123}
124
Zhang Shengju43367ef2015-08-12 06:03:23 +0000125static void bridge_print_help(struct link_util *lu, int argc, char **argv,
126 FILE *f)
127{
128 print_explain(f);
129}
130
Jiri Pirko28d84b42014-09-28 16:33:29 -0700131struct link_util bridge_link_util = {
132 .id = "bridge",
133 .maxattr = IFLA_BR_MAX,
134 .parse_opt = bridge_parse_opt,
135 .print_opt = bridge_print_opt,
Zhang Shengju43367ef2015-08-12 06:03:23 +0000136 .print_help = bridge_print_help,
Jiri Pirko28d84b42014-09-28 16:33:29 -0700137};