device.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef __LL_H
  2. #define __LL_H
  3. #include <libubox/avl.h>
  4. #include <netinet/in.h>
  5. struct device;
  6. struct device_hotplug_ops;
  7. typedef int (*device_state_cb)(struct device *, bool up);
  8. struct device_type {
  9. const char *name;
  10. void (*dump_status)(struct device *, struct blob_buf *buf);
  11. int (*check_state)(struct device *);
  12. void (*free)(struct device *);
  13. };
  14. enum {
  15. DEV_OPT_MTU = (1 << 0),
  16. DEV_OPT_MACADDR = (1 << 1),
  17. DEV_OPT_TXQUEUELEN = (1 << 2)
  18. };
  19. enum device_addr_flags {
  20. /* address family for routes and addresses */
  21. DEVADDR_INET4 = (0 << 0),
  22. DEVADDR_INET6 = (1 << 0),
  23. DEVADDR_FAMILY = DEVADDR_INET4 | DEVADDR_INET6,
  24. /* device route (no gateway) */
  25. DEVADDR_DEVICE = (1 << 1),
  26. };
  27. union if_addr {
  28. struct in_addr in;
  29. struct in6_addr in6;
  30. };
  31. struct device_addr {
  32. struct list_head list;
  33. void *ctx;
  34. enum device_addr_flags flags;
  35. unsigned int mask;
  36. union if_addr addr;
  37. };
  38. struct device_route {
  39. struct list_head list;
  40. void *ctx;
  41. enum device_addr_flags flags;
  42. unsigned int mask;
  43. union if_addr addr;
  44. union if_addr nexthop;
  45. };
  46. /*
  47. * link layer device. typically represents a linux network device.
  48. * can be used to support VLANs as well
  49. */
  50. struct device {
  51. const struct device_type *type;
  52. struct avl_node avl;
  53. struct list_head users;
  54. char ifname[IFNAMSIZ + 1];
  55. int ifindex;
  56. bool present;
  57. int active;
  58. /* set interface up or down */
  59. device_state_cb set_state;
  60. const struct device_hotplug_ops *hotplug_ops;
  61. /* settings */
  62. unsigned int flags;
  63. unsigned int mtu;
  64. unsigned int txqueuelen;
  65. uint8_t macaddr[6];
  66. uint32_t config_hash;
  67. };
  68. /* events broadcasted to all users of a device */
  69. enum device_event {
  70. DEV_EVENT_ADD,
  71. DEV_EVENT_REMOVE,
  72. DEV_EVENT_SETUP,
  73. DEV_EVENT_TEARDOWN,
  74. DEV_EVENT_UP,
  75. DEV_EVENT_DOWN,
  76. DEV_EVENT_LINK_UP,
  77. DEV_EVENT_LINK_DOWN,
  78. };
  79. /*
  80. * device dependency with callbacks
  81. */
  82. struct device_user {
  83. struct list_head list;
  84. struct device *dev;
  85. void (*cb)(struct device_user *, enum device_event);
  86. };
  87. struct device_hotplug_ops {
  88. int (*add)(struct device *main, struct device *member);
  89. int (*del)(struct device *main, struct device *member);
  90. };
  91. static inline void
  92. device_free(struct device *dev)
  93. {
  94. dev->type->free(dev);
  95. }
  96. extern const struct config_param_list device_attr_list;
  97. struct device *device_create(struct blob_attr *attr, struct uci_section *s);
  98. void init_virtual_device(struct device *dev, const struct device_type *type, const char *name);
  99. int init_device(struct device *iface, const struct device_type *type, const char *ifname);
  100. void cleanup_device(struct device *iface);
  101. struct device *get_device(const char *name, bool create);
  102. void device_add_user(struct device_user *dep, struct device *iface);
  103. void device_remove_user(struct device_user *dep);
  104. void device_set_present(struct device *dev, bool state);
  105. int claim_device(struct device *dev);
  106. void release_device(struct device *dev);
  107. int check_device_state(struct device *dev);
  108. void device_free_all(void);
  109. struct device *get_vlan_device_chain(const char *ifname, bool create);
  110. struct device *bridge_create(const char *name, struct uci_section *s);
  111. #endif