if_team.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #ifdef __KERNEL__
  13. struct team_pcpu_stats {
  14. u64 rx_packets;
  15. u64 rx_bytes;
  16. u64 rx_multicast;
  17. u64 tx_packets;
  18. u64 tx_bytes;
  19. struct u64_stats_sync syncp;
  20. u32 rx_dropped;
  21. u32 tx_dropped;
  22. };
  23. struct team;
  24. struct team_port {
  25. struct net_device *dev;
  26. struct hlist_node hlist; /* node in hash list */
  27. struct list_head list; /* node in ordinary list */
  28. struct team *team;
  29. int index;
  30. /*
  31. * A place for storing original values of the device before it
  32. * become a port.
  33. */
  34. struct {
  35. unsigned char dev_addr[MAX_ADDR_LEN];
  36. unsigned int mtu;
  37. } orig;
  38. bool linkup;
  39. u32 speed;
  40. u8 duplex;
  41. /* Custom gennetlink interface related flags */
  42. bool changed;
  43. bool removed;
  44. struct rcu_head rcu;
  45. };
  46. struct team_mode_ops {
  47. int (*init)(struct team *team);
  48. void (*exit)(struct team *team);
  49. rx_handler_result_t (*receive)(struct team *team,
  50. struct team_port *port,
  51. struct sk_buff *skb);
  52. bool (*transmit)(struct team *team, struct sk_buff *skb);
  53. int (*port_enter)(struct team *team, struct team_port *port);
  54. void (*port_leave)(struct team *team, struct team_port *port);
  55. void (*port_change_mac)(struct team *team, struct team_port *port);
  56. };
  57. enum team_option_type {
  58. TEAM_OPTION_TYPE_U32,
  59. TEAM_OPTION_TYPE_STRING,
  60. };
  61. struct team_option {
  62. struct list_head list;
  63. const char *name;
  64. enum team_option_type type;
  65. int (*getter)(struct team *team, void *arg);
  66. int (*setter)(struct team *team, void *arg);
  67. /* Custom gennetlink interface related flags */
  68. bool changed;
  69. bool removed;
  70. };
  71. struct team_mode {
  72. struct list_head list;
  73. const char *kind;
  74. struct module *owner;
  75. size_t priv_size;
  76. const struct team_mode_ops *ops;
  77. };
  78. #define TEAM_PORT_HASHBITS 4
  79. #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
  80. #define TEAM_MODE_PRIV_LONGS 4
  81. #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
  82. struct team {
  83. struct net_device *dev; /* associated netdevice */
  84. struct team_pcpu_stats __percpu *pcpu_stats;
  85. struct mutex lock; /* used for overall locking, e.g. port lists write */
  86. /*
  87. * port lists with port count
  88. */
  89. int port_count;
  90. struct hlist_head port_hlist[TEAM_PORT_HASHENTRIES];
  91. struct list_head port_list;
  92. struct list_head option_list;
  93. const struct team_mode *mode;
  94. struct team_mode_ops ops;
  95. bool port_mtu_change_allowed;
  96. long mode_priv[TEAM_MODE_PRIV_LONGS];
  97. };
  98. static inline struct hlist_head *team_port_index_hash(struct team *team,
  99. int port_index)
  100. {
  101. return &team->port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
  102. }
  103. static inline struct team_port *team_get_port_by_index(struct team *team,
  104. int port_index)
  105. {
  106. struct hlist_node *p;
  107. struct team_port *port;
  108. struct hlist_head *head = team_port_index_hash(team, port_index);
  109. hlist_for_each_entry(port, p, head, hlist)
  110. if (port->index == port_index)
  111. return port;
  112. return NULL;
  113. }
  114. static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
  115. int port_index)
  116. {
  117. struct hlist_node *p;
  118. struct team_port *port;
  119. struct hlist_head *head = team_port_index_hash(team, port_index);
  120. hlist_for_each_entry_rcu(port, p, head, hlist)
  121. if (port->index == port_index)
  122. return port;
  123. return NULL;
  124. }
  125. extern int team_port_set_team_mac(struct team_port *port);
  126. extern int team_options_register(struct team *team,
  127. const struct team_option *option,
  128. size_t option_count);
  129. extern void team_options_unregister(struct team *team,
  130. const struct team_option *option,
  131. size_t option_count);
  132. extern int team_mode_register(struct team_mode *mode);
  133. extern int team_mode_unregister(struct team_mode *mode);
  134. #endif /* __KERNEL__ */
  135. #define TEAM_STRING_MAX_LEN 32
  136. /**********************************
  137. * NETLINK_GENERIC netlink family.
  138. **********************************/
  139. enum {
  140. TEAM_CMD_NOOP,
  141. TEAM_CMD_OPTIONS_SET,
  142. TEAM_CMD_OPTIONS_GET,
  143. TEAM_CMD_PORT_LIST_GET,
  144. __TEAM_CMD_MAX,
  145. TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
  146. };
  147. enum {
  148. TEAM_ATTR_UNSPEC,
  149. TEAM_ATTR_TEAM_IFINDEX, /* u32 */
  150. TEAM_ATTR_LIST_OPTION, /* nest */
  151. TEAM_ATTR_LIST_PORT, /* nest */
  152. __TEAM_ATTR_MAX,
  153. TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
  154. };
  155. /* Nested layout of get/set msg:
  156. *
  157. * [TEAM_ATTR_LIST_OPTION]
  158. * [TEAM_ATTR_ITEM_OPTION]
  159. * [TEAM_ATTR_OPTION_*], ...
  160. * [TEAM_ATTR_ITEM_OPTION]
  161. * [TEAM_ATTR_OPTION_*], ...
  162. * ...
  163. * [TEAM_ATTR_LIST_PORT]
  164. * [TEAM_ATTR_ITEM_PORT]
  165. * [TEAM_ATTR_PORT_*], ...
  166. * [TEAM_ATTR_ITEM_PORT]
  167. * [TEAM_ATTR_PORT_*], ...
  168. * ...
  169. */
  170. enum {
  171. TEAM_ATTR_ITEM_OPTION_UNSPEC,
  172. TEAM_ATTR_ITEM_OPTION, /* nest */
  173. __TEAM_ATTR_ITEM_OPTION_MAX,
  174. TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
  175. };
  176. enum {
  177. TEAM_ATTR_OPTION_UNSPEC,
  178. TEAM_ATTR_OPTION_NAME, /* string */
  179. TEAM_ATTR_OPTION_CHANGED, /* flag */
  180. TEAM_ATTR_OPTION_TYPE, /* u8 */
  181. TEAM_ATTR_OPTION_DATA, /* dynamic */
  182. TEAM_ATTR_OPTION_REMOVED, /* flag */
  183. __TEAM_ATTR_OPTION_MAX,
  184. TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
  185. };
  186. enum {
  187. TEAM_ATTR_ITEM_PORT_UNSPEC,
  188. TEAM_ATTR_ITEM_PORT, /* nest */
  189. __TEAM_ATTR_ITEM_PORT_MAX,
  190. TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
  191. };
  192. enum {
  193. TEAM_ATTR_PORT_UNSPEC,
  194. TEAM_ATTR_PORT_IFINDEX, /* u32 */
  195. TEAM_ATTR_PORT_CHANGED, /* flag */
  196. TEAM_ATTR_PORT_LINKUP, /* flag */
  197. TEAM_ATTR_PORT_SPEED, /* u32 */
  198. TEAM_ATTR_PORT_DUPLEX, /* u8 */
  199. TEAM_ATTR_PORT_REMOVED, /* flag */
  200. __TEAM_ATTR_PORT_MAX,
  201. TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
  202. };
  203. /*
  204. * NETLINK_GENERIC related info
  205. */
  206. #define TEAM_GENL_NAME "team"
  207. #define TEAM_GENL_VERSION 0x1
  208. #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
  209. #endif /* _LINUX_IF_TEAM_H_ */