blob: 2f2972535cc0deae3bdb462e49123ce206344929 [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
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
16struct 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
27struct team;
28
29struct team_port {
30 struct net_device *dev;
Jiri Pirko19a0b582012-04-20 04:42:05 +000031 struct hlist_node hlist; /* node in enabled ports hash list */
Jiri Pirko3d249d42011-11-11 22:16:48 +000032 struct list_head list; /* node in ordinary list */
33 struct team *team;
Jiri Pirko19a0b582012-04-20 04:42:05 +000034 int index; /* index of enabled port. If disabled, it's set to -1 */
Jiri Pirko3d249d42011-11-11 22:16:48 +000035
Jiri Pirko71472ec2012-04-10 05:15:44 +000036 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 Pirko3d249d42011-11-11 22:16:48 +000054 /*
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 Pirko3d249d42011-11-11 22:16:48 +000063 struct rcu_head rcu;
Jiri Pirko5149ee52012-06-19 05:54:05 +000064 long mode_priv[0];
Jiri Pirko3d249d42011-11-11 22:16:48 +000065};
66
67struct 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
79enum team_option_type {
80 TEAM_OPTION_TYPE_U32,
81 TEAM_OPTION_TYPE_STRING,
Jiri Pirko26155982012-04-04 12:16:26 +000082 TEAM_OPTION_TYPE_BINARY,
Jiri Pirko14f066b2012-04-10 05:15:43 +000083 TEAM_OPTION_TYPE_BOOL,
Jiri Pirko3d249d42011-11-11 22:16:48 +000084};
85
Jiri Pirko85d59a82012-06-19 05:54:10 +000086struct team_option_inst_info {
87 u32 array_index;
88 struct team_port *port; /* != NULL if per-port */
89};
90
Jiri Pirko80f7c662012-04-10 05:15:42 +000091struct team_gsetter_ctx {
92 union {
93 u32 u32_val;
94 const char *str_val;
95 struct {
96 const void *ptr;
97 u32 len;
98 } bin_val;
Jiri Pirko14f066b2012-04-10 05:15:43 +000099 bool bool_val;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000100 } data;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000101 struct team_option_inst_info *info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000102};
103
Jiri Pirko3d249d42011-11-11 22:16:48 +0000104struct team_option {
105 struct list_head list;
106 const char *name;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000107 bool per_port;
Jiri Pirkob1303322012-06-19 05:54:08 +0000108 unsigned int array_size; /* != 0 means the option is array */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000109 enum team_option_type type;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000110 int (*init)(struct team *team, struct team_option_inst_info *info);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000111 int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
112 int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000113};
114
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000115extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
116extern void team_options_change_check(struct team *team);
117
Jiri Pirko3d249d42011-11-11 22:16:48 +0000118struct team_mode {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000119 const char *kind;
120 struct module *owner;
121 size_t priv_size;
Jiri Pirko5149ee52012-06-19 05:54:05 +0000122 size_t port_priv_size;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000123 const struct team_mode_ops *ops;
124};
125
126#define TEAM_PORT_HASHBITS 4
127#define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
128
129#define TEAM_MODE_PRIV_LONGS 4
130#define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
131
132struct team {
133 struct net_device *dev; /* associated netdevice */
134 struct team_pcpu_stats __percpu *pcpu_stats;
135
Jiri Pirko61dc3462011-11-16 11:09:08 +0000136 struct mutex lock; /* used for overall locking, e.g. port lists write */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000137
138 /*
Jiri Pirko19a0b582012-04-20 04:42:05 +0000139 * List of enabled ports and their count
Jiri Pirko3d249d42011-11-11 22:16:48 +0000140 */
Jiri Pirko19a0b582012-04-20 04:42:05 +0000141 int en_port_count;
142 struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
143
144 struct list_head port_list; /* list of all ports */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000145
146 struct list_head option_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000147 struct list_head option_inst_list; /* list of option instances */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000148
149 const struct team_mode *mode;
150 struct team_mode_ops ops;
151 long mode_priv[TEAM_MODE_PRIV_LONGS];
152};
153
154static inline struct hlist_head *team_port_index_hash(struct team *team,
155 int port_index)
156{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000157 return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
Jiri Pirko3d249d42011-11-11 22:16:48 +0000158}
159
160static inline struct team_port *team_get_port_by_index(struct team *team,
161 int port_index)
162{
163 struct hlist_node *p;
164 struct team_port *port;
165 struct hlist_head *head = team_port_index_hash(team, port_index);
166
167 hlist_for_each_entry(port, p, head, hlist)
168 if (port->index == port_index)
169 return port;
170 return NULL;
171}
172static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
173 int port_index)
174{
175 struct hlist_node *p;
176 struct team_port *port;
177 struct hlist_head *head = team_port_index_hash(team, port_index);
178
179 hlist_for_each_entry_rcu(port, p, head, hlist)
180 if (port->index == port_index)
181 return port;
182 return NULL;
183}
184
185extern int team_port_set_team_mac(struct team_port *port);
Jiri Pirko358b8382011-11-16 11:09:09 +0000186extern int team_options_register(struct team *team,
187 const struct team_option *option,
188 size_t option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000189extern void team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000190 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000191 size_t option_count);
Jiri Pirko04027882012-06-19 05:54:03 +0000192extern int team_mode_register(const struct team_mode *mode);
193extern void team_mode_unregister(const struct team_mode *mode);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000194
195#endif /* __KERNEL__ */
196
197#define TEAM_STRING_MAX_LEN 32
198
199/**********************************
200 * NETLINK_GENERIC netlink family.
201 **********************************/
202
203enum {
204 TEAM_CMD_NOOP,
205 TEAM_CMD_OPTIONS_SET,
206 TEAM_CMD_OPTIONS_GET,
207 TEAM_CMD_PORT_LIST_GET,
208
209 __TEAM_CMD_MAX,
210 TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
211};
212
213enum {
214 TEAM_ATTR_UNSPEC,
215 TEAM_ATTR_TEAM_IFINDEX, /* u32 */
216 TEAM_ATTR_LIST_OPTION, /* nest */
217 TEAM_ATTR_LIST_PORT, /* nest */
218
219 __TEAM_ATTR_MAX,
220 TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
221};
222
223/* Nested layout of get/set msg:
224 *
225 * [TEAM_ATTR_LIST_OPTION]
226 * [TEAM_ATTR_ITEM_OPTION]
227 * [TEAM_ATTR_OPTION_*], ...
228 * [TEAM_ATTR_ITEM_OPTION]
229 * [TEAM_ATTR_OPTION_*], ...
230 * ...
231 * [TEAM_ATTR_LIST_PORT]
232 * [TEAM_ATTR_ITEM_PORT]
233 * [TEAM_ATTR_PORT_*], ...
234 * [TEAM_ATTR_ITEM_PORT]
235 * [TEAM_ATTR_PORT_*], ...
236 * ...
237 */
238
239enum {
240 TEAM_ATTR_ITEM_OPTION_UNSPEC,
241 TEAM_ATTR_ITEM_OPTION, /* nest */
242
243 __TEAM_ATTR_ITEM_OPTION_MAX,
244 TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
245};
246
247enum {
248 TEAM_ATTR_OPTION_UNSPEC,
249 TEAM_ATTR_OPTION_NAME, /* string */
250 TEAM_ATTR_OPTION_CHANGED, /* flag */
251 TEAM_ATTR_OPTION_TYPE, /* u8 */
252 TEAM_ATTR_OPTION_DATA, /* dynamic */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000253 TEAM_ATTR_OPTION_REMOVED, /* flag */
Jiri Pirko80f7c662012-04-10 05:15:42 +0000254 TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +0000255 TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000256
257 __TEAM_ATTR_OPTION_MAX,
258 TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
259};
260
261enum {
262 TEAM_ATTR_ITEM_PORT_UNSPEC,
263 TEAM_ATTR_ITEM_PORT, /* nest */
264
265 __TEAM_ATTR_ITEM_PORT_MAX,
266 TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
267};
268
269enum {
270 TEAM_ATTR_PORT_UNSPEC,
271 TEAM_ATTR_PORT_IFINDEX, /* u32 */
272 TEAM_ATTR_PORT_CHANGED, /* flag */
273 TEAM_ATTR_PORT_LINKUP, /* flag */
274 TEAM_ATTR_PORT_SPEED, /* u32 */
275 TEAM_ATTR_PORT_DUPLEX, /* u8 */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000276 TEAM_ATTR_PORT_REMOVED, /* flag */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000277
278 __TEAM_ATTR_PORT_MAX,
279 TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
280};
281
282/*
283 * NETLINK_GENERIC related info
284 */
285#define TEAM_GENL_NAME "team"
286#define TEAM_GENL_VERSION 0x1
287#define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
288
289#endif /* _LINUX_IF_TEAM_H_ */