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>
46 print_attrs(const struct switch_attr
*attr
)
55 case SWITCH_TYPE_STRING
:
58 case SWITCH_TYPE_PORTS
:
61 case SWITCH_TYPE_NOVAL
:
68 printf("\tAttribute %d (%s): %s (%s)\n", ++i
, type
, attr
->name
, attr
->description
);
74 list_attributes(struct switch_dev
*dev
)
76 printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev
->id
, dev
->dev_name
, dev
->name
, dev
->ports
, dev
->vlans
);
77 printf(" --switch\n");
78 print_attrs(dev
->ops
);
80 print_attrs(dev
->vlan_ops
);
82 print_attrs(dev
->port_ops
);
86 print_attr_val(const struct switch_attr
*attr
, const struct switch_val
*val
)
92 printf("%d", val
->value
.i
);
94 case SWITCH_TYPE_STRING
:
95 printf("%s", val
->value
.s
);
97 case SWITCH_TYPE_PORTS
:
98 for(i
= 0; i
< val
->len
; i
++) {
100 val
->value
.ports
[i
].id
,
101 (val
->value
.ports
[i
].flags
&
102 SWLIB_PORT_FLAG_TAGGED
) ? "t" : "");
106 printf("?unknown-type?");
111 show_attrs(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
114 if (attr
->type
!= SWITCH_TYPE_NOVAL
) {
115 printf("\t%s: ", attr
->name
);
116 if (swlib_get_attr(dev
, attr
, val
) < 0)
119 print_attr_val(attr
, val
);
127 show_global(struct switch_dev
*dev
)
129 struct switch_val val
;
131 printf("Global attributes:\n");
132 show_attrs(dev
, dev
->ops
, &val
);
136 show_port(struct switch_dev
*dev
, int port
)
138 struct switch_val val
;
140 printf("Port %d:\n", port
);
141 val
.port_vlan
= port
;
142 show_attrs(dev
, dev
->port_ops
, &val
);
146 show_vlan(struct switch_dev
*dev
, int vlan
)
148 struct switch_val val
;
150 printf("VLAN %d:\n", vlan
);
151 val
.port_vlan
= vlan
;
152 show_attrs(dev
, dev
->vlan_ops
, &val
);
158 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
163 swconfig_load_uci(struct switch_dev
*dev
, const char *name
)
165 struct uci_context
*ctx
;
166 struct uci_package
*p
= NULL
;
167 struct uci_element
*e
;
170 ctx
= uci_alloc_context();
174 uci_load(ctx
, name
, &p
);
176 uci_perror(ctx
, "Failed to load config file: ");
180 ret
= swlib_apply_from_uci(dev
, p
);
182 fprintf(stderr
, "Failed to apply configuration for switch '%s'\n", dev
->dev_name
);
185 uci_free_context(ctx
);
189 int main(int argc
, char **argv
)
192 struct switch_dev
*dev
;
193 struct switch_attr
*a
;
194 struct switch_val val
;
198 struct switch_port
*ports
;
210 if(strcmp(argv
[1], "dev"))
215 for(i
= 3; i
< argc
; i
++)
218 if (cmd
!= CMD_NONE
) {
220 } else if (!strcmp(arg
, "port") && i
+1 < argc
) {
221 cport
= atoi(argv
[++i
]);
222 } else if (!strcmp(arg
, "vlan") && i
+1 < argc
) {
223 cvlan
= atoi(argv
[++i
]);
224 } else if (!strcmp(arg
, "help")) {
226 } else if (!strcmp(arg
, "set") && i
+1 < argc
) {
231 } else if (!strcmp(arg
, "get") && i
+1 < argc
) {
234 } else if (!strcmp(arg
, "load") && i
+1 < argc
) {
235 if ((cport
>= 0) || (cvlan
>= 0))
239 } else if (!strcmp(arg
, "show")) {
248 if (cport
> -1 && cvlan
> -1)
251 dev
= swlib_connect(cdev
);
253 fprintf(stderr
, "Failed to connect to the switch\n");
257 ports
= malloc(sizeof(struct switch_port
) * dev
->ports
);
258 memset(ports
, 0, sizeof(struct switch_port
) * dev
->ports
);
261 if (cmd
== CMD_GET
|| cmd
== CMD_SET
) {
263 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_PORT
, ckey
);
265 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_VLAN
, ckey
);
267 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, ckey
);
271 fprintf(stderr
, "Unknown attribute \"%s\"\n", ckey
);
279 if ((a
->type
!= SWITCH_TYPE_NOVAL
) &&
286 if(swlib_set_attr_string(dev
, a
, cport
, cvalue
) < 0)
288 fprintf(stderr
, "failed\n");
295 val
.port_vlan
= cvlan
;
297 val
.port_vlan
= cport
;
298 if(swlib_get_attr(dev
, a
, &val
) < 0)
300 fprintf(stderr
, "failed\n");
305 case SWITCH_TYPE_INT
:
306 printf("%d\n", val
.value
.i
);
308 case SWITCH_TYPE_STRING
:
309 printf("%s\n", val
.value
.s
);
311 case SWITCH_TYPE_PORTS
:
312 for(i
= 0; i
< val
.len
; i
++) {
314 val
.value
.ports
[i
].id
,
315 (val
.value
.ports
[i
].flags
&
316 SWLIB_PORT_FLAG_TAGGED
) ? "t" : "");
323 swconfig_load_uci(dev
, ckey
);
326 list_attributes(dev
);
329 if (cport
>= 0 || cvlan
>= 0) {
331 show_port(dev
, cport
);
333 show_vlan(dev
, cvlan
);
336 for (i
=0; i
< dev
->ports
; i
++)
338 for (i
=0; i
< dev
->vlans
; i
++)
This page took 0.057639 seconds and 5 git commands to generate.