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