/net/netfilter/ipvs/ip_vs_wrr.c

http://github.com/mirrors/linux · C · 265 lines · 159 code · 36 blank · 70 comment · 21 complexity · 1f02329a2b5e453c344f586dcde85ea9 MD5 · raw file

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