ip_vs_nq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * IPVS: Never Queue 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 NQ algorithm adopts a two-speed model. When there is an idle server
  16. * available, the job will be sent to the idle server, instead of waiting
  17. * for a fast one. When there is no idle server available, the job will be
  18. * sent to the server that minimize its expected delay (The Shortest
  19. * Expected Delay scheduling algorithm).
  20. *
  21. * See the following paper for more information:
  22. * A. Weinrib and S. Shenker, Greed is not enough: Adaptive load sharing
  23. * in large heterogeneous systems. In Proceedings IEEE INFOCOM'88,
  24. * pages 986-994, 1988.
  25. *
  26. * Thanks must go to Marko Buuri <marko@buuri.name> for talking NQ to me.
  27. *
  28. * The difference between NQ and SED is that NQ can improve overall
  29. * system utilization.
  30. *
  31. */
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <net/ip_vs.h>
  35. static int
  36. ip_vs_nq_init_svc(struct ip_vs_service *svc)
  37. {
  38. return 0;
  39. }
  40. static int
  41. ip_vs_nq_done_svc(struct ip_vs_service *svc)
  42. {
  43. return 0;
  44. }
  45. static int
  46. ip_vs_nq_update_svc(struct ip_vs_service *svc)
  47. {
  48. return 0;
  49. }
  50. static inline unsigned int
  51. ip_vs_nq_dest_overhead(struct ip_vs_dest *dest)
  52. {
  53. /*
  54. * We only use the active connection number in the cost
  55. * calculation here.
  56. */
  57. return atomic_read(&dest->activeconns) + 1;
  58. }
  59. /*
  60. * Weighted Least Connection scheduling
  61. */
  62. static struct ip_vs_dest *
  63. ip_vs_nq_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  64. {
  65. struct ip_vs_dest *dest, *least = NULL;
  66. unsigned int loh = 0, doh;
  67. IP_VS_DBG(6, "ip_vs_nq_schedule(): Scheduling...\n");
  68. /*
  69. * We calculate the load of each dest server as follows:
  70. * (server expected overhead) / dest->weight
  71. *
  72. * Remember -- no floats in kernel mode!!!
  73. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  74. * h1/w1 > h2/w2
  75. * if every weight is larger than zero.
  76. *
  77. * The server with weight=0 is quiesced and will not receive any
  78. * new connections.
  79. */
  80. list_for_each_entry(dest, &svc->destinations, n_list) {
  81. if (dest->flags & IP_VS_DEST_F_OVERLOAD ||
  82. !atomic_read(&dest->weight))
  83. continue;
  84. doh = ip_vs_nq_dest_overhead(dest);
  85. /* return the server directly if it is idle */
  86. if (atomic_read(&dest->activeconns) == 0) {
  87. least = dest;
  88. loh = doh;
  89. goto out;
  90. }
  91. if (!least ||
  92. (loh * atomic_read(&dest->weight) >
  93. doh * atomic_read(&least->weight))) {
  94. least = dest;
  95. loh = doh;
  96. }
  97. }
  98. if (!least)
  99. return NULL;
  100. out:
  101. IP_VS_DBG(6, "NQ: server %u.%u.%u.%u:%u "
  102. "activeconns %d refcnt %d weight %d overhead %d\n",
  103. NIPQUAD(least->addr), ntohs(least->port),
  104. atomic_read(&least->activeconns),
  105. atomic_read(&least->refcnt),
  106. atomic_read(&least->weight), loh);
  107. return least;
  108. }
  109. static struct ip_vs_scheduler ip_vs_nq_scheduler =
  110. {
  111. .name = "nq",
  112. .refcnt = ATOMIC_INIT(0),
  113. .module = THIS_MODULE,
  114. .init_service = ip_vs_nq_init_svc,
  115. .done_service = ip_vs_nq_done_svc,
  116. .update_service = ip_vs_nq_update_svc,
  117. .schedule = ip_vs_nq_schedule,
  118. };
  119. static int __init ip_vs_nq_init(void)
  120. {
  121. INIT_LIST_HEAD(&ip_vs_nq_scheduler.n_list);
  122. return register_ip_vs_scheduler(&ip_vs_nq_scheduler);
  123. }
  124. static void __exit ip_vs_nq_cleanup(void)
  125. {
  126. unregister_ip_vs_scheduler(&ip_vs_nq_scheduler);
  127. }
  128. module_init(ip_vs_nq_init);
  129. module_exit(ip_vs_nq_cleanup);
  130. MODULE_LICENSE("GPL");