blob: 5f5e5f4bad5e49f6805730e36e501d1bb6ed3631 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS: Locality-Based Least-Connection with Replication scheduler
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070040#define KMSG_COMPONENT "IPVS"
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020043#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
45#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020046#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050047#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* for sysctl */
50#include <linux/fs.h>
51#include <linux/sysctl.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020052#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <net/ip_vs.h>
55
56
57/*
58 * It is for garbage collection of stale IPVS lblcr 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_lblcr_expiration = 24*60*60*HZ;
72
73
74/*
75 * for IPVS lblcr entry hash table
76 */
77#ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
78#define CONFIG_IP_VS_LBLCR_TAB_BITS 10
79#endif
80#define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS
81#define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS)
82#define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1)
83
84
85/*
86 * IPVS destination set structure and operations
87 */
88struct ip_vs_dest_list {
89 struct ip_vs_dest_list *next; /* list link */
90 struct ip_vs_dest *dest; /* destination server */
91};
92
93struct ip_vs_dest_set {
94 atomic_t size; /* set size */
95 unsigned long lastmod; /* last modified time */
96 struct ip_vs_dest_list *list; /* destination list */
97 rwlock_t lock; /* lock for this list */
98};
99
100
101static struct ip_vs_dest_list *
102ip_vs_dest_set_insert(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
103{
104 struct ip_vs_dest_list *e;
105
106 for (e=set->list; e!=NULL; e=e->next) {
107 if (e->dest == dest)
108 /* already existed */
109 return NULL;
110 }
111
Sven Wegenerf728baf2008-08-19 08:16:19 +0200112 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (e == NULL) {
114 IP_VS_ERR("ip_vs_dest_set_insert(): no memory\n");
115 return NULL;
116 }
117
118 atomic_inc(&dest->refcnt);
119 e->dest = dest;
120
121 /* link it to the list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 e->next = set->list;
123 set->list = e;
124 atomic_inc(&set->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 set->lastmod = jiffies;
127 return e;
128}
129
130static void
131ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
132{
133 struct ip_vs_dest_list *e, **ep;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
136 if (e->dest == dest) {
137 /* HIT */
138 *ep = e->next;
139 atomic_dec(&set->size);
140 set->lastmod = jiffies;
141 atomic_dec(&e->dest->refcnt);
142 kfree(e);
143 break;
144 }
145 ep = &e->next;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
150{
151 struct ip_vs_dest_list *e, **ep;
152
153 write_lock(&set->lock);
154 for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
155 *ep = e->next;
156 /*
157 * We don't kfree dest because it is refered either
158 * by its service or by the trash dest list.
159 */
160 atomic_dec(&e->dest->refcnt);
161 kfree(e);
162 }
163 write_unlock(&set->lock);
164}
165
166/* get weighted least-connection node in the destination set */
167static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
168{
169 register struct ip_vs_dest_list *e;
170 struct ip_vs_dest *dest, *least;
171 int loh, doh;
172
173 if (set == NULL)
174 return NULL;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* select the first destination server, whose weight > 0 */
177 for (e=set->list; e!=NULL; e=e->next) {
178 least = e->dest;
179 if (least->flags & IP_VS_DEST_F_OVERLOAD)
180 continue;
181
182 if ((atomic_read(&least->weight) > 0)
183 && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
184 loh = atomic_read(&least->activeconns) * 50
185 + atomic_read(&least->inactconns);
186 goto nextstage;
187 }
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return NULL;
190
191 /* find the destination with the weighted least load */
192 nextstage:
193 for (e=e->next; e!=NULL; e=e->next) {
194 dest = e->dest;
195 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
196 continue;
197
198 doh = atomic_read(&dest->activeconns) * 50
199 + atomic_read(&dest->inactconns);
200 if ((loh * atomic_read(&dest->weight) >
201 doh * atomic_read(&least->weight))
202 && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
203 least = dest;
204 loh = doh;
205 }
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Julius Volz44548372008-11-03 17:08:28 -0800208 IP_VS_DBG_BUF(6, "ip_vs_dest_set_min: server %s:%d "
209 "activeconns %d refcnt %d weight %d overhead %d\n",
210 IP_VS_DBG_ADDR(least->af, &least->addr),
211 ntohs(least->port),
212 atomic_read(&least->activeconns),
213 atomic_read(&least->refcnt),
214 atomic_read(&least->weight), loh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return least;
216}
217
218
219/* get weighted most-connection node in the destination set */
220static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
221{
222 register struct ip_vs_dest_list *e;
223 struct ip_vs_dest *dest, *most;
224 int moh, doh;
225
226 if (set == NULL)
227 return NULL;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* select the first destination server, whose weight > 0 */
230 for (e=set->list; e!=NULL; e=e->next) {
231 most = e->dest;
232 if (atomic_read(&most->weight) > 0) {
233 moh = atomic_read(&most->activeconns) * 50
234 + atomic_read(&most->inactconns);
235 goto nextstage;
236 }
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return NULL;
239
240 /* find the destination with the weighted most load */
241 nextstage:
242 for (e=e->next; e!=NULL; e=e->next) {
243 dest = e->dest;
244 doh = atomic_read(&dest->activeconns) * 50
245 + atomic_read(&dest->inactconns);
246 /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
247 if ((moh * atomic_read(&dest->weight) <
248 doh * atomic_read(&most->weight))
249 && (atomic_read(&dest->weight) > 0)) {
250 most = dest;
251 moh = doh;
252 }
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Julius Volz44548372008-11-03 17:08:28 -0800255 IP_VS_DBG_BUF(6, "ip_vs_dest_set_max: server %s:%d "
256 "activeconns %d refcnt %d weight %d overhead %d\n",
257 IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port),
258 atomic_read(&most->activeconns),
259 atomic_read(&most->refcnt),
260 atomic_read(&most->weight), moh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return most;
262}
263
264
265/*
266 * IPVS lblcr entry represents an association between destination
267 * IP address and its destination server set
268 */
269struct ip_vs_lblcr_entry {
270 struct list_head list;
Julius Volz44548372008-11-03 17:08:28 -0800271 int af; /* address family */
272 union nf_inet_addr addr; /* destination IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct ip_vs_dest_set set; /* destination server set */
274 unsigned long lastuse; /* last used time */
275};
276
277
278/*
279 * IPVS lblcr hash table
280 */
281struct ip_vs_lblcr_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct list_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */
283 atomic_t entries; /* number of entries */
284 int max_size; /* maximum size of entries */
285 struct timer_list periodic_timer; /* collect stale entries */
286 int rover; /* rover for expire check */
287 int counter; /* counter for no expire */
288};
289
290
291/*
292 * IPVS LBLCR sysctl table
293 */
294
295static ctl_table vs_vars_table[] = {
296 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .procname = "lblcr_expiration",
298 .data = &sysctl_ip_vs_lblcr_expiration,
299 .maxlen = sizeof(int),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900300 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800301 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 },
303 { .ctl_name = 0 }
304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static struct ctl_table_header * sysctl_header;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
309{
310 list_del(&en->list);
311 ip_vs_dest_set_eraseall(&en->set);
312 kfree(en);
313}
314
315
316/*
317 * Returns hash value for IPVS LBLCR entry
318 */
Julius Volz44548372008-11-03 17:08:28 -0800319static inline unsigned
320ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Julius Volz44548372008-11-03 17:08:28 -0800322 __be32 addr_fold = addr->ip;
323
324#ifdef CONFIG_IP_VS_IPV6
325 if (af == AF_INET6)
326 addr_fold = addr->ip6[0]^addr->ip6[1]^
327 addr->ip6[2]^addr->ip6[3];
328#endif
329 return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332
333/*
334 * Hash an entry in the ip_vs_lblcr_table.
335 * returns bool success.
336 */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200337static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
339{
Julius Volz44548372008-11-03 17:08:28 -0800340 unsigned hash = ip_vs_lblcr_hashkey(en->af, &en->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 list_add(&en->list, &tbl->bucket[hash]);
343 atomic_inc(&tbl->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
Sven Wegenerf728baf2008-08-19 08:16:19 +0200348 * Get ip_vs_lblcr_entry associated with supplied parameters. Called under
349 * read lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351static inline struct ip_vs_lblcr_entry *
Julius Volz44548372008-11-03 17:08:28 -0800352ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl,
353 const union nf_inet_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Julius Volz44548372008-11-03 17:08:28 -0800355 unsigned hash = ip_vs_lblcr_hashkey(af, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct ip_vs_lblcr_entry *en;
357
Sven Wegenerf728baf2008-08-19 08:16:19 +0200358 list_for_each_entry(en, &tbl->bucket[hash], list)
Julius Volz44548372008-11-03 17:08:28 -0800359 if (ip_vs_addr_equal(af, &en->addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 return NULL;
363}
364
365
366/*
Sven Wegenerf728baf2008-08-19 08:16:19 +0200367 * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination
368 * IP address to a server. Called under write lock.
369 */
370static inline struct ip_vs_lblcr_entry *
Julius Volz44548372008-11-03 17:08:28 -0800371ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr,
Sven Wegenerf728baf2008-08-19 08:16:19 +0200372 struct ip_vs_dest *dest)
373{
374 struct ip_vs_lblcr_entry *en;
375
Julius Volz44548372008-11-03 17:08:28 -0800376 en = ip_vs_lblcr_get(dest->af, tbl, daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200377 if (!en) {
378 en = kmalloc(sizeof(*en), GFP_ATOMIC);
379 if (!en) {
380 IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
381 return NULL;
382 }
383
Julius Volz44548372008-11-03 17:08:28 -0800384 en->af = dest->af;
385 ip_vs_addr_copy(dest->af, &en->addr, daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200386 en->lastuse = jiffies;
387
388 /* initilize its dest set */
389 atomic_set(&(en->set.size), 0);
390 en->set.list = NULL;
391 rwlock_init(&en->set.lock);
392
393 ip_vs_lblcr_hash(tbl, en);
394 }
395
396 write_lock(&en->set.lock);
397 ip_vs_dest_set_insert(&en->set, dest);
398 write_unlock(&en->set.lock);
399
400 return en;
401}
402
403
404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * Flush all the entries of the specified table.
406 */
407static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
408{
409 int i;
410 struct ip_vs_lblcr_entry *en, *nxt;
411
Sven Wegenerf728baf2008-08-19 08:16:19 +0200412 /* No locking required, only called during cleanup. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
415 ip_vs_lblcr_free(en);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418}
419
420
Sven Wegenerf728baf2008-08-19 08:16:19 +0200421static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200423 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned long now = jiffies;
425 int i, j;
426 struct ip_vs_lblcr_entry *en, *nxt;
427
428 for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
429 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
430
Sven Wegenerf728baf2008-08-19 08:16:19 +0200431 write_lock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
433 if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration,
434 now))
435 continue;
436
437 ip_vs_lblcr_free(en);
438 atomic_dec(&tbl->entries);
439 }
Sven Wegenerf728baf2008-08-19 08:16:19 +0200440 write_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 tbl->rover = j;
443}
444
445
446/*
447 * Periodical timer handler for IPVS lblcr table
448 * It is used to collect stale entries when the number of entries
449 * exceeds the maximum size of the table.
450 *
451 * Fixme: we probably need more complicated algorithm to collect
452 * entries that have not been used for a long time even
453 * if the number of entries doesn't exceed the maximum size
454 * of the table.
455 * The full expiration check is for this purpose now.
456 */
457static void ip_vs_lblcr_check_expire(unsigned long data)
458{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200459 struct ip_vs_service *svc = (struct ip_vs_service *) data;
460 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unsigned long now = jiffies;
462 int goal;
463 int i, j;
464 struct ip_vs_lblcr_entry *en, *nxt;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
467 /* do full expiration check */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200468 ip_vs_lblcr_full_check(svc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tbl->counter = 1;
470 goto out;
471 }
472
473 if (atomic_read(&tbl->entries) <= tbl->max_size) {
474 tbl->counter++;
475 goto out;
476 }
477
478 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
479 if (goal > tbl->max_size/2)
480 goal = tbl->max_size/2;
481
482 for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
483 j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
484
Sven Wegenerf728baf2008-08-19 08:16:19 +0200485 write_lock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
487 if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
488 continue;
489
490 ip_vs_lblcr_free(en);
491 atomic_dec(&tbl->entries);
492 goal--;
493 }
Sven Wegenerf728baf2008-08-19 08:16:19 +0200494 write_unlock(&svc->sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (goal <= 0)
496 break;
497 }
498 tbl->rover = j;
499
500 out:
501 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
505{
506 int i;
507 struct ip_vs_lblcr_table *tbl;
508
509 /*
510 * Allocate the ip_vs_lblcr_table for this service
511 */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200512 tbl = kmalloc(sizeof(*tbl), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (tbl == NULL) {
514 IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
515 return -ENOMEM;
516 }
517 svc->sched_data = tbl;
518 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
Sven Wegenerf728baf2008-08-19 08:16:19 +0200519 "current service\n", sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /*
522 * Initialize the hash buckets
523 */
524 for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
525 INIT_LIST_HEAD(&tbl->bucket[i]);
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
528 tbl->rover = 0;
529 tbl->counter = 1;
530
531 /*
532 * Hook periodic timer for garbage collection
533 */
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800534 setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
Sven Wegenerf728baf2008-08-19 08:16:19 +0200535 (unsigned long)svc);
536 mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
540
541
542static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
543{
544 struct ip_vs_lblcr_table *tbl = svc->sched_data;
545
546 /* remove periodic timer */
547 del_timer_sync(&tbl->periodic_timer);
548
549 /* got to clean up table entries here */
550 ip_vs_lblcr_flush(tbl);
551
552 /* release the table itself */
Sven Wegenerf728baf2008-08-19 08:16:19 +0200553 kfree(tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
Sven Wegenerf728baf2008-08-19 08:16:19 +0200555 sizeof(*tbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 return 0;
558}
559
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static inline struct ip_vs_dest *
Julius Volz44548372008-11-03 17:08:28 -0800562__ip_vs_lblcr_schedule(struct ip_vs_service *svc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct ip_vs_dest *dest, *least;
565 int loh, doh;
566
567 /*
568 * We think the overhead of processing active connections is fifty
569 * times higher than that of inactive connections in average. (This
570 * fifty times might not be accurate, we will change it later.) We
571 * use the following formula to estimate the overhead:
572 * dest->activeconns*50 + dest->inactconns
573 * and the load:
574 * (dest overhead) / dest->weight
575 *
576 * Remember -- no floats in kernel mode!!!
577 * The comparison of h1*w2 > h2*w1 is equivalent to that of
578 * h1/w1 > h2/w2
579 * if every weight is larger than zero.
580 *
581 * The server with weight=0 is quiesced and will not receive any
582 * new connection.
583 */
584 list_for_each_entry(dest, &svc->destinations, n_list) {
585 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
586 continue;
587
588 if (atomic_read(&dest->weight) > 0) {
589 least = dest;
590 loh = atomic_read(&least->activeconns) * 50
591 + atomic_read(&least->inactconns);
592 goto nextstage;
593 }
594 }
595 return NULL;
596
597 /*
598 * Find the destination with the least load.
599 */
600 nextstage:
601 list_for_each_entry_continue(dest, &svc->destinations, n_list) {
602 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
603 continue;
604
605 doh = atomic_read(&dest->activeconns) * 50
606 + atomic_read(&dest->inactconns);
607 if (loh * atomic_read(&dest->weight) >
608 doh * atomic_read(&least->weight)) {
609 least = dest;
610 loh = doh;
611 }
612 }
613
Julius Volz44548372008-11-03 17:08:28 -0800614 IP_VS_DBG_BUF(6, "LBLCR: server %s:%d "
615 "activeconns %d refcnt %d weight %d overhead %d\n",
616 IP_VS_DBG_ADDR(least->af, &least->addr),
617 ntohs(least->port),
618 atomic_read(&least->activeconns),
619 atomic_read(&least->refcnt),
620 atomic_read(&least->weight), loh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 return least;
623}
624
625
626/*
627 * If this destination server is overloaded and there is a less loaded
628 * server, then return true.
629 */
630static inline int
631is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
632{
633 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
634 struct ip_vs_dest *d;
635
636 list_for_each_entry(d, &svc->destinations, n_list) {
637 if (atomic_read(&d->activeconns)*2
638 < atomic_read(&d->weight)) {
639 return 1;
640 }
641 }
642 }
643 return 0;
644}
645
646
647/*
648 * Locality-Based (weighted) Least-Connection scheduling
649 */
650static struct ip_vs_dest *
651ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
652{
Sven Wegenerf728baf2008-08-19 08:16:19 +0200653 struct ip_vs_lblcr_table *tbl = svc->sched_data;
Julius Volz44548372008-11-03 17:08:28 -0800654 struct ip_vs_iphdr iph;
Sven Wegenerf728baf2008-08-19 08:16:19 +0200655 struct ip_vs_dest *dest = NULL;
656 struct ip_vs_lblcr_entry *en;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Julius Volz44548372008-11-03 17:08:28 -0800658 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
661
Sven Wegenerf728baf2008-08-19 08:16:19 +0200662 /* First look in our cache */
663 read_lock(&svc->sched_lock);
Julius Volz44548372008-11-03 17:08:28 -0800664 en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200665 if (en) {
666 /* We only hold a read lock, but this is atomic */
667 en->lastuse = jiffies;
668
669 /* Get the least loaded destination */
670 read_lock(&en->set.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 dest = ip_vs_dest_set_min(&en->set);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200672 read_unlock(&en->set.lock);
673
674 /* More than one destination + enough time passed by, cleanup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (atomic_read(&en->set.size) > 1 &&
Sven Wegenerf728baf2008-08-19 08:16:19 +0200676 time_after(jiffies, en->set.lastmod +
677 sysctl_ip_vs_lblcr_expiration)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct ip_vs_dest *m;
Sven Wegenerf728baf2008-08-19 08:16:19 +0200679
680 write_lock(&en->set.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 m = ip_vs_dest_set_max(&en->set);
682 if (m)
683 ip_vs_dest_set_erase(&en->set, m);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200684 write_unlock(&en->set.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Sven Wegenerf728baf2008-08-19 08:16:19 +0200687 /* If the destination is not overloaded, use it */
688 if (dest && !is_overloaded(dest, svc)) {
689 read_unlock(&svc->sched_lock);
690 goto out;
691 }
692
693 /* The cache entry is invalid, time to schedule */
Julius Volz44548372008-11-03 17:08:28 -0800694 dest = __ip_vs_lblcr_schedule(svc);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200695 if (!dest) {
Simon Horman68888d12008-12-29 18:37:36 -0800696 IP_VS_ERR_RL("LBLCR: no destination available\n");
Sven Wegenerf728baf2008-08-19 08:16:19 +0200697 read_unlock(&svc->sched_lock);
698 return NULL;
699 }
700
701 /* Update our cache entry */
702 write_lock(&en->set.lock);
703 ip_vs_dest_set_insert(&en->set, dest);
704 write_unlock(&en->set.lock);
705 }
706 read_unlock(&svc->sched_lock);
707
708 if (dest)
709 goto out;
710
711 /* No cache entry, time to schedule */
Julius Volz44548372008-11-03 17:08:28 -0800712 dest = __ip_vs_lblcr_schedule(svc);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200713 if (!dest) {
714 IP_VS_DBG(1, "no destination available\n");
715 return NULL;
716 }
717
718 /* If we fail to create a cache entry, we'll just use the valid dest */
719 write_lock(&svc->sched_lock);
Julius Volz44548372008-11-03 17:08:28 -0800720 ip_vs_lblcr_new(tbl, &iph.daddr, dest);
Sven Wegenerf728baf2008-08-19 08:16:19 +0200721 write_unlock(&svc->sched_lock);
722
723out:
Julius Volz44548372008-11-03 17:08:28 -0800724 IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n",
725 IP_VS_DBG_ADDR(svc->af, &iph.daddr),
726 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 return dest;
729}
730
731
732/*
733 * IPVS LBLCR Scheduler structure
734 */
735static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
736{
737 .name = "lblcr",
738 .refcnt = ATOMIC_INIT(0),
739 .module = THIS_MODULE,
Sven Wegenerd149ccc2008-08-10 09:18:02 +0000740 .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .init_service = ip_vs_lblcr_init_svc,
742 .done_service = ip_vs_lblcr_done_svc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .schedule = ip_vs_lblcr_schedule,
744};
745
746
747static int __init ip_vs_lblcr_init(void)
748{
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800749 int ret;
750
Pavel Emelyanov90754f82008-01-12 02:33:50 -0800751 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table);
Pavel Emelyanova014bc82007-12-04 00:43:24 -0800752 ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
753 if (ret)
754 unregister_sysctl_table(sysctl_header);
755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758
759static void __exit ip_vs_lblcr_cleanup(void)
760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unregister_sysctl_table(sysctl_header);
762 unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
763}
764
765
766module_init(ip_vs_lblcr_init);
767module_exit(ip_vs_lblcr_cleanup);
768MODULE_LICENSE("GPL");