blob: a970d9691496dfb488ce93887e0591e0b6effa60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Hannes Eder9aada7a2009-07-30 14:29:44 -070025#define KMSG_COMPONENT "IPVS"
26#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
27
Andrew Mortone9242832006-01-05 14:57:36 -080028#include <linux/interrupt.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/in.h>
Arnaldo Carvalho de Melof1900552006-01-04 02:02:20 -020030#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/vmalloc.h>
34#include <linux/proc_fs.h> /* for proc_net_* */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/seq_file.h>
37#include <linux/jhash.h>
38#include <linux/random.h>
39
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020040#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/ip_vs.h>
42
43
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010044#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
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 */
Christoph Lametere18b8902006-12-06 20:33:20 -080065static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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 */
Julius Volz28364a52008-09-02 15:55:43 +0200136static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
137 const union nf_inet_addr *addr,
138 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Julius Volz28364a52008-09-02 15:55:43 +0200140#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)
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100144 & ip_vs_conn_tab_mask;
Julius Volz28364a52008-09-02 15:55:43 +0200145#endif
146 return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
147 ip_vs_conn_rnd)
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100148 & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
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
Nick Chalk26ec0372010-06-22 08:07:01 +0200161 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
162 return 0;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Hash by protocol, client address and port */
Julius Volz28364a52008-09-02 15:55:43 +0200165 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200168 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
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 {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000176 pr_err("%s(): request for already hashed, called from %pF\n",
177 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ret = 0;
179 }
180
Sven Wegeneraea9d712010-06-09 16:10:57 +0200181 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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 */
Julius Volz28364a52008-09-02 15:55:43 +0200198 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200201 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
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
Sven Wegeneraea9d712010-06-09 16:10:57 +0200211 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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
Julius Volz28364a52008-09-02 15:55:43 +0200225(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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 unsigned hash;
229 struct ip_vs_conn *cp;
230
Julius Volz28364a52008-09-02 15:55:43 +0200231 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 ct_read_lock(hash);
234
235 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julius Volz28364a52008-09-02 15:55:43 +0200236 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 &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700240 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Julius Volze7ade462008-09-02 15:55:33 +0200241 protocol == cp->protocol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* 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
Julius Volz28364a52008-09-02 15:55:43 +0200255(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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct ip_vs_conn *cp;
259
Julius Volz28364a52008-09-02 15:55:43 +0200260 cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
Julius Volz28364a52008-09-02 15:55:43 +0200262 cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
263 d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Julius Volz28364a52008-09-02 15:55:43 +0200265 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return cp;
272}
273
Simon Horman5c0d2372010-08-02 17:12:44 +0200274struct ip_vs_conn *
275ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
276 struct ip_vs_protocol *pp,
277 const struct ip_vs_iphdr *iph,
278 unsigned int proto_off, int inverse)
279{
280 __be16 _ports[2], *pptr;
281
282 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
283 if (pptr == NULL)
284 return NULL;
285
286 if (likely(!inverse))
287 return ip_vs_conn_in_get(af, iph->protocol,
288 &iph->saddr, pptr[0],
289 &iph->daddr, pptr[1]);
290 else
291 return ip_vs_conn_in_get(af, iph->protocol,
292 &iph->daddr, pptr[1],
293 &iph->saddr, pptr[0]);
294}
295EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
296
Julian Anastasov87375ab2005-09-14 21:08:51 -0700297/* Get reference to connection template */
298struct ip_vs_conn *ip_vs_ct_in_get
Julius Volz28364a52008-09-02 15:55:43 +0200299(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
300 const union nf_inet_addr *d_addr, __be16 d_port)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700301{
302 unsigned hash;
303 struct ip_vs_conn *cp;
304
Julius Volz28364a52008-09-02 15:55:43 +0200305 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700306
307 ct_read_lock(hash);
308
309 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julius Volz28364a52008-09-02 15:55:43 +0200310 if (cp->af == af &&
311 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000312 /* protocol should only be IPPROTO_IP if
313 * d_addr is a fwmark */
314 ip_vs_addr_equal(protocol == IPPROTO_IP ? AF_UNSPEC : af,
315 d_addr, &cp->vaddr) &&
Julius Volz28364a52008-09-02 15:55:43 +0200316 s_port == cp->cport && d_port == cp->vport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700317 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julius Volze7ade462008-09-02 15:55:33 +0200318 protocol == cp->protocol) {
Julian Anastasov87375ab2005-09-14 21:08:51 -0700319 /* HIT */
320 atomic_inc(&cp->refcnt);
321 goto out;
322 }
323 }
324 cp = NULL;
325
326 out:
327 ct_read_unlock(hash);
328
Julius Volz28364a52008-09-02 15:55:43 +0200329 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
330 ip_vs_proto_name(protocol),
331 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
332 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
333 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700334
335 return cp;
336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/*
339 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
340 * Called for pkts coming from inside-to-OUTside.
341 * s_addr, s_port: pkt source address (inside host)
342 * d_addr, d_port: pkt dest address (foreign host)
343 */
344struct ip_vs_conn *ip_vs_conn_out_get
Julius Volz28364a52008-09-02 15:55:43 +0200345(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
346 const union nf_inet_addr *d_addr, __be16 d_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 unsigned hash;
349 struct ip_vs_conn *cp, *ret=NULL;
350
351 /*
352 * Check for "full" addressed entries
353 */
Julius Volz28364a52008-09-02 15:55:43 +0200354 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 ct_read_lock(hash);
357
358 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julius Volz28364a52008-09-02 15:55:43 +0200359 if (cp->af == af &&
360 ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
361 ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
362 d_port == cp->cport && s_port == cp->dport &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 protocol == cp->protocol) {
364 /* HIT */
365 atomic_inc(&cp->refcnt);
366 ret = cp;
367 break;
368 }
369 }
370
371 ct_read_unlock(hash);
372
Julius Volz28364a52008-09-02 15:55:43 +0200373 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
374 ip_vs_proto_name(protocol),
375 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
376 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
377 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 return ret;
380}
381
Simon Horman5c0d2372010-08-02 17:12:44 +0200382struct ip_vs_conn *
383ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
384 struct ip_vs_protocol *pp,
385 const struct ip_vs_iphdr *iph,
386 unsigned int proto_off, int inverse)
387{
388 __be16 _ports[2], *pptr;
389
390 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
391 if (pptr == NULL)
392 return NULL;
393
394 if (likely(!inverse))
395 return ip_vs_conn_out_get(af, iph->protocol,
396 &iph->saddr, pptr[0],
397 &iph->daddr, pptr[1]);
398 else
399 return ip_vs_conn_out_get(af, iph->protocol,
400 &iph->daddr, pptr[1],
401 &iph->saddr, pptr[0]);
402}
403EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * Put back the conn and restart its timer with its timeout
407 */
408void ip_vs_conn_put(struct ip_vs_conn *cp)
409{
Nick Chalk26ec0372010-06-22 08:07:01 +0200410 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
411 0 : cp->timeout;
412 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 __ip_vs_conn_put(cp);
415}
416
417
418/*
419 * Fill a no_client_port connection with a client port number
420 */
Al Viro014d7302006-09-28 14:29:52 -0700421void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 if (ip_vs_conn_unhash(cp)) {
424 spin_lock(&cp->lock);
425 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
426 atomic_dec(&ip_vs_conn_no_cport_cnt);
427 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
428 cp->cport = cport;
429 }
430 spin_unlock(&cp->lock);
431
432 /* hash on new dport */
433 ip_vs_conn_hash(cp);
434 }
435}
436
437
438/*
439 * Bind a connection entry with the corresponding packet_xmit.
440 * Called by ip_vs_conn_new.
441 */
442static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
443{
444 switch (IP_VS_FWD_METHOD(cp)) {
445 case IP_VS_CONN_F_MASQ:
446 cp->packet_xmit = ip_vs_nat_xmit;
447 break;
448
449 case IP_VS_CONN_F_TUNNEL:
450 cp->packet_xmit = ip_vs_tunnel_xmit;
451 break;
452
453 case IP_VS_CONN_F_DROUTE:
454 cp->packet_xmit = ip_vs_dr_xmit;
455 break;
456
457 case IP_VS_CONN_F_LOCALNODE:
458 cp->packet_xmit = ip_vs_null_xmit;
459 break;
460
461 case IP_VS_CONN_F_BYPASS:
462 cp->packet_xmit = ip_vs_bypass_xmit;
463 break;
464 }
465}
466
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200467#ifdef CONFIG_IP_VS_IPV6
468static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
469{
470 switch (IP_VS_FWD_METHOD(cp)) {
471 case IP_VS_CONN_F_MASQ:
472 cp->packet_xmit = ip_vs_nat_xmit_v6;
473 break;
474
475 case IP_VS_CONN_F_TUNNEL:
476 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
477 break;
478
479 case IP_VS_CONN_F_DROUTE:
480 cp->packet_xmit = ip_vs_dr_xmit_v6;
481 break;
482
483 case IP_VS_CONN_F_LOCALNODE:
484 cp->packet_xmit = ip_vs_null_xmit;
485 break;
486
487 case IP_VS_CONN_F_BYPASS:
488 cp->packet_xmit = ip_vs_bypass_xmit_v6;
489 break;
490 }
491}
492#endif
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
496{
497 return atomic_read(&dest->activeconns)
498 + atomic_read(&dest->inactconns);
499}
500
501/*
502 * Bind a connection entry with a virtual service destination
503 * Called just after a new connection entry is created.
504 */
505static inline void
506ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
507{
Julian Anastasov35757922010-09-17 14:18:16 +0200508 unsigned int conn_flags;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* if dest is NULL, then return directly */
511 if (!dest)
512 return;
513
514 /* Increase the refcnt counter of the dest */
515 atomic_inc(&dest->refcnt);
516
Julian Anastasov35757922010-09-17 14:18:16 +0200517 conn_flags = atomic_read(&dest->conn_flags);
518 if (cp->protocol != IPPROTO_UDP)
519 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Bind with the destination and its corresponding transmitter */
Julian Anastasov35757922010-09-17 14:18:16 +0200521 if (cp->flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800522 /* if the connection is not template and is created
523 * by sync, preserve the activity flag.
524 */
Julian Anastasov35757922010-09-17 14:18:16 +0200525 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE))
526 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
527 }
528 cp->flags |= conn_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 cp->dest = dest;
530
Julius Volzcfc78c52008-09-02 15:55:53 +0200531 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
532 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
533 "dest->refcnt:%d\n",
534 ip_vs_proto_name(cp->protocol),
535 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
536 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
537 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
538 ip_vs_fwd_tag(cp), cp->state,
539 cp->flags, atomic_read(&cp->refcnt),
540 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700543 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* It is a normal connection, so increase the inactive
545 connection counter because it is in TCP SYNRECV
546 state (inactive) or other protocol inacive state */
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800547 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
548 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
549 atomic_inc(&dest->activeconns);
550 else
551 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 } else {
553 /* It is a persistent connection/template, so increase
554 the peristent connection counter */
555 atomic_inc(&dest->persistconns);
556 }
557
558 if (dest->u_threshold != 0 &&
559 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
560 dest->flags |= IP_VS_DEST_F_OVERLOAD;
561}
562
563
564/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800565 * Check if there is a destination for the connection, if so
566 * bind the connection to the destination.
567 */
568struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
569{
570 struct ip_vs_dest *dest;
571
572 if ((cp) && (!cp->dest)) {
Julius Volz7937df12008-09-02 15:55:48 +0200573 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
574 &cp->vaddr, cp->vport,
575 cp->protocol);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800576 ip_vs_bind_dest(cp, dest);
577 return dest;
578 } else
579 return NULL;
580}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800581
582
583/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Unbind a connection entry with its VS destination
585 * Called by the ip_vs_conn_expire function.
586 */
587static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
588{
589 struct ip_vs_dest *dest = cp->dest;
590
591 if (!dest)
592 return;
593
Julius Volzcfc78c52008-09-02 15:55:53 +0200594 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
595 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
596 "dest->refcnt:%d\n",
597 ip_vs_proto_name(cp->protocol),
598 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
599 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
600 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
601 ip_vs_fwd_tag(cp), cp->state,
602 cp->flags, atomic_read(&cp->refcnt),
603 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700606 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* It is a normal connection, so decrease the inactconns
608 or activeconns counter */
609 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
610 atomic_dec(&dest->inactconns);
611 } else {
612 atomic_dec(&dest->activeconns);
613 }
614 } else {
615 /* It is a persistent connection/template, so decrease
616 the peristent connection counter */
617 atomic_dec(&dest->persistconns);
618 }
619
620 if (dest->l_threshold != 0) {
621 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
622 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
623 } else if (dest->u_threshold != 0) {
624 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
625 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
626 } else {
627 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
628 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
629 }
630
631 /*
632 * Simply decrease the refcnt of the dest, because the
633 * dest will be either in service's destination list
634 * or in the trash.
635 */
636 atomic_dec(&dest->refcnt);
637}
638
639
640/*
641 * Checking if the destination of a connection template is available.
642 * If available, return 1, otherwise invalidate this connection
643 * template and return 0.
644 */
645int ip_vs_check_template(struct ip_vs_conn *ct)
646{
647 struct ip_vs_dest *dest = ct->dest;
648
649 /*
650 * Checking the dest server status.
651 */
652 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900653 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
654 (sysctl_ip_vs_expire_quiescent_template &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 (atomic_read(&dest->weight) == 0))) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200656 IP_VS_DBG_BUF(9, "check_template: dest not available for "
657 "protocol %s s:%s:%d v:%s:%d "
658 "-> d:%s:%d\n",
659 ip_vs_proto_name(ct->protocol),
660 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
661 ntohs(ct->cport),
662 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
663 ntohs(ct->vport),
664 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
665 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
668 * Invalidate the connection template
669 */
Al Viro014d7302006-09-28 14:29:52 -0700670 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700672 ct->dport = htons(0xffff);
673 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ct->cport = 0;
675 ip_vs_conn_hash(ct);
676 }
677 }
678
679 /*
680 * Simply decrease the refcnt of the template,
681 * don't restart its timer.
682 */
683 atomic_dec(&ct->refcnt);
684 return 0;
685 }
686 return 1;
687}
688
689static void ip_vs_conn_expire(unsigned long data)
690{
691 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
692
693 cp->timeout = 60*HZ;
694
695 /*
696 * hey, I'm using it
697 */
698 atomic_inc(&cp->refcnt);
699
700 /*
701 * do I control anybody?
702 */
703 if (atomic_read(&cp->n_control))
704 goto expire_later;
705
706 /*
707 * unhash it if it is hashed in the conn table
708 */
Nick Chalk26ec0372010-06-22 08:07:01 +0200709 if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto expire_later;
711
712 /*
713 * refcnt==1 implies I'm the only one referrer
714 */
715 if (likely(atomic_read(&cp->refcnt) == 1)) {
716 /* delete the timer if it is activated by other users */
717 if (timer_pending(&cp->timer))
718 del_timer(&cp->timer);
719
720 /* does anybody control me? */
721 if (cp->control)
722 ip_vs_control_del(cp);
723
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200724 if (cp->flags & IP_VS_CONN_F_NFCT)
725 ip_vs_conn_drop_conntrack(cp);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (unlikely(cp->app != NULL))
728 ip_vs_unbind_app(cp);
729 ip_vs_unbind_dest(cp);
730 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
731 atomic_dec(&ip_vs_conn_no_cport_cnt);
732 atomic_dec(&ip_vs_conn_count);
733
734 kmem_cache_free(ip_vs_conn_cachep, cp);
735 return;
736 }
737
738 /* hash it back to the table */
739 ip_vs_conn_hash(cp);
740
741 expire_later:
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800742 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 atomic_read(&cp->refcnt)-1,
744 atomic_read(&cp->n_control));
745
746 ip_vs_conn_put(cp);
747}
748
749
750void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
751{
752 if (del_timer(&cp->timer))
753 mod_timer(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756
757/*
758 * Create a new connection entry and hash it into the ip_vs_conn_tab
759 */
760struct ip_vs_conn *
Julius Volz28364a52008-09-02 15:55:43 +0200761ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
762 const union nf_inet_addr *vaddr, __be16 vport,
763 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct ip_vs_dest *dest)
765{
766 struct ip_vs_conn *cp;
767 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
768
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800769 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000771 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return NULL;
773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 INIT_LIST_HEAD(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800776 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Julius Volz28364a52008-09-02 15:55:43 +0200777 cp->af = af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cp->protocol = proto;
Julius Volz28364a52008-09-02 15:55:43 +0200779 ip_vs_addr_copy(af, &cp->caddr, caddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 cp->cport = cport;
Julius Volz28364a52008-09-02 15:55:43 +0200781 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 cp->vport = vport;
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000783 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
784 ip_vs_addr_copy(proto == IPPROTO_IP ? AF_UNSPEC : af,
785 &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 cp->dport = dport;
787 cp->flags = flags;
788 spin_lock_init(&cp->lock);
789
790 /*
791 * Set the entry is referenced by the current thread before hashing
792 * it in the table, so that other thread run ip_vs_random_dropentry
793 * but cannot drop this entry.
794 */
795 atomic_set(&cp->refcnt, 1);
796
797 atomic_set(&cp->n_control, 0);
798 atomic_set(&cp->in_pkts, 0);
799
800 atomic_inc(&ip_vs_conn_count);
801 if (flags & IP_VS_CONN_F_NO_CPORT)
802 atomic_inc(&ip_vs_conn_no_cport_cnt);
803
804 /* Bind the connection with a destination server */
805 ip_vs_bind_dest(cp, dest);
806
807 /* Set its state and timeout */
808 cp->state = 0;
809 cp->timeout = 3*HZ;
810
811 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200812#ifdef CONFIG_IP_VS_IPV6
813 if (af == AF_INET6)
814 ip_vs_bind_xmit_v6(cp);
815 else
816#endif
817 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (unlikely(pp && atomic_read(&pp->appcnt)))
820 ip_vs_bind_app(cp, pp);
821
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200822 /*
823 * Allow conntrack to be preserved. By default, conntrack
824 * is created and destroyed for every packet.
825 * Sometimes keeping conntrack can be useful for
826 * IP_VS_CONN_F_ONE_PACKET too.
827 */
828
829 if (ip_vs_conntrack_enabled())
830 cp->flags |= IP_VS_CONN_F_NFCT;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Hash it in the ip_vs_conn_tab finally */
833 ip_vs_conn_hash(cp);
834
835 return cp;
836}
837
838
839/*
840 * /proc/net/ip_vs_conn entries
841 */
842#ifdef CONFIG_PROC_FS
843
844static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
845{
846 int idx;
847 struct ip_vs_conn *cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900848
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100849 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ct_read_lock_bh(idx);
851 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
852 if (pos-- == 0) {
853 seq->private = &ip_vs_conn_tab[idx];
854 return cp;
855 }
856 }
857 ct_read_unlock_bh(idx);
858 }
859
860 return NULL;
861}
862
863static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
864{
865 seq->private = NULL;
866 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
867}
868
869static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
870{
871 struct ip_vs_conn *cp = v;
872 struct list_head *e, *l = seq->private;
873 int idx;
874
875 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900876 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return ip_vs_conn_array(seq, 0);
878
879 /* more on same hash chain? */
880 if ((e = cp->c_list.next) != l)
881 return list_entry(e, struct ip_vs_conn, c_list);
882
883 idx = l - ip_vs_conn_tab;
884 ct_read_unlock_bh(idx);
885
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100886 while (++idx < ip_vs_conn_tab_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ct_read_lock_bh(idx);
888 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
889 seq->private = &ip_vs_conn_tab[idx];
890 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ct_read_unlock_bh(idx);
893 }
894 seq->private = NULL;
895 return NULL;
896}
897
898static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
899{
900 struct list_head *l = seq->private;
901
902 if (l)
903 ct_read_unlock_bh(l - ip_vs_conn_tab);
904}
905
906static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
907{
908
909 if (v == SEQ_START_TOKEN)
910 seq_puts(seq,
911 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
912 else {
913 const struct ip_vs_conn *cp = v;
914
Vince Busam667a5f12008-09-02 15:55:49 +0200915#ifdef CONFIG_IP_VS_IPV6
916 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700917 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +0200918 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700919 &cp->caddr.in6, ntohs(cp->cport),
920 &cp->vaddr.in6, ntohs(cp->vport),
921 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +0200922 ip_vs_state_name(cp->protocol, cp->state),
923 (cp->timer.expires-jiffies)/HZ);
924 else
925#endif
926 seq_printf(seq,
927 "%-3s %08X %04X %08X %04X"
928 " %08X %04X %-11s %7lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +0200930 ntohl(cp->caddr.ip), ntohs(cp->cport),
931 ntohl(cp->vaddr.ip), ntohs(cp->vport),
932 ntohl(cp->daddr.ip), ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ip_vs_state_name(cp->protocol, cp->state),
934 (cp->timer.expires-jiffies)/HZ);
935 }
936 return 0;
937}
938
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700939static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .start = ip_vs_conn_seq_start,
941 .next = ip_vs_conn_seq_next,
942 .stop = ip_vs_conn_seq_stop,
943 .show = ip_vs_conn_seq_show,
944};
945
946static int ip_vs_conn_open(struct inode *inode, struct file *file)
947{
948 return seq_open(file, &ip_vs_conn_seq_ops);
949}
950
Arjan van de Ven9a321442007-02-12 00:55:35 -0800951static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 .owner = THIS_MODULE,
953 .open = ip_vs_conn_open,
954 .read = seq_read,
955 .llseek = seq_lseek,
956 .release = seq_release,
957};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800958
959static const char *ip_vs_origin_name(unsigned flags)
960{
961 if (flags & IP_VS_CONN_F_SYNC)
962 return "SYNC";
963 else
964 return "LOCAL";
965}
966
967static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
968{
969
970 if (v == SEQ_START_TOKEN)
971 seq_puts(seq,
972 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
973 else {
974 const struct ip_vs_conn *cp = v;
975
Vince Busam667a5f12008-09-02 15:55:49 +0200976#ifdef CONFIG_IP_VS_IPV6
977 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700978 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +0200979 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700980 &cp->caddr.in6, ntohs(cp->cport),
981 &cp->vaddr.in6, ntohs(cp->vport),
982 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +0200983 ip_vs_state_name(cp->protocol, cp->state),
984 ip_vs_origin_name(cp->flags),
985 (cp->timer.expires-jiffies)/HZ);
986 else
987#endif
988 seq_printf(seq,
989 "%-3s %08X %04X %08X %04X "
990 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800991 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +0200992 ntohl(cp->caddr.ip), ntohs(cp->cport),
993 ntohl(cp->vaddr.ip), ntohs(cp->vport),
994 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800995 ip_vs_state_name(cp->protocol, cp->state),
996 ip_vs_origin_name(cp->flags),
997 (cp->timer.expires-jiffies)/HZ);
998 }
999 return 0;
1000}
1001
1002static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1003 .start = ip_vs_conn_seq_start,
1004 .next = ip_vs_conn_seq_next,
1005 .stop = ip_vs_conn_seq_stop,
1006 .show = ip_vs_conn_sync_seq_show,
1007};
1008
1009static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1010{
1011 return seq_open(file, &ip_vs_conn_sync_seq_ops);
1012}
1013
1014static const struct file_operations ip_vs_conn_sync_fops = {
1015 .owner = THIS_MODULE,
1016 .open = ip_vs_conn_sync_open,
1017 .read = seq_read,
1018 .llseek = seq_lseek,
1019 .release = seq_release,
1020};
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#endif
1023
1024
1025/*
1026 * Randomly drop connection entries before running out of memory
1027 */
1028static inline int todrop_entry(struct ip_vs_conn *cp)
1029{
1030 /*
1031 * The drop rate array needs tuning for real environments.
1032 * Called from timer bh only => no locking
1033 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001034 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 static char todrop_counter[9] = {0};
1036 int i;
1037
1038 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1039 This will leave enough time for normal connection to get
1040 through. */
1041 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1042 return 0;
1043
1044 /* Don't drop the entry if its number of incoming packets is not
1045 located in [0, 8] */
1046 i = atomic_read(&cp->in_pkts);
1047 if (i > 8 || i < 0) return 0;
1048
1049 if (!todrop_rate[i]) return 0;
1050 if (--todrop_counter[i] > 0) return 0;
1051
1052 todrop_counter[i] = todrop_rate[i];
1053 return 1;
1054}
1055
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001056/* Called from keventd and must protect itself from softirqs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057void ip_vs_random_dropentry(void)
1058{
1059 int idx;
1060 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /*
1063 * Randomly scan 1/32 of the whole table every second
1064 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001065 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1066 unsigned hash = net_random() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /*
1069 * Lock is actually needed in this loop.
1070 */
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001071 ct_write_lock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001074 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* connection template */
1076 continue;
1077
1078 if (cp->protocol == IPPROTO_TCP) {
1079 switch(cp->state) {
1080 case IP_VS_TCP_S_SYN_RECV:
1081 case IP_VS_TCP_S_SYNACK:
1082 break;
1083
1084 case IP_VS_TCP_S_ESTABLISHED:
1085 if (todrop_entry(cp))
1086 break;
1087 continue;
1088
1089 default:
1090 continue;
1091 }
1092 } else {
1093 if (!todrop_entry(cp))
1094 continue;
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 IP_VS_DBG(4, "del connection\n");
1098 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001099 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001101 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001104 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
1108
1109/*
1110 * Flush all the connection entries in the ip_vs_conn_tab
1111 */
1112static void ip_vs_conn_flush(void)
1113{
1114 int idx;
1115 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 flush_again:
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001118 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Lock is actually needed in this loop.
1121 */
1122 ct_write_lock_bh(idx);
1123
1124 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 IP_VS_DBG(4, "del connection\n");
1127 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001128 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001130 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 ct_write_unlock_bh(idx);
1134 }
1135
1136 /* the counter may be not NULL, because maybe some conn entries
1137 are run by slow timer handler or unhashed but still referred */
1138 if (atomic_read(&ip_vs_conn_count) != 0) {
1139 schedule();
1140 goto flush_again;
1141 }
1142}
1143
1144
Sven Wegener048cf482008-08-10 18:24:35 +00001145int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int idx;
1148
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001149 /* Compute size and mask */
1150 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1151 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Allocate the connection hash table and initialize its list heads
1155 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001156 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size *
1157 sizeof(struct list_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!ip_vs_conn_tab)
1159 return -ENOMEM;
1160
1161 /* Allocate ip_vs_conn slab cache */
1162 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1163 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001164 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!ip_vs_conn_cachep) {
1166 vfree(ip_vs_conn_tab);
1167 return -ENOMEM;
1168 }
1169
Hannes Eder1e3e2382009-08-02 11:05:41 +00001170 pr_info("Connection hash table configured "
1171 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001172 ip_vs_conn_tab_size,
1173 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1175 sizeof(struct ip_vs_conn));
1176
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001177 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1179 }
1180
1181 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1182 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1183 }
1184
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001185 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001186 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* calculate the random value for connection hash */
1189 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1190
1191 return 0;
1192}
1193
1194
1195void ip_vs_conn_cleanup(void)
1196{
1197 /* flush all the connection entries first */
1198 ip_vs_conn_flush();
1199
1200 /* Release the empty cache */
1201 kmem_cache_destroy(ip_vs_conn_cachep);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001202 proc_net_remove(&init_net, "ip_vs_conn");
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001203 proc_net_remove(&init_net, "ip_vs_conn_sync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 vfree(ip_vs_conn_tab);
1205}