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