team.c 69.2 KB
Newer Older
1
/*
2
 * drivers/net/team/team.c - Network team device driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/rcupdate.h>
#include <linux/errno.h>
#include <linux/ctype.h>
#include <linux/notifier.h>
#include <linux/netdevice.h>
Jiri Pirko's avatar
Jiri Pirko committed
21
#include <linux/netpoll.h>
22
#include <linux/if_vlan.h>
23 24 25 26 27 28 29
#include <linux/if_arp.h>
#include <linux/socket.h>
#include <linux/etherdevice.h>
#include <linux/rtnetlink.h>
#include <net/rtnetlink.h>
#include <net/genetlink.h>
#include <net/netlink.h>
Jiri Pirko's avatar
Jiri Pirko committed
30
#include <net/sch_generic.h>
31
#include <net/switchdev.h>
Flavio Leitner's avatar
Flavio Leitner committed
32
#include <generated/utsrelease.h>
33 34 35 36 37 38 39 40 41 42 43 44 45
#include <linux/if_team.h>

#define DRV_NAME "team"


/**********
 * Helpers
 **********/

#define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)

static struct team_port *team_port_get_rcu(const struct net_device *dev)
{
46
	return rcu_dereference(dev->rx_handler_data);
47 48 49 50 51 52 53 54 55 56
}

static struct team_port *team_port_get_rtnl(const struct net_device *dev)
{
	struct team_port *port = rtnl_dereference(dev->rx_handler_data);

	return team_port_exists(dev) ? port : NULL;
}

/*
57
 * Since the ability to change device address for open port device is tested in
58 59
 * team_port_add, this function can be called without control of return value
 */
60 61
static int __set_port_dev_addr(struct net_device *port_dev,
			       const unsigned char *dev_addr)
62 63 64
{
	struct sockaddr addr;

65 66
	memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
	addr.sa_family = port_dev->type;
67 68 69
	return dev_set_mac_address(port_dev, &addr);
}

70
static int team_port_set_orig_dev_addr(struct team_port *port)
71
{
72
	return __set_port_dev_addr(port->dev, port->orig.dev_addr);
73 74
}

75 76
static int team_port_set_team_dev_addr(struct team *team,
				       struct team_port *port)
77
{
78
	return __set_port_dev_addr(port->dev, team->dev->dev_addr);
79
}
80 81 82 83 84 85 86 87 88 89 90 91 92

int team_modeop_port_enter(struct team *team, struct team_port *port)
{
	return team_port_set_team_dev_addr(team, port);
}
EXPORT_SYMBOL(team_modeop_port_enter);

void team_modeop_port_change_dev_addr(struct team *team,
				      struct team_port *port)
{
	team_port_set_team_dev_addr(team, port);
}
EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
93

94 95 96 97 98
static void team_refresh_port_linkup(struct team_port *port)
{
	port->linkup = port->user.linkup_enabled ? port->user.linkup :
						   port->state.linkup;
}
99

Jiri Pirko's avatar
Jiri Pirko committed
100

101 102 103 104
/*******************
 * Options handling
 *******************/

105 106
struct team_option_inst { /* One for each option instance */
	struct list_head list;
107
	struct list_head tmp_list;
108
	struct team_option *option;
109
	struct team_option_inst_info info;
110 111 112 113 114 115
	bool changed;
	bool removed;
};

static struct team_option *__team_find_option(struct team *team,
					      const char *opt_name)
116 117 118 119 120 121 122 123 124 125
{
	struct team_option *option;

	list_for_each_entry(option, &team->option_list, list) {
		if (strcmp(option->name, opt_name) == 0)
			return option;
	}
	return NULL;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void __team_option_inst_del(struct team_option_inst *opt_inst)
{
	list_del(&opt_inst->list);
	kfree(opt_inst);
}

static void __team_option_inst_del_option(struct team *team,
					  struct team_option *option)
{
	struct team_option_inst *opt_inst, *tmp;

	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
		if (opt_inst->option == option)
			__team_option_inst_del(opt_inst);
	}
}

Jiri Pirko's avatar
Jiri Pirko committed
143 144 145 146 147 148
static int __team_option_inst_add(struct team *team, struct team_option *option,
				  struct team_port *port)
{
	struct team_option_inst *opt_inst;
	unsigned int array_size;
	unsigned int i;
149
	int err;
Jiri Pirko's avatar
Jiri Pirko committed
150 151 152 153 154 155 156 157 158 159

	array_size = option->array_size;
	if (!array_size)
		array_size = 1; /* No array but still need one instance */

