if_team.h 5.6 KB

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