samba36: enable parallel build
[openwrt/svn-archive/archive.git] / package / swconfig / src / cli.c
1 /*
2 * swconfig.c: Switch configuration utility
3 *
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2010 Martin Mares <mj@ucw.cz>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundatio.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <inttypes.h>
21 #include <errno.h>
22 #include <stdint.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <uci.h>
27
28 #include <linux/types.h>
29 #include <linux/netlink.h>
30 #include <linux/genetlink.h>
31 #include <netlink/netlink.h>
32 #include <netlink/genl/genl.h>
33 #include <netlink/genl/ctrl.h>
34 #include <linux/switch.h>
35 #include "swlib.h"
36
37 enum {
38 CMD_NONE,
39 CMD_GET,
40 CMD_SET,
41 CMD_LOAD,
42 CMD_HELP,
43 CMD_SHOW,
44 };
45
46 static void
47 print_attrs(const struct switch_attr *attr)
48 {
49 int i = 0;
50 while (attr) {
51 const char *type;
52 switch(attr->type) {
53 case SWITCH_TYPE_INT:
54 type = "int";
55 break;
56 case SWITCH_TYPE_STRING:
57 type = "string";
58 break;
59 case SWITCH_TYPE_PORTS:
60 type = "ports";
61 break;
62 case SWITCH_TYPE_NOVAL:
63 type = "none";
64 break;
65 default:
66 type = "unknown";
67 break;
68 }
69 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
70 attr = attr->next;
71 }
72 }
73
74 static void
75 list_attributes(struct switch_dev *dev)
76 {
77 printf("%s: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev->dev_name, dev->alias, dev->name, dev->ports, dev->cpu_port, dev->vlans);
78 printf(" --switch\n");
79 print_attrs(dev->ops);
80 printf(" --vlan\n");
81 print_attrs(dev->vlan_ops);
82 printf(" --port\n");
83 print_attrs(dev->port_ops);
84 }
85
86 static void
87 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
88 {
89 int i;
90
91 switch (attr->type) {
92 case SWITCH_TYPE_INT:
93 printf("%d", val->value.i);
94 break;
95 case SWITCH_TYPE_STRING:
96 printf("%s", val->value.s);
97 break;
98 case SWITCH_TYPE_PORTS:
99 for(i = 0; i < val->len; i++) {
100 printf("%d%s ",
101 val->value.ports[i].id,
102 (val->value.ports[i].flags &
103 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
104 }
105 break;
106 default:
107 printf("?unknown-type?");
108 }
109 }
110
111 static void
112 show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
113 {
114 while (attr) {
115 if (attr->type != SWITCH_TYPE_NOVAL) {
116 printf("\t%s: ", attr->name);
117 if (swlib_get_attr(dev, attr, val) < 0)
118 printf("???");
119 else
120 print_attr_val(attr, val);
121 putchar('\n');
122 }
123 attr = attr->next;
124 }
125 }
126
127 static void
128 show_global(struct switch_dev *dev)
129 {
130 struct switch_val val;
131
132 printf("Global attributes:\n");
133 show_attrs(dev, dev->ops, &val);
134 }
135
136 static void
137 show_port(struct switch_dev *dev, int port)
138 {
139 struct switch_val val;
140
141 printf("Port %d:\n", port);
142 val.port_vlan = port;
143 show_attrs(dev, dev->port_ops, &val);
144 }
145
146 static void
147 show_vlan(struct switch_dev *dev, int vlan, bool all)
148 {
149 struct switch_val val;
150 struct switch_attr *attr;
151
152 val.port_vlan = vlan;
153
154 if (all) {
155 attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports");
156 if (swlib_get_attr(dev, attr, &val) < 0)
157 return;
158
159 if (!val.len)
160 return;
161 }
162
163 printf("VLAN %d:\n", vlan);
164 show_attrs(dev, dev->vlan_ops, &val);
165 }
166
167 static void
168 print_usage(void)
169 {
170 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
171 exit(1);
172 }
173
174 static void
175 swconfig_load_uci(struct switch_dev *dev, const char *name)
176 {
177 struct uci_context *ctx;
178 struct uci_package *p = NULL;
179 struct uci_element *e;
180 int ret = -1;
181
182 ctx = uci_alloc_context();
183 if (!ctx)
184 return;
185
186 uci_load(ctx, name, &p);
187 if (!p) {
188 uci_perror(ctx, "Failed to load config file: ");
189 goto out;
190 }
191
192 ret = swlib_apply_from_uci(dev, p);
193 if (ret < 0)
194 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
195
196 out:
197 uci_free_context(ctx);
198 exit(ret);
199 }
200
201 int main(int argc, char **argv)
202 {
203 int retval = 0;
204 struct switch_dev *dev;
205 struct switch_attr *a;
206 struct switch_val val;
207 int err;
208 int i;
209
210 int cmd = CMD_NONE;
211 char *cdev = NULL;
212 int cport = -1;
213 int cvlan = -1;
214 char *ckey = NULL;
215 char *cvalue = NULL;
216
217 if(argc < 4)
218 print_usage();
219
220 if(strcmp(argv[1], "dev"))
221 print_usage();
222
223 cdev = argv[2];
224
225 for(i = 3; i < argc; i++)
226 {
227 char *arg = argv[i];
228 if (cmd != CMD_NONE) {
229 print_usage();
230 } else if (!strcmp(arg, "port") && i+1 < argc) {
231 cport = atoi(argv[++i]);
232 } else if (!strcmp(arg, "vlan") && i+1 < argc) {
233 cvlan = atoi(argv[++i]);
234 } else if (!strcmp(arg, "help")) {
235 cmd = CMD_HELP;
236 } else if (!strcmp(arg, "set") && i+1 < argc) {
237 cmd = CMD_SET;
238 ckey = argv[++i];
239 if (i+1 < argc)
240 cvalue = argv[++i];
241 } else if (!strcmp(arg, "get") && i+1 < argc) {
242 cmd = CMD_GET;
243 ckey = argv[++i];
244 } else if (!strcmp(arg, "load") && i+1 < argc) {
245 if ((cport >= 0) || (cvlan >= 0))
246 print_usage();
247 cmd = CMD_LOAD;
248 ckey = argv[++i];
249 } else if (!strcmp(arg, "show")) {
250 cmd = CMD_SHOW;
251 } else {
252 print_usage();
253 }
254 }
255
256 if (cmd == CMD_NONE)
257 print_usage();
258 if (cport > -1 && cvlan > -1)
259 print_usage();
260
261 dev = swlib_connect(cdev);
262 if (!dev) {
263 fprintf(stderr, "Failed to connect to the switch\n");
264 return 1;
265 }
266
267 swlib_scan(dev);
268
269 if (cmd == CMD_GET || cmd == CMD_SET) {
270 if(cport > -1)
271 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
272 else if(cvlan > -1)
273 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
274 else
275 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
276
277 if(!a)
278 {
279 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
280 goto out;
281 }
282 }
283
284 switch(cmd)
285 {
286 case CMD_SET:
287 if ((a->type != SWITCH_TYPE_NOVAL) &&
288 (cvalue == NULL))
289 print_usage();
290
291 if(cvlan > -1)
292 cport = cvlan;
293
294 if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
295 {
296 fprintf(stderr, "failed\n");
297 retval = -1;
298 goto out;
299 }
300 break;
301 case CMD_GET:
302 if(cvlan > -1)
303 val.port_vlan = cvlan;
304 if(cport > -1)
305 val.port_vlan = cport;
306 if(swlib_get_attr(dev, a, &val) < 0)
307 {
308 fprintf(stderr, "failed\n");
309 retval = -1;
310 goto out;
311 }
312 print_attr_val(a, &val);
313 putchar('\n');
314 break;
315 case CMD_LOAD:
316 swconfig_load_uci(dev, ckey);
317 break;
318 case CMD_HELP:
319 list_attributes(dev);
320 break;
321 case CMD_SHOW:
322 if (cport >= 0 || cvlan >= 0) {
323 if (cport >= 0)
324 show_port(dev, cport);
325 else
326 show_vlan(dev, cvlan, false);
327 } else {
328 show_global(dev);
329 for (i=0; i < dev->ports; i++)
330 show_port(dev, i);
331 for (i=0; i < dev->vlans; i++)
332 show_vlan(dev, i, true);
333 }
334 break;
335 }
336
337 out:
338 swlib_free_all(dev);
339 return 0;
340 }