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>
47 print_attrs(const struct switch_attr
*attr
)
56 case SWITCH_TYPE_STRING
:
59 case SWITCH_TYPE_PORTS
:
62 case SWITCH_TYPE_NOVAL
:
69 printf("\tAttribute %d (%s): %s (%s)\n", ++i
, type
, attr
->name
, attr
->description
);
75 list_attributes(struct switch_dev
*dev
)
77 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
);
78 printf(" --switch\n");
79 print_attrs(dev
->ops
);
81 print_attrs(dev
->vlan_ops
);
83 print_attrs(dev
->port_ops
);
104 print_attr_val(const struct switch_attr
*attr
, const struct switch_val
*val
)
106 struct switch_port_link
*link
;
109 switch (attr
->type
) {
110 case SWITCH_TYPE_INT
:
111 printf("%d", val
->value
.i
);
113 case SWITCH_TYPE_STRING
:
114 printf("%s", val
->value
.s
);
116 case SWITCH_TYPE_PORTS
:
117 for(i
= 0; i
< val
->len
; i
++) {
119 val
->value
.ports
[i
].id
,
120 (val
->value
.ports
[i
].flags
&
121 SWLIB_PORT_FLAG_TAGGED
) ? "t" : "");
124 case SWITCH_TYPE_LINK
:
125 link
= val
->value
.link
;
127 printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
129 speed_str(link
->speed
),
130 link
->duplex
? "full" : "half",
131 link
->tx_flow
? "txflow " : "",
132 link
->rx_flow
? "rxflow " : "",
133 link
->eee
& SWLIB_LINK_FLAG_EEE_100BASET
? "eee100 " : "",
134 link
->eee
& SWLIB_LINK_FLAG_EEE_1000BASET
? "eee1000 " : "",
135 link
->aneg
? "auto" : "");
137 printf("port:%d link:down", val
->port_vlan
);
140 printf("?unknown-type?");
145 show_attrs(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
148 if (attr
->type
!= SWITCH_TYPE_NOVAL
) {
149 printf("\t%s: ", attr
->name
);
150 if (swlib_get_attr(dev
, attr
, val
) < 0)
153 print_attr_val(attr
, val
);
161 show_global(struct switch_dev
*dev
)
163 struct switch_val val
;
165 printf("Global attributes:\n");
166 show_attrs(dev
, dev
->ops
, &val
);
170 show_port(struct switch_dev
*dev
, int port
)
172 struct switch_val val
;
174 printf("Port %d:\n", port
);
175 val
.port_vlan
= port
;
176 show_attrs(dev
, dev
->port_ops
, &val
);
180 show_vlan(struct switch_dev
*dev
, int vlan
, bool all
)
182 struct switch_val val
;
183 struct switch_attr
*attr
;
185 val
.port_vlan
= vlan
;
188 attr
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_VLAN
, "ports");
189 if (swlib_get_attr(dev
, attr
, &val
) < 0)
196 printf("VLAN %d:\n", vlan
);
197 show_attrs(dev
, dev
->vlan_ops
, &val
);
203 printf("swconfig list\n");
204 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
209 swconfig_load_uci(struct switch_dev
*dev
, const char *name
)
211 struct uci_context
*ctx
;
212 struct uci_package
*p
= NULL
;
215 ctx
= uci_alloc_context();
219 uci_load(ctx
, name
, &p
);
221 uci_perror(ctx
, "Failed to load config file: ");
225 ret
= swlib_apply_from_uci(dev
, p
);
227 fprintf(stderr
, "Failed to apply configuration for switch '%s'\n", dev
->dev_name
);
230 uci_free_context(ctx
);
234 int main(int argc
, char **argv
)
237 struct switch_dev
*dev
;
238 struct switch_attr
*a
;
239 struct switch_val val
;
248 char *csegment
= NULL
;
250 if((argc
== 2) && !strcmp(argv
[1], "list")) {
258 if(strcmp(argv
[1], "dev"))
263 for(i
= 3; i
< argc
; i
++)
266 if (cmd
!= CMD_NONE
) {
268 } else if (!strcmp(arg
, "port") && i
+1 < argc
) {
269 cport
= atoi(argv
[++i
]);
270 } else if (!strcmp(arg
, "vlan") && i
+1 < argc
) {
271 cvlan
= atoi(argv
[++i
]);
272 } else if (!strcmp(arg
, "help")) {
274 } else if (!strcmp(arg
, "set") && i
+1 < argc
) {
279 } else if (!strcmp(arg
, "get") && i
+1 < argc
) {
282 } else if (!strcmp(arg
, "load") && i
+1 < argc
) {
283 if ((cport
>= 0) || (cvlan
>= 0))
287 } else if (!strcmp(arg
, "show")) {
296 if (cport
> -1 && cvlan
> -1)
299 dev
= swlib_connect(cdev
);
301 fprintf(stderr
, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
307 if (cmd
== CMD_GET
|| cmd
== CMD_SET
) {
309 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_PORT
, ckey
);
311 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_VLAN
, ckey
);
313 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, ckey
);
317 fprintf(stderr
, "Unknown attribute \"%s\"\n", ckey
);
326 if ((a
->type
!= SWITCH_TYPE_NOVAL
) &&
333 retval
= swlib_set_attr_string(dev
, a
, cport
, cvalue
);
336 nl_perror(-retval
, "Failed to set attribute");
342 val
.port_vlan
= cvlan
;
344 val
.port_vlan
= cport
;
345 retval
= swlib_get_attr(dev
, a
, &val
);
348 nl_perror(-retval
, "Failed to get attribute");
351 print_attr_val(a
, &val
);
355 swconfig_load_uci(dev
, ckey
);
358 list_attributes(dev
);
361 if (cport
>= 0 || cvlan
>= 0) {
363 show_port(dev
, cport
);
365 show_vlan(dev
, cvlan
, false);
368 for (i
=0; i
< dev
->ports
; i
++)
370 for (i
=0; i
< dev
->vlans
; i
++)
371 show_vlan(dev
, i
, true);