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