Linux 4.4.145
[linux/fpc-iii.git] / drivers / net / team / team.c
blob49174837c2ba6c049b3c955fc9e2fda6e044e3bb
1 /*
2 * drivers/net/team/team.c - Network team device driver
3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
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 */
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 <net/switchdev.h>
32 #include <generated/utsrelease.h>
33 #include <linux/if_team.h>
35 #define DRV_NAME "team"
38 /**********
39 * Helpers
40 **********/
42 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
44 static struct team_port *team_port_get_rcu(const struct net_device *dev)
46 return rcu_dereference(dev->rx_handler_data);
49 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
53 return team_port_exists(dev) ? port : NULL;
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
60 static int __set_port_dev_addr(struct net_device *port_dev,
61 const unsigned char *dev_addr)
63 struct sockaddr addr;
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);
70 static int team_port_set_orig_dev_addr(struct team_port *port)
72 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
75 static int team_port_set_team_dev_addr(struct team *team,
76 struct team_port *port)
78 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
81 int team_modeop_port_enter(struct team *team, struct team_port *port)
83 return team_port_set_team_dev_addr(team, port);
85 EXPORT_SYMBOL(team_modeop_port_enter);
87 void team_modeop_port_change_dev_addr(struct team *team,
88 struct team_port *port)
90 team_port_set_team_dev_addr(team, port);
92 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
94 static void team_refresh_port_linkup(struct team_port *port)
96 port->linkup = port->user.linkup_enabled ? port->user.linkup :
97 port->state.linkup;
101 /*******************
102 * Options handling
103 *******************/
105 struct team_option_inst { /* One for each option instance */
106 struct list_head list;
107 struct list_head tmp_list;
108 struct team_option *option;
109 struct team_option_inst_info info;
110 bool changed;
111 bool removed;
114 static struct team_option *__team_find_option(struct team *team,
115 const char *opt_name)
117 struct team_option *option;
119 list_for_each_entry(option, &team->option_list, list) {
120 if (strcmp(option->name, opt_name) == 0)
121 return option;
123 return NULL;
126 static void __team_option_inst_del(struct team_option_inst *opt_inst)
128 list_del(&opt_inst->list);
129 kfree(opt_inst);
132 static void __team_option_inst_del_option(struct team *team,
133 struct team_option *option)
135 struct team_option_inst *opt_inst, *tmp;
137 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
138 if (opt_inst->option == option)
139 __team_option_inst_del(opt_inst);
143 static int __team_option_inst_add(struct team *team, struct team_option *option,
144 struct team_port *port)
146 struct team_option_inst *opt_inst;
147 unsigned int array_size;
148 unsigned int i;
149 int err;
151 array_size = option->array_size;
152 if (!array_size)
153 array_size = 1; /* No array but still need one instance */
155 for (i = 0; i < array_size; i++) {
156 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
157 if (!opt_inst)
158 return -ENOMEM;
159 opt_inst->option = option;
160 opt_inst->info.port = port;
161 opt_inst->info.array_index = i;
162 opt_inst->changed = true;
163 opt_inst->removed = false;
164 list_add_tail(&opt_inst->list, &team->option_inst_list);
165 if (option->init) {
166 err = option->init(team, &opt_inst->info);
167 if (err)
168 return err;
172 return 0;
175 static int __team_option_inst_add_option(struct team *team,
176 struct team_option *option)
178 int err;
180 if (!option->per_port) {
181 err = __team_option_inst_add(team, option, NULL);
182 if (err)
183 goto inst_del_option;
185 return 0;
187 inst_del_option:
188 __team_option_inst_del_option(team, option);
189 return err;
192 static void __team_option_inst_mark_removed_option(struct team *team,
193 struct team_option *option)
195 struct team_option_inst *opt_inst;
197 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
198 if (opt_inst->option == option) {
199 opt_inst->changed = true;
200 opt_inst->removed = true;
205 static void __team_option_inst_del_port(struct team *team,
206 struct team_port *port)
208 struct team_option_inst *opt_inst, *tmp;
210 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
211 if (opt_inst->option->per_port &&
212 opt_inst->info.port == port)
213 __team_option_inst_del(opt_inst);
217 static int __team_option_inst_add_port(struct team *team,
218 struct team_port *port)
220 struct team_option *option;
221 int err;
223 list_for_each_entry(option, &team->option_list, list) {
224 if (!option->per_port)
225 continue;
226 err = __team_option_inst_add(team, option, port);
227 if (err)
228 goto inst_del_port;
230 return 0;
232 inst_del_port:
233 __team_option_inst_del_port(team, port);
234 return err;
237 static void __team_option_inst_mark_removed_port(struct team *team,
238 struct team_port *port)
240 struct team_option_inst *opt_inst;
242 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
243 if (opt_inst->info.port == port) {
244 opt_inst->changed = true;
245 opt_inst->removed = true;
250 static bool __team_option_inst_tmp_find(const struct list_head *opts,
251 const struct team_option_inst *needle)
253 struct team_option_inst *opt_inst;
255 list_for_each_entry(opt_inst, opts, tmp_list)
256 if (opt_inst == needle)
257 return true;
258 return false;
261 static int __team_options_register(struct team *team,
262 const struct team_option *option,
263 size_t option_count)
265 int i;
266 struct team_option **dst_opts;
267 int err;
269 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
270 GFP_KERNEL);
271 if (!dst_opts)
272 return -ENOMEM;
273 for (i = 0; i < option_count; i++, option++) {
274 if (__team_find_option(team, option->name)) {
275 err = -EEXIST;
276 goto alloc_rollback;
278 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
279 if (!dst_opts[i]) {
280 err = -ENOMEM;
281 goto alloc_rollback;
285 for (i = 0; i < option_count; i++) {
286 err = __team_option_inst_add_option(team, dst_opts[i]);
287 if (err)
288 goto inst_rollback;
289 list_add_tail(&dst_opts[i]->list, &team->option_list);
292 kfree(dst_opts);
293 return 0;
295 inst_rollback:
296 for (i--; i >= 0; i--)
297 __team_option_inst_del_option(team, dst_opts[i]);
299 i = option_count - 1;
300 alloc_rollback:
301 for (i--; i >= 0; i--)
302 kfree(dst_opts[i]);
304 kfree(dst_opts);
305 return err;
308 static void __team_options_mark_removed(struct team *team,
309 const struct team_option *option,
310 size_t option_count)
312 int i;
314 for (i = 0; i < option_count; i++, option++) {
315 struct team_option *del_opt;
317 del_opt = __team_find_option(team, option->name);
318 if (del_opt)
319 __team_option_inst_mark_removed_option(team, del_opt);
323 static void __team_options_unregister(struct team *team,
324 const struct team_option *option,
325 size_t option_count)
327 int i;
329 for (i = 0; i < option_count; i++, option++) {
330 struct team_option *del_opt;
332 del_opt = __team_find_option(team, option->name);
333 if (del_opt) {
334 __team_option_inst_del_option(team, del_opt);
335 list_del(&del_opt->list);
336 kfree(del_opt);
341 static void __team_options_change_check(struct team *team);
343 int team_options_register(struct team *team,
344 const struct team_option *option,
345 size_t option_count)
347 int err;
349 err = __team_options_register(team, option, option_count);
350 if (err)
351 return err;
352 __team_options_change_check(team);
353 return 0;
355 EXPORT_SYMBOL(team_options_register);
357 void team_options_unregister(struct team *team,
358 const struct team_option *option,
359 size_t option_count)
361 __team_options_mark_removed(team, option, option_count);
362 __team_options_change_check(team);
363 __team_options_unregister(team, option, option_count);
365 EXPORT_SYMBOL(team_options_unregister);
367 static int team_option_get(struct team *team,
368 struct team_option_inst *opt_inst,
369 struct team_gsetter_ctx *ctx)
371 if (!opt_inst->option->getter)
372 return -EOPNOTSUPP;
373 return opt_inst->option->getter(team, ctx);
376 static int team_option_set(struct team *team,
377 struct team_option_inst *opt_inst,
378 struct team_gsetter_ctx *ctx)
380 if (!opt_inst->option->setter)
381 return -EOPNOTSUPP;
382 return opt_inst->option->setter(team, ctx);
385 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
387 struct team_option_inst *opt_inst;
389 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
390 opt_inst->changed = true;
392 EXPORT_SYMBOL(team_option_inst_set_change);
394 void team_options_change_check(struct team *team)
396 __team_options_change_check(team);
398 EXPORT_SYMBOL(team_options_change_check);
401 /****************
402 * Mode handling
403 ****************/
405 static LIST_HEAD(mode_list);
406 static DEFINE_SPINLOCK(mode_list_lock);
408 struct team_mode_item {
409 struct list_head list;
410 const struct team_mode *mode;
413 static struct team_mode_item *__find_mode(const char *kind)
415 struct team_mode_item *mitem;
417 list_for_each_entry(mitem, &mode_list, list) {
418 if (strcmp(mitem->mode->kind, kind) == 0)
419 return mitem;
421 return NULL;
424 static bool is_good_mode_name(const char *name)
426 while (*name != '\0') {
427 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
428 return false;
429 name++;
431 return true;
434 int team_mode_register(const struct team_mode *mode)
436 int err = 0;
437 struct team_mode_item *mitem;
439 if (!is_good_mode_name(mode->kind) ||
440 mode->priv_size > TEAM_MODE_PRIV_SIZE)
441 return -EINVAL;
443 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
444 if (!mitem)
445 return -ENOMEM;
447 spin_lock(&mode_list_lock);
448 if (__find_mode(mode->kind)) {
449 err = -EEXIST;
450 kfree(mitem);
451 goto unlock;
453 mitem->mode = mode;
454 list_add_tail(&mitem->list, &mode_list);
455 unlock:
456 spin_unlock(&mode_list_lock);
457 return err;
459 EXPORT_SYMBOL(team_mode_register);
461 void team_mode_unregister(const struct team_mode *mode)
463 struct team_mode_item *mitem;
465 spin_lock(&mode_list_lock);
466 mitem = __find_mode(mode->kind);
467 if (mitem) {
468 list_del_init(&mitem->list);
469 kfree(mitem);
471 spin_unlock(&mode_list_lock);
473 EXPORT_SYMBOL(team_mode_unregister);
475 static const struct team_mode *team_mode_get(const char *kind)
477 struct team_mode_item *mitem;
478 const struct team_mode *mode = NULL;
480 spin_lock(&mode_list_lock);
481 mitem = __find_mode(kind);
482 if (!mitem) {
483 spin_unlock(&mode_list_lock);
484 request_module("team-mode-%s", kind);
485 spin_lock(&mode_list_lock);
486 mitem = __find_mode(kind);
488 if (mitem) {
489 mode = mitem->mode;
490 if (!try_module_get(mode->owner))
491 mode = NULL;
494 spin_unlock(&mode_list_lock);
495 return mode;
498 static void team_mode_put(const struct team_mode *mode)
500 module_put(mode->owner);
503 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
505 dev_kfree_skb_any(skb);
506 return false;
509 static rx_handler_result_t team_dummy_receive(struct team *team,
510 struct team_port *port,
511 struct sk_buff *skb)
513 return RX_HANDLER_ANOTHER;
516 static const struct team_mode __team_no_mode = {
517 .kind = "*NOMODE*",
520 static bool team_is_mode_set(struct team *team)
522 return team->mode != &__team_no_mode;
525 static void team_set_no_mode(struct team *team)
527 team->user_carrier_enabled = false;
528 team->mode = &__team_no_mode;
531 static void team_adjust_ops(struct team *team)
534 * To avoid checks in rx/tx skb paths, ensure here that non-null and
535 * correct ops are always set.
538 if (!team->en_port_count || !team_is_mode_set(team) ||
539 !team->mode->ops->transmit)
540 team->ops.transmit = team_dummy_transmit;
541 else
542 team->ops.transmit = team->mode->ops->transmit;
544 if (!team->en_port_count || !team_is_mode_set(team) ||
545 !team->mode->ops->receive)
546 team->ops.receive = team_dummy_receive;
547 else
548 team->ops.receive = team->mode->ops->receive;
552 * We can benefit from the fact that it's ensured no port is present
553 * at the time of mode change. Therefore no packets are in fly so there's no
554 * need to set mode operations in any special way.
556 static int __team_change_mode(struct team *team,
557 const struct team_mode *new_mode)
559 /* Check if mode was previously set and do cleanup if so */
560 if (team_is_mode_set(team)) {
561 void (*exit_op)(struct team *team) = team->ops.exit;
563 /* Clear ops area so no callback is called any longer */
564 memset(&team->ops, 0, sizeof(struct team_mode_ops));
565 team_adjust_ops(team);
567 if (exit_op)
568 exit_op(team);
569 team_mode_put(team->mode);
570 team_set_no_mode(team);
571 /* zero private data area */
572 memset(&team->mode_priv, 0,
573 sizeof(struct team) - offsetof(struct team, mode_priv));
576 if (!new_mode)
577 return 0;
579 if (new_mode->ops->init) {
580 int err;
582 err = new_mode->ops->init(team);
583 if (err)
584 return err;
587 team->mode = new_mode;
588 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
589 team_adjust_ops(team);
591 return 0;
594 static int team_change_mode(struct team *team, const char *kind)
596 const struct team_mode *new_mode;
597 struct net_device *dev = team->dev;
598 int err;
600 if (!list_empty(&team->port_list)) {
601 netdev_err(dev, "No ports can be present during mode change\n");
602 return -EBUSY;
605 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
606 netdev_err(dev, "Unable to change to the same mode the team is in\n");
607 return -EINVAL;
610 new_mode = team_mode_get(kind);
611 if (!new_mode) {
612 netdev_err(dev, "Mode \"%s\" not found\n", kind);
613 return -EINVAL;
616 err = __team_change_mode(team, new_mode);
617 if (err) {
618 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
619 team_mode_put(new_mode);
620 return err;
623 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
624 return 0;
628 /*********************
629 * Peers notification
630 *********************/
632 static void team_notify_peers_work(struct work_struct *work)
634 struct team *team;
635 int val;
637 team = container_of(work, struct team, notify_peers.dw.work);
639 if (!rtnl_trylock()) {
640 schedule_delayed_work(&team->notify_peers.dw, 0);
641 return;
643 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
644 if (val < 0) {
645 rtnl_unlock();
646 return;
648 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
649 rtnl_unlock();
650 if (val)
651 schedule_delayed_work(&team->notify_peers.dw,
652 msecs_to_jiffies(team->notify_peers.interval));
655 static void team_notify_peers(struct team *team)
657 if (!team->notify_peers.count || !netif_running(team->dev))
658 return;
659 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
660 schedule_delayed_work(&team->notify_peers.dw, 0);
663 static void team_notify_peers_init(struct team *team)
665 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
668 static void team_notify_peers_fini(struct team *team)
670 cancel_delayed_work_sync(&team->notify_peers.dw);
674 /*******************************
675 * Send multicast group rejoins
676 *******************************/
678 static void team_mcast_rejoin_work(struct work_struct *work)
680 struct team *team;
681 int val;
683 team = container_of(work, struct team, mcast_rejoin.dw.work);
685 if (!rtnl_trylock()) {
686 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
687 return;
689 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
690 if (val < 0) {
691 rtnl_unlock();
692 return;
694 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
695 rtnl_unlock();
696 if (val)
697 schedule_delayed_work(&team->mcast_rejoin.dw,
698 msecs_to_jiffies(team->mcast_rejoin.interval));
701 static void team_mcast_rejoin(struct team *team)
703 if (!team->mcast_rejoin.count || !netif_running(team->dev))
704 return;
705 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
706 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
709 static void team_mcast_rejoin_init(struct team *team)
711 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
714 static void team_mcast_rejoin_fini(struct team *team)
716 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
720 /************************
721 * Rx path frame handler
722 ************************/
724 /* note: already called with rcu_read_lock */
725 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
727 struct sk_buff *skb = *pskb;
728 struct team_port *port;
729 struct team *team;
730 rx_handler_result_t res;
732 skb = skb_share_check(skb, GFP_ATOMIC);
733 if (!skb)
734 return RX_HANDLER_CONSUMED;
736 *pskb = skb;
738 port = team_port_get_rcu(skb->dev);
739 team = port->team;
740 if (!team_port_enabled(port)) {
741 /* allow exact match delivery for disabled ports */
742 res = RX_HANDLER_EXACT;
743 } else {
744 res = team->ops.receive(team, port, skb);
746 if (res == RX_HANDLER_ANOTHER) {
747 struct team_pcpu_stats *pcpu_stats;
749 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
750 u64_stats_update_begin(&pcpu_stats->syncp);
751 pcpu_stats->rx_packets++;
752 pcpu_stats->rx_bytes += skb->len;
753 if (skb->pkt_type == PACKET_MULTICAST)
754 pcpu_stats->rx_multicast++;
755 u64_stats_update_end(&pcpu_stats->syncp);
757 skb->dev = team->dev;
758 } else {
759 this_cpu_inc(team->pcpu_stats->rx_dropped);
762 return res;
766 /*************************************
767 * Multiqueue Tx port select override
768 *************************************/
770 static int team_queue_override_init(struct team *team)
772 struct list_head *listarr;
773 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
774 unsigned int i;
776 if (!queue_cnt)
777 return 0;
778 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
779 if (!listarr)
780 return -ENOMEM;
781 team->qom_lists = listarr;
782 for (i = 0; i < queue_cnt; i++)
783 INIT_LIST_HEAD(listarr++);
784 return 0;
787 static void team_queue_override_fini(struct team *team)
789 kfree(team->qom_lists);
792 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
794 return &team->qom_lists[queue_id - 1];
798 * note: already called with rcu_read_lock
800 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
802 struct list_head *qom_list;
803 struct team_port *port;
805 if (!team->queue_override_enabled || !skb->queue_mapping)
806 return false;
807 qom_list = __team_get_qom_list(team, skb->queue_mapping);
808 list_for_each_entry_rcu(port, qom_list, qom_list) {
809 if (!team_dev_queue_xmit(team, port, skb))
810 return true;
812 return false;
815 static void __team_queue_override_port_del(struct team *team,
816 struct team_port *port)
818 if (!port->queue_id)
819 return;
820 list_del_rcu(&port->qom_list);
823 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
824 struct team_port *cur)
826 if (port->priority < cur->priority)
827 return true;
828 if (port->priority > cur->priority)
829 return false;
830 if (port->index < cur->index)
831 return true;
832 return false;
835 static void __team_queue_override_port_add(struct team *team,
836 struct team_port *port)
838 struct team_port *cur;
839 struct list_head *qom_list;
840 struct list_head *node;
842 if (!port->queue_id)
843 return;
844 qom_list = __team_get_qom_list(team, port->queue_id);
845 node = qom_list;
846 list_for_each_entry(cur, qom_list, qom_list) {
847 if (team_queue_override_port_has_gt_prio_than(port, cur))
848 break;
849 node = &cur->qom_list;
851 list_add_tail_rcu(&port->qom_list, node);
854 static void __team_queue_override_enabled_check(struct team *team)
856 struct team_port *port;
857 bool enabled = false;
859 list_for_each_entry(port, &team->port_list, list) {
860 if (port->queue_id) {
861 enabled = true;
862 break;
865 if (enabled == team->queue_override_enabled)
866 return;
867 netdev_dbg(team->dev, "%s queue override\n",
868 enabled ? "Enabling" : "Disabling");
869 team->queue_override_enabled = enabled;
872 static void team_queue_override_port_prio_changed(struct team *team,
873 struct team_port *port)
875 if (!port->queue_id || team_port_enabled(port))
876 return;
877 __team_queue_override_port_del(team, port);
878 __team_queue_override_port_add(team, port);
879 __team_queue_override_enabled_check(team);
882 static void team_queue_override_port_change_queue_id(struct team *team,
883 struct team_port *port,
884 u16 new_queue_id)
886 if (team_port_enabled(port)) {
887 __team_queue_override_port_del(team, port);
888 port->queue_id = new_queue_id;
889 __team_queue_override_port_add(team, port);
890 __team_queue_override_enabled_check(team);
891 } else {
892 port->queue_id = new_queue_id;
896 static void team_queue_override_port_add(struct team *team,
897 struct team_port *port)
899 __team_queue_override_port_add(team, port);
900 __team_queue_override_enabled_check(team);
903 static void team_queue_override_port_del(struct team *team,
904 struct team_port *port)
906 __team_queue_override_port_del(team, port);
907 __team_queue_override_enabled_check(team);
911 /****************
912 * Port handling
913 ****************/
915 static bool team_port_find(const struct team *team,
916 const struct team_port *port)
918 struct team_port *cur;
920 list_for_each_entry(cur, &team->port_list, list)
921 if (cur == port)
922 return true;
923 return false;
927 * Enable/disable port by adding to enabled port hashlist and setting
928 * port->index (Might be racy so reader could see incorrect ifindex when
929 * processing a flying packet, but that is not a problem). Write guarded
930 * by team->lock.
932 static void team_port_enable(struct team *team,
933 struct team_port *port)
935 if (team_port_enabled(port))
936 return;
937 port->index = team->en_port_count++;
938 hlist_add_head_rcu(&port->hlist,
939 team_port_index_hash(team, port->index));
940 team_adjust_ops(team);
941 team_queue_override_port_add(team, port);
942 if (team->ops.port_enabled)
943 team->ops.port_enabled(team, port);
944 team_notify_peers(team);
945 team_mcast_rejoin(team);
948 static void __reconstruct_port_hlist(struct team *team, int rm_index)
950 int i;
951 struct team_port *port;
953 for (i = rm_index + 1; i < team->en_port_count; i++) {
954 port = team_get_port_by_index(team, i);
955 hlist_del_rcu(&port->hlist);
956 port->index--;
957 hlist_add_head_rcu(&port->hlist,
958 team_port_index_hash(team, port->index));
962 static void team_port_disable(struct team *team,
963 struct team_port *port)
965 if (!team_port_enabled(port))
966 return;
967 if (team->ops.port_disabled)
968 team->ops.port_disabled(team, port);
969 hlist_del_rcu(&port->hlist);
970 __reconstruct_port_hlist(team, port->index);
971 port->index = -1;
972 team->en_port_count--;
973 team_queue_override_port_del(team, port);
974 team_adjust_ops(team);
975 team_notify_peers(team);
976 team_mcast_rejoin(team);
979 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
980 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
981 NETIF_F_HIGHDMA | NETIF_F_LRO)
983 static void ___team_compute_features(struct team *team)
985 struct team_port *port;
986 netdev_features_t vlan_features = TEAM_VLAN_FEATURES &
987 NETIF_F_ALL_FOR_ALL;
988 unsigned short max_hard_header_len = ETH_HLEN;
989 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
990 IFF_XMIT_DST_RELEASE_PERM;
992 list_for_each_entry(port, &team->port_list, list) {
993 vlan_features = netdev_increment_features(vlan_features,
994 port->dev->vlan_features,
995 TEAM_VLAN_FEATURES);
997 dst_release_flag &= port->dev->priv_flags;
998 if (port->dev->hard_header_len > max_hard_header_len)
999 max_hard_header_len = port->dev->hard_header_len;
1002 team->dev->vlan_features = vlan_features;
1003 team->dev->hard_header_len = max_hard_header_len;
1005 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1006 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1007 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1010 static void __team_compute_features(struct team *team)
1012 ___team_compute_features(team);
1013 netdev_change_features(team->dev);
1016 static void team_compute_features(struct team *team)
1018 mutex_lock(&team->lock);
1019 ___team_compute_features(team);
1020 mutex_unlock(&team->lock);
1021 netdev_change_features(team->dev);
1024 static int team_port_enter(struct team *team, struct team_port *port)
1026 int err = 0;
1028 dev_hold(team->dev);
1029 if (team->ops.port_enter) {
1030 err = team->ops.port_enter(team, port);
1031 if (err) {
1032 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1033 port->dev->name);
1034 goto err_port_enter;
1038 return 0;
1040 err_port_enter:
1041 dev_put(team->dev);
1043 return err;
1046 static void team_port_leave(struct team *team, struct team_port *port)
1048 if (team->ops.port_leave)
1049 team->ops.port_leave(team, port);
1050 dev_put(team->dev);
1053 #ifdef CONFIG_NET_POLL_CONTROLLER
1054 static int __team_port_enable_netpoll(struct team_port *port)
1056 struct netpoll *np;
1057 int err;
1059 np = kzalloc(sizeof(*np), GFP_KERNEL);
1060 if (!np)
1061 return -ENOMEM;
1063 err = __netpoll_setup(np, port->dev);
1064 if (err) {
1065 kfree(np);
1066 return err;
1068 port->np = np;
1069 return err;
1072 static int team_port_enable_netpoll(struct team_port *port)
1074 if (!port->team->dev->npinfo)
1075 return 0;
1077 return __team_port_enable_netpoll(port);
1080 static void team_port_disable_netpoll(struct team_port *port)
1082 struct netpoll *np = port->np;
1084 if (!np)
1085 return;
1086 port->np = NULL;
1088 /* Wait for transmitting packets to finish before freeing. */
1089 synchronize_rcu_bh();
1090 __netpoll_cleanup(np);
1091 kfree(np);
1093 #else
1094 static int team_port_enable_netpoll(struct team_port *port)
1096 return 0;
1098 static void team_port_disable_netpoll(struct team_port *port)
1101 #endif
1103 static int team_upper_dev_link(struct net_device *dev,
1104 struct net_device *port_dev)
1106 int err;
1108 err = netdev_master_upper_dev_link(port_dev, dev);
1109 if (err)
1110 return err;
1111 port_dev->priv_flags |= IFF_TEAM_PORT;
1112 return 0;
1115 static void team_upper_dev_unlink(struct net_device *dev,
1116 struct net_device *port_dev)
1118 netdev_upper_dev_unlink(port_dev, dev);
1119 port_dev->priv_flags &= ~IFF_TEAM_PORT;
1122 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1123 static int team_dev_type_check_change(struct net_device *dev,
1124 struct net_device *port_dev);
1126 static int team_port_add(struct team *team, struct net_device *port_dev)
1128 struct net_device *dev = team->dev;
1129 struct team_port *port;
1130 char *portname = port_dev->name;
1131 int err;
1133 if (port_dev->flags & IFF_LOOPBACK) {
1134 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1135 portname);
1136 return -EINVAL;
1139 if (team_port_exists(port_dev)) {
1140 netdev_err(dev, "Device %s is already a port "
1141 "of a team device\n", portname);
1142 return -EBUSY;
1145 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1146 vlan_uses_dev(dev)) {
1147 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1148 portname);
1149 return -EPERM;
1152 err = team_dev_type_check_change(dev, port_dev);
1153 if (err)
1154 return err;
1156 if (port_dev->flags & IFF_UP) {
1157 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1158 portname);
1159 return -EBUSY;
1162 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1163 GFP_KERNEL);
1164 if (!port)
1165 return -ENOMEM;
1167 port->dev = port_dev;
1168 port->team = team;
1169 INIT_LIST_HEAD(&port->qom_list);
1171 port->orig.mtu = port_dev->mtu;
1172 err = dev_set_mtu(port_dev, dev->mtu);
1173 if (err) {
1174 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1175 goto err_set_mtu;
1178 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1180 err = team_port_enter(team, port);
1181 if (err) {
1182 netdev_err(dev, "Device %s failed to enter team mode\n",
1183 portname);
1184 goto err_port_enter;
1187 err = dev_open(port_dev);
1188 if (err) {
1189 netdev_dbg(dev, "Device %s opening failed\n",
1190 portname);
1191 goto err_dev_open;
1194 err = vlan_vids_add_by_dev(port_dev, dev);
1195 if (err) {
1196 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1197 portname);
1198 goto err_vids_add;
1201 err = team_port_enable_netpoll(port);
1202 if (err) {
1203 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1204 portname);
1205 goto err_enable_netpoll;
1208 if (!(dev->features & NETIF_F_LRO))
1209 dev_disable_lro(port_dev);
1211 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1212 port);
1213 if (err) {
1214 netdev_err(dev, "Device %s failed to register rx_handler\n",
1215 portname);
1216 goto err_handler_register;
1219 err = team_upper_dev_link(dev, port_dev);
1220 if (err) {
1221 netdev_err(dev, "Device %s failed to set upper link\n",
1222 portname);
1223 goto err_set_upper_link;
1226 err = __team_option_inst_add_port(team, port);
1227 if (err) {
1228 netdev_err(dev, "Device %s failed to add per-port options\n",
1229 portname);
1230 goto err_option_port_add;
1233 port->index = -1;
1234 list_add_tail_rcu(&port->list, &team->port_list);
1235 team_port_enable(team, port);
1236 __team_compute_features(team);
1237 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1238 __team_options_change_check(team);
1240 netdev_info(dev, "Port device %s added\n", portname);
1242 return 0;
1244 err_option_port_add:
1245 team_upper_dev_unlink(dev, port_dev);
1247 err_set_upper_link:
1248 netdev_rx_handler_unregister(port_dev);
1250 err_handler_register:
1251 team_port_disable_netpoll(port);
1253 err_enable_netpoll:
1254 vlan_vids_del_by_dev(port_dev, dev);
1256 err_vids_add:
1257 dev_close(port_dev);
1259 err_dev_open:
1260 team_port_leave(team, port);
1261 team_port_set_orig_dev_addr(port);
1263 err_port_enter:
1264 dev_set_mtu(port_dev, port->orig.mtu);
1266 err_set_mtu:
1267 kfree(port);
1269 return err;
1272 static void __team_port_change_port_removed(struct team_port *port);
1274 static int team_port_del(struct team *team, struct net_device *port_dev)
1276 struct net_device *dev = team->dev;
1277 struct team_port *port;
1278 char *portname = port_dev->name;
1280 port = team_port_get_rtnl(port_dev);
1281 if (!port || !team_port_find(team, port)) {
1282 netdev_err(dev, "Device %s does not act as a port of this team\n",
1283 portname);
1284 return -ENOENT;
1287 team_port_disable(team, port);
1288 list_del_rcu(&port->list);
1289 team_upper_dev_unlink(dev, port_dev);
1290 netdev_rx_handler_unregister(port_dev);
1291 team_port_disable_netpoll(port);
1292 vlan_vids_del_by_dev(port_dev, dev);
1293 dev_uc_unsync(port_dev, dev);
1294 dev_mc_unsync(port_dev, dev);
1295 dev_close(port_dev);
1296 team_port_leave(team, port);
1298 __team_option_inst_mark_removed_port(team, port);
1299 __team_options_change_check(team);
1300 __team_option_inst_del_port(team, port);
1301 __team_port_change_port_removed(port);
1303 team_port_set_orig_dev_addr(port);
1304 dev_set_mtu(port_dev, port->orig.mtu);
1305 kfree_rcu(port, rcu);
1306 netdev_info(dev, "Port device %s removed\n", portname);
1307 __team_compute_features(team);
1309 return 0;
1313 /*****************
1314 * Net device ops
1315 *****************/
1317 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1319 ctx->data.str_val = team->mode->kind;
1320 return 0;
1323 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1325 return team_change_mode(team, ctx->data.str_val);
1328 static int team_notify_peers_count_get(struct team *team,
1329 struct team_gsetter_ctx *ctx)
1331 ctx->data.u32_val = team->notify_peers.count;
1332 return 0;
1335 static int team_notify_peers_count_set(struct team *team,
1336 struct team_gsetter_ctx *ctx)
1338 team->notify_peers.count = ctx->data.u32_val;
1339 return 0;
1342 static int team_notify_peers_interval_get(struct team *team,
1343 struct team_gsetter_ctx *ctx)
1345 ctx->data.u32_val = team->notify_peers.interval;
1346 return 0;
1349 static int team_notify_peers_interval_set(struct team *team,
1350 struct team_gsetter_ctx *ctx)
1352 team->notify_peers.interval = ctx->data.u32_val;
1353 return 0;
1356 static int team_mcast_rejoin_count_get(struct team *team,
1357 struct team_gsetter_ctx *ctx)
1359 ctx->data.u32_val = team->mcast_rejoin.count;
1360 return 0;
1363 static int team_mcast_rejoin_count_set(struct team *team,
1364 struct team_gsetter_ctx *ctx)
1366 team->mcast_rejoin.count = ctx->data.u32_val;
1367 return 0;
1370 static int team_mcast_rejoin_interval_get(struct team *team,
1371 struct team_gsetter_ctx *ctx)
1373 ctx->data.u32_val = team->mcast_rejoin.interval;
1374 return 0;
1377 static int team_mcast_rejoin_interval_set(struct team *team,
1378 struct team_gsetter_ctx *ctx)
1380 team->mcast_rejoin.interval = ctx->data.u32_val;
1381 return 0;
1384 static int team_port_en_option_get(struct team *team,
1385 struct team_gsetter_ctx *ctx)
1387 struct team_port *port = ctx->info->port;
1389 ctx->data.bool_val = team_port_enabled(port);
1390 return 0;
1393 static int team_port_en_option_set(struct team *team,
1394 struct team_gsetter_ctx *ctx)
1396 struct team_port *port = ctx->info->port;
1398 if (ctx->data.bool_val)
1399 team_port_enable(team, port);
1400 else
1401 team_port_disable(team, port);
1402 return 0;
1405 static int team_user_linkup_option_get(struct team *team,
1406 struct team_gsetter_ctx *ctx)
1408 struct team_port *port = ctx->info->port;
1410 ctx->data.bool_val = port->user.linkup;
1411 return 0;
1414 static void __team_carrier_check(struct team *team);
1416 static int team_user_linkup_option_set(struct team *team,
1417 struct team_gsetter_ctx *ctx)
1419 struct team_port *port = ctx->info->port;
1421 port->user.linkup = ctx->data.bool_val;
1422 team_refresh_port_linkup(port);
1423 __team_carrier_check(port->team);
1424 return 0;
1427 static int team_user_linkup_en_option_get(struct team *team,
1428 struct team_gsetter_ctx *ctx)
1430 struct team_port *port = ctx->info->port;
1432 ctx->data.bool_val = port->user.linkup_enabled;
1433 return 0;
1436 static int team_user_linkup_en_option_set(struct team *team,
1437 struct team_gsetter_ctx *ctx)
1439 struct team_port *port = ctx->info->port;
1441 port->user.linkup_enabled = ctx->data.bool_val;
1442 team_refresh_port_linkup(port);
1443 __team_carrier_check(port->team);
1444 return 0;
1447 static int team_priority_option_get(struct team *team,
1448 struct team_gsetter_ctx *ctx)
1450 struct team_port *port = ctx->info->port;
1452 ctx->data.s32_val = port->priority;
1453 return 0;
1456 static int team_priority_option_set(struct team *team,
1457 struct team_gsetter_ctx *ctx)
1459 struct team_port *port = ctx->info->port;
1460 s32 priority = ctx->data.s32_val;
1462 if (port->priority == priority)
1463 return 0;
1464 port->priority = priority;
1465 team_queue_override_port_prio_changed(team, port);
1466 return 0;
1469 static int team_queue_id_option_get(struct team *team,
1470 struct team_gsetter_ctx *ctx)
1472 struct team_port *port = ctx->info->port;
1474 ctx->data.u32_val = port->queue_id;
1475 return 0;
1478 static int team_queue_id_option_set(struct team *team,
1479 struct team_gsetter_ctx *ctx)
1481 struct team_port *port = ctx->info->port;
1482 u16 new_queue_id = ctx->data.u32_val;
1484 if (port->queue_id == new_queue_id)
1485 return 0;
1486 if (new_queue_id >= team->dev->real_num_tx_queues)
1487 return -EINVAL;
1488 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1489 return 0;
1492 static const struct team_option team_options[] = {
1494 .name = "mode",
1495 .type = TEAM_OPTION_TYPE_STRING,
1496 .getter = team_mode_option_get,
1497 .setter = team_mode_option_set,
1500 .name = "notify_peers_count",
1501 .type = TEAM_OPTION_TYPE_U32,
1502 .getter = team_notify_peers_count_get,
1503 .setter = team_notify_peers_count_set,
1506 .name = "notify_peers_interval",
1507 .type = TEAM_OPTION_TYPE_U32,
1508 .getter = team_notify_peers_interval_get,
1509 .setter = team_notify_peers_interval_set,
1512 .name = "mcast_rejoin_count",
1513 .type = TEAM_OPTION_TYPE_U32,
1514 .getter = team_mcast_rejoin_count_get,
1515 .setter = team_mcast_rejoin_count_set,
1518 .name = "mcast_rejoin_interval",
1519 .type = TEAM_OPTION_TYPE_U32,
1520 .getter = team_mcast_rejoin_interval_get,
1521 .setter = team_mcast_rejoin_interval_set,
1524 .name = "enabled",
1525 .type = TEAM_OPTION_TYPE_BOOL,
1526 .per_port = true,
1527 .getter = team_port_en_option_get,
1528 .setter = team_port_en_option_set,
1531 .name = "user_linkup",
1532 .type = TEAM_OPTION_TYPE_BOOL,
1533 .per_port = true,
1534 .getter = team_user_linkup_option_get,
1535 .setter = team_user_linkup_option_set,
1538 .name = "user_linkup_enabled",
1539 .type = TEAM_OPTION_TYPE_BOOL,
1540 .per_port = true,
1541 .getter = team_user_linkup_en_option_get,
1542 .setter = team_user_linkup_en_option_set,
1545 .name = "priority",
1546 .type = TEAM_OPTION_TYPE_S32,
1547 .per_port = true,
1548 .getter = team_priority_option_get,
1549 .setter = team_priority_option_set,
1552 .name = "queue_id",
1553 .type = TEAM_OPTION_TYPE_U32,
1554 .per_port = true,
1555 .getter = team_queue_id_option_get,
1556 .setter = team_queue_id_option_set,
1560 static struct lock_class_key team_netdev_xmit_lock_key;
1561 static struct lock_class_key team_netdev_addr_lock_key;
1562 static struct lock_class_key team_tx_busylock_key;
1564 static void team_set_lockdep_class_one(struct net_device *dev,
1565 struct netdev_queue *txq,
1566 void *unused)
1568 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1571 static void team_set_lockdep_class(struct net_device *dev)
1573 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1574 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1575 dev->qdisc_tx_busylock = &team_tx_busylock_key;
1578 static int team_init(struct net_device *dev)
1580 struct team *team = netdev_priv(dev);
1581 int i;
1582 int err;
1584 team->dev = dev;
1585 mutex_init(&team->lock);
1586 team_set_no_mode(team);
1588 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1589 if (!team->pcpu_stats)
1590 return -ENOMEM;
1592 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1593 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1594 INIT_LIST_HEAD(&team->port_list);
1595 err = team_queue_override_init(team);
1596 if (err)
1597 goto err_team_queue_override_init;
1599 team_adjust_ops(team);
1601 INIT_LIST_HEAD(&team->option_list);
1602 INIT_LIST_HEAD(&team->option_inst_list);
1604 team_notify_peers_init(team);
1605 team_mcast_rejoin_init(team);
1607 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1608 if (err)
1609 goto err_options_register;
1610 netif_carrier_off(dev);
1612 team_set_lockdep_class(dev);
1614 return 0;
1616 err_options_register:
1617 team_mcast_rejoin_fini(team);
1618 team_notify_peers_fini(team);
1619 team_queue_override_fini(team);
1620 err_team_queue_override_init:
1621 free_percpu(team->pcpu_stats);
1623 return err;
1626 static void team_uninit(struct net_device *dev)
1628 struct team *team = netdev_priv(dev);
1629 struct team_port *port;
1630 struct team_port *tmp;
1632 mutex_lock(&team->lock);
1633 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1634 team_port_del(team, port->dev);
1636 __team_change_mode(team, NULL); /* cleanup */
1637 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1638 team_mcast_rejoin_fini(team);
1639 team_notify_peers_fini(team);
1640 team_queue_override_fini(team);
1641 mutex_unlock(&team->lock);
1644 static void team_destructor(struct net_device *dev)
1646 struct team *team = netdev_priv(dev);
1648 free_percpu(team->pcpu_stats);
1649 free_netdev(dev);
1652 static int team_open(struct net_device *dev)
1654 return 0;
1657 static int team_close(struct net_device *dev)
1659 return 0;
1663 * note: already called with rcu_read_lock
1665 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1667 struct team *team = netdev_priv(dev);
1668 bool tx_success;
1669 unsigned int len = skb->len;
1671 tx_success = team_queue_override_transmit(team, skb);
1672 if (!tx_success)
1673 tx_success = team->ops.transmit(team, skb);
1674 if (tx_success) {
1675 struct team_pcpu_stats *pcpu_stats;
1677 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1678 u64_stats_update_begin(&pcpu_stats->syncp);
1679 pcpu_stats->tx_packets++;
1680 pcpu_stats->tx_bytes += len;
1681 u64_stats_update_end(&pcpu_stats->syncp);
1682 } else {
1683 this_cpu_inc(team->pcpu_stats->tx_dropped);
1686 return NETDEV_TX_OK;
1689 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1690 void *accel_priv, select_queue_fallback_t fallback)
1693 * This helper function exists to help dev_pick_tx get the correct
1694 * destination queue. Using a helper function skips a call to
1695 * skb_tx_hash and will put the skbs in the queue we expect on their
1696 * way down to the team driver.
1698 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1701 * Save the original txq to restore before passing to the driver
1703 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1705 if (unlikely(txq >= dev->real_num_tx_queues)) {
1706 do {
1707 txq -= dev->real_num_tx_queues;
1708 } while (txq >= dev->real_num_tx_queues);
1710 return txq;
1713 static void team_change_rx_flags(struct net_device *dev, int change)
1715 struct team *team = netdev_priv(dev);
1716 struct team_port *port;
1717 int inc;
1719 rcu_read_lock();
1720 list_for_each_entry_rcu(port, &team->port_list, list) {
1721 if (change & IFF_PROMISC) {
1722 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1723 dev_set_promiscuity(port->dev, inc);
1725 if (change & IFF_ALLMULTI) {
1726 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1727 dev_set_allmulti(port->dev, inc);
1730 rcu_read_unlock();
1733 static void team_set_rx_mode(struct net_device *dev)
1735 struct team *team = netdev_priv(dev);
1736 struct team_port *port;
1738 rcu_read_lock();
1739 list_for_each_entry_rcu(port, &team->port_list, list) {
1740 dev_uc_sync_multiple(port->dev, dev);
1741 dev_mc_sync_multiple(port->dev, dev);
1743 rcu_read_unlock();
1746 static int team_set_mac_address(struct net_device *dev, void *p)
1748 struct sockaddr *addr = p;
1749 struct team *team = netdev_priv(dev);
1750 struct team_port *port;
1752 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1753 return -EADDRNOTAVAIL;
1754 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1755 mutex_lock(&team->lock);
1756 list_for_each_entry(port, &team->port_list, list)
1757 if (team->ops.port_change_dev_addr)
1758 team->ops.port_change_dev_addr(team, port);
1759 mutex_unlock(&team->lock);
1760 return 0;
1763 static int team_change_mtu(struct net_device *dev, int new_mtu)
1765 struct team *team = netdev_priv(dev);
1766 struct team_port *port;
1767 int err;
1770 * Alhough this is reader, it's guarded by team lock. It's not possible
1771 * to traverse list in reverse under rcu_read_lock
1773 mutex_lock(&team->lock);
1774 team->port_mtu_change_allowed = true;
1775 list_for_each_entry(port, &team->port_list, list) {
1776 err = dev_set_mtu(port->dev, new_mtu);
1777 if (err) {
1778 netdev_err(dev, "Device %s failed to change mtu",
1779 port->dev->name);
1780 goto unwind;
1783 team->port_mtu_change_allowed = false;
1784 mutex_unlock(&team->lock);
1786 dev->mtu = new_mtu;
1788 return 0;
1790 unwind:
1791 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1792 dev_set_mtu(port->dev, dev->mtu);
1793 team->port_mtu_change_allowed = false;
1794 mutex_unlock(&team->lock);
1796 return err;
1799 static struct rtnl_link_stats64 *
1800 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1802 struct team *team = netdev_priv(dev);
1803 struct team_pcpu_stats *p;
1804 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1805 u32 rx_dropped = 0, tx_dropped = 0;
1806 unsigned int start;
1807 int i;
1809 for_each_possible_cpu(i) {
1810 p = per_cpu_ptr(team->pcpu_stats, i);
1811 do {
1812 start = u64_stats_fetch_begin_irq(&p->syncp);
1813 rx_packets = p->rx_packets;
1814 rx_bytes = p->rx_bytes;
1815 rx_multicast = p->rx_multicast;
1816 tx_packets = p->tx_packets;
1817 tx_bytes = p->tx_bytes;
1818 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1820 stats->rx_packets += rx_packets;
1821 stats->rx_bytes += rx_bytes;
1822 stats->multicast += rx_multicast;
1823 stats->tx_packets += tx_packets;
1824 stats->tx_bytes += tx_bytes;
1826 * rx_dropped & tx_dropped are u32, updated
1827 * without syncp protection.
1829 rx_dropped += p->rx_dropped;
1830 tx_dropped += p->tx_dropped;
1832 stats->rx_dropped = rx_dropped;
1833 stats->tx_dropped = tx_dropped;
1834 return stats;
1837 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1839 struct team *team = netdev_priv(dev);
1840 struct team_port *port;
1841 int err;
1844 * Alhough this is reader, it's guarded by team lock. It's not possible
1845 * to traverse list in reverse under rcu_read_lock
1847 mutex_lock(&team->lock);
1848 list_for_each_entry(port, &team->port_list, list) {
1849 err = vlan_vid_add(port->dev, proto, vid);
1850 if (err)
1851 goto unwind;
1853 mutex_unlock(&team->lock);
1855 return 0;
1857 unwind:
1858 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1859 vlan_vid_del(port->dev, proto, vid);
1860 mutex_unlock(&team->lock);
1862 return err;
1865 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1867 struct team *team = netdev_priv(dev);
1868 struct team_port *port;
1870 mutex_lock(&team->lock);
1871 list_for_each_entry(port, &team->port_list, list)
1872 vlan_vid_del(port->dev, proto, vid);
1873 mutex_unlock(&team->lock);
1875 return 0;
1878 #ifdef CONFIG_NET_POLL_CONTROLLER
1879 static void team_poll_controller(struct net_device *dev)
1883 static void __team_netpoll_cleanup(struct team *team)
1885 struct team_port *port;
1887 list_for_each_entry(port, &team->port_list, list)
1888 team_port_disable_netpoll(port);
1891 static void team_netpoll_cleanup(struct net_device *dev)
1893 struct team *team = netdev_priv(dev);
1895 mutex_lock(&team->lock);
1896 __team_netpoll_cleanup(team);
1897 mutex_unlock(&team->lock);
1900 static int team_netpoll_setup(struct net_device *dev,
1901 struct netpoll_info *npifo)
1903 struct team *team = netdev_priv(dev);
1904 struct team_port *port;
1905 int err = 0;
1907 mutex_lock(&team->lock);
1908 list_for_each_entry(port, &team->port_list, list) {
1909 err = __team_port_enable_netpoll(port);
1910 if (err) {
1911 __team_netpoll_cleanup(team);
1912 break;
1915 mutex_unlock(&team->lock);
1916 return err;
1918 #endif
1920 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1922 struct team *team = netdev_priv(dev);
1923 int err;
1925 mutex_lock(&team->lock);
1926 err = team_port_add(team, port_dev);
1927 mutex_unlock(&team->lock);
1928 return err;
1931 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1933 struct team *team = netdev_priv(dev);
1934 int err;
1936 mutex_lock(&team->lock);
1937 err = team_port_del(team, port_dev);
1938 mutex_unlock(&team->lock);
1939 return err;
1942 static netdev_features_t team_fix_features(struct net_device *dev,
1943 netdev_features_t features)
1945 struct team_port *port;
1946 struct team *team = netdev_priv(dev);
1947 netdev_features_t mask;
1949 mask = features;
1950 features &= ~NETIF_F_ONE_FOR_ALL;
1951 features |= NETIF_F_ALL_FOR_ALL;
1953 rcu_read_lock();
1954 list_for_each_entry_rcu(port, &team->port_list, list) {
1955 features = netdev_increment_features(features,
1956 port->dev->features,
1957 mask);
1959 rcu_read_unlock();
1961 features = netdev_add_tso_features(features, mask);
1963 return features;
1966 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1968 struct team *team = netdev_priv(dev);
1970 team->user_carrier_enabled = true;
1972 if (new_carrier)
1973 netif_carrier_on(dev);
1974 else
1975 netif_carrier_off(dev);
1976 return 0;
1979 static const struct net_device_ops team_netdev_ops = {
1980 .ndo_init = team_init,
1981 .ndo_uninit = team_uninit,
1982 .ndo_open = team_open,
1983 .ndo_stop = team_close,
1984 .ndo_start_xmit = team_xmit,
1985 .ndo_select_queue = team_select_queue,
1986 .ndo_change_rx_flags = team_change_rx_flags,
1987 .ndo_set_rx_mode = team_set_rx_mode,
1988 .ndo_set_mac_address = team_set_mac_address,
1989 .ndo_change_mtu = team_change_mtu,
1990 .ndo_get_stats64 = team_get_stats64,
1991 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1992 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1993 #ifdef CONFIG_NET_POLL_CONTROLLER
1994 .ndo_poll_controller = team_poll_controller,
1995 .ndo_netpoll_setup = team_netpoll_setup,
1996 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1997 #endif
1998 .ndo_add_slave = team_add_slave,
1999 .ndo_del_slave = team_del_slave,
2000 .ndo_fix_features = team_fix_features,
2001 .ndo_change_carrier = team_change_carrier,
2002 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
2003 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
2004 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
2005 .ndo_fdb_add = switchdev_port_fdb_add,
2006 .ndo_fdb_del = switchdev_port_fdb_del,
2007 .ndo_fdb_dump = switchdev_port_fdb_dump,
2008 .ndo_features_check = passthru_features_check,
2011 /***********************
2012 * ethtool interface
2013 ***********************/
2015 static void team_ethtool_get_drvinfo(struct net_device *dev,
2016 struct ethtool_drvinfo *drvinfo)
2018 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2019 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2022 static const struct ethtool_ops team_ethtool_ops = {
2023 .get_drvinfo = team_ethtool_get_drvinfo,
2024 .get_link = ethtool_op_get_link,
2027 /***********************
2028 * rt netlink interface
2029 ***********************/
2031 static void team_setup_by_port(struct net_device *dev,
2032 struct net_device *port_dev)
2034 dev->header_ops = port_dev->header_ops;
2035 dev->type = port_dev->type;
2036 dev->hard_header_len = port_dev->hard_header_len;
2037 dev->addr_len = port_dev->addr_len;
2038 dev->mtu = port_dev->mtu;
2039 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2040 eth_hw_addr_inherit(dev, port_dev);
2043 static int team_dev_type_check_change(struct net_device *dev,
2044 struct net_device *port_dev)
2046 struct team *team = netdev_priv(dev);
2047 char *portname = port_dev->name;
2048 int err;
2050 if (dev->type == port_dev->type)
2051 return 0;
2052 if (!list_empty(&team->port_list)) {
2053 netdev_err(dev, "Device %s is of different type\n", portname);
2054 return -EBUSY;
2056 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2057 err = notifier_to_errno(err);
2058 if (err) {
2059 netdev_err(dev, "Refused to change device type\n");
2060 return err;
2062 dev_uc_flush(dev);
2063 dev_mc_flush(dev);
2064 team_setup_by_port(dev, port_dev);
2065 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2066 return 0;
2069 static void team_setup(struct net_device *dev)
2071 ether_setup(dev);
2073 dev->netdev_ops = &team_netdev_ops;
2074 dev->ethtool_ops = &team_ethtool_ops;
2075 dev->destructor = team_destructor;
2076 dev->flags |= IFF_MULTICAST;
2077 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2078 dev->priv_flags |= IFF_NO_QUEUE;
2081 * Indicate we support unicast address filtering. That way core won't
2082 * bring us to promisc mode in case a unicast addr is added.
2083 * Let this up to underlay drivers.
2085 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2087 dev->features |= NETIF_F_LLTX;
2088 dev->features |= NETIF_F_GRO;
2090 /* Don't allow team devices to change network namespaces. */
2091 dev->features |= NETIF_F_NETNS_LOCAL;
2093 dev->hw_features = TEAM_VLAN_FEATURES |
2094 NETIF_F_HW_VLAN_CTAG_TX |
2095 NETIF_F_HW_VLAN_CTAG_RX |
2096 NETIF_F_HW_VLAN_CTAG_FILTER;
2098 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
2099 dev->features |= dev->hw_features;
2102 static int team_newlink(struct net *src_net, struct net_device *dev,
2103 struct nlattr *tb[], struct nlattr *data[])
2105 if (tb[IFLA_ADDRESS] == NULL)
2106 eth_hw_addr_random(dev);
2108 return register_netdevice(dev);
2111 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2113 if (tb[IFLA_ADDRESS]) {
2114 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2115 return -EINVAL;
2116 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2117 return -EADDRNOTAVAIL;
2119 return 0;
2122 static unsigned int team_get_num_tx_queues(void)
2124 return TEAM_DEFAULT_NUM_TX_QUEUES;
2127 static unsigned int team_get_num_rx_queues(void)
2129 return TEAM_DEFAULT_NUM_RX_QUEUES;
2132 static struct rtnl_link_ops team_link_ops __read_mostly = {
2133 .kind = DRV_NAME,
2134 .priv_size = sizeof(struct team),
2135 .setup = team_setup,
2136 .newlink = team_newlink,
2137 .validate = team_validate,
2138 .get_num_tx_queues = team_get_num_tx_queues,
2139 .get_num_rx_queues = team_get_num_rx_queues,
2143 /***********************************
2144 * Generic netlink custom interface
2145 ***********************************/
2147 static struct genl_family team_nl_family = {
2148 .id = GENL_ID_GENERATE,
2149 .name = TEAM_GENL_NAME,
2150 .version = TEAM_GENL_VERSION,
2151 .maxattr = TEAM_ATTR_MAX,
2152 .netnsok = true,
2155 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2156 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2157 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2158 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2159 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2162 static const struct nla_policy
2163 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2164 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2165 [TEAM_ATTR_OPTION_NAME] = {
2166 .type = NLA_STRING,
2167 .len = TEAM_STRING_MAX_LEN,
2169 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2170 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2171 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
2174 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2176 struct sk_buff *msg;
2177 void *hdr;
2178 int err;
2180 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2181 if (!msg)
2182 return -ENOMEM;
2184 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2185 &team_nl_family, 0, TEAM_CMD_NOOP);
2186 if (!hdr) {
2187 err = -EMSGSIZE;
2188 goto err_msg_put;
2191 genlmsg_end(msg, hdr);
2193 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2195 err_msg_put:
2196 nlmsg_free(msg);
2198 return err;
2202 * Netlink cmd functions should be locked by following two functions.
2203 * Since dev gets held here, that ensures dev won't disappear in between.
2205 static struct team *team_nl_team_get(struct genl_info *info)
2207 struct net *net = genl_info_net(info);
2208 int ifindex;
2209 struct net_device *dev;
2210 struct team *team;
2212 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2213 return NULL;
2215 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2216 dev = dev_get_by_index(net, ifindex);
2217 if (!dev || dev->netdev_ops != &team_netdev_ops) {
2218 if (dev)
2219 dev_put(dev);
2220 return NULL;
2223 team = netdev_priv(dev);
2224 mutex_lock(&team->lock);
2225 return team;
2228 static void team_nl_team_put(struct team *team)
2230 mutex_unlock(&team->lock);
2231 dev_put(team->dev);
2234 typedef int team_nl_send_func_t(struct sk_buff *skb,
2235 struct team *team, u32 portid);
2237 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2239 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2242 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2243 struct team_option_inst *opt_inst)
2245 struct nlattr *option_item;
2246 struct team_option *option = opt_inst->option;
2247 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2248 struct team_gsetter_ctx ctx;
2249 int err;
2251 ctx.info = opt_inst_info;
2252 err = team_option_get(team, opt_inst, &ctx);
2253 if (err)
2254 return err;
2256 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2257 if (!option_item)
2258 return -EMSGSIZE;
2260 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2261 goto nest_cancel;
2262 if (opt_inst_info->port &&
2263 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2264 opt_inst_info->port->dev->ifindex))
2265 goto nest_cancel;
2266 if (opt_inst->option->array_size &&
2267 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2268 opt_inst_info->array_index))
2269 goto nest_cancel;
2271 switch (option->type) {
2272 case TEAM_OPTION_TYPE_U32:
2273 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2274 goto nest_cancel;
2275 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2276 goto nest_cancel;
2277 break;
2278 case TEAM_OPTION_TYPE_STRING:
2279 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2280 goto nest_cancel;
2281 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2282 ctx.data.str_val))
2283 goto nest_cancel;
2284 break;
2285 case TEAM_OPTION_TYPE_BINARY:
2286 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2287 goto nest_cancel;
2288 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2289 ctx.data.bin_val.ptr))
2290 goto nest_cancel;
2291 break;
2292 case TEAM_OPTION_TYPE_BOOL:
2293 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2294 goto nest_cancel;
2295 if (ctx.data.bool_val &&
2296 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2297 goto nest_cancel;
2298 break;
2299 case TEAM_OPTION_TYPE_S32:
2300 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2301 goto nest_cancel;
2302 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2303 goto nest_cancel;
2304 break;
2305 default:
2306 BUG();
2308 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2309 goto nest_cancel;
2310 if (opt_inst->changed) {
2311 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2312 goto nest_cancel;
2313 opt_inst->changed = false;
2315 nla_nest_end(skb, option_item);
2316 return 0;
2318 nest_cancel:
2319 nla_nest_cancel(skb, option_item);
2320 return -EMSGSIZE;
2323 static int __send_and_alloc_skb(struct sk_buff **pskb,
2324 struct team *team, u32 portid,
2325 team_nl_send_func_t *send_func)
2327 int err;
2329 if (*pskb) {
2330 err = send_func(*pskb, team, portid);
2331 if (err)
2332 return err;
2334 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2335 if (!*pskb)
2336 return -ENOMEM;
2337 return 0;
2340 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2341 int flags, team_nl_send_func_t *send_func,
2342 struct list_head *sel_opt_inst_list)
2344 struct nlattr *option_list;
2345 struct nlmsghdr *nlh;
2346 void *hdr;
2347 struct team_option_inst *opt_inst;
2348 int err;
2349 struct sk_buff *skb = NULL;
2350 bool incomplete;
2351 int i;
2353 opt_inst = list_first_entry(sel_opt_inst_list,
2354 struct team_option_inst, tmp_list);
2356 start_again:
2357 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2358 if (err)
2359 return err;
2361 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2362 TEAM_CMD_OPTIONS_GET);
2363 if (!hdr) {
2364 nlmsg_free(skb);
2365 return -EMSGSIZE;
2368 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2369 goto nla_put_failure;
2370 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2371 if (!option_list)
2372 goto nla_put_failure;
2374 i = 0;
2375 incomplete = false;
2376 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2377 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2378 if (err) {
2379 if (err == -EMSGSIZE) {
2380 if (!i)
2381 goto errout;
2382 incomplete = true;
2383 break;
2385 goto errout;
2387 i++;
2390 nla_nest_end(skb, option_list);
2391 genlmsg_end(skb, hdr);
2392 if (incomplete)
2393 goto start_again;
2395 send_done:
2396 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2397 if (!nlh) {
2398 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2399 if (err)
2400 return err;
2401 goto send_done;
2404 return send_func(skb, team, portid);
2406 nla_put_failure:
2407 err = -EMSGSIZE;
2408 errout:
2409 genlmsg_cancel(skb, hdr);
2410 nlmsg_free(skb);
2411 return err;
2414 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2416 struct team *team;
2417 struct team_option_inst *opt_inst;
2418 int err;
2419 LIST_HEAD(sel_opt_inst_list);
2421 team = team_nl_team_get(info);
2422 if (!team)
2423 return -EINVAL;
2425 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2426 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2427 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2428 NLM_F_ACK, team_nl_send_unicast,
2429 &sel_opt_inst_list);
2431 team_nl_team_put(team);
2433 return err;
2436 static int team_nl_send_event_options_get(struct team *team,
2437 struct list_head *sel_opt_inst_list);
2439 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2441 struct team *team;
2442 int err = 0;
2443 int i;
2444 struct nlattr *nl_option;
2445 LIST_HEAD(opt_inst_list);
2447 team = team_nl_team_get(info);
2448 if (!team)
2449 return -EINVAL;
2451 err = -EINVAL;
2452 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2453 err = -EINVAL;
2454 goto team_put;
2457 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2458 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2459 struct nlattr *attr;
2460 struct nlattr *attr_data;
2461 enum team_option_type opt_type;
2462 int opt_port_ifindex = 0; /* != 0 for per-port options */
2463 u32 opt_array_index = 0;
2464 bool opt_is_array = false;
2465 struct team_option_inst *opt_inst;
2466 char *opt_name;
2467 bool opt_found = false;
2469 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2470 err = -EINVAL;
2471 goto team_put;
2473 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2474 nl_option, team_nl_option_policy);
2475 if (err)
2476 goto team_put;
2477 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2478 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2479 err = -EINVAL;
2480 goto team_put;
2482 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2483 case NLA_U32:
2484 opt_type = TEAM_OPTION_TYPE_U32;
2485 break;
2486 case NLA_STRING:
2487 opt_type = TEAM_OPTION_TYPE_STRING;
2488 break;
2489 case NLA_BINARY:
2490 opt_type = TEAM_OPTION_TYPE_BINARY;
2491 break;
2492 case NLA_FLAG:
2493 opt_type = TEAM_OPTION_TYPE_BOOL;
2494 break;
2495 case NLA_S32:
2496 opt_type = TEAM_OPTION_TYPE_S32;
2497 break;
2498 default:
2499 goto team_put;
2502 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2503 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2504 err = -EINVAL;
2505 goto team_put;
2508 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2509 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2510 if (attr)
2511 opt_port_ifindex = nla_get_u32(attr);
2513 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2514 if (attr) {
2515 opt_is_array = true;
2516 opt_array_index = nla_get_u32(attr);
2519 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2520 struct team_option *option = opt_inst->option;
2521 struct team_gsetter_ctx ctx;
2522 struct team_option_inst_info *opt_inst_info;
2523 int tmp_ifindex;
2525 opt_inst_info = &opt_inst->info;
2526 tmp_ifindex = opt_inst_info->port ?
2527 opt_inst_info->port->dev->ifindex : 0;
2528 if (option->type != opt_type ||
2529 strcmp(option->name, opt_name) ||
2530 tmp_ifindex != opt_port_ifindex ||
2531 (option->array_size && !opt_is_array) ||
2532 opt_inst_info->array_index != opt_array_index)
2533 continue;
2534 opt_found = true;
2535 ctx.info = opt_inst_info;
2536 switch (opt_type) {
2537 case TEAM_OPTION_TYPE_U32:
2538 ctx.data.u32_val = nla_get_u32(attr_data);
2539 break;
2540 case TEAM_OPTION_TYPE_STRING:
2541 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2542 err = -EINVAL;
2543 goto team_put;
2545 ctx.data.str_val = nla_data(attr_data);
2546 break;
2547 case TEAM_OPTION_TYPE_BINARY:
2548 ctx.data.bin_val.len = nla_len(attr_data);
2549 ctx.data.bin_val.ptr = nla_data(attr_data);
2550 break;
2551 case TEAM_OPTION_TYPE_BOOL:
2552 ctx.data.bool_val = attr_data ? true : false;
2553 break;
2554 case TEAM_OPTION_TYPE_S32:
2555 ctx.data.s32_val = nla_get_s32(attr_data);
2556 break;
2557 default:
2558 BUG();
2560 err = team_option_set(team, opt_inst, &ctx);
2561 if (err)
2562 goto team_put;
2563 opt_inst->changed = true;
2565 /* dumb/evil user-space can send us duplicate opt,
2566 * keep only the last one
2568 if (__team_option_inst_tmp_find(&opt_inst_list,
2569 opt_inst))
2570 continue;
2572 list_add(&opt_inst->tmp_list, &opt_inst_list);
2574 if (!opt_found) {
2575 err = -ENOENT;
2576 goto team_put;
2580 err = team_nl_send_event_options_get(team, &opt_inst_list);
2582 team_put:
2583 team_nl_team_put(team);
2585 return err;
2588 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2589 struct team_port *port)
2591 struct nlattr *port_item;
2593 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2594 if (!port_item)
2595 goto nest_cancel;
2596 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2597 goto nest_cancel;
2598 if (port->changed) {
2599 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2600 goto nest_cancel;
2601 port->changed = false;
2603 if ((port->removed &&
2604 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2605 (port->state.linkup &&
2606 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2607 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2608 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2609 goto nest_cancel;
2610 nla_nest_end(skb, port_item);
2611 return 0;
2613 nest_cancel:
2614 nla_nest_cancel(skb, port_item);
2615 return -EMSGSIZE;
2618 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2619 int flags, team_nl_send_func_t *send_func,
2620 struct team_port *one_port)
2622 struct nlattr *port_list;
2623 struct nlmsghdr *nlh;
2624 void *hdr;
2625 struct team_port *port;
2626 int err;
2627 struct sk_buff *skb = NULL;
2628 bool incomplete;
2629 int i;
2631 port = list_first_entry_or_null(&team->port_list,
2632 struct team_port, list);
2634 start_again:
2635 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2636 if (err)
2637 return err;
2639 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2640 TEAM_CMD_PORT_LIST_GET);
2641 if (!hdr) {
2642 nlmsg_free(skb);
2643 return -EMSGSIZE;
2646 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2647 goto nla_put_failure;
2648 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2649 if (!port_list)
2650 goto nla_put_failure;
2652 i = 0;
2653 incomplete = false;
2655 /* If one port is selected, called wants to send port list containing
2656 * only this port. Otherwise go through all listed ports and send all
2658 if (one_port) {
2659 err = team_nl_fill_one_port_get(skb, one_port);
2660 if (err)
2661 goto errout;
2662 } else if (port) {
2663 list_for_each_entry_from(port, &team->port_list, list) {
2664 err = team_nl_fill_one_port_get(skb, port);
2665 if (err) {
2666 if (err == -EMSGSIZE) {
2667 if (!i)
2668 goto errout;
2669 incomplete = true;
2670 break;
2672 goto errout;
2674 i++;
2678 nla_nest_end(skb, port_list);
2679 genlmsg_end(skb, hdr);
2680 if (incomplete)
2681 goto start_again;
2683 send_done:
2684 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2685 if (!nlh) {
2686 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2687 if (err)
2688 return err;
2689 goto send_done;
2692 return send_func(skb, team, portid);
2694 nla_put_failure:
2695 err = -EMSGSIZE;
2696 errout:
2697 genlmsg_cancel(skb, hdr);
2698 nlmsg_free(skb);
2699 return err;
2702 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2703 struct genl_info *info)
2705 struct team *team;
2706 int err;
2708 team = team_nl_team_get(info);
2709 if (!team)
2710 return -EINVAL;
2712 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2713 NLM_F_ACK, team_nl_send_unicast, NULL);
2715 team_nl_team_put(team);
2717 return err;
2720 static const struct genl_ops team_nl_ops[] = {
2722 .cmd = TEAM_CMD_NOOP,
2723 .doit = team_nl_cmd_noop,
2724 .policy = team_nl_policy,
2727 .cmd = TEAM_CMD_OPTIONS_SET,
2728 .doit = team_nl_cmd_options_set,
2729 .policy = team_nl_policy,
2730 .flags = GENL_ADMIN_PERM,
2733 .cmd = TEAM_CMD_OPTIONS_GET,
2734 .doit = team_nl_cmd_options_get,
2735 .policy = team_nl_policy,
2736 .flags = GENL_ADMIN_PERM,
2739 .cmd = TEAM_CMD_PORT_LIST_GET,
2740 .doit = team_nl_cmd_port_list_get,
2741 .policy = team_nl_policy,
2742 .flags = GENL_ADMIN_PERM,
2746 static const struct genl_multicast_group team_nl_mcgrps[] = {
2747 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2750 static int team_nl_send_multicast(struct sk_buff *skb,
2751 struct team *team, u32 portid)
2753 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2754 skb, 0, 0, GFP_KERNEL);
2757 static int team_nl_send_event_options_get(struct team *team,
2758 struct list_head *sel_opt_inst_list)
2760 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2761 sel_opt_inst_list);
2764 static int team_nl_send_event_port_get(struct team *team,
2765 struct team_port *port)
2767 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2768 port);
2771 static int team_nl_init(void)
2773 return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2774 team_nl_mcgrps);
2777 static void team_nl_fini(void)
2779 genl_unregister_family(&team_nl_family);
2783 /******************
2784 * Change checkers
2785 ******************/
2787 static void __team_options_change_check(struct team *team)
2789 int err;
2790 struct team_option_inst *opt_inst;
2791 LIST_HEAD(sel_opt_inst_list);
2793 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2794 if (opt_inst->changed)
2795 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2797 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2798 if (err && err != -ESRCH)
2799 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2800 err);
2803 /* rtnl lock is held */
2805 static void __team_port_change_send(struct team_port *port, bool linkup)
2807 int err;
2809 port->changed = true;
2810 port->state.linkup = linkup;
2811 team_refresh_port_linkup(port);
2812 if (linkup) {
2813 struct ethtool_cmd ecmd;
2815 err = __ethtool_get_settings(port->dev, &ecmd);
2816 if (!err) {
2817 port->state.speed = ethtool_cmd_speed(&ecmd);
2818 port->state.duplex = ecmd.duplex;
2819 goto send_event;
2822 port->state.speed = 0;
2823 port->state.duplex = 0;
2825 send_event:
2826 err = team_nl_send_event_port_get(port->team, port);
2827 if (err && err != -ESRCH)
2828 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2829 port->dev->name, err);
2833 static void __team_carrier_check(struct team *team)
2835 struct team_port *port;
2836 bool team_linkup;
2838 if (team->user_carrier_enabled)
2839 return;
2841 team_linkup = false;
2842 list_for_each_entry(port, &team->port_list, list) {
2843 if (port->linkup) {
2844 team_linkup = true;
2845 break;
2849 if (team_linkup)
2850 netif_carrier_on(team->dev);
2851 else
2852 netif_carrier_off(team->dev);
2855 static void __team_port_change_check(struct team_port *port, bool linkup)
2857 if (port->state.linkup != linkup)
2858 __team_port_change_send(port, linkup);
2859 __team_carrier_check(port->team);
2862 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2864 __team_port_change_send(port, linkup);
2865 __team_carrier_check(port->team);
2868 static void __team_port_change_port_removed(struct team_port *port)
2870 port->removed = true;
2871 __team_port_change_send(port, false);
2872 __team_carrier_check(port->team);
2875 static void team_port_change_check(struct team_port *port, bool linkup)
2877 struct team *team = port->team;
2879 mutex_lock(&team->lock);
2880 __team_port_change_check(port, linkup);
2881 mutex_unlock(&team->lock);
2885 /************************************
2886 * Net device notifier event handler
2887 ************************************/
2889 static int team_device_event(struct notifier_block *unused,
2890 unsigned long event, void *ptr)
2892 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2893 struct team_port *port;
2895 port = team_port_get_rtnl(dev);
2896 if (!port)
2897 return NOTIFY_DONE;
2899 switch (event) {
2900 case NETDEV_UP:
2901 if (netif_carrier_ok(dev))
2902 team_port_change_check(port, true);
2903 break;
2904 case NETDEV_DOWN:
2905 team_port_change_check(port, false);
2906 break;
2907 case NETDEV_CHANGE:
2908 if (netif_running(port->dev))
2909 team_port_change_check(port,
2910 !!netif_carrier_ok(port->dev));
2911 break;
2912 case NETDEV_UNREGISTER:
2913 team_del_slave(port->team->dev, dev);
2914 break;
2915 case NETDEV_FEAT_CHANGE:
2916 team_compute_features(port->team);
2917 break;
2918 case NETDEV_PRECHANGEMTU:
2919 /* Forbid to change mtu of underlaying device */
2920 if (!port->team->port_mtu_change_allowed)
2921 return NOTIFY_BAD;
2922 break;
2923 case NETDEV_PRE_TYPE_CHANGE:
2924 /* Forbid to change type of underlaying device */
2925 return NOTIFY_BAD;
2926 case NETDEV_RESEND_IGMP:
2927 /* Propagate to master device */
2928 call_netdevice_notifiers(event, port->team->dev);
2929 break;
2931 return NOTIFY_DONE;
2934 static struct notifier_block team_notifier_block __read_mostly = {
2935 .notifier_call = team_device_event,
2939 /***********************
2940 * Module init and exit
2941 ***********************/
2943 static int __init team_module_init(void)
2945 int err;
2947 register_netdevice_notifier(&team_notifier_block);
2949 err = rtnl_link_register(&team_link_ops);
2950 if (err)
2951 goto err_rtnl_reg;
2953 err = team_nl_init();
2954 if (err)
2955 goto err_nl_init;
2957 return 0;
2959 err_nl_init:
2960 rtnl_link_unregister(&team_link_ops);
2962 err_rtnl_reg:
2963 unregister_netdevice_notifier(&team_notifier_block);
2965 return err;
2968 static void __exit team_module_exit(void)
2970 team_nl_fini();
2971 rtnl_link_unregister(&team_link_ops);
2972 unregister_netdevice_notifier(&team_notifier_block);
2975 module_init(team_module_init);
2976 module_exit(team_module_exit);
2978 MODULE_LICENSE("GPL v2");
2979 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2980 MODULE_DESCRIPTION("Ethernet team device driver");
2981 MODULE_ALIAS_RTNL_LINK(DRV_NAME);