ip_vs_wlc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #define KMSG_COMPONENT "IPVS"
  22. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <net/ip_vs.h>
  26. /*
  27. * Weighted Least Connection scheduling
  28. */
  29. static struct ip_vs_dest *
  30. ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
  31. struct ip_vs_iphdr *iph)
  32. {
  33. struct ip_vs_dest *dest, *least;
  34. int loh, doh;
  35. IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
  36. /*
  37. * We calculate the load of each dest server as follows:
  38. * (dest overhead) / dest->weight
  39. *
  40. * Remember -- no floats in kernel mode!!!
  41. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  42. * h1/w1 > h2/w2
  43. * if every weight is larger than zero.
  44. *
  45. * The server with weight=0 is quiesced and will not receive any
  46. * new connections.
  47. */
  48. list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
  49. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  50. atomic_read(&dest->weight) > 0) {
  51. least = dest;
  52. loh = ip_vs_dest_conn_overhead(least);
  53. goto nextstage;
  54. }
  55. }
  56. ip_vs_scheduler_err(svc, "no destination available");
  57. return NULL;
  58. /*
  59. * Find the destination with the least load.
  60. */
  61. nextstage:
  62. list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
  63. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  64. continue;
  65. doh = ip_vs_dest_conn_overhead(dest);
  66. if ((__s64)loh * atomic_read(&dest->weight) >
  67. (__s64)doh * atomic_read(&least->weight)) {
  68. least = dest;
  69. loh = doh;
  70. }
  71. }
  72. IP_VS_DBG_BUF(6, "WLC: server %s:%u "
  73. "activeconns %d refcnt %d weight %d overhead %d\n",
  74. IP_VS_DBG_ADDR(least->af, &least->addr),
  75. ntohs(least->port),
  76. atomic_read(&least->activeconns),
  77. atomic_read(&least->refcnt),
  78. atomic_read(&least->weight), loh);
  79. return least;
  80. }
  81. static struct ip_vs_scheduler ip_vs_wlc_scheduler =
  82. {
  83. .name = "wlc",
  84. .refcnt = ATOMIC_INIT(0),
  85. .module = THIS_MODULE,
  86. .n_list = LIST_HEAD_INIT(ip_vs_wlc_scheduler.n_list),
  87. .schedule = ip_vs_wlc_schedule,
  88. };
  89. static int __init ip_vs_wlc_init(void)
  90. {
  91. return register_ip_vs_scheduler(&ip_vs_wlc_scheduler);
  92. }
  93. static void __exit ip_vs_wlc_cleanup(void)
  94. {
  95. unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler);
  96. synchronize_rcu();
  97. }
  98. module_init(ip_vs_wlc_init);
  99. module_exit(ip_vs_wlc_cleanup);
  100. MODULE_LICENSE("GPL");