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