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