1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65struct ip_vs_wrr_mark {
66 struct ip_vs_dest *cl;
67 int cw;
68 int mw;
69 int di;
70 struct rcu_head rcu_head;
71};
72
73
74static 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
95
96static 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
111static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
112{
113 struct ip_vs_wrr_mark *mark;
114
115
116
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
132static void ip_vs_wrr_done_svc(struct ip_vs_service *svc)
133{
134 struct ip_vs_wrr_mark *mark = svc->sched_data;
135
136
137
138
139 kfree_rcu(mark, rcu_head);
140}
141
142
143static int ip_vs_wrr_dest_changed(struct ip_vs_service *svc,
144 struct ip_vs_dest *dest)
145{
146 struct ip_vs_wrr_mark *mark = svc->sched_data;
147
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) {
153 gmb();
154 mark->cw = mark->mw;
155 } else if (mark->di > 1) {
156 gmb();
157 mark->cw = (mark->cw / mark->di) * mark->di + 1;
158 }
159 spin_unlock_bh(&svc->sched_lock);
160 return 0;
161}
162
163
164
165
166
167static struct ip_vs_dest *
168ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
169 struct ip_vs_iphdr *iph)
170{
171 struct ip_vs_dest *dest, *last, *stop = NULL;
172 struct ip_vs_wrr_mark *mark = svc->sched_data;
173 bool last_pass = false, restarted = false;
174
175 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
176
177 spin_lock_bh(&svc->sched_lock);
178 dest = mark->cl;
179
180 if (mark->mw == 0)
181 goto err_noavail;
182 last = dest;
183
184 while (1) {
185 list_for_each_entry_continue_rcu(dest,
186 &svc->destinations,
187 n_list) {
188 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
189 atomic_read(&dest->weight) >= mark->cw)
190 goto found;
191 if (dest == stop)
192 goto err_over;
193 }
194 mark->cw -= mark->di;
195 if (mark->cw <= 0) {
196 mark->cw = mark->mw;
197
198
199
200
201
202
203 if (last_pass ||
204 &last->n_list == &svc->destinations)
205 goto err_over;
206 restarted = true;
207 }
208 last_pass = mark->cw <= mark->di;
209 if (last_pass && restarted &&
210 &last->n_list != &svc->destinations) {
211
212
213
214
215 stop = last;
216 }
217 }
218
219found:
220 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
221 "activeconns %d refcnt %d weight %d\n",
222 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port),
223 atomic_read(&dest->activeconns),
224 atomic_read(&dest->refcnt),
225 atomic_read(&dest->weight));
226 mark->cl = dest;
227
228 out:
229 spin_unlock_bh(&svc->sched_lock);
230 return dest;
231
232err_noavail:
233 mark->cl = dest;
234 dest = NULL;
235 ip_vs_scheduler_err(svc, "no destination available");
236 goto out;
237
238err_over:
239 mark->cl = dest;
240 dest = NULL;
241 ip_vs_scheduler_err(svc, "no destination available: "
242 "all destinations are overloaded");
243 goto out;
244}
245
246
247static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
248 .name = "wrr",
249 .refcnt = ATOMIC_INIT(0),
250 .module = THIS_MODULE,
251 .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
252 .init_service = ip_vs_wrr_init_svc,
253 .done_service = ip_vs_wrr_done_svc,
254 .add_dest = ip_vs_wrr_dest_changed,
255 .del_dest = ip_vs_wrr_dest_changed,
256 .upd_dest = ip_vs_wrr_dest_changed,
257 .schedule = ip_vs_wrr_schedule,
258};
259
260static int __init ip_vs_wrr_init(void)
261{
262 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
263}
264
265static void __exit ip_vs_wrr_cleanup(void)
266{
267 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
268 synchronize_rcu();
269}
270
271module_init(ip_vs_wrr_init);
272module_exit(ip_vs_wrr_cleanup);
273MODULE_LICENSE("GPL");
274