device.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_SETUP,
  61. DEV_EVENT_TEARDOWN,
  62. DEV_EVENT_UP,
  63. DEV_EVENT_DOWN,
  64. DEV_EVENT_LINK_UP,
  65. DEV_EVENT_LINK_DOWN,
  66. __DEV_EVENT_MAX
  67. };
  68. /*
  69. * device dependency with callbacks
  70. */
  71. struct device_user {
  72. struct safe_list list;
  73. bool claimed;
  74. bool hotplug;
  75. bool alias;
  76. uint8_t ev_idx[__DEV_EVENT_MAX];
  77. struct device *dev;
  78. void (*cb)(struct device_user *, enum device_event);
  79. };
  80. struct device_settings {
  81. unsigned int flags;
  82. unsigned int mtu;
  83. unsigned int txqueuelen;
  84. uint8_t macaddr[6];
  85. };
  86. /*
  87. * link layer device. typically represents a linux network device.
  88. * can be used to support VLANs as well
  89. */
  90. struct device {
  91. const struct device_type *type;
  92. struct avl_node avl;
  93. struct safe_list users;
  94. struct safe_list aliases;
  95. char ifname[IFNAMSIZ + 1];
  96. int ifindex;
  97. struct blob_attr *config;
  98. bool config_pending;
  99. bool sys_present;
  100. bool present;
  101. int active;
  102. bool external;
  103. bool disabled;
  104. bool deferred;
  105. bool hidden;
  106. bool current_config;
  107. bool default_config;
  108. /* set interface up or down */
  109. device_state_cb set_state;
  110. const struct device_hotplug_ops *hotplug_ops;
  111. struct device_user parent;
  112. struct device_settings orig_settings;
  113. struct device_settings settings;
  114. };
  115. struct device_hotplug_ops {
  116. int (*prepare)(struct device *dev);
  117. int (*add)(struct device *main, struct device *member);
  118. int (*del)(struct device *main, struct device *member);
  119. };
  120. extern const struct uci_blob_param_list device_attr_list;
  121. extern const struct device_type simple_device_type;
  122. extern const struct device_type bridge_device_type;
  123. extern const struct device_type tunnel_device_type;
  124. void device_lock(void);
  125. void device_unlock(void);
  126. struct device *device_create(const char *name, const struct device_type *type,
  127. struct blob_attr *config);
  128. void device_init_settings(struct device *dev, struct blob_attr **tb);
  129. void device_init_pending(void);
  130. enum dev_change_type
  131. device_set_config(struct device *dev, const struct device_type *type,
  132. struct blob_attr *attr);
  133. void device_reset_config(void);
  134. void device_reset_old(void);
  135. void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
  136. int device_init(struct device *iface, const struct device_type *type, const char *ifname);
  137. void device_cleanup(struct device *iface);
  138. struct device *device_get(const char *name, int create);
  139. void device_add_user(struct device_user *dep, struct device *iface);
  140. void device_remove_user(struct device_user *dep);
  141. void device_broadcast_event(struct device *dev, enum device_event ev);
  142. void device_set_present(struct device *dev, bool state);
  143. void device_refresh_present(struct device *dev);
  144. int device_claim(struct device_user *dep);
  145. void device_release(struct device_user *dep);
  146. int device_check_state(struct device *dev);
  147. void device_dump_status(struct blob_buf *b, struct device *dev);
  148. void device_free(struct device *dev);
  149. void device_free_unused(struct device *dev);
  150. struct device *get_vlan_device_chain(const char *ifname, bool create);
  151. void alias_notify_device(const char *name, struct device *dev);
  152. struct device *device_alias_get(const char *name);
  153. static inline void
  154. device_set_deferred(struct device *dev, bool value)
  155. {
  156. dev->deferred = value;
  157. device_refresh_present(dev);
  158. }
  159. static inline void
  160. device_set_disabled(struct device *dev, bool value)
  161. {
  162. dev->disabled = value;
  163. device_refresh_present(dev);
  164. }
  165. #endif