Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
| 8 | * Version: $Id: ip_vs_conn.c,v 1.31 2003/04/18 09:03:16 wensong Exp $ |
| 9 | * |
| 10 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 11 | * Peter Kese <peter.kese@ijs.si> |
| 12 | * Julian Anastasov <ja@ssi.bg> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese, |
| 20 | * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms |
| 21 | * and others. Many code here is taken from IP MASQ code of kernel 2.2. |
| 22 | * |
| 23 | * Changes: |
| 24 | * |
| 25 | */ |
| 26 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 27 | #include <linux/in.h> |
Arnaldo Carvalho de Melo | f190055 | 2006-01-04 02:02:20 -0200 | [diff] [blame^] | 28 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 30 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
| 32 | #include <linux/proc_fs.h> /* for proc_net_* */ |
| 33 | #include <linux/seq_file.h> |
| 34 | #include <linux/jhash.h> |
| 35 | #include <linux/random.h> |
| 36 | |
| 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 */ |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 46 | static kmem_cache_t *ip_vs_conn_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 117 | static unsigned int ip_vs_conn_hashkey(unsigned proto, __u32 addr, __u16 port) |
| 118 | { |
| 119 | return jhash_3words(addr, port, proto, ip_vs_conn_rnd) |
| 120 | & IP_VS_CONN_TAB_MASK; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* |
| 125 | * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port. |
| 126 | * returns bool success. |
| 127 | */ |
| 128 | static inline int ip_vs_conn_hash(struct ip_vs_conn *cp) |
| 129 | { |
| 130 | unsigned hash; |
| 131 | int ret; |
| 132 | |
| 133 | /* Hash by protocol, client address and port */ |
| 134 | hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport); |
| 135 | |
| 136 | ct_write_lock(hash); |
| 137 | |
| 138 | if (!(cp->flags & IP_VS_CONN_F_HASHED)) { |
| 139 | list_add(&cp->c_list, &ip_vs_conn_tab[hash]); |
| 140 | cp->flags |= IP_VS_CONN_F_HASHED; |
| 141 | atomic_inc(&cp->refcnt); |
| 142 | ret = 1; |
| 143 | } else { |
| 144 | IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, " |
| 145 | "called from %p\n", __builtin_return_address(0)); |
| 146 | ret = 0; |
| 147 | } |
| 148 | |
| 149 | ct_write_unlock(hash); |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * UNhashes ip_vs_conn from ip_vs_conn_tab. |
| 157 | * returns bool success. |
| 158 | */ |
| 159 | static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp) |
| 160 | { |
| 161 | unsigned hash; |
| 162 | int ret; |
| 163 | |
| 164 | /* unhash it and decrease its reference counter */ |
| 165 | hash = ip_vs_conn_hashkey(cp->protocol, cp->caddr, cp->cport); |
| 166 | |
| 167 | ct_write_lock(hash); |
| 168 | |
| 169 | if (cp->flags & IP_VS_CONN_F_HASHED) { |
| 170 | list_del(&cp->c_list); |
| 171 | cp->flags &= ~IP_VS_CONN_F_HASHED; |
| 172 | atomic_dec(&cp->refcnt); |
| 173 | ret = 1; |
| 174 | } else |
| 175 | ret = 0; |
| 176 | |
| 177 | ct_write_unlock(hash); |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
| 185 | * Called for pkts coming from OUTside-to-INside. |
| 186 | * s_addr, s_port: pkt source address (foreign host) |
| 187 | * d_addr, d_port: pkt dest address (load balancer) |
| 188 | */ |
| 189 | static inline struct ip_vs_conn *__ip_vs_conn_in_get |
| 190 | (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port) |
| 191 | { |
| 192 | unsigned hash; |
| 193 | struct ip_vs_conn *cp; |
| 194 | |
| 195 | hash = ip_vs_conn_hashkey(protocol, s_addr, s_port); |
| 196 | |
| 197 | ct_read_lock(hash); |
| 198 | |
| 199 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { |
| 200 | if (s_addr==cp->caddr && s_port==cp->cport && |
| 201 | d_port==cp->vport && d_addr==cp->vaddr && |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 202 | ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | protocol==cp->protocol) { |
| 204 | /* HIT */ |
| 205 | atomic_inc(&cp->refcnt); |
| 206 | ct_read_unlock(hash); |
| 207 | return cp; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | ct_read_unlock(hash); |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | struct ip_vs_conn *ip_vs_conn_in_get |
| 217 | (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port) |
| 218 | { |
| 219 | struct ip_vs_conn *cp; |
| 220 | |
| 221 | cp = __ip_vs_conn_in_get(protocol, s_addr, s_port, d_addr, d_port); |
| 222 | if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) |
| 223 | cp = __ip_vs_conn_in_get(protocol, s_addr, 0, d_addr, d_port); |
| 224 | |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 225 | IP_VS_DBG(9, "lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | ip_vs_proto_name(protocol), |
| 227 | NIPQUAD(s_addr), ntohs(s_port), |
| 228 | NIPQUAD(d_addr), ntohs(d_port), |
| 229 | cp?"hit":"not hit"); |
| 230 | |
| 231 | return cp; |
| 232 | } |
| 233 | |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 234 | /* Get reference to connection template */ |
| 235 | struct ip_vs_conn *ip_vs_ct_in_get |
| 236 | (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port) |
| 237 | { |
| 238 | unsigned hash; |
| 239 | struct ip_vs_conn *cp; |
| 240 | |
| 241 | hash = ip_vs_conn_hashkey(protocol, s_addr, s_port); |
| 242 | |
| 243 | ct_read_lock(hash); |
| 244 | |
| 245 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { |
| 246 | if (s_addr==cp->caddr && s_port==cp->cport && |
| 247 | d_port==cp->vport && d_addr==cp->vaddr && |
| 248 | cp->flags & IP_VS_CONN_F_TEMPLATE && |
| 249 | protocol==cp->protocol) { |
| 250 | /* HIT */ |
| 251 | atomic_inc(&cp->refcnt); |
| 252 | goto out; |
| 253 | } |
| 254 | } |
| 255 | cp = NULL; |
| 256 | |
| 257 | out: |
| 258 | ct_read_unlock(hash); |
| 259 | |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 260 | IP_VS_DBG(9, "template lookup/in %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n", |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 261 | ip_vs_proto_name(protocol), |
| 262 | NIPQUAD(s_addr), ntohs(s_port), |
| 263 | NIPQUAD(d_addr), ntohs(d_port), |
| 264 | cp?"hit":"not hit"); |
| 265 | |
| 266 | return cp; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
| 271 | * Called for pkts coming from inside-to-OUTside. |
| 272 | * s_addr, s_port: pkt source address (inside host) |
| 273 | * d_addr, d_port: pkt dest address (foreign host) |
| 274 | */ |
| 275 | struct ip_vs_conn *ip_vs_conn_out_get |
| 276 | (int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port) |
| 277 | { |
| 278 | unsigned hash; |
| 279 | struct ip_vs_conn *cp, *ret=NULL; |
| 280 | |
| 281 | /* |
| 282 | * Check for "full" addressed entries |
| 283 | */ |
| 284 | hash = ip_vs_conn_hashkey(protocol, d_addr, d_port); |
| 285 | |
| 286 | ct_read_lock(hash); |
| 287 | |
| 288 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { |
| 289 | if (d_addr == cp->caddr && d_port == cp->cport && |
| 290 | s_port == cp->dport && s_addr == cp->daddr && |
| 291 | protocol == cp->protocol) { |
| 292 | /* HIT */ |
| 293 | atomic_inc(&cp->refcnt); |
| 294 | ret = cp; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | ct_read_unlock(hash); |
| 300 | |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 301 | IP_VS_DBG(9, "lookup/out %s %u.%u.%u.%u:%d->%u.%u.%u.%u:%d %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | ip_vs_proto_name(protocol), |
| 303 | NIPQUAD(s_addr), ntohs(s_port), |
| 304 | NIPQUAD(d_addr), ntohs(d_port), |
| 305 | ret?"hit":"not hit"); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /* |
| 312 | * Put back the conn and restart its timer with its timeout |
| 313 | */ |
| 314 | void ip_vs_conn_put(struct ip_vs_conn *cp) |
| 315 | { |
| 316 | /* reset it expire in its timeout */ |
| 317 | mod_timer(&cp->timer, jiffies+cp->timeout); |
| 318 | |
| 319 | __ip_vs_conn_put(cp); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /* |
| 324 | * Fill a no_client_port connection with a client port number |
| 325 | */ |
| 326 | void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __u16 cport) |
| 327 | { |
| 328 | if (ip_vs_conn_unhash(cp)) { |
| 329 | spin_lock(&cp->lock); |
| 330 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) { |
| 331 | atomic_dec(&ip_vs_conn_no_cport_cnt); |
| 332 | cp->flags &= ~IP_VS_CONN_F_NO_CPORT; |
| 333 | cp->cport = cport; |
| 334 | } |
| 335 | spin_unlock(&cp->lock); |
| 336 | |
| 337 | /* hash on new dport */ |
| 338 | ip_vs_conn_hash(cp); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* |
| 344 | * Bind a connection entry with the corresponding packet_xmit. |
| 345 | * Called by ip_vs_conn_new. |
| 346 | */ |
| 347 | static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp) |
| 348 | { |
| 349 | switch (IP_VS_FWD_METHOD(cp)) { |
| 350 | case IP_VS_CONN_F_MASQ: |
| 351 | cp->packet_xmit = ip_vs_nat_xmit; |
| 352 | break; |
| 353 | |
| 354 | case IP_VS_CONN_F_TUNNEL: |
| 355 | cp->packet_xmit = ip_vs_tunnel_xmit; |
| 356 | break; |
| 357 | |
| 358 | case IP_VS_CONN_F_DROUTE: |
| 359 | cp->packet_xmit = ip_vs_dr_xmit; |
| 360 | break; |
| 361 | |
| 362 | case IP_VS_CONN_F_LOCALNODE: |
| 363 | cp->packet_xmit = ip_vs_null_xmit; |
| 364 | break; |
| 365 | |
| 366 | case IP_VS_CONN_F_BYPASS: |
| 367 | cp->packet_xmit = ip_vs_bypass_xmit; |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | |
| 373 | static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest) |
| 374 | { |
| 375 | return atomic_read(&dest->activeconns) |
| 376 | + atomic_read(&dest->inactconns); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Bind a connection entry with a virtual service destination |
| 381 | * Called just after a new connection entry is created. |
| 382 | */ |
| 383 | static inline void |
| 384 | ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) |
| 385 | { |
| 386 | /* if dest is NULL, then return directly */ |
| 387 | if (!dest) |
| 388 | return; |
| 389 | |
| 390 | /* Increase the refcnt counter of the dest */ |
| 391 | atomic_inc(&dest->refcnt); |
| 392 | |
| 393 | /* Bind with the destination and its corresponding transmitter */ |
| 394 | cp->flags |= atomic_read(&dest->conn_flags); |
| 395 | cp->dest = dest; |
| 396 | |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 397 | IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d " |
| 398 | "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " |
| 399 | "dest->refcnt:%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | ip_vs_proto_name(cp->protocol), |
| 401 | NIPQUAD(cp->caddr), ntohs(cp->cport), |
| 402 | NIPQUAD(cp->vaddr), ntohs(cp->vport), |
| 403 | NIPQUAD(cp->daddr), ntohs(cp->dport), |
| 404 | ip_vs_fwd_tag(cp), cp->state, |
| 405 | cp->flags, atomic_read(&cp->refcnt), |
| 406 | atomic_read(&dest->refcnt)); |
| 407 | |
| 408 | /* Update the connection counters */ |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 409 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* It is a normal connection, so increase the inactive |
| 411 | connection counter because it is in TCP SYNRECV |
| 412 | state (inactive) or other protocol inacive state */ |
| 413 | atomic_inc(&dest->inactconns); |
| 414 | } else { |
| 415 | /* It is a persistent connection/template, so increase |
| 416 | the peristent connection counter */ |
| 417 | atomic_inc(&dest->persistconns); |
| 418 | } |
| 419 | |
| 420 | if (dest->u_threshold != 0 && |
| 421 | ip_vs_dest_totalconns(dest) >= dest->u_threshold) |
| 422 | dest->flags |= IP_VS_DEST_F_OVERLOAD; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
| 427 | * Unbind a connection entry with its VS destination |
| 428 | * Called by the ip_vs_conn_expire function. |
| 429 | */ |
| 430 | static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp) |
| 431 | { |
| 432 | struct ip_vs_dest *dest = cp->dest; |
| 433 | |
| 434 | if (!dest) |
| 435 | return; |
| 436 | |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 437 | IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d " |
| 438 | "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " |
| 439 | "dest->refcnt:%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | ip_vs_proto_name(cp->protocol), |
| 441 | NIPQUAD(cp->caddr), ntohs(cp->cport), |
| 442 | NIPQUAD(cp->vaddr), ntohs(cp->vport), |
| 443 | NIPQUAD(cp->daddr), ntohs(cp->dport), |
| 444 | ip_vs_fwd_tag(cp), cp->state, |
| 445 | cp->flags, atomic_read(&cp->refcnt), |
| 446 | atomic_read(&dest->refcnt)); |
| 447 | |
| 448 | /* Update the connection counters */ |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 449 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /* It is a normal connection, so decrease the inactconns |
| 451 | or activeconns counter */ |
| 452 | if (cp->flags & IP_VS_CONN_F_INACTIVE) { |
| 453 | atomic_dec(&dest->inactconns); |
| 454 | } else { |
| 455 | atomic_dec(&dest->activeconns); |
| 456 | } |
| 457 | } else { |
| 458 | /* It is a persistent connection/template, so decrease |
| 459 | the peristent connection counter */ |
| 460 | atomic_dec(&dest->persistconns); |
| 461 | } |
| 462 | |
| 463 | if (dest->l_threshold != 0) { |
| 464 | if (ip_vs_dest_totalconns(dest) < dest->l_threshold) |
| 465 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 466 | } else if (dest->u_threshold != 0) { |
| 467 | if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3) |
| 468 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 469 | } else { |
| 470 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) |
| 471 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Simply decrease the refcnt of the dest, because the |
| 476 | * dest will be either in service's destination list |
| 477 | * or in the trash. |
| 478 | */ |
| 479 | atomic_dec(&dest->refcnt); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | /* |
| 484 | * Checking if the destination of a connection template is available. |
| 485 | * If available, return 1, otherwise invalidate this connection |
| 486 | * template and return 0. |
| 487 | */ |
| 488 | int ip_vs_check_template(struct ip_vs_conn *ct) |
| 489 | { |
| 490 | struct ip_vs_dest *dest = ct->dest; |
| 491 | |
| 492 | /* |
| 493 | * Checking the dest server status. |
| 494 | */ |
| 495 | if ((dest == NULL) || |
| 496 | !(dest->flags & IP_VS_DEST_F_AVAILABLE) || |
| 497 | (sysctl_ip_vs_expire_quiescent_template && |
| 498 | (atomic_read(&dest->weight) == 0))) { |
| 499 | IP_VS_DBG(9, "check_template: dest not available for " |
| 500 | "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d " |
| 501 | "-> d:%u.%u.%u.%u:%d\n", |
| 502 | ip_vs_proto_name(ct->protocol), |
| 503 | NIPQUAD(ct->caddr), ntohs(ct->cport), |
| 504 | NIPQUAD(ct->vaddr), ntohs(ct->vport), |
| 505 | NIPQUAD(ct->daddr), ntohs(ct->dport)); |
| 506 | |
| 507 | /* |
| 508 | * Invalidate the connection template |
| 509 | */ |
Julian Anastasov | f5e229d | 2005-09-14 21:04:23 -0700 | [diff] [blame] | 510 | if (ct->vport != 65535) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | if (ip_vs_conn_unhash(ct)) { |
| 512 | ct->dport = 65535; |
| 513 | ct->vport = 65535; |
| 514 | ct->cport = 0; |
| 515 | ip_vs_conn_hash(ct); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Simply decrease the refcnt of the template, |
| 521 | * don't restart its timer. |
| 522 | */ |
| 523 | atomic_dec(&ct->refcnt); |
| 524 | return 0; |
| 525 | } |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | static void ip_vs_conn_expire(unsigned long data) |
| 530 | { |
| 531 | struct ip_vs_conn *cp = (struct ip_vs_conn *)data; |
| 532 | |
| 533 | cp->timeout = 60*HZ; |
| 534 | |
| 535 | /* |
| 536 | * hey, I'm using it |
| 537 | */ |
| 538 | atomic_inc(&cp->refcnt); |
| 539 | |
| 540 | /* |
| 541 | * do I control anybody? |
| 542 | */ |
| 543 | if (atomic_read(&cp->n_control)) |
| 544 | goto expire_later; |
| 545 | |
| 546 | /* |
| 547 | * unhash it if it is hashed in the conn table |
| 548 | */ |
| 549 | if (!ip_vs_conn_unhash(cp)) |
| 550 | goto expire_later; |
| 551 | |
| 552 | /* |
| 553 | * refcnt==1 implies I'm the only one referrer |
| 554 | */ |
| 555 | if (likely(atomic_read(&cp->refcnt) == 1)) { |
| 556 | /* delete the timer if it is activated by other users */ |
| 557 | if (timer_pending(&cp->timer)) |
| 558 | del_timer(&cp->timer); |
| 559 | |
| 560 | /* does anybody control me? */ |
| 561 | if (cp->control) |
| 562 | ip_vs_control_del(cp); |
| 563 | |
| 564 | if (unlikely(cp->app != NULL)) |
| 565 | ip_vs_unbind_app(cp); |
| 566 | ip_vs_unbind_dest(cp); |
| 567 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) |
| 568 | atomic_dec(&ip_vs_conn_no_cport_cnt); |
| 569 | atomic_dec(&ip_vs_conn_count); |
| 570 | |
| 571 | kmem_cache_free(ip_vs_conn_cachep, cp); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | /* hash it back to the table */ |
| 576 | ip_vs_conn_hash(cp); |
| 577 | |
| 578 | expire_later: |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 579 | IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | atomic_read(&cp->refcnt)-1, |
| 581 | atomic_read(&cp->n_control)); |
| 582 | |
| 583 | ip_vs_conn_put(cp); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | void ip_vs_conn_expire_now(struct ip_vs_conn *cp) |
| 588 | { |
| 589 | if (del_timer(&cp->timer)) |
| 590 | mod_timer(&cp->timer, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | /* |
| 595 | * Create a new connection entry and hash it into the ip_vs_conn_tab |
| 596 | */ |
| 597 | struct ip_vs_conn * |
| 598 | ip_vs_conn_new(int proto, __u32 caddr, __u16 cport, __u32 vaddr, __u16 vport, |
| 599 | __u32 daddr, __u16 dport, unsigned flags, |
| 600 | struct ip_vs_dest *dest) |
| 601 | { |
| 602 | struct ip_vs_conn *cp; |
| 603 | struct ip_vs_protocol *pp = ip_vs_proto_get(proto); |
| 604 | |
| 605 | cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC); |
| 606 | if (cp == NULL) { |
| 607 | IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n"); |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | memset(cp, 0, sizeof(*cp)); |
| 612 | INIT_LIST_HEAD(&cp->c_list); |
| 613 | init_timer(&cp->timer); |
| 614 | cp->timer.data = (unsigned long)cp; |
| 615 | cp->timer.function = ip_vs_conn_expire; |
| 616 | cp->protocol = proto; |
| 617 | cp->caddr = caddr; |
| 618 | cp->cport = cport; |
| 619 | cp->vaddr = vaddr; |
| 620 | cp->vport = vport; |
| 621 | cp->daddr = daddr; |
| 622 | cp->dport = dport; |
| 623 | cp->flags = flags; |
| 624 | spin_lock_init(&cp->lock); |
| 625 | |
| 626 | /* |
| 627 | * Set the entry is referenced by the current thread before hashing |
| 628 | * it in the table, so that other thread run ip_vs_random_dropentry |
| 629 | * but cannot drop this entry. |
| 630 | */ |
| 631 | atomic_set(&cp->refcnt, 1); |
| 632 | |
| 633 | atomic_set(&cp->n_control, 0); |
| 634 | atomic_set(&cp->in_pkts, 0); |
| 635 | |
| 636 | atomic_inc(&ip_vs_conn_count); |
| 637 | if (flags & IP_VS_CONN_F_NO_CPORT) |
| 638 | atomic_inc(&ip_vs_conn_no_cport_cnt); |
| 639 | |
| 640 | /* Bind the connection with a destination server */ |
| 641 | ip_vs_bind_dest(cp, dest); |
| 642 | |
| 643 | /* Set its state and timeout */ |
| 644 | cp->state = 0; |
| 645 | cp->timeout = 3*HZ; |
| 646 | |
| 647 | /* Bind its packet transmitter */ |
| 648 | ip_vs_bind_xmit(cp); |
| 649 | |
| 650 | if (unlikely(pp && atomic_read(&pp->appcnt))) |
| 651 | ip_vs_bind_app(cp, pp); |
| 652 | |
| 653 | /* Hash it in the ip_vs_conn_tab finally */ |
| 654 | ip_vs_conn_hash(cp); |
| 655 | |
| 656 | return cp; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | /* |
| 661 | * /proc/net/ip_vs_conn entries |
| 662 | */ |
| 663 | #ifdef CONFIG_PROC_FS |
| 664 | |
| 665 | static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos) |
| 666 | { |
| 667 | int idx; |
| 668 | struct ip_vs_conn *cp; |
| 669 | |
| 670 | for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) { |
| 671 | ct_read_lock_bh(idx); |
| 672 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { |
| 673 | if (pos-- == 0) { |
| 674 | seq->private = &ip_vs_conn_tab[idx]; |
| 675 | return cp; |
| 676 | } |
| 677 | } |
| 678 | ct_read_unlock_bh(idx); |
| 679 | } |
| 680 | |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos) |
| 685 | { |
| 686 | seq->private = NULL; |
| 687 | return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN; |
| 688 | } |
| 689 | |
| 690 | static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 691 | { |
| 692 | struct ip_vs_conn *cp = v; |
| 693 | struct list_head *e, *l = seq->private; |
| 694 | int idx; |
| 695 | |
| 696 | ++*pos; |
| 697 | if (v == SEQ_START_TOKEN) |
| 698 | return ip_vs_conn_array(seq, 0); |
| 699 | |
| 700 | /* more on same hash chain? */ |
| 701 | if ((e = cp->c_list.next) != l) |
| 702 | return list_entry(e, struct ip_vs_conn, c_list); |
| 703 | |
| 704 | idx = l - ip_vs_conn_tab; |
| 705 | ct_read_unlock_bh(idx); |
| 706 | |
| 707 | while (++idx < IP_VS_CONN_TAB_SIZE) { |
| 708 | ct_read_lock_bh(idx); |
| 709 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { |
| 710 | seq->private = &ip_vs_conn_tab[idx]; |
| 711 | return cp; |
| 712 | } |
| 713 | ct_read_unlock_bh(idx); |
| 714 | } |
| 715 | seq->private = NULL; |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v) |
| 720 | { |
| 721 | struct list_head *l = seq->private; |
| 722 | |
| 723 | if (l) |
| 724 | ct_read_unlock_bh(l - ip_vs_conn_tab); |
| 725 | } |
| 726 | |
| 727 | static int ip_vs_conn_seq_show(struct seq_file *seq, void *v) |
| 728 | { |
| 729 | |
| 730 | if (v == SEQ_START_TOKEN) |
| 731 | seq_puts(seq, |
| 732 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n"); |
| 733 | else { |
| 734 | const struct ip_vs_conn *cp = v; |
| 735 | |
| 736 | seq_printf(seq, |
| 737 | "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n", |
| 738 | ip_vs_proto_name(cp->protocol), |
| 739 | ntohl(cp->caddr), ntohs(cp->cport), |
| 740 | ntohl(cp->vaddr), ntohs(cp->vport), |
| 741 | ntohl(cp->daddr), ntohs(cp->dport), |
| 742 | ip_vs_state_name(cp->protocol, cp->state), |
| 743 | (cp->timer.expires-jiffies)/HZ); |
| 744 | } |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static struct seq_operations ip_vs_conn_seq_ops = { |
| 749 | .start = ip_vs_conn_seq_start, |
| 750 | .next = ip_vs_conn_seq_next, |
| 751 | .stop = ip_vs_conn_seq_stop, |
| 752 | .show = ip_vs_conn_seq_show, |
| 753 | }; |
| 754 | |
| 755 | static int ip_vs_conn_open(struct inode *inode, struct file *file) |
| 756 | { |
| 757 | return seq_open(file, &ip_vs_conn_seq_ops); |
| 758 | } |
| 759 | |
| 760 | static struct file_operations ip_vs_conn_fops = { |
| 761 | .owner = THIS_MODULE, |
| 762 | .open = ip_vs_conn_open, |
| 763 | .read = seq_read, |
| 764 | .llseek = seq_lseek, |
| 765 | .release = seq_release, |
| 766 | }; |
| 767 | #endif |
| 768 | |
| 769 | |
| 770 | /* |
| 771 | * Randomly drop connection entries before running out of memory |
| 772 | */ |
| 773 | static inline int todrop_entry(struct ip_vs_conn *cp) |
| 774 | { |
| 775 | /* |
| 776 | * The drop rate array needs tuning for real environments. |
| 777 | * Called from timer bh only => no locking |
| 778 | */ |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 779 | static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | static char todrop_counter[9] = {0}; |
| 781 | int i; |
| 782 | |
| 783 | /* if the conn entry hasn't lasted for 60 seconds, don't drop it. |
| 784 | This will leave enough time for normal connection to get |
| 785 | through. */ |
| 786 | if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ)) |
| 787 | return 0; |
| 788 | |
| 789 | /* Don't drop the entry if its number of incoming packets is not |
| 790 | located in [0, 8] */ |
| 791 | i = atomic_read(&cp->in_pkts); |
| 792 | if (i > 8 || i < 0) return 0; |
| 793 | |
| 794 | if (!todrop_rate[i]) return 0; |
| 795 | if (--todrop_counter[i] > 0) return 0; |
| 796 | |
| 797 | todrop_counter[i] = todrop_rate[i]; |
| 798 | return 1; |
| 799 | } |
| 800 | |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 801 | /* Called from keventd and must protect itself from softirqs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | void ip_vs_random_dropentry(void) |
| 803 | { |
| 804 | int idx; |
| 805 | struct ip_vs_conn *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * Randomly scan 1/32 of the whole table every second |
| 809 | */ |
| 810 | for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) { |
| 811 | unsigned hash = net_random() & IP_VS_CONN_TAB_MASK; |
| 812 | |
| 813 | /* |
| 814 | * Lock is actually needed in this loop. |
| 815 | */ |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 816 | ct_write_lock_bh(hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) { |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 819 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | /* connection template */ |
| 821 | continue; |
| 822 | |
| 823 | if (cp->protocol == IPPROTO_TCP) { |
| 824 | switch(cp->state) { |
| 825 | case IP_VS_TCP_S_SYN_RECV: |
| 826 | case IP_VS_TCP_S_SYNACK: |
| 827 | break; |
| 828 | |
| 829 | case IP_VS_TCP_S_ESTABLISHED: |
| 830 | if (todrop_entry(cp)) |
| 831 | break; |
| 832 | continue; |
| 833 | |
| 834 | default: |
| 835 | continue; |
| 836 | } |
| 837 | } else { |
| 838 | if (!todrop_entry(cp)) |
| 839 | continue; |
| 840 | } |
| 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | IP_VS_DBG(4, "del connection\n"); |
| 843 | ip_vs_conn_expire_now(cp); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 844 | if (cp->control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | IP_VS_DBG(4, "del conn template\n"); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 846 | ip_vs_conn_expire_now(cp->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 849 | ct_write_unlock_bh(hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /* |
| 855 | * Flush all the connection entries in the ip_vs_conn_tab |
| 856 | */ |
| 857 | static void ip_vs_conn_flush(void) |
| 858 | { |
| 859 | int idx; |
| 860 | struct ip_vs_conn *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | flush_again: |
| 863 | for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) { |
| 864 | /* |
| 865 | * Lock is actually needed in this loop. |
| 866 | */ |
| 867 | ct_write_lock_bh(idx); |
| 868 | |
| 869 | list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | IP_VS_DBG(4, "del connection\n"); |
| 872 | ip_vs_conn_expire_now(cp); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 873 | if (cp->control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | IP_VS_DBG(4, "del conn template\n"); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 875 | ip_vs_conn_expire_now(cp->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | ct_write_unlock_bh(idx); |
| 879 | } |
| 880 | |
| 881 | /* the counter may be not NULL, because maybe some conn entries |
| 882 | are run by slow timer handler or unhashed but still referred */ |
| 883 | if (atomic_read(&ip_vs_conn_count) != 0) { |
| 884 | schedule(); |
| 885 | goto flush_again; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | |
| 890 | int ip_vs_conn_init(void) |
| 891 | { |
| 892 | int idx; |
| 893 | |
| 894 | /* |
| 895 | * Allocate the connection hash table and initialize its list heads |
| 896 | */ |
| 897 | ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head)); |
| 898 | if (!ip_vs_conn_tab) |
| 899 | return -ENOMEM; |
| 900 | |
| 901 | /* Allocate ip_vs_conn slab cache */ |
| 902 | ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn", |
| 903 | sizeof(struct ip_vs_conn), 0, |
| 904 | SLAB_HWCACHE_ALIGN, NULL, NULL); |
| 905 | if (!ip_vs_conn_cachep) { |
| 906 | vfree(ip_vs_conn_tab); |
| 907 | return -ENOMEM; |
| 908 | } |
| 909 | |
| 910 | IP_VS_INFO("Connection hash table configured " |
| 911 | "(size=%d, memory=%ldKbytes)\n", |
| 912 | IP_VS_CONN_TAB_SIZE, |
| 913 | (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024); |
| 914 | IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n", |
| 915 | sizeof(struct ip_vs_conn)); |
| 916 | |
| 917 | for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) { |
| 918 | INIT_LIST_HEAD(&ip_vs_conn_tab[idx]); |
| 919 | } |
| 920 | |
| 921 | for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) { |
| 922 | rwlock_init(&__ip_vs_conntbl_lock_array[idx].l); |
| 923 | } |
| 924 | |
| 925 | proc_net_fops_create("ip_vs_conn", 0, &ip_vs_conn_fops); |
| 926 | |
| 927 | /* calculate the random value for connection hash */ |
| 928 | get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd)); |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | |
| 934 | void ip_vs_conn_cleanup(void) |
| 935 | { |
| 936 | /* flush all the connection entries first */ |
| 937 | ip_vs_conn_flush(); |
| 938 | |
| 939 | /* Release the empty cache */ |
| 940 | kmem_cache_destroy(ip_vs_conn_cachep); |
| 941 | proc_net_remove("ip_vs_conn"); |
| 942 | vfree(ip_vs_conn_tab); |
| 943 | } |