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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 25 | #define KMSG_COMPONENT "IPVS" |
| 26 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 27 | |
Andrew Morton | e924283 | 2006-01-05 14:57:36 -0800 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 29 | #include <linux/in.h> |
Arnaldo Carvalho de Melo | f190055 | 2006-01-04 02:02:20 -0200 | [diff] [blame] | 30 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/vmalloc.h> |
| 34 | #include <linux/proc_fs.h> /* for proc_net_* */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/seq_file.h> |
| 37 | #include <linux/jhash.h> |
| 38 | #include <linux/random.h> |
| 39 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 40 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/ip_vs.h> |
| 42 | |
| 43 | |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 44 | #ifndef CONFIG_IP_VS_TAB_BITS |
| 45 | #define CONFIG_IP_VS_TAB_BITS 12 |
| 46 | #endif |
| 47 | |
| 48 | /* |
| 49 | * Connection hash size. Default is what was selected at compile time. |
| 50 | */ |
Eric Dumazet | 4ecd294 | 2010-11-15 18:38:52 +0100 | [diff] [blame] | 51 | static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS; |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 52 | module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444); |
| 53 | MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size"); |
| 54 | |
| 55 | /* size and mask values */ |
Eric Dumazet | 4ecd294 | 2010-11-15 18:38:52 +0100 | [diff] [blame] | 56 | int ip_vs_conn_tab_size __read_mostly; |
| 57 | static int ip_vs_conn_tab_mask __read_mostly; |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Connection hash table: for input and output packets lookups of IPVS |
| 61 | */ |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 62 | static struct hlist_head *ip_vs_conn_tab __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* SLAB cache for IPVS connections */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 65 | static struct kmem_cache *ip_vs_conn_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* counter for no client port connections */ |
| 68 | static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0); |
| 69 | |
| 70 | /* random value for IPVS connection hash */ |
Eric Dumazet | 4ecd294 | 2010-11-15 18:38:52 +0100 | [diff] [blame] | 71 | static unsigned int ip_vs_conn_rnd __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Fine locking granularity for big connection hash table |
| 75 | */ |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 76 | #define CT_LOCKARRAY_BITS 5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS) |
| 78 | #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1) |
| 79 | |
| 80 | struct ip_vs_aligned_lock |
| 81 | { |
| 82 | rwlock_t l; |
| 83 | } __attribute__((__aligned__(SMP_CACHE_BYTES))); |
| 84 | |
| 85 | /* lock array for conn table */ |
| 86 | static struct ip_vs_aligned_lock |
| 87 | __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned; |
| 88 | |
| 89 | static inline void ct_read_lock(unsigned key) |
| 90 | { |
| 91 | read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 92 | } |
| 93 | |
| 94 | static inline void ct_read_unlock(unsigned key) |
| 95 | { |
| 96 | read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 97 | } |
| 98 | |
| 99 | static inline void ct_write_lock(unsigned key) |
| 100 | { |
| 101 | write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 102 | } |
| 103 | |
| 104 | static inline void ct_write_unlock(unsigned key) |
| 105 | { |
| 106 | write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 107 | } |
| 108 | |
| 109 | static inline void ct_read_lock_bh(unsigned key) |
| 110 | { |
| 111 | read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 112 | } |
| 113 | |
| 114 | static inline void ct_read_unlock_bh(unsigned key) |
| 115 | { |
| 116 | read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 117 | } |
| 118 | |
| 119 | static inline void ct_write_lock_bh(unsigned key) |
| 120 | { |
| 121 | write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 122 | } |
| 123 | |
| 124 | static inline void ct_write_unlock_bh(unsigned key) |
| 125 | { |
| 126 | write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | * Returns hash value for IPVS connection entry |
| 132 | */ |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 133 | static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned proto, |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 134 | const union nf_inet_addr *addr, |
| 135 | __be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_IP_VS_IPV6 |
| 138 | if (af == AF_INET6) |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 139 | return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd), |
| 140 | (__force u32)port, proto, ip_vs_conn_rnd) ^ |
| 141 | ((size_t)net>>8)) & ip_vs_conn_tab_mask; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 142 | #endif |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 143 | return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto, |
| 144 | ip_vs_conn_rnd) ^ |
| 145 | ((size_t)net>>8)) & ip_vs_conn_tab_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 148 | static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p, |
| 149 | bool inverse) |
| 150 | { |
| 151 | const union nf_inet_addr *addr; |
| 152 | __be16 port; |
| 153 | |
Simon Horman | f71499a | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 154 | if (p->pe_data && p->pe->hashkey_raw) |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 155 | return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) & |
| 156 | ip_vs_conn_tab_mask; |
| 157 | |
| 158 | if (likely(!inverse)) { |
| 159 | addr = p->caddr; |
| 160 | port = p->cport; |
| 161 | } else { |
| 162 | addr = p->vaddr; |
| 163 | port = p->vport; |
| 164 | } |
| 165 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 166 | return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp) |
| 170 | { |
| 171 | struct ip_vs_conn_param p; |
| 172 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 173 | ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol, |
| 174 | &cp->caddr, cp->cport, NULL, 0, &p); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 175 | |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 176 | if (cp->pe) { |
| 177 | p.pe = cp->pe; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 178 | p.pe_data = cp->pe_data; |
| 179 | p.pe_data_len = cp->pe_data_len; |
| 180 | } |
| 181 | |
| 182 | return ip_vs_conn_hashkey_param(&p, false); |
| 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 186 | * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | * returns bool success. |
| 188 | */ |
| 189 | static inline int ip_vs_conn_hash(struct ip_vs_conn *cp) |
| 190 | { |
| 191 | unsigned hash; |
| 192 | int ret; |
| 193 | |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 194 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
| 195 | return 0; |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* Hash by protocol, client address and port */ |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 198 | hash = ip_vs_conn_hashkey_conn(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | ct_write_lock(hash); |
Sven Wegener | aea9d71 | 2010-06-09 16:10:57 +0200 | [diff] [blame] | 201 | spin_lock(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | if (!(cp->flags & IP_VS_CONN_F_HASHED)) { |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 204 | hlist_add_head(&cp->c_list, &ip_vs_conn_tab[hash]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | cp->flags |= IP_VS_CONN_F_HASHED; |
| 206 | atomic_inc(&cp->refcnt); |
| 207 | ret = 1; |
| 208 | } else { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 209 | pr_err("%s(): request for already hashed, called from %pF\n", |
| 210 | __func__, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | ret = 0; |
| 212 | } |
| 213 | |
Sven Wegener | aea9d71 | 2010-06-09 16:10:57 +0200 | [diff] [blame] | 214 | spin_unlock(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | ct_write_unlock(hash); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /* |
| 222 | * UNhashes ip_vs_conn from ip_vs_conn_tab. |
| 223 | * returns bool success. |
| 224 | */ |
| 225 | static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp) |
| 226 | { |
| 227 | unsigned hash; |
| 228 | int ret; |
| 229 | |
| 230 | /* unhash it and decrease its reference counter */ |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 231 | hash = ip_vs_conn_hashkey_conn(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | ct_write_lock(hash); |
Sven Wegener | aea9d71 | 2010-06-09 16:10:57 +0200 | [diff] [blame] | 234 | spin_lock(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | if (cp->flags & IP_VS_CONN_F_HASHED) { |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 237 | hlist_del(&cp->c_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | cp->flags &= ~IP_VS_CONN_F_HASHED; |
| 239 | atomic_dec(&cp->refcnt); |
| 240 | ret = 1; |
| 241 | } else |
| 242 | ret = 0; |
| 243 | |
Sven Wegener | aea9d71 | 2010-06-09 16:10:57 +0200 | [diff] [blame] | 244 | spin_unlock(&cp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | ct_write_unlock(hash); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /* |
| 252 | * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
| 253 | * Called for pkts coming from OUTside-to-INside. |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 254 | * p->caddr, p->cport: pkt source address (foreign host) |
| 255 | * p->vaddr, p->vport: pkt dest address (load balancer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 257 | static inline struct ip_vs_conn * |
| 258 | __ip_vs_conn_in_get(const struct ip_vs_conn_param *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | unsigned hash; |
| 261 | struct ip_vs_conn *cp; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 262 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 264 | hash = ip_vs_conn_hashkey_param(p, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | ct_read_lock(hash); |
| 267 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 268 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) { |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 269 | if (cp->af == p->af && |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 270 | p->cport == cp->cport && p->vport == cp->vport && |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 271 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && |
| 272 | ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 273 | ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) && |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 274 | p->protocol == cp->protocol && |
| 275 | ip_vs_conn_net_eq(cp, p->net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* HIT */ |
| 277 | atomic_inc(&cp->refcnt); |
| 278 | ct_read_unlock(hash); |
| 279 | return cp; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | ct_read_unlock(hash); |
| 284 | |
| 285 | return NULL; |
| 286 | } |
| 287 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 288 | struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | struct ip_vs_conn *cp; |
| 291 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 292 | cp = __ip_vs_conn_in_get(p); |
| 293 | if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) { |
| 294 | struct ip_vs_conn_param cport_zero_p = *p; |
| 295 | cport_zero_p.cport = 0; |
| 296 | cp = __ip_vs_conn_in_get(&cport_zero_p); |
| 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 299 | IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n", |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 300 | ip_vs_proto_name(p->protocol), |
| 301 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), |
| 302 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 303 | cp ? "hit" : "not hit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | return cp; |
| 306 | } |
| 307 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 308 | static int |
| 309 | ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb, |
| 310 | const struct ip_vs_iphdr *iph, |
| 311 | unsigned int proto_off, int inverse, |
| 312 | struct ip_vs_conn_param *p) |
| 313 | { |
| 314 | __be16 _ports[2], *pptr; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 315 | struct net *net = skb_net(skb); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 316 | |
| 317 | pptr = skb_header_pointer(skb, proto_off, sizeof(_ports), _ports); |
| 318 | if (pptr == NULL) |
| 319 | return 1; |
| 320 | |
| 321 | if (likely(!inverse)) |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 322 | ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr, |
| 323 | pptr[0], &iph->daddr, pptr[1], p); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 324 | else |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 325 | ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr, |
| 326 | pptr[1], &iph->saddr, pptr[0], p); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 330 | struct ip_vs_conn * |
| 331 | ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb, |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 332 | const struct ip_vs_iphdr *iph, |
| 333 | unsigned int proto_off, int inverse) |
| 334 | { |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 335 | struct ip_vs_conn_param p; |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 336 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 337 | if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p)) |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 338 | return NULL; |
| 339 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 340 | return ip_vs_conn_in_get(&p); |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 341 | } |
| 342 | EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto); |
| 343 | |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 344 | /* Get reference to connection template */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 345 | struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p) |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 346 | { |
| 347 | unsigned hash; |
| 348 | struct ip_vs_conn *cp; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 349 | struct hlist_node *n; |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 350 | |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 351 | hash = ip_vs_conn_hashkey_param(p, false); |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 352 | |
| 353 | ct_read_lock(hash); |
| 354 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 355 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 356 | if (!ip_vs_conn_net_eq(cp, p->net)) |
| 357 | continue; |
Simon Horman | f71499a | 2010-08-22 21:37:54 +0900 | [diff] [blame] | 358 | if (p->pe_data && p->pe->ct_match) { |
Simon Horman | ea2c73a | 2010-11-08 20:06:30 +0900 | [diff] [blame] | 359 | if (p->pe == cp->pe && p->pe->ct_match(p, cp)) |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 360 | goto out; |
| 361 | continue; |
| 362 | } |
| 363 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 364 | if (cp->af == p->af && |
| 365 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && |
Simon Horman | be8be9e | 2009-05-06 15:02:29 +0000 | [diff] [blame] | 366 | /* protocol should only be IPPROTO_IP if |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 367 | * p->vaddr is a fwmark */ |
| 368 | ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : |
| 369 | p->af, p->vaddr, &cp->vaddr) && |
| 370 | p->cport == cp->cport && p->vport == cp->vport && |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 371 | cp->flags & IP_VS_CONN_F_TEMPLATE && |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 372 | p->protocol == cp->protocol) |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 373 | goto out; |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 374 | } |
| 375 | cp = NULL; |
| 376 | |
| 377 | out: |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 378 | if (cp) |
| 379 | atomic_inc(&cp->refcnt); |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 380 | ct_read_unlock(hash); |
| 381 | |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 382 | IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n", |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 383 | ip_vs_proto_name(p->protocol), |
| 384 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), |
| 385 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 386 | cp ? "hit" : "not hit"); |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 387 | |
| 388 | return cp; |
| 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 391 | /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
| 392 | * Called for pkts coming from inside-to-OUTside. |
| 393 | * p->caddr, p->cport: pkt source address (inside host) |
| 394 | * p->vaddr, p->vport: pkt dest address (foreign host) */ |
| 395 | struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | unsigned hash; |
| 398 | struct ip_vs_conn *cp, *ret=NULL; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 399 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Check for "full" addressed entries |
| 403 | */ |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 404 | hash = ip_vs_conn_hashkey_param(p, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | ct_read_lock(hash); |
| 407 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 408 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) { |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 409 | if (cp->af == p->af && |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 410 | p->vport == cp->cport && p->cport == cp->dport && |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 411 | ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && |
| 412 | ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 413 | p->protocol == cp->protocol && |
| 414 | ip_vs_conn_net_eq(cp, p->net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* HIT */ |
| 416 | atomic_inc(&cp->refcnt); |
| 417 | ret = cp; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | ct_read_unlock(hash); |
| 423 | |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 424 | IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n", |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 425 | ip_vs_proto_name(p->protocol), |
| 426 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), |
| 427 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 428 | ret ? "hit" : "not hit"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 433 | struct ip_vs_conn * |
| 434 | ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb, |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 435 | const struct ip_vs_iphdr *iph, |
| 436 | unsigned int proto_off, int inverse) |
| 437 | { |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 438 | struct ip_vs_conn_param p; |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 439 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 440 | if (ip_vs_conn_fill_param_proto(af, skb, iph, proto_off, inverse, &p)) |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 441 | return NULL; |
| 442 | |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 443 | return ip_vs_conn_out_get(&p); |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 444 | } |
| 445 | EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * Put back the conn and restart its timer with its timeout |
| 449 | */ |
| 450 | void ip_vs_conn_put(struct ip_vs_conn *cp) |
| 451 | { |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 452 | unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ? |
| 453 | 0 : cp->timeout; |
| 454 | mod_timer(&cp->timer, jiffies+t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | __ip_vs_conn_put(cp); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /* |
| 461 | * Fill a no_client_port connection with a client port number |
| 462 | */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 463 | void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
| 465 | if (ip_vs_conn_unhash(cp)) { |
| 466 | spin_lock(&cp->lock); |
| 467 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) { |
| 468 | atomic_dec(&ip_vs_conn_no_cport_cnt); |
| 469 | cp->flags &= ~IP_VS_CONN_F_NO_CPORT; |
| 470 | cp->cport = cport; |
| 471 | } |
| 472 | spin_unlock(&cp->lock); |
| 473 | |
| 474 | /* hash on new dport */ |
| 475 | ip_vs_conn_hash(cp); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* |
| 481 | * Bind a connection entry with the corresponding packet_xmit. |
| 482 | * Called by ip_vs_conn_new. |
| 483 | */ |
| 484 | static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp) |
| 485 | { |
| 486 | switch (IP_VS_FWD_METHOD(cp)) { |
| 487 | case IP_VS_CONN_F_MASQ: |
| 488 | cp->packet_xmit = ip_vs_nat_xmit; |
| 489 | break; |
| 490 | |
| 491 | case IP_VS_CONN_F_TUNNEL: |
| 492 | cp->packet_xmit = ip_vs_tunnel_xmit; |
| 493 | break; |
| 494 | |
| 495 | case IP_VS_CONN_F_DROUTE: |
| 496 | cp->packet_xmit = ip_vs_dr_xmit; |
| 497 | break; |
| 498 | |
| 499 | case IP_VS_CONN_F_LOCALNODE: |
| 500 | cp->packet_xmit = ip_vs_null_xmit; |
| 501 | break; |
| 502 | |
| 503 | case IP_VS_CONN_F_BYPASS: |
| 504 | cp->packet_xmit = ip_vs_bypass_xmit; |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 509 | #ifdef CONFIG_IP_VS_IPV6 |
| 510 | static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp) |
| 511 | { |
| 512 | switch (IP_VS_FWD_METHOD(cp)) { |
| 513 | case IP_VS_CONN_F_MASQ: |
| 514 | cp->packet_xmit = ip_vs_nat_xmit_v6; |
| 515 | break; |
| 516 | |
| 517 | case IP_VS_CONN_F_TUNNEL: |
| 518 | cp->packet_xmit = ip_vs_tunnel_xmit_v6; |
| 519 | break; |
| 520 | |
| 521 | case IP_VS_CONN_F_DROUTE: |
| 522 | cp->packet_xmit = ip_vs_dr_xmit_v6; |
| 523 | break; |
| 524 | |
| 525 | case IP_VS_CONN_F_LOCALNODE: |
| 526 | cp->packet_xmit = ip_vs_null_xmit; |
| 527 | break; |
| 528 | |
| 529 | case IP_VS_CONN_F_BYPASS: |
| 530 | cp->packet_xmit = ip_vs_bypass_xmit_v6; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | #endif |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest) |
| 538 | { |
| 539 | return atomic_read(&dest->activeconns) |
| 540 | + atomic_read(&dest->inactconns); |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Bind a connection entry with a virtual service destination |
| 545 | * Called just after a new connection entry is created. |
| 546 | */ |
| 547 | static inline void |
| 548 | ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) |
| 549 | { |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 550 | unsigned int conn_flags; |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* if dest is NULL, then return directly */ |
| 553 | if (!dest) |
| 554 | return; |
| 555 | |
| 556 | /* Increase the refcnt counter of the dest */ |
| 557 | atomic_inc(&dest->refcnt); |
| 558 | |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 559 | conn_flags = atomic_read(&dest->conn_flags); |
| 560 | if (cp->protocol != IPPROTO_UDP) |
| 561 | conn_flags &= ~IP_VS_CONN_F_ONE_PACKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* Bind with the destination and its corresponding transmitter */ |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 563 | if (cp->flags & IP_VS_CONN_F_SYNC) { |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 564 | /* if the connection is not template and is created |
| 565 | * by sync, preserve the activity flag. |
| 566 | */ |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 567 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) |
| 568 | conn_flags &= ~IP_VS_CONN_F_INACTIVE; |
Julian Anastasov | 3233759 | 2010-10-17 16:43:36 +0300 | [diff] [blame] | 569 | /* connections inherit forwarding method from dest */ |
| 570 | cp->flags &= ~IP_VS_CONN_F_FWD_MASK; |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 571 | } |
| 572 | cp->flags |= conn_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | cp->dest = dest; |
| 574 | |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 575 | IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d " |
| 576 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " |
| 577 | "dest->refcnt:%d\n", |
| 578 | ip_vs_proto_name(cp->protocol), |
| 579 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), |
| 580 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), |
| 581 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), |
| 582 | ip_vs_fwd_tag(cp), cp->state, |
| 583 | cp->flags, atomic_read(&cp->refcnt), |
| 584 | atomic_read(&dest->refcnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | /* Update the connection counters */ |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 587 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /* It is a normal connection, so increase the inactive |
| 589 | connection counter because it is in TCP SYNRECV |
| 590 | state (inactive) or other protocol inacive state */ |
Rumen G. Bogdanovski | b209639 | 2007-11-19 21:53:27 -0800 | [diff] [blame] | 591 | if ((cp->flags & IP_VS_CONN_F_SYNC) && |
| 592 | (!(cp->flags & IP_VS_CONN_F_INACTIVE))) |
| 593 | atomic_inc(&dest->activeconns); |
| 594 | else |
| 595 | atomic_inc(&dest->inactconns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } else { |
| 597 | /* It is a persistent connection/template, so increase |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 598 | the persistent connection counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | atomic_inc(&dest->persistconns); |
| 600 | } |
| 601 | |
| 602 | if (dest->u_threshold != 0 && |
| 603 | ip_vs_dest_totalconns(dest) >= dest->u_threshold) |
| 604 | dest->flags |= IP_VS_DEST_F_OVERLOAD; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | /* |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 609 | * Check if there is a destination for the connection, if so |
| 610 | * bind the connection to the destination. |
| 611 | */ |
| 612 | struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp) |
| 613 | { |
| 614 | struct ip_vs_dest *dest; |
| 615 | |
| 616 | if ((cp) && (!cp->dest)) { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 617 | dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr, |
| 618 | cp->dport, &cp->vaddr, cp->vport, |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 619 | cp->protocol, cp->fwmark); |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 620 | ip_vs_bind_dest(cp, dest); |
| 621 | return dest; |
| 622 | } else |
| 623 | return NULL; |
| 624 | } |
Rumen G. Bogdanovski | 1e356f9 | 2007-11-07 02:35:54 -0800 | [diff] [blame] | 625 | |
| 626 | |
| 627 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | * Unbind a connection entry with its VS destination |
| 629 | * Called by the ip_vs_conn_expire function. |
| 630 | */ |
| 631 | static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp) |
| 632 | { |
| 633 | struct ip_vs_dest *dest = cp->dest; |
| 634 | |
| 635 | if (!dest) |
| 636 | return; |
| 637 | |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 638 | IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d " |
| 639 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " |
| 640 | "dest->refcnt:%d\n", |
| 641 | ip_vs_proto_name(cp->protocol), |
| 642 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), |
| 643 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), |
| 644 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), |
| 645 | ip_vs_fwd_tag(cp), cp->state, |
| 646 | cp->flags, atomic_read(&cp->refcnt), |
| 647 | atomic_read(&dest->refcnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | /* Update the connection counters */ |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 650 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* It is a normal connection, so decrease the inactconns |
| 652 | or activeconns counter */ |
| 653 | if (cp->flags & IP_VS_CONN_F_INACTIVE) { |
| 654 | atomic_dec(&dest->inactconns); |
| 655 | } else { |
| 656 | atomic_dec(&dest->activeconns); |
| 657 | } |
| 658 | } else { |
| 659 | /* It is a persistent connection/template, so decrease |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 660 | the persistent connection counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | atomic_dec(&dest->persistconns); |
| 662 | } |
| 663 | |
| 664 | if (dest->l_threshold != 0) { |
| 665 | if (ip_vs_dest_totalconns(dest) < dest->l_threshold) |
| 666 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 667 | } else if (dest->u_threshold != 0) { |
| 668 | if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3) |
| 669 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 670 | } else { |
| 671 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) |
| 672 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Simply decrease the refcnt of the dest, because the |
| 677 | * dest will be either in service's destination list |
| 678 | * or in the trash. |
| 679 | */ |
| 680 | atomic_dec(&dest->refcnt); |
| 681 | } |
| 682 | |
Simon Horman | 8e1b0b1 | 2011-02-04 18:33:01 +0900 | [diff] [blame] | 683 | static int expire_quiescent_template(struct netns_ipvs *ipvs, |
| 684 | struct ip_vs_dest *dest) |
| 685 | { |
| 686 | #ifdef CONFIG_SYSCTL |
| 687 | return ipvs->sysctl_expire_quiescent_template && |
| 688 | (atomic_read(&dest->weight) == 0); |
| 689 | #else |
| 690 | return 0; |
| 691 | #endif |
| 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Checking if the destination of a connection template is available. |
| 696 | * If available, return 1, otherwise invalidate this connection |
| 697 | * template and return 0. |
| 698 | */ |
| 699 | int ip_vs_check_template(struct ip_vs_conn *ct) |
| 700 | { |
| 701 | struct ip_vs_dest *dest = ct->dest; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 702 | struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * Checking the dest server status. |
| 706 | */ |
| 707 | if ((dest == NULL) || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 708 | !(dest->flags & IP_VS_DEST_F_AVAILABLE) || |
Simon Horman | 8e1b0b1 | 2011-02-04 18:33:01 +0900 | [diff] [blame] | 709 | expire_quiescent_template(ipvs, dest)) { |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 710 | IP_VS_DBG_BUF(9, "check_template: dest not available for " |
| 711 | "protocol %s s:%s:%d v:%s:%d " |
| 712 | "-> d:%s:%d\n", |
| 713 | ip_vs_proto_name(ct->protocol), |
| 714 | IP_VS_DBG_ADDR(ct->af, &ct->caddr), |
| 715 | ntohs(ct->cport), |
| 716 | IP_VS_DBG_ADDR(ct->af, &ct->vaddr), |
| 717 | ntohs(ct->vport), |
| 718 | IP_VS_DBG_ADDR(ct->af, &ct->daddr), |
| 719 | ntohs(ct->dport)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * Invalidate the connection template |
| 723 | */ |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 724 | if (ct->vport != htons(0xffff)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (ip_vs_conn_unhash(ct)) { |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 726 | ct->dport = htons(0xffff); |
| 727 | ct->vport = htons(0xffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | ct->cport = 0; |
| 729 | ip_vs_conn_hash(ct); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Simply decrease the refcnt of the template, |
| 735 | * don't restart its timer. |
| 736 | */ |
| 737 | atomic_dec(&ct->refcnt); |
| 738 | return 0; |
| 739 | } |
| 740 | return 1; |
| 741 | } |
| 742 | |
| 743 | static void ip_vs_conn_expire(unsigned long data) |
| 744 | { |
| 745 | struct ip_vs_conn *cp = (struct ip_vs_conn *)data; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 746 | struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(cp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | cp->timeout = 60*HZ; |
| 749 | |
| 750 | /* |
| 751 | * hey, I'm using it |
| 752 | */ |
| 753 | atomic_inc(&cp->refcnt); |
| 754 | |
| 755 | /* |
| 756 | * do I control anybody? |
| 757 | */ |
| 758 | if (atomic_read(&cp->n_control)) |
| 759 | goto expire_later; |
| 760 | |
| 761 | /* |
| 762 | * unhash it if it is hashed in the conn table |
| 763 | */ |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 764 | if (!ip_vs_conn_unhash(cp) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | goto expire_later; |
| 766 | |
| 767 | /* |
| 768 | * refcnt==1 implies I'm the only one referrer |
| 769 | */ |
| 770 | if (likely(atomic_read(&cp->refcnt) == 1)) { |
| 771 | /* delete the timer if it is activated by other users */ |
| 772 | if (timer_pending(&cp->timer)) |
| 773 | del_timer(&cp->timer); |
| 774 | |
| 775 | /* does anybody control me? */ |
| 776 | if (cp->control) |
| 777 | ip_vs_control_del(cp); |
| 778 | |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 779 | if (cp->flags & IP_VS_CONN_F_NFCT) |
| 780 | ip_vs_conn_drop_conntrack(cp); |
| 781 | |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 782 | ip_vs_pe_put(cp->pe); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 783 | kfree(cp->pe_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (unlikely(cp->app != NULL)) |
| 785 | ip_vs_unbind_app(cp); |
| 786 | ip_vs_unbind_dest(cp); |
| 787 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) |
| 788 | atomic_dec(&ip_vs_conn_no_cport_cnt); |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 789 | atomic_dec(&ipvs->conn_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | kmem_cache_free(ip_vs_conn_cachep, cp); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | /* hash it back to the table */ |
| 796 | ip_vs_conn_hash(cp); |
| 797 | |
| 798 | expire_later: |
Roberto Nibali | 4b5bdf5 | 2006-01-03 14:22:59 -0800 | [diff] [blame] | 799 | 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] | 800 | atomic_read(&cp->refcnt)-1, |
| 801 | atomic_read(&cp->n_control)); |
| 802 | |
| 803 | ip_vs_conn_put(cp); |
| 804 | } |
| 805 | |
| 806 | |
| 807 | void ip_vs_conn_expire_now(struct ip_vs_conn *cp) |
| 808 | { |
| 809 | if (del_timer(&cp->timer)) |
| 810 | mod_timer(&cp->timer, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | |
| 814 | /* |
| 815 | * Create a new connection entry and hash it into the ip_vs_conn_tab |
| 816 | */ |
| 817 | struct ip_vs_conn * |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 818 | ip_vs_conn_new(const struct ip_vs_conn_param *p, |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 819 | const union nf_inet_addr *daddr, __be16 dport, unsigned flags, |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 820 | struct ip_vs_dest *dest, __u32 fwmark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
| 822 | struct ip_vs_conn *cp; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 823 | struct netns_ipvs *ipvs = net_ipvs(p->net); |
| 824 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net, |
| 825 | p->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 827 | cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | if (cp == NULL) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 829 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return NULL; |
| 831 | } |
| 832 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 833 | INIT_HLIST_NODE(&cp->c_list); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 834 | setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp); |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 835 | ip_vs_conn_net_set(cp, p->net); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 836 | cp->af = p->af; |
| 837 | cp->protocol = p->protocol; |
| 838 | ip_vs_addr_copy(p->af, &cp->caddr, p->caddr); |
| 839 | cp->cport = p->cport; |
| 840 | ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr); |
| 841 | cp->vport = p->vport; |
Simon Horman | be8be9e | 2009-05-06 15:02:29 +0000 | [diff] [blame] | 842 | /* proto should only be IPPROTO_IP if d_addr is a fwmark */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 843 | ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af, |
Simon Horman | be8be9e | 2009-05-06 15:02:29 +0000 | [diff] [blame] | 844 | &cp->daddr, daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | cp->dport = dport; |
| 846 | cp->flags = flags; |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 847 | cp->fwmark = fwmark; |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 848 | if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) { |
| 849 | ip_vs_pe_get(p->pe); |
| 850 | cp->pe = p->pe; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 851 | cp->pe_data = p->pe_data; |
| 852 | cp->pe_data_len = p->pe_data_len; |
| 853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | spin_lock_init(&cp->lock); |
| 855 | |
| 856 | /* |
| 857 | * Set the entry is referenced by the current thread before hashing |
| 858 | * it in the table, so that other thread run ip_vs_random_dropentry |
| 859 | * but cannot drop this entry. |
| 860 | */ |
| 861 | atomic_set(&cp->refcnt, 1); |
| 862 | |
| 863 | atomic_set(&cp->n_control, 0); |
| 864 | atomic_set(&cp->in_pkts, 0); |
| 865 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 866 | atomic_inc(&ipvs->conn_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | if (flags & IP_VS_CONN_F_NO_CPORT) |
| 868 | atomic_inc(&ip_vs_conn_no_cport_cnt); |
| 869 | |
| 870 | /* Bind the connection with a destination server */ |
| 871 | ip_vs_bind_dest(cp, dest); |
| 872 | |
| 873 | /* Set its state and timeout */ |
| 874 | cp->state = 0; |
| 875 | cp->timeout = 3*HZ; |
| 876 | |
| 877 | /* Bind its packet transmitter */ |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 878 | #ifdef CONFIG_IP_VS_IPV6 |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 879 | if (p->af == AF_INET6) |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 880 | ip_vs_bind_xmit_v6(cp); |
| 881 | else |
| 882 | #endif |
| 883 | ip_vs_bind_xmit(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 885 | if (unlikely(pd && atomic_read(&pd->appcnt))) |
| 886 | ip_vs_bind_app(cp, pd->pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 888 | /* |
| 889 | * Allow conntrack to be preserved. By default, conntrack |
| 890 | * is created and destroyed for every packet. |
| 891 | * Sometimes keeping conntrack can be useful for |
| 892 | * IP_VS_CONN_F_ONE_PACKET too. |
| 893 | */ |
| 894 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 895 | if (ip_vs_conntrack_enabled(ipvs)) |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 896 | cp->flags |= IP_VS_CONN_F_NFCT; |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* Hash it in the ip_vs_conn_tab finally */ |
| 899 | ip_vs_conn_hash(cp); |
| 900 | |
| 901 | return cp; |
| 902 | } |
| 903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | /* |
| 905 | * /proc/net/ip_vs_conn entries |
| 906 | */ |
| 907 | #ifdef CONFIG_PROC_FS |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 908 | struct ip_vs_iter_state { |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 909 | struct seq_net_private p; |
| 910 | struct hlist_head *l; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 911 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
| 913 | static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos) |
| 914 | { |
| 915 | int idx; |
| 916 | struct ip_vs_conn *cp; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 917 | struct ip_vs_iter_state *iter = seq->private; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 918 | struct hlist_node *n; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 919 | |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 920 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | ct_read_lock_bh(idx); |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 922 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | if (pos-- == 0) { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 924 | iter->l = &ip_vs_conn_tab[idx]; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 925 | return cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | ct_read_unlock_bh(idx); |
| 929 | } |
| 930 | |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos) |
| 935 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 936 | struct ip_vs_iter_state *iter = seq->private; |
| 937 | |
| 938 | iter->l = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN; |
| 940 | } |
| 941 | |
| 942 | static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 943 | { |
| 944 | struct ip_vs_conn *cp = v; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 945 | struct ip_vs_iter_state *iter = seq->private; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 946 | struct hlist_node *e; |
| 947 | struct hlist_head *l = iter->l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | int idx; |
| 949 | |
| 950 | ++*pos; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 951 | if (v == SEQ_START_TOKEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return ip_vs_conn_array(seq, 0); |
| 953 | |
| 954 | /* more on same hash chain? */ |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 955 | if ((e = cp->c_list.next)) |
| 956 | return hlist_entry(e, struct ip_vs_conn, c_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | idx = l - ip_vs_conn_tab; |
| 959 | ct_read_unlock_bh(idx); |
| 960 | |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 961 | while (++idx < ip_vs_conn_tab_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | ct_read_lock_bh(idx); |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 963 | hlist_for_each_entry(cp, e, &ip_vs_conn_tab[idx], c_list) { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 964 | iter->l = &ip_vs_conn_tab[idx]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | return cp; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | ct_read_unlock_bh(idx); |
| 968 | } |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 969 | iter->l = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return NULL; |
| 971 | } |
| 972 | |
| 973 | static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v) |
| 974 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 975 | struct ip_vs_iter_state *iter = seq->private; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 976 | struct hlist_head *l = iter->l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
| 978 | if (l) |
| 979 | ct_read_unlock_bh(l - ip_vs_conn_tab); |
| 980 | } |
| 981 | |
| 982 | static int ip_vs_conn_seq_show(struct seq_file *seq, void *v) |
| 983 | { |
| 984 | |
| 985 | if (v == SEQ_START_TOKEN) |
| 986 | seq_puts(seq, |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 987 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | else { |
| 989 | const struct ip_vs_conn *cp = v; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 990 | struct net *net = seq_file_net(seq); |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 991 | char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3]; |
| 992 | size_t len = 0; |
| 993 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 994 | if (!ip_vs_conn_net_eq(cp, net)) |
| 995 | return 0; |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 996 | if (cp->pe_data) { |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 997 | pe_data[0] = ' '; |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 998 | len = strlen(cp->pe->name); |
| 999 | memcpy(pe_data + 1, cp->pe->name, len); |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1000 | pe_data[len + 1] = ' '; |
| 1001 | len += 2; |
Simon Horman | e9e5eee | 2010-11-08 20:05:57 +0900 | [diff] [blame] | 1002 | len += cp->pe->show_pe_data(cp, pe_data + len); |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1003 | } |
| 1004 | pe_data[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1006 | #ifdef CONFIG_IP_VS_IPV6 |
| 1007 | if (cp->af == AF_INET6) |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1008 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X " |
| 1009 | "%pI6 %04X %-11s %7lu%s\n", |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1010 | ip_vs_proto_name(cp->protocol), |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 1011 | &cp->caddr.in6, ntohs(cp->cport), |
| 1012 | &cp->vaddr.in6, ntohs(cp->vport), |
| 1013 | &cp->daddr.in6, ntohs(cp->dport), |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1014 | ip_vs_state_name(cp->protocol, cp->state), |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1015 | (cp->timer.expires-jiffies)/HZ, pe_data); |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1016 | else |
| 1017 | #endif |
| 1018 | seq_printf(seq, |
| 1019 | "%-3s %08X %04X %08X %04X" |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1020 | " %08X %04X %-11s %7lu%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | ip_vs_proto_name(cp->protocol), |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 1022 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
| 1023 | ntohl(cp->vaddr.ip), ntohs(cp->vport), |
| 1024 | ntohl(cp->daddr.ip), ntohs(cp->dport), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | ip_vs_state_name(cp->protocol, cp->state), |
Simon Horman | a3c918a | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 1026 | (cp->timer.expires-jiffies)/HZ, pe_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | } |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1031 | static const struct seq_operations ip_vs_conn_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | .start = ip_vs_conn_seq_start, |
| 1033 | .next = ip_vs_conn_seq_next, |
| 1034 | .stop = ip_vs_conn_seq_stop, |
| 1035 | .show = ip_vs_conn_seq_show, |
| 1036 | }; |
| 1037 | |
| 1038 | static int ip_vs_conn_open(struct inode *inode, struct file *file) |
| 1039 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1040 | return seq_open_net(inode, file, &ip_vs_conn_seq_ops, |
| 1041 | sizeof(struct ip_vs_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1044 | static const struct file_operations ip_vs_conn_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | .owner = THIS_MODULE, |
| 1046 | .open = ip_vs_conn_open, |
| 1047 | .read = seq_read, |
| 1048 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 1049 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | }; |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1051 | |
| 1052 | static const char *ip_vs_origin_name(unsigned flags) |
| 1053 | { |
| 1054 | if (flags & IP_VS_CONN_F_SYNC) |
| 1055 | return "SYNC"; |
| 1056 | else |
| 1057 | return "LOCAL"; |
| 1058 | } |
| 1059 | |
| 1060 | static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v) |
| 1061 | { |
| 1062 | |
| 1063 | if (v == SEQ_START_TOKEN) |
| 1064 | seq_puts(seq, |
| 1065 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n"); |
| 1066 | else { |
| 1067 | const struct ip_vs_conn *cp = v; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1068 | struct net *net = seq_file_net(seq); |
| 1069 | |
| 1070 | if (!ip_vs_conn_net_eq(cp, net)) |
| 1071 | return 0; |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1072 | |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1073 | #ifdef CONFIG_IP_VS_IPV6 |
| 1074 | if (cp->af == AF_INET6) |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1075 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n", |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1076 | ip_vs_proto_name(cp->protocol), |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 1077 | &cp->caddr.in6, ntohs(cp->cport), |
| 1078 | &cp->vaddr.in6, ntohs(cp->vport), |
| 1079 | &cp->daddr.in6, ntohs(cp->dport), |
Vince Busam | 667a5f1 | 2008-09-02 15:55:49 +0200 | [diff] [blame] | 1080 | ip_vs_state_name(cp->protocol, cp->state), |
| 1081 | ip_vs_origin_name(cp->flags), |
| 1082 | (cp->timer.expires-jiffies)/HZ); |
| 1083 | else |
| 1084 | #endif |
| 1085 | seq_printf(seq, |
| 1086 | "%-3s %08X %04X %08X %04X " |
| 1087 | "%08X %04X %-11s %-6s %7lu\n", |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1088 | ip_vs_proto_name(cp->protocol), |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 1089 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
| 1090 | ntohl(cp->vaddr.ip), ntohs(cp->vport), |
| 1091 | ntohl(cp->daddr.ip), ntohs(cp->dport), |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1092 | ip_vs_state_name(cp->protocol, cp->state), |
| 1093 | ip_vs_origin_name(cp->flags), |
| 1094 | (cp->timer.expires-jiffies)/HZ); |
| 1095 | } |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static const struct seq_operations ip_vs_conn_sync_seq_ops = { |
| 1100 | .start = ip_vs_conn_seq_start, |
| 1101 | .next = ip_vs_conn_seq_next, |
| 1102 | .stop = ip_vs_conn_seq_stop, |
| 1103 | .show = ip_vs_conn_sync_seq_show, |
| 1104 | }; |
| 1105 | |
| 1106 | static int ip_vs_conn_sync_open(struct inode *inode, struct file *file) |
| 1107 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1108 | return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops, |
| 1109 | sizeof(struct ip_vs_iter_state)); |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | static const struct file_operations ip_vs_conn_sync_fops = { |
| 1113 | .owner = THIS_MODULE, |
| 1114 | .open = ip_vs_conn_sync_open, |
| 1115 | .read = seq_read, |
| 1116 | .llseek = seq_lseek, |
Hans Schillstrom | 0f08190 | 2011-05-15 17:20:29 +0200 | [diff] [blame] | 1117 | .release = seq_release_net, |
Rumen G. Bogdanovski | 7a4fbb1 | 2007-11-19 21:52:42 -0800 | [diff] [blame] | 1118 | }; |
| 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | #endif |
| 1121 | |
| 1122 | |
| 1123 | /* |
| 1124 | * Randomly drop connection entries before running out of memory |
| 1125 | */ |
| 1126 | static inline int todrop_entry(struct ip_vs_conn *cp) |
| 1127 | { |
| 1128 | /* |
| 1129 | * The drop rate array needs tuning for real environments. |
| 1130 | * Called from timer bh only => no locking |
| 1131 | */ |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 1132 | 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] | 1133 | static char todrop_counter[9] = {0}; |
| 1134 | int i; |
| 1135 | |
| 1136 | /* if the conn entry hasn't lasted for 60 seconds, don't drop it. |
| 1137 | This will leave enough time for normal connection to get |
| 1138 | through. */ |
| 1139 | if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ)) |
| 1140 | return 0; |
| 1141 | |
| 1142 | /* Don't drop the entry if its number of incoming packets is not |
| 1143 | located in [0, 8] */ |
| 1144 | i = atomic_read(&cp->in_pkts); |
| 1145 | if (i > 8 || i < 0) return 0; |
| 1146 | |
| 1147 | if (!todrop_rate[i]) return 0; |
| 1148 | if (--todrop_counter[i] > 0) return 0; |
| 1149 | |
| 1150 | todrop_counter[i] = todrop_rate[i]; |
| 1151 | return 1; |
| 1152 | } |
| 1153 | |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 1154 | /* Called from keventd and must protect itself from softirqs */ |
Hans Schillstrom | f6340ee | 2011-01-03 14:44:59 +0100 | [diff] [blame] | 1155 | void ip_vs_random_dropentry(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | { |
| 1157 | int idx; |
| 1158 | struct ip_vs_conn *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
| 1160 | /* |
| 1161 | * Randomly scan 1/32 of the whole table every second |
| 1162 | */ |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 1163 | for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) { |
| 1164 | unsigned hash = net_random() & ip_vs_conn_tab_mask; |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1165 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | /* |
| 1168 | * Lock is actually needed in this loop. |
| 1169 | */ |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 1170 | ct_write_lock_bh(hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1172 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) { |
Julian Anastasov | 87375ab | 2005-09-14 21:08:51 -0700 | [diff] [blame] | 1173 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | /* connection template */ |
| 1175 | continue; |
Hans Schillstrom | f6340ee | 2011-01-03 14:44:59 +0100 | [diff] [blame] | 1176 | if (!ip_vs_conn_net_eq(cp, net)) |
| 1177 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (cp->protocol == IPPROTO_TCP) { |
| 1179 | switch(cp->state) { |
| 1180 | case IP_VS_TCP_S_SYN_RECV: |
| 1181 | case IP_VS_TCP_S_SYNACK: |
| 1182 | break; |
| 1183 | |
| 1184 | case IP_VS_TCP_S_ESTABLISHED: |
| 1185 | if (todrop_entry(cp)) |
| 1186 | break; |
| 1187 | continue; |
| 1188 | |
| 1189 | default: |
| 1190 | continue; |
| 1191 | } |
| 1192 | } else { |
| 1193 | if (!todrop_entry(cp)) |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | IP_VS_DBG(4, "del connection\n"); |
| 1198 | ip_vs_conn_expire_now(cp); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 1199 | if (cp->control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | IP_VS_DBG(4, "del conn template\n"); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 1201 | ip_vs_conn_expire_now(cp->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
Julian Anastasov | af9debd | 2005-07-11 20:59:57 -0700 | [diff] [blame] | 1204 | ct_write_unlock_bh(hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | /* |
| 1210 | * Flush all the connection entries in the ip_vs_conn_tab |
| 1211 | */ |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1212 | static void ip_vs_conn_flush(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | { |
| 1214 | int idx; |
| 1215 | struct ip_vs_conn *cp; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1216 | struct netns_ipvs *ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame] | 1218 | flush_again: |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 1219 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1220 | struct hlist_node *n; |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * Lock is actually needed in this loop. |
| 1224 | */ |
| 1225 | ct_write_lock_bh(idx); |
| 1226 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1227 | hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1228 | if (!ip_vs_conn_net_eq(cp, net)) |
| 1229 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | IP_VS_DBG(4, "del connection\n"); |
| 1231 | ip_vs_conn_expire_now(cp); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 1232 | if (cp->control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | IP_VS_DBG(4, "del conn template\n"); |
Neil Horman | fb3d894 | 2005-06-28 15:40:02 -0700 | [diff] [blame] | 1234 | ip_vs_conn_expire_now(cp->control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | ct_write_unlock_bh(idx); |
| 1238 | } |
| 1239 | |
| 1240 | /* the counter may be not NULL, because maybe some conn entries |
| 1241 | are run by slow timer handler or unhashed but still referred */ |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1242 | if (atomic_read(&ipvs->conn_count) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | schedule(); |
| 1244 | goto flush_again; |
| 1245 | } |
| 1246 | } |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1247 | /* |
| 1248 | * per netns init and exit |
| 1249 | */ |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame^] | 1250 | int __net_init ip_vs_conn_net_init(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1251 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1252 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 1253 | |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1254 | atomic_set(&ipvs->conn_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1256 | proc_net_fops_create(net, "ip_vs_conn", 0, &ip_vs_conn_fops); |
| 1257 | proc_net_fops_create(net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Hans Schillstrom | 503cf15 | 2011-05-01 18:50:16 +0200 | [diff] [blame^] | 1261 | void __net_exit ip_vs_conn_net_cleanup(struct net *net) |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1262 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 1263 | /* flush all the connection entries first */ |
| 1264 | ip_vs_conn_flush(net); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1265 | proc_net_remove(net, "ip_vs_conn"); |
| 1266 | proc_net_remove(net, "ip_vs_conn_sync"); |
| 1267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
Sven Wegener | 048cf48 | 2008-08-10 18:24:35 +0000 | [diff] [blame] | 1269 | int __init ip_vs_conn_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | { |
| 1271 | int idx; |
| 1272 | |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 1273 | /* Compute size and mask */ |
| 1274 | ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits; |
| 1275 | ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1; |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * Allocate the connection hash table and initialize its list heads |
| 1279 | */ |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1280 | ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | if (!ip_vs_conn_tab) |
| 1282 | return -ENOMEM; |
| 1283 | |
| 1284 | /* Allocate ip_vs_conn slab cache */ |
| 1285 | ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn", |
| 1286 | sizeof(struct ip_vs_conn), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1287 | SLAB_HWCACHE_ALIGN, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | if (!ip_vs_conn_cachep) { |
| 1289 | vfree(ip_vs_conn_tab); |
| 1290 | return -ENOMEM; |
| 1291 | } |
| 1292 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1293 | pr_info("Connection hash table configured " |
| 1294 | "(size=%d, memory=%ldKbytes)\n", |
Catalin(ux) M. BOIE | 6f7edb4 | 2010-01-05 05:50:24 +0100 | [diff] [blame] | 1295 | ip_vs_conn_tab_size, |
| 1296 | (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n", |
| 1298 | sizeof(struct ip_vs_conn)); |
| 1299 | |
Changli Gao | 731109e | 2011-02-19 18:05:08 +0800 | [diff] [blame] | 1300 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) |
| 1301 | INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) { |
| 1304 | rwlock_init(&__ip_vs_conntbl_lock_array[idx].l); |
| 1305 | } |
| 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | /* calculate the random value for connection hash */ |
| 1308 | get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd)); |
| 1309 | |
Hans Schillstrom | 7a4f076 | 2011-05-03 22:09:31 +0200 | [diff] [blame] | 1310 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | void ip_vs_conn_cleanup(void) |
| 1314 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* Release the empty cache */ |
| 1316 | kmem_cache_destroy(ip_vs_conn_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | vfree(ip_vs_conn_tab); |
| 1318 | } |