import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / ipv4 / ipvs / ip_vs_lc.c
1 /*
2  * IPVS:        Least-Connection Scheduling module
3  *
4  * Version:     $Id: ip_vs_lc.c,v 1.8.2.1 2003/04/11 14:02:35 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  *     Wensong Zhang            :     added the ip_vs_lc_update_svc
15  *     Wensong Zhang            :     added any dest with weight=0 is quiesced
16  *
17  */
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21
22 #include <net/ip_vs.h>
23
24
25 static int ip_vs_lc_init_svc(struct ip_vs_service *svc)
26 {
27         return 0;
28 }
29
30
31 static int ip_vs_lc_done_svc(struct ip_vs_service *svc)
32 {
33         return 0;
34 }
35
36
37 static int ip_vs_lc_update_svc(struct ip_vs_service *svc)
38 {
39         return 0;
40 }
41
42
43 static inline unsigned int
44 ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
45 {
46         /*
47          * We think the overhead of processing active connections is 256
48          * times higher than that of inactive connections in average. (This
49          * 256 times might not be accurate, we will change it later) We
50          * use the following formula to estimate the overhead now:
51          *                dest->activeconns*256 + dest->inactconns
52          */
53         return (atomic_read(&dest->activeconns) << 8) +
54                 atomic_read(&dest->inactconns);
55 }
56
57
58 /*
59  *      Least Connection scheduling
60  */
61 static struct ip_vs_dest *
62 ip_vs_lc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
63 {
64         struct list_head *l, *e;
65         struct ip_vs_dest *dest, *least;
66         unsigned int loh, doh;
67
68         IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
69
70         /*
71          * Simply select the server with the least number of
72          *        (activeconns<<5) + inactconns
73          * Except whose weight is equal to zero.
74          * If the weight is equal to zero, it means that the server is
75          * quiesced, the existing connections to the server still get
76          * served, but no new connection is assigned to the server.
77          */
78
79         l = &svc->destinations;
80         for (e=l->next; e!=l; e=e->next) {
81                 least = list_entry (e, struct ip_vs_dest, n_list);
82                 if (atomic_read(&least->weight) > 0) {
83                         loh = ip_vs_lc_dest_overhead(least);
84                         goto nextstage;
85                 }
86         }
87         return NULL;
88
89         /*
90          *    Find the destination with the least load.
91          */
92   nextstage:
93         for (e=e->next; e!=l; e=e->next) {
94                 dest = list_entry(e, struct ip_vs_dest, n_list);
95                 if (atomic_read(&dest->weight) == 0)
96                         continue;
97                 doh = ip_vs_lc_dest_overhead(dest);
98                 if (doh < loh) {
99                         least = dest;
100                         loh = doh;
101                 }
102         }
103
104         IP_VS_DBG(6, "LC: server %u.%u.%u.%u:%u activeconns %d inactconns %d\n",
105                   NIPQUAD(least->addr), ntohs(least->port),
106                   atomic_read(&least->activeconns),
107                   atomic_read(&least->inactconns));
108
109         return least;
110 }
111
112
113 static struct ip_vs_scheduler ip_vs_lc_scheduler = {
114         {0},                    /* n_list */
115         "lc",                   /* name */
116         ATOMIC_INIT(0),         /* refcnt */
117         THIS_MODULE,            /* this module */
118         ip_vs_lc_init_svc,      /* service initializer */
119         ip_vs_lc_done_svc,      /* service done */
120         ip_vs_lc_update_svc,    /* service updater */
121         ip_vs_lc_schedule,      /* select a server from the destination list */
122 };
123
124
125 static int __init ip_vs_lc_init(void)
126 {
127         INIT_LIST_HEAD(&ip_vs_lc_scheduler.n_list);
128         return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
129 }
130
131 static void __exit ip_vs_lc_cleanup(void)
132 {
133         unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
134 }
135
136 module_init(ip_vs_lc_init);
137 module_exit(ip_vs_lc_cleanup);
138 MODULE_LICENSE("GPL");