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>
45 print_attrs(const struct switch_attr
*attr
)
54 case SWITCH_TYPE_STRING
:
57 case SWITCH_TYPE_PORTS
:
60 case SWITCH_TYPE_NOVAL
:
67 printf("\tAttribute %d (%s): %s (%s)\n", ++i
, type
, attr
->name
, attr
->description
);
73 list_attributes(struct switch_dev
*dev
)
75 printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev
->id
, dev
->dev_name
, dev
->name
, dev
->ports
, dev
->vlans
);
76 printf(" --switch\n");
77 print_attrs(dev
->ops
);
79 print_attrs(dev
->vlan_ops
);
81 print_attrs(dev
->port_ops
);
87 printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>)\n");
92 swconfig_load_uci(struct switch_dev
*dev
, const char *name
)
94 struct uci_context
*ctx
;
95 struct uci_package
*p
= NULL
;
96 struct uci_element
*e
;
99 ctx
= uci_alloc_context();
103 uci_load(ctx
, name
, &p
);
105 uci_perror(ctx
, "Failed to load config file: ");
109 ret
= swlib_apply_from_uci(dev
, p
);
111 fprintf(stderr
, "Failed to apply configuration for switch '%s'\n", dev
->dev_name
);
114 uci_free_context(ctx
);
118 int main(int argc
, char **argv
)
121 struct switch_dev
*dev
;
122 struct switch_attr
*a
;
123 struct switch_val val
;
127 struct switch_port
*ports
;
139 if(strcmp(argv
[1], "dev"))
144 for(i
= 3; i
< argc
; i
++)
147 if (cmd
!= CMD_NONE
) {
149 } else if (!strcmp(arg
, "port") && i
+1 < argc
) {
150 cport
= atoi(argv
[++i
]);
151 } else if (!strcmp(arg
, "vlan") && i
+1 < argc
) {
152 cvlan
= atoi(argv
[++i
]);
153 } else if (!strcmp(arg
, "help")) {
155 } else if (!strcmp(arg
, "set") && i
+1 < argc
) {
160 } else if (!strcmp(arg
, "get") && i
+1 < argc
) {
163 } else if (!strcmp(arg
, "load") && i
+1 < argc
) {
164 if ((cport
>= 0) || (cvlan
>= 0))
175 if (cport
> -1 && cvlan
> -1)
178 dev
= swlib_connect(cdev
);
180 fprintf(stderr
, "Failed to connect to the switch\n");
184 ports
= malloc(sizeof(struct switch_port
) * dev
->ports
);
185 memset(ports
, 0, sizeof(struct switch_port
) * dev
->ports
);
188 if (cmd
== CMD_GET
|| cmd
== CMD_SET
) {
190 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_PORT
, ckey
);
192 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_VLAN
, ckey
);
194 a
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, ckey
);
198 fprintf(stderr
, "Unknown attribute \"%s\"\n", ckey
);
206 if ((a
->type
!= SWITCH_TYPE_NOVAL
) &&
213 if(swlib_set_attr_string(dev
, a
, cport
, cvalue
) < 0)
215 fprintf(stderr
, "failed\n");
222 val
.port_vlan
= cvlan
;
224 val
.port_vlan
= cport
;
225 if(swlib_get_attr(dev
, a
, &val
) < 0)
227 fprintf(stderr
, "failed\n");
232 case SWITCH_TYPE_INT
:
233 printf("%d\n", val
.value
.i
);
235 case SWITCH_TYPE_STRING
:
236 printf("%s\n", val
.value
.s
);
238 case SWITCH_TYPE_PORTS
:
239 for(i
= 0; i
< val
.len
; i
++) {
241 val
.value
.ports
[i
].id
,
242 (val
.value
.ports
[i
].flags
&
243 SWLIB_PORT_FLAG_TAGGED
) ? "t" : "");
250 swconfig_load_uci(dev
, ckey
);
253 list_attributes(dev
);