device: prepare for adding device handlers dynamically
[project/netifd.git] / device.h
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 #ifndef __NETIFD_DEVICE_H
15 #define __NETIFD_DEVICE_H
16
17 #include <libubox/avl.h>
18 #include <libubox/safe_list.h>
19 #include <netinet/in.h>
20
21 struct device;
22 struct device_user;
23 struct device_hotplug_ops;
24 struct interface;
25
26 typedef int (*device_state_cb)(struct device *, bool up);
27
28 enum {
29 DEV_ATTR_TYPE,
30 DEV_ATTR_MTU,
31 DEV_ATTR_MTU6,
32 DEV_ATTR_MACADDR,
33 DEV_ATTR_TXQUEUELEN,
34 DEV_ATTR_ENABLED,
35 DEV_ATTR_IPV6,
36 DEV_ATTR_PROMISC,
37 DEV_ATTR_RPFILTER,
38 DEV_ATTR_ACCEPTLOCAL,
39 DEV_ATTR_IGMPVERSION,
40 DEV_ATTR_MLDVERSION,
41 DEV_ATTR_NEIGHREACHABLETIME,
42 DEV_ATTR_RPS,
43 DEV_ATTR_XPS,
44 DEV_ATTR_DADTRANSMITS,
45 DEV_ATTR_MULTICAST_TO_UNICAST,
46 DEV_ATTR_MULTICAST_ROUTER,
47 DEV_ATTR_MULTICAST,
48 DEV_ATTR_LEARNING,
49 DEV_ATTR_UNICAST_FLOOD,
50 DEV_ATTR_NEIGHGCSTALETIME,
51 __DEV_ATTR_MAX,
52 };
53
54 enum dev_change_type {
55 DEV_CONFIG_NO_CHANGE,
56 DEV_CONFIG_APPLIED,
57 DEV_CONFIG_RESTART,
58 DEV_CONFIG_RECREATE,
59 };
60
61 struct device_type {
62 struct list_head list;
63 const char *name;
64
65 const struct uci_blob_param_list *config_params;
66
67 struct device *(*create)(const char *name, struct device_type *devtype,
68 struct blob_attr *attr);
69 void (*config_init)(struct device *);
70 enum dev_change_type (*reload)(struct device *, struct blob_attr *);
71 void (*dump_info)(struct device *, struct blob_buf *buf);
72 void (*dump_stats)(struct device *, struct blob_buf *buf);
73 int (*check_state)(struct device *);
74 void (*free)(struct device *);
75 };
76
77 enum {
78 DEV_OPT_MTU = (1 << 0),
79 DEV_OPT_MACADDR = (1 << 1),
80 DEV_OPT_TXQUEUELEN = (1 << 2),
81 DEV_OPT_IPV6 = (1 << 3),
82 DEV_OPT_PROMISC = (1 << 4),
83 DEV_OPT_RPFILTER = (1 << 5),
84 DEV_OPT_ACCEPTLOCAL = (1 << 6),
85 DEV_OPT_IGMPVERSION = (1 << 7),
86 DEV_OPT_MLDVERSION = (1 << 8),
87 DEV_OPT_NEIGHREACHABLETIME = (1 << 9),
88 DEV_OPT_RPS = (1 << 10),
89 DEV_OPT_XPS = (1 << 11),
90 DEV_OPT_MTU6 = (1 << 12),
91 DEV_OPT_DADTRANSMITS = (1 << 13),
92 DEV_OPT_MULTICAST_TO_UNICAST = (1 << 14),
93 DEV_OPT_MULTICAST_ROUTER = (1 << 15),
94 DEV_OPT_MULTICAST = (1 << 16),
95 DEV_OPT_LEARNING = (1 << 17),
96 DEV_OPT_UNICAST_FLOOD = (1 << 18),
97 DEV_OPT_NEIGHGCSTALETIME = (1 << 19),
98 };
99
100 /* events broadcasted to all users of a device */
101 enum device_event {
102 DEV_EVENT_ADD,
103 DEV_EVENT_REMOVE,
104
105 DEV_EVENT_UPDATE_IFNAME,
106 DEV_EVENT_UPDATE_IFINDEX,
107
108 DEV_EVENT_SETUP,
109 DEV_EVENT_TEARDOWN,
110 DEV_EVENT_UP,
111 DEV_EVENT_DOWN,
112
113 DEV_EVENT_LINK_UP,
114 DEV_EVENT_LINK_DOWN,
115
116 /* Topology changed (i.e. bridge member added) */
117 DEV_EVENT_TOPO_CHANGE,
118
119 __DEV_EVENT_MAX
120 };
121
122 /*
123 * device dependency with callbacks
124 */
125 struct device_user {
126 struct safe_list list;
127
128 bool claimed;
129 bool hotplug;
130 bool alias;
131
132 uint8_t ev_idx[__DEV_EVENT_MAX];
133
134 struct device *dev;
135 void (*cb)(struct device_user *, enum device_event);
136 };
137
138 struct device_settings {
139 unsigned int flags;
140 unsigned int valid_flags;
141 unsigned int mtu;
142 unsigned int mtu6;
143 unsigned int txqueuelen;
144 uint8_t macaddr[6];
145 bool ipv6;
146 bool promisc;
147 unsigned int rpfilter;
148 bool acceptlocal;
149 unsigned int igmpversion;
150 unsigned int mldversion;
151 unsigned int neigh4reachabletime;
152 unsigned int neigh6reachabletime;
153 unsigned int neigh4gcstaletime;
154 unsigned int neigh6gcstaletime;
155 bool rps;
156 bool xps;
157 unsigned int dadtransmits;
158 bool multicast_to_unicast;
159 unsigned int multicast_router;
160 bool multicast;
161 bool learning;
162 bool unicast_flood;
163 };
164
165 /*
166 * link layer device. typically represents a linux network device.
167 * can be used to support VLANs as well
168 */
169 struct device {
170 struct device_type *type;
171
172 struct avl_node avl;
173 struct safe_list users;
174 struct safe_list aliases;
175
176 char ifname[IFNAMSIZ + 1];
177 int ifindex;
178
179 struct blob_attr *config;
180 bool config_pending;
181 bool sys_present;
182 /* DEV_EVENT_ADD */
183 bool present;
184 /* DEV_EVENT_UP */
185 int active;
186 /* DEV_EVENT_LINK_UP */
187 bool link_active;
188
189 bool external;
190 bool disabled;
191 bool deferred;
192 bool hidden;
193
194 bool current_config;
195 bool iface_config;
196 bool default_config;
197 bool wireless;
198 bool wireless_ap;
199 bool wireless_isolate;
200
201 struct interface *config_iface;
202
203 /* set interface up or down */
204 device_state_cb set_state;
205
206 const struct device_hotplug_ops *hotplug_ops;
207
208 struct device_user parent;
209
210 struct device_settings orig_settings;
211 struct device_settings settings;
212 };
213
214 struct device_hotplug_ops {
215 int (*prepare)(struct device *dev);
216 int (*add)(struct device *main, struct device *member);
217 int (*del)(struct device *main, struct device *member);
218 };
219
220 extern const struct uci_blob_param_list device_attr_list;
221 extern struct device_type simple_device_type;
222 extern struct device_type bridge_device_type;
223 extern struct device_type tunnel_device_type;
224 extern struct device_type macvlan_device_type;
225 extern struct device_type vlandev_device_type;
226
227 void device_lock(void);
228 void device_unlock(void);
229
230 struct device *device_create(const char *name, struct device_type *type,
231 struct blob_attr *config);
232 void device_init_settings(struct device *dev, struct blob_attr **tb);
233 void device_init_pending(void);
234
235 enum dev_change_type
236 device_apply_config(struct device *dev, struct device_type *type,
237 struct blob_attr *config);
238
239 void device_reset_config(void);
240 void device_reset_old(void);
241 void device_set_default_ps(bool state);
242
243 void device_init_virtual(struct device *dev, struct device_type *type, const char *name);
244 int device_init(struct device *iface, struct device_type *type, const char *ifname);
245 void device_cleanup(struct device *dev);
246 struct device *device_find(const char *name);
247 struct device *device_get(const char *name, int create);
248 void device_add_user(struct device_user *dep, struct device *iface);
249 void device_remove_user(struct device_user *dep);
250 void device_broadcast_event(struct device *dev, enum device_event ev);
251
252 void device_set_present(struct device *dev, bool state);
253 void device_set_link(struct device *dev, bool state);
254 void device_set_ifindex(struct device *dev, int ifindex);
255 int device_set_ifname(struct device *dev, const char *name);
256 void device_refresh_present(struct device *dev);
257 int device_claim(struct device_user *dep);
258 void device_release(struct device_user *dep);
259 int device_check_state(struct device *dev);
260 void device_dump_status(struct blob_buf *b, struct device *dev);
261
262 void device_free(struct device *dev);
263 void device_free_unused(struct device *dev);
264
265 struct device *get_vlan_device_chain(const char *ifname, bool create);
266 void alias_notify_device(const char *name, struct device *dev);
267 struct device *device_alias_get(const char *name);
268
269 static inline void
270 device_set_deferred(struct device *dev, bool value)
271 {
272 dev->deferred = value;
273 device_refresh_present(dev);
274 }
275
276 static inline void
277 device_set_disabled(struct device *dev, bool value)
278 {
279 dev->disabled = value;
280 device_refresh_present(dev);
281 }
282
283 #endif