if_team.h 7.5 KB

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