]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
1da177e4 LT |
8 | * 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 | ||
9aada7ac HE |
25 | #define KMSG_COMPONENT "IPVS" |
26 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
27 | ||
e924283b | 28 | #include <linux/interrupt.h> |
14c85021 | 29 | #include <linux/in.h> |
f190055f | 30 | #include <linux/net.h> |
1da177e4 | 31 | #include <linux/kernel.h> |
14c85021 | 32 | #include <linux/module.h> |
1da177e4 LT |
33 | #include <linux/vmalloc.h> |
34 | #include <linux/proc_fs.h> /* for proc_net_* */ | |
5a0e3ad6 | 35 | #include <linux/slab.h> |
1da177e4 LT |
36 | #include <linux/seq_file.h> |
37 | #include <linux/jhash.h> | |
38 | #include <linux/random.h> | |
39 | ||
457c4cbc | 40 | #include <net/net_namespace.h> |
1da177e4 LT |
41 | #include <net/ip_vs.h> |
42 | ||
43 | ||
6f7edb48 CB |
44 | #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 | */ | |
51 | int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS; | |
52 | module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444); | |
53 | MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size"); | |
54 | ||
55 | /* size and mask values */ | |
56 | int ip_vs_conn_tab_size; | |
57 | int ip_vs_conn_tab_mask; | |
58 | ||
1da177e4 LT |
59 | /* |
60 | * Connection hash table: for input and output packets lookups of IPVS | |
61 | */ | |
62 | static struct list_head *ip_vs_conn_tab; | |
63 | ||
64 | /* SLAB cache for IPVS connections */ | |
e18b890b | 65 | static struct kmem_cache *ip_vs_conn_cachep __read_mostly; |
1da177e4 LT |
66 | |
67 | /* counter for current IPVS connections */ | |
68 | static atomic_t ip_vs_conn_count = ATOMIC_INIT(0); | |
69 | ||
70 | /* counter for no client port connections */ | |
71 | static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0); | |
72 | ||
73 | /* random value for IPVS connection hash */ | |
74 | static unsigned int ip_vs_conn_rnd; | |
75 | ||
76 | /* | |
77 | * Fine locking granularity for big connection hash table | |
78 | */ | |
79 | #define CT_LOCKARRAY_BITS 4 | |
80 | #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS) | |
81 | #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1) | |
82 | ||
83 | struct ip_vs_aligned_lock | |
84 | { | |
85 | rwlock_t l; | |
86 | } __attribute__((__aligned__(SMP_CACHE_BYTES))); | |
87 | ||
88 | /* lock array for conn table */ | |
89 | static struct ip_vs_aligned_lock | |
90 | __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned; | |
91 | ||
92 | static inline void ct_read_lock(unsigned key) | |
93 | { | |
94 | read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
95 | } | |
96 | ||
97 | static inline void ct_read_unlock(unsigned key) | |
98 | { | |
99 | read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
100 | } | |
101 | ||
102 | static inline void ct_write_lock(unsigned key) | |
103 | { | |
104 | write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
105 | } | |
106 | ||
107 | static inline void ct_write_unlock(unsigned key) | |
108 | { | |
109 | write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
110 | } | |
111 | ||
112 | static inline void ct_read_lock_bh(unsigned key) | |
113 | { | |
114 | read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
115 | } | |
116 | ||
117 | static inline void ct_read_unlock_bh(unsigned key) | |
118 | { | |
119 | read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
120 | } | |
121 | ||
122 | static inline void ct_write_lock_bh(unsigned key) | |
123 | { | |
124 | write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
125 | } | |
126 | ||
127 | static inline void ct_write_unlock_bh(unsigned key) | |
128 | { | |
129 | write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); | |
130 | } | |
131 | ||
132 | ||
133 | /* | |
134 | * Returns hash value for IPVS connection entry | |
135 | */ | |
28364a59 JV |
136 | static unsigned int ip_vs_conn_hashkey(int af, unsigned proto, |
137 | const union nf_inet_addr *addr, | |
138 | __be16 port) | |
1da177e4 | 139 | { |
28364a59 JV |
140 | #ifdef CONFIG_IP_VS_IPV6 |
141 | if (af == AF_INET6) | |
142 | return jhash_3words(jhash(addr, 16, ip_vs_conn_rnd), | |
143 | (__force u32)port, proto, ip_vs_conn_rnd) | |
6f7edb48 | 144 | & ip_vs_conn_tab_mask; |
28364a59 JV |
145 | #endif |
146 | return jhash_3words((__force u32)addr->ip, (__force u32)port, proto, | |
147 | ip_vs_conn_rnd) | |
6f7edb48 | 148 | & ip_vs_conn_tab_mask; |
1da177e4 LT |
149 | } |
150 | ||
151 | ||
152 | /* | |
153 | * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port. | |
154 | * returns bool success. | |
155 | */ | |
156 | static inline int ip_vs_conn_hash(struct ip_vs_conn *cp) | |
157 | { | |
158 | unsigned hash; | |
159 | int ret; | |
160 | ||
26ec037f NC |
161 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
162 | return 0; | |
163 | ||
1da177e4 | 164 | /* Hash by protocol, client address and port */ |
28364a59 | 165 | hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport); |
1da177e4 LT |
166 | |
167 | ct_write_lock(hash); | |
aea9d711 | 168 | spin_lock(&cp->lock); |
1da177e4 LT |
169 | |
170 | if (!(cp->flags & IP_VS_CONN_F_HASHED)) { | |
171 | list_add(&cp->c_list, &ip_vs_conn_tab[hash]); | |
172 | cp->flags |= IP_VS_CONN_F_HASHED; | |
173 | atomic_inc(&cp->refcnt); | |
174 | ret = 1; | |
175 | } else { | |
1e3e238e HE |
176 | pr_err("%s(): request for already hashed, called from %pF\n", |
177 | __func__, __builtin_return_address(0)); | |
1da177e4 LT |
178 | ret = 0; |
179 | } | |
180 | ||
aea9d711 | 181 | spin_unlock(&cp->lock); |
1da177e4 LT |
182 | ct_write_unlock(hash); |
183 | ||
184 | return ret; | |
185 | } | |
186 | ||
187 | ||
188 | /* | |
189 | * UNhashes ip_vs_conn from ip_vs_conn_tab. | |
190 | * returns bool success. | |
191 | */ | |
192 | static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp) | |
193 | { | |
194 | unsigned hash; | |
195 | int ret; | |
196 | ||
197 | /* unhash it and decrease its reference counter */ | |
28364a59 | 198 | hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport); |
1da177e4 LT |
199 | |
200 | ct_write_lock(hash); | |
aea9d711 | 201 | spin_lock(&cp->lock); |
1da177e4 LT |
202 | |
203 | if (cp->flags & IP_VS_CONN_F_HASHED) { | |
204 | list_del(&cp->c_list); | |
205 | cp->flags &= ~IP_VS_CONN_F_HASHED; | |
206 | atomic_dec(&cp->refcnt); | |
207 | ret = 1; | |
208 | } else | |
209 | ret = 0; | |
210 | ||
aea9d711 | 211 | spin_unlock(&cp->lock); |
1da177e4 LT |
212 | ct_write_unlock(hash); |
213 | ||
214 | return ret; | |
215 | } | |
216 | ||
217 | ||
218 | /* | |
219 | * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. | |
220 | * Called for pkts coming from OUTside-to-INside. | |
f11017ec SH |
221 | * p->caddr, p->cport: pkt source address (foreign host) |
222 | * p->vaddr, p->vport: pkt dest address (load balancer) | |
1da177e4 | 223 | */ |
f11017ec SH |
224 | static inline struct ip_vs_conn * |
225 | __ip_vs_conn_in_get(const struct ip_vs_conn_param *p) | |
1da177e4 LT |
226 | { |
227 | unsigned hash; | |
228 | struct ip_vs_conn *cp; | |
229 | ||
f11017ec | 230 | hash = ip_vs_conn_hashkey(p->af, p->protocol, p->caddr, p->cport); |
1da177e4 LT |
231 | |
232 | ct_read_lock(hash); | |
233 | ||
234 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { | |
f11017ec SH |
235 | if (cp->af == p->af && |
236 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && | |
237 | ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && | |
238 | p->cport == cp->cport && p->vport == cp->vport && | |
239 | ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) && | |
240 | p->protocol == cp->protocol) { | |
1da177e4 LT |
241 | /* HIT */ |
242 | atomic_inc(&cp->refcnt); | |
243 | ct_read_unlock(hash); | |
244 | return cp; | |
245 | } | |
246 | } | |
247 | ||
248 | ct_read_unlock(hash); | |
249 | ||
250 | return NULL; | |
251 | } | |
252 | ||
f11017ec | 253 | struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p) |
1da177e4 LT |
254 | { |
255 | struct ip_vs_conn *cp; | |
256 | ||
f11017ec SH |
257 | cp = __ip_vs_conn_in_get(p); |
258 | if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) { | |
259 | struct ip_vs_conn_param cport_zero_p = *p; | |
260 | cport_zero_p.cport = 0; | |
261 | cp = __ip_vs_conn_in_get(&cport_zero_p); | |
262 | } | |
1da177e4 | 263 | |
28364a59 | 264 | IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n", |
f11017ec SH |
265 | ip_vs_proto_name(p->protocol), |
266 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
267 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 268 | cp ? "hit" : "not hit"); |
1da177e4 LT |
269 | |
270 | return cp; | |
271 | } | |
272 | ||
f11017ec SH |
273 | static int |
274 | ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb, | |
275 | const struct ip_vs_iphdr *iph, | |
276 | unsigned int proto_off, int inverse, | |
277 | struct ip_vs_conn_param *p) | |
278 | { | |
279 | __be16 _ports[2], *pptr; | |
280 | ||
281 | pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports); | |
282 | if (pptr == NULL) | |
283 | return 1; | |
284 | ||
285 | if (likely(!inverse)) | |
286 | ip_vs_conn_fill_param(af, iph->protocol, &iph->saddr, pptr[0], | |
287 | &iph->daddr, pptr[1], p); | |
288 | else | |
289 | ip_vs_conn_fill_param(af, iph->protocol, &iph->daddr, pptr[1], | |
290 | &iph->saddr, pptr[0], p); | |
291 | return 0; | |
292 | } | |
293 | ||
5c0d2374 SH |
294 | struct ip_vs_conn * |
295 | ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb, | |
296 | struct ip_vs_protocol *pp, | |
297 | const struct ip_vs_iphdr *iph, | |
298 | unsigned int proto_off, int inverse) | |
299 | { | |
f11017ec | 300 | struct ip_vs_conn_param p; |
5c0d2374 | 301 | |
f11017ec | 302 | if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p)) |
5c0d2374 SH |
303 | return NULL; |
304 | ||
f11017ec | 305 | return ip_vs_conn_in_get(&p); |
5c0d2374 SH |
306 | } |
307 | EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto); | |
308 | ||
87375ab4 | 309 | /* Get reference to connection template */ |
f11017ec | 310 | struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p) |
87375ab4 JA |
311 | { |
312 | unsigned hash; | |
313 | struct ip_vs_conn *cp; | |
314 | ||
f11017ec | 315 | hash = ip_vs_conn_hashkey(p->af, p->protocol, p->caddr, p->cport); |
87375ab4 JA |
316 | |
317 | ct_read_lock(hash); | |
318 | ||
319 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { | |
f11017ec SH |
320 | if (cp->af == p->af && |
321 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && | |
be8be9ec | 322 | /* protocol should only be IPPROTO_IP if |
f11017ec SH |
323 | * p->vaddr is a fwmark */ |
324 | ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : | |
325 | p->af, p->vaddr, &cp->vaddr) && | |
326 | p->cport == cp->cport && p->vport == cp->vport && | |
87375ab4 | 327 | cp->flags & IP_VS_CONN_F_TEMPLATE && |
f11017ec | 328 | p->protocol == cp->protocol) { |
87375ab4 JA |
329 | /* HIT */ |
330 | atomic_inc(&cp->refcnt); | |
331 | goto out; | |
332 | } | |
333 | } | |
334 | cp = NULL; | |
335 | ||
336 | out: | |
337 | ct_read_unlock(hash); | |
338 | ||
28364a59 | 339 | IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n", |
f11017ec SH |
340 | ip_vs_proto_name(p->protocol), |
341 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
342 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 343 | cp ? "hit" : "not hit"); |
87375ab4 JA |
344 | |
345 | return cp; | |
346 | } | |
1da177e4 | 347 | |
f11017ec SH |
348 | /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
349 | * Called for pkts coming from inside-to-OUTside. | |
350 | * p->caddr, p->cport: pkt source address (inside host) | |
351 | * p->vaddr, p->vport: pkt dest address (foreign host) */ | |
352 | struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p) | |
1da177e4 LT |
353 | { |
354 | unsigned hash; | |
355 | struct ip_vs_conn *cp, *ret=NULL; | |
356 | ||
357 | /* | |
358 | * Check for "full" addressed entries | |
359 | */ | |
f11017ec | 360 | hash = ip_vs_conn_hashkey(p->af, p->protocol, p->vaddr, p->vport); |
1da177e4 LT |
361 | |
362 | ct_read_lock(hash); | |
363 | ||
364 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { | |
f11017ec SH |
365 | if (cp->af == p->af && |
366 | ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && | |
367 | ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && | |
368 | p->vport == cp->cport && p->cport == cp->dport && | |
369 | p->protocol == cp->protocol) { | |
1da177e4 LT |
370 | /* HIT */ |
371 | atomic_inc(&cp->refcnt); | |
372 | ret = cp; | |
373 | break; | |
374 | } | |
375 | } | |
376 | ||
377 | ct_read_unlock(hash); | |
378 | ||
28364a59 | 379 | IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n", |
f11017ec SH |
380 | ip_vs_proto_name(p->protocol), |
381 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
382 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 383 | ret ? "hit" : "not hit"); |
1da177e4 LT |
384 | |
385 | return ret; | |
386 | } | |
387 | ||
5c0d2374 SH |
388 | struct ip_vs_conn * |
389 | ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb, | |
390 | struct ip_vs_protocol *pp, | |
391 | const struct ip_vs_iphdr *iph, | |
392 | unsigned int proto_off, int inverse) | |
393 | { | |
f11017ec | 394 | struct ip_vs_conn_param p; |
5c0d2374 | 395 | |
f11017ec | 396 | if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p)) |
5c0d2374 SH |
397 | return NULL; |
398 | ||
f11017ec | 399 | return ip_vs_conn_out_get(&p); |
5c0d2374 SH |
400 | } |
401 | EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto); | |
1da177e4 LT |
402 | |
403 | /* | |
404 | * Put back the conn and restart its timer with its timeout | |
405 | */ | |
406 | void ip_vs_conn_put(struct ip_vs_conn *cp) | |
407 | { | |
26ec037f NC |
408 | unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ? |
409 | 0 : cp->timeout; | |
410 | mod_timer(&cp->timer, jiffies+t); | |
1da177e4 LT |
411 | |
412 | __ip_vs_conn_put(cp); | |
413 | } | |
414 | ||
415 | ||
416 | /* | |
417 | * Fill a no_client_port connection with a client port number | |
418 | */ | |
014d730d | 419 | void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport) |
1da177e4 LT |
420 | { |
421 | if (ip_vs_conn_unhash(cp)) { | |
422 | spin_lock(&cp->lock); | |
423 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) { | |
424 | atomic_dec(&ip_vs_conn_no_cport_cnt); | |
425 | cp->flags &= ~IP_VS_CONN_F_NO_CPORT; | |
426 | cp->cport = cport; | |
427 | } | |
428 | spin_unlock(&cp->lock); | |
429 | ||
430 | /* hash on new dport */ | |
431 | ip_vs_conn_hash(cp); | |
432 | } | |
433 | } | |
434 | ||
435 | ||
436 | /* | |
437 | * Bind a connection entry with the corresponding packet_xmit. | |
438 | * Called by ip_vs_conn_new. | |
439 | */ | |
440 | static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp) | |
441 | { | |
442 | switch (IP_VS_FWD_METHOD(cp)) { | |
443 | case IP_VS_CONN_F_MASQ: | |
444 | cp->packet_xmit = ip_vs_nat_xmit; | |
445 | break; | |
446 | ||
447 | case IP_VS_CONN_F_TUNNEL: | |
448 | cp->packet_xmit = ip_vs_tunnel_xmit; | |
449 | break; | |
450 | ||
451 | case IP_VS_CONN_F_DROUTE: | |
452 | cp->packet_xmit = ip_vs_dr_xmit; | |
453 | break; | |
454 | ||
455 | case IP_VS_CONN_F_LOCALNODE: | |
456 | cp->packet_xmit = ip_vs_null_xmit; | |
457 | break; | |
458 | ||
459 | case IP_VS_CONN_F_BYPASS: | |
460 | cp->packet_xmit = ip_vs_bypass_xmit; | |
461 | break; | |
462 | } | |
463 | } | |
464 | ||
b3cdd2a7 JV |
465 | #ifdef CONFIG_IP_VS_IPV6 |
466 | static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp) | |
467 | { | |
468 | switch (IP_VS_FWD_METHOD(cp)) { | |
469 | case IP_VS_CONN_F_MASQ: | |
470 | cp->packet_xmit = ip_vs_nat_xmit_v6; | |
471 | break; | |
472 | ||
473 | case IP_VS_CONN_F_TUNNEL: | |
474 | cp->packet_xmit = ip_vs_tunnel_xmit_v6; | |
475 | break; | |
476 | ||
477 | case IP_VS_CONN_F_DROUTE: | |
478 | cp->packet_xmit = ip_vs_dr_xmit_v6; | |
479 | break; | |
480 | ||
481 | case IP_VS_CONN_F_LOCALNODE: | |
482 | cp->packet_xmit = ip_vs_null_xmit; | |
483 | break; | |
484 | ||
485 | case IP_VS_CONN_F_BYPASS: | |
486 | cp->packet_xmit = ip_vs_bypass_xmit_v6; | |
487 | break; | |
488 | } | |
489 | } | |
490 | #endif | |
491 | ||
1da177e4 LT |
492 | |
493 | static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest) | |
494 | { | |
495 | return atomic_read(&dest->activeconns) | |
496 | + atomic_read(&dest->inactconns); | |
497 | } | |
498 | ||
499 | /* | |
500 | * Bind a connection entry with a virtual service destination | |
501 | * Called just after a new connection entry is created. | |
502 | */ | |
503 | static inline void | |
504 | ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) | |
505 | { | |
3575792e JA |
506 | unsigned int conn_flags; |
507 | ||
1da177e4 LT |
508 | /* if dest is NULL, then return directly */ |
509 | if (!dest) | |
510 | return; | |
511 | ||
512 | /* Increase the refcnt counter of the dest */ | |
513 | atomic_inc(&dest->refcnt); | |
514 | ||
3575792e JA |
515 | conn_flags = atomic_read(&dest->conn_flags); |
516 | if (cp->protocol != IPPROTO_UDP) | |
517 | conn_flags &= ~IP_VS_CONN_F_ONE_PACKET; | |
1da177e4 | 518 | /* Bind with the destination and its corresponding transmitter */ |
3575792e | 519 | if (cp->flags & IP_VS_CONN_F_SYNC) { |
b209639e RB |
520 | /* if the connection is not template and is created |
521 | * by sync, preserve the activity flag. | |
522 | */ | |
3575792e JA |
523 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) |
524 | conn_flags &= ~IP_VS_CONN_F_INACTIVE; | |
525 | } | |
526 | cp->flags |= conn_flags; | |
1da177e4 LT |
527 | cp->dest = dest; |
528 | ||
cfc78c5a JV |
529 | IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d " |
530 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " | |
531 | "dest->refcnt:%d\n", | |
532 | ip_vs_proto_name(cp->protocol), | |
533 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), | |
534 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
535 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), | |
536 | ip_vs_fwd_tag(cp), cp->state, | |
537 | cp->flags, atomic_read(&cp->refcnt), | |
538 | atomic_read(&dest->refcnt)); | |
1da177e4 LT |
539 | |
540 | /* Update the connection counters */ | |
87375ab4 | 541 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
1da177e4 LT |
542 | /* It is a normal connection, so increase the inactive |
543 | connection counter because it is in TCP SYNRECV | |
544 | state (inactive) or other protocol inacive state */ | |
b209639e RB |
545 | if ((cp->flags & IP_VS_CONN_F_SYNC) && |
546 | (!(cp->flags & IP_VS_CONN_F_INACTIVE))) | |
547 | atomic_inc(&dest->activeconns); | |
548 | else | |
549 | atomic_inc(&dest->inactconns); | |
1da177e4 LT |
550 | } else { |
551 | /* It is a persistent connection/template, so increase | |
552 | the peristent connection counter */ | |
553 | atomic_inc(&dest->persistconns); | |
554 | } | |
555 | ||
556 | if (dest->u_threshold != 0 && | |
557 | ip_vs_dest_totalconns(dest) >= dest->u_threshold) | |
558 | dest->flags |= IP_VS_DEST_F_OVERLOAD; | |
559 | } | |
560 | ||
561 | ||
1e356f9c RB |
562 | /* |
563 | * Check if there is a destination for the connection, if so | |
564 | * bind the connection to the destination. | |
565 | */ | |
566 | struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp) | |
567 | { | |
568 | struct ip_vs_dest *dest; | |
569 | ||
570 | if ((cp) && (!cp->dest)) { | |
7937df15 JV |
571 | dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport, |
572 | &cp->vaddr, cp->vport, | |
573 | cp->protocol); | |
1e356f9c RB |
574 | ip_vs_bind_dest(cp, dest); |
575 | return dest; | |
576 | } else | |
577 | return NULL; | |
578 | } | |
1e356f9c RB |
579 | |
580 | ||
1da177e4 LT |
581 | /* |
582 | * Unbind a connection entry with its VS destination | |
583 | * Called by the ip_vs_conn_expire function. | |
584 | */ | |
585 | static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp) | |
586 | { | |
587 | struct ip_vs_dest *dest = cp->dest; | |
588 | ||
589 | if (!dest) | |
590 | return; | |
591 | ||
cfc78c5a JV |
592 | IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d " |
593 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " | |
594 | "dest->refcnt:%d\n", | |
595 | ip_vs_proto_name(cp->protocol), | |
596 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), | |
597 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
598 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), | |
599 | ip_vs_fwd_tag(cp), cp->state, | |
600 | cp->flags, atomic_read(&cp->refcnt), | |
601 | atomic_read(&dest->refcnt)); | |
1da177e4 LT |
602 | |
603 | /* Update the connection counters */ | |
87375ab4 | 604 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
1da177e4 LT |
605 | /* It is a normal connection, so decrease the inactconns |
606 | or activeconns counter */ | |
607 | if (cp->flags & IP_VS_CONN_F_INACTIVE) { | |
608 | atomic_dec(&dest->inactconns); | |
609 | } else { | |
610 | atomic_dec(&dest->activeconns); | |
611 | } | |
612 | } else { | |
613 | /* It is a persistent connection/template, so decrease | |
614 | the peristent connection counter */ | |
615 | atomic_dec(&dest->persistconns); | |
616 | } | |
617 | ||
618 | if (dest->l_threshold != 0) { | |
619 | if (ip_vs_dest_totalconns(dest) < dest->l_threshold) | |
620 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
621 | } else if (dest->u_threshold != 0) { | |
622 | if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3) | |
623 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
624 | } else { | |
625 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) | |
626 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
627 | } | |
628 | ||
629 | /* | |
630 | * Simply decrease the refcnt of the dest, because the | |
631 | * dest will be either in service's destination list | |
632 | * or in the trash. | |
633 | */ | |
634 | atomic_dec(&dest->refcnt); | |
635 | } | |
636 | ||
637 | ||
638 | /* | |
639 | * Checking if the destination of a connection template is available. | |
640 | * If available, return 1, otherwise invalidate this connection | |
641 | * template and return 0. | |
642 | */ | |
643 | int ip_vs_check_template(struct ip_vs_conn *ct) | |
644 | { | |
645 | struct ip_vs_dest *dest = ct->dest; | |
646 | ||
647 | /* | |
648 | * Checking the dest server status. | |
649 | */ | |
650 | if ((dest == NULL) || | |
e905a9ed YH |
651 | !(dest->flags & IP_VS_DEST_F_AVAILABLE) || |
652 | (sysctl_ip_vs_expire_quiescent_template && | |
1da177e4 | 653 | (atomic_read(&dest->weight) == 0))) { |
cfc78c5a JV |
654 | IP_VS_DBG_BUF(9, "check_template: dest not available for " |
655 | "protocol %s s:%s:%d v:%s:%d " | |
656 | "-> d:%s:%d\n", | |
657 | ip_vs_proto_name(ct->protocol), | |
658 | IP_VS_DBG_ADDR(ct->af, &ct->caddr), | |
659 | ntohs(ct->cport), | |
660 | IP_VS_DBG_ADDR(ct->af, &ct->vaddr), | |
661 | ntohs(ct->vport), | |
662 | IP_VS_DBG_ADDR(ct->af, &ct->daddr), | |
663 | ntohs(ct->dport)); | |
1da177e4 LT |
664 | |
665 | /* | |
666 | * Invalidate the connection template | |
667 | */ | |
014d730d | 668 | if (ct->vport != htons(0xffff)) { |
1da177e4 | 669 | if (ip_vs_conn_unhash(ct)) { |
014d730d AV |
670 | ct->dport = htons(0xffff); |
671 | ct->vport = htons(0xffff); | |
1da177e4 LT |
672 | ct->cport = 0; |
673 | ip_vs_conn_hash(ct); | |
674 | } | |
675 | } | |
676 | ||
677 | /* | |
678 | * Simply decrease the refcnt of the template, | |
679 | * don't restart its timer. | |
680 | */ | |
681 | atomic_dec(&ct->refcnt); | |
682 | return 0; | |
683 | } | |
684 | return 1; | |
685 | } | |
686 | ||
687 | static void ip_vs_conn_expire(unsigned long data) | |
688 | { | |
689 | struct ip_vs_conn *cp = (struct ip_vs_conn *)data; | |
690 | ||
691 | cp->timeout = 60*HZ; | |
692 | ||
693 | /* | |
694 | * hey, I'm using it | |
695 | */ | |
696 | atomic_inc(&cp->refcnt); | |
697 | ||
698 | /* | |
699 | * do I control anybody? | |
700 | */ | |
701 | if (atomic_read(&cp->n_control)) | |
702 | goto expire_later; | |
703 | ||
704 | /* | |
705 | * unhash it if it is hashed in the conn table | |
706 | */ | |
26ec037f | 707 | if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) |
1da177e4 LT |
708 | goto expire_later; |
709 | ||
710 | /* | |
711 | * refcnt==1 implies I'm the only one referrer | |
712 | */ | |
713 | if (likely(atomic_read(&cp->refcnt) == 1)) { | |
714 | /* delete the timer if it is activated by other users */ | |
715 | if (timer_pending(&cp->timer)) | |
716 | del_timer(&cp->timer); | |
717 | ||
718 | /* does anybody control me? */ | |
719 | if (cp->control) | |
720 | ip_vs_control_del(cp); | |
721 | ||
f4bc17cd JA |
722 | if (cp->flags & IP_VS_CONN_F_NFCT) |
723 | ip_vs_conn_drop_conntrack(cp); | |
724 | ||
1da177e4 LT |
725 | if (unlikely(cp->app != NULL)) |
726 | ip_vs_unbind_app(cp); | |
727 | ip_vs_unbind_dest(cp); | |
728 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) | |
729 | atomic_dec(&ip_vs_conn_no_cport_cnt); | |
730 | atomic_dec(&ip_vs_conn_count); | |
731 | ||
732 | kmem_cache_free(ip_vs_conn_cachep, cp); | |
733 | return; | |
734 | } | |
735 | ||
736 | /* hash it back to the table */ | |
737 | ip_vs_conn_hash(cp); | |
738 | ||
739 | expire_later: | |
4b5bdf5c | 740 | IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n", |
1da177e4 LT |
741 | atomic_read(&cp->refcnt)-1, |
742 | atomic_read(&cp->n_control)); | |
743 | ||
744 | ip_vs_conn_put(cp); | |
745 | } | |
746 | ||
747 | ||
748 | void ip_vs_conn_expire_now(struct ip_vs_conn *cp) | |
749 | { | |
750 | if (del_timer(&cp->timer)) | |
751 | mod_timer(&cp->timer, jiffies); | |
1da177e4 LT |
752 | } |
753 | ||
754 | ||
755 | /* | |
756 | * Create a new connection entry and hash it into the ip_vs_conn_tab | |
757 | */ | |
758 | struct ip_vs_conn * | |
f11017ec | 759 | ip_vs_conn_new(const struct ip_vs_conn_param *p, |
28364a59 | 760 | const union nf_inet_addr *daddr, __be16 dport, unsigned flags, |
1da177e4 LT |
761 | struct ip_vs_dest *dest) |
762 | { | |
763 | struct ip_vs_conn *cp; | |
f11017ec | 764 | struct ip_vs_protocol *pp = ip_vs_proto_get(p->protocol); |
1da177e4 | 765 | |
c3762229 | 766 | cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC); |
1da177e4 | 767 | if (cp == NULL) { |
1e3e238e | 768 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
1da177e4 LT |
769 | return NULL; |
770 | } | |
771 | ||
1da177e4 | 772 | INIT_LIST_HEAD(&cp->c_list); |
b24b8a24 | 773 | setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp); |
f11017ec SH |
774 | cp->af = p->af; |
775 | cp->protocol = p->protocol; | |
776 | ip_vs_addr_copy(p->af, &cp->caddr, p->caddr); | |
777 | cp->cport = p->cport; | |
778 | ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr); | |
779 | cp->vport = p->vport; | |
be8be9ec | 780 | /* proto should only be IPPROTO_IP if d_addr is a fwmark */ |
f11017ec | 781 | ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af, |
be8be9ec | 782 | &cp->daddr, daddr); |
1da177e4 LT |
783 | cp->dport = dport; |
784 | cp->flags = flags; | |
785 | spin_lock_init(&cp->lock); | |
786 | ||
787 | /* | |
788 | * Set the entry is referenced by the current thread before hashing | |
789 | * it in the table, so that other thread run ip_vs_random_dropentry | |
790 | * but cannot drop this entry. | |
791 | */ | |
792 | atomic_set(&cp->refcnt, 1); | |
793 | ||
794 | atomic_set(&cp->n_control, 0); | |
795 | atomic_set(&cp->in_pkts, 0); | |
796 | ||
797 | atomic_inc(&ip_vs_conn_count); | |
798 | if (flags & IP_VS_CONN_F_NO_CPORT) | |
799 | atomic_inc(&ip_vs_conn_no_cport_cnt); | |
800 | ||
801 | /* Bind the connection with a destination server */ | |
802 | ip_vs_bind_dest(cp, dest); | |
803 | ||
804 | /* Set its state and timeout */ | |
805 | cp->state = 0; | |
806 | cp->timeout = 3*HZ; | |
807 | ||
808 | /* Bind its packet transmitter */ | |
b3cdd2a7 | 809 | #ifdef CONFIG_IP_VS_IPV6 |
f11017ec | 810 | if (p->af == AF_INET6) |
b3cdd2a7 JV |
811 | ip_vs_bind_xmit_v6(cp); |
812 | else | |
813 | #endif | |
814 | ip_vs_bind_xmit(cp); | |
1da177e4 LT |
815 | |
816 | if (unlikely(pp && atomic_read(&pp->appcnt))) | |
817 | ip_vs_bind_app(cp, pp); | |
818 | ||
f4bc17cd JA |
819 | /* |
820 | * Allow conntrack to be preserved. By default, conntrack | |
821 | * is created and destroyed for every packet. | |
822 | * Sometimes keeping conntrack can be useful for | |
823 | * IP_VS_CONN_F_ONE_PACKET too. | |
824 | */ | |
825 | ||
826 | if (ip_vs_conntrack_enabled()) | |
827 | cp->flags |= IP_VS_CONN_F_NFCT; | |
828 | ||
1da177e4 LT |
829 | /* Hash it in the ip_vs_conn_tab finally */ |
830 | ip_vs_conn_hash(cp); | |
831 | ||
832 | return cp; | |
833 | } | |
834 | ||
835 | ||
836 | /* | |
837 | * /proc/net/ip_vs_conn entries | |
838 | */ | |
839 | #ifdef CONFIG_PROC_FS | |
840 | ||
841 | static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos) | |
842 | { | |
843 | int idx; | |
844 | struct ip_vs_conn *cp; | |
e905a9ed | 845 | |
6f7edb48 | 846 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
1da177e4 LT |
847 | ct_read_lock_bh(idx); |
848 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { | |
849 | if (pos-- == 0) { | |
850 | seq->private = &ip_vs_conn_tab[idx]; | |
851 | return cp; | |
852 | } | |
853 | } | |
854 | ct_read_unlock_bh(idx); | |
855 | } | |
856 | ||
857 | return NULL; | |
858 | } | |
859 | ||
860 | static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos) | |
861 | { | |
862 | seq->private = NULL; | |
863 | return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN; | |
864 | } | |
865 | ||
866 | static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
867 | { | |
868 | struct ip_vs_conn *cp = v; | |
869 | struct list_head *e, *l = seq->private; | |
870 | int idx; | |
871 | ||
872 | ++*pos; | |
e905a9ed | 873 | if (v == SEQ_START_TOKEN) |
1da177e4 LT |
874 | return ip_vs_conn_array(seq, 0); |
875 | ||
876 | /* more on same hash chain? */ | |
877 | if ((e = cp->c_list.next) != l) | |
878 | return list_entry(e, struct ip_vs_conn, c_list); | |
879 | ||
880 | idx = l - ip_vs_conn_tab; | |
881 | ct_read_unlock_bh(idx); | |
882 | ||
6f7edb48 | 883 | while (++idx < ip_vs_conn_tab_size) { |
1da177e4 LT |
884 | ct_read_lock_bh(idx); |
885 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { | |
886 | seq->private = &ip_vs_conn_tab[idx]; | |
887 | return cp; | |
e905a9ed | 888 | } |
1da177e4 LT |
889 | ct_read_unlock_bh(idx); |
890 | } | |
891 | seq->private = NULL; | |
892 | return NULL; | |
893 | } | |
894 | ||
895 | static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v) | |
896 | { | |
897 | struct list_head *l = seq->private; | |
898 | ||
899 | if (l) | |
900 | ct_read_unlock_bh(l - ip_vs_conn_tab); | |
901 | } | |
902 | ||
903 | static int ip_vs_conn_seq_show(struct seq_file *seq, void *v) | |
904 | { | |
905 | ||
906 | if (v == SEQ_START_TOKEN) | |
907 | seq_puts(seq, | |
908 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n"); | |
909 | else { | |
910 | const struct ip_vs_conn *cp = v; | |
911 | ||
667a5f18 VB |
912 | #ifdef CONFIG_IP_VS_IPV6 |
913 | if (cp->af == AF_INET6) | |
5b095d98 | 914 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n", |
667a5f18 | 915 | ip_vs_proto_name(cp->protocol), |
38ff4fa4 HH |
916 | &cp->caddr.in6, ntohs(cp->cport), |
917 | &cp->vaddr.in6, ntohs(cp->vport), | |
918 | &cp->daddr.in6, ntohs(cp->dport), | |
667a5f18 VB |
919 | ip_vs_state_name(cp->protocol, cp->state), |
920 | (cp->timer.expires-jiffies)/HZ); | |
921 | else | |
922 | #endif | |
923 | seq_printf(seq, | |
924 | "%-3s %08X %04X %08X %04X" | |
925 | " %08X %04X %-11s %7lu\n", | |
1da177e4 | 926 | ip_vs_proto_name(cp->protocol), |
e7ade46a JV |
927 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
928 | ntohl(cp->vaddr.ip), ntohs(cp->vport), | |
929 | ntohl(cp->daddr.ip), ntohs(cp->dport), | |
1da177e4 LT |
930 | ip_vs_state_name(cp->protocol, cp->state), |
931 | (cp->timer.expires-jiffies)/HZ); | |
932 | } | |
933 | return 0; | |
934 | } | |
935 | ||
56b3d975 | 936 | static const struct seq_operations ip_vs_conn_seq_ops = { |
1da177e4 LT |
937 | .start = ip_vs_conn_seq_start, |
938 | .next = ip_vs_conn_seq_next, | |
939 | .stop = ip_vs_conn_seq_stop, | |
940 | .show = ip_vs_conn_seq_show, | |
941 | }; | |
942 | ||
943 | static int ip_vs_conn_open(struct inode *inode, struct file *file) | |
944 | { | |
945 | return seq_open(file, &ip_vs_conn_seq_ops); | |
946 | } | |
947 | ||
9a32144e | 948 | static const struct file_operations ip_vs_conn_fops = { |
1da177e4 LT |
949 | .owner = THIS_MODULE, |
950 | .open = ip_vs_conn_open, | |
951 | .read = seq_read, | |
952 | .llseek = seq_lseek, | |
953 | .release = seq_release, | |
954 | }; | |
7a4fbb1f RB |
955 | |
956 | static const char *ip_vs_origin_name(unsigned flags) | |
957 | { | |
958 | if (flags & IP_VS_CONN_F_SYNC) | |
959 | return "SYNC"; | |
960 | else | |
961 | return "LOCAL"; | |
962 | } | |
963 | ||
964 | static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v) | |
965 | { | |
966 | ||
967 | if (v == SEQ_START_TOKEN) | |
968 | seq_puts(seq, | |
969 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n"); | |
970 | else { | |
971 | const struct ip_vs_conn *cp = v; | |
972 | ||
667a5f18 VB |
973 | #ifdef CONFIG_IP_VS_IPV6 |
974 | if (cp->af == AF_INET6) | |
5b095d98 | 975 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n", |
667a5f18 | 976 | ip_vs_proto_name(cp->protocol), |
38ff4fa4 HH |
977 | &cp->caddr.in6, ntohs(cp->cport), |
978 | &cp->vaddr.in6, ntohs(cp->vport), | |
979 | &cp->daddr.in6, ntohs(cp->dport), | |
667a5f18 VB |
980 | ip_vs_state_name(cp->protocol, cp->state), |
981 | ip_vs_origin_name(cp->flags), | |
982 | (cp->timer.expires-jiffies)/HZ); | |
983 | else | |
984 | #endif | |
985 | seq_printf(seq, | |
986 | "%-3s %08X %04X %08X %04X " | |
987 | "%08X %04X %-11s %-6s %7lu\n", | |
7a4fbb1f | 988 | ip_vs_proto_name(cp->protocol), |
e7ade46a JV |
989 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
990 | ntohl(cp->vaddr.ip), ntohs(cp->vport), | |
991 | ntohl(cp->daddr.ip), ntohs(cp->dport), | |
7a4fbb1f RB |
992 | ip_vs_state_name(cp->protocol, cp->state), |
993 | ip_vs_origin_name(cp->flags), | |
994 | (cp->timer.expires-jiffies)/HZ); | |
995 | } | |
996 | return 0; | |
997 | } | |
998 | ||
999 | static const struct seq_operations ip_vs_conn_sync_seq_ops = { | |
1000 | .start = ip_vs_conn_seq_start, | |
1001 | .next = ip_vs_conn_seq_next, | |
1002 | .stop = ip_vs_conn_seq_stop, | |
1003 | .show = ip_vs_conn_sync_seq_show, | |
1004 | }; | |
1005 | ||
1006 | static int ip_vs_conn_sync_open(struct inode *inode, struct file *file) | |
1007 | { | |
1008 | return seq_open(file, &ip_vs_conn_sync_seq_ops); | |
1009 | } | |
1010 | ||
1011 | static const struct file_operations ip_vs_conn_sync_fops = { | |
1012 | .owner = THIS_MODULE, | |
1013 | .open = ip_vs_conn_sync_open, | |
1014 | .read = seq_read, | |
1015 | .llseek = seq_lseek, | |
1016 | .release = seq_release, | |
1017 | }; | |
1018 | ||
1da177e4 LT |
1019 | #endif |
1020 | ||
1021 | ||
1022 | /* | |
1023 | * Randomly drop connection entries before running out of memory | |
1024 | */ | |
1025 | static inline int todrop_entry(struct ip_vs_conn *cp) | |
1026 | { | |
1027 | /* | |
1028 | * The drop rate array needs tuning for real environments. | |
1029 | * Called from timer bh only => no locking | |
1030 | */ | |
9b5b5cff | 1031 | static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; |
1da177e4 LT |
1032 | static char todrop_counter[9] = {0}; |
1033 | int i; | |
1034 | ||
1035 | /* if the conn entry hasn't lasted for 60 seconds, don't drop it. | |
1036 | This will leave enough time for normal connection to get | |
1037 | through. */ | |
1038 | if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ)) | |
1039 | return 0; | |
1040 | ||
1041 | /* Don't drop the entry if its number of incoming packets is not | |
1042 | located in [0, 8] */ | |
1043 | i = atomic_read(&cp->in_pkts); | |
1044 | if (i > 8 || i < 0) return 0; | |
1045 | ||
1046 | if (!todrop_rate[i]) return 0; | |
1047 | if (--todrop_counter[i] > 0) return 0; | |
1048 | ||
1049 | todrop_counter[i] = todrop_rate[i]; | |
1050 | return 1; | |
1051 | } | |
1052 | ||
af9debd4 | 1053 | /* Called from keventd and must protect itself from softirqs */ |
1da177e4 LT |
1054 | void ip_vs_random_dropentry(void) |
1055 | { | |
1056 | int idx; | |
1057 | struct ip_vs_conn *cp; | |
1da177e4 LT |
1058 | |
1059 | /* | |
1060 | * Randomly scan 1/32 of the whole table every second | |
1061 | */ | |
6f7edb48 CB |
1062 | for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) { |
1063 | unsigned hash = net_random() & ip_vs_conn_tab_mask; | |
1da177e4 LT |
1064 | |
1065 | /* | |
1066 | * Lock is actually needed in this loop. | |
1067 | */ | |
af9debd4 | 1068 | ct_write_lock_bh(hash); |
1da177e4 LT |
1069 | |
1070 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { | |
87375ab4 | 1071 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
1da177e4 LT |
1072 | /* connection template */ |
1073 | continue; | |
1074 | ||
1075 | if (cp->protocol == IPPROTO_TCP) { | |
1076 | switch(cp->state) { | |
1077 | case IP_VS_TCP_S_SYN_RECV: | |
1078 | case IP_VS_TCP_S_SYNACK: | |
1079 | break; | |
1080 | ||
1081 | case IP_VS_TCP_S_ESTABLISHED: | |
1082 | if (todrop_entry(cp)) | |
1083 | break; | |
1084 | continue; | |
1085 | ||
1086 | default: | |
1087 | continue; | |
1088 | } | |
1089 | } else { | |
1090 | if (!todrop_entry(cp)) | |
1091 | continue; | |
1092 | } | |
1093 | ||
1da177e4 LT |
1094 | IP_VS_DBG(4, "del connection\n"); |
1095 | ip_vs_conn_expire_now(cp); | |
fb3d8949 | 1096 | if (cp->control) { |
1da177e4 | 1097 | IP_VS_DBG(4, "del conn template\n"); |
fb3d8949 | 1098 | ip_vs_conn_expire_now(cp->control); |
1da177e4 | 1099 | } |
1da177e4 | 1100 | } |
af9debd4 | 1101 | ct_write_unlock_bh(hash); |
1da177e4 LT |
1102 | } |
1103 | } | |
1104 | ||
1105 | ||
1106 | /* | |
1107 | * Flush all the connection entries in the ip_vs_conn_tab | |
1108 | */ | |
1109 | static void ip_vs_conn_flush(void) | |
1110 | { | |
1111 | int idx; | |
1112 | struct ip_vs_conn *cp; | |
1da177e4 LT |
1113 | |
1114 | flush_again: | |
6f7edb48 | 1115 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
1da177e4 LT |
1116 | /* |
1117 | * Lock is actually needed in this loop. | |
1118 | */ | |
1119 | ct_write_lock_bh(idx); | |
1120 | ||
1121 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { | |
1da177e4 | 1122 | |
1da177e4 LT |
1123 | IP_VS_DBG(4, "del connection\n"); |
1124 | ip_vs_conn_expire_now(cp); | |
fb3d8949 | 1125 | if (cp->control) { |
1da177e4 | 1126 | IP_VS_DBG(4, "del conn template\n"); |
fb3d8949 | 1127 | ip_vs_conn_expire_now(cp->control); |
1da177e4 | 1128 | } |
1da177e4 LT |
1129 | } |
1130 | ct_write_unlock_bh(idx); | |
1131 | } | |
1132 | ||
1133 | /* the counter may be not NULL, because maybe some conn entries | |
1134 | are run by slow timer handler or unhashed but still referred */ | |
1135 | if (atomic_read(&ip_vs_conn_count) != 0) { | |
1136 | schedule(); | |
1137 | goto flush_again; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | ||
048cf48b | 1142 | int __init ip_vs_conn_init(void) |
1da177e4 LT |
1143 | { |
1144 | int idx; | |
1145 | ||
6f7edb48 CB |
1146 | /* Compute size and mask */ |
1147 | ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits; | |
1148 | ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1; | |
1149 | ||
1da177e4 LT |
1150 | /* |
1151 | * Allocate the connection hash table and initialize its list heads | |
1152 | */ | |
6f7edb48 CB |
1153 | ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * |
1154 | sizeof(struct list_head)); | |
1da177e4 LT |
1155 | if (!ip_vs_conn_tab) |
1156 | return -ENOMEM; | |
1157 | ||
1158 | /* Allocate ip_vs_conn slab cache */ | |
1159 | ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn", | |
1160 | sizeof(struct ip_vs_conn), 0, | |
20c2df83 | 1161 | SLAB_HWCACHE_ALIGN, NULL); |
1da177e4 LT |
1162 | if (!ip_vs_conn_cachep) { |
1163 | vfree(ip_vs_conn_tab); | |
1164 | return -ENOMEM; | |
1165 | } | |
1166 | ||
1e3e238e HE |
1167 | pr_info("Connection hash table configured " |
1168 | "(size=%d, memory=%ldKbytes)\n", | |
6f7edb48 CB |
1169 | ip_vs_conn_tab_size, |
1170 | (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024); | |
1da177e4 LT |
1171 | IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n", |
1172 | sizeof(struct ip_vs_conn)); | |
1173 | ||
6f7edb48 | 1174 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
1da177e4 LT |
1175 | INIT_LIST_HEAD(&ip_vs_conn_tab[idx]); |
1176 | } | |
1177 | ||
1178 | for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) { | |
1179 | rwlock_init(&__ip_vs_conntbl_lock_array[idx].l); | |
1180 | } | |
1181 | ||
457c4cbc | 1182 | proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops); |
7a4fbb1f | 1183 | proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops); |
1da177e4 LT |
1184 | |
1185 | /* calculate the random value for connection hash */ | |
1186 | get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd)); | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | ||
1192 | void ip_vs_conn_cleanup(void) | |
1193 | { | |
1194 | /* flush all the connection entries first */ | |
1195 | ip_vs_conn_flush(); | |
1196 | ||
1197 | /* Release the empty cache */ | |
1198 | kmem_cache_destroy(ip_vs_conn_cachep); | |
457c4cbc | 1199 | proc_net_remove(&init_net, "ip_vs_conn"); |
7a4fbb1f | 1200 | proc_net_remove(&init_net, "ip_vs_conn_sync"); |
1da177e4 LT |
1201 | vfree(ip_vs_conn_tab); |
1202 | } |