if_team.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 struct team_port *team_port_get_rcu(const struct net_device *dev)
  66. {
  67. return rcu_dereference(dev->rx_handler_data);
  68. }
  69. static inline bool team_port_enabled(struct team_port *port)
  70. {
  71. return port->index != -1;
  72. }
  73. static inline bool team_port_txable(struct team_port *port)
  74. {
  75. return port->linkup && team_port_enabled(port);
  76. }
  77. static inline bool team_port_dev_txable(const struct net_device *port_dev)
  78. {
  79. struct team_port *port;
  80. bool txable;
  81. rcu_read_lock();
  82. port = team_port_get_rcu(port_dev);
  83. txable = port ? team_port_txable(port) : false;
  84. rcu_read_unlock();
  85. return txable;
  86. }
  87. #ifdef CONFIG_NET_POLL_CONTROLLER
  88. static inline void team_netpoll_send_skb(struct team_port *port,
  89. struct sk_buff *skb)
  90. {
  91. struct netpoll *np = port->np;
  92. if (np)
  93. netpoll_send_skb(np, skb);
  94. }
  95. #else
  96. static inline void team_netpoll_send_skb(struct team_port *port,
  97. struct sk_buff *skb)
  98. {
  99. }
  100. #endif
  101. struct team_mode_ops {
  102. int (*init)(struct team *team);
  103. void (*exit)(struct team *team);
  104. rx_handler_result_t (*receive)(struct team *team,
  105. struct team_port *port,
  106. struct sk_buff *skb);
  107. bool (*transmit)(struct team *team, struct sk_buff *skb);
  108. int (*port_enter)(struct team *team, struct team_port *port);
  109. void (*port_leave)(struct team *team, struct team_port *port);
  110. void (*port_change_dev_addr)(struct team *team, struct team_port *port);
  111. void (*port_enabled)(struct team *team, struct team_port *port);
  112. void (*port_disabled)(struct team *team, struct team_port *port);
  113. };
  114. extern int team_modeop_port_enter(struct team *team, struct team_port *port);
  115. extern void team_modeop_port_change_dev_addr(struct team *team,
  116. struct team_port *port);
  117. enum team_option_type {
  118. TEAM_OPTION_TYPE_U32,
  119. TEAM_OPTION_TYPE_STRING,
  120. TEAM_OPTION_TYPE_BINARY,
  121. TEAM_OPTION_TYPE_BOOL,
  122. TEAM_OPTION_TYPE_S32,
  123. };
  124. struct team_option_inst_info {
  125. u32 array_index;
  126. struct team_port *port; /* != NULL if per-port */
  127. };
  128. struct team_gsetter_ctx {
  129. union {
  130. u32 u32_val;
  131. const char *str_val;
  132. struct {
  133. const void *ptr;
  134. u32 len;
  135. } bin_val;
  136. bool bool_val;
  137. s32 s32_val;
  138. } data;
  139. struct team_option_inst_info *info;
  140. };
  141. struct team_option {
  142. struct list_head list;
  143. const char *name;
  144. bool per_port;
  145. unsigned int array_size; /* != 0 means the option is array */
  146. enum team_option_type type;
  147. int (*init)(struct team *team, struct team_option_inst_info *info);
  148. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  149. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  150. };
  151. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  152. extern void team_options_change_check(struct team *team);
  153. struct team_mode {
  154. const char *kind;
  155. struct module *owner;
  156. size_t priv_size;
  157. size_t port_priv_size;
  158. const struct team_mode_ops *ops;
  159. enum netdev_lag_tx_type lag_tx_type;
  160. };
  161. #define TEAM_PORT_HASHBITS 4
  162. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  163. #define TEAM_MODE_PRIV_LONGS 4
  164. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  165. struct team {
  166. struct net_device *dev; /* associated netdevice */
  167. struct team_pcpu_stats __percpu *pcpu_stats;
  168. struct mutex lock; /* used for overall locking, e.g. port lists write */
  169. /*
  170. * List of enabled ports and their count
  171. */
  172. int en_port_count;
  173. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  174. struct list_head port_list; /* list of all ports */
  175. struct list_head option_list;
  176. struct list_head option_inst_list; /* list of option instances */
  177. const struct team_mode *mode;
  178. struct team_mode_ops ops;
  179. bool user_carrier_enabled;
  180. bool queue_override_enabled;
  181. struct list_head *qom_lists; /* array of queue override mapping lists */
  182. bool port_mtu_change_allowed;
  183. struct {
  184. unsigned int count;
  185. unsigned int interval; /* in ms */
  186. atomic_t count_pending;
  187. struct delayed_work dw;
  188. } notify_peers;
  189. struct {
  190. unsigned int count;
  191. unsigned int interval; /* in ms */
  192. atomic_t count_pending;
  193. struct delayed_work dw;
  194. } mcast_rejoin;
  195. long mode_priv[TEAM_MODE_PRIV_LONGS];
  196. };
  197. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  198. struct sk_buff *skb)
  199. {
  200. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  201. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  202. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  203. skb->dev = port->dev;
  204. if (unlikely(netpoll_tx_running(team->dev))) {
  205. team_netpoll_send_skb(port, skb);
  206. return 0;
  207. }
  208. return dev_queue_xmit(skb);
  209. }
  210. static inline struct hlist_head *team_port_index_hash(struct team *team,
  211. int port_index)
  212. {
  213. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  214. }
  215. static inline struct team_port *team_get_port_by_index(struct team *team,
  216. int port_index)
  217. {
  218. struct team_port *port;
  219. struct hlist_head *head = team_port_index_hash(team, port_index);
  220. hlist_for_each_entry(port, head, hlist)
  221. if (port->index == port_index)
  222. return port;
  223. return NULL;
  224. }
  225. static inline int team_num_to_port_index(struct team *team, unsigned int num)
  226. {
  227. int en_port_count = READ_ONCE(team->en_port_count);
  228. if (unlikely(!en_port_count))
  229. return 0;
  230. return num % en_port_count;
  231. }
  232. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  233. int port_index)
  234. {
  235. struct team_port *port;
  236. struct hlist_head *head = team_port_index_hash(team, port_index);
  237. hlist_for_each_entry_rcu(port, head, hlist)
  238. if (port->index == port_index)
  239. return port;
  240. return NULL;
  241. }
  242. static inline struct team_port *
  243. team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
  244. {
  245. struct team_port *cur;
  246. if (likely(team_port_txable(port)))
  247. return port;
  248. cur = port;
  249. list_for_each_entry_continue_rcu(cur, &team->port_list, list)
  250. if (team_port_txable(cur))
  251. return cur;
  252. list_for_each_entry_rcu(cur, &team->port_list, list) {
  253. if (cur == port)
  254. break;
  255. if (team_port_txable(cur))
  256. return cur;
  257. }
  258. return NULL;
  259. }
  260. extern int team_options_register(struct team *team,
  261. const struct team_option *option,
  262. size_t option_count);
  263. extern void team_options_unregister(struct team *team,
  264. const struct team_option *option,
  265. size_t option_count);
  266. extern int team_mode_register(const struct team_mode *mode);
  267. extern void team_mode_unregister(const struct team_mode *mode);
  268. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  269. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  270. #define MODULE_ALIAS_TEAM_MODE(kind) MODULE_ALIAS("team-mode-" kind)
  271. #endif /* _LINUX_IF_TEAM_H_ */