7958fed7b4cc8df8413ff0efa9d6f68a397a4867
[project/netifd.git] / device.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <assert.h>
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
22
23 #ifdef linux
24 #include <netinet/ether.h>
25 #endif
26
27 #include "netifd.h"
28 #include "system.h"
29 #include "config.h"
30
31 static struct avl_tree devices;
32 static struct avl_tree aliases;
33
34 struct alias_device {
35 struct avl_node avl;
36 struct device dev;
37 struct device_user dep;
38 bool cleanup;
39 char name[];
40 };
41
42 static const struct device_type alias_device_type;
43
44 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
45 [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
46 [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
47 [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
48 [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
49 [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
50 [DEV_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_BOOL },
51 };
52
53 const struct config_param_list device_attr_list = {
54 .n_params = __DEV_ATTR_MAX,
55 .params = dev_attrs,
56 };
57
58 static int __devlock = 0;
59
60 void device_lock(void)
61 {
62 __devlock++;
63 }
64
65 void device_unlock(void)
66 {
67 __devlock--;
68 if (!__devlock)
69 device_free_unused(NULL);
70 }
71
72 static int set_device_state(struct device *dev, bool state)
73 {
74 if (state)
75 system_if_up(dev);
76 else
77 system_if_down(dev);
78
79 return 0;
80 }
81
82 static int
83 simple_device_set_state(struct device *dev, bool state)
84 {
85 struct device *pdev;
86 int ret = 0;
87
88 pdev = dev->parent.dev;
89 if (state && !pdev) {
90 pdev = system_if_get_parent(dev);
91 if (pdev)
92 device_add_user(&dev->parent, pdev);
93 }
94
95 if (pdev) {
96 if (state)
97 ret = device_claim(&dev->parent);
98 else
99 device_release(&dev->parent);
100
101 if (ret < 0)
102 return ret;
103 }
104 return set_device_state(dev, state);
105 }
106
107 static struct device *
108 simple_device_create(const char *name, struct blob_attr *attr)
109 {
110 struct blob_attr *tb[__DEV_ATTR_MAX];
111 struct device *dev = NULL;
112
113 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
114 dev = device_get(name, true);
115 if (!dev)
116 return NULL;
117
118 dev->set_state = simple_device_set_state;
119 device_init_settings(dev, tb);
120
121 return dev;
122 }
123
124 static void simple_device_free(struct device *dev)
125 {
126 if (dev->parent.dev)
127 device_remove_user(&dev->parent);
128 free(dev);
129 }
130
131 const struct device_type simple_device_type = {
132 .name = "Network device",
133 .config_params = &device_attr_list,
134
135 .create = simple_device_create,
136 .check_state = system_if_check,
137 .free = simple_device_free,
138 };
139
140 static int
141 alias_device_set_state(struct device *dev, bool state)
142 {
143 struct alias_device *alias;
144
145 alias = container_of(dev, struct alias_device, dev);
146 if (!alias->dep.dev)
147 return -1;
148
149 if (state)
150 return device_claim(&alias->dep);
151
152 device_release(&alias->dep);
153 if (alias->cleanup)
154 device_remove_user(&alias->dep);
155 return 0;
156 }
157
158 static struct device *
159 alias_device_create(const char *name, struct blob_attr *attr)
160 {
161 struct alias_device *alias;
162
163 alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
164 strcpy(alias->name, name);
165 alias->dev.set_state = alias_device_set_state;
166 device_init_virtual(&alias->dev, &alias_device_type, NULL);
167 alias->avl.key = alias->name;
168 avl_insert(&aliases, &alias->avl);
169
170 return &alias->dev;
171 }
172
173 static void alias_device_free(struct device *dev)
174 {
175 struct alias_device *alias;
176
177 alias = container_of(dev, struct alias_device, dev);
178 avl_delete(&aliases, &alias->avl);
179 free(alias);
180 }
181
182 static const struct device_type alias_device_type = {
183 .name = "Network alias",
184 .create = alias_device_create,
185 .free = alias_device_free,
186 };
187
188 static void
189 device_merge_settings(struct device *dev, struct device_settings *n)
190 {
191 struct device_settings *os = &dev->orig_settings;
192 struct device_settings *s = &dev->settings;
193
194 memset(n, 0, sizeof(*n));
195 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
196 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
197 s->txqueuelen : os->txqueuelen;
198 memcpy(n->macaddr,
199 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
200 sizeof(n->macaddr));
201 n->flags = s->flags | os->flags;
202 }
203
204 void
205 device_init_settings(struct device *dev, struct blob_attr **tb)
206 {
207 struct device_settings *s = &dev->settings;
208 struct blob_attr *cur;
209 struct ether_addr *ea;
210 bool disabled = false;
211
212 s->flags = 0;
213 if ((cur = tb[DEV_ATTR_ENABLED]))
214 disabled = !blobmsg_get_bool(cur);
215
216 if ((cur = tb[DEV_ATTR_MTU])) {
217 s->mtu = blobmsg_get_u32(cur);
218 s->flags |= DEV_OPT_MTU;
219 }
220
221 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
222 s->txqueuelen = blobmsg_get_u32(cur);
223 s->flags |= DEV_OPT_TXQUEUELEN;
224 }
225
226 if ((cur = tb[DEV_ATTR_MACADDR])) {
227 ea = ether_aton(blobmsg_data(cur));
228 if (ea) {
229 memcpy(s->macaddr, ea, 6);
230 s->flags |= DEV_OPT_MACADDR;
231 }
232 }
233
234 device_set_disabled(dev, disabled);
235 }
236
237 static void __init dev_init(void)
238 {
239 avl_init(&devices, avl_strcmp, true, NULL);
240 avl_init(&aliases, avl_strcmp, false, NULL);
241 }
242
243 static void device_broadcast_event(struct device *dev, enum device_event ev)
244 {
245 struct device_user *dep, *tmp;
246
247 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
248 if (!dep->cb)
249 continue;
250
251 dep->cb(dep, ev);
252 }
253 }
254
255 void
256 alias_notify_device(const char *name, struct device *dev)
257 {
258 struct alias_device *alias;
259
260 device_lock();
261
262 alias = avl_find_element(&aliases, name, alias, avl);
263 if (!alias)
264 return;
265
266 alias->cleanup = !dev;
267 if (dev) {
268 if (dev != alias->dep.dev) {
269 device_remove_user(&alias->dep);
270 strcpy(alias->dev.ifname, dev->ifname);
271 device_add_user(&alias->dep, dev);
272 }
273 }
274
275 device_set_present(&alias->dev, !!dev);
276
277 if (!dev && alias->dep.dev && !alias->dep.dev->active)
278 device_remove_user(&alias->dep);
279
280 device_unlock();
281 }
282
283 int device_claim(struct device_user *dep)
284 {
285 struct device *dev = dep->dev;
286 int ret;
287
288 if (dep->claimed)
289 return 0;
290
291 dep->claimed = true;
292 D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1);
293 if (++dev->active != 1)
294 return 0;
295
296 device_broadcast_event(dev, DEV_EVENT_SETUP);
297 ret = dev->set_state(dev, true);
298 if (ret == 0)
299 device_broadcast_event(dev, DEV_EVENT_UP);
300 else {
301 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
302 dev->active = 0;
303 dep->claimed = false;
304 }
305
306 return ret;
307 }
308
309 void device_release(struct device_user *dep)
310 {
311 struct device *dev = dep->dev;
312
313 if (!dep->claimed)
314 return;
315
316 dep->claimed = false;
317 dev->active--;
318 D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active);
319 assert(dev->active >= 0);
320
321 if (dev->active)
322 return;
323
324 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
325 if (!dep->hotplug)
326 dev->set_state(dev, false);
327 device_broadcast_event(dev, DEV_EVENT_DOWN);
328 }
329
330 int device_check_state(struct device *dev)
331 {
332 if (!dev->type->check_state)
333 return 0;
334
335 return dev->type->check_state(dev);
336 }
337
338 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
339 {
340 assert(dev);
341 assert(type);
342
343 if (name)
344 strncpy(dev->ifname, name, IFNAMSIZ);
345
346 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
347 INIT_LIST_HEAD(&dev->users);
348 dev->type = type;
349 }
350
351 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
352 {
353 int ret;
354
355 device_init_virtual(dev, type, ifname);
356
357 if (!dev->set_state)
358 dev->set_state = set_device_state;
359
360 dev->avl.key = dev->ifname;
361
362 ret = avl_insert(&devices, &dev->avl);
363 if (ret < 0)
364 return ret;
365
366 system_if_clear_state(dev);
367 device_check_state(dev);
368
369 return 0;
370 }
371
372 static struct device *
373 device_create_default(const char *name, bool external)
374 {
375 struct device *dev;
376
377 D(DEVICE, "Create simple device '%s'\n", name);
378 dev = calloc(1, sizeof(*dev));
379 dev->external = external;
380 dev->set_state = simple_device_set_state;
381 device_init(dev, &simple_device_type, name);
382 dev->default_config = true;
383 return dev;
384 }
385
386 static struct device *
387 device_alias_get(const char *name)
388 {
389 struct alias_device *alias;
390
391 alias = avl_find_element(&aliases, name, alias, avl);
392 if (alias)
393 return &alias->dev;
394
395 return alias_device_create(name, NULL);
396 }
397
398 struct device *
399 device_get(const char *name, int create)
400 {
401 struct device *dev;
402
403 if (strchr(name, '.'))
404 return get_vlan_device_chain(name, create);
405
406 if (name[0] == '@')
407 return device_alias_get(name + 1);
408
409 dev = avl_find_element(&devices, name, dev, avl);
410 if (dev)
411 return dev;
412
413 if (!create)
414 return NULL;
415
416 return device_create_default(name, create > 1);
417 }
418
419 static void
420 device_delete(struct device *dev)
421 {
422 if (!dev->avl.key)
423 return;
424
425 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
426 avl_delete(&devices, &dev->avl);
427 dev->avl.key = NULL;
428 }
429
430 void device_cleanup(struct device *dev)
431 {
432 struct device_user *dep, *tmp;
433
434 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
435 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
436 if (!dep->cb)
437 continue;
438
439 dep->cb(dep, DEV_EVENT_REMOVE);
440 device_release(dep);
441 }
442
443 device_delete(dev);
444 }
445
446 static void __device_set_present(struct device *dev, bool state)
447 {
448 if (dev->present == state)
449 return;
450
451 dev->present = state;
452 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
453 }
454
455 void
456 device_refresh_present(struct device *dev)
457 {
458 bool state = dev->sys_present;
459
460 if (dev->disabled || dev->deferred)
461 state = false;
462
463 __device_set_present(dev, state);
464 }
465
466 void device_set_present(struct device *dev, bool state)
467 {
468 if (dev->sys_present == state)
469 return;
470
471 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
472 dev->sys_present = state;
473 device_refresh_present(dev);
474 }
475
476 void device_add_user(struct device_user *dep, struct device *dev)
477 {
478 if (dep->dev)
479 device_remove_user(dep);
480
481 if (!dev)
482 return;
483
484 dep->dev = dev;
485 list_add_tail(&dep->list, &dev->users);
486 if (dep->cb && dev->present) {
487 dep->cb(dep, DEV_EVENT_ADD);
488 if (dev->active)
489 dep->cb(dep, DEV_EVENT_UP);
490 }
491 }
492
493 void
494 device_free(struct device *dev)
495 {
496 __devlock++;
497 free(dev->config);
498 device_cleanup(dev);
499 dev->type->free(dev);
500 __devlock--;
501 }
502
503 static void
504 __device_free_unused(struct device *dev)
505 {
506 if (!list_empty(&dev->users) || dev->current_config || __devlock)
507 return;
508
509 device_free(dev);
510 }
511
512 void device_remove_user(struct device_user *dep)
513 {
514 struct device *dev = dep->dev;
515
516 if (!dep->dev)
517 return;
518
519 dep->hotplug = false;
520 if (dep->claimed)
521 device_release(dep);
522
523 list_del(&dep->list);
524 dep->dev = NULL;
525 __device_free_unused(dev);
526 }
527
528 void
529 device_free_unused(struct device *dev)
530 {
531 struct device *tmp;
532
533 if (dev)
534 return __device_free_unused(dev);
535
536 avl_for_each_element_safe(&devices, dev, avl, tmp)
537 __device_free_unused(dev);
538 }
539
540 void
541 device_init_pending(void)
542 {
543 struct device *dev, *tmp;
544
545 avl_for_each_element_safe(&devices, dev, avl, tmp) {
546 if (!dev->config_pending)
547 continue;
548
549 dev->type->config_init(dev);
550 dev->config_pending = false;
551 }
552 }
553
554 static enum dev_change_type
555 device_reload_config(struct device *dev, struct blob_attr *attr)
556 {
557 struct blob_attr *tb[__DEV_ATTR_MAX];
558 const struct config_param_list *cfg = dev->type->config_params;
559
560 if (config_check_equal(dev->config, attr, cfg))
561 return DEV_CONFIG_NO_CHANGE;
562
563 if (cfg == &device_attr_list) {
564 memset(tb, 0, sizeof(tb));
565
566 if (attr)
567 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
568 blob_data(attr), blob_len(attr));
569
570 device_init_settings(dev, tb);
571 return DEV_CONFIG_RESTART;
572 } else
573 return DEV_CONFIG_RECREATE;
574 }
575
576 enum dev_change_type
577 device_set_config(struct device *dev, const struct device_type *type,
578 struct blob_attr *attr)
579 {
580 if (type != dev->type)
581 return DEV_CONFIG_RECREATE;
582
583 if (dev->type->reload)
584 return dev->type->reload(dev, attr);
585
586 return device_reload_config(dev, attr);
587 }
588
589 static void
590 device_replace(struct device *dev, struct device *odev)
591 {
592 struct device_user *dep, *tmp;
593 bool present = odev->present;
594
595 if (present)
596 device_set_present(odev, false);
597
598 list_for_each_entry_safe(dep, tmp, &odev->users, list) {
599 device_release(dep);
600 list_move_tail(&dep->list, &dev->users);
601 dep->dev = dev;
602 }
603 device_free(odev);
604
605 if (present)
606 device_set_present(dev, true);
607 }
608
609 void
610 device_reset_config(void)
611 {
612 struct device *dev;
613
614 avl_for_each_element(&devices, dev, avl)
615 dev->current_config = false;
616 }
617
618 void
619 device_reset_old(void)
620 {
621 struct device *dev, *tmp, *ndev;
622
623 avl_for_each_element_safe(&devices, dev, avl, tmp) {
624 if (dev->current_config || dev->default_config)
625 continue;
626
627 if (dev->type != &simple_device_type)
628 continue;
629
630 ndev = device_create_default(dev->ifname, dev->external);
631 device_replace(ndev, dev);
632 }
633 }
634
635 struct device *
636 device_create(const char *name, const struct device_type *type,
637 struct blob_attr *config)
638 {
639 struct device *odev = NULL, *dev;
640 enum dev_change_type change;
641
642 config = config_memdup(config);
643 if (!config)
644 return NULL;
645
646 odev = device_get(name, false);
647 if (odev) {
648 odev->current_config = true;
649 change = device_set_config(odev, type, config);
650 switch (change) {
651 case DEV_CONFIG_RESTART:
652 case DEV_CONFIG_APPLIED:
653 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
654 free(odev->config);
655 odev->config = config;
656 if (change == DEV_CONFIG_RESTART && odev->present) {
657 device_set_present(odev, false);
658 device_set_present(odev, true);
659 }
660 return odev;
661 case DEV_CONFIG_NO_CHANGE:
662 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
663 free(config);
664 return odev;
665 case DEV_CONFIG_RECREATE:
666 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
667 device_delete(odev);
668 break;
669 }
670 } else
671 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
672
673 dev = type->create(name, config);
674 if (!dev)
675 return NULL;
676
677 dev->current_config = true;
678 dev->config = config;
679 if (odev)
680 device_replace(dev, odev);
681
682 if (!config_init && dev->config_pending)
683 type->config_init(dev);
684
685 return dev;
686 }
687
688 void
689 device_dump_status(struct blob_buf *b, struct device *dev)
690 {
691 struct device_settings st;
692 void *c, *s;
693
694 if (!dev) {
695 avl_for_each_element(&devices, dev, avl) {
696 if (!dev->present)
697 continue;
698 c = blobmsg_open_table(b, dev->ifname);
699 device_dump_status(b, dev);
700 blobmsg_close_table(b, c);
701 }
702
703 return;
704 }
705
706 blobmsg_add_u8(b, "external", dev->external);
707 blobmsg_add_u8(b, "present", dev->present);
708 blobmsg_add_string(b, "type", dev->type->name);
709
710 if (!dev->present)
711 return;
712
713 blobmsg_add_u8(b, "up", !!dev->active);
714 if (dev->type->dump_info)
715 dev->type->dump_info(dev, b);
716 else
717 system_if_dump_info(dev, b);
718
719 if (dev->active) {
720 device_merge_settings(dev, &st);
721 if (st.flags & DEV_OPT_MTU)
722 blobmsg_add_u32(b, "mtu", st.mtu);
723 if (st.flags & DEV_OPT_MACADDR)
724 blobmsg_add_string(b, "macaddr", ether_ntoa((struct ether_addr *) st.macaddr));
725 if (st.flags & DEV_OPT_TXQUEUELEN)
726 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
727 }
728
729 s = blobmsg_open_table(b, "statistics");
730 if (dev->type->dump_stats)
731 dev->type->dump_stats(dev, b);
732 else
733 system_if_dump_stats(dev, b);
734 blobmsg_close_table(b, s);
735 }