Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 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 | |
| 11 | #ifndef _LINUX_IF_TEAM_H_ |
| 12 | #define _LINUX_IF_TEAM_H_ |
| 13 | |
| 14 | #ifdef __KERNEL__ |
| 15 | |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame^] | 16 | #include <linux/netpoll.h> |
| 17 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 18 | struct team_pcpu_stats { |
| 19 | u64 rx_packets; |
| 20 | u64 rx_bytes; |
| 21 | u64 rx_multicast; |
| 22 | u64 tx_packets; |
| 23 | u64 tx_bytes; |
| 24 | struct u64_stats_sync syncp; |
| 25 | u32 rx_dropped; |
| 26 | u32 tx_dropped; |
| 27 | }; |
| 28 | |
| 29 | struct team; |
| 30 | |
| 31 | struct team_port { |
| 32 | struct net_device *dev; |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 33 | struct hlist_node hlist; /* node in enabled ports hash list */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 34 | struct list_head list; /* node in ordinary list */ |
| 35 | struct team *team; |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 36 | int index; /* index of enabled port. If disabled, it's set to -1 */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 37 | |
Jiri Pirko | 71472ec | 2012-04-10 05:15:44 +0000 | [diff] [blame] | 38 | bool linkup; /* either state.linkup or user.linkup */ |
| 39 | |
| 40 | struct { |
| 41 | bool linkup; |
| 42 | u32 speed; |
| 43 | u8 duplex; |
| 44 | } state; |
| 45 | |
| 46 | /* Values set by userspace */ |
| 47 | struct { |
| 48 | bool linkup; |
| 49 | bool linkup_enabled; |
| 50 | } user; |
| 51 | |
| 52 | /* Custom gennetlink interface related flags */ |
| 53 | bool changed; |
| 54 | bool removed; |
| 55 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 56 | /* |
| 57 | * A place for storing original values of the device before it |
| 58 | * become a port. |
| 59 | */ |
| 60 | struct { |
| 61 | unsigned char dev_addr[MAX_ADDR_LEN]; |
| 62 | unsigned int mtu; |
| 63 | } orig; |
| 64 | |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame^] | 65 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 66 | struct netpoll *np; |
| 67 | #endif |
| 68 | |
Jiri Pirko | 5149ee5 | 2012-06-19 05:54:05 +0000 | [diff] [blame] | 69 | long mode_priv[0]; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Jiri Pirko | 68c4504 | 2012-07-11 05:34:04 +0000 | [diff] [blame] | 72 | static inline bool team_port_enabled(struct team_port *port) |
| 73 | { |
| 74 | return port->index != -1; |
| 75 | } |
| 76 | |
| 77 | static inline bool team_port_txable(struct team_port *port) |
| 78 | { |
| 79 | return port->linkup && team_port_enabled(port); |
| 80 | } |
Jiri Pirko | 52a4fd7 | 2012-06-26 06:52:46 +0000 | [diff] [blame] | 81 | |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame^] | 82 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 83 | static inline void team_netpoll_send_skb(struct team_port *port, |
| 84 | struct sk_buff *skb) |
| 85 | { |
| 86 | struct netpoll *np = port->np; |
| 87 | |
| 88 | if (np) |
| 89 | netpoll_send_skb(np, skb); |
| 90 | } |
| 91 | #else |
| 92 | static inline void team_netpoll_send_skb(struct team_port *port, |
| 93 | struct sk_buff *skb) |
| 94 | { |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | static inline int team_dev_queue_xmit(struct team *team, struct team_port *port, |
| 99 | struct sk_buff *skb) |
| 100 | { |
| 101 | skb->dev = port->dev; |
| 102 | if (unlikely(netpoll_tx_running(port->dev))) { |
| 103 | team_netpoll_send_skb(port, skb); |
| 104 | return 0; |
| 105 | } |
| 106 | return dev_queue_xmit(skb); |
| 107 | } |
| 108 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 109 | struct team_mode_ops { |
| 110 | int (*init)(struct team *team); |
| 111 | void (*exit)(struct team *team); |
| 112 | rx_handler_result_t (*receive)(struct team *team, |
| 113 | struct team_port *port, |
| 114 | struct sk_buff *skb); |
| 115 | bool (*transmit)(struct team *team, struct sk_buff *skb); |
| 116 | int (*port_enter)(struct team *team, struct team_port *port); |
| 117 | void (*port_leave)(struct team *team, struct team_port *port); |
| 118 | void (*port_change_mac)(struct team *team, struct team_port *port); |
Jiri Pirko | 4bccfd1 | 2012-06-19 05:54:16 +0000 | [diff] [blame] | 119 | void (*port_enabled)(struct team *team, struct team_port *port); |
| 120 | void (*port_disabled)(struct team *team, struct team_port *port); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | enum team_option_type { |
| 124 | TEAM_OPTION_TYPE_U32, |
| 125 | TEAM_OPTION_TYPE_STRING, |
Jiri Pirko | 2615598 | 2012-04-04 12:16:26 +0000 | [diff] [blame] | 126 | TEAM_OPTION_TYPE_BINARY, |
Jiri Pirko | 14f066b | 2012-04-10 05:15:43 +0000 | [diff] [blame] | 127 | TEAM_OPTION_TYPE_BOOL, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Jiri Pirko | 85d59a8 | 2012-06-19 05:54:10 +0000 | [diff] [blame] | 130 | struct team_option_inst_info { |
| 131 | u32 array_index; |
| 132 | struct team_port *port; /* != NULL if per-port */ |
| 133 | }; |
| 134 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 135 | struct team_gsetter_ctx { |
| 136 | union { |
| 137 | u32 u32_val; |
| 138 | const char *str_val; |
| 139 | struct { |
| 140 | const void *ptr; |
| 141 | u32 len; |
| 142 | } bin_val; |
Jiri Pirko | 14f066b | 2012-04-10 05:15:43 +0000 | [diff] [blame] | 143 | bool bool_val; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 144 | } data; |
Jiri Pirko | 85d59a8 | 2012-06-19 05:54:10 +0000 | [diff] [blame] | 145 | struct team_option_inst_info *info; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 148 | struct team_option { |
| 149 | struct list_head list; |
| 150 | const char *name; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 151 | bool per_port; |
Jiri Pirko | b130332 | 2012-06-19 05:54:08 +0000 | [diff] [blame] | 152 | unsigned int array_size; /* != 0 means the option is array */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 153 | enum team_option_type type; |
Jiri Pirko | 85d59a8 | 2012-06-19 05:54:10 +0000 | [diff] [blame] | 154 | int (*init)(struct team *team, struct team_option_inst_info *info); |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 155 | int (*getter)(struct team *team, struct team_gsetter_ctx *ctx); |
| 156 | int (*setter)(struct team *team, struct team_gsetter_ctx *ctx); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Jiri Pirko | 0f1aad2 | 2012-06-19 05:54:11 +0000 | [diff] [blame] | 159 | extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info); |
| 160 | extern void team_options_change_check(struct team *team); |
| 161 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 162 | struct team_mode { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 163 | const char *kind; |
| 164 | struct module *owner; |
| 165 | size_t priv_size; |
Jiri Pirko | 5149ee5 | 2012-06-19 05:54:05 +0000 | [diff] [blame] | 166 | size_t port_priv_size; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 167 | const struct team_mode_ops *ops; |
| 168 | }; |
| 169 | |
| 170 | #define TEAM_PORT_HASHBITS 4 |
| 171 | #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS) |
| 172 | |
| 173 | #define TEAM_MODE_PRIV_LONGS 4 |
| 174 | #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS) |
| 175 | |
| 176 | struct team { |
| 177 | struct net_device *dev; /* associated netdevice */ |
| 178 | struct team_pcpu_stats __percpu *pcpu_stats; |
| 179 | |
Jiri Pirko | 61dc346 | 2011-11-16 11:09:08 +0000 | [diff] [blame] | 180 | struct mutex lock; /* used for overall locking, e.g. port lists write */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 181 | |
| 182 | /* |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 183 | * List of enabled ports and their count |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 184 | */ |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 185 | int en_port_count; |
| 186 | struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES]; |
| 187 | |
| 188 | struct list_head port_list; /* list of all ports */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 189 | |
| 190 | struct list_head option_list; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 191 | struct list_head option_inst_list; /* list of option instances */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 192 | |
| 193 | const struct team_mode *mode; |
| 194 | struct team_mode_ops ops; |
| 195 | long mode_priv[TEAM_MODE_PRIV_LONGS]; |
| 196 | }; |
| 197 | |
| 198 | static inline struct hlist_head *team_port_index_hash(struct team *team, |
| 199 | int port_index) |
| 200 | { |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 201 | return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)]; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static inline struct team_port *team_get_port_by_index(struct team *team, |
| 205 | int port_index) |
| 206 | { |
| 207 | struct hlist_node *p; |
| 208 | struct team_port *port; |
| 209 | struct hlist_head *head = team_port_index_hash(team, port_index); |
| 210 | |
| 211 | hlist_for_each_entry(port, p, head, hlist) |
| 212 | if (port->index == port_index) |
| 213 | return port; |
| 214 | return NULL; |
| 215 | } |
| 216 | static inline struct team_port *team_get_port_by_index_rcu(struct team *team, |
| 217 | int port_index) |
| 218 | { |
| 219 | struct hlist_node *p; |
| 220 | struct team_port *port; |
| 221 | struct hlist_head *head = team_port_index_hash(team, port_index); |
| 222 | |
| 223 | hlist_for_each_entry_rcu(port, p, head, hlist) |
| 224 | if (port->index == port_index) |
| 225 | return port; |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | extern int team_port_set_team_mac(struct team_port *port); |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 230 | extern int team_options_register(struct team *team, |
| 231 | const struct team_option *option, |
| 232 | size_t option_count); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 233 | extern void team_options_unregister(struct team *team, |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 234 | const struct team_option *option, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 235 | size_t option_count); |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 236 | extern int team_mode_register(const struct team_mode *mode); |
| 237 | extern void team_mode_unregister(const struct team_mode *mode); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 238 | |
| 239 | #endif /* __KERNEL__ */ |
| 240 | |
| 241 | #define TEAM_STRING_MAX_LEN 32 |
| 242 | |
| 243 | /********************************** |
| 244 | * NETLINK_GENERIC netlink family. |
| 245 | **********************************/ |
| 246 | |
| 247 | enum { |
| 248 | TEAM_CMD_NOOP, |
| 249 | TEAM_CMD_OPTIONS_SET, |
| 250 | TEAM_CMD_OPTIONS_GET, |
| 251 | TEAM_CMD_PORT_LIST_GET, |
| 252 | |
| 253 | __TEAM_CMD_MAX, |
| 254 | TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1), |
| 255 | }; |
| 256 | |
| 257 | enum { |
| 258 | TEAM_ATTR_UNSPEC, |
| 259 | TEAM_ATTR_TEAM_IFINDEX, /* u32 */ |
| 260 | TEAM_ATTR_LIST_OPTION, /* nest */ |
| 261 | TEAM_ATTR_LIST_PORT, /* nest */ |
| 262 | |
| 263 | __TEAM_ATTR_MAX, |
| 264 | TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1, |
| 265 | }; |
| 266 | |
| 267 | /* Nested layout of get/set msg: |
| 268 | * |
| 269 | * [TEAM_ATTR_LIST_OPTION] |
| 270 | * [TEAM_ATTR_ITEM_OPTION] |
| 271 | * [TEAM_ATTR_OPTION_*], ... |
| 272 | * [TEAM_ATTR_ITEM_OPTION] |
| 273 | * [TEAM_ATTR_OPTION_*], ... |
| 274 | * ... |
| 275 | * [TEAM_ATTR_LIST_PORT] |
| 276 | * [TEAM_ATTR_ITEM_PORT] |
| 277 | * [TEAM_ATTR_PORT_*], ... |
| 278 | * [TEAM_ATTR_ITEM_PORT] |
| 279 | * [TEAM_ATTR_PORT_*], ... |
| 280 | * ... |
| 281 | */ |
| 282 | |
| 283 | enum { |
| 284 | TEAM_ATTR_ITEM_OPTION_UNSPEC, |
| 285 | TEAM_ATTR_ITEM_OPTION, /* nest */ |
| 286 | |
| 287 | __TEAM_ATTR_ITEM_OPTION_MAX, |
| 288 | TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1, |
| 289 | }; |
| 290 | |
| 291 | enum { |
| 292 | TEAM_ATTR_OPTION_UNSPEC, |
| 293 | TEAM_ATTR_OPTION_NAME, /* string */ |
| 294 | TEAM_ATTR_OPTION_CHANGED, /* flag */ |
| 295 | TEAM_ATTR_OPTION_TYPE, /* u8 */ |
| 296 | TEAM_ATTR_OPTION_DATA, /* dynamic */ |
Jiri Pirko | b82b918 | 2012-01-24 05:16:00 +0000 | [diff] [blame] | 297 | TEAM_ATTR_OPTION_REMOVED, /* flag */ |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 298 | TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */ |
Jiri Pirko | b130332 | 2012-06-19 05:54:08 +0000 | [diff] [blame] | 299 | TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 300 | |
| 301 | __TEAM_ATTR_OPTION_MAX, |
| 302 | TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1, |
| 303 | }; |
| 304 | |
| 305 | enum { |
| 306 | TEAM_ATTR_ITEM_PORT_UNSPEC, |
| 307 | TEAM_ATTR_ITEM_PORT, /* nest */ |
| 308 | |
| 309 | __TEAM_ATTR_ITEM_PORT_MAX, |
| 310 | TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1, |
| 311 | }; |
| 312 | |
| 313 | enum { |
| 314 | TEAM_ATTR_PORT_UNSPEC, |
| 315 | TEAM_ATTR_PORT_IFINDEX, /* u32 */ |
| 316 | TEAM_ATTR_PORT_CHANGED, /* flag */ |
| 317 | TEAM_ATTR_PORT_LINKUP, /* flag */ |
| 318 | TEAM_ATTR_PORT_SPEED, /* u32 */ |
| 319 | TEAM_ATTR_PORT_DUPLEX, /* u8 */ |
Jiri Pirko | b82b918 | 2012-01-24 05:16:00 +0000 | [diff] [blame] | 320 | TEAM_ATTR_PORT_REMOVED, /* flag */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 321 | |
| 322 | __TEAM_ATTR_PORT_MAX, |
| 323 | TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1, |
| 324 | }; |
| 325 | |
| 326 | /* |
| 327 | * NETLINK_GENERIC related info |
| 328 | */ |
| 329 | #define TEAM_GENL_NAME "team" |
| 330 | #define TEAM_GENL_VERSION 0x1 |
| 331 | #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event" |
| 332 | |
| 333 | #endif /* _LINUX_IF_TEAM_H_ */ |