Merge with Linux 2.6.0-test1.
[linux-2.6/linux-mips.git] / net / ipv4 / ipvs / ip_vs_wlc.c
blob9ba05f2ad458c712eed7372cf01016a4ff5fe039
1 /*
2 * IPVS: Weighted Least-Connection Scheduling module
4 * Version: $Id: ip_vs_wlc.c,v 1.13 2003/04/18 09:03:16 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
7 * Peter Kese <peter.kese@ijs.si>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Changes:
15 * Wensong Zhang : changed the ip_vs_wlc_schedule to return dest
16 * Wensong Zhang : changed to use the inactconns in scheduling
17 * Wensong Zhang : changed some comestics things for debugging
18 * Wensong Zhang : changed for the d-linked destination list
19 * Wensong Zhang : added the ip_vs_wlc_update_svc
20 * Wensong Zhang : added any dest with weight=0 is quiesced
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
31 #include <net/ip_vs.h>
34 static int
35 ip_vs_wlc_init_svc(struct ip_vs_service *svc)
37 return 0;
41 static int
42 ip_vs_wlc_done_svc(struct ip_vs_service *svc)
44 return 0;
48 static int
49 ip_vs_wlc_update_svc(struct ip_vs_service *svc)
51 return 0;
55 static inline unsigned int
56 ip_vs_wlc_dest_overhead(struct ip_vs_dest *dest)
59 * We think the overhead of processing active connections is 256
60 * times higher than that of inactive connections in average. (This
61 * 256 times might not be accurate, we will change it later) We
62 * use the following formula to estimate the overhead now:
63 * dest->activeconns*256 + dest->inactconns
65 return (atomic_read(&dest->activeconns) << 8) +
66 atomic_read(&dest->inactconns);
71 * Weighted Least Connection scheduling
73 static struct ip_vs_dest *
74 ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
76 register struct list_head *l, *e;
77 struct ip_vs_dest *dest, *least;
78 unsigned int loh, doh;
80 IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
83 * We calculate the load of each dest server as follows:
84 * (dest overhead) / dest->weight
86 * Remember -- no floats in kernel mode!!!
87 * The comparison of h1*w2 > h2*w1 is equivalent to that of
88 * h1/w1 > h2/w2
89 * if every weight is larger than zero.
91 * The server with weight=0 is quiesced and will not receive any
92 * new connections.
95 l = &svc->destinations;
96 for (e=l->next; e!=l; e=e->next) {
97 least = list_entry(e, struct ip_vs_dest, n_list);
98 if (!(least->flags & IP_VS_DEST_F_OVERLOAD) &&
99 atomic_read(&least->weight) > 0) {
100 loh = ip_vs_wlc_dest_overhead(least);
101 goto nextstage;
104 return NULL;
107 * Find the destination with the least load.
109 nextstage:
110 for (e=e->next; e!=l; e=e->next) {
111 dest = list_entry(e, struct ip_vs_dest, n_list);
113 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
114 continue;
115 doh = ip_vs_wlc_dest_overhead(dest);
116 if (loh * atomic_read(&dest->weight) >
117 doh * atomic_read(&least->weight)) {
118 least = dest;
119 loh = doh;
123 IP_VS_DBG(6, "WLC: server %u.%u.%u.%u:%u "
124 "activeconns %d refcnt %d weight %d overhead %d\n",
125 NIPQUAD(least->addr), ntohs(least->port),
126 atomic_read(&least->activeconns),
127 atomic_read(&least->refcnt),
128 atomic_read(&least->weight), loh);
130 return least;
134 static struct ip_vs_scheduler ip_vs_wlc_scheduler =
136 .name = "wlc",
137 .refcnt = ATOMIC_INIT(0),
138 .module = THIS_MODULE,
139 .init_service = ip_vs_wlc_init_svc,
140 .done_service = ip_vs_wlc_done_svc,
141 .update_service = ip_vs_wlc_update_svc,
142 .schedule = ip_vs_wlc_schedule,
146 static int __init ip_vs_wlc_init(void)
148 INIT_LIST_HEAD(&ip_vs_wlc_scheduler.n_list);
149 return register_ip_vs_scheduler(&ip_vs_wlc_scheduler);
152 static void __exit ip_vs_wlc_cleanup(void)
154 unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler);
157 module_init(ip_vs_wlc_init);
158 module_exit(ip_vs_wlc_cleanup);
159 MODULE_LICENSE("GPL");