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