team_mode_loadbalance.c 16.9 KB
Newer Older
J
Jiri Pirko 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * drivers/net/team/team_mode_loadbalance.c - Load-balancing mode for team
 * Copyright (c) 2012 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/errno.h>
#include <linux/netdevice.h>
#include <linux/filter.h>
#include <linux/if_team.h>

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
struct lb_priv;

typedef struct team_port *lb_select_tx_port_func_t(struct team *,
						   struct lb_priv *,
						   struct sk_buff *,
						   unsigned char);

#define LB_TX_HASHTABLE_SIZE 256 /* hash is a char */

struct lb_stats {
	u64 tx_bytes;
};

struct lb_pcpu_stats {
	struct lb_stats hash_stats[LB_TX_HASHTABLE_SIZE];
	struct u64_stats_sync syncp;
};

struct lb_stats_info {
	struct lb_stats stats;
	struct lb_stats last_stats;
	struct team_option_inst_info *opt_inst_info;
};

struct lb_port_mapping {
	struct team_port __rcu *port;
	struct team_option_inst_info *opt_inst_info;
};

struct lb_priv_ex {
	struct team *team;
	struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE];
	struct sock_fprog *orig_fprog;
	struct {
		unsigned int refresh_interval; /* in tenths of second */
		struct delayed_work refresh_dw;
		struct lb_stats_info info[LB_TX_HASHTABLE_SIZE];
	} stats;
};

J
Jiri Pirko 已提交
60 61
struct lb_priv {
	struct sk_filter __rcu *fp;
62 63 64
	lb_select_tx_port_func_t __rcu *select_tx_port_func;
	struct lb_pcpu_stats __percpu *pcpu_stats;
	struct lb_priv_ex *ex; /* priv extension */
J
Jiri Pirko 已提交
65 66
};

67
static struct lb_priv *get_lb_priv(struct team *team)
J
Jiri Pirko 已提交
68 69 70 71
{
	return (struct lb_priv *) &team->mode_priv;
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
struct lb_port_priv {
	struct lb_stats __percpu *pcpu_stats;
	struct lb_stats_info stats_info;
};

static struct lb_port_priv *get_lb_port_priv(struct team_port *port)
{
	return (struct lb_port_priv *) &port->mode_priv;
}

#define LB_HTPM_PORT_BY_HASH(lp_priv, hash) \
	(lb_priv)->ex->tx_hash_to_port_mapping[hash].port

#define LB_HTPM_OPT_INST_INFO_BY_HASH(lp_priv, hash) \
	(lb_priv)->ex->tx_hash_to_port_mapping[hash].opt_inst_info

static void lb_tx_hash_to_port_mapping_null_port(struct team *team,
						 struct team_port *port)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	bool changed = false;
	int i;

	for (i = 0; i < LB_TX_HASHTABLE_SIZE; i++) {
		struct lb_port_mapping *pm;

		pm = &lb_priv->ex->tx_hash_to_port_mapping[i];
J
Jiri Pirko 已提交
99 100
		if (rcu_access_pointer(pm->port) == port) {
			RCU_INIT_POINTER(pm->port, NULL);
101 102 103 104 105 106 107 108 109 110 111 112 113 114
			team_option_inst_set_change(pm->opt_inst_info);
			changed = true;
		}
	}
	if (changed)
		team_options_change_check(team);
}

/* Basic tx selection based solely by hash */
static struct team_port *lb_hash_select_tx_port(struct team *team,
						struct lb_priv *lb_priv,
						struct sk_buff *skb,
						unsigned char hash)
{
115
	int port_index = team_num_to_port_index(team, hash);
116 117 118 119 120 121 122 123 124 125

	return team_get_port_by_index_rcu(team, port_index);
}

/* Hash to port mapping select tx port */
static struct team_port *lb_htpm_select_tx_port(struct team *team,
						struct lb_priv *lb_priv,
						struct sk_buff *skb,
						unsigned char hash)
{
126
	return rcu_dereference_bh(LB_HTPM_PORT_BY_HASH(lb_priv, hash));
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

struct lb_select_tx_port {
	char *name;
	lb_select_tx_port_func_t *func;
};

static const struct lb_select_tx_port lb_select_tx_port_list[] = {
	{
		.name = "hash",
		.func = lb_hash_select_tx_port,
	},
	{
		.name = "hash_to_port_mapping",
		.func = lb_htpm_select_tx_port,
	},
};
#define LB_SELECT_TX_PORT_LIST_COUNT ARRAY_SIZE(lb_select_tx_port_list)

static char *lb_select_tx_port_get_name(lb_select_tx_port_func_t *func)
{
	int i;

	for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
		const struct lb_select_tx_port *item;

		item = &lb_select_tx_port_list[i];
		if (item->func == func)
			return item->name;
	}
	return NULL;
}

