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