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 | |
| 67 | struct team_mode_ops { |
| 68 | int (*init)(struct team *team); |
| 69 | void (*exit)(struct team *team); |
| 70 | rx_handler_result_t (*receive)(struct team *team, |
| 71 | struct team_port *port, |
| 72 | struct sk_buff *skb); |
| 73 | bool (*transmit)(struct team *team, struct sk_buff *skb); |
| 74 | int (*port_enter)(struct team *team, struct team_port *port); |
| 75 | void (*port_leave)(struct team *team, struct team_port *port); |
| 76 | void (*port_change_mac)(struct team *team, struct team_port *port); |
| 77 | }; |
| 78 | |
| 79 | enum team_option_type { |
| 80 | TEAM_OPTION_TYPE_U32, |
| 81 | TEAM_OPTION_TYPE_STRING, |
Jiri Pirko | 2615598 | 2012-04-04 12:16:26 +0000 | [diff] [blame] | 82 | TEAM_OPTION_TYPE_BINARY, |
Jiri Pirko | 14f066b | 2012-04-10 05:15:43 +0000 | [diff] [blame] | 83 | TEAM_OPTION_TYPE_BOOL, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 86 | struct team_gsetter_ctx { |
| 87 | union { |
| 88 | u32 u32_val; |
| 89 | const char *str_val; |
| 90 | struct { |
| 91 | const void *ptr; |
| 92 | u32 len; |
| 93 | } bin_val; |
Jiri Pirko | 14f066b | 2012-04-10 05:15:43 +0000 | [diff] [blame] | 94 | bool bool_val; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 95 | } data; |
Jiri Pirko | b130332 | 2012-06-19 05:54:08 +0000 | [diff] [blame^] | 96 | u32 array_index; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 97 | struct team_port *port; |
| 98 | }; |
| 99 | |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 100 | struct team_option { |
| 101 | struct list_head list; |
| 102 | const char *name; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 103 | bool per_port; |
Jiri Pirko | b130332 | 2012-06-19 05:54:08 +0000 | [diff] [blame^] | 104 | unsigned int array_size; /* != 0 means the option is array */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 105 | enum team_option_type type; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 106 | int (*getter)(struct team *team, struct team_gsetter_ctx *ctx); |
| 107 | int (*setter)(struct team *team, struct team_gsetter_ctx *ctx); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct team_mode { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 111 | const char *kind; |
| 112 | struct module *owner; |
| 113 | size_t priv_size; |
Jiri Pirko | 5149ee5 | 2012-06-19 05:54:05 +0000 | [diff] [blame] | 114 | size_t port_priv_size; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 115 | const struct team_mode_ops *ops; |
| 116 | }; |
| 117 | |
| 118 | #define TEAM_PORT_HASHBITS 4 |
| 119 | #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS) |
| 120 | |
| 121 | #define TEAM_MODE_PRIV_LONGS 4 |
| 122 | #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS) |
| 123 | |
| 124 | struct team { |
| 125 | struct net_device *dev; /* associated netdevice */ |
| 126 | struct team_pcpu_stats __percpu *pcpu_stats; |
| 127 | |
Jiri Pirko | 61dc346 | 2011-11-16 11:09:08 +0000 | [diff] [blame] | 128 | struct mutex lock; /* used for overall locking, e.g. port lists write */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 131 | * List of enabled ports and their count |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 132 | */ |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 133 | int en_port_count; |
| 134 | struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES]; |
| 135 | |
| 136 | struct list_head port_list; /* list of all ports */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 137 | |
| 138 | struct list_head option_list; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 139 | struct list_head option_inst_list; /* list of option instances */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 140 | |
| 141 | const struct team_mode *mode; |
| 142 | struct team_mode_ops ops; |
| 143 | long mode_priv[TEAM_MODE_PRIV_LONGS]; |
| 144 | }; |
| 145 | |
| 146 | static inline struct hlist_head *team_port_index_hash(struct team *team, |
| 147 | int port_index) |
| 148 | { |
Jiri Pirko | 19a0b58 | 2012-04-20 04:42:05 +0000 | [diff] [blame] | 149 | return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)]; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static inline struct team_port *team_get_port_by_index(struct team *team, |
| 153 | int port_index) |
| 154 | { |
| 155 | struct hlist_node *p; |
| 156 | struct team_port *port; |
| 157 | struct hlist_head *head = team_port_index_hash(team, port_index); |
| 158 | |
| 159 | hlist_for_each_entry(port, p, head, hlist) |
| 160 | if (port->index == port_index) |
| 161 | return port; |
| 162 | return NULL; |
| 163 | } |
| 164 | static inline struct team_port *team_get_port_by_index_rcu(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_rcu(port, p, head, hlist) |
| 172 | if (port->index == port_index) |
| 173 | return port; |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | extern int team_port_set_team_mac(struct team_port *port); |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 178 | extern int team_options_register(struct team *team, |
| 179 | const struct team_option *option, |
| 180 | size_t option_count); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 181 | extern void team_options_unregister(struct team *team, |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 182 | const struct team_option *option, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 183 | size_t option_count); |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 184 | extern int team_mode_register(const struct team_mode *mode); |
| 185 | extern void team_mode_unregister(const struct team_mode *mode); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 186 | |
| 187 | #endif /* __KERNEL__ */ |
| 188 | |
| 189 | #define TEAM_STRING_MAX_LEN 32 |
| 190 | |
| 191 | /********************************** |
| 192 | * NETLINK_GENERIC netlink family. |
| 193 | **********************************/ |
| 194 | |
| 195 | enum { |
| 196 | TEAM_CMD_NOOP, |
| 197 | TEAM_CMD_OPTIONS_SET, |
| 198 | TEAM_CMD_OPTIONS_GET, |
| 199 | TEAM_CMD_PORT_LIST_GET, |
| 200 | |
| 201 | __TEAM_CMD_MAX, |
| 202 | TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1), |
| 203 | }; |
| 204 | |
| 205 | enum { |
| 206 | TEAM_ATTR_UNSPEC, |
| 207 | TEAM_ATTR_TEAM_IFINDEX, /* u32 */ |
| 208 | TEAM_ATTR_LIST_OPTION, /* nest */ |
| 209 | TEAM_ATTR_LIST_PORT, /* nest */ |
| 210 | |
| 211 | __TEAM_ATTR_MAX, |
| 212 | TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1, |
| 213 | }; |
| 214 | |
| 215 | /* Nested layout of get/set msg: |
| 216 | * |
| 217 | * [TEAM_ATTR_LIST_OPTION] |
| 218 | * [TEAM_ATTR_ITEM_OPTION] |
| 219 | * [TEAM_ATTR_OPTION_*], ... |
| 220 | * [TEAM_ATTR_ITEM_OPTION] |
| 221 | * [TEAM_ATTR_OPTION_*], ... |
| 222 | * ... |
| 223 | * [TEAM_ATTR_LIST_PORT] |
| 224 | * [TEAM_ATTR_ITEM_PORT] |
| 225 | * [TEAM_ATTR_PORT_*], ... |
| 226 | * [TEAM_ATTR_ITEM_PORT] |
| 227 | * [TEAM_ATTR_PORT_*], ... |
| 228 | * ... |
| 229 | */ |
| 230 | |
| 231 | enum { |
| 232 | TEAM_ATTR_ITEM_OPTION_UNSPEC, |
| 233 | TEAM_ATTR_ITEM_OPTION, /* nest */ |
| 234 | |
| 235 | __TEAM_ATTR_ITEM_OPTION_MAX, |
| 236 | TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1, |
| 237 | }; |
| 238 | |
| 239 | enum { |
| 240 | TEAM_ATTR_OPTION_UNSPEC, |
| 241 | TEAM_ATTR_OPTION_NAME, /* string */ |
| 242 | TEAM_ATTR_OPTION_CHANGED, /* flag */ |
| 243 | TEAM_ATTR_OPTION_TYPE, /* u8 */ |
| 244 | TEAM_ATTR_OPTION_DATA, /* dynamic */ |
Jiri Pirko | b82b918 | 2012-01-24 05:16:00 +0000 | [diff] [blame] | 245 | TEAM_ATTR_OPTION_REMOVED, /* flag */ |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 246 | TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */ |
Jiri Pirko | b130332 | 2012-06-19 05:54:08 +0000 | [diff] [blame^] | 247 | TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 248 | |
| 249 | __TEAM_ATTR_OPTION_MAX, |
| 250 | TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1, |
| 251 | }; |
| 252 | |
| 253 | enum { |
| 254 | TEAM_ATTR_ITEM_PORT_UNSPEC, |
| 255 | TEAM_ATTR_ITEM_PORT, /* nest */ |
| 256 | |
| 257 | __TEAM_ATTR_ITEM_PORT_MAX, |
| 258 | TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1, |
| 259 | }; |
| 260 | |
| 261 | enum { |
| 262 | TEAM_ATTR_PORT_UNSPEC, |
| 263 | TEAM_ATTR_PORT_IFINDEX, /* u32 */ |
| 264 | TEAM_ATTR_PORT_CHANGED, /* flag */ |
| 265 | TEAM_ATTR_PORT_LINKUP, /* flag */ |
| 266 | TEAM_ATTR_PORT_SPEED, /* u32 */ |
| 267 | TEAM_ATTR_PORT_DUPLEX, /* u8 */ |
Jiri Pirko | b82b918 | 2012-01-24 05:16:00 +0000 | [diff] [blame] | 268 | TEAM_ATTR_PORT_REMOVED, /* flag */ |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 269 | |
| 270 | __TEAM_ATTR_PORT_MAX, |
| 271 | TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1, |
| 272 | }; |
| 273 | |
| 274 | /* |
| 275 | * NETLINK_GENERIC related info |
| 276 | */ |
| 277 | #define TEAM_GENL_NAME "team" |
| 278 | #define TEAM_GENL_VERSION 0x1 |
| 279 | #define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event" |
| 280 | |
| 281 | #endif /* _LINUX_IF_TEAM_H_ */ |