	for (i = 0; i < array_size; i++) {
		opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
		if (!opt_inst)
			return -ENOMEM;
		opt_inst->option = option;
160 161
		opt_inst->info.port = port;
		opt_inst->info.array_index = i;
Jiri Pirko's avatar
Jiri Pirko committed
162 163 164
		opt_inst->changed = true;
		opt_inst->removed = false;
		list_add_tail(&opt_inst->list, &team->option_inst_list);
165 166 167 168 169 170
		if (option->init) {
			err = option->init(team, &opt_inst->info);
			if (err)
				return err;
		}

Jiri Pirko's avatar
Jiri Pirko committed
171 172 173 174
	}
	return 0;
}

175 176 177 178 179
static int __team_option_inst_add_option(struct team *team,
					 struct team_option *option)
{
	int err;

Jiri Pirko's avatar
Jiri Pirko committed
180
	if (!option->per_port) {
181
		err = __team_option_inst_add(team, option, NULL);
Jiri Pirko's avatar
Jiri Pirko committed
182 183 184
		if (err)
			goto inst_del_option;
	}
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	return 0;

inst_del_option:
	__team_option_inst_del_option(team, option);
	return err;
}

static void __team_option_inst_mark_removed_option(struct team *team,
						   struct team_option *option)
{
	struct team_option_inst *opt_inst;

	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
		if (opt_inst->option == option) {
			opt_inst->changed = true;
			opt_inst->removed = true;
		}
	}
}

static void __team_option_inst_del_port(struct team *team,
					struct team_port *port)
{
	struct team_option_inst *opt_inst, *tmp;

	list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
		if (opt_inst->option->per_port &&
212
		    opt_inst->info.port == port)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
			__team_option_inst_del(opt_inst);
	}
}

static int __team_option_inst_add_port(struct team *team,
				       struct team_port *port)
{
	struct team_option *option;
	int err;

	list_for_each_entry(option, &team->option_list, list) {
		if (!option->per_port)
			continue;
		err = __team_option_inst_add(team, option, port);
		if (err)
			goto inst_del_port;
	}
	return 0;

inst_del_port:
	__team_option_inst_del_port(team, port);
	return err;
}

static void __team_option_inst_mark_removed_port(struct team *team,
						 struct team_port *port)
{
	struct team_option_inst *opt_inst;

	list_for_each_entry(opt_inst, &team->option_inst_list, list) {
243
		if (opt_inst->info.port == port) {
244 245 246 247 248 249 250 251 252
			opt_inst->changed = true;
			opt_inst->removed = true;
		}
	}
}

static int __team_options_register(struct team *team,
				   const struct team_option *option,
				   size_t option_count)
253 254
{
	int i;
255
	struct team_option **dst_opts;
256
	int err;
257

258 259 260 261
	dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
			   GFP_KERNEL);
	if (!dst_opts)
		return -ENOMEM;
262 263 264
	for (i = 0; i < option_count; i++, option++) {
		if (__team_find_option(team, option->name)) {
			err = -EEXIST;
265
			goto alloc_rollback;
266
		}
267 268
		dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
		if (!dst_opts[i]) {
269
			err = -ENOMEM;
270
			goto alloc_rollback;
271 272 273
		}
	}

274
	for (i = 0; i < option_count; i++) {
275 276 277
		err = __team_option_inst_add_option(team, dst_opts[i]);
		if (err)
			goto inst_rollback;
278
		list_add_tail(&dst_opts[i]->list, &team->option_list);
279
	}
280

281
	kfree(dst_opts);
282 283
	return 0;

284 285 286 287 288 289 290
inst_rollback:
	for (i--; i >= 0; i--)
		__team_option_inst_del_option(team, dst_opts[i]);

	i = option_count - 1;
alloc_rollback:
	for (i--; i >= 0; i--)
291 292
		kfree(dst_opts[i]);

293
	kfree(dst_opts);
294
	return err;
295
}
296

297 298 299 300 301 302 303 304
static void __team_options_mark_removed(struct team *team,
					const struct team_option *option,
					size_t option_count)
{
	int i;

	for (i = 0; i < option_count; i++, option++) {
		struct team_option *del_opt;
305

306
		del_opt = __team_find_option(team, option->name);
307 308
		if (del_opt)
			__team_option_inst_mark_removed_option(team, del_opt);
309 310
	}
}
311 312

