Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / net / ipv4 / ipvs / ip_vs_wlc.c
1 /*
2  * IPVS:        Weighted Least-Connection Scheduling module
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *              Peter Kese <peter.kese@ijs.si>
6  *
7  *              This program is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  * Changes:
13  *     Wensong Zhang            :     changed the ip_vs_wlc_schedule to return dest
14  *     Wensong Zhang            :     changed to use the inactconns in scheduling
15  *     Wensong Zhang            :     changed some comestics things for debugging
16  *     Wensong Zhang            :     changed for the d-linked destination list
17  *     Wensong Zhang            :     added the ip_vs_wlc_update_svc
18  *     Wensong Zhang            :     added any dest with weight=0 is quiesced
19  *
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24
25 #include <net/ip_vs.h>
26
27
28 static int
29 ip_vs_wlc_init_svc(struct ip_vs_service *svc)
30 {
31         return 0;
32 }
33
34
35 static int
36 ip_vs_wlc_done_svc(struct ip_vs_service *svc)
37 {
38         return 0;
39 }
40
41
42 static int
43 ip_vs_wlc_update_svc(struct ip_vs_service *svc)
44 {
45         return 0;
46 }
47
48
49 static inline unsigned int
50 ip_vs_wlc_dest_overhead(struct ip_vs_dest *dest)
51 {
52         /*
53          * We think the overhead of processing active connections is 256
54          * times higher than that of inactive connections in average. (This
55          * 256 times might not be accurate, we will change it later) We
56          * use the following formula to estimate the overhead now:
57          *                dest->activeconns*256 + dest->inactconns
58          */
59         return (atomic_read(&dest->activeconns) << 8) +
60                 atomic_read(&dest->inactconns);
61 }
62
63
64 /*
65  *      Weighted Least Connection scheduling
66  */
67 static struct ip_vs_dest *
68 ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
69 {
70         struct ip_vs_dest *dest, *least;
71         unsigned int loh, doh;
72
73         IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
74
75         /*
76          * We calculate the load of each dest server as follows:
77          *                (dest overhead) / dest->weight
78          *
79          * Remember -- no floats in kernel mode!!!
80          * The comparison of h1*w2 > h2*w1 is equivalent to that of
81          *                h1/w1 > h2/w2
82          * if every weight is larger than zero.
83          *
84          * The server with weight=0 is quiesced and will not receive any
85          * new connections.
86          */
87
88         list_for_each_entry(dest, &svc->destinations, n_list) {
89                 if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
90                     atomic_read(&dest->weight) > 0) {
91                         least = dest;
92                         loh = ip_vs_wlc_dest_overhead(least);
93                         goto nextstage;
94                 }
95         }
96         return NULL;
97
98         /*
99          *    Find the destination with the least load.
100          */
101   nextstage:
102         list_for_each_entry_continue(dest, &svc->destinations, n_list) {
103                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
104                         continue;
105                 doh = ip_vs_wlc_dest_overhead(dest);
106                 if (loh * atomic_read(&dest->weight) >
107                     doh * atomic_read(&least->weight)) {
108                         least = dest;
109                         loh = doh;
110                 }
111         }
112
113         IP_VS_DBG(6, "WLC: server %u.%u.%u.%u:%u "
114                   "activeconns %d refcnt %d weight %d overhead %d\n",
115                   NIPQUAD(least->addr), ntohs(least->port),
116                   atomic_read(&least->activeconns),
117                   atomic_read(&least->refcnt),
118                   atomic_read(&least->weight), loh);
119
120         return least;
121 }
122
123
124 static struct ip_vs_scheduler ip_vs_wlc_scheduler =
125 {
126         .name =                 "wlc",
127         .refcnt =               ATOMIC_INIT(0),
128         .module =               THIS_MODULE,
129         .init_service =         ip_vs_wlc_init_svc,
130         .done_service =         ip_vs_wlc_done_svc,
131         .update_service =       ip_vs_wlc_update_svc,
132         .schedule =             ip_vs_wlc_schedule,
133 };
134
135
136 static int __init ip_vs_wlc_init(void)
137 {
138         INIT_LIST_HEAD(&ip_vs_wlc_scheduler.n_list);
139         return register_ip_vs_scheduler(&ip_vs_wlc_scheduler);
140 }
141
142 static void __exit ip_vs_wlc_cleanup(void)
143 {
144         unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler);
145 }
146
147 module_init(ip_vs_wlc_init);
148 module_exit(ip_vs_wlc_cleanup);
149 MODULE_LICENSE("GPL");