if_team.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * include/linux/if_team.h - Network team device driver header
  4. * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
  5. */
  6. #ifndef _LINUX_IF_TEAM_H_
  7. #define _LINUX_IF_TEAM_H_
  8. #include <linux/netpoll.h>
  9. #include <net/sch_generic.h>
  10. #include <linux/types.h>
  11. #include <uapi/linux/if_team.h>
  12. struct team_pcpu_stats {
  13. u64 rx_packets;
  14. u64 rx_bytes;
  15. u64 rx_multicast;
  16. u64 tx_packets;
  17. u64 tx_bytes;
  18. struct u64_stats_sync syncp;
  19. u32 rx_dropped;
  20. u32 tx_dropped;
  21. u32 rx_nohandler;
  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. #ifdef CONFIG_NET_POLL_CONTROLLER
  53. struct netpoll *np;
  54. #endif
  55. s32 priority; /* lower number ~ higher priority */
  56. u16 queue_id;
  57. struct list_head qom_list; /* node in queue override mapping list */
  58. struct rcu_head rcu;
  59. long mode_priv[];
  60. };
  61. static inline struct team_port *team_port_get_rcu(const struct net_device *dev)
  62. {
  63. return rcu_dereference(dev->rx_handler_data);
  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. static inline bool team_port_dev_txable(const struct net_device *port_dev)
  74. {
  75. struct team_port *port;
  76. bool txable;
  77. rcu_read_lock();
  78. port = team_port_get_rcu(port_dev);
  79. txable = port ? team_port_txable(port) : false;
  80. rcu_read_unlock();
  81. return txable;
  82. }
  83. #ifdef CONFIG_NET_POLL_CONTROLLER
  84. static inline void team_netpoll_send_skb(struct team_port *port,
  85. struct sk_buff *skb)
  86. {
  87. netpoll_send_skb(port->np, skb);
  88. }
  89. #else
  90. static inline void team_netpoll_send_skb(struct team_port *port,
  91. struct sk_buff *skb)
  92. {
  93. }
  94. #endif
  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_dev_addr)(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. extern int team_modeop_port_enter(struct team *team, struct team_port *port);
  109. extern void team_modeop_port_change_dev_addr(struct team *team,
  110. struct team_port *port);
  111. enum team_option_type {
  112. TEAM_OPTION_TYPE_U32,
  113. TEAM_OPTION_TYPE_STRING,
  114. TEAM_OPTION_TYPE_BINARY,
  115. TEAM_OPTION_TYPE_BOOL,
  116. TEAM_OPTION_TYPE_S32,
  117. };
  118. struct team_option_inst_info {
  119. u32 array_index;
  120. struct team_port *port; /* != NULL if per-port */
  121. };
  122. struct team_gsetter_ctx {
  123. union {
  124. u32 u32_val;
  125. const char *str_val;
  126. struct {
  127. const void *ptr;
  128. u32 len;
  129. } bin_val;
  130. bool bool_val;
  131. s32 s32_val;
  132. } data;
  133. struct team_option_inst_info *info;
  134. };
  135. struct team_option {
  136. struct list_head list;
  137. const char *name;
  138. bool per_port;
  139. unsigned int array_size; /* != 0 means the option is array */
  140. enum team_option_type type;
  141. int (*init)(struct team *team, struct team_option_inst_info *info);
  142. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  143. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  144. };
  145. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  146. extern void team_options_change_check(struct team *team);
  147. struct team_mode {
  148. const char *kind;
  149. struct module *owner;
  150. size_t priv_size;
  151. size_t port_priv_size;
  152. const struct team_mode_ops *ops;
  153. enum netdev_lag_tx_type lag_tx_type;
  154. };
  155. #define TEAM_PORT_HASHBITS 4
  156. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  157. #define TEAM_MODE_PRIV_LONGS 4
  158. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  159. struct team {
  160. struct net_device *dev; /* associated netdevice */
  161. struct team_pcpu_stats __percpu *pcpu_stats;
  162. struct mutex lock; /* used for overall locking, e.g. port lists write */
  163. /*
  164. * List of enabled ports and their count
  165. */
  166. int en_port_count;
  167. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  168. struct list_head port_list; /* list of all ports */
  169. struct list_head option_list;
  170. struct list_head option_inst_list; /* list of option instances */
  171. const struct team_mode *mode;
  172. struct team_mode_ops ops;
  173. bool user_carrier_enabled;
  174. bool queue_override_enabled;
  175. struct list_head *qom_lists; /* array of queue override mapping lists */
  176. bool port_mtu_change_allowed;
  177. struct {
  178. unsigned int count;
  179. unsigned int interval; /* in ms */
  180. atomic_t count_pending;
  181. struct delayed_work dw;
  182. } notify_peers;
  183. struct {
  184. unsigned int count;
  185. unsigned int interval; /* in ms */
  186. atomic_t count_pending;
  187. struct delayed_work dw;
  188. } mcast_rejoin;
  189. struct lock_class_key team_lock_key;
  190. long mode_priv[TEAM_MODE_PRIV_LONGS];
  191. };
  192. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  193. struct sk_buff *skb)
  194. {
  195. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  196. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  197. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  198. skb->dev = port->dev;
  199. if (unlikely(netpoll_tx_running(team->dev))) {
  200. team_netpoll_send_skb(port, skb);
  201. return 0;
  202. }
  203. return dev_queue_xmit(skb);
  204. }
  205. static inline struct hlist_head *team_port_index_hash(struct team *team,
  206. int port_index)
  207. {
  208. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  209. }
  210. static inline struct team_port *team_get_port_by_index(struct team *team,
  211. int port_index)
  212. {
  213. struct team_port *port;
  214. struct hlist_head *head = team_port_index_hash(team, port_index);
  215. hlist_for_each_entry(port, head, hlist)
  216. if (port->index == port_index)
  217. return port;
  218. return NULL;
  219. }
  220. static inline int team_num_to_port_index(struct team *team, unsigned int num)
  221. {
  222. int en_port_count = READ_ONCE(team->en_port_count);
  223. if (unlikely(!en_port_count))
  224. return 0;
  225. return num % en_port_count;
  226. }
  227. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  228. int port_index)
  229. {
  230. struct team_port *port;
  231. struct hlist_head *head = team_port_index_hash(team, port_index);
  232. hlist_for_each_entry_rcu(port, head, hlist)
  233. if (port->index == port_index)
  234. return port;
  235. return NULL;
  236. }
  237. static inline struct team_port *
  238. team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
  239. {
  240. struct team_port *cur;
  241. if (likely(team_port_txable(port)))
  242. return port;
  243. cur = port;
  244. list_for_each_entry_continue_rcu(cur, &team->port_list, list)
  245. if (team_port_txable(cur))
  246. return cur;
  247. list_for_each_entry_rcu(cur, &team->port_list, list) {
  248. if (cur == port)
  249. break;
  250. if (team_port_txable(cur))
  251. return cur;
  252. }
  253. return NULL;
  254. }
  255. extern int team_options_register(struct team *team,
  256. const struct team_option *option,
  257. size_t option_count);
  258. extern void team_options_unregister(struct team *team,
  259. const struct team_option *option,
  260. size_t option_count);
  261. extern int team_mode_register(const struct team_mode *mode);
  262. extern void team_mode_unregister(const struct team_mode *mode);
  263. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  264. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  265. #define MODULE_ALIAS_TEAM_MODE(kind) MODULE_ALIAS("team-mode-" kind)
  266. #endif /* _LINUX_IF_TEAM_H_ */