team_mode_loadbalance.c 17 KB

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