blob: ebcdbf75ac6583cd76312f307975c04de8409a34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Least-Connection Scheduling module
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
23static int ip_vs_lc_init_svc(struct ip_vs_service *svc)
24{
25 return 0;
26}
27
28
29static int ip_vs_lc_done_svc(struct ip_vs_service *svc)
30{
31 return 0;
32}
33
34
35static int ip_vs_lc_update_svc(struct ip_vs_service *svc)
36{
37 return 0;
38}
39
40
41static inline unsigned int
42ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
43{
44 /*
45 * We think the overhead of processing active connections is 256
46 * times higher than that of inactive connections in average. (This
47 * 256 times might not be accurate, we will change it later) We
48 * use the following formula to estimate the overhead now:
49 * dest->activeconns*256 + dest->inactconns
50 */
51 return (atomic_read(&dest->activeconns) << 8) +
52 atomic_read(&dest->inactconns);
53}
54
55
56/*
57 * Least Connection scheduling
58 */
59static struct ip_vs_dest *
60ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
61{
62 struct ip_vs_dest *dest, *least = NULL;
63 unsigned int loh = 0, doh;
64
65 IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
66
67 /*
68 * Simply select the server with the least number of
69 * (activeconns<<5) + inactconns
70 * Except whose weight is equal to zero.
71 * If the weight is equal to zero, it means that the server is
72 * quiesced, the existing connections to the server still get
73 * served, but no new connection is assigned to the server.
74 */
75
76 list_for_each_entry(dest, &svc->destinations, n_list) {
77 if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
78 atomic_read(&dest->weight) == 0)
79 continue;
80 doh = ip_vs_lc_dest_overhead(dest);
81 if (!least || doh < loh) {
82 least = dest;
83 loh = doh;
84 }
85 }
86
87 if (least)
88 IP_VS_DBG(6, "LC: server %u.%u.%u.%u:%u activeconns %d inactconns %d\n",
89 NIPQUAD(least->addr), ntohs(least->port),
90 atomic_read(&least->activeconns),
91 atomic_read(&least->inactconns));
92
93 return least;
94}
95
96
97static struct ip_vs_scheduler ip_vs_lc_scheduler = {
98 .name = "lc",
99 .refcnt = ATOMIC_INIT(0),
100 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000101 .n_list = LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .init_service = ip_vs_lc_init_svc,
103 .done_service = ip_vs_lc_done_svc,
104 .update_service = ip_vs_lc_update_svc,
105 .schedule = ip_vs_lc_schedule,
106};
107
108
109static int __init ip_vs_lc_init(void)
110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
112}
113
114static void __exit ip_vs_lc_cleanup(void)
115{
116 unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
117}
118
119module_init(ip_vs_lc_init);
120module_exit(ip_vs_lc_cleanup);
121MODULE_LICENSE("GPL");