make bridge configuration more dynamic
[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_hotplug_ops;
9
10 typedef int (*device_state_cb)(struct device *, bool up);
11
12 enum {
13         DEV_ATTR_TYPE,
14         DEV_ATTR_NAME,
15         DEV_ATTR_IFNAME,
16         DEV_ATTR_MTU,
17         DEV_ATTR_MACADDR,
18         DEV_ATTR_TXQUEUELEN,
19         __DEV_ATTR_MAX,
20 };
21
22 struct device_type {
23         struct list_head list;
24         const char *name;
25
26         const struct config_param_list *config_params;
27
28         struct device *(*create)(struct blob_attr *attr);
29         void (*dump_status)(struct device *, struct blob_buf *buf);
30         int (*check_state)(struct device *);
31         void (*free)(struct device *);
32 };
33
34 enum {
35         DEV_OPT_MTU             = (1 << 0),
36         DEV_OPT_MACADDR         = (1 << 1),
37         DEV_OPT_TXQUEUELEN      = (1 << 2)
38 };
39
40 enum device_addr_flags {
41         /* address family for routes and addresses */
42         DEVADDR_INET4   = (0 << 0),
43         DEVADDR_INET6   = (1 << 0),
44         DEVADDR_FAMILY  = DEVADDR_INET4 | DEVADDR_INET6,
45
46         /* device route (no gateway) */
47         DEVADDR_DEVICE  = (1 << 1),
48 };
49
50 union if_addr {
51         struct in_addr in;
52         struct in6_addr in6;
53 };
54
55 struct device_addr {
56         struct list_head list;
57         void *ctx;
58
59         enum device_addr_flags flags;
60
61         unsigned int mask;
62         union if_addr addr;
63 };
64
65 struct device_route {
66         struct list_head list;
67         void *ctx;
68
69         enum device_addr_flags flags;
70
71         unsigned int mask;
72         union if_addr addr;
73         union if_addr nexthop;
74 };
75
76 /* 
77  * link layer device. typically represents a linux network device.
78  * can be used to support VLANs as well
79  */
80 struct device {
81         const struct device_type *type;
82
83         struct avl_node avl;
84         struct list_head users;
85
86         char ifname[IFNAMSIZ + 1];
87         int ifindex;
88
89         bool present;
90         int active;
91
92         /* set interface up or down */
93         device_state_cb set_state;
94
95         const struct device_hotplug_ops *hotplug_ops;
96
97         /* settings */
98         unsigned int flags;
99
100         unsigned int mtu;
101         unsigned int txqueuelen;
102         uint8_t macaddr[6];
103
104         uint32_t config_hash;
105 };
106
107 /* events broadcasted to all users of a device */
108 enum device_event {
109         DEV_EVENT_ADD,
110         DEV_EVENT_REMOVE,
111
112         DEV_EVENT_SETUP,
113         DEV_EVENT_TEARDOWN,
114         DEV_EVENT_UP,
115         DEV_EVENT_DOWN,
116
117         DEV_EVENT_LINK_UP,
118         DEV_EVENT_LINK_DOWN,
119 };
120
121 /*
122  * device dependency with callbacks
123  */
124 struct device_user {
125         struct list_head list;
126
127         struct device *dev;
128         void (*cb)(struct device_user *, enum device_event);
129 };
130
131 struct device_hotplug_ops {
132         int (*add)(struct device *main, struct device *member);
133         int (*del)(struct device *main, struct device *member);
134 };
135
136 extern const struct config_param_list device_attr_list;
137 extern const struct device_type simple_device_type;
138 extern const struct device_type bridge_device_type;
139
140 void device_init_settings(struct device *dev, struct blob_attr **tb);
141
142 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
143 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
144 void device_cleanup(struct device *iface);
145 struct device *device_get(const char *name, bool create);
146 void device_add_user(struct device_user *dep, struct device *iface);
147 void device_remove_user(struct device_user *dep);
148
149 void device_set_present(struct device *dev, bool state);
150 int device_claim(struct device *dev);
151 void device_release(struct device *dev);
152 int check_device_state(struct device *dev);
153
154 static inline void
155 device_free(struct device *dev)
156 {
157         dev->type->free(dev);
158 }
159
160 void device_free_all(void);
161
162 struct device *get_vlan_device_chain(const char *ifname, bool create);
163
164 #endif