/net/netfilter/ipvs/ip_vs_sed.c

http://github.com/mirrors/linux · C · 139 lines · 67 code · 20 blank · 52 comment · 3 complexity · 2953815bcb01b5bef66f05cceae72f72 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPVS: Shortest Expected Delay scheduling module
  4. *
  5. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  6. *
  7. * Changes:
  8. */
  9. /*
  10. * The SED algorithm attempts to minimize each job's expected delay until
  11. * completion. The expected delay that the job will experience is
  12. * (Ci + 1) / Ui if sent to the ith server, in which Ci is the number of
  13. * jobs on the ith server and Ui is the fixed service rate (weight) of
  14. * the ith server. The SED algorithm adopts a greedy policy that each does
  15. * what is in its own best interest, i.e. to join the queue which would
  16. * minimize its expected delay of completion.
  17. *
  18. * See the following paper for more information:
  19. * A. Weinrib and S. Shenker, Greed is not enough: Adaptive load sharing
  20. * in large heterogeneous systems. In Proceedings IEEE INFOCOM'88,
  21. * pages 986-994, 1988.
  22. *
  23. * Thanks must go to Marko Buuri <marko@buuri.name> for talking SED to me.
  24. *
  25. * The difference between SED and WLC is that SED includes the incoming
  26. * job in the cost function (the increment of 1). SED may outperform
  27. * WLC, while scheduling big jobs under larger heterogeneous systems
  28. * (the server weight varies a lot).
  29. *
  30. */
  31. #define KMSG_COMPONENT "IPVS"
  32. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <net/ip_vs.h>
  36. static inline int
  37. ip_vs_sed_dest_overhead(struct ip_vs_dest *dest)
  38. {
  39. /*
  40. * We only use the active connection number in the cost
  41. * calculation here.
  42. */
  43. return atomic_read(&dest->activeconns) + 1;
  44. }
  45. /*
  46. * Weighted Least Connection scheduling
  47. */
  48. static struct ip_vs_dest *
  49. ip_vs_sed_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
  50. struct ip_vs_iphdr *iph)
  51. {
  52. struct ip_vs_dest *dest, *least;
  53. int loh, doh;
  54. IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
  55. /*
  56. * We calculate the load of each dest server as follows:
  57. * (server expected overhead) / dest->weight
  58. *
  59. * Remember -- no floats in kernel mode!!!
  60. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  61. * h1/w1 > h2/w2
  62. * if every weight is larger than zero.
  63. *
  64. * The server with weight=0 is quiesced and will not receive any
  65. * new connections.
  66. */
  67. list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
  68. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  69. atomic_read(&dest->weight) > 0) {
  70. least = dest;
  71. loh = ip_vs_sed_dest_overhead(least);
  72. goto nextstage;
  73. }
  74. }
  75. ip_vs_scheduler_err(svc, "no destination available");
  76. return NULL;
  77. /*
  78. * Find the destination with the least load.
  79. */
  80. nextstage:
  81. list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
  82. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  83. continue;
  84. doh = ip_vs_sed_dest_overhead(dest);
  85. if ((__s64)loh * atomic_read(&dest->weight) >
  86. (__s64)doh * atomic_read(&least->weight)) {
  87. least = dest;
  88. loh = doh;
  89. }
  90. }
  91. IP_VS_DBG_BUF(6, "SED: server %s:%u "
  92. "activeconns %d refcnt %d weight %d overhead %d\n",
  93. IP_VS_DBG_ADDR(least->af, &least->addr),
  94. ntohs(least->port),
  95. atomic_read(&least->activeconns),
  96. refcount_read(&least->refcnt),
  97. atomic_read(&least->weight), loh);
  98. return least;
  99. }
  100. static struct ip_vs_scheduler ip_vs_sed_scheduler =
  101. {
  102. .name = "sed",
  103. .refcnt = ATOMIC_INIT(0),
  104. .module = THIS_MODULE,
  105. .n_list = LIST_HEAD_INIT(ip_vs_sed_scheduler.n_list),
  106. .schedule = ip_vs_sed_schedule,
  107. };
  108. static int __init ip_vs_sed_init(void)
  109. {
  110. return register_ip_vs_scheduler(&ip_vs_sed_scheduler);
  111. }
  112. static void __exit ip_vs_sed_cleanup(void)
  113. {
  114. unregister_ip_vs_scheduler(&ip_vs_sed_scheduler);
  115. synchronize_rcu();
  116. }
  117. module_init(ip_vs_sed_init);
  118. module_exit(ip_vs_sed_cleanup);
  119. MODULE_LICENSE("GPL");