28b7ed8625cb77fc633daeb2139ff8b23bb52698
[openwrt/staging/florian.git] / package / network / config / swconfig / src / cli.c
1 /*
2 * swconfig.c: Switch configuration utility
3 *
4 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
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 const char *
87 speed_str(int speed)
88 {
89 switch (speed) {
90 case 10:
91 return "10baseT";
92 case 100:
93 return "100baseT";
94 case 1000:
95 return "1000baseT";
96 default:
97 break;
98 }
99
100 return "unknown";
101 }
102
103 static void
104 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
105 {
106 struct switch_port_link *link;
107 int i;
108
109 switch (attr->type) {
110 case SWITCH_TYPE_INT:
111 printf("%d", val->value.i);
112 break;
113 case SWITCH_TYPE_STRING:
114 printf("%s", val->value.s);
115 break;
116 case SWITCH_TYPE_PORTS:
117 for(i = 0; i < val->len; i++) {
118 printf("%d%s ",
119 val->value.ports[i].id,
120 (val->value.ports[i].flags &
121 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
122 }
123 break;
124 case SWITCH_TYPE_LINK:
125 link = val->value.link;
126 if (link->link)
127 printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
128 val->port_vlan,
129 speed_str(link->speed),
130 link->duplex ? "full" : "half",
131 link->tx_flow ? "txflow " : "",
132 link->rx_flow ? "rxflow " : "",
133 link->eee & SWLIB_LINK_FLAG_EEE_100BASET ? "eee100 " : "",
134 link->eee & SWLIB_LINK_FLAG_EEE_1000BASET ? "eee1000 " : "",
135 link->aneg ? "auto" : "");
136 else
137 printf("port:%d link:down", val->port_vlan);
138 break;
139 default:
140 printf("?unknown-type?");
141 }
142 }
143
144 static void
145 show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
146 {
147 while (attr) {
148 if (attr->type != SWITCH_TYPE_NOVAL) {
149 printf("\t%s: ", attr->name);
150 if (swlib_get_attr(dev, attr, val) < 0)
151 printf("???");
152 else
153 print_attr_val(attr, val);
154 putchar('\n');
155 }
156 attr = attr->next;
157 }
158 }
159
160 static void
161 show_global(struct switch_dev *dev)
162 {
163 struct switch_val val;
164
165 printf("Global attributes:\n");
166 show_attrs(dev, dev->ops, &val);
167 }
168
169 static void
170 show_port(struct switch_dev *dev, int port)
171 {
172 struct switch_val val;
173
174 printf("Port %d:\n", port);
175 val.port_vlan = port;
176 show_attrs(dev, dev->port_ops, &val);
177 }
178
179 static void
180 show_vlan(struct switch_dev *dev, int vlan, bool all)
181 {
182 struct switch_val val;
183 struct switch_attr *attr;
184
185 val.port_vlan = vlan;
186
187 if (all) {
188 attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports");
189 if (swlib_get_attr(dev, attr, &val) < 0)
190 return;
191
192 if (!val.len)
193 return;
194 }
195
196 printf("VLAN %d:\n", vlan);
197 show_attrs(dev, dev->vlan_ops, &val);
198 }
199
200 static void
201 print_usage(void)
202 {
203 printf("swconfig list\n");
204 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
205 exit(1);
206 }
207
208 static void
209 swconfig_load_uci(struct switch_dev *dev, const char *name)
210 {
211 struct uci_context *ctx;
212 struct uci_package *p = NULL;
213 int ret = -1;
214
215 ctx = uci_alloc_context();
216 if (!ctx)
217 return;
218
219 uci_load(ctx, name, &p);
220 if (!p) {
221 uci_perror(ctx, "Failed to load config file: ");
222 goto out;
223 }
224
225 ret = swlib_apply_from_uci(dev, p);
226 if (ret < 0)
227 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
228
229 out:
230 uci_free_context(ctx);
231 exit(ret);
232 }
233
234 int main(int argc, char **argv)
235 {
236 int retval = 0;
237 struct switch_dev *dev;
238 struct switch_attr *a;
239 struct switch_val val;
240 int i;
241
242 int cmd = CMD_NONE;
243 char *cdev = NULL;
244 int cport = -1;
245 int cvlan = -1;
246 char *ckey = NULL;
247 char *cvalue = NULL;
248 char *csegment = NULL;
249
250 if((argc == 2) && !strcmp(argv[1], "list")) {
251 swlib_list();
252 return 0;
253 }
254
255 if(argc < 4)
256 print_usage();
257
258 if(strcmp(argv[1], "dev"))
259 print_usage();
260
261 cdev = argv[2];
262
263 for(i = 3; i < argc; i++)
264 {
265 char *arg = argv[i];
266 if (cmd != CMD_NONE) {
267 print_usage();
268 } else if (!strcmp(arg, "port") && i+1 < argc) {
269 cport = atoi(argv[++i]);
270 } else if (!strcmp(arg, "vlan") && i+1 < argc) {
271 cvlan = atoi(argv[++i]);
272 } else if (!strcmp(arg, "help")) {
273 cmd = CMD_HELP;
274 } else if (!strcmp(arg, "set") && i+1 < argc) {
275 cmd = CMD_SET;
276 ckey = argv[++i];
277 if (i+1 < argc)
278 cvalue = argv[++i];
279 } else if (!strcmp(arg, "get") && i+1 < argc) {
280 cmd = CMD_GET;
281 ckey = argv[++i];
282 } else if (!strcmp(arg, "load") && i+1 < argc) {
283 if ((cport >= 0) || (cvlan >= 0))
284 print_usage();
285 cmd = CMD_LOAD;
286 ckey = argv[++i];
287 } else if (!strcmp(arg, "show")) {
288 cmd = CMD_SHOW;
289 } else {
290 print_usage();
291 }
292 }
293
294 if (cmd == CMD_NONE)
295 print_usage();
296 if (cport > -1 && cvlan > -1)
297 print_usage();
298
299 dev = swlib_connect(cdev);
300 if (!dev) {
301 fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
302 return 1;
303 }
304
305 swlib_scan(dev);
306
307 if (cmd == CMD_GET || cmd == CMD_SET) {
308 if(cport > -1)
309 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
310 else if(cvlan > -1)
311 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
312 else
313 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
314
315 if(!a)
316 {
317 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
318 retval = -1;
319 goto out;
320 }
321 }
322
323 switch(cmd)
324 {
325 case CMD_SET:
326 if ((a->type != SWITCH_TYPE_NOVAL) &&
327 (cvalue == NULL))
328 print_usage();
329
330 if(cvlan > -1)
331 cport = cvlan;
332
333 retval = swlib_set_attr_string(dev, a, cport, cvalue);
334 if (retval < 0)
335 {
336 nl_perror(-retval, "Failed to set attribute");
337 goto out;
338 }
339 break;
340 case CMD_GET:
341 if(cvlan > -1)
342 val.port_vlan = cvlan;
343 if(cport > -1)
344 val.port_vlan = cport;
345 retval = swlib_get_attr(dev, a, &val);
346 if (retval < 0)
347 {
348 nl_perror(-retval, "Failed to get attribute");
349 goto out;
350 }
351 print_attr_val(a, &val);
352 putchar('\n');
353 break;
354 case CMD_LOAD:
355 swconfig_load_uci(dev, ckey);
356 break;
357 case CMD_HELP:
358 list_attributes(dev);
359 break;
360 case CMD_SHOW:
361 if (cport >= 0 || cvlan >= 0) {
362 if (cport >= 0)
363 show_port(dev, cport);
364 else
365 show_vlan(dev, cvlan, false);
366 } else {
367 show_global(dev);
368 for (i=0; i < dev->ports; i++)
369 show_port(dev, i);
370 for (i=0; i < dev->vlans; i++)
371 show_vlan(dev, i, true);
372 }
373 break;
374 }
375
376 out:
377 swlib_free_all(dev);
378 return retval;
379 }