blob: 45a642edb93bed75df197913989b6ed9b7a61a92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the Netfilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
8 * Version: $Id: ip_vs_conn.c,v 1.31 2003/04/18 09:03:16 wensong Exp $
9 *
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 * Peter Kese <peter.kese@ijs.si>
12 * Julian Anastasov <ja@ssi.bg>
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
20 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
21 * and others. Many code here is taken from IP MASQ code of kernel 2.2.
22 *
23 * Changes:
24 *
25 */
26
Andrew Mortone9242832006-01-05 14:57:36 -080027#include <linux/interrupt.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <linux/in.h>
Arnaldo Carvalho de Melof1900552006-01-04 02:02:20 -020029#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/vmalloc.h>
33#include <linux/proc_fs.h> /* for proc_net_* */
34#include <linux/seq_file.h>
35#include <linux/jhash.h>
36#include <linux/random.h>
37
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020038#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/ip_vs.h>
40
41
42/*
43 * Connection hash table: for input and output packets lookups of IPVS
44 */
45static struct list_head *ip_vs_conn_tab;
46
47/* SLAB cache for IPVS connections */
Christoph Lametere18b8902006-12-06 20:33:20 -080048static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* counter for current IPVS connections */
51static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
52
53/* counter for no client port connections */
54static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
55
56/* random value for IPVS connection hash */
57static unsigned int ip_vs_conn_rnd;
58
59/*
60 * Fine locking granularity for big connection hash table
61 */
62#define CT_LOCKARRAY_BITS 4
63#define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
64#define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
65
66struct ip_vs_aligned_lock
67{
68 rwlock_t l;
69} __attribute__((__aligned__(SMP_CACHE_BYTES)));
70
71/* lock array for conn table */
72static struct ip_vs_aligned_lock
73__ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
74
75static inline void ct_read_lock(unsigned key)
76{
77 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
78}
79
80static inline void ct_read_unlock(unsigned key)
81{
82 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
83}
84
85static inline void ct_write_lock(unsigned key)
86{
87 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
88}
89
90static inline void ct_write_unlock(unsigned key)
91{
92 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
93}
94
95static inline void ct_read_lock_bh(unsigned key)
96{
97 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
98}
99
100static inline void ct_read_unlock_bh(unsigned key)
101{
102 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
103}
104
105static inline void ct_write_lock_bh(unsigned key)
106{
107 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
108}
109
110static inline void ct_write_unlock_bh(unsigned key)
111{
112 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
113}
114
115
116/*
117 * Returns hash value for IPVS connection entry
118 */
Al Viro014d7302006-09-28 14:29:52 -0700119static unsigned int ip_vs_conn_hashkey(unsigned proto, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Al Viro014d7302006-09-28 14:29:52 -0700121 return jhash_3words((__force u32)addr, (__force u32)port, proto, ip_vs_conn_rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 & IP_VS_CONN_TAB_MASK;
123}
124
125
126/*
127 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
128 * returns bool success.
129 */
130static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
131{
132 unsigned hash;
133 int ret;
134
135 /* Hash by protocol, client address and port */
136 hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
137
138 ct_write_lock(hash);
139
140 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
141 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
142 cp->flags |= IP_VS_CONN_F_HASHED;
143 atomic_inc(&cp->refcnt);
144 ret = 1;
145 } else {
146 IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, "
147 "called from %p\n", __builtin_return_address(0));
148 ret = 0;
149 }
150
151 ct_write_unlock(hash);
152
153 return ret;
154}
155
156
157/*
158 * UNhashes ip_vs_conn from ip_vs_conn_tab.
159 * returns bool success.
160 */
161static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
162{
163 unsigned hash;
164 int ret;
165
166 /* unhash it and decrease its reference counter */
167 hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
168
169 ct_write_lock(hash);
170
171 if (cp->flags & IP_VS_CONN_F_HASHED) {
172 list_del(&cp->c_list);
173 cp->flags &= ~IP_VS_CONN_F_HASHED;
174 atomic_dec(&cp->refcnt);
175 ret = 1;
176 } else
177 ret = 0;
178
179 ct_write_unlock(hash);
180
181 return ret;
182}
183
184
185/*
186 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
187 * Called for pkts coming from OUTside-to-INside.
188 * s_addr, s_port: pkt source address (foreign host)
189 * d_addr, d_port: pkt dest address (load balancer)
190 */
191static inline struct ip_vs_conn *__ip_vs_conn_in_get
Al Viro014d7302006-09-28 14:29:52 -0700192(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned hash;
195 struct ip_vs_conn *cp;
196
197 hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
198
199 ct_read_lock(hash);
200
201 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
202 if (s_addr==cp->caddr && s_port==cp->cport &&
203 d_port==cp->vport && d_addr==cp->vaddr &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700204 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 protocol==cp->protocol) {
206 /* HIT */
207 atomic_inc(&cp->refcnt);
208 ct_read_unlock(hash);
209 return cp;
210 }
211 }
212
213 ct_read_unlock(hash);
214
215 return NULL;
216}
217
218struct ip_vs_conn *ip_vs_conn_in_get
Al Viro014d7302006-09-28 14:29:52 -0700219(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct ip_vs_conn *cp;
222
223 cp = __ip_vs_conn_in_get(protocol, s_addr, s_port, d_addr, d_port);
224 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
225 cp = __ip_vs_conn_in_get(protocol, s_addr, 0, d_addr, d_port);
226
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800227 IP_VS_DBG(9, "lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ip_vs_proto_name(protocol),
229 NIPQUAD(s_addr), ntohs(s_port),
230 NIPQUAD(d_addr), ntohs(d_port),
231 cp?"hit":"not hit");
232
233 return cp;
234}
235
Julian Anastasov87375ab2005-09-14 21:08:51 -0700236/* Get reference to connection template */
237struct ip_vs_conn *ip_vs_ct_in_get
Al Viro014d7302006-09-28 14:29:52 -0700238(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700239{
240 unsigned hash;
241 struct ip_vs_conn *cp;
242
243 hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
244
245 ct_read_lock(hash);
246
247 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
248 if (s_addr==cp->caddr && s_port==cp->cport &&
249 d_port==cp->vport && d_addr==cp->vaddr &&
250 cp->flags & IP_VS_CONN_F_TEMPLATE &&
251 protocol==cp->protocol) {
252 /* HIT */
253 atomic_inc(&cp->refcnt);
254 goto out;
255 }
256 }
257 cp = NULL;
258
259 out:
260 ct_read_unlock(hash);
261
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800262 IP_VS_DBG(9, "template lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
Julian Anastasov87375ab2005-09-14 21:08:51 -0700263 ip_vs_proto_name(protocol),
264 NIPQUAD(s_addr), ntohs(s_port),
265 NIPQUAD(d_addr), ntohs(d_port),
266 cp?"hit":"not hit");
267
268 return cp;
269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/*
272 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
273 * Called for pkts coming from inside-to-OUTside.
274 * s_addr, s_port: pkt source address (inside host)
275 * d_addr, d_port: pkt dest address (foreign host)
276 */
277struct ip_vs_conn *ip_vs_conn_out_get
Al Viro014d7302006-09-28 14:29:52 -0700278(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned hash;
281 struct ip_vs_conn *cp, *ret=NULL;
282
283 /*
284 * Check for "full" addressed entries
285 */
286 hash = ip_vs_conn_hashkey(protocol, d_addr, d_port);
287
288 ct_read_lock(hash);
289
290 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
291 if (d_addr == cp->caddr && d_port == cp->cport &&
292 s_port == cp->dport && s_addr == cp->daddr &&
293 protocol == cp->protocol) {
294 /* HIT */
295 atomic_inc(&cp->refcnt);
296 ret = cp;
297 break;
298 }
299 }
300
301 ct_read_unlock(hash);
302
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800303 IP_VS_DBG(9, "lookup/out %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ip_vs_proto_name(protocol),
305 NIPQUAD(s_addr), ntohs(s_port),
306 NIPQUAD(d_addr), ntohs(d_port),
307 ret?"hit":"not hit");
308
309 return ret;
310}
311
312
313/*
314 * Put back the conn and restart its timer with its timeout
315 */
316void ip_vs_conn_put(struct ip_vs_conn *cp)
317{
318 /* reset it expire in its timeout */
319 mod_timer(&cp->timer, jiffies+cp->timeout);
320
321 __ip_vs_conn_put(cp);
322}
323
324
325/*
326 * Fill a no_client_port connection with a client port number
327 */
Al Viro014d7302006-09-28 14:29:52 -0700328void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 if (ip_vs_conn_unhash(cp)) {
331 spin_lock(&cp->lock);
332 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
333 atomic_dec(&ip_vs_conn_no_cport_cnt);
334 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
335 cp->cport = cport;
336 }
337 spin_unlock(&cp->lock);
338
339 /* hash on new dport */
340 ip_vs_conn_hash(cp);
341 }
342}
343
344
345/*
346 * Bind a connection entry with the corresponding packet_xmit.
347 * Called by ip_vs_conn_new.
348 */
349static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
350{
351 switch (IP_VS_FWD_METHOD(cp)) {
352 case IP_VS_CONN_F_MASQ:
353 cp->packet_xmit = ip_vs_nat_xmit;
354 break;
355
356 case IP_VS_CONN_F_TUNNEL:
357 cp->packet_xmit = ip_vs_tunnel_xmit;
358 break;
359
360 case IP_VS_CONN_F_DROUTE:
361 cp->packet_xmit = ip_vs_dr_xmit;
362 break;
363
364 case IP_VS_CONN_F_LOCALNODE:
365 cp->packet_xmit = ip_vs_null_xmit;
366 break;
367
368 case IP_VS_CONN_F_BYPASS:
369 cp->packet_xmit = ip_vs_bypass_xmit;
370 break;
371 }
372}
373
374
375static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
376{
377 return atomic_read(&dest->activeconns)
378 + atomic_read(&dest->inactconns);
379}
380
381/*
382 * Bind a connection entry with a virtual service destination
383 * Called just after a new connection entry is created.
384 */
385static inline void
386ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
387{
388 /* if dest is NULL, then return directly */
389 if (!dest)
390 return;
391
392 /* Increase the refcnt counter of the dest */
393 atomic_inc(&dest->refcnt);
394
395 /* Bind with the destination and its corresponding transmitter */
396 cp->flags |= atomic_read(&dest->conn_flags);
397 cp->dest = dest;
398
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800399 IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
400 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
401 "dest->refcnt:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ip_vs_proto_name(cp->protocol),
403 NIPQUAD(cp->caddr), ntohs(cp->cport),
404 NIPQUAD(cp->vaddr), ntohs(cp->vport),
405 NIPQUAD(cp->daddr), ntohs(cp->dport),
406 ip_vs_fwd_tag(cp), cp->state,
407 cp->flags, atomic_read(&cp->refcnt),
408 atomic_read(&dest->refcnt));
409
410 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700411 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* It is a normal connection, so increase the inactive
413 connection counter because it is in TCP SYNRECV
414 state (inactive) or other protocol inacive state */
415 atomic_inc(&dest->inactconns);
416 } else {
417 /* It is a persistent connection/template, so increase
418 the peristent connection counter */
419 atomic_inc(&dest->persistconns);
420 }
421
422 if (dest->u_threshold != 0 &&
423 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
424 dest->flags |= IP_VS_DEST_F_OVERLOAD;
425}
426
427
428/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800429 * Check if there is a destination for the connection, if so
430 * bind the connection to the destination.
431 */
432struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
433{
434 struct ip_vs_dest *dest;
435
436 if ((cp) && (!cp->dest)) {
437 dest = ip_vs_find_dest(cp->daddr, cp->dport,
438 cp->vaddr, cp->vport, cp->protocol);
439 ip_vs_bind_dest(cp, dest);
440 return dest;
441 } else
442 return NULL;
443}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800444
445
446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * Unbind a connection entry with its VS destination
448 * Called by the ip_vs_conn_expire function.
449 */
450static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
451{
452 struct ip_vs_dest *dest = cp->dest;
453
454 if (!dest)
455 return;
456
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800457 IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
458 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
459 "dest->refcnt:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ip_vs_proto_name(cp->protocol),
461 NIPQUAD(cp->caddr), ntohs(cp->cport),
462 NIPQUAD(cp->vaddr), ntohs(cp->vport),
463 NIPQUAD(cp->daddr), ntohs(cp->dport),
464 ip_vs_fwd_tag(cp), cp->state,
465 cp->flags, atomic_read(&cp->refcnt),
466 atomic_read(&dest->refcnt));
467
468 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700469 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* It is a normal connection, so decrease the inactconns
471 or activeconns counter */
472 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
473 atomic_dec(&dest->inactconns);
474 } else {
475 atomic_dec(&dest->activeconns);
476 }
477 } else {
478 /* It is a persistent connection/template, so decrease
479 the peristent connection counter */
480 atomic_dec(&dest->persistconns);
481 }
482
483 if (dest->l_threshold != 0) {
484 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
485 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
486 } else if (dest->u_threshold != 0) {
487 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
488 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
489 } else {
490 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
491 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
492 }
493
494 /*
495 * Simply decrease the refcnt of the dest, because the
496 * dest will be either in service's destination list
497 * or in the trash.
498 */
499 atomic_dec(&dest->refcnt);
500}
501
502
503/*
504 * Checking if the destination of a connection template is available.
505 * If available, return 1, otherwise invalidate this connection
506 * template and return 0.
507 */
508int ip_vs_check_template(struct ip_vs_conn *ct)
509{
510 struct ip_vs_dest *dest = ct->dest;
511
512 /*
513 * Checking the dest server status.
514 */
515 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900516 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
517 (sysctl_ip_vs_expire_quiescent_template &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 (atomic_read(&dest->weight) == 0))) {
519 IP_VS_DBG(9, "check_template: dest not available for "
520 "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
521 "-> d:%u.%u.%u.%u:%d\n",
522 ip_vs_proto_name(ct->protocol),
523 NIPQUAD(ct->caddr), ntohs(ct->cport),
524 NIPQUAD(ct->vaddr), ntohs(ct->vport),
525 NIPQUAD(ct->daddr), ntohs(ct->dport));
526
527 /*
528 * Invalidate the connection template
529 */
Al Viro014d7302006-09-28 14:29:52 -0700530 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700532 ct->dport = htons(0xffff);
533 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ct->cport = 0;
535 ip_vs_conn_hash(ct);
536 }
537 }
538
539 /*
540 * Simply decrease the refcnt of the template,
541 * don't restart its timer.
542 */
543 atomic_dec(&ct->refcnt);
544 return 0;
545 }
546 return 1;
547}
548
549static void ip_vs_conn_expire(unsigned long data)
550{
551 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
552
553 cp->timeout = 60*HZ;
554
555 /*
556 * hey, I'm using it
557 */
558 atomic_inc(&cp->refcnt);
559
560 /*
561 * do I control anybody?
562 */
563 if (atomic_read(&cp->n_control))
564 goto expire_later;
565
566 /*
567 * unhash it if it is hashed in the conn table
568 */
569 if (!ip_vs_conn_unhash(cp))
570 goto expire_later;
571
572 /*
573 * refcnt==1 implies I'm the only one referrer
574 */
575 if (likely(atomic_read(&cp->refcnt) == 1)) {
576 /* delete the timer if it is activated by other users */
577 if (timer_pending(&cp->timer))
578 del_timer(&cp->timer);
579
580 /* does anybody control me? */
581 if (cp->control)
582 ip_vs_control_del(cp);
583
584 if (unlikely(cp->app != NULL))
585 ip_vs_unbind_app(cp);
586 ip_vs_unbind_dest(cp);
587 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
588 atomic_dec(&ip_vs_conn_no_cport_cnt);
589 atomic_dec(&ip_vs_conn_count);
590
591 kmem_cache_free(ip_vs_conn_cachep, cp);
592 return;
593 }
594
595 /* hash it back to the table */
596 ip_vs_conn_hash(cp);
597
598 expire_later:
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800599 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 atomic_read(&cp->refcnt)-1,
601 atomic_read(&cp->n_control));
602
603 ip_vs_conn_put(cp);
604}
605
606
607void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
608{
609 if (del_timer(&cp->timer))
610 mod_timer(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613
614/*
615 * Create a new connection entry and hash it into the ip_vs_conn_tab
616 */
617struct ip_vs_conn *
Al Viro014d7302006-09-28 14:29:52 -0700618ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
619 __be32 daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct ip_vs_dest *dest)
621{
622 struct ip_vs_conn *cp;
623 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
624
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800625 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (cp == NULL) {
627 IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
628 return NULL;
629 }
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 INIT_LIST_HEAD(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800632 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 cp->protocol = proto;
634 cp->caddr = caddr;
635 cp->cport = cport;
636 cp->vaddr = vaddr;
637 cp->vport = vport;
638 cp->daddr = daddr;
639 cp->dport = dport;
640 cp->flags = flags;
641 spin_lock_init(&cp->lock);
642
643 /*
644 * Set the entry is referenced by the current thread before hashing
645 * it in the table, so that other thread run ip_vs_random_dropentry
646 * but cannot drop this entry.
647 */
648 atomic_set(&cp->refcnt, 1);
649
650 atomic_set(&cp->n_control, 0);
651 atomic_set(&cp->in_pkts, 0);
652
653 atomic_inc(&ip_vs_conn_count);
654 if (flags & IP_VS_CONN_F_NO_CPORT)
655 atomic_inc(&ip_vs_conn_no_cport_cnt);
656
657 /* Bind the connection with a destination server */
658 ip_vs_bind_dest(cp, dest);
659
660 /* Set its state and timeout */
661 cp->state = 0;
662 cp->timeout = 3*HZ;
663
664 /* Bind its packet transmitter */
665 ip_vs_bind_xmit(cp);
666
667 if (unlikely(pp && atomic_read(&pp->appcnt)))
668 ip_vs_bind_app(cp, pp);
669
670 /* Hash it in the ip_vs_conn_tab finally */
671 ip_vs_conn_hash(cp);
672
673 return cp;
674}
675
676
677/*
678 * /proc/net/ip_vs_conn entries
679 */
680#ifdef CONFIG_PROC_FS
681
682static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
683{
684 int idx;
685 struct ip_vs_conn *cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
688 ct_read_lock_bh(idx);
689 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
690 if (pos-- == 0) {
691 seq->private = &ip_vs_conn_tab[idx];
692 return cp;
693 }
694 }
695 ct_read_unlock_bh(idx);
696 }
697
698 return NULL;
699}
700
701static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
702{
703 seq->private = NULL;
704 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
705}
706
707static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
708{
709 struct ip_vs_conn *cp = v;
710 struct list_head *e, *l = seq->private;
711 int idx;
712
713 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900714 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ip_vs_conn_array(seq, 0);
716
717 /* more on same hash chain? */
718 if ((e = cp->c_list.next) != l)
719 return list_entry(e, struct ip_vs_conn, c_list);
720
721 idx = l - ip_vs_conn_tab;
722 ct_read_unlock_bh(idx);
723
724 while (++idx < IP_VS_CONN_TAB_SIZE) {
725 ct_read_lock_bh(idx);
726 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
727 seq->private = &ip_vs_conn_tab[idx];
728 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ct_read_unlock_bh(idx);
731 }
732 seq->private = NULL;
733 return NULL;
734}
735
736static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
737{
738 struct list_head *l = seq->private;
739
740 if (l)
741 ct_read_unlock_bh(l - ip_vs_conn_tab);
742}
743
744static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
745{
746
747 if (v == SEQ_START_TOKEN)
748 seq_puts(seq,
749 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
750 else {
751 const struct ip_vs_conn *cp = v;
752
753 seq_printf(seq,
754 "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
755 ip_vs_proto_name(cp->protocol),
756 ntohl(cp->caddr), ntohs(cp->cport),
757 ntohl(cp->vaddr), ntohs(cp->vport),
758 ntohl(cp->daddr), ntohs(cp->dport),
759 ip_vs_state_name(cp->protocol, cp->state),
760 (cp->timer.expires-jiffies)/HZ);
761 }
762 return 0;
763}
764
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700765static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .start = ip_vs_conn_seq_start,
767 .next = ip_vs_conn_seq_next,
768 .stop = ip_vs_conn_seq_stop,
769 .show = ip_vs_conn_seq_show,
770};
771
772static int ip_vs_conn_open(struct inode *inode, struct file *file)
773{
774 return seq_open(file, &ip_vs_conn_seq_ops);
775}
776
Arjan van de Ven9a321442007-02-12 00:55:35 -0800777static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .owner = THIS_MODULE,
779 .open = ip_vs_conn_open,
780 .read = seq_read,
781 .llseek = seq_lseek,
782 .release = seq_release,
783};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800784
785static const char *ip_vs_origin_name(unsigned flags)
786{
787 if (flags & IP_VS_CONN_F_SYNC)
788 return "SYNC";
789 else
790 return "LOCAL";
791}
792
793static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
794{
795
796 if (v == SEQ_START_TOKEN)
797 seq_puts(seq,
798 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
799 else {
800 const struct ip_vs_conn *cp = v;
801
802 seq_printf(seq,
803 "%-3s %08X %04X %08X %04X %08X %04X %-11s %-6s %7lu\n",
804 ip_vs_proto_name(cp->protocol),
805 ntohl(cp->caddr), ntohs(cp->cport),
806 ntohl(cp->vaddr), ntohs(cp->vport),
807 ntohl(cp->daddr), ntohs(cp->dport),
808 ip_vs_state_name(cp->protocol, cp->state),
809 ip_vs_origin_name(cp->flags),
810 (cp->timer.expires-jiffies)/HZ);
811 }
812 return 0;
813}
814
815static const struct seq_operations ip_vs_conn_sync_seq_ops = {
816 .start = ip_vs_conn_seq_start,
817 .next = ip_vs_conn_seq_next,
818 .stop = ip_vs_conn_seq_stop,
819 .show = ip_vs_conn_sync_seq_show,
820};
821
822static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
823{
824 return seq_open(file, &ip_vs_conn_sync_seq_ops);
825}
826
827static const struct file_operations ip_vs_conn_sync_fops = {
828 .owner = THIS_MODULE,
829 .open = ip_vs_conn_sync_open,
830 .read = seq_read,
831 .llseek = seq_lseek,
832 .release = seq_release,
833};
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835#endif
836
837
838/*
839 * Randomly drop connection entries before running out of memory
840 */
841static inline int todrop_entry(struct ip_vs_conn *cp)
842{
843 /*
844 * The drop rate array needs tuning for real environments.
845 * Called from timer bh only => no locking
846 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -0800847 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 static char todrop_counter[9] = {0};
849 int i;
850
851 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
852 This will leave enough time for normal connection to get
853 through. */
854 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
855 return 0;
856
857 /* Don't drop the entry if its number of incoming packets is not
858 located in [0, 8] */
859 i = atomic_read(&cp->in_pkts);
860 if (i > 8 || i < 0) return 0;
861
862 if (!todrop_rate[i]) return 0;
863 if (--todrop_counter[i] > 0) return 0;
864
865 todrop_counter[i] = todrop_rate[i];
866 return 1;
867}
868
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700869/* Called from keventd and must protect itself from softirqs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870void ip_vs_random_dropentry(void)
871{
872 int idx;
873 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /*
876 * Randomly scan 1/32 of the whole table every second
877 */
878 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
879 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
880
881 /*
882 * Lock is actually needed in this loop.
883 */
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700884 ct_write_lock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -0700887 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* connection template */
889 continue;
890
891 if (cp->protocol == IPPROTO_TCP) {
892 switch(cp->state) {
893 case IP_VS_TCP_S_SYN_RECV:
894 case IP_VS_TCP_S_SYNACK:
895 break;
896
897 case IP_VS_TCP_S_ESTABLISHED:
898 if (todrop_entry(cp))
899 break;
900 continue;
901
902 default:
903 continue;
904 }
905 } else {
906 if (!todrop_entry(cp))
907 continue;
908 }
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 IP_VS_DBG(4, "del connection\n");
911 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -0700912 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -0700914 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Julian Anastasovaf9debd2005-07-11 20:59:57 -0700917 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919}
920
921
922/*
923 * Flush all the connection entries in the ip_vs_conn_tab
924 */
925static void ip_vs_conn_flush(void)
926{
927 int idx;
928 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 flush_again:
931 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
932 /*
933 * Lock is actually needed in this loop.
934 */
935 ct_write_lock_bh(idx);
936
937 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 IP_VS_DBG(4, "del connection\n");
940 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -0700941 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -0700943 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 ct_write_unlock_bh(idx);
947 }
948
949 /* the counter may be not NULL, because maybe some conn entries
950 are run by slow timer handler or unhashed but still referred */
951 if (atomic_read(&ip_vs_conn_count) != 0) {
952 schedule();
953 goto flush_again;
954 }
955}
956
957
958int ip_vs_conn_init(void)
959{
960 int idx;
961
962 /*
963 * Allocate the connection hash table and initialize its list heads
964 */
965 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
966 if (!ip_vs_conn_tab)
967 return -ENOMEM;
968
969 /* Allocate ip_vs_conn slab cache */
970 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
971 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900972 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!ip_vs_conn_cachep) {
974 vfree(ip_vs_conn_tab);
975 return -ENOMEM;
976 }
977
978 IP_VS_INFO("Connection hash table configured "
979 "(size=%d, memory=%ldKbytes)\n",
980 IP_VS_CONN_TAB_SIZE,
981 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
982 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
983 sizeof(struct ip_vs_conn));
984
985 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
986 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
987 }
988
989 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
990 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
991 }
992
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200993 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800994 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* calculate the random value for connection hash */
997 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
998
999 return 0;
1000}
1001
1002
1003void ip_vs_conn_cleanup(void)
1004{
1005 /* flush all the connection entries first */
1006 ip_vs_conn_flush();
1007
1008 /* Release the empty cache */
1009 kmem_cache_destroy(ip_vs_conn_cachep);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001010 proc_net_remove(&init_net, "ip_vs_conn");
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001011 proc_net_remove(&init_net, "ip_vs_conn_sync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 vfree(ip_vs_conn_tab);
1013}