Merge tag 'vfio-v4.5-rc7' of git://github.com/awilliam/linux-vfio
[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 */
754b81a3 111static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, 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) ^
754b81a3 119 ((size_t)ipvs>>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) ^
754b81a3 123 ((size_t)ipvs>>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
754b81a3 144 return ip_vs_conn_hashkey(p->ipvs, 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 316static int
f5099dd4
EB
317ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
318 int af, const struct sk_buff *skb,
f11017ec 319 const struct ip_vs_iphdr *iph,
802c41ad 320 struct ip_vs_conn_param *p)
f11017ec
SH
321{
322 __be16 _ports[2], *pptr;
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 337struct ip_vs_conn *
ab161976
EB
338ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
339 const struct sk_buff *skb,
802c41ad 340 const struct ip_vs_iphdr *iph)
5c0d2374 341{
f11017ec 342 struct ip_vs_conn_param p;
5c0d2374 343
f5099dd4 344 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
5c0d2374
SH
345 return NULL;
346
f11017ec 347 return ip_vs_conn_in_get(&p);
5c0d2374
SH
348}
349EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
350
87375ab4 351/* Get reference to connection template */
f11017ec 352struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
87375ab4 353{
95c96174 354 unsigned int hash;
87375ab4
JA
355 struct ip_vs_conn *cp;
356
85999283 357 hash = ip_vs_conn_hashkey_param(p, false);
87375ab4 358
088339a5 359 rcu_read_lock();
87375ab4 360
088339a5 361 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1845ed0b 362 if (unlikely(p->pe_data && p->pe->ct_match)) {
e64e2b46 363 if (cp->ipvs != p->ipvs)
1845ed0b 364 continue;
088339a5
JA
365 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
366 if (__ip_vs_conn_get(cp))
367 goto out;
368 }
85999283
SH
369 continue;
370 }
371
f11017ec
SH
372 if (cp->af == p->af &&
373 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
be8be9ec 374 /* protocol should only be IPPROTO_IP if
f11017ec
SH
375 * p->vaddr is a fwmark */
376 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
377 p->af, p->vaddr, &cp->vaddr) &&
1845ed0b 378 p->vport == cp->vport && p->cport == cp->cport &&
87375ab4 379 cp->flags & IP_VS_CONN_F_TEMPLATE &&
1845ed0b 380 p->protocol == cp->protocol &&
e64e2b46 381 cp->ipvs == p->ipvs) {
088339a5
JA
382 if (__ip_vs_conn_get(cp))
383 goto out;
384 }
87375ab4
JA
385 }
386 cp = NULL;
387
388 out:
088339a5 389 rcu_read_unlock();
87375ab4 390
28364a59 391 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
f11017ec
SH
392 ip_vs_proto_name(p->protocol),
393 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
394 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
28364a59 395 cp ? "hit" : "not hit");
87375ab4
JA
396
397 return cp;
398}
1da177e4 399
f11017ec
SH
400/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
401 * Called for pkts coming from inside-to-OUTside.
402 * p->caddr, p->cport: pkt source address (inside host)
403 * p->vaddr, p->vport: pkt dest address (foreign host) */
404struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
1da177e4 405{
95c96174 406 unsigned int hash;
1da177e4
LT
407 struct ip_vs_conn *cp, *ret=NULL;
408
409 /*
410 * Check for "full" addressed entries
411 */
85999283 412 hash = ip_vs_conn_hashkey_param(p, true);
1da177e4 413
088339a5 414 rcu_read_lock();
1da177e4 415
088339a5 416 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1845ed0b
JA
417 if (p->vport == cp->cport && p->cport == cp->dport &&
418 cp->af == p->af &&
f11017ec
SH
419 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
420 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
6e67e586 421 p->protocol == cp->protocol &&
e64e2b46 422 cp->ipvs == p->ipvs) {
088339a5
JA
423 if (!__ip_vs_conn_get(cp))
424 continue;
1da177e4 425 /* HIT */
1da177e4
LT
426 ret = cp;
427 break;
428 }
429 }
430
088339a5 431 rcu_read_unlock();
1da177e4 432
28364a59 433 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
f11017ec
SH
434 ip_vs_proto_name(p->protocol),
435 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
436 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
28364a59 437 ret ? "hit" : "not hit");
1da177e4
LT
438
439 return ret;
440}
441
5c0d2374 442struct ip_vs_conn *
0cf705c8
EB
443ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
444 const struct sk_buff *skb,
802c41ad 445 const struct ip_vs_iphdr *iph)
5c0d2374 446{
f11017ec 447 struct ip_vs_conn_param p;
5c0d2374 448
f5099dd4 449 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
5c0d2374
SH
450 return NULL;
451
f11017ec 452 return ip_vs_conn_out_get(&p);
5c0d2374
SH
453}
454EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
1da177e4
LT
455
456/*
457 * Put back the conn and restart its timer with its timeout
458 */
459void ip_vs_conn_put(struct ip_vs_conn *cp)
460{
26ec037f
NC
461 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
462 0 : cp->timeout;
463 mod_timer(&cp->timer, jiffies+t);
1da177e4
LT
464
465 __ip_vs_conn_put(cp);
466}
467
468
469/*
470 * Fill a no_client_port connection with a client port number
471 */
014d730d 472void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
1da177e4
LT
473{
474 if (ip_vs_conn_unhash(cp)) {
ac69269a 475 spin_lock_bh(&cp->lock);
1da177e4
LT
476 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
477 atomic_dec(&ip_vs_conn_no_cport_cnt);
478 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
479 cp->cport = cport;
480 }
ac69269a 481 spin_unlock_bh(&cp->lock);
1da177e4
LT
482
483 /* hash on new dport */
484 ip_vs_conn_hash(cp);
485 }
486}
487
488
489/*
490 * Bind a connection entry with the corresponding packet_xmit.
491 * Called by ip_vs_conn_new.
492 */
493static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
494{
495 switch (IP_VS_FWD_METHOD(cp)) {
496 case IP_VS_CONN_F_MASQ:
497 cp->packet_xmit = ip_vs_nat_xmit;
498 break;
499
500 case IP_VS_CONN_F_TUNNEL:
8052ba29
AG
501#ifdef CONFIG_IP_VS_IPV6
502 if (cp->daf == AF_INET6)
503 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
504 else
505#endif
506 cp->packet_xmit = ip_vs_tunnel_xmit;
1da177e4
LT
507 break;
508
509 case IP_VS_CONN_F_DROUTE:
510 cp->packet_xmit = ip_vs_dr_xmit;
511 break;
512
513 case IP_VS_CONN_F_LOCALNODE:
514 cp->packet_xmit = ip_vs_null_xmit;
515 break;
516
517 case IP_VS_CONN_F_BYPASS:
518 cp->packet_xmit = ip_vs_bypass_xmit;
519 break;
520 }
521}
522
b3cdd2a7
JV
523#ifdef CONFIG_IP_VS_IPV6
524static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
525{
526 switch (IP_VS_FWD_METHOD(cp)) {
527 case IP_VS_CONN_F_MASQ:
528 cp->packet_xmit = ip_vs_nat_xmit_v6;
529 break;
530
531 case IP_VS_CONN_F_TUNNEL:
8052ba29
AG
532 if (cp->daf == AF_INET6)
533 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
534 else
535 cp->packet_xmit = ip_vs_tunnel_xmit;
b3cdd2a7
JV
536 break;
537
538 case IP_VS_CONN_F_DROUTE:
539 cp->packet_xmit = ip_vs_dr_xmit_v6;
540 break;
541
542 case IP_VS_CONN_F_LOCALNODE:
543 cp->packet_xmit = ip_vs_null_xmit;
544 break;
545
546 case IP_VS_CONN_F_BYPASS:
547 cp->packet_xmit = ip_vs_bypass_xmit_v6;
548 break;
549 }
550}
551#endif
552
1da177e4
LT
553
554static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
555{
556 return atomic_read(&dest->activeconns)
557 + atomic_read(&dest->inactconns);
558}
559
560/*
561 * Bind a connection entry with a virtual service destination
562 * Called just after a new connection entry is created.
563 */
564static inline void
565ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
566{
3575792e 567 unsigned int conn_flags;
6b324dbf 568 __u32 flags;
3575792e 569
1da177e4
LT
570 /* if dest is NULL, then return directly */
571 if (!dest)
572 return;
573
574 /* Increase the refcnt counter of the dest */
fca9c20a 575 ip_vs_dest_hold(dest);
1da177e4 576
3575792e
JA
577 conn_flags = atomic_read(&dest->conn_flags);
578 if (cp->protocol != IPPROTO_UDP)
579 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
6b324dbf 580 flags = cp->flags;
1da177e4 581 /* Bind with the destination and its corresponding transmitter */
6b324dbf 582 if (flags & IP_VS_CONN_F_SYNC) {
b209639e
RB
583 /* if the connection is not template and is created
584 * by sync, preserve the activity flag.
585 */
6b324dbf 586 if (!(flags & IP_VS_CONN_F_TEMPLATE))
3575792e 587 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
3233759b 588 /* connections inherit forwarding method from dest */
6b324dbf 589 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
3575792e 590 }
6b324dbf
PNA
591 flags |= conn_flags;
592 cp->flags = flags;
1da177e4
LT
593 cp->dest = dest;
594
cfc78c5a
JV
595 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
596 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
597 "dest->refcnt:%d\n",
598 ip_vs_proto_name(cp->protocol),
599 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
600 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
f18ae720 601 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
cfc78c5a
JV
602 ip_vs_fwd_tag(cp), cp->state,
603 cp->flags, atomic_read(&cp->refcnt),
604 atomic_read(&dest->refcnt));
1da177e4
LT
605
606 /* Update the connection counters */
6b324dbf 607 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
06611f82
JA
608 /* It is a normal connection, so modify the counters
609 * according to the flags, later the protocol can
610 * update them on state change
611 */
6b324dbf 612 if (!(flags & IP_VS_CONN_F_INACTIVE))
b209639e
RB
613 atomic_inc(&dest->activeconns);
614 else
615 atomic_inc(&dest->inactconns);
1da177e4
LT
616 } else {
617 /* It is a persistent connection/template, so increase
25985edc 618 the persistent connection counter */
1da177e4
LT
619 atomic_inc(&dest->persistconns);
620 }
621
622 if (dest->u_threshold != 0 &&
623 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
624 dest->flags |= IP_VS_DEST_F_OVERLOAD;
625}
626
627
1e356f9c
RB
628/*
629 * Check if there is a destination for the connection, if so
630 * bind the connection to the destination.
631 */
413c2d04 632void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
1e356f9c
RB
633{
634 struct ip_vs_dest *dest;
635
413c2d04 636 rcu_read_lock();
655eef10
AG
637
638 /* This function is only invoked by the synchronization code. We do
639 * not currently support heterogeneous pools with synchronization,
640 * so we can make the assumption that the svc_af is the same as the
641 * dest_af
642 */
dc2add6f 643 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
882a844b
JA
644 cp->dport, &cp->vaddr, cp->vport,
645 cp->protocol, cp->fwmark, cp->flags);
646 if (dest) {
647 struct ip_vs_proto_data *pd;
648
ac69269a 649 spin_lock_bh(&cp->lock);
f73181c8 650 if (cp->dest) {
ac69269a 651 spin_unlock_bh(&cp->lock);
413c2d04
JA
652 rcu_read_unlock();
653 return;
f73181c8
PNA
654 }
655
882a844b
JA
656 /* Applications work depending on the forwarding method
657 * but better to reassign them always when binding dest */
658 if (cp->app)
659 ip_vs_unbind_app(cp);
660
1e356f9c 661 ip_vs_bind_dest(cp, dest);
ac69269a 662 spin_unlock_bh(&cp->lock);
882a844b
JA
663
664 /* Update its packet transmitter */
665 cp->packet_xmit = NULL;
666#ifdef CONFIG_IP_VS_IPV6
667 if (cp->af == AF_INET6)
668 ip_vs_bind_xmit_v6(cp);
669 else
670#endif
671 ip_vs_bind_xmit(cp);
672
18d6ade6 673 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
882a844b
JA
674 if (pd && atomic_read(&pd->appcnt))
675 ip_vs_bind_app(cp, pd->pp);
676 }
413c2d04 677 rcu_read_unlock();
1e356f9c 678}
1e356f9c
RB
679
680
1da177e4
LT
681/*
682 * Unbind a connection entry with its VS destination
683 * Called by the ip_vs_conn_expire function.
684 */
685static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
686{
687 struct ip_vs_dest *dest = cp->dest;
688
689 if (!dest)
690 return;
691
cfc78c5a
JV
692 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
693 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
694 "dest->refcnt:%d\n",
695 ip_vs_proto_name(cp->protocol),
696 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
697 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
f18ae720 698 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
cfc78c5a
JV
699 ip_vs_fwd_tag(cp), cp->state,
700 cp->flags, atomic_read(&cp->refcnt),
701 atomic_read(&dest->refcnt));
1da177e4
LT
702
703 /* Update the connection counters */
87375ab4 704 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
1da177e4
LT
705 /* It is a normal connection, so decrease the inactconns
706 or activeconns counter */
707 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
708 atomic_dec(&dest->inactconns);
709 } else {
710 atomic_dec(&dest->activeconns);
711 }
712 } else {
713 /* It is a persistent connection/template, so decrease
25985edc 714 the persistent connection counter */
1da177e4
LT
715 atomic_dec(&dest->persistconns);
716 }
717
718 if (dest->l_threshold != 0) {
719 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
720 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
721 } else if (dest->u_threshold != 0) {
722 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
723 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
724 } else {
725 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
726 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
727 }
728
fca9c20a 729 ip_vs_dest_put(dest);
1da177e4
LT
730}
731
8e1b0b1b
SH
732static int expire_quiescent_template(struct netns_ipvs *ipvs,
733 struct ip_vs_dest *dest)
734{
735#ifdef CONFIG_SYSCTL
736 return ipvs->sysctl_expire_quiescent_template &&
737 (atomic_read(&dest->weight) == 0);
738#else
739 return 0;
740#endif
741}
1da177e4
LT
742
743/*
744 * Checking if the destination of a connection template is available.
745 * If available, return 1, otherwise invalidate this connection
746 * template and return 0.
747 */
748int ip_vs_check_template(struct ip_vs_conn *ct)
749{
750 struct ip_vs_dest *dest = ct->dest;
58dbc6f2 751 struct netns_ipvs *ipvs = ct->ipvs;
1da177e4
LT
752
753 /*
754 * Checking the dest server status.
755 */
756 if ((dest == NULL) ||
e905a9ed 757 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
8e1b0b1b 758 expire_quiescent_template(ipvs, dest)) {
cfc78c5a
JV
759 IP_VS_DBG_BUF(9, "check_template: dest not available for "
760 "protocol %s s:%s:%d v:%s:%d "
761 "-> d:%s:%d\n",
762 ip_vs_proto_name(ct->protocol),
763 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
764 ntohs(ct->cport),
765 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
766 ntohs(ct->vport),
f18ae720 767 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
cfc78c5a 768 ntohs(ct->dport));
1da177e4
LT
769
770 /*
771 * Invalidate the connection template
772 */
014d730d 773 if (ct->vport != htons(0xffff)) {
1da177e4 774 if (ip_vs_conn_unhash(ct)) {
014d730d
AV
775 ct->dport = htons(0xffff);
776 ct->vport = htons(0xffff);
1da177e4
LT
777 ct->cport = 0;
778 ip_vs_conn_hash(ct);
779 }
780 }
781
782 /*
783 * Simply decrease the refcnt of the template,
784 * don't restart its timer.
785 */
088339a5 786 __ip_vs_conn_put(ct);
1da177e4
LT
787 return 0;
788 }
789 return 1;
790}
791
088339a5
JA
792static void ip_vs_conn_rcu_free(struct rcu_head *head)
793{
794 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
795 rcu_head);
796
797 ip_vs_pe_put(cp->pe);
798 kfree(cp->pe_data);
799 kmem_cache_free(ip_vs_conn_cachep, cp);
800}
801
1da177e4
LT
802static void ip_vs_conn_expire(unsigned long data)
803{
804 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
58dbc6f2 805 struct netns_ipvs *ipvs = cp->ipvs;
1da177e4 806
1da177e4
LT
807 /*
808 * do I control anybody?
809 */
810 if (atomic_read(&cp->n_control))
811 goto expire_later;
812
088339a5
JA
813 /* Unlink conn if not referenced anymore */
814 if (likely(ip_vs_conn_unlink(cp))) {
1da177e4 815 /* delete the timer if it is activated by other users */
25cc4ae9 816 del_timer(&cp->timer);
1da177e4
LT
817
818 /* does anybody control me? */
819 if (cp->control)
820 ip_vs_control_del(cp);
821
8f4e0a18 822 if (cp->flags & IP_VS_CONN_F_NFCT) {
8f4e0a18
HS
823 /* Do not access conntracks during subsys cleanup
824 * because nf_conntrack_find_get can not be used after
825 * conntrack cleanup for the net.
826 */
827 smp_rmb();
828 if (ipvs->enable)
829 ip_vs_conn_drop_conntrack(cp);
830 }
f4bc17cd 831
1da177e4
LT
832 if (unlikely(cp->app != NULL))
833 ip_vs_unbind_app(cp);
834 ip_vs_unbind_dest(cp);
835 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
836 atomic_dec(&ip_vs_conn_no_cport_cnt);
088339a5 837 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
6e67e586 838 atomic_dec(&ipvs->conn_count);
1da177e4
LT
839 return;
840 }
841
1da177e4 842 expire_later:
088339a5
JA
843 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
844 atomic_read(&cp->refcnt),
1da177e4
LT
845 atomic_read(&cp->n_control));
846
088339a5
JA
847 atomic_inc(&cp->refcnt);
848 cp->timeout = 60*HZ;
849
749c42b6 850 if (ipvs->sync_state & IP_VS_STATE_MASTER)
b61a8c1a 851 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
749c42b6 852
1da177e4
LT
853 ip_vs_conn_put(cp);
854}
855
088339a5
JA
856/* Modify timer, so that it expires as soon as possible.
857 * Can be called without reference only if under RCU lock.
858 */
1da177e4
LT
859void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
860{
088339a5
JA
861 /* Using mod_timer_pending will ensure the timer is not
862 * modified after the final del_timer in ip_vs_conn_expire.
863 */
864 if (timer_pending(&cp->timer) &&
865 time_after(cp->timer.expires, jiffies))
866 mod_timer_pending(&cp->timer, jiffies);
1da177e4
LT
867}
868
869
870/*
871 * Create a new connection entry and hash it into the ip_vs_conn_tab
872 */
873struct ip_vs_conn *
ba38528a 874ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
95c96174 875 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
0e051e68 876 struct ip_vs_dest *dest, __u32 fwmark)
1da177e4
LT
877{
878 struct ip_vs_conn *cp;
e64e2b46 879 struct netns_ipvs *ipvs = p->ipvs;
18d6ade6 880 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
6e67e586 881 p->protocol);
1da177e4 882
9a05475c 883 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
1da177e4 884 if (cp == NULL) {
1e3e238e 885 IP_VS_ERR_RL("%s(): no memory\n", __func__);
1da177e4
LT
886 return NULL;
887 }
888
731109e7 889 INIT_HLIST_NODE(&cp->c_list);
b24b8a24 890 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
58dbc6f2 891 cp->ipvs = ipvs;
f11017ec 892 cp->af = p->af;
ba38528a 893 cp->daf = dest_af;
f11017ec 894 cp->protocol = p->protocol;
9a05475c 895 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
f11017ec 896 cp->cport = p->cport;
2a971354 897 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
9a05475c 898 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
2a971354
MK
899 &cp->vaddr, p->vaddr);
900 cp->vport = p->vport;
ba38528a 901 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
1da177e4
LT
902 cp->dport = dport;
903 cp->flags = flags;
0e051e68 904 cp->fwmark = fwmark;
e9e5eee8
SH
905 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
906 ip_vs_pe_get(p->pe);
907 cp->pe = p->pe;
85999283
SH
908 cp->pe_data = p->pe_data;
909 cp->pe_data_len = p->pe_data_len;
9a05475c
JA
910 } else {
911 cp->pe = NULL;
912 cp->pe_data = NULL;
913 cp->pe_data_len = 0;
85999283 914 }
1da177e4
LT
915 spin_lock_init(&cp->lock);
916
917 /*
918 * Set the entry is referenced by the current thread before hashing
919 * it in the table, so that other thread run ip_vs_random_dropentry
920 * but cannot drop this entry.
921 */
922 atomic_set(&cp->refcnt, 1);
923
9a05475c 924 cp->control = NULL;
1da177e4
LT
925 atomic_set(&cp->n_control, 0);
926 atomic_set(&cp->in_pkts, 0);
927
9a05475c
JA
928 cp->packet_xmit = NULL;
929 cp->app = NULL;
930 cp->app_data = NULL;
931 /* reset struct ip_vs_seq */
932 cp->in_seq.delta = 0;
933 cp->out_seq.delta = 0;
934
6e67e586 935 atomic_inc(&ipvs->conn_count);
1da177e4
LT
936 if (flags & IP_VS_CONN_F_NO_CPORT)
937 atomic_inc(&ip_vs_conn_no_cport_cnt);
938
939 /* Bind the connection with a destination server */
9a05475c 940 cp->dest = NULL;
1da177e4
LT
941 ip_vs_bind_dest(cp, dest);
942
943 /* Set its state and timeout */
944 cp->state = 0;
9a05475c 945 cp->old_state = 0;
1da177e4 946 cp->timeout = 3*HZ;
749c42b6 947 cp->sync_endtime = jiffies & ~3UL;
1da177e4
LT
948
949 /* Bind its packet transmitter */
b3cdd2a7 950#ifdef CONFIG_IP_VS_IPV6
f11017ec 951 if (p->af == AF_INET6)
b3cdd2a7
JV
952 ip_vs_bind_xmit_v6(cp);
953 else
954#endif
955 ip_vs_bind_xmit(cp);
1da177e4 956
9bbac6a9
HS
957 if (unlikely(pd && atomic_read(&pd->appcnt)))
958 ip_vs_bind_app(cp, pd->pp);
1da177e4 959
f4bc17cd
JA
960 /*
961 * Allow conntrack to be preserved. By default, conntrack
962 * is created and destroyed for every packet.
963 * Sometimes keeping conntrack can be useful for
964 * IP_VS_CONN_F_ONE_PACKET too.
965 */
966
a0840e2e 967 if (ip_vs_conntrack_enabled(ipvs))
f4bc17cd
JA
968 cp->flags |= IP_VS_CONN_F_NFCT;
969
1da177e4
LT
970 /* Hash it in the ip_vs_conn_tab finally */
971 ip_vs_conn_hash(cp);
972
973 return cp;
974}
975
1da177e4
LT
976/*
977 * /proc/net/ip_vs_conn entries
978 */
979#ifdef CONFIG_PROC_FS
6e67e586 980struct ip_vs_iter_state {
731109e7
CG
981 struct seq_net_private p;
982 struct hlist_head *l;
6e67e586 983};
1da177e4
LT
984
985static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
986{
987 int idx;
988 struct ip_vs_conn *cp;
6e67e586 989 struct ip_vs_iter_state *iter = seq->private;
e905a9ed 990
6f7edb48 991 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
088339a5
JA
992 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
993 /* __ip_vs_conn_get() is not needed by
994 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
995 */
1da177e4 996 if (pos-- == 0) {
6e67e586 997 iter->l = &ip_vs_conn_tab[idx];
731109e7 998 return cp;
1da177e4
LT
999 }
1000 }
a38e5e23 1001 cond_resched_rcu();
1da177e4
LT
1002 }
1003
1004 return NULL;
1005}
1006
1007static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
7cf2eb7b 1008 __acquires(RCU)
1da177e4 1009{
6e67e586
HS
1010 struct ip_vs_iter_state *iter = seq->private;
1011
1012 iter->l = NULL;
7cf2eb7b 1013 rcu_read_lock();
1da177e4
LT
1014 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1015}
1016
1017static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1018{
1019 struct ip_vs_conn *cp = v;
6e67e586 1020 struct ip_vs_iter_state *iter = seq->private;
088339a5 1021 struct hlist_node *e;
731109e7 1022 struct hlist_head *l = iter->l;
1da177e4
LT
1023 int idx;
1024
1025 ++*pos;
e905a9ed 1026 if (v == SEQ_START_TOKEN)
1da177e4
LT
1027 return ip_vs_conn_array(seq, 0);
1028
1029 /* more on same hash chain? */
088339a5
JA
1030 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1031 if (e)
1032 return hlist_entry(e, struct ip_vs_conn, c_list);
1da177e4
LT
1033
1034 idx = l - ip_vs_conn_tab;
6f7edb48 1035 while (++idx < ip_vs_conn_tab_size) {
088339a5 1036 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
6e67e586 1037 iter->l = &ip_vs_conn_tab[idx];
1da177e4 1038 return cp;
e905a9ed 1039 }
a38e5e23 1040 cond_resched_rcu();
1da177e4 1041 }
6e67e586 1042 iter->l = NULL;
1da177e4
LT
1043 return NULL;
1044}
1045
1046static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
7cf2eb7b 1047 __releases(RCU)
1da177e4 1048{
7cf2eb7b 1049 rcu_read_unlock();
1da177e4
LT
1050}
1051
1052static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1053{
1054
1055 if (v == SEQ_START_TOKEN)
1056 seq_puts(seq,
a3c918ac 1057 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1da177e4
LT
1058 else {
1059 const struct ip_vs_conn *cp = v;
6e67e586 1060 struct net *net = seq_file_net(seq);
a3c918ac
SH
1061 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1062 size_t len = 0;
f18ae720 1063 char dbuf[IP_VS_ADDRSTRLEN];
a3c918ac 1064
58dbc6f2 1065 if (!net_eq(cp->ipvs->net, net))
6e67e586 1066 return 0;
e9e5eee8 1067 if (cp->pe_data) {
a3c918ac 1068 pe_data[0] = ' ';
e9e5eee8
SH
1069 len = strlen(cp->pe->name);
1070 memcpy(pe_data + 1, cp->pe->name, len);
a3c918ac
SH
1071 pe_data[len + 1] = ' ';
1072 len += 2;
e9e5eee8 1073 len += cp->pe->show_pe_data(cp, pe_data + len);
a3c918ac
SH
1074 }
1075 pe_data[len] = '\0';
1da177e4 1076
f18ae720
JA
1077#ifdef CONFIG_IP_VS_IPV6
1078 if (cp->daf == AF_INET6)
1079 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1080 else
1081#endif
1082 snprintf(dbuf, sizeof(dbuf), "%08X",
1083 ntohl(cp->daddr.ip));
1084
667a5f18
VB
1085#ifdef CONFIG_IP_VS_IPV6
1086 if (cp->af == AF_INET6)
a3c918ac 1087 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
f18ae720 1088 "%s %04X %-11s %7lu%s\n",
667a5f18 1089 ip_vs_proto_name(cp->protocol),
38ff4fa4
HH
1090 &cp->caddr.in6, ntohs(cp->cport),
1091 &cp->vaddr.in6, ntohs(cp->vport),
f18ae720 1092 dbuf, ntohs(cp->dport),
667a5f18 1093 ip_vs_state_name(cp->protocol, cp->state),
a3c918ac 1094 (cp->timer.expires-jiffies)/HZ, pe_data);
667a5f18
VB
1095 else
1096#endif
1097 seq_printf(seq,
1098 "%-3s %08X %04X %08X %04X"
f18ae720 1099 " %s %04X %-11s %7lu%s\n",
1da177e4 1100 ip_vs_proto_name(cp->protocol),
e7ade46a
JV
1101 ntohl(cp->caddr.ip), ntohs(cp->cport),
1102 ntohl(cp->vaddr.ip), ntohs(cp->vport),
f18ae720 1103 dbuf, ntohs(cp->dport),
1da177e4 1104 ip_vs_state_name(cp->protocol, cp->state),
a3c918ac 1105 (cp->timer.expires-jiffies)/HZ, pe_data);
1da177e4
LT
1106 }
1107 return 0;
1108}
1109
56b3d975 1110static const struct seq_operations ip_vs_conn_seq_ops = {
1da177e4
LT
1111 .start = ip_vs_conn_seq_start,
1112 .next = ip_vs_conn_seq_next,
1113 .stop = ip_vs_conn_seq_stop,
1114 .show = ip_vs_conn_seq_show,
1115};
1116
1117static int ip_vs_conn_open(struct inode *inode, struct file *file)
1118{
6e67e586
HS
1119 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1120 sizeof(struct ip_vs_iter_state));
1da177e4
LT
1121}
1122
9a32144e 1123static const struct file_operations ip_vs_conn_fops = {
1da177e4
LT
1124 .owner = THIS_MODULE,
1125 .open = ip_vs_conn_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
0f08190f 1128 .release = seq_release_net,
1da177e4 1129};
7a4fbb1f 1130
95c96174 1131static const char *ip_vs_origin_name(unsigned int flags)
7a4fbb1f
RB
1132{
1133 if (flags & IP_VS_CONN_F_SYNC)
1134 return "SYNC";
1135 else
1136 return "LOCAL";
1137}
1138
1139static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1140{
f18ae720 1141 char dbuf[IP_VS_ADDRSTRLEN];
7a4fbb1f
RB
1142
1143 if (v == SEQ_START_TOKEN)
1144 seq_puts(seq,
1145 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1146 else {
1147 const struct ip_vs_conn *cp = v;
6e67e586
HS
1148 struct net *net = seq_file_net(seq);
1149
58dbc6f2 1150 if (!net_eq(cp->ipvs->net, net))
6e67e586 1151 return 0;
7a4fbb1f 1152
f18ae720
JA
1153#ifdef CONFIG_IP_VS_IPV6
1154 if (cp->daf == AF_INET6)
1155 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1156 else
1157#endif
1158 snprintf(dbuf, sizeof(dbuf), "%08X",
1159 ntohl(cp->daddr.ip));
1160
667a5f18
VB
1161#ifdef CONFIG_IP_VS_IPV6
1162 if (cp->af == AF_INET6)
f18ae720
JA
1163 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1164 "%s %04X %-11s %-6s %7lu\n",
667a5f18 1165 ip_vs_proto_name(cp->protocol),
38ff4fa4
HH
1166 &cp->caddr.in6, ntohs(cp->cport),
1167 &cp->vaddr.in6, ntohs(cp->vport),
f18ae720 1168 dbuf, ntohs(cp->dport),
667a5f18
VB
1169 ip_vs_state_name(cp->protocol, cp->state),
1170 ip_vs_origin_name(cp->flags),
1171 (cp->timer.expires-jiffies)/HZ);
1172 else
1173#endif
1174 seq_printf(seq,
1175 "%-3s %08X %04X %08X %04X "
f18ae720 1176 "%s %04X %-11s %-6s %7lu\n",
7a4fbb1f 1177 ip_vs_proto_name(cp->protocol),
e7ade46a
JV
1178 ntohl(cp->caddr.ip), ntohs(cp->cport),
1179 ntohl(cp->vaddr.ip), ntohs(cp->vport),
f18ae720 1180 dbuf, ntohs(cp->dport),
7a4fbb1f
RB
1181 ip_vs_state_name(cp->protocol, cp->state),
1182 ip_vs_origin_name(cp->flags),
1183 (cp->timer.expires-jiffies)/HZ);
1184 }
1185 return 0;
1186}
1187
1188static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1189 .start = ip_vs_conn_seq_start,
1190 .next = ip_vs_conn_seq_next,
1191 .stop = ip_vs_conn_seq_stop,
1192 .show = ip_vs_conn_sync_seq_show,
1193};
1194
1195static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1196{
6e67e586
HS
1197 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1198 sizeof(struct ip_vs_iter_state));
7a4fbb1f
RB
1199}
1200
1201static const struct file_operations ip_vs_conn_sync_fops = {
1202 .owner = THIS_MODULE,
1203 .open = ip_vs_conn_sync_open,
1204 .read = seq_read,
1205 .llseek = seq_lseek,
0f08190f 1206 .release = seq_release_net,
7a4fbb1f
RB
1207};
1208
1da177e4
LT
1209#endif
1210
1211
1212/*
1213 * Randomly drop connection entries before running out of memory
1214 */
1215static inline int todrop_entry(struct ip_vs_conn *cp)
1216{
1217 /*
1218 * The drop rate array needs tuning for real environments.
1219 * Called from timer bh only => no locking
1220 */
9b5b5cff 1221 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1da177e4
LT
1222 static char todrop_counter[9] = {0};
1223 int i;
1224
1225 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1226 This will leave enough time for normal connection to get
1227 through. */
1228 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1229 return 0;
1230
1231 /* Don't drop the entry if its number of incoming packets is not
1232 located in [0, 8] */
1233 i = atomic_read(&cp->in_pkts);
1234 if (i > 8 || i < 0) return 0;
1235
1236 if (!todrop_rate[i]) return 0;
1237 if (--todrop_counter[i] > 0) return 0;
1238
1239 todrop_counter[i] = todrop_rate[i];
1240 return 1;
1241}
1242
af9debd4 1243/* Called from keventd and must protect itself from softirqs */
423b5595 1244void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
1da177e4
LT
1245{
1246 int idx;
088339a5 1247 struct ip_vs_conn *cp, *cp_c;
1da177e4 1248
a38e5e23 1249 rcu_read_lock();
1da177e4
LT
1250 /*
1251 * Randomly scan 1/32 of the whole table every second
1252 */
6f7edb48 1253 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
63862b5b 1254 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
1da177e4 1255
088339a5 1256 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
87375ab4 1257 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
1da177e4
LT
1258 /* connection template */
1259 continue;
423b5595 1260 if (cp->ipvs != ipvs)
f6340ee0 1261 continue;
1da177e4
LT
1262 if (cp->protocol == IPPROTO_TCP) {
1263 switch(cp->state) {
1264 case IP_VS_TCP_S_SYN_RECV:
1265 case IP_VS_TCP_S_SYNACK:
1266 break;
1267
1268 case IP_VS_TCP_S_ESTABLISHED:
1269 if (todrop_entry(cp))
1270 break;
1271 continue;
1272
1273 default:
1274 continue;
1275 }
acaac5d8
JA
1276 } else if (cp->protocol == IPPROTO_SCTP) {
1277 switch (cp->state) {
1278 case IP_VS_SCTP_S_INIT1:
1279 case IP_VS_SCTP_S_INIT:
1280 break;
1281 case IP_VS_SCTP_S_ESTABLISHED:
1282 if (todrop_entry(cp))
1283 break;
1284 continue;
1285 default:
1286 continue;
1287 }
1da177e4
LT
1288 } else {
1289 if (!todrop_entry(cp))
1290 continue;
1291 }
1292
1da177e4
LT
1293 IP_VS_DBG(4, "del connection\n");
1294 ip_vs_conn_expire_now(cp);
088339a5
JA
1295 cp_c = cp->control;
1296 /* cp->control is valid only with reference to cp */
1297 if (cp_c && __ip_vs_conn_get(cp)) {
1da177e4 1298 IP_VS_DBG(4, "del conn template\n");
088339a5
JA
1299 ip_vs_conn_expire_now(cp_c);
1300 __ip_vs_conn_put(cp);
1da177e4 1301 }
1da177e4 1302 }
a38e5e23 1303 cond_resched_rcu();
1da177e4 1304 }
a38e5e23 1305 rcu_read_unlock();
1da177e4
LT
1306}
1307
1308
1309/*
1310 * Flush all the connection entries in the ip_vs_conn_tab
1311 */
d889717a 1312static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
1da177e4
LT
1313{
1314 int idx;
088339a5 1315 struct ip_vs_conn *cp, *cp_c;
1da177e4 1316
a0840e2e 1317flush_again:
a38e5e23 1318 rcu_read_lock();
6f7edb48 1319 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1da177e4 1320
088339a5 1321 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
58dbc6f2 1322 if (cp->ipvs != ipvs)
6e67e586 1323 continue;
1da177e4
LT
1324 IP_VS_DBG(4, "del connection\n");
1325 ip_vs_conn_expire_now(cp);
088339a5
JA
1326 cp_c = cp->control;
1327 /* cp->control is valid only with reference to cp */
1328 if (cp_c && __ip_vs_conn_get(cp)) {
1da177e4 1329 IP_VS_DBG(4, "del conn template\n");
088339a5
JA
1330 ip_vs_conn_expire_now(cp_c);
1331 __ip_vs_conn_put(cp);
1da177e4 1332 }
1da177e4 1333 }
a38e5e23 1334 cond_resched_rcu();
1da177e4 1335 }
a38e5e23 1336 rcu_read_unlock();
1da177e4
LT
1337
1338 /* the counter may be not NULL, because maybe some conn entries
1339 are run by slow timer handler or unhashed but still referred */
6e67e586 1340 if (atomic_read(&ipvs->conn_count) != 0) {
1da177e4
LT
1341 schedule();
1342 goto flush_again;
1343 }
1344}
61b1ab45
HS
1345/*
1346 * per netns init and exit
1347 */
2f3edc6a 1348int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
61b1ab45 1349{
6e67e586 1350 atomic_set(&ipvs->conn_count, 0);
1da177e4 1351
92240e8d
SH
1352 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1353 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1354 &ip_vs_conn_sync_fops);
61b1ab45
HS
1355 return 0;
1356}
1357
2f3edc6a 1358void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
61b1ab45 1359{
6e67e586 1360 /* flush all the connection entries first */
d889717a 1361 ip_vs_conn_flush(ipvs);
92240e8d
SH
1362 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1363 remove_proc_entry("ip_vs_conn_sync", ipvs->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}