7 #include <sys/socket.h>
8 #include <net/ethernet.h>
11 #include <netinet/ether.h>
18 static struct avl_tree devices;
20 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
21 [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
22 [DEV_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
23 [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
24 [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
25 [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
26 [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
29 const struct config_param_list device_attr_list = {
30 .n_params = __DEV_ATTR_MAX,
34 static struct device *
35 simple_device_create(struct blob_attr *attr)
37 struct blob_attr *tb[__DEV_ATTR_MAX];
38 struct device *dev = NULL;
41 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
42 if (!tb[DEV_ATTR_NAME])
45 name = blobmsg_data(tb[DEV_ATTR_NAME]);
49 dev = device_get(name, true);
53 device_init_settings(dev, tb);
58 static void simple_device_free(struct device *dev)
64 const struct device_type simple_device_type = {
65 .name = "Network device",
66 .config_params = &device_attr_list,
68 .create = simple_device_create,
69 .check_state = system_if_check,
70 .free = simple_device_free,
74 device_init_settings(struct device *dev, struct blob_attr **tb)
76 struct blob_attr *cur;
77 struct ether_addr *ea;
81 if ((cur = tb[DEV_ATTR_MTU])) {
82 dev->mtu = blobmsg_get_u32(cur);
83 dev->flags |= DEV_OPT_MTU;
86 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
87 dev->txqueuelen = blobmsg_get_u32(cur);
88 dev->flags |= DEV_OPT_TXQUEUELEN;
91 if ((cur = tb[DEV_ATTR_MACADDR])) {
92 ea = ether_aton(blob_data(cur));
94 memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
95 dev->flags |= DEV_OPT_MACADDR;
100 static void __init dev_init(void)
102 avl_init(&devices, avl_strcmp, false, NULL);
105 static void device_broadcast_event(struct device *dev, enum device_event ev)
107 struct device_user *dep, *tmp;
109 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
117 static int set_device_state(struct device *dev, bool state)
127 int device_claim(struct device_user *dep)
129 struct device *dev = dep->dev;
136 DPRINTF("claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
137 if (++dev->active != 1)
140 device_broadcast_event(dev, DEV_EVENT_SETUP);
141 ret = dev->set_state(dev, true);
143 device_broadcast_event(dev, DEV_EVENT_UP);
150 void device_release(struct device_user *dep)
152 struct device *dev = dep->dev;
157 dep->claimed = false;
159 DPRINTF("release device %s, new refcount: %d\n", dev->ifname, dev->active);
160 assert(dev->active >= 0);
165 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
166 dev->set_state(dev, false);
167 device_broadcast_event(dev, DEV_EVENT_DOWN);
170 int check_device_state(struct device *dev)
172 if (!dev->type->check_state)
175 return dev->type->check_state(dev);
178 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
184 strncpy(dev->ifname, name, IFNAMSIZ);
186 DPRINTF("Initialize device '%s'\n", dev->ifname);
187 INIT_LIST_HEAD(&dev->users);
191 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
195 device_init_virtual(dev, type, ifname);
198 dev->set_state = set_device_state;
200 dev->avl.key = dev->ifname;
202 ret = avl_insert(&devices, &dev->avl);
206 check_device_state(dev);
211 struct device *device_get(const char *name, bool create)
215 if (strchr(name, '.'))
216 return get_vlan_device_chain(name, create);
218 dev = avl_find_element(&devices, name, dev, avl);
225 dev = calloc(1, sizeof(*dev));
226 device_init(dev, &simple_device_type, name);
231 void device_cleanup(struct device *dev)
233 struct device_user *dep, *tmp;
235 DPRINTF("Clean up device '%s'\n", dev->ifname);
236 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
240 dep->cb(dep, DEV_EVENT_REMOVE);
244 avl_delete(&devices, &dev->avl);
247 void device_set_present(struct device *dev, bool state)
249 if (dev->present == state)
252 DPRINTF("Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
253 dev->present = state;
254 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
257 void device_add_user(struct device_user *dep, struct device *dev)
260 list_add_tail(&dep->list, &dev->users);
261 if (dep->cb && dev->present) {
262 dep->cb(dep, DEV_EVENT_ADD);
264 dep->cb(dep, DEV_EVENT_UP);
269 __device_free_unused(struct device *dev)
271 if (!list_empty(&dev->users))
277 void device_remove_user(struct device_user *dep)
279 struct device *dev = dep->dev;
284 list_del(&dep->list);
286 __device_free_unused(dev);
290 device_free_unused(struct device *dev)
295 return __device_free_unused(dev);
297 avl_for_each_element_safe(&devices, dev, avl, tmp)
298 __device_free_unused(dev);