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