]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/ipvs/ip_vs_lc.c
IPVS: Change IPVS data structures to support IPv6 addresses
[net-next-2.6.git] / net / ipv4 / ipvs / ip_vs_lc.c
1 /*
2  * IPVS:        Least-Connection Scheduling module
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *
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.
10  *
11  * Changes:
12  *     Wensong Zhang            :     added the ip_vs_lc_update_svc
13  *     Wensong Zhang            :     added any dest with weight=0 is quiesced
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19
20 #include <net/ip_vs.h>
21
22
23 static inline unsigned int
24 ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
25 {
26         /*
27          * We think the overhead of processing active connections is 256
28          * times higher than that of inactive connections in average. (This
29          * 256 times might not be accurate, we will change it later) We
30          * use the following formula to estimate the overhead now:
31          *                dest->activeconns*256 + dest->inactconns
32          */
33         return (atomic_read(&dest->activeconns) << 8) +
34                 atomic_read(&dest->inactconns);
35 }
36
37
38 /*
39  *      Least Connection scheduling
40  */
41 static struct ip_vs_dest *
42 ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
43 {
44         struct ip_vs_dest *dest, *least = NULL;
45         unsigned int loh = 0, doh;
46
47         IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
48
49         /*
50          * Simply select the server with the least number of
51          *        (activeconns<<5) + inactconns
52          * Except whose weight is equal to zero.
53          * If the weight is equal to zero, it means that the server is
54          * quiesced, the existing connections to the server still get
55          * served, but no new connection is assigned to the server.
56          */
57
58         list_for_each_entry(dest, &svc->destinations, n_list) {
59                 if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
60                     atomic_read(&dest->weight) == 0)
61                         continue;
62                 doh = ip_vs_lc_dest_overhead(dest);
63                 if (!least || doh < loh) {
64                         least = dest;
65                         loh = doh;
66                 }
67         }
68
69         if (least)
70         IP_VS_DBG(6, "LC: server %u.%u.%u.%u:%u activeconns %d inactconns %d\n",
71                   NIPQUAD(least->addr.ip), ntohs(least->port),
72                   atomic_read(&least->activeconns),
73                   atomic_read(&least->inactconns));
74
75         return least;
76 }
77
78
79 static struct ip_vs_scheduler ip_vs_lc_scheduler = {
80         .name =                 "lc",
81         .refcnt =               ATOMIC_INIT(0),
82         .module =               THIS_MODULE,
83         .n_list =               LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
84         .schedule =             ip_vs_lc_schedule,
85 };
86
87
88 static int __init ip_vs_lc_init(void)
89 {
90         return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
91 }
92
93 static void __exit ip_vs_lc_cleanup(void)
94 {
95         unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
96 }
97
98 module_init(ip_vs_lc_init);
99 module_exit(ip_vs_lc_cleanup);
100 MODULE_LICENSE("GPL");