static lb_select_tx_port_func_t *lb_select_tx_port_get_func(const char *name)
{
	int i;

	for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
		const struct lb_select_tx_port *item;

		item = &lb_select_tx_port_list[i];
		if (!strcmp(item->name, name))
			return item->func;
	}
	return NULL;
}

static unsigned int lb_get_skb_hash(struct lb_priv *lb_priv,
				    struct sk_buff *skb)
J
Jiri Pirko 已提交
176 177
{
	struct sk_filter *fp;
178 179 180
	uint32_t lhash;
	unsigned char *c;

181
	fp = rcu_dereference_bh(lb_priv->fp);
182 183 184 185 186 187 188
	if (unlikely(!fp))
		return 0;
	lhash = SK_RUN_FILTER(fp, skb);
	c = (char *) &lhash;
	return c[0] ^ c[1] ^ c[2] ^ c[3];
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static void lb_update_tx_stats(unsigned int tx_bytes, struct lb_priv *lb_priv,
			       struct lb_port_priv *lb_port_priv,
			       unsigned char hash)
{
	struct lb_pcpu_stats *pcpu_stats;
	struct lb_stats *port_stats;
	struct lb_stats *hash_stats;

	pcpu_stats = this_cpu_ptr(lb_priv->pcpu_stats);
	port_stats = this_cpu_ptr(lb_port_priv->pcpu_stats);
	hash_stats = &pcpu_stats->hash_stats[hash];
	u64_stats_update_begin(&pcpu_stats->syncp);
	port_stats->tx_bytes += tx_bytes;
	hash_stats->tx_bytes += tx_bytes;
	u64_stats_update_end(&pcpu_stats->syncp);
}

206 207
static bool lb_transmit(struct team *team, struct sk_buff *skb)
{
208 209
	struct lb_priv *lb_priv = get_lb_priv(team);
	lb_select_tx_port_func_t *select_tx_port_func;
J
Jiri Pirko 已提交
210
	struct team_port *port;
211 212
	unsigned char hash;
	unsigned int tx_bytes = skb->len;
J
Jiri Pirko 已提交
213

214
	hash = lb_get_skb_hash(lb_priv, skb);
215
	select_tx_port_func = rcu_dereference_bh(lb_priv->select_tx_port_func);
216
	port = select_tx_port_func(team, lb_priv, skb, hash);
J
Jiri Pirko 已提交
217 218
	if (unlikely(!port))
		goto drop;
J
Jiri Pirko 已提交
219
	if (team_dev_queue_xmit(team, port, skb))
J
Jiri Pirko 已提交
220
		return false;
221
	lb_update_tx_stats(tx_bytes, lb_priv, get_lb_port_priv(port), hash);
J
Jiri Pirko 已提交
222 223 224 225 226 227 228
	return true;

drop:
	dev_kfree_skb_any(skb);
	return false;
}

229
static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
J
Jiri Pirko 已提交
230
{
231 232 233
	struct lb_priv *lb_priv = get_lb_priv(team);

	if (!lb_priv->ex->orig_fprog) {
234 235
		ctx->data.bin_val.len = 0;
		ctx->data.bin_val.ptr = NULL;
J
Jiri Pirko 已提交
236
		return 0;
237
	}
238
	ctx->data.bin_val.len = lb_priv->ex->orig_fprog->len *
239
				sizeof(struct sock_filter);
240
	ctx->data.bin_val.ptr = lb_priv->ex->orig_fprog->filter;
J
Jiri Pirko 已提交
241 242 243 244
	return 0;
}

static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
245
			  const void *data)
