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