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/net.h>
27 #include <linux/gcd.h>
29 #include <net/ip_vs.h>
32 * current destination pointer for weighted round-robin scheduling
34 struct ip_vs_wrr_mark {
35 struct list_head *cl; /* current list head */
36 int cw; /* current weight */
37 int mw; /* maximum weight */
38 int di; /* decreasing interval */
42 static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
44 struct ip_vs_dest *dest;
48 list_for_each_entry(dest, &svc->destinations, n_list) {
49 weight = atomic_read(&dest->weight);
62 * Get the maximum weight of the service destinations.
64 static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
66 struct ip_vs_dest *dest;
67 int new_weight, weight = 0;
69 list_for_each_entry(dest, &svc->destinations, n_list) {
70 new_weight = atomic_read(&dest->weight);
71 if (new_weight > weight)
79 static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
81 struct ip_vs_wrr_mark *mark;
84 * Allocate the mark variable for WRR scheduling
86 mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_ATOMIC);
88 pr_err("%s(): no memory\n", __func__);
91 mark->cl = &svc->destinations;
93 mark->mw = ip_vs_wrr_max_weight(svc);
94 mark->di = ip_vs_wrr_gcd_weight(svc);
95 svc->sched_data = mark;
101 static int ip_vs_wrr_done_svc(struct ip_vs_service *svc)
104 * Release the mark variable
106 kfree(svc->sched_data);
112 static int ip_vs_wrr_update_svc(struct ip_vs_service *svc)
114 struct ip_vs_wrr_mark *mark = svc->sched_data;
116 mark->cl = &svc->destinations;
117 mark->mw = ip_vs_wrr_max_weight(svc);
118 mark->di = ip_vs_wrr_gcd_weight(svc);
119 if (mark->cw > mark->mw)
126 * Weighted Round-Robin Scheduling
128 static struct ip_vs_dest *
129 ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
131 struct ip_vs_dest *dest;
132 struct ip_vs_wrr_mark *mark = svc->sched_data;
135 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
138 * This loop will always terminate, because mark->cw in (0, max_weight]
139 * and at least one server has its weight equal to max_weight.
141 write_lock(&svc->sched_lock);
144 if (mark->cl == &svc->destinations) {
145 /* it is at the head of the destination list */
147 if (mark->cl == mark->cl->next) {
149 IP_VS_ERR_RL("WRR: no destination available: "
150 "no destinations present\n");
155 mark->cl = svc->destinations.next;
156 mark->cw -= mark->di;
160 * Still zero, which means no available servers.
163 mark->cl = &svc->destinations;
164 IP_VS_ERR_RL("WRR: no destination "
171 mark->cl = mark->cl->next;
173 if (mark->cl != &svc->destinations) {
174 /* not at the head of the list */
175 dest = list_entry(mark->cl, struct ip_vs_dest, n_list);
176 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
177 atomic_read(&dest->weight) >= mark->cw) {
183 if (mark->cl == p && mark->cw == mark->di) {
184 /* back to the start, and no dest is found.
185 It is only possible when all dests are OVERLOADED */
187 IP_VS_ERR_RL("WRR: no destination available: "
188 "all destinations are overloaded\n");
193 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
194 "activeconns %d refcnt %d weight %d\n",
195 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port),
196 atomic_read(&dest->activeconns),
197 atomic_read(&dest->refcnt),
198 atomic_read(&dest->weight));
201 write_unlock(&svc->sched_lock);
206 static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
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 .update_service = ip_vs_wrr_update_svc,
214 .schedule = ip_vs_wrr_schedule,
217 static int __init ip_vs_wrr_init(void)
219 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
222 static void __exit ip_vs_wrr_cleanup(void)
224 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
227 module_init(ip_vs_wrr_init);
228 module_exit(ip_vs_wrr_cleanup);
229 MODULE_LICENSE("GPL");