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;
40 #define KMSG_COMPONENT "IPVS"
41 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/skbuff.h>
47 #include <linux/jiffies.h>
48 #include <linux/list.h>
49 #include <linux/slab.h>
50 #include <linux/hash.h>
54 #include <linux/sysctl.h>
55 #include <net/net_namespace.h>
57 #include <net/ip_vs.h>
61 * It is for garbage collection of stale IPVS lblcr entries,
62 * when the table is full.
64 #define CHECK_EXPIRE_INTERVAL (60*HZ)
65 #define ENTRY_TIMEOUT (6*60*HZ)
67 #define DEFAULT_EXPIRATION (24*60*60*HZ)
70 * It is for full expiration check.
71 * When there is no partial expiration check (garbage collection)
72 * in a half hour, do a full expiration check to collect stale
73 * entries that haven't been touched for a day.
75 #define COUNT_FOR_FULL_EXPIRATION 30
78 * for IPVS lblcr entry hash table
80 #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
81 #define CONFIG_IP_VS_LBLCR_TAB_BITS 10
83 #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS
84 #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS)
85 #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1)
89 * IPVS destination set structure and operations
91 struct ip_vs_dest_set_elem {
92 struct list_head list; /* list link */
93 struct ip_vs_dest *dest; /* destination server */
94 struct rcu_head rcu_head;
97 struct ip_vs_dest_set {
98 atomic_t size; /* set size */
99 unsigned long lastmod; /* last modified time */
100 struct list_head list; /* destination list */
104 static void ip_vs_dest_set_insert(struct ip_vs_dest_set *set,
105 struct ip_vs_dest *dest, bool check)
107 struct ip_vs_dest_set_elem *e;
110 list_for_each_entry(e, &set->list, list) {
116 e = kmalloc(sizeof(*e), GFP_ATOMIC);
120 ip_vs_dest_hold(dest);
123 list_add_rcu(&e->list, &set->list);
124 atomic_inc(&set->size);
126 set->lastmod = jiffies;
129 static void ip_vs_lblcr_elem_rcu_free(struct rcu_head *head)
131 struct ip_vs_dest_set_elem *e;
133 e = container_of(head, struct ip_vs_dest_set_elem, rcu_head);
134 ip_vs_dest_put_and_free(e->dest);
139 ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
141 struct ip_vs_dest_set_elem *e;
143 list_for_each_entry(e, &set->list, list) {
144 if (e->dest == dest) {
146 atomic_dec(&set->size);
147 set->lastmod = jiffies;
148 list_del_rcu(&e->list);
149 call_rcu(&e->rcu_head, ip_vs_lblcr_elem_rcu_free);
155 static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
157 struct ip_vs_dest_set_elem *e, *ep;
159 list_for_each_entry_safe(e, ep, &set->list, list) {
160 list_del_rcu(&e->list);
161 call_rcu(&e->rcu_head, ip_vs_lblcr_elem_rcu_free);
165 /* get weighted least-connection node in the destination set */
166 static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
168 register struct ip_vs_dest_set_elem *e;
169 struct ip_vs_dest *dest, *least;
172 /* select the first destination server, whose weight > 0 */
173 list_for_each_entry_rcu(e, &set->list, list) {
175 if (least->flags & IP_VS_DEST_F_OVERLOAD)
178 if ((atomic_read(&least->weight) > 0)
179 && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
180 loh = ip_vs_dest_conn_overhead(least);
186 /* find the destination with the weighted least load */
188 list_for_each_entry_continue_rcu(e, &set->list, list) {
190 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
193 doh = ip_vs_dest_conn_overhead(dest);
194 if (((__s64)loh * atomic_read(&dest->weight) >
195 (__s64)doh * atomic_read(&least->weight))
196 && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
202 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
203 "activeconns %d refcnt %d weight %d overhead %d\n",
205 IP_VS_DBG_ADDR(least->af, &least->addr),
207 atomic_read(&least->activeconns),
208 refcount_read(&least->refcnt),
209 atomic_read(&least->weight), loh);
214 /* get weighted most-connection node in the destination set */
215 static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
217 register struct ip_vs_dest_set_elem *e;
218 struct ip_vs_dest *dest, *most;
224 /* select the first destination server, whose weight > 0 */
225 list_for_each_entry(e, &set->list, list) {
227 if (atomic_read(&most->weight) > 0) {
228 moh = ip_vs_dest_conn_overhead(most);
234 /* find the destination with the weighted most load */
236 list_for_each_entry_continue(e, &set->list, list) {
238 doh = ip_vs_dest_conn_overhead(dest);
239 /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
240 if (((__s64)moh * atomic_read(&dest->weight) <
241 (__s64)doh * atomic_read(&most->weight))
242 && (atomic_read(&dest->weight) > 0)) {
248 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
249 "activeconns %d refcnt %d weight %d overhead %d\n",
251 IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port),
252 atomic_read(&most->activeconns),
253 refcount_read(&most->refcnt),
254 atomic_read(&most->weight), moh);
260 * IPVS lblcr entry represents an association between destination
261 * IP address and its destination server set
263 struct ip_vs_lblcr_entry {
264 struct hlist_node list;
265 int af; /* address family */
266 union nf_inet_addr addr; /* destination IP address */
267 struct ip_vs_dest_set set; /* destination server set */
268 unsigned long lastuse; /* last used time */
269 struct rcu_head rcu_head;
274 * IPVS lblcr hash table
276 struct ip_vs_lblcr_table {
277 struct rcu_head rcu_head;
278 struct hlist_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */
279 atomic_t entries; /* number of entries */
280 int max_size; /* maximum size of entries */
281 struct timer_list periodic_timer; /* collect stale entries */
282 struct ip_vs_service *svc; /* pointer back to service */
283 int rover; /* rover for expire check */
284 int counter; /* counter for no expire */
291 * IPVS LBLCR sysctl table
294 static struct ctl_table vs_vars_table[] = {
296 .procname = "lblcr_expiration",
298 .maxlen = sizeof(int),
300 .proc_handler = proc_dointvec_jiffies,
306 static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
308 hlist_del_rcu(&en->list);
309 ip_vs_dest_set_eraseall(&en->set);
310 kfree_rcu(en, rcu_head);
315 * Returns hash value for IPVS LBLCR entry
317 static inline unsigned int
318 ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
320 __be32 addr_fold = addr->ip;
322 #ifdef CONFIG_IP_VS_IPV6
324 addr_fold = addr->ip6[0]^addr->ip6[1]^
325 addr->ip6[2]^addr->ip6[3];
327 return hash_32(ntohl(addr_fold), IP_VS_LBLCR_TAB_BITS);
332 * Hash an entry in the ip_vs_lblcr_table.
333 * returns bool success.
336 ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
338 unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
340 hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
341 atomic_inc(&tbl->entries);
345 /* Get ip_vs_lblcr_entry associated with supplied parameters. */
346 static inline struct ip_vs_lblcr_entry *
347 ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
348 const union nf_inet_addr *addr)
350 unsigned int hash = ip_vs_lblcr_hashkey(af, addr);
351 struct ip_vs_lblcr_entry *en;
353 hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
354 if (ip_vs_addr_equal(af, &en->addr, addr))
362 * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination
363 * IP address to a server. Called under spin lock.
365 static inline struct ip_vs_lblcr_entry *
366 ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
367 u16 af, struct ip_vs_dest *dest)
369 struct ip_vs_lblcr_entry *en;
371 en = ip_vs_lblcr_get(af, tbl, daddr);
373 en = kmalloc(sizeof(*en), GFP_ATOMIC);
378 ip_vs_addr_copy(af, &en->addr, daddr);
379 en->lastuse = jiffies;
381 /* initialize its dest set */
382 atomic_set(&(en->set.size), 0);
383 INIT_LIST_HEAD(&en->set.list);
385 ip_vs_dest_set_insert(&en->set, dest, false);
387 ip_vs_lblcr_hash(tbl, en);
391 ip_vs_dest_set_insert(&en->set, dest, true);
398 * Flush all the entries of the specified table.
400 static void ip_vs_lblcr_flush(struct ip_vs_service *svc)
402 struct ip_vs_lblcr_table *tbl = svc->sched_data;
404 struct ip_vs_lblcr_entry *en;
405 struct hlist_node *next;
407 spin_lock_bh(&svc->sched_lock);
409 for (i = 0; i < IP_VS_LBLCR_TAB_SIZE; i++) {
410 hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) {
411 ip_vs_lblcr_free(en);
414 spin_unlock_bh(&svc->sched_lock);
417 static int sysctl_lblcr_expiration(struct ip_vs_service *svc)
420 return svc->ipvs->sysctl_lblcr_expiration;
422 return DEFAULT_EXPIRATION;
426 static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
428 struct ip_vs_lblcr_table *tbl = svc->sched_data;
429 unsigned long now = jiffies;
431 struct ip_vs_lblcr_entry *en;
432 struct hlist_node *next;
434 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) {
435 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
437 spin_lock(&svc->sched_lock);
438 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
439 if (time_after(en->lastuse +
440 sysctl_lblcr_expiration(svc), now))
443 ip_vs_lblcr_free(en);
444 atomic_dec(&tbl->entries);
446 spin_unlock(&svc->sched_lock);
453 * Periodical timer handler for IPVS lblcr table
454 * It is used to collect stale entries when the number of entries
455 * exceeds the maximum size of the table.
457 * Fixme: we probably need more complicated algorithm to collect
458 * entries that have not been used for a long time even
459 * if the number of entries doesn't exceed the maximum size
461 * The full expiration check is for this purpose now.
463 static void ip_vs_lblcr_check_expire(struct timer_list *t)
465 struct ip_vs_lblcr_table *tbl = from_timer(tbl, t, periodic_timer);
466 struct ip_vs_service *svc = tbl->svc;
467 unsigned long now = jiffies;
470 struct ip_vs_lblcr_entry *en;
471 struct hlist_node *next;
473 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
474 /* do full expiration check */
475 ip_vs_lblcr_full_check(svc);
480 if (atomic_read(&tbl->entries) <= tbl->max_size) {
485 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
486 if (goal > tbl->max_size/2)
487 goal = tbl->max_size/2;
489 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) {
490 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
492 spin_lock(&svc->sched_lock);
493 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
494 if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
497 ip_vs_lblcr_free(en);
498 atomic_dec(&tbl->entries);
501 spin_unlock(&svc->sched_lock);
508 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
511 static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
514 struct ip_vs_lblcr_table *tbl;
517 * Allocate the ip_vs_lblcr_table for this service
519 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
523 svc->sched_data = tbl;
524 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) allocated for "
525 "current service\n", sizeof(*tbl));
528 * Initialize the hash buckets
530 for (i = 0; i < IP_VS_LBLCR_TAB_SIZE; i++) {
531 INIT_HLIST_HEAD(&tbl->bucket[i]);
533 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
540 * Hook periodic timer for garbage collection
542 timer_setup(&tbl->periodic_timer, ip_vs_lblcr_check_expire, 0);
543 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
549 static void ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
551 struct ip_vs_lblcr_table *tbl = svc->sched_data;
553 /* remove periodic timer */
554 del_timer_sync(&tbl->periodic_timer);
556 /* got to clean up table entries here */
557 ip_vs_lblcr_flush(svc);
559 /* release the table itself */
560 kfree_rcu(tbl, rcu_head);
561 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) released\n",
566 static inline struct ip_vs_dest *
567 __ip_vs_lblcr_schedule(struct ip_vs_service *svc)
569 struct ip_vs_dest *dest, *least;
573 * We use the following formula to estimate the load:
574 * (dest overhead) / dest->weight
576 * Remember -- no floats in kernel mode!!!
577 * The comparison of h1*w2 > h2*w1 is equivalent to that of
579 * if every weight is larger than zero.
581 * The server with weight=0 is quiesced and will not receive any
584 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
585 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
588 if (atomic_read(&dest->weight) > 0) {
590 loh = ip_vs_dest_conn_overhead(least);
597 * Find the destination with the least load.
600 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
601 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
604 doh = ip_vs_dest_conn_overhead(dest);
605 if ((__s64)loh * atomic_read(&dest->weight) >
606 (__s64)doh * atomic_read(&least->weight)) {
612 IP_VS_DBG_BUF(6, "LBLCR: server %s:%d "
613 "activeconns %d refcnt %d weight %d overhead %d\n",
614 IP_VS_DBG_ADDR(least->af, &least->addr),
616 atomic_read(&least->activeconns),
617 refcount_read(&least->refcnt),
618 atomic_read(&least->weight), loh);
625 * If this destination server is overloaded and there is a less loaded
626 * server, then return true.
629 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
631 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
632 struct ip_vs_dest *d;
634 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
635 if (atomic_read(&d->activeconns)*2
636 < atomic_read(&d->weight)) {
646 * Locality-Based (weighted) Least-Connection scheduling
648 static struct ip_vs_dest *
649 ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
650 struct ip_vs_iphdr *iph)
652 struct ip_vs_lblcr_table *tbl = svc->sched_data;
653 struct ip_vs_dest *dest;
654 struct ip_vs_lblcr_entry *en;
656 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
658 /* First look in our cache */
659 en = ip_vs_lblcr_get(svc->af, tbl, &iph->daddr);
661 en->lastuse = jiffies;
663 /* Get the least loaded destination */
664 dest = ip_vs_dest_set_min(&en->set);
666 /* More than one destination + enough time passed by, cleanup */
667 if (atomic_read(&en->set.size) > 1 &&
668 time_after(jiffies, en->set.lastmod +
669 sysctl_lblcr_expiration(svc))) {
670 spin_lock_bh(&svc->sched_lock);
671 if (atomic_read(&en->set.size) > 1) {
672 struct ip_vs_dest *m;
674 m = ip_vs_dest_set_max(&en->set);
676 ip_vs_dest_set_erase(&en->set, m);
678 spin_unlock_bh(&svc->sched_lock);
681 /* If the destination is not overloaded, use it */
682 if (dest && !is_overloaded(dest, svc))
685 /* The cache entry is invalid, time to schedule */
686 dest = __ip_vs_lblcr_schedule(svc);
688 ip_vs_scheduler_err(svc, "no destination available");
692 /* Update our cache entry */
693 spin_lock_bh(&svc->sched_lock);
695 ip_vs_dest_set_insert(&en->set, dest, true);
696 spin_unlock_bh(&svc->sched_lock);
700 /* No cache entry, time to schedule */
701 dest = __ip_vs_lblcr_schedule(svc);
703 IP_VS_DBG(1, "no destination available\n");
707 /* If we fail to create a cache entry, we'll just use the valid dest */
708 spin_lock_bh(&svc->sched_lock);
710 ip_vs_lblcr_new(tbl, &iph->daddr, svc->af, dest);
711 spin_unlock_bh(&svc->sched_lock);
714 IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n",
715 IP_VS_DBG_ADDR(svc->af, &iph->daddr),
716 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
723 * IPVS LBLCR Scheduler structure
725 static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
728 .refcnt = ATOMIC_INIT(0),
729 .module = THIS_MODULE,
730 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
731 .init_service = ip_vs_lblcr_init_svc,
732 .done_service = ip_vs_lblcr_done_svc,
733 .schedule = ip_vs_lblcr_schedule,
740 static int __net_init __ip_vs_lblcr_init(struct net *net)
742 struct netns_ipvs *ipvs = net_ipvs(net);
747 if (!net_eq(net, &init_net)) {
748 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table,
749 sizeof(vs_vars_table),
751 if (ipvs->lblcr_ctl_table == NULL)
754 /* Don't export sysctls to unprivileged users */
755 if (net->user_ns != &init_user_ns)
756 ipvs->lblcr_ctl_table[0].procname = NULL;
758 ipvs->lblcr_ctl_table = vs_vars_table;
759 ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION;
760 ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration;
762 ipvs->lblcr_ctl_header =
763 register_net_sysctl(net, "net/ipv4/vs", ipvs->lblcr_ctl_table);
764 if (!ipvs->lblcr_ctl_header) {
765 if (!net_eq(net, &init_net))
766 kfree(ipvs->lblcr_ctl_table);
773 static void __net_exit __ip_vs_lblcr_exit(struct net *net)
775 struct netns_ipvs *ipvs = net_ipvs(net);
777 unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
779 if (!net_eq(net, &init_net))
780 kfree(ipvs->lblcr_ctl_table);
785 static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
786 static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
790 static struct pernet_operations ip_vs_lblcr_ops = {
791 .init = __ip_vs_lblcr_init,
792 .exit = __ip_vs_lblcr_exit,
795 static int __init ip_vs_lblcr_init(void)
799 ret = register_pernet_subsys(&ip_vs_lblcr_ops);
803 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
805 unregister_pernet_subsys(&ip_vs_lblcr_ops);
809 static void __exit ip_vs_lblcr_cleanup(void)
811 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
812 unregister_pernet_subsys(&ip_vs_lblcr_ops);
817 module_init(ip_vs_lblcr_init);
818 module_exit(ip_vs_lblcr_cleanup);
819 MODULE_LICENSE("GPL");