2 * swconfig.c: Switch configuration utility
4 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2010 Martin Mares <mj@ucw.cz>
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.
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.
24 #include <sys/types.h>
25 #include <sys/socket.h>
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>
48 print_attrs(const struct switch_attr *attr)
57 case SWITCH_TYPE_STRING:
60 case SWITCH_TYPE_PORTS:
63 case SWITCH_TYPE_NOVAL:
70 printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
76 list_attributes(struct switch_dev *dev)
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);
82 print_attrs(dev->vlan_ops);
84 print_attrs(dev->port_ops);
105 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
107 struct switch_port_link *link;
110 switch (attr->type) {
111 case SWITCH_TYPE_INT:
112 printf("%d", val->value.i);
114 case SWITCH_TYPE_STRING:
115 printf("%s", val->value.s);
117 case SWITCH_TYPE_PORTS:
118 for(i = 0; i < val->len; i++) {
120 val->value.ports[i].id,
121 (val->value.ports[i].flags &
122 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
125 case SWITCH_TYPE_LINK:
126 link = val->value.link;
128 printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
130 speed_str(link->speed),
131 link->duplex ? "full" : "half",
132 link->tx_flow ? "txflow " : "",
133 link->rx_flow ? "rxflow " : "",
134 link->eee & SWLIB_LINK_FLAG_EEE_100BASET ? "eee100 " : "",
135 link->eee & SWLIB_LINK_FLAG_EEE_1000BASET ? "eee1000 " : "",
136 link->aneg ? "auto" : "");
138 printf("port:%d link:down", val->port_vlan);
141 printf("?unknown-type?");
146 show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
149 if (attr->type != SWITCH_TYPE_NOVAL) {
150 printf("\t%s: ", attr->name);
151 if (swlib_get_attr(dev, attr, val) < 0)
154 print_attr_val(attr, val);
162 show_global(struct switch_dev *dev)
164 struct switch_val val;
166 printf("Global attributes:\n");
167 show_attrs(dev, dev->ops, &val);
171 show_port(struct switch_dev *dev, int port)
173 struct switch_val val;
175 printf("Port %d:\n", port);
176 val.port_vlan = port;
177 show_attrs(dev, dev->port_ops, &val);
181 show_vlan(struct switch_dev *dev, int vlan, bool all)
183 struct switch_val val;
184 struct switch_attr *attr;
186 val.port_vlan = vlan;
189 attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports");
190 if (swlib_get_attr(dev, attr, &val) < 0)
197 printf("VLAN %d:\n", vlan);
198 show_attrs(dev, dev->vlan_ops, &val);
204 printf("swconfig list\n");
205 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
210 swconfig_load_uci(struct switch_dev *dev, const char *name)
212 struct uci_context *ctx;
213 struct uci_package *p = NULL;
216 ctx = uci_alloc_context();
220 uci_load(ctx, name, &p);
222 uci_perror(ctx, "Failed to load config file: ");
226 ret = swlib_apply_from_uci(dev, p);
228 fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name);
231 uci_free_context(ctx);
235 int main(int argc, char **argv)
238 struct switch_dev *dev;
239 struct switch_attr *a;
240 struct switch_val val;
249 char *csegment = NULL;
251 if((argc == 2) && !strcmp(argv[1], "list")) {
259 if(strcmp(argv[1], "dev"))
264 for(i = 3; i < argc; i++)
267 if (cmd != CMD_NONE) {
269 } else if (!strcmp(arg, "port") && i+1 < argc) {
270 cport = atoi(argv[++i]);
271 } else if (!strcmp(arg, "vlan") && i+1 < argc) {
272 cvlan = atoi(argv[++i]);
273 } else if (!strcmp(arg, "help")) {
275 } else if (!strcmp(arg, "set") && i+1 < argc) {
280 } else if (!strcmp(arg, "get") && i+1 < argc) {
283 } else if (!strcmp(arg, "load") && i+1 < argc) {
284 if ((cport >= 0) || (cvlan >= 0))
288 } else if (!strcmp(arg, "portmap")) {
290 csegment = argv[++i];
292 } else if (!strcmp(arg, "show")) {
301 if (cport > -1 && cvlan > -1)
304 dev = swlib_connect(cdev);
306 fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
312 if (cmd == CMD_GET || cmd == CMD_SET) {
314 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
316 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
318 a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
322 fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
331 if ((a->type != SWITCH_TYPE_NOVAL) &&
338 retval = swlib_set_attr_string(dev, a, cport, cvalue);
341 nl_perror(-retval, "Failed to set attribute");
347 val.port_vlan = cvlan;
349 val.port_vlan = cport;
350 retval = swlib_get_attr(dev, a, &val);
353 nl_perror(-retval, "Failed to get attribute");
356 print_attr_val(a, &val);
360 swconfig_load_uci(dev, ckey);
363 list_attributes(dev);
366 swlib_print_portmap(dev, csegment);
369 if (cport >= 0 || cvlan >= 0) {
371 show_port(dev, cport);
373 show_vlan(dev, cvlan, false);
376 for (i=0; i < dev->ports; i++)
378 for (i=0; i < dev->vlans; i++)
379 show_vlan(dev, i, true);