ip_vs_wlc.c 3.8 KB

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