save and restore previous device settings when overriding them via config
[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 struct device_settings {
78 unsigned int flags;
79 unsigned int mtu;
80 unsigned int txqueuelen;
81 uint8_t macaddr[6];
82 };
83
84 /*
85 * link layer device. typically represents a linux network device.
86 * can be used to support VLANs as well
87 */
88 struct device {
89 const struct device_type *type;
90
91 struct avl_node avl;
92 struct list_head users;
93
94 char ifname[IFNAMSIZ + 1];
95 int ifindex;
96
97 struct blob_attr *config;
98 bool config_pending;
99 bool sys_present;
100 bool present;
101 int active;
102 bool external;
103 bool disabled;
104
105 bool current_config;
106 bool default_config;
107
108 /* set interface up or down */
109 device_state_cb set_state;
110
111 const struct device_hotplug_ops *hotplug_ops;
112
113 struct device_user parent;
114
115 struct device_settings orig_settings;
116 struct device_settings settings;
117 };
118
119 struct device_hotplug_ops {
120 int (*prepare)(struct device *dev);
121 int (*add)(struct device *main, struct device *member);
122 int (*del)(struct device *main, struct device *member);
123 };
124
125 extern const struct config_param_list device_attr_list;
126 extern const struct device_type simple_device_type;
127 extern const struct device_type bridge_device_type;
128
129 void device_lock(void);
130 void device_unlock(void);
131
132 struct device *device_create(const char *name, const struct device_type *type,
133 struct blob_attr *config);
134 void device_init_settings(struct device *dev, struct blob_attr **tb);
135 void device_init_pending(void);
136
137 enum dev_change_type
138 device_set_config(struct device *dev, const struct device_type *type,
139 struct blob_attr *attr);
140
141 void device_reset_config(void);
142 void device_reset_old(void);
143
144 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
145 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
146 void device_cleanup(struct device *iface);
147 struct device *device_get(const char *name, int create);
148 void device_add_user(struct device_user *dep, struct device *iface);
149 void device_remove_user(struct device_user *dep);
150
151 void device_set_present(struct device *dev, bool state);
152 void device_set_disabled(struct device *dev, bool value);
153 int device_claim(struct device_user *dep);
154 void device_release(struct device_user *dep);
155 int device_check_state(struct device *dev);
156 void device_dump_status(struct blob_buf *b, struct device *dev);
157
158 void device_free(struct device *dev);
159 void device_free_unused(struct device *dev);
160
161 struct device *get_vlan_device_chain(const char *ifname, bool create);
162 void alias_notify_device(const char *name, struct device *dev);
163
164 #endif