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