Blackfin: convert gpio irq_chip to new functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_wrr.c
blob1ef41f50723c04c13cfcff46ac1ca387aa6def7a
1 /*
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.
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
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>
33 * current destination pointer for weighted round-robin scheduling
35 struct ip_vs_wrr_mark {
36 struct list_head *cl; /* current list head */
37 int cw; /* current weight */
38 int mw; /* maximum weight */
39 int di; /* decreasing interval */
43 static int ip_vs_wrr_gcd_weight(struct ip_vs_service *svc)
45 struct ip_vs_dest *dest;
46 int weight;
47 int g = 0;
49 list_for_each_entry(dest, &svc->destinations, n_list) {
50 weight = atomic_read(&dest->weight);
51 if (weight > 0) {
52 if (g > 0)
53 g = gcd(weight, g);
54 else
55 g = weight;
58 return g ? g : 1;
63 * Get the maximum weight of the service destinations.
65 static int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
67 struct ip_vs_dest *dest;
68 int new_weight, weight = 0;
70 list_for_each_entry(dest, &svc->destinations, n_list) {
71 new_weight = atomic_read(&dest->weight);
72 if (new_weight > weight)
73 weight = new_weight;
76 return weight;
80 static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
82 struct ip_vs_wrr_mark *mark;
85 * Allocate the mark variable for WRR scheduling
87 mark = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_ATOMIC);
88 if (mark == NULL) {
89 pr_err("%s(): no memory\n", __func__);
90 return -ENOMEM;
92 mark->cl = &svc->destinations;
93 mark->cw = 0;
94 mark->mw = ip_vs_wrr_max_weight(svc);
95 mark->di = ip_vs_wrr_gcd_weight(svc);
96 svc->sched_data = mark;
98 return 0;
102 static int ip_vs_wrr_done_svc(struct ip_vs_service *svc)
105 * Release the mark variable
107 kfree(svc->sched_data);
109 return 0;
113 static int ip_vs_wrr_update_svc(struct ip_vs_service *svc)
115 struct ip_vs_wrr_mark *mark = svc->sched_data;
117 mark->cl = &svc->destinations;
118 mark->mw = ip_vs_wrr_max_weight(svc);
119 mark->di = ip_vs_wrr_gcd_weight(svc);
120 if (mark->cw > mark->mw)
121 mark->cw = 0;
122 return 0;
127 * Weighted Round-Robin Scheduling
129 static struct ip_vs_dest *
130 ip_vs_wrr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
132 struct ip_vs_dest *dest;
133 struct ip_vs_wrr_mark *mark = svc->sched_data;
134 struct list_head *p;
136 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
139 * This loop will always terminate, because mark->cw in (0, max_weight]
140 * and at least one server has its weight equal to max_weight.
142 write_lock(&svc->sched_lock);
143 p = mark->cl;
144 while (1) {
145 if (mark->cl == &svc->destinations) {
146 /* it is at the head of the destination list */
148 if (mark->cl == mark->cl->next) {
149 /* no dest entry */
150 ip_vs_scheduler_err(svc,
151 "no destination available: "
152 "no destinations present");
153 dest = NULL;
154 goto out;
157 mark->cl = svc->destinations.next;
158 mark->cw -= mark->di;
159 if (mark->cw <= 0) {
160 mark->cw = mark->mw;
162 * Still zero, which means no available servers.
164 if (mark->cw == 0) {
165 mark->cl = &svc->destinations;
166 ip_vs_scheduler_err(svc,
167 "no destination available");
168 dest = NULL;
169 goto out;
172 } else
173 mark->cl = mark->cl->next;
175 if (mark->cl != &svc->destinations) {
176 /* not at the head of the list */
177 dest = list_entry(mark->cl, struct ip_vs_dest, n_list);
178 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
179 atomic_read(&dest->weight) >= mark->cw) {
180 /* got it */
181 break;
185 if (mark->cl == p && mark->cw == mark->di) {
186 /* back to the start, and no dest is found.
187 It is only possible when all dests are OVERLOADED */
188 dest = NULL;
189 ip_vs_scheduler_err(svc,
190 "no destination available: "
191 "all destinations are overloaded");
192 goto out;
196 IP_VS_DBG_BUF(6, "WRR: server %s:%u "
197 "activeconns %d refcnt %d weight %d\n",
198 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port),
199 atomic_read(&dest->activeconns),
200 atomic_read(&dest->refcnt),
201 atomic_read(&dest->weight));
203 out:
204 write_unlock(&svc->sched_lock);
205 return dest;
209 static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
210 .name = "wrr",
211 .refcnt = ATOMIC_INIT(0),
212 .module = THIS_MODULE,
213 .n_list = LIST_HEAD_INIT(ip_vs_wrr_scheduler.n_list),
214 .init_service = ip_vs_wrr_init_svc,
215 .done_service = ip_vs_wrr_done_svc,
216 .update_service = ip_vs_wrr_update_svc,
217 .schedule = ip_vs_wrr_schedule,
220 static int __init ip_vs_wrr_init(void)
222 return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
225 static void __exit ip_vs_wrr_cleanup(void)
227 unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler);
230 module_init(ip_vs_wrr_init);
231 module_exit(ip_vs_wrr_cleanup);
232 MODULE_LICENSE("GPL");