static void __team_options_unregister(struct team *team,
313
				      const struct team_option *option,
314 315 316 317
				      size_t option_count)
{
	int i;

318 319 320 321 322
	for (i = 0; i < option_count; i++, option++) {
		struct team_option *del_opt;

		del_opt = __team_find_option(team, option->name);
		if (del_opt) {
323
			__team_option_inst_del_option(team, del_opt);
324 325 326 327
			list_del(&del_opt->list);
			kfree(del_opt);
		}
	}
328 329
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
static void __team_options_change_check(struct team *team);

int team_options_register(struct team *team,
			  const struct team_option *option,
			  size_t option_count)
{
	int err;

	err = __team_options_register(team, option, option_count);
	if (err)
		return err;
	__team_options_change_check(team);
	return 0;
}
EXPORT_SYMBOL(team_options_register);

346 347
void team_options_unregister(struct team *team,
			     const struct team_option *option,
348 349
			     size_t option_count)
{
350 351
	__team_options_mark_removed(team, option, option_count);
	__team_options_change_check(team);
352 353 354 355
	__team_options_unregister(team, option, option_count);
}
EXPORT_SYMBOL(team_options_unregister);

356 357 358 359
static int team_option_get(struct team *team,
			   struct team_option_inst *opt_inst,
			   struct team_gsetter_ctx *ctx)
{
360 361
	if (!opt_inst->option->getter)
		return -EOPNOTSUPP;
362 363 364 365 366 367
	return opt_inst->option->getter(team, ctx);
}

static int team_option_set(struct team *team,
			   struct team_option_inst *opt_inst,
			   struct team_gsetter_ctx *ctx)
368
{
369 370
	if (!opt_inst->option->setter)
		return -EOPNOTSUPP;
371
	return opt_inst->option->setter(team, ctx);
372 373
}

Jiri Pirko's avatar
Jiri Pirko committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
{
	struct team_option_inst *opt_inst;

	opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
	opt_inst->changed = true;
}
EXPORT_SYMBOL(team_option_inst_set_change);

void team_options_change_check(struct team *team)
{
	__team_options_change_check(team);
}
EXPORT_SYMBOL(team_options_change_check);


390 391 392 393 394 395 396
/****************
 * Mode handling
 ****************/

static LIST_HEAD(mode_list);
static DEFINE_SPINLOCK(mode_list_lock);

Jiri Pirko's avatar
Jiri Pirko committed
397 398 399 400 401 402
struct team_mode_item {
	struct list_head list;
	const struct team_mode *mode;
};

static struct team_mode_item *__find_mode(const char *kind)
403
{
Jiri Pirko's avatar
Jiri Pirko committed
404
	struct team_mode_item *mitem;
405

Jiri Pirko's avatar
Jiri Pirko committed
406 407 408
	list_for_each_entry(mitem, &mode_list, list) {
		if (strcmp(mitem->mode->kind, kind) == 0)
			return mitem;
409 410 411 412 413 414 415 416 417 418 419 420 421 422
	}
	return NULL;
}

static bool is_good_mode_name(const char *name)
{
	while (*name != '\0') {
		if (!isalpha(*name) && !isdigit(*name) && *name != '_')
			return false;
		name++;
	}
	return true;
}

Jiri Pirko's avatar
Jiri Pirko committed
423
int team_mode_register(const struct team_mode *mode)
424 425
{
	int err = 0;
Jiri Pirko's avatar
Jiri Pirko committed
426
	struct team_mode_item *mitem;
427 428 429 430

	if (!is_good_mode_name(mode->kind) ||
	    mode->priv_size > TEAM_MODE_PRIV_SIZE)
		return -EINVAL;
Jiri Pirko's avatar
Jiri Pirko committed
431 432 433 434 435

	mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
	if (!mitem)
		return -ENOMEM;

436 437 438
	spin_lock(&mode_list_lock);
	if (__find_mode(mode->kind)) {
		err = -EEXIST;
Jiri Pirko's avatar
Jiri Pirko committed
439
		kfree(mitem);
440 441
		goto unlock;
	}
Jiri Pirko's avatar
Jiri Pirko committed
442 443
	mitem->mode = mode;
	list_add_tail(&mitem->list, &mode_list);
444 445 446 447 448 449
unlock:
	spin_unlock(&mode_list_lock);
	return err;
}
EXPORT_SYMBOL(team_mode_register);

Jiri Pirko's avatar
Jiri Pirko committed
450
void team_mode_unregister(const struct team_mode *mode)
451
{
Jiri Pirko's avatar
Jiri Pirko committed
452 453
	struct team_mode_item *mitem;

454
	spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
455 456 457 458 459
	mitem = __find_mode(mode->kind);
	if (mitem) {
		list_del_init(&mitem->list);
		kfree(mitem);
	}
460 461 462 463
	spin_unlock(&mode_list_lock);
}
EXPORT_SYMBOL(team_mode_unregister);

Jiri Pirko's avatar
Jiri Pirko committed
464
static const struct team_mode *team_mode_get(const char *kind)
465
{
Jiri Pirko's avatar
Jiri Pirko committed
466 467
	struct team_mode_item *mitem;
	const struct team_mode *mode = NULL;
468 469

	spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
470 471
	mitem = __find_mode(kind);
	if (!mitem) {
472 473 474
		spin_unlock(&mode_list_lock);
		request_module("team-mode-%s", kind);
		spin_lock(&mode_list_lock);
Jiri Pirko's avatar
Jiri Pirko committed
475
		mitem = __find_mode(kind);
476
	}
Jiri Pirko's avatar
Jiri Pirko committed
477 478
	if (mitem) {
		mode = mitem->mode;
479 480
		if (!try_module_get(mode->owner))
			mode = NULL;
Jiri Pirko's avatar
Jiri Pirko committed
481
	}
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

	spin_unlock(&mode_list_lock);
	return mode;
}

static void team_mode_put(const struct team_mode *mode)
{
	module_put(mode->owner);
}

static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
{
	dev_kfree_skb_any(skb);
	return false;
}

498 499 500
static rx_handler_result_t team_dummy_receive(struct team *team,
					      struct team_port *port,
					      struct sk_buff *skb)
501 502 503 504
{
	return RX_HANDLER_ANOTHER;
}

505 506 507 508 509 510 511 512 513 514 515
static const struct team_mode __team_no_mode = {
	.kind		= "*NOMODE*",
};

static bool team_is_mode_set(struct team *team)
{
	return team->mode != &__team_no_mode;
}

static void team_set_no_mode(struct team *team)
{
516
	team->user_carrier_enabled = false;
517 518 519
	team->mode = &__team_no_mode;
}

520
static void team_adjust_ops(struct team *team)
521 522 523 524 525 526
{
	/*
	 * To avoid checks in rx/tx skb paths, ensure here that non-null and
	 * correct ops are always set.
	 */

527
	if (!team->en_port_count || !team_is_mode_set(team) ||
528
	    !team->mode->ops->transmit)
529 530 531 532
		team->ops.transmit = team_dummy_transmit;
	else
		team->ops.transmit = team->mode->ops->transmit;

533
	if (!team->en_port_count || !team_is_mode_set(team) ||
534
	    !team->mode->ops->receive)
535 536 537 538 539 540 541 542 543 544 545 546 547 548
		team->ops.receive = team_dummy_receive;
	else
		team->ops.receive = team->mode->ops->receive;
}

/*
 * We can benefit from the fact that it's ensured no port is present
 * at the time of mode change. Therefore no packets are in fly so there's no
 * need to set mode operations in any special way.
 */
static int __team_change_mode(struct team *team,
			      const struct team_mode *new_mode)
{
	/* Check if mode was previously set and do cleanup if so */
549
	if (team_is_mode_set(team)) {
550 551 552 553 554 555 556 557 558
		void (*exit_op)(struct team *team) = team->ops.exit;

		/* Clear ops area so no callback is called any longer */
		memset(&team->ops, 0, sizeof(struct team_mode_ops));
		team_adjust_ops(team);

		if (exit_op)
			exit_op(team);
		team_mode_put(team->mode);
559
		team_set_no_mode(team);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
		/* zero private data area */
		memset(&team->mode_priv, 0,
		       sizeof(struct team) - offsetof(struct team, mode_priv));
	}

	if (!new_mode)
		return 0;

	if (new_mode->ops->init) {
		int err;

		err = new_mode->ops->init(team);
		if (err)
			return err;
	}

	team->mode = new_mode;
	memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
	team_adjust_ops(team);

	return 0;
}

static int team_change_mode(struct team *team, const char *kind)
{
Jiri Pirko's avatar
Jiri Pirko committed
585
	const struct team_mode *new_mode;
586 587 588 589 590 591 592 593
	struct net_device *dev = team->dev;
	int err;

	if (!list_empty(&team->port_list)) {
		netdev_err(dev, "No ports can be present during mode change\n");
		return -EBUSY;
	}

594
	if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		netdev_err(dev, "Unable to change to the same mode the team is in\n");
		return -EINVAL;
	}

	new_mode = team_mode_get(kind);
	if (!new_mode) {
		netdev_err(dev, "Mode \"%s\" not found\n", kind);
		return -EINVAL;
	}

	err = __team_change_mode(team, new_mode);
	if (err) {
		netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
		team_mode_put(new_mode);
		return err;
	}

	netdev_info(dev, "Mode changed to \"%s\"\n", kind);
	return 0;
}


