if_team.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. long mode_priv[0];
  53. };
  54. static inline bool team_port_enabled(struct team_port *port)
  55. {
  56. return port->index != -1;
  57. }
  58. static inline bool team_port_txable(struct team_port *port)
  59. {
  60. return port->linkup && team_port_enabled(port);
  61. }
  62. struct team_mode_ops {
  63. int (*init)(struct team *team);
  64. void (*exit)(struct team *team);
  65. rx_handler_result_t (*receive)(struct team *team,
  66. struct team_port *port,
  67. struct sk_buff *skb);
  68. bool (*transmit)(struct team *team, struct sk_buff *skb);
  69. int (*port_enter)(struct team *team, struct team_port *port);
  70. void (*port_leave)(struct team *team, struct team_port *port);
  71. void (*port_change_mac)(struct team *team, struct team_port *port);
  72. void (*port_enabled)(struct team *team, struct team_port *port);
  73. void (*port_disabled)(struct team *team, struct team_port *port);
  74. };
  75. enum team_option_type {
  76. TEAM_OPTION_TYPE_U32,
  77. TEAM_OPTION_TYPE_STRING,
  78. TEAM_OPTION_TYPE_BINARY,
  79. TEAM_OPTION_TYPE_BOOL,
  80. };
  81. struct team_option_inst_info {
  82. u32 array_index;
  83. struct team_port *port; /* != NULL if per-port */
  84. };
  85. struct team_gsetter_ctx {
  86. union {
  87. u32 u32_val;
  88. const char *str_val;
  89. struct {
  90. const void *ptr;
  91. u32 len;
  92. } bin_val;
  93. bool bool_val;
  94. } data;
  95. struct team_option_inst_info *info;
  96. };
  97. struct team_option {
  98. struct list_head list;
  99. const char *name;
  100. bool per_port;
  101. unsigned int array_size; /* != 0 means the option is array */
  102. enum team_option_type type;
  103. int (*init)(struct team *team, struct team_option_inst_info *info);
  104. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  105. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  106. };
  107. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  108. extern void team_options_change_check(struct team *team);
  109. struct team_mode {
  110. const char *kind;
  111. struct module *owner;
  112. size_t priv_size;
  113. size_t port_priv_size;
  114. const struct team_mode_ops *ops;
  115. };
  116. #define TEAM_PORT_HASHBITS 4
  117. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  118. #define TEAM_MODE_PRIV_LONGS 4
  119. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  120. struct team {
  121. struct net_device *dev; /* associated netdevice */
  122. struct team_pcpu_stats __percpu *pcpu_stats;
  123. struct mutex lock; /* used for overall locking, e.g. port lists write */
  124. /*
  125. * List of enabled ports and their count
  126. */
  127. int en_port_count;
  128. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  129. struct list_head port_list; /* list of all ports */
  130. struct list_head option_list;
  131. struct list_head option_inst_list; /* list of option instances */
  132. const struct team_mode *mode;
  133. struct team_mode_ops ops;
  134. long mode_priv[TEAM_MODE_PRIV_LONGS];
  135. };
  136. static inline struct hlist_head *team_port_index_hash(struct team *team,
  137. int port_index)
  138. {
  139. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  140. }
  141. static inline struct team_port *team_get_port_by_index(struct team *team,
  142. int port_index)
  143. {
  144. struct hlist_node *p;
  145. struct team_port *port;
  146. struct hlist_head *head = team_port_index_hash(team, port_index);
  147. hlist_for_each_entry(port, p, head, hlist)
  148. if (port->index == port_index)
  149. return port;
  150. return NULL;
  151. }
  152. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  153. int port_index)
  154. {
  155. struct hlist_node *p;
  156. struct team_port *port;
  157. struct hlist_head *head = team_port_index_hash(team, port_index);
  158. hlist_for_each_entry_rcu(port, p, head, hlist)
  159. if (port->index == port_index)
  160. return port;
  161. return NULL;
  162. }
  163. extern int team_port_set_team_mac(struct team_port *port);
  164. extern int team_options_register(struct team *team,
  165. const struct team_option *option,
  166. size_t option_count);
  167. extern void team_options_unregister(struct team *team,
  168. const struct team_option *option,
  169. size_t option_count);
  170. extern int team_mode_register(const struct team_mode *mode);
  171. extern void team_mode_unregister(const struct team_mode *mode);
  172. #endif /* __KERNEL__ */
  173. #define TEAM_STRING_MAX_LEN 32
  174. /**********************************
  175. * NETLINK_GENERIC netlink family.
  176. **********************************/
  177. enum {
  178. TEAM_CMD_NOOP,
  179. TEAM_CMD_OPTIONS_SET,
  180. TEAM_CMD_OPTIONS_GET,
  181. TEAM_CMD_PORT_LIST_GET,
  182. __TEAM_CMD_MAX,
  183. TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
  184. };
  185. enum {
  186. TEAM_ATTR_UNSPEC,
  187. TEAM_ATTR_TEAM_IFINDEX, /* u32 */
  188. TEAM_ATTR_LIST_OPTION, /* nest */
  189. TEAM_ATTR_LIST_PORT, /* nest */
  190. __TEAM_ATTR_MAX,
  191. TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
  192. };
  193. /* Nested layout of get/set msg:
  194. *
  195. * [TEAM_ATTR_LIST_OPTION]
  196. * [TEAM_ATTR_ITEM_OPTION]
  197. * [TEAM_ATTR_OPTION_*], ...
  198. * [TEAM_ATTR_ITEM_OPTION]
  199. * [TEAM_ATTR_OPTION_*], ...
  200. * ...
  201. * [TEAM_ATTR_LIST_PORT]
  202. * [TEAM_ATTR_ITEM_PORT]
  203. * [TEAM_ATTR_PORT_*], ...
  204. * [TEAM_ATTR_ITEM_PORT]
  205. * [TEAM_ATTR_PORT_*], ...
  206. * ...
  207. */
  208. enum {
  209. TEAM_ATTR_ITEM_OPTION_UNSPEC,
  210. TEAM_ATTR_ITEM_OPTION, /* nest */
  211. __TEAM_ATTR_ITEM_OPTION_MAX,
  212. TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
  213. };
  214. enum {
  215. TEAM_ATTR_OPTION_UNSPEC,
  216. TEAM_ATTR_OPTION_NAME, /* string */
  217. TEAM_ATTR_OPTION_CHANGED, /* flag */
  218. TEAM_ATTR_OPTION_TYPE, /* u8 */
  219. TEAM_ATTR_OPTION_DATA, /* dynamic */
  220. TEAM_ATTR_OPTION_REMOVED, /* flag */
  221. TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
  222. TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */
  223. __TEAM_ATTR_OPTION_MAX,
  224. TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
  225. };
  226. enum {
  227. TEAM_ATTR_ITEM_PORT_UNSPEC,
  228. TEAM_ATTR_ITEM_PORT, /* nest */
  229. __TEAM_ATTR_ITEM_PORT_MAX,
  230. TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
  231. };
  232. enum {
  233. TEAM_ATTR_PORT_UNSPEC,
  234. TEAM_ATTR_PORT_IFINDEX, /* u32 */
  235. TEAM_ATTR_PORT_CHANGED, /* flag */
  236. TEAM_ATTR_PORT_LINKUP, /* flag */
  237. TEAM_ATTR_PORT_SPEED, /* u32 */
  238. TEAM_ATTR_PORT_DUPLEX, /* u8 */
  239. TEAM_ATTR_PORT_REMOVED, /* flag */
  240. __TEAM_ATTR_PORT_MAX,
  241. TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
  242. };
  243. /*
  244. * NETLINK_GENERIC related info
  245. */
  246. #define TEAM_GENL_NAME "team"
  247. #define TEAM_GENL_VERSION 0x1
  248. #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
  249. #endif /* _LINUX_IF_TEAM_H_ */