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