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