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;
538 atomic_set(&tbl->entries, 0);
541 * Hook periodic timer for garbage collection
543 timer_setup(&tbl->periodic_timer, ip_vs_lblcr_check_expire, 0);
544 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
550 static void ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
552 struct ip_vs_lblcr_table *tbl = svc->sched_data;
554 /* remove periodic timer */
555 del_timer_sync(&tbl->periodic_timer);
557 /* got to clean up table entries here */
558 ip_vs_lblcr_flush(svc);
560 /* release the table itself */
561 kfree_rcu(tbl, rcu_head);
562 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) released\n",
567 static inline struct ip_vs_dest *
568 __ip_vs_lblcr_schedule(struct ip_vs_service *svc)
570 struct ip_vs_dest *dest, *least;
574 * We use the following formula to estimate the load:
575 * (dest overhead) / dest->weight
577 * Remember -- no floats in kernel mode!!!
578 * The comparison of h1*w2 > h2*w1 is equivalent to that of
580 * if every weight is larger than zero.
582 * The server with weight=0 is quiesced and will not receive any
585 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
586 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
589 if (atomic_read(&dest->weight) > 0) {
591 loh = ip_vs_dest_conn_overhead(least);
598 * Find the destination with the least load.
601 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
602 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
605 doh = ip_vs_dest_conn_overhead(dest);
606 if ((__s64)loh * atomic_read(&dest->weight) >
607 (__s64)doh * atomic_read(&least->weight)) {
613 IP_VS_DBG_BUF(6, "LBLCR: server %s:%d "
614 "activeconns %d refcnt %d weight %d overhead %d\n",
615 IP_VS_DBG_ADDR(least->af, &least->addr),
617 atomic_read(&least->activeconns),
618 refcount_read(&least->refcnt),
619 atomic_read(&least->weight), loh);
626 * If this destination server is overloaded and there is a less loaded
627 * server, then return true.
630 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
632 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
633 struct ip_vs_dest *d;
635 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
636 if (atomic_read(&d->activeconns)*2
637 < atomic_read(&d->weight)) {
647 * Locality-Based (weighted) Least-Connection scheduling
649 static struct ip_vs_dest *
650 ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
651 struct ip_vs_iphdr *iph)
653 struct ip_vs_lblcr_table *tbl = svc->sched_data;
654 struct ip_vs_dest *dest;
655 struct ip_vs_lblcr_entry *en;
657 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
659 /* First look in our cache */
660 en = ip_vs_lblcr_get(svc->af, tbl, &iph->daddr);
662 en->lastuse = jiffies;
664 /* Get the least loaded destination */
665 dest = ip_vs_dest_set_min(&en->set);
667 /* More than one destination + enough time passed by, cleanup */
668 if (atomic_read(&en->set.size) > 1 &&
669 time_after(jiffies, en->set.lastmod +
670 sysctl_lblcr_expiration(svc))) {
671 spin_lock_bh(&svc->sched_lock);
672 if (atomic_read(&en->set.size) > 1) {
673 struct ip_vs_dest *m;
675 m = ip_vs_dest_set_max(&en->set);
677 ip_vs_dest_set_erase(&en->set, m);
679 spin_unlock_bh(&svc->sched_lock);
682 /* If the destination is not overloaded, use it */
683 if (dest && !is_overloaded(dest, svc))
686 /* The cache entry is invalid, time to schedule */
687 dest = __ip_vs_lblcr_schedule(svc);
689 ip_vs_scheduler_err(svc, "no destination available");
693 /* Update our cache entry */
694 spin_lock_bh(&svc->sched_lock);
696 ip_vs_dest_set_insert(&en->set, dest, true);
697 spin_unlock_bh(&svc->sched_lock);
701 /* No cache entry, time to schedule */
702 dest = __ip_vs_lblcr_schedule(svc);
704 IP_VS_DBG(1, "no destination available\n");
708 /* If we fail to create a cache entry, we'll just use the valid dest */
709 spin_lock_bh(&svc->sched_lock);
711 ip_vs_lblcr_new(tbl, &iph->daddr, svc->af, dest);
712 spin_unlock_bh(&svc->sched_lock);
715 IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n",
716 IP_VS_DBG_ADDR(svc->af, &iph->daddr),
717 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
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 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
732 .init_service = ip_vs_lblcr_init_svc,
733 .done_service = ip_vs_lblcr_done_svc,
734 .schedule = ip_vs_lblcr_schedule,
741 static int __net_init __ip_vs_lblcr_init(struct net *net)
743 struct netns_ipvs *ipvs = net_ipvs(net);
748 if (!net_eq(net, &init_net)) {
749 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table,
750 sizeof(vs_vars_table),
752 if (ipvs->lblcr_ctl_table == NULL)
755 /* Don't export sysctls to unprivileged users */
756 if (net->user_ns != &init_user_ns)
757 ipvs->lblcr_ctl_table[0].procname = NULL;
759 ipvs->lblcr_ctl_table = vs_vars_table;
760 ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION;
761 ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration;
763 ipvs->lblcr_ctl_header =
764 register_net_sysctl(net, "net/ipv4/vs", ipvs->lblcr_ctl_table);
765 if (!ipvs->lblcr_ctl_header) {
766 if (!net_eq(net, &init_net))
767 kfree(ipvs->lblcr_ctl_table);
774 static void __net_exit __ip_vs_lblcr_exit(struct net *net)
776 struct netns_ipvs *ipvs = net_ipvs(net);
778 unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
780 if (!net_eq(net, &init_net))
781 kfree(ipvs->lblcr_ctl_table);
786 static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
787 static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
791 static struct pernet_operations ip_vs_lblcr_ops = {
792 .init = __ip_vs_lblcr_init,
793 .exit = __ip_vs_lblcr_exit,
796 static int __init ip_vs_lblcr_init(void)
800 ret = register_pernet_subsys(&ip_vs_lblcr_ops);
804 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
806 unregister_pernet_subsys(&ip_vs_lblcr_ops);
810 static void __exit ip_vs_lblcr_cleanup(void)
812 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
813 unregister_pernet_subsys(&ip_vs_lblcr_ops);
818 module_init(ip_vs_lblcr_init);
819 module_exit(ip_vs_lblcr_cleanup);
820 MODULE_LICENSE("GPL");