2 * IPVS: Locality-Based Least-Connection with Replication scheduler
4 * Authors: Wensong Zhang <wensong@gnuchina.org>
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.
12 * Julian Anastasov : Added the missing (dest->weight>0)
13 * condition in the ip_vs_dest_set_max.
18 * The lblc/r algorithm is as follows (pseudo code):
20 * if serverSet[dest_ip] is null then
21 * n, serverSet[dest_ip] <- {weighted least-conn node};
23 * n <- {least-conn (alive) node in serverSet[dest_ip]};
25 * (n.conns>n.weight AND
26 * there is a node m with m.conns<m.weight/2) then
27 * n <- {weighted least-conn node};
28 * add n to serverSet[dest_ip];
29 * if |serverSet[dest_ip]| > 1 AND
30 * now - serverSet[dest_ip].lastMod > T then
31 * m <- {most conn node in serverSet[dest_ip]};
32 * remove m from serverSet[dest_ip];
33 * if serverSet[dest_ip] changed then
34 * serverSet[dest_ip].lastMod <- now;
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/skbuff.h>
44 #include <linux/jiffies.h>
48 #include <linux/sysctl.h>
49 #include <net/net_namespace.h>
51 #include <net/ip_vs.h>
55 * It is for garbage collection of stale IPVS lblcr entries,
56 * when the table is full.
58 #define CHECK_EXPIRE_INTERVAL (60*HZ)
59 #define ENTRY_TIMEOUT (6*60*HZ)
62 * It is for full expiration check.
63 * When there is no partial expiration check (garbage collection)
64 * in a half hour, do a full expiration check to collect stale
65 * entries that haven't been touched for a day.
67 #define COUNT_FOR_FULL_EXPIRATION 30
68 static int sysctl_ip_vs_lblcr_expiration
= 24*60*60*HZ
;
72 * for IPVS lblcr entry hash table
74 #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
75 #define CONFIG_IP_VS_LBLCR_TAB_BITS 10
77 #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS
78 #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS)
79 #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1)
83 * IPVS destination set structure and operations
85 struct ip_vs_dest_list
{
86 struct ip_vs_dest_list
*next
; /* list link */
87 struct ip_vs_dest
*dest
; /* destination server */
90 struct ip_vs_dest_set
{
91 atomic_t size
; /* set size */
92 unsigned long lastmod
; /* last modified time */
93 struct ip_vs_dest_list
*list
; /* destination list */
94 rwlock_t lock
; /* lock for this list */
98 static struct ip_vs_dest_list
*
99 ip_vs_dest_set_insert(struct ip_vs_dest_set
*set
, struct ip_vs_dest
*dest
)
101 struct ip_vs_dest_list
*e
;
103 for (e
=set
->list
; e
!=NULL
; e
=e
->next
) {
105 /* already existed */
109 e
= kmalloc(sizeof(struct ip_vs_dest_list
), GFP_ATOMIC
);
111 IP_VS_ERR("ip_vs_dest_set_insert(): no memory\n");
115 atomic_inc(&dest
->refcnt
);
118 /* link it to the list */
119 write_lock(&set
->lock
);
122 atomic_inc(&set
->size
);
123 write_unlock(&set
->lock
);
125 set
->lastmod
= jiffies
;
130 ip_vs_dest_set_erase(struct ip_vs_dest_set
*set
, struct ip_vs_dest
*dest
)
132 struct ip_vs_dest_list
*e
, **ep
;
134 write_lock(&set
->lock
);
135 for (ep
=&set
->list
, e
=*ep
; e
!=NULL
; e
=*ep
) {
136 if (e
->dest
== dest
) {
139 atomic_dec(&set
->size
);
140 set
->lastmod
= jiffies
;
141 atomic_dec(&e
->dest
->refcnt
);
147 write_unlock(&set
->lock
);
150 static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set
*set
)
152 struct ip_vs_dest_list
*e
, **ep
;
154 write_lock(&set
->lock
);
155 for (ep
=&set
->list
, e
=*ep
; e
!=NULL
; e
=*ep
) {
158 * We don't kfree dest because it is refered either
159 * by its service or by the trash dest list.
161 atomic_dec(&e
->dest
->refcnt
);
164 write_unlock(&set
->lock
);
167 /* get weighted least-connection node in the destination set */
168 static inline struct ip_vs_dest
*ip_vs_dest_set_min(struct ip_vs_dest_set
*set
)
170 register struct ip_vs_dest_list
*e
;
171 struct ip_vs_dest
*dest
, *least
;
177 read_lock(&set
->lock
);
178 /* select the first destination server, whose weight > 0 */
179 for (e
=set
->list
; e
!=NULL
; e
=e
->next
) {
181 if (least
->flags
& IP_VS_DEST_F_OVERLOAD
)
184 if ((atomic_read(&least
->weight
) > 0)
185 && (least
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
186 loh
= atomic_read(&least
->activeconns
) * 50
187 + atomic_read(&least
->inactconns
);
191 read_unlock(&set
->lock
);
194 /* find the destination with the weighted least load */
196 for (e
=e
->next
; e
!=NULL
; e
=e
->next
) {
198 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
201 doh
= atomic_read(&dest
->activeconns
) * 50
202 + atomic_read(&dest
->inactconns
);
203 if ((loh
* atomic_read(&dest
->weight
) >
204 doh
* atomic_read(&least
->weight
))
205 && (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
210 read_unlock(&set
->lock
);
212 IP_VS_DBG(6, "ip_vs_dest_set_min: server %d.%d.%d.%d:%d "
213 "activeconns %d refcnt %d weight %d overhead %d\n",
214 NIPQUAD(least
->addr
), ntohs(least
->port
),
215 atomic_read(&least
->activeconns
),
216 atomic_read(&least
->refcnt
),
217 atomic_read(&least
->weight
), loh
);
222 /* get weighted most-connection node in the destination set */
223 static inline struct ip_vs_dest
*ip_vs_dest_set_max(struct ip_vs_dest_set
*set
)
225 register struct ip_vs_dest_list
*e
;
226 struct ip_vs_dest
*dest
, *most
;
232 read_lock(&set
->lock
);
233 /* select the first destination server, whose weight > 0 */
234 for (e
=set
->list
; e
!=NULL
; e
=e
->next
) {
236 if (atomic_read(&most
->weight
) > 0) {
237 moh
= atomic_read(&most
->activeconns
) * 50
238 + atomic_read(&most
->inactconns
);
242 read_unlock(&set
->lock
);
245 /* find the destination with the weighted most load */
247 for (e
=e
->next
; e
!=NULL
; e
=e
->next
) {
249 doh
= atomic_read(&dest
->activeconns
) * 50
250 + atomic_read(&dest
->inactconns
);
251 /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
252 if ((moh
* atomic_read(&dest
->weight
) <
253 doh
* atomic_read(&most
->weight
))
254 && (atomic_read(&dest
->weight
) > 0)) {
259 read_unlock(&set
->lock
);
261 IP_VS_DBG(6, "ip_vs_dest_set_max: server %d.%d.%d.%d:%d "
262 "activeconns %d refcnt %d weight %d overhead %d\n",
263 NIPQUAD(most
->addr
), ntohs(most
->port
),
264 atomic_read(&most
->activeconns
),
265 atomic_read(&most
->refcnt
),
266 atomic_read(&most
->weight
), moh
);
272 * IPVS lblcr entry represents an association between destination
273 * IP address and its destination server set
275 struct ip_vs_lblcr_entry
{
276 struct list_head list
;
277 __be32 addr
; /* destination IP address */
278 struct ip_vs_dest_set set
; /* destination server set */
279 unsigned long lastuse
; /* last used time */
284 * IPVS lblcr hash table
286 struct ip_vs_lblcr_table
{
287 rwlock_t lock
; /* lock for this table */
288 struct list_head bucket
[IP_VS_LBLCR_TAB_SIZE
]; /* hash bucket */
289 atomic_t entries
; /* number of entries */
290 int max_size
; /* maximum size of entries */
291 struct timer_list periodic_timer
; /* collect stale entries */
292 int rover
; /* rover for expire check */
293 int counter
; /* counter for no expire */
298 * IPVS LBLCR sysctl table
301 static ctl_table vs_vars_table
[] = {
303 .procname
= "lblcr_expiration",
304 .data
= &sysctl_ip_vs_lblcr_expiration
,
305 .maxlen
= sizeof(int),
307 .proc_handler
= &proc_dointvec_jiffies
,
312 static struct ctl_table_header
* sysctl_header
;
315 * new/free a ip_vs_lblcr_entry, which is a mapping of a destination
316 * IP address to a server.
318 static inline struct ip_vs_lblcr_entry
*ip_vs_lblcr_new(__be32 daddr
)
320 struct ip_vs_lblcr_entry
*en
;
322 en
= kmalloc(sizeof(struct ip_vs_lblcr_entry
), GFP_ATOMIC
);
324 IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
328 INIT_LIST_HEAD(&en
->list
);
331 /* initilize its dest set */
332 atomic_set(&(en
->set
.size
), 0);
334 rwlock_init(&en
->set
.lock
);
340 static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry
*en
)
343 ip_vs_dest_set_eraseall(&en
->set
);
349 * Returns hash value for IPVS LBLCR entry
351 static inline unsigned ip_vs_lblcr_hashkey(__be32 addr
)
353 return (ntohl(addr
)*2654435761UL) & IP_VS_LBLCR_TAB_MASK
;
358 * Hash an entry in the ip_vs_lblcr_table.
359 * returns bool success.
362 ip_vs_lblcr_hash(struct ip_vs_lblcr_table
*tbl
, struct ip_vs_lblcr_entry
*en
)
366 if (!list_empty(&en
->list
)) {
367 IP_VS_ERR("ip_vs_lblcr_hash(): request for already hashed, "
368 "called from %p\n", __builtin_return_address(0));
373 * Hash by destination IP address
375 hash
= ip_vs_lblcr_hashkey(en
->addr
);
377 write_lock(&tbl
->lock
);
378 list_add(&en
->list
, &tbl
->bucket
[hash
]);
379 atomic_inc(&tbl
->entries
);
380 write_unlock(&tbl
->lock
);
387 * Get ip_vs_lblcr_entry associated with supplied parameters.
389 static inline struct ip_vs_lblcr_entry
*
390 ip_vs_lblcr_get(struct ip_vs_lblcr_table
*tbl
, __be32 addr
)
393 struct ip_vs_lblcr_entry
*en
;
395 hash
= ip_vs_lblcr_hashkey(addr
);
397 read_lock(&tbl
->lock
);
399 list_for_each_entry(en
, &tbl
->bucket
[hash
], list
) {
400 if (en
->addr
== addr
) {
402 read_unlock(&tbl
->lock
);
407 read_unlock(&tbl
->lock
);
414 * Flush all the entries of the specified table.
416 static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table
*tbl
)
419 struct ip_vs_lblcr_entry
*en
, *nxt
;
421 for (i
=0; i
<IP_VS_LBLCR_TAB_SIZE
; i
++) {
422 write_lock(&tbl
->lock
);
423 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[i
], list
) {
424 ip_vs_lblcr_free(en
);
425 atomic_dec(&tbl
->entries
);
427 write_unlock(&tbl
->lock
);
432 static inline void ip_vs_lblcr_full_check(struct ip_vs_lblcr_table
*tbl
)
434 unsigned long now
= jiffies
;
436 struct ip_vs_lblcr_entry
*en
, *nxt
;
438 for (i
=0, j
=tbl
->rover
; i
<IP_VS_LBLCR_TAB_SIZE
; i
++) {
439 j
= (j
+ 1) & IP_VS_LBLCR_TAB_MASK
;
441 write_lock(&tbl
->lock
);
442 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[j
], list
) {
443 if (time_after(en
->lastuse
+sysctl_ip_vs_lblcr_expiration
,
447 ip_vs_lblcr_free(en
);
448 atomic_dec(&tbl
->entries
);
450 write_unlock(&tbl
->lock
);
457 * Periodical timer handler for IPVS lblcr table
458 * It is used to collect stale entries when the number of entries
459 * exceeds the maximum size of the table.
461 * Fixme: we probably need more complicated algorithm to collect
462 * entries that have not been used for a long time even
463 * if the number of entries doesn't exceed the maximum size
465 * The full expiration check is for this purpose now.
467 static void ip_vs_lblcr_check_expire(unsigned long data
)
469 struct ip_vs_lblcr_table
*tbl
;
470 unsigned long now
= jiffies
;
473 struct ip_vs_lblcr_entry
*en
, *nxt
;
475 tbl
= (struct ip_vs_lblcr_table
*)data
;
477 if ((tbl
->counter
% COUNT_FOR_FULL_EXPIRATION
) == 0) {
478 /* do full expiration check */
479 ip_vs_lblcr_full_check(tbl
);
484 if (atomic_read(&tbl
->entries
) <= tbl
->max_size
) {
489 goal
= (atomic_read(&tbl
->entries
) - tbl
->max_size
)*4/3;
490 if (goal
> tbl
->max_size
/2)
491 goal
= tbl
->max_size
/2;
493 for (i
=0, j
=tbl
->rover
; i
<IP_VS_LBLCR_TAB_SIZE
; i
++) {
494 j
= (j
+ 1) & IP_VS_LBLCR_TAB_MASK
;
496 write_lock(&tbl
->lock
);
497 list_for_each_entry_safe(en
, nxt
, &tbl
->bucket
[j
], list
) {
498 if (time_before(now
, en
->lastuse
+ENTRY_TIMEOUT
))
501 ip_vs_lblcr_free(en
);
502 atomic_dec(&tbl
->entries
);
505 write_unlock(&tbl
->lock
);
512 mod_timer(&tbl
->periodic_timer
, jiffies
+CHECK_EXPIRE_INTERVAL
);
515 static int ip_vs_lblcr_init_svc(struct ip_vs_service
*svc
)
518 struct ip_vs_lblcr_table
*tbl
;
521 * Allocate the ip_vs_lblcr_table for this service
523 tbl
= kmalloc(sizeof(struct ip_vs_lblcr_table
), GFP_ATOMIC
);
525 IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
528 svc
->sched_data
= tbl
;
529 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
531 sizeof(struct ip_vs_lblcr_table
));
534 * Initialize the hash buckets
536 for (i
=0; i
<IP_VS_LBLCR_TAB_SIZE
; i
++) {
537 INIT_LIST_HEAD(&tbl
->bucket
[i
]);
539 rwlock_init(&tbl
->lock
);
540 tbl
->max_size
= IP_VS_LBLCR_TAB_SIZE
*16;
545 * Hook periodic timer for garbage collection
547 setup_timer(&tbl
->periodic_timer
, ip_vs_lblcr_check_expire
,
549 tbl
->periodic_timer
.expires
= jiffies
+CHECK_EXPIRE_INTERVAL
;
550 add_timer(&tbl
->periodic_timer
);
556 static int ip_vs_lblcr_done_svc(struct ip_vs_service
*svc
)
558 struct ip_vs_lblcr_table
*tbl
= svc
->sched_data
;
560 /* remove periodic timer */
561 del_timer_sync(&tbl
->periodic_timer
);
563 /* got to clean up table entries here */
564 ip_vs_lblcr_flush(tbl
);
566 /* release the table itself */
567 kfree(svc
->sched_data
);
568 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
569 sizeof(struct ip_vs_lblcr_table
));
575 static int ip_vs_lblcr_update_svc(struct ip_vs_service
*svc
)
581 static inline struct ip_vs_dest
*
582 __ip_vs_wlc_schedule(struct ip_vs_service
*svc
, struct iphdr
*iph
)
584 struct ip_vs_dest
*dest
, *least
;
588 * We think the overhead of processing active connections is fifty
589 * times higher than that of inactive connections in average. (This
590 * fifty times might not be accurate, we will change it later.) We
591 * use the following formula to estimate the overhead:
592 * dest->activeconns*50 + dest->inactconns
594 * (dest overhead) / dest->weight
596 * Remember -- no floats in kernel mode!!!
597 * The comparison of h1*w2 > h2*w1 is equivalent to that of
599 * if every weight is larger than zero.
601 * The server with weight=0 is quiesced and will not receive any
604 list_for_each_entry(dest
, &svc
->destinations
, n_list
) {
605 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
608 if (atomic_read(&dest
->weight
) > 0) {
610 loh
= atomic_read(&least
->activeconns
) * 50
611 + atomic_read(&least
->inactconns
);
618 * Find the destination with the least load.
621 list_for_each_entry_continue(dest
, &svc
->destinations
, n_list
) {
622 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
625 doh
= atomic_read(&dest
->activeconns
) * 50
626 + atomic_read(&dest
->inactconns
);
627 if (loh
* atomic_read(&dest
->weight
) >
628 doh
* atomic_read(&least
->weight
)) {
634 IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
635 "activeconns %d refcnt %d weight %d overhead %d\n",
636 NIPQUAD(least
->addr
), ntohs(least
->port
),
637 atomic_read(&least
->activeconns
),
638 atomic_read(&least
->refcnt
),
639 atomic_read(&least
->weight
), loh
);
646 * If this destination server is overloaded and there is a less loaded
647 * server, then return true.
650 is_overloaded(struct ip_vs_dest
*dest
, struct ip_vs_service
*svc
)
652 if (atomic_read(&dest
->activeconns
) > atomic_read(&dest
->weight
)) {
653 struct ip_vs_dest
*d
;
655 list_for_each_entry(d
, &svc
->destinations
, n_list
) {
656 if (atomic_read(&d
->activeconns
)*2
657 < atomic_read(&d
->weight
)) {
667 * Locality-Based (weighted) Least-Connection scheduling
669 static struct ip_vs_dest
*
670 ip_vs_lblcr_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
)
672 struct ip_vs_dest
*dest
;
673 struct ip_vs_lblcr_table
*tbl
;
674 struct ip_vs_lblcr_entry
*en
;
675 struct iphdr
*iph
= ip_hdr(skb
);
677 IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
679 tbl
= (struct ip_vs_lblcr_table
*)svc
->sched_data
;
680 en
= ip_vs_lblcr_get(tbl
, iph
->daddr
);
682 dest
= __ip_vs_wlc_schedule(svc
, iph
);
684 IP_VS_DBG(1, "no destination available\n");
687 en
= ip_vs_lblcr_new(iph
->daddr
);
691 ip_vs_dest_set_insert(&en
->set
, dest
);
692 ip_vs_lblcr_hash(tbl
, en
);
694 dest
= ip_vs_dest_set_min(&en
->set
);
695 if (!dest
|| is_overloaded(dest
, svc
)) {
696 dest
= __ip_vs_wlc_schedule(svc
, iph
);
698 IP_VS_DBG(1, "no destination available\n");
701 ip_vs_dest_set_insert(&en
->set
, dest
);
703 if (atomic_read(&en
->set
.size
) > 1 &&
704 jiffies
-en
->set
.lastmod
> sysctl_ip_vs_lblcr_expiration
) {
705 struct ip_vs_dest
*m
;
706 m
= ip_vs_dest_set_max(&en
->set
);
708 ip_vs_dest_set_erase(&en
->set
, m
);
711 en
->lastuse
= jiffies
;
713 IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
714 "--> server %u.%u.%u.%u:%d\n",
724 * IPVS LBLCR Scheduler structure
726 static struct ip_vs_scheduler ip_vs_lblcr_scheduler
=
729 .refcnt
= ATOMIC_INIT(0),
730 .module
= THIS_MODULE
,
731 .init_service
= ip_vs_lblcr_init_svc
,
732 .done_service
= ip_vs_lblcr_done_svc
,
733 .update_service
= ip_vs_lblcr_update_svc
,
734 .schedule
= ip_vs_lblcr_schedule
,
738 static int __init
ip_vs_lblcr_init(void)
742 INIT_LIST_HEAD(&ip_vs_lblcr_scheduler
.n_list
);
743 sysctl_header
= register_sysctl_paths(net_vs_ctl_path
, vs_vars_table
);
744 ret
= register_ip_vs_scheduler(&ip_vs_lblcr_scheduler
);
746 unregister_sysctl_table(sysctl_header
);
751 static void __exit
ip_vs_lblcr_cleanup(void)
753 unregister_sysctl_table(sysctl_header
);
754 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler
);
758 module_init(ip_vs_lblcr_init
);
759 module_exit(ip_vs_lblcr_cleanup
);
760 MODULE_LICENSE("GPL");