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/list.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
26 /* This is for all connections with a full identity, no wildcards.
27 * New scheme, half the table is for TIME_WAIT, the other half is
28 * for the rest. I'll experiment with dynamic table growth later.
30 struct inet_ehash_bucket {
32 struct hlist_head chain;
33 } __attribute__((__aligned__(8)));
35 /* There are a few simple rules, which allow for local port reuse by
36 * an application. In essence:
38 * 1) Sockets bound to different interfaces may share a local port.
39 * Failing that, goto test 2.
40 * 2) If all sockets have sk->sk_reuse set, and none of them are in
41 * TCP_LISTEN state, the port may be shared.
42 * Failing that, goto test 3.
43 * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
44 * address, and none of them are the same, the port may be
46 * Failing this, the port cannot be shared.
48 * The interesting point, is test #2. This is what an FTP server does
49 * all day. To optimize this case we use a specific flag bit defined
50 * below. As we add sockets to a bind bucket list, we perform a
51 * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
52 * As long as all sockets added to a bind bucket pass this test,
53 * the flag bit will be set.
54 * The resulting situation is that tcp_v[46]_verify_bind() can just check
55 * for this flag bit, if it is set and the socket trying to bind has
56 * sk->sk_reuse set, we don't even have to walk the owners list at all,
57 * we return that it is ok to bind this socket to the requested local port.
59 * Sounds like a lot of work, but it is worth it. In a more naive
60 * implementation (ie. current FreeBSD etc.) the entire list of ports
61 * must be walked for each data port opened by an ftp server. Needless
62 * to say, this does not scale at all. With a couple thousand FTP
63 * users logged onto your box, isn't it nice to know that new data
64 * ports are created in O(1) time? I thought so. ;-) -DaveM
66 struct inet_bind_bucket {
68 signed short fastreuse;
69 struct hlist_node node;
70 struct hlist_head owners;
73 #define inet_bind_bucket_for_each(tb, node, head) \
74 hlist_for_each_entry(tb, node, head, node)
76 struct inet_bind_hashbucket {
78 struct hlist_head chain;
81 /* This is for listening sockets, thus all sockets which possess wildcards. */
82 #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
84 struct inet_hashinfo {
85 /* This is for sockets with full identity only. Sockets here will
86 * always be without wildcards and will have the following invariant:
88 * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
90 * First half of the table is for sockets not in TIME_WAIT, second half
91 * is for TIME_WAIT sockets only.
93 struct inet_ehash_bucket *ehash;
95 /* Ok, let's try this, I give up, we do need a local binding
96 * TCP hash as well as the others for fast bind/connect.
98 struct inet_bind_hashbucket *bhash;
103 /* All sockets in TCP_LISTEN state will be in here. This is the only
104 * table where wildcard'd TCP sockets can exist. Hash function here
105 * is just local port number.
107 struct hlist_head listening_hash[INET_LHTABLE_SIZE];
109 /* All the above members are written once at bootup and
110 * never written again _or_ are predominantly read-access.
112 * Now align to a new cache line as all the following members
115 rwlock_t lhash_lock ____cacheline_aligned;
116 atomic_t lhash_users;
117 wait_queue_head_t lhash_wait;
118 spinlock_t portalloc_lock;
119 kmem_cache_t *bind_bucket_cachep;
123 static inline int inet_ehashfn(const __u32 laddr, const __u16 lport,
124 const __u32 faddr, const __u16 fport,
125 const int ehash_size)
127 int h = (laddr ^ lport) ^ (faddr ^ fport);
130 return h & (ehash_size - 1);
133 static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size)
135 const struct inet_sock *inet = inet_sk(sk);
136 const __u32 laddr = inet->rcv_saddr;
137 const __u16 lport = inet->num;
138 const __u32 faddr = inet->daddr;
139 const __u16 fport = inet->dport;
141 return inet_ehashfn(laddr, lport, faddr, fport, ehash_size);
144 extern struct inet_bind_bucket *
145 inet_bind_bucket_create(kmem_cache_t *cachep,
146 struct inet_bind_hashbucket *head,
147 const unsigned short snum);
148 extern void inet_bind_bucket_destroy(kmem_cache_t *cachep,
149 struct inet_bind_bucket *tb);
151 static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
153 return lport & (bhash_size - 1);
156 extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
157 const unsigned short snum);
159 /* These can have wildcards, don't try too hard. */
160 static inline int inet_lhashfn(const unsigned short num)
162 return num & (INET_LHTABLE_SIZE - 1);
165 static inline int inet_sk_listen_hashfn(const struct sock *sk)
167 return inet_lhashfn(inet_sk(sk)->num);
170 /* Caller must disable local BH processing. */
171 static inline void __inet_inherit_port(struct inet_hashinfo *table,
172 struct sock *sk, struct sock *child)
174 const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size);
175 struct inet_bind_hashbucket *head = &table->bhash[bhash];
176 struct inet_bind_bucket *tb;
178 spin_lock(&head->lock);
179 tb = inet_sk(sk)->bind_hash;
180 sk_add_bind_node(child, &tb->owners);
181 inet_sk(child)->bind_hash = tb;
182 spin_unlock(&head->lock);
185 static inline void inet_inherit_port(struct inet_hashinfo *table,
186 struct sock *sk, struct sock *child)
189 __inet_inherit_port(table, sk, child);
193 extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
195 #endif /* _INET_HASHTABLES_H */