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