device.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __LL_H
  15. #define __LL_H
  16. #include <libubox/avl.h>
  17. #include <libubox/safe_list.h>
  18. #include <netinet/in.h>
  19. struct device;
  20. struct device_user;
  21. struct device_hotplug_ops;
  22. typedef int (*device_state_cb)(struct device *, bool up);
  23. enum {
  24. DEV_ATTR_TYPE,
  25. DEV_ATTR_IFNAME,
  26. DEV_ATTR_MTU,
  27. DEV_ATTR_MACADDR,
  28. DEV_ATTR_TXQUEUELEN,
  29. DEV_ATTR_ENABLED,
  30. __DEV_ATTR_MAX,
  31. };
  32. enum dev_change_type {
  33. DEV_CONFIG_NO_CHANGE,
  34. DEV_CONFIG_APPLIED,
  35. DEV_CONFIG_RESTART,
  36. DEV_CONFIG_RECREATE,
  37. };
  38. struct device_type {
  39. struct list_head list;
  40. const char *name;
  41. const struct uci_blob_param_list *config_params;
  42. struct device *(*create)(const char *name, struct blob_attr *attr);
  43. void (*config_init)(struct device *);
  44. enum dev_change_type (*reload)(struct device *, struct blob_attr *);
  45. void (*dump_info)(struct device *, struct blob_buf *buf);
  46. void (*dump_stats)(struct device *, struct blob_buf *buf);
  47. int (*check_state)(struct device *);
  48. void (*free)(struct device *);
  49. };
  50. enum {
  51. DEV_OPT_MTU = (1 << 0),
  52. DEV_OPT_MACADDR = (1 << 1),
  53. DEV_OPT_TXQUEUELEN = (1 << 2),
  54. };
  55. /* events broadcasted to all users of a device */
  56. enum device_event {
  57. DEV_EVENT_ADD,
  58. DEV_EVENT_REMOVE,
  59. DEV_EVENT_UPDATE_IFNAME,
  60. DEV_EVENT_UPDATE_IFINDEX,
  61. DEV_EVENT_SETUP,
  62. DEV_EVENT_TEARDOWN,
  63. DEV_EVENT_UP,
  64. DEV_EVENT_DOWN,
  65. DEV_EVENT_LINK_UP,
  66. DEV_EVENT_LINK_DOWN,
  67. /* Topology changed (i.e. bridge member added) */
  68. DEV_EVENT_TOPO_CHANGE,
  69. __DEV_EVENT_MAX
  70. };
  71. /*
  72. * device dependency with callbacks
  73. */
  74. struct device_user {
  75. struct safe_list list;
  76. bool claimed;
  77. bool hotplug;
  78. bool alias;
  79. uint8_t ev_idx[__DEV_EVENT_MAX];
  80. struct device *dev;
  81. void (*cb)(struct device_user *, enum device_event);
  82. };
  83. struct device_settings {
  84. unsigned int flags;
  85. unsigned int mtu;
  86. unsigned int txqueuelen;
  87. uint8_t macaddr[6];
  88. };
  89. /*
  90. * link layer device. typically represents a linux network device.
  91. * can be used to support VLANs as well
  92. */
  93. struct device {
  94. const struct device_type *type;
  95. struct avl_node avl;
  96. struct safe_list users;
  97. struct safe_list aliases;
  98. char ifname[IFNAMSIZ + 1];
  99. int ifindex;
  100. struct blob_attr *config;
  101. bool config_pending;
  102. bool sys_present;
  103. bool present;
  104. int active;
  105. bool link_active;
  106. bool external;
  107. bool disabled;
  108. bool deferred;
  109. bool hidden;
  110. bool current_config;
  111. bool default_config;
  112. /* set interface up or down */
  113. device_state_cb set_state;
  114. const struct device_hotplug_ops *hotplug_ops;
  115. struct device_user parent;
  116. struct device_settings orig_settings;
  117. struct device_settings settings;
  118. };
  119. struct device_hotplug_ops {
  120. int (*prepare)(struct device *dev);
  121. int (*add)(struct device *main, struct device *member);
  122. int (*del)(struct device *main, struct device *member);
  123. };
  124. extern const struct uci_blob_param_list device_attr_list;
  125. extern const struct device_type simple_device_type;
  126. extern const struct device_type bridge_device_type;
  127. extern const struct device_type tunnel_device_type;
  128. extern const struct device_type macvlan_device_type;
  129. void device_lock(void);
  130. void device_unlock(void);
  131. struct device *device_create(const char *name, const struct device_type *type,
  132. struct blob_attr *config);
  133. void device_init_settings(struct device *dev, struct blob_attr **tb);
  134. void device_init_pending(void);
  135. enum dev_change_type
  136. device_set_config(struct device *dev, const struct device_type *type,
  137. struct blob_attr *attr);
  138. void device_reset_config(void);
  139. void device_reset_old(void);
  140. void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
  141. int device_init(struct device *iface, const struct device_type *type, const char *ifname);
  142. void device_cleanup(struct device *iface);
  143. struct device *device_get(const char *name, int create);
  144. void device_add_user(struct device_user *dep, struct device *iface);
  145. void device_remove_user(struct device_user *dep);
  146. void device_broadcast_event(struct device *dev, enum device_event ev);
  147. void device_set_present(struct device *dev, bool state);
  148. void device_set_link(struct device *dev, bool state);
  149. void device_set_ifindex(struct device *dev, int ifindex);
  150. void device_refresh_present(struct device *dev);
  151. int device_claim(struct device_user *dep);
  152. void device_release(struct device_user *dep);
  153. int device_check_state(struct device *dev);
  154. void device_dump_status(struct blob_buf *b, struct device *dev);
  155. void device_free(struct device *dev);
  156. void device_free_unused(struct device *dev);
  157. struct device *get_vlan_device_chain(const char *ifname, bool create);
  158. void alias_notify_device(const char *name, struct device *dev);
  159. struct device *device_alias_get(const char *name);
  160. static inline void
  161. device_set_deferred(struct device *dev, bool value)
  162. {
  163. dev->deferred = value;
  164. device_refresh_present(dev);
  165. }
  166. static inline void
  167. device_set_disabled(struct device *dev, bool value)
  168. {
  169. dev->disabled = value;
  170. device_refresh_present(dev);
  171. }
  172. #endif