team_mode_loadbalance.c 17 KB

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