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