ip_vs_nq.c 3.5 KB

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