if_team.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * include/linux/if_team.h - Network team device driver header
  3. * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
  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 as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #ifndef _LINUX_IF_TEAM_H_
  11. #define _LINUX_IF_TEAM_H_
  12. #ifdef __KERNEL__
  13. struct team_pcpu_stats {
  14. u64 rx_packets;
  15. u64 rx_bytes;
  16. u64 rx_multicast;
  17. u64 tx_packets;
  18. u64 tx_bytes;
  19. struct u64_stats_sync syncp;
  20. u32 rx_dropped;
  21. u32 tx_dropped;
  22. };
  23. struct team;
  24. struct team_port {
  25. struct net_device *dev;
  26. struct hlist_node hlist; /* node in hash list */
  27. struct list_head list; /* node in ordinary list */
  28. struct team *team;
  29. int index;
  30. /*
  31. * A place for storing original values of the device before it
  32. * become a port.
  33. */
  34. struct {
  35. unsigned char dev_addr[MAX_ADDR_LEN];
  36. unsigned int mtu;
  37. } orig;
  38. bool linkup;
  39. u32 speed;
  40. u8 duplex;
  41. /* Custom gennetlink interface related flags */
  42. bool changed;
  43. bool removed;
  44. struct rcu_head rcu;
  45. };
  46. struct team_mode_ops {
  47. int (*init)(struct team *team);
  48. void (*exit)(struct team *team);
  49. rx_handler_result_t (*receive)(struct team *team,
  50. struct team_port *port,
  51. struct sk_buff *skb);
  52. bool (*transmit)(struct team *team, struct sk_buff *skb);
  53. int (*port_enter)(struct team *team, struct team_port *port);
  54. void (*port_leave)(struct team *team, struct team_port *port);
  55. void (*port_change_mac)(struct team *team, struct team_port *port);
  56. };
  57. enum team_option_type {
  58. TEAM_OPTION_TYPE_U32,
  59. TEAM_OPTION_TYPE_STRING,
  60. };
  61. struct team_option {
  62. struct list_head list;
  63. const char *name;
  64. enum team_option_type type;
  65. int (*getter)(struct team *team, void *arg);
  66. int (*setter)(struct team *team, void *arg);
  67. /* Custom gennetlink interface related flags */
  68. bool changed;
  69. bool removed;
  70. };
  71. struct team_mode {
  72. struct list_head list;
  73. const char *kind;
  74. struct module *owner;
  75. size_t priv_size;
  76. const struct team_mode_ops *ops;
  77. };
  78. #define TEAM_PORT_HASHBITS 4
  79. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  80. #define TEAM_MODE_PRIV_LONGS 4
  81. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  82. struct team {
  83. struct net_device *dev; /* associated netdevice */
  84. struct team_pcpu_stats __percpu *pcpu_stats;
  85. struct mutex lock; /* used for overall locking, e.g. port lists write */
  86. /*
  87. * port lists with port count
  88. */
  89. int port_count;
  90. struct hlist_head port_hlist[TEAM_PORT_HASHENTRIES];
  91. struct list_head port_list;
  92. struct list_head option_list;
  93. const struct team_mode *mode;
  94. struct team_mode_ops ops;
  95. long mode_priv[TEAM_MODE_PRIV_LONGS];
  96. };
  97. static inline struct hlist_head *team_port_index_hash(struct team *team,
  98. int port_index)
  99. {
  100. return &team->port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  101. }
  102. static inline struct team_port *team_get_port_by_index(struct team *team,
  103. int port_index)
  104. {
  105. struct hlist_node *p;
  106. struct team_port *port;
  107. struct hlist_head *head = team_port_index_hash(team, port_index);
  108. hlist_for_each_entry(port, p, head, hlist)
  109. if (port->index == port_index)
  110. return port;
  111. return NULL;
  112. }
  113. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  114. int port_index)
  115. {
  116. struct hlist_node *p;
  117. struct team_port *port;
  118. struct hlist_head *head = team_port_index_hash(team, port_index);
  119. hlist_for_each_entry_rcu(port, p, head, hlist)
  120. if (port->index == port_index)
  121. return port;
  122. return NULL;
  123. }
  124. extern int team_port_set_team_mac(struct team_port *port);
  125. extern int team_options_register(struct team *team,
  126. const struct team_option *option,
  127. size_t option_count);
  128. extern void team_options_unregister(struct team *team,
  129. const struct team_option *option,
  130. size_t option_count);
  131. extern int team_mode_register(struct team_mode *mode);
  132. extern int team_mode_unregister(struct team_mode *mode);
  133. #endif /* __KERNEL__ */
  134. #define TEAM_STRING_MAX_LEN 32
  135. /**********************************
  136. * NETLINK_GENERIC netlink family.
  137. **********************************/
  138. enum {
  139. TEAM_CMD_NOOP,
  140. TEAM_CMD_OPTIONS_SET,
  141. TEAM_CMD_OPTIONS_GET,
  142. TEAM_CMD_PORT_LIST_GET,
  143. __TEAM_CMD_MAX,
  144. TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
  145. };
  146. enum {
  147. TEAM_ATTR_UNSPEC,
  148. TEAM_ATTR_TEAM_IFINDEX, /* u32 */
  149. TEAM_ATTR_LIST_OPTION, /* nest */
  150. TEAM_ATTR_LIST_PORT, /* nest */
  151. __TEAM_ATTR_MAX,
  152. TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
  153. };
  154. /* Nested layout of get/set msg:
  155. *
  156. * [TEAM_ATTR_LIST_OPTION]
  157. * [TEAM_ATTR_ITEM_OPTION]
  158. * [TEAM_ATTR_OPTION_*], ...
  159. * [TEAM_ATTR_ITEM_OPTION]
  160. * [TEAM_ATTR_OPTION_*], ...
  161. * ...
  162. * [TEAM_ATTR_LIST_PORT]
  163. * [TEAM_ATTR_ITEM_PORT]
  164. * [TEAM_ATTR_PORT_*], ...
  165. * [TEAM_ATTR_ITEM_PORT]
  166. * [TEAM_ATTR_PORT_*], ...
  167. * ...
  168. */
  169. enum {
  170. TEAM_ATTR_ITEM_OPTION_UNSPEC,
  171. TEAM_ATTR_ITEM_OPTION, /* nest */
  172. __TEAM_ATTR_ITEM_OPTION_MAX,
  173. TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
  174. };
  175. enum {
  176. TEAM_ATTR_OPTION_UNSPEC,
  177. TEAM_ATTR_OPTION_NAME, /* string */
  178. TEAM_ATTR_OPTION_CHANGED, /* flag */
  179. TEAM_ATTR_OPTION_TYPE, /* u8 */
  180. TEAM_ATTR_OPTION_DATA, /* dynamic */
  181. TEAM_ATTR_OPTION_REMOVED, /* flag */
  182. __TEAM_ATTR_OPTION_MAX,
  183. TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
  184. };
  185. enum {
  186. TEAM_ATTR_ITEM_PORT_UNSPEC,
  187. TEAM_ATTR_ITEM_PORT, /* nest */
  188. __TEAM_ATTR_ITEM_PORT_MAX,
  189. TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
  190. };
  191. enum {
  192. TEAM_ATTR_PORT_UNSPEC,
  193. TEAM_ATTR_PORT_IFINDEX, /* u32 */
  194. TEAM_ATTR_PORT_CHANGED, /* flag */
  195. TEAM_ATTR_PORT_LINKUP, /* flag */
  196. TEAM_ATTR_PORT_SPEED, /* u32 */
  197. TEAM_ATTR_PORT_DUPLEX, /* u8 */
  198. TEAM_ATTR_PORT_REMOVED, /* flag */
  199. __TEAM_ATTR_PORT_MAX,
  200. TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
  201. };
  202. /*
  203. * NETLINK_GENERIC related info
  204. */
  205. #define TEAM_GENL_NAME "team"
  206. #define TEAM_GENL_VERSION 0x1
  207. #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
  208. #endif /* _LINUX_IF_TEAM_H_ */