[IPVS]: Remove unused exports.
[linux-2.6/zen-sources.git] / net / ipv4 / ipvs / ip_vs_conn.c
blob0a9f3c37e18d8caa939f22d40b7b66dc72df3753
1 /*
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.
8 * Version: $Id: ip_vs_conn.c,v 1.31 2003/04/18 09:03:16 wensong Exp $
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 * Peter Kese <peter.kese@ijs.si>
12 * Julian Anastasov <ja@ssi.bg>
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.
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.
23 * Changes:
27 #include <linux/interrupt.h>
28 #include <linux/in.h>
29 #include <linux/net.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #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>
38 #include <net/net_namespace.h>
39 #include <net/ip_vs.h>
43 * Connection hash table: for input and output packets lookups of IPVS
45 static struct list_head *ip_vs_conn_tab;
47 /* SLAB cache for IPVS connections */
48 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
50 /* counter for current IPVS connections */
51 static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
53 /* counter for no client port connections */
54 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
56 /* random value for IPVS connection hash */
57 static unsigned int ip_vs_conn_rnd;
60 * Fine locking granularity for big connection hash table
62 #define CT_LOCKARRAY_BITS 4
63 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
64 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
66 struct ip_vs_aligned_lock
68 rwlock_t l;
69 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
71 /* lock array for conn table */
72 static struct ip_vs_aligned_lock
73 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
75 static inline void ct_read_lock(unsigned key)
77 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
80 static inline void ct_read_unlock(unsigned key)
82 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
85 static inline void ct_write_lock(unsigned key)
87 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
90 static inline void ct_write_unlock(unsigned key)
92 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
95 static inline void ct_read_lock_bh(unsigned key)
97 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
100 static inline void ct_read_unlock_bh(unsigned key)
102 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
105 static inline void ct_write_lock_bh(unsigned key)
107 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
110 static inline void ct_write_unlock_bh(unsigned key)
112 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
117 * Returns hash value for IPVS connection entry
119 static unsigned int ip_vs_conn_hashkey(unsigned proto, __be32 addr, __be16 port)
121 return jhash_3words((__force u32)addr, (__force u32)port, proto, ip_vs_conn_rnd)
122 & IP_VS_CONN_TAB_MASK;
127 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
128 * returns bool success.
130 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
132 unsigned hash;
133 int ret;
135 /* Hash by protocol, client address and port */
136 hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
138 ct_write_lock(hash);
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;
151 ct_write_unlock(hash);
153 return ret;
158 * UNhashes ip_vs_conn from ip_vs_conn_tab.
159 * returns bool success.
161 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
163 unsigned hash;
164 int ret;
166 /* unhash it and decrease its reference counter */
167 hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
169 ct_write_lock(hash);
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;
179 ct_write_unlock(hash);
181 return ret;
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)
191 static inline struct ip_vs_conn *__ip_vs_conn_in_get
192 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
194 unsigned hash;
195 struct ip_vs_conn *cp;
197 hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
199 ct_read_lock(hash);
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 &&
204 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
205 protocol==cp->protocol) {
206 /* HIT */
207 atomic_inc(&cp->refcnt);
208 ct_read_unlock(hash);
209 return cp;
213 ct_read_unlock(hash);
215 return NULL;
218 struct ip_vs_conn *ip_vs_conn_in_get
219 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
221 struct ip_vs_conn *cp;
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);
227 IP_VS_DBG(9, "lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
228 ip_vs_proto_name(protocol),
229 NIPQUAD(s_addr), ntohs(s_port),
230 NIPQUAD(d_addr), ntohs(d_port),
231 cp?"hit":"not hit");
233 return cp;
236 /* Get reference to connection template */
237 struct ip_vs_conn *ip_vs_ct_in_get
238 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
240 unsigned hash;
241 struct ip_vs_conn *cp;
243 hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
245 ct_read_lock(hash);
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;
257 cp = NULL;
259 out:
260 ct_read_unlock(hash);
262 IP_VS_DBG(9, "template lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
263 ip_vs_proto_name(protocol),
264 NIPQUAD(s_addr), ntohs(s_port),
265 NIPQUAD(d_addr), ntohs(d_port),
266 cp?"hit":"not hit");
268 return cp;
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)
277 struct ip_vs_conn *ip_vs_conn_out_get
278 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port)
280 unsigned hash;
281 struct ip_vs_conn *cp, *ret=NULL;
284 * Check for "full" addressed entries
286 hash = ip_vs_conn_hashkey(protocol, d_addr, d_port);
288 ct_read_lock(hash);
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;
301 ct_read_unlock(hash);
303 IP_VS_DBG(9, "lookup/out %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
304 ip_vs_proto_name(protocol),
305 NIPQUAD(s_addr), ntohs(s_port),
306 NIPQUAD(d_addr), ntohs(d_port),
307 ret?"hit":"not hit");
309 return ret;
314 * Put back the conn and restart its timer with its timeout
316 void ip_vs_conn_put(struct ip_vs_conn *cp)
318 /* reset it expire in its timeout */
319 mod_timer(&cp->timer, jiffies+cp->timeout);
321 __ip_vs_conn_put(cp);
326 * Fill a no_client_port connection with a client port number
328 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
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;
337 spin_unlock(&cp->lock);
339 /* hash on new dport */
340 ip_vs_conn_hash(cp);
346 * Bind a connection entry with the corresponding packet_xmit.
347 * Called by ip_vs_conn_new.
349 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
351 switch (IP_VS_FWD_METHOD(cp)) {
352 case IP_VS_CONN_F_MASQ:
353 cp->packet_xmit = ip_vs_nat_xmit;
354 break;
356 case IP_VS_CONN_F_TUNNEL:
357 cp->packet_xmit = ip_vs_tunnel_xmit;
358 break;
360 case IP_VS_CONN_F_DROUTE:
361 cp->packet_xmit = ip_vs_dr_xmit;
362 break;
364 case IP_VS_CONN_F_LOCALNODE:
365 cp->packet_xmit = ip_vs_null_xmit;
366 break;
368 case IP_VS_CONN_F_BYPASS:
369 cp->packet_xmit = ip_vs_bypass_xmit;
370 break;
375 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
377 return atomic_read(&dest->activeconns)
378 + atomic_read(&dest->inactconns);
382 * Bind a connection entry with a virtual service destination
383 * Called just after a new connection entry is created.
385 static inline void
386 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
388 /* if dest is NULL, then return directly */
389 if (!dest)
390 return;
392 /* Increase the refcnt counter of the dest */
393 atomic_inc(&dest->refcnt);
395 /* Bind with the destination and its corresponding transmitter */
396 cp->flags |= atomic_read(&dest->conn_flags);
397 cp->dest = dest;
399 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",
402 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));
410 /* Update the connection counters */
411 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
412 /* 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);
422 if (dest->u_threshold != 0 &&
423 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
424 dest->flags |= IP_VS_DEST_F_OVERLOAD;
429 * Check if there is a destination for the connection, if so
430 * bind the connection to the destination.
432 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
434 struct ip_vs_dest *dest;
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;
447 * Unbind a connection entry with its VS destination
448 * Called by the ip_vs_conn_expire function.
450 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
452 struct ip_vs_dest *dest = cp->dest;
454 if (!dest)
455 return;
457 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",
460 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));
468 /* Update the connection counters */
469 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
470 /* 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);
477 } else {
478 /* It is a persistent connection/template, so decrease
479 the peristent connection counter */
480 atomic_dec(&dest->persistconns);
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;
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.
499 atomic_dec(&dest->refcnt);
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.
508 int ip_vs_check_template(struct ip_vs_conn *ct)
510 struct ip_vs_dest *dest = ct->dest;
513 * Checking the dest server status.
515 if ((dest == NULL) ||
516 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
517 (sysctl_ip_vs_expire_quiescent_template &&
518 (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));
528 * Invalidate the connection template
530 if (ct->vport != htons(0xffff)) {
531 if (ip_vs_conn_unhash(ct)) {
532 ct->dport = htons(0xffff);
533 ct->vport = htons(0xffff);
534 ct->cport = 0;
535 ip_vs_conn_hash(ct);
540 * Simply decrease the refcnt of the template,
541 * don't restart its timer.
543 atomic_dec(&ct->refcnt);
544 return 0;
546 return 1;
549 static void ip_vs_conn_expire(unsigned long data)
551 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
553 cp->timeout = 60*HZ;
556 * hey, I'm using it
558 atomic_inc(&cp->refcnt);
561 * do I control anybody?
563 if (atomic_read(&cp->n_control))
564 goto expire_later;
567 * unhash it if it is hashed in the conn table
569 if (!ip_vs_conn_unhash(cp))
570 goto expire_later;
573 * refcnt==1 implies I'm the only one referrer
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);
580 /* does anybody control me? */
581 if (cp->control)
582 ip_vs_control_del(cp);
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);
591 kmem_cache_free(ip_vs_conn_cachep, cp);
592 return;
595 /* hash it back to the table */
596 ip_vs_conn_hash(cp);
598 expire_later:
599 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
600 atomic_read(&cp->refcnt)-1,
601 atomic_read(&cp->n_control));
603 ip_vs_conn_put(cp);
607 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
609 if (del_timer(&cp->timer))
610 mod_timer(&cp->timer, jiffies);
615 * Create a new connection entry and hash it into the ip_vs_conn_tab
617 struct ip_vs_conn *
618 ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
619 __be32 daddr, __be16 dport, unsigned flags,
620 struct ip_vs_dest *dest)
622 struct ip_vs_conn *cp;
623 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
625 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
626 if (cp == NULL) {
627 IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
628 return NULL;
631 INIT_LIST_HEAD(&cp->c_list);
632 init_timer(&cp->timer);
633 cp->timer.data = (unsigned long)cp;
634 cp->timer.function = ip_vs_conn_expire;
635 cp->protocol = proto;
636 cp->caddr = caddr;
637 cp->cport = cport;
638 cp->vaddr = vaddr;
639 cp->vport = vport;
640 cp->daddr = daddr;
641 cp->dport = dport;
642 cp->flags = flags;
643 spin_lock_init(&cp->lock);
646 * Set the entry is referenced by the current thread before hashing
647 * it in the table, so that other thread run ip_vs_random_dropentry
648 * but cannot drop this entry.
650 atomic_set(&cp->refcnt, 1);
652 atomic_set(&cp->n_control, 0);
653 atomic_set(&cp->in_pkts, 0);
655 atomic_inc(&ip_vs_conn_count);
656 if (flags & IP_VS_CONN_F_NO_CPORT)
657 atomic_inc(&ip_vs_conn_no_cport_cnt);
659 /* Bind the connection with a destination server */
660 ip_vs_bind_dest(cp, dest);
662 /* Set its state and timeout */
663 cp->state = 0;
664 cp->timeout = 3*HZ;
666 /* Bind its packet transmitter */
667 ip_vs_bind_xmit(cp);
669 if (unlikely(pp && atomic_read(&pp->appcnt)))
670 ip_vs_bind_app(cp, pp);
672 /* Hash it in the ip_vs_conn_tab finally */
673 ip_vs_conn_hash(cp);
675 return cp;
680 * /proc/net/ip_vs_conn entries
682 #ifdef CONFIG_PROC_FS
684 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
686 int idx;
687 struct ip_vs_conn *cp;
689 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
690 ct_read_lock_bh(idx);
691 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
692 if (pos-- == 0) {
693 seq->private = &ip_vs_conn_tab[idx];
694 return cp;
697 ct_read_unlock_bh(idx);
700 return NULL;
703 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
705 seq->private = NULL;
706 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
709 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
711 struct ip_vs_conn *cp = v;
712 struct list_head *e, *l = seq->private;
713 int idx;
715 ++*pos;
716 if (v == SEQ_START_TOKEN)
717 return ip_vs_conn_array(seq, 0);
719 /* more on same hash chain? */
720 if ((e = cp->c_list.next) != l)
721 return list_entry(e, struct ip_vs_conn, c_list);
723 idx = l - ip_vs_conn_tab;
724 ct_read_unlock_bh(idx);
726 while (++idx < IP_VS_CONN_TAB_SIZE) {
727 ct_read_lock_bh(idx);
728 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
729 seq->private = &ip_vs_conn_tab[idx];
730 return cp;
732 ct_read_unlock_bh(idx);
734 seq->private = NULL;
735 return NULL;
738 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
740 struct list_head *l = seq->private;
742 if (l)
743 ct_read_unlock_bh(l - ip_vs_conn_tab);
746 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
749 if (v == SEQ_START_TOKEN)
750 seq_puts(seq,
751 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
752 else {
753 const struct ip_vs_conn *cp = v;
755 seq_printf(seq,
756 "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
757 ip_vs_proto_name(cp->protocol),
758 ntohl(cp->caddr), ntohs(cp->cport),
759 ntohl(cp->vaddr), ntohs(cp->vport),
760 ntohl(cp->daddr), ntohs(cp->dport),
761 ip_vs_state_name(cp->protocol, cp->state),
762 (cp->timer.expires-jiffies)/HZ);
764 return 0;
767 static const struct seq_operations ip_vs_conn_seq_ops = {
768 .start = ip_vs_conn_seq_start,
769 .next = ip_vs_conn_seq_next,
770 .stop = ip_vs_conn_seq_stop,
771 .show = ip_vs_conn_seq_show,
774 static int ip_vs_conn_open(struct inode *inode, struct file *file)
776 return seq_open(file, &ip_vs_conn_seq_ops);
779 static const struct file_operations ip_vs_conn_fops = {
780 .owner = THIS_MODULE,
781 .open = ip_vs_conn_open,
782 .read = seq_read,
783 .llseek = seq_lseek,
784 .release = seq_release,
786 #endif
790 * Randomly drop connection entries before running out of memory
792 static inline int todrop_entry(struct ip_vs_conn *cp)
795 * The drop rate array needs tuning for real environments.
796 * Called from timer bh only => no locking
798 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
799 static char todrop_counter[9] = {0};
800 int i;
802 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
803 This will leave enough time for normal connection to get
804 through. */
805 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
806 return 0;
808 /* Don't drop the entry if its number of incoming packets is not
809 located in [0, 8] */
810 i = atomic_read(&cp->in_pkts);
811 if (i > 8 || i < 0) return 0;
813 if (!todrop_rate[i]) return 0;
814 if (--todrop_counter[i] > 0) return 0;
816 todrop_counter[i] = todrop_rate[i];
817 return 1;
820 /* Called from keventd and must protect itself from softirqs */
821 void ip_vs_random_dropentry(void)
823 int idx;
824 struct ip_vs_conn *cp;
827 * Randomly scan 1/32 of the whole table every second
829 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
830 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
833 * Lock is actually needed in this loop.
835 ct_write_lock_bh(hash);
837 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
838 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
839 /* connection template */
840 continue;
842 if (cp->protocol == IPPROTO_TCP) {
843 switch(cp->state) {
844 case IP_VS_TCP_S_SYN_RECV:
845 case IP_VS_TCP_S_SYNACK:
846 break;
848 case IP_VS_TCP_S_ESTABLISHED:
849 if (todrop_entry(cp))
850 break;
851 continue;
853 default:
854 continue;
856 } else {
857 if (!todrop_entry(cp))
858 continue;
861 IP_VS_DBG(4, "del connection\n");
862 ip_vs_conn_expire_now(cp);
863 if (cp->control) {
864 IP_VS_DBG(4, "del conn template\n");
865 ip_vs_conn_expire_now(cp->control);
868 ct_write_unlock_bh(hash);
874 * Flush all the connection entries in the ip_vs_conn_tab
876 static void ip_vs_conn_flush(void)
878 int idx;
879 struct ip_vs_conn *cp;
881 flush_again:
882 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
884 * Lock is actually needed in this loop.
886 ct_write_lock_bh(idx);
888 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
890 IP_VS_DBG(4, "del connection\n");
891 ip_vs_conn_expire_now(cp);
892 if (cp->control) {
893 IP_VS_DBG(4, "del conn template\n");
894 ip_vs_conn_expire_now(cp->control);
897 ct_write_unlock_bh(idx);
900 /* the counter may be not NULL, because maybe some conn entries
901 are run by slow timer handler or unhashed but still referred */
902 if (atomic_read(&ip_vs_conn_count) != 0) {
903 schedule();
904 goto flush_again;
909 int ip_vs_conn_init(void)
911 int idx;
914 * Allocate the connection hash table and initialize its list heads
916 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
917 if (!ip_vs_conn_tab)
918 return -ENOMEM;
920 /* Allocate ip_vs_conn slab cache */
921 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
922 sizeof(struct ip_vs_conn), 0,
923 SLAB_HWCACHE_ALIGN, NULL);
924 if (!ip_vs_conn_cachep) {
925 vfree(ip_vs_conn_tab);
926 return -ENOMEM;
929 IP_VS_INFO("Connection hash table configured "
930 "(size=%d, memory=%ldKbytes)\n",
931 IP_VS_CONN_TAB_SIZE,
932 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
933 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
934 sizeof(struct ip_vs_conn));
936 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
937 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
940 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
941 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
944 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
946 /* calculate the random value for connection hash */
947 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
949 return 0;
953 void ip_vs_conn_cleanup(void)
955 /* flush all the connection entries first */
956 ip_vs_conn_flush();
958 /* Release the empty cache */
959 kmem_cache_destroy(ip_vs_conn_cachep);
960 proc_net_remove(&init_net, "ip_vs_conn");
961 vfree(ip_vs_conn_tab);