blob: 32cb290fb8008377c42a4dd8dde6874cd2d3fcac [file] [log] [blame]
Jiri Pirko3d249d42011-11-11 22:16:48 +00001/*
2 * net/drivers/team/team.c - Network team device driver
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#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/rcupdate.h>
17#include <linux/errno.h>
18#include <linux/ctype.h>
19#include <linux/notifier.h>
20#include <linux/netdevice.h>
Jiri Pirko87002b02011-12-08 04:11:17 +000021#include <linux/if_vlan.h>
Jiri Pirko3d249d42011-11-11 22:16:48 +000022#include <linux/if_arp.h>
23#include <linux/socket.h>
24#include <linux/etherdevice.h>
25#include <linux/rtnetlink.h>
26#include <net/rtnetlink.h>
27#include <net/genetlink.h>
28#include <net/netlink.h>
29#include <linux/if_team.h>
30
31#define DRV_NAME "team"
32
33
34/**********
35 * Helpers
36 **********/
37
38#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
39
40static struct team_port *team_port_get_rcu(const struct net_device *dev)
41{
42 struct team_port *port = rcu_dereference(dev->rx_handler_data);
43
44 return team_port_exists(dev) ? port : NULL;
45}
46
47static struct team_port *team_port_get_rtnl(const struct net_device *dev)
48{
49 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
50
51 return team_port_exists(dev) ? port : NULL;
52}
53
54/*
55 * Since the ability to change mac address for open port device is tested in
56 * team_port_add, this function can be called without control of return value
57 */
58static int __set_port_mac(struct net_device *port_dev,
59 const unsigned char *dev_addr)
60{
61 struct sockaddr addr;
62
63 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
64 addr.sa_family = ARPHRD_ETHER;
65 return dev_set_mac_address(port_dev, &addr);
66}
67
Jiri Pirkocade4552012-04-10 05:15:46 +000068static int team_port_set_orig_mac(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +000069{
70 return __set_port_mac(port->dev, port->orig.dev_addr);
71}
72
73int team_port_set_team_mac(struct team_port *port)
74{
75 return __set_port_mac(port->dev, port->team->dev->dev_addr);
76}
77EXPORT_SYMBOL(team_port_set_team_mac);
78
Jiri Pirko71472ec2012-04-10 05:15:44 +000079static void team_refresh_port_linkup(struct team_port *port)
80{
81 port->linkup = port->user.linkup_enabled ? port->user.linkup :
82 port->state.linkup;
83}
Jiri Pirko3d249d42011-11-11 22:16:48 +000084
85/*******************
86 * Options handling
87 *******************/
88
Jiri Pirko80f7c662012-04-10 05:15:42 +000089struct team_option_inst { /* One for each option instance */
90 struct list_head list;
91 struct team_option *option;
92 struct team_port *port; /* != NULL if per-port */
Jiri Pirkob1303322012-06-19 05:54:08 +000093 u32 array_index;
Jiri Pirko80f7c662012-04-10 05:15:42 +000094 bool changed;
95 bool removed;
96};
97
98static struct team_option *__team_find_option(struct team *team,
99 const char *opt_name)
Jiri Pirko358b8382011-11-16 11:09:09 +0000100{
101 struct team_option *option;
102
103 list_for_each_entry(option, &team->option_list, list) {
104 if (strcmp(option->name, opt_name) == 0)
105 return option;
106 }
107 return NULL;
108}
109
Jiri Pirko80f7c662012-04-10 05:15:42 +0000110static void __team_option_inst_del(struct team_option_inst *opt_inst)
111{
112 list_del(&opt_inst->list);
113 kfree(opt_inst);
114}
115
116static void __team_option_inst_del_option(struct team *team,
117 struct team_option *option)
118{
119 struct team_option_inst *opt_inst, *tmp;
120
121 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
122 if (opt_inst->option == option)
123 __team_option_inst_del(opt_inst);
124 }
125}
126
Jiri Pirkob1303322012-06-19 05:54:08 +0000127static int __team_option_inst_add(struct team *team, struct team_option *option,
128 struct team_port *port)
129{
130 struct team_option_inst *opt_inst;
131 unsigned int array_size;
132 unsigned int i;
133
134 array_size = option->array_size;
135 if (!array_size)
136 array_size = 1; /* No array but still need one instance */
137
138 for (i = 0; i < array_size; i++) {
139 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
140 if (!opt_inst)
141 return -ENOMEM;
142 opt_inst->option = option;
143 opt_inst->port = port;
144 opt_inst->array_index = i;
145 opt_inst->changed = true;
146 opt_inst->removed = false;
147 list_add_tail(&opt_inst->list, &team->option_inst_list);
148 }
149 return 0;
150}
151
Jiri Pirko80f7c662012-04-10 05:15:42 +0000152static int __team_option_inst_add_option(struct team *team,
153 struct team_option *option)
154{
155 struct team_port *port;
156 int err;
157
Jiri Pirkob1303322012-06-19 05:54:08 +0000158 if (!option->per_port) {
159 err = __team_option_inst_add(team, option, 0);
160 if (err)
161 goto inst_del_option;
162 }
Jiri Pirko80f7c662012-04-10 05:15:42 +0000163
164 list_for_each_entry(port, &team->port_list, list) {
165 err = __team_option_inst_add(team, option, port);
166 if (err)
167 goto inst_del_option;
168 }
169 return 0;
170
171inst_del_option:
172 __team_option_inst_del_option(team, option);
173 return err;
174}
175
176static void __team_option_inst_mark_removed_option(struct team *team,
177 struct team_option *option)
178{
179 struct team_option_inst *opt_inst;
180
181 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
182 if (opt_inst->option == option) {
183 opt_inst->changed = true;
184 opt_inst->removed = true;
185 }
186 }
187}
188
189static void __team_option_inst_del_port(struct team *team,
190 struct team_port *port)
191{
192 struct team_option_inst *opt_inst, *tmp;
193
194 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
195 if (opt_inst->option->per_port &&
196 opt_inst->port == port)
197 __team_option_inst_del(opt_inst);
198 }
199}
200
201static int __team_option_inst_add_port(struct team *team,
202 struct team_port *port)
203{
204 struct team_option *option;
205 int err;
206
207 list_for_each_entry(option, &team->option_list, list) {
208 if (!option->per_port)
209 continue;
210 err = __team_option_inst_add(team, option, port);
211 if (err)
212 goto inst_del_port;
213 }
214 return 0;
215
216inst_del_port:
217 __team_option_inst_del_port(team, port);
218 return err;
219}
220
221static void __team_option_inst_mark_removed_port(struct team *team,
222 struct team_port *port)
223{
224 struct team_option_inst *opt_inst;
225
226 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
227 if (opt_inst->port == port) {
228 opt_inst->changed = true;
229 opt_inst->removed = true;
230 }
231 }
232}
233
234static int __team_options_register(struct team *team,
235 const struct team_option *option,
236 size_t option_count)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000237{
238 int i;
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000239 struct team_option **dst_opts;
Jiri Pirko358b8382011-11-16 11:09:09 +0000240 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000241
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000242 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
243 GFP_KERNEL);
244 if (!dst_opts)
245 return -ENOMEM;
Jiri Pirko358b8382011-11-16 11:09:09 +0000246 for (i = 0; i < option_count; i++, option++) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000247 if (__team_find_option(team, option->name)) {
248 err = -EEXIST;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000249 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000250 }
Jiri Pirkof8a15af2011-11-17 06:32:37 +0000251 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
252 if (!dst_opts[i]) {
Jiri Pirko358b8382011-11-16 11:09:09 +0000253 err = -ENOMEM;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000254 goto alloc_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000255 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000256 }
257
Jiri Pirkob82b9182012-01-24 05:16:00 +0000258 for (i = 0; i < option_count; i++) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000259 err = __team_option_inst_add_option(team, dst_opts[i]);
260 if (err)
261 goto inst_rollback;
Jiri Pirko358b8382011-11-16 11:09:09 +0000262 list_add_tail(&dst_opts[i]->list, &team->option_list);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000263 }
Jiri Pirko358b8382011-11-16 11:09:09 +0000264
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000265 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000266 return 0;
267
Jiri Pirko80f7c662012-04-10 05:15:42 +0000268inst_rollback:
269 for (i--; i >= 0; i--)
270 __team_option_inst_del_option(team, dst_opts[i]);
271
272 i = option_count - 1;
273alloc_rollback:
274 for (i--; i >= 0; i--)
Jiri Pirko358b8382011-11-16 11:09:09 +0000275 kfree(dst_opts[i]);
276
Jiri Pirko2bba19f2011-11-17 04:16:05 +0000277 kfree(dst_opts);
Jiri Pirko358b8382011-11-16 11:09:09 +0000278 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000279}
Jiri Pirko358b8382011-11-16 11:09:09 +0000280
Jiri Pirkob82b9182012-01-24 05:16:00 +0000281static void __team_options_mark_removed(struct team *team,
282 const struct team_option *option,
283 size_t option_count)
284{
285 int i;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000286
Jiri Pirkob82b9182012-01-24 05:16:00 +0000287 for (i = 0; i < option_count; i++, option++) {
288 struct team_option *del_opt;
289
290 del_opt = __team_find_option(team, option->name);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000291 if (del_opt)
292 __team_option_inst_mark_removed_option(team, del_opt);
Jiri Pirkob82b9182012-01-24 05:16:00 +0000293 }
294}
Jiri Pirko3d249d42011-11-11 22:16:48 +0000295
296static void __team_options_unregister(struct team *team,
Jiri Pirko358b8382011-11-16 11:09:09 +0000297 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000298 size_t option_count)
299{
300 int i;
301
Jiri Pirko358b8382011-11-16 11:09:09 +0000302 for (i = 0; i < option_count; i++, option++) {
303 struct team_option *del_opt;
304
305 del_opt = __team_find_option(team, option->name);
306 if (del_opt) {
Jiri Pirko80f7c662012-04-10 05:15:42 +0000307 __team_option_inst_del_option(team, del_opt);
Jiri Pirko358b8382011-11-16 11:09:09 +0000308 list_del(&del_opt->list);
309 kfree(del_opt);
310 }
311 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000312}
313
Jiri Pirkob82b9182012-01-24 05:16:00 +0000314static void __team_options_change_check(struct team *team);
315
316int team_options_register(struct team *team,
317 const struct team_option *option,
318 size_t option_count)
319{
320 int err;
321
322 err = __team_options_register(team, option, option_count);
323 if (err)
324 return err;
325 __team_options_change_check(team);
326 return 0;
327}
328EXPORT_SYMBOL(team_options_register);
329
Jiri Pirko358b8382011-11-16 11:09:09 +0000330void team_options_unregister(struct team *team,
331 const struct team_option *option,
Jiri Pirko3d249d42011-11-11 22:16:48 +0000332 size_t option_count)
333{
Jiri Pirkob82b9182012-01-24 05:16:00 +0000334 __team_options_mark_removed(team, option, option_count);
335 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000336 __team_options_unregister(team, option, option_count);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000337}
338EXPORT_SYMBOL(team_options_unregister);
339
Jiri Pirko80f7c662012-04-10 05:15:42 +0000340static int team_option_port_add(struct team *team, struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000341{
342 int err;
343
Jiri Pirko80f7c662012-04-10 05:15:42 +0000344 err = __team_option_inst_add_port(team, port);
345 if (err)
346 return err;
347 __team_options_change_check(team);
348 return 0;
349}
350
351static void team_option_port_del(struct team *team, struct team_port *port)
352{
353 __team_option_inst_mark_removed_port(team, port);
354 __team_options_change_check(team);
355 __team_option_inst_del_port(team, port);
356}
357
358static int team_option_get(struct team *team,
359 struct team_option_inst *opt_inst,
360 struct team_gsetter_ctx *ctx)
361{
Jiri Pirkof82b9592012-06-19 05:54:07 +0000362 if (!opt_inst->option->getter)
363 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000364 return opt_inst->option->getter(team, ctx);
365}
366
367static int team_option_set(struct team *team,
368 struct team_option_inst *opt_inst,
369 struct team_gsetter_ctx *ctx)
370{
371 int err;
372
Jiri Pirkof82b9592012-06-19 05:54:07 +0000373 if (!opt_inst->option->setter)
374 return -EOPNOTSUPP;
Jiri Pirko80f7c662012-04-10 05:15:42 +0000375 err = opt_inst->option->setter(team, ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000376 if (err)
377 return err;
378
Jiri Pirko80f7c662012-04-10 05:15:42 +0000379 opt_inst->changed = true;
Jiri Pirkob82b9182012-01-24 05:16:00 +0000380 __team_options_change_check(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000381 return err;
382}
383
384/****************
385 * Mode handling
386 ****************/
387
388static LIST_HEAD(mode_list);
389static DEFINE_SPINLOCK(mode_list_lock);
390
Jiri Pirko04027882012-06-19 05:54:03 +0000391struct team_mode_item {
392 struct list_head list;
393 const struct team_mode *mode;
394};
Jiri Pirko3d249d42011-11-11 22:16:48 +0000395
Jiri Pirko04027882012-06-19 05:54:03 +0000396static struct team_mode_item *__find_mode(const char *kind)
397{
398 struct team_mode_item *mitem;
399
400 list_for_each_entry(mitem, &mode_list, list) {
401 if (strcmp(mitem->mode->kind, kind) == 0)
402 return mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000403 }
404 return NULL;
405}
406
407static bool is_good_mode_name(const char *name)
408{
409 while (*name != '\0') {
410 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
411 return false;
412 name++;
413 }
414 return true;
415}
416
Jiri Pirko04027882012-06-19 05:54:03 +0000417int team_mode_register(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000418{
419 int err = 0;
Jiri Pirko04027882012-06-19 05:54:03 +0000420 struct team_mode_item *mitem;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000421
422 if (!is_good_mode_name(mode->kind) ||
423 mode->priv_size > TEAM_MODE_PRIV_SIZE)
424 return -EINVAL;
Jiri Pirko04027882012-06-19 05:54:03 +0000425
426 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
427 if (!mitem)
428 return -ENOMEM;
429
Jiri Pirko3d249d42011-11-11 22:16:48 +0000430 spin_lock(&mode_list_lock);
431 if (__find_mode(mode->kind)) {
432 err = -EEXIST;
Jiri Pirko04027882012-06-19 05:54:03 +0000433 kfree(mitem);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000434 goto unlock;
435 }
Jiri Pirko04027882012-06-19 05:54:03 +0000436 mitem->mode = mode;
437 list_add_tail(&mitem->list, &mode_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000438unlock:
439 spin_unlock(&mode_list_lock);
440 return err;
441}
442EXPORT_SYMBOL(team_mode_register);
443
Jiri Pirko04027882012-06-19 05:54:03 +0000444void team_mode_unregister(const struct team_mode *mode)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000445{
Jiri Pirko04027882012-06-19 05:54:03 +0000446 struct team_mode_item *mitem;
447
Jiri Pirko3d249d42011-11-11 22:16:48 +0000448 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000449 mitem = __find_mode(mode->kind);
450 if (mitem) {
451 list_del_init(&mitem->list);
452 kfree(mitem);
453 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000454 spin_unlock(&mode_list_lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000455}
456EXPORT_SYMBOL(team_mode_unregister);
457
Jiri Pirko04027882012-06-19 05:54:03 +0000458static const struct team_mode *team_mode_get(const char *kind)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000459{
Jiri Pirko04027882012-06-19 05:54:03 +0000460 struct team_mode_item *mitem;
461 const struct team_mode *mode = NULL;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000462
463 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000464 mitem = __find_mode(kind);
465 if (!mitem) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000466 spin_unlock(&mode_list_lock);
467 request_module("team-mode-%s", kind);
468 spin_lock(&mode_list_lock);
Jiri Pirko04027882012-06-19 05:54:03 +0000469 mitem = __find_mode(kind);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000470 }
Jiri Pirko04027882012-06-19 05:54:03 +0000471 if (mitem) {
472 mode = mitem->mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000473 if (!try_module_get(mode->owner))
474 mode = NULL;
Jiri Pirko04027882012-06-19 05:54:03 +0000475 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000476
477 spin_unlock(&mode_list_lock);
478 return mode;
479}
480
481static void team_mode_put(const struct team_mode *mode)
482{
483 module_put(mode->owner);
484}
485
486static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
487{
488 dev_kfree_skb_any(skb);
489 return false;
490}
491
492rx_handler_result_t team_dummy_receive(struct team *team,
493 struct team_port *port,
494 struct sk_buff *skb)
495{
496 return RX_HANDLER_ANOTHER;
497}
498
Jiri Pirkod299cd52012-06-19 05:54:04 +0000499static const struct team_mode __team_no_mode = {
500 .kind = "*NOMODE*",
501};
502
503static bool team_is_mode_set(struct team *team)
504{
505 return team->mode != &__team_no_mode;
506}
507
508static void team_set_no_mode(struct team *team)
509{
510 team->mode = &__team_no_mode;
511}
512
Jiri Pirko3d249d42011-11-11 22:16:48 +0000513static void team_adjust_ops(struct team *team)
514{
515 /*
516 * To avoid checks in rx/tx skb paths, ensure here that non-null and
517 * correct ops are always set.
518 */
519
520 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000521 !team_is_mode_set(team) || !team->mode->ops->transmit)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000522 team->ops.transmit = team_dummy_transmit;
523 else
524 team->ops.transmit = team->mode->ops->transmit;
525
526 if (list_empty(&team->port_list) ||
Jiri Pirkod299cd52012-06-19 05:54:04 +0000527 !team_is_mode_set(team) || !team->mode->ops->receive)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000528 team->ops.receive = team_dummy_receive;
529 else
530 team->ops.receive = team->mode->ops->receive;
531}
532
533/*
534 * We can benefit from the fact that it's ensured no port is present
535 * at the time of mode change. Therefore no packets are in fly so there's no
536 * need to set mode operations in any special way.
537 */
538static int __team_change_mode(struct team *team,
539 const struct team_mode *new_mode)
540{
541 /* Check if mode was previously set and do cleanup if so */
Jiri Pirkod299cd52012-06-19 05:54:04 +0000542 if (team_is_mode_set(team)) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000543 void (*exit_op)(struct team *team) = team->ops.exit;
544
545 /* Clear ops area so no callback is called any longer */
546 memset(&team->ops, 0, sizeof(struct team_mode_ops));
547 team_adjust_ops(team);
548
549 if (exit_op)
550 exit_op(team);
551 team_mode_put(team->mode);
Jiri Pirkod299cd52012-06-19 05:54:04 +0000552 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000553 /* zero private data area */
554 memset(&team->mode_priv, 0,
555 sizeof(struct team) - offsetof(struct team, mode_priv));
556 }
557
558 if (!new_mode)
559 return 0;
560
561 if (new_mode->ops->init) {
562 int err;
563
564 err = new_mode->ops->init(team);
565 if (err)
566 return err;
567 }
568
569 team->mode = new_mode;
570 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
571 team_adjust_ops(team);
572
573 return 0;
574}
575
576static int team_change_mode(struct team *team, const char *kind)
577{
Jiri Pirko04027882012-06-19 05:54:03 +0000578 const struct team_mode *new_mode;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000579 struct net_device *dev = team->dev;
580 int err;
581
582 if (!list_empty(&team->port_list)) {
583 netdev_err(dev, "No ports can be present during mode change\n");
584 return -EBUSY;
585 }
586
Jiri Pirkod299cd52012-06-19 05:54:04 +0000587 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000588 netdev_err(dev, "Unable to change to the same mode the team is in\n");
589 return -EINVAL;
590 }
591
592 new_mode = team_mode_get(kind);
593 if (!new_mode) {
594 netdev_err(dev, "Mode \"%s\" not found\n", kind);
595 return -EINVAL;
596 }
597
598 err = __team_change_mode(team, new_mode);
599 if (err) {
600 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
601 team_mode_put(new_mode);
602 return err;
603 }
604
605 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
606 return 0;
607}
608
609
610/************************
611 * Rx path frame handler
612 ************************/
613
Jiri Pirko19a0b582012-04-20 04:42:05 +0000614static bool team_port_enabled(struct team_port *port);
615
Jiri Pirko3d249d42011-11-11 22:16:48 +0000616/* note: already called with rcu_read_lock */
617static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
618{
619 struct sk_buff *skb = *pskb;
620 struct team_port *port;
621 struct team *team;
622 rx_handler_result_t res;
623
624 skb = skb_share_check(skb, GFP_ATOMIC);
625 if (!skb)
626 return RX_HANDLER_CONSUMED;
627
628 *pskb = skb;
629
630 port = team_port_get_rcu(skb->dev);
631 team = port->team;
Jiri Pirko19a0b582012-04-20 04:42:05 +0000632 if (!team_port_enabled(port)) {
633 /* allow exact match delivery for disabled ports */
634 res = RX_HANDLER_EXACT;
635 } else {
636 res = team->ops.receive(team, port, skb);
637 }
Jiri Pirko3d249d42011-11-11 22:16:48 +0000638 if (res == RX_HANDLER_ANOTHER) {
639 struct team_pcpu_stats *pcpu_stats;
640
641 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
642 u64_stats_update_begin(&pcpu_stats->syncp);
643 pcpu_stats->rx_packets++;
644 pcpu_stats->rx_bytes += skb->len;
645 if (skb->pkt_type == PACKET_MULTICAST)
646 pcpu_stats->rx_multicast++;
647 u64_stats_update_end(&pcpu_stats->syncp);
648
649 skb->dev = team->dev;
650 } else {
651 this_cpu_inc(team->pcpu_stats->rx_dropped);
652 }
653
654 return res;
655}
656
657
658/****************
659 * Port handling
660 ****************/
661
662static bool team_port_find(const struct team *team,
663 const struct team_port *port)
664{
665 struct team_port *cur;
666
667 list_for_each_entry(cur, &team->port_list, list)
668 if (cur == port)
669 return true;
670 return false;
671}
672
Jiri Pirko19a0b582012-04-20 04:42:05 +0000673static bool team_port_enabled(struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000674{
Jiri Pirko19a0b582012-04-20 04:42:05 +0000675 return port->index != -1;
676}
677
678/*
679 * Enable/disable port by adding to enabled port hashlist and setting
680 * port->index (Might be racy so reader could see incorrect ifindex when
681 * processing a flying packet, but that is not a problem). Write guarded
682 * by team->lock.
683 */
684static void team_port_enable(struct team *team,
685 struct team_port *port)
686{
687 if (team_port_enabled(port))
688 return;
689 port->index = team->en_port_count++;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000690 hlist_add_head_rcu(&port->hlist,
691 team_port_index_hash(team, port->index));
Jiri Pirko3d249d42011-11-11 22:16:48 +0000692}
693
694static void __reconstruct_port_hlist(struct team *team, int rm_index)
695{
696 int i;
697 struct team_port *port;
698
Jiri Pirko19a0b582012-04-20 04:42:05 +0000699 for (i = rm_index + 1; i < team->en_port_count; i++) {
Jiri Pirko3d249d42011-11-11 22:16:48 +0000700 port = team_get_port_by_index(team, i);
701 hlist_del_rcu(&port->hlist);
702 port->index--;
703 hlist_add_head_rcu(&port->hlist,
704 team_port_index_hash(team, port->index));
705 }
706}
707
Jiri Pirko19a0b582012-04-20 04:42:05 +0000708static void team_port_disable(struct team *team,
709 struct team_port *port)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000710{
711 int rm_index = port->index;
712
Jiri Pirko19a0b582012-04-20 04:42:05 +0000713 if (!team_port_enabled(port))
714 return;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000715 hlist_del_rcu(&port->hlist);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000716 __reconstruct_port_hlist(team, rm_index);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000717 team->en_port_count--;
718 port->index = -1;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000719}
720
721#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
722 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
723 NETIF_F_HIGHDMA | NETIF_F_LRO)
724
725static void __team_compute_features(struct team *team)
726{
727 struct team_port *port;
728 u32 vlan_features = TEAM_VLAN_FEATURES;
729 unsigned short max_hard_header_len = ETH_HLEN;
730
731 list_for_each_entry(port, &team->port_list, list) {
732 vlan_features = netdev_increment_features(vlan_features,
733 port->dev->vlan_features,
734 TEAM_VLAN_FEATURES);
735
736 if (port->dev->hard_header_len > max_hard_header_len)
737 max_hard_header_len = port->dev->hard_header_len;
738 }
739
740 team->dev->vlan_features = vlan_features;
741 team->dev->hard_header_len = max_hard_header_len;
742
743 netdev_change_features(team->dev);
744}
745
746static void team_compute_features(struct team *team)
747{
Jiri Pirko61dc3462011-11-16 11:09:08 +0000748 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000749 __team_compute_features(team);
Jiri Pirko61dc3462011-11-16 11:09:08 +0000750 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000751}
752
753static int team_port_enter(struct team *team, struct team_port *port)
754{
755 int err = 0;
756
757 dev_hold(team->dev);
758 port->dev->priv_flags |= IFF_TEAM_PORT;
759 if (team->ops.port_enter) {
760 err = team->ops.port_enter(team, port);
761 if (err) {
762 netdev_err(team->dev, "Device %s failed to enter team mode\n",
763 port->dev->name);
764 goto err_port_enter;
765 }
766 }
767
768 return 0;
769
770err_port_enter:
771 port->dev->priv_flags &= ~IFF_TEAM_PORT;
772 dev_put(team->dev);
773
774 return err;
775}
776
777static void team_port_leave(struct team *team, struct team_port *port)
778{
779 if (team->ops.port_leave)
780 team->ops.port_leave(team, port);
781 port->dev->priv_flags &= ~IFF_TEAM_PORT;
782 dev_put(team->dev);
783}
784
785static void __team_port_change_check(struct team_port *port, bool linkup);
786
787static int team_port_add(struct team *team, struct net_device *port_dev)
788{
789 struct net_device *dev = team->dev;
790 struct team_port *port;
791 char *portname = port_dev->name;
792 int err;
793
794 if (port_dev->flags & IFF_LOOPBACK ||
795 port_dev->type != ARPHRD_ETHER) {
796 netdev_err(dev, "Device %s is of an unsupported type\n",
797 portname);
798 return -EINVAL;
799 }
800
801 if (team_port_exists(port_dev)) {
802 netdev_err(dev, "Device %s is already a port "
803 "of a team device\n", portname);
804 return -EBUSY;
805 }
806
807 if (port_dev->flags & IFF_UP) {
808 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
809 portname);
810 return -EBUSY;
811 }
812
Jiri Pirko5149ee52012-06-19 05:54:05 +0000813 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
814 GFP_KERNEL);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000815 if (!port)
816 return -ENOMEM;
817
818 port->dev = port_dev;
819 port->team = team;
820
821 port->orig.mtu = port_dev->mtu;
822 err = dev_set_mtu(port_dev, dev->mtu);
823 if (err) {
824 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
825 goto err_set_mtu;
826 }
827
828 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
829
830 err = team_port_enter(team, port);
831 if (err) {
832 netdev_err(dev, "Device %s failed to enter team mode\n",
833 portname);
834 goto err_port_enter;
835 }
836
837 err = dev_open(port_dev);
838 if (err) {
839 netdev_dbg(dev, "Device %s opening failed\n",
840 portname);
841 goto err_dev_open;
842 }
843
Jiri Pirko57459182011-12-08 04:11:20 +0000844 err = vlan_vids_add_by_dev(port_dev, dev);
845 if (err) {
846 netdev_err(dev, "Failed to add vlan ids to device %s\n",
847 portname);
848 goto err_vids_add;
849 }
850
Jiri Pirko3d249d42011-11-11 22:16:48 +0000851 err = netdev_set_master(port_dev, dev);
852 if (err) {
853 netdev_err(dev, "Device %s failed to set master\n", portname);
854 goto err_set_master;
855 }
856
857 err = netdev_rx_handler_register(port_dev, team_handle_frame,
858 port);
859 if (err) {
860 netdev_err(dev, "Device %s failed to register rx_handler\n",
861 portname);
862 goto err_handler_register;
863 }
864
Jiri Pirko80f7c662012-04-10 05:15:42 +0000865 err = team_option_port_add(team, port);
866 if (err) {
867 netdev_err(dev, "Device %s failed to add per-port options\n",
868 portname);
869 goto err_option_port_add;
870 }
871
Jiri Pirko19a0b582012-04-20 04:42:05 +0000872 port->index = -1;
873 team_port_enable(team, port);
874 list_add_tail_rcu(&port->list, &team->port_list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000875 team_adjust_ops(team);
876 __team_compute_features(team);
877 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
878
879 netdev_info(dev, "Port device %s added\n", portname);
880
881 return 0;
882
Jiri Pirko80f7c662012-04-10 05:15:42 +0000883err_option_port_add:
884 netdev_rx_handler_unregister(port_dev);
885
Jiri Pirko3d249d42011-11-11 22:16:48 +0000886err_handler_register:
887 netdev_set_master(port_dev, NULL);
888
889err_set_master:
Jiri Pirko57459182011-12-08 04:11:20 +0000890 vlan_vids_del_by_dev(port_dev, dev);
891
892err_vids_add:
Jiri Pirko3d249d42011-11-11 22:16:48 +0000893 dev_close(port_dev);
894
895err_dev_open:
896 team_port_leave(team, port);
897 team_port_set_orig_mac(port);
898
899err_port_enter:
900 dev_set_mtu(port_dev, port->orig.mtu);
901
902err_set_mtu:
903 kfree(port);
904
905 return err;
906}
907
908static int team_port_del(struct team *team, struct net_device *port_dev)
909{
910 struct net_device *dev = team->dev;
911 struct team_port *port;
912 char *portname = port_dev->name;
913
914 port = team_port_get_rtnl(port_dev);
915 if (!port || !team_port_find(team, port)) {
916 netdev_err(dev, "Device %s does not act as a port of this team\n",
917 portname);
918 return -ENOENT;
919 }
920
Jiri Pirkob82b9182012-01-24 05:16:00 +0000921 port->removed = true;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000922 __team_port_change_check(port, false);
Jiri Pirko19a0b582012-04-20 04:42:05 +0000923 team_port_disable(team, port);
924 list_del_rcu(&port->list);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000925 team_adjust_ops(team);
Jiri Pirko80f7c662012-04-10 05:15:42 +0000926 team_option_port_del(team, port);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000927 netdev_rx_handler_unregister(port_dev);
928 netdev_set_master(port_dev, NULL);
Jiri Pirko57459182011-12-08 04:11:20 +0000929 vlan_vids_del_by_dev(port_dev, dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000930 dev_close(port_dev);
931 team_port_leave(team, port);
932 team_port_set_orig_mac(port);
933 dev_set_mtu(port_dev, port->orig.mtu);
934 synchronize_rcu();
935 kfree(port);
936 netdev_info(dev, "Port device %s removed\n", portname);
937 __team_compute_features(team);
938
939 return 0;
940}
941
942
943/*****************
944 * Net device ops
945 *****************/
946
Jiri Pirko80f7c662012-04-10 05:15:42 +0000947static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000948{
Jiri Pirkod299cd52012-06-19 05:54:04 +0000949 ctx->data.str_val = team->mode->kind;
Jiri Pirko3d249d42011-11-11 22:16:48 +0000950 return 0;
951}
952
Jiri Pirko80f7c662012-04-10 05:15:42 +0000953static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
Jiri Pirko3d249d42011-11-11 22:16:48 +0000954{
Jiri Pirko80f7c662012-04-10 05:15:42 +0000955 return team_change_mode(team, ctx->data.str_val);
Jiri Pirko3d249d42011-11-11 22:16:48 +0000956}
957
Jiri Pirkoacd69962012-04-20 04:42:06 +0000958static int team_port_en_option_get(struct team *team,
959 struct team_gsetter_ctx *ctx)
960{
961 ctx->data.bool_val = team_port_enabled(ctx->port);
962 return 0;
963}
964
965static int team_port_en_option_set(struct team *team,
966 struct team_gsetter_ctx *ctx)
967{
968 if (ctx->data.bool_val)
969 team_port_enable(team, ctx->port);
970 else
971 team_port_disable(team, ctx->port);
972 return 0;
973}
974
Jiri Pirko71472ec2012-04-10 05:15:44 +0000975static int team_user_linkup_option_get(struct team *team,
976 struct team_gsetter_ctx *ctx)
977{
978 ctx->data.bool_val = ctx->port->user.linkup;
979 return 0;
980}
981
982static int team_user_linkup_option_set(struct team *team,
983 struct team_gsetter_ctx *ctx)
984{
985 ctx->port->user.linkup = ctx->data.bool_val;
986 team_refresh_port_linkup(ctx->port);
987 return 0;
988}
989
990static int team_user_linkup_en_option_get(struct team *team,
991 struct team_gsetter_ctx *ctx)
992{
993 struct team_port *port = ctx->port;
994
995 ctx->data.bool_val = port->user.linkup_enabled;
996 return 0;
997}
998
999static int team_user_linkup_en_option_set(struct team *team,
1000 struct team_gsetter_ctx *ctx)
1001{
1002 struct team_port *port = ctx->port;
1003
1004 port->user.linkup_enabled = ctx->data.bool_val;
1005 team_refresh_port_linkup(ctx->port);
1006 return 0;
1007}
1008
Jiri Pirko358b8382011-11-16 11:09:09 +00001009static const struct team_option team_options[] = {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001010 {
1011 .name = "mode",
1012 .type = TEAM_OPTION_TYPE_STRING,
1013 .getter = team_mode_option_get,
1014 .setter = team_mode_option_set,
1015 },
Jiri Pirko71472ec2012-04-10 05:15:44 +00001016 {
Jiri Pirkoacd69962012-04-20 04:42:06 +00001017 .name = "enabled",
1018 .type = TEAM_OPTION_TYPE_BOOL,
1019 .per_port = true,
1020 .getter = team_port_en_option_get,
1021 .setter = team_port_en_option_set,
1022 },
1023 {
Jiri Pirko71472ec2012-04-10 05:15:44 +00001024 .name = "user_linkup",
1025 .type = TEAM_OPTION_TYPE_BOOL,
1026 .per_port = true,
1027 .getter = team_user_linkup_option_get,
1028 .setter = team_user_linkup_option_set,
1029 },
1030 {
1031 .name = "user_linkup_enabled",
1032 .type = TEAM_OPTION_TYPE_BOOL,
1033 .per_port = true,
1034 .getter = team_user_linkup_en_option_get,
1035 .setter = team_user_linkup_en_option_set,
1036 },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001037};
1038
1039static int team_init(struct net_device *dev)
1040{
1041 struct team *team = netdev_priv(dev);
1042 int i;
Jiri Pirko358b8382011-11-16 11:09:09 +00001043 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001044
1045 team->dev = dev;
Jiri Pirko61dc3462011-11-16 11:09:08 +00001046 mutex_init(&team->lock);
Jiri Pirkod299cd52012-06-19 05:54:04 +00001047 team_set_no_mode(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001048
1049 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1050 if (!team->pcpu_stats)
1051 return -ENOMEM;
1052
1053 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
Jiri Pirko19a0b582012-04-20 04:42:05 +00001054 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001055 INIT_LIST_HEAD(&team->port_list);
1056
1057 team_adjust_ops(team);
1058
1059 INIT_LIST_HEAD(&team->option_list);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001060 INIT_LIST_HEAD(&team->option_inst_list);
Jiri Pirko358b8382011-11-16 11:09:09 +00001061 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1062 if (err)
1063 goto err_options_register;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001064 netif_carrier_off(dev);
1065
1066 return 0;
Jiri Pirko358b8382011-11-16 11:09:09 +00001067
1068err_options_register:
1069 free_percpu(team->pcpu_stats);
1070
1071 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001072}
1073
1074static void team_uninit(struct net_device *dev)
1075{
1076 struct team *team = netdev_priv(dev);
1077 struct team_port *port;
1078 struct team_port *tmp;
1079
Jiri Pirko61dc3462011-11-16 11:09:08 +00001080 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001081 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1082 team_port_del(team, port->dev);
1083
1084 __team_change_mode(team, NULL); /* cleanup */
1085 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
Jiri Pirko61dc3462011-11-16 11:09:08 +00001086 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001087}
1088
1089static void team_destructor(struct net_device *dev)
1090{
1091 struct team *team = netdev_priv(dev);
1092
1093 free_percpu(team->pcpu_stats);
1094 free_netdev(dev);
1095}
1096
1097static int team_open(struct net_device *dev)
1098{
1099 netif_carrier_on(dev);
1100 return 0;
1101}
1102
1103static int team_close(struct net_device *dev)
1104{
1105 netif_carrier_off(dev);
1106 return 0;
1107}
1108
1109/*
1110 * note: already called with rcu_read_lock
1111 */
1112static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1113{
1114 struct team *team = netdev_priv(dev);
1115 bool tx_success = false;
1116 unsigned int len = skb->len;
1117
1118 tx_success = team->ops.transmit(team, skb);
1119 if (tx_success) {
1120 struct team_pcpu_stats *pcpu_stats;
1121
1122 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1123 u64_stats_update_begin(&pcpu_stats->syncp);
1124 pcpu_stats->tx_packets++;
1125 pcpu_stats->tx_bytes += len;
1126 u64_stats_update_end(&pcpu_stats->syncp);
1127 } else {
1128 this_cpu_inc(team->pcpu_stats->tx_dropped);
1129 }
1130
1131 return NETDEV_TX_OK;
1132}
1133
1134static void team_change_rx_flags(struct net_device *dev, int change)
1135{
1136 struct team *team = netdev_priv(dev);
1137 struct team_port *port;
1138 int inc;
1139
1140 rcu_read_lock();
1141 list_for_each_entry_rcu(port, &team->port_list, list) {
1142 if (change & IFF_PROMISC) {
1143 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1144 dev_set_promiscuity(port->dev, inc);
1145 }
1146 if (change & IFF_ALLMULTI) {
1147 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1148 dev_set_allmulti(port->dev, inc);
1149 }
1150 }
1151 rcu_read_unlock();
1152}
1153
1154static void team_set_rx_mode(struct net_device *dev)
1155{
1156 struct team *team = netdev_priv(dev);
1157 struct team_port *port;
1158
1159 rcu_read_lock();
1160 list_for_each_entry_rcu(port, &team->port_list, list) {
1161 dev_uc_sync(port->dev, dev);
1162 dev_mc_sync(port->dev, dev);
1163 }
1164 rcu_read_unlock();
1165}
1166
1167static int team_set_mac_address(struct net_device *dev, void *p)
1168{
1169 struct team *team = netdev_priv(dev);
1170 struct team_port *port;
1171 struct sockaddr *addr = p;
1172
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001173 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001174 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1175 rcu_read_lock();
1176 list_for_each_entry_rcu(port, &team->port_list, list)
1177 if (team->ops.port_change_mac)
1178 team->ops.port_change_mac(team, port);
1179 rcu_read_unlock();
1180 return 0;
1181}
1182
1183static int team_change_mtu(struct net_device *dev, int new_mtu)
1184{
1185 struct team *team = netdev_priv(dev);
1186 struct team_port *port;
1187 int err;
1188
1189 /*
1190 * Alhough this is reader, it's guarded by team lock. It's not possible
1191 * to traverse list in reverse under rcu_read_lock
1192 */
Jiri Pirko61dc3462011-11-16 11:09:08 +00001193 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001194 list_for_each_entry(port, &team->port_list, list) {
1195 err = dev_set_mtu(port->dev, new_mtu);
1196 if (err) {
1197 netdev_err(dev, "Device %s failed to change mtu",
1198 port->dev->name);
1199 goto unwind;
1200 }
1201 }
Jiri Pirko61dc3462011-11-16 11:09:08 +00001202 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001203
1204 dev->mtu = new_mtu;
1205
1206 return 0;
1207
1208unwind:
1209 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1210 dev_set_mtu(port->dev, dev->mtu);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001211 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001212
1213 return err;
1214}
1215
1216static struct rtnl_link_stats64 *
1217team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1218{
1219 struct team *team = netdev_priv(dev);
1220 struct team_pcpu_stats *p;
1221 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1222 u32 rx_dropped = 0, tx_dropped = 0;
1223 unsigned int start;
1224 int i;
1225
1226 for_each_possible_cpu(i) {
1227 p = per_cpu_ptr(team->pcpu_stats, i);
1228 do {
1229 start = u64_stats_fetch_begin_bh(&p->syncp);
1230 rx_packets = p->rx_packets;
1231 rx_bytes = p->rx_bytes;
1232 rx_multicast = p->rx_multicast;
1233 tx_packets = p->tx_packets;
1234 tx_bytes = p->tx_bytes;
1235 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1236
1237 stats->rx_packets += rx_packets;
1238 stats->rx_bytes += rx_bytes;
1239 stats->multicast += rx_multicast;
1240 stats->tx_packets += tx_packets;
1241 stats->tx_bytes += tx_bytes;
1242 /*
1243 * rx_dropped & tx_dropped are u32, updated
1244 * without syncp protection.
1245 */
1246 rx_dropped += p->rx_dropped;
1247 tx_dropped += p->tx_dropped;
1248 }
1249 stats->rx_dropped = rx_dropped;
1250 stats->tx_dropped = tx_dropped;
1251 return stats;
1252}
1253
Jiri Pirko8e586132011-12-08 19:52:37 -05001254static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001255{
1256 struct team *team = netdev_priv(dev);
1257 struct team_port *port;
Jiri Pirko87002b02011-12-08 04:11:17 +00001258 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001259
Jiri Pirko87002b02011-12-08 04:11:17 +00001260 /*
1261 * Alhough this is reader, it's guarded by team lock. It's not possible
1262 * to traverse list in reverse under rcu_read_lock
1263 */
1264 mutex_lock(&team->lock);
1265 list_for_each_entry(port, &team->port_list, list) {
1266 err = vlan_vid_add(port->dev, vid);
1267 if (err)
1268 goto unwind;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001269 }
Jiri Pirko87002b02011-12-08 04:11:17 +00001270 mutex_unlock(&team->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001271
1272 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +00001273
1274unwind:
1275 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1276 vlan_vid_del(port->dev, vid);
1277 mutex_unlock(&team->lock);
1278
1279 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001280}
1281
Jiri Pirko8e586132011-12-08 19:52:37 -05001282static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001283{
1284 struct team *team = netdev_priv(dev);
1285 struct team_port *port;
1286
1287 rcu_read_lock();
Jiri Pirko87002b02011-12-08 04:11:17 +00001288 list_for_each_entry_rcu(port, &team->port_list, list)
1289 vlan_vid_del(port->dev, vid);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001290 rcu_read_unlock();
Jiri Pirko8e586132011-12-08 19:52:37 -05001291
1292 return 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001293}
1294
1295static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1296{
1297 struct team *team = netdev_priv(dev);
1298 int err;
1299
Jiri Pirko61dc3462011-11-16 11:09:08 +00001300 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001301 err = team_port_add(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001302 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001303 return err;
1304}
1305
1306static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1307{
1308 struct team *team = netdev_priv(dev);
1309 int err;
1310
Jiri Pirko61dc3462011-11-16 11:09:08 +00001311 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001312 err = team_port_del(team, port_dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001313 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001314 return err;
1315}
1316
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001317static netdev_features_t team_fix_features(struct net_device *dev,
1318 netdev_features_t features)
1319{
1320 struct team_port *port;
1321 struct team *team = netdev_priv(dev);
1322 netdev_features_t mask;
1323
1324 mask = features;
1325 features &= ~NETIF_F_ONE_FOR_ALL;
1326 features |= NETIF_F_ALL_FOR_ALL;
1327
1328 rcu_read_lock();
1329 list_for_each_entry_rcu(port, &team->port_list, list) {
1330 features = netdev_increment_features(features,
1331 port->dev->features,
1332 mask);
1333 }
1334 rcu_read_unlock();
1335 return features;
1336}
1337
Jiri Pirko3d249d42011-11-11 22:16:48 +00001338static const struct net_device_ops team_netdev_ops = {
1339 .ndo_init = team_init,
1340 .ndo_uninit = team_uninit,
1341 .ndo_open = team_open,
1342 .ndo_stop = team_close,
1343 .ndo_start_xmit = team_xmit,
1344 .ndo_change_rx_flags = team_change_rx_flags,
1345 .ndo_set_rx_mode = team_set_rx_mode,
1346 .ndo_set_mac_address = team_set_mac_address,
1347 .ndo_change_mtu = team_change_mtu,
1348 .ndo_get_stats64 = team_get_stats64,
1349 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1350 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1351 .ndo_add_slave = team_add_slave,
1352 .ndo_del_slave = team_del_slave,
Jiri Pirko234a8fd2011-11-17 04:16:04 +00001353 .ndo_fix_features = team_fix_features,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001354};
1355
1356
1357/***********************
1358 * rt netlink interface
1359 ***********************/
1360
1361static void team_setup(struct net_device *dev)
1362{
1363 ether_setup(dev);
1364
1365 dev->netdev_ops = &team_netdev_ops;
1366 dev->destructor = team_destructor;
1367 dev->tx_queue_len = 0;
1368 dev->flags |= IFF_MULTICAST;
1369 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1370
1371 /*
1372 * Indicate we support unicast address filtering. That way core won't
1373 * bring us to promisc mode in case a unicast addr is added.
1374 * Let this up to underlay drivers.
1375 */
1376 dev->priv_flags |= IFF_UNICAST_FLT;
1377
1378 dev->features |= NETIF_F_LLTX;
1379 dev->features |= NETIF_F_GRO;
1380 dev->hw_features = NETIF_F_HW_VLAN_TX |
1381 NETIF_F_HW_VLAN_RX |
1382 NETIF_F_HW_VLAN_FILTER;
1383
1384 dev->features |= dev->hw_features;
1385}
1386
1387static int team_newlink(struct net *src_net, struct net_device *dev,
1388 struct nlattr *tb[], struct nlattr *data[])
1389{
1390 int err;
1391
1392 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001393 eth_hw_addr_random(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001394
1395 err = register_netdevice(dev);
1396 if (err)
1397 return err;
1398
1399 return 0;
1400}
1401
1402static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1403{
1404 if (tb[IFLA_ADDRESS]) {
1405 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1406 return -EINVAL;
1407 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1408 return -EADDRNOTAVAIL;
1409 }
1410 return 0;
1411}
1412
1413static struct rtnl_link_ops team_link_ops __read_mostly = {
1414 .kind = DRV_NAME,
1415 .priv_size = sizeof(struct team),
1416 .setup = team_setup,
1417 .newlink = team_newlink,
1418 .validate = team_validate,
1419};
1420
1421
1422/***********************************
1423 * Generic netlink custom interface
1424 ***********************************/
1425
1426static struct genl_family team_nl_family = {
1427 .id = GENL_ID_GENERATE,
1428 .name = TEAM_GENL_NAME,
1429 .version = TEAM_GENL_VERSION,
1430 .maxattr = TEAM_ATTR_MAX,
1431 .netnsok = true,
1432};
1433
1434static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1435 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1436 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1437 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1438 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1439};
1440
1441static const struct nla_policy
1442team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1443 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1444 [TEAM_ATTR_OPTION_NAME] = {
1445 .type = NLA_STRING,
1446 .len = TEAM_STRING_MAX_LEN,
1447 },
1448 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1449 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
Jiri Pirko26155982012-04-04 12:16:26 +00001450 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
Jiri Pirko3d249d42011-11-11 22:16:48 +00001451};
1452
1453static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1454{
1455 struct sk_buff *msg;
1456 void *hdr;
1457 int err;
1458
1459 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1460 if (!msg)
1461 return -ENOMEM;
1462
1463 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1464 &team_nl_family, 0, TEAM_CMD_NOOP);
1465 if (IS_ERR(hdr)) {
1466 err = PTR_ERR(hdr);
1467 goto err_msg_put;
1468 }
1469
1470 genlmsg_end(msg, hdr);
1471
1472 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1473
1474err_msg_put:
1475 nlmsg_free(msg);
1476
1477 return err;
1478}
1479
1480/*
1481 * Netlink cmd functions should be locked by following two functions.
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001482 * Since dev gets held here, that ensures dev won't disappear in between.
Jiri Pirko3d249d42011-11-11 22:16:48 +00001483 */
1484static struct team *team_nl_team_get(struct genl_info *info)
1485{
1486 struct net *net = genl_info_net(info);
1487 int ifindex;
1488 struct net_device *dev;
1489 struct team *team;
1490
1491 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1492 return NULL;
1493
1494 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001495 dev = dev_get_by_index(net, ifindex);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001496 if (!dev || dev->netdev_ops != &team_netdev_ops) {
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001497 if (dev)
1498 dev_put(dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001499 return NULL;
1500 }
1501
1502 team = netdev_priv(dev);
Jiri Pirko61dc3462011-11-16 11:09:08 +00001503 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001504 return team;
1505}
1506
1507static void team_nl_team_put(struct team *team)
1508{
Jiri Pirko61dc3462011-11-16 11:09:08 +00001509 mutex_unlock(&team->lock);
Jiri Pirko8c0713a2011-11-16 11:55:54 +00001510 dev_put(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001511}
1512
1513static int team_nl_send_generic(struct genl_info *info, struct team *team,
1514 int (*fill_func)(struct sk_buff *skb,
1515 struct genl_info *info,
1516 int flags, struct team *team))
1517{
1518 struct sk_buff *skb;
1519 int err;
1520
1521 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1522 if (!skb)
1523 return -ENOMEM;
1524
1525 err = fill_func(skb, info, NLM_F_ACK, team);
1526 if (err < 0)
1527 goto err_fill;
1528
1529 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1530 return err;
1531
1532err_fill:
1533 nlmsg_free(skb);
1534 return err;
1535}
1536
Jiri Pirkob82b9182012-01-24 05:16:00 +00001537static int team_nl_fill_options_get(struct sk_buff *skb,
1538 u32 pid, u32 seq, int flags,
1539 struct team *team, bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001540{
1541 struct nlattr *option_list;
1542 void *hdr;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001543 struct team_option_inst *opt_inst;
1544 int err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001545
1546 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1547 TEAM_CMD_OPTIONS_GET);
1548 if (IS_ERR(hdr))
1549 return PTR_ERR(hdr);
1550
David S. Miller86ebb022012-04-01 20:25:18 -04001551 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1552 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001553 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1554 if (!option_list)
1555 return -EMSGSIZE;
1556
Jiri Pirko80f7c662012-04-10 05:15:42 +00001557 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001558 struct nlattr *option_item;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001559 struct team_option *option = opt_inst->option;
1560 struct team_gsetter_ctx ctx;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001561
Jiri Pirkob82b9182012-01-24 05:16:00 +00001562 /* Include only changed options if fill all mode is not on */
Jiri Pirko80f7c662012-04-10 05:15:42 +00001563 if (!fillall && !opt_inst->changed)
Jiri Pirkob82b9182012-01-24 05:16:00 +00001564 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001565 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1566 if (!option_item)
1567 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04001568 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1569 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001570 if (opt_inst->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04001571 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1572 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001573 opt_inst->changed = false;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001574 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001575 if (opt_inst->removed &&
David S. Miller86ebb022012-04-01 20:25:18 -04001576 nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1577 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001578 if (opt_inst->port &&
1579 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1580 opt_inst->port->dev->ifindex))
1581 goto nla_put_failure;
1582 ctx.port = opt_inst->port;
Jiri Pirkob1303322012-06-19 05:54:08 +00001583 if (opt_inst->option->array_size &&
1584 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1585 opt_inst->array_index))
1586 goto nla_put_failure;
1587 ctx.array_index = opt_inst->array_index;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001588 switch (option->type) {
1589 case TEAM_OPTION_TYPE_U32:
David S. Miller86ebb022012-04-01 20:25:18 -04001590 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
1591 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001592 err = team_option_get(team, opt_inst, &ctx);
1593 if (err)
1594 goto errout;
1595 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA,
1596 ctx.data.u32_val))
David S. Miller86ebb022012-04-01 20:25:18 -04001597 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001598 break;
1599 case TEAM_OPTION_TYPE_STRING:
David S. Miller86ebb022012-04-01 20:25:18 -04001600 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
1601 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001602 err = team_option_get(team, opt_inst, &ctx);
1603 if (err)
1604 goto errout;
David S. Miller86ebb022012-04-01 20:25:18 -04001605 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
Jiri Pirko80f7c662012-04-10 05:15:42 +00001606 ctx.data.str_val))
David S. Miller86ebb022012-04-01 20:25:18 -04001607 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001608 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001609 case TEAM_OPTION_TYPE_BINARY:
1610 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
1611 goto nla_put_failure;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001612 err = team_option_get(team, opt_inst, &ctx);
1613 if (err)
1614 goto errout;
Jiri Pirko26155982012-04-04 12:16:26 +00001615 if (nla_put(skb, TEAM_ATTR_OPTION_DATA,
Jiri Pirko80f7c662012-04-10 05:15:42 +00001616 ctx.data.bin_val.len, ctx.data.bin_val.ptr))
Jiri Pirko26155982012-04-04 12:16:26 +00001617 goto nla_put_failure;
1618 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001619 case TEAM_OPTION_TYPE_BOOL:
1620 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
1621 goto nla_put_failure;
1622 err = team_option_get(team, opt_inst, &ctx);
1623 if (err)
1624 goto errout;
1625 if (ctx.data.bool_val &&
1626 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
1627 goto nla_put_failure;
1628 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001629 default:
1630 BUG();
1631 }
1632 nla_nest_end(skb, option_item);
1633 }
1634
1635 nla_nest_end(skb, option_list);
1636 return genlmsg_end(skb, hdr);
1637
1638nla_put_failure:
Jiri Pirko80f7c662012-04-10 05:15:42 +00001639 err = -EMSGSIZE;
1640errout:
Jiri Pirko3d249d42011-11-11 22:16:48 +00001641 genlmsg_cancel(skb, hdr);
Jiri Pirko80f7c662012-04-10 05:15:42 +00001642 return err;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001643}
1644
Jiri Pirkob82b9182012-01-24 05:16:00 +00001645static int team_nl_fill_options_get_all(struct sk_buff *skb,
1646 struct genl_info *info, int flags,
1647 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001648{
Jiri Pirkob82b9182012-01-24 05:16:00 +00001649 return team_nl_fill_options_get(skb, info->snd_pid,
1650 info->snd_seq, NLM_F_ACK,
1651 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001652}
1653
1654static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1655{
1656 struct team *team;
1657 int err;
1658
1659 team = team_nl_team_get(info);
1660 if (!team)
1661 return -EINVAL;
1662
Jiri Pirkob82b9182012-01-24 05:16:00 +00001663 err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001664
1665 team_nl_team_put(team);
1666
1667 return err;
1668}
1669
1670static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1671{
1672 struct team *team;
1673 int err = 0;
1674 int i;
1675 struct nlattr *nl_option;
1676
1677 team = team_nl_team_get(info);
1678 if (!team)
1679 return -EINVAL;
1680
1681 err = -EINVAL;
1682 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1683 err = -EINVAL;
1684 goto team_put;
1685 }
1686
1687 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
Jiri Pirko80f7c662012-04-10 05:15:42 +00001688 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
Jiri Pirkob1303322012-06-19 05:54:08 +00001689 struct nlattr *attr;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001690 struct nlattr *attr_data;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001691 enum team_option_type opt_type;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001692 int opt_port_ifindex = 0; /* != 0 for per-port options */
Jiri Pirkob1303322012-06-19 05:54:08 +00001693 u32 opt_array_index = 0;
1694 bool opt_is_array = false;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001695 struct team_option_inst *opt_inst;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001696 char *opt_name;
1697 bool opt_found = false;
1698
1699 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1700 err = -EINVAL;
1701 goto team_put;
1702 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001703 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
Jiri Pirko3d249d42011-11-11 22:16:48 +00001704 nl_option, team_nl_option_policy);
1705 if (err)
1706 goto team_put;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001707 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
Jiri Pirko14f066b2012-04-10 05:15:43 +00001708 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001709 err = -EINVAL;
1710 goto team_put;
1711 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001712 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
Jiri Pirko3d249d42011-11-11 22:16:48 +00001713 case NLA_U32:
1714 opt_type = TEAM_OPTION_TYPE_U32;
1715 break;
1716 case NLA_STRING:
1717 opt_type = TEAM_OPTION_TYPE_STRING;
1718 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001719 case NLA_BINARY:
1720 opt_type = TEAM_OPTION_TYPE_BINARY;
1721 break;
Jiri Pirko14f066b2012-04-10 05:15:43 +00001722 case NLA_FLAG:
1723 opt_type = TEAM_OPTION_TYPE_BOOL;
1724 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001725 default:
1726 goto team_put;
1727 }
1728
Jiri Pirko14f066b2012-04-10 05:15:43 +00001729 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1730 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1731 err = -EINVAL;
1732 goto team_put;
1733 }
1734
Jiri Pirko80f7c662012-04-10 05:15:42 +00001735 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
Jiri Pirkob1303322012-06-19 05:54:08 +00001736 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1737 if (attr)
1738 opt_port_ifindex = nla_get_u32(attr);
1739
1740 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1741 if (attr) {
1742 opt_is_array = true;
1743 opt_array_index = nla_get_u32(attr);
1744 }
Jiri Pirko3d249d42011-11-11 22:16:48 +00001745
Jiri Pirko80f7c662012-04-10 05:15:42 +00001746 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
1747 struct team_option *option = opt_inst->option;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001748 struct team_gsetter_ctx ctx;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001749 int tmp_ifindex;
1750
1751 tmp_ifindex = opt_inst->port ?
1752 opt_inst->port->dev->ifindex : 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001753 if (option->type != opt_type ||
Jiri Pirko80f7c662012-04-10 05:15:42 +00001754 strcmp(option->name, opt_name) ||
Jiri Pirkob1303322012-06-19 05:54:08 +00001755 tmp_ifindex != opt_port_ifindex ||
1756 (option->array_size && !opt_is_array) ||
1757 opt_inst->array_index != opt_array_index)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001758 continue;
1759 opt_found = true;
Jiri Pirko80f7c662012-04-10 05:15:42 +00001760 ctx.port = opt_inst->port;
Jiri Pirkob1303322012-06-19 05:54:08 +00001761 ctx.array_index = opt_inst->array_index;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001762 switch (opt_type) {
1763 case TEAM_OPTION_TYPE_U32:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001764 ctx.data.u32_val = nla_get_u32(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001765 break;
1766 case TEAM_OPTION_TYPE_STRING:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001767 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
Jiri Pirko26155982012-04-04 12:16:26 +00001768 err = -EINVAL;
1769 goto team_put;
1770 }
Jiri Pirko14f066b2012-04-10 05:15:43 +00001771 ctx.data.str_val = nla_data(attr_data);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001772 break;
Jiri Pirko26155982012-04-04 12:16:26 +00001773 case TEAM_OPTION_TYPE_BINARY:
Jiri Pirko14f066b2012-04-10 05:15:43 +00001774 ctx.data.bin_val.len = nla_len(attr_data);
1775 ctx.data.bin_val.ptr = nla_data(attr_data);
1776 break;
1777 case TEAM_OPTION_TYPE_BOOL:
1778 ctx.data.bool_val = attr_data ? true : false;
Jiri Pirko26155982012-04-04 12:16:26 +00001779 break;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001780 default:
1781 BUG();
1782 }
Jiri Pirko80f7c662012-04-10 05:15:42 +00001783 err = team_option_set(team, opt_inst, &ctx);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001784 if (err)
1785 goto team_put;
1786 }
1787 if (!opt_found) {
1788 err = -ENOENT;
1789 goto team_put;
1790 }
1791 }
1792
1793team_put:
1794 team_nl_team_put(team);
1795
1796 return err;
1797}
1798
Jiri Pirkob82b9182012-01-24 05:16:00 +00001799static int team_nl_fill_port_list_get(struct sk_buff *skb,
1800 u32 pid, u32 seq, int flags,
1801 struct team *team,
1802 bool fillall)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001803{
1804 struct nlattr *port_list;
1805 void *hdr;
1806 struct team_port *port;
1807
1808 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1809 TEAM_CMD_PORT_LIST_GET);
1810 if (IS_ERR(hdr))
1811 return PTR_ERR(hdr);
1812
David S. Miller86ebb022012-04-01 20:25:18 -04001813 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1814 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001815 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1816 if (!port_list)
1817 return -EMSGSIZE;
1818
1819 list_for_each_entry(port, &team->port_list, list) {
1820 struct nlattr *port_item;
1821
Jiri Pirkob82b9182012-01-24 05:16:00 +00001822 /* Include only changed ports if fill all mode is not on */
1823 if (!fillall && !port->changed)
1824 continue;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001825 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1826 if (!port_item)
1827 goto nla_put_failure;
David S. Miller86ebb022012-04-01 20:25:18 -04001828 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
1829 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001830 if (port->changed) {
David S. Miller86ebb022012-04-01 20:25:18 -04001831 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
1832 goto nla_put_failure;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001833 port->changed = false;
1834 }
David S. Miller86ebb022012-04-01 20:25:18 -04001835 if ((port->removed &&
1836 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001837 (port->state.linkup &&
David S. Miller86ebb022012-04-01 20:25:18 -04001838 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
Jiri Pirko71472ec2012-04-10 05:15:44 +00001839 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
1840 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
David S. Miller86ebb022012-04-01 20:25:18 -04001841 goto nla_put_failure;
Jiri Pirko3d249d42011-11-11 22:16:48 +00001842 nla_nest_end(skb, port_item);
1843 }
1844
1845 nla_nest_end(skb, port_list);
1846 return genlmsg_end(skb, hdr);
1847
1848nla_put_failure:
1849 genlmsg_cancel(skb, hdr);
1850 return -EMSGSIZE;
1851}
1852
Jiri Pirkob82b9182012-01-24 05:16:00 +00001853static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
1854 struct genl_info *info, int flags,
1855 struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001856{
Jiri Pirkob82b9182012-01-24 05:16:00 +00001857 return team_nl_fill_port_list_get(skb, info->snd_pid,
1858 info->snd_seq, NLM_F_ACK,
1859 team, true);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001860}
1861
1862static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1863 struct genl_info *info)
1864{
1865 struct team *team;
1866 int err;
1867
1868 team = team_nl_team_get(info);
1869 if (!team)
1870 return -EINVAL;
1871
Jiri Pirkob82b9182012-01-24 05:16:00 +00001872 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001873
1874 team_nl_team_put(team);
1875
1876 return err;
1877}
1878
1879static struct genl_ops team_nl_ops[] = {
1880 {
1881 .cmd = TEAM_CMD_NOOP,
1882 .doit = team_nl_cmd_noop,
1883 .policy = team_nl_policy,
1884 },
1885 {
1886 .cmd = TEAM_CMD_OPTIONS_SET,
1887 .doit = team_nl_cmd_options_set,
1888 .policy = team_nl_policy,
1889 .flags = GENL_ADMIN_PERM,
1890 },
1891 {
1892 .cmd = TEAM_CMD_OPTIONS_GET,
1893 .doit = team_nl_cmd_options_get,
1894 .policy = team_nl_policy,
1895 .flags = GENL_ADMIN_PERM,
1896 },
1897 {
1898 .cmd = TEAM_CMD_PORT_LIST_GET,
1899 .doit = team_nl_cmd_port_list_get,
1900 .policy = team_nl_policy,
1901 .flags = GENL_ADMIN_PERM,
1902 },
1903};
1904
1905static struct genl_multicast_group team_change_event_mcgrp = {
1906 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1907};
1908
Jiri Pirkob82b9182012-01-24 05:16:00 +00001909static int team_nl_send_event_options_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001910{
1911 struct sk_buff *skb;
1912 int err;
1913 struct net *net = dev_net(team->dev);
1914
1915 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1916 if (!skb)
1917 return -ENOMEM;
1918
Jiri Pirkob82b9182012-01-24 05:16:00 +00001919 err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001920 if (err < 0)
1921 goto err_fill;
1922
1923 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1924 GFP_KERNEL);
1925 return err;
1926
1927err_fill:
1928 nlmsg_free(skb);
1929 return err;
1930}
1931
Jiri Pirkob82b9182012-01-24 05:16:00 +00001932static int team_nl_send_event_port_list_get(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001933{
1934 struct sk_buff *skb;
1935 int err;
Jiri Pirkob82b9182012-01-24 05:16:00 +00001936 struct net *net = dev_net(team->dev);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001937
1938 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1939 if (!skb)
1940 return -ENOMEM;
1941
Jiri Pirkob82b9182012-01-24 05:16:00 +00001942 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001943 if (err < 0)
1944 goto err_fill;
1945
1946 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1947 GFP_KERNEL);
1948 return err;
1949
1950err_fill:
1951 nlmsg_free(skb);
1952 return err;
1953}
1954
1955static int team_nl_init(void)
1956{
1957 int err;
1958
1959 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1960 ARRAY_SIZE(team_nl_ops));
1961 if (err)
1962 return err;
1963
1964 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1965 if (err)
1966 goto err_change_event_grp_reg;
1967
1968 return 0;
1969
1970err_change_event_grp_reg:
1971 genl_unregister_family(&team_nl_family);
1972
1973 return err;
1974}
1975
1976static void team_nl_fini(void)
1977{
1978 genl_unregister_family(&team_nl_family);
1979}
1980
1981
1982/******************
1983 * Change checkers
1984 ******************/
1985
Jiri Pirkob82b9182012-01-24 05:16:00 +00001986static void __team_options_change_check(struct team *team)
Jiri Pirko3d249d42011-11-11 22:16:48 +00001987{
1988 int err;
1989
Jiri Pirkob82b9182012-01-24 05:16:00 +00001990 err = team_nl_send_event_options_get(team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00001991 if (err)
1992 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1993}
1994
1995/* rtnl lock is held */
1996static void __team_port_change_check(struct team_port *port, bool linkup)
1997{
1998 int err;
1999
Jiri Pirko71472ec2012-04-10 05:15:44 +00002000 if (!port->removed && port->state.linkup == linkup)
Jiri Pirko3d249d42011-11-11 22:16:48 +00002001 return;
2002
Jiri Pirkob82b9182012-01-24 05:16:00 +00002003 port->changed = true;
Jiri Pirko71472ec2012-04-10 05:15:44 +00002004 port->state.linkup = linkup;
2005 team_refresh_port_linkup(port);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002006 if (linkup) {
2007 struct ethtool_cmd ecmd;
2008
2009 err = __ethtool_get_settings(port->dev, &ecmd);
2010 if (!err) {
Jiri Pirko71472ec2012-04-10 05:15:44 +00002011 port->state.speed = ethtool_cmd_speed(&ecmd);
2012 port->state.duplex = ecmd.duplex;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002013 goto send_event;
2014 }
2015 }
Jiri Pirko71472ec2012-04-10 05:15:44 +00002016 port->state.speed = 0;
2017 port->state.duplex = 0;
Jiri Pirko3d249d42011-11-11 22:16:48 +00002018
2019send_event:
Jiri Pirkob82b9182012-01-24 05:16:00 +00002020 err = team_nl_send_event_port_list_get(port->team);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002021 if (err)
2022 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2023 port->dev->name);
2024
2025}
2026
2027static void team_port_change_check(struct team_port *port, bool linkup)
2028{
2029 struct team *team = port->team;
2030
Jiri Pirko61dc3462011-11-16 11:09:08 +00002031 mutex_lock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002032 __team_port_change_check(port, linkup);
Jiri Pirko61dc3462011-11-16 11:09:08 +00002033 mutex_unlock(&team->lock);
Jiri Pirko3d249d42011-11-11 22:16:48 +00002034}
2035
2036/************************************
2037 * Net device notifier event handler
2038 ************************************/
2039
2040static int team_device_event(struct notifier_block *unused,
2041 unsigned long event, void *ptr)
2042{
2043 struct net_device *dev = (struct net_device *) ptr;
2044 struct team_port *port;
2045
2046 port = team_port_get_rtnl(dev);
2047 if (!port)
2048 return NOTIFY_DONE;
2049
2050 switch (event) {
2051 case NETDEV_UP:
2052 if (netif_carrier_ok(dev))
2053 team_port_change_check(port, true);
2054 case NETDEV_DOWN:
2055 team_port_change_check(port, false);
2056 case NETDEV_CHANGE:
2057 if (netif_running(port->dev))
2058 team_port_change_check(port,
2059 !!netif_carrier_ok(port->dev));
2060 break;
2061 case NETDEV_UNREGISTER:
2062 team_del_slave(port->team->dev, dev);
2063 break;
2064 case NETDEV_FEAT_CHANGE:
2065 team_compute_features(port->team);
2066 break;
2067 case NETDEV_CHANGEMTU:
2068 /* Forbid to change mtu of underlaying device */
2069 return NOTIFY_BAD;
2070 case NETDEV_PRE_TYPE_CHANGE:
2071 /* Forbid to change type of underlaying device */
2072 return NOTIFY_BAD;
2073 }
2074 return NOTIFY_DONE;
2075}
2076
2077static struct notifier_block team_notifier_block __read_mostly = {
2078 .notifier_call = team_device_event,
2079};
2080
2081
2082/***********************
2083 * Module init and exit
2084 ***********************/
2085
2086static int __init team_module_init(void)
2087{
2088 int err;
2089
2090 register_netdevice_notifier(&team_notifier_block);
2091
2092 err = rtnl_link_register(&team_link_ops);
2093 if (err)
2094 goto err_rtnl_reg;
2095
2096 err = team_nl_init();
2097 if (err)
2098 goto err_nl_init;
2099
2100 return 0;
2101
2102err_nl_init:
2103 rtnl_link_unregister(&team_link_ops);
2104
2105err_rtnl_reg:
2106 unregister_netdevice_notifier(&team_notifier_block);
2107
2108 return err;
2109}
2110
2111static void __exit team_module_exit(void)
2112{
2113 team_nl_fini();
2114 rtnl_link_unregister(&team_link_ops);
2115 unregister_netdevice_notifier(&team_notifier_block);
2116}
2117
2118module_init(team_module_init);
2119module_exit(team_module_exit);
2120
2121MODULE_LICENSE("GPL v2");
2122MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2123MODULE_DESCRIPTION("Ethernet team device driver");
2124MODULE_ALIAS_RTNL_LINK(DRV_NAME);