2 * swconfig.c: Switch configuration utility
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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.
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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
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>
43 print_attrs(const struct switch_attr *attr)
52 case SWITCH_TYPE_STRING:
55 case SWITCH_TYPE_PORTS:
58 case SWITCH_TYPE_NOVAL:
65 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
71 list_attributes(struct switch_dev *dev)
73 printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev->id, dev->dev_name, dev->name, dev->ports, dev->vlans);
74 printf(" --switch\n");
75 print_attrs(dev->ops);
77 print_attrs(dev->vlan_ops);
79 print_attrs(dev->port_ops);
85 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>)\n");
90 swconfig_load_uci(struct switch_dev *dev, const char *name)
92 struct uci_context *ctx;
93 struct uci_package *p = NULL;
94 struct uci_element *e;
97 ctx = uci_alloc_context();
101 uci_load(ctx, name, &p);
103 uci_perror(ctx, "Failed to load config file: ");
107 ret = swlib_apply_from_uci(dev, p);
109 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
112 uci_free_context(ctx);
116 int main(int argc, char **argv)
119 struct switch_dev *dev;
120 struct switch_attr *a;
121 struct switch_val val;
125 struct switch_port *ports;
138 if(strcmp(argv[1], "dev"))
143 for(i = 3; i < argc; i++)
146 if (!strcmp(argv[i], "help")) {
152 p = atoi(argv[i + 1]);
153 if (!strcmp(argv[i], "port")) {
155 } else if (!strcmp(argv[i], "vlan")) {
157 } else if (!strcmp(argv[i], "set")) {
163 cvalue = argv[i + 2];
167 } else if (!strcmp(argv[i], "get")) {
170 } else if (!strcmp(argv[i], "load")) {
171 if ((cport >= 0) || (cvlan >= 0))
182 if(cport > -1 && cvlan > -1)
185 dev = swlib_connect(cdev);
187 fprintf(stderr, "Failed to connect to the switch\n");
191 ports = malloc(sizeof(struct switch_port) * dev->ports);
192 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
197 list_attributes(dev);
203 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
205 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
207 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
211 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
219 if ((a->type != SWITCH_TYPE_NOVAL) &&
226 if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
228 fprintf(stderr, "failed\n");
235 val.port_vlan = cvlan;
237 val.port_vlan = cport;
238 if(swlib_get_attr(dev, a, &val) < 0)
240 fprintf(stderr, "failed\n");
245 case SWITCH_TYPE_INT:
246 printf("%d\n", val.value.i);
248 case SWITCH_TYPE_STRING:
249 printf("%s\n", val.value.s);
251 case SWITCH_TYPE_PORTS:
252 for(i = 0; i < val.len; i++)
253 printf("%d ", val.value.ports[i]);
259 swconfig_load_uci(dev, ckey);