Jiri Pirko's avatar
Jiri Pirko committed
617 618 619 620 621 622 623
/*********************
 * Peers notification
 *********************/

static void team_notify_peers_work(struct work_struct *work)
{
	struct team *team;
624
	int val;
Jiri Pirko's avatar
Jiri Pirko committed
625 626 627 628 629 630 631

	team = container_of(work, struct team, notify_peers.dw.work);

	if (!rtnl_trylock()) {
		schedule_delayed_work(&team->notify_peers.dw, 0);
		return;
	}
632 633 634 635 636
	val = atomic_dec_if_positive(&team->notify_peers.count_pending);
	if (val < 0) {
		rtnl_unlock();
		return;
	}
Jiri Pirko's avatar
Jiri Pirko committed
637 638
	call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
	rtnl_unlock();
639
	if (val)
Jiri Pirko's avatar
Jiri Pirko committed
640 641 642 643 644 645 646 647
		schedule_delayed_work(&team->notify_peers.dw,
				      msecs_to_jiffies(team->notify_peers.interval));
}

static void team_notify_peers(struct team *team)
{
	if (!team->notify_peers.count || !netif_running(team->dev))
		return;
648
	atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
Jiri Pirko's avatar
Jiri Pirko committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662
	schedule_delayed_work(&team->notify_peers.dw, 0);
}

