add code for cleaning up unused devices mentioned in the config
[project/netifd.git] / device.h
1 #ifndef __LL_H
2 #define __LL_H
3
4 #include <libubox/avl.h>
5
6 struct device;
7 struct device_hotplug_ops;
8
9 typedef int (*device_state_cb)(struct device *, bool up);
10
11 struct device_type {
12 const char *name;
13
14 void (*dump_status)(struct device *, struct blob_buf *buf);
15 int (*check_state)(struct device *);
16 void (*free)(struct device *);
17 };
18
19 enum {
20 DEV_OPT_MTU = (1 << 0),
21 DEV_OPT_MACADDR = (1 << 1),
22 DEV_OPT_TXQUEUELEN = (1 << 2)
23 };
24
25 /*
26 * link layer device. typically represents a linux network device.
27 * can be used to support VLANs as well
28 */
29 struct device {
30 const struct device_type *type;
31
32 struct avl_node avl;
33 struct list_head users;
34
35 char ifname[IFNAMSIZ + 1];
36 int ifindex;
37
38 bool present;
39 int active;
40
41 /* set interface up or down */
42 device_state_cb set_state;
43
44 const struct device_hotplug_ops *hotplug_ops;
45
46 /* settings */
47 unsigned int flags;
48
49 unsigned int mtu;
50 unsigned int txqueuelen;
51 uint8_t macaddr[6];
52
53 uint32_t config_hash;
54 };
55
56 /* events broadcasted to all users of a device */
57 enum device_event {
58 DEV_EVENT_ADD,
59 DEV_EVENT_REMOVE,
60
61 DEV_EVENT_SETUP,
62 DEV_EVENT_TEARDOWN,
63 DEV_EVENT_UP,
64 DEV_EVENT_DOWN,
65
66 DEV_EVENT_LINK_UP,
67 DEV_EVENT_LINK_DOWN,
68 };
69
70 /*
71 * device dependency with callbacks
72 */
73 struct device_user {
74 struct list_head list;
75
76 struct device *dev;
77 void (*cb)(struct device_user *, enum device_event);
78 };
79
80 struct device_hotplug_ops {
81 int (*add)(struct device *main, struct device *member);
82 int (*del)(struct device *main, struct device *member);
83 };
84
85 static inline void
86 free_device(struct device *dev)
87 {
88 dev->type->free(dev);
89 }
90
91 void init_virtual_device(struct device *dev, const struct device_type *type, const char *name);
92 int init_device(struct device *iface, const struct device_type *type, const char *ifname);
93 void cleanup_device(struct device *iface);
94 struct device *get_device(const char *name, bool create);
95 void add_device_user(struct device_user *dep, struct device *iface);
96 void remove_device_user(struct device_user *dep);
97
98 void set_device_present(struct device *dev, bool state);
99 int claim_device(struct device *dev);
100 void release_device(struct device *dev);
101 int check_device_state(struct device *dev);
102
103 void cleanup_devices(void);
104
105 struct device *get_vlan_device_chain(const char *ifname, bool create);
106 struct device *bridge_create(const char *name, struct uci_section *s);
107
108 #endif