]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/netfilter/ipvs/ip_vs_conn.c
ipvs: remove EXPERIMENTAL tag
[mirror_ubuntu-eoan-kernel.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>
f190055f 30#include <linux/net.h>
1da177e4 31#include <linux/kernel.h>
14c85021 32#include <linux/module.h>
1da177e4
LT
33#include <linux/vmalloc.h>
34#include <linux/proc_fs.h> /* for proc_net_* */
5a0e3ad6 35#include <linux/slab.h>
1da177e4
LT
36#include <linux/seq_file.h>
37#include <linux/jhash.h>
38#include <linux/random.h>
39
457c4cbc 40#include <net/net_namespace.h>
1da177e4
LT
41#include <net/ip_vs.h>
42
43
6f7edb48
CB
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*/
51int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
52module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
53MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
54
55/* size and mask values */
56int ip_vs_conn_tab_size;
57int ip_vs_conn_tab_mask;
58
1da177e4
LT
59/*
60 * Connection hash table: for input and output packets lookups of IPVS
61 */
62static struct list_head *ip_vs_conn_tab;
63
64/* SLAB cache for IPVS connections */
e18b890b 65static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
1da177e4
LT
66
67/* counter for current IPVS connections */
68static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
69
70/* counter for no client port connections */
71static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
72
73/* random value for IPVS connection hash */
74static unsigned int ip_vs_conn_rnd;
75
76/*
77 * Fine locking granularity for big connection hash table
78 */
79#define CT_LOCKARRAY_BITS 4
80#define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
81#define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
82
83struct ip_vs_aligned_lock
84{
85 rwlock_t l;
86} __attribute__((__aligned__(SMP_CACHE_BYTES)));
87
88/* lock array for conn table */
89static struct ip_vs_aligned_lock
90__ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
91
92static inline void ct_read_lock(unsigned key)
93{
94 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
95}
96
97static inline void ct_read_unlock(unsigned key)
98{
99 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
100}
101
102static inline void ct_write_lock(unsigned key)
103{
104 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
105}
106
107static inline void ct_write_unlock(unsigned key)
108{
109 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
110}
111
112static inline void ct_read_lock_bh(unsigned key)
113{
114 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
115}
116
117static inline void ct_read_unlock_bh(unsigned key)
118{
119 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
120}
121
122static inline void ct_write_lock_bh(unsigned key)
123{
124 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
125}
126
127static inline void ct_write_unlock_bh(unsigned key)
128{
129 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
130}
131
132
133/*
134 * Returns hash value for IPVS connection entry
135 */
28364a59
JV
136static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
137 const union nf_inet_addr *addr,
138 __be16 port)
1da177e4 139{
28364a59
JV
140#ifdef CONFIG_IP_VS_IPV6
141 if (af == AF_INET6)
142 return jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
143 (__force u32)port, proto, ip_vs_conn_rnd)
6f7edb48 144 & ip_vs_conn_tab_mask;
28364a59
JV
145#endif
146 return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
147 ip_vs_conn_rnd)
6f7edb48 148 & ip_vs_conn_tab_mask;
1da177e4
LT
149}
150
151
152/*
153 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
154 * returns bool success.
155 */
156static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
157{
158 unsigned hash;
159 int ret;
160
26ec037f
NC
161 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
162 return 0;
163
1da177e4 164 /* Hash by protocol, client address and port */
28364a59 165 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
1da177e4
LT
166
167 ct_write_lock(hash);
aea9d711 168 spin_lock(&cp->lock);
1da177e4
LT
169
170 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
171 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
172 cp->flags |= IP_VS_CONN_F_HASHED;
173 atomic_inc(&cp->refcnt);
174 ret = 1;
175 } else {
1e3e238e
HE
176 pr_err("%s(): request for already hashed, called from %pF\n",
177 __func__, __builtin_return_address(0));
1da177e4
LT
178 ret = 0;
179 }
180
aea9d711 181 spin_unlock(&cp->lock);
1da177e4
LT
182 ct_write_unlock(hash);
183
184 return ret;
185}
186
187
188/*
189 * UNhashes ip_vs_conn from ip_vs_conn_tab.
190 * returns bool success.
191 */
192static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
193{
194 unsigned hash;
195 int ret;
196
197 /* unhash it and decrease its reference counter */
28364a59 198 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
1da177e4
LT
199
200 ct_write_lock(hash);
aea9d711 201 spin_lock(&cp->lock);
1da177e4
LT
202
203 if (cp->flags & IP_VS_CONN_F_HASHED) {
204 list_del(&cp->c_list);
205 cp->flags &= ~IP_VS_CONN_F_HASHED;
206 atomic_dec(&cp->refcnt);
207 ret = 1;
208 } else
209 ret = 0;
210
aea9d711 211 spin_unlock(&cp->lock);
1da177e4
LT
212 ct_write_unlock(hash);
213
214 return ret;
215}
216
217
218/*
219 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
220 * Called for pkts coming from OUTside-to-INside.
221 * s_addr, s_port: pkt source address (foreign host)
222 * d_addr, d_port: pkt dest address (load balancer)
223 */
224static inline struct ip_vs_conn *__ip_vs_conn_in_get
28364a59
JV
225(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
226 const union nf_inet_addr *d_addr, __be16 d_port)
1da177e4
LT
227{
228 unsigned hash;
229 struct ip_vs_conn *cp;
230
28364a59 231 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
1da177e4
LT
232
233 ct_read_lock(hash);
234
235 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
28364a59
JV
236 if (cp->af == af &&
237 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
238 ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
239 s_port == cp->cport && d_port == cp->vport &&
87375ab4 240 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
e7ade46a 241 protocol == cp->protocol) {
1da177e4
LT
242 /* HIT */
243 atomic_inc(&cp->refcnt);
244 ct_read_unlock(hash);
245 return cp;
246 }
247 }
248
249 ct_read_unlock(hash);
250
251 return NULL;
252}
253
254struct ip_vs_conn *ip_vs_conn_in_get
28364a59
JV
255(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
256 const union nf_inet_addr *d_addr, __be16 d_port)
1da177e4
LT
257{
258 struct ip_vs_conn *cp;
259
28364a59 260 cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
1da177e4 261 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
28364a59
JV
262 cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
263 d_port);
1da177e4 264
28364a59
JV
265 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
266 ip_vs_proto_name(protocol),
267 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
268 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
269 cp ? "hit" : "not hit");
1da177e4
LT
270
271 return cp;
272}
273
87375ab4
JA
274/* Get reference to connection template */
275struct ip_vs_conn *ip_vs_ct_in_get
28364a59
JV
276(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
277 const union nf_inet_addr *d_addr, __be16 d_port)
87375ab4
JA
278{
279 unsigned hash;
280 struct ip_vs_conn *cp;
281
28364a59 282 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
87375ab4
JA
283
284 ct_read_lock(hash);
285
286 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
28364a59
JV
287 if (cp->af == af &&
288 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
be8be9ec
SH
289 /* protocol should only be IPPROTO_IP if
290 * d_addr is a fwmark */
291 ip_vs_addr_equal(protocol == IPPROTO_IP ? AF_UNSPEC : af,
292 d_addr, &cp->vaddr) &&
28364a59 293 s_port == cp->cport && d_port == cp->vport &&
87375ab4 294 cp->flags & IP_VS_CONN_F_TEMPLATE &&
e7ade46a 295 protocol == cp->protocol) {
87375ab4
JA
296 /* HIT */
297 atomic_inc(&cp->refcnt);
298 goto out;
299 }
300 }
301 cp = NULL;
302
303 out:
304 ct_read_unlock(hash);
305
28364a59
JV
306 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
307 ip_vs_proto_name(protocol),
308 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
309 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
310 cp ? "hit" : "not hit");
87375ab4
JA
311
312 return cp;
313}
1da177e4
LT
314
315/*
316 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
317 * Called for pkts coming from inside-to-OUTside.
318 * s_addr, s_port: pkt source address (inside host)
319 * d_addr, d_port: pkt dest address (foreign host)
320 */
321struct ip_vs_conn *ip_vs_conn_out_get
28364a59
JV
322(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
323 const union nf_inet_addr *d_addr, __be16 d_port)
1da177e4
LT
324{
325 unsigned hash;
326 struct ip_vs_conn *cp, *ret=NULL;
327
328 /*
329 * Check for "full" addressed entries
330 */
28364a59 331 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
1da177e4
LT
332
333 ct_read_lock(hash);
334
335 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
28364a59
JV
336 if (cp->af == af &&
337 ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
338 ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
339 d_port == cp->cport && s_port == cp->dport &&
1da177e4
LT
340 protocol == cp->protocol) {
341 /* HIT */
342 atomic_inc(&cp->refcnt);
343 ret = cp;
344 break;
345 }
346 }
347
348 ct_read_unlock(hash);
349
28364a59
JV
350 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
351 ip_vs_proto_name(protocol),
352 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
353 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
354 ret ? "hit" : "not hit");
1da177e4
LT
355
356 return ret;
357}
358
359
360/*
361 * Put back the conn and restart its timer with its timeout
362 */
363void ip_vs_conn_put(struct ip_vs_conn *cp)
364{
26ec037f
NC
365 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
366 0 : cp->timeout;
367 mod_timer(&cp->timer, jiffies+t);
1da177e4
LT
368
369 __ip_vs_conn_put(cp);
370}
371
372
373/*
374 * Fill a no_client_port connection with a client port number
375 */
014d730d 376void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
1da177e4
LT
377{
378 if (ip_vs_conn_unhash(cp)) {
379 spin_lock(&cp->lock);
380 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
381 atomic_dec(&ip_vs_conn_no_cport_cnt);
382 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
383 cp->cport = cport;
384 }
385 spin_unlock(&cp->lock);
386
387 /* hash on new dport */
388 ip_vs_conn_hash(cp);
389 }
390}
391
392
393/*
394 * Bind a connection entry with the corresponding packet_xmit.
395 * Called by ip_vs_conn_new.
396 */
397static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
398{
399 switch (IP_VS_FWD_METHOD(cp)) {
400 case IP_VS_CONN_F_MASQ:
401 cp->packet_xmit = ip_vs_nat_xmit;
402 break;
403
404 case IP_VS_CONN_F_TUNNEL:
405 cp->packet_xmit = ip_vs_tunnel_xmit;
406 break;
407
408 case IP_VS_CONN_F_DROUTE:
409 cp->packet_xmit = ip_vs_dr_xmit;
410 break;
411
412 case IP_VS_CONN_F_LOCALNODE:
413 cp->packet_xmit = ip_vs_null_xmit;
414 break;
415
416 case IP_VS_CONN_F_BYPASS:
417 cp->packet_xmit = ip_vs_bypass_xmit;
418 break;
419 }
420}
421
b3cdd2a7
JV
422#ifdef CONFIG_IP_VS_IPV6
423static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
424{
425 switch (IP_VS_FWD_METHOD(cp)) {
426 case IP_VS_CONN_F_MASQ:
427 cp->packet_xmit = ip_vs_nat_xmit_v6;
428 break;
429
430 case IP_VS_CONN_F_TUNNEL:
431 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
432 break;
433
434 case IP_VS_CONN_F_DROUTE:
435 cp->packet_xmit = ip_vs_dr_xmit_v6;
436 break;
437
438 case IP_VS_CONN_F_LOCALNODE:
439 cp->packet_xmit = ip_vs_null_xmit;
440 break;
441
442 case IP_VS_CONN_F_BYPASS:
443 cp->packet_xmit = ip_vs_bypass_xmit_v6;
444 break;
445 }
446}
447#endif
448
1da177e4
LT
449
450static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
451{
452 return atomic_read(&dest->activeconns)
453 + atomic_read(&dest->inactconns);
454}
455
456/*
457 * Bind a connection entry with a virtual service destination
458 * Called just after a new connection entry is created.
459 */
460static inline void
461ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
462{
463 /* if dest is NULL, then return directly */
464 if (!dest)
465 return;
466
467 /* Increase the refcnt counter of the dest */
468 atomic_inc(&dest->refcnt);
469
470 /* Bind with the destination and its corresponding transmitter */
b209639e
RB
471 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
472 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
473 /* if the connection is not template and is created
474 * by sync, preserve the activity flag.
475 */
476 cp->flags |= atomic_read(&dest->conn_flags) &
477 (~IP_VS_CONN_F_INACTIVE);
478 else
479 cp->flags |= atomic_read(&dest->conn_flags);
1da177e4
LT
480 cp->dest = dest;
481
cfc78c5a
JV
482 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
483 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
484 "dest->refcnt:%d\n",
485 ip_vs_proto_name(cp->protocol),
486 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
487 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
488 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
489 ip_vs_fwd_tag(cp), cp->state,
490 cp->flags, atomic_read(&cp->refcnt),
491 atomic_read(&dest->refcnt));
1da177e4
LT
492
493 /* Update the connection counters */
87375ab4 494 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
1da177e4
LT
495 /* It is a normal connection, so increase the inactive
496 connection counter because it is in TCP SYNRECV
497 state (inactive) or other protocol inacive state */
b209639e
RB
498 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
499 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
500 atomic_inc(&dest->activeconns);
501 else
502 atomic_inc(&dest->inactconns);
1da177e4
LT
503 } else {
504 /* It is a persistent connection/template, so increase
505 the peristent connection counter */
506 atomic_inc(&dest->persistconns);
507 }
508
509 if (dest->u_threshold != 0 &&
510 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
511 dest->flags |= IP_VS_DEST_F_OVERLOAD;
512}
513
514
1e356f9c
RB
515/*
516 * Check if there is a destination for the connection, if so
517 * bind the connection to the destination.
518 */
519struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
520{
521 struct ip_vs_dest *dest;
522
523 if ((cp) && (!cp->dest)) {
7937df15
JV
524 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
525 &cp->vaddr, cp->vport,
526 cp->protocol);
1e356f9c
RB
527 ip_vs_bind_dest(cp, dest);
528 return dest;
529 } else
530 return NULL;
531}
1e356f9c
RB
532
533
1da177e4
LT
534/*
535 * Unbind a connection entry with its VS destination
536 * Called by the ip_vs_conn_expire function.
537 */
538static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
539{
540 struct ip_vs_dest *dest = cp->dest;
541
542 if (!dest)
543 return;
544
cfc78c5a
JV
545 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
546 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
547 "dest->refcnt:%d\n",
548 ip_vs_proto_name(cp->protocol),
549 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
550 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
551 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
552 ip_vs_fwd_tag(cp), cp->state,
553 cp->flags, atomic_read(&cp->refcnt),
554 atomic_read(&dest->refcnt));
1da177e4
LT
555
556 /* Update the connection counters */
87375ab4 557 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
1da177e4
LT
558 /* It is a normal connection, so decrease the inactconns
559 or activeconns counter */
560 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
561 atomic_dec(&dest->inactconns);
562 } else {
563 atomic_dec(&dest->activeconns);
564 }
565 } else {
566 /* It is a persistent connection/template, so decrease
567 the peristent connection counter */
568 atomic_dec(&dest->persistconns);
569 }
570
571 if (dest->l_threshold != 0) {
572 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
573 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
574 } else if (dest->u_threshold != 0) {
575 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
576 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
577 } else {
578 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
579 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
580 }
581
582 /*
583 * Simply decrease the refcnt of the dest, because the
584 * dest will be either in service's destination list
585 * or in the trash.
586 */
587 atomic_dec(&dest->refcnt);
588}
589
590
591/*
592 * Checking if the destination of a connection template is available.
593 * If available, return 1, otherwise invalidate this connection
594 * template and return 0.
595 */
596int ip_vs_check_template(struct ip_vs_conn *ct)
597{
598 struct ip_vs_dest *dest = ct->dest;
599
600 /*
601 * Checking the dest server status.
602 */
603 if ((dest == NULL) ||
e905a9ed
YH
604 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
605 (sysctl_ip_vs_expire_quiescent_template &&
1da177e4 606 (atomic_read(&dest->weight) == 0))) {
cfc78c5a
JV
607 IP_VS_DBG_BUF(9, "check_template: dest not available for "
608 "protocol %s s:%s:%d v:%s:%d "
609 "-> d:%s:%d\n",
610 ip_vs_proto_name(ct->protocol),
611 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
612 ntohs(ct->cport),
613 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
614 ntohs(ct->vport),
615 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
616 ntohs(ct->dport));
1da177e4
LT
617
618 /*
619 * Invalidate the connection template
620 */
014d730d 621 if (ct->vport != htons(0xffff)) {
1da177e4 622 if (ip_vs_conn_unhash(ct)) {
014d730d
AV
623 ct->dport = htons(0xffff);
624 ct->vport = htons(0xffff);
1da177e4
LT
625 ct->cport = 0;
626 ip_vs_conn_hash(ct);
627 }
628 }
629
630 /*
631 * Simply decrease the refcnt of the template,
632 * don't restart its timer.
633 */
634 atomic_dec(&ct->refcnt);
635 return 0;
636 }
637 return 1;
638}
639
640static void ip_vs_conn_expire(unsigned long data)
641{
642 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
643
644 cp->timeout = 60*HZ;
645
646 /*
647 * hey, I'm using it
648 */
649 atomic_inc(&cp->refcnt);
650
651 /*
652 * do I control anybody?
653 */
654 if (atomic_read(&cp->n_control))
655 goto expire_later;
656
657 /*
658 * unhash it if it is hashed in the conn table
659 */
26ec037f 660 if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
1da177e4
LT
661 goto expire_later;
662
663 /*
664 * refcnt==1 implies I'm the only one referrer
665 */
666 if (likely(atomic_read(&cp->refcnt) == 1)) {
667 /* delete the timer if it is activated by other users */
668 if (timer_pending(&cp->timer))
669 del_timer(&cp->timer);
670
671 /* does anybody control me? */
672 if (cp->control)
673 ip_vs_control_del(cp);
674
675 if (unlikely(cp->app != NULL))
676 ip_vs_unbind_app(cp);
677 ip_vs_unbind_dest(cp);
678 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
679 atomic_dec(&ip_vs_conn_no_cport_cnt);
680 atomic_dec(&ip_vs_conn_count);
681
682 kmem_cache_free(ip_vs_conn_cachep, cp);
683 return;
684 }
685
686 /* hash it back to the table */
687 ip_vs_conn_hash(cp);
688
689 expire_later:
4b5bdf5c 690 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
1da177e4
LT
691 atomic_read(&cp->refcnt)-1,
692 atomic_read(&cp->n_control));
693
694 ip_vs_conn_put(cp);
695}
696
697
698void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
699{
700 if (del_timer(&cp->timer))
701 mod_timer(&cp->timer, jiffies);
1da177e4
LT
702}
703
704
705/*
706 * Create a new connection entry and hash it into the ip_vs_conn_tab
707 */
708struct ip_vs_conn *
28364a59
JV
709ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
710 const union nf_inet_addr *vaddr, __be16 vport,
711 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
1da177e4
LT
712 struct ip_vs_dest *dest)
713{
714 struct ip_vs_conn *cp;
715 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
716
c3762229 717 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
1da177e4 718 if (cp == NULL) {
1e3e238e 719 IP_VS_ERR_RL("%s(): no memory\n", __func__);
1da177e4
LT
720 return NULL;
721 }
722
1da177e4 723 INIT_LIST_HEAD(&cp->c_list);
b24b8a24 724 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
28364a59 725 cp->af = af;
1da177e4 726 cp->protocol = proto;
28364a59 727 ip_vs_addr_copy(af, &cp->caddr, caddr);
1da177e4 728 cp->cport = cport;
28364a59 729 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
1da177e4 730 cp->vport = vport;
be8be9ec
SH
731 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
732 ip_vs_addr_copy(proto == IPPROTO_IP ? AF_UNSPEC : af,
733 &cp->daddr, daddr);
1da177e4
LT
734 cp->dport = dport;
735 cp->flags = flags;
736 spin_lock_init(&cp->lock);
737
738 /*
739 * Set the entry is referenced by the current thread before hashing
740 * it in the table, so that other thread run ip_vs_random_dropentry
741 * but cannot drop this entry.
742 */
743 atomic_set(&cp->refcnt, 1);
744
745 atomic_set(&cp->n_control, 0);
746 atomic_set(&cp->in_pkts, 0);
747
748 atomic_inc(&ip_vs_conn_count);
749 if (flags & IP_VS_CONN_F_NO_CPORT)
750 atomic_inc(&ip_vs_conn_no_cport_cnt);
751
752 /* Bind the connection with a destination server */
753 ip_vs_bind_dest(cp, dest);
754
755 /* Set its state and timeout */
756 cp->state = 0;
757 cp->timeout = 3*HZ;
758
759 /* Bind its packet transmitter */
b3cdd2a7
JV
760#ifdef CONFIG_IP_VS_IPV6
761 if (af == AF_INET6)
762 ip_vs_bind_xmit_v6(cp);
763 else
764#endif
765 ip_vs_bind_xmit(cp);
1da177e4
LT
766
767 if (unlikely(pp && atomic_read(&pp->appcnt)))
768 ip_vs_bind_app(cp, pp);
769
770 /* Hash it in the ip_vs_conn_tab finally */
771 ip_vs_conn_hash(cp);
772
773 return cp;
774}
775
776
777/*
778 * /proc/net/ip_vs_conn entries
779 */
780#ifdef CONFIG_PROC_FS
781
782static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
783{
784 int idx;
785 struct ip_vs_conn *cp;
e905a9ed 786
6f7edb48 787 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1da177e4
LT
788 ct_read_lock_bh(idx);
789 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
790 if (pos-- == 0) {
791 seq->private = &ip_vs_conn_tab[idx];
792 return cp;
793 }
794 }
795 ct_read_unlock_bh(idx);
796 }
797
798 return NULL;
799}
800
801static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
802{
803 seq->private = NULL;
804 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
805}
806
807static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
808{
809 struct ip_vs_conn *cp = v;
810 struct list_head *e, *l = seq->private;
811 int idx;
812
813 ++*pos;
e905a9ed 814 if (v == SEQ_START_TOKEN)
1da177e4
LT
815 return ip_vs_conn_array(seq, 0);
816
817 /* more on same hash chain? */
818 if ((e = cp->c_list.next) != l)
819 return list_entry(e, struct ip_vs_conn, c_list);
820
821 idx = l - ip_vs_conn_tab;
822 ct_read_unlock_bh(idx);
823
6f7edb48 824 while (++idx < ip_vs_conn_tab_size) {
1da177e4
LT
825 ct_read_lock_bh(idx);
826 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
827 seq->private = &ip_vs_conn_tab[idx];
828 return cp;
e905a9ed 829 }
1da177e4
LT
830 ct_read_unlock_bh(idx);
831 }
832 seq->private = NULL;
833 return NULL;
834}
835
836static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
837{
838 struct list_head *l = seq->private;
839
840 if (l)
841 ct_read_unlock_bh(l - ip_vs_conn_tab);
842}
843
844static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
845{
846
847 if (v == SEQ_START_TOKEN)
848 seq_puts(seq,
849 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
850 else {
851 const struct ip_vs_conn *cp = v;
852
667a5f18
VB
853#ifdef CONFIG_IP_VS_IPV6
854 if (cp->af == AF_INET6)
5b095d98 855 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
667a5f18 856 ip_vs_proto_name(cp->protocol),
38ff4fa4
HH
857 &cp->caddr.in6, ntohs(cp->cport),
858 &cp->vaddr.in6, ntohs(cp->vport),
859 &cp->daddr.in6, ntohs(cp->dport),
667a5f18
VB
860 ip_vs_state_name(cp->protocol, cp->state),
861 (cp->timer.expires-jiffies)/HZ);
862 else
863#endif
864 seq_printf(seq,
865 "%-3s %08X %04X %08X %04X"
866 " %08X %04X %-11s %7lu\n",
1da177e4 867 ip_vs_proto_name(cp->protocol),
e7ade46a
JV
868 ntohl(cp->caddr.ip), ntohs(cp->cport),
869 ntohl(cp->vaddr.ip), ntohs(cp->vport),
870 ntohl(cp->daddr.ip), ntohs(cp->dport),
1da177e4
LT
871 ip_vs_state_name(cp->protocol, cp->state),
872 (cp->timer.expires-jiffies)/HZ);
873 }
874 return 0;
875}
876
56b3d975 877static const struct seq_operations ip_vs_conn_seq_ops = {
1da177e4
LT
878 .start = ip_vs_conn_seq_start,
879 .next = ip_vs_conn_seq_next,
880 .stop = ip_vs_conn_seq_stop,
881 .show = ip_vs_conn_seq_show,
882};
883
884static int ip_vs_conn_open(struct inode *inode, struct file *file)
885{
886 return seq_open(file, &ip_vs_conn_seq_ops);
887}
888
9a32144e 889static const struct file_operations ip_vs_conn_fops = {
1da177e4
LT
890 .owner = THIS_MODULE,
891 .open = ip_vs_conn_open,
892 .read = seq_read,
893 .llseek = seq_lseek,
894 .release = seq_release,
895};
7a4fbb1f
RB
896
897static const char *ip_vs_origin_name(unsigned flags)
898{
899 if (flags & IP_VS_CONN_F_SYNC)
900 return "SYNC";
901 else
902 return "LOCAL";
903}
904
905static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
906{
907
908 if (v == SEQ_START_TOKEN)
909 seq_puts(seq,
910 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
911 else {
912 const struct ip_vs_conn *cp = v;
913
667a5f18
VB
914#ifdef CONFIG_IP_VS_IPV6
915 if (cp->af == AF_INET6)
5b095d98 916 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
667a5f18 917 ip_vs_proto_name(cp->protocol),
38ff4fa4
HH
918 &cp->caddr.in6, ntohs(cp->cport),
919 &cp->vaddr.in6, ntohs(cp->vport),
920 &cp->daddr.in6, ntohs(cp->dport),
667a5f18
VB
921 ip_vs_state_name(cp->protocol, cp->state),
922 ip_vs_origin_name(cp->flags),
923 (cp->timer.expires-jiffies)/HZ);
924 else
925#endif
926 seq_printf(seq,
927 "%-3s %08X %04X %08X %04X "
928 "%08X %04X %-11s %-6s %7lu\n",
7a4fbb1f 929 ip_vs_proto_name(cp->protocol),
e7ade46a
JV
930 ntohl(cp->caddr.ip), ntohs(cp->cport),
931 ntohl(cp->vaddr.ip), ntohs(cp->vport),
932 ntohl(cp->daddr.ip), ntohs(cp->dport),
7a4fbb1f
RB
933 ip_vs_state_name(cp->protocol, cp->state),
934 ip_vs_origin_name(cp->flags),
935 (cp->timer.expires-jiffies)/HZ);
936 }
937 return 0;
938}
939
940static const struct seq_operations ip_vs_conn_sync_seq_ops = {
941 .start = ip_vs_conn_seq_start,
942 .next = ip_vs_conn_seq_next,
943 .stop = ip_vs_conn_seq_stop,
944 .show = ip_vs_conn_sync_seq_show,
945};
946
947static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
948{
949 return seq_open(file, &ip_vs_conn_sync_seq_ops);
950}
951
952static const struct file_operations ip_vs_conn_sync_fops = {
953 .owner = THIS_MODULE,
954 .open = ip_vs_conn_sync_open,
955 .read = seq_read,
956 .llseek = seq_lseek,
957 .release = seq_release,
958};
959
1da177e4
LT
960#endif
961
962
963/*
964 * Randomly drop connection entries before running out of memory
965 */
966static inline int todrop_entry(struct ip_vs_conn *cp)
967{
968 /*
969 * The drop rate array needs tuning for real environments.
970 * Called from timer bh only => no locking
971 */
9b5b5cff 972 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1da177e4
LT
973 static char todrop_counter[9] = {0};
974 int i;
975
976 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
977 This will leave enough time for normal connection to get
978 through. */
979 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
980 return 0;
981
982 /* Don't drop the entry if its number of incoming packets is not
983 located in [0, 8] */
984 i = atomic_read(&cp->in_pkts);
985 if (i > 8 || i < 0) return 0;
986
987 if (!todrop_rate[i]) return 0;
988 if (--todrop_counter[i] > 0) return 0;
989
990 todrop_counter[i] = todrop_rate[i];
991 return 1;
992}
993
af9debd4 994/* Called from keventd and must protect itself from softirqs */
1da177e4
LT
995void ip_vs_random_dropentry(void)
996{
997 int idx;
998 struct ip_vs_conn *cp;
1da177e4
LT
999
1000 /*
1001 * Randomly scan 1/32 of the whole table every second
1002 */
6f7edb48
CB
1003 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1004 unsigned hash = net_random() & ip_vs_conn_tab_mask;
1da177e4
LT
1005
1006 /*
1007 * Lock is actually needed in this loop.
1008 */
af9debd4 1009 ct_write_lock_bh(hash);
1da177e4
LT
1010
1011 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
87375ab4 1012 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
1da177e4
LT
1013 /* connection template */
1014 continue;
1015
1016 if (cp->protocol == IPPROTO_TCP) {
1017 switch(cp->state) {
1018 case IP_VS_TCP_S_SYN_RECV:
1019 case IP_VS_TCP_S_SYNACK:
1020 break;
1021
1022 case IP_VS_TCP_S_ESTABLISHED:
1023 if (todrop_entry(cp))
1024 break;
1025 continue;
1026
1027 default:
1028 continue;
1029 }
1030 } else {
1031 if (!todrop_entry(cp))
1032 continue;
1033 }
1034
1da177e4
LT
1035 IP_VS_DBG(4, "del connection\n");
1036 ip_vs_conn_expire_now(cp);
fb3d8949 1037 if (cp->control) {
1da177e4 1038 IP_VS_DBG(4, "del conn template\n");
fb3d8949 1039 ip_vs_conn_expire_now(cp->control);
1da177e4 1040 }
1da177e4 1041 }
af9debd4 1042 ct_write_unlock_bh(hash);
1da177e4
LT
1043 }
1044}
1045
1046
1047/*
1048 * Flush all the connection entries in the ip_vs_conn_tab
1049 */
1050static void ip_vs_conn_flush(void)
1051{
1052 int idx;
1053 struct ip_vs_conn *cp;
1da177e4
LT
1054
1055 flush_again:
6f7edb48 1056 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1da177e4
LT
1057 /*
1058 * Lock is actually needed in this loop.
1059 */
1060 ct_write_lock_bh(idx);
1061
1062 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1da177e4 1063
1da177e4
LT
1064 IP_VS_DBG(4, "del connection\n");
1065 ip_vs_conn_expire_now(cp);
fb3d8949 1066 if (cp->control) {
1da177e4 1067 IP_VS_DBG(4, "del conn template\n");
fb3d8949 1068 ip_vs_conn_expire_now(cp->control);
1da177e4 1069 }
1da177e4
LT
1070 }
1071 ct_write_unlock_bh(idx);
1072 }
1073
1074 /* the counter may be not NULL, because maybe some conn entries
1075 are run by slow timer handler or unhashed but still referred */
1076 if (atomic_read(&ip_vs_conn_count) != 0) {
1077 schedule();
1078 goto flush_again;
1079 }
1080}
1081
1082
048cf48b 1083int __init ip_vs_conn_init(void)
1da177e4
LT
1084{
1085 int idx;
1086
6f7edb48
CB
1087 /* Compute size and mask */
1088 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1089 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1090
1da177e4
LT
1091 /*
1092 * Allocate the connection hash table and initialize its list heads
1093 */
6f7edb48
CB
1094 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size *
1095 sizeof(struct list_head));
1da177e4
LT
1096 if (!ip_vs_conn_tab)
1097 return -ENOMEM;
1098
1099 /* Allocate ip_vs_conn slab cache */
1100 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1101 sizeof(struct ip_vs_conn), 0,
20c2df83 1102 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1103 if (!ip_vs_conn_cachep) {
1104 vfree(ip_vs_conn_tab);
1105 return -ENOMEM;
1106 }
1107
1e3e238e
HE
1108 pr_info("Connection hash table configured "
1109 "(size=%d, memory=%ldKbytes)\n",
6f7edb48
CB
1110 ip_vs_conn_tab_size,
1111 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
1da177e4
LT
1112 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1113 sizeof(struct ip_vs_conn));
1114
6f7edb48 1115 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1da177e4
LT
1116 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1117 }
1118
1119 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1120 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1121 }
1122
457c4cbc 1123 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
7a4fbb1f 1124 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1da177e4
LT
1125
1126 /* calculate the random value for connection hash */
1127 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1128
1129 return 0;
1130}
1131
1132
1133void ip_vs_conn_cleanup(void)
1134{
1135 /* flush all the connection entries first */
1136 ip_vs_conn_flush();
1137
1138 /* Release the empty cache */
1139 kmem_cache_destroy(ip_vs_conn_cachep);
457c4cbc 1140 proc_net_remove(&init_net, "ip_vs_conn");
7a4fbb1f 1141 proc_net_remove(&init_net, "ip_vs_conn_sync");
1da177e4
LT
1142 vfree(ip_vs_conn_tab);
1143}