blob: 4f3205def28f891a691a6fe6c2dbc2a46a3235ca [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 */
Changli Gao731109e2011-02-19 18:05:08 +080062static struct hlist_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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* counter for no client port connections */
68static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
69
70/* random value for IPVS connection hash */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010071static unsigned int ip_vs_conn_rnd __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Fine locking granularity for big connection hash table
75 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +010076#define CT_LOCKARRAY_BITS 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
78#define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
79
80struct ip_vs_aligned_lock
81{
82 rwlock_t l;
83} __attribute__((__aligned__(SMP_CACHE_BYTES)));
84
85/* lock array for conn table */
86static struct ip_vs_aligned_lock
87__ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
88
Eric Dumazet95c96172012-04-15 05:58:06 +000089static inline void ct_read_lock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
92}
93
Eric Dumazet95c96172012-04-15 05:58:06 +000094static inline void ct_read_unlock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
97}
98
Eric Dumazet95c96172012-04-15 05:58:06 +000099static inline void ct_write_lock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
102}
103
Eric Dumazet95c96172012-04-15 05:58:06 +0000104static inline void ct_write_unlock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
107}
108
Eric Dumazet95c96172012-04-15 05:58:06 +0000109static inline void ct_read_lock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
112}
113
Eric Dumazet95c96172012-04-15 05:58:06 +0000114static inline void ct_read_unlock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
117}
118
Eric Dumazet95c96172012-04-15 05:58:06 +0000119static inline void ct_write_lock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
122}
123
Eric Dumazet95c96172012-04-15 05:58:06 +0000124static inline void ct_write_unlock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
127}
128
129
130/*
131 * Returns hash value for IPVS connection entry
132 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000133static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto,
Julius Volz28364a52008-09-02 15:55:43 +0200134 const union nf_inet_addr *addr,
135 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Julius Volz28364a52008-09-02 15:55:43 +0200137#ifdef CONFIG_IP_VS_IPV6
138 if (af == AF_INET6)
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100139 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
140 (__force u32)port, proto, ip_vs_conn_rnd) ^
141 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Julius Volz28364a52008-09-02 15:55:43 +0200142#endif
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100143 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
144 ip_vs_conn_rnd) ^
145 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Simon Horman85999282010-08-22 21:37:53 +0900148static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
149 bool inverse)
150{
151 const union nf_inet_addr *addr;
152 __be16 port;
153
Simon Hormanf71499a2010-08-22 21:37:54 +0900154 if (p->pe_data && p->pe->hashkey_raw)
Simon Horman85999282010-08-22 21:37:53 +0900155 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
156 ip_vs_conn_tab_mask;
157
158 if (likely(!inverse)) {
159 addr = p->caddr;
160 port = p->cport;
161 } else {
162 addr = p->vaddr;
163 port = p->vport;
164 }
165
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100166 return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port);
Simon Horman85999282010-08-22 21:37:53 +0900167}
168
169static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
170{
171 struct ip_vs_conn_param p;
172
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100173 ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol,
174 &cp->caddr, cp->cport, NULL, 0, &p);
Simon Horman85999282010-08-22 21:37:53 +0900175
Simon Hormane9e5eee2010-11-08 20:05:57 +0900176 if (cp->pe) {
177 p.pe = cp->pe;
Simon Horman85999282010-08-22 21:37:53 +0900178 p.pe_data = cp->pe_data;
179 p.pe_data_len = cp->pe_data_len;
180 }
181
182 return ip_vs_conn_hashkey_param(&p, false);
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/*
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100186 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * returns bool success.
188 */
189static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
190{
Eric Dumazet95c96172012-04-15 05:58:06 +0000191 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int ret;
193
Nick Chalk26ec0372010-06-22 08:07:01 +0200194 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
195 return 0;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Hash by protocol, client address and port */
Simon Horman85999282010-08-22 21:37:53 +0900198 hash = ip_vs_conn_hashkey_conn(cp);
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)) {
Changli Gao731109e2011-02-19 18:05:08 +0800204 hlist_add_head(&cp->c_list, &ip_vs_conn_tab[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 cp->flags |= IP_VS_CONN_F_HASHED;
206 atomic_inc(&cp->refcnt);
207 ret = 1;
208 } else {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000209 pr_err("%s(): request for already hashed, called from %pF\n",
210 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret = 0;
212 }
213
Sven Wegeneraea9d712010-06-09 16:10:57 +0200214 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ct_write_unlock(hash);
216
217 return ret;
218}
219
220
221/*
222 * UNhashes ip_vs_conn from ip_vs_conn_tab.
223 * returns bool success.
224 */
225static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
226{
Eric Dumazet95c96172012-04-15 05:58:06 +0000227 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int ret;
229
230 /* unhash it and decrease its reference counter */
Simon Horman85999282010-08-22 21:37:53 +0900231 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200234 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (cp->flags & IP_VS_CONN_F_HASHED) {
Changli Gao731109e2011-02-19 18:05:08 +0800237 hlist_del(&cp->c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 cp->flags &= ~IP_VS_CONN_F_HASHED;
239 atomic_dec(&cp->refcnt);
240 ret = 1;
241 } else
242 ret = 0;
243
Sven Wegeneraea9d712010-06-09 16:10:57 +0200244 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ct_write_unlock(hash);
246
247 return ret;
248}
249
250
251/*
252 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
253 * Called for pkts coming from OUTside-to-INside.
Simon Hormanf11017e2010-08-22 21:37:52 +0900254 * p->caddr, p->cport: pkt source address (foreign host)
255 * p->vaddr, p->vport: pkt dest address (load balancer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900257static inline struct ip_vs_conn *
258__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Eric Dumazet95c96172012-04-15 05:58:06 +0000260 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct ip_vs_conn *cp;
Changli Gao731109e2011-02-19 18:05:08 +0800262 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Simon Horman85999282010-08-22 21:37:53 +0900264 hash = ip_vs_conn_hashkey_param(p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 ct_read_lock(hash);
267
Changli Gao731109e2011-02-19 18:05:08 +0800268 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900269 if (cp->af == p->af &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100270 p->cport == cp->cport && p->vport == cp->vport &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900271 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
272 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900273 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100274 p->protocol == cp->protocol &&
275 ip_vs_conn_net_eq(cp, p->net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* HIT */
277 atomic_inc(&cp->refcnt);
278 ct_read_unlock(hash);
279 return cp;
280 }
281 }
282
283 ct_read_unlock(hash);
284
285 return NULL;
286}
287
Simon Hormanf11017e2010-08-22 21:37:52 +0900288struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct ip_vs_conn *cp;
291
Simon Hormanf11017e2010-08-22 21:37:52 +0900292 cp = __ip_vs_conn_in_get(p);
293 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
294 struct ip_vs_conn_param cport_zero_p = *p;
295 cport_zero_p.cport = 0;
296 cp = __ip_vs_conn_in_get(&cport_zero_p);
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Julius Volz28364a52008-09-02 15:55:43 +0200299 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900300 ip_vs_proto_name(p->protocol),
301 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
302 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200303 cp ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 return cp;
306}
307
Simon Hormanf11017e2010-08-22 21:37:52 +0900308static int
309ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
310 const struct ip_vs_iphdr *iph,
311 unsigned int proto_off, int inverse,
312 struct ip_vs_conn_param *p)
313{
314 __be16 _ports[2], *pptr;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100315 struct net *net = skb_net(skb);
Simon Hormanf11017e2010-08-22 21:37:52 +0900316
317 pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports);
318 if (pptr == NULL)
319 return 1;
320
321 if (likely(!inverse))
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100322 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr,
323 pptr[0], &iph->daddr, pptr[1], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900324 else
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100325 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr,
326 pptr[1], &iph->saddr, pptr[0], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900327 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{
Eric Dumazet95c96172012-04-15 05:58:06 +0000347 unsigned int hash;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700348 struct ip_vs_conn *cp;
Changli Gao731109e2011-02-19 18:05:08 +0800349 struct hlist_node *n;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700350
Simon Horman85999282010-08-22 21:37:53 +0900351 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700352
353 ct_read_lock(hash);
354
Changli Gao731109e2011-02-19 18:05:08 +0800355 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100356 if (!ip_vs_conn_net_eq(cp, p->net))
357 continue;
Simon Hormanf71499a2010-08-22 21:37:54 +0900358 if (p->pe_data && p->pe->ct_match) {
Simon Hormanea2c73a2010-11-08 20:06:30 +0900359 if (p->pe == cp->pe && p->pe->ct_match(p, cp))
Simon Horman85999282010-08-22 21:37:53 +0900360 goto out;
361 continue;
362 }
363
Simon Hormanf11017e2010-08-22 21:37:52 +0900364 if (cp->af == p->af &&
365 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000366 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900367 * p->vaddr is a fwmark */
368 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
369 p->af, p->vaddr, &cp->vaddr) &&
370 p->cport == cp->cport && p->vport == cp->vport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700371 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Simon Horman85999282010-08-22 21:37:53 +0900372 p->protocol == cp->protocol)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700373 goto out;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700374 }
375 cp = NULL;
376
377 out:
Simon Horman85999282010-08-22 21:37:53 +0900378 if (cp)
379 atomic_inc(&cp->refcnt);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700380 ct_read_unlock(hash);
381
Julius Volz28364a52008-09-02 15:55:43 +0200382 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900383 ip_vs_proto_name(p->protocol),
384 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
385 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200386 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700387
388 return cp;
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Simon Hormanf11017e2010-08-22 21:37:52 +0900391/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
392 * Called for pkts coming from inside-to-OUTside.
393 * p->caddr, p->cport: pkt source address (inside host)
394 * p->vaddr, p->vport: pkt dest address (foreign host) */
395struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Eric Dumazet95c96172012-04-15 05:58:06 +0000397 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct ip_vs_conn *cp, *ret=NULL;
Changli Gao731109e2011-02-19 18:05:08 +0800399 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * Check for "full" addressed entries
403 */
Simon Horman85999282010-08-22 21:37:53 +0900404 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 ct_read_lock(hash);
407
Changli Gao731109e2011-02-19 18:05:08 +0800408 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900409 if (cp->af == p->af &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100410 p->vport == cp->cport && p->cport == cp->dport &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900411 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
412 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100413 p->protocol == cp->protocol &&
414 ip_vs_conn_net_eq(cp, p->net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* HIT */
416 atomic_inc(&cp->refcnt);
417 ret = cp;
418 break;
419 }
420 }
421
422 ct_read_unlock(hash);
423
Julius Volz28364a52008-09-02 15:55:43 +0200424 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900425 ip_vs_proto_name(p->protocol),
426 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
427 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200428 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return ret;
431}
432
Simon Horman5c0d2372010-08-02 17:12:44 +0200433struct ip_vs_conn *
434ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
Simon Horman5c0d2372010-08-02 17:12:44 +0200435 const struct ip_vs_iphdr *iph,
436 unsigned int proto_off, int inverse)
437{
Simon Hormanf11017e2010-08-22 21:37:52 +0900438 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200439
Simon Hormanf11017e2010-08-22 21:37:52 +0900440 if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200441 return NULL;
442
Simon Hormanf11017e2010-08-22 21:37:52 +0900443 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200444}
445EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/*
448 * Put back the conn and restart its timer with its timeout
449 */
450void ip_vs_conn_put(struct ip_vs_conn *cp)
451{
Nick Chalk26ec0372010-06-22 08:07:01 +0200452 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
453 0 : cp->timeout;
454 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 __ip_vs_conn_put(cp);
457}
458
459
460/*
461 * Fill a no_client_port connection with a client port number
462 */
Al Viro014d7302006-09-28 14:29:52 -0700463void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 if (ip_vs_conn_unhash(cp)) {
466 spin_lock(&cp->lock);
467 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
468 atomic_dec(&ip_vs_conn_no_cport_cnt);
469 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
470 cp->cport = cport;
471 }
472 spin_unlock(&cp->lock);
473
474 /* hash on new dport */
475 ip_vs_conn_hash(cp);
476 }
477}
478
479
480/*
481 * Bind a connection entry with the corresponding packet_xmit.
482 * Called by ip_vs_conn_new.
483 */
484static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
485{
486 switch (IP_VS_FWD_METHOD(cp)) {
487 case IP_VS_CONN_F_MASQ:
488 cp->packet_xmit = ip_vs_nat_xmit;
489 break;
490
491 case IP_VS_CONN_F_TUNNEL:
492 cp->packet_xmit = ip_vs_tunnel_xmit;
493 break;
494
495 case IP_VS_CONN_F_DROUTE:
496 cp->packet_xmit = ip_vs_dr_xmit;
497 break;
498
499 case IP_VS_CONN_F_LOCALNODE:
500 cp->packet_xmit = ip_vs_null_xmit;
501 break;
502
503 case IP_VS_CONN_F_BYPASS:
504 cp->packet_xmit = ip_vs_bypass_xmit;
505 break;
506 }
507}
508
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200509#ifdef CONFIG_IP_VS_IPV6
510static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
511{
512 switch (IP_VS_FWD_METHOD(cp)) {
513 case IP_VS_CONN_F_MASQ:
514 cp->packet_xmit = ip_vs_nat_xmit_v6;
515 break;
516
517 case IP_VS_CONN_F_TUNNEL:
518 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
519 break;
520
521 case IP_VS_CONN_F_DROUTE:
522 cp->packet_xmit = ip_vs_dr_xmit_v6;
523 break;
524
525 case IP_VS_CONN_F_LOCALNODE:
526 cp->packet_xmit = ip_vs_null_xmit;
527 break;
528
529 case IP_VS_CONN_F_BYPASS:
530 cp->packet_xmit = ip_vs_bypass_xmit_v6;
531 break;
532 }
533}
534#endif
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
538{
539 return atomic_read(&dest->activeconns)
540 + atomic_read(&dest->inactconns);
541}
542
543/*
544 * Bind a connection entry with a virtual service destination
545 * Called just after a new connection entry is created.
546 */
547static inline void
548ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
549{
Julian Anastasov35757922010-09-17 14:18:16 +0200550 unsigned int conn_flags;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* if dest is NULL, then return directly */
553 if (!dest)
554 return;
555
556 /* Increase the refcnt counter of the dest */
557 atomic_inc(&dest->refcnt);
558
Julian Anastasov35757922010-09-17 14:18:16 +0200559 conn_flags = atomic_read(&dest->conn_flags);
560 if (cp->protocol != IPPROTO_UDP)
561 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Bind with the destination and its corresponding transmitter */
Julian Anastasov35757922010-09-17 14:18:16 +0200563 if (cp->flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800564 /* if the connection is not template and is created
565 * by sync, preserve the activity flag.
566 */
Julian Anastasov35757922010-09-17 14:18:16 +0200567 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE))
568 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300569 /* connections inherit forwarding method from dest */
Julian Anastasov82cfc062012-04-24 23:46:35 +0300570 cp->flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200571 }
572 cp->flags |= conn_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 cp->dest = dest;
574
Julius Volzcfc78c52008-09-02 15:55:53 +0200575 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
576 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
577 "dest->refcnt:%d\n",
578 ip_vs_proto_name(cp->protocol),
579 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
580 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
581 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
582 ip_vs_fwd_tag(cp), cp->state,
583 cp->flags, atomic_read(&cp->refcnt),
584 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700587 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300588 /* It is a normal connection, so modify the counters
589 * according to the flags, later the protocol can
590 * update them on state change
591 */
592 if (!(cp->flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800593 atomic_inc(&dest->activeconns);
594 else
595 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
597 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300598 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 atomic_inc(&dest->persistconns);
600 }
601
602 if (dest->u_threshold != 0 &&
603 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
604 dest->flags |= IP_VS_DEST_F_OVERLOAD;
605}
606
607
608/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800609 * Check if there is a destination for the connection, if so
610 * bind the connection to the destination.
611 */
612struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
613{
614 struct ip_vs_dest *dest;
615
Julian Anastasov882a8442012-04-24 23:46:37 +0300616 dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr,
617 cp->dport, &cp->vaddr, cp->vport,
618 cp->protocol, cp->fwmark, cp->flags);
619 if (dest) {
620 struct ip_vs_proto_data *pd;
621
622 /* Applications work depending on the forwarding method
623 * but better to reassign them always when binding dest */
624 if (cp->app)
625 ip_vs_unbind_app(cp);
626
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800627 ip_vs_bind_dest(cp, dest);
Julian Anastasov882a8442012-04-24 23:46:37 +0300628
629 /* Update its packet transmitter */
630 cp->packet_xmit = NULL;
631#ifdef CONFIG_IP_VS_IPV6
632 if (cp->af == AF_INET6)
633 ip_vs_bind_xmit_v6(cp);
634 else
635#endif
636 ip_vs_bind_xmit(cp);
637
638 pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol);
639 if (pd && atomic_read(&pd->appcnt))
640 ip_vs_bind_app(cp, pd->pp);
641 }
642 return dest;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800643}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800644
645
646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * Unbind a connection entry with its VS destination
648 * Called by the ip_vs_conn_expire function.
649 */
650static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
651{
652 struct ip_vs_dest *dest = cp->dest;
653
654 if (!dest)
655 return;
656
Julius Volzcfc78c52008-09-02 15:55:53 +0200657 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
658 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
659 "dest->refcnt:%d\n",
660 ip_vs_proto_name(cp->protocol),
661 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
662 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
663 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
664 ip_vs_fwd_tag(cp), cp->state,
665 cp->flags, atomic_read(&cp->refcnt),
666 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700669 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* It is a normal connection, so decrease the inactconns
671 or activeconns counter */
672 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
673 atomic_dec(&dest->inactconns);
674 } else {
675 atomic_dec(&dest->activeconns);
676 }
677 } else {
678 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300679 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 atomic_dec(&dest->persistconns);
681 }
682
683 if (dest->l_threshold != 0) {
684 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
685 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
686 } else if (dest->u_threshold != 0) {
687 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
688 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
689 } else {
690 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
691 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
692 }
693
694 /*
695 * Simply decrease the refcnt of the dest, because the
696 * dest will be either in service's destination list
697 * or in the trash.
698 */
699 atomic_dec(&dest->refcnt);
700}
701
Simon Horman8e1b0b12011-02-04 18:33:01 +0900702static int expire_quiescent_template(struct netns_ipvs *ipvs,
703 struct ip_vs_dest *dest)
704{
705#ifdef CONFIG_SYSCTL
706 return ipvs->sysctl_expire_quiescent_template &&
707 (atomic_read(&dest->weight) == 0);
708#else
709 return 0;
710#endif
711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713/*
714 * Checking if the destination of a connection template is available.
715 * If available, return 1, otherwise invalidate this connection
716 * template and return 0.
717 */
718int ip_vs_check_template(struct ip_vs_conn *ct)
719{
720 struct ip_vs_dest *dest = ct->dest;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100721 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * Checking the dest server status.
725 */
726 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900727 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900728 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200729 IP_VS_DBG_BUF(9, "check_template: dest not available for "
730 "protocol %s s:%s:%d v:%s:%d "
731 "-> d:%s:%d\n",
732 ip_vs_proto_name(ct->protocol),
733 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
734 ntohs(ct->cport),
735 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
736 ntohs(ct->vport),
737 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
738 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /*
741 * Invalidate the connection template
742 */
Al Viro014d7302006-09-28 14:29:52 -0700743 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700745 ct->dport = htons(0xffff);
746 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ct->cport = 0;
748 ip_vs_conn_hash(ct);
749 }
750 }
751
752 /*
753 * Simply decrease the refcnt of the template,
754 * don't restart its timer.
755 */
756 atomic_dec(&ct->refcnt);
757 return 0;
758 }
759 return 1;
760}
761
762static void ip_vs_conn_expire(unsigned long data)
763{
764 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300765 struct net *net = ip_vs_conn_net(cp);
766 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 cp->timeout = 60*HZ;
769
770 /*
771 * hey, I'm using it
772 */
773 atomic_inc(&cp->refcnt);
774
775 /*
776 * do I control anybody?
777 */
778 if (atomic_read(&cp->n_control))
779 goto expire_later;
780
781 /*
782 * unhash it if it is hashed in the conn table
783 */
Nick Chalk26ec0372010-06-22 08:07:01 +0200784 if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto expire_later;
786
787 /*
788 * refcnt==1 implies I'm the only one referrer
789 */
790 if (likely(atomic_read(&cp->refcnt) == 1)) {
791 /* delete the timer if it is activated by other users */
792 if (timer_pending(&cp->timer))
793 del_timer(&cp->timer);
794
795 /* does anybody control me? */
796 if (cp->control)
797 ip_vs_control_del(cp);
798
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200799 if (cp->flags & IP_VS_CONN_F_NFCT) {
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200800 ip_vs_conn_drop_conntrack(cp);
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200801 /* Do not access conntracks during subsys cleanup
802 * because nf_conntrack_find_get can not be used after
803 * conntrack cleanup for the net.
804 */
805 smp_rmb();
806 if (ipvs->enable)
807 ip_vs_conn_drop_conntrack(cp);
808 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200809
Simon Hormane9e5eee2010-11-08 20:05:57 +0900810 ip_vs_pe_put(cp->pe);
Simon Horman85999282010-08-22 21:37:53 +0900811 kfree(cp->pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (unlikely(cp->app != NULL))
813 ip_vs_unbind_app(cp);
814 ip_vs_unbind_dest(cp);
815 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
816 atomic_dec(&ip_vs_conn_no_cport_cnt);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100817 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 kmem_cache_free(ip_vs_conn_cachep, cp);
820 return;
821 }
822
823 /* hash it back to the table */
824 ip_vs_conn_hash(cp);
825
826 expire_later:
Roberto Nibali4b5bdf52006-01-03 14:22:59 -0800827 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 atomic_read(&cp->refcnt)-1,
829 atomic_read(&cp->n_control));
830
Julian Anastasov749c42b2012-04-24 23:46:40 +0300831 if (ipvs->sync_state & IP_VS_STATE_MASTER)
832 ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs));
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ip_vs_conn_put(cp);
835}
836
837
838void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
839{
840 if (del_timer(&cp->timer))
841 mod_timer(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844
845/*
846 * Create a new connection entry and hash it into the ip_vs_conn_tab
847 */
848struct ip_vs_conn *
Simon Hormanf11017e2010-08-22 21:37:52 +0900849ip_vs_conn_new(const struct ip_vs_conn_param *p,
Eric Dumazet95c96172012-04-15 05:58:06 +0000850 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100851 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100854 struct netns_ipvs *ipvs = net_ipvs(p->net);
855 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net,
856 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800858 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000860 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return NULL;
862 }
863
Changli Gao731109e2011-02-19 18:05:08 +0800864 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800865 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100866 ip_vs_conn_net_set(cp, p->net);
Simon Hormanf11017e2010-08-22 21:37:52 +0900867 cp->af = p->af;
868 cp->protocol = p->protocol;
869 ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
870 cp->cport = p->cport;
871 ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
872 cp->vport = p->vport;
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000873 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
Simon Hormanf11017e2010-08-22 21:37:52 +0900874 ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000875 &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 cp->dport = dport;
877 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100878 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900879 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
880 ip_vs_pe_get(p->pe);
881 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900882 cp->pe_data = p->pe_data;
883 cp->pe_data_len = p->pe_data_len;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_lock_init(&cp->lock);
886
887 /*
888 * Set the entry is referenced by the current thread before hashing
889 * it in the table, so that other thread run ip_vs_random_dropentry
890 * but cannot drop this entry.
891 */
892 atomic_set(&cp->refcnt, 1);
893
894 atomic_set(&cp->n_control, 0);
895 atomic_set(&cp->in_pkts, 0);
896
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100897 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (flags & IP_VS_CONN_F_NO_CPORT)
899 atomic_inc(&ip_vs_conn_no_cport_cnt);
900
901 /* Bind the connection with a destination server */
902 ip_vs_bind_dest(cp, dest);
903
904 /* Set its state and timeout */
905 cp->state = 0;
906 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300907 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200910#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900911 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200912 ip_vs_bind_xmit_v6(cp);
913 else
914#endif
915 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100917 if (unlikely(pd && atomic_read(&pd->appcnt)))
918 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200920 /*
921 * Allow conntrack to be preserved. By default, conntrack
922 * is created and destroyed for every packet.
923 * Sometimes keeping conntrack can be useful for
924 * IP_VS_CONN_F_ONE_PACKET too.
925 */
926
Hans Schillstroma0840e22011-01-03 14:44:58 +0100927 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200928 cp->flags |= IP_VS_CONN_F_NFCT;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* Hash it in the ip_vs_conn_tab finally */
931 ip_vs_conn_hash(cp);
932
933 return cp;
934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * /proc/net/ip_vs_conn entries
938 */
939#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100940struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +0800941 struct seq_net_private p;
942 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100943};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
946{
947 int idx;
948 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100949 struct ip_vs_iter_state *iter = seq->private;
Changli Gao731109e2011-02-19 18:05:08 +0800950 struct hlist_node *n;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900951
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100952 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ct_read_lock_bh(idx);
Changli Gao731109e2011-02-19 18:05:08 +0800954 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100956 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +0800957 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 }
960 ct_read_unlock_bh(idx);
961 }
962
963 return NULL;
964}
965
966static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
967{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100968 struct ip_vs_iter_state *iter = seq->private;
969
970 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
972}
973
974static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
975{
976 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100977 struct ip_vs_iter_state *iter = seq->private;
Changli Gao731109e2011-02-19 18:05:08 +0800978 struct hlist_node *e;
979 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int idx;
981
982 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900983 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return ip_vs_conn_array(seq, 0);
985
986 /* more on same hash chain? */
Changli Gao731109e2011-02-19 18:05:08 +0800987 if ((e = cp->c_list.next))
988 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 idx = l - ip_vs_conn_tab;
991 ct_read_unlock_bh(idx);
992
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100993 while (++idx < ip_vs_conn_tab_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ct_read_lock_bh(idx);
Changli Gao731109e2011-02-19 18:05:08 +0800995 hlist_for_each_entry(cp, e, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100996 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 ct_read_unlock_bh(idx);
1000 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001001 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return NULL;
1003}
1004
1005static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1006{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001007 struct ip_vs_iter_state *iter = seq->private;
Changli Gao731109e2011-02-19 18:05:08 +08001008 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (l)
1011 ct_read_unlock_bh(l - ip_vs_conn_tab);
1012}
1013
1014static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1015{
1016
1017 if (v == SEQ_START_TOKEN)
1018 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001019 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 else {
1021 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001022 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001023 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1024 size_t len = 0;
1025
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001026 if (!ip_vs_conn_net_eq(cp, net))
1027 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001028 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001029 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001030 len = strlen(cp->pe->name);
1031 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001032 pe_data[len + 1] = ' ';
1033 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001034 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001035 }
1036 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Vince Busam667a5f12008-09-02 15:55:49 +02001038#ifdef CONFIG_IP_VS_IPV6
1039 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001040 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1041 "%pI6 %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001042 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001043 &cp->caddr.in6, ntohs(cp->cport),
1044 &cp->vaddr.in6, ntohs(cp->vport),
1045 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001046 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001047 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001048 else
1049#endif
1050 seq_printf(seq,
1051 "%-3s %08X %04X %08X %04X"
Simon Hormana3c918a2010-08-22 21:37:53 +09001052 " %08X %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001054 ntohl(cp->caddr.ip), ntohs(cp->cport),
1055 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1056 ntohl(cp->daddr.ip), ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001058 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 return 0;
1061}
1062
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001063static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 .start = ip_vs_conn_seq_start,
1065 .next = ip_vs_conn_seq_next,
1066 .stop = ip_vs_conn_seq_stop,
1067 .show = ip_vs_conn_seq_show,
1068};
1069
1070static int ip_vs_conn_open(struct inode *inode, struct file *file)
1071{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001072 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1073 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Arjan van de Ven9a321442007-02-12 00:55:35 -08001076static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 .owner = THIS_MODULE,
1078 .open = ip_vs_conn_open,
1079 .read = seq_read,
1080 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001081 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001083
Eric Dumazet95c96172012-04-15 05:58:06 +00001084static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001085{
1086 if (flags & IP_VS_CONN_F_SYNC)
1087 return "SYNC";
1088 else
1089 return "LOCAL";
1090}
1091
1092static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1093{
1094
1095 if (v == SEQ_START_TOKEN)
1096 seq_puts(seq,
1097 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1098 else {
1099 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001100 struct net *net = seq_file_net(seq);
1101
1102 if (!ip_vs_conn_net_eq(cp, net))
1103 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001104
Vince Busam667a5f12008-09-02 15:55:49 +02001105#ifdef CONFIG_IP_VS_IPV6
1106 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001107 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001108 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001109 &cp->caddr.in6, ntohs(cp->cport),
1110 &cp->vaddr.in6, ntohs(cp->vport),
1111 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001112 ip_vs_state_name(cp->protocol, cp->state),
1113 ip_vs_origin_name(cp->flags),
1114 (cp->timer.expires-jiffies)/HZ);
1115 else
1116#endif
1117 seq_printf(seq,
1118 "%-3s %08X %04X %08X %04X "
1119 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001120 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001121 ntohl(cp->caddr.ip), ntohs(cp->cport),
1122 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1123 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001124 ip_vs_state_name(cp->protocol, cp->state),
1125 ip_vs_origin_name(cp->flags),
1126 (cp->timer.expires-jiffies)/HZ);
1127 }
1128 return 0;
1129}
1130
1131static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1132 .start = ip_vs_conn_seq_start,
1133 .next = ip_vs_conn_seq_next,
1134 .stop = ip_vs_conn_seq_stop,
1135 .show = ip_vs_conn_sync_seq_show,
1136};
1137
1138static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1139{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001140 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1141 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001142}
1143
1144static const struct file_operations ip_vs_conn_sync_fops = {
1145 .owner = THIS_MODULE,
1146 .open = ip_vs_conn_sync_open,
1147 .read = seq_read,
1148 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001149 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001150};
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152#endif
1153
1154
1155/*
1156 * Randomly drop connection entries before running out of memory
1157 */
1158static inline int todrop_entry(struct ip_vs_conn *cp)
1159{
1160 /*
1161 * The drop rate array needs tuning for real environments.
1162 * Called from timer bh only => no locking
1163 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001164 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 static char todrop_counter[9] = {0};
1166 int i;
1167
1168 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1169 This will leave enough time for normal connection to get
1170 through. */
1171 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1172 return 0;
1173
1174 /* Don't drop the entry if its number of incoming packets is not
1175 located in [0, 8] */
1176 i = atomic_read(&cp->in_pkts);
1177 if (i > 8 || i < 0) return 0;
1178
1179 if (!todrop_rate[i]) return 0;
1180 if (--todrop_counter[i] > 0) return 0;
1181
1182 todrop_counter[i] = todrop_rate[i];
1183 return 1;
1184}
1185
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001186/* Called from keventd and must protect itself from softirqs */
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001187void ip_vs_random_dropentry(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 int idx;
1190 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * Randomly scan 1/32 of the whole table every second
1194 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001195 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001196 unsigned int hash = net_random() & ip_vs_conn_tab_mask;
Changli Gao731109e2011-02-19 18:05:08 +08001197 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Lock is actually needed in this loop.
1201 */
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001202 ct_write_lock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Changli Gao731109e2011-02-19 18:05:08 +08001204 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001205 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* connection template */
1207 continue;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001208 if (!ip_vs_conn_net_eq(cp, net))
1209 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (cp->protocol == IPPROTO_TCP) {
1211 switch(cp->state) {
1212 case IP_VS_TCP_S_SYN_RECV:
1213 case IP_VS_TCP_S_SYNACK:
1214 break;
1215
1216 case IP_VS_TCP_S_ESTABLISHED:
1217 if (todrop_entry(cp))
1218 break;
1219 continue;
1220
1221 default:
1222 continue;
1223 }
1224 } else {
1225 if (!todrop_entry(cp))
1226 continue;
1227 }
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 IP_VS_DBG(4, "del connection\n");
1230 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001231 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001233 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001236 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
1240
1241/*
1242 * Flush all the connection entries in the ip_vs_conn_tab
1243 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001244static void ip_vs_conn_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 int idx;
1247 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001248 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Hans Schillstroma0840e22011-01-03 14:44:58 +01001250flush_again:
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001251 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Changli Gao731109e2011-02-19 18:05:08 +08001252 struct hlist_node *n;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Lock is actually needed in this loop.
1256 */
1257 ct_write_lock_bh(idx);
1258
Changli Gao731109e2011-02-19 18:05:08 +08001259 hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001260 if (!ip_vs_conn_net_eq(cp, net))
1261 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 IP_VS_DBG(4, "del connection\n");
1263 ip_vs_conn_expire_now(cp);
Neil Hormanfb3d8942005-06-28 15:40:02 -07001264 if (cp->control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 IP_VS_DBG(4, "del conn template\n");
Neil Hormanfb3d8942005-06-28 15:40:02 -07001266 ip_vs_conn_expire_now(cp->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 ct_write_unlock_bh(idx);
1270 }
1271
1272 /* the counter may be not NULL, because maybe some conn entries
1273 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001274 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 schedule();
1276 goto flush_again;
1277 }
1278}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001279/*
1280 * per netns init and exit
1281 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001282int __net_init ip_vs_conn_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001283{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001284 struct netns_ipvs *ipvs = net_ipvs(net);
1285
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001286 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001288 proc_net_fops_create(net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1289 proc_net_fops_create(net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1290 return 0;
1291}
1292
Hans Schillstrom503cf152011-05-01 18:50:16 +02001293void __net_exit ip_vs_conn_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001294{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001295 /* flush all the connection entries first */
1296 ip_vs_conn_flush(net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001297 proc_net_remove(net, "ip_vs_conn");
1298 proc_net_remove(net, "ip_vs_conn_sync");
1299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Sven Wegener048cf482008-08-10 18:24:35 +00001301int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 int idx;
1304
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001305 /* Compute size and mask */
1306 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1307 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * Allocate the connection hash table and initialize its list heads
1311 */
Changli Gao731109e2011-02-19 18:05:08 +08001312 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!ip_vs_conn_tab)
1314 return -ENOMEM;
1315
1316 /* Allocate ip_vs_conn slab cache */
1317 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1318 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001319 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!ip_vs_conn_cachep) {
1321 vfree(ip_vs_conn_tab);
1322 return -ENOMEM;
1323 }
1324
Hannes Eder1e3e2382009-08-02 11:05:41 +00001325 pr_info("Connection hash table configured "
1326 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001327 ip_vs_conn_tab_size,
1328 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1330 sizeof(struct ip_vs_conn));
1331
Changli Gao731109e2011-02-19 18:05:08 +08001332 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1333 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1336 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* calculate the random value for connection hash */
1340 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1341
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345void ip_vs_conn_cleanup(void)
1346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* Release the empty cache */
1348 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 vfree(ip_vs_conn_tab);
1350}