]>
Commit | Line | Data |
---|---|---|
d04bc300 SH |
1 | /* |
2 | * Get/set/delete bridge with netlink | |
3 | * | |
4 | * Authors: Stephen Hemminger <shemminger@vyatta.com> | |
5 | */ | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <unistd.h> | |
10 | #include <sys/socket.h> | |
11 | #include <string.h> | |
12 | ||
13 | #include "SNAPSHOT.h" | |
14 | #include "utils.h" | |
15 | #include "br_common.h" | |
527910c8 | 16 | #include "namespace.h" |
d04bc300 SH |
17 | |
18 | struct rtnl_handle rth = { .fd = -1 }; | |
0849e60a | 19 | int preferred_family = AF_UNSPEC; |
d04bc300 | 20 | int resolve_hosts; |
4cd20da1 | 21 | int oneline = 0; |
d04bc300 SH |
22 | int show_stats; |
23 | int show_details; | |
24 | int timestamp; | |
4cd20da1 | 25 | char * _SL_ = NULL; |
d04bc300 SH |
26 | |
27 | static void usage(void) __attribute__((noreturn)); | |
28 | ||
29 | static void usage(void) | |
30 | { | |
31 | fprintf(stderr, | |
90698170 | 32 | "Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }\n" |
64108901 | 33 | "where OBJECT := { link | fdb | mdb | vlan | monitor }\n" |
4cd20da1 | 34 | " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n" |
527910c8 | 35 | " -o[neline] | -t[imestamp] | -n[etns] name }\n"); |
d04bc300 SH |
36 | exit(-1); |
37 | } | |
38 | ||
39 | static int do_help(int argc, char **argv) | |
40 | { | |
41 | usage(); | |
42 | } | |
43 | ||
44 | ||
45 | static const struct cmd { | |
46 | const char *cmd; | |
47 | int (*func)(int argc, char **argv); | |
48 | } cmds[] = { | |
64108901 | 49 | { "link", do_link }, |
d04bc300 | 50 | { "fdb", do_fdb }, |
e06c7f7e | 51 | { "mdb", do_mdb }, |
9eff0e5c | 52 | { "vlan", do_vlan }, |
d04bc300 SH |
53 | { "monitor", do_monitor }, |
54 | { "help", do_help }, | |
55 | { 0 } | |
56 | }; | |
57 | ||
58 | static int do_cmd(const char *argv0, int argc, char **argv) | |
59 | { | |
60 | const struct cmd *c; | |
61 | ||
62 | for (c = cmds; c->cmd; ++c) { | |
63 | if (matches(argv0, c->cmd) == 0) | |
64 | return c->func(argc-1, argv+1); | |
65 | } | |
66 | ||
90698170 | 67 | fprintf(stderr, "Object \"%s\" is unknown, try \"bridge help\".\n", argv0); |
d04bc300 SH |
68 | return -1; |
69 | } | |
70 | ||
71 | int | |
72 | main(int argc, char **argv) | |
73 | { | |
74 | while (argc > 1) { | |
75 | char *opt = argv[1]; | |
76 | if (strcmp(opt,"--") == 0) { | |
77 | argc--; argv++; | |
78 | break; | |
79 | } | |
80 | if (opt[0] != '-') | |
81 | break; | |
82 | if (opt[1] == '-') | |
83 | opt++; | |
84 | ||
85 | if (matches(opt, "-help") == 0) { | |
86 | usage(); | |
87 | } else if (matches(opt, "-Version") == 0) { | |
90698170 | 88 | printf("bridge utility, 0.0\n"); |
d04bc300 SH |
89 | exit(0); |
90 | } else if (matches(opt, "-stats") == 0 || | |
91 | matches(opt, "-statistics") == 0) { | |
92 | ++show_stats; | |
93 | } else if (matches(opt, "-details") == 0) { | |
94 | ++show_details; | |
4cd20da1 SH |
95 | } else if (matches(opt, "-oneline") == 0) { |
96 | ++oneline; | |
d04bc300 SH |
97 | } else if (matches(opt, "-timestamp") == 0) { |
98 | ++timestamp; | |
0849e60a SH |
99 | } else if (matches(opt, "-family") == 0) { |
100 | argc--; | |
101 | argv++; | |
102 | if (argc <= 1) | |
103 | usage(); | |
104 | if (strcmp(argv[1], "inet") == 0) | |
105 | preferred_family = AF_INET; | |
106 | else if (strcmp(argv[1], "inet6") == 0) | |
107 | preferred_family = AF_INET6; | |
108 | else if (strcmp(argv[1], "help") == 0) | |
109 | usage(); | |
110 | else | |
111 | invarg("invalid protocol family", argv[1]); | |
112 | } else if (strcmp(opt, "-4") == 0) { | |
113 | preferred_family = AF_INET; | |
114 | } else if (strcmp(opt, "-6") == 0) { | |
115 | preferred_family = AF_INET6; | |
527910c8 VK |
116 | } else if (matches(opt, "-netns") == 0) { |
117 | NEXT_ARG(); | |
118 | if (netns_switch(argv[1])) | |
119 | exit(-1); | |
d04bc300 | 120 | } else { |
90698170 | 121 | fprintf(stderr, "Option \"%s\" is unknown, try \"bridge help\".\n", opt); |
d04bc300 SH |
122 | exit(-1); |
123 | } | |
124 | argc--; argv++; | |
125 | } | |
126 | ||
4cd20da1 SH |
127 | _SL_ = oneline ? "\\" : "\n" ; |
128 | ||
d04bc300 SH |
129 | if (rtnl_open(&rth, 0) < 0) |
130 | exit(1); | |
131 | ||
132 | if (argc > 1) | |
133 | return do_cmd(argv[1], argc-1, argv+1); | |
134 | ||
135 | rtnl_close(&rth); | |
136 | usage(); | |
137 | } |