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