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