if_team.h 7.0 KB

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