team_mode_loadbalance.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * drivers/net/team/team_mode_loadbalance.c - Load-balancing mode for team
  3. * Copyright (c) 2012 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. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/filter.h>
  17. #include <linux/if_team.h>
  18. struct lb_priv;
  19. typedef struct team_port *lb_select_tx_port_func_t(struct team *,
  20. struct lb_priv *,
  21. struct sk_buff *,
  22. unsigned char);
  23. #define LB_TX_HASHTABLE_SIZE 256 /* hash is a char */
  24. struct lb_stats {
  25. u64 tx_bytes;
  26. };
  27. struct lb_pcpu_stats {
  28. struct lb_stats hash_stats[LB_TX_HASHTABLE_SIZE];
  29. struct u64_stats_sync syncp;
  30. };
  31. struct lb_stats_info {
  32. struct lb_stats stats;
  33. struct lb_stats last_stats;
  34. struct team_option_inst_info *opt_inst_info;
  35. };
  36. struct lb_port_mapping {
  37. struct team_port __rcu *port;
  38. struct team_option_inst_info *opt_inst_info;
  39. };
  40. struct lb_priv_ex {
  41. struct team *team;
  42. struct lb_port_mapping tx_hash_to_port_mapping[LB_TX_HASHTABLE_SIZE];
  43. struct sock_fprog *orig_fprog;
  44. struct {
  45. unsigned int refresh_interval; /* in tenths of second */
  46. struct delayed_work refresh_dw;
  47. struct lb_stats_info info[LB_TX_HASHTABLE_SIZE];
  48. } stats;
  49. };
  50. struct lb_priv {
  51. struct sk_filter __rcu *fp;
  52. lb_select_tx_port_func_t __rcu *select_tx_port_func;
  53. struct lb_pcpu_stats __percpu *pcpu_stats;
  54. struct lb_priv_ex *ex; /* priv extension */
  55. };
  56. static struct lb_priv *get_lb_priv(struct team *team)
  57. {
  58. return (struct lb_priv *) &team->mode_priv;
  59. }
  60. struct lb_port_priv {
  61. struct lb_stats __percpu *pcpu_stats;
  62. struct lb_stats_info stats_info;
  63. };
  64. static struct lb_port_priv *get_lb_port_priv(struct team_port *port)
  65. {
  66. return (struct lb_port_priv *) &port->mode_priv;
  67. }
  68. #define LB_HTPM_PORT_BY_HASH(lp_priv, hash) \
  69. (lb_priv)->ex->tx_hash_to_port_mapping[hash].port
  70. #define LB_HTPM_OPT_INST_INFO_BY_HASH(lp_priv, hash) \
  71. (lb_priv)->ex->tx_hash_to_port_mapping[hash].opt_inst_info
  72. static void lb_tx_hash_to_port_mapping_null_port(struct team *team,
  73. struct team_port *port)
  74. {
  75. struct lb_priv *lb_priv = get_lb_priv(team);
  76. bool changed = false;
  77. int i;
  78. for (i = 0; i < LB_TX_HASHTABLE_SIZE; i++) {
  79. struct lb_port_mapping *pm;
  80. pm = &lb_priv->ex->tx_hash_to_port_mapping[i];
  81. if (rcu_access_pointer(pm->port) == port) {
  82. RCU_INIT_POINTER(pm->port, NULL);
  83. team_option_inst_set_change(pm->opt_inst_info);
  84. changed = true;
  85. }
  86. }
  87. if (changed)
  88. team_options_change_check(team);
  89. }
  90. /* Basic tx selection based solely by hash */
  91. static struct team_port *lb_hash_select_tx_port(struct team *team,
  92. struct lb_priv *lb_priv,
  93. struct sk_buff *skb,
  94. unsigned char hash)
  95. {
  96. int port_index;
  97. port_index = hash % team->en_port_count;
  98. return team_get_port_by_index_rcu(team, port_index);
  99. }
  100. /* Hash to port mapping select tx port */
  101. static struct team_port *lb_htpm_select_tx_port(struct team *team,
  102. struct lb_priv *lb_priv,
  103. struct sk_buff *skb,
  104. unsigned char hash)
  105. {
  106. return rcu_dereference_bh(LB_HTPM_PORT_BY_HASH(lb_priv, hash));
  107. }
  108. struct lb_select_tx_port {
  109. char *name;
  110. lb_select_tx_port_func_t *func;
  111. };
  112. static const struct lb_select_tx_port lb_select_tx_port_list[] = {
  113. {
  114. .name = "hash",
  115. .func = lb_hash_select_tx_port,
  116. },
  117. {
  118. .name = "hash_to_port_mapping",
  119. .func = lb_htpm_select_tx_port,
  120. },
  121. };
  122. #define LB_SELECT_TX_PORT_LIST_COUNT ARRAY_SIZE(lb_select_tx_port_list)
  123. static char *lb_select_tx_port_get_name(lb_select_tx_port_func_t *func)
  124. {
  125. int i;
  126. for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
  127. const struct lb_select_tx_port *item;
  128. item = &lb_select_tx_port_list[i];
  129. if (item->func == func)
  130. return item->name;
  131. }
  132. return NULL;
  133. }
  134. static lb_select_tx_port_func_t *lb_select_tx_port_get_func(const char *name)
  135. {
  136. int i;
  137. for (i = 0; i < LB_SELECT_TX_PORT_LIST_COUNT; i++) {
  138. const struct lb_select_tx_port *item;
  139. item = &lb_select_tx_port_list[i];
  140. if (!strcmp(item->name, name))
  141. return item->func;
  142. }
  143. return NULL;
  144. }
  145. static unsigned int lb_get_skb_hash(struct lb_priv *lb_priv,
  146. struct sk_buff *skb)
  147. {
  148. struct sk_filter *fp;
  149. uint32_t lhash;
  150. unsigned char *c;
  151. fp = rcu_dereference_bh(lb_priv->fp);
  152. if (unlikely(!fp))
  153. return 0;
  154. lhash = SK_RUN_FILTER(fp, skb);
  155. c = (char *) &lhash;
  156. return c[0] ^ c[1] ^ c[2] ^ c[3];
  157. }
  158. static void lb_update_tx_stats(unsigned int tx_bytes, struct lb_priv *lb_priv,
  159. struct lb_port_priv *lb_port_priv,
  160. unsigned char hash)
  161. {
  162. struct lb_pcpu_stats *pcpu_stats;
  163. struct lb_stats *port_stats;
  164. struct lb_stats *hash_stats;
  165. pcpu_stats = this_cpu_ptr(lb_priv->pcpu_stats);
  166. port_stats = this_cpu_ptr(lb_port_priv->pcpu_stats);
  167. hash_stats = &pcpu_stats->hash_stats[hash];
  168. u64_stats_update_begin(&pcpu_stats->syncp);
  169. port_stats->tx_bytes += tx_bytes;
  170. hash_stats->tx_bytes += tx_bytes;
  171. u64_stats_update_end(&pcpu_stats->syncp);
  172. }
  173. static bool lb_transmit(struct team *team, struct sk_buff *skb)
  174. {
  175. struct lb_priv *lb_priv = get_lb_priv(team);
  176. lb_select_tx_port_func_t *select_tx_port_func;
  177. struct team_port *port;
  178. unsigned char hash;
  179. unsigned int tx_bytes = skb->len;
  180. hash = lb_get_skb_hash(lb_priv, skb);
  181. select_tx_port_func = rcu_dereference_bh(lb_priv->select_tx_port_func);
  182. port = select_tx_port_func(team, lb_priv, skb, hash);
  183. if (unlikely(!port))
  184. goto drop;
  185. if (team_dev_queue_xmit(team, port, skb))
  186. return false;
  187. lb_update_tx_stats(tx_bytes, lb_priv, get_lb_port_priv(port), hash);
  188. return true;
  189. drop:
  190. dev_kfree_skb_any(skb);
  191. return false;
  192. }
  193. static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
  194. {
  195. struct lb_priv *lb_priv = get_lb_priv(team);
  196. if (!lb_priv->ex->orig_fprog) {
  197. ctx->data.bin_val.len = 0;
  198. ctx->data.bin_val.ptr = NULL;
  199. return 0;
  200. }
  201. ctx->data.bin_val.len = lb_priv->ex->orig_fprog->len *
  202. sizeof(struct sock_filter);
  203. ctx->data.bin_val.ptr = lb_priv->ex->orig_fprog->filter;
  204. return 0;
  205. }
  206. static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
  207. const void *data)
  208. {
  209. struct sock_fprog *fprog;
  210. struct sock_filter *filter = (struct sock_filter *) data;
  211. if (data_len % sizeof(struct sock_filter))
  212. return -EINVAL;
  213. fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
  214. if (!fprog)
  215. return -ENOMEM;
  216. fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
  217. if (!fprog->filter) {
  218. kfree(fprog);
  219. return -ENOMEM;
  220. }
  221. fprog->len = data_len / sizeof(struct sock_filter);
  222. *pfprog = fprog;
  223. return 0;
  224. }
  225. static void __fprog_destroy(struct sock_fprog *fprog)
  226. {
  227. kfree(fprog->filter);
  228. kfree(fprog);
  229. }
  230. static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
  231. {
  232. struct lb_priv *lb_priv = get_lb_priv(team);
  233. struct sk_filter *fp = NULL;
  234. struct sk_filter *orig_fp;
  235. struct sock_fprog *fprog = NULL;
  236. int err;
  237. if (ctx->data.bin_val.len) {
  238. err = __fprog_create(&fprog, ctx->data.bin_val.len,
  239. ctx->data.bin_val.ptr);
  240. if (err)
  241. return err;
  242. err = sk_unattached_filter_create(&fp, fprog);
  243. if (err) {
  244. __fprog_destroy(fprog);
  245. return err;
  246. }
  247. }
  248. if (lb_priv->ex->orig_fprog) {
  249. /* Clear old filter data */
  250. __fprog_destroy(lb_priv->ex->orig_fprog);
  251. orig_fp = rcu_dereference_protected(lb_priv->fp,
  252. lockdep_is_held(&team->lock));
  253. sk_unattached_filter_destroy(orig_fp);
  254. }
  255. rcu_assign_pointer(lb_priv->fp, fp);
  256. lb_priv->ex->orig_fprog = fprog;
  257. return 0;
  258. }
  259. static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx)
  260. {
  261. struct lb_priv *lb_priv = get_lb_priv(team);
  262. lb_select_tx_port_func_t *func;
  263. char *name;
  264. func = rcu_dereference_protected(lb_priv->select_tx_port_func,
  265. lockdep_is_held(&team->lock));
  266. name = lb_select_tx_port_get_name(func);
  267. BUG_ON(!name);
  268. ctx->data.str_val = name;
  269. return 0;
  270. }
  271. static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx)
  272. {
  273. struct lb_priv *lb_priv = get_lb_priv(team);
  274. lb_select_tx_port_func_t *func;
  275. func = lb_select_tx_port_get_func(ctx->data.str_val);
  276. if (!func)
  277. return -EINVAL;
  278. rcu_assign_pointer(lb_priv->select_tx_port_func, func);
  279. return 0;
  280. }
  281. static int lb_tx_hash_to_port_mapping_init(struct team *team,
  282. struct team_option_inst_info *info)
  283. {
  284. struct lb_priv *lb_priv = get_lb_priv(team);
  285. unsigned char hash = info->array_index;
  286. LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info;
  287. return 0;
  288. }
  289. static int lb_tx_hash_to_port_mapping_get(struct team *team,
  290. struct team_gsetter_ctx *ctx)
  291. {
  292. struct lb_priv *lb_priv = get_lb_priv(team);
  293. struct team_port *port;
  294. unsigned char hash = ctx->info->array_index;
  295. port = LB_HTPM_PORT_BY_HASH(lb_priv, hash);
  296. ctx->data.u32_val = port ? port->dev->ifindex : 0;
  297. return 0;
  298. }
  299. static int lb_tx_hash_to_port_mapping_set(struct team *team,
  300. struct team_gsetter_ctx *ctx)
  301. {
  302. struct lb_priv *lb_priv = get_lb_priv(team);
  303. struct team_port *port;
  304. unsigned char hash = ctx->info->array_index;
  305. list_for_each_entry(port, &team->port_list, list) {
  306. if (ctx->data.u32_val == port->dev->ifindex &&
  307. team_port_enabled(port)) {
  308. rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
  309. port);
  310. return 0;
  311. }
  312. }
  313. return -ENODEV;
  314. }
  315. static int lb_hash_stats_init(struct team *team,
  316. struct team_option_inst_info *info)
  317. {
  318. struct lb_priv *lb_priv = get_lb_priv(team);
  319. unsigned char hash = info->array_index;
  320. lb_priv->ex->stats.info[hash].opt_inst_info = info;
  321. return 0;
  322. }
  323. static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
  324. {
  325. struct lb_priv *lb_priv = get_lb_priv(team);
  326. unsigned char hash = ctx->info->array_index;
  327. ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats;
  328. ctx->data.bin_val.len = sizeof(struct lb_stats);
  329. return 0;
  330. }
  331. static int lb_port_stats_init(struct team *team,
  332. struct team_option_inst_info *info)
  333. {
  334. struct team_port *port = info->port;
  335. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  336. lb_port_priv->stats_info.opt_inst_info = info;
  337. return 0;
  338. }
  339. static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
  340. {
  341. struct team_port *port = ctx->info->port;
  342. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  343. ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats;
  344. ctx->data.bin_val.len = sizeof(struct lb_stats);
  345. return 0;
  346. }
  347. static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info)
  348. {
  349. memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats));
  350. memset(&s_info->stats, 0, sizeof(struct lb_stats));
  351. }
  352. static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info,
  353. struct team *team)
  354. {
  355. if (memcmp(&s_info->last_stats, &s_info->stats,
  356. sizeof(struct lb_stats))) {
  357. team_option_inst_set_change(s_info->opt_inst_info);
  358. return true;
  359. }
  360. return false;
  361. }
  362. static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats,
  363. struct lb_stats *cpu_stats,
  364. struct u64_stats_sync *syncp)
  365. {
  366. unsigned int start;
  367. struct lb_stats tmp;
  368. do {
  369. start = u64_stats_fetch_begin_bh(syncp);
  370. tmp.tx_bytes = cpu_stats->tx_bytes;
  371. } while (u64_stats_fetch_retry_bh(syncp, start));
  372. acc_stats->tx_bytes += tmp.tx_bytes;
  373. }
  374. static void lb_stats_refresh(struct work_struct *work)
  375. {
  376. struct team *team;
  377. struct lb_priv *lb_priv;
  378. struct lb_priv_ex *lb_priv_ex;
  379. struct lb_pcpu_stats *pcpu_stats;
  380. struct lb_stats *stats;
  381. struct lb_stats_info *s_info;
  382. struct team_port *port;
  383. bool changed = false;
  384. int i;
  385. int j;
  386. lb_priv_ex = container_of(work, struct lb_priv_ex,
  387. stats.refresh_dw.work);
  388. team = lb_priv_ex->team;
  389. lb_priv = get_lb_priv(team);
  390. if (!mutex_trylock(&team->lock)) {
  391. schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0);
  392. return;
  393. }
  394. for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) {
  395. s_info = &lb_priv->ex->stats.info[j];
  396. __lb_stats_info_refresh_prepare(s_info);
  397. for_each_possible_cpu(i) {
  398. pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
  399. stats = &pcpu_stats->hash_stats[j];
  400. __lb_one_cpu_stats_add(&s_info->stats, stats,
  401. &pcpu_stats->syncp);
  402. }
  403. changed |= __lb_stats_info_refresh_check(s_info, team);
  404. }
  405. list_for_each_entry(port, &team->port_list, list) {
  406. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  407. s_info = &lb_port_priv->stats_info;
  408. __lb_stats_info_refresh_prepare(s_info);
  409. for_each_possible_cpu(i) {
  410. pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
  411. stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i);
  412. __lb_one_cpu_stats_add(&s_info->stats, stats,
  413. &pcpu_stats->syncp);
  414. }
  415. changed |= __lb_stats_info_refresh_check(s_info, team);
  416. }
  417. if (changed)
  418. team_options_change_check(team);
  419. schedule_delayed_work(&lb_priv_ex->stats.refresh_dw,
  420. (lb_priv_ex->stats.refresh_interval * HZ) / 10);
  421. mutex_unlock(&team->lock);
  422. }
  423. static int lb_stats_refresh_interval_get(struct team *team,
  424. struct team_gsetter_ctx *ctx)
  425. {
  426. struct lb_priv *lb_priv = get_lb_priv(team);
  427. ctx->data.u32_val = lb_priv->ex->stats.refresh_interval;
  428. return 0;
  429. }
  430. static int lb_stats_refresh_interval_set(struct team *team,
  431. struct team_gsetter_ctx *ctx)
  432. {
  433. struct lb_priv *lb_priv = get_lb_priv(team);
  434. unsigned int interval;
  435. interval = ctx->data.u32_val;
  436. if (lb_priv->ex->stats.refresh_interval == interval)
  437. return 0;
  438. lb_priv->ex->stats.refresh_interval = interval;
  439. if (interval)
  440. schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0);
  441. else
  442. cancel_delayed_work(&lb_priv->ex->stats.refresh_dw);
  443. return 0;
  444. }
  445. static const struct team_option lb_options[] = {
  446. {
  447. .name = "bpf_hash_func",
  448. .type = TEAM_OPTION_TYPE_BINARY,
  449. .getter = lb_bpf_func_get,
  450. .setter = lb_bpf_func_set,
  451. },
  452. {
  453. .name = "lb_tx_method",
  454. .type = TEAM_OPTION_TYPE_STRING,
  455. .getter = lb_tx_method_get,
  456. .setter = lb_tx_method_set,
  457. },
  458. {
  459. .name = "lb_tx_hash_to_port_mapping",
  460. .array_size = LB_TX_HASHTABLE_SIZE,
  461. .type = TEAM_OPTION_TYPE_U32,
  462. .init = lb_tx_hash_to_port_mapping_init,
  463. .getter = lb_tx_hash_to_port_mapping_get,
  464. .setter = lb_tx_hash_to_port_mapping_set,
  465. },
  466. {
  467. .name = "lb_hash_stats",
  468. .array_size = LB_TX_HASHTABLE_SIZE,
  469. .type = TEAM_OPTION_TYPE_BINARY,
  470. .init = lb_hash_stats_init,
  471. .getter = lb_hash_stats_get,
  472. },
  473. {
  474. .name = "lb_port_stats",
  475. .per_port = true,
  476. .type = TEAM_OPTION_TYPE_BINARY,
  477. .init = lb_port_stats_init,
  478. .getter = lb_port_stats_get,
  479. },
  480. {
  481. .name = "lb_stats_refresh_interval",
  482. .type = TEAM_OPTION_TYPE_U32,
  483. .getter = lb_stats_refresh_interval_get,
  484. .setter = lb_stats_refresh_interval_set,
  485. },
  486. };
  487. static int lb_init(struct team *team)
  488. {
  489. struct lb_priv *lb_priv = get_lb_priv(team);
  490. lb_select_tx_port_func_t *func;
  491. int err;
  492. /* set default tx port selector */
  493. func = lb_select_tx_port_get_func("hash");
  494. BUG_ON(!func);
  495. rcu_assign_pointer(lb_priv->select_tx_port_func, func);
  496. lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL);
  497. if (!lb_priv->ex)
  498. return -ENOMEM;
  499. lb_priv->ex->team = team;
  500. lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats);
  501. if (!lb_priv->pcpu_stats) {
  502. err = -ENOMEM;
  503. goto err_alloc_pcpu_stats;
  504. }
  505. INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh);
  506. err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options));
  507. if (err)
  508. goto err_options_register;
  509. return 0;
  510. err_options_register:
  511. free_percpu(lb_priv->pcpu_stats);
  512. err_alloc_pcpu_stats:
  513. kfree(lb_priv->ex);
  514. return err;
  515. }
  516. static void lb_exit(struct team *team)
  517. {
  518. struct lb_priv *lb_priv = get_lb_priv(team);
  519. team_options_unregister(team, lb_options,
  520. ARRAY_SIZE(lb_options));
  521. cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw);
  522. free_percpu(lb_priv->pcpu_stats);
  523. kfree(lb_priv->ex);
  524. }
  525. static int lb_port_enter(struct team *team, struct team_port *port)
  526. {
  527. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  528. lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats);
  529. if (!lb_port_priv->pcpu_stats)
  530. return -ENOMEM;
  531. return 0;
  532. }
  533. static void lb_port_leave(struct team *team, struct team_port *port)
  534. {
  535. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  536. free_percpu(lb_port_priv->pcpu_stats);
  537. }
  538. static void lb_port_disabled(struct team *team, struct team_port *port)
  539. {
  540. lb_tx_hash_to_port_mapping_null_port(team, port);
  541. }
  542. static const struct team_mode_ops lb_mode_ops = {
  543. .init = lb_init,
  544. .exit = lb_exit,
  545. .port_enter = lb_port_enter,
  546. .port_leave = lb_port_leave,
  547. .port_disabled = lb_port_disabled,
  548. .transmit = lb_transmit,
  549. };
  550. static const struct team_mode lb_mode = {
  551. .kind = "loadbalance",
  552. .owner = THIS_MODULE,
  553. .priv_size = sizeof(struct lb_priv),
  554. .port_priv_size = sizeof(struct lb_port_priv),
  555. .ops = &lb_mode_ops,
  556. };
  557. static int __init lb_init_module(void)
  558. {
  559. return team_mode_register(&lb_mode);
  560. }
  561. static void __exit lb_cleanup_module(void)
  562. {
  563. team_mode_unregister(&lb_mode);
  564. }
  565. module_init(lb_init_module);
  566. module_exit(lb_cleanup_module);
  567. MODULE_LICENSE("GPL v2");
  568. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  569. MODULE_DESCRIPTION("Load-balancing mode for team");
  570. MODULE_ALIAS("team-mode-loadbalance");