team: add netpoll support
[cascardo/linux.git] / include / linux / if_team.h
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 #include <linux/netpoll.h>
17
18 struct 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
29 struct team;
30
31 struct team_port {
32         struct net_device *dev;
33         struct hlist_node hlist; /* node in enabled ports hash list */
34         struct list_head list; /* node in ordinary list */
35         struct team *team;
36         int index; /* index of enabled port. If disabled, it's set to -1 */
37
38         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
56         /*
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
65 #ifdef CONFIG_NET_POLL_CONTROLLER
66         struct netpoll *np;
67 #endif
68
69         long mode_priv[0];
70 };
71
72 static inline bool team_port_enabled(struct team_port *port)
73 {
74         return port->index != -1;
75 }
76
77 static inline bool team_port_txable(struct team_port *port)
78 {
79         return port->linkup && team_port_enabled(port);
80 }
81
82 #ifdef CONFIG_NET_POLL_CONTROLLER
83 static 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
92 static inline void team_netpoll_send_skb(struct team_port *port,
93                                          struct sk_buff *skb)
94 {
95 }
96 #endif
97
98 static 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
109 struct 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);
119         void (*port_enabled)(struct team *team, struct team_port *port);
120         void (*port_disabled)(struct team *team, struct team_port *port);
121 };
122
123 enum team_option_type {
124         TEAM_OPTION_TYPE_U32,
125         TEAM_OPTION_TYPE_STRING,
126         TEAM_OPTION_TYPE_BINARY,
127         TEAM_OPTION_TYPE_BOOL,
128 };
129
130 struct team_option_inst_info {
131         u32 array_index;
132         struct team_port *port; /* != NULL if per-port */
133 };
134
135 struct 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;
143                 bool bool_val;
144         } data;
145         struct team_option_inst_info *info;
146 };
147
148 struct team_option {
149         struct list_head list;
150         const char *name;
151         bool per_port;
152         unsigned int array_size; /* != 0 means the option is array */
153         enum team_option_type type;
154         int (*init)(struct team *team, struct team_option_inst_info *info);
155         int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
156         int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
157 };
158
159 extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
160 extern void team_options_change_check(struct team *team);
161
162 struct team_mode {
163         const char *kind;
164         struct module *owner;
165         size_t priv_size;
166         size_t port_priv_size;
167         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
176 struct team {
177         struct net_device *dev; /* associated netdevice */
178         struct team_pcpu_stats __percpu *pcpu_stats;
179
180         struct mutex lock; /* used for overall locking, e.g. port lists write */
181
182         /*
183          * List of enabled ports and their count
184          */
185         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 */
189
190         struct list_head option_list;
191         struct list_head option_inst_list; /* list of option instances */
192
193         const struct team_mode *mode;
194         struct team_mode_ops ops;
195         long mode_priv[TEAM_MODE_PRIV_LONGS];
196 };
197
198 static inline struct hlist_head *team_port_index_hash(struct team *team,
199                                                       int port_index)
200 {
201         return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
202 }
203
204 static 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 }
216 static 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
229 extern int team_port_set_team_mac(struct team_port *port);
230 extern int team_options_register(struct team *team,
231                                  const struct team_option *option,
232                                  size_t option_count);
233 extern void team_options_unregister(struct team *team,
234                                     const struct team_option *option,
235                                     size_t option_count);
236 extern int team_mode_register(const struct team_mode *mode);
237 extern void team_mode_unregister(const struct team_mode *mode);
238
239 #endif /* __KERNEL__ */
240
241 #define TEAM_STRING_MAX_LEN 32
242
243 /**********************************
244  * NETLINK_GENERIC netlink family.
245  **********************************/
246
247 enum {
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
257 enum {
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
283 enum {
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
291 enum {
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 */
297         TEAM_ATTR_OPTION_REMOVED,       /* flag */
298         TEAM_ATTR_OPTION_PORT_IFINDEX,  /* u32 */ /* for per-port options */
299         TEAM_ATTR_OPTION_ARRAY_INDEX,   /* u32 */ /* for array options */
300
301         __TEAM_ATTR_OPTION_MAX,
302         TEAM_ATTR_OPTION_MAX = __TEAM_ATTR_OPTION_MAX - 1,
303 };
304
305 enum {
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
313 enum {
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 */
320         TEAM_ATTR_PORT_REMOVED,         /* flag */
321
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_ */