2 * netifd - network interface daemon
3 * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
20 #include "interface.h"
27 VLANDEV_ATTR_INGRESS_QOS_MAPPING
,
28 VLANDEV_ATTR_EGRESS_QOS_MAPPING
,
32 static const struct blobmsg_policy vlandev_attrs
[__VLANDEV_ATTR_MAX
] = {
33 [VLANDEV_ATTR_IFNAME
] = { "ifname", BLOBMSG_TYPE_STRING
},
34 [VLANDEV_ATTR_VID
] = { "vid", BLOBMSG_TYPE_STRING
},
35 [VLANDEV_ATTR_INGRESS_QOS_MAPPING
] = { "ingress_qos_mapping", BLOBMSG_TYPE_ARRAY
},
36 [VLANDEV_ATTR_EGRESS_QOS_MAPPING
] = { "egress_qos_mapping", BLOBMSG_TYPE_ARRAY
},
39 static const struct uci_blob_param_list vlandev_attr_list
= {
40 .n_params
= __VLANDEV_ATTR_MAX
,
41 .params
= vlandev_attrs
,
44 .next
= { &device_attr_list
},
47 static struct device_type vlan8021q_device_type
;
48 static struct blob_buf b
;
50 struct vlandev_device
{
52 struct device_user parent
;
54 device_state_cb set_state
;
56 struct blob_attr
*config_data
;
57 struct blob_attr
*ifname
;
58 struct blob_attr
*vid
;
60 struct vlandev_config config
;
64 vlandev_hotplug_add(struct device
*dev
, struct device
*member
, struct blob_attr
*vlan
)
66 struct vlandev_device
*mvdev
= container_of(dev
, struct vlandev_device
, dev
);
69 dev
= mvdev
->parent
.dev
;
70 if (!dev
|| !dev
->hotplug_ops
)
71 return UBUS_STATUS_NOT_SUPPORTED
;
74 a
= blobmsg_open_array(&b
, "vlans");
75 blobmsg_printf(&b
, NULL
, "%d", mvdev
->config
.vid
);
76 blobmsg_close_array(&b
, a
);
78 return dev
->hotplug_ops
->add(dev
, member
, blobmsg_data(b
.head
));
82 vlandev_hotplug_del(struct device
*dev
, struct device
*member
)
84 struct vlandev_device
*mvdev
= container_of(dev
, struct vlandev_device
, dev
);
86 dev
= mvdev
->parent
.dev
;
87 if (!dev
|| !dev
->hotplug_ops
)
88 return UBUS_STATUS_NOT_SUPPORTED
;
90 return dev
->hotplug_ops
->del(dev
, member
);
94 vlandev_hotplug_prepare(struct device
*dev
, struct device
**bridge_dev
)
96 struct vlandev_device
*mvdev
= container_of(dev
, struct vlandev_device
, dev
);
98 dev
= mvdev
->parent
.dev
;
99 if (!dev
|| !dev
->hotplug_ops
)
100 return UBUS_STATUS_NOT_SUPPORTED
;
102 return dev
->hotplug_ops
->prepare(dev
, bridge_dev
);
105 static void vlandev_hotplug_check(struct vlandev_device
*mvdev
)
107 static const struct device_hotplug_ops hotplug_ops
= {
108 .prepare
= vlandev_hotplug_prepare
,
109 .add
= vlandev_hotplug_add
,
110 .del
= vlandev_hotplug_del
112 struct device
*dev
= mvdev
->parent
.dev
;
114 if (!dev
|| !dev
->hotplug_ops
|| avl_is_empty(&dev
->vlans
.avl
) ||
115 mvdev
->dev
.type
!= &vlan8021q_device_type
) {
116 mvdev
->dev
.hotplug_ops
= NULL
;
120 mvdev
->dev
.hotplug_ops
= &hotplug_ops
;
125 vlandev_base_cb(struct device_user
*dev
, enum device_event ev
)
127 struct vlandev_device
*mvdev
= container_of(dev
, struct vlandev_device
, parent
);
131 device_set_present(&mvdev
->dev
, true);
133 case DEV_EVENT_REMOVE
:
134 device_set_present(&mvdev
->dev
, false);
136 case DEV_EVENT_UPDATE_IFNAME
:
137 vlandev_hotplug_check(mvdev
);
145 vlandev_set_down(struct vlandev_device
*mvdev
)
147 mvdev
->set_state(&mvdev
->dev
, false);
148 system_vlandev_del(&mvdev
->dev
);
149 device_release(&mvdev
->parent
);
155 vlandev_set_up(struct vlandev_device
*mvdev
)
159 ret
= device_claim(&mvdev
->parent
);
163 ret
= system_vlandev_add(&mvdev
->dev
, mvdev
->parent
.dev
, &mvdev
->config
);
167 ret
= mvdev
->set_state(&mvdev
->dev
, true);
174 system_vlandev_del(&mvdev
->dev
);
176 device_release(&mvdev
->parent
);
181 vlandev_set_state(struct device
*dev
, bool up
)
183 struct vlandev_device
*mvdev
;
185 D(SYSTEM
, "vlandev_set_state(%s, %u)\n", dev
->ifname
, up
);
187 mvdev
= container_of(dev
, struct vlandev_device
, dev
);
189 return vlandev_set_up(mvdev
);
191 return vlandev_set_down(mvdev
);
195 vlandev_free(struct device
*dev
)
197 struct vlandev_device
*mvdev
;
199 mvdev
= container_of(dev
, struct vlandev_device
, dev
);
200 device_remove_user(&mvdev
->parent
);
201 free(mvdev
->config_data
);
202 vlist_simple_flush_all(&mvdev
->config
.ingress_qos_mapping_list
);
203 vlist_simple_flush_all(&mvdev
->config
.egress_qos_mapping_list
);
207 static void vlandev_qos_mapping_dump(struct blob_buf
*b
, const char *name
, const struct vlist_simple_tree
*qos_mapping_li
)
209 const struct vlan_qos_mapping
*elem
;
212 a
= blobmsg_open_array(b
, name
);
214 vlist_simple_for_each_element(qos_mapping_li
, elem
, node
) {
215 t
= blobmsg_open_table(b
, NULL
);
217 blobmsg_add_u32(b
, "from", elem
->from
);
218 blobmsg_add_u32(b
, "to", elem
->to
);
220 blobmsg_close_table(b
, t
);
223 blobmsg_close_array(b
, a
);
227 vlandev_dump_info(struct device
*dev
, struct blob_buf
*b
)
229 struct vlandev_device
*mvdev
;
231 mvdev
= container_of(dev
, struct vlandev_device
, dev
);
232 blobmsg_add_string(b
, "parent", mvdev
->parent
.dev
->ifname
);
233 system_if_dump_info(dev
, b
);
234 blobmsg_add_u32(b
, "vid", mvdev
->config
.vid
);
235 vlandev_qos_mapping_dump(b
, "ingress_qos_mapping", &mvdev
->config
.ingress_qos_mapping_list
);
236 vlandev_qos_mapping_dump(b
, "egress_qos_mapping", &mvdev
->config
.egress_qos_mapping_list
);
240 vlandev_get_vid(struct device
*dev
, const char *id_str
)
246 id
= strtoul(id_str
, &err
, 10);
251 alias_id
= kvlist_get(&dev
->vlan_aliases
, id_str
);
262 vlandev_config_init(struct device
*dev
)
264 struct vlandev_device
*mvdev
;
265 struct device
*basedev
= NULL
;
267 mvdev
= container_of(dev
, struct vlandev_device
, dev
);
269 basedev
= device_get(blobmsg_data(mvdev
->ifname
), true);
272 mvdev
->config
.vid
= vlandev_get_vid(basedev
, blobmsg_get_string(mvdev
->vid
));
274 mvdev
->config
.vid
= 1;
276 device_add_user(&mvdev
->parent
, basedev
);
277 vlandev_hotplug_check(mvdev
);
280 static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree
*qos_mapping_li
, struct blob_attr
*list
)
282 struct blob_attr
*cur
;
283 struct vlan_qos_mapping
*qos_mapping
;
286 blobmsg_for_each_attr(cur
, list
, rem
) {
287 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
290 if (!blobmsg_check_attr(cur
, false))
293 qos_mapping
= calloc(1, sizeof(*qos_mapping
));
297 rc
= sscanf(blobmsg_data(cur
), "%" PRIu32
":%" PRIu32
, &qos_mapping
->from
, &qos_mapping
->to
);
302 vlist_simple_add(qos_mapping_li
, &qos_mapping
->node
);
307 vlandev_apply_settings(struct vlandev_device
*mvdev
, struct blob_attr
**tb
)
309 struct vlandev_config
*cfg
= &mvdev
->config
;
310 struct blob_attr
*cur
;
312 cfg
->proto
= (mvdev
->dev
.type
== &vlan8021q_device_type
) ?
313 VLAN_PROTO_8021Q
: VLAN_PROTO_8021AD
;
315 vlist_simple_update(&cfg
->ingress_qos_mapping_list
);
316 vlist_simple_update(&cfg
->egress_qos_mapping_list
);
318 if ((cur
= tb
[VLANDEV_ATTR_INGRESS_QOS_MAPPING
]))
319 vlandev_qos_mapping_list_apply(&cfg
->ingress_qos_mapping_list
, cur
);
321 if ((cur
= tb
[VLANDEV_ATTR_EGRESS_QOS_MAPPING
]))
322 vlandev_qos_mapping_list_apply(&cfg
->egress_qos_mapping_list
, cur
);
324 vlist_simple_flush(&cfg
->ingress_qos_mapping_list
);
325 vlist_simple_flush(&cfg
->egress_qos_mapping_list
);
328 static enum dev_change_type
329 vlandev_reload(struct device
*dev
, struct blob_attr
*attr
)
331 struct blob_attr
*tb_dev
[__DEV_ATTR_MAX
];
332 struct blob_attr
*tb_mv
[__VLANDEV_ATTR_MAX
];
333 enum dev_change_type ret
= DEV_CONFIG_APPLIED
;
334 struct vlandev_device
*mvdev
;
336 mvdev
= container_of(dev
, struct vlandev_device
, dev
);
337 attr
= blob_memdup(attr
);
339 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, tb_dev
,
340 blob_data(attr
), blob_len(attr
));
341 blobmsg_parse(vlandev_attrs
, __VLANDEV_ATTR_MAX
, tb_mv
,
342 blob_data(attr
), blob_len(attr
));
344 device_init_settings(dev
, tb_dev
);
345 vlandev_apply_settings(mvdev
, tb_mv
);
346 mvdev
->ifname
= tb_mv
[VLANDEV_ATTR_IFNAME
];
347 mvdev
->vid
= tb_mv
[VLANDEV_ATTR_VID
];
349 if (mvdev
->config_data
) {
350 struct blob_attr
*otb_dev
[__DEV_ATTR_MAX
];
351 struct blob_attr
*otb_mv
[__VLANDEV_ATTR_MAX
];
353 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb_dev
,
354 blob_data(mvdev
->config_data
), blob_len(mvdev
->config_data
));
356 if (uci_blob_diff(tb_dev
, otb_dev
, &device_attr_list
, NULL
))
357 ret
= DEV_CONFIG_RESTART
;
359 blobmsg_parse(vlandev_attrs
, __VLANDEV_ATTR_MAX
, otb_mv
,
360 blob_data(mvdev
->config_data
), blob_len(mvdev
->config_data
));
362 if (uci_blob_diff(tb_mv
, otb_mv
, &vlandev_attr_list
, NULL
))
363 ret
= DEV_CONFIG_RESTART
;
365 vlandev_config_init(dev
);
368 free(mvdev
->config_data
);
369 mvdev
->config_data
= attr
;
373 static struct device
*
374 vlandev_create(const char *name
, struct device_type
*devtype
,
375 struct blob_attr
*attr
)
377 struct vlandev_device
*mvdev
;
378 struct device
*dev
= NULL
;
380 mvdev
= calloc(1, sizeof(*mvdev
));
384 vlist_simple_init(&mvdev
->config
.ingress_qos_mapping_list
,
385 struct vlan_qos_mapping
, node
);
386 vlist_simple_init(&mvdev
->config
.egress_qos_mapping_list
,
387 struct vlan_qos_mapping
, node
);
391 if (device_init(dev
, devtype
, name
) < 0) {
397 dev
->config_pending
= true;
399 mvdev
->set_state
= dev
->set_state
;
400 dev
->set_state
= vlandev_set_state
;
402 dev
->hotplug_ops
= NULL
;
403 mvdev
->parent
.cb
= vlandev_base_cb
;
405 vlandev_reload(dev
, attr
);
410 static struct device_type vlan8021ad_device_type
= {
412 .config_params
= &vlandev_attr_list
,
413 .create
= vlandev_create
,
414 .config_init
= vlandev_config_init
,
415 .reload
= vlandev_reload
,
416 .free
= vlandev_free
,
417 .dump_info
= vlandev_dump_info
,
420 static struct device_type vlan8021q_device_type
= {
422 .config_params
= &vlandev_attr_list
,
423 .create
= vlandev_create
,
424 .config_init
= vlandev_config_init
,
425 .reload
= vlandev_reload
,
426 .free
= vlandev_free
,
427 .dump_info
= vlandev_dump_info
,
430 static void __init
vlandev_device_type_init(void)
432 device_type_add(&vlan8021ad_device_type
);
433 device_type_add(&vlan8021q_device_type
);