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
[] = {
50 { .name
= "enable_vlan" },
53 static struct swlib_setting
*settings
;
54 static struct swlib_setting
**head
;
57 swlib_map_settings(struct switch_dev
*dev
, int type
, int port_vlan
, struct uci_section
*s
)
59 struct swlib_setting
*setting
;
60 struct switch_attr
*attr
;
61 struct uci_element
*e
;
65 uci_foreach_element(&s
->options
, e
) {
68 if (o
->type
!= UCI_TYPE_STRING
)
71 if (!strcmp(e
->name
, "device"))
74 /* map early settings */
75 if (type
== SWLIB_ATTR_GROUP_GLOBAL
) {
78 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
79 if (strcmp(e
->name
, early_settings
[i
].name
) != 0)
82 early_settings
[i
].val
= o
->v
.string
;
87 attr
= swlib_lookup_attr(dev
, type
, e
->name
);
91 setting
= malloc(sizeof(struct swlib_setting
));
92 memset(setting
, 0, sizeof(struct swlib_setting
));
94 setting
->port_vlan
= port_vlan
;
95 setting
->val
= o
->v
.string
;
97 head
= &setting
->next
;
103 int swlib_apply_from_uci(struct switch_dev
*dev
, struct uci_package
*p
)
105 struct switch_attr
*attr
;
106 struct uci_context
*ctx
= p
->ctx
;
107 struct uci_element
*e
;
108 struct uci_section
*s
;
109 struct uci_option
*o
;
110 struct switch_val val
;
116 uci_foreach_element(&p
->sections
, e
) {
117 s
= uci_to_section(e
);
119 if (strcmp(s
->type
, "switch") != 0)
122 if (strcmp(e
->name
, dev
->dev_name
) != 0)
132 /* look up available early options, which need to be taken care
133 * of in the correct order */
134 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
135 early_settings
[i
].attr
= swlib_lookup_attr(dev
,
136 SWLIB_ATTR_GROUP_GLOBAL
, early_settings
[i
].name
);
138 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_GLOBAL
, 0, s
);
140 /* look for port or vlan sections */
141 uci_foreach_element(&p
->sections
, e
) {
142 struct uci_element
*os
;
143 s
= uci_to_section(e
);
145 if (!strcmp(s
->type
, "switch_port")) {
146 char *devn
, *port
, *port_err
= NULL
;
149 uci_foreach_element(&s
->options
, os
) {
150 o
= uci_to_option(os
);
151 if (o
->type
!= UCI_TYPE_STRING
)
154 if (!strcmp(os
->name
, "device")) {
156 if (strcmp(devn
, dev
->dev_name
) != 0)
158 } else if (!strcmp(os
->name
, "port")) {
162 if (!dev
|| !port
|| !port
[0])
165 port_n
= strtoul(port
, &port_err
, 0);
166 if (port_err
&& port_err
[0])
169 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_PORT
, port_n
, s
);
170 } else if (!strcmp(s
->type
, "switch_vlan")) {
171 char *devn
, *vlan
, *vlan_err
= NULL
;
174 uci_foreach_element(&s
->options
, os
) {
175 o
= uci_to_option(os
);
176 if (o
->type
!= UCI_TYPE_STRING
)
179 if (!strcmp(os
->name
, "device")) {
181 if (strcmp(devn
, dev
->dev_name
) != 0)
183 } else if (!strcmp(os
->name
, "vlan")) {
187 if (!dev
|| !vlan
|| !vlan
[0])
190 vlan_n
= strtoul(vlan
, &vlan_err
, 0);
191 if (vlan_err
&& vlan_err
[0])
194 swlib_map_settings(dev
, SWLIB_ATTR_GROUP_VLAN
, vlan_n
, s
);
198 for (i
= 0; i
< ARRAY_SIZE(early_settings
); i
++) {
199 struct swlib_setting
*st
= &early_settings
[i
];
200 if (!st
->attr
|| !st
->val
)
202 swlib_set_attr_string(dev
, st
->attr
, st
->port_vlan
, st
->val
);
207 struct swlib_setting
*st
= settings
;
209 swlib_set_attr_string(dev
, st
->attr
, st
->port_vlan
, st
->val
);
215 /* Apply the config */
216 attr
= swlib_lookup_attr(dev
, SWLIB_ATTR_GROUP_GLOBAL
, "apply");
220 memset(&val
, 0, sizeof(val
));
221 swlib_set_attr(dev
, attr
, &val
);