2 * IPVS: Locality-Based Least-Connection scheduling module
4 * Version: $Id: ip_vs_lblc.c,v 1.10 2002/09/15 08:14:08 wensong Exp $
6 * Authors: Wensong Zhang <wensong@gnuchina.org>
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.
14 * Martin Hamilton : fixed the terrible locking bugs
15 * *lock(tbl->lock) ==> *lock(&tbl->lock)
16 * Wensong Zhang : fixed the uninitilized tbl->lock bug
17 * Wensong Zhang : added doing full expiration check to
18 * collect stale entries of 24+ hours when
19 * no partial expire check in a half hour
20 * Julian Anastasov : replaced del_timer call with del_timer_sync
21 * to avoid the possible race between timer
22 * handler and del_timer thread in SMP
27 * The lblc algorithm is as follows (pseudo code):
29 * if cachenode[dest_ip] is null then
30 * n, cachenode[dest_ip] <- {weighted least-conn node};
32 * n <- cachenode[dest_ip];
34 * (n.conns>n.weight AND
35 * there is a node m with m.conns<m.weight/2) then
36 * n, cachenode[dest_ip] <- {weighted least-conn node};
40 * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing
41 * me to write this module.
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/skbuff.h>
48 #include <linux/jiffies.h>
52 #include <linux/sysctl.h>
54 #include <net/ip_vs.h>
58 * It is for garbage collection of stale IPVS lblc entries,
59 * when the table is full.
61 #define CHECK_EXPIRE_INTERVAL (60*HZ)
62 #define ENTRY_TIMEOUT (6*60*HZ)
65 * It is for full expiration check.
66 * When there is no partial expiration check (garbage collection)
67 * in a half hour, do a full expiration check to collect stale
68 * entries that haven't been touched for a day.
70 #define COUNT_FOR_FULL_EXPIRATION 30
71 static int sysctl_ip_vs_lblc_expiration
= 24*60*60*HZ
;
75 * for IPVS lblc entry hash table
77 #ifndef CONFIG_IP_VS_LBLC_TAB_BITS
78 #define CONFIG_IP_VS_LBLC_TAB_BITS 10
80 #define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS
81 #define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)
82 #define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)
86 * IPVS lblc entry represents an association between destination
87 * IP address and its destination server
89 struct ip_vs_lblc_entry
{
90 struct list_head list
;
91 __be32 addr
; /* destination IP address */
92 struct ip_vs_dest
*dest
; /* real server (cache) */
93 unsigned long lastuse
; /* last used time */
98 * IPVS lblc hash table
100 struct ip_vs_lblc_table
{
101 rwlock_t lock
; /* lock for this table */
102 struct list_head bucket
[IP_VS_LBLC_TAB_SIZE
]; /* hash bucket */
103 atomic_t entries
; /* number of entries */
104 int max_size
; /* maximum size of entries */
105 struct timer_list periodic_timer
; /* collect stale entries */
106 int rover
; /* rover for expire check */
107 int counter
; /* counter for no expire */
112 * IPVS LBLC sysctl table
115 static ctl_table vs_vars_table
[] = {
117 .procname
= "lblc_expiration",
118 .data
= &sysctl_ip_vs_lblc_expiration
,
119 .maxlen
= sizeof(int),
121 .proc_handler
= &proc_dointvec_jiffies
,
126 static struct ctl_table_header
* sysctl_header
;
129 * new/free a ip_vs_lblc_entry, which is a mapping of a destionation
130 * IP address to a server.
132 static inline struct ip_vs_lblc_entry
*
133 ip_vs_lblc_new(__be32 daddr
, struct ip_vs_dest
*dest
)
135 struct ip_vs_lblc_entry
*en
;
137 en
= kmalloc(sizeof(struct ip_vs_lblc_entry
), GFP_ATOMIC
);
139 IP_VS_ERR("ip_vs_lblc_new(): no memory\n");
143 INIT_LIST_HEAD(&en
->list
);
146 atomic_inc(&dest
->refcnt
);
153 static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry
*en
)
157 * We don't kfree dest because it is refered either by its service
158 * or the trash dest list.
160 atomic_dec(&en
->dest
->refcnt
);
166 * Returns hash value for IPVS LBLC entry
168 static inline unsigned ip_vs_lblc_hashkey(__be32 addr
)
170 return (ntohl(addr
)*2654435761UL) & IP_VS_LBLC_TAB_MASK
;
175 * Hash an entry in the ip_vs_lblc_table.
176 * returns bool success.
179 ip_vs_lblc_hash(struct ip_vs_lblc_table
*tbl
, struct ip_vs_lblc_entry
*en
)
183 if (!list_empty(&en
->list
)) {
184 IP_VS_ERR("ip_vs_lblc_hash(): request for already hashed, "
185 "called from %p\n", __builtin_return_address(0));
190 * Hash by destination IP address
192 hash
= ip_vs_lblc_hashkey(en
->addr
);
194 write_lock(&tbl
->lock
);
195 list_add(&en
->list
, &tbl
->bucket
[hash
]);
196 atomic_inc(&tbl
->entries
);
197 write_unlock(&tbl
->lock
);
204 * Get ip_vs_lblc_entry associated with supplied parameters.
206 static inline struct ip_vs_lblc_entry
*
207 ip_vs_lblc_get(struct ip_vs_lblc_table
*tbl
, __be32 addr
)
210 struct ip_vs_lblc_entry
*en
;
212 hash
= ip_vs_lblc_hashkey(addr
);
214 read_lock(&tbl
->lock
);
216 list_for_each_entry(en
, &tbl
->bucket
[hash
], list
) {
217 if (en
->addr
== addr
) {
219 read_unlock(&tbl
->lock
);
224 read_unlock(&tbl
->lock
);
231 * Flush all the entries of the specified table.
233 static void ip_vs_lblc_flush(struct ip_vs_lblc_table
*tbl
)
236 struct ip_vs_lblc_entry
*en
, *nxt
;
238 for (i
=0; i
<IP_VS_LBLC_TAB_SIZE
; i
++) {
239 write_lock(&tbl
->lock
);
240 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[i
], list
) {
242 atomic_dec(&tbl
->entries
);
244 write_unlock(&tbl
->lock
);
249 static inline void ip_vs_lblc_full_check(struct ip_vs_lblc_table
*tbl
)
251 unsigned long now
= jiffies
;
253 struct ip_vs_lblc_entry
*en
, *nxt
;
255 for (i
=0, j
=tbl
->rover
; i
<IP_VS_LBLC_TAB_SIZE
; i
++) {
256 j
= (j
+ 1) & IP_VS_LBLC_TAB_MASK
;
258 write_lock(&tbl
->lock
);
259 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[j
], list
) {
261 en
->lastuse
+ sysctl_ip_vs_lblc_expiration
))
265 atomic_dec(&tbl
->entries
);
267 write_unlock(&tbl
->lock
);
274 * Periodical timer handler for IPVS lblc table
275 * It is used to collect stale entries when the number of entries
276 * exceeds the maximum size of the table.
278 * Fixme: we probably need more complicated algorithm to collect
279 * entries that have not been used for a long time even
280 * if the number of entries doesn't exceed the maximum size
282 * The full expiration check is for this purpose now.
284 static void ip_vs_lblc_check_expire(unsigned long data
)
286 struct ip_vs_lblc_table
*tbl
;
287 unsigned long now
= jiffies
;
290 struct ip_vs_lblc_entry
*en
, *nxt
;
292 tbl
= (struct ip_vs_lblc_table
*)data
;
294 if ((tbl
->counter
% COUNT_FOR_FULL_EXPIRATION
) == 0) {
295 /* do full expiration check */
296 ip_vs_lblc_full_check(tbl
);
301 if (atomic_read(&tbl
->entries
) <= tbl
->max_size
) {
306 goal
= (atomic_read(&tbl
->entries
) - tbl
->max_size
)*4/3;
307 if (goal
> tbl
->max_size
/2)
308 goal
= tbl
->max_size
/2;
310 for (i
=0, j
=tbl
->rover
; i
<IP_VS_LBLC_TAB_SIZE
; i
++) {
311 j
= (j
+ 1) & IP_VS_LBLC_TAB_MASK
;
313 write_lock(&tbl
->lock
);
314 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[j
], list
) {
315 if (time_before(now
, en
->lastuse
+ ENTRY_TIMEOUT
))
319 atomic_dec(&tbl
->entries
);
322 write_unlock(&tbl
->lock
);
329 mod_timer(&tbl
->periodic_timer
, jiffies
+CHECK_EXPIRE_INTERVAL
);
333 static int ip_vs_lblc_init_svc(struct ip_vs_service
*svc
)
336 struct ip_vs_lblc_table
*tbl
;
339 * Allocate the ip_vs_lblc_table for this service
341 tbl
= kmalloc(sizeof(struct ip_vs_lblc_table
), GFP_ATOMIC
);
343 IP_VS_ERR("ip_vs_lblc_init_svc(): no memory\n");
346 svc
->sched_data
= tbl
;
347 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
349 sizeof(struct ip_vs_lblc_table
));
352 * Initialize the hash buckets
354 for (i
=0; i
<IP_VS_LBLC_TAB_SIZE
; i
++) {
355 INIT_LIST_HEAD(&tbl
->bucket
[i
]);
357 rwlock_init(&tbl
->lock
);
358 tbl
->max_size
= IP_VS_LBLC_TAB_SIZE
*16;
363 * Hook periodic timer for garbage collection
365 setup_timer(&tbl
->periodic_timer
, ip_vs_lblc_check_expire
,
367 tbl
->periodic_timer
.expires
= jiffies
+CHECK_EXPIRE_INTERVAL
;
368 add_timer(&tbl
->periodic_timer
);
374 static int ip_vs_lblc_done_svc(struct ip_vs_service
*svc
)
376 struct ip_vs_lblc_table
*tbl
= svc
->sched_data
;
378 /* remove periodic timer */
379 del_timer_sync(&tbl
->periodic_timer
);
381 /* got to clean up table entries here */
382 ip_vs_lblc_flush(tbl
);
384 /* release the table itself */
385 kfree(svc
->sched_data
);
386 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
387 sizeof(struct ip_vs_lblc_table
));
393 static int ip_vs_lblc_update_svc(struct ip_vs_service
*svc
)
399 static inline struct ip_vs_dest
*
400 __ip_vs_wlc_schedule(struct ip_vs_service
*svc
, struct iphdr
*iph
)
402 struct ip_vs_dest
*dest
, *least
;
406 * We think the overhead of processing active connections is fifty
407 * times higher than that of inactive connections in average. (This
408 * fifty times might not be accurate, we will change it later.) We
409 * use the following formula to estimate the overhead:
410 * dest->activeconns*50 + dest->inactconns
412 * (dest overhead) / dest->weight
414 * Remember -- no floats in kernel mode!!!
415 * The comparison of h1*w2 > h2*w1 is equivalent to that of
417 * if every weight is larger than zero.
419 * The server with weight=0 is quiesced and will not receive any
422 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
423 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
425 if (atomic_read(&dest
->weight
) > 0) {
427 loh
= atomic_read(&least
->activeconns
) * 50
428 + atomic_read(&least
->inactconns
);
435 * Find the destination with the least load.
438 list_for_each_entry_continue(dest
, &svc
->destinations
, n_list
) {
439 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
442 doh
= atomic_read(&dest
->activeconns
) * 50
443 + atomic_read(&dest
->inactconns
);
444 if (loh
* atomic_read(&dest
->weight
) >
445 doh
* atomic_read(&least
->weight
)) {
451 IP_VS_DBG(6, "LBLC: server %d.%d.%d.%d:%d "
452 "activeconns %d refcnt %d weight %d overhead %d\n",
453 NIPQUAD(least
->addr
), ntohs(least
->port
),
454 atomic_read(&least
->activeconns
),
455 atomic_read(&least
->refcnt
),
456 atomic_read(&least
->weight
), loh
);
463 * If this destination server is overloaded and there is a less loaded
464 * server, then return true.
467 is_overloaded(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
469 if (atomic_read(&dest
->activeconns
) > atomic_read(&dest
->weight
)) {
470 struct ip_vs_dest
*d
;
472 list_for_each_entry(d
, &svc
->destinations
, n_list
) {
473 if (atomic_read(&d
->activeconns
)*2
474 < atomic_read(&d
->weight
)) {
484 * Locality-Based (weighted) Least-Connection scheduling
486 static struct ip_vs_dest
*
487 ip_vs_lblc_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
)
489 struct ip_vs_dest
*dest
;
490 struct ip_vs_lblc_table
*tbl
;
491 struct ip_vs_lblc_entry
*en
;
492 struct iphdr
*iph
= ip_hdr(skb
);
494 IP_VS_DBG(6, "ip_vs_lblc_schedule(): Scheduling...\n");
496 tbl
= (struct ip_vs_lblc_table
*)svc
->sched_data
;
497 en
= ip_vs_lblc_get(tbl
, iph
->daddr
);
499 dest
= __ip_vs_wlc_schedule(svc
, iph
);
501 IP_VS_DBG(1, "no destination available\n");
504 en
= ip_vs_lblc_new(iph
->daddr
, dest
);
508 ip_vs_lblc_hash(tbl
, en
);
511 if (!(dest
->flags
& IP_VS_DEST_F_AVAILABLE
)
512 || atomic_read(&dest
->weight
) <= 0
513 || is_overloaded(dest
, svc
)) {
514 dest
= __ip_vs_wlc_schedule(svc
, iph
);
516 IP_VS_DBG(1, "no destination available\n");
519 atomic_dec(&en
->dest
->refcnt
);
520 atomic_inc(&dest
->refcnt
);
524 en
->lastuse
= jiffies
;
526 IP_VS_DBG(6, "LBLC: destination IP address %u.%u.%u.%u "
527 "--> server %u.%u.%u.%u:%d\n",
537 * IPVS LBLC Scheduler structure
539 static struct ip_vs_scheduler ip_vs_lblc_scheduler
=
542 .refcnt
= ATOMIC_INIT(0),
543 .module
= THIS_MODULE
,
544 .init_service
= ip_vs_lblc_init_svc
,
545 .done_service
= ip_vs_lblc_done_svc
,
546 .update_service
= ip_vs_lblc_update_svc
,
547 .schedule
= ip_vs_lblc_schedule
,
551 static int __init
ip_vs_lblc_init(void)
555 INIT_LIST_HEAD(&ip_vs_lblc_scheduler
.n_list
);
556 sysctl_header
= register_sysctl_paths(net_vs_ctl_path
, vs_vars_table
);
557 ret
= register_ip_vs_scheduler(&ip_vs_lblc_scheduler
);
559 unregister_sysctl_table(sysctl_header
);
564 static void __exit
ip_vs_lblc_cleanup(void)
566 unregister_sysctl_table(sysctl_header
);
567 unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler
);
571 module_init(ip_vs_lblc_init
);
572 module_exit(ip_vs_lblc_cleanup
);
573 MODULE_LICENSE("GPL");