blob: b0cd2be01d753f5ff594d4695ce00f4da71612cf [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{
Julian Anastasov088339a2013-03-21 11:58:10 +020082 spinlock_t l;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083} __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_write_lock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Julian Anastasov088339a2013-03-21 11:58:10 +020091 spin_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Eric Dumazet95c96172012-04-15 05:58:06 +000094static inline void ct_write_unlock(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Julian Anastasov088339a2013-03-21 11:58:10 +020096 spin_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99
100/*
101 * Returns hash value for IPVS connection entry
102 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000103static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto,
Julius Volz28364a52008-09-02 15:55:43 +0200104 const union nf_inet_addr *addr,
105 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Julius Volz28364a52008-09-02 15:55:43 +0200107#ifdef CONFIG_IP_VS_IPV6
108 if (af == AF_INET6)
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100109 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
110 (__force u32)port, proto, ip_vs_conn_rnd) ^
111 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Julius Volz28364a52008-09-02 15:55:43 +0200112#endif
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100113 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
114 ip_vs_conn_rnd) ^
115 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Simon Horman85999282010-08-22 21:37:53 +0900118static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
119 bool inverse)
120{
121 const union nf_inet_addr *addr;
122 __be16 port;
123
Simon Hormanf71499a2010-08-22 21:37:54 +0900124 if (p->pe_data && p->pe->hashkey_raw)
Simon Horman85999282010-08-22 21:37:53 +0900125 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
126 ip_vs_conn_tab_mask;
127
128 if (likely(!inverse)) {
129 addr = p->caddr;
130 port = p->cport;
131 } else {
132 addr = p->vaddr;
133 port = p->vport;
134 }
135
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100136 return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port);
Simon Horman85999282010-08-22 21:37:53 +0900137}
138
139static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
140{
141 struct ip_vs_conn_param p;
142
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100143 ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol,
144 &cp->caddr, cp->cport, NULL, 0, &p);
Simon Horman85999282010-08-22 21:37:53 +0900145
Simon Hormane9e5eee2010-11-08 20:05:57 +0900146 if (cp->pe) {
147 p.pe = cp->pe;
Simon Horman85999282010-08-22 21:37:53 +0900148 p.pe_data = cp->pe_data;
149 p.pe_data_len = cp->pe_data_len;
150 }
151
152 return ip_vs_conn_hashkey_param(&p, false);
153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100156 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * returns bool success.
158 */
159static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
160{
Eric Dumazet95c96172012-04-15 05:58:06 +0000161 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int ret;
163
Nick Chalk26ec0372010-06-22 08:07:01 +0200164 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
165 return 0;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* Hash by protocol, client address and port */
Simon Horman85999282010-08-22 21:37:53 +0900168 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 ct_write_lock(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200171 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 cp->flags |= IP_VS_CONN_F_HASHED;
175 atomic_inc(&cp->refcnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200176 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ret = 1;
178 } else {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000179 pr_err("%s(): request for already hashed, called from %pF\n",
180 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ret = 0;
182 }
183
Sven Wegeneraea9d712010-06-09 16:10:57 +0200184 spin_unlock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ct_write_unlock(hash);
186
187 return ret;
188}
189
190
191/*
192 * UNhashes ip_vs_conn from ip_vs_conn_tab.
Julian Anastasov088339a2013-03-21 11:58:10 +0200193 * returns bool success. Caller should hold conn reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
196{
Eric Dumazet95c96172012-04-15 05:58:06 +0000197 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int ret;
199
200 /* unhash it and decrease its reference counter */
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) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200207 hlist_del_rcu(&cp->c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 cp->flags &= ~IP_VS_CONN_F_HASHED;
209 atomic_dec(&cp->refcnt);
210 ret = 1;
211 } else
212 ret = 0;
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
Julian Anastasov088339a2013-03-21 11:58:10 +0200220/* Try to unlink ip_vs_conn from ip_vs_conn_tab.
221 * returns bool success.
222 */
223static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
224{
225 unsigned int hash;
226 bool ret;
227
228 hash = ip_vs_conn_hashkey_conn(cp);
229
230 ct_write_lock(hash);
231 spin_lock(&cp->lock);
232
233 if (cp->flags & IP_VS_CONN_F_HASHED) {
234 ret = false;
235 /* Decrease refcnt and unlink conn only if we are last user */
236 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
237 hlist_del_rcu(&cp->c_list);
238 cp->flags &= ~IP_VS_CONN_F_HASHED;
239 ret = true;
240 }
241 } else
242 ret = atomic_read(&cp->refcnt) ? false : true;
243
244 spin_unlock(&cp->lock);
245 ct_write_unlock(hash);
246
247 return ret;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
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;
262
Simon Horman85999282010-08-22 21:37:53 +0900263 hash = ip_vs_conn_hashkey_param(p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Julian Anastasov088339a2013-03-21 11:58:10 +0200265 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Julian Anastasov088339a2013-03-21 11:58:10 +0200267 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900268 if (cp->af == p->af &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100269 p->cport == cp->cport && p->vport == cp->vport &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900270 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
271 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900272 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100273 p->protocol == cp->protocol &&
274 ip_vs_conn_net_eq(cp, p->net)) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200275 if (!__ip_vs_conn_get(cp))
276 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* HIT */
Julian Anastasov088339a2013-03-21 11:58:10 +0200278 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return cp;
280 }
281 }
282
Julian Anastasov088339a2013-03-21 11:58:10 +0200283 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
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,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200311 int inverse, struct ip_vs_conn_param *p)
Simon Hormanf11017e2010-08-22 21:37:52 +0900312{
313 __be16 _ports[2], *pptr;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100314 struct net *net = skb_net(skb);
Simon Hormanf11017e2010-08-22 21:37:52 +0900315
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200316 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Simon Hormanf11017e2010-08-22 21:37:52 +0900317 if (pptr == NULL)
318 return 1;
319
320 if (likely(!inverse))
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100321 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr,
322 pptr[0], &iph->daddr, pptr[1], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900323 else
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100324 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr,
325 pptr[1], &iph->saddr, pptr[0], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900326 return 0;
327}
328
Simon Horman5c0d2372010-08-02 17:12:44 +0200329struct ip_vs_conn *
330ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200331 const struct ip_vs_iphdr *iph, int inverse)
Simon Horman5c0d2372010-08-02 17:12:44 +0200332{
Simon Hormanf11017e2010-08-22 21:37:52 +0900333 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200334
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200335 if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200336 return NULL;
337
Simon Hormanf11017e2010-08-22 21:37:52 +0900338 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200339}
340EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
341
Julian Anastasov87375ab2005-09-14 21:08:51 -0700342/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900343struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700344{
Eric Dumazet95c96172012-04-15 05:58:06 +0000345 unsigned int hash;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700346 struct ip_vs_conn *cp;
347
Simon Horman85999282010-08-22 21:37:53 +0900348 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700349
Julian Anastasov088339a2013-03-21 11:58:10 +0200350 rcu_read_lock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700351
Julian Anastasov088339a2013-03-21 11:58:10 +0200352 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100353 if (!ip_vs_conn_net_eq(cp, p->net))
354 continue;
Simon Hormanf71499a2010-08-22 21:37:54 +0900355 if (p->pe_data && p->pe->ct_match) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200356 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
357 if (__ip_vs_conn_get(cp))
358 goto out;
359 }
Simon Horman85999282010-08-22 21:37:53 +0900360 continue;
361 }
362
Simon Hormanf11017e2010-08-22 21:37:52 +0900363 if (cp->af == p->af &&
364 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000365 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900366 * p->vaddr is a fwmark */
367 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
368 p->af, p->vaddr, &cp->vaddr) &&
369 p->cport == cp->cport && p->vport == cp->vport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700370 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julian Anastasov088339a2013-03-21 11:58:10 +0200371 p->protocol == cp->protocol) {
372 if (__ip_vs_conn_get(cp))
373 goto out;
374 }
Julian Anastasov87375ab2005-09-14 21:08:51 -0700375 }
376 cp = NULL;
377
378 out:
Julian Anastasov088339a2013-03-21 11:58:10 +0200379 rcu_read_unlock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700380
Julius Volz28364a52008-09-02 15:55:43 +0200381 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900382 ip_vs_proto_name(p->protocol),
383 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
384 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200385 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700386
387 return cp;
388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Simon Hormanf11017e2010-08-22 21:37:52 +0900390/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
391 * Called for pkts coming from inside-to-OUTside.
392 * p->caddr, p->cport: pkt source address (inside host)
393 * p->vaddr, p->vport: pkt dest address (foreign host) */
394struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Eric Dumazet95c96172012-04-15 05:58:06 +0000396 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct ip_vs_conn *cp, *ret=NULL;
398
399 /*
400 * Check for "full" addressed entries
401 */
Simon Horman85999282010-08-22 21:37:53 +0900402 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Julian Anastasov088339a2013-03-21 11:58:10 +0200404 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Julian Anastasov088339a2013-03-21 11:58:10 +0200406 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Simon Hormanf11017e2010-08-22 21:37:52 +0900407 if (cp->af == p->af &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100408 p->vport == cp->cport && p->cport == cp->dport &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900409 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
410 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100411 p->protocol == cp->protocol &&
412 ip_vs_conn_net_eq(cp, p->net)) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200413 if (!__ip_vs_conn_get(cp))
414 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ret = cp;
417 break;
418 }
419 }
420
Julian Anastasov088339a2013-03-21 11:58:10 +0200421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Julius Volz28364a52008-09-02 15:55:43 +0200423 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900424 ip_vs_proto_name(p->protocol),
425 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
426 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200427 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return ret;
430}
431
Simon Horman5c0d2372010-08-02 17:12:44 +0200432struct ip_vs_conn *
433ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200434 const struct ip_vs_iphdr *iph, int inverse)
Simon Horman5c0d2372010-08-02 17:12:44 +0200435{
Simon Hormanf11017e2010-08-22 21:37:52 +0900436 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200437
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200438 if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200439 return NULL;
440
Simon Hormanf11017e2010-08-22 21:37:52 +0900441 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200442}
443EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445/*
446 * Put back the conn and restart its timer with its timeout
447 */
448void ip_vs_conn_put(struct ip_vs_conn *cp)
449{
Nick Chalk26ec0372010-06-22 08:07:01 +0200450 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
451 0 : cp->timeout;
452 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 __ip_vs_conn_put(cp);
455}
456
457
458/*
459 * Fill a no_client_port connection with a client port number
460 */
Al Viro014d7302006-09-28 14:29:52 -0700461void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 if (ip_vs_conn_unhash(cp)) {
464 spin_lock(&cp->lock);
465 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
466 atomic_dec(&ip_vs_conn_no_cport_cnt);
467 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
468 cp->cport = cport;
469 }
470 spin_unlock(&cp->lock);
471
472 /* hash on new dport */
473 ip_vs_conn_hash(cp);
474 }
475}
476
477
478/*
479 * Bind a connection entry with the corresponding packet_xmit.
480 * Called by ip_vs_conn_new.
481 */
482static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
483{
484 switch (IP_VS_FWD_METHOD(cp)) {
485 case IP_VS_CONN_F_MASQ:
486 cp->packet_xmit = ip_vs_nat_xmit;
487 break;
488
489 case IP_VS_CONN_F_TUNNEL:
490 cp->packet_xmit = ip_vs_tunnel_xmit;
491 break;
492
493 case IP_VS_CONN_F_DROUTE:
494 cp->packet_xmit = ip_vs_dr_xmit;
495 break;
496
497 case IP_VS_CONN_F_LOCALNODE:
498 cp->packet_xmit = ip_vs_null_xmit;
499 break;
500
501 case IP_VS_CONN_F_BYPASS:
502 cp->packet_xmit = ip_vs_bypass_xmit;
503 break;
504 }
505}
506
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200507#ifdef CONFIG_IP_VS_IPV6
508static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
509{
510 switch (IP_VS_FWD_METHOD(cp)) {
511 case IP_VS_CONN_F_MASQ:
512 cp->packet_xmit = ip_vs_nat_xmit_v6;
513 break;
514
515 case IP_VS_CONN_F_TUNNEL:
516 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
517 break;
518
519 case IP_VS_CONN_F_DROUTE:
520 cp->packet_xmit = ip_vs_dr_xmit_v6;
521 break;
522
523 case IP_VS_CONN_F_LOCALNODE:
524 cp->packet_xmit = ip_vs_null_xmit;
525 break;
526
527 case IP_VS_CONN_F_BYPASS:
528 cp->packet_xmit = ip_vs_bypass_xmit_v6;
529 break;
530 }
531}
532#endif
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
536{
537 return atomic_read(&dest->activeconns)
538 + atomic_read(&dest->inactconns);
539}
540
541/*
542 * Bind a connection entry with a virtual service destination
543 * Called just after a new connection entry is created.
544 */
545static inline void
546ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
547{
Julian Anastasov35757922010-09-17 14:18:16 +0200548 unsigned int conn_flags;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200549 __u32 flags;
Julian Anastasov35757922010-09-17 14:18:16 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* if dest is NULL, then return directly */
552 if (!dest)
553 return;
554
555 /* Increase the refcnt counter of the dest */
556 atomic_inc(&dest->refcnt);
557
Julian Anastasov35757922010-09-17 14:18:16 +0200558 conn_flags = atomic_read(&dest->conn_flags);
559 if (cp->protocol != IPPROTO_UDP)
560 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200561 flags = cp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Bind with the destination and its corresponding transmitter */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200563 if (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 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200567 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julian Anastasov35757922010-09-17 14:18:16 +0200568 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300569 /* connections inherit forwarding method from dest */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200570 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200571 }
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200572 flags |= conn_flags;
573 cp->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 cp->dest = dest;
575
Julius Volzcfc78c52008-09-02 15:55:53 +0200576 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
577 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
578 "dest->refcnt:%d\n",
579 ip_vs_proto_name(cp->protocol),
580 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
581 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
582 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
583 ip_vs_fwd_tag(cp), cp->state,
584 cp->flags, atomic_read(&cp->refcnt),
585 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Update the connection counters */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200588 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300589 /* It is a normal connection, so modify the counters
590 * according to the flags, later the protocol can
591 * update them on state change
592 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200593 if (!(flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800594 atomic_inc(&dest->activeconns);
595 else
596 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
598 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300599 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 atomic_inc(&dest->persistconns);
601 }
602
603 if (dest->u_threshold != 0 &&
604 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
605 dest->flags |= IP_VS_DEST_F_OVERLOAD;
606}
607
608
609/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800610 * Check if there is a destination for the connection, if so
611 * bind the connection to the destination.
612 */
613struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
614{
615 struct ip_vs_dest *dest;
616
Julian Anastasov882a8442012-04-24 23:46:37 +0300617 dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr,
618 cp->dport, &cp->vaddr, cp->vport,
619 cp->protocol, cp->fwmark, cp->flags);
620 if (dest) {
621 struct ip_vs_proto_data *pd;
622
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200623 spin_lock(&cp->lock);
624 if (cp->dest) {
625 spin_unlock(&cp->lock);
626 return dest;
627 }
628
Julian Anastasov882a8442012-04-24 23:46:37 +0300629 /* Applications work depending on the forwarding method
630 * but better to reassign them always when binding dest */
631 if (cp->app)
632 ip_vs_unbind_app(cp);
633
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800634 ip_vs_bind_dest(cp, dest);
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200635 spin_unlock(&cp->lock);
Julian Anastasov882a8442012-04-24 23:46:37 +0300636
637 /* Update its packet transmitter */
638 cp->packet_xmit = NULL;
639#ifdef CONFIG_IP_VS_IPV6
640 if (cp->af == AF_INET6)
641 ip_vs_bind_xmit_v6(cp);
642 else
643#endif
644 ip_vs_bind_xmit(cp);
645
646 pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol);
647 if (pd && atomic_read(&pd->appcnt))
648 ip_vs_bind_app(cp, pd->pp);
649 }
650 return dest;
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800651}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800652
653
654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * Unbind a connection entry with its VS destination
656 * Called by the ip_vs_conn_expire function.
657 */
658static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
659{
660 struct ip_vs_dest *dest = cp->dest;
661
662 if (!dest)
663 return;
664
Julius Volzcfc78c52008-09-02 15:55:53 +0200665 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
666 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
667 "dest->refcnt:%d\n",
668 ip_vs_proto_name(cp->protocol),
669 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
670 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
671 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
672 ip_vs_fwd_tag(cp), cp->state,
673 cp->flags, atomic_read(&cp->refcnt),
674 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700677 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* It is a normal connection, so decrease the inactconns
679 or activeconns counter */
680 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
681 atomic_dec(&dest->inactconns);
682 } else {
683 atomic_dec(&dest->activeconns);
684 }
685 } else {
686 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300687 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 atomic_dec(&dest->persistconns);
689 }
690
691 if (dest->l_threshold != 0) {
692 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
693 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
694 } else if (dest->u_threshold != 0) {
695 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
696 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
697 } else {
698 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
699 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
700 }
701
702 /*
703 * Simply decrease the refcnt of the dest, because the
704 * dest will be either in service's destination list
705 * or in the trash.
706 */
707 atomic_dec(&dest->refcnt);
708}
709
Simon Horman8e1b0b12011-02-04 18:33:01 +0900710static int expire_quiescent_template(struct netns_ipvs *ipvs,
711 struct ip_vs_dest *dest)
712{
713#ifdef CONFIG_SYSCTL
714 return ipvs->sysctl_expire_quiescent_template &&
715 (atomic_read(&dest->weight) == 0);
716#else
717 return 0;
718#endif
719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721/*
722 * Checking if the destination of a connection template is available.
723 * If available, return 1, otherwise invalidate this connection
724 * template and return 0.
725 */
726int ip_vs_check_template(struct ip_vs_conn *ct)
727{
728 struct ip_vs_dest *dest = ct->dest;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100729 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
732 * Checking the dest server status.
733 */
734 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900735 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900736 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200737 IP_VS_DBG_BUF(9, "check_template: dest not available for "
738 "protocol %s s:%s:%d v:%s:%d "
739 "-> d:%s:%d\n",
740 ip_vs_proto_name(ct->protocol),
741 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
742 ntohs(ct->cport),
743 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
744 ntohs(ct->vport),
745 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
746 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 * Invalidate the connection template
750 */
Al Viro014d7302006-09-28 14:29:52 -0700751 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700753 ct->dport = htons(0xffff);
754 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ct->cport = 0;
756 ip_vs_conn_hash(ct);
757 }
758 }
759
760 /*
761 * Simply decrease the refcnt of the template,
762 * don't restart its timer.
763 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200764 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766 }
767 return 1;
768}
769
Julian Anastasov088339a2013-03-21 11:58:10 +0200770static void ip_vs_conn_rcu_free(struct rcu_head *head)
771{
772 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
773 rcu_head);
774
775 ip_vs_pe_put(cp->pe);
776 kfree(cp->pe_data);
777 kmem_cache_free(ip_vs_conn_cachep, cp);
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static void ip_vs_conn_expire(unsigned long data)
781{
782 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300783 struct net *net = ip_vs_conn_net(cp);
784 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * do I control anybody?
788 */
789 if (atomic_read(&cp->n_control))
790 goto expire_later;
791
Julian Anastasov088339a2013-03-21 11:58:10 +0200792 /* Unlink conn if not referenced anymore */
793 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000795 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* does anybody control me? */
798 if (cp->control)
799 ip_vs_control_del(cp);
800
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200801 if (cp->flags & IP_VS_CONN_F_NFCT) {
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200802 ip_vs_conn_drop_conntrack(cp);
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200803 /* Do not access conntracks during subsys cleanup
804 * because nf_conntrack_find_get can not be used after
805 * conntrack cleanup for the net.
806 */
807 smp_rmb();
808 if (ipvs->enable)
809 ip_vs_conn_drop_conntrack(cp);
810 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200811
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);
Julian Anastasov088339a2013-03-21 11:58:10 +0200817 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100818 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200823 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
824 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 atomic_read(&cp->n_control));
826
Julian Anastasov088339a2013-03-21 11:58:10 +0200827 atomic_inc(&cp->refcnt);
828 cp->timeout = 60*HZ;
829
Julian Anastasov749c42b2012-04-24 23:46:40 +0300830 if (ipvs->sync_state & IP_VS_STATE_MASTER)
831 ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs));
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ip_vs_conn_put(cp);
834}
835
Julian Anastasov088339a2013-03-21 11:58:10 +0200836/* Modify timer, so that it expires as soon as possible.
837 * Can be called without reference only if under RCU lock.
838 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
840{
Julian Anastasov088339a2013-03-21 11:58:10 +0200841 /* Using mod_timer_pending will ensure the timer is not
842 * modified after the final del_timer in ip_vs_conn_expire.
843 */
844 if (timer_pending(&cp->timer) &&
845 time_after(cp->timer.expires, jiffies))
846 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849
850/*
851 * Create a new connection entry and hash it into the ip_vs_conn_tab
852 */
853struct ip_vs_conn *
Simon Hormanf11017e2010-08-22 21:37:52 +0900854ip_vs_conn_new(const struct ip_vs_conn_param *p,
Eric Dumazet95c96172012-04-15 05:58:06 +0000855 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100856 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100859 struct netns_ipvs *ipvs = net_ipvs(p->net);
860 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net,
861 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800863 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000865 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return NULL;
867 }
868
Changli Gao731109e2011-02-19 18:05:08 +0800869 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800870 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100871 ip_vs_conn_net_set(cp, p->net);
Simon Hormanf11017e2010-08-22 21:37:52 +0900872 cp->af = p->af;
873 cp->protocol = p->protocol;
874 ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
875 cp->cport = p->cport;
876 ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
877 cp->vport = p->vport;
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000878 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
Simon Hormanf11017e2010-08-22 21:37:52 +0900879 ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000880 &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 cp->dport = dport;
882 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100883 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900884 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
885 ip_vs_pe_get(p->pe);
886 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900887 cp->pe_data = p->pe_data;
888 cp->pe_data_len = p->pe_data_len;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 spin_lock_init(&cp->lock);
891
892 /*
893 * Set the entry is referenced by the current thread before hashing
894 * it in the table, so that other thread run ip_vs_random_dropentry
895 * but cannot drop this entry.
896 */
897 atomic_set(&cp->refcnt, 1);
898
899 atomic_set(&cp->n_control, 0);
900 atomic_set(&cp->in_pkts, 0);
901
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100902 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (flags & IP_VS_CONN_F_NO_CPORT)
904 atomic_inc(&ip_vs_conn_no_cport_cnt);
905
906 /* Bind the connection with a destination server */
907 ip_vs_bind_dest(cp, dest);
908
909 /* Set its state and timeout */
910 cp->state = 0;
911 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300912 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200915#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900916 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200917 ip_vs_bind_xmit_v6(cp);
918 else
919#endif
920 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100922 if (unlikely(pd && atomic_read(&pd->appcnt)))
923 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200925 /*
926 * Allow conntrack to be preserved. By default, conntrack
927 * is created and destroyed for every packet.
928 * Sometimes keeping conntrack can be useful for
929 * IP_VS_CONN_F_ONE_PACKET too.
930 */
931
Hans Schillstroma0840e22011-01-03 14:44:58 +0100932 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200933 cp->flags |= IP_VS_CONN_F_NFCT;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* Hash it in the ip_vs_conn_tab finally */
936 ip_vs_conn_hash(cp);
937
938 return cp;
939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/*
942 * /proc/net/ip_vs_conn entries
943 */
944#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100945struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +0800946 struct seq_net_private p;
947 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100948};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
951{
952 int idx;
953 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100954 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900955
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100956 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200957 rcu_read_lock();
958 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
959 /* __ip_vs_conn_get() is not needed by
960 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100963 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +0800964 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 }
Julian Anastasov088339a2013-03-21 11:58:10 +0200967 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
970 return NULL;
971}
972
973static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
974{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100975 struct ip_vs_iter_state *iter = seq->private;
976
977 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
979}
980
981static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
982{
983 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100984 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +0200985 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +0800986 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int idx;
988
989 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900990 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ip_vs_conn_array(seq, 0);
992
993 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +0200994 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
995 if (e)
996 return hlist_entry(e, struct ip_vs_conn, c_list);
997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001000 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001001 rcu_read_lock();
1002 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001003 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001005 }
Julian Anastasov088339a2013-03-21 11:58:10 +02001006 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001008 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return NULL;
1010}
1011
1012static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1013{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001014 struct ip_vs_iter_state *iter = seq->private;
Changli Gao731109e2011-02-19 18:05:08 +08001015 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (l)
Julian Anastasov088339a2013-03-21 11:58:10 +02001018 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1022{
1023
1024 if (v == SEQ_START_TOKEN)
1025 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001026 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else {
1028 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001029 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001030 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1031 size_t len = 0;
1032
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001033 if (!ip_vs_conn_net_eq(cp, net))
1034 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001035 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001036 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001037 len = strlen(cp->pe->name);
1038 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001039 pe_data[len + 1] = ' ';
1040 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001041 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001042 }
1043 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Vince Busam667a5f12008-09-02 15:55:49 +02001045#ifdef CONFIG_IP_VS_IPV6
1046 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001047 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1048 "%pI6 %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001049 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001050 &cp->caddr.in6, ntohs(cp->cport),
1051 &cp->vaddr.in6, ntohs(cp->vport),
1052 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001053 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001054 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001055 else
1056#endif
1057 seq_printf(seq,
1058 "%-3s %08X %04X %08X %04X"
Simon Hormana3c918a2010-08-22 21:37:53 +09001059 " %08X %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001061 ntohl(cp->caddr.ip), ntohs(cp->cport),
1062 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1063 ntohl(cp->daddr.ip), ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001065 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 return 0;
1068}
1069
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001070static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 .start = ip_vs_conn_seq_start,
1072 .next = ip_vs_conn_seq_next,
1073 .stop = ip_vs_conn_seq_stop,
1074 .show = ip_vs_conn_seq_show,
1075};
1076
1077static int ip_vs_conn_open(struct inode *inode, struct file *file)
1078{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001079 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1080 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Arjan van de Ven9a321442007-02-12 00:55:35 -08001083static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 .owner = THIS_MODULE,
1085 .open = ip_vs_conn_open,
1086 .read = seq_read,
1087 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001088 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001090
Eric Dumazet95c96172012-04-15 05:58:06 +00001091static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001092{
1093 if (flags & IP_VS_CONN_F_SYNC)
1094 return "SYNC";
1095 else
1096 return "LOCAL";
1097}
1098
1099static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1100{
1101
1102 if (v == SEQ_START_TOKEN)
1103 seq_puts(seq,
1104 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1105 else {
1106 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001107 struct net *net = seq_file_net(seq);
1108
1109 if (!ip_vs_conn_net_eq(cp, net))
1110 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001111
Vince Busam667a5f12008-09-02 15:55:49 +02001112#ifdef CONFIG_IP_VS_IPV6
1113 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001114 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001115 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001116 &cp->caddr.in6, ntohs(cp->cport),
1117 &cp->vaddr.in6, ntohs(cp->vport),
1118 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001119 ip_vs_state_name(cp->protocol, cp->state),
1120 ip_vs_origin_name(cp->flags),
1121 (cp->timer.expires-jiffies)/HZ);
1122 else
1123#endif
1124 seq_printf(seq,
1125 "%-3s %08X %04X %08X %04X "
1126 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001127 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001128 ntohl(cp->caddr.ip), ntohs(cp->cport),
1129 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1130 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001131 ip_vs_state_name(cp->protocol, cp->state),
1132 ip_vs_origin_name(cp->flags),
1133 (cp->timer.expires-jiffies)/HZ);
1134 }
1135 return 0;
1136}
1137
1138static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1139 .start = ip_vs_conn_seq_start,
1140 .next = ip_vs_conn_seq_next,
1141 .stop = ip_vs_conn_seq_stop,
1142 .show = ip_vs_conn_sync_seq_show,
1143};
1144
1145static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1146{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001147 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1148 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001149}
1150
1151static const struct file_operations ip_vs_conn_sync_fops = {
1152 .owner = THIS_MODULE,
1153 .open = ip_vs_conn_sync_open,
1154 .read = seq_read,
1155 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001156 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001157};
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159#endif
1160
1161
1162/*
1163 * Randomly drop connection entries before running out of memory
1164 */
1165static inline int todrop_entry(struct ip_vs_conn *cp)
1166{
1167 /*
1168 * The drop rate array needs tuning for real environments.
1169 * Called from timer bh only => no locking
1170 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001171 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 static char todrop_counter[9] = {0};
1173 int i;
1174
1175 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1176 This will leave enough time for normal connection to get
1177 through. */
1178 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1179 return 0;
1180
1181 /* Don't drop the entry if its number of incoming packets is not
1182 located in [0, 8] */
1183 i = atomic_read(&cp->in_pkts);
1184 if (i > 8 || i < 0) return 0;
1185
1186 if (!todrop_rate[i]) return 0;
1187 if (--todrop_counter[i] > 0) return 0;
1188
1189 todrop_counter[i] = todrop_rate[i];
1190 return 1;
1191}
1192
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001193/* Called from keventd and must protect itself from softirqs */
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001194void ip_vs_random_dropentry(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001197 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Randomly scan 1/32 of the whole table every second
1201 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001202 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001203 unsigned int hash = net_random() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /*
1206 * Lock is actually needed in this loop.
1207 */
Julian Anastasov088339a2013-03-21 11:58:10 +02001208 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Julian Anastasov088339a2013-03-21 11:58:10 +02001210 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001211 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* connection template */
1213 continue;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001214 if (!ip_vs_conn_net_eq(cp, net))
1215 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (cp->protocol == IPPROTO_TCP) {
1217 switch(cp->state) {
1218 case IP_VS_TCP_S_SYN_RECV:
1219 case IP_VS_TCP_S_SYNACK:
1220 break;
1221
1222 case IP_VS_TCP_S_ESTABLISHED:
1223 if (todrop_entry(cp))
1224 break;
1225 continue;
1226
1227 default:
1228 continue;
1229 }
1230 } else {
1231 if (!todrop_entry(cp))
1232 continue;
1233 }
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 IP_VS_DBG(4, "del connection\n");
1236 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001237 cp_c = cp->control;
1238 /* cp->control is valid only with reference to cp */
1239 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001241 ip_vs_conn_expire_now(cp_c);
1242 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Julian Anastasov088339a2013-03-21 11:58:10 +02001245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247}
1248
1249
1250/*
1251 * Flush all the connection entries in the ip_vs_conn_tab
1252 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001253static void ip_vs_conn_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001256 struct ip_vs_conn *cp, *cp_c;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001257 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Hans Schillstroma0840e22011-01-03 14:44:58 +01001259flush_again:
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001260 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Lock is actually needed in this loop.
1263 */
Julian Anastasov088339a2013-03-21 11:58:10 +02001264 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Julian Anastasov088339a2013-03-21 11:58:10 +02001266 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001267 if (!ip_vs_conn_net_eq(cp, net))
1268 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 IP_VS_DBG(4, "del connection\n");
1270 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001271 cp_c = cp->control;
1272 /* cp->control is valid only with reference to cp */
1273 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001275 ip_vs_conn_expire_now(cp_c);
1276 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Julian Anastasov088339a2013-03-21 11:58:10 +02001279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 /* the counter may be not NULL, because maybe some conn entries
1283 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001284 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 schedule();
1286 goto flush_again;
1287 }
1288}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001289/*
1290 * per netns init and exit
1291 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001292int __net_init ip_vs_conn_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001293{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001294 struct netns_ipvs *ipvs = net_ipvs(net);
1295
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001296 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Gao fengd4beaa62013-02-18 01:34:54 +00001298 proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1299 proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001300 return 0;
1301}
1302
Hans Schillstrom503cf152011-05-01 18:50:16 +02001303void __net_exit ip_vs_conn_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001304{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001305 /* flush all the connection entries first */
1306 ip_vs_conn_flush(net);
Gao fengece31ff2013-02-18 01:34:56 +00001307 remove_proc_entry("ip_vs_conn", net->proc_net);
1308 remove_proc_entry("ip_vs_conn_sync", net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001309}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Sven Wegener048cf482008-08-10 18:24:35 +00001311int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int idx;
1314
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001315 /* Compute size and mask */
1316 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1317 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * Allocate the connection hash table and initialize its list heads
1321 */
Changli Gao731109e2011-02-19 18:05:08 +08001322 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!ip_vs_conn_tab)
1324 return -ENOMEM;
1325
1326 /* Allocate ip_vs_conn slab cache */
1327 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1328 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001329 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!ip_vs_conn_cachep) {
1331 vfree(ip_vs_conn_tab);
1332 return -ENOMEM;
1333 }
1334
Hannes Eder1e3e2382009-08-02 11:05:41 +00001335 pr_info("Connection hash table configured "
1336 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001337 ip_vs_conn_tab_size,
1338 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1340 sizeof(struct ip_vs_conn));
1341
Changli Gao731109e2011-02-19 18:05:08 +08001342 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1343 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001346 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* calculate the random value for connection hash */
1350 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1351
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355void ip_vs_conn_cleanup(void)
1356{
Julian Anastasov088339a2013-03-21 11:58:10 +02001357 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1358 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Release the empty cache */
1360 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 vfree(ip_vs_conn_tab);
1362}