Commit | Line | Data |
---|---|---|
304a1618 ACM |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * Authors: Lotsa people, from code originally in tcp | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #ifndef _INET_HASHTABLES_H | |
15 | #define _INET_HASHTABLES_H | |
16 | ||
2d8c4ce5 | 17 | #include <linux/interrupt.h> |
77d8bf9c | 18 | #include <linux/ip.h> |
33b62231 | 19 | #include <linux/ipv6.h> |
77d8bf9c ACM |
20 | #include <linux/list.h> |
21 | #include <linux/slab.h> | |
33b62231 | 22 | #include <linux/socket.h> |
77d8bf9c | 23 | #include <linux/spinlock.h> |
304a1618 | 24 | #include <linux/types.h> |
f3f05f70 | 25 | #include <linux/wait.h> |
304a1618 | 26 | |
2d8c4ce5 | 27 | #include <net/sock.h> |
c752f073 | 28 | #include <net/tcp_states.h> |
2d8c4ce5 | 29 | |
f3f05f70 ACM |
30 | #include <asm/atomic.h> |
31 | ||
77d8bf9c ACM |
32 | /* This is for all connections with a full identity, no wildcards. |
33 | * New scheme, half the table is for TIME_WAIT, the other half is | |
34 | * for the rest. I'll experiment with dynamic table growth later. | |
35 | */ | |
36 | struct inet_ehash_bucket { | |
37 | rwlock_t lock; | |
38 | struct hlist_head chain; | |
39 | } __attribute__((__aligned__(8))); | |
40 | ||
41 | /* There are a few simple rules, which allow for local port reuse by | |
42 | * an application. In essence: | |
43 | * | |
44 | * 1) Sockets bound to different interfaces may share a local port. | |
45 | * Failing that, goto test 2. | |
46 | * 2) If all sockets have sk->sk_reuse set, and none of them are in | |
47 | * TCP_LISTEN state, the port may be shared. | |
48 | * Failing that, goto test 3. | |
49 | * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local | |
50 | * address, and none of them are the same, the port may be | |
51 | * shared. | |
52 | * Failing this, the port cannot be shared. | |
53 | * | |
54 | * The interesting point, is test #2. This is what an FTP server does | |
55 | * all day. To optimize this case we use a specific flag bit defined | |
56 | * below. As we add sockets to a bind bucket list, we perform a | |
57 | * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN)) | |
58 | * As long as all sockets added to a bind bucket pass this test, | |
59 | * the flag bit will be set. | |
60 | * The resulting situation is that tcp_v[46]_verify_bind() can just check | |
61 | * for this flag bit, if it is set and the socket trying to bind has | |
62 | * sk->sk_reuse set, we don't even have to walk the owners list at all, | |
63 | * we return that it is ok to bind this socket to the requested local port. | |
64 | * | |
65 | * Sounds like a lot of work, but it is worth it. In a more naive | |
66 | * implementation (ie. current FreeBSD etc.) the entire list of ports | |
67 | * must be walked for each data port opened by an ftp server. Needless | |
68 | * to say, this does not scale at all. With a couple thousand FTP | |
69 | * users logged onto your box, isn't it nice to know that new data | |
70 | * ports are created in O(1) time? I thought so. ;-) -DaveM | |
71 | */ | |
72 | struct inet_bind_bucket { | |
73 | unsigned short port; | |
74 | signed short fastreuse; | |
75 | struct hlist_node node; | |
76 | struct hlist_head owners; | |
77 | }; | |
78 | ||
79 | #define inet_bind_bucket_for_each(tb, node, head) \ | |
80 | hlist_for_each_entry(tb, node, head, node) | |
81 | ||
82 | struct inet_bind_hashbucket { | |
83 | spinlock_t lock; | |
84 | struct hlist_head chain; | |
85 | }; | |
86 | ||
87 | /* This is for listening sockets, thus all sockets which possess wildcards. */ | |
88 | #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */ | |
89 | ||
90 | struct inet_hashinfo { | |
91 | /* This is for sockets with full identity only. Sockets here will | |
92 | * always be without wildcards and will have the following invariant: | |
93 | * | |
94 | * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE | |
95 | * | |
96 | * First half of the table is for sockets not in TIME_WAIT, second half | |
97 | * is for TIME_WAIT sockets only. | |
98 | */ | |
99 | struct inet_ehash_bucket *ehash; | |
100 | ||
101 | /* Ok, let's try this, I give up, we do need a local binding | |
102 | * TCP hash as well as the others for fast bind/connect. | |
103 | */ | |
104 | struct inet_bind_hashbucket *bhash; | |
105 | ||
106 | int bhash_size; | |
107 | int ehash_size; | |
108 | ||
109 | /* All sockets in TCP_LISTEN state will be in here. This is the only | |
110 | * table where wildcard'd TCP sockets can exist. Hash function here | |
111 | * is just local port number. | |
112 | */ | |
113 | struct hlist_head listening_hash[INET_LHTABLE_SIZE]; | |
114 | ||
115 | /* All the above members are written once at bootup and | |
116 | * never written again _or_ are predominantly read-access. | |
117 | * | |
118 | * Now align to a new cache line as all the following members | |
119 | * are often dirty. | |
120 | */ | |
121 | rwlock_t lhash_lock ____cacheline_aligned; | |
122 | atomic_t lhash_users; | |
123 | wait_queue_head_t lhash_wait; | |
124 | spinlock_t portalloc_lock; | |
2d8c4ce5 | 125 | kmem_cache_t *bind_bucket_cachep; |
6e04e021 | 126 | int port_rover; |
77d8bf9c ACM |
127 | }; |
128 | ||
304a1618 ACM |
129 | static inline int inet_ehashfn(const __u32 laddr, const __u16 lport, |
130 | const __u32 faddr, const __u16 fport, | |
131 | const int ehash_size) | |
132 | { | |
133 | int h = (laddr ^ lport) ^ (faddr ^ fport); | |
134 | h ^= h >> 16; | |
135 | h ^= h >> 8; | |
136 | return h & (ehash_size - 1); | |
137 | } | |
138 | ||
139 | static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size) | |
140 | { | |
141 | const struct inet_sock *inet = inet_sk(sk); | |
142 | const __u32 laddr = inet->rcv_saddr; | |
143 | const __u16 lport = inet->num; | |
144 | const __u32 faddr = inet->daddr; | |
145 | const __u16 fport = inet->dport; | |
146 | ||
147 | return inet_ehashfn(laddr, lport, faddr, fport, ehash_size); | |
148 | } | |
149 | ||
77d8bf9c ACM |
150 | extern struct inet_bind_bucket * |
151 | inet_bind_bucket_create(kmem_cache_t *cachep, | |
152 | struct inet_bind_hashbucket *head, | |
153 | const unsigned short snum); | |
154 | extern void inet_bind_bucket_destroy(kmem_cache_t *cachep, | |
155 | struct inet_bind_bucket *tb); | |
156 | ||
157 | static inline int inet_bhashfn(const __u16 lport, const int bhash_size) | |
158 | { | |
159 | return lport & (bhash_size - 1); | |
160 | } | |
161 | ||
2d8c4ce5 ACM |
162 | extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb, |
163 | const unsigned short snum); | |
164 | ||
77d8bf9c ACM |
165 | /* These can have wildcards, don't try too hard. */ |
166 | static inline int inet_lhashfn(const unsigned short num) | |
167 | { | |
168 | return num & (INET_LHTABLE_SIZE - 1); | |
169 | } | |
170 | ||
171 | static inline int inet_sk_listen_hashfn(const struct sock *sk) | |
172 | { | |
173 | return inet_lhashfn(inet_sk(sk)->num); | |
174 | } | |
175 | ||
2d8c4ce5 ACM |
176 | /* Caller must disable local BH processing. */ |
177 | static inline void __inet_inherit_port(struct inet_hashinfo *table, | |
178 | struct sock *sk, struct sock *child) | |
179 | { | |
180 | const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size); | |
181 | struct inet_bind_hashbucket *head = &table->bhash[bhash]; | |
182 | struct inet_bind_bucket *tb; | |
183 | ||
184 | spin_lock(&head->lock); | |
185 | tb = inet_sk(sk)->bind_hash; | |
186 | sk_add_bind_node(child, &tb->owners); | |
187 | inet_sk(child)->bind_hash = tb; | |
188 | spin_unlock(&head->lock); | |
189 | } | |
190 | ||
191 | static inline void inet_inherit_port(struct inet_hashinfo *table, | |
192 | struct sock *sk, struct sock *child) | |
193 | { | |
194 | local_bh_disable(); | |
195 | __inet_inherit_port(table, sk, child); | |
196 | local_bh_enable(); | |
197 | } | |
198 | ||
199 | extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk); | |
200 | ||
f3f05f70 ACM |
201 | extern void inet_listen_wlock(struct inet_hashinfo *hashinfo); |
202 | ||
203 | /* | |
204 | * - We may sleep inside this lock. | |
205 | * - If sleeping is not required (or called from BH), | |
206 | * use plain read_(un)lock(&inet_hashinfo.lhash_lock). | |
207 | */ | |
208 | static inline void inet_listen_lock(struct inet_hashinfo *hashinfo) | |
209 | { | |
210 | /* read_lock synchronizes to candidates to writers */ | |
211 | read_lock(&hashinfo->lhash_lock); | |
212 | atomic_inc(&hashinfo->lhash_users); | |
213 | read_unlock(&hashinfo->lhash_lock); | |
214 | } | |
215 | ||
216 | static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo) | |
217 | { | |
218 | if (atomic_dec_and_test(&hashinfo->lhash_users)) | |
219 | wake_up(&hashinfo->lhash_wait); | |
220 | } | |
221 | ||
222 | static inline void __inet_hash(struct inet_hashinfo *hashinfo, | |
223 | struct sock *sk, const int listen_possible) | |
224 | { | |
225 | struct hlist_head *list; | |
226 | rwlock_t *lock; | |
227 | ||
228 | BUG_TRAP(sk_unhashed(sk)); | |
229 | if (listen_possible && sk->sk_state == TCP_LISTEN) { | |
230 | list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; | |
231 | lock = &hashinfo->lhash_lock; | |
232 | inet_listen_wlock(hashinfo); | |
233 | } else { | |
234 | sk->sk_hashent = inet_sk_ehashfn(sk, hashinfo->ehash_size); | |
235 | list = &hashinfo->ehash[sk->sk_hashent].chain; | |
236 | lock = &hashinfo->ehash[sk->sk_hashent].lock; | |
237 | write_lock(lock); | |
238 | } | |
239 | __sk_add_node(sk, list); | |
240 | sock_prot_inc_use(sk->sk_prot); | |
241 | write_unlock(lock); | |
242 | if (listen_possible && sk->sk_state == TCP_LISTEN) | |
243 | wake_up(&hashinfo->lhash_wait); | |
244 | } | |
81849d10 ACM |
245 | |
246 | static inline void inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk) | |
247 | { | |
248 | if (sk->sk_state != TCP_CLOSE) { | |
249 | local_bh_disable(); | |
250 | __inet_hash(hashinfo, sk, 1); | |
251 | local_bh_enable(); | |
252 | } | |
253 | } | |
254 | ||
255 | static inline void inet_unhash(struct inet_hashinfo *hashinfo, struct sock *sk) | |
256 | { | |
257 | rwlock_t *lock; | |
258 | ||
259 | if (sk_unhashed(sk)) | |
260 | goto out; | |
261 | ||
262 | if (sk->sk_state == TCP_LISTEN) { | |
263 | local_bh_disable(); | |
264 | inet_listen_wlock(hashinfo); | |
265 | lock = &hashinfo->lhash_lock; | |
266 | } else { | |
267 | struct inet_ehash_bucket *head = &hashinfo->ehash[sk->sk_hashent]; | |
268 | lock = &head->lock; | |
269 | write_lock_bh(&head->lock); | |
270 | } | |
271 | ||
272 | if (__sk_del_node_init(sk)) | |
273 | sock_prot_dec_use(sk->sk_prot); | |
274 | write_unlock_bh(lock); | |
275 | out: | |
276 | if (sk->sk_state == TCP_LISTEN) | |
277 | wake_up(&hashinfo->lhash_wait); | |
278 | } | |
33b62231 ACM |
279 | |
280 | extern struct sock *__inet_lookup_listener(const struct hlist_head *head, | |
281 | const u32 daddr, | |
282 | const unsigned short hnum, | |
283 | const int dif); | |
284 | ||
285 | /* Optimize the common listener case. */ | |
286 | static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo, | |
287 | const u32 daddr, | |
288 | const unsigned short hnum, | |
289 | const int dif) | |
290 | { | |
291 | struct sock *sk = NULL; | |
292 | struct hlist_head *head; | |
293 | ||
294 | read_lock(&hashinfo->lhash_lock); | |
295 | head = &hashinfo->listening_hash[inet_lhashfn(hnum)]; | |
296 | if (!hlist_empty(head)) { | |
297 | const struct inet_sock *inet = inet_sk((sk = __sk_head(head))); | |
298 | ||
299 | if (inet->num == hnum && !sk->sk_node.next && | |
300 | (!inet->rcv_saddr || inet->rcv_saddr == daddr) && | |
301 | (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) && | |
302 | !sk->sk_bound_dev_if) | |
303 | goto sherry_cache; | |
304 | sk = __inet_lookup_listener(head, daddr, hnum, dif); | |
305 | } | |
306 | if (sk) { | |
307 | sherry_cache: | |
308 | sock_hold(sk); | |
309 | } | |
310 | read_unlock(&hashinfo->lhash_lock); | |
311 | return sk; | |
312 | } | |
304a1618 | 313 | #endif /* _INET_HASHTABLES_H */ |