]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - net/ipv4/ipvs/ip_vs_conn.c
[IPVS]: ip_vs_ftp breaks connections using persistence
[linux-3.10.git] / net / ipv4 / ipvs / ip_vs_conn.c
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.
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
27 #include <linux/kernel.h>
28 #include <linux/vmalloc.h>
29 #include <linux/proc_fs.h>              /* for proc_net_* */
30 #include <linux/seq_file.h>
31 #include <linux/jhash.h>
32 #include <linux/random.h>
33
34 #include <net/ip_vs.h>
35
36
37 /*
38  *  Connection hash table: for input and output packets lookups of IPVS
39  */
40 static struct list_head *ip_vs_conn_tab;
41
42 /*  SLAB cache for IPVS connections */
43 static kmem_cache_t *ip_vs_conn_cachep __read_mostly;
44
45 /*  counter for current IPVS connections */
46 static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
47
48 /*  counter for no client port connections */
49 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
50
51 /* random value for IPVS connection hash */
52 static unsigned int ip_vs_conn_rnd;
53
54 /*
55  *  Fine locking granularity for big connection hash table
56  */
57 #define CT_LOCKARRAY_BITS  4
58 #define CT_LOCKARRAY_SIZE  (1<<CT_LOCKARRAY_BITS)
59 #define CT_LOCKARRAY_MASK  (CT_LOCKARRAY_SIZE-1)
60
61 struct ip_vs_aligned_lock
62 {
63         rwlock_t        l;
64 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
65
66 /* lock array for conn table */
67 static struct ip_vs_aligned_lock
68 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
69
70 static inline void ct_read_lock(unsigned key)
71 {
72         read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
73 }
74
75 static inline void ct_read_unlock(unsigned key)
76 {
77         read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
78 }
79
80 static inline void ct_write_lock(unsigned key)
81 {
82         write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
83 }
84
85 static inline void ct_write_unlock(unsigned key)
86 {
87         write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
88 }
89
90 static inline void ct_read_lock_bh(unsigned key)
91 {
92         read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
93 }
94
95 static inline void ct_read_unlock_bh(unsigned key)
96 {
97         read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
98 }
99
100 static inline void ct_write_lock_bh(unsigned key)
101 {
102         write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
103 }
104
105 static inline void ct_write_unlock_bh(unsigned key)
106 {
107         write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
108 }
109
110
111 /*
112  *      Returns hash value for IPVS connection entry
113  */
114 static unsigned int ip_vs_conn_hashkey(unsigned proto, __u32 addr, __u16 port)
115 {
116         return jhash_3words(addr, port, proto, ip_vs_conn_rnd)
117                 & IP_VS_CONN_TAB_MASK;
118 }
119
120
121 /*
122  *      Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
123  *      returns bool success.
124  */
125 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
126 {
127         unsigned hash;
128         int ret;
129
130         /* Hash by protocol, client address and port */
131         hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
132
133         ct_write_lock(hash);
134
135         if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
136                 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
137                 cp->flags |= IP_VS_CONN_F_HASHED;
138                 atomic_inc(&cp->refcnt);
139                 ret = 1;
140         } else {
141                 IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, "
142                           "called from %p\n", __builtin_return_address(0));
143                 ret = 0;
144         }
145
146         ct_write_unlock(hash);
147
148         return ret;
149 }
150
151
152 /*
153  *      UNhashes ip_vs_conn from ip_vs_conn_tab.
154  *      returns bool success.
155  */
156 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
157 {
158         unsigned hash;
159         int ret;
160
161         /* unhash it and decrease its reference counter */
162         hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport);
163
164         ct_write_lock(hash);
165
166         if (cp->flags & IP_VS_CONN_F_HASHED) {
167                 list_del(&cp->c_list);
168                 cp->flags &= ~IP_VS_CONN_F_HASHED;
169                 atomic_dec(&cp->refcnt);
170                 ret = 1;
171         } else
172                 ret = 0;
173
174         ct_write_unlock(hash);
175
176         return ret;
177 }
178
179
180 /*
181  *  Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
182  *  Called for pkts coming from OUTside-to-INside.
183  *      s_addr, s_port: pkt source address (foreign host)
184  *      d_addr, d_port: pkt dest address (load balancer)
185  */
186 static inline struct ip_vs_conn *__ip_vs_conn_in_get
187 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
188 {
189         unsigned hash;
190         struct ip_vs_conn *cp;
191
192         hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
193
194         ct_read_lock(hash);
195
196         list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
197                 if (s_addr==cp->caddr && s_port==cp->cport &&
198                     d_port==cp->vport && d_addr==cp->vaddr &&
199                     ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
200                     protocol==cp->protocol) {
201                         /* HIT */
202                         atomic_inc(&cp->refcnt);
203                         ct_read_unlock(hash);
204                         return cp;
205                 }
206         }
207
208         ct_read_unlock(hash);
209
210         return NULL;
211 }
212
213 struct ip_vs_conn *ip_vs_conn_in_get
214 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
215 {
216         struct ip_vs_conn *cp;
217
218         cp = __ip_vs_conn_in_get(protocol, s_addr, s_port, d_addr, d_port);
219         if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
220                 cp = __ip_vs_conn_in_get(protocol, s_addr, 0, d_addr, d_port);
221
222         IP_VS_DBG(7, "lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
223                   ip_vs_proto_name(protocol),
224                   NIPQUAD(s_addr), ntohs(s_port),
225                   NIPQUAD(d_addr), ntohs(d_port),
226                   cp?"hit":"not hit");
227
228         return cp;
229 }
230
231 /* Get reference to connection template */
232 struct ip_vs_conn *ip_vs_ct_in_get
233 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
234 {
235         unsigned hash;
236         struct ip_vs_conn *cp;
237
238         hash = ip_vs_conn_hashkey(protocol, s_addr, s_port);
239
240         ct_read_lock(hash);
241
242         list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
243                 if (s_addr==cp->caddr && s_port==cp->cport &&
244                     d_port==cp->vport && d_addr==cp->vaddr &&
245                     cp->flags & IP_VS_CONN_F_TEMPLATE &&
246                     protocol==cp->protocol) {
247                         /* HIT */
248                         atomic_inc(&cp->refcnt);
249                         goto out;
250                 }
251         }
252         cp = NULL;
253
254   out:
255         ct_read_unlock(hash);
256
257         IP_VS_DBG(7, "template lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
258                   ip_vs_proto_name(protocol),
259                   NIPQUAD(s_addr), ntohs(s_port),
260                   NIPQUAD(d_addr), ntohs(d_port),
261                   cp?"hit":"not hit");
262
263         return cp;
264 }
265
266 /*
267  *  Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
268  *  Called for pkts coming from inside-to-OUTside.
269  *      s_addr, s_port: pkt source address (inside host)
270  *      d_addr, d_port: pkt dest address (foreign host)
271  */
272 struct ip_vs_conn *ip_vs_conn_out_get
273 (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
274 {
275         unsigned hash;
276         struct ip_vs_conn *cp, *ret=NULL;
277
278         /*
279          *      Check for "full" addressed entries
280          */
281         hash = ip_vs_conn_hashkey(protocol, d_addr, d_port);
282
283         ct_read_lock(hash);
284
285         list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
286                 if (d_addr == cp->caddr && d_port == cp->cport &&
287                     s_port == cp->dport && s_addr == cp->daddr &&
288                     protocol == cp->protocol) {
289                         /* HIT */
290                         atomic_inc(&cp->refcnt);
291                         ret = cp;
292                         break;
293                 }
294         }
295
296         ct_read_unlock(hash);
297
298         IP_VS_DBG(7, "lookup/out %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n",
299                   ip_vs_proto_name(protocol),
300                   NIPQUAD(s_addr), ntohs(s_port),
301                   NIPQUAD(d_addr), ntohs(d_port),
302                   ret?"hit":"not hit");
303
304         return ret;
305 }
306
307
308 /*
309  *      Put back the conn and restart its timer with its timeout
310  */
311 void ip_vs_conn_put(struct ip_vs_conn *cp)
312 {
313         /* reset it expire in its timeout */
314         mod_timer(&cp->timer, jiffies+cp->timeout);
315
316         __ip_vs_conn_put(cp);
317 }
318
319
320 /*
321  *      Fill a no_client_port connection with a client port number
322  */
323 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __u16 cport)
324 {
325         if (ip_vs_conn_unhash(cp)) {
326                 spin_lock(&cp->lock);
327                 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
328                         atomic_dec(&ip_vs_conn_no_cport_cnt);
329                         cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
330                         cp->cport = cport;
331                 }
332                 spin_unlock(&cp->lock);
333
334                 /* hash on new dport */
335                 ip_vs_conn_hash(cp);
336         }
337 }
338
339
340 /*
341  *      Bind a connection entry with the corresponding packet_xmit.
342  *      Called by ip_vs_conn_new.
343  */
344 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
345 {
346         switch (IP_VS_FWD_METHOD(cp)) {
347         case IP_VS_CONN_F_MASQ:
348                 cp->packet_xmit = ip_vs_nat_xmit;
349                 break;
350
351         case IP_VS_CONN_F_TUNNEL:
352                 cp->packet_xmit = ip_vs_tunnel_xmit;
353                 break;
354
355         case IP_VS_CONN_F_DROUTE:
356                 cp->packet_xmit = ip_vs_dr_xmit;
357                 break;
358
359         case IP_VS_CONN_F_LOCALNODE:
360                 cp->packet_xmit = ip_vs_null_xmit;
361                 break;
362
363         case IP_VS_CONN_F_BYPASS:
364                 cp->packet_xmit = ip_vs_bypass_xmit;
365                 break;
366         }
367 }
368
369
370 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
371 {
372         return atomic_read(&dest->activeconns)
373                 + atomic_read(&dest->inactconns);
374 }
375
376 /*
377  *      Bind a connection entry with a virtual service destination
378  *      Called just after a new connection entry is created.
379  */
380 static inline void
381 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
382 {
383         /* if dest is NULL, then return directly */
384         if (!dest)
385                 return;
386
387         /* Increase the refcnt counter of the dest */
388         atomic_inc(&dest->refcnt);
389
390         /* Bind with the destination and its corresponding transmitter */
391         cp->flags |= atomic_read(&dest->conn_flags);
392         cp->dest = dest;
393
394         IP_VS_DBG(9, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
395                   "d:%u.%u.%u.%u:%d fwd:%c s:%u flg:%X cnt:%d destcnt:%d\n",
396                   ip_vs_proto_name(cp->protocol),
397                   NIPQUAD(cp->caddr), ntohs(cp->cport),
398                   NIPQUAD(cp->vaddr), ntohs(cp->vport),
399                   NIPQUAD(cp->daddr), ntohs(cp->dport),
400                   ip_vs_fwd_tag(cp), cp->state,
401                   cp->flags, atomic_read(&cp->refcnt),
402                   atomic_read(&dest->refcnt));
403
404         /* Update the connection counters */
405         if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
406                 /* It is a normal connection, so increase the inactive
407                    connection counter because it is in TCP SYNRECV
408                    state (inactive) or other protocol inacive state */
409                 atomic_inc(&dest->inactconns);
410         } else {
411                 /* It is a persistent connection/template, so increase
412                    the peristent connection counter */
413                 atomic_inc(&dest->persistconns);
414         }
415
416         if (dest->u_threshold != 0 &&
417             ip_vs_dest_totalconns(dest) >= dest->u_threshold)
418                 dest->flags |= IP_VS_DEST_F_OVERLOAD;
419 }
420
421
422 /*
423  *      Unbind a connection entry with its VS destination
424  *      Called by the ip_vs_conn_expire function.
425  */
426 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
427 {
428         struct ip_vs_dest *dest = cp->dest;
429
430         if (!dest)
431                 return;
432
433         IP_VS_DBG(9, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
434                   "d:%u.%u.%u.%u:%d fwd:%c s:%u flg:%X cnt:%d destcnt:%d\n",
435                   ip_vs_proto_name(cp->protocol),
436                   NIPQUAD(cp->caddr), ntohs(cp->cport),
437                   NIPQUAD(cp->vaddr), ntohs(cp->vport),
438                   NIPQUAD(cp->daddr), ntohs(cp->dport),
439                   ip_vs_fwd_tag(cp), cp->state,
440                   cp->flags, atomic_read(&cp->refcnt),
441                   atomic_read(&dest->refcnt));
442
443         /* Update the connection counters */
444         if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
445                 /* It is a normal connection, so decrease the inactconns
446                    or activeconns counter */
447                 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
448                         atomic_dec(&dest->inactconns);
449                 } else {
450                         atomic_dec(&dest->activeconns);
451                 }
452         } else {
453                 /* It is a persistent connection/template, so decrease
454                    the peristent connection counter */
455                 atomic_dec(&dest->persistconns);
456         }
457
458         if (dest->l_threshold != 0) {
459                 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
460                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
461         } else if (dest->u_threshold != 0) {
462                 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
463                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
464         } else {
465                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
466                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
467         }
468
469         /*
470          * Simply decrease the refcnt of the dest, because the
471          * dest will be either in service's destination list
472          * or in the trash.
473          */
474         atomic_dec(&dest->refcnt);
475 }
476
477
478 /*
479  *      Checking if the destination of a connection template is available.
480  *      If available, return 1, otherwise invalidate this connection
481  *      template and return 0.
482  */
483 int ip_vs_check_template(struct ip_vs_conn *ct)
484 {
485         struct ip_vs_dest *dest = ct->dest;
486
487         /*
488          * Checking the dest server status.
489          */
490         if ((dest == NULL) ||
491             !(dest->flags & IP_VS_DEST_F_AVAILABLE) || 
492             (sysctl_ip_vs_expire_quiescent_template && 
493              (atomic_read(&dest->weight) == 0))) {
494                 IP_VS_DBG(9, "check_template: dest not available for "
495                           "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
496                           "-> d:%u.%u.%u.%u:%d\n",
497                           ip_vs_proto_name(ct->protocol),
498                           NIPQUAD(ct->caddr), ntohs(ct->cport),
499                           NIPQUAD(ct->vaddr), ntohs(ct->vport),
500                           NIPQUAD(ct->daddr), ntohs(ct->dport));
501
502                 /*
503                  * Invalidate the connection template
504                  */
505                 if (ct->vport != 65535) {
506                         if (ip_vs_conn_unhash(ct)) {
507                                 ct->dport = 65535;
508                                 ct->vport = 65535;
509                                 ct->cport = 0;
510                                 ip_vs_conn_hash(ct);
511                         }
512                 }
513
514                 /*
515                  * Simply decrease the refcnt of the template,
516                  * don't restart its timer.
517                  */
518                 atomic_dec(&ct->refcnt);
519                 return 0;
520         }
521         return 1;
522 }
523
524 static void ip_vs_conn_expire(unsigned long data)
525 {
526         struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
527
528         cp->timeout = 60*HZ;
529
530         /*
531          *      hey, I'm using it
532          */
533         atomic_inc(&cp->refcnt);
534
535         /*
536          *      do I control anybody?
537          */
538         if (atomic_read(&cp->n_control))
539                 goto expire_later;
540
541         /*
542          *      unhash it if it is hashed in the conn table
543          */
544         if (!ip_vs_conn_unhash(cp))
545                 goto expire_later;
546
547         /*
548          *      refcnt==1 implies I'm the only one referrer
549          */
550         if (likely(atomic_read(&cp->refcnt) == 1)) {
551                 /* delete the timer if it is activated by other users */
552                 if (timer_pending(&cp->timer))
553                         del_timer(&cp->timer);
554
555                 /* does anybody control me? */
556                 if (cp->control)
557                         ip_vs_control_del(cp);
558
559                 if (unlikely(cp->app != NULL))
560                         ip_vs_unbind_app(cp);
561                 ip_vs_unbind_dest(cp);
562                 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
563                         atomic_dec(&ip_vs_conn_no_cport_cnt);
564                 atomic_dec(&ip_vs_conn_count);
565
566                 kmem_cache_free(ip_vs_conn_cachep, cp);
567                 return;
568         }
569
570         /* hash it back to the table */
571         ip_vs_conn_hash(cp);
572
573   expire_later:
574         IP_VS_DBG(7, "delayed: refcnt-1=%d conn.n_control=%d\n",
575                   atomic_read(&cp->refcnt)-1,
576                   atomic_read(&cp->n_control));
577
578         ip_vs_conn_put(cp);
579 }
580
581
582 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
583 {
584         if (del_timer(&cp->timer))
585                 mod_timer(&cp->timer, jiffies);
586 }
587
588
589 /*
590  *      Create a new connection entry and hash it into the ip_vs_conn_tab
591  */
592 struct ip_vs_conn *
593 ip_vs_conn_new(int proto, __u32 caddr, __u16 cport, __u32 vaddr, __u16 vport,
594                __u32 daddr, __u16 dport, unsigned flags,
595                struct ip_vs_dest *dest)
596 {
597         struct ip_vs_conn *cp;
598         struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
599
600         cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
601         if (cp == NULL) {
602                 IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
603                 return NULL;
604         }
605
606         memset(cp, 0, sizeof(*cp));
607         INIT_LIST_HEAD(&cp->c_list);
608         init_timer(&cp->timer);
609         cp->timer.data     = (unsigned long)cp;
610         cp->timer.function = ip_vs_conn_expire;
611         cp->protocol       = proto;
612         cp->caddr          = caddr;
613         cp->cport          = cport;
614         cp->vaddr          = vaddr;
615         cp->vport          = vport;
616         cp->daddr          = daddr;
617         cp->dport          = dport;
618         cp->flags          = flags;
619         spin_lock_init(&cp->lock);
620
621         /*
622          * Set the entry is referenced by the current thread before hashing
623          * it in the table, so that other thread run ip_vs_random_dropentry
624          * but cannot drop this entry.
625          */
626         atomic_set(&cp->refcnt, 1);
627
628         atomic_set(&cp->n_control, 0);
629         atomic_set(&cp->in_pkts, 0);
630
631         atomic_inc(&ip_vs_conn_count);
632         if (flags & IP_VS_CONN_F_NO_CPORT)
633                 atomic_inc(&ip_vs_conn_no_cport_cnt);
634
635         /* Bind the connection with a destination server */
636         ip_vs_bind_dest(cp, dest);
637
638         /* Set its state and timeout */
639         cp->state = 0;
640         cp->timeout = 3*HZ;
641
642         /* Bind its packet transmitter */
643         ip_vs_bind_xmit(cp);
644
645         if (unlikely(pp && atomic_read(&pp->appcnt)))
646                 ip_vs_bind_app(cp, pp);
647
648         /* Hash it in the ip_vs_conn_tab finally */
649         ip_vs_conn_hash(cp);
650
651         return cp;
652 }
653
654
655 /*
656  *      /proc/net/ip_vs_conn entries
657  */
658 #ifdef CONFIG_PROC_FS
659
660 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
661 {
662         int idx;
663         struct ip_vs_conn *cp;
664         
665         for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
666                 ct_read_lock_bh(idx);
667                 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
668                         if (pos-- == 0) {
669                                 seq->private = &ip_vs_conn_tab[idx];
670                                 return cp;
671                         }
672                 }
673                 ct_read_unlock_bh(idx);
674         }
675
676         return NULL;
677 }
678
679 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
680 {
681         seq->private = NULL;
682         return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
683 }
684
685 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
686 {
687         struct ip_vs_conn *cp = v;
688         struct list_head *e, *l = seq->private;
689         int idx;
690
691         ++*pos;
692         if (v == SEQ_START_TOKEN) 
693                 return ip_vs_conn_array(seq, 0);
694
695         /* more on same hash chain? */
696         if ((e = cp->c_list.next) != l)
697                 return list_entry(e, struct ip_vs_conn, c_list);
698
699         idx = l - ip_vs_conn_tab;
700         ct_read_unlock_bh(idx);
701
702         while (++idx < IP_VS_CONN_TAB_SIZE) {
703                 ct_read_lock_bh(idx);
704                 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
705                         seq->private = &ip_vs_conn_tab[idx];
706                         return cp;
707                 }       
708                 ct_read_unlock_bh(idx);
709         }
710         seq->private = NULL;
711         return NULL;
712 }
713
714 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
715 {
716         struct list_head *l = seq->private;
717
718         if (l)
719                 ct_read_unlock_bh(l - ip_vs_conn_tab);
720 }
721
722 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
723 {
724
725         if (v == SEQ_START_TOKEN)
726                 seq_puts(seq,
727    "Pro FromIP   FPrt ToIP     TPrt DestIP   DPrt State       Expires\n");
728         else {
729                 const struct ip_vs_conn *cp = v;
730
731                 seq_printf(seq,
732                         "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
733                                 ip_vs_proto_name(cp->protocol),
734                                 ntohl(cp->caddr), ntohs(cp->cport),
735                                 ntohl(cp->vaddr), ntohs(cp->vport),
736                                 ntohl(cp->daddr), ntohs(cp->dport),
737                                 ip_vs_state_name(cp->protocol, cp->state),
738                                 (cp->timer.expires-jiffies)/HZ);
739         }
740         return 0;
741 }
742
743 static struct seq_operations ip_vs_conn_seq_ops = {
744         .start = ip_vs_conn_seq_start,
745         .next  = ip_vs_conn_seq_next,
746         .stop  = ip_vs_conn_seq_stop,
747         .show  = ip_vs_conn_seq_show,
748 };
749
750 static int ip_vs_conn_open(struct inode *inode, struct file *file)
751 {
752         return seq_open(file, &ip_vs_conn_seq_ops);
753 }
754
755 static struct file_operations ip_vs_conn_fops = {
756         .owner   = THIS_MODULE,
757         .open    = ip_vs_conn_open,
758         .read    = seq_read,
759         .llseek  = seq_lseek,
760         .release = seq_release,
761 };
762 #endif
763
764
765 /*
766  *      Randomly drop connection entries before running out of memory
767  */
768 static inline int todrop_entry(struct ip_vs_conn *cp)
769 {
770         /*
771          * The drop rate array needs tuning for real environments.
772          * Called from timer bh only => no locking
773          */
774         static char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
775         static char todrop_counter[9] = {0};
776         int i;
777
778         /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
779            This will leave enough time for normal connection to get
780            through. */
781         if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
782                 return 0;
783
784         /* Don't drop the entry if its number of incoming packets is not
785            located in [0, 8] */
786         i = atomic_read(&cp->in_pkts);
787         if (i > 8 || i < 0) return 0;
788
789         if (!todrop_rate[i]) return 0;
790         if (--todrop_counter[i] > 0) return 0;
791
792         todrop_counter[i] = todrop_rate[i];
793         return 1;
794 }
795
796 /* Called from keventd and must protect itself from softirqs */
797 void ip_vs_random_dropentry(void)
798 {
799         int idx;
800         struct ip_vs_conn *cp;
801
802         /*
803          * Randomly scan 1/32 of the whole table every second
804          */
805         for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
806                 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
807
808                 /*
809                  *  Lock is actually needed in this loop.
810                  */
811                 ct_write_lock_bh(hash);
812
813                 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
814                         if (cp->flags & IP_VS_CONN_F_TEMPLATE)
815                                 /* connection template */
816                                 continue;
817
818                         if (cp->protocol == IPPROTO_TCP) {
819                                 switch(cp->state) {
820                                 case IP_VS_TCP_S_SYN_RECV:
821                                 case IP_VS_TCP_S_SYNACK:
822                                         break;
823
824                                 case IP_VS_TCP_S_ESTABLISHED:
825                                         if (todrop_entry(cp))
826                                                 break;
827                                         continue;
828
829                                 default:
830                                         continue;
831                                 }
832                         } else {
833                                 if (!todrop_entry(cp))
834                                         continue;
835                         }
836
837                         IP_VS_DBG(4, "del connection\n");
838                         ip_vs_conn_expire_now(cp);
839                         if (cp->control) {
840                                 IP_VS_DBG(4, "del conn template\n");
841                                 ip_vs_conn_expire_now(cp->control);
842                         }
843                 }
844                 ct_write_unlock_bh(hash);
845         }
846 }
847
848
849 /*
850  *      Flush all the connection entries in the ip_vs_conn_tab
851  */
852 static void ip_vs_conn_flush(void)
853 {
854         int idx;
855         struct ip_vs_conn *cp;
856
857   flush_again:
858         for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
859                 /*
860                  *  Lock is actually needed in this loop.
861                  */
862                 ct_write_lock_bh(idx);
863
864                 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
865
866                         IP_VS_DBG(4, "del connection\n");
867                         ip_vs_conn_expire_now(cp);
868                         if (cp->control) {
869                                 IP_VS_DBG(4, "del conn template\n");
870                                 ip_vs_conn_expire_now(cp->control);
871                         }
872                 }
873                 ct_write_unlock_bh(idx);
874         }
875
876         /* the counter may be not NULL, because maybe some conn entries
877            are run by slow timer handler or unhashed but still referred */
878         if (atomic_read(&ip_vs_conn_count) != 0) {
879                 schedule();
880                 goto flush_again;
881         }
882 }
883
884
885 int ip_vs_conn_init(void)
886 {
887         int idx;
888
889         /*
890          * Allocate the connection hash table and initialize its list heads
891          */
892         ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
893         if (!ip_vs_conn_tab)
894                 return -ENOMEM;
895
896         /* Allocate ip_vs_conn slab cache */
897         ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
898                                               sizeof(struct ip_vs_conn), 0,
899                                               SLAB_HWCACHE_ALIGN, NULL, NULL);
900         if (!ip_vs_conn_cachep) {
901                 vfree(ip_vs_conn_tab);
902                 return -ENOMEM;
903         }
904
905         IP_VS_INFO("Connection hash table configured "
906                    "(size=%d, memory=%ldKbytes)\n",
907                    IP_VS_CONN_TAB_SIZE,
908                    (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
909         IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
910                   sizeof(struct ip_vs_conn));
911
912         for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
913                 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
914         }
915
916         for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++)  {
917                 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
918         }
919
920         proc_net_fops_create("ip_vs_conn", 0, &ip_vs_conn_fops);
921
922         /* calculate the random value for connection hash */
923         get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
924
925         return 0;
926 }
927
928
929 void ip_vs_conn_cleanup(void)
930 {
931         /* flush all the connection entries first */
932         ip_vs_conn_flush();
933
934         /* Release the empty cache */
935         kmem_cache_destroy(ip_vs_conn_cachep);
936         proc_net_remove("ip_vs_conn");
937         vfree(ip_vs_conn_tab);
938 }