Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / team / team.c
blob36916bf51ee6e64d6c45e60690e444cf168708f2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/net/team/team.c - Network team device driver
4 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
5 */
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/rcupdate.h>
13 #include <linux/errno.h>
14 #include <linux/ctype.h>
15 #include <linux/notifier.h>
16 #include <linux/netdevice.h>
17 #include <linux/netpoll.h>
18 #include <linux/if_vlan.h>
19 #include <linux/if_arp.h>
20 #include <linux/socket.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <net/rtnetlink.h>
24 #include <net/genetlink.h>
25 #include <net/netlink.h>
26 #include <net/sch_generic.h>
27 #include <generated/utsrelease.h>
28 #include <linux/if_team.h>
30 #define DRV_NAME "team"
33 /**********
34 * Helpers
35 **********/
37 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
39 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
41 return netif_is_team_port(dev) ? port : NULL;
45 * Since the ability to change device address for open port device is tested in
46 * team_port_add, this function can be called without control of return value
48 static int __set_port_dev_addr(struct net_device *port_dev,
49 const unsigned char *dev_addr)
51 struct sockaddr_storage addr;
53 memcpy(addr.__data, dev_addr, port_dev->addr_len);
54 addr.ss_family = port_dev->type;
55 return dev_set_mac_address(port_dev, (struct sockaddr *)&addr, NULL);
58 static int team_port_set_orig_dev_addr(struct team_port *port)
60 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
63 static int team_port_set_team_dev_addr(struct team *team,
64 struct team_port *port)
66 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
69 int team_modeop_port_enter(struct team *team, struct team_port *port)
71 return team_port_set_team_dev_addr(team, port);
73 EXPORT_SYMBOL(team_modeop_port_enter);
75 void team_modeop_port_change_dev_addr(struct team *team,
76 struct team_port *port)
78 team_port_set_team_dev_addr(team, port);
80 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
82 static void team_lower_state_changed(struct team_port *port)
84 struct netdev_lag_lower_state_info info;
86 info.link_up = port->linkup;
87 info.tx_enabled = team_port_enabled(port);
88 netdev_lower_state_changed(port->dev, &info);
91 static void team_refresh_port_linkup(struct team_port *port)
93 bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
94 port->state.linkup;
96 if (port->linkup != new_linkup) {
97 port->linkup = new_linkup;
98 team_lower_state_changed(port);
103 /*******************
104 * Options handling
105 *******************/
107 struct team_option_inst { /* One for each option instance */
108 struct list_head list;
109 struct list_head tmp_list;
110 struct team_option *option;
111 struct team_option_inst_info info;
112 bool changed;
113 bool removed;
116 static struct team_option *__team_find_option(struct team *team,
117 const char *opt_name)
119 struct team_option *option;
121 list_for_each_entry(option, &team->option_list, list) {
122 if (strcmp(option->name, opt_name) == 0)
123 return option;
125 return NULL;
128 static void __team_option_inst_del(struct team_option_inst *opt_inst)
130 list_del(&opt_inst->list);
131 kfree(opt_inst);
134 static void __team_option_inst_del_option(struct team *team,
135 struct team_option *option)
137 struct team_option_inst *opt_inst, *tmp;
139 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
140 if (opt_inst->option == option)
141 __team_option_inst_del(opt_inst);
145 static int __team_option_inst_add(struct team *team, struct team_option *option,
146 struct team_port *port)
148 struct team_option_inst *opt_inst;
149 unsigned int array_size;
150 unsigned int i;
151 int err;
153 array_size = option->array_size;
154 if (!array_size)
155 array_size = 1; /* No array but still need one instance */
157 for (i = 0; i < array_size; i++) {
158 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
159 if (!opt_inst)
160 return -ENOMEM;
161 opt_inst->option = option;
162 opt_inst->info.port = port;
163 opt_inst->info.array_index = i;
164 opt_inst->changed = true;
165 opt_inst->removed = false;
166 list_add_tail(&opt_inst->list, &team->option_inst_list);
167 if (option->init) {
168 err = option->init(team, &opt_inst->info);
169 if (err)
170 return err;
174 return 0;
177 static int __team_option_inst_add_option(struct team *team,
178 struct team_option *option)
180 int err;
182 if (!option->per_port) {
183 err = __team_option_inst_add(team, option, NULL);
184 if (err)
185 goto inst_del_option;
187 return 0;
189 inst_del_option:
190 __team_option_inst_del_option(team, option);
191 return err;
194 static void __team_option_inst_mark_removed_option(struct team *team,
195 struct team_option *option)
197 struct team_option_inst *opt_inst;
199 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
200 if (opt_inst->option == option) {
201 opt_inst->changed = true;
202 opt_inst->removed = true;
207 static void __team_option_inst_del_port(struct team *team,
208 struct team_port *port)
210 struct team_option_inst *opt_inst, *tmp;
212 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
213 if (opt_inst->option->per_port &&
214 opt_inst->info.port == port)
215 __team_option_inst_del(opt_inst);
219 static int __team_option_inst_add_port(struct team *team,
220 struct team_port *port)
222 struct team_option *option;
223 int err;
225 list_for_each_entry(option, &team->option_list, list) {
226 if (!option->per_port)
227 continue;
228 err = __team_option_inst_add(team, option, port);
229 if (err)
230 goto inst_del_port;
232 return 0;
234 inst_del_port:
235 __team_option_inst_del_port(team, port);
236 return err;
239 static void __team_option_inst_mark_removed_port(struct team *team,
240 struct team_port *port)
242 struct team_option_inst *opt_inst;
244 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
245 if (opt_inst->info.port == port) {
246 opt_inst->changed = true;
247 opt_inst->removed = true;
252 static int __team_options_register(struct team *team,
253 const struct team_option *option,
254 size_t option_count)
256 int i;
257 struct team_option **dst_opts;
258 int err;
260 dst_opts = kcalloc(option_count, sizeof(struct team_option *),
261 GFP_KERNEL);
262 if (!dst_opts)
263 return -ENOMEM;
264 for (i = 0; i < option_count; i++, option++) {
265 if (__team_find_option(team, option->name)) {
266 err = -EEXIST;
267 goto alloc_rollback;
269 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
270 if (!dst_opts[i]) {
271 err = -ENOMEM;
272 goto alloc_rollback;
276 for (i = 0; i < option_count; i++) {
277 err = __team_option_inst_add_option(team, dst_opts[i]);
278 if (err)
279 goto inst_rollback;
280 list_add_tail(&dst_opts[i]->list, &team->option_list);
283 kfree(dst_opts);
284 return 0;
286 inst_rollback:
287 for (i--; i >= 0; i--)
288 __team_option_inst_del_option(team, dst_opts[i]);
290 i = option_count - 1;
291 alloc_rollback:
292 for (i--; i >= 0; i--)
293 kfree(dst_opts[i]);
295 kfree(dst_opts);
296 return err;
299 static void __team_options_mark_removed(struct team *team,
300 const struct team_option *option,
301 size_t option_count)
303 int i;
305 for (i = 0; i < option_count; i++, option++) {
306 struct team_option *del_opt;
308 del_opt = __team_find_option(team, option->name);
309 if (del_opt)
310 __team_option_inst_mark_removed_option(team, del_opt);
314 static void __team_options_unregister(struct team *team,
315 const struct team_option *option,
316 size_t option_count)
318 int i;
320 for (i = 0; i < option_count; i++, option++) {
321 struct team_option *del_opt;
323 del_opt = __team_find_option(team, option->name);
324 if (del_opt) {
325 __team_option_inst_del_option(team, del_opt);
326 list_del(&del_opt->list);
327 kfree(del_opt);
332 static void __team_options_change_check(struct team *team);
334 int team_options_register(struct team *team,
335 const struct team_option *option,
336 size_t option_count)
338 int err;
340 err = __team_options_register(team, option, option_count);
341 if (err)
342 return err;
343 __team_options_change_check(team);
344 return 0;
346 EXPORT_SYMBOL(team_options_register);
348 void team_options_unregister(struct team *team,
349 const struct team_option *option,
350 size_t option_count)
352 __team_options_mark_removed(team, option, option_count);
353 __team_options_change_check(team);
354 __team_options_unregister(team, option, option_count);
356 EXPORT_SYMBOL(team_options_unregister);
358 static int team_option_get(struct team *team,
359 struct team_option_inst *opt_inst,
360 struct team_gsetter_ctx *ctx)
362 if (!opt_inst->option->getter)
363 return -EOPNOTSUPP;
364 return opt_inst->option->getter(team, ctx);
367 static int team_option_set(struct team *team,
368 struct team_option_inst *opt_inst,
369 struct team_gsetter_ctx *ctx)
371 if (!opt_inst->option->setter)
372 return -EOPNOTSUPP;
373 return opt_inst->option->setter(team, ctx);
376 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
378 struct team_option_inst *opt_inst;
380 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
381 opt_inst->changed = true;
383 EXPORT_SYMBOL(team_option_inst_set_change);
385 void team_options_change_check(struct team *team)
387 __team_options_change_check(team);
389 EXPORT_SYMBOL(team_options_change_check);
392 /****************
393 * Mode handling
394 ****************/
396 static LIST_HEAD(mode_list);
397 static DEFINE_SPINLOCK(mode_list_lock);
399 struct team_mode_item {
400 struct list_head list;
401 const struct team_mode *mode;
404 static struct team_mode_item *__find_mode(const char *kind)
406 struct team_mode_item *mitem;
408 list_for_each_entry(mitem, &mode_list, list) {
409 if (strcmp(mitem->mode->kind, kind) == 0)
410 return mitem;
412 return NULL;
415 static bool is_good_mode_name(const char *name)
417 while (*name != '\0') {
418 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
419 return false;
420 name++;
422 return true;
425 int team_mode_register(const struct team_mode *mode)
427 int err = 0;
428 struct team_mode_item *mitem;
430 if (!is_good_mode_name(mode->kind) ||
431 mode->priv_size > TEAM_MODE_PRIV_SIZE)
432 return -EINVAL;
434 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
435 if (!mitem)
436 return -ENOMEM;
438 spin_lock(&mode_list_lock);
439 if (__find_mode(mode->kind)) {
440 err = -EEXIST;
441 kfree(mitem);
442 goto unlock;
444 mitem->mode = mode;
445 list_add_tail(&mitem->list, &mode_list);
446 unlock:
447 spin_unlock(&mode_list_lock);
448 return err;
450 EXPORT_SYMBOL(team_mode_register);
452 void team_mode_unregister(const struct team_mode *mode)
454 struct team_mode_item *mitem;
456 spin_lock(&mode_list_lock);
457 mitem = __find_mode(mode->kind);
458 if (mitem) {
459 list_del_init(&mitem->list);
460 kfree(mitem);
462 spin_unlock(&mode_list_lock);
464 EXPORT_SYMBOL(team_mode_unregister);
466 static const struct team_mode *team_mode_get(const char *kind)
468 struct team_mode_item *mitem;
469 const struct team_mode *mode = NULL;
471 spin_lock(&mode_list_lock);
472 mitem = __find_mode(kind);
473 if (!mitem) {
474 spin_unlock(&mode_list_lock);
475 request_module("team-mode-%s", kind);
476 spin_lock(&mode_list_lock);
477 mitem = __find_mode(kind);
479 if (mitem) {
480 mode = mitem->mode;
481 if (!try_module_get(mode->owner))
482 mode = NULL;
485 spin_unlock(&mode_list_lock);
486 return mode;
489 static void team_mode_put(const struct team_mode *mode)
491 module_put(mode->owner);
494 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
496 dev_kfree_skb_any(skb);
497 return false;
500 static rx_handler_result_t team_dummy_receive(struct team *team,
501 struct team_port *port,
502 struct sk_buff *skb)
504 return RX_HANDLER_ANOTHER;
507 static const struct team_mode __team_no_mode = {
508 .kind = "*NOMODE*",
511 static bool team_is_mode_set(struct team *team)
513 return team->mode != &__team_no_mode;
516 static void team_set_no_mode(struct team *team)
518 team->user_carrier_enabled = false;
519 team->mode = &__team_no_mode;
522 static void team_adjust_ops(struct team *team)
525 * To avoid checks in rx/tx skb paths, ensure here that non-null and
526 * correct ops are always set.
529 if (!team->en_port_count || !team_is_mode_set(team) ||
530 !team->mode->ops->transmit)
531 team->ops.transmit = team_dummy_transmit;
532 else
533 team->ops.transmit = team->mode->ops->transmit;
535 if (!team->en_port_count || !team_is_mode_set(team) ||
536 !team->mode->ops->receive)
537 team->ops.receive = team_dummy_receive;
538 else
539 team->ops.receive = team->mode->ops->receive;
543 * We can benefit from the fact that it's ensured no port is present
544 * at the time of mode change. Therefore no packets are in fly so there's no
545 * need to set mode operations in any special way.
547 static int __team_change_mode(struct team *team,
548 const struct team_mode *new_mode)
550 /* Check if mode was previously set and do cleanup if so */
551 if (team_is_mode_set(team)) {
552 void (*exit_op)(struct team *team) = team->ops.exit;
554 /* Clear ops area so no callback is called any longer */
555 memset(&team->ops, 0, sizeof(struct team_mode_ops));
556 team_adjust_ops(team);
558 if (exit_op)
559 exit_op(team);
560 team_mode_put(team->mode);
561 team_set_no_mode(team);
562 /* zero private data area */
563 memset(&team->mode_priv, 0,
564 sizeof(struct team) - offsetof(struct team, mode_priv));
567 if (!new_mode)
568 return 0;
570 if (new_mode->ops->init) {
571 int err;
573 err = new_mode->ops->init(team);
574 if (err)
575 return err;
578 team->mode = new_mode;
579 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
580 team_adjust_ops(team);
582 return 0;
585 static int team_change_mode(struct team *team, const char *kind)
587 const struct team_mode *new_mode;
588 struct net_device *dev = team->dev;
589 int err;
591 if (!list_empty(&team->port_list)) {
592 netdev_err(dev, "No ports can be present during mode change\n");
593 return -EBUSY;
596 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
597 netdev_err(dev, "Unable to change to the same mode the team is in\n");
598 return -EINVAL;
601 new_mode = team_mode_get(kind);
602 if (!new_mode) {
603 netdev_err(dev, "Mode \"%s\" not found\n", kind);
604 return -EINVAL;
607 err = __team_change_mode(team, new_mode);
608 if (err) {
609 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
610 team_mode_put(new_mode);
611 return err;
614 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
615 return 0;
619 /*********************
620 * Peers notification
621 *********************/
623 static void team_notify_peers_work(struct work_struct *work)
625 struct team *team;
626 int val;
628 team = container_of(work, struct team, notify_peers.dw.work);
630 if (!rtnl_trylock()) {
631 schedule_delayed_work(&team->notify_peers.dw, 0);
632 return;
634 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
635 if (val < 0) {
636 rtnl_unlock();
637 return;
639 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
640 rtnl_unlock();
641 if (val)
642 schedule_delayed_work(&team->notify_peers.dw,
643 msecs_to_jiffies(team->notify_peers.interval));
646 static void team_notify_peers(struct team *team)
648 if (!team->notify_peers.count || !netif_running(team->dev))
649 return;
650 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
651 schedule_delayed_work(&team->notify_peers.dw, 0);
654 static void team_notify_peers_init(struct team *team)
656 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
659 static void team_notify_peers_fini(struct team *team)
661 cancel_delayed_work_sync(&team->notify_peers.dw);
665 /*******************************
666 * Send multicast group rejoins
667 *******************************/
669 static void team_mcast_rejoin_work(struct work_struct *work)
671 struct team *team;
672 int val;
674 team = container_of(work, struct team, mcast_rejoin.dw.work);
676 if (!rtnl_trylock()) {
677 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
678 return;
680 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
681 if (val < 0) {
682 rtnl_unlock();
683 return;
685 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
686 rtnl_unlock();
687 if (val)
688 schedule_delayed_work(&team->mcast_rejoin.dw,
689 msecs_to_jiffies(team->mcast_rejoin.interval));
692 static void team_mcast_rejoin(struct team *team)
694 if (!team->mcast_rejoin.count || !netif_running(team->dev))
695 return;
696 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
697 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
700 static void team_mcast_rejoin_init(struct team *team)
702 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
705 static void team_mcast_rejoin_fini(struct team *team)
707 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
711 /************************
712 * Rx path frame handler
713 ************************/
715 /* note: already called with rcu_read_lock */
716 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
718 struct sk_buff *skb = *pskb;
719 struct team_port *port;
720 struct team *team;
721 rx_handler_result_t res;
723 skb = skb_share_check(skb, GFP_ATOMIC);
724 if (!skb)
725 return RX_HANDLER_CONSUMED;
727 *pskb = skb;
729 port = team_port_get_rcu(skb->dev);
730 team = port->team;
731 if (!team_port_enabled(port)) {
732 /* allow exact match delivery for disabled ports */
733 res = RX_HANDLER_EXACT;
734 } else {
735 res = team->ops.receive(team, port, skb);
737 if (res == RX_HANDLER_ANOTHER) {
738 struct team_pcpu_stats *pcpu_stats;
740 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
741 u64_stats_update_begin(&pcpu_stats->syncp);
742 pcpu_stats->rx_packets++;
743 pcpu_stats->rx_bytes += skb->len;
744 if (skb->pkt_type == PACKET_MULTICAST)
745 pcpu_stats->rx_multicast++;
746 u64_stats_update_end(&pcpu_stats->syncp);
748 skb->dev = team->dev;
749 } else if (res == RX_HANDLER_EXACT) {
750 this_cpu_inc(team->pcpu_stats->rx_nohandler);
751 } else {
752 this_cpu_inc(team->pcpu_stats->rx_dropped);
755 return res;
759 /*************************************
760 * Multiqueue Tx port select override
761 *************************************/
763 static int team_queue_override_init(struct team *team)
765 struct list_head *listarr;
766 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
767 unsigned int i;
769 if (!queue_cnt)
770 return 0;
771 listarr = kmalloc_array(queue_cnt, sizeof(struct list_head),
772 GFP_KERNEL);
773 if (!listarr)
774 return -ENOMEM;
775 team->qom_lists = listarr;
776 for (i = 0; i < queue_cnt; i++)
777 INIT_LIST_HEAD(listarr++);
778 return 0;
781 static void team_queue_override_fini(struct team *team)
783 kfree(team->qom_lists);
786 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
788 return &team->qom_lists[queue_id - 1];
792 * note: already called with rcu_read_lock
794 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
796 struct list_head *qom_list;
797 struct team_port *port;
799 if (!team->queue_override_enabled || !skb->queue_mapping)
800 return false;
801 qom_list = __team_get_qom_list(team, skb->queue_mapping);
802 list_for_each_entry_rcu(port, qom_list, qom_list) {
803 if (!team_dev_queue_xmit(team, port, skb))
804 return true;
806 return false;
809 static void __team_queue_override_port_del(struct team *team,
810 struct team_port *port)
812 if (!port->queue_id)
813 return;
814 list_del_rcu(&port->qom_list);
817 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
818 struct team_port *cur)
820 if (port->priority < cur->priority)
821 return true;
822 if (port->priority > cur->priority)
823 return false;
824 if (port->index < cur->index)
825 return true;
826 return false;
829 static void __team_queue_override_port_add(struct team *team,
830 struct team_port *port)
832 struct team_port *cur;
833 struct list_head *qom_list;
834 struct list_head *node;
836 if (!port->queue_id)
837 return;
838 qom_list = __team_get_qom_list(team, port->queue_id);
839 node = qom_list;
840 list_for_each_entry(cur, qom_list, qom_list) {
841 if (team_queue_override_port_has_gt_prio_than(port, cur))
842 break;
843 node = &cur->qom_list;
845 list_add_tail_rcu(&port->qom_list, node);
848 static void __team_queue_override_enabled_check(struct team *team)
850 struct team_port *port;
851 bool enabled = false;
853 list_for_each_entry(port, &team->port_list, list) {
854 if (port->queue_id) {
855 enabled = true;
856 break;
859 if (enabled == team->queue_override_enabled)
860 return;
861 netdev_dbg(team->dev, "%s queue override\n",
862 enabled ? "Enabling" : "Disabling");
863 team->queue_override_enabled = enabled;
866 static void team_queue_override_port_prio_changed(struct team *team,
867 struct team_port *port)
869 if (!port->queue_id || team_port_enabled(port))
870 return;
871 __team_queue_override_port_del(team, port);
872 __team_queue_override_port_add(team, port);
873 __team_queue_override_enabled_check(team);
876 static void team_queue_override_port_change_queue_id(struct team *team,
877 struct team_port *port,
878 u16 new_queue_id)
880 if (team_port_enabled(port)) {
881 __team_queue_override_port_del(team, port);
882 port->queue_id = new_queue_id;
883 __team_queue_override_port_add(team, port);
884 __team_queue_override_enabled_check(team);
885 } else {
886 port->queue_id = new_queue_id;
890 static void team_queue_override_port_add(struct team *team,
891 struct team_port *port)
893 __team_queue_override_port_add(team, port);
894 __team_queue_override_enabled_check(team);
897 static void team_queue_override_port_del(struct team *team,
898 struct team_port *port)
900 __team_queue_override_port_del(team, port);
901 __team_queue_override_enabled_check(team);
905 /****************
906 * Port handling
907 ****************/
909 static bool team_port_find(const struct team *team,
910 const struct team_port *port)
912 struct team_port *cur;
914 list_for_each_entry(cur, &team->port_list, list)
915 if (cur == port)
916 return true;
917 return false;
921 * Enable/disable port by adding to enabled port hashlist and setting
922 * port->index (Might be racy so reader could see incorrect ifindex when
923 * processing a flying packet, but that is not a problem). Write guarded
924 * by team->lock.
926 static void team_port_enable(struct team *team,
927 struct team_port *port)
929 if (team_port_enabled(port))
930 return;
931 port->index = team->en_port_count++;
932 hlist_add_head_rcu(&port->hlist,
933 team_port_index_hash(team, port->index));
934 team_adjust_ops(team);
935 team_queue_override_port_add(team, port);
936 if (team->ops.port_enabled)
937 team->ops.port_enabled(team, port);
938 team_notify_peers(team);
939 team_mcast_rejoin(team);
940 team_lower_state_changed(port);
943 static void __reconstruct_port_hlist(struct team *team, int rm_index)
945 int i;
946 struct team_port *port;
948 for (i = rm_index + 1; i < team->en_port_count; i++) {
949 port = team_get_port_by_index(team, i);
950 hlist_del_rcu(&port->hlist);
951 port->index--;
952 hlist_add_head_rcu(&port->hlist,
953 team_port_index_hash(team, port->index));
957 static void team_port_disable(struct team *team,
958 struct team_port *port)
960 if (!team_port_enabled(port))
961 return;
962 if (team->ops.port_disabled)
963 team->ops.port_disabled(team, port);
964 hlist_del_rcu(&port->hlist);
965 __reconstruct_port_hlist(team, port->index);
966 port->index = -1;
967 team->en_port_count--;
968 team_queue_override_port_del(team, port);
969 team_adjust_ops(team);
970 team_lower_state_changed(port);
973 #define TEAM_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
974 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
975 NETIF_F_HIGHDMA | NETIF_F_LRO)
977 #define TEAM_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
978 NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
980 static void __team_compute_features(struct team *team)
982 struct team_port *port;
983 netdev_features_t vlan_features = TEAM_VLAN_FEATURES &
984 NETIF_F_ALL_FOR_ALL;
985 netdev_features_t enc_features = TEAM_ENC_FEATURES;
986 unsigned short max_hard_header_len = ETH_HLEN;
987 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
988 IFF_XMIT_DST_RELEASE_PERM;
990 list_for_each_entry(port, &team->port_list, list) {
991 vlan_features = netdev_increment_features(vlan_features,
992 port->dev->vlan_features,
993 TEAM_VLAN_FEATURES);
994 enc_features =
995 netdev_increment_features(enc_features,
996 port->dev->hw_enc_features,
997 TEAM_ENC_FEATURES);
1000 dst_release_flag &= port->dev->priv_flags;
1001 if (port->dev->hard_header_len > max_hard_header_len)
1002 max_hard_header_len = port->dev->hard_header_len;
1005 team->dev->vlan_features = vlan_features;
1006 team->dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1007 NETIF_F_GSO_UDP_L4;
1008 team->dev->hard_header_len = max_hard_header_len;
1010 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1011 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1012 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1015 static void team_compute_features(struct team *team)
1017 mutex_lock(&team->lock);
1018 __team_compute_features(team);
1019 mutex_unlock(&team->lock);
1020 netdev_change_features(team->dev);
1023 static int team_port_enter(struct team *team, struct team_port *port)
1025 int err = 0;
1027 dev_hold(team->dev);
1028 if (team->ops.port_enter) {
1029 err = team->ops.port_enter(team, port);
1030 if (err) {
1031 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1032 port->dev->name);
1033 goto err_port_enter;
1037 return 0;
1039 err_port_enter:
1040 dev_put(team->dev);
1042 return err;
1045 static void team_port_leave(struct team *team, struct team_port *port)
1047 if (team->ops.port_leave)
1048 team->ops.port_leave(team, port);
1049 dev_put(team->dev);
1052 #ifdef CONFIG_NET_POLL_CONTROLLER
1053 static int __team_port_enable_netpoll(struct team_port *port)
1055 struct netpoll *np;
1056 int err;
1058 np = kzalloc(sizeof(*np), GFP_KERNEL);
1059 if (!np)
1060 return -ENOMEM;
1062 err = __netpoll_setup(np, port->dev);
1063 if (err) {
1064 kfree(np);
1065 return err;
1067 port->np = np;
1068 return err;
1071 static int team_port_enable_netpoll(struct team_port *port)
1073 if (!port->team->dev->npinfo)
1074 return 0;
1076 return __team_port_enable_netpoll(port);
1079 static void team_port_disable_netpoll(struct team_port *port)
1081 struct netpoll *np = port->np;
1083 if (!np)
1084 return;
1085 port->np = NULL;
1087 __netpoll_free(np);
1089 #else
1090 static int team_port_enable_netpoll(struct team_port *port)
1092 return 0;
1094 static void team_port_disable_netpoll(struct team_port *port)
1097 #endif
1099 static int team_upper_dev_link(struct team *team, struct team_port *port,
1100 struct netlink_ext_ack *extack)
1102 struct netdev_lag_upper_info lag_upper_info;
1103 int err;
1105 lag_upper_info.tx_type = team->mode->lag_tx_type;
1106 lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
1107 err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
1108 &lag_upper_info, extack);
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 team *team, struct team_port *port)
1117 netdev_upper_dev_unlink(port->dev, team->dev);
1118 port->dev->priv_flags &= ~IFF_TEAM_PORT;
1121 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1122 static int team_dev_type_check_change(struct net_device *dev,
1123 struct net_device *port_dev);
1125 static int team_port_add(struct team *team, struct net_device *port_dev,
1126 struct netlink_ext_ack *extack)
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 NL_SET_ERR_MSG(extack, "Loopback device can't be added as a team port");
1135 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1136 portname);
1137 return -EINVAL;
1140 if (netif_is_team_port(port_dev)) {
1141 NL_SET_ERR_MSG(extack, "Device is already a port of a team device");
1142 netdev_err(dev, "Device %s is already a port "
1143 "of a team device\n", portname);
1144 return -EBUSY;
1147 if (dev == port_dev) {
1148 NL_SET_ERR_MSG(extack, "Cannot enslave team device to itself");
1149 netdev_err(dev, "Cannot enslave team device to itself\n");
1150 return -EINVAL;
1153 if (netdev_has_upper_dev(dev, port_dev)) {
1154 NL_SET_ERR_MSG(extack, "Device is already an upper device of the team interface");
1155 netdev_err(dev, "Device %s is already an upper device of the team interface\n",
1156 portname);
1157 return -EBUSY;
1160 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1161 vlan_uses_dev(dev)) {
1162 NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
1163 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1164 portname);
1165 return -EPERM;
1168 err = team_dev_type_check_change(dev, port_dev);
1169 if (err)
1170 return err;
1172 if (port_dev->flags & IFF_UP) {
1173 NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
1174 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1175 portname);
1176 return -EBUSY;
1179 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1180 GFP_KERNEL);
1181 if (!port)
1182 return -ENOMEM;
1184 port->dev = port_dev;
1185 port->team = team;
1186 INIT_LIST_HEAD(&port->qom_list);
1188 port->orig.mtu = port_dev->mtu;
1189 err = dev_set_mtu(port_dev, dev->mtu);
1190 if (err) {
1191 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1192 goto err_set_mtu;
1195 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1197 err = team_port_enter(team, port);
1198 if (err) {
1199 netdev_err(dev, "Device %s failed to enter team mode\n",
1200 portname);
1201 goto err_port_enter;
1204 err = dev_open(port_dev, extack);
1205 if (err) {
1206 netdev_dbg(dev, "Device %s opening failed\n",
1207 portname);
1208 goto err_dev_open;
1211 err = vlan_vids_add_by_dev(port_dev, dev);
1212 if (err) {
1213 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1214 portname);
1215 goto err_vids_add;
1218 err = team_port_enable_netpoll(port);
1219 if (err) {
1220 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1221 portname);
1222 goto err_enable_netpoll;
1225 if (!(dev->features & NETIF_F_LRO))
1226 dev_disable_lro(port_dev);
1228 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1229 port);
1230 if (err) {
1231 netdev_err(dev, "Device %s failed to register rx_handler\n",
1232 portname);
1233 goto err_handler_register;
1236 err = team_upper_dev_link(team, port, extack);
1237 if (err) {
1238 netdev_err(dev, "Device %s failed to set upper link\n",
1239 portname);
1240 goto err_set_upper_link;
1243 err = __team_option_inst_add_port(team, port);
1244 if (err) {
1245 netdev_err(dev, "Device %s failed to add per-port options\n",
1246 portname);
1247 goto err_option_port_add;
1250 /* set promiscuity level to new slave */
1251 if (dev->flags & IFF_PROMISC) {
1252 err = dev_set_promiscuity(port_dev, 1);
1253 if (err)
1254 goto err_set_slave_promisc;
1257 /* set allmulti level to new slave */
1258 if (dev->flags & IFF_ALLMULTI) {
1259 err = dev_set_allmulti(port_dev, 1);
1260 if (err) {
1261 if (dev->flags & IFF_PROMISC)
1262 dev_set_promiscuity(port_dev, -1);
1263 goto err_set_slave_promisc;
1267 netif_addr_lock_bh(dev);
1268 dev_uc_sync_multiple(port_dev, dev);
1269 dev_mc_sync_multiple(port_dev, dev);
1270 netif_addr_unlock_bh(dev);
1272 port->index = -1;
1273 list_add_tail_rcu(&port->list, &team->port_list);
1274 team_port_enable(team, port);
1275 __team_compute_features(team);
1276 __team_port_change_port_added(port, !!netif_oper_up(port_dev));
1277 __team_options_change_check(team);
1279 netdev_info(dev, "Port device %s added\n", portname);
1281 return 0;
1283 err_set_slave_promisc:
1284 __team_option_inst_del_port(team, port);
1286 err_option_port_add:
1287 team_upper_dev_unlink(team, port);
1289 err_set_upper_link:
1290 netdev_rx_handler_unregister(port_dev);
1292 err_handler_register:
1293 team_port_disable_netpoll(port);
1295 err_enable_netpoll:
1296 vlan_vids_del_by_dev(port_dev, dev);
1298 err_vids_add:
1299 dev_close(port_dev);
1301 err_dev_open:
1302 team_port_leave(team, port);
1303 team_port_set_orig_dev_addr(port);
1305 err_port_enter:
1306 dev_set_mtu(port_dev, port->orig.mtu);
1308 err_set_mtu:
1309 kfree(port);
1311 return err;
1314 static void __team_port_change_port_removed(struct team_port *port);
1316 static int team_port_del(struct team *team, struct net_device *port_dev)
1318 struct net_device *dev = team->dev;
1319 struct team_port *port;
1320 char *portname = port_dev->name;
1322 port = team_port_get_rtnl(port_dev);
1323 if (!port || !team_port_find(team, port)) {
1324 netdev_err(dev, "Device %s does not act as a port of this team\n",
1325 portname);
1326 return -ENOENT;
1329 team_port_disable(team, port);
1330 list_del_rcu(&port->list);
1332 if (dev->flags & IFF_PROMISC)
1333 dev_set_promiscuity(port_dev, -1);
1334 if (dev->flags & IFF_ALLMULTI)
1335 dev_set_allmulti(port_dev, -1);
1337 team_upper_dev_unlink(team, port);
1338 netdev_rx_handler_unregister(port_dev);
1339 team_port_disable_netpoll(port);
1340 vlan_vids_del_by_dev(port_dev, dev);
1341 dev_uc_unsync(port_dev, dev);
1342 dev_mc_unsync(port_dev, dev);
1343 dev_close(port_dev);
1344 team_port_leave(team, port);
1346 __team_option_inst_mark_removed_port(team, port);
1347 __team_options_change_check(team);
1348 __team_option_inst_del_port(team, port);
1349 __team_port_change_port_removed(port);
1351 team_port_set_orig_dev_addr(port);
1352 dev_set_mtu(port_dev, port->orig.mtu);
1353 kfree_rcu(port, rcu);
1354 netdev_info(dev, "Port device %s removed\n", portname);
1355 __team_compute_features(team);
1357 return 0;
1361 /*****************
1362 * Net device ops
1363 *****************/
1365 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1367 ctx->data.str_val = team->mode->kind;
1368 return 0;
1371 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1373 return team_change_mode(team, ctx->data.str_val);
1376 static int team_notify_peers_count_get(struct team *team,
1377 struct team_gsetter_ctx *ctx)
1379 ctx->data.u32_val = team->notify_peers.count;
1380 return 0;
1383 static int team_notify_peers_count_set(struct team *team,
1384 struct team_gsetter_ctx *ctx)
1386 team->notify_peers.count = ctx->data.u32_val;
1387 return 0;
1390 static int team_notify_peers_interval_get(struct team *team,
1391 struct team_gsetter_ctx *ctx)
1393 ctx->data.u32_val = team->notify_peers.interval;
1394 return 0;
1397 static int team_notify_peers_interval_set(struct team *team,
1398 struct team_gsetter_ctx *ctx)
1400 team->notify_peers.interval = ctx->data.u32_val;
1401 return 0;
1404 static int team_mcast_rejoin_count_get(struct team *team,
1405 struct team_gsetter_ctx *ctx)
1407 ctx->data.u32_val = team->mcast_rejoin.count;
1408 return 0;
1411 static int team_mcast_rejoin_count_set(struct team *team,
1412 struct team_gsetter_ctx *ctx)
1414 team->mcast_rejoin.count = ctx->data.u32_val;
1415 return 0;
1418 static int team_mcast_rejoin_interval_get(struct team *team,
1419 struct team_gsetter_ctx *ctx)
1421 ctx->data.u32_val = team->mcast_rejoin.interval;
1422 return 0;
1425 static int team_mcast_rejoin_interval_set(struct team *team,
1426 struct team_gsetter_ctx *ctx)
1428 team->mcast_rejoin.interval = ctx->data.u32_val;
1429 return 0;
1432 static int team_port_en_option_get(struct team *team,
1433 struct team_gsetter_ctx *ctx)
1435 struct team_port *port = ctx->info->port;
1437 ctx->data.bool_val = team_port_enabled(port);
1438 return 0;
1441 static int team_port_en_option_set(struct team *team,
1442 struct team_gsetter_ctx *ctx)
1444 struct team_port *port = ctx->info->port;
1446 if (ctx->data.bool_val)
1447 team_port_enable(team, port);
1448 else
1449 team_port_disable(team, port);
1450 return 0;
1453 static int team_user_linkup_option_get(struct team *team,
1454 struct team_gsetter_ctx *ctx)
1456 struct team_port *port = ctx->info->port;
1458 ctx->data.bool_val = port->user.linkup;
1459 return 0;
1462 static void __team_carrier_check(struct team *team);
1464 static int team_user_linkup_option_set(struct team *team,
1465 struct team_gsetter_ctx *ctx)
1467 struct team_port *port = ctx->info->port;
1469 port->user.linkup = ctx->data.bool_val;
1470 team_refresh_port_linkup(port);
1471 __team_carrier_check(port->team);
1472 return 0;
1475 static int team_user_linkup_en_option_get(struct team *team,
1476 struct team_gsetter_ctx *ctx)
1478 struct team_port *port = ctx->info->port;
1480 ctx->data.bool_val = port->user.linkup_enabled;
1481 return 0;
1484 static int team_user_linkup_en_option_set(struct team *team,
1485 struct team_gsetter_ctx *ctx)
1487 struct team_port *port = ctx->info->port;
1489 port->user.linkup_enabled = ctx->data.bool_val;
1490 team_refresh_port_linkup(port);
1491 __team_carrier_check(port->team);
1492 return 0;
1495 static int team_priority_option_get(struct team *team,
1496 struct team_gsetter_ctx *ctx)
1498 struct team_port *port = ctx->info->port;
1500 ctx->data.s32_val = port->priority;
1501 return 0;
1504 static int team_priority_option_set(struct team *team,
1505 struct team_gsetter_ctx *ctx)
1507 struct team_port *port = ctx->info->port;
1508 s32 priority = ctx->data.s32_val;
1510 if (port->priority == priority)
1511 return 0;
1512 port->priority = priority;
1513 team_queue_override_port_prio_changed(team, port);
1514 return 0;
1517 static int team_queue_id_option_get(struct team *team,
1518 struct team_gsetter_ctx *ctx)
1520 struct team_port *port = ctx->info->port;
1522 ctx->data.u32_val = port->queue_id;
1523 return 0;
1526 static int team_queue_id_option_set(struct team *team,
1527 struct team_gsetter_ctx *ctx)
1529 struct team_port *port = ctx->info->port;
1530 u16 new_queue_id = ctx->data.u32_val;
1532 if (port->queue_id == new_queue_id)
1533 return 0;
1534 if (new_queue_id >= team->dev->real_num_tx_queues)
1535 return -EINVAL;
1536 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1537 return 0;
1540 static const struct team_option team_options[] = {
1542 .name = "mode",
1543 .type = TEAM_OPTION_TYPE_STRING,
1544 .getter = team_mode_option_get,
1545 .setter = team_mode_option_set,
1548 .name = "notify_peers_count",
1549 .type = TEAM_OPTION_TYPE_U32,
1550 .getter = team_notify_peers_count_get,
1551 .setter = team_notify_peers_count_set,
1554 .name = "notify_peers_interval",
1555 .type = TEAM_OPTION_TYPE_U32,
1556 .getter = team_notify_peers_interval_get,
1557 .setter = team_notify_peers_interval_set,
1560 .name = "mcast_rejoin_count",
1561 .type = TEAM_OPTION_TYPE_U32,
1562 .getter = team_mcast_rejoin_count_get,
1563 .setter = team_mcast_rejoin_count_set,
1566 .name = "mcast_rejoin_interval",
1567 .type = TEAM_OPTION_TYPE_U32,
1568 .getter = team_mcast_rejoin_interval_get,
1569 .setter = team_mcast_rejoin_interval_set,
1572 .name = "enabled",
1573 .type = TEAM_OPTION_TYPE_BOOL,
1574 .per_port = true,
1575 .getter = team_port_en_option_get,
1576 .setter = team_port_en_option_set,
1579 .name = "user_linkup",
1580 .type = TEAM_OPTION_TYPE_BOOL,
1581 .per_port = true,
1582 .getter = team_user_linkup_option_get,
1583 .setter = team_user_linkup_option_set,
1586 .name = "user_linkup_enabled",
1587 .type = TEAM_OPTION_TYPE_BOOL,
1588 .per_port = true,
1589 .getter = team_user_linkup_en_option_get,
1590 .setter = team_user_linkup_en_option_set,
1593 .name = "priority",
1594 .type = TEAM_OPTION_TYPE_S32,
1595 .per_port = true,
1596 .getter = team_priority_option_get,
1597 .setter = team_priority_option_set,
1600 .name = "queue_id",
1601 .type = TEAM_OPTION_TYPE_U32,
1602 .per_port = true,
1603 .getter = team_queue_id_option_get,
1604 .setter = team_queue_id_option_set,
1609 static int team_init(struct net_device *dev)
1611 struct team *team = netdev_priv(dev);
1612 int i;
1613 int err;
1615 team->dev = dev;
1616 mutex_init(&team->lock);
1617 team_set_no_mode(team);
1619 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1620 if (!team->pcpu_stats)
1621 return -ENOMEM;
1623 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1624 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1625 INIT_LIST_HEAD(&team->port_list);
1626 err = team_queue_override_init(team);
1627 if (err)
1628 goto err_team_queue_override_init;
1630 team_adjust_ops(team);
1632 INIT_LIST_HEAD(&team->option_list);
1633 INIT_LIST_HEAD(&team->option_inst_list);
1635 team_notify_peers_init(team);
1636 team_mcast_rejoin_init(team);
1638 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1639 if (err)
1640 goto err_options_register;
1641 netif_carrier_off(dev);
1643 netdev_lockdep_set_classes(dev);
1645 return 0;
1647 err_options_register:
1648 team_mcast_rejoin_fini(team);
1649 team_notify_peers_fini(team);
1650 team_queue_override_fini(team);
1651 err_team_queue_override_init:
1652 free_percpu(team->pcpu_stats);
1654 return err;
1657 static void team_uninit(struct net_device *dev)
1659 struct team *team = netdev_priv(dev);
1660 struct team_port *port;
1661 struct team_port *tmp;
1663 mutex_lock(&team->lock);
1664 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1665 team_port_del(team, port->dev);
1667 __team_change_mode(team, NULL); /* cleanup */
1668 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1669 team_mcast_rejoin_fini(team);
1670 team_notify_peers_fini(team);
1671 team_queue_override_fini(team);
1672 mutex_unlock(&team->lock);
1673 netdev_change_features(dev);
1676 static void team_destructor(struct net_device *dev)
1678 struct team *team = netdev_priv(dev);
1680 free_percpu(team->pcpu_stats);
1683 static int team_open(struct net_device *dev)
1685 return 0;
1688 static int team_close(struct net_device *dev)
1690 return 0;
1694 * note: already called with rcu_read_lock
1696 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1698 struct team *team = netdev_priv(dev);
1699 bool tx_success;
1700 unsigned int len = skb->len;
1702 tx_success = team_queue_override_transmit(team, skb);
1703 if (!tx_success)
1704 tx_success = team->ops.transmit(team, skb);
1705 if (tx_success) {
1706 struct team_pcpu_stats *pcpu_stats;
1708 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1709 u64_stats_update_begin(&pcpu_stats->syncp);
1710 pcpu_stats->tx_packets++;
1711 pcpu_stats->tx_bytes += len;
1712 u64_stats_update_end(&pcpu_stats->syncp);
1713 } else {
1714 this_cpu_inc(team->pcpu_stats->tx_dropped);
1717 return NETDEV_TX_OK;
1720 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1721 struct net_device *sb_dev)
1724 * This helper function exists to help dev_pick_tx get the correct
1725 * destination queue. Using a helper function skips a call to
1726 * skb_tx_hash and will put the skbs in the queue we expect on their
1727 * way down to the team driver.
1729 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1732 * Save the original txq to restore before passing to the driver
1734 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1736 if (unlikely(txq >= dev->real_num_tx_queues)) {
1737 do {
1738 txq -= dev->real_num_tx_queues;
1739 } while (txq >= dev->real_num_tx_queues);
1741 return txq;
1744 static void team_change_rx_flags(struct net_device *dev, int change)
1746 struct team *team = netdev_priv(dev);
1747 struct team_port *port;
1748 int inc;
1750 rcu_read_lock();
1751 list_for_each_entry_rcu(port, &team->port_list, list) {
1752 if (change & IFF_PROMISC) {
1753 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1754 dev_set_promiscuity(port->dev, inc);
1756 if (change & IFF_ALLMULTI) {
1757 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1758 dev_set_allmulti(port->dev, inc);
1761 rcu_read_unlock();
1764 static void team_set_rx_mode(struct net_device *dev)
1766 struct team *team = netdev_priv(dev);
1767 struct team_port *port;
1769 rcu_read_lock();
1770 list_for_each_entry_rcu(port, &team->port_list, list) {
1771 dev_uc_sync_multiple(port->dev, dev);
1772 dev_mc_sync_multiple(port->dev, dev);
1774 rcu_read_unlock();
1777 static int team_set_mac_address(struct net_device *dev, void *p)
1779 struct sockaddr *addr = p;
1780 struct team *team = netdev_priv(dev);
1781 struct team_port *port;
1783 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1784 return -EADDRNOTAVAIL;
1785 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1786 mutex_lock(&team->lock);
1787 list_for_each_entry(port, &team->port_list, list)
1788 if (team->ops.port_change_dev_addr)
1789 team->ops.port_change_dev_addr(team, port);
1790 mutex_unlock(&team->lock);
1791 return 0;
1794 static int team_change_mtu(struct net_device *dev, int new_mtu)
1796 struct team *team = netdev_priv(dev);
1797 struct team_port *port;
1798 int err;
1801 * Alhough this is reader, it's guarded by team lock. It's not possible
1802 * to traverse list in reverse under rcu_read_lock
1804 mutex_lock(&team->lock);
1805 team->port_mtu_change_allowed = true;
1806 list_for_each_entry(port, &team->port_list, list) {
1807 err = dev_set_mtu(port->dev, new_mtu);
1808 if (err) {
1809 netdev_err(dev, "Device %s failed to change mtu",
1810 port->dev->name);
1811 goto unwind;
1814 team->port_mtu_change_allowed = false;
1815 mutex_unlock(&team->lock);
1817 dev->mtu = new_mtu;
1819 return 0;
1821 unwind:
1822 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1823 dev_set_mtu(port->dev, dev->mtu);
1824 team->port_mtu_change_allowed = false;
1825 mutex_unlock(&team->lock);
1827 return err;
1830 static void
1831 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1833 struct team *team = netdev_priv(dev);
1834 struct team_pcpu_stats *p;
1835 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1836 u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
1837 unsigned int start;
1838 int i;
1840 for_each_possible_cpu(i) {
1841 p = per_cpu_ptr(team->pcpu_stats, i);
1842 do {
1843 start = u64_stats_fetch_begin_irq(&p->syncp);
1844 rx_packets = p->rx_packets;
1845 rx_bytes = p->rx_bytes;
1846 rx_multicast = p->rx_multicast;
1847 tx_packets = p->tx_packets;
1848 tx_bytes = p->tx_bytes;
1849 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1851 stats->rx_packets += rx_packets;
1852 stats->rx_bytes += rx_bytes;
1853 stats->multicast += rx_multicast;
1854 stats->tx_packets += tx_packets;
1855 stats->tx_bytes += tx_bytes;
1857 * rx_dropped, tx_dropped & rx_nohandler are u32,
1858 * updated without syncp protection.
1860 rx_dropped += p->rx_dropped;
1861 tx_dropped += p->tx_dropped;
1862 rx_nohandler += p->rx_nohandler;
1864 stats->rx_dropped = rx_dropped;
1865 stats->tx_dropped = tx_dropped;
1866 stats->rx_nohandler = rx_nohandler;
1869 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1871 struct team *team = netdev_priv(dev);
1872 struct team_port *port;
1873 int err;
1876 * Alhough this is reader, it's guarded by team lock. It's not possible
1877 * to traverse list in reverse under rcu_read_lock
1879 mutex_lock(&team->lock);
1880 list_for_each_entry(port, &team->port_list, list) {
1881 err = vlan_vid_add(port->dev, proto, vid);
1882 if (err)
1883 goto unwind;
1885 mutex_unlock(&team->lock);
1887 return 0;
1889 unwind:
1890 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1891 vlan_vid_del(port->dev, proto, vid);
1892 mutex_unlock(&team->lock);
1894 return err;
1897 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1899 struct team *team = netdev_priv(dev);
1900 struct team_port *port;
1902 mutex_lock(&team->lock);
1903 list_for_each_entry(port, &team->port_list, list)
1904 vlan_vid_del(port->dev, proto, vid);
1905 mutex_unlock(&team->lock);
1907 return 0;
1910 #ifdef CONFIG_NET_POLL_CONTROLLER
1911 static void team_poll_controller(struct net_device *dev)
1915 static void __team_netpoll_cleanup(struct team *team)
1917 struct team_port *port;
1919 list_for_each_entry(port, &team->port_list, list)
1920 team_port_disable_netpoll(port);
1923 static void team_netpoll_cleanup(struct net_device *dev)
1925 struct team *team = netdev_priv(dev);
1927 mutex_lock(&team->lock);
1928 __team_netpoll_cleanup(team);
1929 mutex_unlock(&team->lock);
1932 static int team_netpoll_setup(struct net_device *dev,
1933 struct netpoll_info *npifo)
1935 struct team *team = netdev_priv(dev);
1936 struct team_port *port;
1937 int err = 0;
1939 mutex_lock(&team->lock);
1940 list_for_each_entry(port, &team->port_list, list) {
1941 err = __team_port_enable_netpoll(port);
1942 if (err) {
1943 __team_netpoll_cleanup(team);
1944 break;
1947 mutex_unlock(&team->lock);
1948 return err;
1950 #endif
1952 static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
1953 struct netlink_ext_ack *extack)
1955 struct team *team = netdev_priv(dev);
1956 int err;
1958 mutex_lock(&team->lock);
1959 err = team_port_add(team, port_dev, extack);
1960 mutex_unlock(&team->lock);
1962 if (!err)
1963 netdev_change_features(dev);
1965 return err;
1968 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1970 struct team *team = netdev_priv(dev);
1971 int err;
1973 mutex_lock(&team->lock);
1974 err = team_port_del(team, port_dev);
1975 mutex_unlock(&team->lock);
1977 if (!err)
1978 netdev_change_features(dev);
1980 return err;
1983 static netdev_features_t team_fix_features(struct net_device *dev,
1984 netdev_features_t features)
1986 struct team_port *port;
1987 struct team *team = netdev_priv(dev);
1988 netdev_features_t mask;
1990 mask = features;
1991 features &= ~NETIF_F_ONE_FOR_ALL;
1992 features |= NETIF_F_ALL_FOR_ALL;
1994 rcu_read_lock();
1995 list_for_each_entry_rcu(port, &team->port_list, list) {
1996 features = netdev_increment_features(features,
1997 port->dev->features,
1998 mask);
2000 rcu_read_unlock();
2002 features = netdev_add_tso_features(features, mask);
2004 return features;
2007 static int team_change_carrier(struct net_device *dev, bool new_carrier)
2009 struct team *team = netdev_priv(dev);
2011 team->user_carrier_enabled = true;
2013 if (new_carrier)
2014 netif_carrier_on(dev);
2015 else
2016 netif_carrier_off(dev);
2017 return 0;
2020 static const struct net_device_ops team_netdev_ops = {
2021 .ndo_init = team_init,
2022 .ndo_uninit = team_uninit,
2023 .ndo_open = team_open,
2024 .ndo_stop = team_close,
2025 .ndo_start_xmit = team_xmit,
2026 .ndo_select_queue = team_select_queue,
2027 .ndo_change_rx_flags = team_change_rx_flags,
2028 .ndo_set_rx_mode = team_set_rx_mode,
2029 .ndo_set_mac_address = team_set_mac_address,
2030 .ndo_change_mtu = team_change_mtu,
2031 .ndo_get_stats64 = team_get_stats64,
2032 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
2033 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
2034 #ifdef CONFIG_NET_POLL_CONTROLLER
2035 .ndo_poll_controller = team_poll_controller,
2036 .ndo_netpoll_setup = team_netpoll_setup,
2037 .ndo_netpoll_cleanup = team_netpoll_cleanup,
2038 #endif
2039 .ndo_add_slave = team_add_slave,
2040 .ndo_del_slave = team_del_slave,
2041 .ndo_fix_features = team_fix_features,
2042 .ndo_change_carrier = team_change_carrier,
2043 .ndo_features_check = passthru_features_check,
2046 /***********************
2047 * ethtool interface
2048 ***********************/
2050 static void team_ethtool_get_drvinfo(struct net_device *dev,
2051 struct ethtool_drvinfo *drvinfo)
2053 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2054 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2057 static const struct ethtool_ops team_ethtool_ops = {
2058 .get_drvinfo = team_ethtool_get_drvinfo,
2059 .get_link = ethtool_op_get_link,
2062 /***********************
2063 * rt netlink interface
2064 ***********************/
2066 static void team_setup_by_port(struct net_device *dev,
2067 struct net_device *port_dev)
2069 dev->header_ops = port_dev->header_ops;
2070 dev->type = port_dev->type;
2071 dev->hard_header_len = port_dev->hard_header_len;
2072 dev->addr_len = port_dev->addr_len;
2073 dev->mtu = port_dev->mtu;
2074 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2075 eth_hw_addr_inherit(dev, port_dev);
2078 static int team_dev_type_check_change(struct net_device *dev,
2079 struct net_device *port_dev)
2081 struct team *team = netdev_priv(dev);
2082 char *portname = port_dev->name;
2083 int err;
2085 if (dev->type == port_dev->type)
2086 return 0;
2087 if (!list_empty(&team->port_list)) {
2088 netdev_err(dev, "Device %s is of different type\n", portname);
2089 return -EBUSY;
2091 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2092 err = notifier_to_errno(err);
2093 if (err) {
2094 netdev_err(dev, "Refused to change device type\n");
2095 return err;
2097 dev_uc_flush(dev);
2098 dev_mc_flush(dev);
2099 team_setup_by_port(dev, port_dev);
2100 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2101 return 0;
2104 static void team_setup(struct net_device *dev)
2106 ether_setup(dev);
2107 dev->max_mtu = ETH_MAX_MTU;
2109 dev->netdev_ops = &team_netdev_ops;
2110 dev->ethtool_ops = &team_ethtool_ops;
2111 dev->needs_free_netdev = true;
2112 dev->priv_destructor = team_destructor;
2113 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2114 dev->priv_flags |= IFF_NO_QUEUE;
2115 dev->priv_flags |= IFF_TEAM;
2118 * Indicate we support unicast address filtering. That way core won't
2119 * bring us to promisc mode in case a unicast addr is added.
2120 * Let this up to underlay drivers.
2122 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2124 dev->features |= NETIF_F_LLTX;
2125 dev->features |= NETIF_F_GRO;
2127 /* Don't allow team devices to change network namespaces. */
2128 dev->features |= NETIF_F_NETNS_LOCAL;
2130 dev->hw_features = TEAM_VLAN_FEATURES |
2131 NETIF_F_HW_VLAN_CTAG_RX |
2132 NETIF_F_HW_VLAN_CTAG_FILTER;
2134 dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
2135 dev->features |= dev->hw_features;
2136 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2139 static int team_newlink(struct net *src_net, struct net_device *dev,
2140 struct nlattr *tb[], struct nlattr *data[],
2141 struct netlink_ext_ack *extack)
2143 if (tb[IFLA_ADDRESS] == NULL)
2144 eth_hw_addr_random(dev);
2146 return register_netdevice(dev);
2149 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2150 struct netlink_ext_ack *extack)
2152 if (tb[IFLA_ADDRESS]) {
2153 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2154 return -EINVAL;
2155 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2156 return -EADDRNOTAVAIL;
2158 return 0;
2161 static unsigned int team_get_num_tx_queues(void)
2163 return TEAM_DEFAULT_NUM_TX_QUEUES;
2166 static unsigned int team_get_num_rx_queues(void)
2168 return TEAM_DEFAULT_NUM_RX_QUEUES;
2171 static struct rtnl_link_ops team_link_ops __read_mostly = {
2172 .kind = DRV_NAME,
2173 .priv_size = sizeof(struct team),
2174 .setup = team_setup,
2175 .newlink = team_newlink,
2176 .validate = team_validate,
2177 .get_num_tx_queues = team_get_num_tx_queues,
2178 .get_num_rx_queues = team_get_num_rx_queues,
2182 /***********************************
2183 * Generic netlink custom interface
2184 ***********************************/
2186 static struct genl_family team_nl_family;
2188 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2189 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2190 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2191 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2192 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2195 static const struct nla_policy
2196 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2197 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2198 [TEAM_ATTR_OPTION_NAME] = {
2199 .type = NLA_STRING,
2200 .len = TEAM_STRING_MAX_LEN,
2202 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2203 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2204 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
2207 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2209 struct sk_buff *msg;
2210 void *hdr;
2211 int err;
2213 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2214 if (!msg)
2215 return -ENOMEM;
2217 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2218 &team_nl_family, 0, TEAM_CMD_NOOP);
2219 if (!hdr) {
2220 err = -EMSGSIZE;
2221 goto err_msg_put;
2224 genlmsg_end(msg, hdr);
2226 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2228 err_msg_put:
2229 nlmsg_free(msg);
2231 return err;
2235 * Netlink cmd functions should be locked by following two functions.
2236 * Since dev gets held here, that ensures dev won't disappear in between.
2238 static struct team *team_nl_team_get(struct genl_info *info)
2240 struct net *net = genl_info_net(info);
2241 int ifindex;
2242 struct net_device *dev;
2243 struct team *team;
2245 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2246 return NULL;
2248 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2249 dev = dev_get_by_index(net, ifindex);
2250 if (!dev || dev->netdev_ops != &team_netdev_ops) {
2251 if (dev)
2252 dev_put(dev);
2253 return NULL;
2256 team = netdev_priv(dev);
2257 mutex_lock(&team->lock);
2258 return team;
2261 static void team_nl_team_put(struct team *team)
2263 mutex_unlock(&team->lock);
2264 dev_put(team->dev);
2267 typedef int team_nl_send_func_t(struct sk_buff *skb,
2268 struct team *team, u32 portid);
2270 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2272 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2275 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2276 struct team_option_inst *opt_inst)
2278 struct nlattr *option_item;
2279 struct team_option *option = opt_inst->option;
2280 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2281 struct team_gsetter_ctx ctx;
2282 int err;
2284 ctx.info = opt_inst_info;
2285 err = team_option_get(team, opt_inst, &ctx);
2286 if (err)
2287 return err;
2289 option_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_OPTION);
2290 if (!option_item)
2291 return -EMSGSIZE;
2293 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2294 goto nest_cancel;
2295 if (opt_inst_info->port &&
2296 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2297 opt_inst_info->port->dev->ifindex))
2298 goto nest_cancel;
2299 if (opt_inst->option->array_size &&
2300 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2301 opt_inst_info->array_index))
2302 goto nest_cancel;
2304 switch (option->type) {
2305 case TEAM_OPTION_TYPE_U32:
2306 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2307 goto nest_cancel;
2308 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2309 goto nest_cancel;
2310 break;
2311 case TEAM_OPTION_TYPE_STRING:
2312 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2313 goto nest_cancel;
2314 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2315 ctx.data.str_val))
2316 goto nest_cancel;
2317 break;
2318 case TEAM_OPTION_TYPE_BINARY:
2319 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2320 goto nest_cancel;
2321 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2322 ctx.data.bin_val.ptr))
2323 goto nest_cancel;
2324 break;
2325 case TEAM_OPTION_TYPE_BOOL:
2326 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2327 goto nest_cancel;
2328 if (ctx.data.bool_val &&
2329 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2330 goto nest_cancel;
2331 break;
2332 case TEAM_OPTION_TYPE_S32:
2333 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2334 goto nest_cancel;
2335 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2336 goto nest_cancel;
2337 break;
2338 default:
2339 BUG();
2341 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2342 goto nest_cancel;
2343 if (opt_inst->changed) {
2344 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2345 goto nest_cancel;
2346 opt_inst->changed = false;
2348 nla_nest_end(skb, option_item);
2349 return 0;
2351 nest_cancel:
2352 nla_nest_cancel(skb, option_item);
2353 return -EMSGSIZE;
2356 static int __send_and_alloc_skb(struct sk_buff **pskb,
2357 struct team *team, u32 portid,
2358 team_nl_send_func_t *send_func)
2360 int err;
2362 if (*pskb) {
2363 err = send_func(*pskb, team, portid);
2364 if (err)
2365 return err;
2367 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2368 if (!*pskb)
2369 return -ENOMEM;
2370 return 0;
2373 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2374 int flags, team_nl_send_func_t *send_func,
2375 struct list_head *sel_opt_inst_list)
2377 struct nlattr *option_list;
2378 struct nlmsghdr *nlh;
2379 void *hdr;
2380 struct team_option_inst *opt_inst;
2381 int err;
2382 struct sk_buff *skb = NULL;
2383 bool incomplete;
2384 int i;
2386 opt_inst = list_first_entry(sel_opt_inst_list,
2387 struct team_option_inst, tmp_list);
2389 start_again:
2390 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2391 if (err)
2392 return err;
2394 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2395 TEAM_CMD_OPTIONS_GET);
2396 if (!hdr) {
2397 nlmsg_free(skb);
2398 return -EMSGSIZE;
2401 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2402 goto nla_put_failure;
2403 option_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_OPTION);
2404 if (!option_list)
2405 goto nla_put_failure;
2407 i = 0;
2408 incomplete = false;
2409 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2410 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2411 if (err) {
2412 if (err == -EMSGSIZE) {
2413 if (!i)
2414 goto errout;
2415 incomplete = true;
2416 break;
2418 goto errout;
2420 i++;
2423 nla_nest_end(skb, option_list);
2424 genlmsg_end(skb, hdr);
2425 if (incomplete)
2426 goto start_again;
2428 send_done:
2429 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2430 if (!nlh) {
2431 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2432 if (err)
2433 return err;
2434 goto send_done;
2437 return send_func(skb, team, portid);
2439 nla_put_failure:
2440 err = -EMSGSIZE;
2441 errout:
2442 nlmsg_free(skb);
2443 return err;
2446 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2448 struct team *team;
2449 struct team_option_inst *opt_inst;
2450 int err;
2451 LIST_HEAD(sel_opt_inst_list);
2453 team = team_nl_team_get(info);
2454 if (!team)
2455 return -EINVAL;
2457 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2458 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2459 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2460 NLM_F_ACK, team_nl_send_unicast,
2461 &sel_opt_inst_list);
2463 team_nl_team_put(team);
2465 return err;
2468 static int team_nl_send_event_options_get(struct team *team,
2469 struct list_head *sel_opt_inst_list);
2471 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2473 struct team *team;
2474 int err = 0;
2475 int i;
2476 struct nlattr *nl_option;
2478 rtnl_lock();
2480 team = team_nl_team_get(info);
2481 if (!team) {
2482 err = -EINVAL;
2483 goto rtnl_unlock;
2486 err = -EINVAL;
2487 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2488 err = -EINVAL;
2489 goto team_put;
2492 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2493 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2494 struct nlattr *attr;
2495 struct nlattr *attr_data;
2496 LIST_HEAD(opt_inst_list);
2497 enum team_option_type opt_type;
2498 int opt_port_ifindex = 0; /* != 0 for per-port options */
2499 u32 opt_array_index = 0;
2500 bool opt_is_array = false;
2501 struct team_option_inst *opt_inst;
2502 char *opt_name;
2503 bool opt_found = false;
2505 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2506 err = -EINVAL;
2507 goto team_put;
2509 err = nla_parse_nested_deprecated(opt_attrs,
2510 TEAM_ATTR_OPTION_MAX,
2511 nl_option,
2512 team_nl_option_policy,
2513 info->extack);
2514 if (err)
2515 goto team_put;
2516 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2517 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2518 err = -EINVAL;
2519 goto team_put;
2521 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2522 case NLA_U32:
2523 opt_type = TEAM_OPTION_TYPE_U32;
2524 break;
2525 case NLA_STRING:
2526 opt_type = TEAM_OPTION_TYPE_STRING;
2527 break;
2528 case NLA_BINARY:
2529 opt_type = TEAM_OPTION_TYPE_BINARY;
2530 break;
2531 case NLA_FLAG:
2532 opt_type = TEAM_OPTION_TYPE_BOOL;
2533 break;
2534 case NLA_S32:
2535 opt_type = TEAM_OPTION_TYPE_S32;
2536 break;
2537 default:
2538 goto team_put;
2541 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2542 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2543 err = -EINVAL;
2544 goto team_put;
2547 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2548 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2549 if (attr)
2550 opt_port_ifindex = nla_get_u32(attr);
2552 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2553 if (attr) {
2554 opt_is_array = true;
2555 opt_array_index = nla_get_u32(attr);
2558 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2559 struct team_option *option = opt_inst->option;
2560 struct team_gsetter_ctx ctx;
2561 struct team_option_inst_info *opt_inst_info;
2562 int tmp_ifindex;
2564 opt_inst_info = &opt_inst->info;
2565 tmp_ifindex = opt_inst_info->port ?
2566 opt_inst_info->port->dev->ifindex : 0;
2567 if (option->type != opt_type ||
2568 strcmp(option->name, opt_name) ||
2569 tmp_ifindex != opt_port_ifindex ||
2570 (option->array_size && !opt_is_array) ||
2571 opt_inst_info->array_index != opt_array_index)
2572 continue;
2573 opt_found = true;
2574 ctx.info = opt_inst_info;
2575 switch (opt_type) {
2576 case TEAM_OPTION_TYPE_U32:
2577 ctx.data.u32_val = nla_get_u32(attr_data);
2578 break;
2579 case TEAM_OPTION_TYPE_STRING:
2580 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2581 err = -EINVAL;
2582 goto team_put;
2584 ctx.data.str_val = nla_data(attr_data);
2585 break;
2586 case TEAM_OPTION_TYPE_BINARY:
2587 ctx.data.bin_val.len = nla_len(attr_data);
2588 ctx.data.bin_val.ptr = nla_data(attr_data);
2589 break;
2590 case TEAM_OPTION_TYPE_BOOL:
2591 ctx.data.bool_val = attr_data ? true : false;
2592 break;
2593 case TEAM_OPTION_TYPE_S32:
2594 ctx.data.s32_val = nla_get_s32(attr_data);
2595 break;
2596 default:
2597 BUG();
2599 err = team_option_set(team, opt_inst, &ctx);
2600 if (err)
2601 goto team_put;
2602 opt_inst->changed = true;
2603 list_add(&opt_inst->tmp_list, &opt_inst_list);
2605 if (!opt_found) {
2606 err = -ENOENT;
2607 goto team_put;
2610 err = team_nl_send_event_options_get(team, &opt_inst_list);
2611 if (err)
2612 break;
2615 team_put:
2616 team_nl_team_put(team);
2617 rtnl_unlock:
2618 rtnl_unlock();
2619 return err;
2622 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2623 struct team_port *port)
2625 struct nlattr *port_item;
2627 port_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_PORT);
2628 if (!port_item)
2629 goto nest_cancel;
2630 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2631 goto nest_cancel;
2632 if (port->changed) {
2633 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2634 goto nest_cancel;
2635 port->changed = false;
2637 if ((port->removed &&
2638 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2639 (port->state.linkup &&
2640 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2641 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2642 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2643 goto nest_cancel;
2644 nla_nest_end(skb, port_item);
2645 return 0;
2647 nest_cancel:
2648 nla_nest_cancel(skb, port_item);
2649 return -EMSGSIZE;
2652 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2653 int flags, team_nl_send_func_t *send_func,
2654 struct team_port *one_port)
2656 struct nlattr *port_list;
2657 struct nlmsghdr *nlh;
2658 void *hdr;
2659 struct team_port *port;
2660 int err;
2661 struct sk_buff *skb = NULL;
2662 bool incomplete;
2663 int i;
2665 port = list_first_entry_or_null(&team->port_list,
2666 struct team_port, list);
2668 start_again:
2669 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2670 if (err)
2671 return err;
2673 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2674 TEAM_CMD_PORT_LIST_GET);
2675 if (!hdr) {
2676 nlmsg_free(skb);
2677 return -EMSGSIZE;
2680 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2681 goto nla_put_failure;
2682 port_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_PORT);
2683 if (!port_list)
2684 goto nla_put_failure;
2686 i = 0;
2687 incomplete = false;
2689 /* If one port is selected, called wants to send port list containing
2690 * only this port. Otherwise go through all listed ports and send all
2692 if (one_port) {
2693 err = team_nl_fill_one_port_get(skb, one_port);
2694 if (err)
2695 goto errout;
2696 } else if (port) {
2697 list_for_each_entry_from(port, &team->port_list, list) {
2698 err = team_nl_fill_one_port_get(skb, port);
2699 if (err) {
2700 if (err == -EMSGSIZE) {
2701 if (!i)
2702 goto errout;
2703 incomplete = true;
2704 break;
2706 goto errout;
2708 i++;
2712 nla_nest_end(skb, port_list);
2713 genlmsg_end(skb, hdr);
2714 if (incomplete)
2715 goto start_again;
2717 send_done:
2718 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2719 if (!nlh) {
2720 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2721 if (err)
2722 return err;
2723 goto send_done;
2726 return send_func(skb, team, portid);
2728 nla_put_failure:
2729 err = -EMSGSIZE;
2730 errout:
2731 nlmsg_free(skb);
2732 return err;
2735 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2736 struct genl_info *info)
2738 struct team *team;
2739 int err;
2741 team = team_nl_team_get(info);
2742 if (!team)
2743 return -EINVAL;
2745 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2746 NLM_F_ACK, team_nl_send_unicast, NULL);
2748 team_nl_team_put(team);
2750 return err;
2753 static const struct genl_ops team_nl_ops[] = {
2755 .cmd = TEAM_CMD_NOOP,
2756 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2757 .doit = team_nl_cmd_noop,
2760 .cmd = TEAM_CMD_OPTIONS_SET,
2761 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2762 .doit = team_nl_cmd_options_set,
2763 .flags = GENL_ADMIN_PERM,
2766 .cmd = TEAM_CMD_OPTIONS_GET,
2767 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2768 .doit = team_nl_cmd_options_get,
2769 .flags = GENL_ADMIN_PERM,
2772 .cmd = TEAM_CMD_PORT_LIST_GET,
2773 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2774 .doit = team_nl_cmd_port_list_get,
2775 .flags = GENL_ADMIN_PERM,
2779 static const struct genl_multicast_group team_nl_mcgrps[] = {
2780 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2783 static struct genl_family team_nl_family __ro_after_init = {
2784 .name = TEAM_GENL_NAME,
2785 .version = TEAM_GENL_VERSION,
2786 .maxattr = TEAM_ATTR_MAX,
2787 .policy = team_nl_policy,
2788 .netnsok = true,
2789 .module = THIS_MODULE,
2790 .ops = team_nl_ops,
2791 .n_ops = ARRAY_SIZE(team_nl_ops),
2792 .mcgrps = team_nl_mcgrps,
2793 .n_mcgrps = ARRAY_SIZE(team_nl_mcgrps),
2796 static int team_nl_send_multicast(struct sk_buff *skb,
2797 struct team *team, u32 portid)
2799 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2800 skb, 0, 0, GFP_KERNEL);
2803 static int team_nl_send_event_options_get(struct team *team,
2804 struct list_head *sel_opt_inst_list)
2806 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2807 sel_opt_inst_list);
2810 static int team_nl_send_event_port_get(struct team *team,
2811 struct team_port *port)
2813 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2814 port);
2817 static int __init team_nl_init(void)
2819 return genl_register_family(&team_nl_family);
2822 static void team_nl_fini(void)
2824 genl_unregister_family(&team_nl_family);
2828 /******************
2829 * Change checkers
2830 ******************/
2832 static void __team_options_change_check(struct team *team)
2834 int err;
2835 struct team_option_inst *opt_inst;
2836 LIST_HEAD(sel_opt_inst_list);
2838 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2839 if (opt_inst->changed)
2840 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2842 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2843 if (err && err != -ESRCH)
2844 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2845 err);
2848 /* rtnl lock is held */
2850 static void __team_port_change_send(struct team_port *port, bool linkup)
2852 int err;
2854 port->changed = true;
2855 port->state.linkup = linkup;
2856 team_refresh_port_linkup(port);
2857 if (linkup) {
2858 struct ethtool_link_ksettings ecmd;
2860 err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2861 if (!err) {
2862 port->state.speed = ecmd.base.speed;
2863 port->state.duplex = ecmd.base.duplex;
2864 goto send_event;
2867 port->state.speed = 0;
2868 port->state.duplex = 0;
2870 send_event:
2871 err = team_nl_send_event_port_get(port->team, port);
2872 if (err && err != -ESRCH)
2873 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2874 port->dev->name, err);
2878 static void __team_carrier_check(struct team *team)
2880 struct team_port *port;
2881 bool team_linkup;
2883 if (team->user_carrier_enabled)
2884 return;
2886 team_linkup = false;
2887 list_for_each_entry(port, &team->port_list, list) {
2888 if (port->linkup) {
2889 team_linkup = true;
2890 break;
2894 if (team_linkup)
2895 netif_carrier_on(team->dev);
2896 else
2897 netif_carrier_off(team->dev);
2900 static void __team_port_change_check(struct team_port *port, bool linkup)
2902 if (port->state.linkup != linkup)
2903 __team_port_change_send(port, linkup);
2904 __team_carrier_check(port->team);
2907 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2909 __team_port_change_send(port, linkup);
2910 __team_carrier_check(port->team);
2913 static void __team_port_change_port_removed(struct team_port *port)
2915 port->removed = true;
2916 __team_port_change_send(port, false);
2917 __team_carrier_check(port->team);
2920 static void team_port_change_check(struct team_port *port, bool linkup)
2922 struct team *team = port->team;
2924 mutex_lock(&team->lock);
2925 __team_port_change_check(port, linkup);
2926 mutex_unlock(&team->lock);
2930 /************************************
2931 * Net device notifier event handler
2932 ************************************/
2934 static int team_device_event(struct notifier_block *unused,
2935 unsigned long event, void *ptr)
2937 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2938 struct team_port *port;
2940 port = team_port_get_rtnl(dev);
2941 if (!port)
2942 return NOTIFY_DONE;
2944 switch (event) {
2945 case NETDEV_UP:
2946 if (netif_oper_up(dev))
2947 team_port_change_check(port, true);
2948 break;
2949 case NETDEV_DOWN:
2950 team_port_change_check(port, false);
2951 break;
2952 case NETDEV_CHANGE:
2953 if (netif_running(port->dev))
2954 team_port_change_check(port,
2955 !!netif_oper_up(port->dev));
2956 break;
2957 case NETDEV_UNREGISTER:
2958 team_del_slave(port->team->dev, dev);
2959 break;
2960 case NETDEV_FEAT_CHANGE:
2961 team_compute_features(port->team);
2962 break;
2963 case NETDEV_PRECHANGEMTU:
2964 /* Forbid to change mtu of underlaying device */
2965 if (!port->team->port_mtu_change_allowed)
2966 return NOTIFY_BAD;
2967 break;
2968 case NETDEV_PRE_TYPE_CHANGE:
2969 /* Forbid to change type of underlaying device */
2970 return NOTIFY_BAD;
2971 case NETDEV_RESEND_IGMP:
2972 /* Propagate to master device */
2973 call_netdevice_notifiers(event, port->team->dev);
2974 break;
2976 return NOTIFY_DONE;
2979 static struct notifier_block team_notifier_block __read_mostly = {
2980 .notifier_call = team_device_event,
2984 /***********************
2985 * Module init and exit
2986 ***********************/
2988 static int __init team_module_init(void)
2990 int err;
2992 register_netdevice_notifier(&team_notifier_block);
2994 err = rtnl_link_register(&team_link_ops);
2995 if (err)
2996 goto err_rtnl_reg;
2998 err = team_nl_init();
2999 if (err)
3000 goto err_nl_init;
3002 return 0;
3004 err_nl_init:
3005 rtnl_link_unregister(&team_link_ops);
3007 err_rtnl_reg:
3008 unregister_netdevice_notifier(&team_notifier_block);
3010 return err;
3013 static void __exit team_module_exit(void)
3015 team_nl_fini();
3016 rtnl_link_unregister(&team_link_ops);
3017 unregister_netdevice_notifier(&team_notifier_block);
3020 module_init(team_module_init);
3021 module_exit(team_module_exit);
3023 MODULE_LICENSE("GPL v2");
3024 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
3025 MODULE_DESCRIPTION("Ethernet team device driver");
3026 MODULE_ALIAS_RTNL_LINK(DRV_NAME);