blob: d0dadc8a65fda50d8b26d1e64f66d8c1a854baa0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static inline unsigned int
24ip_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 */
41static struct ip_vs_dest *
42ip_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
Simon Horman68888d12008-12-29 18:37:36 -080069 if (!least)
70 IP_VS_ERR_RL("LC: no destination available\n");
71 else
72 IP_VS_DBG_BUF(6, "LC: server %s:%u activeconns %d "
73 "inactconns %d\n",
74 IP_VS_DBG_ADDR(svc->af, &least->addr),
75 ntohs(least->port),
76 atomic_read(&least->activeconns),
77 atomic_read(&least->inactconns));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 return least;
80}
81
82
83static struct ip_vs_scheduler ip_vs_lc_scheduler = {
84 .name = "lc",
85 .refcnt = ATOMIC_INIT(0),
86 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +000087 .n_list = LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .schedule = ip_vs_lc_schedule,
89};
90
91
92static int __init ip_vs_lc_init(void)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
95}
96
97static void __exit ip_vs_lc_cleanup(void)
98{
99 unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
100}
101
102module_init(ip_vs_lc_init);
103module_exit(ip_vs_lc_cleanup);
104MODULE_LICENSE("GPL");