add a callback for dumping device status information
[project/netifd.git] / device.h
1 #ifndef __LL_H
2 #define __LL_H
3
4 #include <libubox/avl.h>
5
6 struct device;
7 struct device_hotplug_ops;
8
9 typedef int (*device_state_cb)(struct device *, bool up);
10
11 struct device_type {
12 const char *name;
13
14 void (*dump_status)(struct device *, struct blob_buf *buf);
15 int (*check_state)(struct device *);
16 void (*free)(struct device *);
17 };
18
19 /*
20 * link layer device. typically represents a linux network device.
21 * can be used to support VLANs as well
22 */
23 struct device {
24 const struct device_type *type;
25
26 struct avl_node avl;
27 struct list_head users;
28
29 char ifname[IFNAMSIZ + 1];
30 int ifindex;
31
32 bool present;
33 int active;
34
35 /* set interface up or down */
36 device_state_cb set_state;
37
38 const struct device_hotplug_ops *hotplug_ops;
39 };
40
41 /* events broadcasted to all users of a device */
42 enum device_event {
43 /* device has been added to the system and can be brought up */
44 DEV_EVENT_ADD,
45
46 /* device has been removed */
47 DEV_EVENT_REMOVE,
48
49 /* device is being brought up */
50 DEV_EVENT_SETUP,
51
52 /* device is being brought down */
53 DEV_EVENT_TEARDOWN,
54
55 /* device has been brought up */
56 DEV_EVENT_UP,
57
58 /* device has been brought down */
59 DEV_EVENT_DOWN,
60
61 /* device has changed its link state to up */
62 DEV_EVENT_LINK_UP,
63
64 /* device has changed its link state to down */
65 DEV_EVENT_LINK_DOWN,
66 };
67
68 /*
69 * device dependency with callbacks
70 */
71 struct device_user {
72 struct list_head list;
73
74 struct device *dev;
75 void (*cb)(struct device_user *, enum device_event);
76 };
77
78 struct device_hotplug_ops {
79 int (*add)(struct device *main, struct device *member);
80 int (*del)(struct device *main, struct device *member);
81 };
82
83 void init_virtual_device(struct device *dev, const struct device_type *type, const char *name);
84 int init_device(struct device *iface, const struct device_type *type, const char *ifname);
85 void cleanup_device(struct device *iface);
86 struct device *get_device(const char *name, bool create);
87 void add_device_user(struct device_user *dep, struct device *iface);
88 void remove_device_user(struct device_user *dep);
89
90 void set_device_present(struct device *dev, bool state);
91 int claim_device(struct device *dev);
92 void release_device(struct device *dev);
93 int check_device_state(struct device *dev);
94
95 struct device *get_vlan_device_chain(const char *ifname, bool create);
96
97 #endif