]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/netfilter/ipvs/ip_vs_wrr.c
98cb05e345c9e07755747275f25807810a5db12e
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / ipvs / ip_vs_wrr.c
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
21 #define KMSG_COMPONENT "IPVS"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
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>
29
30 #include <net/ip_vs.h>
31
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.
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
62 /*
63 * current destination pointer for weighted round-robin scheduling
64 */
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;
71 };
72
73
74 static 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 */
96 static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
97 {
98 struct ip_vs_dest *dest;
99 int new_weight, weight = 0;
100
101 list_for_each_entry(dest, &svc->destinations, n_list) {
102 new_weight = atomic_read(&dest->weight);
103 if (new_weight > weight)
104 weight = new_weight;
105 }
106
107 return weight;
108 }
109
110
111 static 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 */
118 mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_KERNEL);
119 if (mark == NULL)
120 return -ENOMEM;
121
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);
125 mark->cw = mark->mw;
126 svc->sched_data = mark;
127
128 return 0;
129 }
130
131
132 static int ip_vs_wrr_done_svc(struct ip_vs_service *svc)
133 {
134 struct ip_vs_wrr_mark *mark = svc->sched_data;
135
136 /*
137 * Release the mark variable
138 */
139 kfree_rcu(mark, rcu_head);
140
141 return 0;
142 }
143
144
145 static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc,
146 struct ip_vs_dest *dest)
147 {
148 struct ip_vs_wrr_mark *mark = svc->sched_data;
149
150 write_lock_bh(&svc->sched_lock);
151 mark->cl = list_entry(&svc->destinations, struct ip_vs_dest, n_list);
152 mark->di = ip_vs_wrr_gcd_weight(svc);
153 mark->mw = ip_vs_wrr_max_weight(svc) - (mark->di - 1);
154 if (mark->cw > mark->mw || !mark->cw)
155 mark->cw = mark->mw;
156 else if (mark->di > 1)
157 mark->cw = (mark->cw / mark->di) * mark->di + 1;
158 write_unlock_bh(&svc->sched_lock);
159 return 0;
160 }
161
162
163 /*
164 * Weighted Round-Robin Scheduling
165 */
166 static struct ip_vs_dest *
167 ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
168 {
169 struct ip_vs_dest *dest, *last, *stop = NULL;
170 struct ip_vs_wrr_mark *mark = svc->sched_data;
171 bool last_pass = false, restarted = false;
172
173 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
174
175 write_lock(&svc->sched_lock);
176 dest = mark->cl;
177 /* No available dests? */
178 if (mark->mw == 0)
179 goto err_noavail;
180 last = dest;
181 /* Stop only after all dests were checked for weight >= 1 (last pass) */
182 while (1) {
183 list_for_each_entry_continue_rcu(dest,
184 &svc->destinations,
185 n_list) {
186 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
187 atomic_read(&dest->weight) >= mark->cw)
188 goto found;
189 if (dest == stop)
190 goto err_over;
191 }
192 mark->cw -= mark->di;
193 if (mark->cw <= 0) {
194 mark->cw = mark->mw;
195 /* Stop if we tried last pass from first dest:
196 * 1. last_pass: we started checks when cw > di but
197 * then all dests were checked for w >= 1
198 * 2. last was head: the first and only traversal
199 * was for weight >= 1, for all dests.
200 */
201 if (last_pass ||
202 &last->n_list == &svc->destinations)
203 goto err_over;
204 restarted = true;
205 }
206 last_pass = mark->cw <= mark->di;
207 if (last_pass && restarted &&
208 &last->n_list != &svc->destinations) {
209 /* First traversal was for w >= 1 but only
210 * for dests after 'last', now do the same
211 * for all dests up to 'last'.
212 */
213 stop = last;
214 }
215 }
216
217 found:
218 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
219 "activeconns %d refcnt %d weight %d\n",
220 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port),
221 atomic_read(&dest->activeconns),
222 atomic_read(&dest->refcnt),
223 atomic_read(&dest->weight));
224 mark->cl = dest;
225
226 out:
227 write_unlock(&svc->sched_lock);
228 return dest;
229
230 err_noavail:
231 mark->cl = dest;
232 dest = NULL;
233 ip_vs_scheduler_err(svc, "no destination available");
234 goto out;
235
236 err_over:
237 mark->cl = dest;
238 dest = NULL;
239 ip_vs_scheduler_err(svc, "no destination available: "
240 "all destinations are overloaded");
241 goto out;
242 }
243
244
245 static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
246 .name = "wrr",
247 .refcnt = ATOMIC_INIT(0),
248 .module = THIS_MODULE,
249 .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
250 .init_service = ip_vs_wrr_init_svc,
251 .done_service = ip_vs_wrr_done_svc,
252 .add_dest = ip_vs_wrr_dest_changed,
253 .del_dest = ip_vs_wrr_dest_changed,
254 .upd_dest = ip_vs_wrr_dest_changed,
255 .schedule = ip_vs_wrr_schedule,
256 };
257
258 static int __init ip_vs_wrr_init(void)
259 {
260 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
261 }
262
263 static void __exit ip_vs_wrr_cleanup(void)
264 {
265 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
266 }
267
268 module_init(ip_vs_wrr_init);
269 module_exit(ip_vs_wrr_cleanup);
270 MODULE_LICENSE("GPL");