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.
6 * Authors: Lotsa people, from code originally in tcp
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.
14 #ifndef _INET_HASHTABLES_H
15 #define _INET_HASHTABLES_H
17 #include <linux/interrupt.h>
19 #include <linux/ipv6.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
22 #include <linux/socket.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/wait.h>
28 #include <net/tcp_states.h>
30 #include <asm/atomic.h>
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.
36 struct inet_ehash_bucket {
38 struct hlist_head chain;
39 } __attribute__((__aligned__(8)));
41 /* There are a few simple rules, which allow for local port reuse by
42 * an application. In essence:
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
52 * Failing this, the port cannot be shared.
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.
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
72 struct inet_bind_bucket {
74 signed short fastreuse;
75 struct hlist_node node;
76 struct hlist_head owners;
79 #define inet_bind_bucket_for_each(tb, node, head) \
80 hlist_for_each_entry(tb, node, head, node)
82 struct inet_bind_hashbucket {
84 struct hlist_head chain;
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. */
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:
94 * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
96 * First half of the table is for sockets not in TIME_WAIT, second half
97 * is for TIME_WAIT sockets only.
99 struct inet_ehash_bucket *ehash;
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.
104 struct inet_bind_hashbucket *bhash;
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.
113 struct hlist_head listening_hash[INET_LHTABLE_SIZE];
115 /* All the above members are written once at bootup and
116 * never written again _or_ are predominantly read-access.
118 * Now align to a new cache line as all the following members
121 rwlock_t lhash_lock ____cacheline_aligned;
122 atomic_t lhash_users;
123 wait_queue_head_t lhash_wait;
124 spinlock_t portalloc_lock;
125 kmem_cache_t *bind_bucket_cachep;
129 static inline int inet_ehashfn(const __u32 laddr, const __u16 lport,
130 const __u32 faddr, const __u16 fport,
131 const int ehash_size)
133 int h = (laddr ^ lport) ^ (faddr ^ fport);
136 return h & (ehash_size - 1);
139 static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size)
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;
147 return inet_ehashfn(laddr, lport, faddr, fport, ehash_size);
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);
157 static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
159 return lport & (bhash_size - 1);
162 extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
163 const unsigned short snum);
165 /* These can have wildcards, don't try too hard. */
166 static inline int inet_lhashfn(const unsigned short num)
168 return num & (INET_LHTABLE_SIZE - 1);
171 static inline int inet_sk_listen_hashfn(const struct sock *sk)
173 return inet_lhashfn(inet_sk(sk)->num);
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)
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;
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);
191 static inline void inet_inherit_port(struct inet_hashinfo *table,
192 struct sock *sk, struct sock *child)
195 __inet_inherit_port(table, sk, child);
199 extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
201 extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
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).
208 static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
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);
216 static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
218 if (atomic_dec_and_test(&hashinfo->lhash_users))
219 wake_up(&hashinfo->lhash_wait);
222 static inline void __inet_hash(struct inet_hashinfo *hashinfo,
223 struct sock *sk, const int listen_possible)
225 struct hlist_head *list;
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);
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;
239 __sk_add_node(sk, list);
240 sock_prot_inc_use(sk->sk_prot);
242 if (listen_possible && sk->sk_state == TCP_LISTEN)
243 wake_up(&hashinfo->lhash_wait);
246 static inline void inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
248 if (sk->sk_state != TCP_CLOSE) {
250 __inet_hash(hashinfo, sk, 1);
255 static inline void inet_unhash(struct inet_hashinfo *hashinfo, struct sock *sk)
262 if (sk->sk_state == TCP_LISTEN) {
264 inet_listen_wlock(hashinfo);
265 lock = &hashinfo->lhash_lock;
267 struct inet_ehash_bucket *head = &hashinfo->ehash[sk->sk_hashent];
269 write_lock_bh(&head->lock);
272 if (__sk_del_node_init(sk))
273 sock_prot_dec_use(sk->sk_prot);
274 write_unlock_bh(lock);
276 if (sk->sk_state == TCP_LISTEN)
277 wake_up(&hashinfo->lhash_wait);
280 extern struct sock *__inet_lookup_listener(const struct hlist_head *head,
282 const unsigned short hnum,
285 /* Optimize the common listener case. */
286 static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo,
288 const unsigned short hnum,
291 struct sock *sk = NULL;
292 struct hlist_head *head;
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)));
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)
304 sk = __inet_lookup_listener(head, daddr, hnum, dif);
310 read_unlock(&hashinfo->lhash_lock);
313 #endif /* _INET_HASHTABLES_H */