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