blob: 7fd0cdeb9444a73577057b9c48ee87aca6b4055b [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
Jiri Pirkobd2d0832012-07-17 05:22:36 +000016#include <linux/netpoll.h>
17
Jiri Pirko3d249d42011-11-11 22:16:48 +000018struct team_pcpu_stats {
19 u64 rx_packets;
20 u64 rx_bytes;
21 u64 rx_multicast;
22 u64 tx_packets;
23 u64 tx_bytes;
24 struct u64_stats_sync syncp;
25 u32 rx_dropped;
26 u32 tx_dropped;
27};
28
29struct team;
30
31struct team_port {
32 struct net_device *dev;
Jiri Pirko19a0b582012-04-20 04:42:05 +000033 struct hlist_node hlist; /* node in enabled ports hash list */
Jiri Pirko3d249d42011-11-11 22:16:48 +000034 struct list_head list; /* node in ordinary list */
35 struct team *team;
Jiri Pirko19a0b582012-04-20 04:42:05 +000036 int index; /* index of enabled port. If disabled, it's set to -1 */
Jiri Pirko3d249d42011-11-11 22:16:48 +000037
Jiri Pirko71472ec2012-04-10 05:15:44 +000038 bool linkup; /* either state.linkup or user.linkup */
39
40 struct {
41 bool linkup;
42 u32 speed;
43 u8 duplex;
44 } state;
45
46 /* Values set by userspace */
47 struct {
48 bool linkup;
49 bool linkup_enabled;
50 } user;
51
52 /* Custom gennetlink interface related flags */
53 bool changed;
54 bool removed;
55
Jiri Pirko3d249d42011-11-11 22:16:48 +000056 /*
57 * A place for storing original values of the device before it
58 * become a port.
59 */
60 struct {
61 unsigned char dev_addr[MAX_ADDR_LEN];
62 unsigned int mtu;
63 } orig;
64
Jiri Pirkobd2d0832012-07-17 05:22:36 +000065#ifdef CONFIG_NET_POLL_CONTROLLER
66 struct netpoll *np;
67#endif
68
Jiri Pirko5149ee52012-06-19 05:54:05 +000069 long mode_priv[0];
Jiri Pirko3d249d42011-11-11 22:16:48 +000070};
71
Jiri Pirko68c45042012-07-11 05:34:04 +000072static inline bool team_port_enabled(struct team_port *port)
73{
74 return port->index != -1;
75}
76
77static inline bool team_port_txable(struct team_port *port)
78{
79 return port->linkup && team_port_enabled(port);
80}
Jiri Pirko52a4fd72012-06-26 06:52:46 +000081
Jiri Pirkobd2d0832012-07-17 05:22:36 +000082#ifdef CONFIG_NET_POLL_CONTROLLER
83static inline void team_netpoll_send_skb(struct team_port *port,
84 struct sk_buff *skb)
85{
86 struct netpoll *np = port->np;
87
88 if (np)
89 netpoll_send_skb(np, skb);
90}
91#else
92static inline void team_netpoll_send_skb(struct team_port *port,
93 struct sk_buff *skb)
94{
95}
96#endif
97
98static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
99 struct sk_buff *skb)
100{
101 skb->dev = port->dev;
102 if (unlikely(netpoll_tx_running(port->dev))) {
103 team_netpoll_send_skb(port, skb);
104 return 0;
105 }
106 return dev_queue_xmit(skb);
107}
108
Jiri Pirko3d249d42011-11-11 22:16:48 +0000109struct team_mode_ops {
110 int (*init)(struct team *team);
111 void (*exit)(struct team *team);
112 rx_handler_result_t (*receive)(struct team *team,
113 struct team_port *port,
114 struct sk_buff *skb);
115 bool (*transmit)(struct team *team, struct sk_buff *skb);
116 int (*port_enter)(struct team *team, struct team_port *port);
117 void (*port_leave)(struct team *team, struct team_port *port);
118 void (*port_change_mac)(struct team *team, struct team_port *port);
Jiri Pirko4bccfd12012-06-19 05:54:16 +0000119 void (*port_enabled)(struct team *team, struct team_port *port);
120 void (*port_disabled)(struct team *team, struct team_port *port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000121};
122
123enum team_option_type {
124 TEAM_OPTION_TYPE_U32,
125 TEAM_OPTION_TYPE_STRING,
Jiri Pirko26155982012-04-04 12:16:26 +0000126 TEAM_OPTION_TYPE_BINARY,
Jiri Pirko14f066b2012-04-10 05:15:43 +0000127 TEAM_OPTION_TYPE_BOOL,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000128};
129
Jiri Pirko85d59a82012-06-19 05:54:10 +0000130struct team_option_inst_info {
131 u32 array_index;
132 struct team_port *port; /* != NULL if per-port */
133};
134
Jiri Pirko80f7c662012-04-10 05:15:42 +0000135struct team_gsetter_ctx {
136 union {
137 u32 u32_val;
138 const char *str_val;
139 struct {
140 const void *ptr;
141 u32 len;
142 } bin_val;
Jiri Pirko14f066b2012-04-10 05:15:43 +0000143 bool bool_val;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000144 } data;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000145 struct team_option_inst_info *info;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000146};
147
Jiri Pirko3d249d42011-11-11 22:16:48 +0000148struct team_option {
149 struct list_head list;
150 const char *name;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000151 bool per_port;
Jiri Pirkob1303322012-06-19 05:54:08 +0000152 unsigned int array_size; /* != 0 means the option is array */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000153 enum team_option_type type;
Jiri Pirko85d59a82012-06-19 05:54:10 +0000154 int (*init)(struct team *team, struct team_option_inst_info *info);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000155 int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
156 int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000157};
158
Jiri Pirko0f1aad22012-06-19 05:54:11 +0000159extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
160extern void team_options_change_check(struct team *team);
161
Jiri Pirko3d249d42011-11-11 22:16:48 +0000162struct team_mode {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000163 const char *kind;
164 struct module *owner;
165 size_t priv_size;
Jiri Pirko5149ee52012-06-19 05:54:05 +0000166 size_t port_priv_size;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000167 const struct team_mode_ops *ops;
168};
169
170#define TEAM_PORT_HASHBITS 4
171#define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
172
173#define TEAM_MODE_PRIV_LONGS 4
174#define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
175
176struct team {
177 struct net_device *dev; /* associated netdevice */
178 struct team_pcpu_stats __percpu *pcpu_stats;
179
Jiri Pirko61dc3462011-11-16 11:09:08 +0000180 struct mutex lock; /* used for overall locking, e.g. port lists write */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000181
182 /*
Jiri Pirko19a0b582012-04-20 04:42:05 +0000183 * List of enabled ports and their count
Jiri Pirko3d249d42011-11-11 22:16:48 +0000184 */
Jiri Pirko19a0b582012-04-20 04:42:05 +0000185 int en_port_count;
186 struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
187
188 struct list_head port_list; /* list of all ports */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000189
190 struct list_head option_list;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000191 struct list_head option_inst_list; /* list of option instances */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000192
193 const struct team_mode *mode;
194 struct team_mode_ops ops;
195 long mode_priv[TEAM_MODE_PRIV_LONGS];
196};
197
198static inline struct hlist_head *team_port_index_hash(struct team *team,
199 int port_index)
200{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000201 return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
Jiri Pirko3d249d42011-11-11 22:16:48 +0000202}
203
204static inline struct team_port *team_get_port_by_index(struct team *team,
205 int port_index)
206{
207 struct hlist_node *p;
208 struct team_port *port;
209 struct hlist_head *head = team_port_index_hash(team, port_index);
210
211 hlist_for_each_entry(port, p, head, hlist)
212 if (port->index == port_index)
213 return port;
214 return NULL;
215}
216static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
217 int port_index)
218{
219 struct hlist_node *p;
220 struct team_port *port;
221 struct hlist_head *head = team_port_index_hash(team, port_index);
222
223 hlist_for_each_entry_rcu(port, p, head, hlist)
224 if (port->index == port_index)
225 return port;
226 return NULL;
227}
228
229extern int team_port_set_team_mac(struct team_port *port);
Jiri Pirko358b8382011-11-16 11:09:09 +0000230extern int team_options_register(struct team *team,
231 const struct team_option *option,
232 size_t option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000233extern void team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000234 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000235 size_t option_count);
Jiri Pirko04027882012-06-19 05:54:03 +0000236extern int team_mode_register(const struct team_mode *mode);
237extern void team_mode_unregister(const struct team_mode *mode);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000238
239#endif /* __KERNEL__ */
240
241#define TEAM_STRING_MAX_LEN 32
242
243/**********************************
244 * NETLINK_GENERIC netlink family.
245 **********************************/
246
247enum {
248 TEAM_CMD_NOOP,
249 TEAM_CMD_OPTIONS_SET,
250 TEAM_CMD_OPTIONS_GET,
251 TEAM_CMD_PORT_LIST_GET,
252
253 __TEAM_CMD_MAX,
254 TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1),
255};
256
257enum {
258 TEAM_ATTR_UNSPEC,
259 TEAM_ATTR_TEAM_IFINDEX, /* u32 */
260 TEAM_ATTR_LIST_OPTION, /* nest */
261 TEAM_ATTR_LIST_PORT, /* nest */
262
263 __TEAM_ATTR_MAX,
264 TEAM_ATTR_MAX = __TEAM_ATTR_MAX - 1,
265};
266
267/* Nested layout of get/set msg:
268 *
269 * [TEAM_ATTR_LIST_OPTION]
270 * [TEAM_ATTR_ITEM_OPTION]
271 * [TEAM_ATTR_OPTION_*], ...
272 * [TEAM_ATTR_ITEM_OPTION]
273 * [TEAM_ATTR_OPTION_*], ...
274 * ...
275 * [TEAM_ATTR_LIST_PORT]
276 * [TEAM_ATTR_ITEM_PORT]
277 * [TEAM_ATTR_PORT_*], ...
278 * [TEAM_ATTR_ITEM_PORT]
279 * [TEAM_ATTR_PORT_*], ...
280 * ...
281 */
282
283enum {
284 TEAM_ATTR_ITEM_OPTION_UNSPEC,
285 TEAM_ATTR_ITEM_OPTION, /* nest */
286
287 __TEAM_ATTR_ITEM_OPTION_MAX,
288 TEAM_ATTR_ITEM_OPTION_MAX = __TEAM_ATTR_ITEM_OPTION_MAX - 1,
289};
290
291enum {
292 TEAM_ATTR_OPTION_UNSPEC,
293 TEAM_ATTR_OPTION_NAME, /* string */
294 TEAM_ATTR_OPTION_CHANGED, /* flag */
295 TEAM_ATTR_OPTION_TYPE, /* u8 */
296 TEAM_ATTR_OPTION_DATA, /* dynamic */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000297 TEAM_ATTR_OPTION_REMOVED, /* flag */
Jiri Pirko80f7c662012-04-10 05:15:42 +0000298 TEAM_ATTR_OPTION_PORT_IFINDEX, /* u32 */ /* for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +0000299 TEAM_ATTR_OPTION_ARRAY_INDEX, /* u32 */ /* for array options */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000300
301 __TEAM_ATTR_OPTION_MAX,
302 TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
303};
304
305enum {
306 TEAM_ATTR_ITEM_PORT_UNSPEC,
307 TEAM_ATTR_ITEM_PORT, /* nest */
308
309 __TEAM_ATTR_ITEM_PORT_MAX,
310 TEAM_ATTR_ITEM_PORT_MAX = __TEAM_ATTR_ITEM_PORT_MAX - 1,
311};
312
313enum {
314 TEAM_ATTR_PORT_UNSPEC,
315 TEAM_ATTR_PORT_IFINDEX, /* u32 */
316 TEAM_ATTR_PORT_CHANGED, /* flag */
317 TEAM_ATTR_PORT_LINKUP, /* flag */
318 TEAM_ATTR_PORT_SPEED, /* u32 */
319 TEAM_ATTR_PORT_DUPLEX, /* u8 */
Jiri Pirkob82b9182012-01-24 05:16:00 +0000320 TEAM_ATTR_PORT_REMOVED, /* flag */
Jiri Pirko3d249d42011-11-11 22:16:48 +0000321
322 __TEAM_ATTR_PORT_MAX,
323 TEAM_ATTR_PORT_MAX = __TEAM_ATTR_PORT_MAX - 1,
324};
325
326/*
327 * NETLINK_GENERIC related info
328 */
329#define TEAM_GENL_NAME "team"
330#define TEAM_GENL_VERSION 0x1
331#define TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME "change_event"
332
333#endif /* _LINUX_IF_TEAM_H_ */