xref: /openbmc/linux/drivers/net/team/team.c (revision dd5b2498)
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 (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1158 	    vlan_uses_dev(dev)) {
1159 		NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
1160 		netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1161 			   portname);
1162 		return -EPERM;
1163 	}
1164 
1165 	err = team_dev_type_check_change(dev, port_dev);
1166 	if (err)
1167 		return err;
1168 
1169 	if (port_dev->flags & IFF_UP) {
1170 		NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
1171 		netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1172 			   portname);
1173 		return -EBUSY;
1174 	}
1175 
1176 	port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1177 		       GFP_KERNEL);
1178 	if (!port)
1179 		return -ENOMEM;
1180 
1181 	port->dev = port_dev;
1182 	port->team = team;
1183 	INIT_LIST_HEAD(&port->qom_list);
1184 
1185 	port->orig.mtu = port_dev->mtu;
1186 	err = dev_set_mtu(port_dev, dev->mtu);
1187 	if (err) {
1188 		netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1189 		goto err_set_mtu;
1190 	}
1191 
1192 	memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1193 
1194 	err = team_port_enter(team, port);
1195 	if (err) {
1196 		netdev_err(dev, "Device %s failed to enter team mode\n",
1197 			   portname);
1198 		goto err_port_enter;
1199 	}
1200 
1201 	err = dev_open(port_dev, extack);
1202 	if (err) {
1203 		netdev_dbg(dev, "Device %s opening failed\n",
1204 			   portname);
1205 		goto err_dev_open;
1206 	}
1207 
1208 	err = vlan_vids_add_by_dev(port_dev, dev);
1209 	if (err) {
1210 		netdev_err(dev, "Failed to add vlan ids to device %s\n",
1211 				portname);
1212 		goto err_vids_add;
1213 	}
1214 
1215 	err = team_port_enable_netpoll(port);
1216 	if (err) {
1217 		netdev_err(dev, "Failed to enable netpoll on device %s\n",
1218 			   portname);
1219 		goto err_enable_netpoll;
1220 	}
1221 
1222 	if (!(dev->features & NETIF_F_LRO))
1223 		dev_disable_lro(port_dev);
1224 
1225 	err = netdev_rx_handler_register(port_dev, team_handle_frame,
1226 					 port);
1227 	if (err) {
1228 		netdev_err(dev, "Device %s failed to register rx_handler\n",
1229 			   portname);
1230 		goto err_handler_register;
1231 	}
1232 
1233 	err = team_upper_dev_link(team, port, extack);
1234 	if (err) {
1235 		netdev_err(dev, "Device %s failed to set upper link\n",
1236 			   portname);
1237 		goto err_set_upper_link;
1238 	}
1239 
1240 	err = __team_option_inst_add_port(team, port);
1241 	if (err) {
1242 		netdev_err(dev, "Device %s failed to add per-port options\n",
1243 			   portname);
1244 		goto err_option_port_add;
1245 	}
1246 
1247 	netif_addr_lock_bh(dev);
1248 	dev_uc_sync_multiple(port_dev, dev);
1249 	dev_mc_sync_multiple(port_dev, dev);
1250 	netif_addr_unlock_bh(dev);
1251 
1252 	port->index = -1;
1253 	list_add_tail_rcu(&port->list, &team->port_list);
1254 	team_port_enable(team, port);
1255 	__team_compute_features(team);
1256 	__team_port_change_port_added(port, !!netif_oper_up(port_dev));
1257 	__team_options_change_check(team);
1258 
1259 	netdev_info(dev, "Port device %s added\n", portname);
1260 
1261 	return 0;
1262 
1263 err_option_port_add:
1264 	team_upper_dev_unlink(team, port);
1265 
1266 err_set_upper_link:
1267 	netdev_rx_handler_unregister(port_dev);
1268 
1269 err_handler_register:
1270 	team_port_disable_netpoll(port);
1271 
1272 err_enable_netpoll:
1273 	vlan_vids_del_by_dev(port_dev, dev);
1274 
1275 err_vids_add:
1276 	dev_close(port_dev);
1277 
1278 err_dev_open:
1279 	team_port_leave(team, port);
1280 	team_port_set_orig_dev_addr(port);
1281 
1282 err_port_enter:
1283 	dev_set_mtu(port_dev, port->orig.mtu);
1284 
1285 err_set_mtu:
1286 	kfree(port);
1287 
1288 	return err;
1289 }
1290 
1291 static void __team_port_change_port_removed(struct team_port *port);
1292 
1293 static int team_port_del(struct team *team, struct net_device *port_dev)
1294 {
1295 	struct net_device *dev = team->dev;
1296 	struct team_port *port;
1297 	char *portname = port_dev->name;
1298 
1299 	port = team_port_get_rtnl(port_dev);
1300 	if (!port || !team_port_find(team, port)) {
1301 		netdev_err(dev, "Device %s does not act as a port of this team\n",
1302 			   portname);
1303 		return -ENOENT;
1304 	}
1305 
1306 	team_port_disable(team, port);
1307 	list_del_rcu(&port->list);
1308 	team_upper_dev_unlink(team, port);
1309 	netdev_rx_handler_unregister(port_dev);
1310 	team_port_disable_netpoll(port);
1311 	vlan_vids_del_by_dev(port_dev, dev);
1312 	dev_uc_unsync(port_dev, dev);
1313 	dev_mc_unsync(port_dev, dev);
1314 	dev_close(port_dev);
1315 	team_port_leave(team, port);
1316 
1317 	__team_option_inst_mark_removed_port(team, port);
1318 	__team_options_change_check(team);
1319 	__team_option_inst_del_port(team, port);
1320 	__team_port_change_port_removed(port);
1321 
1322 	team_port_set_orig_dev_addr(port);
1323 	dev_set_mtu(port_dev, port->orig.mtu);
1324 	kfree_rcu(port, rcu);
1325 	netdev_info(dev, "Port device %s removed\n", portname);
1326 	__team_compute_features(team);
1327 
1328 	return 0;
1329 }
1330 
1331 
1332 /*****************
1333  * Net device ops
1334  *****************/
1335 
1336 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1337 {
1338 	ctx->data.str_val = team->mode->kind;
1339 	return 0;
1340 }
1341 
1342 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1343 {
1344 	return team_change_mode(team, ctx->data.str_val);
1345 }
1346 
1347 static int team_notify_peers_count_get(struct team *team,
1348 				       struct team_gsetter_ctx *ctx)
1349 {
1350 	ctx->data.u32_val = team->notify_peers.count;
1351 	return 0;
1352 }
1353 
1354 static int team_notify_peers_count_set(struct team *team,
1355 				       struct team_gsetter_ctx *ctx)
1356 {
1357 	team->notify_peers.count = ctx->data.u32_val;
1358 	return 0;
1359 }
1360 
1361 static int team_notify_peers_interval_get(struct team *team,
1362 					  struct team_gsetter_ctx *ctx)
1363 {
1364 	ctx->data.u32_val = team->notify_peers.interval;
1365 	return 0;
1366 }
1367 
1368 static int team_notify_peers_interval_set(struct team *team,
1369 					  struct team_gsetter_ctx *ctx)
1370 {
1371 	team->notify_peers.interval = ctx->data.u32_val;
1372 	return 0;
1373 }
1374 
1375 static int team_mcast_rejoin_count_get(struct team *team,
1376 				       struct team_gsetter_ctx *ctx)
1377 {
1378 	ctx->data.u32_val = team->mcast_rejoin.count;
1379 	return 0;
1380 }
1381 
1382 static int team_mcast_rejoin_count_set(struct team *team,
1383 				       struct team_gsetter_ctx *ctx)
1384 {
1385 	team->mcast_rejoin.count = ctx->data.u32_val;
1386 	return 0;
1387 }
1388 
1389 static int team_mcast_rejoin_interval_get(struct team *team,
1390 					  struct team_gsetter_ctx *ctx)
1391 {
1392 	ctx->data.u32_val = team->mcast_rejoin.interval;
1393 	return 0;
1394 }
1395 
1396 static int team_mcast_rejoin_interval_set(struct team *team,
1397 					  struct team_gsetter_ctx *ctx)
1398 {
1399 	team->mcast_rejoin.interval = ctx->data.u32_val;
1400 	return 0;
1401 }
1402 
1403 static int team_port_en_option_get(struct team *team,
1404 				   struct team_gsetter_ctx *ctx)
1405 {
1406 	struct team_port *port = ctx->info->port;
1407 
1408 	ctx->data.bool_val = team_port_enabled(port);
1409 	return 0;
1410 }
1411 
1412 static int team_port_en_option_set(struct team *team,
1413 				   struct team_gsetter_ctx *ctx)
1414 {
1415 	struct team_port *port = ctx->info->port;
1416 
1417 	if (ctx->data.bool_val)
1418 		team_port_enable(team, port);
1419 	else
1420 		team_port_disable(team, port);
1421 	return 0;
1422 }
1423 
1424 static int team_user_linkup_option_get(struct team *team,
1425 				       struct team_gsetter_ctx *ctx)
1426 {
1427 	struct team_port *port = ctx->info->port;
1428 
1429 	ctx->data.bool_val = port->user.linkup;
1430 	return 0;
1431 }
1432 
1433 static void __team_carrier_check(struct team *team);
1434 
1435 static int team_user_linkup_option_set(struct team *team,
1436 				       struct team_gsetter_ctx *ctx)
1437 {
1438 	struct team_port *port = ctx->info->port;
1439 
1440 	port->user.linkup = ctx->data.bool_val;
1441 	team_refresh_port_linkup(port);
1442 	__team_carrier_check(port->team);
1443 	return 0;
1444 }
1445 
1446 static int team_user_linkup_en_option_get(struct team *team,
1447 					  struct team_gsetter_ctx *ctx)
1448 {
1449 	struct team_port *port = ctx->info->port;
1450 
1451 	ctx->data.bool_val = port->user.linkup_enabled;
1452 	return 0;
1453 }
1454 
1455 static int team_user_linkup_en_option_set(struct team *team,
1456 					  struct team_gsetter_ctx *ctx)
1457 {
1458 	struct team_port *port = ctx->info->port;
1459 
1460 	port->user.linkup_enabled = ctx->data.bool_val;
1461 	team_refresh_port_linkup(port);
1462 	__team_carrier_check(port->team);
1463 	return 0;
1464 }
1465 
1466 static int team_priority_option_get(struct team *team,
1467 				    struct team_gsetter_ctx *ctx)
1468 {
1469 	struct team_port *port = ctx->info->port;
1470 
1471 	ctx->data.s32_val = port->priority;
1472 	return 0;
1473 }
1474 
1475 static int team_priority_option_set(struct team *team,
1476 				    struct team_gsetter_ctx *ctx)
1477 {
1478 	struct team_port *port = ctx->info->port;
1479 	s32 priority = ctx->data.s32_val;
1480 
1481 	if (port->priority == priority)
1482 		return 0;
1483 	port->priority = priority;
1484 	team_queue_override_port_prio_changed(team, port);
1485 	return 0;
1486 }
1487 
1488 static int team_queue_id_option_get(struct team *team,
1489 				    struct team_gsetter_ctx *ctx)
1490 {
1491 	struct team_port *port = ctx->info->port;
1492 
1493 	ctx->data.u32_val = port->queue_id;
1494 	return 0;
1495 }
1496 
1497 static int team_queue_id_option_set(struct team *team,
1498 				    struct team_gsetter_ctx *ctx)
1499 {
1500 	struct team_port *port = ctx->info->port;
1501 	u16 new_queue_id = ctx->data.u32_val;
1502 
1503 	if (port->queue_id == new_queue_id)
1504 		return 0;
1505 	if (new_queue_id >= team->dev->real_num_tx_queues)
1506 		return -EINVAL;
1507 	team_queue_override_port_change_queue_id(team, port, new_queue_id);
1508 	return 0;
1509 }
1510 
1511 static const struct team_option team_options[] = {
1512 	{
1513 		.name = "mode",
1514 		.type = TEAM_OPTION_TYPE_STRING,
1515 		.getter = team_mode_option_get,
1516 		.setter = team_mode_option_set,
1517 	},
1518 	{
1519 		.name = "notify_peers_count",
1520 		.type = TEAM_OPTION_TYPE_U32,
1521 		.getter = team_notify_peers_count_get,
1522 		.setter = team_notify_peers_count_set,
1523 	},
1524 	{
1525 		.name = "notify_peers_interval",
1526 		.type = TEAM_OPTION_TYPE_U32,
1527 		.getter = team_notify_peers_interval_get,
1528 		.setter = team_notify_peers_interval_set,
1529 	},
1530 	{
1531 		.name = "mcast_rejoin_count",
1532 		.type = TEAM_OPTION_TYPE_U32,
1533 		.getter = team_mcast_rejoin_count_get,
1534 		.setter = team_mcast_rejoin_count_set,
1535 	},
1536 	{
1537 		.name = "mcast_rejoin_interval",
1538 		.type = TEAM_OPTION_TYPE_U32,
1539 		.getter = team_mcast_rejoin_interval_get,
1540 		.setter = team_mcast_rejoin_interval_set,
1541 	},
1542 	{
1543 		.name = "enabled",
1544 		.type = TEAM_OPTION_TYPE_BOOL,
1545 		.per_port = true,
1546 		.getter = team_port_en_option_get,
1547 		.setter = team_port_en_option_set,
1548 	},
1549 	{
1550 		.name = "user_linkup",
1551 		.type = TEAM_OPTION_TYPE_BOOL,
1552 		.per_port = true,
1553 		.getter = team_user_linkup_option_get,
1554 		.setter = team_user_linkup_option_set,
1555 	},
1556 	{
1557 		.name = "user_linkup_enabled",
1558 		.type = TEAM_OPTION_TYPE_BOOL,
1559 		.per_port = true,
1560 		.getter = team_user_linkup_en_option_get,
1561 		.setter = team_user_linkup_en_option_set,
1562 	},
1563 	{
1564 		.name = "priority",
1565 		.type = TEAM_OPTION_TYPE_S32,
1566 		.per_port = true,
1567 		.getter = team_priority_option_get,
1568 		.setter = team_priority_option_set,
1569 	},
1570 	{
1571 		.name = "queue_id",
1572 		.type = TEAM_OPTION_TYPE_U32,
1573 		.per_port = true,
1574 		.getter = team_queue_id_option_get,
1575 		.setter = team_queue_id_option_set,
1576 	},
1577 };
1578 
1579 
1580 static int team_init(struct net_device *dev)
1581 {
1582 	struct team *team = netdev_priv(dev);
1583 	int i;
1584 	int err;
1585 
1586 	team->dev = dev;
1587 	mutex_init(&team->lock);
1588 	team_set_no_mode(team);
1589 
1590 	team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1591 	if (!team->pcpu_stats)
1592 		return -ENOMEM;
1593 
1594 	for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1595 		INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1596 	INIT_LIST_HEAD(&team->port_list);
1597 	err = team_queue_override_init(team);
1598 	if (err)
1599 		goto err_team_queue_override_init;
1600 
1601 	team_adjust_ops(team);
1602 
1603 	INIT_LIST_HEAD(&team->option_list);
1604 	INIT_LIST_HEAD(&team->option_inst_list);
1605 
1606 	team_notify_peers_init(team);
1607 	team_mcast_rejoin_init(team);
1608 
1609 	err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1610 	if (err)
1611 		goto err_options_register;
1612 	netif_carrier_off(dev);
1613 
1614 	netdev_lockdep_set_classes(dev);
1615 
1616 	return 0;
1617 
1618 err_options_register:
1619 	team_mcast_rejoin_fini(team);
1620 	team_notify_peers_fini(team);
1621 	team_queue_override_fini(team);
1622 err_team_queue_override_init:
1623 	free_percpu(team->pcpu_stats);
1624 
1625 	return err;
1626 }
1627 
1628 static void team_uninit(struct net_device *dev)
1629 {
1630 	struct team *team = netdev_priv(dev);
1631 	struct team_port *port;
1632 	struct team_port *tmp;
1633 
1634 	mutex_lock(&team->lock);
1635 	list_for_each_entry_safe(port, tmp, &team->port_list, list)
1636 		team_port_del(team, port->dev);
1637 
1638 	__team_change_mode(team, NULL); /* cleanup */
1639 	__team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1640 	team_mcast_rejoin_fini(team);
1641 	team_notify_peers_fini(team);
1642 	team_queue_override_fini(team);
1643 	mutex_unlock(&team->lock);
1644 	netdev_change_features(dev);
1645 }
1646 
1647 static void team_destructor(struct net_device *dev)
1648 {
1649 	struct team *team = netdev_priv(dev);
1650 
1651 	free_percpu(team->pcpu_stats);
1652 }
1653 
1654 static int team_open(struct net_device *dev)
1655 {
1656 	return 0;
1657 }
1658 
1659 static int team_close(struct net_device *dev)
1660 {
1661 	return 0;
1662 }
1663 
1664 /*
1665  * note: already called with rcu_read_lock
1666  */
1667 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1668 {
1669 	struct team *team = netdev_priv(dev);
1670 	bool tx_success;
1671 	unsigned int len = skb->len;
1672 
1673 	tx_success = team_queue_override_transmit(team, skb);
1674 	if (!tx_success)
1675 		tx_success = team->ops.transmit(team, skb);
1676 	if (tx_success) {
1677 		struct team_pcpu_stats *pcpu_stats;
1678 
1679 		pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1680 		u64_stats_update_begin(&pcpu_stats->syncp);
1681 		pcpu_stats->tx_packets++;
1682 		pcpu_stats->tx_bytes += len;
1683 		u64_stats_update_end(&pcpu_stats->syncp);
1684 	} else {
1685 		this_cpu_inc(team->pcpu_stats->tx_dropped);
1686 	}
1687 
1688 	return NETDEV_TX_OK;
1689 }
1690 
1691 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1692 			     struct net_device *sb_dev)
1693 {
1694 	/*
1695 	 * This helper function exists to help dev_pick_tx get the correct
1696 	 * destination queue.  Using a helper function skips a call to
1697 	 * skb_tx_hash and will put the skbs in the queue we expect on their
1698 	 * way down to the team driver.
1699 	 */
1700 	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1701 
1702 	/*
1703 	 * Save the original txq to restore before passing to the driver
1704 	 */
1705 	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1706 
1707 	if (unlikely(txq >= dev->real_num_tx_queues)) {
1708 		do {
1709 			txq -= dev->real_num_tx_queues;
1710 		} while (txq >= dev->real_num_tx_queues);
1711 	}
1712 	return txq;
1713 }
1714 
1715 static void team_change_rx_flags(struct net_device *dev, int change)
1716 {
1717 	struct team *team = netdev_priv(dev);
1718 	struct team_port *port;
1719 	int inc;
1720 
1721 	rcu_read_lock();
1722 	list_for_each_entry_rcu(port, &team->port_list, list) {
1723 		if (change & IFF_PROMISC) {
1724 			inc = dev->flags & IFF_PROMISC ? 1 : -1;
1725 			dev_set_promiscuity(port->dev, inc);
1726 		}
1727 		if (change & IFF_ALLMULTI) {
1728 			inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1729 			dev_set_allmulti(port->dev, inc);
1730 		}
1731 	}
1732 	rcu_read_unlock();
1733 }
1734 
1735 static void team_set_rx_mode(struct net_device *dev)
1736 {
1737 	struct team *team = netdev_priv(dev);
1738 	struct team_port *port;
1739 
1740 	rcu_read_lock();
1741 	list_for_each_entry_rcu(port, &team->port_list, list) {
1742 		dev_uc_sync_multiple(port->dev, dev);
1743 		dev_mc_sync_multiple(port->dev, dev);
1744 	}
1745 	rcu_read_unlock();
1746 }
1747 
1748 static int team_set_mac_address(struct net_device *dev, void *p)
1749 {
1750 	struct sockaddr *addr = p;
1751 	struct team *team = netdev_priv(dev);
1752 	struct team_port *port;
1753 
1754 	if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1755 		return -EADDRNOTAVAIL;
1756 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1757 	mutex_lock(&team->lock);
1758 	list_for_each_entry(port, &team->port_list, list)
1759 		if (team->ops.port_change_dev_addr)
1760 			team->ops.port_change_dev_addr(team, port);
1761 	mutex_unlock(&team->lock);
1762 	return 0;
1763 }
1764 
1765 static int team_change_mtu(struct net_device *dev, int new_mtu)
1766 {
1767 	struct team *team = netdev_priv(dev);
1768 	struct team_port *port;
1769 	int err;
1770 
1771 	/*
1772 	 * Alhough this is reader, it's guarded by team lock. It's not possible
1773 	 * to traverse list in reverse under rcu_read_lock
1774 	 */
1775 	mutex_lock(&team->lock);
1776 	team->port_mtu_change_allowed = true;
1777 	list_for_each_entry(port, &team->port_list, list) {
1778 		err = dev_set_mtu(port->dev, new_mtu);
1779 		if (err) {
1780 			netdev_err(dev, "Device %s failed to change mtu",
1781 				   port->dev->name);
1782 			goto unwind;
1783 		}
1784 	}
1785 	team->port_mtu_change_allowed = false;
1786 	mutex_unlock(&team->lock);
1787 
1788 	dev->mtu = new_mtu;
1789 
1790 	return 0;
1791 
1792 unwind:
1793 	list_for_each_entry_continue_reverse(port, &team->port_list, list)
1794 		dev_set_mtu(port->dev, dev->mtu);
1795 	team->port_mtu_change_allowed = false;
1796 	mutex_unlock(&team->lock);
1797 
1798 	return err;
1799 }
1800 
1801 static void
1802 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1803 {
1804 	struct team *team = netdev_priv(dev);
1805 	struct team_pcpu_stats *p;
1806 	u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1807 	u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
1808 	unsigned int start;
1809 	int i;
1810 
1811 	for_each_possible_cpu(i) {
1812 		p = per_cpu_ptr(team->pcpu_stats, i);
1813 		do {
1814 			start = u64_stats_fetch_begin_irq(&p->syncp);
1815 			rx_packets	= p->rx_packets;
1816 			rx_bytes	= p->rx_bytes;
1817 			rx_multicast	= p->rx_multicast;
1818 			tx_packets	= p->tx_packets;
1819 			tx_bytes	= p->tx_bytes;
1820 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
1821 
1822 		stats->rx_packets	+= rx_packets;
1823 		stats->rx_bytes		+= rx_bytes;
1824 		stats->multicast	+= rx_multicast;
1825 		stats->tx_packets	+= tx_packets;
1826 		stats->tx_bytes		+= tx_bytes;
1827 		/*
1828 		 * rx_dropped, tx_dropped & rx_nohandler are u32,
1829 		 * updated without syncp protection.
1830 		 */
1831 		rx_dropped	+= p->rx_dropped;
1832 		tx_dropped	+= p->tx_dropped;
1833 		rx_nohandler	+= p->rx_nohandler;
1834 	}
1835 	stats->rx_dropped	= rx_dropped;
1836 	stats->tx_dropped	= tx_dropped;
1837 	stats->rx_nohandler	= rx_nohandler;
1838 }
1839 
1840 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1841 {
1842 	struct team *team = netdev_priv(dev);
1843 	struct team_port *port;
1844 	int err;
1845 
1846 	/*
1847 	 * Alhough this is reader, it's guarded by team lock. It's not possible
1848 	 * to traverse list in reverse under rcu_read_lock
1849 	 */
1850 	mutex_lock(&team->lock);
1851 	list_for_each_entry(port, &team->port_list, list) {
1852 		err = vlan_vid_add(port->dev, proto, vid);
1853 		if (err)
1854 			goto unwind;
1855 	}
1856 	mutex_unlock(&team->lock);
1857 
1858 	return 0;
1859 
1860 unwind:
1861 	list_for_each_entry_continue_reverse(port, &team->port_list, list)
1862 		vlan_vid_del(port->dev, proto, vid);
1863 	mutex_unlock(&team->lock);
1864 
1865 	return err;
1866 }
1867 
1868 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1869 {
1870 	struct team *team = netdev_priv(dev);
1871 	struct team_port *port;
1872 
1873 	mutex_lock(&team->lock);
1874 	list_for_each_entry(port, &team->port_list, list)
1875 		vlan_vid_del(port->dev, proto, vid);
1876 	mutex_unlock(&team->lock);
1877 
1878 	return 0;
1879 }
1880 
1881 #ifdef CONFIG_NET_POLL_CONTROLLER
1882 static void team_poll_controller(struct net_device *dev)
1883 {
1884 }
1885 
1886 static void __team_netpoll_cleanup(struct team *team)
1887 {
1888 	struct team_port *port;
1889 
1890 	list_for_each_entry(port, &team->port_list, list)
1891 		team_port_disable_netpoll(port);
1892 }
1893 
1894 static void team_netpoll_cleanup(struct net_device *dev)
1895 {
1896 	struct team *team = netdev_priv(dev);
1897 
1898 	mutex_lock(&team->lock);
1899 	__team_netpoll_cleanup(team);
1900 	mutex_unlock(&team->lock);
1901 }
1902 
1903 static int team_netpoll_setup(struct net_device *dev,
1904 			      struct netpoll_info *npifo)
1905 {
1906 	struct team *team = netdev_priv(dev);
1907 	struct team_port *port;
1908 	int err = 0;
1909 
1910 	mutex_lock(&team->lock);
1911 	list_for_each_entry(port, &team->port_list, list) {
1912 		err = __team_port_enable_netpoll(port);
1913 		if (err) {
1914 			__team_netpoll_cleanup(team);
1915 			break;
1916 		}
1917 	}
1918 	mutex_unlock(&team->lock);
1919 	return err;
1920 }
1921 #endif
1922 
1923 static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
1924 			  struct netlink_ext_ack *extack)
1925 {
1926 	struct team *team = netdev_priv(dev);
1927 	int err;
1928 
1929 	mutex_lock(&team->lock);
1930 	err = team_port_add(team, port_dev, extack);
1931 	mutex_unlock(&team->lock);
1932 
1933 	if (!err)
1934 		netdev_change_features(dev);
1935 
1936 	return err;
1937 }
1938 
1939 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1940 {
1941 	struct team *team = netdev_priv(dev);
1942 	int err;
1943 
1944 	mutex_lock(&team->lock);
1945 	err = team_port_del(team, port_dev);
1946 	mutex_unlock(&team->lock);
1947 
1948 	if (!err)
1949 		netdev_change_features(dev);
1950 
1951 	return err;
1952 }
1953 
1954 static netdev_features_t team_fix_features(struct net_device *dev,
1955 					   netdev_features_t features)
1956 {
1957 	struct team_port *port;
1958 	struct team *team = netdev_priv(dev);
1959 	netdev_features_t mask;
1960 
1961 	mask = features;
1962 	features &= ~NETIF_F_ONE_FOR_ALL;
1963 	features |= NETIF_F_ALL_FOR_ALL;
1964 
1965 	rcu_read_lock();
1966 	list_for_each_entry_rcu(port, &team->port_list, list) {
1967 		features = netdev_increment_features(features,
1968 						     port->dev->features,
1969 						     mask);
1970 	}
1971 	rcu_read_unlock();
1972 
1973 	features = netdev_add_tso_features(features, mask);
1974 
1975 	return features;
1976 }
1977 
1978 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1979 {
1980 	struct team *team = netdev_priv(dev);
1981 
1982 	team->user_carrier_enabled = true;
1983 
1984 	if (new_carrier)
1985 		netif_carrier_on(dev);
1986 	else
1987 		netif_carrier_off(dev);
1988 	return 0;
1989 }
1990 
1991 static const struct net_device_ops team_netdev_ops = {
1992 	.ndo_init		= team_init,
1993 	.ndo_uninit		= team_uninit,
1994 	.ndo_open		= team_open,
1995 	.ndo_stop		= team_close,
1996 	.ndo_start_xmit		= team_xmit,
1997 	.ndo_select_queue	= team_select_queue,
1998 	.ndo_change_rx_flags	= team_change_rx_flags,
1999 	.ndo_set_rx_mode	= team_set_rx_mode,
2000 	.ndo_set_mac_address	= team_set_mac_address,
2001 	.ndo_change_mtu		= team_change_mtu,
2002 	.ndo_get_stats64	= team_get_stats64,
2003 	.ndo_vlan_rx_add_vid	= team_vlan_rx_add_vid,
2004 	.ndo_vlan_rx_kill_vid	= team_vlan_rx_kill_vid,
2005 #ifdef CONFIG_NET_POLL_CONTROLLER
2006 	.ndo_poll_controller	= team_poll_controller,
2007 	.ndo_netpoll_setup	= team_netpoll_setup,
2008 	.ndo_netpoll_cleanup	= team_netpoll_cleanup,
2009 #endif
2010 	.ndo_add_slave		= team_add_slave,
2011 	.ndo_del_slave		= team_del_slave,
2012 	.ndo_fix_features	= team_fix_features,
2013 	.ndo_change_carrier     = team_change_carrier,
2014 	.ndo_features_check	= passthru_features_check,
2015 };
2016 
2017 /***********************
2018  * ethtool interface
2019  ***********************/
2020 
2021 static void team_ethtool_get_drvinfo(struct net_device *dev,
2022 				     struct ethtool_drvinfo *drvinfo)
2023 {
2024 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2025 	strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2026 }
2027 
2028 static const struct ethtool_ops team_ethtool_ops = {
2029 	.get_drvinfo		= team_ethtool_get_drvinfo,
2030 	.get_link		= ethtool_op_get_link,
2031 };
2032 
2033 /***********************
2034  * rt netlink interface
2035  ***********************/
2036 
2037 static void team_setup_by_port(struct net_device *dev,
2038 			       struct net_device *port_dev)
2039 {
2040 	dev->header_ops	= port_dev->header_ops;
2041 	dev->type = port_dev->type;
2042 	dev->hard_header_len = port_dev->hard_header_len;
2043 	dev->addr_len = port_dev->addr_len;
2044 	dev->mtu = port_dev->mtu;
2045 	memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2046 	eth_hw_addr_inherit(dev, port_dev);
2047 }
2048 
2049 static int team_dev_type_check_change(struct net_device *dev,
2050 				      struct net_device *port_dev)
2051 {
2052 	struct team *team = netdev_priv(dev);
2053 	char *portname = port_dev->name;
2054 	int err;
2055 
2056 	if (dev->type == port_dev->type)
2057 		return 0;
2058 	if (!list_empty(&team->port_list)) {
2059 		netdev_err(dev, "Device %s is of different type\n", portname);
2060 		return -EBUSY;
2061 	}
2062 	err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2063 	err = notifier_to_errno(err);
2064 	if (err) {
2065 		netdev_err(dev, "Refused to change device type\n");
2066 		return err;
2067 	}
2068 	dev_uc_flush(dev);
2069 	dev_mc_flush(dev);
2070 	team_setup_by_port(dev, port_dev);
2071 	call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2072 	return 0;
2073 }
2074 
2075 static void team_setup(struct net_device *dev)
2076 {
2077 	ether_setup(dev);
2078 	dev->max_mtu = ETH_MAX_MTU;
2079 
2080 	dev->netdev_ops = &team_netdev_ops;
2081 	dev->ethtool_ops = &team_ethtool_ops;
2082 	dev->needs_free_netdev = true;
2083 	dev->priv_destructor = team_destructor;
2084 	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2085 	dev->priv_flags |= IFF_NO_QUEUE;
2086 	dev->priv_flags |= IFF_TEAM;
2087 
2088 	/*
2089 	 * Indicate we support unicast address filtering. That way core won't
2090 	 * bring us to promisc mode in case a unicast addr is added.
2091 	 * Let this up to underlay drivers.
2092 	 */
2093 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2094 
2095 	dev->features |= NETIF_F_LLTX;
2096 	dev->features |= NETIF_F_GRO;
2097 
2098 	/* Don't allow team devices to change network namespaces. */
2099 	dev->features |= NETIF_F_NETNS_LOCAL;
2100 
2101 	dev->hw_features = TEAM_VLAN_FEATURES |
2102 			   NETIF_F_HW_VLAN_CTAG_TX |
2103 			   NETIF_F_HW_VLAN_CTAG_RX |
2104 			   NETIF_F_HW_VLAN_CTAG_FILTER;
2105 
2106 	dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
2107 	dev->features |= dev->hw_features;
2108 }
2109 
2110 static int team_newlink(struct net *src_net, struct net_device *dev,
2111 			struct nlattr *tb[], struct nlattr *data[],
2112 			struct netlink_ext_ack *extack)
2113 {
2114 	if (tb[IFLA_ADDRESS] == NULL)
2115 		eth_hw_addr_random(dev);
2116 
2117 	return register_netdevice(dev);
2118 }
2119 
2120 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2121 			 struct netlink_ext_ack *extack)
2122 {
2123 	if (tb[IFLA_ADDRESS]) {
2124 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2125 			return -EINVAL;
2126 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2127 			return -EADDRNOTAVAIL;
2128 	}
2129 	return 0;
2130 }
2131 
2132 static unsigned int team_get_num_tx_queues(void)
2133 {
2134 	return TEAM_DEFAULT_NUM_TX_QUEUES;
2135 }
2136 
2137 static unsigned int team_get_num_rx_queues(void)
2138 {
2139 	return TEAM_DEFAULT_NUM_RX_QUEUES;
2140 }
2141 
2142 static struct rtnl_link_ops team_link_ops __read_mostly = {
2143 	.kind			= DRV_NAME,
2144 	.priv_size		= sizeof(struct team),
2145 	.setup			= team_setup,
2146 	.newlink		= team_newlink,
2147 	.validate		= team_validate,
2148 	.get_num_tx_queues	= team_get_num_tx_queues,
2149 	.get_num_rx_queues	= team_get_num_rx_queues,
2150 };
2151 
2152 
2153 /***********************************
2154  * Generic netlink custom interface
2155  ***********************************/
2156 
2157 static struct genl_family team_nl_family;
2158 
2159 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2160 	[TEAM_ATTR_UNSPEC]			= { .type = NLA_UNSPEC, },
2161 	[TEAM_ATTR_TEAM_IFINDEX]		= { .type = NLA_U32 },
2162 	[TEAM_ATTR_LIST_OPTION]			= { .type = NLA_NESTED },
2163 	[TEAM_ATTR_LIST_PORT]			= { .type = NLA_NESTED },
2164 };
2165 
2166 static const struct nla_policy
2167 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2168 	[TEAM_ATTR_OPTION_UNSPEC]		= { .type = NLA_UNSPEC, },
2169 	[TEAM_ATTR_OPTION_NAME] = {
2170 		.type = NLA_STRING,
2171 		.len = TEAM_STRING_MAX_LEN,
2172 	},
2173 	[TEAM_ATTR_OPTION_CHANGED]		= { .type = NLA_FLAG },
2174 	[TEAM_ATTR_OPTION_TYPE]			= { .type = NLA_U8 },
2175 	[TEAM_ATTR_OPTION_DATA]			= { .type = NLA_BINARY },
2176 };
2177 
2178 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2179 {
2180 	struct sk_buff *msg;
2181 	void *hdr;
2182 	int err;
2183 
2184 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2185 	if (!msg)
2186 		return -ENOMEM;
2187 
2188 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2189 			  &team_nl_family, 0, TEAM_CMD_NOOP);
2190 	if (!hdr) {
2191 		err = -EMSGSIZE;
2192 		goto err_msg_put;
2193 	}
2194 
2195 	genlmsg_end(msg, hdr);
2196 
2197 	return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2198 
2199 err_msg_put:
2200 	nlmsg_free(msg);
2201 
2202 	return err;
2203 }
2204 
2205 /*
2206  * Netlink cmd functions should be locked by following two functions.
2207  * Since dev gets held here, that ensures dev won't disappear in between.
2208  */
2209 static struct team *team_nl_team_get(struct genl_info *info)
2210 {
2211 	struct net *net = genl_info_net(info);
2212 	int ifindex;
2213 	struct net_device *dev;
2214 	struct team *team;
2215 
2216 	if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2217 		return NULL;
2218 
2219 	ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2220 	dev = dev_get_by_index(net, ifindex);
2221 	if (!dev || dev->netdev_ops != &team_netdev_ops) {
2222 		if (dev)
2223 			dev_put(dev);
2224 		return NULL;
2225 	}
2226 
2227 	team = netdev_priv(dev);
2228 	mutex_lock(&team->lock);
2229 	return team;
2230 }
2231 
2232 static void team_nl_team_put(struct team *team)
2233 {
2234 	mutex_unlock(&team->lock);
2235 	dev_put(team->dev);
2236 }
2237 
2238 typedef int team_nl_send_func_t(struct sk_buff *skb,
2239 				struct team *team, u32 portid);
2240 
2241 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2242 {
2243 	return genlmsg_unicast(dev_net(team->dev), skb, portid);
2244 }
2245 
2246 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2247 				       struct team_option_inst *opt_inst)
2248 {
2249 	struct nlattr *option_item;
2250 	struct team_option *option = opt_inst->option;
2251 	struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2252 	struct team_gsetter_ctx ctx;
2253 	int err;
2254 
2255 	ctx.info = opt_inst_info;
2256 	err = team_option_get(team, opt_inst, &ctx);
2257 	if (err)
2258 		return err;
2259 
2260 	option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2261 	if (!option_item)
2262 		return -EMSGSIZE;
2263 
2264 	if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2265 		goto nest_cancel;
2266 	if (opt_inst_info->port &&
2267 	    nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2268 			opt_inst_info->port->dev->ifindex))
2269 		goto nest_cancel;
2270 	if (opt_inst->option->array_size &&
2271 	    nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2272 			opt_inst_info->array_index))
2273 		goto nest_cancel;
2274 
2275 	switch (option->type) {
2276 	case TEAM_OPTION_TYPE_U32:
2277 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2278 			goto nest_cancel;
2279 		if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2280 			goto nest_cancel;
2281 		break;
2282 	case TEAM_OPTION_TYPE_STRING:
2283 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2284 			goto nest_cancel;
2285 		if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2286 				   ctx.data.str_val))
2287 			goto nest_cancel;
2288 		break;
2289 	case TEAM_OPTION_TYPE_BINARY:
2290 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2291 			goto nest_cancel;
2292 		if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2293 			    ctx.data.bin_val.ptr))
2294 			goto nest_cancel;
2295 		break;
2296 	case TEAM_OPTION_TYPE_BOOL:
2297 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2298 			goto nest_cancel;
2299 		if (ctx.data.bool_val &&
2300 		    nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2301 			goto nest_cancel;
2302 		break;
2303 	case TEAM_OPTION_TYPE_S32:
2304 		if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2305 			goto nest_cancel;
2306 		if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2307 			goto nest_cancel;
2308 		break;
2309 	default:
2310 		BUG();
2311 	}
2312 	if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2313 		goto nest_cancel;
2314 	if (opt_inst->changed) {
2315 		if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2316 			goto nest_cancel;
2317 		opt_inst->changed = false;
2318 	}
2319 	nla_nest_end(skb, option_item);
2320 	return 0;
2321 
2322 nest_cancel:
2323 	nla_nest_cancel(skb, option_item);
2324 	return -EMSGSIZE;
2325 }
2326 
2327 static int __send_and_alloc_skb(struct sk_buff **pskb,
2328 				struct team *team, u32 portid,
2329 				team_nl_send_func_t *send_func)
2330 {
2331 	int err;
2332 
2333 	if (*pskb) {
2334 		err = send_func(*pskb, team, portid);
2335 		if (err)
2336 			return err;
2337 	}
2338 	*pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2339 	if (!*pskb)
2340 		return -ENOMEM;
2341 	return 0;
2342 }
2343 
2344 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2345 				    int flags, team_nl_send_func_t *send_func,
2346 				    struct list_head *sel_opt_inst_list)
2347 {
2348 	struct nlattr *option_list;
2349 	struct nlmsghdr *nlh;
2350 	void *hdr;
2351 	struct team_option_inst *opt_inst;
2352 	int err;
2353 	struct sk_buff *skb = NULL;
2354 	bool incomplete;
2355 	int i;
2356 
2357 	opt_inst = list_first_entry(sel_opt_inst_list,
2358 				    struct team_option_inst, tmp_list);
2359 
2360 start_again:
2361 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2362 	if (err)
2363 		return err;
2364 
2365 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2366 			  TEAM_CMD_OPTIONS_GET);
2367 	if (!hdr) {
2368 		nlmsg_free(skb);
2369 		return -EMSGSIZE;
2370 	}
2371 
2372 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2373 		goto nla_put_failure;
2374 	option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2375 	if (!option_list)
2376 		goto nla_put_failure;
2377 
2378 	i = 0;
2379 	incomplete = false;
2380 	list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2381 		err = team_nl_fill_one_option_get(skb, team, opt_inst);
2382 		if (err) {
2383 			if (err == -EMSGSIZE) {
2384 				if (!i)
2385 					goto errout;
2386 				incomplete = true;
2387 				break;
2388 			}
2389 			goto errout;
2390 		}
2391 		i++;
2392 	}
2393 
2394 	nla_nest_end(skb, option_list);
2395 	genlmsg_end(skb, hdr);
2396 	if (incomplete)
2397 		goto start_again;
2398 
2399 send_done:
2400 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2401 	if (!nlh) {
2402 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2403 		if (err)
2404 			return err;
2405 		goto send_done;
2406 	}
2407 
2408 	return send_func(skb, team, portid);
2409 
2410 nla_put_failure:
2411 	err = -EMSGSIZE;
2412 errout:
2413 	nlmsg_free(skb);
2414 	return err;
2415 }
2416 
2417 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2418 {
2419 	struct team *team;
2420 	struct team_option_inst *opt_inst;
2421 	int err;
2422 	LIST_HEAD(sel_opt_inst_list);
2423 
2424 	team = team_nl_team_get(info);
2425 	if (!team)
2426 		return -EINVAL;
2427 
2428 	list_for_each_entry(opt_inst, &team->option_inst_list, list)
2429 		list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2430 	err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2431 				       NLM_F_ACK, team_nl_send_unicast,
2432 				       &sel_opt_inst_list);
2433 
2434 	team_nl_team_put(team);
2435 
2436 	return err;
2437 }
2438 
2439 static int team_nl_send_event_options_get(struct team *team,
2440 					  struct list_head *sel_opt_inst_list);
2441 
2442 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2443 {
2444 	struct team *team;
2445 	int err = 0;
2446 	int i;
2447 	struct nlattr *nl_option;
2448 
2449 	rtnl_lock();
2450 
2451 	team = team_nl_team_get(info);
2452 	if (!team) {
2453 		err = -EINVAL;
2454 		goto rtnl_unlock;
2455 	}
2456 
2457 	err = -EINVAL;
2458 	if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2459 		err = -EINVAL;
2460 		goto team_put;
2461 	}
2462 
2463 	nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2464 		struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2465 		struct nlattr *attr;
2466 		struct nlattr *attr_data;
2467 		LIST_HEAD(opt_inst_list);
2468 		enum team_option_type opt_type;
2469 		int opt_port_ifindex = 0; /* != 0 for per-port options */
2470 		u32 opt_array_index = 0;
2471 		bool opt_is_array = false;
2472 		struct team_option_inst *opt_inst;
2473 		char *opt_name;
2474 		bool opt_found = false;
2475 
2476 		if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2477 			err = -EINVAL;
2478 			goto team_put;
2479 		}
2480 		err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2481 				       nl_option, team_nl_option_policy,
2482 				       info->extack);
2483 		if (err)
2484 			goto team_put;
2485 		if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2486 		    !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2487 			err = -EINVAL;
2488 			goto team_put;
2489 		}
2490 		switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2491 		case NLA_U32:
2492 			opt_type = TEAM_OPTION_TYPE_U32;
2493 			break;
2494 		case NLA_STRING:
2495 			opt_type = TEAM_OPTION_TYPE_STRING;
2496 			break;
2497 		case NLA_BINARY:
2498 			opt_type = TEAM_OPTION_TYPE_BINARY;
2499 			break;
2500 		case NLA_FLAG:
2501 			opt_type = TEAM_OPTION_TYPE_BOOL;
2502 			break;
2503 		case NLA_S32:
2504 			opt_type = TEAM_OPTION_TYPE_S32;
2505 			break;
2506 		default:
2507 			goto team_put;
2508 		}
2509 
2510 		attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2511 		if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2512 			err = -EINVAL;
2513 			goto team_put;
2514 		}
2515 
2516 		opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2517 		attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2518 		if (attr)
2519 			opt_port_ifindex = nla_get_u32(attr);
2520 
2521 		attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2522 		if (attr) {
2523 			opt_is_array = true;
2524 			opt_array_index = nla_get_u32(attr);
2525 		}
2526 
2527 		list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2528 			struct team_option *option = opt_inst->option;
2529 			struct team_gsetter_ctx ctx;
2530 			struct team_option_inst_info *opt_inst_info;
2531 			int tmp_ifindex;
2532 
2533 			opt_inst_info = &opt_inst->info;
2534 			tmp_ifindex = opt_inst_info->port ?
2535 				      opt_inst_info->port->dev->ifindex : 0;
2536 			if (option->type != opt_type ||
2537 			    strcmp(option->name, opt_name) ||
2538 			    tmp_ifindex != opt_port_ifindex ||
2539 			    (option->array_size && !opt_is_array) ||
2540 			    opt_inst_info->array_index != opt_array_index)
2541 				continue;
2542 			opt_found = true;
2543 			ctx.info = opt_inst_info;
2544 			switch (opt_type) {
2545 			case TEAM_OPTION_TYPE_U32:
2546 				ctx.data.u32_val = nla_get_u32(attr_data);
2547 				break;
2548 			case TEAM_OPTION_TYPE_STRING:
2549 				if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2550 					err = -EINVAL;
2551 					goto team_put;
2552 				}
2553 				ctx.data.str_val = nla_data(attr_data);
2554 				break;
2555 			case TEAM_OPTION_TYPE_BINARY:
2556 				ctx.data.bin_val.len = nla_len(attr_data);
2557 				ctx.data.bin_val.ptr = nla_data(attr_data);
2558 				break;
2559 			case TEAM_OPTION_TYPE_BOOL:
2560 				ctx.data.bool_val = attr_data ? true : false;
2561 				break;
2562 			case TEAM_OPTION_TYPE_S32:
2563 				ctx.data.s32_val = nla_get_s32(attr_data);
2564 				break;
2565 			default:
2566 				BUG();
2567 			}
2568 			err = team_option_set(team, opt_inst, &ctx);
2569 			if (err)
2570 				goto team_put;
2571 			opt_inst->changed = true;
2572 			list_add(&opt_inst->tmp_list, &opt_inst_list);
2573 		}
2574 		if (!opt_found) {
2575 			err = -ENOENT;
2576 			goto team_put;
2577 		}
2578 
2579 		err = team_nl_send_event_options_get(team, &opt_inst_list);
2580 		if (err)
2581 			break;
2582 	}
2583 
2584 team_put:
2585 	team_nl_team_put(team);
2586 rtnl_unlock:
2587 	rtnl_unlock();
2588 	return err;
2589 }
2590 
2591 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2592 				     struct team_port *port)
2593 {
2594 	struct nlattr *port_item;
2595 
2596 	port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2597 	if (!port_item)
2598 		goto nest_cancel;
2599 	if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2600 		goto nest_cancel;
2601 	if (port->changed) {
2602 		if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2603 			goto nest_cancel;
2604 		port->changed = false;
2605 	}
2606 	if ((port->removed &&
2607 	     nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2608 	    (port->state.linkup &&
2609 	     nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2610 	    nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2611 	    nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2612 		goto nest_cancel;
2613 	nla_nest_end(skb, port_item);
2614 	return 0;
2615 
2616 nest_cancel:
2617 	nla_nest_cancel(skb, port_item);
2618 	return -EMSGSIZE;
2619 }
2620 
2621 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2622 				      int flags, team_nl_send_func_t *send_func,
2623 				      struct team_port *one_port)
2624 {
2625 	struct nlattr *port_list;
2626 	struct nlmsghdr *nlh;
2627 	void *hdr;
2628 	struct team_port *port;
2629 	int err;
2630 	struct sk_buff *skb = NULL;
2631 	bool incomplete;
2632 	int i;
2633 
2634 	port = list_first_entry_or_null(&team->port_list,
2635 					struct team_port, list);
2636 
2637 start_again:
2638 	err = __send_and_alloc_skb(&skb, team, portid, send_func);
2639 	if (err)
2640 		return err;
2641 
2642 	hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2643 			  TEAM_CMD_PORT_LIST_GET);
2644 	if (!hdr) {
2645 		nlmsg_free(skb);
2646 		return -EMSGSIZE;
2647 	}
2648 
2649 	if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2650 		goto nla_put_failure;
2651 	port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2652 	if (!port_list)
2653 		goto nla_put_failure;
2654 
2655 	i = 0;
2656 	incomplete = false;
2657 
2658 	/* If one port is selected, called wants to send port list containing
2659 	 * only this port. Otherwise go through all listed ports and send all
2660 	 */
2661 	if (one_port) {
2662 		err = team_nl_fill_one_port_get(skb, one_port);
2663 		if (err)
2664 			goto errout;
2665 	} else if (port) {
2666 		list_for_each_entry_from(port, &team->port_list, list) {
2667 			err = team_nl_fill_one_port_get(skb, port);
2668 			if (err) {
2669 				if (err == -EMSGSIZE) {
2670 					if (!i)
2671 						goto errout;
2672 					incomplete = true;
2673 					break;
2674 				}
2675 				goto errout;
2676 			}
2677 			i++;
2678 		}
2679 	}
2680 
2681 	nla_nest_end(skb, port_list);
2682 	genlmsg_end(skb, hdr);
2683 	if (incomplete)
2684 		goto start_again;
2685 
2686 send_done:
2687 	nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2688 	if (!nlh) {
2689 		err = __send_and_alloc_skb(&skb, team, portid, send_func);
2690 		if (err)
2691 			return err;
2692 		goto send_done;
2693 	}
2694 
2695 	return send_func(skb, team, portid);
2696 
2697 nla_put_failure:
2698 	err = -EMSGSIZE;
2699 errout:
2700 	nlmsg_free(skb);
2701 	return err;
2702 }
2703 
2704 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2705 				     struct genl_info *info)
2706 {
2707 	struct team *team;
2708 	int err;
2709 
2710 	team = team_nl_team_get(info);
2711 	if (!team)
2712 		return -EINVAL;
2713 
2714 	err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2715 					 NLM_F_ACK, team_nl_send_unicast, NULL);
2716 
2717 	team_nl_team_put(team);
2718 
2719 	return err;
2720 }
2721 
2722 static const struct genl_ops team_nl_ops[] = {
2723 	{
2724 		.cmd = TEAM_CMD_NOOP,
2725 		.doit = team_nl_cmd_noop,
2726 	},
2727 	{
2728 		.cmd = TEAM_CMD_OPTIONS_SET,
2729 		.doit = team_nl_cmd_options_set,
2730 		.flags = GENL_ADMIN_PERM,
2731 	},
2732 	{
2733 		.cmd = TEAM_CMD_OPTIONS_GET,
2734 		.doit = team_nl_cmd_options_get,
2735 		.flags = GENL_ADMIN_PERM,
2736 	},
2737 	{
2738 		.cmd = TEAM_CMD_PORT_LIST_GET,
2739 		.doit = team_nl_cmd_port_list_get,
2740 		.flags = GENL_ADMIN_PERM,
2741 	},
2742 };
2743 
2744 static const struct genl_multicast_group team_nl_mcgrps[] = {
2745 	{ .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2746 };
2747 
2748 static struct genl_family team_nl_family __ro_after_init = {
2749 	.name		= TEAM_GENL_NAME,
2750 	.version	= TEAM_GENL_VERSION,
2751 	.maxattr	= TEAM_ATTR_MAX,
2752 	.policy = team_nl_policy,
2753 	.netnsok	= true,
2754 	.module		= THIS_MODULE,
2755 	.ops		= team_nl_ops,
2756 	.n_ops		= ARRAY_SIZE(team_nl_ops),
2757 	.mcgrps		= team_nl_mcgrps,
2758 	.n_mcgrps	= ARRAY_SIZE(team_nl_mcgrps),
2759 };
2760 
2761 static int team_nl_send_multicast(struct sk_buff *skb,
2762 				  struct team *team, u32 portid)
2763 {
2764 	return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2765 				       skb, 0, 0, GFP_KERNEL);
2766 }
2767 
2768 static int team_nl_send_event_options_get(struct team *team,
2769 					  struct list_head *sel_opt_inst_list)
2770 {
2771 	return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2772 					sel_opt_inst_list);
2773 }
2774 
2775 static int team_nl_send_event_port_get(struct team *team,
2776 				       struct team_port *port)
2777 {
2778 	return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2779 					  port);
2780 }
2781 
2782 static int __init team_nl_init(void)
2783 {
2784 	return genl_register_family(&team_nl_family);
2785 }
2786 
2787 static void team_nl_fini(void)
2788 {
2789 	genl_unregister_family(&team_nl_family);
2790 }
2791 
2792 
2793 /******************
2794  * Change checkers
2795  ******************/
2796 
2797 static void __team_options_change_check(struct team *team)
2798 {
2799 	int err;
2800 	struct team_option_inst *opt_inst;
2801 	LIST_HEAD(sel_opt_inst_list);
2802 
2803 	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2804 		if (opt_inst->changed)
2805 			list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2806 	}
2807 	err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2808 	if (err && err != -ESRCH)
2809 		netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2810 			    err);
2811 }
2812 
2813 /* rtnl lock is held */
2814 
2815 static void __team_port_change_send(struct team_port *port, bool linkup)
2816 {
2817 	int err;
2818 
2819 	port->changed = true;
2820 	port->state.linkup = linkup;
2821 	team_refresh_port_linkup(port);
2822 	if (linkup) {
2823 		struct ethtool_link_ksettings ecmd;
2824 
2825 		err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2826 		if (!err) {
2827 			port->state.speed = ecmd.base.speed;
2828 			port->state.duplex = ecmd.base.duplex;
2829 			goto send_event;
2830 		}
2831 	}
2832 	port->state.speed = 0;
2833 	port->state.duplex = 0;
2834 
2835 send_event:
2836 	err = team_nl_send_event_port_get(port->team, port);
2837 	if (err && err != -ESRCH)
2838 		netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2839 			    port->dev->name, err);
2840 
2841 }
2842 
2843 static void __team_carrier_check(struct team *team)
2844 {
2845 	struct team_port *port;
2846 	bool team_linkup;
2847 
2848 	if (team->user_carrier_enabled)
2849 		return;
2850 
2851 	team_linkup = false;
2852 	list_for_each_entry(port, &team->port_list, list) {
2853 		if (port->linkup) {
2854 			team_linkup = true;
2855 			break;
2856 		}
2857 	}
2858 
2859 	if (team_linkup)
2860 		netif_carrier_on(team->dev);
2861 	else
2862 		netif_carrier_off(team->dev);
2863 }
2864 
2865 static void __team_port_change_check(struct team_port *port, bool linkup)
2866 {
2867 	if (port->state.linkup != linkup)
2868 		__team_port_change_send(port, linkup);
2869 	__team_carrier_check(port->team);
2870 }
2871 
2872 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2873 {
2874 	__team_port_change_send(port, linkup);
2875 	__team_carrier_check(port->team);
2876 }
2877 
2878 static void __team_port_change_port_removed(struct team_port *port)
2879 {
2880 	port->removed = true;
2881 	__team_port_change_send(port, false);
2882 	__team_carrier_check(port->team);
2883 }
2884 
2885 static void team_port_change_check(struct team_port *port, bool linkup)
2886 {
2887 	struct team *team = port->team;
2888 
2889 	mutex_lock(&team->lock);
2890 	__team_port_change_check(port, linkup);
2891 	mutex_unlock(&team->lock);
2892 }
2893 
2894 
2895 /************************************
2896  * Net device notifier event handler
2897  ************************************/
2898 
2899 static int team_device_event(struct notifier_block *unused,
2900 			     unsigned long event, void *ptr)
2901 {
2902 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2903 	struct team_port *port;
2904 
2905 	port = team_port_get_rtnl(dev);
2906 	if (!port)
2907 		return NOTIFY_DONE;
2908 
2909 	switch (event) {
2910 	case NETDEV_UP:
2911 		if (netif_oper_up(dev))
2912 			team_port_change_check(port, true);
2913 		break;
2914 	case NETDEV_DOWN:
2915 		team_port_change_check(port, false);
2916 		break;
2917 	case NETDEV_CHANGE:
2918 		if (netif_running(port->dev))
2919 			team_port_change_check(port,
2920 					       !!netif_oper_up(port->dev));
2921 		break;
2922 	case NETDEV_UNREGISTER:
2923 		team_del_slave(port->team->dev, dev);
2924 		break;
2925 	case NETDEV_FEAT_CHANGE:
2926 		team_compute_features(port->team);
2927 		break;
2928 	case NETDEV_PRECHANGEMTU:
2929 		/* Forbid to change mtu of underlaying device */
2930 		if (!port->team->port_mtu_change_allowed)
2931 			return NOTIFY_BAD;
2932 		break;
2933 	case NETDEV_PRE_TYPE_CHANGE:
2934 		/* Forbid to change type of underlaying device */
2935 		return NOTIFY_BAD;
2936 	case NETDEV_RESEND_IGMP:
2937 		/* Propagate to master device */
2938 		call_netdevice_notifiers(event, port->team->dev);
2939 		break;
2940 	}
2941 	return NOTIFY_DONE;
2942 }
2943 
2944 static struct notifier_block team_notifier_block __read_mostly = {
2945 	.notifier_call = team_device_event,
2946 };
2947 
2948 
2949 /***********************
2950  * Module init and exit
2951  ***********************/
2952 
2953 static int __init team_module_init(void)
2954 {
2955 	int err;
2956 
2957 	register_netdevice_notifier(&team_notifier_block);
2958 
2959 	err = rtnl_link_register(&team_link_ops);
2960 	if (err)
2961 		goto err_rtnl_reg;
2962 
2963 	err = team_nl_init();
2964 	if (err)
2965 		goto err_nl_init;
2966 
2967 	return 0;
2968 
2969 err_nl_init:
2970 	rtnl_link_unregister(&team_link_ops);
2971 
2972 err_rtnl_reg:
2973 	unregister_netdevice_notifier(&team_notifier_block);
2974 
2975 	return err;
2976 }
2977 
2978 static void __exit team_module_exit(void)
2979 {
2980 	team_nl_fini();
2981 	rtnl_link_unregister(&team_link_ops);
2982 	unregister_netdevice_notifier(&team_notifier_block);
2983 }
2984 
2985 module_init(team_module_init);
2986 module_exit(team_module_exit);
2987 
2988 MODULE_LICENSE("GPL v2");
2989 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2990 MODULE_DESCRIPTION("Ethernet team device driver");
2991 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
2992