ipvs: convert lblcr scheduler to rcu
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipvs / ip_vs_lblcr.c
1 /*
2  * IPVS:        Locality-Based Least-Connection with Replication scheduler
3  *
4  * Authors:     Wensong Zhang <wensong@gnuchina.org>
5  *
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.
10  *
11  * Changes:
12  *     Julian Anastasov        :    Added the missing (dest->weight>0)
13  *                                  condition in the ip_vs_dest_set_max.
14  *
15  */
16
17 /*
18  * The lblc/r algorithm is as follows (pseudo code):
19  *
20  *       if serverSet[dest_ip] is null then
21  *               n, serverSet[dest_ip] <- {weighted least-conn node};
22  *       else
23  *               n <- {least-conn (alive) node in serverSet[dest_ip]};
24  *               if (n is null) OR
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;
35  *
36  *       return n;
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
51 /* for sysctl */
52 #include <linux/fs.h>
53 #include <linux/sysctl.h>
54 #include <net/net_namespace.h>
55
56 #include <net/ip_vs.h>
57
58
59 /*
60  *    It is for garbage collection of stale IPVS lblcr entries,
61  *    when the table is full.
62  */
63 #define CHECK_EXPIRE_INTERVAL   (60*HZ)
64 #define ENTRY_TIMEOUT           (6*60*HZ)
65
66 #define DEFAULT_EXPIRATION      (24*60*60*HZ)
67
68 /*
69  *    It is for full expiration check.
70  *    When there is no partial expiration check (garbage collection)
71  *    in a half hour, do a full expiration check to collect stale
72  *    entries that haven't been touched for a day.
73  */
74 #define COUNT_FOR_FULL_EXPIRATION   30
75
76 /*
77  *     for IPVS lblcr entry hash table
78  */
79 #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
80 #define CONFIG_IP_VS_LBLCR_TAB_BITS      10
81 #endif
82 #define IP_VS_LBLCR_TAB_BITS     CONFIG_IP_VS_LBLCR_TAB_BITS
83 #define IP_VS_LBLCR_TAB_SIZE     (1 << IP_VS_LBLCR_TAB_BITS)
84 #define IP_VS_LBLCR_TAB_MASK     (IP_VS_LBLCR_TAB_SIZE - 1)
85
86
87 /*
88  *      IPVS destination set structure and operations
89  */
90 struct ip_vs_dest_set_elem {
91         struct list_head        list;          /* list link */
92         struct ip_vs_dest __rcu *dest;         /* destination server */
93         struct rcu_head         rcu_head;
94 };
95
96 struct ip_vs_dest_set {
97         atomic_t                size;           /* set size */
98         unsigned long           lastmod;        /* last modified time */
99         struct list_head        list;           /* destination list */
100 };
101
102
103 static void ip_vs_dest_set_insert(struct ip_vs_dest_set *set,
104                                   struct ip_vs_dest *dest, bool check)
105 {
106         struct ip_vs_dest_set_elem *e;
107
108         if (check) {
109                 list_for_each_entry(e, &set->list, list) {
110                         struct ip_vs_dest *d;
111
112                         d = rcu_dereference_protected(e->dest, 1);
113                         if (d == dest)
114                                 /* already existed */
115                                 return;
116                 }
117         }
118
119         e = kmalloc(sizeof(*e), GFP_ATOMIC);
120         if (e == NULL)
121                 return;
122
123         ip_vs_dest_hold(dest);
124         RCU_INIT_POINTER(e->dest, dest);
125
126         list_add_rcu(&e->list, &set->list);
127         atomic_inc(&set->size);
128
129         set->lastmod = jiffies;
130 }
131
132 static void
133 ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
134 {
135         struct ip_vs_dest_set_elem *e;
136
137         list_for_each_entry(e, &set->list, list) {
138                 struct ip_vs_dest *d;
139
140                 d = rcu_dereference_protected(e->dest, 1);
141                 if (d == dest) {
142                         /* HIT */
143                         atomic_dec(&set->size);
144                         set->lastmod = jiffies;
145                         ip_vs_dest_put(dest);
146                         list_del_rcu(&e->list);
147                         kfree_rcu(e, rcu_head);
148                         break;
149                 }
150         }
151 }
152
153 static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
154 {
155         struct ip_vs_dest_set_elem *e, *ep;
156
157         list_for_each_entry_safe(e, ep, &set->list, list) {
158                 struct ip_vs_dest *d;
159
160                 d = rcu_dereference_protected(e->dest, 1);
161                 /*
162                  * We don't kfree dest because it is referred either
163                  * by its service or by the trash dest list.
164                  */
165                 ip_vs_dest_put(d);
166                 list_del_rcu(&e->list);
167                 kfree_rcu(e, rcu_head);
168         }
169 }
170
171 /* get weighted least-connection node in the destination set */
172 static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
173 {
174         register struct ip_vs_dest_set_elem *e;
175         struct ip_vs_dest *dest, *least;
176         int loh, doh;
177
178         if (set == NULL)
179                 return NULL;
180
181         /* select the first destination server, whose weight > 0 */
182         list_for_each_entry_rcu(e, &set->list, list) {
183                 least = rcu_dereference(e->dest);
184                 if (least->flags & IP_VS_DEST_F_OVERLOAD)
185                         continue;
186
187                 if ((atomic_read(&least->weight) > 0)
188                     && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
189                         loh = ip_vs_dest_conn_overhead(least);
190                         goto nextstage;
191                 }
192         }
193         return NULL;
194
195         /* find the destination with the weighted least load */
196   nextstage:
197         list_for_each_entry_continue_rcu(e, &set->list, list) {
198                 dest = rcu_dereference(e->dest);
199                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
200                         continue;
201
202                 doh = ip_vs_dest_conn_overhead(dest);
203                 if ((loh * atomic_read(&dest->weight) >
204                      doh * atomic_read(&least->weight))
205                     && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
206                         least = dest;
207                         loh = doh;
208                 }
209         }
210
211         IP_VS_DBG_BUF(6, "%s(): server %s:%d "
212                       "activeconns %d refcnt %d weight %d overhead %d\n",
213                       __func__,
214                       IP_VS_DBG_ADDR(least->af, &least->addr),
215                       ntohs(least->port),
216                       atomic_read(&least->activeconns),
217                       atomic_read(&least->refcnt),
218                       atomic_read(&least->weight), loh);
219         return least;
220 }
221
222
223 /* get weighted most-connection node in the destination set */
224 static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
225 {
226         register struct ip_vs_dest_set_elem *e;
227         struct ip_vs_dest *dest, *most;
228         int moh, doh;
229
230         if (set == NULL)
231                 return NULL;
232
233         /* select the first destination server, whose weight > 0 */
234         list_for_each_entry(e, &set->list, list) {
235                 most = rcu_dereference_protected(e->dest, 1);
236                 if (atomic_read(&most->weight) > 0) {
237                         moh = ip_vs_dest_conn_overhead(most);
238                         goto nextstage;
239                 }
240         }
241         return NULL;
242
243         /* find the destination with the weighted most load */
244   nextstage:
245         list_for_each_entry_continue(e, &set->list, list) {
246                 dest = rcu_dereference_protected(e->dest, 1);
247                 doh = ip_vs_dest_conn_overhead(dest);
248                 /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
249                 if ((moh * atomic_read(&dest->weight) <
250                      doh * atomic_read(&most->weight))
251                     && (atomic_read(&dest->weight) > 0)) {
252                         most = dest;
253                         moh = doh;
254                 }
255         }
256
257         IP_VS_DBG_BUF(6, "%s(): server %s:%d "
258                       "activeconns %d refcnt %d weight %d overhead %d\n",
259                       __func__,
260                       IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port),
261                       atomic_read(&most->activeconns),
262                       atomic_read(&most->refcnt),
263                       atomic_read(&most->weight), moh);
264         return most;
265 }
266
267
268 /*
269  *      IPVS lblcr entry represents an association between destination
270  *      IP address and its destination server set
271  */
272 struct ip_vs_lblcr_entry {
273         struct hlist_node       list;
274         int                     af;             /* address family */
275         union nf_inet_addr      addr;           /* destination IP address */
276         struct ip_vs_dest_set   set;            /* destination server set */
277         unsigned long           lastuse;        /* last used time */
278         struct rcu_head         rcu_head;
279 };
280
281
282 /*
283  *      IPVS lblcr hash table
284  */
285 struct ip_vs_lblcr_table {
286         struct rcu_head         rcu_head;
287         struct hlist_head __rcu bucket[IP_VS_LBLCR_TAB_SIZE];  /* hash bucket */
288         atomic_t                entries;        /* number of entries */
289         int                     max_size;       /* maximum size of entries */
290         struct timer_list       periodic_timer; /* collect stale entries */
291         int                     rover;          /* rover for expire check */
292         int                     counter;        /* counter for no expire */
293         bool                    dead;
294 };
295
296
297 #ifdef CONFIG_SYSCTL
298 /*
299  *      IPVS LBLCR sysctl table
300  */
301
302 static ctl_table vs_vars_table[] = {
303         {
304                 .procname       = "lblcr_expiration",
305                 .data           = NULL,
306                 .maxlen         = sizeof(int),
307                 .mode           = 0644,
308                 .proc_handler   = proc_dointvec_jiffies,
309         },
310         { }
311 };
312 #endif
313
314 static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
315 {
316         hlist_del_rcu(&en->list);
317         ip_vs_dest_set_eraseall(&en->set);
318         kfree_rcu(en, rcu_head);
319 }
320
321
322 /*
323  *      Returns hash value for IPVS LBLCR entry
324  */
325 static inline unsigned int
326 ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
327 {
328         __be32 addr_fold = addr->ip;
329
330 #ifdef CONFIG_IP_VS_IPV6
331         if (af == AF_INET6)
332                 addr_fold = addr->ip6[0]^addr->ip6[1]^
333                             addr->ip6[2]^addr->ip6[3];
334 #endif
335         return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
336 }
337
338
339 /*
340  *      Hash an entry in the ip_vs_lblcr_table.
341  *      returns bool success.
342  */
343 static void
344 ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
345 {
346         unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
347
348         hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
349         atomic_inc(&tbl->entries);
350 }
351
352
353 /* Get ip_vs_lblcr_entry associated with supplied parameters. */
354 static inline struct ip_vs_lblcr_entry *
355 ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
356                 const union nf_inet_addr *addr)
357 {
358         unsigned int hash = ip_vs_lblcr_hashkey(af, addr);
359         struct ip_vs_lblcr_entry *en;
360
361         hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
362                 if (ip_vs_addr_equal(af, &en->addr, addr))
363                         return en;
364
365         return NULL;
366 }
367
368
369 /*
370  * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination
371  * IP address to a server. Called under write lock.
372  */
373 static inline struct ip_vs_lblcr_entry *
374 ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
375                 struct ip_vs_dest *dest)
376 {
377         struct ip_vs_lblcr_entry *en;
378
379         en = ip_vs_lblcr_get(dest->af, tbl, daddr);
380         if (!en) {
381                 en = kmalloc(sizeof(*en), GFP_ATOMIC);
382                 if (!en)
383                         return NULL;
384
385                 en->af = dest->af;
386                 ip_vs_addr_copy(dest->af, &en->addr, daddr);
387                 en->lastuse = jiffies;
388
389                 /* initialize its dest set */
390                 atomic_set(&(en->set.size), 0);
391                 INIT_LIST_HEAD(&en->set.list);
392
393                 ip_vs_dest_set_insert(&en->set, dest, false);
394
395                 ip_vs_lblcr_hash(tbl, en);
396                 return en;
397         }
398
399         ip_vs_dest_set_insert(&en->set, dest, true);
400
401         return en;
402 }
403
404
405 /*
406  *      Flush all the entries of the specified table.
407  */
408 static void ip_vs_lblcr_flush(struct ip_vs_service *svc)
409 {
410         struct ip_vs_lblcr_table *tbl = svc->sched_data;
411         int i;
412         struct ip_vs_lblcr_entry *en;
413         struct hlist_node *next;
414
415         write_lock_bh(&svc->sched_lock);
416         tbl->dead = 1;
417         for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
418                 hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) {
419                         ip_vs_lblcr_free(en);
420                 }
421         }
422         write_unlock_bh(&svc->sched_lock);
423 }
424
425 static int sysctl_lblcr_expiration(struct ip_vs_service *svc)
426 {
427 #ifdef CONFIG_SYSCTL
428         struct netns_ipvs *ipvs = net_ipvs(svc->net);
429         return ipvs->sysctl_lblcr_expiration;
430 #else
431         return DEFAULT_EXPIRATION;
432 #endif
433 }
434
435 static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
436 {
437         struct ip_vs_lblcr_table *tbl = svc->sched_data;
438         unsigned long now = jiffies;
439         int i, j;
440         struct ip_vs_lblcr_entry *en;
441         struct hlist_node *next;
442
443         for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
444                 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
445
446                 write_lock(&svc->sched_lock);
447                 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
448                         if (time_after(en->lastuse +
449                                        sysctl_lblcr_expiration(svc), now))
450                                 continue;
451
452                         ip_vs_lblcr_free(en);
453                         atomic_dec(&tbl->entries);
454                 }
455                 write_unlock(&svc->sched_lock);
456         }
457         tbl->rover = j;
458 }
459
460
461 /*
462  *      Periodical timer handler for IPVS lblcr table
463  *      It is used to collect stale entries when the number of entries
464  *      exceeds the maximum size of the table.
465  *
466  *      Fixme: we probably need more complicated algorithm to collect
467  *             entries that have not been used for a long time even
468  *             if the number of entries doesn't exceed the maximum size
469  *             of the table.
470  *      The full expiration check is for this purpose now.
471  */
472 static void ip_vs_lblcr_check_expire(unsigned long data)
473 {
474         struct ip_vs_service *svc = (struct ip_vs_service *) data;
475         struct ip_vs_lblcr_table *tbl = svc->sched_data;
476         unsigned long now = jiffies;
477         int goal;
478         int i, j;
479         struct ip_vs_lblcr_entry *en;
480         struct hlist_node *next;
481
482         if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
483                 /* do full expiration check */
484                 ip_vs_lblcr_full_check(svc);
485                 tbl->counter = 1;
486                 goto out;
487         }
488
489         if (atomic_read(&tbl->entries) <= tbl->max_size) {
490                 tbl->counter++;
491                 goto out;
492         }
493
494         goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
495         if (goal > tbl->max_size/2)
496                 goal = tbl->max_size/2;
497
498         for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
499                 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
500
501                 write_lock(&svc->sched_lock);
502                 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
503                         if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
504                                 continue;
505
506                         ip_vs_lblcr_free(en);
507                         atomic_dec(&tbl->entries);
508                         goal--;
509                 }
510                 write_unlock(&svc->sched_lock);
511                 if (goal <= 0)
512                         break;
513         }
514         tbl->rover = j;
515
516   out:
517         mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
518 }
519
520 static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
521 {
522         int i;
523         struct ip_vs_lblcr_table *tbl;
524
525         /*
526          *    Allocate the ip_vs_lblcr_table for this service
527          */
528         tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
529         if (tbl == NULL)
530                 return -ENOMEM;
531
532         svc->sched_data = tbl;
533         IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
534                   "current service\n", sizeof(*tbl));
535
536         /*
537          *    Initialize the hash buckets
538          */
539         for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
540                 INIT_HLIST_HEAD(&tbl->bucket[i]);
541         }
542         tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
543         tbl->rover = 0;
544         tbl->counter = 1;
545         tbl->dead = 0;
546
547         /*
548          *    Hook periodic timer for garbage collection
549          */
550         setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
551                         (unsigned long)svc);
552         mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
553
554         return 0;
555 }
556
557
558 static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
559 {
560         struct ip_vs_lblcr_table *tbl = svc->sched_data;
561
562         /* remove periodic timer */
563         del_timer_sync(&tbl->periodic_timer);
564
565         /* got to clean up table entries here */
566         ip_vs_lblcr_flush(svc);
567
568         /* release the table itself */
569         kfree_rcu(tbl, rcu_head);
570         IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
571                   sizeof(*tbl));
572
573         return 0;
574 }
575
576
577 static inline struct ip_vs_dest *
578 __ip_vs_lblcr_schedule(struct ip_vs_service *svc)
579 {
580         struct ip_vs_dest *dest, *least;
581         int loh, doh;
582
583         /*
584          * We use the following formula to estimate the load:
585          *                (dest overhead) / dest->weight
586          *
587          * Remember -- no floats in kernel mode!!!
588          * The comparison of h1*w2 > h2*w1 is equivalent to that of
589          *                h1/w1 > h2/w2
590          * if every weight is larger than zero.
591          *
592          * The server with weight=0 is quiesced and will not receive any
593          * new connection.
594          */
595         list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
596                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
597                         continue;
598
599                 if (atomic_read(&dest->weight) > 0) {
600                         least = dest;
601                         loh = ip_vs_dest_conn_overhead(least);
602                         goto nextstage;
603                 }
604         }
605         return NULL;
606
607         /*
608          *    Find the destination with the least load.
609          */
610   nextstage:
611         list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
612                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
613                         continue;
614
615                 doh = ip_vs_dest_conn_overhead(dest);
616                 if (loh * atomic_read(&dest->weight) >
617                     doh * atomic_read(&least->weight)) {
618                         least = dest;
619                         loh = doh;
620                 }
621         }
622
623         IP_VS_DBG_BUF(6, "LBLCR: server %s:%d "
624                       "activeconns %d refcnt %d weight %d overhead %d\n",
625                       IP_VS_DBG_ADDR(least->af, &least->addr),
626                       ntohs(least->port),
627                       atomic_read(&least->activeconns),
628                       atomic_read(&least->refcnt),
629                       atomic_read(&least->weight), loh);
630
631         return least;
632 }
633
634
635 /*
636  *   If this destination server is overloaded and there is a less loaded
637  *   server, then return true.
638  */
639 static inline int
640 is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
641 {
642         if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
643                 struct ip_vs_dest *d;
644
645                 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
646                         if (atomic_read(&d->activeconns)*2
647                             < atomic_read(&d->weight)) {
648                                 return 1;
649                         }
650                 }
651         }
652         return 0;
653 }
654
655
656 /*
657  *    Locality-Based (weighted) Least-Connection scheduling
658  */
659 static struct ip_vs_dest *
660 ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
661 {
662         struct ip_vs_lblcr_table *tbl = svc->sched_data;
663         struct ip_vs_iphdr iph;
664         struct ip_vs_dest *dest;
665         struct ip_vs_lblcr_entry *en;
666
667         ip_vs_fill_iph_addr_only(svc->af, skb, &iph);
668
669         IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
670
671         /* First look in our cache */
672         en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr);
673         if (en) {
674                 en->lastuse = jiffies;
675
676                 /* Get the least loaded destination */
677                 dest = ip_vs_dest_set_min(&en->set);
678
679                 /* More than one destination + enough time passed by, cleanup */
680                 if (atomic_read(&en->set.size) > 1 &&
681                     time_after(jiffies, en->set.lastmod +
682                                 sysctl_lblcr_expiration(svc))) {
683                         write_lock(&svc->sched_lock);
684                         if (atomic_read(&en->set.size) > 1) {
685                                 struct ip_vs_dest *m;
686
687                                 m = ip_vs_dest_set_max(&en->set);
688                                 if (m)
689                                         ip_vs_dest_set_erase(&en->set, m);
690                         }
691                         write_unlock(&svc->sched_lock);
692                 }
693
694                 /* If the destination is not overloaded, use it */
695                 if (dest && !is_overloaded(dest, svc))
696                         goto out;
697
698                 /* The cache entry is invalid, time to schedule */
699                 dest = __ip_vs_lblcr_schedule(svc);
700                 if (!dest) {
701                         ip_vs_scheduler_err(svc, "no destination available");
702                         return NULL;
703                 }
704
705                 /* Update our cache entry */
706                 write_lock(&svc->sched_lock);
707                 if (!tbl->dead)
708                         ip_vs_dest_set_insert(&en->set, dest, true);
709                 write_unlock(&svc->sched_lock);
710                 goto out;
711         }
712
713         /* No cache entry, time to schedule */
714         dest = __ip_vs_lblcr_schedule(svc);
715         if (!dest) {
716                 IP_VS_DBG(1, "no destination available\n");
717                 return NULL;
718         }
719
720         /* If we fail to create a cache entry, we'll just use the valid dest */
721         write_lock(&svc->sched_lock);
722         if (!tbl->dead)
723                 ip_vs_lblcr_new(tbl, &iph.daddr, dest);
724         write_unlock(&svc->sched_lock);
725
726 out:
727         IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n",
728                       IP_VS_DBG_ADDR(svc->af, &iph.daddr),
729                       IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port));
730
731         return dest;
732 }
733
734
735 /*
736  *      IPVS LBLCR Scheduler structure
737  */
738 static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
739 {
740         .name =                 "lblcr",
741         .refcnt =               ATOMIC_INIT(0),
742         .module =               THIS_MODULE,
743         .n_list =               LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
744         .init_service =         ip_vs_lblcr_init_svc,
745         .done_service =         ip_vs_lblcr_done_svc,
746         .schedule =             ip_vs_lblcr_schedule,
747 };
748
749 /*
750  *  per netns init.
751  */
752 #ifdef CONFIG_SYSCTL
753 static int __net_init __ip_vs_lblcr_init(struct net *net)
754 {
755         struct netns_ipvs *ipvs = net_ipvs(net);
756
757         if (!ipvs)
758                 return -ENOENT;
759
760         if (!net_eq(net, &init_net)) {
761                 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table,
762                                                 sizeof(vs_vars_table),
763                                                 GFP_KERNEL);
764                 if (ipvs->lblcr_ctl_table == NULL)
765                         return -ENOMEM;
766
767                 /* Don't export sysctls to unprivileged users */
768                 if (net->user_ns != &init_user_ns)
769                         ipvs->lblcr_ctl_table[0].procname = NULL;
770         } else
771                 ipvs->lblcr_ctl_table = vs_vars_table;
772         ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION;
773         ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration;
774
775         ipvs->lblcr_ctl_header =
776                 register_net_sysctl(net, "net/ipv4/vs", ipvs->lblcr_ctl_table);
777         if (!ipvs->lblcr_ctl_header) {
778                 if (!net_eq(net, &init_net))
779                         kfree(ipvs->lblcr_ctl_table);
780                 return -ENOMEM;
781         }
782
783         return 0;
784 }
785
786 static void __net_exit __ip_vs_lblcr_exit(struct net *net)
787 {
788         struct netns_ipvs *ipvs = net_ipvs(net);
789
790         unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
791
792         if (!net_eq(net, &init_net))
793                 kfree(ipvs->lblcr_ctl_table);
794 }
795
796 #else
797
798 static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
799 static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
800
801 #endif
802
803 static struct pernet_operations ip_vs_lblcr_ops = {
804         .init = __ip_vs_lblcr_init,
805         .exit = __ip_vs_lblcr_exit,
806 };
807
808 static int __init ip_vs_lblcr_init(void)
809 {
810         int ret;
811
812         ret = register_pernet_subsys(&ip_vs_lblcr_ops);
813         if (ret)
814                 return ret;
815
816         ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
817         if (ret)
818                 unregister_pernet_subsys(&ip_vs_lblcr_ops);
819         return ret;
820 }
821
822 static void __exit ip_vs_lblcr_cleanup(void)
823 {
824         unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
825         unregister_pernet_subsys(&ip_vs_lblcr_ops);
826 }
827
828
829 module_init(ip_vs_lblcr_init);
830 module_exit(ip_vs_lblcr_cleanup);
831 MODULE_LICENSE("GPL");