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