2 * IPVS: Weighted Least-Connection Scheduling module
4 * Version: $Id: ip_vs_wlc.c,v 1.13 2003/04/18 09:03:16 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
7 * Peter Kese <peter.kese@ijs.si>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 * Wensong Zhang : changed the ip_vs_wlc_schedule to return dest
16 * Wensong Zhang : changed to use the inactconns in scheduling
17 * Wensong Zhang : changed some comestics things for debugging
18 * Wensong Zhang : changed for the d-linked destination list
19 * Wensong Zhang : added the ip_vs_wlc_update_svc
20 * Wensong Zhang : added any dest with weight=0 is quiesced
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
31 #include <net/ip_vs.h>
35 ip_vs_wlc_init_svc(struct ip_vs_service
*svc
)
42 ip_vs_wlc_done_svc(struct ip_vs_service
*svc
)
49 ip_vs_wlc_update_svc(struct ip_vs_service
*svc
)
55 static inline unsigned int
56 ip_vs_wlc_dest_overhead(struct ip_vs_dest
*dest
)
59 * We think the overhead of processing active connections is 256
60 * times higher than that of inactive connections in average. (This
61 * 256 times might not be accurate, we will change it later) We
62 * use the following formula to estimate the overhead now:
63 * dest->activeconns*256 + dest->inactconns
65 return (atomic_read(&dest
->activeconns
) << 8) +
66 atomic_read(&dest
->inactconns
);
71 * Weighted Least Connection scheduling
73 static struct ip_vs_dest
*
74 ip_vs_wlc_schedule(struct ip_vs_service
*svc
, struct iphdr
*iph
)
76 register struct list_head
*l
, *e
;
77 struct ip_vs_dest
*dest
, *least
;
78 unsigned int loh
, doh
;
80 IP_VS_DBG(6, "ip_vs_wlc_schedule(): Scheduling...\n");
83 * We calculate the load of each dest server as follows:
84 * (dest overhead) / dest->weight
86 * Remember -- no floats in kernel mode!!!
87 * The comparison of h1*w2 > h2*w1 is equivalent to that of
89 * if every weight is larger than zero.
91 * The server with weight=0 is quiesced and will not receive any
95 l
= &svc
->destinations
;
96 for (e
=l
->next
; e
!=l
; e
=e
->next
) {
97 least
= list_entry(e
, struct ip_vs_dest
, n_list
);
98 if (!(least
->flags
& IP_VS_DEST_F_OVERLOAD
) &&
99 atomic_read(&least
->weight
) > 0) {
100 loh
= ip_vs_wlc_dest_overhead(least
);
107 * Find the destination with the least load.
110 for (e
=e
->next
; e
!=l
; e
=e
->next
) {
111 dest
= list_entry(e
, struct ip_vs_dest
, n_list
);
113 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
115 doh
= ip_vs_wlc_dest_overhead(dest
);
116 if (loh
* atomic_read(&dest
->weight
) >
117 doh
* atomic_read(&least
->weight
)) {
123 IP_VS_DBG(6, "WLC: server %u.%u.%u.%u:%u "
124 "activeconns %d refcnt %d weight %d overhead %d\n",
125 NIPQUAD(least
->addr
), ntohs(least
->port
),
126 atomic_read(&least
->activeconns
),
127 atomic_read(&least
->refcnt
),
128 atomic_read(&least
->weight
), loh
);
134 static struct ip_vs_scheduler ip_vs_wlc_scheduler
=
137 .refcnt
= ATOMIC_INIT(0),
138 .module
= THIS_MODULE
,
139 .init_service
= ip_vs_wlc_init_svc
,
140 .done_service
= ip_vs_wlc_done_svc
,
141 .update_service
= ip_vs_wlc_update_svc
,
142 .schedule
= ip_vs_wlc_schedule
,
146 static int __init
ip_vs_wlc_init(void)
148 INIT_LIST_HEAD(&ip_vs_wlc_scheduler
.n_list
);
149 return register_ip_vs_scheduler(&ip_vs_wlc_scheduler
);
152 static void __exit
ip_vs_wlc_cleanup(void)
154 unregister_ip_vs_scheduler(&ip_vs_wlc_scheduler
);
157 module_init(ip_vs_wlc_init
);
158 module_exit(ip_vs_wlc_cleanup
);
159 MODULE_LICENSE("GPL");