constify
[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 /* 
41  * link layer device. typically represents a linux network device.
42  * can be used to support VLANs as well
43  */
44 struct device {
45         const struct device_type *type;
46
47         struct avl_node avl;
48         struct list_head users;
49
50         char ifname[IFNAMSIZ + 1];
51         int ifindex;
52
53         bool present;
54         int active;
55
56         /* set interface up or down */
57         device_state_cb set_state;
58
59         const struct device_hotplug_ops *hotplug_ops;
60
61         /* settings */
62         unsigned int flags;
63
64         unsigned int mtu;
65         unsigned int txqueuelen;
66         uint8_t macaddr[6];
67
68         uint32_t config_hash;
69 };
70
71 /* events broadcasted to all users of a device */
72 enum device_event {
73         DEV_EVENT_ADD,
74         DEV_EVENT_REMOVE,
75
76         DEV_EVENT_SETUP,
77         DEV_EVENT_TEARDOWN,
78         DEV_EVENT_UP,
79         DEV_EVENT_DOWN,
80
81         DEV_EVENT_LINK_UP,
82         DEV_EVENT_LINK_DOWN,
83 };
84
85 /*
86  * device dependency with callbacks
87  */
88 struct device_user {
89         struct list_head list;
90
91         bool claimed;
92         struct device *dev;
93         void (*cb)(struct device_user *, enum device_event);
94 };
95
96 struct device_hotplug_ops {
97         int (*add)(struct device *main, struct device *member);
98         int (*del)(struct device *main, struct device *member);
99 };
100
101 extern const struct config_param_list device_attr_list;
102 extern const struct device_type simple_device_type;
103 extern const struct device_type bridge_device_type;
104
105 void device_init_settings(struct device *dev, struct blob_attr **tb);
106
107 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
108 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
109 void device_cleanup(struct device *iface);
110 struct device *device_get(const char *name, bool create);
111 void device_add_user(struct device_user *dep, struct device *iface);
112 void device_remove_user(struct device_user *dep);
113
114 void device_set_present(struct device *dev, bool state);
115 int device_claim(struct device_user *dep);
116 void device_release(struct device_user *dep);
117 int check_device_state(struct device *dev);
118
119 static inline void
120 device_free(struct device *dev)
121 {
122         dev->type->free(dev);
123 }
124
125 void device_free_unused(struct device *dev);
126
127 struct device *get_vlan_device_chain(const char *ifname, bool create);
128
129 #endif