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