if_team.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include <linux/netpoll.h>
  13. #include <net/sch_generic.h>
  14. #include <linux/types.h>
  15. #include <uapi/linux/if_team.h>
  16. struct team_pcpu_stats {
  17. u64 rx_packets;
  18. u64 rx_bytes;
  19. u64 rx_multicast;
  20. u64 tx_packets;
  21. u64 tx_bytes;
  22. struct u64_stats_sync syncp;
  23. u32 rx_dropped;
  24. u32 tx_dropped;
  25. u32 rx_nohandler;
  26. };
  27. struct team;
  28. struct team_port {
  29. struct net_device *dev;
  30. struct hlist_node hlist; /* node in enabled ports hash list */
  31. struct list_head list; /* node in ordinary list */
  32. struct team *team;
  33. int index; /* index of enabled port. If disabled, it's set to -1 */
  34. bool linkup; /* either state.linkup or user.linkup */
  35. struct {
  36. bool linkup;
  37. u32 speed;
  38. u8 duplex;
  39. } state;
  40. /* Values set by userspace */
  41. struct {
  42. bool linkup;
  43. bool linkup_enabled;
  44. } user;
  45. /* Custom gennetlink interface related flags */
  46. bool changed;
  47. bool removed;
  48. /*
  49. * A place for storing original values of the device before it
  50. * become a port.
  51. */
  52. struct {
  53. unsigned char dev_addr[MAX_ADDR_LEN];
  54. unsigned int mtu;
  55. } orig;
  56. #ifdef CONFIG_NET_POLL_CONTROLLER
  57. struct netpoll *np;
  58. #endif
  59. s32 priority; /* lower number ~ higher priority */
  60. u16 queue_id;
  61. struct list_head qom_list; /* node in queue override mapping list */
  62. struct rcu_head rcu;
  63. long mode_priv[0];
  64. };
  65. static inline bool team_port_enabled(struct team_port *port)
  66. {
  67. return port->index != -1;
  68. }
  69. static inline bool team_port_txable(struct team_port *port)
  70. {
  71. return port->linkup && team_port_enabled(port);
  72. }
  73. #ifdef CONFIG_NET_POLL_CONTROLLER
  74. static inline void team_netpoll_send_skb(struct team_port *port,
  75. struct sk_buff *skb)
  76. {
  77. struct netpoll *np = port->np;
  78. if (np)
  79. netpoll_send_skb(np, skb);
  80. }
  81. #else
  82. static inline void team_netpoll_send_skb(struct team_port *port,
  83. struct sk_buff *skb)
  84. {
  85. }
  86. #endif
  87. struct team_mode_ops {
  88. int (*init)(struct team *team);
  89. void (*exit)(struct team *team);
  90. rx_handler_result_t (*receive)(struct team *team,
  91. struct team_port *port,
  92. struct sk_buff *skb);
  93. bool (*transmit)(struct team *team, struct sk_buff *skb);
  94. int (*port_enter)(struct team *team, struct team_port *port);
  95. void (*port_leave)(struct team *team, struct team_port *port);
  96. void (*port_change_dev_addr)(struct team *team, struct team_port *port);
  97. void (*port_enabled)(struct team *team, struct team_port *port);
  98. void (*port_disabled)(struct team *team, struct team_port *port);
  99. };
  100. extern int team_modeop_port_enter(struct team *team, struct team_port *port);
  101. extern void team_modeop_port_change_dev_addr(struct team *team,
  102. struct team_port *port);
  103. enum team_option_type {
  104. TEAM_OPTION_TYPE_U32,
  105. TEAM_OPTION_TYPE_STRING,
  106. TEAM_OPTION_TYPE_BINARY,
  107. TEAM_OPTION_TYPE_BOOL,
  108. TEAM_OPTION_TYPE_S32,
  109. };
  110. struct team_option_inst_info {
  111. u32 array_index;
  112. struct team_port *port; /* != NULL if per-port */
  113. };
  114. struct team_gsetter_ctx {
  115. union {
  116. u32 u32_val;
  117. const char *str_val;
  118. struct {
  119. const void *ptr;
  120. u32 len;
  121. } bin_val;
  122. bool bool_val;
  123. s32 s32_val;
  124. } data;
  125. struct team_option_inst_info *info;
  126. };
  127. struct team_option {
  128. struct list_head list;
  129. const char *name;
  130. bool per_port;
  131. unsigned int array_size; /* != 0 means the option is array */
  132. enum team_option_type type;
  133. int (*init)(struct team *team, struct team_option_inst_info *info);
  134. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  135. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  136. };
  137. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  138. extern void team_options_change_check(struct team *team);
  139. struct team_mode {
  140. const char *kind;
  141. struct module *owner;
  142. size_t priv_size;
  143. size_t port_priv_size;
  144. const struct team_mode_ops *ops;
  145. enum netdev_lag_tx_type lag_tx_type;
  146. };
  147. #define TEAM_PORT_HASHBITS 4
  148. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  149. #define TEAM_MODE_PRIV_LONGS 4
  150. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  151. struct team {
  152. struct net_device *dev; /* associated netdevice */
  153. struct team_pcpu_stats __percpu *pcpu_stats;
  154. struct mutex lock; /* used for overall locking, e.g. port lists write */
  155. /*
  156. * List of enabled ports and their count
  157. */
  158. int en_port_count;
  159. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  160. struct list_head port_list; /* list of all ports */
  161. struct list_head option_list;
  162. struct list_head option_inst_list; /* list of option instances */
  163. const struct team_mode *mode;
  164. struct team_mode_ops ops;
  165. bool user_carrier_enabled;
  166. bool queue_override_enabled;
  167. struct list_head *qom_lists; /* array of queue override mapping lists */
  168. bool port_mtu_change_allowed;
  169. struct {
  170. unsigned int count;
  171. unsigned int interval; /* in ms */
  172. atomic_t count_pending;
  173. struct delayed_work dw;
  174. } notify_peers;
  175. struct {
  176. unsigned int count;
  177. unsigned int interval; /* in ms */
  178. atomic_t count_pending;
  179. struct delayed_work dw;
  180. } mcast_rejoin;
  181. long mode_priv[TEAM_MODE_PRIV_LONGS];
  182. };
  183. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  184. struct sk_buff *skb)
  185. {
  186. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  187. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  188. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  189. skb->dev = port->dev;
  190. if (unlikely(netpoll_tx_running(team->dev))) {
  191. team_netpoll_send_skb(port, skb);
  192. return 0;
  193. }
  194. return dev_queue_xmit(skb);
  195. }
  196. static inline struct hlist_head *team_port_index_hash(struct team *team,
  197. int port_index)
  198. {
  199. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  200. }
  201. static inline struct team_port *team_get_port_by_index(struct team *team,
  202. int port_index)
  203. {
  204. struct team_port *port;
  205. struct hlist_head *head = team_port_index_hash(team, port_index);
  206. hlist_for_each_entry(port, head, hlist)
  207. if (port->index == port_index)
  208. return port;
  209. return NULL;
  210. }
  211. static inline int team_num_to_port_index(struct team *team, unsigned int num)
  212. {
  213. int en_port_count = ACCESS_ONCE(team->en_port_count);
  214. if (unlikely(!en_port_count))
  215. return 0;
  216. return num % en_port_count;
  217. }
  218. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  219. int port_index)
  220. {
  221. struct team_port *port;
  222. struct hlist_head *head = team_port_index_hash(team, port_index);
  223. hlist_for_each_entry_rcu(port, head, hlist)
  224. if (port->index == port_index)
  225. return port;
  226. return NULL;
  227. }
  228. static inline struct team_port *
  229. team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
  230. {
  231. struct team_port *cur;
  232. if (likely(team_port_txable(port)))
  233. return port;
  234. cur = port;
  235. list_for_each_entry_continue_rcu(cur, &team->port_list, list)
  236. if (team_port_txable(cur))
  237. return cur;
  238. list_for_each_entry_rcu(cur, &team->port_list, list) {
  239. if (cur == port)
  240. break;
  241. if (team_port_txable(cur))
  242. return cur;
  243. }
  244. return NULL;
  245. }
  246. extern int team_options_register(struct team *team,
  247. const struct team_option *option,
  248. size_t option_count);
  249. extern void team_options_unregister(struct team *team,
  250. const struct team_option *option,
  251. size_t option_count);
  252. extern int team_mode_register(const struct team_mode *mode);
  253. extern void team_mode_unregister(const struct team_mode *mode);
  254. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  255. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  256. #endif /* _LINUX_IF_TEAM_H_ */