J
Jiri Pirko 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
	struct sock_fprog *fprog;
	struct sock_filter *filter = (struct sock_filter *) data;

	if (data_len % sizeof(struct sock_filter))
		return -EINVAL;
	fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
	if (!fprog)
		return -ENOMEM;
	fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
	if (!fprog->filter) {
		kfree(fprog);
		return -ENOMEM;
	}
	fprog->len = data_len / sizeof(struct sock_filter);
	*pfprog = fprog;
	return 0;
}

static void __fprog_destroy(struct sock_fprog *fprog)
{
	kfree(fprog->filter);
	kfree(fprog);
}

271
static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
J
Jiri Pirko 已提交
272
{
273
	struct lb_priv *lb_priv = get_lb_priv(team);
J
Jiri Pirko 已提交
274
	struct sk_filter *fp = NULL;
J
Jiri Pirko 已提交
275
	struct sk_filter *orig_fp;
J
Jiri Pirko 已提交
276 277 278
	struct sock_fprog *fprog = NULL;
	int err;

279 280 281
	if (ctx->data.bin_val.len) {
		err = __fprog_create(&fprog, ctx->data.bin_val.len,
				     ctx->data.bin_val.ptr);
J
Jiri Pirko 已提交
282 283 284 285 286 287 288 289 290
		if (err)
			return err;
		err = sk_unattached_filter_create(&fp, fprog);
		if (err) {
			__fprog_destroy(fprog);
			return err;
		}
	}

291
	if (lb_priv->ex->orig_fprog) {
J
Jiri Pirko 已提交
292
		/* Clear old filter data */
293
		__fprog_destroy(lb_priv->ex->orig_fprog);
J
Jiri Pirko 已提交
294 295 296
		orig_fp = rcu_dereference_protected(lb_priv->fp,
						lockdep_is_held(&team->lock));
		sk_unattached_filter_destroy(orig_fp);
J
Jiri Pirko 已提交
297 298
	}

299 300 301 302 303 304 305 306
	rcu_assign_pointer(lb_priv->fp, fp);
	lb_priv->ex->orig_fprog = fprog;
	return 0;
}

static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
J
Jiri Pirko 已提交
307
	lb_select_tx_port_func_t *func;
308 309
	char *name;

J
Jiri Pirko 已提交
310 311 312
	func = rcu_dereference_protected(lb_priv->select_tx_port_func,
					 lockdep_is_held(&team->lock));
	name = lb_select_tx_port_get_name(func);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	BUG_ON(!name);
	ctx->data.str_val = name;
	return 0;
}

static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	lb_select_tx_port_func_t *func;

	func = lb_select_tx_port_get_func(ctx->data.str_val);
	if (!func)
		return -EINVAL;
	rcu_assign_pointer(lb_priv->select_tx_port_func, func);
	return 0;
}

static int lb_tx_hash_to_port_mapping_init(struct team *team,
					   struct team_option_inst_info *info)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	unsigned char hash = info->array_index;

	LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info;
	return 0;
}

static int lb_tx_hash_to_port_mapping_get(struct team *team,
					  struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	struct team_port *port;
	unsigned char hash = ctx->info->array_index;

	port = LB_HTPM_PORT_BY_HASH(lb_priv, hash);
	ctx->data.u32_val = port ? port->dev->ifindex : 0;
	return 0;
}

static int lb_tx_hash_to_port_mapping_set(struct team *team,
					  struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	struct team_port *port;
	unsigned char hash = ctx->info->array_index;

	list_for_each_entry(port, &team->port_list, list) {
360 361
		if (ctx->data.u32_val == port->dev->ifindex &&
		    team_port_enabled(port)) {
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
			rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
					   port);
			return 0;
		}
	}
	return -ENODEV;
}

static int lb_hash_stats_init(struct team *team,
			      struct team_option_inst_info *info)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	unsigned char hash = info->array_index;

	lb_priv->ex->stats.info[hash].opt_inst_info = info;
	return 0;
}

static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	unsigned char hash = ctx->info->array_index;

	ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats;
	ctx->data.bin_val.len = sizeof(struct lb_stats);
	return 0;
}

static int lb_port_stats_init(struct team *team,
			      struct team_option_inst_info *info)
{
	struct team_port *port = info->port;
	struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);

	lb_port_priv->stats_info.opt_inst_info = info;
	return 0;
}