static void team_notify_peers_init(struct team *team)
{
	INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
}

static void team_notify_peers_fini(struct team *team)
{
	cancel_delayed_work_sync(&team->notify_peers.dw);
}


663 664 665 666 667 668 669
/*******************************
 * Send multicast group rejoins
 *******************************/

static void team_mcast_rejoin_work(struct work_struct *work)
{
	struct team *team;
670
	int val;
671 672 673 674 675 676 677

	team = container_of(work, struct team, mcast_rejoin.dw.work);

	if (!rtnl_trylock()) {
		schedule_delayed_work(&team->mcast_rejoin.dw, 0);
		return;
	}
678 679 680 681 682
	val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
	if (val < 0) {
		rtnl_unlock();
		return;
	}
683 684
	call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
	rtnl_unlock();
685
	if (val)
686 687 688 689 690 691 692 693
		schedule_delayed_work(&team->mcast_rejoin.dw,
				      msecs_to_jiffies(team->mcast_rejoin.interval));
}

static void team_mcast_rejoin(struct team *team)
{
	if (!team->mcast_rejoin.count || !netif_running(team->dev))
		return;
694
	atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
695 696 697 698 699 700 701 702 703 704 705 706 707 708
	schedule_delayed_work(&team->mcast_rejoin.dw, 0);
}

static void team_mcast_rejoin_init(struct team *team)
{
	INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
}

static void team_mcast_rejoin_fini(struct team *team)
{
	cancel_delayed_work_sync(&team->mcast_rejoin.dw);
}


709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
/************************
 * Rx path frame handler
 ************************/

/* note: already called with rcu_read_lock */
static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
{
	struct sk_buff *skb = *pskb;
	struct team_port *port;
	struct team *team;
	rx_handler_result_t res;

	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
		return RX_HANDLER_CONSUMED;

	*pskb = skb;

	port = team_port_get_rcu(skb->dev);
	team = port->team;
729 730 731 732 733 734
	if (!team_port_enabled(port)) {
		/* allow exact match delivery for disabled ports */
		res = RX_HANDLER_EXACT;
	} else {
		res = team->ops.receive(team, port, skb);
	}
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	if (res == RX_HANDLER_ANOTHER) {
		struct team_pcpu_stats *pcpu_stats;

		pcpu_stats = this_cpu_ptr(team->pcpu_stats);
		u64_stats_update_begin(&pcpu_stats->syncp);
		pcpu_stats->rx_packets++;
		pcpu_stats->rx_bytes += skb->len;
		if (skb->pkt_type == PACKET_MULTICAST)
			pcpu_stats->rx_multicast++;
		u64_stats_update_end(&pcpu_stats->syncp);

		skb->dev = team->dev;
	} else {
		this_cpu_inc(team->pcpu_stats->rx_dropped);
	}

	return res;
}


755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
/*************************************
 * Multiqueue Tx port select override
 *************************************/

static int team_queue_override_init(struct team *team)
{
	struct list_head *listarr;
	unsigned int queue_cnt = team->dev->num_tx_queues - 1;
	unsigned int i;

	if (!queue_cnt)
		return 0;
	listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
	if (!listarr)
		return -ENOMEM;
	team->qom_lists = listarr;
	for (i = 0; i < queue_cnt; i++)
		INIT_LIST_HEAD(listarr++);
	return 0;
}

