ip_vs_wrr.c 5.3 KB

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