static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
{
	struct team_port *port = ctx->info->port;
	struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);

	ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats;
	ctx->data.bin_val.len = sizeof(struct lb_stats);
	return 0;
}

static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info)
{
	memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats));
	memset(&s_info->stats, 0, sizeof(struct lb_stats));
}

static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info,
					  struct team *team)
{
	if (memcmp(&s_info->last_stats, &s_info->stats,
	    sizeof(struct lb_stats))) {
		team_option_inst_set_change(s_info->opt_inst_info);
		return true;
	}
	return false;
}

static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats,
				   struct lb_stats *cpu_stats,
				   struct u64_stats_sync *syncp)
{
	unsigned int start;
	struct lb_stats tmp;

	do {
		start = u64_stats_fetch_begin_bh(syncp);
		tmp.tx_bytes = cpu_stats->tx_bytes;
	} while (u64_stats_fetch_retry_bh(syncp, start));
	acc_stats->tx_bytes += tmp.tx_bytes;
}

static void lb_stats_refresh(struct work_struct *work)
{
	struct team *team;
	struct lb_priv *lb_priv;
	struct lb_priv_ex *lb_priv_ex;
	struct lb_pcpu_stats *pcpu_stats;
	struct lb_stats *stats;
	struct lb_stats_info *s_info;
	struct team_port *port;
	bool changed = false;
	int i;
	int j;

	lb_priv_ex = container_of(work, struct lb_priv_ex,
				  stats.refresh_dw.work);

	team = lb_priv_ex->team;
	lb_priv = get_lb_priv(team);

	if (!mutex_trylock(&team->lock)) {
		schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0);
		return;
	}

	for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) {
		s_info = &lb_priv->ex->stats.info[j];
		__lb_stats_info_refresh_prepare(s_info);
		for_each_possible_cpu(i) {
			pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
			stats = &pcpu_stats->hash_stats[j];
			__lb_one_cpu_stats_add(&s_info->stats, stats,
					       &pcpu_stats->syncp);
		}
		changed |= __lb_stats_info_refresh_check(s_info, team);
	}

	list_for_each_entry(port, &team->port_list, list) {
		struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);

		s_info = &lb_port_priv->stats_info;
		__lb_stats_info_refresh_prepare(s_info);
		for_each_possible_cpu(i) {
			pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
			stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i);
			__lb_one_cpu_stats_add(&s_info->stats, stats,
					       &pcpu_stats->syncp);
		}
		changed |= __lb_stats_info_refresh_check(s_info, team);
	}

	if (changed)
		team_options_change_check(team);

	schedule_delayed_work(&lb_priv_ex->stats.refresh_dw,
			      (lb_priv_ex->stats.refresh_interval * HZ) / 10);

	mutex_unlock(&team->lock);
}

static int lb_stats_refresh_interval_get(struct team *team,
					 struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);

	ctx->data.u32_val = lb_priv->ex->stats.refresh_interval;
	return 0;
}

static int lb_stats_refresh_interval_set(struct team *team,
					 struct team_gsetter_ctx *ctx)
{
	struct lb_priv *lb_priv = get_lb_priv(team);
	unsigned int interval;

	interval = ctx->data.u32_val;
	if (lb_priv->ex->stats.refresh_interval == interval)
		return 0;
	lb_priv->ex->stats.refresh_interval = interval;
	if (interval)
		schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0);
	else
		cancel_delayed_work(&lb_priv->ex->stats.refresh_dw);
J
Jiri Pirko 已提交
523 524 525 526 527 528 529 530 531 532
	return 0;
}

