swconfig: clean up command line parsing
[openwrt/svn-archive/archive.git] / package / swconfig / src / cli.c
1 /*
2 * swconfig.c: Switch configuration utility
3 *
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundatio.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <uci.h>
26
27 #include <linux/types.h>
28 #include <linux/netlink.h>
29 #include <linux/genetlink.h>
30 #include <netlink/netlink.h>
31 #include <netlink/genl/genl.h>
32 #include <netlink/genl/ctrl.h>
33 #include <linux/switch.h>
34 #include "swlib.h"
35
36 enum {
37 CMD_NONE,
38 CMD_GET,
39 CMD_SET,
40 CMD_LOAD,
41 CMD_HELP,
42 };
43
44 static void
45 print_attrs(const struct switch_attr *attr)
46 {
47 int i = 0;
48 while (attr) {
49 const char *type;
50 switch(attr->type) {
51 case SWITCH_TYPE_INT:
52 type = "int";
53 break;
54 case SWITCH_TYPE_STRING:
55 type = "string";
56 break;
57 case SWITCH_TYPE_PORTS:
58 type = "ports";
59 break;
60 case SWITCH_TYPE_NOVAL:
61 type = "none";
62 break;
63 default:
64 type = "unknown";
65 break;
66 }
67 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
68 attr = attr->next;
69 }
70 }
71
72 static void
73 list_attributes(struct switch_dev *dev)
74 {
75 printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev->id, dev->dev_name, dev->name, dev->ports, dev->vlans);
76 printf(" --switch\n");
77 print_attrs(dev->ops);
78 printf(" --vlan\n");
79 print_attrs(dev->vlan_ops);
80 printf(" --port\n");
81 print_attrs(dev->port_ops);
82 }
83
84 static void
85 print_usage(void)
86 {
87 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>)\n");
88 exit(1);
89 }
90
91 static void
92 swconfig_load_uci(struct switch_dev *dev, const char *name)
93 {
94 struct uci_context *ctx;
95 struct uci_package *p = NULL;
96 struct uci_element *e;
97 int ret = -1;
98
99 ctx = uci_alloc_context();
100 if (!ctx)
101 return;
102
103 uci_load(ctx, name, &p);
104 if (!p) {
105 uci_perror(ctx, "Failed to load config file: ");
106 goto out;
107 }
108
109 ret = swlib_apply_from_uci(dev, p);
110 if (ret < 0)
111 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
112
113 out:
114 uci_free_context(ctx);
115 exit(ret);
116 }
117
118 int main(int argc, char **argv)
119 {
120 int retval = 0;
121 struct switch_dev *dev;
122 struct switch_attr *a;
123 struct switch_val val;
124 int err;
125 int i;
126
127 struct switch_port *ports;
128
129 int cmd = CMD_NONE;
130 char *cdev = NULL;
131 int cport = -1;
132 int cvlan = -1;
133 char *ckey = NULL;
134 char *cvalue = NULL;
135
136 if(argc < 4)
137 print_usage();
138
139 if(strcmp(argv[1], "dev"))
140 print_usage();
141
142 cdev = argv[2];
143
144 for(i = 3; i < argc; i++)
145 {
146 char *arg = argv[i];
147 if (cmd != CMD_NONE) {
148 print_usage();
149 } else if (!strcmp(arg, "port") && i+1 < argc) {
150 cport = atoi(argv[++i]);
151 } else if (!strcmp(arg, "vlan") && i+1 < argc) {
152 cvlan = atoi(argv[++i]);
153 } else if (!strcmp(arg, "help")) {
154 cmd = CMD_HELP;
155 } else if (!strcmp(arg, "set") && i+1 < argc) {
156 cmd = CMD_SET;
157 ckey = argv[++i];
158 if (i+1 < argc)
159 cvalue = argv[++i];
160 } else if (!strcmp(arg, "get") && i+1 < argc) {
161 cmd = CMD_GET;
162 ckey = argv[++i];
163 } else if (!strcmp(arg, "load") && i+1 < argc) {
164 if ((cport >= 0) || (cvlan >= 0))
165 print_usage();
166 cmd = CMD_LOAD;
167 ckey = argv[++i];
168 } else {
169 print_usage();
170 }
171 }
172
173 if (cmd == CMD_NONE)
174 print_usage();
175 if (cport > -1 && cvlan > -1)
176 print_usage();
177
178 dev = swlib_connect(cdev);
179 if (!dev) {
180 fprintf(stderr, "Failed to connect to the switch\n");
181 return 1;
182 }
183
184 ports = malloc(sizeof(struct switch_port) * dev->ports);
185 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
186 swlib_scan(dev);
187
188 if (cmd == CMD_GET || cmd == CMD_SET) {
189 if(cport > -1)
190 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
191 else if(cvlan > -1)
192 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
193 else
194 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
195
196 if(!a)
197 {
198 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
199 goto out;
200 }
201 }
202
203 switch(cmd)
204 {
205 case CMD_SET:
206 if ((a->type != SWITCH_TYPE_NOVAL) &&
207 (cvalue == NULL))
208 print_usage();
209
210 if(cvlan > -1)
211 cport = cvlan;
212
213 if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
214 {
215 fprintf(stderr, "failed\n");
216 retval = -1;
217 goto out;
218 }
219 break;
220 case CMD_GET:
221 if(cvlan > -1)
222 val.port_vlan = cvlan;
223 if(cport > -1)
224 val.port_vlan = cport;
225 if(swlib_get_attr(dev, a, &val) < 0)
226 {
227 fprintf(stderr, "failed\n");
228 retval = -1;
229 goto out;
230 }
231 switch(a->type) {
232 case SWITCH_TYPE_INT:
233 printf("%d\n", val.value.i);
234 break;
235 case SWITCH_TYPE_STRING:
236 printf("%s\n", val.value.s);
237 break;
238 case SWITCH_TYPE_PORTS:
239 for(i = 0; i < val.len; i++) {
240 printf("%d%s ",
241 val.value.ports[i].id,
242 (val.value.ports[i].flags &
243 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
244 }
245 printf("\n");
246 break;
247 }
248 break;
249 case CMD_LOAD:
250 swconfig_load_uci(dev, ckey);
251 break;
252 case CMD_HELP:
253 list_attributes(dev);
254 break;
255 }
256
257 out:
258 swlib_free_all(dev);
259 free(ports);
260
261 return 0;
262 }