]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/netfilter/ipvs/ip_vs_conn.c
hlist: drop the node parameter from iterators
[karo-tx-linux.git] / net / netfilter / 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  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
9  *              Peter Kese <peter.kese@ijs.si>
10  *              Julian Anastasov <ja@ssi.bg>
11  *
12  *              This program is free software; you can redistribute it and/or
13  *              modify it under the terms of the GNU General Public License
14  *              as published by the Free Software Foundation; either version
15  *              2 of the License, or (at your option) any later version.
16  *
17  * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18  * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
19  * and others. Many code here is taken from IP MASQ code of kernel 2.2.
20  *
21  * Changes:
22  *
23  */
24
25 #define KMSG_COMPONENT "IPVS"
26 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
27
28 #include <linux/interrupt.h>
29 #include <linux/in.h>
30 #include <linux/net.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/vmalloc.h>
34 #include <linux/proc_fs.h>              /* for proc_net_* */
35 #include <linux/slab.h>
36 #include <linux/seq_file.h>
37 #include <linux/jhash.h>
38 #include <linux/random.h>
39
40 #include <net/net_namespace.h>
41 #include <net/ip_vs.h>
42
43
44 #ifndef CONFIG_IP_VS_TAB_BITS
45 #define CONFIG_IP_VS_TAB_BITS   12
46 #endif
47
48 /*
49  * Connection hash size. Default is what was selected at compile time.
50 */
51 static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
52 module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
53 MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
54
55 /* size and mask values */
56 int ip_vs_conn_tab_size __read_mostly;
57 static int ip_vs_conn_tab_mask __read_mostly;
58
59 /*
60  *  Connection hash table: for input and output packets lookups of IPVS
61  */
62 static struct hlist_head *ip_vs_conn_tab __read_mostly;
63
64 /*  SLAB cache for IPVS connections */
65 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
66
67 /*  counter for no client port connections */
68 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
69
70 /* random value for IPVS connection hash */
71 static unsigned int ip_vs_conn_rnd __read_mostly;
72
73 /*
74  *  Fine locking granularity for big connection hash table
75  */
76 #define CT_LOCKARRAY_BITS  5
77 #define CT_LOCKARRAY_SIZE  (1<<CT_LOCKARRAY_BITS)
78 #define CT_LOCKARRAY_MASK  (CT_LOCKARRAY_SIZE-1)
79
80 struct ip_vs_aligned_lock
81 {
82         rwlock_t        l;
83 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
84
85 /* lock array for conn table */
86 static struct ip_vs_aligned_lock
87 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
88
89 static inline void ct_read_lock(unsigned int key)
90 {
91         read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
92 }
93
94 static inline void ct_read_unlock(unsigned int key)
95 {
96         read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
97 }
98
99 static inline void ct_write_lock(unsigned int key)
100 {
101         write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
102 }
103
104 static inline void ct_write_unlock(unsigned int key)
105 {
106         write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
107 }
108
109 static inline void ct_read_lock_bh(unsigned int key)
110 {
111         read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
112 }
113
114 static inline void ct_read_unlock_bh(unsigned int key)
115 {
116         read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
117 }
118
119 static inline void ct_write_lock_bh(unsigned int key)
120 {
121         write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
122 }
123
124 static inline void ct_write_unlock_bh(unsigned int key)
125 {
126         write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
127 }
128
129
130 /*
131  *      Returns hash value for IPVS connection entry
132  */
133 static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto,
134                                        const union nf_inet_addr *addr,
135                                        __be16 port)
136 {
137 #ifdef CONFIG_IP_VS_IPV6
138         if (af == AF_INET6)
139                 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
140                                     (__force u32)port, proto, ip_vs_conn_rnd) ^
141                         ((size_t)net>>8)) & ip_vs_conn_tab_mask;
142 #endif
143         return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
144                             ip_vs_conn_rnd) ^
145                 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
146 }
147
148 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
149                                              bool inverse)
150 {
151         const union nf_inet_addr *addr;
152         __be16 port;
153
154         if (p->pe_data && p->pe->hashkey_raw)
155                 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
156                         ip_vs_conn_tab_mask;
157
158         if (likely(!inverse)) {
159                 addr = p->caddr;
160                 port = p->cport;
161         } else {
162                 addr = p->vaddr;
163                 port = p->vport;
164         }
165
166         return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port);
167 }
168
169 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
170 {
171         struct ip_vs_conn_param p;
172
173         ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol,
174                               &cp->caddr, cp->cport, NULL, 0, &p);
175
176         if (cp->pe) {
177                 p.pe = cp->pe;
178                 p.pe_data = cp->pe_data;
179                 p.pe_data_len = cp->pe_data_len;
180         }
181
182         return ip_vs_conn_hashkey_param(&p, false);
183 }
184
185 /*
186  *      Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
187  *      returns bool success.
188  */
189 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
190 {
191         unsigned int hash;
192         int ret;
193
194         if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
195                 return 0;
196
197         /* Hash by protocol, client address and port */
198         hash = ip_vs_conn_hashkey_conn(cp);
199
200         ct_write_lock(hash);
201         spin_lock(&cp->lock);
202
203         if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
204                 hlist_add_head(&cp->c_list, &ip_vs_conn_tab[hash]);
205                 cp->flags |= IP_VS_CONN_F_HASHED;
206                 atomic_inc(&cp->refcnt);
207                 ret = 1;
208         } else {
209                 pr_err("%s(): request for already hashed, called from %pF\n",
210                        __func__, __builtin_return_address(0));
211                 ret = 0;
212         }
213
214         spin_unlock(&cp->lock);
215         ct_write_unlock(hash);
216
217         return ret;
218 }
219
220
221 /*
222  *      UNhashes ip_vs_conn from ip_vs_conn_tab.
223  *      returns bool success.
224  */
225 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
226 {
227         unsigned int hash;
228         int ret;
229
230         /* unhash it and decrease its reference counter */
231         hash = ip_vs_conn_hashkey_conn(cp);
232
233         ct_write_lock(hash);
234         spin_lock(&cp->lock);
235
236         if (cp->flags & IP_VS_CONN_F_HASHED) {
237                 hlist_del(&cp->c_list);
238                 cp->flags &= ~IP_VS_CONN_F_HASHED;
239                 atomic_dec(&cp->refcnt);
240                 ret = 1;
241         } else
242                 ret = 0;
243
244         spin_unlock(&cp->lock);
245         ct_write_unlock(hash);
246
247         return ret;
248 }
249
250
251 /*
252  *  Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
253  *  Called for pkts coming from OUTside-to-INside.
254  *      p->caddr, p->cport: pkt source address (foreign host)
255  *      p->vaddr, p->vport: pkt dest address (load balancer)
256  */
257 static inline struct ip_vs_conn *
258 __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
259 {
260         unsigned int hash;
261         struct ip_vs_conn *cp;
262
263         hash = ip_vs_conn_hashkey_param(p, false);
264
265         ct_read_lock(hash);
266
267         hlist_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
268                 if (cp->af == p->af &&
269                     p->cport == cp->cport && p->vport == cp->vport &&
270                     ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
271                     ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
272                     ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
273                     p->protocol == cp->protocol &&
274                     ip_vs_conn_net_eq(cp, p->net)) {
275                         /* HIT */
276                         atomic_inc(&cp->refcnt);
277                         ct_read_unlock(hash);
278                         return cp;
279                 }
280         }
281
282         ct_read_unlock(hash);
283
284         return NULL;
285 }
286
287 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
288 {
289         struct ip_vs_conn *cp;
290
291         cp = __ip_vs_conn_in_get(p);
292         if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
293                 struct ip_vs_conn_param cport_zero_p = *p;
294                 cport_zero_p.cport = 0;
295                 cp = __ip_vs_conn_in_get(&cport_zero_p);
296         }
297
298         IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
299                       ip_vs_proto_name(p->protocol),
300                       IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
301                       IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
302                       cp ? "hit" : "not hit");
303
304         return cp;
305 }
306
307 static int
308 ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
309                             const struct ip_vs_iphdr *iph,
310                             int inverse, struct ip_vs_conn_param *p)
311 {
312         __be16 _ports[2], *pptr;
313         struct net *net = skb_net(skb);
314
315         pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
316         if (pptr == NULL)
317                 return 1;
318
319         if (likely(!inverse))
320                 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr,
321                                       pptr[0], &iph->daddr, pptr[1], p);
322         else
323                 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr,
324                                       pptr[1], &iph->saddr, pptr[0], p);
325         return 0;
326 }
327
328 struct ip_vs_conn *
329 ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
330                         const struct ip_vs_iphdr *iph, int inverse)
331 {
332         struct ip_vs_conn_param p;
333
334         if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
335                 return NULL;
336
337         return ip_vs_conn_in_get(&p);
338 }
339 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
340
341 /* Get reference to connection template */
342 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
343 {
344         unsigned int hash;
345         struct ip_vs_conn *cp;
346
347         hash = ip_vs_conn_hashkey_param(p, false);
348
349         ct_read_lock(hash);
350
351         hlist_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
352                 if (!ip_vs_conn_net_eq(cp, p->net))
353                         continue;
354                 if (p->pe_data && p->pe->ct_match) {
355                         if (p->pe == cp->pe && p->pe->ct_match(p, cp))
356                                 goto out;
357                         continue;
358                 }
359
360                 if (cp->af == p->af &&
361                     ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
362                     /* protocol should only be IPPROTO_IP if
363                      * p->vaddr is a fwmark */
364                     ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
365                                      p->af, p->vaddr, &cp->vaddr) &&
366                     p->cport == cp->cport && p->vport == cp->vport &&
367                     cp->flags & IP_VS_CONN_F_TEMPLATE &&
368                     p->protocol == cp->protocol)
369                         goto out;
370         }
371         cp = NULL;
372
373   out:
374         if (cp)
375                 atomic_inc(&cp->refcnt);
376         ct_read_unlock(hash);
377
378         IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
379                       ip_vs_proto_name(p->protocol),
380                       IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
381                       IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
382                       cp ? "hit" : "not hit");
383
384         return cp;
385 }
386
387 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
388  * Called for pkts coming from inside-to-OUTside.
389  *      p->caddr, p->cport: pkt source address (inside host)
390  *      p->vaddr, p->vport: pkt dest address (foreign host) */
391 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
392 {
393         unsigned int hash;
394         struct ip_vs_conn *cp, *ret=NULL;
395
396         /*
397          *      Check for "full" addressed entries
398          */
399         hash = ip_vs_conn_hashkey_param(p, true);
400
401         ct_read_lock(hash);
402
403         hlist_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
404                 if (cp->af == p->af &&
405                     p->vport == cp->cport && p->cport == cp->dport &&
406                     ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
407                     ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
408                     p->protocol == cp->protocol &&
409                     ip_vs_conn_net_eq(cp, p->net)) {
410                         /* HIT */
411                         atomic_inc(&cp->refcnt);
412                         ret = cp;
413                         break;
414                 }
415         }
416
417         ct_read_unlock(hash);
418
419         IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
420                       ip_vs_proto_name(p->protocol),
421                       IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
422                       IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
423                       ret ? "hit" : "not hit");
424
425         return ret;
426 }
427
428 struct ip_vs_conn *
429 ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
430                          const struct ip_vs_iphdr *iph, int inverse)
431 {
432         struct ip_vs_conn_param p;
433
434         if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
435                 return NULL;
436
437         return ip_vs_conn_out_get(&p);
438 }
439 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
440
441 /*
442  *      Put back the conn and restart its timer with its timeout
443  */
444 void ip_vs_conn_put(struct ip_vs_conn *cp)
445 {
446         unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
447                 0 : cp->timeout;
448         mod_timer(&cp->timer, jiffies+t);
449
450         __ip_vs_conn_put(cp);
451 }
452
453
454 /*
455  *      Fill a no_client_port connection with a client port number
456  */
457 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
458 {
459         if (ip_vs_conn_unhash(cp)) {
460                 spin_lock(&cp->lock);
461                 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
462                         atomic_dec(&ip_vs_conn_no_cport_cnt);
463                         cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
464                         cp->cport = cport;
465                 }
466                 spin_unlock(&cp->lock);
467
468                 /* hash on new dport */
469                 ip_vs_conn_hash(cp);
470         }
471 }
472
473
474 /*
475  *      Bind a connection entry with the corresponding packet_xmit.
476  *      Called by ip_vs_conn_new.
477  */
478 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
479 {
480         switch (IP_VS_FWD_METHOD(cp)) {
481         case IP_VS_CONN_F_MASQ:
482                 cp->packet_xmit = ip_vs_nat_xmit;
483                 break;
484
485         case IP_VS_CONN_F_TUNNEL:
486                 cp->packet_xmit = ip_vs_tunnel_xmit;
487                 break;
488
489         case IP_VS_CONN_F_DROUTE:
490                 cp->packet_xmit = ip_vs_dr_xmit;
491                 break;
492
493         case IP_VS_CONN_F_LOCALNODE:
494                 cp->packet_xmit = ip_vs_null_xmit;
495                 break;
496
497         case IP_VS_CONN_F_BYPASS:
498                 cp->packet_xmit = ip_vs_bypass_xmit;
499                 break;
500         }
501 }
502
503 #ifdef CONFIG_IP_VS_IPV6
504 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
505 {
506         switch (IP_VS_FWD_METHOD(cp)) {
507         case IP_VS_CONN_F_MASQ:
508                 cp->packet_xmit = ip_vs_nat_xmit_v6;
509                 break;
510
511         case IP_VS_CONN_F_TUNNEL:
512                 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
513                 break;
514
515         case IP_VS_CONN_F_DROUTE:
516                 cp->packet_xmit = ip_vs_dr_xmit_v6;
517                 break;
518
519         case IP_VS_CONN_F_LOCALNODE:
520                 cp->packet_xmit = ip_vs_null_xmit;
521                 break;
522
523         case IP_VS_CONN_F_BYPASS:
524                 cp->packet_xmit = ip_vs_bypass_xmit_v6;
525                 break;
526         }
527 }
528 #endif
529
530
531 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
532 {
533         return atomic_read(&dest->activeconns)
534                 + atomic_read(&dest->inactconns);
535 }
536
537 /*
538  *      Bind a connection entry with a virtual service destination
539  *      Called just after a new connection entry is created.
540  */
541 static inline void
542 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
543 {
544         unsigned int conn_flags;
545         __u32 flags;
546
547         /* if dest is NULL, then return directly */
548         if (!dest)
549                 return;
550
551         /* Increase the refcnt counter of the dest */
552         atomic_inc(&dest->refcnt);
553
554         conn_flags = atomic_read(&dest->conn_flags);
555         if (cp->protocol != IPPROTO_UDP)
556                 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
557         flags = cp->flags;
558         /* Bind with the destination and its corresponding transmitter */
559         if (flags & IP_VS_CONN_F_SYNC) {
560                 /* if the connection is not template and is created
561                  * by sync, preserve the activity flag.
562                  */
563                 if (!(flags & IP_VS_CONN_F_TEMPLATE))
564                         conn_flags &= ~IP_VS_CONN_F_INACTIVE;
565                 /* connections inherit forwarding method from dest */
566                 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
567         }
568         flags |= conn_flags;
569         cp->flags = flags;
570         cp->dest = dest;
571
572         IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
573                       "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
574                       "dest->refcnt:%d\n",
575                       ip_vs_proto_name(cp->protocol),
576                       IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
577                       IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
578                       IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
579                       ip_vs_fwd_tag(cp), cp->state,
580                       cp->flags, atomic_read(&cp->refcnt),
581                       atomic_read(&dest->refcnt));
582
583         /* Update the connection counters */
584         if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
585                 /* It is a normal connection, so modify the counters
586                  * according to the flags, later the protocol can
587                  * update them on state change
588                  */
589                 if (!(flags & IP_VS_CONN_F_INACTIVE))
590                         atomic_inc(&dest->activeconns);
591                 else
592                         atomic_inc(&dest->inactconns);
593         } else {
594                 /* It is a persistent connection/template, so increase
595                    the persistent connection counter */
596                 atomic_inc(&dest->persistconns);
597         }
598
599         if (dest->u_threshold != 0 &&
600             ip_vs_dest_totalconns(dest) >= dest->u_threshold)
601                 dest->flags |= IP_VS_DEST_F_OVERLOAD;
602 }
603
604
605 /*
606  * Check if there is a destination for the connection, if so
607  * bind the connection to the destination.
608  */
609 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
610 {
611         struct ip_vs_dest *dest;
612
613         dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr,
614                                cp->dport, &cp->vaddr, cp->vport,
615                                cp->protocol, cp->fwmark, cp->flags);
616         if (dest) {
617                 struct ip_vs_proto_data *pd;
618
619                 spin_lock(&cp->lock);
620                 if (cp->dest) {
621                         spin_unlock(&cp->lock);
622                         return dest;
623                 }
624
625                 /* Applications work depending on the forwarding method
626                  * but better to reassign them always when binding dest */
627                 if (cp->app)
628                         ip_vs_unbind_app(cp);
629
630                 ip_vs_bind_dest(cp, dest);
631                 spin_unlock(&cp->lock);
632
633                 /* Update its packet transmitter */
634                 cp->packet_xmit = NULL;
635 #ifdef CONFIG_IP_VS_IPV6
636                 if (cp->af == AF_INET6)
637                         ip_vs_bind_xmit_v6(cp);
638                 else
639 #endif
640                         ip_vs_bind_xmit(cp);
641
642                 pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol);
643                 if (pd && atomic_read(&pd->appcnt))
644                         ip_vs_bind_app(cp, pd->pp);
645         }
646         return dest;
647 }
648
649
650 /*
651  *      Unbind a connection entry with its VS destination
652  *      Called by the ip_vs_conn_expire function.
653  */
654 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
655 {
656         struct ip_vs_dest *dest = cp->dest;
657
658         if (!dest)
659                 return;
660
661         IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
662                       "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
663                       "dest->refcnt:%d\n",
664                       ip_vs_proto_name(cp->protocol),
665                       IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
666                       IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
667                       IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
668                       ip_vs_fwd_tag(cp), cp->state,
669                       cp->flags, atomic_read(&cp->refcnt),
670                       atomic_read(&dest->refcnt));
671
672         /* Update the connection counters */
673         if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
674                 /* It is a normal connection, so decrease the inactconns
675                    or activeconns counter */
676                 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
677                         atomic_dec(&dest->inactconns);
678                 } else {
679                         atomic_dec(&dest->activeconns);
680                 }
681         } else {
682                 /* It is a persistent connection/template, so decrease
683                    the persistent connection counter */
684                 atomic_dec(&dest->persistconns);
685         }
686
687         if (dest->l_threshold != 0) {
688                 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
689                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
690         } else if (dest->u_threshold != 0) {
691                 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
692                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
693         } else {
694                 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
695                         dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
696         }
697
698         /*
699          * Simply decrease the refcnt of the dest, because the
700          * dest will be either in service's destination list
701          * or in the trash.
702          */
703         atomic_dec(&dest->refcnt);
704 }
705
706 static int expire_quiescent_template(struct netns_ipvs *ipvs,
707                                      struct ip_vs_dest *dest)
708 {
709 #ifdef CONFIG_SYSCTL
710         return ipvs->sysctl_expire_quiescent_template &&
711                 (atomic_read(&dest->weight) == 0);
712 #else
713         return 0;
714 #endif
715 }
716
717 /*
718  *      Checking if the destination of a connection template is available.
719  *      If available, return 1, otherwise invalidate this connection
720  *      template and return 0.
721  */
722 int ip_vs_check_template(struct ip_vs_conn *ct)
723 {
724         struct ip_vs_dest *dest = ct->dest;
725         struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct));
726
727         /*
728          * Checking the dest server status.
729          */
730         if ((dest == NULL) ||
731             !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
732             expire_quiescent_template(ipvs, dest)) {
733                 IP_VS_DBG_BUF(9, "check_template: dest not available for "
734                               "protocol %s s:%s:%d v:%s:%d "
735                               "-> d:%s:%d\n",
736                               ip_vs_proto_name(ct->protocol),
737                               IP_VS_DBG_ADDR(ct->af, &ct->caddr),
738                               ntohs(ct->cport),
739                               IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
740                               ntohs(ct->vport),
741                               IP_VS_DBG_ADDR(ct->af, &ct->daddr),
742                               ntohs(ct->dport));
743
744                 /*
745                  * Invalidate the connection template
746                  */
747                 if (ct->vport != htons(0xffff)) {
748                         if (ip_vs_conn_unhash(ct)) {
749                                 ct->dport = htons(0xffff);
750                                 ct->vport = htons(0xffff);
751                                 ct->cport = 0;
752                                 ip_vs_conn_hash(ct);
753                         }
754                 }
755
756                 /*
757                  * Simply decrease the refcnt of the template,
758                  * don't restart its timer.
759                  */
760                 atomic_dec(&ct->refcnt);
761                 return 0;
762         }
763         return 1;
764 }
765
766 static void ip_vs_conn_expire(unsigned long data)
767 {
768         struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
769         struct net *net = ip_vs_conn_net(cp);
770         struct netns_ipvs *ipvs = net_ipvs(net);
771
772         cp->timeout = 60*HZ;
773
774         /*
775          *      hey, I'm using it
776          */
777         atomic_inc(&cp->refcnt);
778
779         /*
780          *      do I control anybody?
781          */
782         if (atomic_read(&cp->n_control))
783                 goto expire_later;
784
785         /*
786          *      unhash it if it is hashed in the conn table
787          */
788         if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
789                 goto expire_later;
790
791         /*
792          *      refcnt==1 implies I'm the only one referrer
793          */
794         if (likely(atomic_read(&cp->refcnt) == 1)) {
795                 /* delete the timer if it is activated by other users */
796                 del_timer(&cp->timer);
797
798                 /* does anybody control me? */
799                 if (cp->control)
800                         ip_vs_control_del(cp);
801
802                 if (cp->flags & IP_VS_CONN_F_NFCT) {
803                         ip_vs_conn_drop_conntrack(cp);
804                         /* Do not access conntracks during subsys cleanup
805                          * because nf_conntrack_find_get can not be used after
806                          * conntrack cleanup for the net.
807                          */
808                         smp_rmb();
809                         if (ipvs->enable)
810                                 ip_vs_conn_drop_conntrack(cp);
811                 }
812
813                 ip_vs_pe_put(cp->pe);
814                 kfree(cp->pe_data);
815                 if (unlikely(cp->app != NULL))
816                         ip_vs_unbind_app(cp);
817                 ip_vs_unbind_dest(cp);
818                 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
819                         atomic_dec(&ip_vs_conn_no_cport_cnt);
820                 atomic_dec(&ipvs->conn_count);
821
822                 kmem_cache_free(ip_vs_conn_cachep, cp);
823                 return;
824         }
825
826         /* hash it back to the table */
827         ip_vs_conn_hash(cp);
828
829   expire_later:
830         IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
831                   atomic_read(&cp->refcnt)-1,
832                   atomic_read(&cp->n_control));
833
834         if (ipvs->sync_state & IP_VS_STATE_MASTER)
835                 ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs));
836
837         ip_vs_conn_put(cp);
838 }
839
840
841 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
842 {
843         if (del_timer(&cp->timer))
844                 mod_timer(&cp->timer, jiffies);
845 }
846
847
848 /*
849  *      Create a new connection entry and hash it into the ip_vs_conn_tab
850  */
851 struct ip_vs_conn *
852 ip_vs_conn_new(const struct ip_vs_conn_param *p,
853                const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
854                struct ip_vs_dest *dest, __u32 fwmark)
855 {
856         struct ip_vs_conn *cp;
857         struct netns_ipvs *ipvs = net_ipvs(p->net);
858         struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net,
859                                                            p->protocol);
860
861         cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
862         if (cp == NULL) {
863                 IP_VS_ERR_RL("%s(): no memory\n", __func__);
864                 return NULL;
865         }
866
867         INIT_HLIST_NODE(&cp->c_list);
868         setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
869         ip_vs_conn_net_set(cp, p->net);
870         cp->af             = p->af;
871         cp->protocol       = p->protocol;
872         ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
873         cp->cport          = p->cport;
874         ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
875         cp->vport          = p->vport;
876         /* proto should only be IPPROTO_IP if d_addr is a fwmark */
877         ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
878                         &cp->daddr, daddr);
879         cp->dport          = dport;
880         cp->flags          = flags;
881         cp->fwmark         = fwmark;
882         if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
883                 ip_vs_pe_get(p->pe);
884                 cp->pe = p->pe;
885                 cp->pe_data = p->pe_data;
886                 cp->pe_data_len = p->pe_data_len;
887         }
888         spin_lock_init(&cp->lock);
889
890         /*
891          * Set the entry is referenced by the current thread before hashing
892          * it in the table, so that other thread run ip_vs_random_dropentry
893          * but cannot drop this entry.
894          */
895         atomic_set(&cp->refcnt, 1);
896
897         atomic_set(&cp->n_control, 0);
898         atomic_set(&cp->in_pkts, 0);
899
900         atomic_inc(&ipvs->conn_count);
901         if (flags & IP_VS_CONN_F_NO_CPORT)
902                 atomic_inc(&ip_vs_conn_no_cport_cnt);
903
904         /* Bind the connection with a destination server */
905         ip_vs_bind_dest(cp, dest);
906
907         /* Set its state and timeout */
908         cp->state = 0;
909         cp->timeout = 3*HZ;
910         cp->sync_endtime = jiffies & ~3UL;
911
912         /* Bind its packet transmitter */
913 #ifdef CONFIG_IP_VS_IPV6
914         if (p->af == AF_INET6)
915                 ip_vs_bind_xmit_v6(cp);
916         else
917 #endif
918                 ip_vs_bind_xmit(cp);
919
920         if (unlikely(pd && atomic_read(&pd->appcnt)))
921                 ip_vs_bind_app(cp, pd->pp);
922
923         /*
924          * Allow conntrack to be preserved. By default, conntrack
925          * is created and destroyed for every packet.
926          * Sometimes keeping conntrack can be useful for
927          * IP_VS_CONN_F_ONE_PACKET too.
928          */
929
930         if (ip_vs_conntrack_enabled(ipvs))
931                 cp->flags |= IP_VS_CONN_F_NFCT;
932
933         /* Hash it in the ip_vs_conn_tab finally */
934         ip_vs_conn_hash(cp);
935
936         return cp;
937 }
938
939 /*
940  *      /proc/net/ip_vs_conn entries
941  */
942 #ifdef CONFIG_PROC_FS
943 struct ip_vs_iter_state {
944         struct seq_net_private  p;
945         struct hlist_head       *l;
946 };
947
948 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
949 {
950         int idx;
951         struct ip_vs_conn *cp;
952         struct ip_vs_iter_state *iter = seq->private;
953
954         for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
955                 ct_read_lock_bh(idx);
956                 hlist_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
957                         if (pos-- == 0) {
958                                 iter->l = &ip_vs_conn_tab[idx];
959                                 return cp;
960                         }
961                 }
962                 ct_read_unlock_bh(idx);
963         }
964
965         return NULL;
966 }
967
968 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
969 {
970         struct ip_vs_iter_state *iter = seq->private;
971
972         iter->l = NULL;
973         return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
974 }
975
976 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
977 {
978         struct ip_vs_conn *cp = v;
979         struct ip_vs_iter_state *iter = seq->private;
980         struct hlist_head *l = iter->l;
981         int idx;
982
983         ++*pos;
984         if (v == SEQ_START_TOKEN)
985                 return ip_vs_conn_array(seq, 0);
986
987         /* more on same hash chain? */
988         if (cp->c_list.next)
989                 return hlist_entry(cp->c_list.next, struct ip_vs_conn, c_list);
990
991         idx = l - ip_vs_conn_tab;
992         ct_read_unlock_bh(idx);
993
994         while (++idx < ip_vs_conn_tab_size) {
995                 ct_read_lock_bh(idx);
996                 hlist_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
997                         iter->l = &ip_vs_conn_tab[idx];
998                         return cp;
999                 }
1000                 ct_read_unlock_bh(idx);
1001         }
1002         iter->l = NULL;
1003         return NULL;
1004 }
1005
1006 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1007 {
1008         struct ip_vs_iter_state *iter = seq->private;
1009         struct hlist_head *l = iter->l;
1010
1011         if (l)
1012                 ct_read_unlock_bh(l - ip_vs_conn_tab);
1013 }
1014
1015 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1016 {
1017
1018         if (v == SEQ_START_TOKEN)
1019                 seq_puts(seq,
1020    "Pro FromIP   FPrt ToIP     TPrt DestIP   DPrt State       Expires PEName PEData\n");
1021         else {
1022                 const struct ip_vs_conn *cp = v;
1023                 struct net *net = seq_file_net(seq);
1024                 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1025                 size_t len = 0;
1026
1027                 if (!ip_vs_conn_net_eq(cp, net))
1028                         return 0;
1029                 if (cp->pe_data) {
1030                         pe_data[0] = ' ';
1031                         len = strlen(cp->pe->name);
1032                         memcpy(pe_data + 1, cp->pe->name, len);
1033                         pe_data[len + 1] = ' ';
1034                         len += 2;
1035                         len += cp->pe->show_pe_data(cp, pe_data + len);
1036                 }
1037                 pe_data[len] = '\0';
1038
1039 #ifdef CONFIG_IP_VS_IPV6
1040                 if (cp->af == AF_INET6)
1041                         seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1042                                 "%pI6 %04X %-11s %7lu%s\n",
1043                                 ip_vs_proto_name(cp->protocol),
1044                                 &cp->caddr.in6, ntohs(cp->cport),
1045                                 &cp->vaddr.in6, ntohs(cp->vport),
1046                                 &cp->daddr.in6, ntohs(cp->dport),
1047                                 ip_vs_state_name(cp->protocol, cp->state),
1048                                 (cp->timer.expires-jiffies)/HZ, pe_data);
1049                 else
1050 #endif
1051                         seq_printf(seq,
1052                                 "%-3s %08X %04X %08X %04X"
1053                                 " %08X %04X %-11s %7lu%s\n",
1054                                 ip_vs_proto_name(cp->protocol),
1055                                 ntohl(cp->caddr.ip), ntohs(cp->cport),
1056                                 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1057                                 ntohl(cp->daddr.ip), ntohs(cp->dport),
1058                                 ip_vs_state_name(cp->protocol, cp->state),
1059                                 (cp->timer.expires-jiffies)/HZ, pe_data);
1060         }
1061         return 0;
1062 }
1063
1064 static const struct seq_operations ip_vs_conn_seq_ops = {
1065         .start = ip_vs_conn_seq_start,
1066         .next  = ip_vs_conn_seq_next,
1067         .stop  = ip_vs_conn_seq_stop,
1068         .show  = ip_vs_conn_seq_show,
1069 };
1070
1071 static int ip_vs_conn_open(struct inode *inode, struct file *file)
1072 {
1073         return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1074                             sizeof(struct ip_vs_iter_state));
1075 }
1076
1077 static const struct file_operations ip_vs_conn_fops = {
1078         .owner   = THIS_MODULE,
1079         .open    = ip_vs_conn_open,
1080         .read    = seq_read,
1081         .llseek  = seq_lseek,
1082         .release = seq_release_net,
1083 };
1084
1085 static const char *ip_vs_origin_name(unsigned int flags)
1086 {
1087         if (flags & IP_VS_CONN_F_SYNC)
1088                 return "SYNC";
1089         else
1090                 return "LOCAL";
1091 }
1092
1093 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1094 {
1095
1096         if (v == SEQ_START_TOKEN)
1097                 seq_puts(seq,
1098    "Pro FromIP   FPrt ToIP     TPrt DestIP   DPrt State       Origin Expires\n");
1099         else {
1100                 const struct ip_vs_conn *cp = v;
1101                 struct net *net = seq_file_net(seq);
1102
1103                 if (!ip_vs_conn_net_eq(cp, net))
1104                         return 0;
1105
1106 #ifdef CONFIG_IP_VS_IPV6
1107                 if (cp->af == AF_INET6)
1108                         seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
1109                                 ip_vs_proto_name(cp->protocol),
1110                                 &cp->caddr.in6, ntohs(cp->cport),
1111                                 &cp->vaddr.in6, ntohs(cp->vport),
1112                                 &cp->daddr.in6, ntohs(cp->dport),
1113                                 ip_vs_state_name(cp->protocol, cp->state),
1114                                 ip_vs_origin_name(cp->flags),
1115                                 (cp->timer.expires-jiffies)/HZ);
1116                 else
1117 #endif
1118                         seq_printf(seq,
1119                                 "%-3s %08X %04X %08X %04X "
1120                                 "%08X %04X %-11s %-6s %7lu\n",
1121                                 ip_vs_proto_name(cp->protocol),
1122                                 ntohl(cp->caddr.ip), ntohs(cp->cport),
1123                                 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1124                                 ntohl(cp->daddr.ip), ntohs(cp->dport),
1125                                 ip_vs_state_name(cp->protocol, cp->state),
1126                                 ip_vs_origin_name(cp->flags),
1127                                 (cp->timer.expires-jiffies)/HZ);
1128         }
1129         return 0;
1130 }
1131
1132 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1133         .start = ip_vs_conn_seq_start,
1134         .next  = ip_vs_conn_seq_next,
1135         .stop  = ip_vs_conn_seq_stop,
1136         .show  = ip_vs_conn_sync_seq_show,
1137 };
1138
1139 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1140 {
1141         return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1142                             sizeof(struct ip_vs_iter_state));
1143 }
1144
1145 static const struct file_operations ip_vs_conn_sync_fops = {
1146         .owner   = THIS_MODULE,
1147         .open    = ip_vs_conn_sync_open,
1148         .read    = seq_read,
1149         .llseek  = seq_lseek,
1150         .release = seq_release_net,
1151 };
1152
1153 #endif
1154
1155
1156 /*
1157  *      Randomly drop connection entries before running out of memory
1158  */
1159 static inline int todrop_entry(struct ip_vs_conn *cp)
1160 {
1161         /*
1162          * The drop rate array needs tuning for real environments.
1163          * Called from timer bh only => no locking
1164          */
1165         static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1166         static char todrop_counter[9] = {0};
1167         int i;
1168
1169         /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1170            This will leave enough time for normal connection to get
1171            through. */
1172         if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1173                 return 0;
1174
1175         /* Don't drop the entry if its number of incoming packets is not
1176            located in [0, 8] */
1177         i = atomic_read(&cp->in_pkts);
1178         if (i > 8 || i < 0) return 0;
1179
1180         if (!todrop_rate[i]) return 0;
1181         if (--todrop_counter[i] > 0) return 0;
1182
1183         todrop_counter[i] = todrop_rate[i];
1184         return 1;
1185 }
1186
1187 /* Called from keventd and must protect itself from softirqs */
1188 void ip_vs_random_dropentry(struct net *net)
1189 {
1190         int idx;
1191         struct ip_vs_conn *cp;
1192
1193         /*
1194          * Randomly scan 1/32 of the whole table every second
1195          */
1196         for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1197                 unsigned int hash = net_random() & ip_vs_conn_tab_mask;
1198
1199                 /*
1200                  *  Lock is actually needed in this loop.
1201                  */
1202                 ct_write_lock_bh(hash);
1203
1204                 hlist_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
1205                         if (cp->flags & IP_VS_CONN_F_TEMPLATE)
1206                                 /* connection template */
1207                                 continue;
1208                         if (!ip_vs_conn_net_eq(cp, net))
1209                                 continue;
1210                         if (cp->protocol == IPPROTO_TCP) {
1211                                 switch(cp->state) {
1212                                 case IP_VS_TCP_S_SYN_RECV:
1213                                 case IP_VS_TCP_S_SYNACK:
1214                                         break;
1215
1216                                 case IP_VS_TCP_S_ESTABLISHED:
1217                                         if (todrop_entry(cp))
1218                                                 break;
1219                                         continue;
1220
1221                                 default:
1222                                         continue;
1223                                 }
1224                         } else {
1225                                 if (!todrop_entry(cp))
1226                                         continue;
1227                         }
1228
1229                         IP_VS_DBG(4, "del connection\n");
1230                         ip_vs_conn_expire_now(cp);
1231                         if (cp->control) {
1232                                 IP_VS_DBG(4, "del conn template\n");
1233                                 ip_vs_conn_expire_now(cp->control);
1234                         }
1235                 }
1236                 ct_write_unlock_bh(hash);
1237         }
1238 }
1239
1240
1241 /*
1242  *      Flush all the connection entries in the ip_vs_conn_tab
1243  */
1244 static void ip_vs_conn_flush(struct net *net)
1245 {
1246         int idx;
1247         struct ip_vs_conn *cp;
1248         struct netns_ipvs *ipvs = net_ipvs(net);
1249
1250 flush_again:
1251         for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1252                 /*
1253                  *  Lock is actually needed in this loop.
1254                  */
1255                 ct_write_lock_bh(idx);
1256
1257                 hlist_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1258                         if (!ip_vs_conn_net_eq(cp, net))
1259                                 continue;
1260                         IP_VS_DBG(4, "del connection\n");
1261                         ip_vs_conn_expire_now(cp);
1262                         if (cp->control) {
1263                                 IP_VS_DBG(4, "del conn template\n");
1264                                 ip_vs_conn_expire_now(cp->control);
1265                         }
1266                 }
1267                 ct_write_unlock_bh(idx);
1268         }
1269
1270         /* the counter may be not NULL, because maybe some conn entries
1271            are run by slow timer handler or unhashed but still referred */
1272         if (atomic_read(&ipvs->conn_count) != 0) {
1273                 schedule();
1274                 goto flush_again;
1275         }
1276 }
1277 /*
1278  * per netns init and exit
1279  */
1280 int __net_init ip_vs_conn_net_init(struct net *net)
1281 {
1282         struct netns_ipvs *ipvs = net_ipvs(net);
1283
1284         atomic_set(&ipvs->conn_count, 0);
1285
1286         proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1287         proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
1288         return 0;
1289 }
1290
1291 void __net_exit ip_vs_conn_net_cleanup(struct net *net)
1292 {
1293         /* flush all the connection entries first */
1294         ip_vs_conn_flush(net);
1295         remove_proc_entry("ip_vs_conn", net->proc_net);
1296         remove_proc_entry("ip_vs_conn_sync", net->proc_net);
1297 }
1298
1299 int __init ip_vs_conn_init(void)
1300 {
1301         int idx;
1302
1303         /* Compute size and mask */
1304         ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1305         ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1306
1307         /*
1308          * Allocate the connection hash table and initialize its list heads
1309          */
1310         ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
1311         if (!ip_vs_conn_tab)
1312                 return -ENOMEM;
1313
1314         /* Allocate ip_vs_conn slab cache */
1315         ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1316                                               sizeof(struct ip_vs_conn), 0,
1317                                               SLAB_HWCACHE_ALIGN, NULL);
1318         if (!ip_vs_conn_cachep) {
1319                 vfree(ip_vs_conn_tab);
1320                 return -ENOMEM;
1321         }
1322
1323         pr_info("Connection hash table configured "
1324                 "(size=%d, memory=%ldKbytes)\n",
1325                 ip_vs_conn_tab_size,
1326                 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
1327         IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1328                   sizeof(struct ip_vs_conn));
1329
1330         for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1331                 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
1332
1333         for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++)  {
1334                 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1335         }
1336
1337         /* calculate the random value for connection hash */
1338         get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1339
1340         return 0;
1341 }
1342
1343 void ip_vs_conn_cleanup(void)
1344 {
1345         /* Release the empty cache */
1346         kmem_cache_destroy(ip_vs_conn_cachep);
1347         vfree(ip_vs_conn_tab);
1348 }