ip_vs_lc.c 3.0 KB

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