ip_vs_nq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 inline unsigned int
  36. ip_vs_nq_dest_overhead(struct ip_vs_dest *dest)
  37. {
  38. /*
  39. * We only use the active connection number in the cost
  40. * calculation here.
  41. */
  42. return atomic_read(&dest->activeconns) + 1;
  43. }
  44. /*
  45. * Weighted Least Connection scheduling
  46. */
  47. static struct ip_vs_dest *
  48. ip_vs_nq_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  49. {
  50. struct ip_vs_dest *dest, *least = NULL;
  51. unsigned int loh = 0, doh;
  52. IP_VS_DBG(6, "ip_vs_nq_schedule(): Scheduling...\n");
  53. /*
  54. * We calculate the load of each dest server as follows:
  55. * (server expected overhead) / dest->weight
  56. *
  57. * Remember -- no floats in kernel mode!!!
  58. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  59. * h1/w1 > h2/w2
  60. * if every weight is larger than zero.
  61. *
  62. * The server with weight=0 is quiesced and will not receive any
  63. * new connections.
  64. */
  65. list_for_each_entry(dest, &svc->destinations, n_list) {
  66. if (dest->flags & IP_VS_DEST_F_OVERLOAD ||
  67. !atomic_read(&dest->weight))
  68. continue;
  69. doh = ip_vs_nq_dest_overhead(dest);
  70. /* return the server directly if it is idle */
  71. if (atomic_read(&dest->activeconns) == 0) {
  72. least = dest;
  73. loh = doh;
  74. goto out;
  75. }
  76. if (!least ||
  77. (loh * atomic_read(&dest->weight) >
  78. doh * atomic_read(&least->weight))) {
  79. least = dest;
  80. loh = doh;
  81. }
  82. }
  83. if (!least)
  84. return NULL;
  85. out:
  86. IP_VS_DBG(6, "NQ: server %u.%u.%u.%u:%u "
  87. "activeconns %d refcnt %d weight %d overhead %d\n",
  88. NIPQUAD(least->addr.ip), ntohs(least->port),
  89. atomic_read(&least->activeconns),
  90. atomic_read(&least->refcnt),
  91. atomic_read(&least->weight), loh);
  92. return least;
  93. }
  94. static struct ip_vs_scheduler ip_vs_nq_scheduler =
  95. {
  96. .name = "nq",
  97. .refcnt = ATOMIC_INIT(0),
  98. .module = THIS_MODULE,
  99. .n_list = LIST_HEAD_INIT(ip_vs_nq_scheduler.n_list),
  100. .schedule = ip_vs_nq_schedule,
  101. };
  102. static int __init ip_vs_nq_init(void)
  103. {
  104. return register_ip_vs_scheduler(&ip_vs_nq_scheduler);
  105. }
  106. static void __exit ip_vs_nq_cleanup(void)
  107. {
  108. unregister_ip_vs_scheduler(&ip_vs_nq_scheduler);
  109. }
  110. module_init(ip_vs_nq_init);
  111. module_exit(ip_vs_nq_cleanup);
  112. MODULE_LICENSE("GPL");