ip_vs_lc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * IPVS: Least-Connection Scheduling module
  3. *
  4. * Version: $Id: ip_vs_lc.c,v 1.10 2003/04/18 09:03:16 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Changes:
  14. * Wensong Zhang : added the ip_vs_lc_update_svc
  15. * Wensong Zhang : added any dest with weight=0 is quiesced
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <net/ip_vs.h>
  21. static int ip_vs_lc_init_svc(struct ip_vs_service *svc)
  22. {
  23. return 0;
  24. }
  25. static int ip_vs_lc_done_svc(struct ip_vs_service *svc)
  26. {
  27. return 0;
  28. }
  29. static int ip_vs_lc_update_svc(struct ip_vs_service *svc)
  30. {
  31. return 0;
  32. }
  33. static inline unsigned int
  34. ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
  35. {
  36. /*
  37. * We think the overhead of processing active connections is 256
  38. * times higher than that of inactive connections in average. (This
  39. * 256 times might not be accurate, we will change it later) We
  40. * use the following formula to estimate the overhead now:
  41. * dest->activeconns*256 + dest->inactconns
  42. */
  43. return (atomic_read(&dest->activeconns) << 8) +
  44. atomic_read(&dest->inactconns);
  45. }
  46. /*
  47. * Least Connection scheduling
  48. */
  49. static struct ip_vs_dest *
  50. ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  51. {
  52. struct ip_vs_dest *dest, *least = NULL;
  53. unsigned int loh = 0, doh;
  54. IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
  55. /*
  56. * Simply select the server with the least number of
  57. * (activeconns<<5) + inactconns
  58. * Except whose weight is equal to zero.
  59. * If the weight is equal to zero, it means that the server is
  60. * quiesced, the existing connections to the server still get
  61. * served, but no new connection is assigned to the server.
  62. */
  63. list_for_each_entry(dest, &svc->destinations, n_list) {
  64. if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
  65. atomic_read(&dest->weight) == 0)
  66. continue;
  67. doh = ip_vs_lc_dest_overhead(dest);
  68. if (!least || doh < loh) {
  69. least = dest;
  70. loh = doh;
  71. }
  72. }
  73. if (least)
  74. IP_VS_DBG(6, "LC: server %u.%u.%u.%u:%u activeconns %d inactconns %d\n",
  75. NIPQUAD(least->addr), ntohs(least->port),
  76. atomic_read(&least->activeconns),
  77. atomic_read(&least->inactconns));
  78. return least;
  79. }
  80. static struct ip_vs_scheduler ip_vs_lc_scheduler = {
  81. .name = "lc",
  82. .refcnt = ATOMIC_INIT(0),
  83. .module = THIS_MODULE,
  84. .init_service = ip_vs_lc_init_svc,
  85. .done_service = ip_vs_lc_done_svc,
  86. .update_service = ip_vs_lc_update_svc,
  87. .schedule = ip_vs_lc_schedule,
  88. };
  89. static int __init ip_vs_lc_init(void)
  90. {
  91. INIT_LIST_HEAD(&ip_vs_lc_scheduler.n_list);
  92. return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
  93. }
  94. static void __exit ip_vs_lc_cleanup(void)
  95. {
  96. unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
  97. }
  98. module_init(ip_vs_lc_init);
  99. module_exit(ip_vs_lc_cleanup);
  100. MODULE_LICENSE("GPL");