ip_vs_sed.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <linux/module.h>
  37. #include <linux/kernel.h>
  38. #include <net/ip_vs.h>
  39. static inline unsigned int
  40. ip_vs_sed_dest_overhead(struct ip_vs_dest *dest)
  41. {
  42. /*
  43. * We only use the active connection number in the cost
  44. * calculation here.
  45. */
  46. return atomic_read(&dest->activeconns) + 1;
  47. }
  48. /*
  49. * Weighted Least Connection scheduling
  50. */
  51. static struct ip_vs_dest *
  52. ip_vs_sed_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  53. {
  54. struct ip_vs_dest *dest, *least;
  55. unsigned int loh, doh;
  56. IP_VS_DBG(6, "ip_vs_sed_schedule(): Scheduling...\n");
  57. /*
  58. * We calculate the load of each dest server as follows:
  59. * (server expected overhead) / dest->weight
  60. *
  61. * Remember -- no floats in kernel mode!!!
  62. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  63. * h1/w1 > h2/w2
  64. * if every weight is larger than zero.
  65. *
  66. * The server with weight=0 is quiesced and will not receive any
  67. * new connections.
  68. */
  69. list_for_each_entry(dest, &svc->destinations, n_list) {
  70. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  71. atomic_read(&dest->weight) > 0) {
  72. least = dest;
  73. loh = ip_vs_sed_dest_overhead(least);
  74. goto nextstage;
  75. }
  76. }
  77. return NULL;
  78. /*
  79. * Find the destination with the least load.
  80. */
  81. nextstage:
  82. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  83. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  84. continue;
  85. doh = ip_vs_sed_dest_overhead(dest);
  86. if (loh * atomic_read(&dest->weight) >
  87. doh * atomic_read(&least->weight)) {
  88. least = dest;
  89. loh = doh;
  90. }
  91. }
  92. IP_VS_DBG(6, "SED: server %u.%u.%u.%u:%u "
  93. "activeconns %d refcnt %d weight %d overhead %d\n",
  94. NIPQUAD(least->addr.ip), ntohs(least->port),
  95. atomic_read(&least->activeconns),
  96. atomic_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. }
  116. module_init(ip_vs_sed_init);
  117. module_exit(ip_vs_sed_cleanup);
  118. MODULE_LICENSE("GPL");