ip_vs_nq.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * IPVS: Never Queue scheduling module
  3. *
  4. * Version: $Id: ip_vs_nq.c,v 1.2 2003/06/08 09:31:19 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Changes:
  14. *
  15. */
  16. /*
  17. * The NQ algorithm adopts a two-speed model. When there is an idle server
  18. * available, the job will be sent to the idle server, instead of waiting
  19. * for a fast one. When there is no idle server available, the job will be
  20. * sent to the server that minimize its expected delay (The Shortest
  21. * Expected Delay scheduling algorithm).
  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 NQ to me.
  29. *
  30. * The difference between NQ and SED is that NQ can improve overall
  31. * system utilization.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <net/ip_vs.h>
  37. static int
  38. ip_vs_nq_init_svc(struct ip_vs_service *svc)
  39. {
  40. return 0;
  41. }
  42. static int
  43. ip_vs_nq_done_svc(struct ip_vs_service *svc)
  44. {
  45. return 0;
  46. }
  47. static int
  48. ip_vs_nq_update_svc(struct ip_vs_service *svc)
  49. {
  50. return 0;
  51. }
  52. static inline unsigned int
  53. ip_vs_nq_dest_overhead(struct ip_vs_dest *dest)
  54. {
  55. /*
  56. * We only use the active connection number in the cost
  57. * calculation here.
  58. */
  59. return atomic_read(&dest->activeconns) + 1;
  60. }
  61. /*
  62. * Weighted Least Connection scheduling
  63. */
  64. static struct ip_vs_dest *
  65. ip_vs_nq_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  66. {
  67. struct ip_vs_dest *dest, *least = NULL;
  68. unsigned int loh = 0, doh;
  69. IP_VS_DBG(6, "ip_vs_nq_schedule(): Scheduling...\n");
  70. /*
  71. * We calculate the load of each dest server as follows:
  72. * (server expected overhead) / dest->weight
  73. *
  74. * Remember -- no floats in kernel mode!!!
  75. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  76. * h1/w1 > h2/w2
  77. * if every weight is larger than zero.
  78. *
  79. * The server with weight=0 is quiesced and will not receive any
  80. * new connections.
  81. */
  82. list_for_each_entry(dest, &svc->destinations, n_list) {
  83. if (dest->flags & IP_VS_DEST_F_OVERLOAD ||
  84. !atomic_read(&dest->weight))
  85. continue;
  86. doh = ip_vs_nq_dest_overhead(dest);
  87. /* return the server directly if it is idle */
  88. if (atomic_read(&dest->activeconns) == 0) {
  89. least = dest;
  90. loh = doh;
  91. goto out;
  92. }
  93. if (!least ||
  94. (loh * atomic_read(&dest->weight) >
  95. doh * atomic_read(&least->weight))) {
  96. least = dest;
  97. loh = doh;
  98. }
  99. }
  100. if (!least)
  101. return NULL;
  102. out:
  103. IP_VS_DBG(6, "NQ: server %u.%u.%u.%u:%u "
  104. "activeconns %d refcnt %d weight %d overhead %d\n",
  105. NIPQUAD(least->addr), ntohs(least->port),
  106. atomic_read(&least->activeconns),
  107. atomic_read(&least->refcnt),
  108. atomic_read(&least->weight), loh);
  109. return least;
  110. }
  111. static struct ip_vs_scheduler ip_vs_nq_scheduler =
  112. {
  113. .name = "nq",
  114. .refcnt = ATOMIC_INIT(0),
  115. .module = THIS_MODULE,
  116. .init_service = ip_vs_nq_init_svc,
  117. .done_service = ip_vs_nq_done_svc,
  118. .update_service = ip_vs_nq_update_svc,
  119. .schedule = ip_vs_nq_schedule,
  120. };
  121. static int __init ip_vs_nq_init(void)
  122. {
  123. INIT_LIST_HEAD(&ip_vs_nq_scheduler.n_list);
  124. return register_ip_vs_scheduler(&ip_vs_nq_scheduler);
  125. }
  126. static void __exit ip_vs_nq_cleanup(void)
  127. {
  128. unregister_ip_vs_scheduler(&ip_vs_nq_scheduler);
  129. }
  130. module_init(ip_vs_nq_init);
  131. module_exit(ip_vs_nq_cleanup);
  132. MODULE_LICENSE("GPL");