if_team.h 8.5 KB

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