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