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