ip_vs_sed.c 3.9 KB

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