ip_vs_wrr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #define KMSG_COMPONENT "IPVS"
  21. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/net.h>
  26. #include <linux/gcd.h>
  27. #include <net/ip_vs.h>
  28. /* The WRR algorithm depends on some caclulations:
  29. * - mw: maximum weight
  30. * - di: weight step, greatest common divisor from all weights
  31. * - cw: current required weight
  32. * As result, all weights are in the [di..mw] range with a step=di.
  33. *
  34. * First, we start with cw = mw and select dests with weight >= cw.
  35. * Then cw is reduced with di and all dests are checked again.
  36. * Last pass should be with cw = di. We have mw/di passes in total:
  37. *
  38. * pass 1: cw = max weight
  39. * pass 2: cw = max weight - di
  40. * pass 3: cw = max weight - 2 * di
  41. * ...
  42. * last pass: cw = di
  43. *
  44. * Weights are supposed to be >= di but we run in parallel with
  45. * weight changes, it is possible some dest weight to be reduced
  46. * below di, bad if it is the only available dest.
  47. *
  48. * So, we modify how mw is calculated, now it is reduced with (di - 1),
  49. * so that last cw is 1 to catch such dests with weight below di:
  50. * pass 1: cw = max weight - (di - 1)
  51. * pass 2: cw = max weight - di - (di - 1)
  52. * pass 3: cw = max weight - 2 * di - (di - 1)
  53. * ...
  54. * last pass: cw = 1
  55. *
  56. */
  57. /*
  58. * current destination pointer for weighted round-robin scheduling
  59. */
  60. struct ip_vs_wrr_mark {
  61. struct ip_vs_dest *cl; /* current dest or head */
  62. int cw; /* current weight */
  63. int mw; /* maximum weight */
  64. int di; /* decreasing interval */
  65. struct rcu_head rcu_head;
  66. };
  67. static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
  68. {
  69. struct ip_vs_dest *dest;
  70. int weight;
  71. int g = 0;
  72. list_for_each_entry(dest, &svc->destinations, n_list) {
  73. weight = atomic_read(&dest->weight);
  74. if (weight > 0) {
  75. if (g > 0)
  76. g = gcd(weight, g);
  77. else
  78. g = weight;
  79. }
  80. }
  81. return g ? g : 1;
  82. }
  83. /*
  84. * Get the maximum weight of the service destinations.
  85. */
  86. static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
  87. {
  88. struct ip_vs_dest *dest;
  89. int new_weight, weight = 0;
  90. list_for_each_entry(dest, &svc->destinations, n_list) {
  91. new_weight = atomic_read(&dest->weight);
  92. if (new_weight > weight)
  93. weight = new_weight;
  94. }
  95. return weight;
  96. }
  97. static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
  98. {
  99. struct ip_vs_wrr_mark *mark;
  100. /*
  101. * Allocate the mark variable for WRR scheduling
  102. */
  103. mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_KERNEL);
  104. if (mark == NULL)
  105. return -ENOMEM;
  106. mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
  107. mark->di = ip_vs_wrr_gcd_weight(svc);
  108. mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
  109. mark->cw = mark->mw;
  110. svc->sched_data = mark;
  111. return 0;
  112. }
  113. static void ip_vs_wrr_done_svc(struct ip_vs_service *svc)
  114. {
  115. struct ip_vs_wrr_mark *mark = svc->sched_data;
  116. /*
  117. * Release the mark variable
  118. */
  119. kfree_rcu(mark, rcu_head);
  120. }
  121. static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc,
  122. struct ip_vs_dest *dest)
  123. {
  124. struct ip_vs_wrr_mark *mark = svc->sched_data;
  125. spin_lock_bh(&svc->sched_lock);
  126. mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
  127. mark->di = ip_vs_wrr_gcd_weight(svc);
  128. mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
  129. if (mark->cw > mark->mw || !mark->cw)
  130. mark->cw = mark->mw;
  131. else if (mark->di > 1)
  132. mark->cw = (mark->cw / mark->di) * mark->di + 1;
  133. spin_unlock_bh(&svc->sched_lock);
  134. return 0;
  135. }
  136. /*
  137. * Weighted Round-Robin Scheduling
  138. */
  139. static struct ip_vs_dest *
  140. ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
  141. struct ip_vs_iphdr *iph)
  142. {
  143. struct ip_vs_dest *dest, *last, *stop = NULL;
  144. struct ip_vs_wrr_mark *mark = svc->sched_data;
  145. bool last_pass = false, restarted = false;
  146. IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
  147. spin_lock_bh(&svc->sched_lock);
  148. dest = mark->cl;
  149. /* No available dests? */
  150. if (mark->mw == 0)
  151. goto err_noavail;
  152. last = dest;
  153. /* Stop only after all dests were checked for weight >= 1 (last pass) */
  154. while (1) {
  155. list_for_each_entry_continue_rcu(dest,
  156. &svc->destinations,
  157. n_list) {
  158. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  159. atomic_read(&dest->weight) >= mark->cw)
  160. goto found;
  161. if (dest == stop)
  162. goto err_over;
  163. }
  164. mark->cw -= mark->di;
  165. if (mark->cw <= 0) {
  166. mark->cw = mark->mw;
  167. /* Stop if we tried last pass from first dest:
  168. * 1. last_pass: we started checks when cw > di but
  169. * then all dests were checked for w >= 1
  170. * 2. last was head: the first and only traversal
  171. * was for weight >= 1, for all dests.
  172. */
  173. if (last_pass ||
  174. &last->n_list == &svc->destinations)
  175. goto err_over;
  176. restarted = true;
  177. }
  178. last_pass = mark->cw <= mark->di;
  179. if (last_pass && restarted &&
  180. &last->n_list != &svc->destinations) {
  181. /* First traversal was for w >= 1 but only
  182. * for dests after 'last', now do the same
  183. * for all dests up to 'last'.
  184. */
  185. stop = last;
  186. }
  187. }
  188. found:
  189. IP_VS_DBG_BUF(6, "WRR: server %s:%u "
  190. "activeconns %d refcnt %d weight %d\n",
  191. IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
  192. atomic_read(&dest->activeconns),
  193. atomic_read(&dest->refcnt),
  194. atomic_read(&dest->weight));
  195. mark->cl = dest;
  196. out:
  197. spin_unlock_bh(&svc->sched_lock);
  198. return dest;
  199. err_noavail:
  200. mark->cl = dest;
  201. dest = NULL;
  202. ip_vs_scheduler_err(svc, "no destination available");
  203. goto out;
  204. err_over:
  205. mark->cl = dest;
  206. dest = NULL;
  207. ip_vs_scheduler_err(svc, "no destination available: "
  208. "all destinations are overloaded");
  209. goto out;
  210. }
  211. static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
  212. .name = "wrr",
  213. .refcnt = ATOMIC_INIT(0),
  214. .module = THIS_MODULE,
  215. .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
  216. .init_service = ip_vs_wrr_init_svc,
  217. .done_service = ip_vs_wrr_done_svc,
  218. .add_dest = ip_vs_wrr_dest_changed,
  219. .del_dest = ip_vs_wrr_dest_changed,
  220. .upd_dest = ip_vs_wrr_dest_changed,
  221. .schedule = ip_vs_wrr_schedule,
  222. };
  223. static int __init ip_vs_wrr_init(void)
  224. {
  225. return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
  226. }
  227. static void __exit ip_vs_wrr_cleanup(void)
  228. {
  229. unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
  230. synchronize_rcu();
  231. }
  232. module_init(ip_vs_wrr_init);
  233. module_exit(ip_vs_wrr_cleanup);
  234. MODULE_LICENSE("GPL");