if_team.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 enabled ports hash list */
  27. struct list_head list; /* node in ordinary list */
  28. struct team *team;
  29. int index; /* index of enabled port. If disabled, it's set to -1 */
  30. bool linkup; /* either state.linkup or user.linkup */
  31. struct {
  32. bool linkup;
  33. u32 speed;
  34. u8 duplex;
  35. } state;
  36. /* Values set by userspace */
  37. struct {
  38. bool linkup;
  39. bool linkup_enabled;
  40. } user;
  41. /* Custom gennetlink interface related flags */
  42. bool changed;
  43. bool removed;
  44. /*
  45. * A place for storing original values of the device before it
  46. * become a port.
  47. */
  48. struct {
  49. unsigned char dev_addr[MAX_ADDR_LEN];
  50. unsigned int mtu;
  51. } orig;
  52. struct rcu_head rcu;
  53. };
  54. struct team_mode_ops {
  55. int (*init)(struct team *team);
  56. void (*exit)(struct team *team);
  57. rx_handler_result_t (*receive)(struct team *team,
  58. struct team_port *port,
  59. struct sk_buff *skb);
  60. bool (*transmit)(struct team *team, struct sk_buff *skb);
  61. int (*port_enter)(struct team *team, struct team_port *port);
  62. void (*port_leave)(struct team *team, struct team_port *port);
  63. void (*port_change_mac)(struct team *team, struct team_port *port);
  64. };
  65. enum team_option_type {
  66. TEAM_OPTION_TYPE_U32,
  67. TEAM_OPTION_TYPE_STRING,
  68. TEAM_OPTION_TYPE_BINARY,
  69. TEAM_OPTION_TYPE_BOOL,
  70. };
  71. struct team_gsetter_ctx {
  72. union {
  73. u32 u32_val;
  74. const char *str_val;
  75. struct {
  76. const void *ptr;
  77. u32 len;
  78. } bin_val;
  79. bool bool_val;
  80. } data;
  81. struct team_port *port;
  82. };
  83. struct team_option {
  84. struct list_head list;
  85. const char *name;
  86. bool per_port;
  87. enum team_option_type type;
  88. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  89. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  90. };
  91. struct team_mode {
  92. const char *kind;
  93. struct module *owner;
  94. size_t priv_size;
  95. const struct team_mode_ops *ops;
  96. };
  97. #define TEAM_PORT_HASHBITS 4
  98. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  99. #define TEAM_MODE_PRIV_LONGS 4
  100. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  101. struct team {
  102. struct net_device *dev; /* associated netdevice */
  103. struct team_pcpu_stats __percpu *pcpu_stats;
  104. struct mutex lock; /* used for overall locking, e.g. port lists write */
  105. /*
  106. * List of enabled ports and their count
  107. */
  108. int en_port_count;
  109. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  110. struct list_head port_list; /* list of all ports */
  111. struct list_head option_list;
  112. struct list_head option_inst_list; /* list of option instances */
  113. const struct team_mode *mode;
  114. struct team_mode_ops ops;
  115. long mode_priv[TEAM_MODE_PRIV_LONGS];
  116. };
  117. static inline struct hlist_head *team_port_index_hash(struct team *team,
  118. int port_index)
  119. {
  120. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  121. }
  122. static inline struct team_port *team_get_port_by_index(struct team *team,
  123. int port_index)
  124. {
  125. struct hlist_node *p;
  126. struct team_port *port;
  127. struct hlist_head *head = team_port_index_hash(team, port_index);
  128. hlist_for_each_entry(port, p, head, hlist)
  129. if (port->index == port_index)
  130. return port;
  131. return NULL;
  132. }
  133. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  134. int port_index)
  135. {
  136. struct hlist_node *p;
  137. struct team_port *port;
  138. struct hlist_head *head = team_port_index_hash(team, port_index);
  139. hlist_for_each_entry_rcu(port, p, head, hlist)
  140. if (port->index == port_index)
  141. return port;
  142. return NULL;
  143. }
  144. extern int team_port_set_team_mac(struct team_port *port);
  145. extern int team_options_register(struct team *team,
  146. const struct team_option *option,
  147. size_t option_count);
  148. extern void team_options_unregister(struct team *team,
  149. const struct team_option *option,
  150. size_t option_count);
  151. extern int team_mode_register(const struct team_mode *mode);
  152. extern void team_mode_unregister(const struct team_mode *mode);
  153. #endif /* __KERNEL__ */
  154. #define TEAM_STRING_MAX_LEN 32
  155. /**********************************
  156. * NETLINK_GENERIC netlink family.
  157. **********************************/
  158. enum {
  159. TEAM_CMD_NOOP,
  160. TEAM_CMD_OPTIONS_SET,
  161. TEAM_CMD_OPTIONS_GET,
  162. TEAM_CMD_PORT_LIST_GET,
  163. __TEAM_CMD_MAX,
  164. TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
  165. };
  166. enum {
  167. TEAM_ATTR_UNSPEC,
  168. TEAM_ATTR_TEAM_IFINDEX, /* u32 */
  169. TEAM_ATTR_LIST_OPTION, /* nest */
  170. TEAM_ATTR_LIST_PORT, /* nest */
  171. __TEAM_ATTR_MAX,
  172. TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
  173. };
  174. /* Nested layout of get/set msg:
  175. *
  176. * [TEAM_ATTR_LIST_OPTION]
  177. * [TEAM_ATTR_ITEM_OPTION]
  178. * [TEAM_ATTR_OPTION_*], ...
  179. * [TEAM_ATTR_ITEM_OPTION]
  180. * [TEAM_ATTR_OPTION_*], ...
  181. * ...
  182. * [TEAM_ATTR_LIST_PORT]
  183. * [TEAM_ATTR_ITEM_PORT]
  184. * [TEAM_ATTR_PORT_*], ...
  185. * [TEAM_ATTR_ITEM_PORT]
  186. * [TEAM_ATTR_PORT_*], ...
  187. * ...
  188. */
  189. enum {
  190. TEAM_ATTR_ITEM_OPTION_UNSPEC,
  191. TEAM_ATTR_ITEM_OPTION, /* nest */
  192. __TEAM_ATTR_ITEM_OPTION_MAX,
  193. TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
  194. };
  195. enum {
  196. TEAM_ATTR_OPTION_UNSPEC,
  197. TEAM_ATTR_OPTION_NAME, /* string */
  198. TEAM_ATTR_OPTION_CHANGED, /* flag */
  199. TEAM_ATTR_OPTION_TYPE, /* u8 */
  200. TEAM_ATTR_OPTION_DATA, /* dynamic */
  201. TEAM_ATTR_OPTION_REMOVED, /* flag */
  202. TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
  203. __TEAM_ATTR_OPTION_MAX,
  204. TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
  205. };
  206. enum {
  207. TEAM_ATTR_ITEM_PORT_UNSPEC,
  208. TEAM_ATTR_ITEM_PORT, /* nest */
  209. __TEAM_ATTR_ITEM_PORT_MAX,
  210. TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
  211. };
  212. enum {
  213. TEAM_ATTR_PORT_UNSPEC,
  214. TEAM_ATTR_PORT_IFINDEX, /* u32 */
  215. TEAM_ATTR_PORT_CHANGED, /* flag */
  216. TEAM_ATTR_PORT_LINKUP, /* flag */
  217. TEAM_ATTR_PORT_SPEED, /* u32 */
  218. TEAM_ATTR_PORT_DUPLEX, /* u8 */
  219. TEAM_ATTR_PORT_REMOVED, /* flag */
  220. __TEAM_ATTR_PORT_MAX,
  221. TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
  222. };
  223. /*
  224. * NETLINK_GENERIC related info
  225. */
  226. #define TEAM_GENL_NAME "team"
  227. #define TEAM_GENL_VERSION 0x1
  228. #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
  229. #endif /* _LINUX_IF_TEAM_H_ */