]>
git.proxmox.com Git - mirror_iproute2.git/blob - bridge/bridge.c
1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Get/set/delete bridge with netlink
5 * Authors: Stephen Hemminger <shemminger@vyatta.com>
11 #include <sys/socket.h>
17 #include "br_common.h"
18 #include "namespace.h"
21 struct rtnl_handle rth
= { .fd
= -1 };
22 int preferred_family
= AF_UNSPEC
;
30 static const char *batch_file
;
33 static void usage(void) __attribute__((noreturn
));
35 static void usage(void)
38 "Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }\n"
39 " bridge [ -force ] -batch filename\n"
40 "where OBJECT := { link | fdb | mdb | vlan | monitor }\n"
41 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
42 " -o[neline] | -t[imestamp] | -n[etns] name |\n"
43 " -c[ompressvlans] -color -p[retty] -j[son] }\n");
47 static int do_help(int argc
, char **argv
)
53 static const struct cmd
{
55 int (*func
)(int argc
, char **argv
);
61 { "monitor", do_monitor
},
66 static int do_cmd(const char *argv0
, int argc
, char **argv
)
70 for (c
= cmds
; c
->cmd
; ++c
) {
71 if (matches(argv0
, c
->cmd
) == 0)
72 return c
->func(argc
-1, argv
+1);
76 "Object \"%s\" is unknown, try \"bridge help\".\n", argv0
);
80 static int batch(const char *name
)
84 int ret
= EXIT_SUCCESS
;
86 if (name
&& strcmp(name
, "-") != 0) {
87 if (freopen(name
, "r", stdin
) == NULL
) {
89 "Cannot open file \"%s\" for reading: %s\n",
90 name
, strerror(errno
));
95 if (rtnl_open(&rth
, 0) < 0) {
96 fprintf(stderr
, "Cannot open rtnetlink\n");
100 rtnl_set_strict_dump(&rth
);
103 while (getcmdline(&line
, &len
, stdin
) != -1) {
107 largc
= makeargs(line
, largv
, 100);
109 continue; /* blank line */
111 if (do_cmd(largv
[0], largc
, largv
)) {
112 fprintf(stderr
, "Command failed %s:%d\n",
127 main(int argc
, char **argv
)
130 const char *opt
= argv
[1];
132 if (strcmp(opt
, "--") == 0) {
141 if (matches(opt
, "-help") == 0) {
143 } else if (matches(opt
, "-Version") == 0) {
144 printf("bridge utility, 0.0\n");
146 } else if (matches(opt
, "-stats") == 0 ||
147 matches(opt
, "-statistics") == 0) {
149 } else if (matches(opt
, "-details") == 0) {
151 } else if (matches(opt
, "-oneline") == 0) {
153 } else if (matches(opt
, "-timestamp") == 0) {
155 } else if (matches(opt
, "-family") == 0) {
160 if (strcmp(argv
[1], "inet") == 0)
161 preferred_family
= AF_INET
;
162 else if (strcmp(argv
[1], "inet6") == 0)
163 preferred_family
= AF_INET6
;
164 else if (strcmp(argv
[1], "help") == 0)
167 invarg("invalid protocol family", argv
[1]);
168 } else if (strcmp(opt
, "-4") == 0) {
169 preferred_family
= AF_INET
;
170 } else if (strcmp(opt
, "-6") == 0) {
171 preferred_family
= AF_INET6
;
172 } else if (matches(opt
, "-netns") == 0) {
174 if (netns_switch(argv
[1]))
176 } else if (matches(opt
, "-compressvlans") == 0) {
178 } else if (matches_color(opt
, &color
)) {
179 } else if (matches(opt
, "-force") == 0) {
181 } else if (matches(opt
, "-json") == 0) {
183 } else if (matches(opt
, "-pretty") == 0) {
185 } else if (matches(opt
, "-batch") == 0) {
190 batch_file
= argv
[1];
193 "Option \"%s\" is unknown, try \"bridge help\".\n",
200 _SL_
= oneline
? "\\" : "\n";
202 check_enable_color(color
, json
);
205 return batch(batch_file
);
207 if (rtnl_open(&rth
, 0) < 0)
210 rtnl_set_strict_dump(&rth
);
213 return do_cmd(argv
[1], argc
-1, argv
+1);