2 * IPVS: Locality-Based Least-Connection with Replication scheduler
4 * Version: $Id: ip_vs_lblcr.c,v 1.11 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 * Julian Anastasov : Added the missing (dest->weight>0)
15 * condition in the ip_vs_dest_set_max.
20 * The lblc/r algorithm is as follows (pseudo code):
22 * if serverSet[dest_ip] is null then
23 * n, serverSet[dest_ip] <- {weighted least-conn node};
25 * n <- {least-conn (alive) node in serverSet[dest_ip]};
27 * (n.conns>n.weight AND
28 * there is a node m with m.conns<m.weight/2) then
29 * n <- {weighted least-conn node};
30 * add n to serverSet[dest_ip];
31 * if |serverSet[dest_ip]| > 1 AND
32 * now - serverSet[dest_ip].lastMod > T then
33 * m <- {most conn node in serverSet[dest_ip]};
34 * remove m from serverSet[dest_ip];
35 * if serverSet[dest_ip] changed then
36 * serverSet[dest_ip].lastMod <- now;
42 #include <linux/module.h>
43 #include <linux/kernel.h>
47 #include <linux/sysctl.h>
48 /* for proc_net_create/proc_net_remove */
49 #include <linux/proc_fs.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 __u32 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 .ctl_name = NET_IPV4_VS_LBLCR_EXPIRE,
304 .procname = "lblcr_expiration",
305 .data = &sysctl_ip_vs_lblcr_expiration,
306 .maxlen = sizeof(int),
308 .proc_handler = &proc_dointvec_jiffies,
313 static ctl_table vs_table[] = {
315 .ctl_name = NET_IPV4_VS,
318 .child = vs_vars_table
323 static ctl_table ipv4_table[] = {
325 .ctl_name = NET_IPV4,
333 static ctl_table lblcr_root_table[] = {
343 static struct ctl_table_header * sysctl_header;
346 * new/free a ip_vs_lblcr_entry, which is a mapping of a destination
347 * IP address to a server.
349 static inline struct ip_vs_lblcr_entry *ip_vs_lblcr_new(__u32 daddr)
351 struct ip_vs_lblcr_entry *en;
353 en = kmalloc(sizeof(struct ip_vs_lblcr_entry), GFP_ATOMIC);
355 IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
359 INIT_LIST_HEAD(&en->list);
362 /* initilize its dest set */
363 atomic_set(&(en->set.size), 0);
365 rwlock_init(&en->set.lock);
371 static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
374 ip_vs_dest_set_eraseall(&en->set);
380 * Returns hash value for IPVS LBLCR entry
382 static inline unsigned ip_vs_lblcr_hashkey(__u32 addr)
384 return (ntohl(addr)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
389 * Hash an entry in the ip_vs_lblcr_table.
390 * returns bool success.
393 ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
397 if (!list_empty(&en->list)) {
398 IP_VS_ERR("ip_vs_lblcr_hash(): request for already hashed, "
399 "called from %p\n", __builtin_return_address(0));
404 * Hash by destination IP address
406 hash = ip_vs_lblcr_hashkey(en->addr);
408 write_lock(&tbl->lock);
409 list_add(&en->list, &tbl->bucket[hash]);
410 atomic_inc(&tbl->entries);
411 write_unlock(&tbl->lock);
419 * Unhash ip_vs_lblcr_entry from ip_vs_lblcr_table.
420 * returns bool success.
422 static int ip_vs_lblcr_unhash(struct ip_vs_lblcr_table *tbl,
423 struct ip_vs_lblcr_entry *en)
425 if (list_empty(&en->list)) {
426 IP_VS_ERR("ip_vs_lblcr_unhash(): request for not hashed entry, "
427 "called from %p\n", __builtin_return_address(0));
432 * Remove it from the table
434 write_lock(&tbl->lock);
436 INIT_LIST_HEAD(&en->list);
437 write_unlock(&tbl->lock);
445 * Get ip_vs_lblcr_entry associated with supplied parameters.
447 static inline struct ip_vs_lblcr_entry *
448 ip_vs_lblcr_get(struct ip_vs_lblcr_table *tbl, __u32 addr)
451 struct ip_vs_lblcr_entry *en;
453 hash = ip_vs_lblcr_hashkey(addr);
455 read_lock(&tbl->lock);
457 list_for_each_entry(en, &tbl->bucket[hash], list) {
458 if (en->addr == addr) {
460 read_unlock(&tbl->lock);
465 read_unlock(&tbl->lock);
472 * Flush all the entries of the specified table.
474 static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
477 struct ip_vs_lblcr_entry *en, *nxt;
479 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
480 write_lock(&tbl->lock);
481 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
482 ip_vs_lblcr_free(en);
483 atomic_dec(&tbl->entries);
485 write_unlock(&tbl->lock);
490 static inline void ip_vs_lblcr_full_check(struct ip_vs_lblcr_table *tbl)
492 unsigned long now = jiffies;
494 struct ip_vs_lblcr_entry *en, *nxt;
496 for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
497 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
499 write_lock(&tbl->lock);
500 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
501 if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration,
505 ip_vs_lblcr_free(en);
506 atomic_dec(&tbl->entries);
508 write_unlock(&tbl->lock);
515 * Periodical timer handler for IPVS lblcr table
516 * It is used to collect stale entries when the number of entries
517 * exceeds the maximum size of the table.
519 * Fixme: we probably need more complicated algorithm to collect
520 * entries that have not been used for a long time even
521 * if the number of entries doesn't exceed the maximum size
523 * The full expiration check is for this purpose now.
525 static void ip_vs_lblcr_check_expire(unsigned long data)
527 struct ip_vs_lblcr_table *tbl;
528 unsigned long now = jiffies;
531 struct ip_vs_lblcr_entry *en, *nxt;
533 tbl = (struct ip_vs_lblcr_table *)data;
535 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
536 /* do full expiration check */
537 ip_vs_lblcr_full_check(tbl);
542 if (atomic_read(&tbl->entries) <= tbl->max_size) {
547 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
548 if (goal > tbl->max_size/2)
549 goal = tbl->max_size/2;
551 for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
552 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
554 write_lock(&tbl->lock);
555 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
556 if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
559 ip_vs_lblcr_free(en);
560 atomic_dec(&tbl->entries);
563 write_unlock(&tbl->lock);
570 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
574 #ifdef CONFIG_IP_VS_LBLCR_DEBUG
575 static struct ip_vs_lblcr_table *lblcr_table_list;
578 * /proc/net/ip_vs_lblcr to display the mappings of
579 * destination IP address <==> its serverSet
582 ip_vs_lblcr_getinfo(char *buffer, char **start, off_t offset, int length)
586 struct ip_vs_lblcr_table *tbl;
587 unsigned long now = jiffies;
589 struct ip_vs_lblcr_entry *en;
591 tbl = lblcr_table_list;
593 size = sprintf(buffer, "LastTime Dest IP address Server set\n");
597 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
598 read_lock_bh(&tbl->lock);
599 list_for_each_entry(en, &tbl->bucket[i], list) {
601 struct ip_vs_dest_list *d;
603 sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(en->addr));
604 size = sprintf(buffer+len, "%8lu %-16s ",
605 now-en->lastuse, tbuf);
607 read_lock(&en->set.lock);
608 for (d=en->set.list; d!=NULL; d=d->next) {
609 size += sprintf(buffer+len+size,
611 NIPQUAD(d->dest->addr));
613 read_unlock(&en->set.lock);
614 size += sprintf(buffer+len+size, "\n");
619 if (pos >= offset+length) {
620 read_unlock_bh(&tbl->lock);
624 read_unlock_bh(&tbl->lock);
628 begin = len - (pos - offset);
629 *start = buffer + begin;
638 static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
641 struct ip_vs_lblcr_table *tbl;
644 * Allocate the ip_vs_lblcr_table for this service
646 tbl = kmalloc(sizeof(struct ip_vs_lblcr_table), GFP_ATOMIC);
648 IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
651 svc->sched_data = tbl;
652 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
654 sizeof(struct ip_vs_lblcr_table));
657 * Initialize the hash buckets
659 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
660 INIT_LIST_HEAD(&tbl->bucket[i]);
662 rwlock_init(&tbl->lock);
663 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
668 * Hook periodic timer for garbage collection
670 init_timer(&tbl->periodic_timer);
671 tbl->periodic_timer.data = (unsigned long)tbl;
672 tbl->periodic_timer.function = ip_vs_lblcr_check_expire;
673 tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
674 add_timer(&tbl->periodic_timer);
676 #ifdef CONFIG_IP_VS_LBLCR_DEBUG
677 lblcr_table_list = tbl;
683 static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
685 struct ip_vs_lblcr_table *tbl = svc->sched_data;
687 /* remove periodic timer */
688 del_timer_sync(&tbl->periodic_timer);
690 /* got to clean up table entries here */
691 ip_vs_lblcr_flush(tbl);
693 /* release the table itself */
694 kfree(svc->sched_data);
695 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
696 sizeof(struct ip_vs_lblcr_table));
702 static int ip_vs_lblcr_update_svc(struct ip_vs_service *svc)
708 static inline struct ip_vs_dest *
709 __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
711 struct ip_vs_dest *dest, *least;
715 * We think the overhead of processing active connections is fifty
716 * times higher than that of inactive connections in average. (This
717 * fifty times might not be accurate, we will change it later.) We
718 * use the following formula to estimate the overhead:
719 * dest->activeconns*50 + dest->inactconns
721 * (dest overhead) / dest->weight
723 * Remember -- no floats in kernel mode!!!
724 * The comparison of h1*w2 > h2*w1 is equivalent to that of
726 * if every weight is larger than zero.
728 * The server with weight=0 is quiesced and will not receive any
731 list_for_each_entry(dest, &svc->destinations, n_list) {
732 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
735 if (atomic_read(&dest->weight) > 0) {
737 loh = atomic_read(&least->activeconns) * 50
738 + atomic_read(&least->inactconns);
745 * Find the destination with the least load.
748 list_for_each_entry_continue(dest, &svc->destinations, n_list) {
749 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
752 doh = atomic_read(&dest->activeconns) * 50
753 + atomic_read(&dest->inactconns);
754 if (loh * atomic_read(&dest->weight) >
755 doh * atomic_read(&least->weight)) {
761 IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
762 "activeconns %d refcnt %d weight %d overhead %d\n",
763 NIPQUAD(least->addr), ntohs(least->port),
764 atomic_read(&least->activeconns),
765 atomic_read(&least->refcnt),
766 atomic_read(&least->weight), loh);
773 * If this destination server is overloaded and there is a less loaded
774 * server, then return true.
777 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
779 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
780 struct ip_vs_dest *d;
782 list_for_each_entry(d, &svc->destinations, n_list) {
783 if (atomic_read(&d->activeconns)*2
784 < atomic_read(&d->weight)) {
794 * Locality-Based (weighted) Least-Connection scheduling
796 static struct ip_vs_dest *
797 ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
799 struct ip_vs_dest *dest;
800 struct ip_vs_lblcr_table *tbl;
801 struct ip_vs_lblcr_entry *en;
802 struct iphdr *iph = skb->nh.iph;
804 IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
806 tbl = (struct ip_vs_lblcr_table *)svc->sched_data;
807 en = ip_vs_lblcr_get(tbl, iph->daddr);
809 dest = __ip_vs_wlc_schedule(svc, iph);
811 IP_VS_DBG(1, "no destination available\n");
814 en = ip_vs_lblcr_new(iph->daddr);
818 ip_vs_dest_set_insert(&en->set, dest);
819 ip_vs_lblcr_hash(tbl, en);
821 dest = ip_vs_dest_set_min(&en->set);
822 if (!dest || is_overloaded(dest, svc)) {
823 dest = __ip_vs_wlc_schedule(svc, iph);
825 IP_VS_DBG(1, "no destination available\n");
828 ip_vs_dest_set_insert(&en->set, dest);
830 if (atomic_read(&en->set.size) > 1 &&
831 jiffies-en->set.lastmod > sysctl_ip_vs_lblcr_expiration) {
832 struct ip_vs_dest *m;
833 m = ip_vs_dest_set_max(&en->set);
835 ip_vs_dest_set_erase(&en->set, m);
838 en->lastuse = jiffies;
840 IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
841 "--> server %u.%u.%u.%u:%d\n",
851 * IPVS LBLCR Scheduler structure
853 static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
856 .refcnt = ATOMIC_INIT(0),
857 .module = THIS_MODULE,
858 .init_service = ip_vs_lblcr_init_svc,
859 .done_service = ip_vs_lblcr_done_svc,
860 .update_service = ip_vs_lblcr_update_svc,
861 .schedule = ip_vs_lblcr_schedule,
865 static int __init ip_vs_lblcr_init(void)
867 INIT_LIST_HEAD(&ip_vs_lblcr_scheduler.n_list);
868 sysctl_header = register_sysctl_table(lblcr_root_table, 0);
869 #ifdef CONFIG_IP_VS_LBLCR_DEBUG
870 proc_net_create("ip_vs_lblcr", 0, ip_vs_lblcr_getinfo);
872 return register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
876 static void __exit ip_vs_lblcr_cleanup(void)
878 #ifdef CONFIG_IP_VS_LBLCR_DEBUG
879 proc_net_remove("ip_vs_lblcr");
881 unregister_sysctl_table(sysctl_header);
882 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
886 module_init(ip_vs_lblcr_init);
887 module_exit(ip_vs_lblcr_cleanup);
888 MODULE_LICENSE("GPL");