if_team.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <[email protected]>
  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_stats_t rx_packets;
  14. u64_stats_t rx_bytes;
  15. u64_stats_t rx_multicast;
  16. u64_stats_t tx_packets;
  17. u64_stats_t 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. const struct header_ops *header_ops_cache;
  163. struct mutex lock; /* used for overall locking, e.g. port lists write */
  164. /*
  165. * List of enabled ports and their count
  166. */
  167. int en_port_count;
  168. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  169. struct list_head port_list; /* list of all ports */
  170. struct list_head option_list;
  171. struct list_head option_inst_list; /* list of option instances */
  172. const struct team_mode *mode;
  173. struct team_mode_ops ops;
  174. bool user_carrier_enabled;
  175. bool queue_override_enabled;
  176. struct list_head *qom_lists; /* array of queue override mapping lists */
  177. bool port_mtu_change_allowed;
  178. bool notifier_ctx;
  179. struct {
  180. unsigned int count;
  181. unsigned int interval; /* in ms */
  182. atomic_t count_pending;
  183. struct delayed_work dw;
  184. } notify_peers;
  185. struct {
  186. unsigned int count;
  187. unsigned int interval; /* in ms */
  188. atomic_t count_pending;
  189. struct delayed_work dw;
  190. } mcast_rejoin;
  191. struct lock_class_key team_lock_key;
  192. long mode_priv[TEAM_MODE_PRIV_LONGS];
  193. };
  194. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  195. struct sk_buff *skb)
  196. {
  197. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  198. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  199. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  200. skb->dev = port->dev;
  201. if (unlikely(netpoll_tx_running(team->dev))) {
  202. team_netpoll_send_skb(port, skb);
  203. return 0;
  204. }
  205. return dev_queue_xmit(skb);
  206. }
  207. static inline struct hlist_head *team_port_index_hash(struct team *team,
  208. int port_index)
  209. {
  210. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  211. }
  212. static inline struct team_port *team_get_port_by_index(struct team *team,
  213. int port_index)
  214. {
  215. struct team_port *port;
  216. struct hlist_head *head = team_port_index_hash(team, port_index);
  217. hlist_for_each_entry(port, head, hlist)
  218. if (port->index == port_index)
  219. return port;
  220. return NULL;
  221. }
  222. static inline int team_num_to_port_index(struct team *team, unsigned int num)
  223. {
  224. int en_port_count = READ_ONCE(team->en_port_count);
  225. if (unlikely(!en_port_count))
  226. return 0;
  227. return num % en_port_count;
  228. }
  229. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  230. int port_index)
  231. {
  232. struct team_port *port;
  233. struct hlist_head *head = team_port_index_hash(team, port_index);
  234. hlist_for_each_entry_rcu(port, head, hlist)
  235. if (port->index == port_index)
  236. return port;
  237. return NULL;
  238. }
  239. static inline struct team_port *
  240. team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
  241. {
  242. struct team_port *cur;
  243. if (likely(team_port_txable(port)))
  244. return port;
  245. cur = port;
  246. list_for_each_entry_continue_rcu(cur, &team->port_list, list)
  247. if (team_port_txable(cur))
  248. return cur;
  249. list_for_each_entry_rcu(cur, &team->port_list, list) {
  250. if (cur == port)
  251. break;
  252. if (team_port_txable(cur))
  253. return cur;
  254. }
  255. return NULL;
  256. }
  257. extern int team_options_register(struct team *team,
  258. const struct team_option *option,
  259. size_t option_count);
  260. extern void team_options_unregister(struct team *team,
  261. const struct team_option *option,
  262. size_t option_count);
  263. extern int team_mode_register(const struct team_mode *mode);
  264. extern void team_mode_unregister(const struct team_mode *mode);
  265. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  266. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  267. #define MODULE_ALIAS_TEAM_MODE(kind) MODULE_ALIAS("team-mode-" kind)
  268. #endif /* _LINUX_IF_TEAM_H_ */