ip_vs_wrr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * IPVS: Weighted Round-Robin Scheduling module
  3. *
  4. * Version: $Id: ip_vs_wrr.c,v 1.12 2002/09/15 08:14:08 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 : changed the ip_vs_wrr_schedule to return dest
  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_wrr_update_svc
  18. * Julian Anastasov : fixed the bug of returning destination
  19. * with weight 0 when all weights are zero
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/net.h>
  25. #include <net/ip_vs.h>
  26. /*
  27. * current destination pointer for weighted round-robin scheduling
  28. */
  29. struct ip_vs_wrr_mark {
  30. struct list_head *cl; /* current list head */
  31. int cw; /* current weight */
  32. int mw; /* maximum weight */
  33. int di; /* decreasing interval */
  34. };
  35. /*
  36. * Get the gcd of server weights
  37. */
  38. static int gcd(int a, int b)
  39. {
  40. int c;
  41. while ((c = a % b)) {
  42. a = b;
  43. b = c;
  44. }
  45. return b;
  46. }
  47. static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
  48. {
  49. struct ip_vs_dest *dest;
  50. int weight;
  51. int g = 0;
  52. list_for_each_entry(dest, &svc->destinations, n_list) {
  53. weight = atomic_read(&dest->weight);
  54. if (weight > 0) {
  55. if (g > 0)
  56. g = gcd(weight, g);
  57. else
  58. g = weight;
  59. }
  60. }
  61. return g ? g : 1;
  62. }
  63. /*
  64. * Get the maximum weight of the service destinations.
  65. */
  66. static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
  67. {
  68. struct ip_vs_dest *dest;
  69. int weight = 0;
  70. list_for_each_entry(dest, &svc->destinations, n_list) {
  71. if (atomic_read(&dest->weight) > weight)
  72. weight = atomic_read(&dest->weight);
  73. }
  74. return weight;
  75. }
  76. static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
  77. {
  78. struct ip_vs_wrr_mark *mark;
  79. /*
  80. * Allocate the mark variable for WRR scheduling
  81. */
  82. mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_ATOMIC);
  83. if (mark == NULL) {
  84. IP_VS_ERR("ip_vs_wrr_init_svc(): no memory\n");
  85. return -ENOMEM;
  86. }
  87. mark->cl = &svc->destinations;
  88. mark->cw = 0;
  89. mark->mw = ip_vs_wrr_max_weight(svc);
  90. mark->di = ip_vs_wrr_gcd_weight(svc);
  91. svc->sched_data = mark;
  92. return 0;
  93. }
  94. static int ip_vs_wrr_done_svc(struct ip_vs_service *svc)
  95. {
  96. /*
  97. * Release the mark variable
  98. */
  99. kfree(svc->sched_data);
  100. return 0;
  101. }
  102. static int ip_vs_wrr_update_svc(struct ip_vs_service *svc)
  103. {
  104. struct ip_vs_wrr_mark *mark = svc->sched_data;
  105. mark->cl = &svc->destinations;
  106. mark->mw = ip_vs_wrr_max_weight(svc);
  107. mark->di = ip_vs_wrr_gcd_weight(svc);
  108. if (mark->cw > mark->mw)
  109. mark->cw = 0;
  110. return 0;
  111. }
  112. /*
  113. * Weighted Round-Robin Scheduling
  114. */
  115. static struct ip_vs_dest *
  116. ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  117. {
  118. struct ip_vs_dest *dest;
  119. struct ip_vs_wrr_mark *mark = svc->sched_data;
  120. struct list_head *p;
  121. IP_VS_DBG(6, "ip_vs_wrr_schedule(): Scheduling...\n");
  122. /*
  123. * This loop will always terminate, because mark->cw in (0, max_weight]
  124. * and at least one server has its weight equal to max_weight.
  125. */
  126. write_lock(&svc->sched_lock);
  127. p = mark->cl;
  128. while (1) {
  129. if (mark->cl == &svc->destinations) {
  130. /* it is at the head of the destination list */
  131. if (mark->cl == mark->cl->next) {
  132. /* no dest entry */
  133. dest = NULL;
  134. goto out;
  135. }
  136. mark->cl = svc->destinations.next;
  137. mark->cw -= mark->di;
  138. if (mark->cw <= 0) {
  139. mark->cw = mark->mw;
  140. /*
  141. * Still zero, which means no available servers.
  142. */
  143. if (mark->cw == 0) {
  144. mark->cl = &svc->destinations;
  145. IP_VS_ERR_RL("ip_vs_wrr_schedule(): "
  146. "no available servers\n");
  147. dest = NULL;
  148. goto out;
  149. }
  150. }
  151. } else
  152. mark->cl = mark->cl->next;
  153. if (mark->cl != &svc->destinations) {
  154. /* not at the head of the list */
  155. dest = list_entry(mark->cl, struct ip_vs_dest, n_list);
  156. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  157. atomic_read(&dest->weight) >= mark->cw) {
  158. /* got it */
  159. break;
  160. }
  161. }
  162. if (mark->cl == p && mark->cw == mark->di) {
  163. /* back to the start, and no dest is found.
  164. It is only possible when all dests are OVERLOADED */
  165. dest = NULL;
  166. goto out;
  167. }
  168. }
  169. IP_VS_DBG(6, "WRR: server %u.%u.%u.%u:%u "
  170. "activeconns %d refcnt %d weight %d\n",
  171. NIPQUAD(dest->addr), ntohs(dest->port),
  172. atomic_read(&dest->activeconns),
  173. atomic_read(&dest->refcnt),
  174. atomic_read(&dest->weight));
  175. out:
  176. write_unlock(&svc->sched_lock);
  177. return dest;
  178. }
  179. static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
  180. .name = "wrr",
  181. .refcnt = ATOMIC_INIT(0),
  182. .module = THIS_MODULE,
  183. .init_service = ip_vs_wrr_init_svc,
  184. .done_service = ip_vs_wrr_done_svc,
  185. .update_service = ip_vs_wrr_update_svc,
  186. .schedule = ip_vs_wrr_schedule,
  187. };
  188. static int __init ip_vs_wrr_init(void)
  189. {
  190. INIT_LIST_HEAD(&ip_vs_wrr_scheduler.n_list);
  191. return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
  192. }
  193. static void __exit ip_vs_wrr_cleanup(void)
  194. {
  195. unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
  196. }
  197. module_init(ip_vs_wrr_init);
  198. module_exit(ip_vs_wrr_cleanup);
  199. MODULE_LICENSE("GPL");