blob: ad89644ef5d27e797f9a188451dc4f5c315cce70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Locality-Based Least-Connection scheduling module
3 *
4 * Version: $Id: ip_vs_lblc.c,v 1.10 2002/09/15 08:14:08 wensong Exp $
5 *
6 * Authors: Wensong Zhang <wensong@gnuchina.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * Changes:
14 * Martin Hamilton : fixed the terrible locking bugs
15 * *lock(tbl->lock) ==> *lock(&tbl->lock)
16 * Wensong Zhang : fixed the uninitilized tbl->lock bug
17 * Wensong Zhang : added doing full expiration check to
18 * collect stale entries of 24+ hours when
19 * no partial expire check in a half hour
20 * Julian Anastasov : replaced del_timer call with del_timer_sync
21 * to avoid the possible race between timer
22 * handler and del_timer thread in SMP
23 *
24 */
25
26/*
27 * The lblc algorithm is as follows (pseudo code):
28 *
29 * if cachenode[dest_ip] is null then
30 * n, cachenode[dest_ip] <- {weighted least-conn node};
31 * else
32 * n <- cachenode[dest_ip];
33 * if (n is dead) OR
34 * (n.conns>n.weight AND
35 * there is a node m with m.conns<m.weight/2) then
36 * n, cachenode[dest_ip] <- {weighted least-conn node};
37 *
38 * return n;
39 *
40 * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing
41 * me to write this module.
42 */
43
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020044#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/module.h>
46#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020047#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050048#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* for sysctl */
51#include <linux/fs.h>
52#include <linux/sysctl.h>
53
54#include <net/ip_vs.h>
55
56
57/*
58 * It is for garbage collection of stale IPVS lblc entries,
59 * when the table is full.
60 */
61#define CHECK_EXPIRE_INTERVAL (60*HZ)
62#define ENTRY_TIMEOUT (6*60*HZ)
63
64/*
65 * It is for full expiration check.
66 * When there is no partial expiration check (garbage collection)
67 * in a half hour, do a full expiration check to collect stale
68 * entries that haven't been touched for a day.
69 */
70#define COUNT_FOR_FULL_EXPIRATION 30
71static int sysctl_ip_vs_lblc_expiration = 24*60*60*HZ;
72
73
74/*
75 * for IPVS lblc entry hash table
76 */
77#ifndef CONFIG_IP_VS_LBLC_TAB_BITS
78#define CONFIG_IP_VS_LBLC_TAB_BITS 10
79#endif
80#define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS
81#define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)
82#define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)
83
84
85/*
86 * IPVS lblc entry represents an association between destination
87 * IP address and its destination server
88 */
89struct ip_vs_lblc_entry {
90 struct list_head list;
Al Viro014d7302006-09-28 14:29:52 -070091 __be32 addr; /* destination IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct ip_vs_dest *dest; /* real server (cache) */
93 unsigned long lastuse; /* last used time */
94};
95
96
97/*
98 * IPVS lblc hash table
99 */
100struct ip_vs_lblc_table {
101 rwlock_t lock; /* lock for this table */
102 struct list_head bucket[IP_VS_LBLC_TAB_SIZE]; /* hash bucket */
103 atomic_t entries; /* number of entries */
104 int max_size; /* maximum size of entries */
105 struct timer_list periodic_timer; /* collect stale entries */
106 int rover; /* rover for expire check */
107 int counter; /* counter for no expire */
108};
109
110
111/*
112 * IPVS LBLC sysctl table
113 */
114
115static ctl_table vs_vars_table[] = {
116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "lblc_expiration",
118 .data = &sysctl_ip_vs_lblc_expiration,
119 .maxlen = sizeof(int),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900120 .mode = 0644,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .proc_handler = &proc_dointvec_jiffies,
122 },
123 { .ctl_name = 0 }
124};
125
126static ctl_table vs_table[] = {
127 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .procname = "vs",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900129 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .child = vs_vars_table
131 },
132 { .ctl_name = 0 }
133};
134
David S. Millerbf0ff9e2005-08-19 16:37:30 -0700135static ctl_table ipvs_ipv4_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 {
137 .ctl_name = NET_IPV4,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900138 .procname = "ipv4",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .mode = 0555,
140 .child = vs_table
141 },
142 { .ctl_name = 0 }
143};
144
145static ctl_table lblc_root_table[] = {
146 {
147 .ctl_name = CTL_NET,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900148 .procname = "net",
149 .mode = 0555,
David S. Millerbf0ff9e2005-08-19 16:37:30 -0700150 .child = ipvs_ipv4_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 },
152 { .ctl_name = 0 }
153};
154
155static struct ctl_table_header * sysctl_header;
156
157/*
158 * new/free a ip_vs_lblc_entry, which is a mapping of a destionation
159 * IP address to a server.
160 */
161static inline struct ip_vs_lblc_entry *
Al Viro014d7302006-09-28 14:29:52 -0700162ip_vs_lblc_new(__be32 daddr, struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct ip_vs_lblc_entry *en;
165
166 en = kmalloc(sizeof(struct ip_vs_lblc_entry), GFP_ATOMIC);
167 if (en == NULL) {
168 IP_VS_ERR("ip_vs_lblc_new(): no memory\n");
169 return NULL;
170 }
171
172 INIT_LIST_HEAD(&en->list);
173 en->addr = daddr;
174
175 atomic_inc(&dest->refcnt);
176 en->dest = dest;
177
178 return en;
179}
180
181
182static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en)
183{
184 list_del(&en->list);
185 /*
186 * We don't kfree dest because it is refered either by its service
187 * or the trash dest list.
188 */
189 atomic_dec(&en->dest->refcnt);
190 kfree(en);
191}
192
193
194/*
195 * Returns hash value for IPVS LBLC entry
196 */
Al Viro014d7302006-09-28 14:29:52 -0700197static inline unsigned ip_vs_lblc_hashkey(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 return (ntohl(addr)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
200}
201
202
203/*
204 * Hash an entry in the ip_vs_lblc_table.
205 * returns bool success.
206 */
207static int
208ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
209{
210 unsigned hash;
211
212 if (!list_empty(&en->list)) {
213 IP_VS_ERR("ip_vs_lblc_hash(): request for already hashed, "
214 "called from %p\n", __builtin_return_address(0));
215 return 0;
216 }
217
218 /*
219 * Hash by destination IP address
220 */
221 hash = ip_vs_lblc_hashkey(en->addr);
222
223 write_lock(&tbl->lock);
224 list_add(&en->list, &tbl->bucket[hash]);
225 atomic_inc(&tbl->entries);
226 write_unlock(&tbl->lock);
227
228 return 1;
229}
230
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * Get ip_vs_lblc_entry associated with supplied parameters.
234 */
235static inline struct ip_vs_lblc_entry *
Al Viro014d7302006-09-28 14:29:52 -0700236ip_vs_lblc_get(struct ip_vs_lblc_table *tbl, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned hash;
239 struct ip_vs_lblc_entry *en;
240
241 hash = ip_vs_lblc_hashkey(addr);
242
243 read_lock(&tbl->lock);
244
245 list_for_each_entry(en, &tbl->bucket[hash], list) {
246 if (en->addr == addr) {
247 /* HIT */
248 read_unlock(&tbl->lock);
249 return en;
250 }
251 }
252
253 read_unlock(&tbl->lock);
254
255 return NULL;
256}
257
258
259/*
260 * Flush all the entries of the specified table.
261 */
262static void ip_vs_lblc_flush(struct ip_vs_lblc_table *tbl)
263{
264 int i;
265 struct ip_vs_lblc_entry *en, *nxt;
266
267 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
268 write_lock(&tbl->lock);
269 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
270 ip_vs_lblc_free(en);
271 atomic_dec(&tbl->entries);
272 }
273 write_unlock(&tbl->lock);
274 }
275}
276
277
278static inline void ip_vs_lblc_full_check(struct ip_vs_lblc_table *tbl)
279{
280 unsigned long now = jiffies;
281 int i, j;
282 struct ip_vs_lblc_entry *en, *nxt;
283
284 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
285 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
286
287 write_lock(&tbl->lock);
288 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900289 if (time_before(now,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 en->lastuse + sysctl_ip_vs_lblc_expiration))
291 continue;
292
293 ip_vs_lblc_free(en);
294 atomic_dec(&tbl->entries);
295 }
296 write_unlock(&tbl->lock);
297 }
298 tbl->rover = j;
299}
300
301
302/*
303 * Periodical timer handler for IPVS lblc table
304 * It is used to collect stale entries when the number of entries
305 * exceeds the maximum size of the table.
306 *
307 * Fixme: we probably need more complicated algorithm to collect
308 * entries that have not been used for a long time even
309 * if the number of entries doesn't exceed the maximum size
310 * of the table.
311 * The full expiration check is for this purpose now.
312 */
313static void ip_vs_lblc_check_expire(unsigned long data)
314{
315 struct ip_vs_lblc_table *tbl;
316 unsigned long now = jiffies;
317 int goal;
318 int i, j;
319 struct ip_vs_lblc_entry *en, *nxt;
320
321 tbl = (struct ip_vs_lblc_table *)data;
322
323 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
324 /* do full expiration check */
325 ip_vs_lblc_full_check(tbl);
326 tbl->counter = 1;
327 goto out;
328 }
329
330 if (atomic_read(&tbl->entries) <= tbl->max_size) {
331 tbl->counter++;
332 goto out;
333 }
334
335 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
336 if (goal > tbl->max_size/2)
337 goal = tbl->max_size/2;
338
339 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
340 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
341
342 write_lock(&tbl->lock);
343 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
344 if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
345 continue;
346
347 ip_vs_lblc_free(en);
348 atomic_dec(&tbl->entries);
349 goal--;
350 }
351 write_unlock(&tbl->lock);
352 if (goal <= 0)
353 break;
354 }
355 tbl->rover = j;
356
357 out:
358 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
359}
360
361
362static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
363{
364 int i;
365 struct ip_vs_lblc_table *tbl;
366
367 /*
368 * Allocate the ip_vs_lblc_table for this service
369 */
370 tbl = kmalloc(sizeof(struct ip_vs_lblc_table), GFP_ATOMIC);
371 if (tbl == NULL) {
372 IP_VS_ERR("ip_vs_lblc_init_svc(): no memory\n");
373 return -ENOMEM;
374 }
375 svc->sched_data = tbl;
376 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
377 "current service\n",
378 sizeof(struct ip_vs_lblc_table));
379
380 /*
381 * Initialize the hash buckets
382 */
383 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
384 INIT_LIST_HEAD(&tbl->bucket[i]);
385 }
386 rwlock_init(&tbl->lock);
387 tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
388 tbl->rover = 0;
389 tbl->counter = 1;
390
391 /*
392 * Hook periodic timer for garbage collection
393 */
394 init_timer(&tbl->periodic_timer);
395 tbl->periodic_timer.data = (unsigned long)tbl;
396 tbl->periodic_timer.function = ip_vs_lblc_check_expire;
397 tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
398 add_timer(&tbl->periodic_timer);
399
400 return 0;
401}
402
403
404static int ip_vs_lblc_done_svc(struct ip_vs_service *svc)
405{
406 struct ip_vs_lblc_table *tbl = svc->sched_data;
407
408 /* remove periodic timer */
409 del_timer_sync(&tbl->periodic_timer);
410
411 /* got to clean up table entries here */
412 ip_vs_lblc_flush(tbl);
413
414 /* release the table itself */
415 kfree(svc->sched_data);
416 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
417 sizeof(struct ip_vs_lblc_table));
418
419 return 0;
420}
421
422
423static int ip_vs_lblc_update_svc(struct ip_vs_service *svc)
424{
425 return 0;
426}
427
428
429static inline struct ip_vs_dest *
430__ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
431{
432 struct ip_vs_dest *dest, *least;
433 int loh, doh;
434
435 /*
436 * We think the overhead of processing active connections is fifty
437 * times higher than that of inactive connections in average. (This
438 * fifty times might not be accurate, we will change it later.) We
439 * use the following formula to estimate the overhead:
440 * dest->activeconns*50 + dest->inactconns
441 * and the load:
442 * (dest overhead) / dest->weight
443 *
444 * Remember -- no floats in kernel mode!!!
445 * The comparison of h1*w2 > h2*w1 is equivalent to that of
446 * h1/w1 > h2/w2
447 * if every weight is larger than zero.
448 *
449 * The server with weight=0 is quiesced and will not receive any
450 * new connection.
451 */
452 list_for_each_entry(dest, &svc->destinations, n_list) {
453 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
454 continue;
455 if (atomic_read(&dest->weight) > 0) {
456 least = dest;
457 loh = atomic_read(&least->activeconns) * 50
458 + atomic_read(&least->inactconns);
459 goto nextstage;
460 }
461 }
462 return NULL;
463
464 /*
465 * Find the destination with the least load.
466 */
467 nextstage:
468 list_for_each_entry_continue(dest, &svc->destinations, n_list) {
469 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
470 continue;
471
472 doh = atomic_read(&dest->activeconns) * 50
473 + atomic_read(&dest->inactconns);
474 if (loh * atomic_read(&dest->weight) >
475 doh * atomic_read(&least->weight)) {
476 least = dest;
477 loh = doh;
478 }
479 }
480
481 IP_VS_DBG(6, "LBLC: server %d.%d.%d.%d:%d "
482 "activeconns %d refcnt %d weight %d overhead %d\n",
483 NIPQUAD(least->addr), ntohs(least->port),
484 atomic_read(&least->activeconns),
485 atomic_read(&least->refcnt),
486 atomic_read(&least->weight), loh);
487
488 return least;
489}
490
491
492/*
493 * If this destination server is overloaded and there is a less loaded
494 * server, then return true.
495 */
496static inline int
497is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
498{
499 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
500 struct ip_vs_dest *d;
501
502 list_for_each_entry(d, &svc->destinations, n_list) {
503 if (atomic_read(&d->activeconns)*2
504 < atomic_read(&d->weight)) {
505 return 1;
506 }
507 }
508 }
509 return 0;
510}
511
512
513/*
514 * Locality-Based (weighted) Least-Connection scheduling
515 */
516static struct ip_vs_dest *
517ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
518{
519 struct ip_vs_dest *dest;
520 struct ip_vs_lblc_table *tbl;
521 struct ip_vs_lblc_entry *en;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700522 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 IP_VS_DBG(6, "ip_vs_lblc_schedule(): Scheduling...\n");
525
526 tbl = (struct ip_vs_lblc_table *)svc->sched_data;
527 en = ip_vs_lblc_get(tbl, iph->daddr);
528 if (en == NULL) {
529 dest = __ip_vs_wlc_schedule(svc, iph);
530 if (dest == NULL) {
531 IP_VS_DBG(1, "no destination available\n");
532 return NULL;
533 }
534 en = ip_vs_lblc_new(iph->daddr, dest);
535 if (en == NULL) {
536 return NULL;
537 }
538 ip_vs_lblc_hash(tbl, en);
539 } else {
540 dest = en->dest;
541 if (!(dest->flags & IP_VS_DEST_F_AVAILABLE)
542 || atomic_read(&dest->weight) <= 0
543 || is_overloaded(dest, svc)) {
544 dest = __ip_vs_wlc_schedule(svc, iph);
545 if (dest == NULL) {
546 IP_VS_DBG(1, "no destination available\n");
547 return NULL;
548 }
549 atomic_dec(&en->dest->refcnt);
550 atomic_inc(&dest->refcnt);
551 en->dest = dest;
552 }
553 }
554 en->lastuse = jiffies;
555
556 IP_VS_DBG(6, "LBLC: destination IP address %u.%u.%u.%u "
557 "--> server %u.%u.%u.%u:%d\n",
558 NIPQUAD(en->addr),
559 NIPQUAD(dest->addr),
560 ntohs(dest->port));
561
562 return dest;
563}
564
565
566/*
567 * IPVS LBLC Scheduler structure
568 */
569static struct ip_vs_scheduler ip_vs_lblc_scheduler =
570{
571 .name = "lblc",
572 .refcnt = ATOMIC_INIT(0),
573 .module = THIS_MODULE,
574 .init_service = ip_vs_lblc_init_svc,
575 .done_service = ip_vs_lblc_done_svc,
576 .update_service = ip_vs_lblc_update_svc,
577 .schedule = ip_vs_lblc_schedule,
578};
579
580
581static int __init ip_vs_lblc_init(void)
582{
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800583 int ret;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 INIT_LIST_HEAD(&ip_vs_lblc_scheduler.n_list);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800586 sysctl_header = register_sysctl_table(lblc_root_table);
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800587 ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
588 if (ret)
589 unregister_sysctl_table(sysctl_header);
590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593
594static void __exit ip_vs_lblc_cleanup(void)
595{
596 unregister_sysctl_table(sysctl_header);
597 unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
598}
599
600
601module_init(ip_vs_lblc_init);
602module_exit(ip_vs_lblc_cleanup);
603MODULE_LICENSE("GPL");