blob: c38c637e0734fd571a05e1dd97088fb1e8f2f75f [file] [log] [blame]
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -07001/*
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
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070017#include <linux/config.h>
18
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070019#include <linux/interrupt.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070020#include <linux/ip.h>
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -070021#include <linux/ipv6.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070022#include <linux/list.h>
23#include <linux/slab.h>
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -070024#include <linux/socket.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070025#include <linux/spinlock.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070026#include <linux/types.h>
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070027#include <linux/wait.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070028
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070029#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070030#include <net/tcp_states.h>
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070031
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070032#include <asm/atomic.h>
33
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070034/* This is for all connections with a full identity, no wildcards.
35 * New scheme, half the table is for TIME_WAIT, the other half is
36 * for the rest. I'll experiment with dynamic table growth later.
37 */
38struct inet_ehash_bucket {
39 rwlock_t lock;
40 struct hlist_head chain;
41} __attribute__((__aligned__(8)));
42
43/* There are a few simple rules, which allow for local port reuse by
44 * an application. In essence:
45 *
46 * 1) Sockets bound to different interfaces may share a local port.
47 * Failing that, goto test 2.
48 * 2) If all sockets have sk->sk_reuse set, and none of them are in
49 * TCP_LISTEN state, the port may be shared.
50 * Failing that, goto test 3.
51 * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
52 * address, and none of them are the same, the port may be
53 * shared.
54 * Failing this, the port cannot be shared.
55 *
56 * The interesting point, is test #2. This is what an FTP server does
57 * all day. To optimize this case we use a specific flag bit defined
58 * below. As we add sockets to a bind bucket list, we perform a
59 * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
60 * As long as all sockets added to a bind bucket pass this test,
61 * the flag bit will be set.
62 * The resulting situation is that tcp_v[46]_verify_bind() can just check
63 * for this flag bit, if it is set and the socket trying to bind has
64 * sk->sk_reuse set, we don't even have to walk the owners list at all,
65 * we return that it is ok to bind this socket to the requested local port.
66 *
67 * Sounds like a lot of work, but it is worth it. In a more naive
68 * implementation (ie. current FreeBSD etc.) the entire list of ports
69 * must be walked for each data port opened by an ftp server. Needless
70 * to say, this does not scale at all. With a couple thousand FTP
71 * users logged onto your box, isn't it nice to know that new data
72 * ports are created in O(1) time? I thought so. ;-) -DaveM
73 */
74struct inet_bind_bucket {
75 unsigned short port;
76 signed short fastreuse;
77 struct hlist_node node;
78 struct hlist_head owners;
79};
80
81#define inet_bind_bucket_for_each(tb, node, head) \
82 hlist_for_each_entry(tb, node, head, node)
83
84struct inet_bind_hashbucket {
85 spinlock_t lock;
86 struct hlist_head chain;
87};
88
89/* This is for listening sockets, thus all sockets which possess wildcards. */
90#define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
91
92struct inet_hashinfo {
93 /* This is for sockets with full identity only. Sockets here will
94 * always be without wildcards and will have the following invariant:
95 *
96 * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
97 *
98 * First half of the table is for sockets not in TIME_WAIT, second half
99 * is for TIME_WAIT sockets only.
100 */
101 struct inet_ehash_bucket *ehash;
102
103 /* Ok, let's try this, I give up, we do need a local binding
104 * TCP hash as well as the others for fast bind/connect.
105 */
106 struct inet_bind_hashbucket *bhash;
107
108 int bhash_size;
109 int ehash_size;
110
111 /* All sockets in TCP_LISTEN state will be in here. This is the only
112 * table where wildcard'd TCP sockets can exist. Hash function here
113 * is just local port number.
114 */
115 struct hlist_head listening_hash[INET_LHTABLE_SIZE];
116
117 /* All the above members are written once at bootup and
118 * never written again _or_ are predominantly read-access.
119 *
120 * Now align to a new cache line as all the following members
121 * are often dirty.
122 */
123 rwlock_t lhash_lock ____cacheline_aligned;
124 atomic_t lhash_users;
125 wait_queue_head_t lhash_wait;
126 spinlock_t portalloc_lock;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700127 kmem_cache_t *bind_bucket_cachep;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -0700128 int port_rover;
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700129};
130
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -0700131static inline int inet_ehashfn(const __u32 laddr, const __u16 lport,
132 const __u32 faddr, const __u16 fport,
133 const int ehash_size)
134{
135 int h = (laddr ^ lport) ^ (faddr ^ fport);
136 h ^= h >> 16;
137 h ^= h >> 8;
138 return h & (ehash_size - 1);
139}
140
141static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size)
142{
143 const struct inet_sock *inet = inet_sk(sk);
144 const __u32 laddr = inet->rcv_saddr;
145 const __u16 lport = inet->num;
146 const __u32 faddr = inet->daddr;
147 const __u16 fport = inet->dport;
148
149 return inet_ehashfn(laddr, lport, faddr, fport, ehash_size);
150}
151
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700152extern struct inet_bind_bucket *
153 inet_bind_bucket_create(kmem_cache_t *cachep,
154 struct inet_bind_hashbucket *head,
155 const unsigned short snum);
156extern void inet_bind_bucket_destroy(kmem_cache_t *cachep,
157 struct inet_bind_bucket *tb);
158
159static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
160{
161 return lport & (bhash_size - 1);
162}
163
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700164extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
165 const unsigned short snum);
166
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700167/* These can have wildcards, don't try too hard. */
168static inline int inet_lhashfn(const unsigned short num)
169{
170 return num & (INET_LHTABLE_SIZE - 1);
171}
172
173static inline int inet_sk_listen_hashfn(const struct sock *sk)
174{
175 return inet_lhashfn(inet_sk(sk)->num);
176}
177
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700178/* Caller must disable local BH processing. */
179static inline void __inet_inherit_port(struct inet_hashinfo *table,
180 struct sock *sk, struct sock *child)
181{
182 const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size);
183 struct inet_bind_hashbucket *head = &table->bhash[bhash];
184 struct inet_bind_bucket *tb;
185
186 spin_lock(&head->lock);
187 tb = inet_sk(sk)->bind_hash;
188 sk_add_bind_node(child, &tb->owners);
189 inet_sk(child)->bind_hash = tb;
190 spin_unlock(&head->lock);
191}
192
193static inline void inet_inherit_port(struct inet_hashinfo *table,
194 struct sock *sk, struct sock *child)
195{
196 local_bh_disable();
197 __inet_inherit_port(table, sk, child);
198 local_bh_enable();
199}
200
201extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
202
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -0700203extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
204
205/*
206 * - We may sleep inside this lock.
207 * - If sleeping is not required (or called from BH),
208 * use plain read_(un)lock(&inet_hashinfo.lhash_lock).
209 */
210static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
211{
212 /* read_lock synchronizes to candidates to writers */
213 read_lock(&hashinfo->lhash_lock);
214 atomic_inc(&hashinfo->lhash_users);
215 read_unlock(&hashinfo->lhash_lock);
216}
217
218static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
219{
220 if (atomic_dec_and_test(&hashinfo->lhash_users))
221 wake_up(&hashinfo->lhash_wait);
222}
223
224static inline void __inet_hash(struct inet_hashinfo *hashinfo,
225 struct sock *sk, const int listen_possible)
226{
227 struct hlist_head *list;
228 rwlock_t *lock;
229
230 BUG_TRAP(sk_unhashed(sk));
231 if (listen_possible && sk->sk_state == TCP_LISTEN) {
232 list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
233 lock = &hashinfo->lhash_lock;
234 inet_listen_wlock(hashinfo);
235 } else {
236 sk->sk_hashent = inet_sk_ehashfn(sk, hashinfo->ehash_size);
237 list = &hashinfo->ehash[sk->sk_hashent].chain;
238 lock = &hashinfo->ehash[sk->sk_hashent].lock;
239 write_lock(lock);
240 }
241 __sk_add_node(sk, list);
242 sock_prot_inc_use(sk->sk_prot);
243 write_unlock(lock);
244 if (listen_possible && sk->sk_state == TCP_LISTEN)
245 wake_up(&hashinfo->lhash_wait);
246}
Arnaldo Carvalho de Melo81849d12005-08-09 20:08:50 -0700247
248static inline void inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
249{
250 if (sk->sk_state != TCP_CLOSE) {
251 local_bh_disable();
252 __inet_hash(hashinfo, sk, 1);
253 local_bh_enable();
254 }
255}
256
257static inline void inet_unhash(struct inet_hashinfo *hashinfo, struct sock *sk)
258{
259 rwlock_t *lock;
260
261 if (sk_unhashed(sk))
262 goto out;
263
264 if (sk->sk_state == TCP_LISTEN) {
265 local_bh_disable();
266 inet_listen_wlock(hashinfo);
267 lock = &hashinfo->lhash_lock;
268 } else {
269 struct inet_ehash_bucket *head = &hashinfo->ehash[sk->sk_hashent];
270 lock = &head->lock;
271 write_lock_bh(&head->lock);
272 }
273
274 if (__sk_del_node_init(sk))
275 sock_prot_dec_use(sk->sk_prot);
276 write_unlock_bh(lock);
277out:
278 if (sk->sk_state == TCP_LISTEN)
279 wake_up(&hashinfo->lhash_wait);
280}
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -0700281
282extern struct sock *__inet_lookup_listener(const struct hlist_head *head,
283 const u32 daddr,
284 const unsigned short hnum,
285 const int dif);
286
287/* Optimize the common listener case. */
288static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo,
289 const u32 daddr,
290 const unsigned short hnum,
291 const int dif)
292{
293 struct sock *sk = NULL;
294 struct hlist_head *head;
295
296 read_lock(&hashinfo->lhash_lock);
297 head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
298 if (!hlist_empty(head)) {
299 const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
300
301 if (inet->num == hnum && !sk->sk_node.next &&
302 (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
303 (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
304 !sk->sk_bound_dev_if)
305 goto sherry_cache;
306 sk = __inet_lookup_listener(head, daddr, hnum, dif);
307 }
308 if (sk) {
309sherry_cache:
310 sock_hold(sk);
311 }
312 read_unlock(&hashinfo->lhash_lock);
313 return sk;
314}
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700315
316/* Socket demux engine toys. */
317#ifdef __BIG_ENDIAN
318#define INET_COMBINED_PORTS(__sport, __dport) \
319 (((__u32)(__sport) << 16) | (__u32)(__dport))
320#else /* __LITTLE_ENDIAN */
321#define INET_COMBINED_PORTS(__sport, __dport) \
322 (((__u32)(__dport) << 16) | (__u32)(__sport))
323#endif
324
325#if (BITS_PER_LONG == 64)
326#ifdef __BIG_ENDIAN
327#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
328 const __u64 __name = (((__u64)(__saddr)) << 32) | ((__u64)(__daddr));
329#else /* __LITTLE_ENDIAN */
330#define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
331 const __u64 __name = (((__u64)(__daddr)) << 32) | ((__u64)(__saddr));
332#endif /* __BIG_ENDIAN */
333#define INET_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
334 (((*((__u64 *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
335 ((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
336 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
337#define INET_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
338 (((*((__u64 *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
339 ((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
340 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
341#else /* 32-bit arch */
342#define INET_ADDR_COOKIE(__name, __saddr, __daddr)
343#define INET_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif) \
344 ((inet_sk(__sk)->daddr == (__saddr)) && \
345 (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
346 ((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
347 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
348#define INET_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif) \
349 ((inet_twsk(__sk)->tw_daddr == (__saddr)) && \
350 (inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
351 ((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
352 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
353#endif /* 64-bit arch */
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -0700354#endif /* _INET_HASHTABLES_H */