ip_vs_wrr.c 5.3 KB

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