if_team.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <linux/netpoll.h>
  14. #include <net/sch_generic.h>
  15. struct team_pcpu_stats {
  16. u64 rx_packets;
  17. u64 rx_bytes;
  18. u64 rx_multicast;
  19. u64 tx_packets;
  20. u64 tx_bytes;
  21. struct u64_stats_sync syncp;
  22. u32 rx_dropped;
  23. u32 tx_dropped;
  24. };
  25. struct team;
  26. struct team_port {
  27. struct net_device *dev;
  28. struct hlist_node hlist; /* node in enabled ports hash list */
  29. struct list_head list; /* node in ordinary list */
  30. struct team *team;
  31. int index; /* index of enabled port. If disabled, it's set to -1 */
  32. bool linkup; /* either state.linkup or user.linkup */
  33. struct {
  34. bool linkup;
  35. u32 speed;
  36. u8 duplex;
  37. } state;
  38. /* Values set by userspace */
  39. struct {
  40. bool linkup;
  41. bool linkup_enabled;
  42. } user;
  43. /* Custom gennetlink interface related flags */
  44. bool changed;
  45. bool removed;
  46. /*
  47. * A place for storing original values of the device before it
  48. * become a port.
  49. */
  50. struct {
  51. unsigned char dev_addr[MAX_ADDR_LEN];
  52. unsigned int mtu;
  53. } orig;
  54. #ifdef CONFIG_NET_POLL_CONTROLLER
  55. struct netpoll *np;
  56. #endif
  57. s32 priority; /* lower number ~ higher priority */
  58. long mode_priv[0];
  59. };
  60. static inline bool team_port_enabled(struct team_port *port)
  61. {
  62. return port->index != -1;
  63. }
  64. static inline bool team_port_txable(struct team_port *port)
  65. {
  66. return port->linkup && team_port_enabled(port);
  67. }
  68. #ifdef CONFIG_NET_POLL_CONTROLLER
  69. static inline void team_netpoll_send_skb(struct team_port *port,
  70. struct sk_buff *skb)
  71. {
  72. struct netpoll *np = port->np;
  73. if (np)
  74. netpoll_send_skb(np, skb);
  75. }
  76. #else
  77. static inline void team_netpoll_send_skb(struct team_port *port,
  78. struct sk_buff *skb)
  79. {
  80. }
  81. #endif
  82. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  83. struct sk_buff *skb)
  84. {
  85. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  86. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  87. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  88. skb->dev = port->dev;
  89. if (unlikely(netpoll_tx_running(port->dev))) {
  90. team_netpoll_send_skb(port, skb);
  91. return 0;
  92. }
  93. return dev_queue_xmit(skb);
  94. }
  95. struct team_mode_ops {
  96. int (*init)(struct team *team);
  97. void (*exit)(struct team *team);
  98. rx_handler_result_t (*receive)(struct team *team,
  99. struct team_port *port,
  100. struct sk_buff *skb);
  101. bool (*transmit)(struct team *team, struct sk_buff *skb);
  102. int (*port_enter)(struct team *team, struct team_port *port);
  103. void (*port_leave)(struct team *team, struct team_port *port);
  104. void (*port_change_mac)(struct team *team, struct team_port *port);
  105. void (*port_enabled)(struct team *team, struct team_port *port);
  106. void (*port_disabled)(struct team *team, struct team_port *port);
  107. };
  108. enum team_option_type {
  109. TEAM_OPTION_TYPE_U32,
  110. TEAM_OPTION_TYPE_STRING,
  111. TEAM_OPTION_TYPE_BINARY,
  112. TEAM_OPTION_TYPE_BOOL,
  113. TEAM_OPTION_TYPE_S32,
  114. };
  115. struct team_option_inst_info {
  116. u32 array_index;
  117. struct team_port *port; /* != NULL if per-port */
  118. };
  119. struct team_gsetter_ctx {
  120. union {
  121. u32 u32_val;
  122. const char *str_val;
  123. struct {
  124. const void *ptr;
  125. u32 len;
  126. } bin_val;
  127. bool bool_val;
  128. s32 s32_val;
  129. } data;
  130. struct team_option_inst_info *info;
  131. };
  132. struct team_option {
  133. struct list_head list;
  134. const char *name;
  135. bool per_port;
  136. unsigned int array_size; /* != 0 means the option is array */
  137. enum team_option_type type;
  138. int (*init)(struct team *team, struct team_option_inst_info *info);
  139. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  140. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  141. };
  142. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  143. extern void team_options_change_check(struct team *team);
  144. struct team_mode {
  145. const char *kind;
  146. struct module *owner;
  147. size_t priv_size;
  148. size_t port_priv_size;
  149. const struct team_mode_ops *ops;
  150. };
  151. #define TEAM_PORT_HASHBITS 4
  152. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  153. #define TEAM_MODE_PRIV_LONGS 4
  154. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  155. struct team {
  156. struct net_device *dev; /* associated netdevice */
  157. struct team_pcpu_stats __percpu *pcpu_stats;
  158. struct mutex lock; /* used for overall locking, e.g. port lists write */
  159. /*
  160. * List of enabled ports and their count
  161. */
  162. int en_port_count;
  163. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  164. struct list_head port_list; /* list of all ports */
  165. struct list_head option_list;
  166. struct list_head option_inst_list; /* list of option instances */
  167. const struct team_mode *mode;
  168. struct team_mode_ops ops;
  169. long mode_priv[TEAM_MODE_PRIV_LONGS];
  170. };
  171. static inline struct hlist_head *team_port_index_hash(struct team *team,
  172. int port_index)
  173. {
  174. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  175. }
  176. static inline struct team_port *team_get_port_by_index(struct team *team,
  177. int port_index)
  178. {
  179. struct hlist_node *p;
  180. struct team_port *port;
  181. struct hlist_head *head = team_port_index_hash(team, port_index);
  182. hlist_for_each_entry(port, p, head, hlist)
  183. if (port->index == port_index)
  184. return port;
  185. return NULL;
  186. }
  187. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  188. int port_index)
  189. {
  190. struct hlist_node *p;
  191. struct team_port *port;
  192. struct hlist_head *head = team_port_index_hash(team, port_index);
  193. hlist_for_each_entry_rcu(port, p, head, hlist)
  194. if (port->index == port_index)
  195. return port;
  196. return NULL;
  197. }
  198. extern int team_port_set_team_mac(struct team_port *port);
  199. extern int team_options_register(struct team *team,
  200. const struct team_option *option,
  201. size_t option_count);
  202. extern void team_options_unregister(struct team *team,
  203. const struct team_option *option,
  204. size_t option_count);
  205. extern int team_mode_register(const struct team_mode *mode);
  206. extern void team_mode_unregister(const struct team_mode *mode);
  207. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  208. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  209. #endif /* __KERNEL__ */
  210. #define TEAM_STRING_MAX_LEN 32
  211. /**********************************
  212. * NETLINK_GENERIC netlink family.
  213. **********************************/
  214. enum {
  215. TEAM_CMD_NOOP,
  216. TEAM_CMD_OPTIONS_SET,
  217. TEAM_CMD_OPTIONS_GET,
  218. TEAM_CMD_PORT_LIST_GET,
  219. __TEAM_CMD_MAX,
  220. TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
  221. };
  222. enum {
  223. TEAM_ATTR_UNSPEC,
  224. TEAM_ATTR_TEAM_IFINDEX, /* u32 */
  225. TEAM_ATTR_LIST_OPTION, /* nest */
  226. TEAM_ATTR_LIST_PORT, /* nest */
  227. __TEAM_ATTR_MAX,
  228. TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
  229. };
  230. /* Nested layout of get/set msg:
  231. *
  232. * [TEAM_ATTR_LIST_OPTION]
  233. * [TEAM_ATTR_ITEM_OPTION]
  234. * [TEAM_ATTR_OPTION_*], ...
  235. * [TEAM_ATTR_ITEM_OPTION]
  236. * [TEAM_ATTR_OPTION_*], ...
  237. * ...
  238. * [TEAM_ATTR_LIST_PORT]
  239. * [TEAM_ATTR_ITEM_PORT]
  240. * [TEAM_ATTR_PORT_*], ...
  241. * [TEAM_ATTR_ITEM_PORT]
  242. * [TEAM_ATTR_PORT_*], ...
  243. * ...
  244. */
  245. enum {
  246. TEAM_ATTR_ITEM_OPTION_UNSPEC,
  247. TEAM_ATTR_ITEM_OPTION, /* nest */
  248. __TEAM_ATTR_ITEM_OPTION_MAX,
  249. TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
  250. };
  251. enum {
  252. TEAM_ATTR_OPTION_UNSPEC,
  253. TEAM_ATTR_OPTION_NAME, /* string */
  254. TEAM_ATTR_OPTION_CHANGED, /* flag */
  255. TEAM_ATTR_OPTION_TYPE, /* u8 */
  256. TEAM_ATTR_OPTION_DATA, /* dynamic */
  257. TEAM_ATTR_OPTION_REMOVED, /* flag */
  258. TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
  259. TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */
  260. __TEAM_ATTR_OPTION_MAX,
  261. TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
  262. };
  263. enum {
  264. TEAM_ATTR_ITEM_PORT_UNSPEC,
  265. TEAM_ATTR_ITEM_PORT, /* nest */
  266. __TEAM_ATTR_ITEM_PORT_MAX,
  267. TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
  268. };
  269. enum {
  270. TEAM_ATTR_PORT_UNSPEC,
  271. TEAM_ATTR_PORT_IFINDEX, /* u32 */
  272. TEAM_ATTR_PORT_CHANGED, /* flag */
  273. TEAM_ATTR_PORT_LINKUP, /* flag */
  274. TEAM_ATTR_PORT_SPEED, /* u32 */
  275. TEAM_ATTR_PORT_DUPLEX, /* u8 */
  276. TEAM_ATTR_PORT_REMOVED, /* flag */
  277. __TEAM_ATTR_PORT_MAX,
  278. TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
  279. };
  280. /*
  281. * NETLINK_GENERIC related info
  282. */
  283. #define TEAM_GENL_NAME "team"
  284. #define TEAM_GENL_VERSION 0x1
  285. #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
  286. #endif /* _LINUX_IF_TEAM_H_ */