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