blob: b2024c94234598e51874329ab237010c970ae05f [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*/
Eric Dumazet4ecd2942010-11-15 18:38:52 +010051static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010052module_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 */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010056int ip_vs_conn_tab_size __read_mostly;
57static int ip_vs_conn_tab_mask __read_mostly;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Connection hash table: for input and output packets lookups of IPVS
61 */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010062static struct list_head *ip_vs_conn_tab __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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 */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010074static unsigned int ip_vs_conn_rnd __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
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
Simon Horman85999282010-08-22 21:37:53 +0900151static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
152 bool inverse)
153{
154 const union nf_inet_addr *addr;
155 __be16 port;
156
Simon Hormanf71499a2010-08-22 21:37:54 +0900157 if (p->pe_data && p->pe->hashkey_raw)
Simon Horman85999282010-08-22 21:37:53 +0900158 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
159 ip_vs_conn_tab_mask;
160
161 if (likely(!inverse)) {
162 addr = p->caddr;
163 port = p->cport;
164 } else {
165 addr = p->vaddr;
166 port = p->vport;
167 }
168
169 return ip_vs_conn_hashkey(p->af, p->protocol, addr, port);
170}
171
172static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
173{
174 struct ip_vs_conn_param p;
175
176 ip_vs_conn_fill_param(cp->af, cp->protocol, &cp->caddr, cp->cport,
177 NULL, 0, &p);
178
Simon Hormane9e5eee2010-11-08 20:05:57 +0900179 if (cp->pe) {
180 p.pe = cp->pe;
Simon Horman85999282010-08-22 21:37:53 +0900181 p.pe_data = cp->pe_data;
182 p.pe_data_len = cp->pe_data_len;
183 }
184
185 return ip_vs_conn_hashkey_param(&p, false);
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
189 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
190 * returns bool success.
191 */
192static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
193{
194 unsigned hash;
195 int ret;
196
Nick Chalk26ec0372010-06-22 08:07:01 +0200197 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
198 return 0;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Hash by protocol, client address and port */
Simon Horman85999282010-08-22 21:37:53 +0900201 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200204 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
207 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
208 cp->flags |= IP_VS_CONN_F_HASHED;
209 atomic_inc(&cp->refcnt);
210 ret = 1;
211 } else {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000212 pr_err("%s(): request for already hashed, called from %pF\n",
213 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ret = 0;
215 }
216
Sven Wegeneraea9d712010-06-09 16:10:57 +0200217 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ct_write_unlock(hash);
219
220 return ret;
221}
222
223
224/*
225 * UNhashes ip_vs_conn from ip_vs_conn_tab.
226 * returns bool success.
227 */
228static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
229{
230 unsigned hash;
231 int ret;
232
233 /* unhash it and decrease its reference counter */
Simon Horman85999282010-08-22 21:37:53 +0900234 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200237 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (cp->flags & IP_VS_CONN_F_HASHED) {
240 list_del(&cp->c_list);
241 cp->flags &= ~IP_VS_CONN_F_HASHED;
242 atomic_dec(&cp->refcnt);
243 ret = 1;
244 } else
245 ret = 0;
246
Sven Wegeneraea9d712010-06-09 16:10:57 +0200247 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ct_write_unlock(hash);
249
250 return ret;
251}
252
253
254/*
255 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
256 * Called for pkts coming from OUTside-to-INside.
Simon Hormanf11017e2010-08-22 21:37:52 +0900257 * p->caddr, p->cport: pkt source address (foreign host)
258 * p->vaddr, p->vport: pkt dest address (load balancer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900260static inline struct ip_vs_conn *
261__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 unsigned hash;
264 struct ip_vs_conn *cp;
265
Simon Horman85999282010-08-22 21:37:53 +0900266 hash = ip_vs_conn_hashkey_param(p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 ct_read_lock(hash);
269
270 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900271 if (cp->af == p->af &&
272 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
273 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
274 p->cport == cp->cport && p->vport == cp->vport &&
275 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
276 p->protocol == cp->protocol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* HIT */
278 atomic_inc(&cp->refcnt);
279 ct_read_unlock(hash);
280 return cp;
281 }
282 }
283
284 ct_read_unlock(hash);
285
286 return NULL;
287}
288
Simon Hormanf11017e2010-08-22 21:37:52 +0900289struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct ip_vs_conn *cp;
292
Simon Hormanf11017e2010-08-22 21:37:52 +0900293 cp = __ip_vs_conn_in_get(p);
294 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
295 struct ip_vs_conn_param cport_zero_p = *p;
296 cport_zero_p.cport = 0;
297 cp = __ip_vs_conn_in_get(&cport_zero_p);
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Julius Volz28364a52008-09-02 15:55:43 +0200300 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900301 ip_vs_proto_name(p->protocol),
302 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
303 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200304 cp ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 return cp;
307}
308
Simon Hormanf11017e2010-08-22 21:37:52 +0900309static int
310ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
311 const struct ip_vs_iphdr *iph,
312 unsigned int proto_off, int inverse,
313 struct ip_vs_conn_param *p)
314{
315 __be16 _ports[2], *pptr;
316
317 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
318 if (pptr == NULL)
319 return 1;
320
321 if (likely(!inverse))
322 ip_vs_conn_fill_param(af, iph->protocol, &iph->saddr, pptr[0],
323 &iph->daddr, pptr[1], p);
324 else
325 ip_vs_conn_fill_param(af, iph->protocol, &iph->daddr, pptr[1],
326 &iph->saddr, pptr[0], p);
327 return 0;
328}
329
Simon Horman5c0d2372010-08-02 17:12:44 +0200330struct ip_vs_conn *
331ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
Simon Horman5c0d2372010-08-02 17:12:44 +0200332 const struct ip_vs_iphdr *iph,
333 unsigned int proto_off, int inverse)
334{
Simon Hormanf11017e2010-08-22 21:37:52 +0900335 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200336
Simon Hormanf11017e2010-08-22 21:37:52 +0900337 if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200338 return NULL;
339
Simon Hormanf11017e2010-08-22 21:37:52 +0900340 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200341}
342EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
343
Julian Anastasov87375ab2005-09-14 21:08:51 -0700344/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900345struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700346{
347 unsigned hash;
348 struct ip_vs_conn *cp;
349
Simon Horman85999282010-08-22 21:37:53 +0900350 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700351
352 ct_read_lock(hash);
353
354 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf71499a2010-08-22 21:37:54 +0900355 if (p->pe_data && p->pe->ct_match) {
Simon Hormanea2c73a2010-11-08 20:06:30 +0900356 if (p->pe == cp->pe && p->pe->ct_match(p, cp))
Simon Horman85999282010-08-22 21:37:53 +0900357 goto out;
358 continue;
359 }
360
Simon Hormanf11017e2010-08-22 21:37:52 +0900361 if (cp->af == p->af &&
362 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000363 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900364 * p->vaddr is a fwmark */
365 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
366 p->af, p->vaddr, &cp->vaddr) &&
367 p->cport == cp->cport && p->vport == cp->vport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700368 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Simon Horman85999282010-08-22 21:37:53 +0900369 p->protocol == cp->protocol)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700370 goto out;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700371 }
372 cp = NULL;
373
374 out:
Simon Horman85999282010-08-22 21:37:53 +0900375 if (cp)
376 atomic_inc(&cp->refcnt);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700377 ct_read_unlock(hash);
378
Julius Volz28364a52008-09-02 15:55:43 +0200379 IP_VS_DBG_BUF(9, "template lookup/in %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 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700384
385 return cp;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Simon Hormanf11017e2010-08-22 21:37:52 +0900388/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
389 * Called for pkts coming from inside-to-OUTside.
390 * p->caddr, p->cport: pkt source address (inside host)
391 * p->vaddr, p->vport: pkt dest address (foreign host) */
392struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 unsigned hash;
395 struct ip_vs_conn *cp, *ret=NULL;
396
397 /*
398 * Check for "full" addressed entries
399 */
Simon Horman85999282010-08-22 21:37:53 +0900400 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 ct_read_lock(hash);
403
404 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900405 if (cp->af == p->af &&
406 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
407 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
408 p->vport == cp->cport && p->cport == cp->dport &&
409 p->protocol == cp->protocol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* HIT */
411 atomic_inc(&cp->refcnt);
412 ret = cp;
413 break;
414 }
415 }
416
417 ct_read_unlock(hash);
418
Julius Volz28364a52008-09-02 15:55:43 +0200419 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900420 ip_vs_proto_name(p->protocol),
421 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
422 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200423 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return ret;
426}
427
Simon Horman5c0d2372010-08-02 17:12:44 +0200428struct ip_vs_conn *
429ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
Simon Horman5c0d2372010-08-02 17:12:44 +0200430 const struct ip_vs_iphdr *iph,
431 unsigned int proto_off, int inverse)
432{
Simon Hormanf11017e2010-08-22 21:37:52 +0900433 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200434
Simon Hormanf11017e2010-08-22 21:37:52 +0900435 if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200436 return NULL;
437
Simon Hormanf11017e2010-08-22 21:37:52 +0900438 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200439}
440EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/*
443 * Put back the conn and restart its timer with its timeout
444 */
445void ip_vs_conn_put(struct ip_vs_conn *cp)
446{
Nick Chalk26ec0372010-06-22 08:07:01 +0200447 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
448 0 : cp->timeout;
449 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 __ip_vs_conn_put(cp);
452}
453
454
455/*
456 * Fill a no_client_port connection with a client port number
457 */
Al Viro014d7302006-09-28 14:29:52 -0700458void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 if (ip_vs_conn_unhash(cp)) {
461 spin_lock(&cp->lock);
462 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
463 atomic_dec(&ip_vs_conn_no_cport_cnt);
464 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
465 cp->cport = cport;
466 }
467 spin_unlock(&cp->lock);
468
469 /* hash on new dport */
470 ip_vs_conn_hash(cp);
471 }
472}
473
474
475/*
476 * Bind a connection entry with the corresponding packet_xmit.
477 * Called by ip_vs_conn_new.
478 */
479static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
480{
481 switch (IP_VS_FWD_METHOD(cp)) {
482 case IP_VS_CONN_F_MASQ:
483 cp->packet_xmit = ip_vs_nat_xmit;
484 break;
485
486 case IP_VS_CONN_F_TUNNEL:
487 cp->packet_xmit = ip_vs_tunnel_xmit;
488 break;
489
490 case IP_VS_CONN_F_DROUTE:
491 cp->packet_xmit = ip_vs_dr_xmit;
492 break;
493
494 case IP_VS_CONN_F_LOCALNODE:
495 cp->packet_xmit = ip_vs_null_xmit;
496 break;
497
498 case IP_VS_CONN_F_BYPASS:
499 cp->packet_xmit = ip_vs_bypass_xmit;
500 break;
501 }
502}
503
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200504#ifdef CONFIG_IP_VS_IPV6
505static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
506{
507 switch (IP_VS_FWD_METHOD(cp)) {
508 case IP_VS_CONN_F_MASQ:
509 cp->packet_xmit = ip_vs_nat_xmit_v6;
510 break;
511
512 case IP_VS_CONN_F_TUNNEL:
513 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
514 break;
515
516 case IP_VS_CONN_F_DROUTE:
517 cp->packet_xmit = ip_vs_dr_xmit_v6;
518 break;
519
520 case IP_VS_CONN_F_LOCALNODE:
521 cp->packet_xmit = ip_vs_null_xmit;
522 break;
523
524 case IP_VS_CONN_F_BYPASS:
525 cp->packet_xmit = ip_vs_bypass_xmit_v6;
526 break;
527 }
528}
529#endif
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
533{
534 return atomic_read(&dest->activeconns)
535 + atomic_read(&dest->inactconns);
536}
537
538/*
539 * Bind a connection entry with a virtual service destination
540 * Called just after a new connection entry is created.
541 */
542static inline void
543ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
544{
Julian Anastasov35757922010-09-17 14:18:16 +0200545 unsigned int conn_flags;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* if dest is NULL, then return directly */
548 if (!dest)
549 return;
550
551 /* Increase the refcnt counter of the dest */
552 atomic_inc(&dest->refcnt);
553
Julian Anastasov35757922010-09-17 14:18:16 +0200554 conn_flags = atomic_read(&dest->conn_flags);
555 if (cp->protocol != IPPROTO_UDP)
556 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Bind with the destination and its corresponding transmitter */
Julian Anastasov35757922010-09-17 14:18:16 +0200558 if (cp->flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800559 /* if the connection is not template and is created
560 * by sync, preserve the activity flag.
561 */
Julian Anastasov35757922010-09-17 14:18:16 +0200562 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE))
563 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300564 /* connections inherit forwarding method from dest */
565 cp->flags &= ~IP_VS_CONN_F_FWD_MASK;
Julian Anastasov35757922010-09-17 14:18:16 +0200566 }
567 cp->flags |= conn_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 cp->dest = dest;
569
Julius Volzcfc78c52008-09-02 15:55:53 +0200570 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
571 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
572 "dest->refcnt:%d\n",
573 ip_vs_proto_name(cp->protocol),
574 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
575 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
576 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
577 ip_vs_fwd_tag(cp), cp->state,
578 cp->flags, atomic_read(&cp->refcnt),
579 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700582 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* It is a normal connection, so increase the inactive
584 connection counter because it is in TCP SYNRECV
585 state (inactive) or other protocol inacive state */
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800586 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
587 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
588 atomic_inc(&dest->activeconns);
589 else
590 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 } else {
592 /* It is a persistent connection/template, so increase
593 the peristent connection counter */
594 atomic_inc(&dest->persistconns);
595 }
596
597 if (dest->u_threshold != 0 &&
598 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
599 dest->flags |= IP_VS_DEST_F_OVERLOAD;
600}
601
602
603/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800604 * Check if there is a destination for the connection, if so
605 * bind the connection to the destination.
606 */
607struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
608{
609 struct ip_vs_dest *dest;
610
611 if ((cp) && (!cp->dest)) {
Hans Schillstromfc723252011-01-03 14:44:43 +0100612 dest = ip_vs_find_dest(&init_net, cp->af, &cp->daddr, cp->dport,
Julius Volz7937df12008-09-02 15:55:48 +0200613 &cp->vaddr, cp->vport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100614 cp->protocol, cp->fwmark);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800615 ip_vs_bind_dest(cp, dest);
616 return dest;
617 } else
618 return NULL;
619}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800620
621
622/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * Unbind a connection entry with its VS destination
624 * Called by the ip_vs_conn_expire function.
625 */
626static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
627{
628 struct ip_vs_dest *dest = cp->dest;
629
630 if (!dest)
631 return;
632
Julius Volzcfc78c52008-09-02 15:55:53 +0200633 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
634 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
635 "dest->refcnt:%d\n",
636 ip_vs_proto_name(cp->protocol),
637 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
638 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
639 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
640 ip_vs_fwd_tag(cp), cp->state,
641 cp->flags, atomic_read(&cp->refcnt),
642 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700645 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* It is a normal connection, so decrease the inactconns
647 or activeconns counter */
648 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
649 atomic_dec(&dest->inactconns);
650 } else {
651 atomic_dec(&dest->activeconns);
652 }
653 } else {
654 /* It is a persistent connection/template, so decrease
655 the peristent connection counter */
656 atomic_dec(&dest->persistconns);
657 }
658
659 if (dest->l_threshold != 0) {
660 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
661 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
662 } else if (dest->u_threshold != 0) {
663 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
664 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
665 } else {
666 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
667 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
668 }
669
670 /*
671 * Simply decrease the refcnt of the dest, because the
672 * dest will be either in service's destination list
673 * or in the trash.
674 */
675 atomic_dec(&dest->refcnt);
676}
677
678
679/*
680 * Checking if the destination of a connection template is available.
681 * If available, return 1, otherwise invalidate this connection
682 * template and return 0.
683 */
684int ip_vs_check_template(struct ip_vs_conn *ct)
685{
686 struct ip_vs_dest *dest = ct->dest;
687
688 /*
689 * Checking the dest server status.
690 */
691 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900692 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
693 (sysctl_ip_vs_expire_quiescent_template &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 (atomic_read(&dest->weight) == 0))) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200695 IP_VS_DBG_BUF(9, "check_template: dest not available for "
696 "protocol %s s:%s:%d v:%s:%d "
697 "-> d:%s:%d\n",
698 ip_vs_proto_name(ct->protocol),
699 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
700 ntohs(ct->cport),
701 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
702 ntohs(ct->vport),
703 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
704 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /*
707 * Invalidate the connection template
708 */
Al Viro014d7302006-09-28 14:29:52 -0700709 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700711 ct->dport = htons(0xffff);
712 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ct->cport = 0;
714 ip_vs_conn_hash(ct);
715 }
716 }
717
718 /*
719 * Simply decrease the refcnt of the template,
720 * don't restart its timer.
721 */
722 atomic_dec(&ct->refcnt);
723 return 0;
724 }
725 return 1;
726}
727
728static void ip_vs_conn_expire(unsigned long data)
729{
730 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
731
732 cp->timeout = 60*HZ;
733
734 /*
735 * hey, I'm using it
736 */
737 atomic_inc(&cp->refcnt);
738
739 /*
740 * do I control anybody?
741 */
742 if (atomic_read(&cp->n_control))
743 goto expire_later;
744
745 /*
746 * unhash it if it is hashed in the conn table
747 */
Nick Chalk26ec0372010-06-22 08:07:01 +0200748 if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto expire_later;
750
751 /*
752 * refcnt==1 implies I'm the only one referrer
753 */
754 if (likely(atomic_read(&cp->refcnt) == 1)) {
755 /* delete the timer if it is activated by other users */
756 if (timer_pending(&cp->timer))
757 del_timer(&cp->timer);
758
759 /* does anybody control me? */
760 if (cp->control)
761 ip_vs_control_del(cp);
762
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200763 if (cp->flags & IP_VS_CONN_F_NFCT)
764 ip_vs_conn_drop_conntrack(cp);
765
Simon Hormane9e5eee2010-11-08 20:05:57 +0900766 ip_vs_pe_put(cp->pe);
Simon Horman85999282010-08-22 21:37:53 +0900767 kfree(cp->pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (unlikely(cp->app != NULL))
769 ip_vs_unbind_app(cp);
770 ip_vs_unbind_dest(cp);
771 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
772 atomic_dec(&ip_vs_conn_no_cport_cnt);
773 atomic_dec(&ip_vs_conn_count);
774
775 kmem_cache_free(ip_vs_conn_cachep, cp);
776 return;
777 }
778
779 /* hash it back to the table */
780 ip_vs_conn_hash(cp);
781
782 expire_later:
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800783 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 atomic_read(&cp->refcnt)-1,
785 atomic_read(&cp->n_control));
786
787 ip_vs_conn_put(cp);
788}
789
790
791void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
792{
793 if (del_timer(&cp->timer))
794 mod_timer(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797
798/*
799 * Create a new connection entry and hash it into the ip_vs_conn_tab
800 */
801struct ip_vs_conn *
Simon Hormanf11017e2010-08-22 21:37:52 +0900802ip_vs_conn_new(const struct ip_vs_conn_param *p,
Julius Volz28364a52008-09-02 15:55:43 +0200803 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100804 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct ip_vs_conn *cp;
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100807 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(&init_net, p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800809 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000811 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return NULL;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 INIT_LIST_HEAD(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800816 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Simon Hormanf11017e2010-08-22 21:37:52 +0900817 cp->af = p->af;
818 cp->protocol = p->protocol;
819 ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
820 cp->cport = p->cport;
821 ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
822 cp->vport = p->vport;
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000823 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
Simon Hormanf11017e2010-08-22 21:37:52 +0900824 ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000825 &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 cp->dport = dport;
827 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100828 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900829 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
830 ip_vs_pe_get(p->pe);
831 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900832 cp->pe_data = p->pe_data;
833 cp->pe_data_len = p->pe_data_len;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 spin_lock_init(&cp->lock);
836
837 /*
838 * Set the entry is referenced by the current thread before hashing
839 * it in the table, so that other thread run ip_vs_random_dropentry
840 * but cannot drop this entry.
841 */
842 atomic_set(&cp->refcnt, 1);
843
844 atomic_set(&cp->n_control, 0);
845 atomic_set(&cp->in_pkts, 0);
846
847 atomic_inc(&ip_vs_conn_count);
848 if (flags & IP_VS_CONN_F_NO_CPORT)
849 atomic_inc(&ip_vs_conn_no_cport_cnt);
850
851 /* Bind the connection with a destination server */
852 ip_vs_bind_dest(cp, dest);
853
854 /* Set its state and timeout */
855 cp->state = 0;
856 cp->timeout = 3*HZ;
857
858 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200859#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900860 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200861 ip_vs_bind_xmit_v6(cp);
862 else
863#endif
864 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100866 if (unlikely(pd && atomic_read(&pd->appcnt)))
867 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200869 /*
870 * Allow conntrack to be preserved. By default, conntrack
871 * is created and destroyed for every packet.
872 * Sometimes keeping conntrack can be useful for
873 * IP_VS_CONN_F_ONE_PACKET too.
874 */
875
876 if (ip_vs_conntrack_enabled())
877 cp->flags |= IP_VS_CONN_F_NFCT;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Hash it in the ip_vs_conn_tab finally */
880 ip_vs_conn_hash(cp);
881
882 return cp;
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/*
886 * /proc/net/ip_vs_conn entries
887 */
888#ifdef CONFIG_PROC_FS
889
890static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
891{
892 int idx;
893 struct ip_vs_conn *cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900894
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100895 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 ct_read_lock_bh(idx);
897 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
898 if (pos-- == 0) {
899 seq->private = &ip_vs_conn_tab[idx];
Simon Horman85999282010-08-22 21:37:53 +0900900 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 }
903 ct_read_unlock_bh(idx);
904 }
905
906 return NULL;
907}
908
909static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
910{
911 seq->private = NULL;
912 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
913}
914
915static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
916{
917 struct ip_vs_conn *cp = v;
918 struct list_head *e, *l = seq->private;
919 int idx;
920
921 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900922 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return ip_vs_conn_array(seq, 0);
924
925 /* more on same hash chain? */
926 if ((e = cp->c_list.next) != l)
927 return list_entry(e, struct ip_vs_conn, c_list);
928
929 idx = l - ip_vs_conn_tab;
930 ct_read_unlock_bh(idx);
931
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100932 while (++idx < ip_vs_conn_tab_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ct_read_lock_bh(idx);
934 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
935 seq->private = &ip_vs_conn_tab[idx];
936 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ct_read_unlock_bh(idx);
939 }
940 seq->private = NULL;
941 return NULL;
942}
943
944static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
945{
946 struct list_head *l = seq->private;
947
948 if (l)
949 ct_read_unlock_bh(l - ip_vs_conn_tab);
950}
951
952static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
953{
954
955 if (v == SEQ_START_TOKEN)
956 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +0900957 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 else {
959 const struct ip_vs_conn *cp = v;
Simon Hormana3c918a2010-08-22 21:37:53 +0900960 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
961 size_t len = 0;
962
Simon Hormane9e5eee2010-11-08 20:05:57 +0900963 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +0900964 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +0900965 len = strlen(cp->pe->name);
966 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +0900967 pe_data[len + 1] = ' ';
968 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900969 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +0900970 }
971 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Vince Busam667a5f12008-09-02 15:55:49 +0200973#ifdef CONFIG_IP_VS_IPV6
974 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +0900975 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
976 "%pI6 %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +0200977 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700978 &cp->caddr.in6, ntohs(cp->cport),
979 &cp->vaddr.in6, ntohs(cp->vport),
980 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +0200981 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +0900982 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +0200983 else
984#endif
985 seq_printf(seq,
986 "%-3s %08X %04X %08X %04X"
Simon Hormana3c918a2010-08-22 21:37:53 +0900987 " %08X %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 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),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +0900993 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 return 0;
996}
997
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700998static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .start = ip_vs_conn_seq_start,
1000 .next = ip_vs_conn_seq_next,
1001 .stop = ip_vs_conn_seq_stop,
1002 .show = ip_vs_conn_seq_show,
1003};
1004
1005static int ip_vs_conn_open(struct inode *inode, struct file *file)
1006{
1007 return seq_open(file, &ip_vs_conn_seq_ops);
1008}
1009
Arjan van de Ven9a321442007-02-12 00:55:35 -08001010static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 .owner = THIS_MODULE,
1012 .open = ip_vs_conn_open,
1013 .read = seq_read,
1014 .llseek = seq_lseek,
1015 .release = seq_release,
1016};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001017
1018static const char *ip_vs_origin_name(unsigned flags)
1019{
1020 if (flags & IP_VS_CONN_F_SYNC)
1021 return "SYNC";
1022 else
1023 return "LOCAL";
1024}
1025
1026static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1027{
1028
1029 if (v == SEQ_START_TOKEN)
1030 seq_puts(seq,
1031 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1032 else {
1033 const struct ip_vs_conn *cp = v;
1034
Vince Busam667a5f12008-09-02 15:55:49 +02001035#ifdef CONFIG_IP_VS_IPV6
1036 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001037 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001038 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001039 &cp->caddr.in6, ntohs(cp->cport),
1040 &cp->vaddr.in6, ntohs(cp->vport),
1041 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001042 ip_vs_state_name(cp->protocol, cp->state),
1043 ip_vs_origin_name(cp->flags),
1044 (cp->timer.expires-jiffies)/HZ);
1045 else
1046#endif
1047 seq_printf(seq,
1048 "%-3s %08X %04X %08X %04X "
1049 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001050 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001051 ntohl(cp->caddr.ip), ntohs(cp->cport),
1052 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1053 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001054 ip_vs_state_name(cp->protocol, cp->state),
1055 ip_vs_origin_name(cp->flags),
1056 (cp->timer.expires-jiffies)/HZ);
1057 }
1058 return 0;
1059}
1060
1061static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1062 .start = ip_vs_conn_seq_start,
1063 .next = ip_vs_conn_seq_next,
1064 .stop = ip_vs_conn_seq_stop,
1065 .show = ip_vs_conn_sync_seq_show,
1066};
1067
1068static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1069{
1070 return seq_open(file, &ip_vs_conn_sync_seq_ops);
1071}
1072
1073static const struct file_operations ip_vs_conn_sync_fops = {
1074 .owner = THIS_MODULE,
1075 .open = ip_vs_conn_sync_open,
1076 .read = seq_read,
1077 .llseek = seq_lseek,
1078 .release = seq_release,
1079};
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081#endif
1082
1083
1084/*
1085 * Randomly drop connection entries before running out of memory
1086 */
1087static inline int todrop_entry(struct ip_vs_conn *cp)
1088{
1089 /*
1090 * The drop rate array needs tuning for real environments.
1091 * Called from timer bh only => no locking
1092 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001093 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 static char todrop_counter[9] = {0};
1095 int i;
1096
1097 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1098 This will leave enough time for normal connection to get
1099 through. */
1100 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1101 return 0;
1102
1103 /* Don't drop the entry if its number of incoming packets is not
1104 located in [0, 8] */
1105 i = atomic_read(&cp->in_pkts);
1106 if (i > 8 || i < 0) return 0;
1107
1108 if (!todrop_rate[i]) return 0;
1109 if (--todrop_counter[i] > 0) return 0;
1110
1111 todrop_counter[i] = todrop_rate[i];
1112 return 1;
1113}
1114
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001115/* Called from keventd and must protect itself from softirqs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116void ip_vs_random_dropentry(void)
1117{
1118 int idx;
1119 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /*
1122 * Randomly scan 1/32 of the whole table every second
1123 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001124 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1125 unsigned hash = net_random() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Lock is actually needed in this loop.
1129 */
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001130 ct_write_lock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001133 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* connection template */
1135 continue;
1136
1137 if (cp->protocol == IPPROTO_TCP) {
1138 switch(cp->state) {
1139 case IP_VS_TCP_S_SYN_RECV:
1140 case IP_VS_TCP_S_SYNACK:
1141 break;
1142
1143 case IP_VS_TCP_S_ESTABLISHED:
1144 if (todrop_entry(cp))
1145 break;
1146 continue;
1147
1148 default:
1149 continue;
1150 }
1151 } else {
1152 if (!todrop_entry(cp))
1153 continue;
1154 }
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 IP_VS_DBG(4, "del connection\n");
1157 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001158 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001160 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001163 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165}
1166
1167
1168/*
1169 * Flush all the connection entries in the ip_vs_conn_tab
1170 */
1171static void ip_vs_conn_flush(void)
1172{
1173 int idx;
1174 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 flush_again:
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 /*
1179 * Lock is actually needed in this loop.
1180 */
1181 ct_write_lock_bh(idx);
1182
1183 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 IP_VS_DBG(4, "del connection\n");
1186 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001187 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001189 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192 ct_write_unlock_bh(idx);
1193 }
1194
1195 /* the counter may be not NULL, because maybe some conn entries
1196 are run by slow timer handler or unhashed but still referred */
1197 if (atomic_read(&ip_vs_conn_count) != 0) {
1198 schedule();
1199 goto flush_again;
1200 }
1201}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001202/*
1203 * per netns init and exit
1204 */
1205int __net_init __ip_vs_conn_init(struct net *net)
1206{
1207 if (!net_eq(net, &init_net)) /* netns not enabled yet */
1208 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001210 proc_net_fops_create(net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1211 proc_net_fops_create(net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1212 return 0;
1213}
1214
1215static void __net_exit __ip_vs_conn_cleanup(struct net *net)
1216{
1217 if (!net_eq(net, &init_net)) /* netns not enabled yet */
1218 return;
1219
1220 proc_net_remove(net, "ip_vs_conn");
1221 proc_net_remove(net, "ip_vs_conn_sync");
1222}
1223static struct pernet_operations ipvs_conn_ops = {
1224 .init = __ip_vs_conn_init,
1225 .exit = __ip_vs_conn_cleanup,
1226};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Sven Wegener048cf482008-08-10 18:24:35 +00001228int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 int idx;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001231 int retc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001233 /* Compute size and mask */
1234 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1235 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /*
1238 * Allocate the connection hash table and initialize its list heads
1239 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001240 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size *
1241 sizeof(struct list_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!ip_vs_conn_tab)
1243 return -ENOMEM;
1244
1245 /* Allocate ip_vs_conn slab cache */
1246 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1247 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001248 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (!ip_vs_conn_cachep) {
1250 vfree(ip_vs_conn_tab);
1251 return -ENOMEM;
1252 }
1253
Hannes Eder1e3e2382009-08-02 11:05:41 +00001254 pr_info("Connection hash table configured "
1255 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001256 ip_vs_conn_tab_size,
1257 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1259 sizeof(struct ip_vs_conn));
1260
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001261 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1263 }
1264
1265 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1266 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1267 }
1268
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001269 retc = register_pernet_subsys(&ipvs_conn_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* calculate the random value for connection hash */
1272 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1273
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001274 return retc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277void ip_vs_conn_cleanup(void)
1278{
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001279 unregister_pernet_subsys(&ipvs_conn_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* flush all the connection entries first */
1281 ip_vs_conn_flush();
1282
1283 /* Release the empty cache */
1284 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 vfree(ip_vs_conn_tab);
1286}