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