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 ctl_table vs_table[] = {
130 .child = vs_vars_table
135 static ctl_table ipvs_ipv4_table[] = {
137 .ctl_name = NET_IPV4,
145 static ctl_table lblc_root_table[] = {
150 .child = ipvs_ipv4_table
155 static struct ctl_table_header * sysctl_header;
158 * new/free a ip_vs_lblc_entry, which is a mapping of a destionation
159 * IP address to a server.
161 static inline struct ip_vs_lblc_entry *
162 ip_vs_lblc_new(__be32 daddr, struct ip_vs_dest *dest)
164 struct ip_vs_lblc_entry *en;
166 en = kmalloc(sizeof(struct ip_vs_lblc_entry), GFP_ATOMIC);
168 IP_VS_ERR("ip_vs_lblc_new(): no memory\n");
172 INIT_LIST_HEAD(&en->list);
175 atomic_inc(&dest->refcnt);
182 static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en)
186 * We don't kfree dest because it is refered either by its service
187 * or the trash dest list.
189 atomic_dec(&en->dest->refcnt);
195 * Returns hash value for IPVS LBLC entry
197 static inline unsigned ip_vs_lblc_hashkey(__be32 addr)
199 return (ntohl(addr)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
204 * Hash an entry in the ip_vs_lblc_table.
205 * returns bool success.
208 ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
212 if (!list_empty(&en->list)) {
213 IP_VS_ERR("ip_vs_lblc_hash(): request for already hashed, "
214 "called from %p\n", __builtin_return_address(0));
219 * Hash by destination IP address
221 hash = ip_vs_lblc_hashkey(en->addr);
223 write_lock(&tbl->lock);
224 list_add(&en->list, &tbl->bucket[hash]);
225 atomic_inc(&tbl->entries);
226 write_unlock(&tbl->lock);
233 * Get ip_vs_lblc_entry associated with supplied parameters.
235 static inline struct ip_vs_lblc_entry *
236 ip_vs_lblc_get(struct ip_vs_lblc_table *tbl, __be32 addr)
239 struct ip_vs_lblc_entry *en;
241 hash = ip_vs_lblc_hashkey(addr);
243 read_lock(&tbl->lock);
245 list_for_each_entry(en, &tbl->bucket[hash], list) {
246 if (en->addr == addr) {
248 read_unlock(&tbl->lock);
253 read_unlock(&tbl->lock);
260 * Flush all the entries of the specified table.
262 static void ip_vs_lblc_flush(struct ip_vs_lblc_table *tbl)
265 struct ip_vs_lblc_entry *en, *nxt;
267 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
268 write_lock(&tbl->lock);
269 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
271 atomic_dec(&tbl->entries);
273 write_unlock(&tbl->lock);
278 static inline void ip_vs_lblc_full_check(struct ip_vs_lblc_table *tbl)
280 unsigned long now = jiffies;
282 struct ip_vs_lblc_entry *en, *nxt;
284 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
285 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
287 write_lock(&tbl->lock);
288 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
290 en->lastuse + sysctl_ip_vs_lblc_expiration))
294 atomic_dec(&tbl->entries);
296 write_unlock(&tbl->lock);
303 * Periodical timer handler for IPVS lblc table
304 * It is used to collect stale entries when the number of entries
305 * exceeds the maximum size of the table.
307 * Fixme: we probably need more complicated algorithm to collect
308 * entries that have not been used for a long time even
309 * if the number of entries doesn't exceed the maximum size
311 * The full expiration check is for this purpose now.
313 static void ip_vs_lblc_check_expire(unsigned long data)
315 struct ip_vs_lblc_table *tbl;
316 unsigned long now = jiffies;
319 struct ip_vs_lblc_entry *en, *nxt;
321 tbl = (struct ip_vs_lblc_table *)data;
323 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
324 /* do full expiration check */
325 ip_vs_lblc_full_check(tbl);
330 if (atomic_read(&tbl->entries) <= tbl->max_size) {
335 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
336 if (goal > tbl->max_size/2)
337 goal = tbl->max_size/2;
339 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
340 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
342 write_lock(&tbl->lock);
343 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
344 if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
348 atomic_dec(&tbl->entries);
351 write_unlock(&tbl->lock);
358 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
362 static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
365 struct ip_vs_lblc_table *tbl;
368 * Allocate the ip_vs_lblc_table for this service
370 tbl = kmalloc(sizeof(struct ip_vs_lblc_table), GFP_ATOMIC);
372 IP_VS_ERR("ip_vs_lblc_init_svc(): no memory\n");
375 svc->sched_data = tbl;
376 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
378 sizeof(struct ip_vs_lblc_table));
381 * Initialize the hash buckets
383 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
384 INIT_LIST_HEAD(&tbl->bucket[i]);
386 rwlock_init(&tbl->lock);
387 tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
392 * Hook periodic timer for garbage collection
394 setup_timer(&tbl->periodic_timer, ip_vs_lblc_check_expire,
396 tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
397 add_timer(&tbl->periodic_timer);
403 static int ip_vs_lblc_done_svc(struct ip_vs_service *svc)
405 struct ip_vs_lblc_table *tbl = svc->sched_data;
407 /* remove periodic timer */
408 del_timer_sync(&tbl->periodic_timer);
410 /* got to clean up table entries here */
411 ip_vs_lblc_flush(tbl);
413 /* release the table itself */
414 kfree(svc->sched_data);
415 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
416 sizeof(struct ip_vs_lblc_table));
422 static int ip_vs_lblc_update_svc(struct ip_vs_service *svc)
428 static inline struct ip_vs_dest *
429 __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
431 struct ip_vs_dest *dest, *least;
435 * We think the overhead of processing active connections is fifty
436 * times higher than that of inactive connections in average. (This
437 * fifty times might not be accurate, we will change it later.) We
438 * use the following formula to estimate the overhead:
439 * dest->activeconns*50 + dest->inactconns
441 * (dest overhead) / dest->weight
443 * Remember -- no floats in kernel mode!!!
444 * The comparison of h1*w2 > h2*w1 is equivalent to that of
446 * if every weight is larger than zero.
448 * The server with weight=0 is quiesced and will not receive any
451 list_for_each_entry(dest, &svc->destinations, n_list) {
452 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
454 if (atomic_read(&dest->weight) > 0) {
456 loh = atomic_read(&least->activeconns) * 50
457 + atomic_read(&least->inactconns);
464 * Find the destination with the least load.
467 list_for_each_entry_continue(dest, &svc->destinations, n_list) {
468 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
471 doh = atomic_read(&dest->activeconns) * 50
472 + atomic_read(&dest->inactconns);
473 if (loh * atomic_read(&dest->weight) >
474 doh * atomic_read(&least->weight)) {
480 IP_VS_DBG(6, "LBLC: server %d.%d.%d.%d:%d "
481 "activeconns %d refcnt %d weight %d overhead %d\n",
482 NIPQUAD(least->addr), ntohs(least->port),
483 atomic_read(&least->activeconns),
484 atomic_read(&least->refcnt),
485 atomic_read(&least->weight), loh);
492 * If this destination server is overloaded and there is a less loaded
493 * server, then return true.
496 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
498 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
499 struct ip_vs_dest *d;
501 list_for_each_entry(d, &svc->destinations, n_list) {
502 if (atomic_read(&d->activeconns)*2
503 < atomic_read(&d->weight)) {
513 * Locality-Based (weighted) Least-Connection scheduling
515 static struct ip_vs_dest *
516 ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
518 struct ip_vs_dest *dest;
519 struct ip_vs_lblc_table *tbl;
520 struct ip_vs_lblc_entry *en;
521 struct iphdr *iph = ip_hdr(skb);
523 IP_VS_DBG(6, "ip_vs_lblc_schedule(): Scheduling...\n");
525 tbl = (struct ip_vs_lblc_table *)svc->sched_data;
526 en = ip_vs_lblc_get(tbl, iph->daddr);
528 dest = __ip_vs_wlc_schedule(svc, iph);
530 IP_VS_DBG(1, "no destination available\n");
533 en = ip_vs_lblc_new(iph->daddr, dest);
537 ip_vs_lblc_hash(tbl, en);
540 if (!(dest->flags & IP_VS_DEST_F_AVAILABLE)
541 || atomic_read(&dest->weight) <= 0
542 || is_overloaded(dest, svc)) {
543 dest = __ip_vs_wlc_schedule(svc, iph);
545 IP_VS_DBG(1, "no destination available\n");
548 atomic_dec(&en->dest->refcnt);
549 atomic_inc(&dest->refcnt);
553 en->lastuse = jiffies;
555 IP_VS_DBG(6, "LBLC: destination IP address %u.%u.%u.%u "
556 "--> server %u.%u.%u.%u:%d\n",
566 * IPVS LBLC Scheduler structure
568 static struct ip_vs_scheduler ip_vs_lblc_scheduler =
571 .refcnt = ATOMIC_INIT(0),
572 .module = THIS_MODULE,
573 .init_service = ip_vs_lblc_init_svc,
574 .done_service = ip_vs_lblc_done_svc,
575 .update_service = ip_vs_lblc_update_svc,
576 .schedule = ip_vs_lblc_schedule,
580 static int __init ip_vs_lblc_init(void)
584 INIT_LIST_HEAD(&ip_vs_lblc_scheduler.n_list);
585 sysctl_header = register_sysctl_table(lblc_root_table);
586 ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
588 unregister_sysctl_table(sysctl_header);
593 static void __exit ip_vs_lblc_cleanup(void)
595 unregister_sysctl_table(sysctl_header);
596 unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
600 module_init(ip_vs_lblc_init);
601 module_exit(ip_vs_lblc_cleanup);
602 MODULE_LICENSE("GPL");