team_mode_loadbalance.c 17 KB

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