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