swconfig: add SWITCH_TYPE_LINK and support sending link info to user space
[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 CMD_PORTMAP,
45 };
46
47 static void
48 print_attrs(const struct switch_attr *attr)
49 {
50 int i = 0;
51 while (attr) {
52 const char *type;
53 switch(attr->type) {
54 case SWITCH_TYPE_INT:
55 type = "int";
56 break;
57 case SWITCH_TYPE_STRING:
58 type = "string";
59 break;
60 case SWITCH_TYPE_PORTS:
61 type = "ports";
62 break;
63 case SWITCH_TYPE_NOVAL:
64 type = "none";
65 break;
66 default:
67 type = "unknown";
68 break;
69 }
70 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
71 attr = attr->next;
72 }
73 }
74
75 static void
76 list_attributes(struct switch_dev *dev)
77 {
78 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);
79 printf(" --switch\n");
80 print_attrs(dev->ops);
81 printf(" --vlan\n");
82 print_attrs(dev->vlan_ops);
83 printf(" --port\n");
84 print_attrs(dev->port_ops);
85 }
86
87 static void
88 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
89 {
90 int i;
91
92 switch (attr->type) {
93 case SWITCH_TYPE_INT:
94 printf("%d", val->value.i);
95 break;
96 case SWITCH_TYPE_STRING:
97 printf("%s", val->value.s);
98 break;
99 case SWITCH_TYPE_PORTS:
100 for(i = 0; i < val->len; i++) {
101 printf("%d%s ",
102 val->value.ports[i].id,
103 (val->value.ports[i].flags &
104 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
105 }
106 break;
107 default:
108 printf("?unknown-type?");
109 }
110 }
111
112 static void
113 show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
114 {
115 while (attr) {
116 if (attr->type != SWITCH_TYPE_NOVAL) {
117 printf("\t%s: ", attr->name);
118 if (swlib_get_attr(dev, attr, val) < 0)
119 printf("???");
120 else
121 print_attr_val(attr, val);
122 putchar('\n');
123 }
124 attr = attr->next;
125 }
126 }
127
128 static void
129 show_global(struct switch_dev *dev)
130 {
131 struct switch_val val;
132
133 printf("Global attributes:\n");
134 show_attrs(dev, dev->ops, &val);
135 }
136
137 static void
138 show_port(struct switch_dev *dev, int port)
139 {
140 struct switch_val val;
141
142 printf("Port %d:\n", port);
143 val.port_vlan = port;
144 show_attrs(dev, dev->port_ops, &val);
145 }
146
147 static void
148 show_vlan(struct switch_dev *dev, int vlan, bool all)
149 {
150 struct switch_val val;
151 struct switch_attr *attr;
152
153 val.port_vlan = vlan;
154
155 if (all) {
156 attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports");
157 if (swlib_get_attr(dev, attr, &val) < 0)
158 return;
159
160 if (!val.len)
161 return;
162 }
163
164 printf("VLAN %d:\n", vlan);
165 show_attrs(dev, dev->vlan_ops, &val);
166 }
167
168 static void
169 print_usage(void)
170 {
171 printf("swconfig list\n");
172 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
173 exit(1);
174 }
175
176 static void
177 swconfig_load_uci(struct switch_dev *dev, const char *name)
178 {
179 struct uci_context *ctx;
180 struct uci_package *p = NULL;
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 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 char *csegment = 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, "portmap")) {
256 if (i + 1 < argc)
257 csegment = argv[++i];
258 cmd = CMD_PORTMAP;
259 } else if (!strcmp(arg, "show")) {
260 cmd = CMD_SHOW;
261 } else {
262 print_usage();
263 }
264 }
265
266 if (cmd == CMD_NONE)
267 print_usage();
268 if (cport > -1 && cvlan > -1)
269 print_usage();
270
271 dev = swlib_connect(cdev);
272 if (!dev) {
273 fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
274 return 1;
275 }
276
277 swlib_scan(dev);
278
279 if (cmd == CMD_GET || cmd == CMD_SET) {
280 if(cport > -1)
281 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
282 else if(cvlan > -1)
283 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
284 else
285 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
286
287 if(!a)
288 {
289 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
290 retval = -1;
291 goto out;
292 }
293 }
294
295 switch(cmd)
296 {
297 case CMD_SET:
298 if ((a->type != SWITCH_TYPE_NOVAL) &&
299 (cvalue == NULL))
300 print_usage();
301
302 if(cvlan > -1)
303 cport = cvlan;
304
305 if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
306 {
307 fprintf(stderr, "failed\n");
308 retval = -1;
309 goto out;
310 }
311 break;
312 case CMD_GET:
313 if(cvlan > -1)
314 val.port_vlan = cvlan;
315 if(cport > -1)
316 val.port_vlan = cport;
317 if(swlib_get_attr(dev, a, &val) < 0)
318 {
319 fprintf(stderr, "failed\n");
320 retval = -1;
321 goto out;
322 }
323 print_attr_val(a, &val);
324 putchar('\n');
325 break;
326 case CMD_LOAD:
327 swconfig_load_uci(dev, ckey);
328 break;
329 case CMD_HELP:
330 list_attributes(dev);
331 break;
332 case CMD_PORTMAP:
333 swlib_print_portmap(dev, csegment);
334 break;
335 case CMD_SHOW:
336 if (cport >= 0 || cvlan >= 0) {
337 if (cport >= 0)
338 show_port(dev, cport);
339 else
340 show_vlan(dev, cvlan, false);
341 } else {
342 show_global(dev);
343 for (i=0; i < dev->ports; i++)
344 show_port(dev, i);
345 for (i=0; i < dev->vlans; i++)
346 show_vlan(dev, i, true);
347 }
348 break;
349 }
350
351 out:
352 swlib_free_all(dev);
353 return retval;
354 }