xref: /openbmc/linux/drivers/net/team/team.c (revision 7d545e77)
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/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>
29 
30 #define DRV_NAME "team"
31 
32 
33 /**********
34  * Helpers
35  **********/
36 
37 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
38 {
39 	struct team_port *port = rtnl_dereference(dev->rx_handler_data);
40 
41 	return netif_is_team_port(dev) ? port : NULL;
42 }
43 
44 /*
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
47  */
48 static int __set_port_dev_addr(struct net_device *port_dev,
49 			       const unsigned char *dev_addr)
50 {
51 	struct sockaddr_storage addr;
52 
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);
56 }
57 
58 static int team_port_set_orig_dev_addr(struct team_port *port)
59 {
60 	return __set_port_dev_addr(port->dev, port->orig.dev_addr);
61 }
62 
63 static int team_port_set_team_dev_addr(struct team *team,
64 				       struct team_port *port)
65 {
66 	return __set_port_dev_addr(port->dev, team->dev->dev_addr);
67 }
68 
69 int team_modeop_port_enter(struct team *team, struct team_port *port)
70 {
71 	return team_port_set_team_dev_addr(team, port);
72 }
73 EXPORT_SYMBOL(team_modeop_port_enter);
74 
75 void team_modeop_port_change_dev_addr(struct team *team,
76 				      struct team_port *port)
77 {
78 	team_port_set_team_dev_addr(team, port);
79 }
80 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
81 
82 static void team_lower_state_changed(struct team_port *port)
83 {
84 	struct netdev_lag_lower_state_info info;
85 
86 	info.link_up = port->linkup;
87 	info.tx_enabled = team_port_enabled(port);
88 	netdev_lower_state_changed(port->dev, &info);
89 }
90 
91 static void team_refresh_port_linkup(struct team_port *port)
92 {
93 	bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
94 						      port->state.linkup;
95 
96 	if (port->linkup != new_linkup) {
97 		port->linkup = new_linkup;
98 		team_lower_state_changed(port);
99 	}
100 }
101 
102 
103 /*******************
104  * Options handling
105  *******************/
106 
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;
114 };
115 
116 static struct team_option *__team_find_option(struct team *team,
117 					      const char *opt_name)
118 {
119 	struct team_option *option;
120 
121 	list_for_each_entry(option, &team->option_list, list) {
122 		if (strcmp(option->name, opt_name) == 0)
123 			return option;
124 	}
125 	return NULL;
126 }
127 
128 static void __team_option_inst_del(struct team_option_inst *opt_inst)
129 {
130 	list_del(&opt_inst->list);
131 	kfree(opt_inst);
132 }
133 
134 static void __team_option_inst_del_option(struct team *team,
135 					  struct team_option *option)
136 {
137 	struct team_option_inst *opt_inst, *tmp;
138 
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);
142 	}
143 }
144 
145 static int __team_option_inst_add(struct team *team, struct team_option *option,
146 				  struct team_port *port)
147 {
148 	struct team_option_inst *opt_inst;
149 	unsigned int array_size;
150 	unsigned int i;
151 	int err;
152 
153 	array_size = option->array_size;
154 	if (!array_size)
155 		array_size = 1; /* No array but still need one instance */
156 
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;
171 		}
172 
173 	}
174 	return 0;
175 }
176 
177 static int __team_option_inst_add_option(struct team *team,
178 					 struct team_option *option)
179 {
180 	int err;
181 
182 	if (!option->per_port) {
183 		err = __team_option_inst_add(team, option, NULL);
184 		if (err)
185 			goto inst_del_option;
186 	}
187 	return 0;
188 
189 inst_del_option:
190 	__team_option_inst_del_option(team, option);
191 	return err;
192 }
193 
194 static void __team_option_inst_mark_removed_option(struct team *team,
195 						   struct team_option *option)
196 {
197 	struct team_option_inst *opt_inst;
198 
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;
203 		}
204 	}
205 }
206 
207 static void __team_option_inst_del_port(struct team *team,
208 					struct team_port *port)
209 {
210 	struct team_option_inst *opt_inst, *tmp;
211 
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);
216 	}
217 }
218 
219 static int __team_option_inst_add_port(struct team *team,
220 				       struct team_port *port)
221 {
222 	struct team_option *option;
223 	int err;
224 
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;
231 	}
232 	return 0;
233 
234 inst_del_port:
235 	__team_option_inst_del_port(team, port);
236 	return err;
237 }
238 
239 static void __team_option_inst_mark_removed_port(struct team *team,
240 						 struct team_port *port)
241 {
242 	struct team_option_inst *opt_inst;
243 
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;
248 		}
249 	}
250 }
251 
252 static int __team_options_register(struct team *team,
253 				   const struct team_option *option,
254 				   size_t option_count)
255 {
256 	int i;
257 	struct team_option **dst_opts;
258 	int err;
259 
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;
268 		}
269 		dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
270 		if (!dst_opts[i]) {
271 			err = -ENOMEM;
272 			goto alloc_rollback;
273 		}
274 	}
275 
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);
281 	}
282 
283 	kfree(dst_opts);
284 	return 0;
285 
286 inst_rollback:
287 	for (i--; i >= 0; i--)
288 		__team_option_inst_del_option(team, dst_opts[i]);
289 
290 	i = option_count - 1;
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 	return opt_inst->option->getter(team, ctx);
365 }
366 
367 static int team_option_set(struct team *team,
368 			   struct team_option_inst *opt_inst,
369 			   struct team_gsetter_ctx *ctx)
370 {
371 	if (!opt_inst->option->setter)
372 		return -EOPNOTSUPP;
373 	return opt_inst->option->setter(team, ctx);
374 }
375 
376 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
377 {
378 	struct team_option_inst *opt_inst;
379 
380 	opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
381 	opt_inst->changed = true;
382 }
383 EXPORT_SYMBOL(team_option_inst_set_change);
384 
385 void team_options_change_check(struct team *team)
386 {
387 	__team_options_change_check(team);
388 }
389 EXPORT_SYMBOL(team_options_change_check);
390 
391 
392 /****************
393  * Mode handling
394  ****************/
395 
396 static LIST_HEAD(mode_list);
397 static DEFINE_SPINLOCK(mode_list_lock);
398 
399 struct team_mode_item {
400 	struct list_head list;
401 	const struct team_mode *mode;
402 };
403 
404 static struct team_mode_item *__find_mode(const char *kind)
405 {
406 	struct team_mode_item *mitem;
407 
408 	list_for_each_entry(mitem, &mode_list, list) {
409 		if (strcmp(mitem->mode->kind, kind) == 0)
410 			return mitem;
411 	}
412 	return NULL;
413 }
414 
415 static bool is_good_mode_name(const char *name)
416 {
417 	while (*name != '\0') {
418 		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
419 			return false;
420 		name++;
421 	}
422 	return true;
423 }
424 
425 int team_mode_register(const struct team_mode *mode)
426 {
427 	int err = 0;
428 	struct team_mode_item *mitem;
429 
430 	if (!is_good_mode_name(mode->kind) ||
431 	    mode->priv_size > TEAM_MODE_PRIV_SIZE)
432 		return -EINVAL;
433 
434 	mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
435 	if (!mitem)
436 		return -ENOMEM;
437 
438 	spin_lock(&mode_list_lock);
439 	if (__find_mode(mode->kind)) {
440 		err = -EEXIST;
441 		kfree(mitem);
442 		goto unlock;
443 	}
444 	mitem->mode = mode;
445 	list_add_tail(&mitem->list, &mode_list);
446 unlock:
447 	spin_unlock(&mode_list_lock);
448 	return err;
449 }
450 EXPORT_SYMBOL(team_mode_register);
451 
452 void team_mode_unregister(const struct team_mode *mode)
453 {
454 	struct team_mode_item *mitem;
455 
456 	spin_lock(&mode_list_lock);
457 	mitem = __find_mode(mode->kind);
458 	if (mitem) {
459 		list_del_init(&mitem->list);
460 		kfree(mitem);
461 	}
462 	spin_unlock(&mode_list_lock);
463 }
464 EXPORT_SYMBOL(team_mode_unregister);
465 
466 static const struct team_mode *team_mode_get(const char *kind)
467 {
468 	struct team_mode_item *mitem;
469 	const struct team_mode *mode = NULL;
470 
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);
478 	}
479 	if (mitem) {
480 		mode = mitem->mode;
481 		if (!try_module_get(mode->owner))
482 			mode = NULL;
483 	}
484 
485 	spin_unlock(&mode_list_lock);
486 	return mode;
487 }
488 
489 static void team_mode_put(const struct team_mode *mode)
490 {
491 	module_put(mode->owner);
492 }
493 
494 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
495 {
496 	dev_kfree_skb_any(skb);
497 	return false;
498 }
499 
500 static rx_handler_result_t team_dummy_receive(struct team *team,
501 					      struct team_port *port,
502 					      struct sk_buff *skb)
503 {
504 	return RX_HANDLER_ANOTHER;
505 }
506 
507 static const struct team_mode __team_no_mode = {
508 	.kind		= "*NOMODE*",
509 };
510 
511 static bool team_is_mode_set(struct team *team)
512 {
513 	return team->mode != &__team_no_mode;
514 }
515 
516 static void team_set_no_mode(struct team *team)
517 {
518 	team->user_carrier_enabled = false;
519 	team->mode = &__team_no_mode;
520 }
521 
522 static void team_adjust_ops(struct team *team)
523 {
524 	/*
525 	 * To avoid checks in rx/tx skb paths, ensure here that non-null and
526 	 * correct ops are always set.
527 	 */
528 
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;
534 
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;
540 }
541 
542 /*
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.
546  */
547 static int __team_change_mode(struct team *team,
548 			      const struct team_mode *new_mode)
549 {
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;
553 
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);
557 
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));
565 	}
566 
567 	if (!new_mode)
568 		return 0;
569 
570 	if (new_mode->ops->init) {
571 		int err;
572 
573 		err = new_mode->ops->init(team);
574 		if (err)
575 			return err;
576 	}
577 
578 	team->mode = new_mode;
579 	memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
580 	team_adjust_ops(team);
581 
582 	return 0;
583 }
584 
585 static int team_change_mode(struct team *team, const char *kind)
586 {
587 	const struct team_mode *new_mode;
588 	struct net_device *dev = team->dev;
589 	int err;
590 
591 	if (!list_empty(&team->port_list)) {
592 		netdev_err(dev, "No ports can be present during mode change\n");
593 		return -EBUSY;
594 	}
595 
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;
599 	}
600 
601 	new_mode = team_mode_get(kind);
602 	if (!new_mode) {
603 		netdev_err(dev, "Mode \"%s\" not found\n", kind);
604 		return -EINVAL;
605 	}
606 
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;
612 	}
613 
614 	netdev_info(dev, "Mode changed to \"%s\"\n", kind);
615 	return 0;
616 }
617 
618 
619 /*********************
620  * Peers notification
621  *********************/
622 
623 static void team_notify_peers_work(struct work_struct *work)
624 {
625 	struct team *team;
626 	int val;
627 
628 	team = container_of(work, struct team, notify_peers.dw.work);
629 
630 	if (!rtnl_trylock()) {
631 		schedule_delayed_work(&team->notify_peers.dw, 0);
632 		return;
633 	}
634 	val = atomic_dec_if_positive(&team->notify_peers.count_pending);
635 	if (val < 0) {
636 		rtnl_unlock();
637 		return;
638 	}
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));
644 }
645 
646 static void team_notify_peers(struct team *team)
647 {
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);
652 }
653 
654 static void team_notify_peers_init(struct team *team)
655 {
656 	INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
657 }
658 
659 static void team_notify_peers_fini(struct team *team)
660 {
661 	cancel_delayed_work_sync(&team->notify_peers.dw);
662 }
663 
664 
665 /*******************************
666  * Send multicast group rejoins
667  *******************************/
668 
669 static void team_mcast_rejoin_work(struct work_struct *work)
670 {
671 	struct team *team;
672 	int val;
673 
674 	team = container_of(work, struct team, mcast_rejoin.dw.work);
675 
676 	if (!rtnl_trylock()) {
677 		schedule_delayed_work(&team->mcast_rejoin.dw, 0);
678 		return;
679 	}
680 	val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
681 	if (val < 0) {
682 		rtnl_unlock();
683 		return;
684 	}
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));
690 }
691 
692 static void team_mcast_rejoin(struct team *team)
693 {
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);
698 }
699 
700 static void team_mcast_rejoin_init(struct team *team)
701 {
702 	INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
703 }
704 
705 static void team_mcast_rejoin_fini(struct team *team)
706 {
707 	cancel_delayed_work_sync(&team->mcast_rejoin.dw);
708 }
709 
710 
711 /************************
712  * Rx path frame handler
713  ************************/
714 
715 /* note: already called with rcu_read_lock */
716 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
717 {
718 	struct sk_buff *skb = *pskb;
719 	struct team_port *port;
720 	struct team *team;
721 	rx_handler_result_t res;
722 
723 	skb = skb_share_check(skb, GFP_ATOMIC);
724 	if (!skb)
725 		return RX_HANDLER_CONSUMED;
726 
727 	*pskb = skb;
728 
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);
736 	}
737 	if (res == RX_HANDLER_ANOTHER) {
738 		struct team_pcpu_stats *pcpu_stats;
739 
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);
747 
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);
753 	}
754 
755 	return res;
756 }
757 
758 
759 /*************************************
760  * Multiqueue Tx port select override
761  *************************************/
762 
763 static int team_queue_override_init(struct team *team)
764 {
765 	struct list_head *listarr;
766 	unsigned int queue_cnt = team->dev->num_tx_queues - 1;
767 	unsigned int i;
768 
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;
779 }
780 
781 static void team_queue_override_fini(struct team *team)
782 {
783 	kfree(team->qom_lists);
784 }
785 
786 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
787 {
788 	return &team->qom_lists[queue_id - 1];
789 }
790 
791 /*
792  * note: already called with rcu_read_lock
793  */
794 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
795 {
796 	struct list_head *qom_list;
797 	struct team_port *port;
798 
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;
805 	}
806 	return false;
807 }
808 
809 static void __team_queue_override_port_del(struct team *team,
810 					   struct team_port *port)
811 {
812 	if (!port->queue_id)
813 		return;
814 	list_del_rcu(&port->qom_list);
815 }
816 
817 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
818 						      struct team_port *cur)
819 {
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;
827 }
828 
829 static void __team_queue_override_port_add(struct team *team,
830 					   struct team_port *port)
831 {
832 	struct team_port *cur;
833 	struct list_head *qom_list;
834 	struct list_head *node;
835 
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;
844 	}
845 	list_add_tail_rcu(&port->qom_list, node);
846 }
847 
848 static void __team_queue_override_enabled_check(struct team *team)
849 {
850 	struct team_port *port;
851 	bool enabled = false;
852 
853 	list_for_each_entry(port, &team->port_list, list) {
854 		if (port->queue_id) {
855 			enabled = true;
856 			break;
857 		}
858 	}
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;
864 }
865 
866 static void team_queue_override_port_prio_changed(struct team *team,
867 						  struct team_port *port)
868 {
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);
874 }
875 
876 static void team_queue_override_port_change_queue_id(struct team *team,
877 						     struct team_port *port,
878 						     u16 new_queue_id)
879 {
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;
887 	}
888 }
889 
890 static void team_queue_override_port_add(struct team *team,
891 					 struct team_port *port)
892 {
893 	__team_queue_override_port_add(team, port);
894 	__team_queue_override_enabled_check(team);
895 }
896 
897 static void team_queue_override_port_del(struct team *team,
898 					 struct team_port *port)
899 {
900 	__team_queue_override_port_del(team, port);
901 	__team_queue_override_enabled_check(team);
902 }
903 
904 
905 /****************
906  * Port handling
907  ****************/
908 
909 static bool team_port_find(const struct team *team,
910 			   const struct team_port *port)
911 {
912 	struct team_port *cur;
913 
914 	list_for_each_entry(cur, &team->port_list, list)
915 		if (cur == port)
916 			return true;
917 	return false;
918 }
919 
920 /*
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.
925  */
926 static void team_port_enable(struct team *team,
927 			     struct team_port *port)
928 {
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);
941 }
942 
943 static void __reconstruct_port_hlist(struct team *team, int rm_index)
944 {
945 	int i;
946 	struct team_port *port;
947 
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));
954 	}
955 }
956 
957 static void team_port_disable(struct team *team,
958 			      struct team_port *port)
959 {
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);
971 }
972 
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)
976 
977 #define TEAM_ENC_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
978 				 NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
979 
980 static void __team_compute_features(struct team *team)
981 {
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;
989 
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);
998 
999 
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;
1003 	}
1004 
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;
1009 
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;
1013 }
1014 
1015 static void team_compute_features(struct team *team)
1016 {
1017 	mutex_lock(&team->lock);
1018 	__team_compute_features(team);
1019 	mutex_unlock(&team->lock);
1020 	netdev_change_features(team->dev);
1021 }
1022 
1023 static int team_port_enter(struct team *team, struct team_port *port)
1024 {
1025 	int err = 0;
1026 
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;
1034 		}
1035 	}
1036 
1037 	return 0;
1038 
1039 err_port_enter:
1040 	dev_put(team->dev);
1041 
1042 	return err;
1043 }
1044 
1045 static void team_port_leave(struct team *team, struct team_port *port)
1046 {
1047 	if (team->ops.port_leave)
1048 		team->ops.port_leave(team, port);
1049 	dev_put(team->dev);
1050 }
1051 
1052 #ifdef CONFIG_NET_POLL_CONTROLLER
1053 static int __team_port_enable_netpoll(struct team_port *port)
1054 {
1055 	struct netpoll *np;
1056 	int err;
1057 
1058 	np = kzalloc(sizeof(*np), GFP_KERNEL);
1059 	if (!np)
1060 		return -ENOMEM;
1061 
1062 	err = __netpoll_setup(np, port->dev);
1063 	if (err) {
1064 		kfree(np);
1065 		return err;
1066 	}
1067 	port->np = np;
1068 	return err;
1069 }
1070 
1071 static int team_port_enable_netpoll(struct team_port *port)
1072 {
1073 	if (!port->team->dev->npinfo)
1074 		return 0;
1075 
1076 	return __team_port_enable_netpoll(port);
1077 }
1078 
1079 static void team_port_disable_netpoll(struct team_port *port)
1080 {
1081 	struct netpoll *np = port->np;
1082 
1083 	if (!np)
1084 		return;
1085 	port->np = NULL;
1086 
1087 	__netpoll_free(np);
1088 }
1089 #else
1090 static int team_port_enable_netpoll(struct team_port *port)
1091 {
1092 	return 0;
1093 }
1094 static void team_port_disable_netpoll(struct team_port *port)
1095 {
1096 }
1097 #endif
1098 
1099 static int team_upper_dev_link(struct team *team, struct team_port *port,
1100 			       struct netlink_ext_ack *extack)
1101 {
1102 	struct netdev_lag_upper_info lag_upper_info;
1103 	int err;
1104 
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;
1113 }
1114 
1115 static void team_upper_dev_unlink(struct team *team, struct team_port *port)
1116 {
1117 	netdev_upper_dev_unlink(port->dev, team->dev);
1118 	port->dev->priv_flags &= ~IFF_TEAM_PORT;
1119 }
1120 
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);
1124 
1125 static int team_port_add(struct team *team, struct net_device *port_dev,
1126 			 struct netlink_ext_ack *extack)
1127 {
1128 	struct net_device *dev = team->dev;
1129 	struct team_port *port;
1130 	char *portname = port_dev->name;
1131 	int err;
1132 
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;
1138 	}
1139 
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;
1145 	}
1146 
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;
1151 	}
1152 
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;
1158 	}
1159 
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;
1166 	}
1167 
1168 	err = team_dev_type_check_change(dev, port_dev);
1169 	if (err)
1170 		return err;
1171 
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;
1177 	}
1178 
1179 	port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1180 		       GFP_KERNEL);
1181 	if (!port)
1182 		return -ENOMEM;
1183 
1184 	port->dev = port_dev;
1185 	port->team = team;
1186 	INIT_LIST_HEAD(&port->qom_list);
1187 
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;
1193 	}
1194 
1195 	memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1196 
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;
1202 	}
1203 
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;
1209 	}
1210 
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;
1216 	}
1217 
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;
1223 	}
1224 
1225 	if (!(dev->features & NETIF_F_LRO))
1226 		dev_disable_lro(port_dev);
1227 
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;
1234 	}
1235 
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;
1241 	}
1242 
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;
1248 	}
1249 
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;
1255 	}
1256 
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;
1264 		}
1265 	}
1266 
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);
1271 
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);
1278 
1279 	netdev_info(dev, "Port device %s added\n", portname);
1280 
1281 	return 0;
1282 
1283 err_set_slave_promisc:
1284 	__team_option_inst_del_port(team, port);
1285 
1286 err_option_port_add:
1287 	team_upper_dev_unlink(team, port);
1288 
1289 err_set_upper_link:
1290 	netdev_rx_handler_unregister(port_dev);
1291 
1292 err_handler_register:
1293 	team_port_disable_netpoll(port);
1294 
1295 err_enable_netpoll:
1296 	vlan_vids_del_by_dev(port_dev, dev);
1297 
1298 err_vids_add:
1299 	dev_close(port_dev);
1300 
1301 err_dev_open:
1302 	team_port_leave(team, port);
1303 	team_port_set_orig_dev_addr(port);
1304 
1305 err_port_enter:
1306 	dev_set_mtu(port_dev, port->orig.mtu);
1307 
1308 err_set_mtu:
1309 	kfree(port);
1310 
1311 	return err;
1312 }
1313 
1314 static void __team_port_change_port_removed(struct team_port *port);
1315 
1316 static int team_port_del(struct team *team, struct net_device *port_dev)
1317 {
1318 	struct net_device *dev = team->dev;
1319 	struct team_port *port;
1320 	char *portname = port_dev->name;
1321 
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;
1327 	}
1328 
1329 	team_port_disable(team, port);
1330 	list_del_rcu(&port->list);
1331 
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);
1336 
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);
1345 
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);
1350 
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);
1356 
1357 	return 0;
1358 }
1359 
1360 
1361 /*****************
1362  * Net device ops
1363  *****************/
1364 
1365 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1366 {
1367 	ctx->data.str_val = team->mode->kind;
1368 	return 0;
1369 }
1370 
1371 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1372 {
1373 	return team_change_mode(team, ctx->data.str_val);
1374 }
1375 
1376 static int team_notify_peers_count_get(struct team *team,
1377 				       struct team_gsetter_ctx *ctx)
1378 {
1379 	ctx->data.u32_val = team->notify_peers.count;
1380 	return 0;
1381 }
1382 
1383 static int team_notify_peers_count_set(struct team *team,
1384 				       struct team_gsetter_ctx *ctx)
1385 {
1386 	team->notify_peers.count = ctx->data.u32_val;
1387 	return 0;
1388 }
1389 
1390 static int team_notify_peers_interval_get(struct team *team,
1391 					  struct team_gsetter_ctx *ctx)
1392 {
1393 	ctx->data.u32_val = team->notify_peers.interval;
1394 	return 0;
1395 }
1396 
1397 static int team_notify_peers_interval_set(struct team *team,
1398 					  struct team_gsetter_ctx *ctx)
1399 {
1400 	team->notify_peers.interval = ctx->data.u32_val;
1401 	return 0;
1402 }
1403 
1404 static int team_mcast_rejoin_count_get(struct team *team,
1405 				       struct team_gsetter_ctx *ctx)
1406 {
1407 	ctx->data.u32_val = team->mcast_rejoin.count;
1408 	return 0;
1409 }
1410 
1411 static int team_mcast_rejoin_count_set(struct team *team,
1412 				       struct team_gsetter_ctx *ctx)
1413 {
1414 	team->mcast_rejoin.count = ctx->data.u32_val;
1415 	return 0;
1416 }
1417 
1418 static int team_mcast_rejoin_interval_get(struct team *team,
1419 					  struct team_gsetter_ctx *ctx)
1420 {
1421 	ctx->data.u32_val = team->mcast_rejoin.interval;
1422 	return 0;
1423 }
1424 
1425 static int team_mcast_rejoin_interval_set(struct team *team,
1426 					  struct team_gsetter_ctx *ctx)
1427 {
1428 	team->mcast_rejoin.interval = ctx->data.u32_val;
1429 	return 0;
1430 }
1431 
1432 static int team_port_en_option_get(struct team *team,
1433 				   struct team_gsetter_ctx *ctx)
1434 {
1435 	struct team_port *port = ctx->info->port;
1436 
1437 	ctx->data.bool_val = team_port_enabled(port);
1438 	return 0;
1439 }
1440 
1441 static int team_port_en_option_set(struct team *team,
1442 				   struct team_gsetter_ctx *ctx)
1443 {
1444 	struct team_port *port = ctx->info->port;
1445 
1446 	if (ctx->data.bool_val)
1447 		team_port_enable(team, port);
1448 	else
1449 		team_port_disable(team, port);
1450 	return 0;
1451 }
1452 
1453 static int team_user_linkup_option_get(struct team *team,
1454 				       struct team_gsetter_ctx *ctx)
1455 {
1456 	struct team_port *port = ctx->info->port;
1457 
1458 	ctx->data.bool_val = port->user.linkup;
1459 	return 0;
1460 }
1461 
1462 static void __team_carrier_check(struct team *team);
1463 
1464 static int team_user_linkup_option_set(struct team *team,
1465 				       struct team_gsetter_ctx *ctx)
1466 {
1467 	struct team_port *port = ctx->info->port;
1468 
1469 	port->user.linkup = ctx->data.bool_val;
1470 	team_refresh_port_linkup(port);
1471 	__team_carrier_check(port->team);
1472 	return 0;
1473 }
1474 
1475 static int team_user_linkup_en_option_get(struct team *team,
1476 					  struct team_gsetter_ctx *ctx)
1477 {
1478 	struct team_port *port = ctx->info->port;
1479 
1480 	ctx->data.bool_val = port->user.linkup_enabled;
1481 	return 0;
1482 }
1483 
1484 static int team_user_linkup_en_option_set(struct team *team,
1485 					  struct team_gsetter_ctx *ctx)
1486 {
1487 	struct team_port *port = ctx->info->port;
1488 
1489 	port->user.linkup_enabled = ctx->data.bool_val;
1490 	team_refresh_port_linkup(port);
1491 	__team_carrier_check(port->team);
1492 	return 0;
1493 }
1494 
1495 static int team_priority_option_get(struct team *team,
1496 				    struct team_gsetter_ctx *ctx)
1497 {
1498 	struct team_port *port = ctx->info->port;
1499 
1500 	ctx->data.s32_val = port->priority;
1501 	return 0;
1502 }
1503 
1504 static int team_priority_option_set(struct team *team,
1505 				    struct team_gsetter_ctx *ctx)
1506 {
1507 	struct team_port *port = ctx->info->port;
1508 	s32 priority = ctx->data.s32_val;
1509 
1510 	if (port->priority == priority)
1511 		return 0;
1512 	port->priority = priority;
1513 	team_queue_override_port_prio_changed(team, port);
1514 	return 0;
1515 }
1516 
1517 static int team_queue_id_option_get(struct team *team,
1518 				    struct team_gsetter_ctx *ctx)
1519 {
1520 	struct team_port *port = ctx->info->port;
1521 
1522 	ctx->data.u32_val = port->queue_id;
1523 	return 0;
1524 }
1525 
1526 static int team_queue_id_option_set(struct team *team,
1527 				    struct team_gsetter_ctx *ctx)
1528 {
1529 	struct team_port *port = ctx->info->port;
1530 	u16 new_queue_id = ctx->data.u32_val;
1531 
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;
1538 }
1539 
1540 static const struct team_option team_options[] = {
1541 	{
1542 		.name = "mode",
1543 		.type = TEAM_OPTION_TYPE_STRING,
1544 		.getter = team_mode_option_get,
1545 		.setter = team_mode_option_set,
1546 	},
1547 	{
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,
1552 	},
1553 	{
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,
1558 	},
1559 	{
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,
1564 	},
1565 	{
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,
1570 	},
1571 	{
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,
1577 	},
1578 	{
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,
1584 	},
1585 	{
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,
1591 	},
1592 	{
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,
1598 	},
1599 	{
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,
1605 	},
1606 };
1607 
1608 
1609 static int team_init(struct net_device *dev)
1610 {
1611 	struct team *team = netdev_priv(dev);
1612 	int i;
1613 	int err;
1614 
1615 	team->dev = dev;
1616 	mutex_init(&team->lock);
1617 	team_set_no_mode(team);
1618 
1619 	team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1620 	if (!team->pcpu_stats)
1621 		return -ENOMEM;
1622 
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;
1629 
1630 	team_adjust_ops(team);
1631 
1632 	INIT_LIST_HEAD(&team->option_list);
1633 	INIT_LIST_HEAD(&team->option_inst_list);
1634 
1635 	team_notify_peers_init(team);
1636 	team_mcast_rejoin_init(team);
1637 
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);
1642 
1643 	netdev_lockdep_set_classes(dev);
1644 
1645 	return 0;
1646 
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);
1653 
1654 	return err;
1655 }
1656 
1657 static void team_uninit(struct net_device *dev)
1658 {
1659 	struct team *team = netdev_priv(dev);
1660 	struct team_port *port;
1661 	struct team_port *tmp;
1662 
1663 	mutex_lock(&team->lock);
1664 	list_for_each_entry_safe(port, tmp, &team->port_list, list)
1665 		team_port_del(team, port->dev);
1666 
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);
1674 }
1675 
1676 static void team_destructor(struct net_device *dev)
1677 {
1678 	struct team *team = netdev_priv(dev);
1679 
1680 	free_percpu(team->pcpu_stats);
1681 }
1682 
1683 static int team_open(struct net_device *dev)
1684 {
1685 	return 0;
1686 }
1687 
1688 static int team_close(struct net_device *dev)
1689 {
1690 	return 0;
1691 }
1692 
1693 /*
1694  * note: already called with rcu_read_lock
1695  */
1696 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1697 {
1698 	struct team *team = netdev_priv(dev);
1699 	bool tx_success;
1700 	unsigned int len = skb->len;
1701 
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;
1707 
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);
1715 	}
1716 
1717 	return NETDEV_TX_OK;
1718 }
1719 
1720 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1721 			     struct net_device *sb_dev)
1722 {
1723 	/*
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.
1728 	 */
1729 	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1730 
1731 	/*
1732 	 * Save the original txq to restore before passing to the driver
1733 	 */
1734 	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1735 
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);
1740 	}
1741 	return txq;
1742 }
1743 
1744 static void team_change_rx_flags(struct net_device *dev, int change)
1745 {
1746 	struct team *team = netdev_priv(dev);
1747 	struct team_port *port;
1748 	int inc;
1749 
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);
1755 		}
1756 		if (change & IFF_ALLMULTI) {
1757 			inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1758 			dev_set_allmulti(port->dev, inc);
1759 		}
1760 	}
1761 	rcu_read_unlock();
1762 }
1763 
1764 static void team_set_rx_mode(struct net_device *dev)
1765 {
1766 	struct team *team = netdev_priv(dev);
1767 	struct team_port *port;
1768 
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);
1773 	}
1774 	rcu_read_unlock();
1775 }
1776 
1777 static int team_set_mac_address(struct net_device *dev, void *p)
1778 {
1779 	struct sockaddr *addr = p;
1780 	struct team *team = netdev_priv(dev);
1781 	struct team_port *port;
1782 
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;
1792 }
1793 
1794 static int team_change_mtu(struct net_device *dev, int new_mtu)
1795 {
1796 	struct team *team = netdev_priv(dev);
1797 	struct team_port *port;
1798 	int err;
1799 
1800 	/*
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
1803 	 */
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;
1812 		}
1813 	}
1814 	team->port_mtu_change_allowed = false;
1815 	mutex_unlock(&team->lock);
1816 
1817 	dev->mtu = new_mtu;
1818 
1819 	return 0;
1820 
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);
1826 
1827 	return err;
1828 }
1829 
1830 static void
1831 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1832 {
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;
1839 
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));
1850 
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;
1856 		/*
1857 		 * rx_dropped, tx_dropped & rx_nohandler are u32,
1858 		 * updated without syncp protection.
1859 		 */
1860 		rx_dropped	+= p->rx_dropped;
1861 		tx_dropped	+= p->tx_dropped;
1862 		rx_nohandler	+= p->rx_nohandler;
1863 	}
1864 	stats->rx_dropped	= rx_dropped;
1865 	stats->tx_dropped	= tx_dropped;
1866 	stats->rx_nohandler	= rx_nohandler;
1867 }
1868 
1869 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1870 {
1871 	struct team *team = netdev_priv(dev);
1872 	struct team_port *port;
1873 	int err;
1874 
1875 	/*
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
1878 	 */
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;
1884 	}
1885 	mutex_unlock(&team->lock);
1886 
1887 	return 0;
1888 
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);
1893 
1894 	return err;
1895 }
1896 
1897 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1898 {
1899 	struct team *team = netdev_priv(dev);
1900 	struct team_port *port;
1901 
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);
1906 
1907 	return 0;
1908 }
1909 
1910 #ifdef CONFIG_NET_POLL_CONTROLLER
1911 static void team_poll_controller(struct net_device *dev)
1912 {
1913 }
1914 
1915 static void __team_netpoll_cleanup(struct team *team)
1916 {
1917 	struct team_port *port;
1918 
1919 	list_for_each_entry(port, &team->port_list, list)
1920 		team_port_disable_netpoll(port);
1921 }
1922 
1923 static void team_netpoll_cleanup(struct net_device *dev)
1924 {
1925 	struct team *team = netdev_priv(dev);
1926 
1927 	mutex_lock(&team->lock);
1928 	__team_netpoll_cleanup(team);
1929 	mutex_unlock(&team->lock);
1930 }
1931 
1932 static int team_netpoll_setup(struct net_device *dev,
1933 			      struct netpoll_info *npifo)
1934 {
1935 	struct team *team = netdev_priv(dev);
1936 	struct team_port *port;
1937 	int err = 0;
1938 
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;
1945 		}
1946 	}
1947 	mutex_unlock(&team->lock);
1948 	return err;
1949 }
1950 #endif
1951 
1952 static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
1953 			  struct netlink_ext_ack *extack)
1954 {
1955 	struct team *team = netdev_priv(dev);
1956 	int err;
1957 
1958 	mutex_lock(&team->lock);
1959 	err = team_port_add(team, port_dev, extack);
1960 	mutex_unlock(&team->lock);
1961 
1962 	if (!err)
1963 		netdev_change_features(dev);
1964 
1965 	return err;
1966 }
1967 
1968 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1969 {
1970 	struct team *team = netdev_priv(dev);
1971 	int err;
1972 
1973 	mutex_lock(&team->lock);
1974 	err = team_port_del(team, port_dev);
1975 	mutex_unlock(&team->lock);
1976 
1977 	if (!err)
1978 		netdev_change_features(dev);
1979 
1980 	return err;
1981 }
1982 
1983 static netdev_features_t team_fix_features(struct net_device *dev,
1984 					   netdev_features_t features)
1985 {
1986 	struct team_port *port;
1987 	struct team *team = netdev_priv(dev);
1988 	netdev_features_t mask;
1989 
1990 	mask = features;
1991 	features &= ~NETIF_F_ONE_FOR_ALL;
1992 	features |= NETIF_F_ALL_FOR_ALL;
1993 
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);
1999 	}
2000 	rcu_read_unlock();
2001 
2002 	features = netdev_add_tso_features(features, mask);
2003 
2004 	return features;
2005 }
2006 
2007 static int team_change_carrier(struct net_device *dev, bool new_carrier)
2008 {
2009 	struct team *team = netdev_priv(dev);
2010 
2011 	team->user_carrier_enabled = true;
2012 
2013 	if (new_carrier)
2014 		netif_carrier_on(dev);
2015 	else
2016 		netif_carrier_off(dev);
2017 	return 0;
2018 }
2019 
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,
2044 };
2045 
2046 /***********************
2047  * ethtool interface
2048  ***********************/
2049 
2050 static void team_ethtool_get_drvinfo(struct net_device *dev,
2051 				     struct ethtool_drvinfo *drvinfo)
2052 {
2053 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2054 	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2055 }
2056 
2057 static int team_ethtool_get_link_ksettings(struct net_device *dev,
2058 					   struct ethtool_link_ksettings *cmd)
2059 {
2060 	struct team *team= netdev_priv(dev);
2061 	unsigned long speed = 0;
2062 	struct team_port *port;
2063 
2064 	cmd->base.duplex = DUPLEX_UNKNOWN;
2065 	cmd->base.port = PORT_OTHER;
2066 
2067 	list_for_each_entry(port, &team->port_list, list) {
2068 		if (team_port_txable(port)) {
2069 			if (port->state.speed != SPEED_UNKNOWN)
2070 				speed += port->state.speed;
2071 			if (cmd->base.duplex == DUPLEX_UNKNOWN &&
2072 			    port->state.duplex != DUPLEX_UNKNOWN)
2073 				cmd->base.duplex = port->state.duplex;
2074 		}
2075 	}
2076 	cmd->base.speed = speed ? : SPEED_UNKNOWN;
2077 
2078 	return 0;
2079 }
2080 
2081 static const struct ethtool_ops team_ethtool_ops = {
2082 	.get_drvinfo		= team_ethtool_get_drvinfo,
2083 	.get_link		= ethtool_op_get_link,
2084 	.get_link_ksettings	= team_ethtool_get_link_ksettings,
2085 };
2086 
2087 /***********************
2088  * rt netlink interface
2089  ***********************/
2090 
2091 static void team_setup_by_port(struct net_device *dev,
2092 			       struct net_device *port_dev)
2093 {
2094 	dev->header_ops	= port_dev->header_ops;
2095 	dev->type = port_dev->type;
2096 	dev->hard_header_len = port_dev->hard_header_len;
2097 	dev->addr_len = port_dev->addr_len;
2098 	dev->mtu = port_dev->mtu;
2099 	memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2100 	eth_hw_addr_inherit(dev, port_dev);
2101 }
2102 
2103 static int team_dev_type_check_change(struct net_device *dev,
2104 				      struct net_device *port_dev)
2105 {
2106 	struct team *team = netdev_priv(dev);
2107 	char *portname = port_dev->name;
2108 	int err;
2109 
2110 	if (dev->type == port_dev->type)
2111 		return 0;
2112 	if (!list_empty(&team->port_list)) {
2113 		netdev_err(dev, "Device %s is of different type\n", portname);
2114 		return -EBUSY;
2115 	}
2116 	err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2117 	err = notifier_to_errno(err);
2118 	if (err) {
2119 		netdev_err(dev, "Refused to change device type\n");
2120 		return err;
2121 	}
2122 	dev_uc_flush(dev);
2123 	dev_mc_flush(dev);
2124 	team_setup_by_port(dev, port_dev);
2125 	call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2126 	return 0;
2127 }
2128 
2129 static void team_setup(struct net_device *dev)
2130 {
2131 	ether_setup(dev);
2132 	dev->max_mtu = ETH_MAX_MTU;
2133 
2134 	dev->netdev_ops = &team_netdev_ops;
2135 	dev->ethtool_ops = &team_ethtool_ops;
2136 	dev->needs_free_netdev = true;
2137 	dev->priv_destructor = team_destructor;
2138 	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2139 	dev->priv_flags |= IFF_NO_QUEUE;
2140 	dev->priv_flags |= IFF_TEAM;
2141 
2142 	/*
2143 	 * Indicate we support unicast address filtering. That way core won't
2144 	 * bring us to promisc mode in case a unicast addr is added.
2145 	 * Let this up to underlay drivers.
2146 	 */
2147 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2148 
2149 	dev->features |= NETIF_F_LLTX;
2150 	dev->features |= NETIF_F_GRO;
2151 
2152 	/* Don't allow team devices to change network namespaces. */
2153 	dev->features |= NETIF_F_NETNS_LOCAL;
2154 
2155 	dev->hw_features = TEAM_VLAN_FEATURES |
2156 			   NETIF_F_HW_VLAN_CTAG_RX |
2157 			   NETIF_F_HW_VLAN_CTAG_FILTER;
2158 
2159 	dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
2160 	dev->features |= dev->hw_features;
2161 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2162 }
2163 
2164 static int team_newlink(struct net *src_net, struct net_device *dev,
2165 			struct nlattr *tb[], struct nlattr *data[],
2166 			struct netlink_ext_ack *extack)
2167 {
2168 	if (tb[IFLA_ADDRESS] == NULL)
2169 		eth_hw_addr_random(dev);
2170 
2171 	return register_netdevice(dev);
2172 }
2173 
2174 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2175 			 struct netlink_ext_ack *extack)
2176 {
2177 	if (tb[IFLA_ADDRESS]) {
2178 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2179 			return -EINVAL;
2180 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2181 			return -EADDRNOTAVAIL;
2182 	}
2183 	return 0;
2184 }
2185 
2186 static unsigned int team_get_num_tx_queues(void)
2187 {
2188 	return TEAM_DEFAULT_NUM_TX_QUEUES;
2189 }
2190 
2191 static unsigned int team_get_num_rx_queues(void)
2192 {
2193 	return TEAM_DEFAULT_NUM_RX_QUEUES;
2194 }
2195 
2196 static struct rtnl_link_ops team_link_ops __read_mostly = {
2197 	.kind			= DRV_NAME,
2198 	.priv_size		= sizeof(struct team),
2199 	.setup			= team_setup,
2200 	.newlink		= team_newlink,
2201 	.validate		= team_validate,
2202 	.get_num_tx_queues	= team_get_num_tx_queues,
2203 	.get_num_rx_queues	= team_get_num_rx_queues,
2204 };
2205 
2206 
2207 /***********************************
2208  * Generic netlink custom interface
2209  ***********************************/
2210 
2211 static struct genl_family team_nl_family;
2212 
2213 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2214 	[TEAM_ATTR_UNSPEC]			= { .type = NLA_UNSPEC, },
2215 	[TEAM_ATTR_TEAM_IFINDEX]		= { .type = NLA_U32 },
2216 	[TEAM_ATTR_LIST_OPTION]			= { .type = NLA_NESTED },
2217 	[TEAM_ATTR_LIST_PORT]			= { .type = NLA_NESTED },
2218 };
2219 
2220 static const struct nla_policy
2221 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2222 	[TEAM_ATTR_OPTION_UNSPEC]		= { .type = NLA_UNSPEC, },
2223 	[TEAM_ATTR_OPTION_NAME] = {
2224 		.type = NLA_STRING,
2225 		.len = TEAM_STRING_MAX_LEN,
2226 	},
2227 	[TEAM_ATTR_OPTION_CHANGED]		= { .type = NLA_FLAG },
2228 	[TEAM_ATTR_OPTION_TYPE]			= { .type = NLA_U8 },
2229 	[TEAM_ATTR_OPTION_DATA]			= { .type = NLA_BINARY },
2230 };
2231 
2232 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2233 {
2234 	struct sk_buff *msg;
2235 	void *hdr;
2236 	int err;
2237 
2238 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2239 	if (!msg)
2240 		return -ENOMEM;
2241 
2242 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2243 			  &team_nl_family, 0, TEAM_CMD_NOOP);
2244 	if (!hdr) {
2245 		err = -EMSGSIZE;
2246 		goto err_msg_put;
2247 	}
2248 
2249 	genlmsg_end(msg, hdr);
2250 
2251 	return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2252 
2253 err_msg_put:
2254 	nlmsg_free(msg);
2255 
2256 	return err;
2257 }
2258 
2259 /*
2260  * Netlink cmd functions should be locked by following two functions.
2261  * Since dev gets held here, that ensures dev won't disappear in between.
2262  */
2263 static struct team *team_nl_team_get(struct genl_info *info)
2264 {
2265 	struct net *net = genl_info_net(info);
2266 	int ifindex;
2267 	struct net_device *dev;
2268 	struct team *team;
2269 
2270 	if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2271 		return NULL;
2272 
2273 	ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2274 	dev = dev_get_by_index(net, ifindex);
2275 	if (!dev || dev->netdev_ops != &team_netdev_ops) {
2276 		if (dev)
2277 			dev_put(dev);
2278 		return NULL;
2279 	}
2280 
2281 	team = netdev_priv(dev);
2282 	mutex_lock(&team->lock);
2283 	return team;
2284 }
2285 
2286 static void team_nl_team_put(struct team *team)
2287 {
2288 	mutex_unlock(&team->lock);
2289 	dev_put(team->dev);
2290 }
2291 
2292 typedef int team_nl_send_func_t(struct sk_buff *skb,
2293 				struct team *team, u32 portid);
2294 
2295 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2296 {
2297 	return genlmsg_unicast(dev_net(team->dev), skb, portid);
2298 }
2299 
2300 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2301 				       struct team_option_inst *opt_inst)
2302 {
2303 	struct nlattr *option_item;
2304 	struct team_option *option = opt_inst->option;
2305 	struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2306 	struct team_gsetter_ctx ctx;
2307 	int err;
2308 
2309 	ctx.info = opt_inst_info;
2310 	err = team_option_get(team, opt_inst, &ctx);
2311 	if (err)
2312 		return err;
2313 
2314 	option_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_OPTION);
2315 	if (!option_item)
2316 		return -EMSGSIZE;
2317 
2318 	if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2319 		goto nest_cancel;
2320 	if (opt_inst_info->port &&
2321 	    nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2322 			opt_inst_info->port->dev->ifindex))
2323 		goto nest_cancel;
2324 	if (opt_inst->option->array_size &&
2325 	    nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2326 			opt_inst_info->array_index))
2327 		goto nest_cancel;
2328 
2329 	switch (option->type) {
2330 	case TEAM_OPTION_TYPE_U32:
2331 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2332 			goto nest_cancel;
2333 		if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2334 			goto nest_cancel;
2335 		break;
2336 	case TEAM_OPTION_TYPE_STRING:
2337 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2338 			goto nest_cancel;
2339 		if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2340 				   ctx.data.str_val))
2341 			goto nest_cancel;
2342 		break;
2343 	case TEAM_OPTION_TYPE_BINARY:
2344 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2345 			goto nest_cancel;
2346 		if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2347 			    ctx.data.bin_val.ptr))
2348 			goto nest_cancel;
2349 		break;
2350 	case TEAM_OPTION_TYPE_BOOL:
2351 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2352 			goto nest_cancel;
2353 		if (ctx.data.bool_val &&
2354 		    nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2355 			goto nest_cancel;
2356 		break;
2357 	case TEAM_OPTION_TYPE_S32:
2358 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2359 			goto nest_cancel;
2360 		if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2361 			goto nest_cancel;
2362 		break;
2363 	default:
2364 		BUG();
2365 	}
2366 	if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2367 		goto nest_cancel;
2368 	if (opt_inst->changed) {
2369 		if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2370 			goto nest_cancel;
2371 		opt_inst->changed = false;
2372 	}
2373 	nla_nest_end(skb, option_item);
2374 	return 0;
2375 
2376 nest_cancel:
2377 	nla_nest_cancel(skb, option_item);
2378 	return -EMSGSIZE;
2379 }
2380 
2381 static int __send_and_alloc_skb(struct sk_buff **pskb,
2382 				struct team *team, u32 portid,
2383 				team_nl_send_func_t *send_func)
2384 {
2385 	int err;
2386 
2387 	if (*pskb) {
2388 		err = send_func(*pskb, team, portid);
2389 		if (err)
2390 			return err;
2391 	}
2392 	*pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2393 	if (!*pskb)
2394 		return -ENOMEM;
2395 	return 0;
2396 }
2397 
2398 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2399 				    int flags, team_nl_send_func_t *send_func,
2400 				    struct list_head *sel_opt_inst_list)
2401 {
2402 	struct nlattr *option_list;
2403 	struct nlmsghdr *nlh;
2404 	void *hdr;
2405 	struct team_option_inst *opt_inst;
2406 	int err;
2407 	struct sk_buff *skb = NULL;
2408 	bool incomplete;
2409 	int i;
2410 
2411 	opt_inst = list_first_entry(sel_opt_inst_list,
2412 				    struct team_option_inst, tmp_list);
2413 
2414 start_again:
2415 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2416 	if (err)
2417 		return err;
2418 
2419 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2420 			  TEAM_CMD_OPTIONS_GET);
2421 	if (!hdr) {
2422 		nlmsg_free(skb);
2423 		return -EMSGSIZE;
2424 	}
2425 
2426 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2427 		goto nla_put_failure;
2428 	option_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_OPTION);
2429 	if (!option_list)
2430 		goto nla_put_failure;
2431 
2432 	i = 0;
2433 	incomplete = false;
2434 	list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2435 		err = team_nl_fill_one_option_get(skb, team, opt_inst);
2436 		if (err) {
2437 			if (err == -EMSGSIZE) {
2438 				if (!i)
2439 					goto errout;
2440 				incomplete = true;
2441 				break;
2442 			}
2443 			goto errout;
2444 		}
2445 		i++;
2446 	}
2447 
2448 	nla_nest_end(skb, option_list);
2449 	genlmsg_end(skb, hdr);
2450 	if (incomplete)
2451 		goto start_again;
2452 
2453 send_done:
2454 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2455 	if (!nlh) {
2456 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2457 		if (err)
2458 			return err;
2459 		goto send_done;
2460 	}
2461 
2462 	return send_func(skb, team, portid);
2463 
2464 nla_put_failure:
2465 	err = -EMSGSIZE;
2466 errout:
2467 	nlmsg_free(skb);
2468 	return err;
2469 }
2470 
2471 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2472 {
2473 	struct team *team;
2474 	struct team_option_inst *opt_inst;
2475 	int err;
2476 	LIST_HEAD(sel_opt_inst_list);
2477 
2478 	team = team_nl_team_get(info);
2479 	if (!team)
2480 		return -EINVAL;
2481 
2482 	list_for_each_entry(opt_inst, &team->option_inst_list, list)
2483 		list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2484 	err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2485 				       NLM_F_ACK, team_nl_send_unicast,
2486 				       &sel_opt_inst_list);
2487 
2488 	team_nl_team_put(team);
2489 
2490 	return err;
2491 }
2492 
2493 static int team_nl_send_event_options_get(struct team *team,
2494 					  struct list_head *sel_opt_inst_list);
2495 
2496 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2497 {
2498 	struct team *team;
2499 	int err = 0;
2500 	int i;
2501 	struct nlattr *nl_option;
2502 
2503 	rtnl_lock();
2504 
2505 	team = team_nl_team_get(info);
2506 	if (!team) {
2507 		err = -EINVAL;
2508 		goto rtnl_unlock;
2509 	}
2510 
2511 	err = -EINVAL;
2512 	if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2513 		err = -EINVAL;
2514 		goto team_put;
2515 	}
2516 
2517 	nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2518 		struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2519 		struct nlattr *attr;
2520 		struct nlattr *attr_data;
2521 		LIST_HEAD(opt_inst_list);
2522 		enum team_option_type opt_type;
2523 		int opt_port_ifindex = 0; /* != 0 for per-port options */
2524 		u32 opt_array_index = 0;
2525 		bool opt_is_array = false;
2526 		struct team_option_inst *opt_inst;
2527 		char *opt_name;
2528 		bool opt_found = false;
2529 
2530 		if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2531 			err = -EINVAL;
2532 			goto team_put;
2533 		}
2534 		err = nla_parse_nested_deprecated(opt_attrs,
2535 						  TEAM_ATTR_OPTION_MAX,
2536 						  nl_option,
2537 						  team_nl_option_policy,
2538 						  info->extack);
2539 		if (err)
2540 			goto team_put;
2541 		if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2542 		    !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2543 			err = -EINVAL;
2544 			goto team_put;
2545 		}
2546 		switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2547 		case NLA_U32:
2548 			opt_type = TEAM_OPTION_TYPE_U32;
2549 			break;
2550 		case NLA_STRING:
2551 			opt_type = TEAM_OPTION_TYPE_STRING;
2552 			break;
2553 		case NLA_BINARY:
2554 			opt_type = TEAM_OPTION_TYPE_BINARY;
2555 			break;
2556 		case NLA_FLAG:
2557 			opt_type = TEAM_OPTION_TYPE_BOOL;
2558 			break;
2559 		case NLA_S32:
2560 			opt_type = TEAM_OPTION_TYPE_S32;
2561 			break;
2562 		default:
2563 			goto team_put;
2564 		}
2565 
2566 		attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2567 		if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2568 			err = -EINVAL;
2569 			goto team_put;
2570 		}
2571 
2572 		opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2573 		attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2574 		if (attr)
2575 			opt_port_ifindex = nla_get_u32(attr);
2576 
2577 		attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2578 		if (attr) {
2579 			opt_is_array = true;
2580 			opt_array_index = nla_get_u32(attr);
2581 		}
2582 
2583 		list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2584 			struct team_option *option = opt_inst->option;
2585 			struct team_gsetter_ctx ctx;
2586 			struct team_option_inst_info *opt_inst_info;
2587 			int tmp_ifindex;
2588 
2589 			opt_inst_info = &opt_inst->info;
2590 			tmp_ifindex = opt_inst_info->port ?
2591 				      opt_inst_info->port->dev->ifindex : 0;
2592 			if (option->type != opt_type ||
2593 			    strcmp(option->name, opt_name) ||
2594 			    tmp_ifindex != opt_port_ifindex ||
2595 			    (option->array_size && !opt_is_array) ||
2596 			    opt_inst_info->array_index != opt_array_index)
2597 				continue;
2598 			opt_found = true;
2599 			ctx.info = opt_inst_info;
2600 			switch (opt_type) {
2601 			case TEAM_OPTION_TYPE_U32:
2602 				ctx.data.u32_val = nla_get_u32(attr_data);
2603 				break;
2604 			case TEAM_OPTION_TYPE_STRING:
2605 				if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2606 					err = -EINVAL;
2607 					goto team_put;
2608 				}
2609 				ctx.data.str_val = nla_data(attr_data);
2610 				break;
2611 			case TEAM_OPTION_TYPE_BINARY:
2612 				ctx.data.bin_val.len = nla_len(attr_data);
2613 				ctx.data.bin_val.ptr = nla_data(attr_data);
2614 				break;
2615 			case TEAM_OPTION_TYPE_BOOL:
2616 				ctx.data.bool_val = attr_data ? true : false;
2617 				break;
2618 			case TEAM_OPTION_TYPE_S32:
2619 				ctx.data.s32_val = nla_get_s32(attr_data);
2620 				break;
2621 			default:
2622 				BUG();
2623 			}
2624 			err = team_option_set(team, opt_inst, &ctx);
2625 			if (err)
2626 				goto team_put;
2627 			opt_inst->changed = true;
2628 			list_add(&opt_inst->tmp_list, &opt_inst_list);
2629 		}
2630 		if (!opt_found) {
2631 			err = -ENOENT;
2632 			goto team_put;
2633 		}
2634 
2635 		err = team_nl_send_event_options_get(team, &opt_inst_list);
2636 		if (err)
2637 			break;
2638 	}
2639 
2640 team_put:
2641 	team_nl_team_put(team);
2642 rtnl_unlock:
2643 	rtnl_unlock();
2644 	return err;
2645 }
2646 
2647 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2648 				     struct team_port *port)
2649 {
2650 	struct nlattr *port_item;
2651 
2652 	port_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_PORT);
2653 	if (!port_item)
2654 		goto nest_cancel;
2655 	if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2656 		goto nest_cancel;
2657 	if (port->changed) {
2658 		if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2659 			goto nest_cancel;
2660 		port->changed = false;
2661 	}
2662 	if ((port->removed &&
2663 	     nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2664 	    (port->state.linkup &&
2665 	     nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2666 	    nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2667 	    nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2668 		goto nest_cancel;
2669 	nla_nest_end(skb, port_item);
2670 	return 0;
2671 
2672 nest_cancel:
2673 	nla_nest_cancel(skb, port_item);
2674 	return -EMSGSIZE;
2675 }
2676 
2677 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2678 				      int flags, team_nl_send_func_t *send_func,
2679 				      struct team_port *one_port)
2680 {
2681 	struct nlattr *port_list;
2682 	struct nlmsghdr *nlh;
2683 	void *hdr;
2684 	struct team_port *port;
2685 	int err;
2686 	struct sk_buff *skb = NULL;
2687 	bool incomplete;
2688 	int i;
2689 
2690 	port = list_first_entry_or_null(&team->port_list,
2691 					struct team_port, list);
2692 
2693 start_again:
2694 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2695 	if (err)
2696 		return err;
2697 
2698 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2699 			  TEAM_CMD_PORT_LIST_GET);
2700 	if (!hdr) {
2701 		nlmsg_free(skb);
2702 		return -EMSGSIZE;
2703 	}
2704 
2705 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2706 		goto nla_put_failure;
2707 	port_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_PORT);
2708 	if (!port_list)
2709 		goto nla_put_failure;
2710 
2711 	i = 0;
2712 	incomplete = false;
2713 
2714 	/* If one port is selected, called wants to send port list containing
2715 	 * only this port. Otherwise go through all listed ports and send all
2716 	 */
2717 	if (one_port) {
2718 		err = team_nl_fill_one_port_get(skb, one_port);
2719 		if (err)
2720 			goto errout;
2721 	} else if (port) {
2722 		list_for_each_entry_from(port, &team->port_list, list) {
2723 			err = team_nl_fill_one_port_get(skb, port);
2724 			if (err) {
2725 				if (err == -EMSGSIZE) {
2726 					if (!i)
2727 						goto errout;
2728 					incomplete = true;
2729 					break;
2730 				}
2731 				goto errout;
2732 			}
2733 			i++;
2734 		}
2735 	}
2736 
2737 	nla_nest_end(skb, port_list);
2738 	genlmsg_end(skb, hdr);
2739 	if (incomplete)
2740 		goto start_again;
2741 
2742 send_done:
2743 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2744 	if (!nlh) {
2745 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2746 		if (err)
2747 			return err;
2748 		goto send_done;
2749 	}
2750 
2751 	return send_func(skb, team, portid);
2752 
2753 nla_put_failure:
2754 	err = -EMSGSIZE;
2755 errout:
2756 	nlmsg_free(skb);
2757 	return err;
2758 }
2759 
2760 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2761 				     struct genl_info *info)
2762 {
2763 	struct team *team;
2764 	int err;
2765 
2766 	team = team_nl_team_get(info);
2767 	if (!team)
2768 		return -EINVAL;
2769 
2770 	err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2771 					 NLM_F_ACK, team_nl_send_unicast, NULL);
2772 
2773 	team_nl_team_put(team);
2774 
2775 	return err;
2776 }
2777 
2778 static const struct genl_ops team_nl_ops[] = {
2779 	{
2780 		.cmd = TEAM_CMD_NOOP,
2781 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2782 		.doit = team_nl_cmd_noop,
2783 	},
2784 	{
2785 		.cmd = TEAM_CMD_OPTIONS_SET,
2786 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2787 		.doit = team_nl_cmd_options_set,
2788 		.flags = GENL_ADMIN_PERM,
2789 	},
2790 	{
2791 		.cmd = TEAM_CMD_OPTIONS_GET,
2792 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2793 		.doit = team_nl_cmd_options_get,
2794 		.flags = GENL_ADMIN_PERM,
2795 	},
2796 	{
2797 		.cmd = TEAM_CMD_PORT_LIST_GET,
2798 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2799 		.doit = team_nl_cmd_port_list_get,
2800 		.flags = GENL_ADMIN_PERM,
2801 	},
2802 };
2803 
2804 static const struct genl_multicast_group team_nl_mcgrps[] = {
2805 	{ .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2806 };
2807 
2808 static struct genl_family team_nl_family __ro_after_init = {
2809 	.name		= TEAM_GENL_NAME,
2810 	.version	= TEAM_GENL_VERSION,
2811 	.maxattr	= TEAM_ATTR_MAX,
2812 	.policy = team_nl_policy,
2813 	.netnsok	= true,
2814 	.module		= THIS_MODULE,
2815 	.ops		= team_nl_ops,
2816 	.n_ops		= ARRAY_SIZE(team_nl_ops),
2817 	.mcgrps		= team_nl_mcgrps,
2818 	.n_mcgrps	= ARRAY_SIZE(team_nl_mcgrps),
2819 };
2820 
2821 static int team_nl_send_multicast(struct sk_buff *skb,
2822 				  struct team *team, u32 portid)
2823 {
2824 	return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2825 				       skb, 0, 0, GFP_KERNEL);
2826 }
2827 
2828 static int team_nl_send_event_options_get(struct team *team,
2829 					  struct list_head *sel_opt_inst_list)
2830 {
2831 	return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2832 					sel_opt_inst_list);
2833 }
2834 
2835 static int team_nl_send_event_port_get(struct team *team,
2836 				       struct team_port *port)
2837 {
2838 	return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2839 					  port);
2840 }
2841 
2842 static int __init team_nl_init(void)
2843 {
2844 	return genl_register_family(&team_nl_family);
2845 }
2846 
2847 static void team_nl_fini(void)
2848 {
2849 	genl_unregister_family(&team_nl_family);
2850 }
2851 
2852 
2853 /******************
2854  * Change checkers
2855  ******************/
2856 
2857 static void __team_options_change_check(struct team *team)
2858 {
2859 	int err;
2860 	struct team_option_inst *opt_inst;
2861 	LIST_HEAD(sel_opt_inst_list);
2862 
2863 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2864 		if (opt_inst->changed)
2865 			list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2866 	}
2867 	err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2868 	if (err && err != -ESRCH)
2869 		netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2870 			    err);
2871 }
2872 
2873 /* rtnl lock is held */
2874 
2875 static void __team_port_change_send(struct team_port *port, bool linkup)
2876 {
2877 	int err;
2878 
2879 	port->changed = true;
2880 	port->state.linkup = linkup;
2881 	team_refresh_port_linkup(port);
2882 	if (linkup) {
2883 		struct ethtool_link_ksettings ecmd;
2884 
2885 		err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2886 		if (!err) {
2887 			port->state.speed = ecmd.base.speed;
2888 			port->state.duplex = ecmd.base.duplex;
2889 			goto send_event;
2890 		}
2891 	}
2892 	port->state.speed = 0;
2893 	port->state.duplex = 0;
2894 
2895 send_event:
2896 	err = team_nl_send_event_port_get(port->team, port);
2897 	if (err && err != -ESRCH)
2898 		netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2899 			    port->dev->name, err);
2900 
2901 }
2902 
2903 static void __team_carrier_check(struct team *team)
2904 {
2905 	struct team_port *port;
2906 	bool team_linkup;
2907 
2908 	if (team->user_carrier_enabled)
2909 		return;
2910 
2911 	team_linkup = false;
2912 	list_for_each_entry(port, &team->port_list, list) {
2913 		if (port->linkup) {
2914 			team_linkup = true;
2915 			break;
2916 		}
2917 	}
2918 
2919 	if (team_linkup)
2920 		netif_carrier_on(team->dev);
2921 	else
2922 		netif_carrier_off(team->dev);
2923 }
2924 
2925 static void __team_port_change_check(struct team_port *port, bool linkup)
2926 {
2927 	if (port->state.linkup != linkup)
2928 		__team_port_change_send(port, linkup);
2929 	__team_carrier_check(port->team);
2930 }
2931 
2932 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2933 {
2934 	__team_port_change_send(port, linkup);
2935 	__team_carrier_check(port->team);
2936 }
2937 
2938 static void __team_port_change_port_removed(struct team_port *port)
2939 {
2940 	port->removed = true;
2941 	__team_port_change_send(port, false);
2942 	__team_carrier_check(port->team);
2943 }
2944 
2945 static void team_port_change_check(struct team_port *port, bool linkup)
2946 {
2947 	struct team *team = port->team;
2948 
2949 	mutex_lock(&team->lock);
2950 	__team_port_change_check(port, linkup);
2951 	mutex_unlock(&team->lock);
2952 }
2953 
2954 
2955 /************************************
2956  * Net device notifier event handler
2957  ************************************/
2958 
2959 static int team_device_event(struct notifier_block *unused,
2960 			     unsigned long event, void *ptr)
2961 {
2962 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2963 	struct team_port *port;
2964 
2965 	port = team_port_get_rtnl(dev);
2966 	if (!port)
2967 		return NOTIFY_DONE;
2968 
2969 	switch (event) {
2970 	case NETDEV_UP:
2971 		if (netif_oper_up(dev))
2972 			team_port_change_check(port, true);
2973 		break;
2974 	case NETDEV_DOWN:
2975 		team_port_change_check(port, false);
2976 		break;
2977 	case NETDEV_CHANGE:
2978 		if (netif_running(port->dev))
2979 			team_port_change_check(port,
2980 					       !!netif_oper_up(port->dev));
2981 		break;
2982 	case NETDEV_UNREGISTER:
2983 		team_del_slave(port->team->dev, dev);
2984 		break;
2985 	case NETDEV_FEAT_CHANGE:
2986 		team_compute_features(port->team);
2987 		break;
2988 	case NETDEV_PRECHANGEMTU:
2989 		/* Forbid to change mtu of underlaying device */
2990 		if (!port->team->port_mtu_change_allowed)
2991 			return NOTIFY_BAD;
2992 		break;
2993 	case NETDEV_PRE_TYPE_CHANGE:
2994 		/* Forbid to change type of underlaying device */
2995 		return NOTIFY_BAD;
2996 	case NETDEV_RESEND_IGMP:
2997 		/* Propagate to master device */
2998 		call_netdevice_notifiers(event, port->team->dev);
2999 		break;
3000 	}
3001 	return NOTIFY_DONE;
3002 }
3003 
3004 static struct notifier_block team_notifier_block __read_mostly = {
3005 	.notifier_call = team_device_event,
3006 };
3007 
3008 
3009 /***********************
3010  * Module init and exit
3011  ***********************/
3012 
3013 static int __init team_module_init(void)
3014 {
3015 	int err;
3016 
3017 	register_netdevice_notifier(&team_notifier_block);
3018 
3019 	err = rtnl_link_register(&team_link_ops);
3020 	if (err)
3021 		goto err_rtnl_reg;
3022 
3023 	err = team_nl_init();
3024 	if (err)
3025 		goto err_nl_init;
3026 
3027 	return 0;
3028 
3029 err_nl_init:
3030 	rtnl_link_unregister(&team_link_ops);
3031 
3032 err_rtnl_reg:
3033 	unregister_netdevice_notifier(&team_notifier_block);
3034 
3035 	return err;
3036 }
3037 
3038 static void __exit team_module_exit(void)
3039 {
3040 	team_nl_fini();
3041 	rtnl_link_unregister(&team_link_ops);
3042 	unregister_netdevice_notifier(&team_notifier_block);
3043 }
3044 
3045 module_init(team_module_init);
3046 module_exit(team_module_exit);
3047 
3048 MODULE_LICENSE("GPL v2");
3049 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
3050 MODULE_DESCRIPTION("Ethernet team device driver");
3051 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
3052