team_mode_loadbalance.c 4.2 KB

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