blob: deeb906a797b22ab8ea4b83209a6fa638637b846 [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.
Simon Hormanf11017e2010-08-22 21:37:52 +0900221 * p->caddr, p->cport: pkt source address (foreign host)
222 * p->vaddr, p->vport: pkt dest address (load balancer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900224static inline struct ip_vs_conn *
225__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned hash;
228 struct ip_vs_conn *cp;
229
Simon Hormanf11017e2010-08-22 21:37:52 +0900230 hash = ip_vs_conn_hashkey(p->af, p->protocol, p->caddr, p->cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 ct_read_lock(hash);
233
234 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900235 if (cp->af == p->af &&
236 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
237 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
238 p->cport == cp->cport && p->vport == cp->vport &&
239 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
240 p->protocol == cp->protocol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* HIT */
242 atomic_inc(&cp->refcnt);
243 ct_read_unlock(hash);
244 return cp;
245 }
246 }
247
248 ct_read_unlock(hash);
249
250 return NULL;
251}
252
Simon Hormanf11017e2010-08-22 21:37:52 +0900253struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct ip_vs_conn *cp;
256
Simon Hormanf11017e2010-08-22 21:37:52 +0900257 cp = __ip_vs_conn_in_get(p);
258 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
259 struct ip_vs_conn_param cport_zero_p = *p;
260 cport_zero_p.cport = 0;
261 cp = __ip_vs_conn_in_get(&cport_zero_p);
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Julius Volz28364a52008-09-02 15:55:43 +0200264 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900265 ip_vs_proto_name(p->protocol),
266 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
267 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200268 cp ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return cp;
271}
272
Simon Hormanf11017e2010-08-22 21:37:52 +0900273static int
274ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
275 const struct ip_vs_iphdr *iph,
276 unsigned int proto_off, int inverse,
277 struct ip_vs_conn_param *p)
278{
279 __be16 _ports[2], *pptr;
280
281 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
282 if (pptr == NULL)
283 return 1;
284
285 if (likely(!inverse))
286 ip_vs_conn_fill_param(af, iph->protocol, &iph->saddr, pptr[0],
287 &iph->daddr, pptr[1], p);
288 else
289 ip_vs_conn_fill_param(af, iph->protocol, &iph->daddr, pptr[1],
290 &iph->saddr, pptr[0], p);
291 return 0;
292}
293
Simon Horman5c0d2372010-08-02 17:12:44 +0200294struct ip_vs_conn *
295ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
296 struct ip_vs_protocol *pp,
297 const struct ip_vs_iphdr *iph,
298 unsigned int proto_off, int inverse)
299{
Simon Hormanf11017e2010-08-22 21:37:52 +0900300 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200301
Simon Hormanf11017e2010-08-22 21:37:52 +0900302 if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200303 return NULL;
304
Simon Hormanf11017e2010-08-22 21:37:52 +0900305 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200306}
307EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
308
Julian Anastasov87375ab2005-09-14 21:08:51 -0700309/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900310struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700311{
312 unsigned hash;
313 struct ip_vs_conn *cp;
314
Simon Hormanf11017e2010-08-22 21:37:52 +0900315 hash = ip_vs_conn_hashkey(p->af, p->protocol, p->caddr, p->cport);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700316
317 ct_read_lock(hash);
318
319 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900320 if (cp->af == p->af &&
321 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000322 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900323 * p->vaddr is a fwmark */
324 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
325 p->af, p->vaddr, &cp->vaddr) &&
326 p->cport == cp->cport && p->vport == cp->vport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700327 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900328 p->protocol == cp->protocol) {
Julian Anastasov87375ab2005-09-14 21:08:51 -0700329 /* HIT */
330 atomic_inc(&cp->refcnt);
331 goto out;
332 }
333 }
334 cp = NULL;
335
336 out:
337 ct_read_unlock(hash);
338
Julius Volz28364a52008-09-02 15:55:43 +0200339 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900340 ip_vs_proto_name(p->protocol),
341 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
342 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200343 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700344
345 return cp;
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Simon Hormanf11017e2010-08-22 21:37:52 +0900348/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
349 * Called for pkts coming from inside-to-OUTside.
350 * p->caddr, p->cport: pkt source address (inside host)
351 * p->vaddr, p->vport: pkt dest address (foreign host) */
352struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unsigned hash;
355 struct ip_vs_conn *cp, *ret=NULL;
356
357 /*
358 * Check for "full" addressed entries
359 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900360 hash = ip_vs_conn_hashkey(p->af, p->protocol, p->vaddr, p->vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 ct_read_lock(hash);
363
364 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900365 if (cp->af == p->af &&
366 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
367 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
368 p->vport == cp->cport && p->cport == cp->dport &&
369 p->protocol == cp->protocol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* HIT */
371 atomic_inc(&cp->refcnt);
372 ret = cp;
373 break;
374 }
375 }
376
377 ct_read_unlock(hash);
378
Julius Volz28364a52008-09-02 15:55:43 +0200379 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900380 ip_vs_proto_name(p->protocol),
381 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
382 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200383 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return ret;
386}
387
Simon Horman5c0d2372010-08-02 17:12:44 +0200388struct ip_vs_conn *
389ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
390 struct ip_vs_protocol *pp,
391 const struct ip_vs_iphdr *iph,
392 unsigned int proto_off, int inverse)
393{
Simon Hormanf11017e2010-08-22 21:37:52 +0900394 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200395
Simon Hormanf11017e2010-08-22 21:37:52 +0900396 if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200397 return NULL;
398
Simon Hormanf11017e2010-08-22 21:37:52 +0900399 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200400}
401EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/*
404 * Put back the conn and restart its timer with its timeout
405 */
406void ip_vs_conn_put(struct ip_vs_conn *cp)
407{
Nick Chalk26ec0372010-06-22 08:07:01 +0200408 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
409 0 : cp->timeout;
410 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 __ip_vs_conn_put(cp);
413}
414
415
416/*
417 * Fill a no_client_port connection with a client port number
418 */
Al Viro014d7302006-09-28 14:29:52 -0700419void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 if (ip_vs_conn_unhash(cp)) {
422 spin_lock(&cp->lock);
423 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
424 atomic_dec(&ip_vs_conn_no_cport_cnt);
425 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
426 cp->cport = cport;
427 }
428 spin_unlock(&cp->lock);
429
430 /* hash on new dport */
431 ip_vs_conn_hash(cp);
432 }
433}
434
435
436/*
437 * Bind a connection entry with the corresponding packet_xmit.
438 * Called by ip_vs_conn_new.
439 */
440static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
441{
442 switch (IP_VS_FWD_METHOD(cp)) {
443 case IP_VS_CONN_F_MASQ:
444 cp->packet_xmit = ip_vs_nat_xmit;
445 break;
446
447 case IP_VS_CONN_F_TUNNEL:
448 cp->packet_xmit = ip_vs_tunnel_xmit;
449 break;
450
451 case IP_VS_CONN_F_DROUTE:
452 cp->packet_xmit = ip_vs_dr_xmit;
453 break;
454
455 case IP_VS_CONN_F_LOCALNODE:
456 cp->packet_xmit = ip_vs_null_xmit;
457 break;
458
459 case IP_VS_CONN_F_BYPASS:
460 cp->packet_xmit = ip_vs_bypass_xmit;
461 break;
462 }
463}
464
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200465#ifdef CONFIG_IP_VS_IPV6
466static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
467{
468 switch (IP_VS_FWD_METHOD(cp)) {
469 case IP_VS_CONN_F_MASQ:
470 cp->packet_xmit = ip_vs_nat_xmit_v6;
471 break;
472
473 case IP_VS_CONN_F_TUNNEL:
474 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
475 break;
476
477 case IP_VS_CONN_F_DROUTE:
478 cp->packet_xmit = ip_vs_dr_xmit_v6;
479 break;
480
481 case IP_VS_CONN_F_LOCALNODE:
482 cp->packet_xmit = ip_vs_null_xmit;
483 break;
484
485 case IP_VS_CONN_F_BYPASS:
486 cp->packet_xmit = ip_vs_bypass_xmit_v6;
487 break;
488 }
489}
490#endif
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
494{
495 return atomic_read(&dest->activeconns)
496 + atomic_read(&dest->inactconns);
497}
498
499/*
500 * Bind a connection entry with a virtual service destination
501 * Called just after a new connection entry is created.
502 */
503static inline void
504ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
505{
Julian Anastasov35757922010-09-17 14:18:16 +0200506 unsigned int conn_flags;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* if dest is NULL, then return directly */
509 if (!dest)
510 return;
511
512 /* Increase the refcnt counter of the dest */
513 atomic_inc(&dest->refcnt);
514
Julian Anastasov35757922010-09-17 14:18:16 +0200515 conn_flags = atomic_read(&dest->conn_flags);
516 if (cp->protocol != IPPROTO_UDP)
517 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Bind with the destination and its corresponding transmitter */
Julian Anastasov35757922010-09-17 14:18:16 +0200519 if (cp->flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800520 /* if the connection is not template and is created
521 * by sync, preserve the activity flag.
522 */
Julian Anastasov35757922010-09-17 14:18:16 +0200523 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE))
524 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
525 }
526 cp->flags |= conn_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 cp->dest = dest;
528
Julius Volzcfc78c52008-09-02 15:55:53 +0200529 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
530 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
531 "dest->refcnt:%d\n",
532 ip_vs_proto_name(cp->protocol),
533 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
534 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
535 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
536 ip_vs_fwd_tag(cp), cp->state,
537 cp->flags, atomic_read(&cp->refcnt),
538 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700541 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* It is a normal connection, so increase the inactive
543 connection counter because it is in TCP SYNRECV
544 state (inactive) or other protocol inacive state */
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800545 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
546 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
547 atomic_inc(&dest->activeconns);
548 else
549 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 } else {
551 /* It is a persistent connection/template, so increase
552 the peristent connection counter */
553 atomic_inc(&dest->persistconns);
554 }
555
556 if (dest->u_threshold != 0 &&
557 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
558 dest->flags |= IP_VS_DEST_F_OVERLOAD;
559}
560
561
562/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800563 * Check if there is a destination for the connection, if so
564 * bind the connection to the destination.
565 */
566struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
567{
568 struct ip_vs_dest *dest;
569
570 if ((cp) && (!cp->dest)) {
Julius Volz7937df12008-09-02 15:55:48 +0200571 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
572 &cp->vaddr, cp->vport,
573 cp->protocol);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800574 ip_vs_bind_dest(cp, dest);
575 return dest;
576 } else
577 return NULL;
578}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800579
580
581/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * Unbind a connection entry with its VS destination
583 * Called by the ip_vs_conn_expire function.
584 */
585static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
586{
587 struct ip_vs_dest *dest = cp->dest;
588
589 if (!dest)
590 return;
591
Julius Volzcfc78c52008-09-02 15:55:53 +0200592 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
593 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
594 "dest->refcnt:%d\n",
595 ip_vs_proto_name(cp->protocol),
596 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
597 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
598 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
599 ip_vs_fwd_tag(cp), cp->state,
600 cp->flags, atomic_read(&cp->refcnt),
601 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700604 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* It is a normal connection, so decrease the inactconns
606 or activeconns counter */
607 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
608 atomic_dec(&dest->inactconns);
609 } else {
610 atomic_dec(&dest->activeconns);
611 }
612 } else {
613 /* It is a persistent connection/template, so decrease
614 the peristent connection counter */
615 atomic_dec(&dest->persistconns);
616 }
617
618 if (dest->l_threshold != 0) {
619 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
620 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
621 } else if (dest->u_threshold != 0) {
622 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
623 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
624 } else {
625 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
626 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
627 }
628
629 /*
630 * Simply decrease the refcnt of the dest, because the
631 * dest will be either in service's destination list
632 * or in the trash.
633 */
634 atomic_dec(&dest->refcnt);
635}
636
637
638/*
639 * Checking if the destination of a connection template is available.
640 * If available, return 1, otherwise invalidate this connection
641 * template and return 0.
642 */
643int ip_vs_check_template(struct ip_vs_conn *ct)
644{
645 struct ip_vs_dest *dest = ct->dest;
646
647 /*
648 * Checking the dest server status.
649 */
650 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900651 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
652 (sysctl_ip_vs_expire_quiescent_template &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 (atomic_read(&dest->weight) == 0))) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200654 IP_VS_DBG_BUF(9, "check_template: dest not available for "
655 "protocol %s s:%s:%d v:%s:%d "
656 "-> d:%s:%d\n",
657 ip_vs_proto_name(ct->protocol),
658 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
659 ntohs(ct->cport),
660 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
661 ntohs(ct->vport),
662 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
663 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * Invalidate the connection template
667 */
Al Viro014d7302006-09-28 14:29:52 -0700668 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700670 ct->dport = htons(0xffff);
671 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ct->cport = 0;
673 ip_vs_conn_hash(ct);
674 }
675 }
676
677 /*
678 * Simply decrease the refcnt of the template,
679 * don't restart its timer.
680 */
681 atomic_dec(&ct->refcnt);
682 return 0;
683 }
684 return 1;
685}
686
687static void ip_vs_conn_expire(unsigned long data)
688{
689 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
690
691 cp->timeout = 60*HZ;
692
693 /*
694 * hey, I'm using it
695 */
696 atomic_inc(&cp->refcnt);
697
698 /*
699 * do I control anybody?
700 */
701 if (atomic_read(&cp->n_control))
702 goto expire_later;
703
704 /*
705 * unhash it if it is hashed in the conn table
706 */
Nick Chalk26ec0372010-06-22 08:07:01 +0200707 if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto expire_later;
709
710 /*
711 * refcnt==1 implies I'm the only one referrer
712 */
713 if (likely(atomic_read(&cp->refcnt) == 1)) {
714 /* delete the timer if it is activated by other users */
715 if (timer_pending(&cp->timer))
716 del_timer(&cp->timer);
717
718 /* does anybody control me? */
719 if (cp->control)
720 ip_vs_control_del(cp);
721
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200722 if (cp->flags & IP_VS_CONN_F_NFCT)
723 ip_vs_conn_drop_conntrack(cp);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (unlikely(cp->app != NULL))
726 ip_vs_unbind_app(cp);
727 ip_vs_unbind_dest(cp);
728 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
729 atomic_dec(&ip_vs_conn_no_cport_cnt);
730 atomic_dec(&ip_vs_conn_count);
731
732 kmem_cache_free(ip_vs_conn_cachep, cp);
733 return;
734 }
735
736 /* hash it back to the table */
737 ip_vs_conn_hash(cp);
738
739 expire_later:
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800740 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 atomic_read(&cp->refcnt)-1,
742 atomic_read(&cp->n_control));
743
744 ip_vs_conn_put(cp);
745}
746
747
748void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
749{
750 if (del_timer(&cp->timer))
751 mod_timer(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754
755/*
756 * Create a new connection entry and hash it into the ip_vs_conn_tab
757 */
758struct ip_vs_conn *
Simon Hormanf11017e2010-08-22 21:37:52 +0900759ip_vs_conn_new(const struct ip_vs_conn_param *p,
Julius Volz28364a52008-09-02 15:55:43 +0200760 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct ip_vs_dest *dest)
762{
763 struct ip_vs_conn *cp;
Simon Hormanf11017e2010-08-22 21:37:52 +0900764 struct ip_vs_protocol *pp = ip_vs_proto_get(p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800766 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000768 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return NULL;
770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 INIT_LIST_HEAD(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800773 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Simon Hormanf11017e2010-08-22 21:37:52 +0900774 cp->af = p->af;
775 cp->protocol = p->protocol;
776 ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
777 cp->cport = p->cport;
778 ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
779 cp->vport = p->vport;
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000780 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
Simon Hormanf11017e2010-08-22 21:37:52 +0900781 ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000782 &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 cp->dport = dport;
784 cp->flags = flags;
785 spin_lock_init(&cp->lock);
786
787 /*
788 * Set the entry is referenced by the current thread before hashing
789 * it in the table, so that other thread run ip_vs_random_dropentry
790 * but cannot drop this entry.
791 */
792 atomic_set(&cp->refcnt, 1);
793
794 atomic_set(&cp->n_control, 0);
795 atomic_set(&cp->in_pkts, 0);
796
797 atomic_inc(&ip_vs_conn_count);
798 if (flags & IP_VS_CONN_F_NO_CPORT)
799 atomic_inc(&ip_vs_conn_no_cport_cnt);
800
801 /* Bind the connection with a destination server */
802 ip_vs_bind_dest(cp, dest);
803
804 /* Set its state and timeout */
805 cp->state = 0;
806 cp->timeout = 3*HZ;
807
808 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200809#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900810 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200811 ip_vs_bind_xmit_v6(cp);
812 else
813#endif
814 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (unlikely(pp && atomic_read(&pp->appcnt)))
817 ip_vs_bind_app(cp, pp);
818
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200819 /*
820 * Allow conntrack to be preserved. By default, conntrack
821 * is created and destroyed for every packet.
822 * Sometimes keeping conntrack can be useful for
823 * IP_VS_CONN_F_ONE_PACKET too.
824 */
825
826 if (ip_vs_conntrack_enabled())
827 cp->flags |= IP_VS_CONN_F_NFCT;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Hash it in the ip_vs_conn_tab finally */
830 ip_vs_conn_hash(cp);
831
832 return cp;
833}
834
835
836/*
837 * /proc/net/ip_vs_conn entries
838 */
839#ifdef CONFIG_PROC_FS
840
841static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
842{
843 int idx;
844 struct ip_vs_conn *cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900845
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100846 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ct_read_lock_bh(idx);
848 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
849 if (pos-- == 0) {
850 seq->private = &ip_vs_conn_tab[idx];
851 return cp;
852 }
853 }
854 ct_read_unlock_bh(idx);
855 }
856
857 return NULL;
858}
859
860static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
861{
862 seq->private = NULL;
863 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
864}
865
866static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
867{
868 struct ip_vs_conn *cp = v;
869 struct list_head *e, *l = seq->private;
870 int idx;
871
872 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900873 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return ip_vs_conn_array(seq, 0);
875
876 /* more on same hash chain? */
877 if ((e = cp->c_list.next) != l)
878 return list_entry(e, struct ip_vs_conn, c_list);
879
880 idx = l - ip_vs_conn_tab;
881 ct_read_unlock_bh(idx);
882
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100883 while (++idx < ip_vs_conn_tab_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ct_read_lock_bh(idx);
885 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
886 seq->private = &ip_vs_conn_tab[idx];
887 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ct_read_unlock_bh(idx);
890 }
891 seq->private = NULL;
892 return NULL;
893}
894
895static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
896{
897 struct list_head *l = seq->private;
898
899 if (l)
900 ct_read_unlock_bh(l - ip_vs_conn_tab);
901}
902
903static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
904{
905
906 if (v == SEQ_START_TOKEN)
907 seq_puts(seq,
908 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
909 else {
910 const struct ip_vs_conn *cp = v;
911
Vince Busam667a5f12008-09-02 15:55:49 +0200912#ifdef CONFIG_IP_VS_IPV6
913 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700914 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +0200915 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700916 &cp->caddr.in6, ntohs(cp->cport),
917 &cp->vaddr.in6, ntohs(cp->vport),
918 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +0200919 ip_vs_state_name(cp->protocol, cp->state),
920 (cp->timer.expires-jiffies)/HZ);
921 else
922#endif
923 seq_printf(seq,
924 "%-3s %08X %04X %08X %04X"
925 " %08X %04X %-11s %7lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +0200927 ntohl(cp->caddr.ip), ntohs(cp->cport),
928 ntohl(cp->vaddr.ip), ntohs(cp->vport),
929 ntohl(cp->daddr.ip), ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ip_vs_state_name(cp->protocol, cp->state),
931 (cp->timer.expires-jiffies)/HZ);
932 }
933 return 0;
934}
935
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700936static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 .start = ip_vs_conn_seq_start,
938 .next = ip_vs_conn_seq_next,
939 .stop = ip_vs_conn_seq_stop,
940 .show = ip_vs_conn_seq_show,
941};
942
943static int ip_vs_conn_open(struct inode *inode, struct file *file)
944{
945 return seq_open(file, &ip_vs_conn_seq_ops);
946}
947
Arjan van de Ven9a321442007-02-12 00:55:35 -0800948static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .owner = THIS_MODULE,
950 .open = ip_vs_conn_open,
951 .read = seq_read,
952 .llseek = seq_lseek,
953 .release = seq_release,
954};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800955
956static const char *ip_vs_origin_name(unsigned flags)
957{
958 if (flags & IP_VS_CONN_F_SYNC)
959 return "SYNC";
960 else
961 return "LOCAL";
962}
963
964static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
965{
966
967 if (v == SEQ_START_TOKEN)
968 seq_puts(seq,
969 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
970 else {
971 const struct ip_vs_conn *cp = v;
972
Vince Busam667a5f12008-09-02 15:55:49 +0200973#ifdef CONFIG_IP_VS_IPV6
974 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700975 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +0200976 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700977 &cp->caddr.in6, ntohs(cp->cport),
978 &cp->vaddr.in6, ntohs(cp->vport),
979 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +0200980 ip_vs_state_name(cp->protocol, cp->state),
981 ip_vs_origin_name(cp->flags),
982 (cp->timer.expires-jiffies)/HZ);
983 else
984#endif
985 seq_printf(seq,
986 "%-3s %08X %04X %08X %04X "
987 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800988 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +0200989 ntohl(cp->caddr.ip), ntohs(cp->cport),
990 ntohl(cp->vaddr.ip), ntohs(cp->vport),
991 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -0800992 ip_vs_state_name(cp->protocol, cp->state),
993 ip_vs_origin_name(cp->flags),
994 (cp->timer.expires-jiffies)/HZ);
995 }
996 return 0;
997}
998
999static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1000 .start = ip_vs_conn_seq_start,
1001 .next = ip_vs_conn_seq_next,
1002 .stop = ip_vs_conn_seq_stop,
1003 .show = ip_vs_conn_sync_seq_show,
1004};
1005
1006static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1007{
1008 return seq_open(file, &ip_vs_conn_sync_seq_ops);
1009}
1010
1011static const struct file_operations ip_vs_conn_sync_fops = {
1012 .owner = THIS_MODULE,
1013 .open = ip_vs_conn_sync_open,
1014 .read = seq_read,
1015 .llseek = seq_lseek,
1016 .release = seq_release,
1017};
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#endif
1020
1021
1022/*
1023 * Randomly drop connection entries before running out of memory
1024 */
1025static inline int todrop_entry(struct ip_vs_conn *cp)
1026{
1027 /*
1028 * The drop rate array needs tuning for real environments.
1029 * Called from timer bh only => no locking
1030 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001031 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 static char todrop_counter[9] = {0};
1033 int i;
1034
1035 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1036 This will leave enough time for normal connection to get
1037 through. */
1038 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1039 return 0;
1040
1041 /* Don't drop the entry if its number of incoming packets is not
1042 located in [0, 8] */
1043 i = atomic_read(&cp->in_pkts);
1044 if (i > 8 || i < 0) return 0;
1045
1046 if (!todrop_rate[i]) return 0;
1047 if (--todrop_counter[i] > 0) return 0;
1048
1049 todrop_counter[i] = todrop_rate[i];
1050 return 1;
1051}
1052
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001053/* Called from keventd and must protect itself from softirqs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054void ip_vs_random_dropentry(void)
1055{
1056 int idx;
1057 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /*
1060 * Randomly scan 1/32 of the whole table every second
1061 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001062 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1063 unsigned hash = net_random() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /*
1066 * Lock is actually needed in this loop.
1067 */
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001068 ct_write_lock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001071 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* connection template */
1073 continue;
1074
1075 if (cp->protocol == IPPROTO_TCP) {
1076 switch(cp->state) {
1077 case IP_VS_TCP_S_SYN_RECV:
1078 case IP_VS_TCP_S_SYNACK:
1079 break;
1080
1081 case IP_VS_TCP_S_ESTABLISHED:
1082 if (todrop_entry(cp))
1083 break;
1084 continue;
1085
1086 default:
1087 continue;
1088 }
1089 } else {
1090 if (!todrop_entry(cp))
1091 continue;
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 IP_VS_DBG(4, "del connection\n");
1095 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001096 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001098 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001101 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103}
1104
1105
1106/*
1107 * Flush all the connection entries in the ip_vs_conn_tab
1108 */
1109static void ip_vs_conn_flush(void)
1110{
1111 int idx;
1112 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 flush_again:
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001115 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Lock is actually needed in this loop.
1118 */
1119 ct_write_lock_bh(idx);
1120
1121 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 IP_VS_DBG(4, "del connection\n");
1124 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001125 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001127 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 ct_write_unlock_bh(idx);
1131 }
1132
1133 /* the counter may be not NULL, because maybe some conn entries
1134 are run by slow timer handler or unhashed but still referred */
1135 if (atomic_read(&ip_vs_conn_count) != 0) {
1136 schedule();
1137 goto flush_again;
1138 }
1139}
1140
1141
Sven Wegener048cf482008-08-10 18:24:35 +00001142int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 int idx;
1145
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001146 /* Compute size and mask */
1147 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1148 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Allocate the connection hash table and initialize its list heads
1152 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001153 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size *
1154 sizeof(struct list_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!ip_vs_conn_tab)
1156 return -ENOMEM;
1157
1158 /* Allocate ip_vs_conn slab cache */
1159 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1160 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001161 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!ip_vs_conn_cachep) {
1163 vfree(ip_vs_conn_tab);
1164 return -ENOMEM;
1165 }
1166
Hannes Eder1e3e2382009-08-02 11:05:41 +00001167 pr_info("Connection hash table configured "
1168 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001169 ip_vs_conn_tab_size,
1170 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1172 sizeof(struct ip_vs_conn));
1173
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001174 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1176 }
1177
1178 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1179 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1180 }
1181
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001182 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001183 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* calculate the random value for connection hash */
1186 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1187
1188 return 0;
1189}
1190
1191
1192void ip_vs_conn_cleanup(void)
1193{
1194 /* flush all the connection entries first */
1195 ip_vs_conn_flush();
1196
1197 /* Release the empty cache */
1198 kmem_cache_destroy(ip_vs_conn_cachep);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001199 proc_net_remove(&init_net, "ip_vs_conn");
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001200 proc_net_remove(&init_net, "ip_vs_conn_sync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 vfree(ip_vs_conn_tab);
1202}