inet_hashtables.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #ifndef _INET_HASHTABLES_H
  14. #define _INET_HASHTABLES_H
  15. #include <linux/interrupt.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/socket.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/wait.h>
  23. #include <linux/vmalloc.h>
  24. #include <net/inet_connection_sock.h>
  25. #include <net/inet_sock.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <net/netns/hash.h>
  29. #include <asm/atomic.h>
  30. #include <asm/byteorder.h>
  31. /* This is for all connections with a full identity, no wildcards.
  32. * One chain is dedicated to TIME_WAIT sockets.
  33. * I'll experiment with dynamic table growth later.
  34. */
  35. struct inet_ehash_bucket {
  36. struct hlist_head chain;
  37. struct hlist_head twchain;
  38. };
  39. /* There are a few simple rules, which allow for local port reuse by
  40. * an application. In essence:
  41. *
  42. * 1) Sockets bound to different interfaces may share a local port.
  43. * Failing that, goto test 2.
  44. * 2) If all sockets have sk->sk_reuse set, and none of them are in
  45. * TCP_LISTEN state, the port may be shared.
  46. * Failing that, goto test 3.
  47. * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
  48. * address, and none of them are the same, the port may be
  49. * shared.
  50. * Failing this, the port cannot be shared.
  51. *
  52. * The interesting point, is test #2. This is what an FTP server does
  53. * all day. To optimize this case we use a specific flag bit defined
  54. * below. As we add sockets to a bind bucket list, we perform a
  55. * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
  56. * As long as all sockets added to a bind bucket pass this test,
  57. * the flag bit will be set.
  58. * The resulting situation is that tcp_v[46]_verify_bind() can just check
  59. * for this flag bit, if it is set and the socket trying to bind has
  60. * sk->sk_reuse set, we don't even have to walk the owners list at all,
  61. * we return that it is ok to bind this socket to the requested local port.
  62. *
  63. * Sounds like a lot of work, but it is worth it. In a more naive
  64. * implementation (ie. current FreeBSD etc.) the entire list of ports
  65. * must be walked for each data port opened by an ftp server. Needless
  66. * to say, this does not scale at all. With a couple thousand FTP
  67. * users logged onto your box, isn't it nice to know that new data
  68. * ports are created in O(1) time? I thought so. ;-) -DaveM
  69. */
  70. struct inet_bind_bucket {
  71. struct net *ib_net;
  72. unsigned short port;
  73. signed short fastreuse;
  74. struct hlist_node node;
  75. struct hlist_head owners;
  76. };
  77. #define inet_bind_bucket_for_each(tb, node, head) \
  78. hlist_for_each_entry(tb, node, head, node)
  79. struct inet_bind_hashbucket {
  80. spinlock_t lock;
  81. struct hlist_head chain;
  82. };
  83. /* This is for listening sockets, thus all sockets which possess wildcards. */
  84. #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
  85. struct inet_hashinfo {
  86. /* This is for sockets with full identity only. Sockets here will
  87. * always be without wildcards and will have the following invariant:
  88. *
  89. * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
  90. *
  91. * TIME_WAIT sockets use a separate chain (twchain).
  92. */
  93. struct inet_ehash_bucket *ehash;
  94. rwlock_t *ehash_locks;
  95. unsigned int ehash_size;
  96. unsigned int ehash_locks_mask;
  97. /* Ok, let's try this, I give up, we do need a local binding
  98. * TCP hash as well as the others for fast bind/connect.
  99. */
  100. struct inet_bind_hashbucket *bhash;
  101. unsigned int bhash_size;
  102. /* Note : 4 bytes padding on 64 bit arches */
  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. /* All the above members are written once at bootup and
  109. * never written again _or_ are predominantly read-access.
  110. *
  111. * Now align to a new cache line as all the following members
  112. * are often dirty.
  113. */
  114. rwlock_t lhash_lock ____cacheline_aligned;
  115. atomic_t lhash_users;
  116. wait_queue_head_t lhash_wait;
  117. struct kmem_cache *bind_bucket_cachep;
  118. };
  119. static inline struct inet_ehash_bucket *inet_ehash_bucket(
  120. struct inet_hashinfo *hashinfo,
  121. unsigned int hash)
  122. {
  123. return &hashinfo->ehash[hash & (hashinfo->ehash_size - 1)];
  124. }
  125. static inline rwlock_t *inet_ehash_lockp(
  126. struct inet_hashinfo *hashinfo,
  127. unsigned int hash)
  128. {
  129. return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask];
  130. }
  131. static inline int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
  132. {
  133. unsigned int i, size = 256;
  134. #if defined(CONFIG_PROVE_LOCKING)
  135. unsigned int nr_pcpus = 2;
  136. #else
  137. unsigned int nr_pcpus = num_possible_cpus();
  138. #endif
  139. if (nr_pcpus >= 4)
  140. size = 512;
  141. if (nr_pcpus >= 8)
  142. size = 1024;
  143. if (nr_pcpus >= 16)
  144. size = 2048;
  145. if (nr_pcpus >= 32)
  146. size = 4096;
  147. if (sizeof(rwlock_t) != 0) {
  148. #ifdef CONFIG_NUMA
  149. if (size * sizeof(rwlock_t) > PAGE_SIZE)
  150. hashinfo->ehash_locks = vmalloc(size * sizeof(rwlock_t));
  151. else
  152. #endif
  153. hashinfo->ehash_locks = kmalloc(size * sizeof(rwlock_t),
  154. GFP_KERNEL);
  155. if (!hashinfo->ehash_locks)
  156. return ENOMEM;
  157. for (i = 0; i < size; i++)
  158. rwlock_init(&hashinfo->ehash_locks[i]);
  159. }
  160. hashinfo->ehash_locks_mask = size - 1;
  161. return 0;
  162. }
  163. static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo)
  164. {
  165. if (hashinfo->ehash_locks) {
  166. #ifdef CONFIG_NUMA
  167. unsigned int size = (hashinfo->ehash_locks_mask + 1) *
  168. sizeof(rwlock_t);
  169. if (size > PAGE_SIZE)
  170. vfree(hashinfo->ehash_locks);
  171. else
  172. #endif
  173. kfree(hashinfo->ehash_locks);
  174. hashinfo->ehash_locks = NULL;
  175. }
  176. }
  177. extern struct inet_bind_bucket *
  178. inet_bind_bucket_create(struct kmem_cache *cachep,
  179. struct net *net,
  180. struct inet_bind_hashbucket *head,
  181. const unsigned short snum);
  182. extern void inet_bind_bucket_destroy(struct kmem_cache *cachep,
  183. struct inet_bind_bucket *tb);
  184. static inline int inet_bhashfn(struct net *net,
  185. const __u16 lport, const int bhash_size)
  186. {
  187. return (lport + net_hash_mix(net)) & (bhash_size - 1);
  188. }
  189. extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  190. const unsigned short snum);
  191. /* These can have wildcards, don't try too hard. */
  192. static inline int inet_lhashfn(struct net *net, const unsigned short num)
  193. {
  194. return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1);
  195. }
  196. static inline int inet_sk_listen_hashfn(const struct sock *sk)
  197. {
  198. return inet_lhashfn(sock_net(sk), inet_sk(sk)->num);
  199. }
  200. /* Caller must disable local BH processing. */
  201. extern void __inet_inherit_port(struct sock *sk, struct sock *child);
  202. extern void inet_put_port(struct sock *sk);
  203. extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
  204. /*
  205. * - We may sleep inside this lock.
  206. * - If sleeping is not required (or called from BH),
  207. * use plain read_(un)lock(&inet_hashinfo.lhash_lock).
  208. */
  209. static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
  210. {
  211. /* read_lock synchronizes to candidates to writers */
  212. read_lock(&hashinfo->lhash_lock);
  213. atomic_inc(&hashinfo->lhash_users);
  214. read_unlock(&hashinfo->lhash_lock);
  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. extern void __inet_hash_nolisten(struct sock *sk);
  222. extern void inet_hash(struct sock *sk);
  223. extern void inet_unhash(struct sock *sk);
  224. extern struct sock *__inet_lookup_listener(struct net *net,
  225. struct inet_hashinfo *hashinfo,
  226. const __be32 daddr,
  227. const unsigned short hnum,
  228. const int dif);
  229. static inline struct sock *inet_lookup_listener(struct net *net,
  230. struct inet_hashinfo *hashinfo,
  231. __be32 daddr, __be16 dport, int dif)
  232. {
  233. return __inet_lookup_listener(net, hashinfo, daddr, ntohs(dport), dif);
  234. }
  235. /* Socket demux engine toys. */
  236. /* What happens here is ugly; there's a pair of adjacent fields in
  237. struct inet_sock; __be16 dport followed by __u16 num. We want to
  238. search by pair, so we combine the keys into a single 32bit value
  239. and compare with 32bit value read from &...->dport. Let's at least
  240. make sure that it's not mixed with anything else...
  241. On 64bit targets we combine comparisons with pair of adjacent __be32
  242. fields in the same way.
  243. */
  244. typedef __u32 __bitwise __portpair;
  245. #ifdef __BIG_ENDIAN
  246. #define INET_COMBINED_PORTS(__sport, __dport) \
  247. ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
  248. #else /* __LITTLE_ENDIAN */
  249. #define INET_COMBINED_PORTS(__sport, __dport) \
  250. ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
  251. #endif
  252. #if (BITS_PER_LONG == 64)
  253. typedef __u64 __bitwise __addrpair;
  254. #ifdef __BIG_ENDIAN
  255. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  256. const __addrpair __name = (__force __addrpair) ( \
  257. (((__force __u64)(__be32)(__saddr)) << 32) | \
  258. ((__force __u64)(__be32)(__daddr)));
  259. #else /* __LITTLE_ENDIAN */
  260. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  261. const __addrpair __name = (__force __addrpair) ( \
  262. (((__force __u64)(__be32)(__daddr)) << 32) | \
  263. ((__force __u64)(__be32)(__saddr)));
  264. #endif /* __BIG_ENDIAN */
  265. #define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  266. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  267. ((*((__addrpair *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
  268. ((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  269. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  270. #define INET_TW_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  271. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  272. ((*((__addrpair *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
  273. ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  274. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  275. #else /* 32-bit arch */
  276. #define INET_ADDR_COOKIE(__name, __saddr, __daddr)
  277. #define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif) \
  278. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  279. (inet_sk(__sk)->daddr == (__saddr)) && \
  280. (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
  281. ((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  282. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  283. #define INET_TW_MATCH(__sk, __net, __hash,__cookie, __saddr, __daddr, __ports, __dif) \
  284. (((__sk)->sk_hash == (__hash)) && sock_net((__sk)) == (__net) && \
  285. (inet_twsk(__sk)->tw_daddr == (__saddr)) && \
  286. (inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
  287. ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  288. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  289. #endif /* 64-bit arch */
  290. /*
  291. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
  292. * not check it for lookups anymore, thanks Alexey. -DaveM
  293. *
  294. * Local BH must be disabled here.
  295. */
  296. extern struct sock * __inet_lookup_established(struct net *net,
  297. struct inet_hashinfo *hashinfo,
  298. const __be32 saddr, const __be16 sport,
  299. const __be32 daddr, const u16 hnum, const int dif);
  300. static inline struct sock *
  301. inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo,
  302. const __be32 saddr, const __be16 sport,
  303. const __be32 daddr, const __be16 dport,
  304. const int dif)
  305. {
  306. return __inet_lookup_established(net, hashinfo, saddr, sport, daddr,
  307. ntohs(dport), dif);
  308. }
  309. static inline struct sock *__inet_lookup(struct net *net,
  310. struct inet_hashinfo *hashinfo,
  311. const __be32 saddr, const __be16 sport,
  312. const __be32 daddr, const __be16 dport,
  313. const int dif)
  314. {
  315. u16 hnum = ntohs(dport);
  316. struct sock *sk = __inet_lookup_established(net, hashinfo,
  317. saddr, sport, daddr, hnum, dif);
  318. return sk ? : __inet_lookup_listener(net, hashinfo, daddr, hnum, dif);
  319. }
  320. static inline struct sock *inet_lookup(struct net *net,
  321. struct inet_hashinfo *hashinfo,
  322. const __be32 saddr, const __be16 sport,
  323. const __be32 daddr, const __be16 dport,
  324. const int dif)
  325. {
  326. struct sock *sk;
  327. local_bh_disable();
  328. sk = __inet_lookup(net, hashinfo, saddr, sport, daddr, dport, dif);
  329. local_bh_enable();
  330. return sk;
  331. }
  332. extern int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  333. struct sock *sk, u32 port_offset,
  334. int (*check_established)(struct inet_timewait_death_row *,
  335. struct sock *, __u16, struct inet_timewait_sock **),
  336. void (*hash)(struct sock *sk));
  337. extern int inet_hash_connect(struct inet_timewait_death_row *death_row,
  338. struct sock *sk);
  339. #endif /* _INET_HASHTABLES_H */