add "swconfig list" support
[openwrt/openwrt.git] / package / network / config / 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 list\n");
171 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
172 exit(1);
173 }
174
175 static void
176 swconfig_load_uci(struct switch_dev *dev, const char *name)
177 {
178 struct uci_context *ctx;
179 struct uci_package *p = NULL;
180 struct uci_element *e;
181 int ret = -1;
182
183 ctx = uci_alloc_context();
184 if (!ctx)
185 return;
186
187 uci_load(ctx, name, &p);
188 if (!p) {
189 uci_perror(ctx, "Failed to load config file: ");
190 goto out;
191 }
192
193 ret = swlib_apply_from_uci(dev, p);
194 if (ret < 0)
195 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
196
197 out:
198 uci_free_context(ctx);
199 exit(ret);
200 }
201
202 int main(int argc, char **argv)
203 {
204 int retval = 0;
205 struct switch_dev *dev;
206 struct switch_attr *a;
207 struct switch_val val;
208 int err;
209 int i;
210
211 int cmd = CMD_NONE;
212 char *cdev = NULL;
213 int cport = -1;
214 int cvlan = -1;
215 char *ckey = NULL;
216 char *cvalue = NULL;
217
218 if((argc == 2) && !strcmp(argv[1], "list")) {
219 swlib_list();
220 return 0;
221 }
222
223 if(argc < 4)
224 print_usage();
225
226 if(strcmp(argv[1], "dev"))
227 print_usage();
228
229 cdev = argv[2];
230
231 for(i = 3; i < argc; i++)
232 {
233 char *arg = argv[i];
234 if (cmd != CMD_NONE) {
235 print_usage();
236 } else if (!strcmp(arg, "port") && i+1 < argc) {
237 cport = atoi(argv[++i]);
238 } else if (!strcmp(arg, "vlan") && i+1 < argc) {
239 cvlan = atoi(argv[++i]);
240 } else if (!strcmp(arg, "help")) {
241 cmd = CMD_HELP;
242 } else if (!strcmp(arg, "set") && i+1 < argc) {
243 cmd = CMD_SET;
244 ckey = argv[++i];
245 if (i+1 < argc)
246 cvalue = argv[++i];
247 } else if (!strcmp(arg, "get") && i+1 < argc) {
248 cmd = CMD_GET;
249 ckey = argv[++i];
250 } else if (!strcmp(arg, "load") && i+1 < argc) {
251 if ((cport >= 0) || (cvlan >= 0))
252 print_usage();
253 cmd = CMD_LOAD;
254 ckey = argv[++i];
255 } else if (!strcmp(arg, "show")) {
256 cmd = CMD_SHOW;
257 } else {
258 print_usage();
259 }
260 }
261
262 if (cmd == CMD_NONE)
263 print_usage();
264 if (cport > -1 && cvlan > -1)
265 print_usage();
266
267 dev = swlib_connect(cdev);
268 if (!dev) {
269 fprintf(stderr, "Failed to connect to the switch\n");
270 return 1;
271 }
272
273 swlib_scan(dev);
274
275 if (cmd == CMD_GET || cmd == CMD_SET) {
276 if(cport > -1)
277 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
278 else if(cvlan > -1)
279 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
280 else
281 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
282
283 if(!a)
284 {
285 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
286 goto out;
287 }
288 }
289
290 switch(cmd)
291 {
292 case CMD_SET:
293 if ((a->type != SWITCH_TYPE_NOVAL) &&
294 (cvalue == NULL))
295 print_usage();
296
297 if(cvlan > -1)
298 cport = cvlan;
299
300 if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
301 {
302 fprintf(stderr, "failed\n");
303 retval = -1;
304 goto out;
305 }
306 break;
307 case CMD_GET:
308 if(cvlan > -1)
309 val.port_vlan = cvlan;
310 if(cport > -1)
311 val.port_vlan = cport;
312 if(swlib_get_attr(dev, a, &val) < 0)
313 {
314 fprintf(stderr, "failed\n");
315 retval = -1;
316 goto out;
317 }
318 print_attr_val(a, &val);
319 putchar('\n');
320 break;
321 case CMD_LOAD:
322 swconfig_load_uci(dev, ckey);
323 break;
324 case CMD_HELP:
325 list_attributes(dev);
326 break;
327 case CMD_SHOW:
328 if (cport >= 0 || cvlan >= 0) {
329 if (cport >= 0)
330 show_port(dev, cport);
331 else
332 show_vlan(dev, cvlan, false);
333 } else {
334 show_global(dev);
335 for (i=0; i < dev->ports; i++)
336 show_port(dev, i);
337 for (i=0; i < dev->vlans; i++)
338 show_vlan(dev, i, true);
339 }
340 break;
341 }
342
343 out:
344 swlib_free_all(dev);
345 return 0;
346 }