1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#define KMSG_COMPONENT "IPVS"
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
43#include <linux/ip.h>
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>
51
52
53#include <linux/fs.h>
54#include <linux/sysctl.h>
55#include <net/net_namespace.h>
56
57#include <net/ip_vs.h>
58
59
60
61
62
63
64#define CHECK_EXPIRE_INTERVAL (60*HZ)
65#define ENTRY_TIMEOUT (6*60*HZ)
66
67#define DEFAULT_EXPIRATION (24*60*60*HZ)
68
69
70
71
72
73
74
75#define COUNT_FOR_FULL_EXPIRATION 30
76
77
78
79
80#ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
81#define CONFIG_IP_VS_LBLCR_TAB_BITS 10
82#endif
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)
86
87
88
89
90
91struct ip_vs_dest_set_elem {
92 struct list_head list;
93 struct ip_vs_dest *dest;
94 struct rcu_head rcu_head;
95};
96
97struct ip_vs_dest_set {
98 atomic_t size;
99 unsigned long lastmod;
100 struct list_head list;
101};
102
103
104static void ip_vs_dest_set_insert(struct ip_vs_dest_set *set,
105 struct ip_vs_dest *dest, bool check)
106{
107 struct ip_vs_dest_set_elem *e;
108
109 if (check) {
110 list_for_each_entry(e, &set->list, list) {
111 if (e->dest == dest)
112 return;
113 }
114 }
115
116 e = kmalloc(sizeof(*e), GFP_ATOMIC);
117 if (e == NULL)
118 return;
119
120 ip_vs_dest_hold(dest);
121 e->dest = dest;
122
123 list_add_rcu(&e->list, &set->list);
124 atomic_inc(&set->size);
125
126 set->lastmod = jiffies;
127}
128
129static void ip_vs_lblcr_elem_rcu_free(struct rcu_head *head)
130{
131 struct ip_vs_dest_set_elem *e;
132
133 e = container_of(head, struct ip_vs_dest_set_elem, rcu_head);
134 ip_vs_dest_put_and_free(e->dest);
135 kfree(e);
136}
137
138static void
139ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
140{
141 struct ip_vs_dest_set_elem *e;
142
143 list_for_each_entry(e, &set->list, list) {
144 if (e->dest == dest) {
145
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);
150 break;
151 }
152 }
153}
154
155static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
156{
157 struct ip_vs_dest_set_elem *e, *ep;
158
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);
162 }
163}
164
165
166static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
167{
168 register struct ip_vs_dest_set_elem *e;
169 struct ip_vs_dest *dest, *least;
170 int loh, doh;
171
172
173 list_for_each_entry_rcu(e, &set->list, list) {
174 least = e->dest;
175 if (least->flags & IP_VS_DEST_F_OVERLOAD)
176 continue;
177
178 if ((atomic_read(&least->weight) > 0)
179 && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
180 loh = ip_vs_dest_conn_overhead(least);
181 goto nextstage;
182 }
183 }
184 return NULL;
185
186
187 nextstage:
188 list_for_each_entry_continue_rcu(e, &set->list, list) {
189 dest = e->dest;
190 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
191 continue;
192
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)) {
197 least = dest;
198 loh = doh;
199 }
200 }
201
202 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
203 "activeconns %d refcnt %d weight %d overhead %d\n",
204 __func__,
205 IP_VS_DBG_ADDR(least->af, &least->addr),
206 ntohs(least->port),
207 atomic_read(&least->activeconns),
208 refcount_read(&least->refcnt),
209 atomic_read(&least->weight), loh);
210 return least;
211}
212
213
214
215static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
216{
217 register struct ip_vs_dest_set_elem *e;
218 struct ip_vs_dest *dest, *most;
219 int moh, doh;
220
221 if (set == NULL)
222 return NULL;
223
224
225 list_for_each_entry(e, &set->list, list) {
226 most = e->dest;
227 if (atomic_read(&most->weight) > 0) {
228 moh = ip_vs_dest_conn_overhead(most);
229 goto nextstage;
230 }
231 }
232 return NULL;
233
234
235 nextstage:
236 list_for_each_entry_continue(e, &set->list, list) {
237 dest = e->dest;
238 doh = ip_vs_dest_conn_overhead(dest);
239
240 if (((__s64)moh * atomic_read(&dest->weight) <
241 (__s64)doh * atomic_read(&most->weight))
242 && (atomic_read(&dest->weight) > 0)) {
243 most = dest;
244 moh = doh;
245 }
246 }
247
248 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
249 "activeconns %d refcnt %d weight %d overhead %d\n",
250 __func__,
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);
255 return most;
256}
257
258
259
260
261
262
263struct ip_vs_lblcr_entry {
264 struct hlist_node list;
265 int af;
266 union nf_inet_addr addr;
267 struct ip_vs_dest_set set;
268 unsigned long lastuse;
269 struct rcu_head rcu_head;
270};
271
272
273
274
275
276struct ip_vs_lblcr_table {
277 struct rcu_head rcu_head;
278 struct hlist_head bucket[IP_VS_LBLCR_TAB_SIZE];
279 atomic_t entries;
280 int max_size;
281 struct timer_list periodic_timer;
282 struct ip_vs_service *svc;
283 int rover;
284 int counter;
285 bool dead;
286};
287
288
289#ifdef CONFIG_SYSCTL
290
291
292
293
294static struct ctl_table vs_vars_table[] = {
295 {
296 .procname = "lblcr_expiration",
297 .data = NULL,
298 .maxlen = sizeof(int),
299 .mode = 0644,
300 .proc_handler = proc_dointvec_jiffies,
301 },
302 { }
303};
304#endif
305
306static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
307{
308 hlist_del_rcu(&en->list);
309 ip_vs_dest_set_eraseall(&en->set);
310 kfree_rcu(en, rcu_head);
311}
312
313
314
315
316
317static inline unsigned int
318ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
319{
320 __be32 addr_fold = addr->ip;
321
322#ifdef CONFIG_IP_VS_IPV6
323 if (af == AF_INET6)
324 addr_fold = addr->ip6[0]^addr->ip6[1]^
325 addr->ip6[2]^addr->ip6[3];
326#endif
327 return hash_32(ntohl(addr_fold), IP_VS_LBLCR_TAB_BITS);
328}
329
330
331
332
333
334
335static void
336ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
337{
338 unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
339
340 hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
341 atomic_inc(&tbl->entries);
342}
343
344
345
346static inline struct ip_vs_lblcr_entry *
347ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
348 const union nf_inet_addr *addr)
349{
350 unsigned int hash = ip_vs_lblcr_hashkey(af, addr);
351 struct ip_vs_lblcr_entry *en;
352
353 hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
354 if (ip_vs_addr_equal(af, &en->addr, addr))
355 return en;
356
357 return NULL;
358}
359
360
361
362
363
364
365static inline struct ip_vs_lblcr_entry *
366ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
367 u16 af, struct ip_vs_dest *dest)
368{
369 struct ip_vs_lblcr_entry *en;
370
371 en = ip_vs_lblcr_get(af, tbl, daddr);
372 if (!en) {
373 en = kmalloc(sizeof(*en), GFP_ATOMIC);
374 if (!en)
375 return NULL;
376
377 en->af = af;
378 ip_vs_addr_copy(af, &en->addr, daddr);
379 en->lastuse = jiffies;
380
381
382 atomic_set(&(en->set.size), 0);
383 INIT_LIST_HEAD(&en->set.list);
384
385 ip_vs_dest_set_insert(&en->set, dest, false);
386
387 ip_vs_lblcr_hash(tbl, en);
388 return en;
389 }
390
391 ip_vs_dest_set_insert(&en->set, dest, true);
392
393 return en;
394}
395
396
397
398
399
400static void ip_vs_lblcr_flush(struct ip_vs_service *svc)
401{
402 struct ip_vs_lblcr_table *tbl = svc->sched_data;
403 int i;
404 struct ip_vs_lblcr_entry *en;
405 struct hlist_node *next;
406
407 spin_lock_bh(&svc->sched_lock);
408 tbl->dead = true;
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);
412 }
413 }
414 spin_unlock_bh(&svc->sched_lock);
415}
416
417static int sysctl_lblcr_expiration(struct ip_vs_service *svc)
418{
419#ifdef CONFIG_SYSCTL
420 return svc->ipvs->sysctl_lblcr_expiration;
421#else
422 return DEFAULT_EXPIRATION;
423#endif
424}
425
426static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
427{
428 struct ip_vs_lblcr_table *tbl = svc->sched_data;
429 unsigned long now = jiffies;
430 int i, j;
431 struct ip_vs_lblcr_entry *en;
432 struct hlist_node *next;
433
434 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) {
435 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
436
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))
441 continue;
442
443 ip_vs_lblcr_free(en);
444 atomic_dec(&tbl->entries);
445 }
446 spin_unlock(&svc->sched_lock);
447 }
448 tbl->rover = j;
449}
450
451
452
453
454
455
456
457
458
459
460
461
462
463static void ip_vs_lblcr_check_expire(struct timer_list *t)
464{
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;
468 int goal;
469 int i, j;
470 struct ip_vs_lblcr_entry *en;
471 struct hlist_node *next;
472
473 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
474
475 ip_vs_lblcr_full_check(svc);
476 tbl->counter = 1;
477 goto out;
478 }
479
480 if (atomic_read(&tbl->entries) <= tbl->max_size) {
481 tbl->counter++;
482 goto out;
483 }
484
485 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
486 if (goal > tbl->max_size/2)
487 goal = tbl->max_size/2;
488
489 for (i = 0, j = tbl->rover; i < IP_VS_LBLCR_TAB_SIZE; i++) {
490 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
491
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))
495 continue;
496
497 ip_vs_lblcr_free(en);
498 atomic_dec(&tbl->entries);
499 goal--;
500 }
501 spin_unlock(&svc->sched_lock);
502 if (goal <= 0)
503 break;
504 }
505 tbl->rover = j;
506
507 out:
508 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
509}
510
511static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
512{
513 int i;
514 struct ip_vs_lblcr_table *tbl;
515
516
517
518
519 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
520 if (tbl == NULL)
521 return -ENOMEM;
522
523 svc->sched_data = tbl;
524 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) allocated for "
525 "current service\n", sizeof(*tbl));
526
527
528
529
530 for (i = 0; i < IP_VS_LBLCR_TAB_SIZE; i++) {
531 INIT_HLIST_HEAD(&tbl->bucket[i]);
532 }
533 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
534 tbl->rover = 0;
535 tbl->counter = 1;
536 tbl->dead = false;
537 tbl->svc = svc;
538 atomic_set(&tbl->entries, 0);
539
540
541
542
543 timer_setup(&tbl->periodic_timer, ip_vs_lblcr_check_expire, 0);
544 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
545
546 return 0;
547}
548
549
550static void ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
551{
552 struct ip_vs_lblcr_table *tbl = svc->sched_data;
553
554
555 del_timer_sync(&tbl->periodic_timer);
556
557
558 ip_vs_lblcr_flush(svc);
559
560
561 kfree_rcu(tbl, rcu_head);
562 IP_VS_DBG(6, "LBLCR hash table (memory=%zdbytes) released\n",
563 sizeof(*tbl));
564}
565
566
567static inline struct ip_vs_dest *
568__ip_vs_lblcr_schedule(struct ip_vs_service *svc)
569{
570 struct ip_vs_dest *dest, *least;
571 int loh, doh;
572
573
574
575
576
577
578
579
580
581
582
583
584
585 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
586 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
587 continue;
588
589 if (atomic_read(&dest->weight) > 0) {
590 least = dest;
591 loh = ip_vs_dest_conn_overhead(least);
592 goto nextstage;
593 }
594 }
595 return NULL;
596
597
598
599
600 nextstage:
601 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
602 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
603 continue;
604
605 doh = ip_vs_dest_conn_overhead(dest);
606 if ((__s64)loh * atomic_read(&dest->weight) >
607 (__s64)doh * atomic_read(&least->weight)) {
608 least = dest;
609 loh = doh;
610 }
611 }
612
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),
616 ntohs(least->port),
617 atomic_read(&least->activeconns),
618 refcount_read(&least->refcnt),
619 atomic_read(&least->weight), loh);
620
621 return least;
622}
623
624
625
626
627
628
629static inline int
630is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
631{
632 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
633 struct ip_vs_dest *d;
634
635 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
636 if (atomic_read(&d->activeconns)*2
637 < atomic_read(&d->weight)) {
638 return 1;
639 }
640 }
641 }
642 return 0;
643}
644
645
646
647
648
649static struct ip_vs_dest *
650ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
651 struct ip_vs_iphdr *iph)
652{
653 struct ip_vs_lblcr_table *tbl = svc->sched_data;
654 struct ip_vs_dest *dest;
655 struct ip_vs_lblcr_entry *en;
656
657 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
658
659
660 en = ip_vs_lblcr_get(svc->af, tbl, &iph->daddr);
661 if (en) {
662 en->lastuse = jiffies;
663
664
665 dest = ip_vs_dest_set_min(&en->set);
666
667
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;
674
675 m = ip_vs_dest_set_max(&en->set);
676 if (m)
677 ip_vs_dest_set_erase(&en->set, m);
678 }
679 spin_unlock_bh(&svc->sched_lock);
680 }
681
682
683 if (dest && !is_overloaded(dest, svc))
684 goto out;
685
686
687 dest = __ip_vs_lblcr_schedule(svc);
688 if (!dest) {
689 ip_vs_scheduler_err(svc, "no destination available");
690 return NULL;
691 }
692
693
694 spin_lock_bh(&svc->sched_lock);
695 if (!tbl->dead)
696 ip_vs_dest_set_insert(&en->set, dest, true);
697 spin_unlock_bh(&svc->sched_lock);
698 goto out;
699 }
700
701
702 dest = __ip_vs_lblcr_schedule(svc);
703 if (!dest) {
704 IP_VS_DBG(1, "no destination available\n");
705 return NULL;
706 }
707
708
709 spin_lock_bh(&svc->sched_lock);
710 if (!tbl->dead)
711 ip_vs_lblcr_new(tbl, &iph->daddr, svc->af, dest);
712 spin_unlock_bh(&svc->sched_lock);
713
714out:
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));
718
719 return dest;
720}
721
722
723
724
725
726static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
727{
728 .name = "lblcr",
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,
735};
736
737
738
739
740#ifdef CONFIG_SYSCTL
741static int __net_init __ip_vs_lblcr_init(struct net *net)
742{
743 struct netns_ipvs *ipvs = net_ipvs(net);
744
745 if (!ipvs)
746 return -ENOENT;
747
748 if (!net_eq(net, &init_net)) {
749 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table,
750 sizeof(vs_vars_table),
751 GFP_KERNEL);
752 if (ipvs->lblcr_ctl_table == NULL)
753 return -ENOMEM;
754
755
756 if (net->user_ns != &init_user_ns)
757 ipvs->lblcr_ctl_table[0].procname = NULL;
758 } else
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;
762
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);
768 return -ENOMEM;
769 }
770
771 return 0;
772}
773
774static void __net_exit __ip_vs_lblcr_exit(struct net *net)
775{
776 struct netns_ipvs *ipvs = net_ipvs(net);
777
778 unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
779
780 if (!net_eq(net, &init_net))
781 kfree(ipvs->lblcr_ctl_table);
782}
783
784#else
785
786static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
787static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
788
789#endif
790
791static struct pernet_operations ip_vs_lblcr_ops = {
792 .init = __ip_vs_lblcr_init,
793 .exit = __ip_vs_lblcr_exit,
794};
795
796static int __init ip_vs_lblcr_init(void)
797{
798 int ret;
799
800 ret = register_pernet_subsys(&ip_vs_lblcr_ops);
801 if (ret)
802 return ret;
803
804 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
805 if (ret)
806 unregister_pernet_subsys(&ip_vs_lblcr_ops);
807 return ret;
808}
809
810static void __exit ip_vs_lblcr_cleanup(void)
811{
812 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
813 unregister_pernet_subsys(&ip_vs_lblcr_ops);
814 rcu_barrier();
815}
816
817
818module_init(ip_vs_lblcr_init);
819module_exit(ip_vs_lblcr_cleanup);
820MODULE_LICENSE("GPL");
821