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>
26 #include <linux/types.h>
27 #include <linux/netlink.h>
28 #include <linux/genetlink.h>
29 #include <netlink/netlink.h>
30 #include <netlink/genl/genl.h>
31 #include <netlink/genl/ctrl.h>
32 #include <linux/switch.h>
38 void print_attrs(struct switch_attr *attr)
47 case SWITCH_TYPE_STRING:
50 case SWITCH_TYPE_PORTS:
53 case SWITCH_TYPE_NOVAL:
60 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
65 void list_attributes(struct switch_dev *dev)
67 printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev->id, dev->dev_name, dev->name, dev->ports, dev->vlans);
68 printf(" --switch\n");
69 print_attrs(dev->ops);
71 print_attrs(dev->vlan_ops);
73 print_attrs(dev->port_ops);
76 void print_usage(void)
78 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>)\n");
82 int main(int argc, char **argv)
85 struct switch_dev *dev;
86 struct switch_attr *a;
87 struct switch_val val;
91 struct switch_port *ports;
104 if(strcmp(argv[1], "dev"))
109 for(i = 3; i < argc; i++)
112 if(!strcmp(argv[i], "help"))
119 p = atoi(argv[i + 1]);
120 if(!strcmp(argv[i], "port"))
123 } else if(!strcmp(argv[i], "vlan"))
126 } else if(!strcmp(argv[i], "set"))
133 cvalue = argv[i + 2];
137 } else if(!strcmp(argv[i], "get"))
147 if(cport > -1 && cvlan > -1)
150 dev = swlib_connect(cdev);
152 fprintf(stderr, "Failed to connect to the switch\n");
156 ports = malloc(sizeof(struct switch_port) * dev->ports);
157 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
162 list_attributes(dev);
167 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
169 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
171 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
175 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
182 if ((a->type != SWITCH_TYPE_NOVAL) &&
187 case SWITCH_TYPE_INT:
188 val.value.i = atoi(cvalue);
190 case SWITCH_TYPE_STRING:
191 val.value.s = cvalue;
193 case SWITCH_TYPE_PORTS:
195 while(cvalue && *cvalue)
197 ports[val.len].flags = 0;
198 ports[val.len].id = strtol(cvalue, &cvalue, 10);
199 while(*cvalue && !isspace(*cvalue)) {
201 ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
208 val.value.ports = ports;
214 val.port_vlan = cvlan;
216 val.port_vlan = cport;
217 if(swlib_set_attr(dev, a, &val) < 0)
219 fprintf(stderr, "failed\n");
226 val.port_vlan = cvlan;
228 val.port_vlan = cport;
229 if(swlib_get_attr(dev, a, &val) < 0)
231 fprintf(stderr, "failed\n");
236 case SWITCH_TYPE_INT:
237 printf("%d\n", val.value.i);
239 case SWITCH_TYPE_STRING:
240 printf("%s\n", val.value.s);
242 case SWITCH_TYPE_PORTS:
243 for(i = 0; i < val.len; i++)
244 printf("%d ", val.value.ports[i]);