team_mode_loadbalance.c 18 KB

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