2 * swconfig.c: Switch configuration utility
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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("Switch %d: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev
->id
, dev
->dev_name
, 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
);
87 print_attr_val(const struct switch_attr
*attr
, const struct switch_val
*val
)
93 printf("%d", val
->value
.i
);
95 case SWITCH_TYPE_STRING
:
96 printf("%s", val
->value
.s
);
98 case SWITCH_TYPE_PORTS
:
99 for(i
= 0; i
< val
->len
; i
++) {
101 val
->value
.ports
[i
].id
,
102 (val
->value
.ports
[i
].flags
&
103 SWLIB_PORT_FLAG_TAGGED
) ? "t" : "");
107 printf("?unknown-type?");
112 show_attrs(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
115 if (attr
->type
!= SWITCH_TYPE_NOVAL
) {
116 printf("\t%s: ", attr
->name
);
117 if (swlib_get_attr(dev
, attr
, val
) < 0)
120 print_attr_val(attr
, val
);
128 show_global(struct switch_dev
*dev
)
130 struct switch_val val
;
132 printf("Global attributes:\n");
133 show_attrs(dev
, dev
->ops
, &val
);
137 show_port(struct switch_dev
*dev
, int port
)
139 struct switch_val val
;
141 printf("Port %d:\n", port
);
142 val
.port_vlan
= port
;
143 show_attrs(dev
, dev
->port_ops
, &val
);
147 show_vlan(struct switch_dev
*dev
, int vlan
)
149 struct switch_val val
;
151 printf("VLAN %d:\n", vlan
);
152 val
.port_vlan
= vlan
;
153 show_attrs(dev
, dev
->vlan_ops
, &val
);
159 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n");
164 swconfig_load_uci(struct switch_dev
*dev
, const char *name
)
166 struct uci_context
*ctx
;
167 struct uci_package
*p
= NULL
;
168 struct uci_element
*e
;
171 ctx
= uci_alloc_context();
175 uci_load(ctx
, name
, &p
);
177 uci_perror(ctx
, "Failed to load config file: ");
181 ret
= swlib_apply_from_uci(dev
, p
);
183 fprintf(stderr
, "Failed to apply configuration for switch '%s'\n", dev
->dev_name
);
186 uci_free_context(ctx
);
190 int main(int argc
, char **argv
)
193 struct switch_dev
*dev
;
194 struct switch_attr
*a
;
195 struct switch_val val
;
209 if(strcmp(argv
[1], "dev"))
214 for(i
= 3; i
< argc
; i
++)
217 if (cmd
!= CMD_NONE
) {
219 } else if (!strcmp(arg
, "port") && i
+1 < argc
) {
220 cport
= atoi(argv
[++i
]);
221 } else if (!strcmp(arg
, "vlan") && i
+1 < argc
) {
222 cvlan
= atoi(argv
[++i
]);
223 } else if (!strcmp(arg
, "help")) {
225 } else if (!strcmp(arg
, "set") && i
+1 < argc
) {
230 } else if (!strcmp(arg
, "get") && i
+1 < argc
) {
233 } else if (!strcmp(arg
, "load") && i
+1 < argc
) {
234 if ((cport
>= 0) || (cvlan
>= 0))
238 } else if (!strcmp(arg
, "show")) {
247 if (cport
> -1 && cvlan
> -1)
250 dev
= swlib_connect(cdev
);
252 fprintf(stderr
, "Failed to connect to the switch\n");
258 if (cmd
== CMD_GET
|| cmd
== CMD_SET
) {
260 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_PORT
, ckey
);
262 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_VLAN
, ckey
);
264 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, ckey
);
268 fprintf(stderr
, "Unknown attribute \"%s\"\n", ckey
);
276 if ((a
->type
!= SWITCH_TYPE_NOVAL
) &&
283 if(swlib_set_attr_string(dev
, a
, cport
, cvalue
) < 0)
285 fprintf(stderr
, "failed\n");
292 val
.port_vlan
= cvlan
;
294 val
.port_vlan
= cport
;
295 if(swlib_get_attr(dev
, a
, &val
) < 0)
297 fprintf(stderr
, "failed\n");
301 print_attr_val(a
, &val
);
305 swconfig_load_uci(dev
, ckey
);
308 list_attributes(dev
);
311 if (cport
>= 0 || cvlan
>= 0) {
313 show_port(dev
, cport
);
315 show_vlan(dev
, cvlan
);
318 for (i
=0; i
< dev
->ports
; i
++)
320 for (i
=0; i
< dev
->vlans
; i
++)
This page took 0.070485 seconds and 5 git commands to generate.