2 * IPVS: Weighted Round-Robin Scheduling module
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
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.
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
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/net.h>
28 #include <linux/gcd.h>
30 #include <net/ip_vs.h>
32 /* The WRR algorithm depends on some caclulations:
33 * - mw: maximum weight
34 * - di: weight step, greatest common divisor from all weights
35 * - cw: current required weight
36 * As result, all weights are in the [di..mw] range with a step=di.
38 * First, we start with cw = mw and select dests with weight >= cw.
39 * Then cw is reduced with di and all dests are checked again.
40 * Last pass should be with cw = di. We have mw/di passes in total:
42 * pass 1: cw = max weight
43 * pass 2: cw = max weight - di
44 * pass 3: cw = max weight - 2 * di
48 * Weights are supposed to be >= di but we run in parallel with
49 * weight changes, it is possible some dest weight to be reduced
50 * below di, bad if it is the only available dest.
52 * So, we modify how mw is calculated, now it is reduced with (di - 1),
53 * so that last cw is 1 to catch such dests with weight below di:
54 * pass 1: cw = max weight - (di - 1)
55 * pass 2: cw = max weight - di - (di - 1)
56 * pass 3: cw = max weight - 2 * di - (di - 1)
63 * current destination pointer for weighted round-robin scheduling
65 struct ip_vs_wrr_mark {
66 struct ip_vs_dest *cl; /* current dest or head */
67 int cw; /* current weight */
68 int mw; /* maximum weight */
69 int di; /* decreasing interval */
70 struct rcu_head rcu_head;
74 static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
76 struct ip_vs_dest *dest;
80 list_for_each_entry(dest, &svc->destinations, n_list) {
81 weight = atomic_read(&dest->weight);
94 * Get the maximum weight of the service destinations.
96 static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
98 struct ip_vs_dest *dest;
99 int new_weight, weight = 0;
101 list_for_each_entry(dest, &svc->destinations, n_list) {
102 new_weight = atomic_read(&dest->weight);
103 if (new_weight > weight)
111 static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
113 struct ip_vs_wrr_mark *mark;
116 * Allocate the mark variable for WRR scheduling
118 mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_KERNEL);
122 mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
123 mark->di = ip_vs_wrr_gcd_weight(svc);
124 mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
126 svc->sched_data = mark;
132 static void ip_vs_wrr_done_svc(struct ip_vs_service *svc)
134 struct ip_vs_wrr_mark *mark = svc->sched_data;
137 * Release the mark variable
139 kfree_rcu(mark, rcu_head);
143 static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc,
144 struct ip_vs_dest *dest)
146 struct ip_vs_wrr_mark *mark = svc->sched_data;
148 spin_lock_bh(&svc->sched_lock);
149 mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
150 mark->di = ip_vs_wrr_gcd_weight(svc);
151 mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
152 if (mark->cw > mark->mw || !mark->cw)
154 else if (mark->di > 1)
155 mark->cw = (mark->cw / mark->di) * mark->di + 1;
156 spin_unlock_bh(&svc->sched_lock);
162 * Weighted Round-Robin Scheduling
164 static struct ip_vs_dest *
165 ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
166 struct ip_vs_iphdr *iph)
168 struct ip_vs_dest *dest, *last, *stop = NULL;
169 struct ip_vs_wrr_mark *mark = svc->sched_data;
170 bool last_pass = false, restarted = false;
172 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
174 spin_lock_bh(&svc->sched_lock);
176 /* No available dests? */
180 /* Stop only after all dests were checked for weight >= 1 (last pass) */
182 list_for_each_entry_continue_rcu(dest,
185 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
186 atomic_read(&dest->weight) >= mark->cw)
191 mark->cw -= mark->di;
194 /* Stop if we tried last pass from first dest:
195 * 1. last_pass: we started checks when cw > di but
196 * then all dests were checked for w >= 1
197 * 2. last was head: the first and only traversal
198 * was for weight >= 1, for all dests.
201 &last->n_list == &svc->destinations)
205 last_pass = mark->cw <= mark->di;
206 if (last_pass && restarted &&
207 &last->n_list != &svc->destinations) {
208 /* First traversal was for w >= 1 but only
209 * for dests after 'last', now do the same
210 * for all dests up to 'last'.
217 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
218 "activeconns %d refcnt %d weight %d\n",
219 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
220 atomic_read(&dest->activeconns),
221 atomic_read(&dest->refcnt),
222 atomic_read(&dest->weight));
226 spin_unlock_bh(&svc->sched_lock);
232 ip_vs_scheduler_err(svc, "no destination available");
238 ip_vs_scheduler_err(svc, "no destination available: "
239 "all destinations are overloaded");
244 static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
246 .refcnt = ATOMIC_INIT(0),
247 .module = THIS_MODULE,
248 .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
249 .init_service = ip_vs_wrr_init_svc,
250 .done_service = ip_vs_wrr_done_svc,
251 .add_dest = ip_vs_wrr_dest_changed,
252 .del_dest = ip_vs_wrr_dest_changed,
253 .upd_dest = ip_vs_wrr_dest_changed,
254 .schedule = ip_vs_wrr_schedule,
257 static int __init ip_vs_wrr_init(void)
259 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
262 static void __exit ip_vs_wrr_cleanup(void)
264 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
268 module_init(ip_vs_wrr_init);
269 module_exit(ip_vs_wrr_cleanup);
270 MODULE_LICENSE("GPL");