xref: /openbmc/linux/drivers/net/team/team.c (revision 22d55f02)
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 const struct ethtool_ops team_ethtool_ops = {
2058 	.get_drvinfo		= team_ethtool_get_drvinfo,
2059 	.get_link		= ethtool_op_get_link,
2060 };
2061 
2062 /***********************
2063  * rt netlink interface
2064  ***********************/
2065 
2066 static void team_setup_by_port(struct net_device *dev,
2067 			       struct net_device *port_dev)
2068 {
2069 	dev->header_ops	= port_dev->header_ops;
2070 	dev->type = port_dev->type;
2071 	dev->hard_header_len = port_dev->hard_header_len;
2072 	dev->addr_len = port_dev->addr_len;
2073 	dev->mtu = port_dev->mtu;
2074 	memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2075 	eth_hw_addr_inherit(dev, port_dev);
2076 }
2077 
2078 static int team_dev_type_check_change(struct net_device *dev,
2079 				      struct net_device *port_dev)
2080 {
2081 	struct team *team = netdev_priv(dev);
2082 	char *portname = port_dev->name;
2083 	int err;
2084 
2085 	if (dev->type == port_dev->type)
2086 		return 0;
2087 	if (!list_empty(&team->port_list)) {
2088 		netdev_err(dev, "Device %s is of different type\n", portname);
2089 		return -EBUSY;
2090 	}
2091 	err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2092 	err = notifier_to_errno(err);
2093 	if (err) {
2094 		netdev_err(dev, "Refused to change device type\n");
2095 		return err;
2096 	}
2097 	dev_uc_flush(dev);
2098 	dev_mc_flush(dev);
2099 	team_setup_by_port(dev, port_dev);
2100 	call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2101 	return 0;
2102 }
2103 
2104 static void team_setup(struct net_device *dev)
2105 {
2106 	ether_setup(dev);
2107 	dev->max_mtu = ETH_MAX_MTU;
2108 
2109 	dev->netdev_ops = &team_netdev_ops;
2110 	dev->ethtool_ops = &team_ethtool_ops;
2111 	dev->needs_free_netdev = true;
2112 	dev->priv_destructor = team_destructor;
2113 	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2114 	dev->priv_flags |= IFF_NO_QUEUE;
2115 	dev->priv_flags |= IFF_TEAM;
2116 
2117 	/*
2118 	 * Indicate we support unicast address filtering. That way core won't
2119 	 * bring us to promisc mode in case a unicast addr is added.
2120 	 * Let this up to underlay drivers.
2121 	 */
2122 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2123 
2124 	dev->features |= NETIF_F_LLTX;
2125 	dev->features |= NETIF_F_GRO;
2126 
2127 	/* Don't allow team devices to change network namespaces. */
2128 	dev->features |= NETIF_F_NETNS_LOCAL;
2129 
2130 	dev->hw_features = TEAM_VLAN_FEATURES |
2131 			   NETIF_F_HW_VLAN_CTAG_RX |
2132 			   NETIF_F_HW_VLAN_CTAG_FILTER;
2133 
2134 	dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
2135 	dev->features |= dev->hw_features;
2136 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2137 }
2138 
2139 static int team_newlink(struct net *src_net, struct net_device *dev,
2140 			struct nlattr *tb[], struct nlattr *data[],
2141 			struct netlink_ext_ack *extack)
2142 {
2143 	if (tb[IFLA_ADDRESS] == NULL)
2144 		eth_hw_addr_random(dev);
2145 
2146 	return register_netdevice(dev);
2147 }
2148 
2149 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2150 			 struct netlink_ext_ack *extack)
2151 {
2152 	if (tb[IFLA_ADDRESS]) {
2153 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2154 			return -EINVAL;
2155 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2156 			return -EADDRNOTAVAIL;
2157 	}
2158 	return 0;
2159 }
2160 
2161 static unsigned int team_get_num_tx_queues(void)
2162 {
2163 	return TEAM_DEFAULT_NUM_TX_QUEUES;
2164 }
2165 
2166 static unsigned int team_get_num_rx_queues(void)
2167 {
2168 	return TEAM_DEFAULT_NUM_RX_QUEUES;
2169 }
2170 
2171 static struct rtnl_link_ops team_link_ops __read_mostly = {
2172 	.kind			= DRV_NAME,
2173 	.priv_size		= sizeof(struct team),
2174 	.setup			= team_setup,
2175 	.newlink		= team_newlink,
2176 	.validate		= team_validate,
2177 	.get_num_tx_queues	= team_get_num_tx_queues,
2178 	.get_num_rx_queues	= team_get_num_rx_queues,
2179 };
2180 
2181 
2182 /***********************************
2183  * Generic netlink custom interface
2184  ***********************************/
2185 
2186 static struct genl_family team_nl_family;
2187 
2188 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2189 	[TEAM_ATTR_UNSPEC]			= { .type = NLA_UNSPEC, },
2190 	[TEAM_ATTR_TEAM_IFINDEX]		= { .type = NLA_U32 },
2191 	[TEAM_ATTR_LIST_OPTION]			= { .type = NLA_NESTED },
2192 	[TEAM_ATTR_LIST_PORT]			= { .type = NLA_NESTED },
2193 };
2194 
2195 static const struct nla_policy
2196 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2197 	[TEAM_ATTR_OPTION_UNSPEC]		= { .type = NLA_UNSPEC, },
2198 	[TEAM_ATTR_OPTION_NAME] = {
2199 		.type = NLA_STRING,
2200 		.len = TEAM_STRING_MAX_LEN,
2201 	},
2202 	[TEAM_ATTR_OPTION_CHANGED]		= { .type = NLA_FLAG },
2203 	[TEAM_ATTR_OPTION_TYPE]			= { .type = NLA_U8 },
2204 	[TEAM_ATTR_OPTION_DATA]			= { .type = NLA_BINARY },
2205 };
2206 
2207 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2208 {
2209 	struct sk_buff *msg;
2210 	void *hdr;
2211 	int err;
2212 
2213 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2214 	if (!msg)
2215 		return -ENOMEM;
2216 
2217 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2218 			  &team_nl_family, 0, TEAM_CMD_NOOP);
2219 	if (!hdr) {
2220 		err = -EMSGSIZE;
2221 		goto err_msg_put;
2222 	}
2223 
2224 	genlmsg_end(msg, hdr);
2225 
2226 	return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2227 
2228 err_msg_put:
2229 	nlmsg_free(msg);
2230 
2231 	return err;
2232 }
2233 
2234 /*
2235  * Netlink cmd functions should be locked by following two functions.
2236  * Since dev gets held here, that ensures dev won't disappear in between.
2237  */
2238 static struct team *team_nl_team_get(struct genl_info *info)
2239 {
2240 	struct net *net = genl_info_net(info);
2241 	int ifindex;
2242 	struct net_device *dev;
2243 	struct team *team;
2244 
2245 	if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2246 		return NULL;
2247 
2248 	ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2249 	dev = dev_get_by_index(net, ifindex);
2250 	if (!dev || dev->netdev_ops != &team_netdev_ops) {
2251 		if (dev)
2252 			dev_put(dev);
2253 		return NULL;
2254 	}
2255 
2256 	team = netdev_priv(dev);
2257 	mutex_lock(&team->lock);
2258 	return team;
2259 }
2260 
2261 static void team_nl_team_put(struct team *team)
2262 {
2263 	mutex_unlock(&team->lock);
2264 	dev_put(team->dev);
2265 }
2266 
2267 typedef int team_nl_send_func_t(struct sk_buff *skb,
2268 				struct team *team, u32 portid);
2269 
2270 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2271 {
2272 	return genlmsg_unicast(dev_net(team->dev), skb, portid);
2273 }
2274 
2275 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2276 				       struct team_option_inst *opt_inst)
2277 {
2278 	struct nlattr *option_item;
2279 	struct team_option *option = opt_inst->option;
2280 	struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2281 	struct team_gsetter_ctx ctx;
2282 	int err;
2283 
2284 	ctx.info = opt_inst_info;
2285 	err = team_option_get(team, opt_inst, &ctx);
2286 	if (err)
2287 		return err;
2288 
2289 	option_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_OPTION);
2290 	if (!option_item)
2291 		return -EMSGSIZE;
2292 
2293 	if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2294 		goto nest_cancel;
2295 	if (opt_inst_info->port &&
2296 	    nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2297 			opt_inst_info->port->dev->ifindex))
2298 		goto nest_cancel;
2299 	if (opt_inst->option->array_size &&
2300 	    nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2301 			opt_inst_info->array_index))
2302 		goto nest_cancel;
2303 
2304 	switch (option->type) {
2305 	case TEAM_OPTION_TYPE_U32:
2306 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2307 			goto nest_cancel;
2308 		if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2309 			goto nest_cancel;
2310 		break;
2311 	case TEAM_OPTION_TYPE_STRING:
2312 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2313 			goto nest_cancel;
2314 		if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2315 				   ctx.data.str_val))
2316 			goto nest_cancel;
2317 		break;
2318 	case TEAM_OPTION_TYPE_BINARY:
2319 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2320 			goto nest_cancel;
2321 		if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2322 			    ctx.data.bin_val.ptr))
2323 			goto nest_cancel;
2324 		break;
2325 	case TEAM_OPTION_TYPE_BOOL:
2326 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2327 			goto nest_cancel;
2328 		if (ctx.data.bool_val &&
2329 		    nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2330 			goto nest_cancel;
2331 		break;
2332 	case TEAM_OPTION_TYPE_S32:
2333 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2334 			goto nest_cancel;
2335 		if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2336 			goto nest_cancel;
2337 		break;
2338 	default:
2339 		BUG();
2340 	}
2341 	if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2342 		goto nest_cancel;
2343 	if (opt_inst->changed) {
2344 		if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2345 			goto nest_cancel;
2346 		opt_inst->changed = false;
2347 	}
2348 	nla_nest_end(skb, option_item);
2349 	return 0;
2350 
2351 nest_cancel:
2352 	nla_nest_cancel(skb, option_item);
2353 	return -EMSGSIZE;
2354 }
2355 
2356 static int __send_and_alloc_skb(struct sk_buff **pskb,
2357 				struct team *team, u32 portid,
2358 				team_nl_send_func_t *send_func)
2359 {
2360 	int err;
2361 
2362 	if (*pskb) {
2363 		err = send_func(*pskb, team, portid);
2364 		if (err)
2365 			return err;
2366 	}
2367 	*pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2368 	if (!*pskb)
2369 		return -ENOMEM;
2370 	return 0;
2371 }
2372 
2373 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2374 				    int flags, team_nl_send_func_t *send_func,
2375 				    struct list_head *sel_opt_inst_list)
2376 {
2377 	struct nlattr *option_list;
2378 	struct nlmsghdr *nlh;
2379 	void *hdr;
2380 	struct team_option_inst *opt_inst;
2381 	int err;
2382 	struct sk_buff *skb = NULL;
2383 	bool incomplete;
2384 	int i;
2385 
2386 	opt_inst = list_first_entry(sel_opt_inst_list,
2387 				    struct team_option_inst, tmp_list);
2388 
2389 start_again:
2390 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2391 	if (err)
2392 		return err;
2393 
2394 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2395 			  TEAM_CMD_OPTIONS_GET);
2396 	if (!hdr) {
2397 		nlmsg_free(skb);
2398 		return -EMSGSIZE;
2399 	}
2400 
2401 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2402 		goto nla_put_failure;
2403 	option_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_OPTION);
2404 	if (!option_list)
2405 		goto nla_put_failure;
2406 
2407 	i = 0;
2408 	incomplete = false;
2409 	list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2410 		err = team_nl_fill_one_option_get(skb, team, opt_inst);
2411 		if (err) {
2412 			if (err == -EMSGSIZE) {
2413 				if (!i)
2414 					goto errout;
2415 				incomplete = true;
2416 				break;
2417 			}
2418 			goto errout;
2419 		}
2420 		i++;
2421 	}
2422 
2423 	nla_nest_end(skb, option_list);
2424 	genlmsg_end(skb, hdr);
2425 	if (incomplete)
2426 		goto start_again;
2427 
2428 send_done:
2429 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2430 	if (!nlh) {
2431 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2432 		if (err)
2433 			return err;
2434 		goto send_done;
2435 	}
2436 
2437 	return send_func(skb, team, portid);
2438 
2439 nla_put_failure:
2440 	err = -EMSGSIZE;
2441 errout:
2442 	nlmsg_free(skb);
2443 	return err;
2444 }
2445 
2446 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2447 {
2448 	struct team *team;
2449 	struct team_option_inst *opt_inst;
2450 	int err;
2451 	LIST_HEAD(sel_opt_inst_list);
2452 
2453 	team = team_nl_team_get(info);
2454 	if (!team)
2455 		return -EINVAL;
2456 
2457 	list_for_each_entry(opt_inst, &team->option_inst_list, list)
2458 		list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2459 	err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2460 				       NLM_F_ACK, team_nl_send_unicast,
2461 				       &sel_opt_inst_list);
2462 
2463 	team_nl_team_put(team);
2464 
2465 	return err;
2466 }
2467 
2468 static int team_nl_send_event_options_get(struct team *team,
2469 					  struct list_head *sel_opt_inst_list);
2470 
2471 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2472 {
2473 	struct team *team;
2474 	int err = 0;
2475 	int i;
2476 	struct nlattr *nl_option;
2477 
2478 	rtnl_lock();
2479 
2480 	team = team_nl_team_get(info);
2481 	if (!team) {
2482 		err = -EINVAL;
2483 		goto rtnl_unlock;
2484 	}
2485 
2486 	err = -EINVAL;
2487 	if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2488 		err = -EINVAL;
2489 		goto team_put;
2490 	}
2491 
2492 	nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2493 		struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2494 		struct nlattr *attr;
2495 		struct nlattr *attr_data;
2496 		LIST_HEAD(opt_inst_list);
2497 		enum team_option_type opt_type;
2498 		int opt_port_ifindex = 0; /* != 0 for per-port options */
2499 		u32 opt_array_index = 0;
2500 		bool opt_is_array = false;
2501 		struct team_option_inst *opt_inst;
2502 		char *opt_name;
2503 		bool opt_found = false;
2504 
2505 		if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2506 			err = -EINVAL;
2507 			goto team_put;
2508 		}
2509 		err = nla_parse_nested_deprecated(opt_attrs,
2510 						  TEAM_ATTR_OPTION_MAX,
2511 						  nl_option,
2512 						  team_nl_option_policy,
2513 						  info->extack);
2514 		if (err)
2515 			goto team_put;
2516 		if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2517 		    !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2518 			err = -EINVAL;
2519 			goto team_put;
2520 		}
2521 		switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2522 		case NLA_U32:
2523 			opt_type = TEAM_OPTION_TYPE_U32;
2524 			break;
2525 		case NLA_STRING:
2526 			opt_type = TEAM_OPTION_TYPE_STRING;
2527 			break;
2528 		case NLA_BINARY:
2529 			opt_type = TEAM_OPTION_TYPE_BINARY;
2530 			break;
2531 		case NLA_FLAG:
2532 			opt_type = TEAM_OPTION_TYPE_BOOL;
2533 			break;
2534 		case NLA_S32:
2535 			opt_type = TEAM_OPTION_TYPE_S32;
2536 			break;
2537 		default:
2538 			goto team_put;
2539 		}
2540 
2541 		attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2542 		if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2543 			err = -EINVAL;
2544 			goto team_put;
2545 		}
2546 
2547 		opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2548 		attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2549 		if (attr)
2550 			opt_port_ifindex = nla_get_u32(attr);
2551 
2552 		attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2553 		if (attr) {
2554 			opt_is_array = true;
2555 			opt_array_index = nla_get_u32(attr);
2556 		}
2557 
2558 		list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2559 			struct team_option *option = opt_inst->option;
2560 			struct team_gsetter_ctx ctx;
2561 			struct team_option_inst_info *opt_inst_info;
2562 			int tmp_ifindex;
2563 
2564 			opt_inst_info = &opt_inst->info;
2565 			tmp_ifindex = opt_inst_info->port ?
2566 				      opt_inst_info->port->dev->ifindex : 0;
2567 			if (option->type != opt_type ||
2568 			    strcmp(option->name, opt_name) ||
2569 			    tmp_ifindex != opt_port_ifindex ||
2570 			    (option->array_size && !opt_is_array) ||
2571 			    opt_inst_info->array_index != opt_array_index)
2572 				continue;
2573 			opt_found = true;
2574 			ctx.info = opt_inst_info;
2575 			switch (opt_type) {
2576 			case TEAM_OPTION_TYPE_U32:
2577 				ctx.data.u32_val = nla_get_u32(attr_data);
2578 				break;
2579 			case TEAM_OPTION_TYPE_STRING:
2580 				if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2581 					err = -EINVAL;
2582 					goto team_put;
2583 				}
2584 				ctx.data.str_val = nla_data(attr_data);
2585 				break;
2586 			case TEAM_OPTION_TYPE_BINARY:
2587 				ctx.data.bin_val.len = nla_len(attr_data);
2588 				ctx.data.bin_val.ptr = nla_data(attr_data);
2589 				break;
2590 			case TEAM_OPTION_TYPE_BOOL:
2591 				ctx.data.bool_val = attr_data ? true : false;
2592 				break;
2593 			case TEAM_OPTION_TYPE_S32:
2594 				ctx.data.s32_val = nla_get_s32(attr_data);
2595 				break;
2596 			default:
2597 				BUG();
2598 			}
2599 			err = team_option_set(team, opt_inst, &ctx);
2600 			if (err)
2601 				goto team_put;
2602 			opt_inst->changed = true;
2603 			list_add(&opt_inst->tmp_list, &opt_inst_list);
2604 		}
2605 		if (!opt_found) {
2606 			err = -ENOENT;
2607 			goto team_put;
2608 		}
2609 
2610 		err = team_nl_send_event_options_get(team, &opt_inst_list);
2611 		if (err)
2612 			break;
2613 	}
2614 
2615 team_put:
2616 	team_nl_team_put(team);
2617 rtnl_unlock:
2618 	rtnl_unlock();
2619 	return err;
2620 }
2621 
2622 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2623 				     struct team_port *port)
2624 {
2625 	struct nlattr *port_item;
2626 
2627 	port_item = nla_nest_start_noflag(skb, TEAM_ATTR_ITEM_PORT);
2628 	if (!port_item)
2629 		goto nest_cancel;
2630 	if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2631 		goto nest_cancel;
2632 	if (port->changed) {
2633 		if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2634 			goto nest_cancel;
2635 		port->changed = false;
2636 	}
2637 	if ((port->removed &&
2638 	     nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2639 	    (port->state.linkup &&
2640 	     nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2641 	    nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2642 	    nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2643 		goto nest_cancel;
2644 	nla_nest_end(skb, port_item);
2645 	return 0;
2646 
2647 nest_cancel:
2648 	nla_nest_cancel(skb, port_item);
2649 	return -EMSGSIZE;
2650 }
2651 
2652 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2653 				      int flags, team_nl_send_func_t *send_func,
2654 				      struct team_port *one_port)
2655 {
2656 	struct nlattr *port_list;
2657 	struct nlmsghdr *nlh;
2658 	void *hdr;
2659 	struct team_port *port;
2660 	int err;
2661 	struct sk_buff *skb = NULL;
2662 	bool incomplete;
2663 	int i;
2664 
2665 	port = list_first_entry_or_null(&team->port_list,
2666 					struct team_port, list);
2667 
2668 start_again:
2669 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2670 	if (err)
2671 		return err;
2672 
2673 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2674 			  TEAM_CMD_PORT_LIST_GET);
2675 	if (!hdr) {
2676 		nlmsg_free(skb);
2677 		return -EMSGSIZE;
2678 	}
2679 
2680 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2681 		goto nla_put_failure;
2682 	port_list = nla_nest_start_noflag(skb, TEAM_ATTR_LIST_PORT);
2683 	if (!port_list)
2684 		goto nla_put_failure;
2685 
2686 	i = 0;
2687 	incomplete = false;
2688 
2689 	/* If one port is selected, called wants to send port list containing
2690 	 * only this port. Otherwise go through all listed ports and send all
2691 	 */
2692 	if (one_port) {
2693 		err = team_nl_fill_one_port_get(skb, one_port);
2694 		if (err)
2695 			goto errout;
2696 	} else if (port) {
2697 		list_for_each_entry_from(port, &team->port_list, list) {
2698 			err = team_nl_fill_one_port_get(skb, port);
2699 			if (err) {
2700 				if (err == -EMSGSIZE) {
2701 					if (!i)
2702 						goto errout;
2703 					incomplete = true;
2704 					break;
2705 				}
2706 				goto errout;
2707 			}
2708 			i++;
2709 		}
2710 	}
2711 
2712 	nla_nest_end(skb, port_list);
2713 	genlmsg_end(skb, hdr);
2714 	if (incomplete)
2715 		goto start_again;
2716 
2717 send_done:
2718 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2719 	if (!nlh) {
2720 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2721 		if (err)
2722 			return err;
2723 		goto send_done;
2724 	}
2725 
2726 	return send_func(skb, team, portid);
2727 
2728 nla_put_failure:
2729 	err = -EMSGSIZE;
2730 errout:
2731 	nlmsg_free(skb);
2732 	return err;
2733 }
2734 
2735 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2736 				     struct genl_info *info)
2737 {
2738 	struct team *team;
2739 	int err;
2740 
2741 	team = team_nl_team_get(info);
2742 	if (!team)
2743 		return -EINVAL;
2744 
2745 	err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2746 					 NLM_F_ACK, team_nl_send_unicast, NULL);
2747 
2748 	team_nl_team_put(team);
2749 
2750 	return err;
2751 }
2752 
2753 static const struct genl_ops team_nl_ops[] = {
2754 	{
2755 		.cmd = TEAM_CMD_NOOP,
2756 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2757 		.doit = team_nl_cmd_noop,
2758 	},
2759 	{
2760 		.cmd = TEAM_CMD_OPTIONS_SET,
2761 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2762 		.doit = team_nl_cmd_options_set,
2763 		.flags = GENL_ADMIN_PERM,
2764 	},
2765 	{
2766 		.cmd = TEAM_CMD_OPTIONS_GET,
2767 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2768 		.doit = team_nl_cmd_options_get,
2769 		.flags = GENL_ADMIN_PERM,
2770 	},
2771 	{
2772 		.cmd = TEAM_CMD_PORT_LIST_GET,
2773 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2774 		.doit = team_nl_cmd_port_list_get,
2775 		.flags = GENL_ADMIN_PERM,
2776 	},
2777 };
2778 
2779 static const struct genl_multicast_group team_nl_mcgrps[] = {
2780 	{ .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2781 };
2782 
2783 static struct genl_family team_nl_family __ro_after_init = {
2784 	.name		= TEAM_GENL_NAME,
2785 	.version	= TEAM_GENL_VERSION,
2786 	.maxattr	= TEAM_ATTR_MAX,
2787 	.policy = team_nl_policy,
2788 	.netnsok	= true,
2789 	.module		= THIS_MODULE,
2790 	.ops		= team_nl_ops,
2791 	.n_ops		= ARRAY_SIZE(team_nl_ops),
2792 	.mcgrps		= team_nl_mcgrps,
2793 	.n_mcgrps	= ARRAY_SIZE(team_nl_mcgrps),
2794 };
2795 
2796 static int team_nl_send_multicast(struct sk_buff *skb,
2797 				  struct team *team, u32 portid)
2798 {
2799 	return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2800 				       skb, 0, 0, GFP_KERNEL);
2801 }
2802 
2803 static int team_nl_send_event_options_get(struct team *team,
2804 					  struct list_head *sel_opt_inst_list)
2805 {
2806 	return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2807 					sel_opt_inst_list);
2808 }
2809 
2810 static int team_nl_send_event_port_get(struct team *team,
2811 				       struct team_port *port)
2812 {
2813 	return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2814 					  port);
2815 }
2816 
2817 static int __init team_nl_init(void)
2818 {
2819 	return genl_register_family(&team_nl_family);
2820 }
2821 
2822 static void team_nl_fini(void)
2823 {
2824 	genl_unregister_family(&team_nl_family);
2825 }
2826 
2827 
2828 /******************
2829  * Change checkers
2830  ******************/
2831 
2832 static void __team_options_change_check(struct team *team)
2833 {
2834 	int err;
2835 	struct team_option_inst *opt_inst;
2836 	LIST_HEAD(sel_opt_inst_list);
2837 
2838 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2839 		if (opt_inst->changed)
2840 			list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2841 	}
2842 	err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2843 	if (err && err != -ESRCH)
2844 		netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2845 			    err);
2846 }
2847 
2848 /* rtnl lock is held */
2849 
2850 static void __team_port_change_send(struct team_port *port, bool linkup)
2851 {
2852 	int err;
2853 
2854 	port->changed = true;
2855 	port->state.linkup = linkup;
2856 	team_refresh_port_linkup(port);
2857 	if (linkup) {
2858 		struct ethtool_link_ksettings ecmd;
2859 
2860 		err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2861 		if (!err) {
2862 			port->state.speed = ecmd.base.speed;
2863 			port->state.duplex = ecmd.base.duplex;
2864 			goto send_event;
2865 		}
2866 	}
2867 	port->state.speed = 0;
2868 	port->state.duplex = 0;
2869 
2870 send_event:
2871 	err = team_nl_send_event_port_get(port->team, port);
2872 	if (err && err != -ESRCH)
2873 		netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2874 			    port->dev->name, err);
2875 
2876 }
2877 
2878 static void __team_carrier_check(struct team *team)
2879 {
2880 	struct team_port *port;
2881 	bool team_linkup;
2882 
2883 	if (team->user_carrier_enabled)
2884 		return;
2885 
2886 	team_linkup = false;
2887 	list_for_each_entry(port, &team->port_list, list) {
2888 		if (port->linkup) {
2889 			team_linkup = true;
2890 			break;
2891 		}
2892 	}
2893 
2894 	if (team_linkup)
2895 		netif_carrier_on(team->dev);
2896 	else
2897 		netif_carrier_off(team->dev);
2898 }
2899 
2900 static void __team_port_change_check(struct team_port *port, bool linkup)
2901 {
2902 	if (port->state.linkup != linkup)
2903 		__team_port_change_send(port, linkup);
2904 	__team_carrier_check(port->team);
2905 }
2906 
2907 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2908 {
2909 	__team_port_change_send(port, linkup);
2910 	__team_carrier_check(port->team);
2911 }
2912 
2913 static void __team_port_change_port_removed(struct team_port *port)
2914 {
2915 	port->removed = true;
2916 	__team_port_change_send(port, false);
2917 	__team_carrier_check(port->team);
2918 }
2919 
2920 static void team_port_change_check(struct team_port *port, bool linkup)
2921 {
2922 	struct team *team = port->team;
2923 
2924 	mutex_lock(&team->lock);
2925 	__team_port_change_check(port, linkup);
2926 	mutex_unlock(&team->lock);
2927 }
2928 
2929 
2930 /************************************
2931  * Net device notifier event handler
2932  ************************************/
2933 
2934 static int team_device_event(struct notifier_block *unused,
2935 			     unsigned long event, void *ptr)
2936 {
2937 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2938 	struct team_port *port;
2939 
2940 	port = team_port_get_rtnl(dev);
2941 	if (!port)
2942 		return NOTIFY_DONE;
2943 
2944 	switch (event) {
2945 	case NETDEV_UP:
2946 		if (netif_oper_up(dev))
2947 			team_port_change_check(port, true);
2948 		break;
2949 	case NETDEV_DOWN:
2950 		team_port_change_check(port, false);
2951 		break;
2952 	case NETDEV_CHANGE:
2953 		if (netif_running(port->dev))
2954 			team_port_change_check(port,
2955 					       !!netif_oper_up(port->dev));
2956 		break;
2957 	case NETDEV_UNREGISTER:
2958 		team_del_slave(port->team->dev, dev);
2959 		break;
2960 	case NETDEV_FEAT_CHANGE:
2961 		team_compute_features(port->team);
2962 		break;
2963 	case NETDEV_PRECHANGEMTU:
2964 		/* Forbid to change mtu of underlaying device */
2965 		if (!port->team->port_mtu_change_allowed)
2966 			return NOTIFY_BAD;
2967 		break;
2968 	case NETDEV_PRE_TYPE_CHANGE:
2969 		/* Forbid to change type of underlaying device */
2970 		return NOTIFY_BAD;
2971 	case NETDEV_RESEND_IGMP:
2972 		/* Propagate to master device */
2973 		call_netdevice_notifiers(event, port->team->dev);
2974 		break;
2975 	}
2976 	return NOTIFY_DONE;
2977 }
2978 
2979 static struct notifier_block team_notifier_block __read_mostly = {
2980 	.notifier_call = team_device_event,
2981 };
2982 
2983 
2984 /***********************
2985  * Module init and exit
2986  ***********************/
2987 
2988 static int __init team_module_init(void)
2989 {
2990 	int err;
2991 
2992 	register_netdevice_notifier(&team_notifier_block);
2993 
2994 	err = rtnl_link_register(&team_link_ops);
2995 	if (err)
2996 		goto err_rtnl_reg;
2997 
2998 	err = team_nl_init();
2999 	if (err)
3000 		goto err_nl_init;
3001 
3002 	return 0;
3003 
3004 err_nl_init:
3005 	rtnl_link_unregister(&team_link_ops);
3006 
3007 err_rtnl_reg:
3008 	unregister_netdevice_notifier(&team_notifier_block);
3009 
3010 	return err;
3011 }
3012 
3013 static void __exit team_module_exit(void)
3014 {
3015 	team_nl_fini();
3016 	rtnl_link_unregister(&team_link_ops);
3017 	unregister_netdevice_notifier(&team_notifier_block);
3018 }
3019 
3020 module_init(team_module_init);
3021 module_exit(team_module_exit);
3022 
3023 MODULE_LICENSE("GPL v2");
3024 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
3025 MODULE_DESCRIPTION("Ethernet team device driver");
3026 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
3027