team_mode_loadbalance.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/filter.h>
  17. #include <linux/if_team.h>
  18. struct lb_priv {
  19. struct sk_filter __rcu *fp;
  20. struct sock_fprog *orig_fprog;
  21. };
  22. static struct lb_priv *lb_priv(struct team *team)
  23. {
  24. return (struct lb_priv *) &team->mode_priv;
  25. }
  26. static unsigned char lb_get_skb_hash(struct lb_priv *lb_priv,
  27. struct sk_buff *skb)
  28. {
  29. struct sk_filter *fp;
  30. uint32_t lhash;
  31. unsigned char *c;
  32. fp = rcu_dereference(lb_priv->fp);
  33. if (unlikely(!fp))
  34. return 0;
  35. lhash = SK_RUN_FILTER(fp, skb);
  36. c = (char *) &lhash;
  37. return c[0] ^ c[1] ^ c[2] ^ c[3];
  38. }
  39. static bool lb_transmit(struct team *team, struct sk_buff *skb)
  40. {
  41. struct team_port *port;
  42. int port_index;
  43. port_index = lb_get_skb_hash(lb_priv(team), skb) % team->en_port_count;
  44. port = team_get_port_by_index_rcu(team, port_index);
  45. if (unlikely(!port))
  46. goto drop;
  47. skb->dev = port->dev;
  48. if (dev_queue_xmit(skb))
  49. return false;
  50. return true;
  51. drop:
  52. dev_kfree_skb_any(skb);
  53. return false;
  54. }
  55. static int lb_bpf_func_get(struct team *team, struct team_gsetter_ctx *ctx)
  56. {
  57. if (!lb_priv(team)->orig_fprog) {
  58. ctx->data.bin_val.len = 0;
  59. ctx->data.bin_val.ptr = NULL;
  60. return 0;
  61. }
  62. ctx->data.bin_val.len = lb_priv(team)->orig_fprog->len *
  63. sizeof(struct sock_filter);
  64. ctx->data.bin_val.ptr = lb_priv(team)->orig_fprog->filter;
  65. return 0;
  66. }
  67. static int __fprog_create(struct sock_fprog **pfprog, u32 data_len,
  68. const void *data)
  69. {
  70. struct sock_fprog *fprog;
  71. struct sock_filter *filter = (struct sock_filter *) data;
  72. if (data_len % sizeof(struct sock_filter))
  73. return -EINVAL;
  74. fprog = kmalloc(sizeof(struct sock_fprog), GFP_KERNEL);
  75. if (!fprog)
  76. return -ENOMEM;
  77. fprog->filter = kmemdup(filter, data_len, GFP_KERNEL);
  78. if (!fprog->filter) {
  79. kfree(fprog);
  80. return -ENOMEM;
  81. }
  82. fprog->len = data_len / sizeof(struct sock_filter);
  83. *pfprog = fprog;
  84. return 0;
  85. }
  86. static void __fprog_destroy(struct sock_fprog *fprog)
  87. {
  88. kfree(fprog->filter);
  89. kfree(fprog);
  90. }
  91. static int lb_bpf_func_set(struct team *team, struct team_gsetter_ctx *ctx)
  92. {
  93. struct sk_filter *fp = NULL;
  94. struct sock_fprog *fprog = NULL;
  95. int err;
  96. if (ctx->data.bin_val.len) {
  97. err = __fprog_create(&fprog, ctx->data.bin_val.len,
  98. ctx->data.bin_val.ptr);
  99. if (err)
  100. return err;
  101. err = sk_unattached_filter_create(&fp, fprog);
  102. if (err) {
  103. __fprog_destroy(fprog);
  104. return err;
  105. }
  106. }
  107. if (lb_priv(team)->orig_fprog) {
  108. /* Clear old filter data */
  109. __fprog_destroy(lb_priv(team)->orig_fprog);
  110. sk_unattached_filter_destroy(lb_priv(team)->fp);
  111. }
  112. rcu_assign_pointer(lb_priv(team)->fp, fp);
  113. lb_priv(team)->orig_fprog = fprog;
  114. return 0;
  115. }
  116. static const struct team_option lb_options[] = {
  117. {
  118. .name = "bpf_hash_func",
  119. .type = TEAM_OPTION_TYPE_BINARY,
  120. .getter = lb_bpf_func_get,
  121. .setter = lb_bpf_func_set,
  122. },
  123. };
  124. static int lb_init(struct team *team)
  125. {
  126. return team_options_register(team, lb_options,
  127. ARRAY_SIZE(lb_options));
  128. }
  129. static void lb_exit(struct team *team)
  130. {
  131. team_options_unregister(team, lb_options,
  132. ARRAY_SIZE(lb_options));
  133. }
  134. static const struct team_mode_ops lb_mode_ops = {
  135. .init = lb_init,
  136. .exit = lb_exit,
  137. .transmit = lb_transmit,
  138. };
  139. static const struct team_mode lb_mode = {
  140. .kind = "loadbalance",
  141. .owner = THIS_MODULE,
  142. .priv_size = sizeof(struct lb_priv),
  143. .ops = &lb_mode_ops,
  144. };
  145. static int __init lb_init_module(void)
  146. {
  147. return team_mode_register(&lb_mode);
  148. }
  149. static void __exit lb_cleanup_module(void)
  150. {
  151. team_mode_unregister(&lb_mode);
  152. }
  153. module_init(lb_init_module);
  154. module_exit(lb_cleanup_module);
  155. MODULE_LICENSE("GPL v2");
  156. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  157. MODULE_DESCRIPTION("Load-balancing mode for team");
  158. MODULE_ALIAS("team-mode-loadbalance");