blob: 8f4c602bb34bfb4d7f9ad82cd0ac7fb08526a589 [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
Julian Anastasovac692692013-03-22 11:46:54 +020089static inline void ct_write_lock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Julian Anastasovac692692013-03-22 11:46:54 +020091 spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Julian Anastasovac692692013-03-22 11:46:54 +020094static inline void ct_write_unlock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Julian Anastasovac692692013-03-22 11:46:54 +020096 spin_unlock_bh(&__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
Julian Anastasovac692692013-03-22 11:46:54 +0200170 ct_write_lock_bh(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);
Julian Anastasovac692692013-03-22 11:46:54 +0200185 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
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
Julian Anastasovac692692013-03-22 11:46:54 +0200203 ct_write_lock_bh(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);
Julian Anastasovac692692013-03-22 11:46:54 +0200215 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
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
Julian Anastasovac692692013-03-22 11:46:54 +0200230 ct_write_lock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200231 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);
Julian Anastasovac692692013-03-22 11:46:54 +0200245 ct_write_unlock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200246
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) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200268 if (p->cport == cp->cport && p->vport == cp->vport &&
269 cp->af == p->af &&
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) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200353 if (unlikely(p->pe_data && p->pe->ct_match)) {
354 if (!ip_vs_conn_net_eq(cp, p->net))
355 continue;
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) &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200369 p->vport == cp->vport && p->cport == cp->cport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700370 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200371 p->protocol == cp->protocol &&
372 ip_vs_conn_net_eq(cp, p->net)) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200373 if (__ip_vs_conn_get(cp))
374 goto out;
375 }
Julian Anastasov87375ab2005-09-14 21:08:51 -0700376 }
377 cp = NULL;
378
379 out:
Julian Anastasov088339a2013-03-21 11:58:10 +0200380 rcu_read_unlock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700381
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;
399
400 /*
401 * Check for "full" addressed entries
402 */
Simon Horman85999282010-08-22 21:37:53 +0900403 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Julian Anastasov088339a2013-03-21 11:58:10 +0200405 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Julian Anastasov088339a2013-03-21 11:58:10 +0200407 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200408 if (p->vport == cp->cport && p->cport == cp->dport &&
409 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900410 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
411 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100412 p->protocol == cp->protocol &&
413 ip_vs_conn_net_eq(cp, p->net)) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200414 if (!__ip_vs_conn_get(cp))
415 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ret = cp;
418 break;
419 }
420 }
421
Julian Anastasov088339a2013-03-21 11:58:10 +0200422 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
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,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200435 const struct ip_vs_iphdr *iph, int inverse)
Simon Horman5c0d2372010-08-02 17:12:44 +0200436{
Simon Hormanf11017e2010-08-22 21:37:52 +0900437 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200438
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200439 if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200440 return NULL;
441
Simon Hormanf11017e2010-08-22 21:37:52 +0900442 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200443}
444EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446/*
447 * Put back the conn and restart its timer with its timeout
448 */
449void ip_vs_conn_put(struct ip_vs_conn *cp)
450{
Nick Chalk26ec0372010-06-22 08:07:01 +0200451 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
452 0 : cp->timeout;
453 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 __ip_vs_conn_put(cp);
456}
457
458
459/*
460 * Fill a no_client_port connection with a client port number
461 */
Al Viro014d7302006-09-28 14:29:52 -0700462void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 if (ip_vs_conn_unhash(cp)) {
Julian Anastasovac692692013-03-22 11:46:54 +0200465 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
467 atomic_dec(&ip_vs_conn_no_cport_cnt);
468 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
469 cp->cport = cport;
470 }
Julian Anastasovac692692013-03-22 11:46:54 +0200471 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* hash on new dport */
474 ip_vs_conn_hash(cp);
475 }
476}
477
478
479/*
480 * Bind a connection entry with the corresponding packet_xmit.
481 * Called by ip_vs_conn_new.
482 */
483static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
484{
485 switch (IP_VS_FWD_METHOD(cp)) {
486 case IP_VS_CONN_F_MASQ:
487 cp->packet_xmit = ip_vs_nat_xmit;
488 break;
489
490 case IP_VS_CONN_F_TUNNEL:
491 cp->packet_xmit = ip_vs_tunnel_xmit;
492 break;
493
494 case IP_VS_CONN_F_DROUTE:
495 cp->packet_xmit = ip_vs_dr_xmit;
496 break;
497
498 case IP_VS_CONN_F_LOCALNODE:
499 cp->packet_xmit = ip_vs_null_xmit;
500 break;
501
502 case IP_VS_CONN_F_BYPASS:
503 cp->packet_xmit = ip_vs_bypass_xmit;
504 break;
505 }
506}
507
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200508#ifdef CONFIG_IP_VS_IPV6
509static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
510{
511 switch (IP_VS_FWD_METHOD(cp)) {
512 case IP_VS_CONN_F_MASQ:
513 cp->packet_xmit = ip_vs_nat_xmit_v6;
514 break;
515
516 case IP_VS_CONN_F_TUNNEL:
517 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
518 break;
519
520 case IP_VS_CONN_F_DROUTE:
521 cp->packet_xmit = ip_vs_dr_xmit_v6;
522 break;
523
524 case IP_VS_CONN_F_LOCALNODE:
525 cp->packet_xmit = ip_vs_null_xmit;
526 break;
527
528 case IP_VS_CONN_F_BYPASS:
529 cp->packet_xmit = ip_vs_bypass_xmit_v6;
530 break;
531 }
532}
533#endif
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
537{
538 return atomic_read(&dest->activeconns)
539 + atomic_read(&dest->inactconns);
540}
541
542/*
543 * Bind a connection entry with a virtual service destination
544 * Called just after a new connection entry is created.
545 */
546static inline void
547ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
548{
Julian Anastasov35757922010-09-17 14:18:16 +0200549 unsigned int conn_flags;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200550 __u32 flags;
Julian Anastasov35757922010-09-17 14:18:16 +0200551
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 */
Julian Anastasovfca9c202013-03-22 11:46:38 +0200557 ip_vs_dest_hold(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
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;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200562 flags = cp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Bind with the destination and its corresponding transmitter */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200564 if (flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800565 /* if the connection is not template and is created
566 * by sync, preserve the activity flag.
567 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200568 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julian Anastasov35757922010-09-17 14:18:16 +0200569 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300570 /* connections inherit forwarding method from dest */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200571 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200572 }
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200573 flags |= conn_flags;
574 cp->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 cp->dest = dest;
576
Julius Volzcfc78c52008-09-02 15:55:53 +0200577 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
578 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
579 "dest->refcnt:%d\n",
580 ip_vs_proto_name(cp->protocol),
581 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
582 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
583 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
584 ip_vs_fwd_tag(cp), cp->state,
585 cp->flags, atomic_read(&cp->refcnt),
586 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Update the connection counters */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200589 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300590 /* It is a normal connection, so modify the counters
591 * according to the flags, later the protocol can
592 * update them on state change
593 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200594 if (!(flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800595 atomic_inc(&dest->activeconns);
596 else
597 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else {
599 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300600 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 atomic_inc(&dest->persistconns);
602 }
603
604 if (dest->u_threshold != 0 &&
605 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
606 dest->flags |= IP_VS_DEST_F_OVERLOAD;
607}
608
609
610/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800611 * Check if there is a destination for the connection, if so
612 * bind the connection to the destination.
613 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200614void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800615{
616 struct ip_vs_dest *dest;
617
Julian Anastasov413c2d042013-03-22 11:46:52 +0200618 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -0700619
620 /* This function is only invoked by the synchronization code. We do
621 * not currently support heterogeneous pools with synchronization,
622 * so we can make the assumption that the svc_af is the same as the
623 * dest_af
624 */
625 dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, cp->af, &cp->daddr,
Julian Anastasov882a8442012-04-24 23:46:37 +0300626 cp->dport, &cp->vaddr, cp->vport,
627 cp->protocol, cp->fwmark, cp->flags);
628 if (dest) {
629 struct ip_vs_proto_data *pd;
630
Julian Anastasovac692692013-03-22 11:46:54 +0200631 spin_lock_bh(&cp->lock);
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200632 if (cp->dest) {
Julian Anastasovac692692013-03-22 11:46:54 +0200633 spin_unlock_bh(&cp->lock);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200634 rcu_read_unlock();
635 return;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200636 }
637
Julian Anastasov882a8442012-04-24 23:46:37 +0300638 /* Applications work depending on the forwarding method
639 * but better to reassign them always when binding dest */
640 if (cp->app)
641 ip_vs_unbind_app(cp);
642
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800643 ip_vs_bind_dest(cp, dest);
Julian Anastasovac692692013-03-22 11:46:54 +0200644 spin_unlock_bh(&cp->lock);
Julian Anastasov882a8442012-04-24 23:46:37 +0300645
646 /* Update its packet transmitter */
647 cp->packet_xmit = NULL;
648#ifdef CONFIG_IP_VS_IPV6
649 if (cp->af == AF_INET6)
650 ip_vs_bind_xmit_v6(cp);
651 else
652#endif
653 ip_vs_bind_xmit(cp);
654
655 pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol);
656 if (pd && atomic_read(&pd->appcnt))
657 ip_vs_bind_app(cp, pd->pp);
658 }
Julian Anastasov413c2d042013-03-22 11:46:52 +0200659 rcu_read_unlock();
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800660}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800661
662
663/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * Unbind a connection entry with its VS destination
665 * Called by the ip_vs_conn_expire function.
666 */
667static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
668{
669 struct ip_vs_dest *dest = cp->dest;
670
671 if (!dest)
672 return;
673
Julius Volzcfc78c52008-09-02 15:55:53 +0200674 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
675 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
676 "dest->refcnt:%d\n",
677 ip_vs_proto_name(cp->protocol),
678 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
679 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
680 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
681 ip_vs_fwd_tag(cp), cp->state,
682 cp->flags, atomic_read(&cp->refcnt),
683 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700686 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* It is a normal connection, so decrease the inactconns
688 or activeconns counter */
689 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
690 atomic_dec(&dest->inactconns);
691 } else {
692 atomic_dec(&dest->activeconns);
693 }
694 } else {
695 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300696 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 atomic_dec(&dest->persistconns);
698 }
699
700 if (dest->l_threshold != 0) {
701 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
702 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
703 } else if (dest->u_threshold != 0) {
704 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
705 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
706 } else {
707 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
708 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
709 }
710
Julian Anastasovfca9c202013-03-22 11:46:38 +0200711 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Simon Horman8e1b0b12011-02-04 18:33:01 +0900714static int expire_quiescent_template(struct netns_ipvs *ipvs,
715 struct ip_vs_dest *dest)
716{
717#ifdef CONFIG_SYSCTL
718 return ipvs->sysctl_expire_quiescent_template &&
719 (atomic_read(&dest->weight) == 0);
720#else
721 return 0;
722#endif
723}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725/*
726 * Checking if the destination of a connection template is available.
727 * If available, return 1, otherwise invalidate this connection
728 * template and return 0.
729 */
730int ip_vs_check_template(struct ip_vs_conn *ct)
731{
732 struct ip_vs_dest *dest = ct->dest;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100733 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * Checking the dest server status.
737 */
738 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900739 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900740 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200741 IP_VS_DBG_BUF(9, "check_template: dest not available for "
742 "protocol %s s:%s:%d v:%s:%d "
743 "-> d:%s:%d\n",
744 ip_vs_proto_name(ct->protocol),
745 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
746 ntohs(ct->cport),
747 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
748 ntohs(ct->vport),
749 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
750 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * Invalidate the connection template
754 */
Al Viro014d7302006-09-28 14:29:52 -0700755 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700757 ct->dport = htons(0xffff);
758 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ct->cport = 0;
760 ip_vs_conn_hash(ct);
761 }
762 }
763
764 /*
765 * Simply decrease the refcnt of the template,
766 * don't restart its timer.
767 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200768 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
770 }
771 return 1;
772}
773
Julian Anastasov088339a2013-03-21 11:58:10 +0200774static void ip_vs_conn_rcu_free(struct rcu_head *head)
775{
776 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
777 rcu_head);
778
779 ip_vs_pe_put(cp->pe);
780 kfree(cp->pe_data);
781 kmem_cache_free(ip_vs_conn_cachep, cp);
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784static void ip_vs_conn_expire(unsigned long data)
785{
786 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300787 struct net *net = ip_vs_conn_net(cp);
788 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * do I control anybody?
792 */
793 if (atomic_read(&cp->n_control))
794 goto expire_later;
795
Julian Anastasov088339a2013-03-21 11:58:10 +0200796 /* Unlink conn if not referenced anymore */
797 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000799 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* does anybody control me? */
802 if (cp->control)
803 ip_vs_control_del(cp);
804
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200805 if (cp->flags & IP_VS_CONN_F_NFCT) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200806 /* Do not access conntracks during subsys cleanup
807 * because nf_conntrack_find_get can not be used after
808 * conntrack cleanup for the net.
809 */
810 smp_rmb();
811 if (ipvs->enable)
812 ip_vs_conn_drop_conntrack(cp);
813 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (unlikely(cp->app != NULL))
816 ip_vs_unbind_app(cp);
817 ip_vs_unbind_dest(cp);
818 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
819 atomic_dec(&ip_vs_conn_no_cport_cnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200820 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100821 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return;
823 }
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200826 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
827 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 atomic_read(&cp->n_control));
829
Julian Anastasov088339a2013-03-21 11:58:10 +0200830 atomic_inc(&cp->refcnt);
831 cp->timeout = 60*HZ;
832
Julian Anastasov749c42b2012-04-24 23:46:40 +0300833 if (ipvs->sync_state & IP_VS_STATE_MASTER)
834 ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs));
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ip_vs_conn_put(cp);
837}
838
Julian Anastasov088339a2013-03-21 11:58:10 +0200839/* Modify timer, so that it expires as soon as possible.
840 * Can be called without reference only if under RCU lock.
841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
843{
Julian Anastasov088339a2013-03-21 11:58:10 +0200844 /* Using mod_timer_pending will ensure the timer is not
845 * modified after the final del_timer in ip_vs_conn_expire.
846 */
847 if (timer_pending(&cp->timer) &&
848 time_after(cp->timer.expires, jiffies))
849 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852
853/*
854 * Create a new connection entry and hash it into the ip_vs_conn_tab
855 */
856struct ip_vs_conn *
Simon Hormanf11017e2010-08-22 21:37:52 +0900857ip_vs_conn_new(const struct ip_vs_conn_param *p,
Eric Dumazet95c96172012-04-15 05:58:06 +0000858 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100859 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100862 struct netns_ipvs *ipvs = net_ipvs(p->net);
863 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net,
864 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200866 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000868 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return NULL;
870 }
871
Changli Gao731109e2011-02-19 18:05:08 +0800872 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800873 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100874 ip_vs_conn_net_set(cp, p->net);
Simon Hormanf11017e2010-08-22 21:37:52 +0900875 cp->af = p->af;
876 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200877 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900878 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100879 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200880 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100881 &cp->vaddr, p->vaddr);
882 cp->vport = p->vport;
883 ip_vs_addr_set(p->af, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 cp->dport = dport;
885 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100886 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900887 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
888 ip_vs_pe_get(p->pe);
889 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900890 cp->pe_data = p->pe_data;
891 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200892 } else {
893 cp->pe = NULL;
894 cp->pe_data = NULL;
895 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 spin_lock_init(&cp->lock);
898
899 /*
900 * Set the entry is referenced by the current thread before hashing
901 * it in the table, so that other thread run ip_vs_random_dropentry
902 * but cannot drop this entry.
903 */
904 atomic_set(&cp->refcnt, 1);
905
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200906 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 atomic_set(&cp->n_control, 0);
908 atomic_set(&cp->in_pkts, 0);
909
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200910 cp->packet_xmit = NULL;
911 cp->app = NULL;
912 cp->app_data = NULL;
913 /* reset struct ip_vs_seq */
914 cp->in_seq.delta = 0;
915 cp->out_seq.delta = 0;
916
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100917 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (flags & IP_VS_CONN_F_NO_CPORT)
919 atomic_inc(&ip_vs_conn_no_cport_cnt);
920
921 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200922 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ip_vs_bind_dest(cp, dest);
924
925 /* Set its state and timeout */
926 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200927 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300929 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200932#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900933 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200934 ip_vs_bind_xmit_v6(cp);
935 else
936#endif
937 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100939 if (unlikely(pd && atomic_read(&pd->appcnt)))
940 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200942 /*
943 * Allow conntrack to be preserved. By default, conntrack
944 * is created and destroyed for every packet.
945 * Sometimes keeping conntrack can be useful for
946 * IP_VS_CONN_F_ONE_PACKET too.
947 */
948
Hans Schillstroma0840e22011-01-03 14:44:58 +0100949 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200950 cp->flags |= IP_VS_CONN_F_NFCT;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Hash it in the ip_vs_conn_tab finally */
953 ip_vs_conn_hash(cp);
954
955 return cp;
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * /proc/net/ip_vs_conn entries
960 */
961#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100962struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +0800963 struct seq_net_private p;
964 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100965};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
968{
969 int idx;
970 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100971 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900972
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100973 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200974 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
975 /* __ip_vs_conn_get() is not needed by
976 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100979 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +0800980 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 }
Simon Hormana38e5e22013-05-22 14:50:32 +0900983 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 return NULL;
987}
988
989static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +0300990 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100992 struct ip_vs_iter_state *iter = seq->private;
993
994 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +0300995 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
997}
998
999static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1000{
1001 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001002 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001003 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001004 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int idx;
1006
1007 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001008 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return ip_vs_conn_array(seq, 0);
1010
1011 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001012 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1013 if (e)
1014 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001017 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001018 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001019 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001021 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001022 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001024 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return NULL;
1026}
1027
1028static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001029 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001031 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1035{
1036
1037 if (v == SEQ_START_TOKEN)
1038 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001039 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 else {
1041 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001042 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001043 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1044 size_t len = 0;
1045
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001046 if (!ip_vs_conn_net_eq(cp, net))
1047 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001048 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001049 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001050 len = strlen(cp->pe->name);
1051 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001052 pe_data[len + 1] = ' ';
1053 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001054 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001055 }
1056 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Vince Busam667a5f12008-09-02 15:55:49 +02001058#ifdef CONFIG_IP_VS_IPV6
1059 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001060 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1061 "%pI6 %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001062 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001063 &cp->caddr.in6, ntohs(cp->cport),
1064 &cp->vaddr.in6, ntohs(cp->vport),
1065 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001066 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001067 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001068 else
1069#endif
1070 seq_printf(seq,
1071 "%-3s %08X %04X %08X %04X"
Simon Hormana3c918a2010-08-22 21:37:53 +09001072 " %08X %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001074 ntohl(cp->caddr.ip), ntohs(cp->cport),
1075 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1076 ntohl(cp->daddr.ip), ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001078 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 return 0;
1081}
1082
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001083static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 .start = ip_vs_conn_seq_start,
1085 .next = ip_vs_conn_seq_next,
1086 .stop = ip_vs_conn_seq_stop,
1087 .show = ip_vs_conn_seq_show,
1088};
1089
1090static int ip_vs_conn_open(struct inode *inode, struct file *file)
1091{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001092 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1093 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Arjan van de Ven9a321442007-02-12 00:55:35 -08001096static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 .owner = THIS_MODULE,
1098 .open = ip_vs_conn_open,
1099 .read = seq_read,
1100 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001101 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001103
Eric Dumazet95c96172012-04-15 05:58:06 +00001104static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001105{
1106 if (flags & IP_VS_CONN_F_SYNC)
1107 return "SYNC";
1108 else
1109 return "LOCAL";
1110}
1111
1112static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1113{
1114
1115 if (v == SEQ_START_TOKEN)
1116 seq_puts(seq,
1117 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1118 else {
1119 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001120 struct net *net = seq_file_net(seq);
1121
1122 if (!ip_vs_conn_net_eq(cp, net))
1123 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001124
Vince Busam667a5f12008-09-02 15:55:49 +02001125#ifdef CONFIG_IP_VS_IPV6
1126 if (cp->af == AF_INET6)
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001127 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001128 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001129 &cp->caddr.in6, ntohs(cp->cport),
1130 &cp->vaddr.in6, ntohs(cp->vport),
1131 &cp->daddr.in6, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001132 ip_vs_state_name(cp->protocol, cp->state),
1133 ip_vs_origin_name(cp->flags),
1134 (cp->timer.expires-jiffies)/HZ);
1135 else
1136#endif
1137 seq_printf(seq,
1138 "%-3s %08X %04X %08X %04X "
1139 "%08X %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001140 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001141 ntohl(cp->caddr.ip), ntohs(cp->cport),
1142 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1143 ntohl(cp->daddr.ip), ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001144 ip_vs_state_name(cp->protocol, cp->state),
1145 ip_vs_origin_name(cp->flags),
1146 (cp->timer.expires-jiffies)/HZ);
1147 }
1148 return 0;
1149}
1150
1151static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1152 .start = ip_vs_conn_seq_start,
1153 .next = ip_vs_conn_seq_next,
1154 .stop = ip_vs_conn_seq_stop,
1155 .show = ip_vs_conn_sync_seq_show,
1156};
1157
1158static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1159{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001160 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1161 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001162}
1163
1164static const struct file_operations ip_vs_conn_sync_fops = {
1165 .owner = THIS_MODULE,
1166 .open = ip_vs_conn_sync_open,
1167 .read = seq_read,
1168 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001169 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001170};
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#endif
1173
1174
1175/*
1176 * Randomly drop connection entries before running out of memory
1177 */
1178static inline int todrop_entry(struct ip_vs_conn *cp)
1179{
1180 /*
1181 * The drop rate array needs tuning for real environments.
1182 * Called from timer bh only => no locking
1183 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001184 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 static char todrop_counter[9] = {0};
1186 int i;
1187
1188 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1189 This will leave enough time for normal connection to get
1190 through. */
1191 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1192 return 0;
1193
1194 /* Don't drop the entry if its number of incoming packets is not
1195 located in [0, 8] */
1196 i = atomic_read(&cp->in_pkts);
1197 if (i > 8 || i < 0) return 0;
1198
1199 if (!todrop_rate[i]) return 0;
1200 if (--todrop_counter[i] > 0) return 0;
1201
1202 todrop_counter[i] = todrop_rate[i];
1203 return 1;
1204}
1205
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001206/* Called from keventd and must protect itself from softirqs */
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001207void ip_vs_random_dropentry(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001210 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Simon Hormana38e5e22013-05-22 14:50:32 +09001212 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * Randomly scan 1/32 of the whole table every second
1215 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001216 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001217 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Julian Anastasov088339a2013-03-21 11:58:10 +02001219 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001220 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* connection template */
1222 continue;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001223 if (!ip_vs_conn_net_eq(cp, net))
1224 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (cp->protocol == IPPROTO_TCP) {
1226 switch(cp->state) {
1227 case IP_VS_TCP_S_SYN_RECV:
1228 case IP_VS_TCP_S_SYNACK:
1229 break;
1230
1231 case IP_VS_TCP_S_ESTABLISHED:
1232 if (todrop_entry(cp))
1233 break;
1234 continue;
1235
1236 default:
1237 continue;
1238 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001239 } else if (cp->protocol == IPPROTO_SCTP) {
1240 switch (cp->state) {
1241 case IP_VS_SCTP_S_INIT1:
1242 case IP_VS_SCTP_S_INIT:
1243 break;
1244 case IP_VS_SCTP_S_ESTABLISHED:
1245 if (todrop_entry(cp))
1246 break;
1247 continue;
1248 default:
1249 continue;
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 } else {
1252 if (!todrop_entry(cp))
1253 continue;
1254 }
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 IP_VS_DBG(4, "del connection\n");
1257 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001258 cp_c = cp->control;
1259 /* cp->control is valid only with reference to cp */
1260 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001262 ip_vs_conn_expire_now(cp_c);
1263 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001266 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001268 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271
1272/*
1273 * Flush all the connection entries in the ip_vs_conn_tab
1274 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001275static void ip_vs_conn_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001278 struct ip_vs_conn *cp, *cp_c;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001279 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Hans Schillstroma0840e22011-01-03 14:44:58 +01001281flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001282 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001283 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Julian Anastasov088339a2013-03-21 11:58:10 +02001285 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001286 if (!ip_vs_conn_net_eq(cp, net))
1287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 IP_VS_DBG(4, "del connection\n");
1289 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001290 cp_c = cp->control;
1291 /* cp->control is valid only with reference to cp */
1292 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001294 ip_vs_conn_expire_now(cp_c);
1295 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001298 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001300 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* the counter may be not NULL, because maybe some conn entries
1303 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001304 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 schedule();
1306 goto flush_again;
1307 }
1308}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001309/*
1310 * per netns init and exit
1311 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001312int __net_init ip_vs_conn_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001313{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001314 struct netns_ipvs *ipvs = net_ipvs(net);
1315
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001316 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Gao fengd4beaa62013-02-18 01:34:54 +00001318 proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1319 proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001320 return 0;
1321}
1322
Hans Schillstrom503cf152011-05-01 18:50:16 +02001323void __net_exit ip_vs_conn_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001324{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001325 /* flush all the connection entries first */
1326 ip_vs_conn_flush(net);
Gao fengece31ff2013-02-18 01:34:56 +00001327 remove_proc_entry("ip_vs_conn", net->proc_net);
1328 remove_proc_entry("ip_vs_conn_sync", net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Sven Wegener048cf482008-08-10 18:24:35 +00001331int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 int idx;
1334
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001335 /* Compute size and mask */
1336 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1337 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
1340 * Allocate the connection hash table and initialize its list heads
1341 */
Changli Gao731109e2011-02-19 18:05:08 +08001342 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!ip_vs_conn_tab)
1344 return -ENOMEM;
1345
1346 /* Allocate ip_vs_conn slab cache */
1347 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1348 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001349 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!ip_vs_conn_cachep) {
1351 vfree(ip_vs_conn_tab);
1352 return -ENOMEM;
1353 }
1354
Hannes Eder1e3e2382009-08-02 11:05:41 +00001355 pr_info("Connection hash table configured "
1356 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001357 ip_vs_conn_tab_size,
1358 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1360 sizeof(struct ip_vs_conn));
1361
Changli Gao731109e2011-02-19 18:05:08 +08001362 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1363 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001366 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* calculate the random value for connection hash */
1370 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1371
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375void ip_vs_conn_cleanup(void)
1376{
Julian Anastasov088339a2013-03-21 11:58:10 +02001377 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1378 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* Release the empty cache */
1380 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 vfree(ip_vs_conn_tab);
1382}