blob: 32c646eb8747b9d8394fb54a293f722916ea8815 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Weighted Round-Robin Scheduling module
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070021#define KMSG_COMPONENT "IPVS"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Sven Wegener9c1ca6e2008-02-05 20:00:10 -080027#include <linux/net.h>
Florian Fainelliae24e572009-12-22 09:42:06 +010028#include <linux/gcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/ip_vs.h>
31
Julian Anastasov08cb2d02013-03-22 11:46:48 +020032/* 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.
37 *
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:
41 *
42 * pass 1: cw = max weight
43 * pass 2: cw = max weight - di
44 * pass 3: cw = max weight - 2 * di
45 * ...
46 * last pass: cw = di
47 *
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.
51 *
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)
57 * ...
58 * last pass: cw = 1
59 *
60 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * current destination pointer for weighted round-robin scheduling
64 */
65struct ip_vs_wrr_mark {
Julian Anastasov08cb2d02013-03-22 11:46:48 +020066 struct ip_vs_dest *cl; /* current dest or head */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int cw; /* current weight */
68 int mw; /* maximum weight */
69 int di; /* decreasing interval */
Julian Anastasov08cb2d02013-03-22 11:46:48 +020070 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
75{
76 struct ip_vs_dest *dest;
77 int weight;
78 int g = 0;
79
80 list_for_each_entry(dest, &svc->destinations, n_list) {
81 weight = atomic_read(&dest->weight);
82 if (weight > 0) {
83 if (g > 0)
84 g = gcd(weight, g);
85 else
86 g = weight;
87 }
88 }
89 return g ? g : 1;
90}
91
92
93/*
94 * Get the maximum weight of the service destinations.
95 */
96static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
97{
98 struct ip_vs_dest *dest;
Simon Horman1e66daf2009-08-31 14:18:48 +020099 int new_weight, weight = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 list_for_each_entry(dest, &svc->destinations, n_list) {
Simon Horman1e66daf2009-08-31 14:18:48 +0200102 new_weight = atomic_read(&dest->weight);
103 if (new_weight > weight)
104 weight = new_weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 return weight;
108}
109
110
111static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
112{
113 struct ip_vs_wrr_mark *mark;
114
115 /*
116 * Allocate the mark variable for WRR scheduling
117 */
Julian Anastasov4f2a94d2012-04-13 16:49:42 +0300118 mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_KERNEL);
Joe Perches0a9ee812011-08-29 14:17:25 -0700119 if (mark == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -0700121
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200122 mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 mark->di = ip_vs_wrr_gcd_weight(svc);
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200124 mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
125 mark->cw = mark->mw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 svc->sched_data = mark;
127
128 return 0;
129}
130
131
Julian Anastasoved3ffc42013-03-22 11:46:50 +0200132static void ip_vs_wrr_done_svc(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200134 struct ip_vs_wrr_mark *mark = svc->sched_data;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * Release the mark variable
138 */
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200139 kfree_rcu(mark, rcu_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200143static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc,
144 struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct ip_vs_wrr_mark *mark = svc->sched_data;
147
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200148 spin_lock_bh(&svc->sched_lock);
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200149 mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 mark->di = ip_vs_wrr_gcd_weight(svc);
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200151 mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
152 if (mark->cw > mark->mw || !mark->cw)
153 mark->cw = mark->mw;
154 else if (mark->di > 1)
155 mark->cw = (mark->cw / mark->di) * mark->di + 1;
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200156 spin_unlock_bh(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
159
160
161/*
162 * Weighted Round-Robin Scheduling
163 */
164static struct ip_vs_dest *
165ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
166{
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200167 struct ip_vs_dest *dest, *last, *stop = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct ip_vs_wrr_mark *mark = svc->sched_data;
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200169 bool last_pass = false, restarted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Hannes Eder1e3e2382009-08-02 11:05:41 +0000171 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200173 spin_lock(&svc->sched_lock);
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200174 dest = mark->cl;
175 /* No available dests? */
176 if (mark->mw == 0)
177 goto err_noavail;
178 last = dest;
179 /* Stop only after all dests were checked for weight >= 1 (last pass) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 while (1) {
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200181 list_for_each_entry_continue_rcu(dest,
182 &svc->destinations,
183 n_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200185 atomic_read(&dest->weight) >= mark->cw)
186 goto found;
187 if (dest == stop)
188 goto err_over;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200190 mark->cw -= mark->di;
191 if (mark->cw <= 0) {
192 mark->cw = mark->mw;
193 /* Stop if we tried last pass from first dest:
194 * 1. last_pass: we started checks when cw > di but
195 * then all dests were checked for w >= 1
196 * 2. last was head: the first and only traversal
197 * was for weight >= 1, for all dests.
198 */
199 if (last_pass ||
200 &last->n_list == &svc->destinations)
201 goto err_over;
202 restarted = true;
203 }
204 last_pass = mark->cw <= mark->di;
205 if (last_pass && restarted &&
206 &last->n_list != &svc->destinations) {
207 /* First traversal was for w >= 1 but only
208 * for dests after 'last', now do the same
209 * for all dests up to 'last'.
210 */
211 stop = last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 }
214
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200215found:
Julius Volzb14198f2008-09-02 15:55:39 +0200216 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
217 "activeconns %d refcnt %d weight %d\n",
218 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port),
219 atomic_read(&dest->activeconns),
220 atomic_read(&dest->refcnt),
221 atomic_read(&dest->weight));
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200222 mark->cl = dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 out:
Julian Anastasovba3a3ce2013-03-22 11:46:51 +0200225 spin_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return dest;
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200227
228err_noavail:
229 mark->cl = dest;
230 dest = NULL;
231 ip_vs_scheduler_err(svc, "no destination available");
232 goto out;
233
234err_over:
235 mark->cl = dest;
236 dest = NULL;
237 ip_vs_scheduler_err(svc, "no destination available: "
238 "all destinations are overloaded");
239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
244 .name = "wrr",
245 .refcnt = ATOMIC_INIT(0),
246 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000247 .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .init_service = ip_vs_wrr_init_svc,
249 .done_service = ip_vs_wrr_done_svc,
Julian Anastasov08cb2d02013-03-22 11:46:48 +0200250 .add_dest = ip_vs_wrr_dest_changed,
251 .del_dest = ip_vs_wrr_dest_changed,
252 .upd_dest = ip_vs_wrr_dest_changed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .schedule = ip_vs_wrr_schedule,
254};
255
256static int __init ip_vs_wrr_init(void)
257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
259}
260
261static void __exit ip_vs_wrr_cleanup(void)
262{
263 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200264 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267module_init(ip_vs_wrr_init);
268module_exit(ip_vs_wrr_cleanup);
269MODULE_LICENSE("GPL");