team_mode_loadbalance.c 18 KB

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