]> git.proxmox.com Git - mirror_iproute2.git/blob - bridge/bridge.c
rdma: Properly mark RDMAtool license
[mirror_iproute2.git] / bridge / bridge.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Get/set/delete bridge with netlink
4 *
5 * Authors: Stephen Hemminger <shemminger@vyatta.com>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/socket.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "SNAPSHOT.h"
16 #include "utils.h"
17 #include "br_common.h"
18 #include "namespace.h"
19 #include "color.h"
20
21 struct rtnl_handle rth = { .fd = -1 };
22 int preferred_family = AF_UNSPEC;
23 int oneline;
24 int show_stats;
25 int show_details;
26 static int color;
27 int compress_vlans;
28 int json;
29 int timestamp;
30 static const char *batch_file;
31 int force;
32
33 static void usage(void) __attribute__((noreturn));
34
35 static void usage(void)
36 {
37 fprintf(stderr,
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");
44 exit(-1);
45 }
46
47 static int do_help(int argc, char **argv)
48 {
49 usage();
50 }
51
52
53 static const struct cmd {
54 const char *cmd;
55 int (*func)(int argc, char **argv);
56 } cmds[] = {
57 { "link", do_link },
58 { "fdb", do_fdb },
59 { "mdb", do_mdb },
60 { "vlan", do_vlan },
61 { "monitor", do_monitor },
62 { "help", do_help },
63 { 0 }
64 };
65
66 static int do_cmd(const char *argv0, int argc, char **argv)
67 {
68 const struct cmd *c;
69
70 for (c = cmds; c->cmd; ++c) {
71 if (matches(argv0, c->cmd) == 0)
72 return c->func(argc-1, argv+1);
73 }
74
75 fprintf(stderr,
76 "Object \"%s\" is unknown, try \"bridge help\".\n", argv0);
77 return -1;
78 }
79
80 static int batch(const char *name)
81 {
82 char *line = NULL;
83 size_t len = 0;
84 int ret = EXIT_SUCCESS;
85
86 if (name && strcmp(name, "-") != 0) {
87 if (freopen(name, "r", stdin) == NULL) {
88 fprintf(stderr,
89 "Cannot open file \"%s\" for reading: %s\n",
90 name, strerror(errno));
91 return EXIT_FAILURE;
92 }
93 }
94
95 if (rtnl_open(&rth, 0) < 0) {
96 fprintf(stderr, "Cannot open rtnetlink\n");
97 return EXIT_FAILURE;
98 }
99
100 rtnl_set_strict_dump(&rth);
101
102 cmdlineno = 0;
103 while (getcmdline(&line, &len, stdin) != -1) {
104 char *largv[100];
105 int largc;
106
107 largc = makeargs(line, largv, 100);
108 if (largc == 0)
109 continue; /* blank line */
110
111 if (do_cmd(largv[0], largc, largv)) {
112 fprintf(stderr, "Command failed %s:%d\n",
113 name, cmdlineno);
114 ret = EXIT_FAILURE;
115 if (!force)
116 break;
117 }
118 }
119 if (line)
120 free(line);
121
122 rtnl_close(&rth);
123 return ret;
124 }
125
126 int
127 main(int argc, char **argv)
128 {
129 while (argc > 1) {
130 const char *opt = argv[1];
131
132 if (strcmp(opt, "--") == 0) {
133 argc--; argv++;
134 break;
135 }
136 if (opt[0] != '-')
137 break;
138 if (opt[1] == '-')
139 opt++;
140
141 if (matches(opt, "-help") == 0) {
142 usage();
143 } else if (matches(opt, "-Version") == 0) {
144 printf("bridge utility, 0.0\n");
145 exit(0);
146 } else if (matches(opt, "-stats") == 0 ||
147 matches(opt, "-statistics") == 0) {
148 ++show_stats;
149 } else if (matches(opt, "-details") == 0) {
150 ++show_details;
151 } else if (matches(opt, "-oneline") == 0) {
152 ++oneline;
153 } else if (matches(opt, "-timestamp") == 0) {
154 ++timestamp;
155 } else if (matches(opt, "-family") == 0) {
156 argc--;
157 argv++;
158 if (argc <= 1)
159 usage();
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)
165 usage();
166 else
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) {
173 NEXT_ARG();
174 if (netns_switch(argv[1]))
175 exit(-1);
176 } else if (matches(opt, "-compressvlans") == 0) {
177 ++compress_vlans;
178 } else if (matches_color(opt, &color)) {
179 } else if (matches(opt, "-force") == 0) {
180 ++force;
181 } else if (matches(opt, "-json") == 0) {
182 ++json;
183 } else if (matches(opt, "-pretty") == 0) {
184 ++pretty;
185 } else if (matches(opt, "-batch") == 0) {
186 argc--;
187 argv++;
188 if (argc <= 1)
189 usage();
190 batch_file = argv[1];
191 } else {
192 fprintf(stderr,
193 "Option \"%s\" is unknown, try \"bridge help\".\n",
194 opt);
195 exit(-1);
196 }
197 argc--; argv++;
198 }
199
200 _SL_ = oneline ? "\\" : "\n";
201
202 check_enable_color(color, json);
203
204 if (batch_file)
205 return batch(batch_file);
206
207 if (rtnl_open(&rth, 0) < 0)
208 exit(1);
209
210 rtnl_set_strict_dump(&rth);
211
212 if (argc > 1)
213 return do_cmd(argv[1], argc-1, argv+1);
214
215 rtnl_close(&rth);
216 usage();
217 }