1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <linux/ip.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/skbuff.h>
48#include <linux/jiffies.h>
49
50
51#include <linux/fs.h>
52#include <linux/sysctl.h>
53
54#include <net/ip_vs.h>
55
56
57
58
59
60
61#define CHECK_EXPIRE_INTERVAL (60*HZ)
62#define ENTRY_TIMEOUT (6*60*HZ)
63
64
65
66
67
68
69
70#define COUNT_FOR_FULL_EXPIRATION 30
71static int sysctl_ip_vs_lblc_expiration = 24*60*60*HZ;
72
73
74
75
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
87
88
89struct ip_vs_lblc_entry {
90 struct list_head list;
91 __be32 addr;
92 struct ip_vs_dest *dest;
93 unsigned long lastuse;
94};
95
96
97
98
99
100struct ip_vs_lblc_table {
101 rwlock_t lock;
102 struct list_head bucket[IP_VS_LBLC_TAB_SIZE];
103 atomic_t entries;
104 int max_size;
105 struct timer_list periodic_timer;
106 int rover;
107 int counter;
108};
109
110
111
112
113
114
115static ctl_table vs_vars_table[] = {
116 {
117 .procname = "lblc_expiration",
118 .data = &sysctl_ip_vs_lblc_expiration,
119 .maxlen = sizeof(int),
120 .mode = 0644,
121 .proc_handler = &proc_dointvec_jiffies,
122 },
123 { .ctl_name = 0 }
124};
125
126static struct ctl_table_header * sysctl_header;
127
128
129
130
131
132static inline struct ip_vs_lblc_entry *
133ip_vs_lblc_new(__be32 daddr, struct ip_vs_dest *dest)
134{
135 struct ip_vs_lblc_entry *en;
136
137 en = kmalloc(sizeof(struct ip_vs_lblc_entry), GFP_ATOMIC);
138 if (en == NULL) {
139 IP_VS_ERR("ip_vs_lblc_new(): no memory\n");
140 return NULL;
141 }
142
143 INIT_LIST_HEAD(&en->list);
144 en->addr = daddr;
145
146 atomic_inc(&dest->refcnt);
147 en->dest = dest;
148
149 return en;
150}
151
152
153static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en)
154{
155 list_del(&en->list);
156
157
158
159
160 atomic_dec(&en->dest->refcnt);
161 kfree(en);
162}
163
164
165
166
167
168static inline unsigned ip_vs_lblc_hashkey(__be32 addr)
169{
170 return (ntohl(addr)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
171}
172
173
174
175
176
177
178static int
179ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
180{
181 unsigned hash;
182
183 if (!list_empty(&en->list)) {
184 IP_VS_ERR("ip_vs_lblc_hash(): request for already hashed, "
185 "called from %p\n", __builtin_return_address(0));
186 return 0;
187 }
188
189
190
191
192 hash = ip_vs_lblc_hashkey(en->addr);
193
194 write_lock(&tbl->lock);
195 list_add(&en->list, &tbl->bucket[hash]);
196 atomic_inc(&tbl->entries);
197 write_unlock(&tbl->lock);
198
199 return 1;
200}
201
202
203
204
205
206static inline struct ip_vs_lblc_entry *
207ip_vs_lblc_get(struct ip_vs_lblc_table *tbl, __be32 addr)
208{
209 unsigned hash;
210 struct ip_vs_lblc_entry *en;
211
212 hash = ip_vs_lblc_hashkey(addr);
213
214 read_lock(&tbl->lock);
215
216 list_for_each_entry(en, &tbl->bucket[hash], list) {
217 if (en->addr == addr) {
218
219 read_unlock(&tbl->lock);
220 return en;
221 }
222 }
223
224 read_unlock(&tbl->lock);
225
226 return NULL;
227}
228
229
230
231
232
233static void ip_vs_lblc_flush(struct ip_vs_lblc_table *tbl)
234{
235 int i;
236 struct ip_vs_lblc_entry *en, *nxt;
237
238 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
239 write_lock(&tbl->lock);
240 list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
241 ip_vs_lblc_free(en);
242 atomic_dec(&tbl->entries);
243 }
244 write_unlock(&tbl->lock);
245 }
246}
247
248
249static inline void ip_vs_lblc_full_check(struct ip_vs_lblc_table *tbl)
250{
251 unsigned long now = jiffies;
252 int i, j;
253 struct ip_vs_lblc_entry *en, *nxt;
254
255 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
256 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
257
258 write_lock(&tbl->lock);
259 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
260 if (time_before(now,
261 en->lastuse + sysctl_ip_vs_lblc_expiration))
262 continue;
263
264 ip_vs_lblc_free(en);
265 atomic_dec(&tbl->entries);
266 }
267 write_unlock(&tbl->lock);
268 }
269 tbl->rover = j;
270}
271
272
273
274
275
276
277
278
279
280
281
282
283
284static void ip_vs_lblc_check_expire(unsigned long data)
285{
286 struct ip_vs_lblc_table *tbl;
287 unsigned long now = jiffies;
288 int goal;
289 int i, j;
290 struct ip_vs_lblc_entry *en, *nxt;
291
292 tbl = (struct ip_vs_lblc_table *)data;
293
294 if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
295
296 ip_vs_lblc_full_check(tbl);
297 tbl->counter = 1;
298 goto out;
299 }
300
301 if (atomic_read(&tbl->entries) <= tbl->max_size) {
302 tbl->counter++;
303 goto out;
304 }
305
306 goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
307 if (goal > tbl->max_size/2)
308 goal = tbl->max_size/2;
309
310 for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
311 j = (j + 1) & IP_VS_LBLC_TAB_MASK;
312
313 write_lock(&tbl->lock);
314 list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
315 if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
316 continue;
317
318 ip_vs_lblc_free(en);
319 atomic_dec(&tbl->entries);
320 goal--;
321 }
322 write_unlock(&tbl->lock);
323 if (goal <= 0)
324 break;
325 }
326 tbl->rover = j;
327
328 out:
329 mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
330}
331
332
333static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
334{
335 int i;
336 struct ip_vs_lblc_table *tbl;
337
338
339
340
341 tbl = kmalloc(sizeof(struct ip_vs_lblc_table), GFP_ATOMIC);
342 if (tbl == NULL) {
343 IP_VS_ERR("ip_vs_lblc_init_svc(): no memory\n");
344 return -ENOMEM;
345 }
346 svc->sched_data = tbl;
347 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
348 "current service\n",
349 sizeof(struct ip_vs_lblc_table));
350
351
352
353
354 for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
355 INIT_LIST_HEAD(&tbl->bucket[i]);
356 }
357 rwlock_init(&tbl->lock);
358 tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
359 tbl->rover = 0;
360 tbl->counter = 1;
361
362
363
364
365 setup_timer(&tbl->periodic_timer, ip_vs_lblc_check_expire,
366 (unsigned long)tbl);
367 tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
368 add_timer(&tbl->periodic_timer);
369
370 return 0;
371}
372
373
374static int ip_vs_lblc_done_svc(struct ip_vs_service *svc)
375{
376 struct ip_vs_lblc_table *tbl = svc->sched_data;
377
378
379 del_timer_sync(&tbl->periodic_timer);
380
381
382 ip_vs_lblc_flush(tbl);
383
384
385 kfree(svc->sched_data);
386 IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
387 sizeof(struct ip_vs_lblc_table));
388
389 return 0;
390}
391
392
393static int ip_vs_lblc_update_svc(struct ip_vs_service *svc)
394{
395 return 0;
396}
397
398
399static inline struct ip_vs_dest *
400__ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
401{
402 struct ip_vs_dest *dest, *least;
403 int loh, doh;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 list_for_each_entry(dest, &svc->destinations, n_list) {
423 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
424 continue;
425 if (atomic_read(&dest->weight) > 0) {
426 least = dest;
427 loh = atomic_read(&least->activeconns) * 50
428 + atomic_read(&least->inactconns);
429 goto nextstage;
430 }
431 }
432 return NULL;
433
434
435
436
437 nextstage:
438 list_for_each_entry_continue(dest, &svc->destinations, n_list) {
439 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
440 continue;
441
442 doh = atomic_read(&dest->activeconns) * 50
443 + atomic_read(&dest->inactconns);
444 if (loh * atomic_read(&dest->weight) >
445 doh * atomic_read(&least->weight)) {
446 least = dest;
447 loh = doh;
448 }
449 }
450
451 IP_VS_DBG(6, "LBLC: server %d.%d.%d.%d:%d "
452 "activeconns %d refcnt %d weight %d overhead %d\n",
453 NIPQUAD(least->addr), ntohs(least->port),
454 atomic_read(&least->activeconns),
455 atomic_read(&least->refcnt),
456 atomic_read(&least->weight), loh);
457
458 return least;
459}
460
461
462
463
464
465
466static inline int
467is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
468{
469 if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
470 struct ip_vs_dest *d;
471
472 list_for_each_entry(d, &svc->destinations, n_list) {
473 if (atomic_read(&d->activeconns)*2
474 < atomic_read(&d->weight)) {
475 return 1;
476 }
477 }
478 }
479 return 0;
480}
481
482
483
484
485
486static struct ip_vs_dest *
487ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
488{
489 struct ip_vs_dest *dest;
490 struct ip_vs_lblc_table *tbl;
491 struct ip_vs_lblc_entry *en;
492 struct iphdr *iph = ip_hdr(skb);
493
494 IP_VS_DBG(6, "ip_vs_lblc_schedule(): Scheduling...\n");
495
496 tbl = (struct ip_vs_lblc_table *)svc->sched_data;
497 en = ip_vs_lblc_get(tbl, iph->daddr);
498 if (en == NULL) {
499 dest = __ip_vs_wlc_schedule(svc, iph);
500 if (dest == NULL) {
501 IP_VS_DBG(1, "no destination available\n");
502 return NULL;
503 }
504 en = ip_vs_lblc_new(iph->daddr, dest);
505 if (en == NULL) {
506 return NULL;
507 }
508 ip_vs_lblc_hash(tbl, en);
509 } else {
510 dest = en->dest;
511 if (!(dest->flags & IP_VS_DEST_F_AVAILABLE)
512 || atomic_read(&dest->weight) <= 0
513 || is_overloaded(dest, svc)) {
514 dest = __ip_vs_wlc_schedule(svc, iph);
515 if (dest == NULL) {
516 IP_VS_DBG(1, "no destination available\n");
517 return NULL;
518 }
519 atomic_dec(&en->dest->refcnt);
520 atomic_inc(&dest->refcnt);
521 en->dest = dest;
522 }
523 }
524 en->lastuse = jiffies;
525
526 IP_VS_DBG(6, "LBLC: destination IP address %u.%u.%u.%u "
527 "--> server %u.%u.%u.%u:%d\n",
528 NIPQUAD(en->addr),
529 NIPQUAD(dest->addr),
530 ntohs(dest->port));
531
532 return dest;
533}
534
535
536
537
538
539static struct ip_vs_scheduler ip_vs_lblc_scheduler =
540{
541 .name = "lblc",
542 .refcnt = ATOMIC_INIT(0),
543 .module = THIS_MODULE,
544 .init_service = ip_vs_lblc_init_svc,
545 .done_service = ip_vs_lblc_done_svc,
546 .update_service = ip_vs_lblc_update_svc,
547 .schedule = ip_vs_lblc_schedule,
548};
549
550
551static int __init ip_vs_lblc_init(void)
552{
553 int ret;
554
555 INIT_LIST_HEAD(&ip_vs_lblc_scheduler.n_list);
556 sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table);
557 ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
558 if (ret)
559 unregister_sysctl_table(sysctl_header);
560 return ret;
561}
562
563
564static void __exit ip_vs_lblc_cleanup(void)
565{
566 unregister_sysctl_table(sysctl_header);
567 unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
568}
569
570
571module_init(ip_vs_lblc_init);
572module_exit(ip_vs_lblc_cleanup);
573MODULE_LICENSE("GPL");
574