static const struct team_option lb_options[] = {
	{
		.name = "bpf_hash_func",
		.type = TEAM_OPTION_TYPE_BINARY,
		.getter = lb_bpf_func_get,
		.setter = lb_bpf_func_set,
	},
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	{
		.name = "lb_tx_method",
		.type = TEAM_OPTION_TYPE_STRING,
		.getter = lb_tx_method_get,
		.setter = lb_tx_method_set,
	},
	{
		.name = "lb_tx_hash_to_port_mapping",
		.array_size = LB_TX_HASHTABLE_SIZE,
		.type = TEAM_OPTION_TYPE_U32,
		.init = lb_tx_hash_to_port_mapping_init,
		.getter = lb_tx_hash_to_port_mapping_get,
		.setter = lb_tx_hash_to_port_mapping_set,
	},
	{
		.name = "lb_hash_stats",
		.array_size = LB_TX_HASHTABLE_SIZE,
		.type = TEAM_OPTION_TYPE_BINARY,
		.init = lb_hash_stats_init,
		.getter = lb_hash_stats_get,
	},
	{
		.name = "lb_port_stats",
		.per_port = true,
		.type = TEAM_OPTION_TYPE_BINARY,
		.init = lb_port_stats_init,
		.getter = lb_port_stats_get,
	},
	{
		.name = "lb_stats_refresh_interval",
		.type = TEAM_OPTION_TYPE_U32,
		.getter = lb_stats_refresh_interval_get,
		.setter = lb_stats_refresh_interval_set,
	},
J
Jiri Pirko 已提交
567 568
};

J
Jiri Pirko 已提交
569
static int lb_init(struct team *team)
J
Jiri Pirko 已提交
570
{
571 572
	struct lb_priv *lb_priv = get_lb_priv(team);
	lb_select_tx_port_func_t *func;
573
	int i, err;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

	/* set default tx port selector */
	func = lb_select_tx_port_get_func("hash");
	BUG_ON(!func);
	rcu_assign_pointer(lb_priv->select_tx_port_func, func);

	lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL);
	if (!lb_priv->ex)
		return -ENOMEM;
	lb_priv->ex->team = team;

	lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats);
	if (!lb_priv->pcpu_stats) {
		err = -ENOMEM;
		goto err_alloc_pcpu_stats;
	}

591 592 593 594 595 596 597
	for_each_possible_cpu(i) {
		struct lb_pcpu_stats *team_lb_stats;
		team_lb_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
		u64_stats_init(&team_lb_stats->syncp);
	}


598 599 600 601 602 603 604 605 606 607 608 609
	INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh);

	err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options));
	if (err)
		goto err_options_register;
	return 0;

err_options_register:
	free_percpu(lb_priv->pcpu_stats);
err_alloc_pcpu_stats:
	kfree(lb_priv->ex);
	return err;
J
Jiri Pirko 已提交
610 611
}

J
Jiri Pirko 已提交
612
static void lb_exit(struct team *team)
J
Jiri Pirko 已提交
613
{
614 615
	struct lb_priv *lb_priv = get_lb_priv(team);

J
Jiri Pirko 已提交
616 617
	team_options_unregister(team, lb_options,
				ARRAY_SIZE(lb_options));
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw);
	free_percpu(lb_priv->pcpu_stats);
	kfree(lb_priv->ex);
}

static int lb_port_enter(struct team *team, struct team_port *port)
{
	struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);

	lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats);
	if (!lb_port_priv->pcpu_stats)
		return -ENOMEM;
	return 0;
}

static void lb_port_leave(struct team *team, struct team_port *port)
{
	struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);

	free_percpu(lb_port_priv->pcpu_stats);
}

static void lb_port_disabled(struct team *team, struct team_port *port)
{
	lb_tx_hash_to_port_mapping_null_port(team, port);
J
Jiri Pirko 已提交
643 644 645 646 647
}

static const struct team_mode_ops lb_mode_ops = {
	.init			= lb_init,
	.exit			= lb_exit,
648 649 650
	.port_enter		= lb_port_enter,
	.port_leave		= lb_port_leave,
	.port_disabled		= lb_port_disabled,
J
Jiri Pirko 已提交
651 652 653
	.transmit		= lb_transmit,
};

J
Jiri Pirko 已提交
654
static const struct team_mode lb_mode = {
J
Jiri Pirko 已提交
655 656 657
	.kind		= "loadbalance",
	.owner		= THIS_MODULE,
	.priv_size	= sizeof(struct lb_priv),
658
	.port_priv_size	= sizeof(struct lb_port_priv),
J
Jiri Pirko 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	.ops		= &lb_mode_ops,
};

static int __init lb_init_module(void)
{
	return team_mode_register(&lb_mode);
}

static void __exit lb_cleanup_module(void)
{
	team_mode_unregister(&lb_mode);
}

module_init(lb_init_module);
module_exit(lb_cleanup_module);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
MODULE_DESCRIPTION("Load-balancing mode for team");
MODULE_ALIAS("team-mode-loadbalance");