static void team_queue_override_fini(struct team *team)
{
	kfree(team->qom_lists);
}

static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
{
	return &team->qom_lists[queue_id - 1];
}

/*
 * note: already called with rcu_read_lock
 */
static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
{
	struct list_head *qom_list;
	struct team_port *port;

	if (!team->queue_override_enabled || !skb->queue_mapping)
		return false;
	qom_list = __team_get_qom_list(team, skb->queue_mapping);
	list_for_each_entry_rcu(port, qom_list, qom_list) {
		if (!team_dev_queue_xmit(team, port, skb))
			return true;
	}
	return false;
}

static void __team_queue_override_port_del(struct team *team,
					   struct team_port *port)
{
807 808
	if (!port->queue_id)
		return;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	list_del_rcu(&port->qom_list);
}

static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
						      struct team_port *cur)
{
	if (port->priority < cur->priority)
		return true;
	if (port->priority > cur->priority)
		return false;
	if (port->index < cur->index)
		return true;
	return false;
}

static void __team_queue_override_port_add(struct team *team,
					   struct team_port *port)
{
	struct team_port *cur;
	struct list_head *qom_list;
	struct list_head *node;

831
	if (!port->queue_id)
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		return;
	qom_list = __team_get_qom_list(team, port->queue_id);
	node = qom_list;
	list_for_each_entry(cur, qom_list, qom_list) {
		if (team_queue_override_port_has_gt_prio_than(port, cur))
			break;
		node = &cur->qom_list;
	}
	list_add_tail_rcu(&port->qom_list, node);
}

static void __team_queue_override_enabled_check(struct team *team)
{
	struct team_port *port;
	bool enabled = false;

	list_for_each_entry(port, &team->port_list, list) {
849
		if (port->queue_id) {
850 851 852 853 854 855 856 857 858 859 860
			enabled = true;
			break;
		}
	}
	if (enabled == team->queue_override_enabled)
		return;
	netdev_dbg(team->dev, "%s queue override\n",
		   enabled ? "Enabling" : "Disabling");
	team->queue_override_enabled = enabled;
}

861 862
static void team_queue_override_port_prio_changed(struct team *team,
						  struct team_port *port)
863
{
864 865
	if (!port->queue_id || team_port_enabled(port))
		return;
866 867 868 869 870
	__team_queue_override_port_del(team, port);
	__team_queue_override_port_add(team, port);
	__team_queue_override_enabled_check(team);
}

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
static void team_queue_override_port_change_queue_id(struct team *team,
						     struct team_port *port,
						     u16 new_queue_id)
{
	if (team_port_enabled(port)) {
		__team_queue_override_port_del(team, port);
		port->queue_id = new_queue_id;
		__team_queue_override_port_add(team, port);
		__team_queue_override_enabled_check(team);
	} else {
		port->queue_id = new_queue_id;
	}
}

static void team_queue_override_port_add(struct team *team,
					 struct team_port *port)
{
	__team_queue_override_port_add(team, port);
	__team_queue_override_enabled_check(team);
}

static void team_queue_override_port_del(struct team *team,
					 struct team_port *port)
{
	__team_queue_override_port_del(team, port);
	__team_queue_override_enabled_check(team);
}

899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
/****************
 * Port handling
 ****************/

static bool team_port_find(const struct team *team,
			   const struct team_port *port)
{
	struct team_port *cur;

	list_for_each_entry(cur, &team->port_list, list)
		if (cur == port)
			return true;
	return false;
}

/*
916 917 918 919
 * Enable/disable port by adding to enabled port hashlist and setting
 * port->index (Might be racy so reader could see incorrect ifindex when
 * processing a flying packet, but that is not a problem). Write guarded
 * by team->lock.
920
 */
921 922
static void team_port_enable(struct team *team,
			     struct team_port *port)
923
{
924 925 926
	if (team_port_enabled(port))
		return;
	port->index = team->en_port_count++;
927 928
	hlist_add_head_rcu(&port->hlist,
			   team_port_index_hash(team, port->index));
929
	team_adjust_ops(team);
930
	team_queue_override_port_add(team, port);
931 932
	if (team->ops.port_enabled)
		team->ops.port_enabled(team, port);
Jiri Pirko's avatar
Jiri Pirko committed
933
	team_notify_peers(team);
934
	team_mcast_rejoin(team);
935 936 937 938 939 940 941
}

