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