if_team.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. enum team_option_type {
  98. TEAM_OPTION_TYPE_U32,
  99. TEAM_OPTION_TYPE_STRING,
  100. TEAM_OPTION_TYPE_BINARY,
  101. TEAM_OPTION_TYPE_BOOL,
  102. TEAM_OPTION_TYPE_S32,
  103. };
  104. struct team_option_inst_info {
  105. u32 array_index;
  106. struct team_port *port; /* != NULL if per-port */
  107. };
  108. struct team_gsetter_ctx {
  109. union {
  110. u32 u32_val;
  111. const char *str_val;
  112. struct {
  113. const void *ptr;
  114. u32 len;
  115. } bin_val;
  116. bool bool_val;
  117. s32 s32_val;
  118. } data;
  119. struct team_option_inst_info *info;
  120. };
  121. struct team_option {
  122. struct list_head list;
  123. const char *name;
  124. bool per_port;
  125. unsigned int array_size; /* != 0 means the option is array */
  126. enum team_option_type type;
  127. int (*init)(struct team *team, struct team_option_inst_info *info);
  128. int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
  129. int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
  130. };
  131. extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
  132. extern void team_options_change_check(struct team *team);
  133. struct team_mode {
  134. const char *kind;
  135. struct module *owner;
  136. size_t priv_size;
  137. size_t port_priv_size;
  138. const struct team_mode_ops *ops;
  139. };
  140. #define TEAM_PORT_HASHBITS 4
  141. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  142. #define TEAM_MODE_PRIV_LONGS 4
  143. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  144. struct team {
  145. struct net_device *dev; /* associated netdevice */
  146. struct team_pcpu_stats __percpu *pcpu_stats;
  147. struct mutex lock; /* used for overall locking, e.g. port lists write */
  148. /*
  149. * List of enabled ports and their count
  150. */
  151. int en_port_count;
  152. struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
  153. struct list_head port_list; /* list of all ports */
  154. struct list_head option_list;
  155. struct list_head option_inst_list; /* list of option instances */
  156. const struct team_mode *mode;
  157. struct team_mode_ops ops;
  158. bool user_carrier_enabled;
  159. bool queue_override_enabled;
  160. struct list_head *qom_lists; /* array of queue override mapping lists */
  161. long mode_priv[TEAM_MODE_PRIV_LONGS];
  162. };
  163. static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
  164. struct sk_buff *skb)
  165. {
  166. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  167. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  168. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  169. skb->dev = port->dev;
  170. if (unlikely(netpoll_tx_running(team->dev))) {
  171. team_netpoll_send_skb(port, skb);
  172. return 0;
  173. }
  174. return dev_queue_xmit(skb);
  175. }
  176. static inline struct hlist_head *team_port_index_hash(struct team *team,
  177. int port_index)
  178. {
  179. return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  180. }
  181. static inline struct team_port *team_get_port_by_index(struct team *team,
  182. int port_index)
  183. {
  184. struct team_port *port;
  185. struct hlist_head *head = team_port_index_hash(team, port_index);
  186. hlist_for_each_entry(port, head, hlist)
  187. if (port->index == port_index)
  188. return port;
  189. return NULL;
  190. }
  191. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  192. int port_index)
  193. {
  194. struct team_port *port;
  195. struct hlist_head *head = team_port_index_hash(team, port_index);
  196. hlist_for_each_entry_rcu(port, head, hlist)
  197. if (port->index == port_index)
  198. return port;
  199. return NULL;
  200. }
  201. extern int team_port_set_team_dev_addr(struct team_port *port);
  202. extern int team_options_register(struct team *team,
  203. const struct team_option *option,
  204. size_t option_count);
  205. extern void team_options_unregister(struct team *team,
  206. const struct team_option *option,
  207. size_t option_count);
  208. extern int team_mode_register(const struct team_mode *mode);
  209. extern void team_mode_unregister(const struct team_mode *mode);
  210. #define TEAM_DEFAULT_NUM_TX_QUEUES 16
  211. #define TEAM_DEFAULT_NUM_RX_QUEUES 16
  212. #endif /* _LINUX_IF_TEAM_H_ */