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