ipvs: do not disable bh for long time
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / ipvs / ip_vs_lblcr.c
CommitLineData
1da177e4
LT
1/*
2 * IPVS: Locality-Based Least-Connection with Replication scheduler
3 *
1da177e4
LT
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
9aada7ac
HE
40#define KMSG_COMPONENT "IPVS"
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
14c85021 43#include <linux/ip.h>
1da177e4
LT
44#include <linux/module.h>
45#include <linux/kernel.h>
14c85021 46#include <linux/skbuff.h>
d7fe0f24 47#include <linux/jiffies.h>
51f0bc78 48#include <linux/list.h>
5a0e3ad6 49#include <linux/slab.h>
1da177e4
LT
50
51/* for sysctl */
52#include <linux/fs.h>
53#include <linux/sysctl.h>
457c4cbc 54#include <net/net_namespace.h>
1da177e4
LT
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
b27d777e
SH
66#define DEFAULT_EXPIRATION (24*60*60*HZ)
67
1da177e4
LT
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
1da177e4
LT
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 */
51f0bc78
SH
90struct ip_vs_dest_set_elem {
91 struct list_head list; /* list link */
c5549571
JA
92 struct ip_vs_dest __rcu *dest; /* destination server */
93 struct rcu_head rcu_head;
1da177e4
LT
94};
95
96struct ip_vs_dest_set {
97 atomic_t size; /* set size */
98 unsigned long lastmod; /* last modified time */
51f0bc78 99 struct list_head list; /* destination list */
1da177e4
LT
100};
101
102
c5549571
JA
103static void ip_vs_dest_set_insert(struct ip_vs_dest_set *set,
104 struct ip_vs_dest *dest, bool check)
1da177e4 105{
51f0bc78 106 struct ip_vs_dest_set_elem *e;
1da177e4 107
c5549571
JA
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 }
1da177e4
LT
117 }
118
f728bafb 119 e = kmalloc(sizeof(*e), GFP_ATOMIC);
0a9ee813 120 if (e == NULL)
c5549571 121 return;
1da177e4 122
c5549571
JA
123 ip_vs_dest_hold(dest);
124 RCU_INIT_POINTER(e->dest, dest);
1da177e4 125
c5549571 126 list_add_rcu(&e->list, &set->list);
1da177e4 127 atomic_inc(&set->size);
1da177e4
LT
128
129 set->lastmod = jiffies;
1da177e4
LT
130}
131
132static void
133ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
134{
51f0bc78 135 struct ip_vs_dest_set_elem *e;
1da177e4 136
51f0bc78 137 list_for_each_entry(e, &set->list, list) {
c5549571
JA
138 struct ip_vs_dest *d;
139
140 d = rcu_dereference_protected(e->dest, 1);
141 if (d == dest) {
1da177e4 142 /* HIT */
1da177e4
LT
143 atomic_dec(&set->size);
144 set->lastmod = jiffies;
c5549571
JA
145 ip_vs_dest_put(dest);
146 list_del_rcu(&e->list);
147 kfree_rcu(e, rcu_head);
1da177e4
LT
148 break;
149 }
1da177e4 150 }
1da177e4
LT
151}
152
153static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
154{
51f0bc78 155 struct ip_vs_dest_set_elem *e, *ep;
1da177e4 156
51f0bc78 157 list_for_each_entry_safe(e, ep, &set->list, list) {
c5549571
JA
158 struct ip_vs_dest *d;
159
160 d = rcu_dereference_protected(e->dest, 1);
1da177e4 161 /*
25985edc 162 * We don't kfree dest because it is referred either
1da177e4
LT
163 * by its service or by the trash dest list.
164 */
c5549571
JA
165 ip_vs_dest_put(d);
166 list_del_rcu(&e->list);
167 kfree_rcu(e, rcu_head);
1da177e4 168 }
1da177e4
LT
169}
170
171/* get weighted least-connection node in the destination set */
172static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
173{
51f0bc78 174 register struct ip_vs_dest_set_elem *e;
1da177e4
LT
175 struct ip_vs_dest *dest, *least;
176 int loh, doh;
177
178 if (set == NULL)
179 return NULL;
180
1da177e4 181 /* select the first destination server, whose weight > 0 */
c5549571
JA
182 list_for_each_entry_rcu(e, &set->list, list) {
183 least = rcu_dereference(e->dest);
1da177e4
LT
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)) {
b552f7e3 189 loh = ip_vs_dest_conn_overhead(least);
1da177e4
LT
190 goto nextstage;
191 }
192 }
1da177e4
LT
193 return NULL;
194
195 /* find the destination with the weighted least load */
196 nextstage:
c5549571
JA
197 list_for_each_entry_continue_rcu(e, &set->list, list) {
198 dest = rcu_dereference(e->dest);
1da177e4
LT
199 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
200 continue;
201
b552f7e3 202 doh = ip_vs_dest_conn_overhead(dest);
1da177e4
LT
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 }
1da177e4 210
1e3e238e 211 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
44548375 212 "activeconns %d refcnt %d weight %d overhead %d\n",
1e3e238e 213 __func__,
44548375
JV
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);
1da177e4
LT
219 return least;
220}
221
222
223/* get weighted most-connection node in the destination set */
224static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
225{
51f0bc78 226 register struct ip_vs_dest_set_elem *e;
1da177e4
LT
227 struct ip_vs_dest *dest, *most;
228 int moh, doh;
229
230 if (set == NULL)
231 return NULL;
232
1da177e4 233 /* select the first destination server, whose weight > 0 */
51f0bc78 234 list_for_each_entry(e, &set->list, list) {
c5549571 235 most = rcu_dereference_protected(e->dest, 1);
1da177e4 236 if (atomic_read(&most->weight) > 0) {
b552f7e3 237 moh = ip_vs_dest_conn_overhead(most);
1da177e4
LT
238 goto nextstage;
239 }
240 }
1da177e4
LT
241 return NULL;
242
243 /* find the destination with the weighted most load */
244 nextstage:
c5549571
JA
245 list_for_each_entry_continue(e, &set->list, list) {
246 dest = rcu_dereference_protected(e->dest, 1);
b552f7e3 247 doh = ip_vs_dest_conn_overhead(dest);
1da177e4
LT
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 }
1da177e4 256
1e3e238e 257 IP_VS_DBG_BUF(6, "%s(): server %s:%d "
44548375 258 "activeconns %d refcnt %d weight %d overhead %d\n",
1e3e238e 259 __func__,
44548375
JV
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);
1da177e4
LT
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 */
272struct ip_vs_lblcr_entry {
c5549571 273 struct hlist_node list;
44548375
JV
274 int af; /* address family */
275 union nf_inet_addr addr; /* destination IP address */
1da177e4
LT
276 struct ip_vs_dest_set set; /* destination server set */
277 unsigned long lastuse; /* last used time */
c5549571 278 struct rcu_head rcu_head;
1da177e4
LT
279};
280
281
282/*
283 * IPVS lblcr hash table
284 */
285struct ip_vs_lblcr_table {
c5549571
JA
286 struct rcu_head rcu_head;
287 struct hlist_head __rcu bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */
1da177e4
LT
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 */
c5549571 293 bool dead;
1da177e4
LT
294};
295
296
fb1de432 297#ifdef CONFIG_SYSCTL
1da177e4
LT
298/*
299 * IPVS LBLCR sysctl table
300 */
301
302static ctl_table vs_vars_table[] = {
303 {
1da177e4 304 .procname = "lblcr_expiration",
d0a1eef9 305 .data = NULL,
1da177e4 306 .maxlen = sizeof(int),
e905a9ed 307 .mode = 0644,
6d9f239a 308 .proc_handler = proc_dointvec_jiffies,
1da177e4 309 },
f8572d8f 310 { }
1da177e4 311};
fb1de432 312#endif
1da177e4 313
1da177e4
LT
314static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
315{
c5549571 316 hlist_del_rcu(&en->list);
1da177e4 317 ip_vs_dest_set_eraseall(&en->set);
c5549571 318 kfree_rcu(en, rcu_head);
1da177e4
LT
319}
320
321
322/*
323 * Returns hash value for IPVS LBLCR entry
324 */
95c96174 325static inline unsigned int
44548375 326ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
1da177e4 327{
44548375
JV
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;
1da177e4
LT
336}
337
338
339/*
340 * Hash an entry in the ip_vs_lblcr_table.
341 * returns bool success.
342 */
f728bafb 343static void
1da177e4
LT
344ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
345{
95c96174 346 unsigned int hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
1da177e4 347
c5549571 348 hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
1da177e4 349 atomic_inc(&tbl->entries);
1da177e4
LT
350}
351
352
c5549571 353/* Get ip_vs_lblcr_entry associated with supplied parameters. */
1da177e4 354static inline struct ip_vs_lblcr_entry *
44548375
JV
355ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
356 const union nf_inet_addr *addr)
1da177e4 357{
95c96174 358 unsigned int hash = ip_vs_lblcr_hashkey(af, addr);
1da177e4
LT
359 struct ip_vs_lblcr_entry *en;
360
c5549571 361 hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
44548375 362 if (ip_vs_addr_equal(af, &en->addr, addr))
f728bafb
SW
363 return en;
364
365 return NULL;
366}
1da177e4 367
1da177e4 368
f728bafb
SW
369/*
370 * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination
ba3a3ce1 371 * IP address to a server. Called under spin lock.
f728bafb
SW
372 */
373static inline struct ip_vs_lblcr_entry *
44548375 374ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
f728bafb
SW
375 struct ip_vs_dest *dest)
376{
377 struct ip_vs_lblcr_entry *en;
378
44548375 379 en = ip_vs_lblcr_get(dest->af, tbl, daddr);
f728bafb
SW
380 if (!en) {
381 en = kmalloc(sizeof(*en), GFP_ATOMIC);
0a9ee813 382 if (!en)
f728bafb 383 return NULL;
f728bafb 384
44548375
JV
385 en->af = dest->af;
386 ip_vs_addr_copy(dest->af, &en->addr, daddr);
f728bafb
SW
387 en->lastuse = jiffies;
388
421f91d2 389 /* initialize its dest set */
f728bafb 390 atomic_set(&(en->set.size), 0);
51f0bc78 391 INIT_LIST_HEAD(&en->set.list);
c5549571
JA
392
393 ip_vs_dest_set_insert(&en->set, dest, false);
f728bafb
SW
394
395 ip_vs_lblcr_hash(tbl, en);
c5549571 396 return en;
1da177e4
LT
397 }
398
c5549571 399 ip_vs_dest_set_insert(&en->set, dest, true);
1da177e4 400
f728bafb 401 return en;
1da177e4
LT
402}
403
404
405/*
406 * Flush all the entries of the specified table.
407 */
c5549571 408static void ip_vs_lblcr_flush(struct ip_vs_service *svc)
1da177e4 409{
c5549571 410 struct ip_vs_lblcr_table *tbl = svc->sched_data;
1da177e4 411 int i;
c5549571
JA
412 struct ip_vs_lblcr_entry *en;
413 struct hlist_node *next;
1da177e4 414
ba3a3ce1 415 spin_lock_bh(&svc->sched_lock);
c5549571 416 tbl->dead = 1;
1da177e4 417 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
c5549571 418 hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) {
1da177e4 419 ip_vs_lblcr_free(en);
1da177e4 420 }
1da177e4 421 }
ba3a3ce1 422 spin_unlock_bh(&svc->sched_lock);
1da177e4
LT
423}
424
b27d777e
SH
425static 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}
1da177e4 434
f728bafb 435static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
1da177e4 436{
f728bafb 437 struct ip_vs_lblcr_table *tbl = svc->sched_data;
1da177e4
LT
438 unsigned long now = jiffies;
439 int i, j;
c5549571
JA
440 struct ip_vs_lblcr_entry *en;
441 struct hlist_node *next;
1da177e4
LT
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
ba3a3ce1 446 spin_lock(&svc->sched_lock);
c5549571 447 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
b27d777e
SH
448 if (time_after(en->lastuse +
449 sysctl_lblcr_expiration(svc), now))
1da177e4
LT
450 continue;
451
452 ip_vs_lblcr_free(en);
453 atomic_dec(&tbl->entries);
454 }
ba3a3ce1 455 spin_unlock(&svc->sched_lock);
1da177e4
LT
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 */
472static void ip_vs_lblcr_check_expire(unsigned long data)
473{
f728bafb
SW
474 struct ip_vs_service *svc = (struct ip_vs_service *) data;
475 struct ip_vs_lblcr_table *tbl = svc->sched_data;
1da177e4
LT
476 unsigned long now = jiffies;
477 int goal;
478 int i, j;
c5549571
JA
479 struct ip_vs_lblcr_entry *en;
480 struct hlist_node *next;
1da177e4 481
1da177e4
LT
482 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
483 /* do full expiration check */
f728bafb 484 ip_vs_lblcr_full_check(svc);
1da177e4
LT
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
ba3a3ce1 501 spin_lock(&svc->sched_lock);
c5549571 502 hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
1da177e4
LT
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 }
ba3a3ce1 510 spin_unlock(&svc->sched_lock);
1da177e4
LT
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
1da177e4
LT
520static 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 */
45d4e71a 528 tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
0a9ee813 529 if (tbl == NULL)
1da177e4 530 return -ENOMEM;
0a9ee813 531
1da177e4
LT
532 svc->sched_data = tbl;
533 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
f728bafb 534 "current service\n", sizeof(*tbl));
1da177e4
LT
535
536 /*
537 * Initialize the hash buckets
538 */
539 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
c5549571 540 INIT_HLIST_HEAD(&tbl->bucket[i]);
1da177e4 541 }
1da177e4
LT
542 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
543 tbl->rover = 0;
544 tbl->counter = 1;
c5549571 545 tbl->dead = 0;
1da177e4
LT
546
547 /*
548 * Hook periodic timer for garbage collection
549 */
b24b8a24 550 setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
f728bafb
SW
551 (unsigned long)svc);
552 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
1da177e4 553
1da177e4
LT
554 return 0;
555}
556
557
ed3ffc4e 558static void ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
1da177e4
LT
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 */
c5549571 566 ip_vs_lblcr_flush(svc);
1da177e4
LT
567
568 /* release the table itself */
c5549571 569 kfree_rcu(tbl, rcu_head);
1da177e4 570 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
f728bafb 571 sizeof(*tbl));
1da177e4
LT
572}
573
574
1da177e4 575static inline struct ip_vs_dest *
44548375 576__ip_vs_lblcr_schedule(struct ip_vs_service *svc)
1da177e4
LT
577{
578 struct ip_vs_dest *dest, *least;
579 int loh, doh;
580
581 /*
b552f7e3 582 * We use the following formula to estimate the load:
1da177e4
LT
583 * (dest overhead) / dest->weight
584 *
585 * Remember -- no floats in kernel mode!!!
586 * The comparison of h1*w2 > h2*w1 is equivalent to that of
587 * h1/w1 > h2/w2
588 * if every weight is larger than zero.
589 *
590 * The server with weight=0 is quiesced and will not receive any
591 * new connection.
592 */
c5549571 593 list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
1da177e4
LT
594 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
595 continue;
596
597 if (atomic_read(&dest->weight) > 0) {
598 least = dest;
b552f7e3 599 loh = ip_vs_dest_conn_overhead(least);
1da177e4
LT
600 goto nextstage;
601 }
602 }
603 return NULL;
604
605 /*
606 * Find the destination with the least load.
607 */
608 nextstage:
c5549571 609 list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
1da177e4
LT
610 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
611 continue;
612
b552f7e3 613 doh = ip_vs_dest_conn_overhead(dest);
1da177e4
LT
614 if (loh * atomic_read(&dest->weight) >
615 doh * atomic_read(&least->weight)) {
616 least = dest;
617 loh = doh;
618 }
619 }
620
44548375
JV
621 IP_VS_DBG_BUF(6, "LBLCR: server %s:%d "
622 "activeconns %d refcnt %d weight %d overhead %d\n",
623 IP_VS_DBG_ADDR(least->af, &least->addr),
624 ntohs(least->port),
625 atomic_read(&least->activeconns),
626 atomic_read(&least->refcnt),
627 atomic_read(&least->weight), loh);
1da177e4
LT
628
629 return least;
630}
631
632
633/*
634 * If this destination server is overloaded and there is a less loaded
635 * server, then return true.
636 */
637static inline int
638is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
639{
640 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
641 struct ip_vs_dest *d;
642
c5549571 643 list_for_each_entry_rcu(d, &svc->destinations, n_list) {
1da177e4
LT
644 if (atomic_read(&d->activeconns)*2
645 < atomic_read(&d->weight)) {
646 return 1;
647 }
648 }
649 }
650 return 0;
651}
652
653
654/*
655 * Locality-Based (weighted) Least-Connection scheduling
656 */
657static struct ip_vs_dest *
658ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
659{
f728bafb 660 struct ip_vs_lblcr_table *tbl = svc->sched_data;
44548375 661 struct ip_vs_iphdr iph;
c5549571 662 struct ip_vs_dest *dest;
f728bafb 663 struct ip_vs_lblcr_entry *en;
1da177e4 664
63dca2c0 665 ip_vs_fill_iph_addr_only(svc->af, skb, &iph);
44548375 666
1e3e238e 667 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
1da177e4 668
f728bafb 669 /* First look in our cache */
44548375 670 en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr);
f728bafb 671 if (en) {
f728bafb
SW
672 en->lastuse = jiffies;
673
674 /* Get the least loaded destination */
1da177e4 675 dest = ip_vs_dest_set_min(&en->set);
f728bafb
SW
676
677 /* More than one destination + enough time passed by, cleanup */
1da177e4 678 if (atomic_read(&en->set.size) > 1 &&
c5549571 679 time_after(jiffies, en->set.lastmod +
b27d777e 680 sysctl_lblcr_expiration(svc))) {
ac69269a 681 spin_lock_bh(&svc->sched_lock);
c5549571
JA
682 if (atomic_read(&en->set.size) > 1) {
683 struct ip_vs_dest *m;
f728bafb 684
c5549571
JA
685 m = ip_vs_dest_set_max(&en->set);
686 if (m)
687 ip_vs_dest_set_erase(&en->set, m);
688 }
ac69269a 689 spin_unlock_bh(&svc->sched_lock);
f728bafb
SW
690 }
691
692 /* If the destination is not overloaded, use it */
c5549571 693 if (dest && !is_overloaded(dest, svc))
f728bafb 694 goto out;
f728bafb
SW
695
696 /* The cache entry is invalid, time to schedule */
44548375 697 dest = __ip_vs_lblcr_schedule(svc);
f728bafb 698 if (!dest) {
41ac51ee 699 ip_vs_scheduler_err(svc, "no destination available");
f728bafb 700 return NULL;
1da177e4 701 }
f728bafb
SW
702
703 /* Update our cache entry */
ac69269a 704 spin_lock_bh(&svc->sched_lock);
c5549571
JA
705 if (!tbl->dead)
706 ip_vs_dest_set_insert(&en->set, dest, true);
ac69269a 707 spin_unlock_bh(&svc->sched_lock);
f728bafb 708 goto out;
c5549571 709 }
f728bafb
SW
710
711 /* No cache entry, time to schedule */
44548375 712 dest = __ip_vs_lblcr_schedule(svc);
f728bafb
SW
713 if (!dest) {
714 IP_VS_DBG(1, "no destination available\n");
715 return NULL;
1da177e4 716 }
1da177e4 717
f728bafb 718 /* If we fail to create a cache entry, we'll just use the valid dest */
ac69269a 719 spin_lock_bh(&svc->sched_lock);
c5549571
JA
720 if (!tbl->dead)
721 ip_vs_lblcr_new(tbl, &iph.daddr, dest);
ac69269a 722 spin_unlock_bh(&svc->sched_lock);
f728bafb
SW
723
724out:
44548375
JV
725 IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n",
726 IP_VS_DBG_ADDR(svc->af, &iph.daddr),
727 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port));
1da177e4
LT
728
729 return dest;
730}
731
732
733/*
734 * IPVS LBLCR Scheduler structure
735 */
736static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
737{
738 .name = "lblcr",
739 .refcnt = ATOMIC_INIT(0),
740 .module = THIS_MODULE,
d149ccc9 741 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
1da177e4
LT
742 .init_service = ip_vs_lblcr_init_svc,
743 .done_service = ip_vs_lblcr_done_svc,
1da177e4
LT
744 .schedule = ip_vs_lblcr_schedule,
745};
746
61b1ab45
HS
747/*
748 * per netns init.
749 */
fb1de432 750#ifdef CONFIG_SYSCTL
61b1ab45
HS
751static int __net_init __ip_vs_lblcr_init(struct net *net)
752{
d0a1eef9
HS
753 struct netns_ipvs *ipvs = net_ipvs(net);
754
4b984cd5
HS
755 if (!ipvs)
756 return -ENOENT;
757
d0a1eef9
HS
758 if (!net_eq(net, &init_net)) {
759 ipvs->lblcr_ctl_table = kmemdup(vs_vars_table,
760 sizeof(vs_vars_table),
761 GFP_KERNEL);
762 if (ipvs->lblcr_ctl_table == NULL)
0443929f 763 return -ENOMEM;
464dc801
EB
764
765 /* Don't export sysctls to unprivileged users */
766 if (net->user_ns != &init_user_ns)
767 ipvs->lblcr_ctl_table[0].procname = NULL;
d0a1eef9
HS
768 } else
769 ipvs->lblcr_ctl_table = vs_vars_table;
b27d777e 770 ipvs->sysctl_lblcr_expiration = DEFAULT_EXPIRATION;
d0a1eef9
HS
771 ipvs->lblcr_ctl_table[0].data = &ipvs->sysctl_lblcr_expiration;
772
773 ipvs->lblcr_ctl_header =
ec8f23ce 774 register_net_sysctl(net, "net/ipv4/vs", ipvs->lblcr_ctl_table);
0443929f
SH
775 if (!ipvs->lblcr_ctl_header) {
776 if (!net_eq(net, &init_net))
777 kfree(ipvs->lblcr_ctl_table);
778 return -ENOMEM;
779 }
61b1ab45
HS
780
781 return 0;
782}
783
784static void __net_exit __ip_vs_lblcr_exit(struct net *net)
785{
d0a1eef9
HS
786 struct netns_ipvs *ipvs = net_ipvs(net);
787
788 unregister_net_sysctl_table(ipvs->lblcr_ctl_header);
61b1ab45 789
d0a1eef9
HS
790 if (!net_eq(net, &init_net))
791 kfree(ipvs->lblcr_ctl_table);
61b1ab45
HS
792}
793
fb1de432
SH
794#else
795
796static int __net_init __ip_vs_lblcr_init(struct net *net) { return 0; }
797static void __net_exit __ip_vs_lblcr_exit(struct net *net) { }
798
799#endif
800
61b1ab45
HS
801static struct pernet_operations ip_vs_lblcr_ops = {
802 .init = __ip_vs_lblcr_init,
803 .exit = __ip_vs_lblcr_exit,
804};
1da177e4
LT
805
806static int __init ip_vs_lblcr_init(void)
807{
a014bc8f
PE
808 int ret;
809
61b1ab45
HS
810 ret = register_pernet_subsys(&ip_vs_lblcr_ops);
811 if (ret)
812 return ret;
813
a014bc8f
PE
814 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
815 if (ret)
61b1ab45 816 unregister_pernet_subsys(&ip_vs_lblcr_ops);
a014bc8f 817 return ret;
1da177e4
LT
818}
819
1da177e4
LT
820static void __exit ip_vs_lblcr_cleanup(void)
821{
1da177e4 822 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
61b1ab45 823 unregister_pernet_subsys(&ip_vs_lblcr_ops);
ceec4c38 824 synchronize_rcu();
1da177e4
LT
825}
826
827
828module_init(ip_vs_lblcr_init);
829module_exit(ip_vs_lblcr_cleanup);
830MODULE_LICENSE("GPL");