45af150c09e5d03844cfd0253fda7530c0953c7d
[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 enum dev_change_type {
23         DEV_CONFIG_NO_CHANGE,
24         DEV_CONFIG_APPLIED,
25         DEV_CONFIG_RECREATE,
26 };
27
28 struct device_type {
29         struct list_head list;
30         const char *name;
31
32         const struct config_param_list *config_params;
33
34         struct device *(*create)(struct blob_attr *attr);
35         void (*config_init)(struct device *);
36         enum dev_change_type (*reload)(struct device *, struct blob_attr *);
37         void (*dump_status)(struct device *, struct blob_buf *buf);
38         int (*check_state)(struct device *);
39         void (*free)(struct device *);
40 };
41
42 enum {
43         DEV_OPT_MTU             = (1 << 0),
44         DEV_OPT_MACADDR         = (1 << 1),
45         DEV_OPT_TXQUEUELEN      = (1 << 2)
46 };
47
48 /* 
49  * link layer device. typically represents a linux network device.
50  * can be used to support VLANs as well
51  */
52 struct device {
53         const struct device_type *type;
54
55         struct avl_node avl;
56         struct list_head users;
57
58         char ifname[IFNAMSIZ + 1];
59         int ifindex;
60
61         struct blob_attr *config;
62         bool config_pending;
63         bool present;
64         int active;
65
66         /* set interface up or down */
67         device_state_cb set_state;
68
69         const struct device_hotplug_ops *hotplug_ops;
70
71         /* settings */
72         unsigned int flags;
73
74         unsigned int mtu;
75         unsigned int txqueuelen;
76         uint8_t macaddr[6];
77 };
78
79 /* events broadcasted to all users of a device */
80 enum device_event {
81         DEV_EVENT_ADD,
82         DEV_EVENT_REMOVE,
83
84         DEV_EVENT_SETUP,
85         DEV_EVENT_TEARDOWN,
86         DEV_EVENT_UP,
87         DEV_EVENT_DOWN,
88
89         DEV_EVENT_LINK_UP,
90         DEV_EVENT_LINK_DOWN,
91 };
92
93 /*
94  * device dependency with callbacks
95  */
96 struct device_user {
97         struct list_head list;
98
99         bool claimed;
100         struct device *dev;
101         void (*cb)(struct device_user *, enum device_event);
102 };
103
104 struct device_hotplug_ops {
105         int (*add)(struct device *main, struct device *member);
106         int (*del)(struct device *main, struct device *member);
107 };
108
109 extern const struct config_param_list device_attr_list;
110 extern const struct device_type simple_device_type;
111 extern const struct device_type bridge_device_type;
112
113 struct device *device_create(const char *name, const struct device_type *type,
114                              struct blob_attr *config);
115 void device_init_settings(struct device *dev, struct blob_attr **tb);
116 void device_init_pending(void);
117
118 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
119 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
120 void device_cleanup(struct device *iface);
121 struct device *device_get(const char *name, bool create);
122 void device_add_user(struct device_user *dep, struct device *iface);
123 void device_remove_user(struct device_user *dep);
124
125 void device_set_present(struct device *dev, bool state);
126 int device_claim(struct device_user *dep);
127 void device_release(struct device_user *dep);
128 int device_check_state(struct device *dev);
129
130 static inline void
131 device_free(struct device *dev)
132 {
133         dev->type->free(dev);
134 }
135
136 void device_free_unused(struct device *dev);
137
138 struct device *get_vlan_device_chain(const char *ifname, bool create);
139
140 #endif