allow devices to be disabled through a config option
[project/netifd.git] / device.h
1 #ifndef __LL_H
2 #define __LL_H
3
4 #include <libubox/avl.h>
5 #include <netinet/in.h>
6
7 struct device;
8 struct device_user;
9 struct device_hotplug_ops;
10
11 typedef int (*device_state_cb)(struct device *, bool up);
12
13 enum {
14 DEV_ATTR_TYPE,
15 DEV_ATTR_IFNAME,
16 DEV_ATTR_MTU,
17 DEV_ATTR_MACADDR,
18 DEV_ATTR_TXQUEUELEN,
19 DEV_ATTR_ENABLED,
20 __DEV_ATTR_MAX,
21 };
22
23 enum dev_change_type {
24 DEV_CONFIG_NO_CHANGE,
25 DEV_CONFIG_APPLIED,
26 DEV_CONFIG_RECREATE,
27 };
28
29 struct device_type {
30 struct list_head list;
31 const char *name;
32
33 const struct config_param_list *config_params;
34
35 struct device *(*create)(const char *name, struct blob_attr *attr);
36 void (*config_init)(struct device *);
37 enum dev_change_type (*reload)(struct device *, struct blob_attr *);
38 void (*dump_info)(struct device *, struct blob_buf *buf);
39 void (*dump_stats)(struct device *, struct blob_buf *buf);
40 int (*check_state)(struct device *);
41 void (*free)(struct device *);
42 };
43
44 enum {
45 DEV_OPT_MTU = (1 << 0),
46 DEV_OPT_MACADDR = (1 << 1),
47 DEV_OPT_TXQUEUELEN = (1 << 2)
48 };
49
50 /* events broadcasted to all users of a device */
51 enum device_event {
52 DEV_EVENT_ADD,
53 DEV_EVENT_REMOVE,
54
55 DEV_EVENT_SETUP,
56 DEV_EVENT_TEARDOWN,
57 DEV_EVENT_UP,
58 DEV_EVENT_DOWN,
59
60 DEV_EVENT_LINK_UP,
61 DEV_EVENT_LINK_DOWN,
62 };
63
64 /*
65 * device dependency with callbacks
66 */
67 struct device_user {
68 struct list_head list;
69
70 bool claimed;
71 bool hotplug;
72
73 struct device *dev;
74 void (*cb)(struct device_user *, enum device_event);
75 };
76
77 /*
78 * link layer device. typically represents a linux network device.
79 * can be used to support VLANs as well
80 */
81 struct device {
82 const struct device_type *type;
83
84 struct avl_node avl;
85 struct list_head users;
86
87 char ifname[IFNAMSIZ + 1];
88 int ifindex;
89
90 struct blob_attr *config;
91 bool config_pending;
92 bool sys_present;
93 bool present;
94 int active;
95 bool external;
96 bool disabled;
97
98 bool current_config;
99 bool default_config;
100
101 /* set interface up or down */
102 device_state_cb set_state;
103
104 const struct device_hotplug_ops *hotplug_ops;
105
106 struct device_user parent;
107
108 /* settings */
109 unsigned int flags;
110
111 unsigned int mtu;
112 unsigned int txqueuelen;
113 uint8_t macaddr[6];
114 };
115
116 struct device_hotplug_ops {
117 int (*prepare)(struct device *dev);
118 int (*add)(struct device *main, struct device *member);
119 int (*del)(struct device *main, struct device *member);
120 };
121
122 extern const struct config_param_list device_attr_list;
123 extern const struct device_type simple_device_type;
124 extern const struct device_type bridge_device_type;
125
126 void device_lock(void);
127 void device_unlock(void);
128
129 struct device *device_create(const char *name, const struct device_type *type,
130 struct blob_attr *config);
131 void device_init_settings(struct device *dev, struct blob_attr **tb);
132 void device_init_pending(void);
133
134 enum dev_change_type
135 device_set_config(struct device *dev, const struct device_type *type,
136 struct blob_attr *attr);
137
138 void device_reset_config(void);
139 void device_reset_old(void);
140
141 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
142 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
143 void device_cleanup(struct device *iface);
144 struct device *device_get(const char *name, int create);
145 void device_add_user(struct device_user *dep, struct device *iface);
146 void device_remove_user(struct device_user *dep);
147
148 void device_set_present(struct device *dev, bool state);
149 void device_set_disabled(struct device *dev, bool value);
150 int device_claim(struct device_user *dep);
151 void device_release(struct device_user *dep);
152 int device_check_state(struct device *dev);
153 void device_dump_status(struct blob_buf *b, struct device *dev);
154
155 void device_free(struct device *dev);
156 void device_free_unused(struct device *dev);
157
158 struct device *get_vlan_device_chain(const char *ifname, bool create);
159 void alias_notify_device(const char *name, struct device *dev);
160
161 #endif