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