2 * uci.c: UCI binding for the switch configuration utility
4 * Copyright (C) 2009 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>
37 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
40 struct swlib_setting
{
41 struct switch_attr
*attr
;
45 struct swlib_setting
*next
;
48 struct swlib_setting early_settings
[] = {
49 { .name
= "reset", .val
= "1" },
50 { .name
= "enable_vlan", .val
= "1" },
53 static struct swlib_setting
*settings
;
54 static struct swlib_setting
**head
;
56 static bool swlib_match_name(struct switch_dev
*dev
, const char *name
)
58 return (strcmp(name
, dev
->dev_name
) == 0 ||
59 strcmp(name
, dev
->alias
) == 0);
63 swlib_map_settings(struct switch_dev
*dev
, int type
, int port_vlan
, struct uci_section
*s
)
65 struct swlib_setting
*setting
;
66 struct switch_attr
*attr
;
67 struct uci_element
*e
;
70 uci_foreach_element(&s
->options
, e
) {
73 if (o
->type
!= UCI_TYPE_STRING
)
76 if (!strcmp(e
->name
, "device"))
79 /* map early settings */
80 if (type
== SWLIB_ATTR_GROUP_GLOBAL
) {
83 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
84 if (strcmp(e
->name
, early_settings
[i
].name
) != 0)
87 early_settings
[i
].val
= o
->v
.string
;
92 attr
= swlib_lookup_attr(dev
, type
, e
->name
);
96 setting
= malloc(sizeof(struct swlib_setting
));
97 memset(setting
, 0, sizeof(struct swlib_setting
));
99 setting
->port_vlan
= port_vlan
;
100 setting
->val
= o
->v
.string
;
102 head
= &setting
->next
;
108 int swlib_apply_from_uci(struct switch_dev
*dev
, struct uci_package
*p
)
110 struct switch_attr
*attr
;
111 struct uci_element
*e
;
112 struct uci_section
*s
;
113 struct uci_option
*o
;
115 struct switch_val val
;
121 uci_foreach_element(&p
->sections
, e
) {
122 struct uci_element
*n
;
124 s
= uci_to_section(e
);
126 if (strcmp(s
->type
, "switch") != 0)
129 uci_foreach_element(&s
->options
, n
) {
130 struct uci_option
*o
= uci_to_option(n
);
132 if (strcmp(n
->name
, "name") != 0)
135 if (o
->type
!= UCI_TYPE_STRING
)
138 if (swlib_match_name(dev
, o
->v
.string
))
144 if (!swlib_match_name(dev
, e
->name
))
154 /* look up available early options, which need to be taken care
155 * of in the correct order */
156 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
157 early_settings
[i
].attr
= swlib_lookup_attr(dev
,
158 SWLIB_ATTR_GROUP_GLOBAL
, early_settings
[i
].name
);
160 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_GLOBAL
, 0, s
);
162 /* look for port or vlan sections */
163 uci_foreach_element(&p
->sections
, e
) {
164 struct uci_element
*os
;
165 s
= uci_to_section(e
);
167 if (!strcmp(s
->type
, "switch_port")) {
168 char *devn
, *port
, *port_err
= NULL
;
171 uci_foreach_element(&s
->options
, os
) {
172 o
= uci_to_option(os
);
173 if (o
->type
!= UCI_TYPE_STRING
)
176 if (!strcmp(os
->name
, "device")) {
178 if (!swlib_match_name(dev
, devn
))
180 } else if (!strcmp(os
->name
, "port")) {
184 if (!devn
|| !port
|| !port
[0])
187 port_n
= strtoul(port
, &port_err
, 0);
188 if (port_err
&& port_err
[0])
191 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_PORT
, port_n
, s
);
192 } else if (!strcmp(s
->type
, "switch_vlan")) {
193 char *devn
, *vlan
, *vlan_err
= NULL
;
196 uci_foreach_element(&s
->options
, os
) {
197 o
= uci_to_option(os
);
198 if (o
->type
!= UCI_TYPE_STRING
)
201 if (!strcmp(os
->name
, "device")) {
203 if (!swlib_match_name(dev
, devn
))
205 } else if (!strcmp(os
->name
, "vlan")) {
209 if (!devn
|| !vlan
|| !vlan
[0])
212 vlan_n
= strtoul(vlan
, &vlan_err
, 0);
213 if (vlan_err
&& vlan_err
[0])
216 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_VLAN
, vlan_n
, s
);
220 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
221 struct swlib_setting
*st
= &early_settings
[i
];
222 if (!st
->attr
|| !st
->val
)
224 swlib_set_attr_string(dev
, st
->attr
, st
->port_vlan
, st
->val
);
229 struct swlib_setting
*st
= settings
;
231 swlib_set_attr_string(dev
, st
->attr
, st
->port_vlan
, st
->val
);
237 /* Apply the config */
238 attr
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, "apply");
242 memset(&val
, 0, sizeof(val
));
243 swlib_set_attr(dev
, attr
, &val
);