team_mode_loadbalance.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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_kern *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 bpf_prog __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 bpf_prog *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 = BPF_PROG_RUN(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_kern **pfprog, u32 data_len,
  206. const void *data)
  207. {
  208. struct sock_fprog_kern *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(*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_kern *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 bpf_prog *fp = NULL;
  233. struct bpf_prog *orig_fp = NULL;
  234. struct sock_fprog_kern *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 = bpf_prog_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. }
  253. rcu_assign_pointer(lb_priv->fp, fp);
  254. lb_priv->ex->orig_fprog = fprog;
  255. if (orig_fp) {
  256. synchronize_rcu();
  257. bpf_prog_destroy(orig_fp);
  258. }
  259. return 0;
  260. }
  261. static int lb_tx_method_get(struct team *team, struct team_gsetter_ctx *ctx)
  262. {
  263. struct lb_priv *lb_priv = get_lb_priv(team);
  264. lb_select_tx_port_func_t *func;
  265. char *name;
  266. func = rcu_dereference_protected(lb_priv->select_tx_port_func,
  267. lockdep_is_held(&team->lock));
  268. name = lb_select_tx_port_get_name(func);
  269. BUG_ON(!name);
  270. ctx->data.str_val = name;
  271. return 0;
  272. }
  273. static int lb_tx_method_set(struct team *team, struct team_gsetter_ctx *ctx)
  274. {
  275. struct lb_priv *lb_priv = get_lb_priv(team);
  276. lb_select_tx_port_func_t *func;
  277. func = lb_select_tx_port_get_func(ctx->data.str_val);
  278. if (!func)
  279. return -EINVAL;
  280. rcu_assign_pointer(lb_priv->select_tx_port_func, func);
  281. return 0;
  282. }
  283. static int lb_tx_hash_to_port_mapping_init(struct team *team,
  284. struct team_option_inst_info *info)
  285. {
  286. struct lb_priv *lb_priv = get_lb_priv(team);
  287. unsigned char hash = info->array_index;
  288. LB_HTPM_OPT_INST_INFO_BY_HASH(lb_priv, hash) = info;
  289. return 0;
  290. }
  291. static int lb_tx_hash_to_port_mapping_get(struct team *team,
  292. struct team_gsetter_ctx *ctx)
  293. {
  294. struct lb_priv *lb_priv = get_lb_priv(team);
  295. struct team_port *port;
  296. unsigned char hash = ctx->info->array_index;
  297. port = LB_HTPM_PORT_BY_HASH(lb_priv, hash);
  298. ctx->data.u32_val = port ? port->dev->ifindex : 0;
  299. return 0;
  300. }
  301. static int lb_tx_hash_to_port_mapping_set(struct team *team,
  302. struct team_gsetter_ctx *ctx)
  303. {
  304. struct lb_priv *lb_priv = get_lb_priv(team);
  305. struct team_port *port;
  306. unsigned char hash = ctx->info->array_index;
  307. list_for_each_entry(port, &team->port_list, list) {
  308. if (ctx->data.u32_val == port->dev->ifindex &&
  309. team_port_enabled(port)) {
  310. rcu_assign_pointer(LB_HTPM_PORT_BY_HASH(lb_priv, hash),
  311. port);
  312. return 0;
  313. }
  314. }
  315. return -ENODEV;
  316. }
  317. static int lb_hash_stats_init(struct team *team,
  318. struct team_option_inst_info *info)
  319. {
  320. struct lb_priv *lb_priv = get_lb_priv(team);
  321. unsigned char hash = info->array_index;
  322. lb_priv->ex->stats.info[hash].opt_inst_info = info;
  323. return 0;
  324. }
  325. static int lb_hash_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
  326. {
  327. struct lb_priv *lb_priv = get_lb_priv(team);
  328. unsigned char hash = ctx->info->array_index;
  329. ctx->data.bin_val.ptr = &lb_priv->ex->stats.info[hash].stats;
  330. ctx->data.bin_val.len = sizeof(struct lb_stats);
  331. return 0;
  332. }
  333. static int lb_port_stats_init(struct team *team,
  334. struct team_option_inst_info *info)
  335. {
  336. struct team_port *port = info->port;
  337. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  338. lb_port_priv->stats_info.opt_inst_info = info;
  339. return 0;
  340. }
  341. static int lb_port_stats_get(struct team *team, struct team_gsetter_ctx *ctx)
  342. {
  343. struct team_port *port = ctx->info->port;
  344. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  345. ctx->data.bin_val.ptr = &lb_port_priv->stats_info.stats;
  346. ctx->data.bin_val.len = sizeof(struct lb_stats);
  347. return 0;
  348. }
  349. static void __lb_stats_info_refresh_prepare(struct lb_stats_info *s_info)
  350. {
  351. memcpy(&s_info->last_stats, &s_info->stats, sizeof(struct lb_stats));
  352. memset(&s_info->stats, 0, sizeof(struct lb_stats));
  353. }
  354. static bool __lb_stats_info_refresh_check(struct lb_stats_info *s_info,
  355. struct team *team)
  356. {
  357. if (memcmp(&s_info->last_stats, &s_info->stats,
  358. sizeof(struct lb_stats))) {
  359. team_option_inst_set_change(s_info->opt_inst_info);
  360. return true;
  361. }
  362. return false;
  363. }
  364. static void __lb_one_cpu_stats_add(struct lb_stats *acc_stats,
  365. struct lb_stats *cpu_stats,
  366. struct u64_stats_sync *syncp)
  367. {
  368. unsigned int start;
  369. struct lb_stats tmp;
  370. do {
  371. start = u64_stats_fetch_begin_irq(syncp);
  372. tmp.tx_bytes = cpu_stats->tx_bytes;
  373. } while (u64_stats_fetch_retry_irq(syncp, start));
  374. acc_stats->tx_bytes += tmp.tx_bytes;
  375. }
  376. static void lb_stats_refresh(struct work_struct *work)
  377. {
  378. struct team *team;
  379. struct lb_priv *lb_priv;
  380. struct lb_priv_ex *lb_priv_ex;
  381. struct lb_pcpu_stats *pcpu_stats;
  382. struct lb_stats *stats;
  383. struct lb_stats_info *s_info;
  384. struct team_port *port;
  385. bool changed = false;
  386. int i;
  387. int j;
  388. lb_priv_ex = container_of(work, struct lb_priv_ex,
  389. stats.refresh_dw.work);
  390. team = lb_priv_ex->team;
  391. lb_priv = get_lb_priv(team);
  392. if (!mutex_trylock(&team->lock)) {
  393. schedule_delayed_work(&lb_priv_ex->stats.refresh_dw, 0);
  394. return;
  395. }
  396. for (j = 0; j < LB_TX_HASHTABLE_SIZE; j++) {
  397. s_info = &lb_priv->ex->stats.info[j];
  398. __lb_stats_info_refresh_prepare(s_info);
  399. for_each_possible_cpu(i) {
  400. pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
  401. stats = &pcpu_stats->hash_stats[j];
  402. __lb_one_cpu_stats_add(&s_info->stats, stats,
  403. &pcpu_stats->syncp);
  404. }
  405. changed |= __lb_stats_info_refresh_check(s_info, team);
  406. }
  407. list_for_each_entry(port, &team->port_list, list) {
  408. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  409. s_info = &lb_port_priv->stats_info;
  410. __lb_stats_info_refresh_prepare(s_info);
  411. for_each_possible_cpu(i) {
  412. pcpu_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
  413. stats = per_cpu_ptr(lb_port_priv->pcpu_stats, i);
  414. __lb_one_cpu_stats_add(&s_info->stats, stats,
  415. &pcpu_stats->syncp);
  416. }
  417. changed |= __lb_stats_info_refresh_check(s_info, team);
  418. }
  419. if (changed)
  420. team_options_change_check(team);
  421. schedule_delayed_work(&lb_priv_ex->stats.refresh_dw,
  422. (lb_priv_ex->stats.refresh_interval * HZ) / 10);
  423. mutex_unlock(&team->lock);
  424. }
  425. static int lb_stats_refresh_interval_get(struct team *team,
  426. struct team_gsetter_ctx *ctx)
  427. {
  428. struct lb_priv *lb_priv = get_lb_priv(team);
  429. ctx->data.u32_val = lb_priv->ex->stats.refresh_interval;
  430. return 0;
  431. }
  432. static int lb_stats_refresh_interval_set(struct team *team,
  433. struct team_gsetter_ctx *ctx)
  434. {
  435. struct lb_priv *lb_priv = get_lb_priv(team);
  436. unsigned int interval;
  437. interval = ctx->data.u32_val;
  438. if (lb_priv->ex->stats.refresh_interval == interval)
  439. return 0;
  440. lb_priv->ex->stats.refresh_interval = interval;
  441. if (interval)
  442. schedule_delayed_work(&lb_priv->ex->stats.refresh_dw, 0);
  443. else
  444. cancel_delayed_work(&lb_priv->ex->stats.refresh_dw);
  445. return 0;
  446. }
  447. static const struct team_option lb_options[] = {
  448. {
  449. .name = "bpf_hash_func",
  450. .type = TEAM_OPTION_TYPE_BINARY,
  451. .getter = lb_bpf_func_get,
  452. .setter = lb_bpf_func_set,
  453. },
  454. {
  455. .name = "lb_tx_method",
  456. .type = TEAM_OPTION_TYPE_STRING,
  457. .getter = lb_tx_method_get,
  458. .setter = lb_tx_method_set,
  459. },
  460. {
  461. .name = "lb_tx_hash_to_port_mapping",
  462. .array_size = LB_TX_HASHTABLE_SIZE,
  463. .type = TEAM_OPTION_TYPE_U32,
  464. .init = lb_tx_hash_to_port_mapping_init,
  465. .getter = lb_tx_hash_to_port_mapping_get,
  466. .setter = lb_tx_hash_to_port_mapping_set,
  467. },
  468. {
  469. .name = "lb_hash_stats",
  470. .array_size = LB_TX_HASHTABLE_SIZE,
  471. .type = TEAM_OPTION_TYPE_BINARY,
  472. .init = lb_hash_stats_init,
  473. .getter = lb_hash_stats_get,
  474. },
  475. {
  476. .name = "lb_port_stats",
  477. .per_port = true,
  478. .type = TEAM_OPTION_TYPE_BINARY,
  479. .init = lb_port_stats_init,
  480. .getter = lb_port_stats_get,
  481. },
  482. {
  483. .name = "lb_stats_refresh_interval",
  484. .type = TEAM_OPTION_TYPE_U32,
  485. .getter = lb_stats_refresh_interval_get,
  486. .setter = lb_stats_refresh_interval_set,
  487. },
  488. };
  489. static int lb_init(struct team *team)
  490. {
  491. struct lb_priv *lb_priv = get_lb_priv(team);
  492. lb_select_tx_port_func_t *func;
  493. int i, err;
  494. /* set default tx port selector */
  495. func = lb_select_tx_port_get_func("hash");
  496. BUG_ON(!func);
  497. rcu_assign_pointer(lb_priv->select_tx_port_func, func);
  498. lb_priv->ex = kzalloc(sizeof(*lb_priv->ex), GFP_KERNEL);
  499. if (!lb_priv->ex)
  500. return -ENOMEM;
  501. lb_priv->ex->team = team;
  502. lb_priv->pcpu_stats = alloc_percpu(struct lb_pcpu_stats);
  503. if (!lb_priv->pcpu_stats) {
  504. err = -ENOMEM;
  505. goto err_alloc_pcpu_stats;
  506. }
  507. for_each_possible_cpu(i) {
  508. struct lb_pcpu_stats *team_lb_stats;
  509. team_lb_stats = per_cpu_ptr(lb_priv->pcpu_stats, i);
  510. u64_stats_init(&team_lb_stats->syncp);
  511. }
  512. INIT_DELAYED_WORK(&lb_priv->ex->stats.refresh_dw, lb_stats_refresh);
  513. err = team_options_register(team, lb_options, ARRAY_SIZE(lb_options));
  514. if (err)
  515. goto err_options_register;
  516. return 0;
  517. err_options_register:
  518. free_percpu(lb_priv->pcpu_stats);
  519. err_alloc_pcpu_stats:
  520. kfree(lb_priv->ex);
  521. return err;
  522. }
  523. static void lb_exit(struct team *team)
  524. {
  525. struct lb_priv *lb_priv = get_lb_priv(team);
  526. team_options_unregister(team, lb_options,
  527. ARRAY_SIZE(lb_options));
  528. cancel_delayed_work_sync(&lb_priv->ex->stats.refresh_dw);
  529. free_percpu(lb_priv->pcpu_stats);
  530. kfree(lb_priv->ex);
  531. }
  532. static int lb_port_enter(struct team *team, struct team_port *port)
  533. {
  534. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  535. lb_port_priv->pcpu_stats = alloc_percpu(struct lb_stats);
  536. if (!lb_port_priv->pcpu_stats)
  537. return -ENOMEM;
  538. return 0;
  539. }
  540. static void lb_port_leave(struct team *team, struct team_port *port)
  541. {
  542. struct lb_port_priv *lb_port_priv = get_lb_port_priv(port);
  543. free_percpu(lb_port_priv->pcpu_stats);
  544. }
  545. static void lb_port_disabled(struct team *team, struct team_port *port)
  546. {
  547. lb_tx_hash_to_port_mapping_null_port(team, port);
  548. }
  549. static const struct team_mode_ops lb_mode_ops = {
  550. .init = lb_init,
  551. .exit = lb_exit,
  552. .port_enter = lb_port_enter,
  553. .port_leave = lb_port_leave,
  554. .port_disabled = lb_port_disabled,
  555. .transmit = lb_transmit,
  556. };
  557. static const struct team_mode lb_mode = {
  558. .kind = "loadbalance",
  559. .owner = THIS_MODULE,
  560. .priv_size = sizeof(struct lb_priv),
  561. .port_priv_size = sizeof(struct lb_port_priv),
  562. .ops = &lb_mode_ops,
  563. };
  564. static int __init lb_init_module(void)
  565. {
  566. return team_mode_register(&lb_mode);
  567. }
  568. static void __exit lb_cleanup_module(void)
  569. {
  570. team_mode_unregister(&lb_mode);
  571. }
  572. module_init(lb_init_module);
  573. module_exit(lb_cleanup_module);
  574. MODULE_LICENSE("GPL v2");
  575. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  576. MODULE_DESCRIPTION("Load-balancing mode for team");
  577. MODULE_ALIAS("team-mode-loadbalance");