more changes on original files
[linux-2.4.git] / net / ipv4 / ipvs / ip_vs_nq.c
1 /*
2  * IPVS:        Never Queue scheduling module
3  *
4  * Version:     $Id: ip_vs_nq.c,v 1.1.2.1 2003/05/20 17:05:02 wensong Exp $
5  *
6  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
7  *
8  *              This program is free software; you can redistribute it and/or
9  *              modify it under the terms of the GNU General Public License
10  *              as published by the Free Software Foundation; either version
11  *              2 of the License, or (at your option) any later version.
12  *
13  * Changes:
14  *
15  */
16
17 /*
18  * The NQ algorithm adopts a two-speed model. When there is an idle server
19  * available, the job will be sent to the idle server, instead of waiting
20  * for a fast one. When there is no idle server available, the job will be
21  * sent to the server that minimize its expected delay (The Shortest
22  * Expected Delay scheduling algorithm).
23  *
24  * See the following paper for more information:
25  * A. Weinrib and S. Shenker, Greed is not enough: Adaptive load sharing
26  * in large heterogeneous systems. In Proceedings IEEE INFOCOM'88,
27  * pages 986-994, 1988.
28  *
29  * Thanks must go to Marko Buuri <marko@buuri.name> for talking NQ to me.
30  *
31  * The difference between NQ and SED is that NQ can improve overall
32  * system utilization.
33  *
34  */
35
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38
39 #include <net/ip_vs.h>
40
41
42 static int
43 ip_vs_nq_init_svc(struct ip_vs_service *svc)
44 {
45         return 0;
46 }
47
48
49 static int
50 ip_vs_nq_done_svc(struct ip_vs_service *svc)
51 {
52         return 0;
53 }
54
55
56 static int
57 ip_vs_nq_update_svc(struct ip_vs_service *svc)
58 {
59         return 0;
60 }
61
62
63 static inline unsigned int
64 ip_vs_nq_dest_overhead(struct ip_vs_dest *dest)
65 {
66         /*
67          * We only use the active connection number in the cost
68          * calculation here.
69          */
70         return atomic_read(&dest->activeconns) + 1;
71 }
72
73
74 /*
75  *      Weighted Least Connection scheduling
76  */
77 static struct ip_vs_dest *
78 ip_vs_nq_schedule(struct ip_vs_service *svc, struct iphdr *iph)
79 {
80         register struct list_head *l, *e;
81         struct ip_vs_dest *dest, *least;
82         unsigned int loh, doh;
83
84         IP_VS_DBG(6, "ip_vs_nq_schedule(): Scheduling...\n");
85
86         /*
87          * We calculate the load of each dest server as follows:
88          *      (server expected overhead) / dest->weight
89          *
90          * Remember -- no floats in kernel mode!!!
91          * The comparison of h1*w2 > h2*w1 is equivalent to that of
92          *                h1/w1 > h2/w2
93          * if every weight is larger than zero.
94          *
95          * The server with weight=0 is quiesced and will not receive any
96          * new connections.
97          */
98
99         l = &svc->destinations;
100         for (e=l->next; e!=l; e=e->next) {
101                 least = list_entry(e, struct ip_vs_dest, n_list);
102                 if (atomic_read(&least->weight) > 0) {
103                         loh = ip_vs_nq_dest_overhead(least);
104
105                         /* return the server directly if it is idle */
106                         if (atomic_read(&least->activeconns) == 0)
107                                 goto out;
108
109                         goto nextstage;
110                 }
111         }
112         return NULL;
113
114         /*
115          *    Find the destination with the least load.
116          */
117   nextstage:
118         for (e=e->next; e!=l; e=e->next) {
119                 dest = list_entry(e, struct ip_vs_dest, n_list);
120                 doh = ip_vs_nq_dest_overhead(dest);
121
122                 /* return the server directly if it is idle */
123                 if (atomic_read(&dest->activeconns) == 0) {
124                         least = dest;
125                         loh = doh;
126                         goto out;
127                 }
128
129                 if (loh * atomic_read(&dest->weight) >
130                     doh * atomic_read(&least->weight)) {
131                         least = dest;
132                         loh = doh;
133                 }
134         }
135
136   out:
137         IP_VS_DBG(6, "NQ: server %u.%u.%u.%u:%u "
138                   "activeconns %d refcnt %d weight %d overhead %d\n",
139                   NIPQUAD(least->addr), ntohs(least->port),
140                   atomic_read(&least->activeconns),
141                   atomic_read(&least->refcnt),
142                   atomic_read(&least->weight), loh);
143
144         return least;
145 }
146
147
148 static struct ip_vs_scheduler ip_vs_nq_scheduler =
149 {
150         .name =                 "nq",
151         .refcnt =               ATOMIC_INIT(0),
152         .module =               THIS_MODULE,
153         .init_service =         ip_vs_nq_init_svc,
154         .done_service =         ip_vs_nq_done_svc,
155         .update_service =       ip_vs_nq_update_svc,
156         .schedule =             ip_vs_nq_schedule,
157 };
158
159
160 static int __init ip_vs_nq_init(void)
161 {
162         INIT_LIST_HEAD(&ip_vs_nq_scheduler.n_list);
163         return register_ip_vs_scheduler(&ip_vs_nq_scheduler);
164 }
165
166 static void __exit ip_vs_nq_cleanup(void)
167 {
168         unregister_ip_vs_scheduler(&ip_vs_nq_scheduler);
169 }
170
171 module_init(ip_vs_nq_init);
172 module_exit(ip_vs_nq_cleanup);
173 MODULE_LICENSE("GPL");