blob: c1938869191f9fd1e91aef51cdc5b142adb7ce28 [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);
Jiri Pirko4bccfd12012-06-19 05:54:16 +000077 void (*port_enabled)(struct team *team, struct team_port *port);
78 void (*port_disabled)(struct team *team, struct team_port *port);
Jiri Pirko3d249d42011-11-11 22:16:48 +000079};
80
81enum team_option_type {
82 TEAM_OPTION_TYPE_U32,
83 TEAM_OPTION_TYPE_STRING,
Jiri Pirko26155982012-04-04 12:16:26 +000084 TEAM_OPTION_TYPE_BINARY,
Jiri Pirko14f066b2012-04-10 05:15:43 +000085 TEAM_OPTION_TYPE_BOOL,
Jiri Pirko3d249d42011-11-11 22:16:48 +000086};
87
Jiri Pirko85d59a82012-06-19 05:54:10 +000088struct team_option_inst_info {
89 u32 array_index;
90 struct team_port *port; /* != NULL if per-port */
91};
92
Jiri Pirko80f7c662012-04-10 05:15:42 +000093struct team_gsetter_ctx {
94 union {
95 u32 u32_val;
96 const char *str_val;
97 struct {
98 const void *ptr;
99 u32 len;
100 } bin_val;
Jiri Pirko14f066b2012-04-10 05:15:43 +0000101 bool bool_val;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000102 } data;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000103 struct team_option_inst_info *info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000104};
105
Jiri Pirko3d249d42011-11-11 22:16:48 +0000106struct team_option {
107 struct list_head list;
108 const char *name;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000109 bool per_port;
Jiri Pirkob1303322012-06-19 05:54:08 +0000110 unsigned int array_size; /* != 0 means the option is array */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000111 enum team_option_type type;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000112 int (*init)(struct team *team, struct team_option_inst_info *info);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000113 int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
114 int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000115};
116
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000117extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
118extern void team_options_change_check(struct team *team);
119
Jiri Pirko3d249d42011-11-11 22:16:48 +0000120struct team_mode {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000121 const char *kind;
122 struct module *owner;
123 size_t priv_size;
Jiri Pirko5149ee52012-06-19 05:54:05 +0000124 size_t port_priv_size;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000125 const struct team_mode_ops *ops;
126};
127
128#define TEAM_PORT_HASHBITS 4
129#define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
130
131#define TEAM_MODE_PRIV_LONGS 4
132#define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
133
134struct team {
135 struct net_device *dev; /* associated netdevice */
136 struct team_pcpu_stats __percpu *pcpu_stats;
137
Jiri Pirko61dc3462011-11-16 11:09:08 +0000138 struct mutex lock; /* used for overall locking, e.g. port lists write */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000139
140 /*
Jiri Pirko19a0b582012-04-20 04:42:05 +0000141 * List of enabled ports and their count
Jiri Pirko3d249d42011-11-11 22:16:48 +0000142 */
Jiri Pirko19a0b582012-04-20 04:42:05 +0000143 int en_port_count;
144 struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
145
146 struct list_head port_list; /* list of all ports */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000147
148 struct list_head option_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000149 struct list_head option_inst_list; /* list of option instances */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000150
151 const struct team_mode *mode;
152 struct team_mode_ops ops;
153 long mode_priv[TEAM_MODE_PRIV_LONGS];
154};
155
156static inline struct hlist_head *team_port_index_hash(struct team *team,
157 int port_index)
158{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000159 return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
Jiri Pirko3d249d42011-11-11 22:16:48 +0000160}
161
162static inline struct team_port *team_get_port_by_index(struct team *team,
163 int port_index)
164{
165 struct hlist_node *p;
166 struct team_port *port;
167 struct hlist_head *head = team_port_index_hash(team, port_index);
168
169 hlist_for_each_entry(port, p, head, hlist)
170 if (port->index == port_index)
171 return port;
172 return NULL;
173}
174static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
175 int port_index)
176{
177 struct hlist_node *p;
178 struct team_port *port;
179 struct hlist_head *head = team_port_index_hash(team, port_index);
180
181 hlist_for_each_entry_rcu(port, p, head, hlist)
182 if (port->index == port_index)
183 return port;
184 return NULL;
185}
186
187extern int team_port_set_team_mac(struct team_port *port);
Jiri Pirko358b8382011-11-16 11:09:09 +0000188extern int team_options_register(struct team *team,
189 const struct team_option *option,
190 size_t option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000191extern void team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000192 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000193 size_t option_count);
Jiri Pirko04027882012-06-19 05:54:03 +0000194extern int team_mode_register(const struct team_mode *mode);
195extern void team_mode_unregister(const struct team_mode *mode);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000196
197#endif /* __KERNEL__ */
198
199#define TEAM_STRING_MAX_LEN 32
200
201/**********************************
202 * NETLINK_GENERIC netlink family.
203 **********************************/
204
205enum {
206 TEAM_CMD_NOOP,
207 TEAM_CMD_OPTIONS_SET,
208 TEAM_CMD_OPTIONS_GET,
209 TEAM_CMD_PORT_LIST_GET,
210
211 __TEAM_CMD_MAX,
212 TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
213};
214
215enum {
216 TEAM_ATTR_UNSPEC,
217 TEAM_ATTR_TEAM_IFINDEX, /* u32 */
218 TEAM_ATTR_LIST_OPTION, /* nest */
219 TEAM_ATTR_LIST_PORT, /* nest */
220
221 __TEAM_ATTR_MAX,
222 TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
223};
224
225/* Nested layout of get/set msg:
226 *
227 * [TEAM_ATTR_LIST_OPTION]
228 * [TEAM_ATTR_ITEM_OPTION]
229 * [TEAM_ATTR_OPTION_*], ...
230 * [TEAM_ATTR_ITEM_OPTION]
231 * [TEAM_ATTR_OPTION_*], ...
232 * ...
233 * [TEAM_ATTR_LIST_PORT]
234 * [TEAM_ATTR_ITEM_PORT]
235 * [TEAM_ATTR_PORT_*], ...
236 * [TEAM_ATTR_ITEM_PORT]
237 * [TEAM_ATTR_PORT_*], ...
238 * ...
239 */
240
241enum {
242 TEAM_ATTR_ITEM_OPTION_UNSPEC,
243 TEAM_ATTR_ITEM_OPTION, /* nest */
244
245 __TEAM_ATTR_ITEM_OPTION_MAX,
246 TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
247};
248
249enum {
250 TEAM_ATTR_OPTION_UNSPEC,
251 TEAM_ATTR_OPTION_NAME, /* string */
252 TEAM_ATTR_OPTION_CHANGED, /* flag */
253 TEAM_ATTR_OPTION_TYPE, /* u8 */
254 TEAM_ATTR_OPTION_DATA, /* dynamic */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000255 TEAM_ATTR_OPTION_REMOVED, /* flag */
Jiri Pirko80f7c662012-04-10 05:15:42 +0000256 TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +0000257 TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000258
259 __TEAM_ATTR_OPTION_MAX,
260 TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
261};
262
263enum {
264 TEAM_ATTR_ITEM_PORT_UNSPEC,
265 TEAM_ATTR_ITEM_PORT, /* nest */
266
267 __TEAM_ATTR_ITEM_PORT_MAX,
268 TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
269};
270
271enum {
272 TEAM_ATTR_PORT_UNSPEC,
273 TEAM_ATTR_PORT_IFINDEX, /* u32 */
274 TEAM_ATTR_PORT_CHANGED, /* flag */
275 TEAM_ATTR_PORT_LINKUP, /* flag */
276 TEAM_ATTR_PORT_SPEED, /* u32 */
277 TEAM_ATTR_PORT_DUPLEX, /* u8 */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000278 TEAM_ATTR_PORT_REMOVED, /* flag */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000279
280 __TEAM_ATTR_PORT_MAX,
281 TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
282};
283
284/*
285 * NETLINK_GENERIC related info
286 */
287#define TEAM_GENL_NAME "team"
288#define TEAM_GENL_VERSION 0x1
289#define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
290
291#endif /* _LINUX_IF_TEAM_H_ */