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