blob: da07411b36d2dd627862f9348cbab42e35a491bf [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 Melo2d8c4ce2005-08-09 20:07:13 -070017#include <linux/interrupt.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070018#include <linux/ip.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070022#include <linux/types.h>
23
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070024#include <net/sock.h>
25
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070026/* 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.
29 */
30struct inet_ehash_bucket {
31 rwlock_t lock;
32 struct hlist_head chain;
33} __attribute__((__aligned__(8)));
34
35/* There are a few simple rules, which allow for local port reuse by
36 * an application. In essence:
37 *
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
45 * shared.
46 * Failing this, the port cannot be shared.
47 *
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.
58 *
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
65 */
66struct inet_bind_bucket {
67 unsigned short port;
68 signed short fastreuse;
69 struct hlist_node node;
70 struct hlist_head owners;
71};
72
73#define inet_bind_bucket_for_each(tb, node, head) \
74 hlist_for_each_entry(tb, node, head, node)
75
76struct inet_bind_hashbucket {
77 spinlock_t lock;
78 struct hlist_head chain;
79};
80
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. */
83
84struct 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:
87 *
88 * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
89 *
90 * First half of the table is for sockets not in TIME_WAIT, second half
91 * is for TIME_WAIT sockets only.
92 */
93 struct inet_ehash_bucket *ehash;
94
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.
97 */
98 struct inet_bind_hashbucket *bhash;
99
100 int bhash_size;
101 int ehash_size;
102
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.
106 */
107 struct hlist_head listening_hash[INET_LHTABLE_SIZE];
108
109 /* All the above members are written once at bootup and
110 * never written again _or_ are predominantly read-access.
111 *
112 * Now align to a new cache line as all the following members
113 * are often dirty.
114 */
115 rwlock_t lhash_lock ____cacheline_aligned;
116 atomic_t lhash_users;
117 wait_queue_head_t lhash_wait;
118 spinlock_t portalloc_lock;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700119 kmem_cache_t *bind_bucket_cachep;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -0700120 int port_rover;
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700121};
122
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -0700123static inline int inet_ehashfn(const __u32 laddr, const __u16 lport,
124 const __u32 faddr, const __u16 fport,
125 const int ehash_size)
126{
127 int h = (laddr ^ lport) ^ (faddr ^ fport);
128 h ^= h >> 16;
129 h ^= h >> 8;
130 return h & (ehash_size - 1);
131}
132
133static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size)
134{
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;
140
141 return inet_ehashfn(laddr, lport, faddr, fport, ehash_size);
142}
143
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700144extern struct inet_bind_bucket *
145 inet_bind_bucket_create(kmem_cache_t *cachep,
146 struct inet_bind_hashbucket *head,
147 const unsigned short snum);
148extern void inet_bind_bucket_destroy(kmem_cache_t *cachep,
149 struct inet_bind_bucket *tb);
150
151static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
152{
153 return lport & (bhash_size - 1);
154}
155
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700156extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
157 const unsigned short snum);
158
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -0700159/* These can have wildcards, don't try too hard. */
160static inline int inet_lhashfn(const unsigned short num)
161{
162 return num & (INET_LHTABLE_SIZE - 1);
163}
164
165static inline int inet_sk_listen_hashfn(const struct sock *sk)
166{
167 return inet_lhashfn(inet_sk(sk)->num);
168}
169
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -0700170/* Caller must disable local BH processing. */
171static inline void __inet_inherit_port(struct inet_hashinfo *table,
172 struct sock *sk, struct sock *child)
173{
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;
177
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);
183}
184
185static inline void inet_inherit_port(struct inet_hashinfo *table,
186 struct sock *sk, struct sock *child)
187{
188 local_bh_disable();
189 __inet_inherit_port(table, sk, child);
190 local_bh_enable();
191}
192
193extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
194
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -0700195#endif /* _INET_HASHTABLES_H */