team_mode_loadbalance.c 18 KB

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