l2tp: fix return value check
[linux-2.6-block.git] / drivers / net / team / team.c
CommitLineData
3d249d4c 1/*
0d572e45 2 * drivers/net/team/team.c - Network team device driver
3d249d4c
JP
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>
bd2d0837 21#include <linux/netpoll.h>
87002b03 22#include <linux/if_vlan.h>
3d249d4c
JP
23#include <linux/if_arp.h>
24#include <linux/socket.h>
25#include <linux/etherdevice.h>
26#include <linux/rtnetlink.h>
27#include <net/rtnetlink.h>
28#include <net/genetlink.h>
29#include <net/netlink.h>
6c85f2bd 30#include <net/sch_generic.h>
3d249d4c
JP
31#include <linux/if_team.h>
32
33#define DRV_NAME "team"
34
35
36/**********
37 * Helpers
38 **********/
39
40#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
41
42static struct team_port *team_port_get_rcu(const struct net_device *dev)
43{
44 struct team_port *port = rcu_dereference(dev->rx_handler_data);
45
46 return team_port_exists(dev) ? port : NULL;
47}
48
49static struct team_port *team_port_get_rtnl(const struct net_device *dev)
50{
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
52
53 return team_port_exists(dev) ? port : NULL;
54}
55
56/*
57 * Since the ability to change mac address for open port device is tested in
58 * team_port_add, this function can be called without control of return value
59 */
60static int __set_port_mac(struct net_device *port_dev,
61 const unsigned char *dev_addr)
62{
63 struct sockaddr addr;
64
65 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
66 addr.sa_family = ARPHRD_ETHER;
67 return dev_set_mac_address(port_dev, &addr);
68}
69
cade4555 70static int team_port_set_orig_mac(struct team_port *port)
3d249d4c
JP
71{
72 return __set_port_mac(port->dev, port->orig.dev_addr);
73}
74
75int team_port_set_team_mac(struct team_port *port)
76{
77 return __set_port_mac(port->dev, port->team->dev->dev_addr);
78}
79EXPORT_SYMBOL(team_port_set_team_mac);
80
71472ec1
JP
81static void team_refresh_port_linkup(struct team_port *port)
82{
83 port->linkup = port->user.linkup_enabled ? port->user.linkup :
84 port->state.linkup;
85}
3d249d4c 86
0f1aad2b 87
3d249d4c
JP
88/*******************
89 * Options handling
90 *******************/
91
80f7c668
JP
92struct team_option_inst { /* One for each option instance */
93 struct list_head list;
01048d9a 94 struct list_head tmp_list;
80f7c668 95 struct team_option *option;
85d59a87 96 struct team_option_inst_info info;
80f7c668
JP
97 bool changed;
98 bool removed;
99};
100
101static struct team_option *__team_find_option(struct team *team,
102 const char *opt_name)
358b8382
JP
103{
104 struct team_option *option;
105
106 list_for_each_entry(option, &team->option_list, list) {
107 if (strcmp(option->name, opt_name) == 0)
108 return option;
109 }
110 return NULL;
111}
112
80f7c668
JP
113static void __team_option_inst_del(struct team_option_inst *opt_inst)
114{
115 list_del(&opt_inst->list);
116 kfree(opt_inst);
117}
118
119static void __team_option_inst_del_option(struct team *team,
120 struct team_option *option)
121{
122 struct team_option_inst *opt_inst, *tmp;
123
124 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
125 if (opt_inst->option == option)
126 __team_option_inst_del(opt_inst);
127 }
128}
129
b1303326
JP
130static int __team_option_inst_add(struct team *team, struct team_option *option,
131 struct team_port *port)
132{
133 struct team_option_inst *opt_inst;
134 unsigned int array_size;
135 unsigned int i;
85d59a87 136 int err;
b1303326
JP
137
138 array_size = option->array_size;
139 if (!array_size)
140 array_size = 1; /* No array but still need one instance */
141
142 for (i = 0; i < array_size; i++) {
143 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
144 if (!opt_inst)
145 return -ENOMEM;
146 opt_inst->option = option;
85d59a87
JP
147 opt_inst->info.port = port;
148 opt_inst->info.array_index = i;
b1303326
JP
149 opt_inst->changed = true;
150 opt_inst->removed = false;
151 list_add_tail(&opt_inst->list, &team->option_inst_list);
85d59a87
JP
152 if (option->init) {
153 err = option->init(team, &opt_inst->info);
154 if (err)
155 return err;
156 }
157
b1303326
JP
158 }
159 return 0;
160}
161
80f7c668
JP
162static int __team_option_inst_add_option(struct team *team,
163 struct team_option *option)
164{
165 struct team_port *port;
166 int err;
167
b1303326 168 if (!option->per_port) {
f88725ff 169 err = __team_option_inst_add(team, option, NULL);
b1303326
JP
170 if (err)
171 goto inst_del_option;
172 }
80f7c668
JP
173
174 list_for_each_entry(port, &team->port_list, list) {
175 err = __team_option_inst_add(team, option, port);
176 if (err)
177 goto inst_del_option;
178 }
179 return 0;
180
181inst_del_option:
182 __team_option_inst_del_option(team, option);
183 return err;
184}
185
186static void __team_option_inst_mark_removed_option(struct team *team,
187 struct team_option *option)
188{
189 struct team_option_inst *opt_inst;
190
191 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
192 if (opt_inst->option == option) {
193 opt_inst->changed = true;
194 opt_inst->removed = true;
195 }
196 }
197}
198
199static void __team_option_inst_del_port(struct team *team,
200 struct team_port *port)
201{
202 struct team_option_inst *opt_inst, *tmp;
203
204 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
205 if (opt_inst->option->per_port &&
85d59a87 206 opt_inst->info.port == port)
80f7c668
JP
207 __team_option_inst_del(opt_inst);
208 }
209}
210
211static int __team_option_inst_add_port(struct team *team,
212 struct team_port *port)
213{
214 struct team_option *option;
215 int err;
216
217 list_for_each_entry(option, &team->option_list, list) {
218 if (!option->per_port)
219 continue;
220 err = __team_option_inst_add(team, option, port);
221 if (err)
222 goto inst_del_port;
223 }
224 return 0;
225
226inst_del_port:
227 __team_option_inst_del_port(team, port);
228 return err;
229}
230
231static void __team_option_inst_mark_removed_port(struct team *team,
232 struct team_port *port)
233{
234 struct team_option_inst *opt_inst;
235
236 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
85d59a87 237 if (opt_inst->info.port == port) {
80f7c668
JP
238 opt_inst->changed = true;
239 opt_inst->removed = true;
240 }
241 }
242}
243
244static int __team_options_register(struct team *team,
245 const struct team_option *option,
246 size_t option_count)
3d249d4c
JP
247{
248 int i;
2bba19ff 249 struct team_option **dst_opts;
358b8382 250 int err;
3d249d4c 251
2bba19ff
JP
252 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
253 GFP_KERNEL);
254 if (!dst_opts)
255 return -ENOMEM;
358b8382 256 for (i = 0; i < option_count; i++, option++) {
358b8382
JP
257 if (__team_find_option(team, option->name)) {
258 err = -EEXIST;
80f7c668 259 goto alloc_rollback;
358b8382 260 }
f8a15af0
JP
261 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
262 if (!dst_opts[i]) {
358b8382 263 err = -ENOMEM;
80f7c668 264 goto alloc_rollback;
358b8382 265 }
358b8382
JP
266 }
267
b82b9183 268 for (i = 0; i < option_count; i++) {
80f7c668
JP
269 err = __team_option_inst_add_option(team, dst_opts[i]);
270 if (err)
271 goto inst_rollback;
358b8382 272 list_add_tail(&dst_opts[i]->list, &team->option_list);
b82b9183 273 }
358b8382 274
2bba19ff 275 kfree(dst_opts);
358b8382
JP
276 return 0;
277
80f7c668
JP
278inst_rollback:
279 for (i--; i >= 0; i--)
280 __team_option_inst_del_option(team, dst_opts[i]);
281
282 i = option_count - 1;
283alloc_rollback:
284 for (i--; i >= 0; i--)
358b8382
JP
285 kfree(dst_opts[i]);
286
2bba19ff 287 kfree(dst_opts);
358b8382 288 return err;
3d249d4c 289}
358b8382 290
b82b9183
JP
291static void __team_options_mark_removed(struct team *team,
292 const struct team_option *option,
293 size_t option_count)
294{
295 int i;
296
297 for (i = 0; i < option_count; i++, option++) {
298 struct team_option *del_opt;
3d249d4c 299
b82b9183 300 del_opt = __team_find_option(team, option->name);
80f7c668
JP
301 if (del_opt)
302 __team_option_inst_mark_removed_option(team, del_opt);
b82b9183
JP
303 }
304}
3d249d4c
JP
305
306static void __team_options_unregister(struct team *team,
358b8382 307 const struct team_option *option,
3d249d4c
JP
308 size_t option_count)
309{
310 int i;
311
358b8382
JP
312 for (i = 0; i < option_count; i++, option++) {
313 struct team_option *del_opt;
314
315 del_opt = __team_find_option(team, option->name);
316 if (del_opt) {
80f7c668 317 __team_option_inst_del_option(team, del_opt);
358b8382
JP
318 list_del(&del_opt->list);
319 kfree(del_opt);
320 }
321 }
3d249d4c
JP
322}
323
b82b9183
JP
324static void __team_options_change_check(struct team *team);
325
326int team_options_register(struct team *team,
327 const struct team_option *option,
328 size_t option_count)
329{
330 int err;
331
332 err = __team_options_register(team, option, option_count);
333 if (err)
334 return err;
335 __team_options_change_check(team);
336 return 0;
337}
338EXPORT_SYMBOL(team_options_register);
339
358b8382
JP
340void team_options_unregister(struct team *team,
341 const struct team_option *option,
3d249d4c
JP
342 size_t option_count)
343{
b82b9183
JP
344 __team_options_mark_removed(team, option, option_count);
345 __team_options_change_check(team);
3d249d4c 346 __team_options_unregister(team, option, option_count);
3d249d4c
JP
347}
348EXPORT_SYMBOL(team_options_unregister);
349
80f7c668
JP
350static int team_option_get(struct team *team,
351 struct team_option_inst *opt_inst,
352 struct team_gsetter_ctx *ctx)
353{
f82b959d
JP
354 if (!opt_inst->option->getter)
355 return -EOPNOTSUPP;
80f7c668
JP
356 return opt_inst->option->getter(team, ctx);
357}
358
359static int team_option_set(struct team *team,
360 struct team_option_inst *opt_inst,
361 struct team_gsetter_ctx *ctx)
3d249d4c 362{
f82b959d
JP
363 if (!opt_inst->option->setter)
364 return -EOPNOTSUPP;
2fcdb2c9 365 return opt_inst->option->setter(team, ctx);
3d249d4c
JP
366}
367
0f1aad2b
JP
368void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
369{
370 struct team_option_inst *opt_inst;
371
372 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
373 opt_inst->changed = true;
374}
375EXPORT_SYMBOL(team_option_inst_set_change);
376
377void team_options_change_check(struct team *team)
378{
379 __team_options_change_check(team);
380}
381EXPORT_SYMBOL(team_options_change_check);
382
383
3d249d4c
JP
384/****************
385 * Mode handling
386 ****************/
387
388static LIST_HEAD(mode_list);
389static DEFINE_SPINLOCK(mode_list_lock);
390
0402788a
JP
391struct team_mode_item {
392 struct list_head list;
393 const struct team_mode *mode;
394};
395
396static struct team_mode_item *__find_mode(const char *kind)
3d249d4c 397{
0402788a 398 struct team_mode_item *mitem;
3d249d4c 399
0402788a
JP
400 list_for_each_entry(mitem, &mode_list, list) {
401 if (strcmp(mitem->mode->kind, kind) == 0)
402 return mitem;
3d249d4c
JP
403 }
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
0402788a 417int team_mode_register(const struct team_mode *mode)
3d249d4c
JP
418{
419 int err = 0;
0402788a 420 struct team_mode_item *mitem;
3d249d4c
JP
421
422 if (!is_good_mode_name(mode->kind) ||
423 mode->priv_size > TEAM_MODE_PRIV_SIZE)
424 return -EINVAL;
0402788a
JP
425
426 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
427 if (!mitem)
428 return -ENOMEM;
429
3d249d4c
JP
430 spin_lock(&mode_list_lock);
431 if (__find_mode(mode->kind)) {
432 err = -EEXIST;
0402788a 433 kfree(mitem);
3d249d4c
JP
434 goto unlock;
435 }
0402788a
JP
436 mitem->mode = mode;
437 list_add_tail(&mitem->list, &mode_list);
3d249d4c
JP
438unlock:
439 spin_unlock(&mode_list_lock);
440 return err;
441}
442EXPORT_SYMBOL(team_mode_register);
443
0402788a 444void team_mode_unregister(const struct team_mode *mode)
3d249d4c 445{
0402788a
JP
446 struct team_mode_item *mitem;
447
3d249d4c 448 spin_lock(&mode_list_lock);
0402788a
JP
449 mitem = __find_mode(mode->kind);
450 if (mitem) {
451 list_del_init(&mitem->list);
452 kfree(mitem);
453 }
3d249d4c 454 spin_unlock(&mode_list_lock);
3d249d4c
JP
455}
456EXPORT_SYMBOL(team_mode_unregister);
457
0402788a 458static const struct team_mode *team_mode_get(const char *kind)
3d249d4c 459{
0402788a
JP
460 struct team_mode_item *mitem;
461 const struct team_mode *mode = NULL;
3d249d4c
JP
462
463 spin_lock(&mode_list_lock);
0402788a
JP
464 mitem = __find_mode(kind);
465 if (!mitem) {
3d249d4c
JP
466 spin_unlock(&mode_list_lock);
467 request_module("team-mode-%s", kind);
468 spin_lock(&mode_list_lock);
0402788a 469 mitem = __find_mode(kind);
3d249d4c 470 }
0402788a
JP
471 if (mitem) {
472 mode = mitem->mode;
3d249d4c
JP
473 if (!try_module_get(mode->owner))
474 mode = NULL;
0402788a 475 }
3d249d4c
JP
476
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
d299cd51
JP
499static 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
122bb046 513static void __team_adjust_ops(struct team *team, int en_port_count)
3d249d4c
JP
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
122bb046
JP
520 if (!en_port_count || !team_is_mode_set(team) ||
521 !team->mode->ops->transmit)
3d249d4c
JP
522 team->ops.transmit = team_dummy_transmit;
523 else
524 team->ops.transmit = team->mode->ops->transmit;
525
122bb046
JP
526 if (!en_port_count || !team_is_mode_set(team) ||
527 !team->mode->ops->receive)
3d249d4c
JP
528 team->ops.receive = team_dummy_receive;
529 else
530 team->ops.receive = team->mode->ops->receive;
531}
532
122bb046
JP
533static void team_adjust_ops(struct team *team)
534{
535 __team_adjust_ops(team, team->en_port_count);
536}
537
3d249d4c
JP
538/*
539 * We can benefit from the fact that it's ensured no port is present
540 * at the time of mode change. Therefore no packets are in fly so there's no
541 * need to set mode operations in any special way.
542 */
543static int __team_change_mode(struct team *team,
544 const struct team_mode *new_mode)
545{
546 /* Check if mode was previously set and do cleanup if so */
d299cd51 547 if (team_is_mode_set(team)) {
3d249d4c
JP
548 void (*exit_op)(struct team *team) = team->ops.exit;
549
550 /* Clear ops area so no callback is called any longer */
551 memset(&team->ops, 0, sizeof(struct team_mode_ops));
552 team_adjust_ops(team);
553
554 if (exit_op)
555 exit_op(team);
556 team_mode_put(team->mode);
d299cd51 557 team_set_no_mode(team);
3d249d4c
JP
558 /* zero private data area */
559 memset(&team->mode_priv, 0,
560 sizeof(struct team) - offsetof(struct team, mode_priv));
561 }
562
563 if (!new_mode)
564 return 0;
565
566 if (new_mode->ops->init) {
567 int err;
568
569 err = new_mode->ops->init(team);
570 if (err)
571 return err;
572 }
573
574 team->mode = new_mode;
575 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
576 team_adjust_ops(team);
577
578 return 0;
579}
580
581static int team_change_mode(struct team *team, const char *kind)
582{
0402788a 583 const struct team_mode *new_mode;
3d249d4c
JP
584 struct net_device *dev = team->dev;
585 int err;
586
587 if (!list_empty(&team->port_list)) {
588 netdev_err(dev, "No ports can be present during mode change\n");
589 return -EBUSY;
590 }
591
d299cd51 592 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
3d249d4c
JP
593 netdev_err(dev, "Unable to change to the same mode the team is in\n");
594 return -EINVAL;
595 }
596
597 new_mode = team_mode_get(kind);
598 if (!new_mode) {
599 netdev_err(dev, "Mode \"%s\" not found\n", kind);
600 return -EINVAL;
601 }
602
603 err = __team_change_mode(team, new_mode);
604 if (err) {
605 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
606 team_mode_put(new_mode);
607 return err;
608 }
609
610 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
611 return 0;
612}
613
614
615/************************
616 * Rx path frame handler
617 ************************/
618
619/* note: already called with rcu_read_lock */
620static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
621{
622 struct sk_buff *skb = *pskb;
623 struct team_port *port;
624 struct team *team;
625 rx_handler_result_t res;
626
627 skb = skb_share_check(skb, GFP_ATOMIC);
628 if (!skb)
629 return RX_HANDLER_CONSUMED;
630
631 *pskb = skb;
632
633 port = team_port_get_rcu(skb->dev);
634 team = port->team;
19a0b58e
JP
635 if (!team_port_enabled(port)) {
636 /* allow exact match delivery for disabled ports */
637 res = RX_HANDLER_EXACT;
638 } else {
639 res = team->ops.receive(team, port, skb);
640 }
3d249d4c
JP
641 if (res == RX_HANDLER_ANOTHER) {
642 struct team_pcpu_stats *pcpu_stats;
643
644 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
645 u64_stats_update_begin(&pcpu_stats->syncp);
646 pcpu_stats->rx_packets++;
647 pcpu_stats->rx_bytes += skb->len;
648 if (skb->pkt_type == PACKET_MULTICAST)
649 pcpu_stats->rx_multicast++;
650 u64_stats_update_end(&pcpu_stats->syncp);
651
652 skb->dev = team->dev;
653 } else {
654 this_cpu_inc(team->pcpu_stats->rx_dropped);
655 }
656
657 return res;
658}
659
660
661/****************
662 * Port handling
663 ****************/
664
665static bool team_port_find(const struct team *team,
666 const struct team_port *port)
667{
668 struct team_port *cur;
669
670 list_for_each_entry(cur, &team->port_list, list)
671 if (cur == port)
672 return true;
673 return false;
674}
675
676/*
19a0b58e
JP
677 * Enable/disable port by adding to enabled port hashlist and setting
678 * port->index (Might be racy so reader could see incorrect ifindex when
679 * processing a flying packet, but that is not a problem). Write guarded
680 * by team->lock.
3d249d4c 681 */
19a0b58e
JP
682static void team_port_enable(struct team *team,
683 struct team_port *port)
3d249d4c 684{
19a0b58e
JP
685 if (team_port_enabled(port))
686 return;
687 port->index = team->en_port_count++;
3d249d4c
JP
688 hlist_add_head_rcu(&port->hlist,
689 team_port_index_hash(team, port->index));
122bb046 690 team_adjust_ops(team);
4bccfd17
JP
691 if (team->ops.port_enabled)
692 team->ops.port_enabled(team, port);
3d249d4c
JP
693}
694
695static void __reconstruct_port_hlist(struct team *team, int rm_index)
696{
697 int i;
698 struct team_port *port;
699
19a0b58e 700 for (i = rm_index + 1; i < team->en_port_count; i++) {
3d249d4c
JP
701 port = team_get_port_by_index(team, i);
702 hlist_del_rcu(&port->hlist);
703 port->index--;
704 hlist_add_head_rcu(&port->hlist,
705 team_port_index_hash(team, port->index));
706 }
707}
708
19a0b58e
JP
709static void team_port_disable(struct team *team,
710 struct team_port *port)
3d249d4c 711{
19a0b58e
JP
712 if (!team_port_enabled(port))
713 return;
4bccfd17
JP
714 if (team->ops.port_disabled)
715 team->ops.port_disabled(team, port);
3d249d4c 716 hlist_del_rcu(&port->hlist);
122bb046 717 __reconstruct_port_hlist(team, port->index);
19a0b58e 718 port->index = -1;
122bb046
JP
719 __team_adjust_ops(team, team->en_port_count - 1);
720 /*
721 * Wait until readers see adjusted ops. This ensures that
722 * readers never see team->en_port_count == 0
723 */
724 synchronize_rcu();
725 team->en_port_count--;
3d249d4c
JP
726}
727
728#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
729 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
730 NETIF_F_HIGHDMA | NETIF_F_LRO)
731
732static void __team_compute_features(struct team *team)
733{
734 struct team_port *port;
735 u32 vlan_features = TEAM_VLAN_FEATURES;
736 unsigned short max_hard_header_len = ETH_HLEN;
dc905951 737 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
3d249d4c
JP
738
739 list_for_each_entry(port, &team->port_list, list) {
740 vlan_features = netdev_increment_features(vlan_features,
741 port->dev->vlan_features,
742 TEAM_VLAN_FEATURES);
743
dc905951 744 dst_release_flag &= port->dev->priv_flags;
3d249d4c
JP
745 if (port->dev->hard_header_len > max_hard_header_len)
746 max_hard_header_len = port->dev->hard_header_len;
747 }
748
749 team->dev->vlan_features = vlan_features;
750 team->dev->hard_header_len = max_hard_header_len;
751
dc905951
JP
752 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
753 team->dev->priv_flags = flags | dst_release_flag;
754
3d249d4c
JP
755 netdev_change_features(team->dev);
756}
757
758static void team_compute_features(struct team *team)
759{
61dc3461 760 mutex_lock(&team->lock);
3d249d4c 761 __team_compute_features(team);
61dc3461 762 mutex_unlock(&team->lock);
3d249d4c
JP
763}
764
765static int team_port_enter(struct team *team, struct team_port *port)
766{
767 int err = 0;
768
769 dev_hold(team->dev);
770 port->dev->priv_flags |= IFF_TEAM_PORT;
771 if (team->ops.port_enter) {
772 err = team->ops.port_enter(team, port);
773 if (err) {
774 netdev_err(team->dev, "Device %s failed to enter team mode\n",
775 port->dev->name);
776 goto err_port_enter;
777 }
778 }
779
780 return 0;
781
782err_port_enter:
783 port->dev->priv_flags &= ~IFF_TEAM_PORT;
784 dev_put(team->dev);
785
786 return err;
787}
788
789static void team_port_leave(struct team *team, struct team_port *port)
790{
791 if (team->ops.port_leave)
792 team->ops.port_leave(team, port);
793 port->dev->priv_flags &= ~IFF_TEAM_PORT;
794 dev_put(team->dev);
795}
796
bd2d0837 797#ifdef CONFIG_NET_POLL_CONTROLLER
47be03a2
AW
798static int team_port_enable_netpoll(struct team *team, struct team_port *port,
799 gfp_t gfp)
bd2d0837
JP
800{
801 struct netpoll *np;
802 int err;
803
47be03a2 804 np = kzalloc(sizeof(*np), gfp);
bd2d0837
JP
805 if (!np)
806 return -ENOMEM;
807
47be03a2 808 err = __netpoll_setup(np, port->dev, gfp);
bd2d0837
JP
809 if (err) {
810 kfree(np);
811 return err;
812 }
813 port->np = np;
814 return err;
815}
816
817static void team_port_disable_netpoll(struct team_port *port)
818{
819 struct netpoll *np = port->np;
820
821 if (!np)
822 return;
823 port->np = NULL;
824
825 /* Wait for transmitting packets to finish before freeing. */
826 synchronize_rcu_bh();
827 __netpoll_cleanup(np);
828 kfree(np);
829}
830
831static struct netpoll_info *team_netpoll_info(struct team *team)
832{
833 return team->dev->npinfo;
834}
835
836#else
47be03a2
AW
837static int team_port_enable_netpoll(struct team *team, struct team_port *port,
838 gfp_t gfp)
bd2d0837
JP
839{
840 return 0;
841}
842static void team_port_disable_netpoll(struct team_port *port)
843{
844}
845static struct netpoll_info *team_netpoll_info(struct team *team)
846{
847 return NULL;
848}
849#endif
850
0b121fd2 851static void __team_port_change_port_added(struct team_port *port, bool linkup);
3d249d4c
JP
852
853static int team_port_add(struct team *team, struct net_device *port_dev)
854{
855 struct net_device *dev = team->dev;
856 struct team_port *port;
857 char *portname = port_dev->name;
858 int err;
859
860 if (port_dev->flags & IFF_LOOPBACK ||
861 port_dev->type != ARPHRD_ETHER) {
862 netdev_err(dev, "Device %s is of an unsupported type\n",
863 portname);
864 return -EINVAL;
865 }
866
867 if (team_port_exists(port_dev)) {
868 netdev_err(dev, "Device %s is already a port "
869 "of a team device\n", portname);
870 return -EBUSY;
871 }
872
873 if (port_dev->flags & IFF_UP) {
874 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
875 portname);
876 return -EBUSY;
877 }
878
5149ee58
JP
879 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
880 GFP_KERNEL);
3d249d4c
JP
881 if (!port)
882 return -ENOMEM;
883
884 port->dev = port_dev;
885 port->team = team;
886
887 port->orig.mtu = port_dev->mtu;
888 err = dev_set_mtu(port_dev, dev->mtu);
889 if (err) {
890 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
891 goto err_set_mtu;
892 }
893
894 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
895
896 err = team_port_enter(team, port);
897 if (err) {
898 netdev_err(dev, "Device %s failed to enter team mode\n",
899 portname);
900 goto err_port_enter;
901 }
902
903 err = dev_open(port_dev);
904 if (err) {
905 netdev_dbg(dev, "Device %s opening failed\n",
906 portname);
907 goto err_dev_open;
908 }
909
57459185
JP
910 err = vlan_vids_add_by_dev(port_dev, dev);
911 if (err) {
912 netdev_err(dev, "Failed to add vlan ids to device %s\n",
913 portname);
914 goto err_vids_add;
915 }
916
bd2d0837 917 if (team_netpoll_info(team)) {
47be03a2 918 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
bd2d0837
JP
919 if (err) {
920 netdev_err(dev, "Failed to enable netpoll on device %s\n",
921 portname);
922 goto err_enable_netpoll;
923 }
924 }
925
3d249d4c
JP
926 err = netdev_set_master(port_dev, dev);
927 if (err) {
928 netdev_err(dev, "Device %s failed to set master\n", portname);
929 goto err_set_master;
930 }
931
932 err = netdev_rx_handler_register(port_dev, team_handle_frame,
933 port);
934 if (err) {
935 netdev_err(dev, "Device %s failed to register rx_handler\n",
936 portname);
937 goto err_handler_register;
938 }
939
35b384bd 940 err = __team_option_inst_add_port(team, port);
80f7c668
JP
941 if (err) {
942 netdev_err(dev, "Device %s failed to add per-port options\n",
943 portname);
944 goto err_option_port_add;
945 }
946
19a0b58e
JP
947 port->index = -1;
948 team_port_enable(team, port);
949 list_add_tail_rcu(&port->list, &team->port_list);
3d249d4c 950 __team_compute_features(team);
0b121fd2 951 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
35b384bd 952 __team_options_change_check(team);
3d249d4c
JP
953
954 netdev_info(dev, "Port device %s added\n", portname);
955
956 return 0;
957
80f7c668
JP
958err_option_port_add:
959 netdev_rx_handler_unregister(port_dev);
960
3d249d4c
JP
961err_handler_register:
962 netdev_set_master(port_dev, NULL);
963
964err_set_master:
bd2d0837
JP
965 team_port_disable_netpoll(port);
966
967err_enable_netpoll:
57459185
JP
968 vlan_vids_del_by_dev(port_dev, dev);
969
970err_vids_add:
3d249d4c
JP
971 dev_close(port_dev);
972
973err_dev_open:
974 team_port_leave(team, port);
975 team_port_set_orig_mac(port);
976
977err_port_enter:
978 dev_set_mtu(port_dev, port->orig.mtu);
979
980err_set_mtu:
981 kfree(port);
982
983 return err;
984}
985
0b121fd2
JP
986static void __team_port_change_port_removed(struct team_port *port);
987
3d249d4c
JP
988static int team_port_del(struct team *team, struct net_device *port_dev)
989{
990 struct net_device *dev = team->dev;
991 struct team_port *port;
992 char *portname = port_dev->name;
993
994 port = team_port_get_rtnl(port_dev);
995 if (!port || !team_port_find(team, port)) {
996 netdev_err(dev, "Device %s does not act as a port of this team\n",
997 portname);
998 return -ENOENT;
999 }
1000
35b384bd
JP
1001 __team_option_inst_mark_removed_port(team, port);
1002 __team_options_change_check(team);
1003 __team_option_inst_del_port(team, port);
0b121fd2 1004 __team_port_change_port_removed(port);
19a0b58e
JP
1005 team_port_disable(team, port);
1006 list_del_rcu(&port->list);
3d249d4c
JP
1007 netdev_rx_handler_unregister(port_dev);
1008 netdev_set_master(port_dev, NULL);
bd2d0837 1009 team_port_disable_netpoll(port);
57459185 1010 vlan_vids_del_by_dev(port_dev, dev);
3d249d4c
JP
1011 dev_close(port_dev);
1012 team_port_leave(team, port);
1013 team_port_set_orig_mac(port);
1014 dev_set_mtu(port_dev, port->orig.mtu);
1015 synchronize_rcu();
1016 kfree(port);
1017 netdev_info(dev, "Port device %s removed\n", portname);
1018 __team_compute_features(team);
1019
1020 return 0;
1021}
1022
1023
1024/*****************
1025 * Net device ops
1026 *****************/
1027
80f7c668 1028static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 1029{
d299cd51 1030 ctx->data.str_val = team->mode->kind;
3d249d4c
JP
1031 return 0;
1032}
1033
80f7c668 1034static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
3d249d4c 1035{
80f7c668 1036 return team_change_mode(team, ctx->data.str_val);
3d249d4c
JP
1037}
1038
acd69962
JP
1039static int team_port_en_option_get(struct team *team,
1040 struct team_gsetter_ctx *ctx)
1041{
85d59a87
JP
1042 struct team_port *port = ctx->info->port;
1043
1044 ctx->data.bool_val = team_port_enabled(port);
acd69962
JP
1045 return 0;
1046}
1047
1048static int team_port_en_option_set(struct team *team,
1049 struct team_gsetter_ctx *ctx)
1050{
85d59a87
JP
1051 struct team_port *port = ctx->info->port;
1052
acd69962 1053 if (ctx->data.bool_val)
85d59a87 1054 team_port_enable(team, port);
acd69962 1055 else
85d59a87 1056 team_port_disable(team, port);
acd69962
JP
1057 return 0;
1058}
1059
71472ec1
JP
1060static int team_user_linkup_option_get(struct team *team,
1061 struct team_gsetter_ctx *ctx)
1062{
85d59a87
JP
1063 struct team_port *port = ctx->info->port;
1064
1065 ctx->data.bool_val = port->user.linkup;
71472ec1
JP
1066 return 0;
1067}
1068
1069static int team_user_linkup_option_set(struct team *team,
1070 struct team_gsetter_ctx *ctx)
1071{
85d59a87
JP
1072 struct team_port *port = ctx->info->port;
1073
1074 port->user.linkup = ctx->data.bool_val;
1075 team_refresh_port_linkup(port);
71472ec1
JP
1076 return 0;
1077}
1078
1079static int team_user_linkup_en_option_get(struct team *team,
1080 struct team_gsetter_ctx *ctx)
1081{
85d59a87 1082 struct team_port *port = ctx->info->port;
71472ec1
JP
1083
1084 ctx->data.bool_val = port->user.linkup_enabled;
1085 return 0;
1086}
1087
1088static int team_user_linkup_en_option_set(struct team *team,
1089 struct team_gsetter_ctx *ctx)
1090{
85d59a87 1091 struct team_port *port = ctx->info->port;
71472ec1
JP
1092
1093 port->user.linkup_enabled = ctx->data.bool_val;
85d59a87 1094 team_refresh_port_linkup(port);
71472ec1
JP
1095 return 0;
1096}
1097
358b8382 1098static const struct team_option team_options[] = {
3d249d4c
JP
1099 {
1100 .name = "mode",
1101 .type = TEAM_OPTION_TYPE_STRING,
1102 .getter = team_mode_option_get,
1103 .setter = team_mode_option_set,
1104 },
acd69962
JP
1105 {
1106 .name = "enabled",
1107 .type = TEAM_OPTION_TYPE_BOOL,
1108 .per_port = true,
1109 .getter = team_port_en_option_get,
1110 .setter = team_port_en_option_set,
1111 },
71472ec1
JP
1112 {
1113 .name = "user_linkup",
1114 .type = TEAM_OPTION_TYPE_BOOL,
1115 .per_port = true,
1116 .getter = team_user_linkup_option_get,
1117 .setter = team_user_linkup_option_set,
1118 },
1119 {
1120 .name = "user_linkup_enabled",
1121 .type = TEAM_OPTION_TYPE_BOOL,
1122 .per_port = true,
1123 .getter = team_user_linkup_en_option_get,
1124 .setter = team_user_linkup_en_option_set,
1125 },
3d249d4c
JP
1126};
1127
6c85f2bd
JP
1128static struct lock_class_key team_netdev_xmit_lock_key;
1129static struct lock_class_key team_netdev_addr_lock_key;
1130
1131static void team_set_lockdep_class_one(struct net_device *dev,
1132 struct netdev_queue *txq,
1133 void *unused)
1134{
1135 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1136}
1137
1138static void team_set_lockdep_class(struct net_device *dev)
1139{
1140 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1141 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1142}
1143
3d249d4c
JP
1144static int team_init(struct net_device *dev)
1145{
1146 struct team *team = netdev_priv(dev);
1147 int i;
358b8382 1148 int err;
3d249d4c
JP
1149
1150 team->dev = dev;
61dc3461 1151 mutex_init(&team->lock);
d299cd51 1152 team_set_no_mode(team);
3d249d4c
JP
1153
1154 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1155 if (!team->pcpu_stats)
1156 return -ENOMEM;
1157
1158 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
19a0b58e 1159 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
3d249d4c
JP
1160 INIT_LIST_HEAD(&team->port_list);
1161
1162 team_adjust_ops(team);
1163
1164 INIT_LIST_HEAD(&team->option_list);
80f7c668 1165 INIT_LIST_HEAD(&team->option_inst_list);
358b8382
JP
1166 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1167 if (err)
1168 goto err_options_register;
3d249d4c
JP
1169 netif_carrier_off(dev);
1170
6c85f2bd
JP
1171 team_set_lockdep_class(dev);
1172
3d249d4c 1173 return 0;
358b8382
JP
1174
1175err_options_register:
1176 free_percpu(team->pcpu_stats);
1177
1178 return err;
3d249d4c
JP
1179}
1180
1181static void team_uninit(struct net_device *dev)
1182{
1183 struct team *team = netdev_priv(dev);
1184 struct team_port *port;
1185 struct team_port *tmp;
1186
61dc3461 1187 mutex_lock(&team->lock);
3d249d4c
JP
1188 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1189 team_port_del(team, port->dev);
1190
1191 __team_change_mode(team, NULL); /* cleanup */
1192 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
61dc3461 1193 mutex_unlock(&team->lock);
3d249d4c
JP
1194}
1195
1196static void team_destructor(struct net_device *dev)
1197{
1198 struct team *team = netdev_priv(dev);
1199
1200 free_percpu(team->pcpu_stats);
1201 free_netdev(dev);
1202}
1203
1204static int team_open(struct net_device *dev)
1205{
1206 netif_carrier_on(dev);
1207 return 0;
1208}
1209
1210static int team_close(struct net_device *dev)
1211{
1212 netif_carrier_off(dev);
1213 return 0;
1214}
1215
1216/*
1217 * note: already called with rcu_read_lock
1218 */
1219static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1220{
1221 struct team *team = netdev_priv(dev);
1222 bool tx_success = false;
1223 unsigned int len = skb->len;
1224
1225 tx_success = team->ops.transmit(team, skb);
1226 if (tx_success) {
1227 struct team_pcpu_stats *pcpu_stats;
1228
1229 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1230 u64_stats_update_begin(&pcpu_stats->syncp);
1231 pcpu_stats->tx_packets++;
1232 pcpu_stats->tx_bytes += len;
1233 u64_stats_update_end(&pcpu_stats->syncp);
1234 } else {
1235 this_cpu_inc(team->pcpu_stats->tx_dropped);
1236 }
1237
1238 return NETDEV_TX_OK;
1239}
1240
6c85f2bd
JP
1241static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1242{
1243 /*
1244 * This helper function exists to help dev_pick_tx get the correct
1245 * destination queue. Using a helper function skips a call to
1246 * skb_tx_hash and will put the skbs in the queue we expect on their
1247 * way down to the team driver.
1248 */
1249 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1250
1251 /*
1252 * Save the original txq to restore before passing to the driver
1253 */
1254 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1255
1256 if (unlikely(txq >= dev->real_num_tx_queues)) {
1257 do {
1258 txq -= dev->real_num_tx_queues;
1259 } while (txq >= dev->real_num_tx_queues);
1260 }
1261 return txq;
1262}
1263
3d249d4c
JP
1264static void team_change_rx_flags(struct net_device *dev, int change)
1265{
1266 struct team *team = netdev_priv(dev);
1267 struct team_port *port;
1268 int inc;
1269
1270 rcu_read_lock();
1271 list_for_each_entry_rcu(port, &team->port_list, list) {
1272 if (change & IFF_PROMISC) {
1273 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1274 dev_set_promiscuity(port->dev, inc);
1275 }
1276 if (change & IFF_ALLMULTI) {
1277 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1278 dev_set_allmulti(port->dev, inc);
1279 }
1280 }
1281 rcu_read_unlock();
1282}
1283
1284static void team_set_rx_mode(struct net_device *dev)
1285{
1286 struct team *team = netdev_priv(dev);
1287 struct team_port *port;
1288
1289 rcu_read_lock();
1290 list_for_each_entry_rcu(port, &team->port_list, list) {
1291 dev_uc_sync(port->dev, dev);
1292 dev_mc_sync(port->dev, dev);
1293 }
1294 rcu_read_unlock();
1295}
1296
1297static int team_set_mac_address(struct net_device *dev, void *p)
1298{
1299 struct team *team = netdev_priv(dev);
1300 struct team_port *port;
5a1d1c8c 1301 int err;
3d249d4c 1302
5a1d1c8c
JP
1303 err = eth_mac_addr(dev, p);
1304 if (err)
1305 return err;
3d249d4c
JP
1306 rcu_read_lock();
1307 list_for_each_entry_rcu(port, &team->port_list, list)
1308 if (team->ops.port_change_mac)
1309 team->ops.port_change_mac(team, port);
1310 rcu_read_unlock();
1311 return 0;
1312}
1313
1314static int team_change_mtu(struct net_device *dev, int new_mtu)
1315{
1316 struct team *team = netdev_priv(dev);
1317 struct team_port *port;
1318 int err;
1319
1320 /*
1321 * Alhough this is reader, it's guarded by team lock. It's not possible
1322 * to traverse list in reverse under rcu_read_lock
1323 */
61dc3461 1324 mutex_lock(&team->lock);
3d249d4c
JP
1325 list_for_each_entry(port, &team->port_list, list) {
1326 err = dev_set_mtu(port->dev, new_mtu);
1327 if (err) {
1328 netdev_err(dev, "Device %s failed to change mtu",
1329 port->dev->name);
1330 goto unwind;
1331 }
1332 }
61dc3461 1333 mutex_unlock(&team->lock);
3d249d4c
JP
1334
1335 dev->mtu = new_mtu;
1336
1337 return 0;
1338
1339unwind:
1340 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1341 dev_set_mtu(port->dev, dev->mtu);
61dc3461 1342 mutex_unlock(&team->lock);
3d249d4c
JP
1343
1344 return err;
1345}
1346
1347static struct rtnl_link_stats64 *
1348team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1349{
1350 struct team *team = netdev_priv(dev);
1351 struct team_pcpu_stats *p;
1352 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1353 u32 rx_dropped = 0, tx_dropped = 0;
1354 unsigned int start;
1355 int i;
1356
1357 for_each_possible_cpu(i) {
1358 p = per_cpu_ptr(team->pcpu_stats, i);
1359 do {
1360 start = u64_stats_fetch_begin_bh(&p->syncp);
1361 rx_packets = p->rx_packets;
1362 rx_bytes = p->rx_bytes;
1363 rx_multicast = p->rx_multicast;
1364 tx_packets = p->tx_packets;
1365 tx_bytes = p->tx_bytes;
1366 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1367
1368 stats->rx_packets += rx_packets;
1369 stats->rx_bytes += rx_bytes;
1370 stats->multicast += rx_multicast;
1371 stats->tx_packets += tx_packets;
1372 stats->tx_bytes += tx_bytes;
1373 /*
1374 * rx_dropped & tx_dropped are u32, updated
1375 * without syncp protection.
1376 */
1377 rx_dropped += p->rx_dropped;
1378 tx_dropped += p->tx_dropped;
1379 }
1380 stats->rx_dropped = rx_dropped;
1381 stats->tx_dropped = tx_dropped;
1382 return stats;
1383}
1384
8e586137 1385static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1386{
1387 struct team *team = netdev_priv(dev);
1388 struct team_port *port;
87002b03 1389 int err;
3d249d4c 1390
87002b03
JP
1391 /*
1392 * Alhough this is reader, it's guarded by team lock. It's not possible
1393 * to traverse list in reverse under rcu_read_lock
1394 */
1395 mutex_lock(&team->lock);
1396 list_for_each_entry(port, &team->port_list, list) {
1397 err = vlan_vid_add(port->dev, vid);
1398 if (err)
1399 goto unwind;
3d249d4c 1400 }
87002b03 1401 mutex_unlock(&team->lock);
8e586137
JP
1402
1403 return 0;
87002b03
JP
1404
1405unwind:
1406 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1407 vlan_vid_del(port->dev, vid);
1408 mutex_unlock(&team->lock);
1409
1410 return err;
3d249d4c
JP
1411}
1412
8e586137 1413static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
3d249d4c
JP
1414{
1415 struct team *team = netdev_priv(dev);
1416 struct team_port *port;
1417
1418 rcu_read_lock();
87002b03
JP
1419 list_for_each_entry_rcu(port, &team->port_list, list)
1420 vlan_vid_del(port->dev, vid);
3d249d4c 1421 rcu_read_unlock();
8e586137
JP
1422
1423 return 0;
3d249d4c
JP
1424}
1425
bd2d0837
JP
1426#ifdef CONFIG_NET_POLL_CONTROLLER
1427static void team_poll_controller(struct net_device *dev)
1428{
1429}
1430
1431static void __team_netpoll_cleanup(struct team *team)
1432{
1433 struct team_port *port;
1434
1435 list_for_each_entry(port, &team->port_list, list)
1436 team_port_disable_netpoll(port);
1437}
1438
1439static void team_netpoll_cleanup(struct net_device *dev)
1440{
1441 struct team *team = netdev_priv(dev);
1442
1443 mutex_lock(&team->lock);
1444 __team_netpoll_cleanup(team);
1445 mutex_unlock(&team->lock);
1446}
1447
1448static int team_netpoll_setup(struct net_device *dev,
47be03a2 1449 struct netpoll_info *npifo, gfp_t gfp)
bd2d0837
JP
1450{
1451 struct team *team = netdev_priv(dev);
1452 struct team_port *port;
3324d024 1453 int err = 0;
bd2d0837
JP
1454
1455 mutex_lock(&team->lock);
1456 list_for_each_entry(port, &team->port_list, list) {
47be03a2 1457 err = team_port_enable_netpoll(team, port, gfp);
bd2d0837
JP
1458 if (err) {
1459 __team_netpoll_cleanup(team);
1460 break;
1461 }
1462 }
1463 mutex_unlock(&team->lock);
1464 return err;
1465}
1466#endif
1467
3d249d4c
JP
1468static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1469{
1470 struct team *team = netdev_priv(dev);
1471 int err;
1472
61dc3461 1473 mutex_lock(&team->lock);
3d249d4c 1474 err = team_port_add(team, port_dev);
61dc3461 1475 mutex_unlock(&team->lock);
3d249d4c
JP
1476 return err;
1477}
1478
1479static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1480{
1481 struct team *team = netdev_priv(dev);
1482 int err;
1483
61dc3461 1484 mutex_lock(&team->lock);
3d249d4c 1485 err = team_port_del(team, port_dev);
61dc3461 1486 mutex_unlock(&team->lock);
3d249d4c
JP
1487 return err;
1488}
1489
234a8fd4
JP
1490static netdev_features_t team_fix_features(struct net_device *dev,
1491 netdev_features_t features)
1492{
1493 struct team_port *port;
1494 struct team *team = netdev_priv(dev);
1495 netdev_features_t mask;
1496
1497 mask = features;
1498 features &= ~NETIF_F_ONE_FOR_ALL;
1499 features |= NETIF_F_ALL_FOR_ALL;
1500
1501 rcu_read_lock();
1502 list_for_each_entry_rcu(port, &team->port_list, list) {
1503 features = netdev_increment_features(features,
1504 port->dev->features,
1505 mask);
1506 }
1507 rcu_read_unlock();
1508 return features;
1509}
1510
3d249d4c
JP
1511static const struct net_device_ops team_netdev_ops = {
1512 .ndo_init = team_init,
1513 .ndo_uninit = team_uninit,
1514 .ndo_open = team_open,
1515 .ndo_stop = team_close,
1516 .ndo_start_xmit = team_xmit,
6c85f2bd 1517 .ndo_select_queue = team_select_queue,
3d249d4c
JP
1518 .ndo_change_rx_flags = team_change_rx_flags,
1519 .ndo_set_rx_mode = team_set_rx_mode,
1520 .ndo_set_mac_address = team_set_mac_address,
1521 .ndo_change_mtu = team_change_mtu,
1522 .ndo_get_stats64 = team_get_stats64,
1523 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1524 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
bd2d0837
JP
1525#ifdef CONFIG_NET_POLL_CONTROLLER
1526 .ndo_poll_controller = team_poll_controller,
1527 .ndo_netpoll_setup = team_netpoll_setup,
1528 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1529#endif
3d249d4c
JP
1530 .ndo_add_slave = team_add_slave,
1531 .ndo_del_slave = team_del_slave,
234a8fd4 1532 .ndo_fix_features = team_fix_features,
3d249d4c
JP
1533};
1534
1535
1536/***********************
1537 * rt netlink interface
1538 ***********************/
1539
1540static void team_setup(struct net_device *dev)
1541{
1542 ether_setup(dev);
1543
1544 dev->netdev_ops = &team_netdev_ops;
1545 dev->destructor = team_destructor;
1546 dev->tx_queue_len = 0;
1547 dev->flags |= IFF_MULTICAST;
1548 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1549
1550 /*
1551 * Indicate we support unicast address filtering. That way core won't
1552 * bring us to promisc mode in case a unicast addr is added.
1553 * Let this up to underlay drivers.
1554 */
5a1d1c8c 1555 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
3d249d4c
JP
1556
1557 dev->features |= NETIF_F_LLTX;
1558 dev->features |= NETIF_F_GRO;
1559 dev->hw_features = NETIF_F_HW_VLAN_TX |
1560 NETIF_F_HW_VLAN_RX |
1561 NETIF_F_HW_VLAN_FILTER;
1562
1563 dev->features |= dev->hw_features;
1564}
1565
1566static int team_newlink(struct net *src_net, struct net_device *dev,
1567 struct nlattr *tb[], struct nlattr *data[])
1568{
1569 int err;
1570
1571 if (tb[IFLA_ADDRESS] == NULL)
7ce5d222 1572 eth_hw_addr_random(dev);
3d249d4c
JP
1573
1574 err = register_netdevice(dev);
1575 if (err)
1576 return err;
1577
1578 return 0;
1579}
1580
1581static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1582{
1583 if (tb[IFLA_ADDRESS]) {
1584 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1585 return -EINVAL;
1586 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1587 return -EADDRNOTAVAIL;
1588 }
1589 return 0;
1590}
1591
6c85f2bd
JP
1592static unsigned int team_get_num_tx_queues(void)
1593{
1594 return TEAM_DEFAULT_NUM_TX_QUEUES;
1595}
1596
1597static unsigned int team_get_num_rx_queues(void)
1598{
1599 return TEAM_DEFAULT_NUM_RX_QUEUES;
1600}
1601
3d249d4c 1602static struct rtnl_link_ops team_link_ops __read_mostly = {
6c85f2bd
JP
1603 .kind = DRV_NAME,
1604 .priv_size = sizeof(struct team),
1605 .setup = team_setup,
1606 .newlink = team_newlink,
1607 .validate = team_validate,
1608 .get_num_tx_queues = team_get_num_tx_queues,
1609 .get_num_rx_queues = team_get_num_rx_queues,
3d249d4c
JP
1610};
1611
1612
1613/***********************************
1614 * Generic netlink custom interface
1615 ***********************************/
1616
1617static struct genl_family team_nl_family = {
1618 .id = GENL_ID_GENERATE,
1619 .name = TEAM_GENL_NAME,
1620 .version = TEAM_GENL_VERSION,
1621 .maxattr = TEAM_ATTR_MAX,
1622 .netnsok = true,
1623};
1624
1625static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1626 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1627 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1628 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1629 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1630};
1631
1632static const struct nla_policy
1633team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1634 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1635 [TEAM_ATTR_OPTION_NAME] = {
1636 .type = NLA_STRING,
1637 .len = TEAM_STRING_MAX_LEN,
1638 },
1639 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1640 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2615598f 1641 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
3d249d4c
JP
1642};
1643
1644static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1645{
1646 struct sk_buff *msg;
1647 void *hdr;
1648 int err;
1649
58050fce 1650 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
1651 if (!msg)
1652 return -ENOMEM;
1653
1654 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1655 &team_nl_family, 0, TEAM_CMD_NOOP);
1656 if (IS_ERR(hdr)) {
1657 err = PTR_ERR(hdr);
1658 goto err_msg_put;
1659 }
1660
1661 genlmsg_end(msg, hdr);
1662
1663 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1664
1665err_msg_put:
1666 nlmsg_free(msg);
1667
1668 return err;
1669}
1670
1671/*
1672 * Netlink cmd functions should be locked by following two functions.
8c0713a5 1673 * Since dev gets held here, that ensures dev won't disappear in between.
3d249d4c
JP
1674 */
1675static struct team *team_nl_team_get(struct genl_info *info)
1676{
1677 struct net *net = genl_info_net(info);
1678 int ifindex;
1679 struct net_device *dev;
1680 struct team *team;
1681
1682 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1683 return NULL;
1684
1685 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
8c0713a5 1686 dev = dev_get_by_index(net, ifindex);
3d249d4c 1687 if (!dev || dev->netdev_ops != &team_netdev_ops) {
8c0713a5
JP
1688 if (dev)
1689 dev_put(dev);
3d249d4c
JP
1690 return NULL;
1691 }
1692
1693 team = netdev_priv(dev);
61dc3461 1694 mutex_lock(&team->lock);
3d249d4c
JP
1695 return team;
1696}
1697
1698static void team_nl_team_put(struct team *team)
1699{
61dc3461 1700 mutex_unlock(&team->lock);
8c0713a5 1701 dev_put(team->dev);
3d249d4c
JP
1702}
1703
1704static int team_nl_send_generic(struct genl_info *info, struct team *team,
1705 int (*fill_func)(struct sk_buff *skb,
1706 struct genl_info *info,
1707 int flags, struct team *team))
1708{
1709 struct sk_buff *skb;
1710 int err;
1711
58050fce 1712 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
1713 if (!skb)
1714 return -ENOMEM;
1715
1716 err = fill_func(skb, info, NLM_F_ACK, team);
1717 if (err < 0)
1718 goto err_fill;
1719
1720 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1721 return err;
1722
1723err_fill:
1724 nlmsg_free(skb);
1725 return err;
1726}
1727
9b00cf2d
JP
1728typedef int team_nl_send_func_t(struct sk_buff *skb,
1729 struct team *team, u32 pid);
1730
1731static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid)
1732{
1733 return genlmsg_unicast(dev_net(team->dev), skb, pid);
1734}
1735
01048d9a
JP
1736static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1737 struct team_option_inst *opt_inst)
1738{
1739 struct nlattr *option_item;
1740 struct team_option *option = opt_inst->option;
9b00cf2d 1741 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
01048d9a
JP
1742 struct team_gsetter_ctx ctx;
1743 int err;
1744
9b00cf2d
JP
1745 ctx.info = opt_inst_info;
1746 err = team_option_get(team, opt_inst, &ctx);
1747 if (err)
1748 return err;
1749
01048d9a
JP
1750 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1751 if (!option_item)
9b00cf2d 1752 return -EMSGSIZE;
01048d9a 1753
9b00cf2d
JP
1754 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1755 goto nest_cancel;
01048d9a
JP
1756 if (opt_inst_info->port &&
1757 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1758 opt_inst_info->port->dev->ifindex))
9b00cf2d 1759 goto nest_cancel;
01048d9a
JP
1760 if (opt_inst->option->array_size &&
1761 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1762 opt_inst_info->array_index))
9b00cf2d 1763 goto nest_cancel;
01048d9a
JP
1764
1765 switch (option->type) {
1766 case TEAM_OPTION_TYPE_U32:
1767 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
9b00cf2d 1768 goto nest_cancel;
01048d9a 1769 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
9b00cf2d 1770 goto nest_cancel;
01048d9a
JP
1771 break;
1772 case TEAM_OPTION_TYPE_STRING:
1773 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
9b00cf2d 1774 goto nest_cancel;
01048d9a
JP
1775 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
1776 ctx.data.str_val))
9b00cf2d 1777 goto nest_cancel;
01048d9a
JP
1778 break;
1779 case TEAM_OPTION_TYPE_BINARY:
1780 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
9b00cf2d 1781 goto nest_cancel;
01048d9a
JP
1782 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
1783 ctx.data.bin_val.ptr))
9b00cf2d 1784 goto nest_cancel;
01048d9a
JP
1785 break;
1786 case TEAM_OPTION_TYPE_BOOL:
1787 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
9b00cf2d 1788 goto nest_cancel;
01048d9a
JP
1789 if (ctx.data.bool_val &&
1790 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
9b00cf2d 1791 goto nest_cancel;
01048d9a
JP
1792 break;
1793 default:
1794 BUG();
1795 }
9b00cf2d
JP
1796 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
1797 goto nest_cancel;
1798 if (opt_inst->changed) {
1799 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
1800 goto nest_cancel;
1801 opt_inst->changed = false;
1802 }
01048d9a
JP
1803 nla_nest_end(skb, option_item);
1804 return 0;
1805
9b00cf2d
JP
1806nest_cancel:
1807 nla_nest_cancel(skb, option_item);
1808 return -EMSGSIZE;
1809}
1810
1811static int __send_and_alloc_skb(struct sk_buff **pskb,
1812 struct team *team, u32 pid,
1813 team_nl_send_func_t *send_func)
1814{
1815 int err;
1816
1817 if (*pskb) {
1818 err = send_func(*pskb, team, pid);
1819 if (err)
1820 return err;
1821 }
58050fce 1822 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
9b00cf2d
JP
1823 if (!*pskb)
1824 return -ENOMEM;
1825 return 0;
01048d9a
JP
1826}
1827
9b00cf2d
JP
1828static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq,
1829 int flags, team_nl_send_func_t *send_func,
01048d9a 1830 struct list_head *sel_opt_inst_list)
3d249d4c
JP
1831{
1832 struct nlattr *option_list;
9b00cf2d 1833 struct nlmsghdr *nlh;
3d249d4c 1834 void *hdr;
80f7c668
JP
1835 struct team_option_inst *opt_inst;
1836 int err;
9b00cf2d
JP
1837 struct sk_buff *skb = NULL;
1838 bool incomplete;
1839 int i;
3d249d4c 1840
9b00cf2d
JP
1841 opt_inst = list_first_entry(sel_opt_inst_list,
1842 struct team_option_inst, tmp_list);
1843
1844start_again:
1845 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1846 if (err)
1847 return err;
1848
1849 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI,
3d249d4c
JP
1850 TEAM_CMD_OPTIONS_GET);
1851 if (IS_ERR(hdr))
1852 return PTR_ERR(hdr);
1853
86ebb02d
DM
1854 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
1855 goto nla_put_failure;
3d249d4c
JP
1856 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1857 if (!option_list)
75db986a 1858 goto nla_put_failure;
3d249d4c 1859
9b00cf2d
JP
1860 i = 0;
1861 incomplete = false;
1862 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
01048d9a 1863 err = team_nl_fill_one_option_get(skb, team, opt_inst);
9b00cf2d
JP
1864 if (err) {
1865 if (err == -EMSGSIZE) {
1866 if (!i)
1867 goto errout;
1868 incomplete = true;
1869 break;
1870 }
01048d9a 1871 goto errout;
9b00cf2d
JP
1872 }
1873 i++;
3d249d4c
JP
1874 }
1875
1876 nla_nest_end(skb, option_list);
9b00cf2d
JP
1877 genlmsg_end(skb, hdr);
1878 if (incomplete)
1879 goto start_again;
1880
1881send_done:
1882 nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
1883 if (!nlh) {
1884 err = __send_and_alloc_skb(&skb, team, pid, send_func);
1885 if (err)
1886 goto errout;
1887 goto send_done;
1888 }
1889
1890 return send_func(skb, team, pid);
3d249d4c
JP
1891
1892nla_put_failure:
80f7c668
JP
1893 err = -EMSGSIZE;
1894errout:
3d249d4c 1895 genlmsg_cancel(skb, hdr);
9b00cf2d 1896 nlmsg_free(skb);
80f7c668 1897 return err;
3d249d4c
JP
1898}
1899
3d249d4c
JP
1900static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1901{
1902 struct team *team;
9b00cf2d 1903 struct team_option_inst *opt_inst;
3d249d4c 1904 int err;
9b00cf2d 1905 LIST_HEAD(sel_opt_inst_list);
3d249d4c
JP
1906
1907 team = team_nl_team_get(info);
1908 if (!team)
1909 return -EINVAL;
1910
9b00cf2d
JP
1911 list_for_each_entry(opt_inst, &team->option_inst_list, list)
1912 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
1913 err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq,
1914 NLM_F_ACK, team_nl_send_unicast,
1915 &sel_opt_inst_list);
3d249d4c
JP
1916
1917 team_nl_team_put(team);
1918
1919 return err;
1920}
1921
2fcdb2c9
JP
1922static int team_nl_send_event_options_get(struct team *team,
1923 struct list_head *sel_opt_inst_list);
1924
3d249d4c
JP
1925static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1926{
1927 struct team *team;
1928 int err = 0;
1929 int i;
1930 struct nlattr *nl_option;
2fcdb2c9 1931 LIST_HEAD(opt_inst_list);
3d249d4c
JP
1932
1933 team = team_nl_team_get(info);
1934 if (!team)
1935 return -EINVAL;
1936
1937 err = -EINVAL;
1938 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1939 err = -EINVAL;
1940 goto team_put;
1941 }
1942
1943 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
80f7c668 1944 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
b1303326 1945 struct nlattr *attr;
14f066ba 1946 struct nlattr *attr_data;
3d249d4c 1947 enum team_option_type opt_type;
80f7c668 1948 int opt_port_ifindex = 0; /* != 0 for per-port options */
b1303326
JP
1949 u32 opt_array_index = 0;
1950 bool opt_is_array = false;
80f7c668 1951 struct team_option_inst *opt_inst;
3d249d4c
JP
1952 char *opt_name;
1953 bool opt_found = false;
1954
1955 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1956 err = -EINVAL;
1957 goto team_put;
1958 }
80f7c668 1959 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
3d249d4c
JP
1960 nl_option, team_nl_option_policy);
1961 if (err)
1962 goto team_put;
80f7c668 1963 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
14f066ba 1964 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
3d249d4c
JP
1965 err = -EINVAL;
1966 goto team_put;
1967 }
80f7c668 1968 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
3d249d4c
JP
1969 case NLA_U32:
1970 opt_type = TEAM_OPTION_TYPE_U32;
1971 break;
1972 case NLA_STRING:
1973 opt_type = TEAM_OPTION_TYPE_STRING;
1974 break;
2615598f
JP
1975 case NLA_BINARY:
1976 opt_type = TEAM_OPTION_TYPE_BINARY;
1977 break;
14f066ba
JP
1978 case NLA_FLAG:
1979 opt_type = TEAM_OPTION_TYPE_BOOL;
1980 break;
3d249d4c
JP
1981 default:
1982 goto team_put;
1983 }
1984
14f066ba
JP
1985 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
1986 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
1987 err = -EINVAL;
1988 goto team_put;
1989 }
1990
80f7c668 1991 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
b1303326
JP
1992 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
1993 if (attr)
1994 opt_port_ifindex = nla_get_u32(attr);
1995
1996 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
1997 if (attr) {
1998 opt_is_array = true;
1999 opt_array_index = nla_get_u32(attr);
2000 }
80f7c668
JP
2001
2002 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2003 struct team_option *option = opt_inst->option;
80f7c668 2004 struct team_gsetter_ctx ctx;
85d59a87 2005 struct team_option_inst_info *opt_inst_info;
80f7c668 2006 int tmp_ifindex;
3d249d4c 2007
85d59a87
JP
2008 opt_inst_info = &opt_inst->info;
2009 tmp_ifindex = opt_inst_info->port ?
2010 opt_inst_info->port->dev->ifindex : 0;
3d249d4c 2011 if (option->type != opt_type ||
80f7c668 2012 strcmp(option->name, opt_name) ||
b1303326
JP
2013 tmp_ifindex != opt_port_ifindex ||
2014 (option->array_size && !opt_is_array) ||
85d59a87 2015 opt_inst_info->array_index != opt_array_index)
3d249d4c
JP
2016 continue;
2017 opt_found = true;
85d59a87 2018 ctx.info = opt_inst_info;
3d249d4c
JP
2019 switch (opt_type) {
2020 case TEAM_OPTION_TYPE_U32:
14f066ba 2021 ctx.data.u32_val = nla_get_u32(attr_data);
3d249d4c
JP
2022 break;
2023 case TEAM_OPTION_TYPE_STRING:
14f066ba 2024 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2615598f
JP
2025 err = -EINVAL;
2026 goto team_put;
2027 }
14f066ba 2028 ctx.data.str_val = nla_data(attr_data);
3d249d4c 2029 break;
2615598f 2030 case TEAM_OPTION_TYPE_BINARY:
14f066ba
JP
2031 ctx.data.bin_val.len = nla_len(attr_data);
2032 ctx.data.bin_val.ptr = nla_data(attr_data);
2033 break;
2034 case TEAM_OPTION_TYPE_BOOL:
2035 ctx.data.bool_val = attr_data ? true : false;
2615598f 2036 break;
3d249d4c
JP
2037 default:
2038 BUG();
2039 }
80f7c668 2040 err = team_option_set(team, opt_inst, &ctx);
3d249d4c
JP
2041 if (err)
2042 goto team_put;
2fcdb2c9
JP
2043 opt_inst->changed = true;
2044 list_add(&opt_inst->tmp_list, &opt_inst_list);
3d249d4c
JP
2045 }
2046 if (!opt_found) {
2047 err = -ENOENT;
2048 goto team_put;
2049 }
2050 }
2051
2fcdb2c9
JP
2052 err = team_nl_send_event_options_get(team, &opt_inst_list);
2053
3d249d4c
JP
2054team_put:
2055 team_nl_team_put(team);
2056
2057 return err;
2058}
2059
b82b9183
JP
2060static int team_nl_fill_port_list_get(struct sk_buff *skb,
2061 u32 pid, u32 seq, int flags,
2062 struct team *team,
2063 bool fillall)
3d249d4c
JP
2064{
2065 struct nlattr *port_list;
2066 void *hdr;
2067 struct team_port *port;
2068
2069 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
2070 TEAM_CMD_PORT_LIST_GET);
2071 if (IS_ERR(hdr))
2072 return PTR_ERR(hdr);
2073
86ebb02d
DM
2074 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2075 goto nla_put_failure;
3d249d4c
JP
2076 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2077 if (!port_list)
3221c646 2078 goto nla_put_failure;
3d249d4c
JP
2079
2080 list_for_each_entry(port, &team->port_list, list) {
2081 struct nlattr *port_item;
2082
b82b9183
JP
2083 /* Include only changed ports if fill all mode is not on */
2084 if (!fillall && !port->changed)
2085 continue;
3d249d4c
JP
2086 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2087 if (!port_item)
2088 goto nla_put_failure;
86ebb02d
DM
2089 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2090 goto nla_put_failure;
b82b9183 2091 if (port->changed) {
86ebb02d
DM
2092 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2093 goto nla_put_failure;
b82b9183
JP
2094 port->changed = false;
2095 }
86ebb02d
DM
2096 if ((port->removed &&
2097 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
71472ec1 2098 (port->state.linkup &&
86ebb02d 2099 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
71472ec1
JP
2100 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2101 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
86ebb02d 2102 goto nla_put_failure;
3d249d4c
JP
2103 nla_nest_end(skb, port_item);
2104 }
2105
2106 nla_nest_end(skb, port_list);
2107 return genlmsg_end(skb, hdr);
2108
2109nla_put_failure:
2110 genlmsg_cancel(skb, hdr);
2111 return -EMSGSIZE;
2112}
2113
b82b9183
JP
2114static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
2115 struct genl_info *info, int flags,
2116 struct team *team)
3d249d4c 2117{
b82b9183
JP
2118 return team_nl_fill_port_list_get(skb, info->snd_pid,
2119 info->snd_seq, NLM_F_ACK,
2120 team, true);
3d249d4c
JP
2121}
2122
2123static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2124 struct genl_info *info)
2125{
2126 struct team *team;
2127 int err;
2128
2129 team = team_nl_team_get(info);
2130 if (!team)
2131 return -EINVAL;
2132
b82b9183 2133 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
3d249d4c
JP
2134
2135 team_nl_team_put(team);
2136
2137 return err;
2138}
2139
2140static struct genl_ops team_nl_ops[] = {
2141 {
2142 .cmd = TEAM_CMD_NOOP,
2143 .doit = team_nl_cmd_noop,
2144 .policy = team_nl_policy,
2145 },
2146 {
2147 .cmd = TEAM_CMD_OPTIONS_SET,
2148 .doit = team_nl_cmd_options_set,
2149 .policy = team_nl_policy,
2150 .flags = GENL_ADMIN_PERM,
2151 },
2152 {
2153 .cmd = TEAM_CMD_OPTIONS_GET,
2154 .doit = team_nl_cmd_options_get,
2155 .policy = team_nl_policy,
2156 .flags = GENL_ADMIN_PERM,
2157 },
2158 {
2159 .cmd = TEAM_CMD_PORT_LIST_GET,
2160 .doit = team_nl_cmd_port_list_get,
2161 .policy = team_nl_policy,
2162 .flags = GENL_ADMIN_PERM,
2163 },
2164};
2165
2166static struct genl_multicast_group team_change_event_mcgrp = {
2167 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2168};
2169
9b00cf2d
JP
2170static int team_nl_send_multicast(struct sk_buff *skb,
2171 struct team *team, u32 pid)
2172{
2173 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2174 team_change_event_mcgrp.id, GFP_KERNEL);
2175}
2176
01048d9a
JP
2177static int team_nl_send_event_options_get(struct team *team,
2178 struct list_head *sel_opt_inst_list)
3d249d4c 2179{
9b00cf2d
JP
2180 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2181 sel_opt_inst_list);
3d249d4c
JP
2182}
2183
b82b9183 2184static int team_nl_send_event_port_list_get(struct team *team)
3d249d4c
JP
2185{
2186 struct sk_buff *skb;
2187 int err;
b82b9183 2188 struct net *net = dev_net(team->dev);
3d249d4c 2189
58050fce 2190 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3d249d4c
JP
2191 if (!skb)
2192 return -ENOMEM;
2193
b82b9183 2194 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
3d249d4c
JP
2195 if (err < 0)
2196 goto err_fill;
2197
2198 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2199 GFP_KERNEL);
2200 return err;
2201
2202err_fill:
2203 nlmsg_free(skb);
2204 return err;
2205}
2206
2207static int team_nl_init(void)
2208{
2209 int err;
2210
2211 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2212 ARRAY_SIZE(team_nl_ops));
2213 if (err)
2214 return err;
2215
2216 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2217 if (err)
2218 goto err_change_event_grp_reg;
2219
2220 return 0;
2221
2222err_change_event_grp_reg:
2223 genl_unregister_family(&team_nl_family);
2224
2225 return err;
2226}
2227
2228static void team_nl_fini(void)
2229{
2230 genl_unregister_family(&team_nl_family);
2231}
2232
2233
2234/******************
2235 * Change checkers
2236 ******************/
2237
b82b9183 2238static void __team_options_change_check(struct team *team)
3d249d4c
JP
2239{
2240 int err;
01048d9a
JP
2241 struct team_option_inst *opt_inst;
2242 LIST_HEAD(sel_opt_inst_list);
3d249d4c 2243
01048d9a
JP
2244 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2245 if (opt_inst->changed)
2246 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2247 }
2248 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
3d249d4c 2249 if (err)
9b00cf2d
JP
2250 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2251 err);
3d249d4c
JP
2252}
2253
2254/* rtnl lock is held */
0b121fd2
JP
2255
2256static void __team_port_change_send(struct team_port *port, bool linkup)
3d249d4c
JP
2257{
2258 int err;
2259
b82b9183 2260 port->changed = true;
71472ec1
JP
2261 port->state.linkup = linkup;
2262 team_refresh_port_linkup(port);
3d249d4c
JP
2263 if (linkup) {
2264 struct ethtool_cmd ecmd;
2265
2266 err = __ethtool_get_settings(port->dev, &ecmd);
2267 if (!err) {
71472ec1
JP
2268 port->state.speed = ethtool_cmd_speed(&ecmd);
2269 port->state.duplex = ecmd.duplex;
3d249d4c
JP
2270 goto send_event;
2271 }
2272 }
71472ec1
JP
2273 port->state.speed = 0;
2274 port->state.duplex = 0;
3d249d4c
JP
2275
2276send_event:
b82b9183 2277 err = team_nl_send_event_port_list_get(port->team);
3d249d4c
JP
2278 if (err)
2279 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
2280 port->dev->name);
2281
2282}
2283
0b121fd2
JP
2284static void __team_port_change_check(struct team_port *port, bool linkup)
2285{
2286 if (port->state.linkup != linkup)
2287 __team_port_change_send(port, linkup);
2288}
2289
2290static void __team_port_change_port_added(struct team_port *port, bool linkup)
2291{
2292 __team_port_change_send(port, linkup);
2293}
2294
2295static void __team_port_change_port_removed(struct team_port *port)
2296{
2297 port->removed = true;
2298 __team_port_change_send(port, false);
2299}
2300
3d249d4c
JP
2301static void team_port_change_check(struct team_port *port, bool linkup)
2302{
2303 struct team *team = port->team;
2304
61dc3461 2305 mutex_lock(&team->lock);
3d249d4c 2306 __team_port_change_check(port, linkup);
61dc3461 2307 mutex_unlock(&team->lock);
3d249d4c
JP
2308}
2309
0f1aad2b 2310
3d249d4c
JP
2311/************************************
2312 * Net device notifier event handler
2313 ************************************/
2314
2315static int team_device_event(struct notifier_block *unused,
2316 unsigned long event, void *ptr)
2317{
2318 struct net_device *dev = (struct net_device *) ptr;
2319 struct team_port *port;
2320
2321 port = team_port_get_rtnl(dev);
2322 if (!port)
2323 return NOTIFY_DONE;
2324
2325 switch (event) {
2326 case NETDEV_UP:
2327 if (netif_carrier_ok(dev))
2328 team_port_change_check(port, true);
2329 case NETDEV_DOWN:
2330 team_port_change_check(port, false);
2331 case NETDEV_CHANGE:
2332 if (netif_running(port->dev))
2333 team_port_change_check(port,
2334 !!netif_carrier_ok(port->dev));
2335 break;
2336 case NETDEV_UNREGISTER:
2337 team_del_slave(port->team->dev, dev);
2338 break;
2339 case NETDEV_FEAT_CHANGE:
2340 team_compute_features(port->team);
2341 break;
2342 case NETDEV_CHANGEMTU:
2343 /* Forbid to change mtu of underlaying device */
2344 return NOTIFY_BAD;
2345 case NETDEV_PRE_TYPE_CHANGE:
2346 /* Forbid to change type of underlaying device */
2347 return NOTIFY_BAD;
2348 }
2349 return NOTIFY_DONE;
2350}
2351
2352static struct notifier_block team_notifier_block __read_mostly = {
2353 .notifier_call = team_device_event,
2354};
2355
2356
2357/***********************
2358 * Module init and exit
2359 ***********************/
2360
2361static int __init team_module_init(void)
2362{
2363 int err;
2364
2365 register_netdevice_notifier(&team_notifier_block);
2366
2367 err = rtnl_link_register(&team_link_ops);
2368 if (err)
2369 goto err_rtnl_reg;
2370
2371 err = team_nl_init();
2372 if (err)
2373 goto err_nl_init;
2374
2375 return 0;
2376
2377err_nl_init:
2378 rtnl_link_unregister(&team_link_ops);
2379
2380err_rtnl_reg:
2381 unregister_netdevice_notifier(&team_notifier_block);
2382
2383 return err;
2384}
2385
2386static void __exit team_module_exit(void)
2387{
2388 team_nl_fini();
2389 rtnl_link_unregister(&team_link_ops);
2390 unregister_netdevice_notifier(&team_notifier_block);
2391}
2392
2393module_init(team_module_init);
2394module_exit(team_module_exit);
2395
2396MODULE_LICENSE("GPL v2");
2397MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2398MODULE_DESCRIPTION("Ethernet team device driver");
2399MODULE_ALIAS_RTNL_LINK(DRV_NAME);