static void __reconstruct_port_hlist(struct team *team, int rm_index)
{
	int i;
	struct team_port *port;

942
	for (i = rm_index + 1; i < team->en_port_count; i++) {
943 944 945 946 947 948 949 950
		port = team_get_port_by_index(team, i);
		hlist_del_rcu(&port->hlist);
		port->index--;
		hlist_add_head_rcu(&port->hlist,
				   team_port_index_hash(team, port->index));
	}
}

951 952
static void team_port_disable(struct team *team,
			      struct team_port *port)
953
{
954 955
	if (!team_port_enabled(port))
		return;
956 957
	if (team->ops.port_disabled)
		team->ops.port_disabled(team, port);
958
	hlist_del_rcu(&port->hlist);
959
	__reconstruct_port_hlist(team, port->index);
960
	port->index = -1;
961
	team->en_port_count--;
962 963
	team_queue_override_port_del(team, port);
	team_adjust_ops(team);
Jiri Pirko's avatar
Jiri Pirko committed
964
	team_notify_peers(team);
965
	team_mcast_rejoin(team);
966 967 968 969 970 971 972 973 974
}

#define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
			    NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
			    NETIF_F_HIGHDMA | NETIF_F_LRO)

static void __team_compute_features(struct team *team)
{
	struct team_port *port;
975
	u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
976
	unsigned short max_hard_header_len = ETH_HLEN;
977 978
	unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
					IFF_XMIT_DST_RELEASE_PERM;
979 980 981 982 983 984

	list_for_each_entry(port, &team->port_list, list) {
		vlan_features = netdev_increment_features(vlan_features,
					port->dev->vlan_features,
					TEAM_VLAN_FEATURES);

985
		dst_release_flag &= port->dev->priv_flags;
986 987 988 989 990 991 992
		if (port->dev->hard_header_len > max_hard_header_len)
			max_hard_header_len = port->dev->hard_header_len;
	}

	team->dev->vlan_features = vlan_features;
	team->dev->hard_header_len = max_hard_header_len;

993 994 995
	team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
	if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
		team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
996

997 998 999 1000 1001
	netdev_change_features(team->dev);
}

static void team_compute_features(struct team *team)
{
1002
	mutex_lock(&team->lock);
1003
	__team_compute_features(team);
1004
	mutex_unlock(&team->lock);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
}

static int team_port_enter(struct team *team, struct team_port *port)
{
	int err = 0;

	dev_hold(team->dev);
	if (team->ops.port_enter) {
		err = team->ops.port_enter(team, port);
		if (err) {
			netdev_err(team->dev, "Device %s failed to enter team mode\n",
				   port->dev->name);
			goto err_port_enter;
		}
	}

	return 0;

err_port_enter:
	dev_put(team->dev);

	return err;
}

static void team_port_leave(struct team *team, struct team_port *port)
{
	if (team->ops.port_leave)
		team->ops.port_leave(team, port);
	dev_put(team->dev);
}

Jiri Pirko's avatar
Jiri Pirko committed
1036
#ifdef CONFIG_NET_POLL_CONTROLLER
1037
static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirko's avatar
Jiri Pirko committed
1038 1039 1040 1041
{
	struct netpoll *np;
	int err;

1042 1043 1044
	if (!team->dev->npinfo)
		return 0;

1045
	np = kzalloc(sizeof(*np), GFP_KERNEL);
Jiri Pirko's avatar
Jiri Pirko committed
1046 1047 1048
	if (!np)
		return -ENOMEM;

1049
	err = __netpoll_setup(np, port->dev);
Jiri Pirko's avatar
Jiri Pirko committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	if (err) {
		kfree(np);
		return err;
	}
	port->np = np;
	return err;
}

static void team_port_disable_netpoll(struct team_port *port)
{
	struct netpoll *np = port->np;

	if (!np)
		return;
	port->np = NULL;

	/* Wait for transmitting packets to finish before freeing. */
	synchronize_rcu_bh();
	__netpoll_cleanup(np);
	kfree(np);
}
#else
1072
static int team_port_enable_netpoll(struct team *team, struct team_port *port)
Jiri Pirko's avatar
Jiri Pirko committed
1073 1074 1075 1076 1077 1078 1079 1080
{
	return 0;
}
static void team_port_disable_netpoll(struct team_port *port)
{
}
#endif

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
static int team_upper_dev_link(struct net_device *dev,
			       struct net_device *port_dev)
{
	int err;

	err = netdev_master_upper_dev_link(port_dev, dev);
	if (err)
		return err;
	port_dev->priv_flags |= IFF_TEAM_PORT;
	return 0;
}

