ip_vs_wlc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * IPVS: Weighted Least-Connection Scheduling module
  3. *
  4. * Version: $Id: ip_vs_wlc.c,v 1.13 2003/04/18 09:03:16 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. * Peter Kese <peter.kese@ijs.si>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. * Wensong Zhang : changed the ip_vs_wlc_schedule to return dest
  16. * Wensong Zhang : changed to use the inactconns in scheduling
  17. * Wensong Zhang : changed some comestics things for debugging
  18. * Wensong Zhang : changed for the d-linked destination list
  19. * Wensong Zhang : added the ip_vs_wlc_update_svc
  20. * Wensong Zhang : added any dest with weight=0 is quiesced
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <net/ip_vs.h>
  26. static int
  27. ip_vs_wlc_init_svc(struct ip_vs_service *svc)
  28. {
  29. return 0;
  30. }
  31. static int
  32. ip_vs_wlc_done_svc(struct ip_vs_service *svc)
  33. {
  34. return 0;
  35. }
  36. static int
  37. ip_vs_wlc_update_svc(struct ip_vs_service *svc)
  38. {
  39. return 0;
  40. }
  41. static inline unsigned int
  42. ip_vs_wlc_dest_overhead(struct ip_vs_dest *dest)
  43. {
  44. /*
  45. * We think the overhead of processing active connections is 256
  46. * times higher than that of inactive connections in average. (This
  47. * 256 times might not be accurate, we will change it later) We
  48. * use the following formula to estimate the overhead now:
  49. * dest->activeconns*256 + dest->inactconns
  50. */
  51. return (atomic_read(&dest->activeconns) << 8) +
  52. atomic_read(&dest->inactconns);
  53. }
  54. /*
  55. * Weighted Least Connection scheduling
  56. */
  57. static struct ip_vs_dest *
  58. ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  59. {
  60. struct ip_vs_dest *dest, *least;
  61. unsigned int loh, doh;
  62. IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
  63. /*
  64. * We calculate the load of each dest server as follows:
  65. * (dest overhead) / dest->weight
  66. *
  67. * Remember -- no floats in kernel mode!!!
  68. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  69. * h1/w1 > h2/w2
  70. * if every weight is larger than zero.
  71. *
  72. * The server with weight=0 is quiesced and will not receive any
  73. * new connections.
  74. */
  75. list_for_each_entry(dest, &svc->destinations, n_list) {
  76. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  77. atomic_read(&dest->weight) > 0) {
  78. least = dest;
  79. loh = ip_vs_wlc_dest_overhead(least);
  80. goto nextstage;
  81. }
  82. }
  83. return NULL;
  84. /*
  85. * Find the destination with the least load.
  86. */
  87. nextstage:
  88. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  89. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  90. continue;
  91. doh = ip_vs_wlc_dest_overhead(dest);
  92. if (loh * atomic_read(&dest->weight) >
  93. doh * atomic_read(&least->weight)) {
  94. least = dest;
  95. loh = doh;
  96. }
  97. }
  98. IP_VS_DBG(6, "WLC: server %u.%u.%u.%u:%u "
  99. "activeconns %d refcnt %d weight %d overhead %d\n",
  100. NIPQUAD(least->addr), ntohs(least->port),
  101. atomic_read(&least->activeconns),
  102. atomic_read(&least->refcnt),
  103. atomic_read(&least->weight), loh);
  104. return least;
  105. }
  106. static struct ip_vs_scheduler ip_vs_wlc_scheduler =
  107. {
  108. .name = "wlc",
  109. .refcnt = ATOMIC_INIT(0),
  110. .module = THIS_MODULE,
  111. .init_service = ip_vs_wlc_init_svc,
  112. .done_service = ip_vs_wlc_done_svc,
  113. .update_service = ip_vs_wlc_update_svc,
  114. .schedule = ip_vs_wlc_schedule,
  115. };
  116. static int __init ip_vs_wlc_init(void)
  117. {
  118. INIT_LIST_HEAD(&ip_vs_wlc_scheduler.n_list);
  119. return register_ip_vs_scheduler(&ip_vs_wlc_scheduler);
  120. }
  121. static void __exit ip_vs_wlc_cleanup(void)
  122. {
  123. unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler);
  124. }
  125. module_init(ip_vs_wlc_init);
  126. module_exit(ip_vs_wlc_cleanup);
  127. MODULE_LICENSE("GPL");