inet_hashtables.h 14 KB

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