static void team_upper_dev_unlink(struct net_device *dev,
				  struct net_device *port_dev)
{
	netdev_upper_dev_unlink(port_dev, dev);
	port_dev->priv_flags &= ~IFF_TEAM_PORT;
}

1100
static void __team_port_change_port_added(struct team_port *port, bool linkup);
1101 1102
static int team_dev_type_check_change(struct net_device *dev,
				      struct net_device *port_dev);
1103 1104 1105 1106 1107 1108 1109 1110

static int team_port_add(struct team *team, struct net_device *port_dev)
{
	struct net_device *dev = team->dev;
	struct team_port *port;
	char *portname = port_dev->name;
	int err;

1111 1112
	if (port_dev->flags & IFF_LOOPBACK) {
		netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
			   portname);
		return -EINVAL;
	}

	if (team_port_exists(port_dev)) {
		netdev_err(dev, "Device %s is already a port "
				"of a team device\n", portname);
		return -EBUSY;
	}

1123 1124 1125 1126 1127 1128 1129
	if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
	    vlan_uses_dev(dev)) {
		netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
			   portname);
		return -EPERM;
	}

1130 1131 1132 1133
	err = team_dev_type_check_change(dev, port_dev);
	if (err)
		return err;

1134 1135 1136 1137 1138 1139
	if (port_dev->flags & IFF_UP) {
		netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
			   portname);
		return -EBUSY;
	}

Jiri Pirko's avatar
Jiri Pirko committed
1140 1141
	port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
		       GFP_KERNEL);
1142 1143 1144 1145 1146
	if (!port)
		return -ENOMEM;

	port->dev = port_dev;
	port->team = team;
1147
	INIT_LIST_HEAD(&port->qom_list);
1148 1149 1150 1151 1152 1153 1154 1155

	port->orig.mtu = port_dev->mtu;
	err = dev_set_mtu(port_dev, dev->mtu);
	if (err) {
		netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
		goto err_set_mtu;
	}

1156
	memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

	err = team_port_enter(team, port);
	if (err) {
		netdev_err(dev, "Device %s failed to enter team mode\n",
			   portname);
		goto err_port_enter;
	}

	err = dev_open(port_dev);
	if (err) {
		netdev_dbg(dev, "Device %s opening failed\n",
			   portname);
		goto err_dev_open;
	}

1172 1173 1174 1175 1176 1177 1178
	err = vlan_vids_add_by_dev(port_dev, dev);
	if (err) {
		netdev_err(dev, "Failed to add vlan ids to device %s\n",
				portname);
		goto err_vids_add;
	}

1179
	err = team_port_enable_netpoll(team, port);
1180 1181 1182 1183
	if (err) {
		netdev_err(dev, "Failed to enable netpoll on device %s\n",
			   portname);
		goto err_enable_netpoll;
Jiri Pirko's avatar
Jiri Pirko committed
1184 1185
	}

1186 1187 1188
	if (!(dev->features & NETIF_F_LRO))
		dev_disable_lro(port_dev);

1189 1190 1191 1192 1193 1194 1195 1196
	err = netdev_rx_handler_register(port_dev, team_handle_frame,
					 port);
	if (err) {
		netdev_err(dev, "Device %s failed to register rx_handler\n",
			   portname);
		goto err_handler_register;
	}

1197 1198 1199 1200 1201 1202 1203
	err = team_upper_dev_link(dev, port_dev);
	if (err) {
		netdev_err(dev, "Device %s failed to set upper link\n",
			   portname);
		goto err_set_upper_link;
	}

1204
	err = __team_option_inst_add_port(team, port);
1205 1206 1207 1208 1209 1210
	if (err) {
		netdev_err(dev, "Device %s failed to add per-port options\n",
			   portname);
		goto err_option_port_add;
	}

1211 1212
	port->index = -1;
	list_add_tail_rcu(&port->list, &team->port_list);
1213
	team_port_enable(team, port);
1214
	__team_compute_features(team);
1215
	__team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1216
	__team_options_change_check(team);
1217 1218 1219 1220 1221

	netdev_info(dev, "Port device %s added\n", portname);

	return 0;

1222
err_option_port_add:
1223 1224 1225
	team_upper_dev_unlink(dev, port_dev);

err_set_upper_link:
1226 1227
	netdev_rx_handler_unregister(port_dev);

1228
err_handler_register:
Jiri Pirko's avatar
Jiri Pirko committed
1229 1230 1231
	team_port_disable_netpoll(port);

err_enable_netpoll:
1232 1233 1234
	vlan_vids_del_by_dev(port_dev, dev);

err_vids_add:
1235 1236 1237 1238
	dev_close(port_dev);

err_dev_open:
	team_port_leave(team, port);