inet_hashtables.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/ip.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/tcp.h> /* only for TCP_LISTEN, damn :-( */
  21. #include <linux/types.h>
  22. #include <linux/wait.h>
  23. #include <net/sock.h>
  24. #include <asm/atomic.h>
  25. /* This is for all connections with a full identity, no wildcards.
  26. * New scheme, half the table is for TIME_WAIT, the other half is
  27. * for the rest. I'll experiment with dynamic table growth later.
  28. */
  29. struct inet_ehash_bucket {
  30. rwlock_t lock;
  31. struct hlist_head chain;
  32. } __attribute__((__aligned__(8)));
  33. /* There are a few simple rules, which allow for local port reuse by
  34. * an application. In essence:
  35. *
  36. * 1) Sockets bound to different interfaces may share a local port.
  37. * Failing that, goto test 2.
  38. * 2) If all sockets have sk->sk_reuse set, and none of them are in
  39. * TCP_LISTEN state, the port may be shared.
  40. * Failing that, goto test 3.
  41. * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
  42. * address, and none of them are the same, the port may be
  43. * shared.
  44. * Failing this, the port cannot be shared.
  45. *
  46. * The interesting point, is test #2. This is what an FTP server does
  47. * all day. To optimize this case we use a specific flag bit defined
  48. * below. As we add sockets to a bind bucket list, we perform a
  49. * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
  50. * As long as all sockets added to a bind bucket pass this test,
  51. * the flag bit will be set.
  52. * The resulting situation is that tcp_v[46]_verify_bind() can just check
  53. * for this flag bit, if it is set and the socket trying to bind has
  54. * sk->sk_reuse set, we don't even have to walk the owners list at all,
  55. * we return that it is ok to bind this socket to the requested local port.
  56. *
  57. * Sounds like a lot of work, but it is worth it. In a more naive
  58. * implementation (ie. current FreeBSD etc.) the entire list of ports
  59. * must be walked for each data port opened by an ftp server. Needless
  60. * to say, this does not scale at all. With a couple thousand FTP
  61. * users logged onto your box, isn't it nice to know that new data
  62. * ports are created in O(1) time? I thought so. ;-) -DaveM
  63. */
  64. struct inet_bind_bucket {
  65. unsigned short port;
  66. signed short fastreuse;
  67. struct hlist_node node;
  68. struct hlist_head owners;
  69. };
  70. #define inet_bind_bucket_for_each(tb, node, head) \
  71. hlist_for_each_entry(tb, node, head, node)
  72. struct inet_bind_hashbucket {
  73. spinlock_t lock;
  74. struct hlist_head chain;
  75. };
  76. /* This is for listening sockets, thus all sockets which possess wildcards. */
  77. #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
  78. struct inet_hashinfo {
  79. /* This is for sockets with full identity only. Sockets here will
  80. * always be without wildcards and will have the following invariant:
  81. *
  82. * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
  83. *
  84. * First half of the table is for sockets not in TIME_WAIT, second half
  85. * is for TIME_WAIT sockets only.
  86. */
  87. struct inet_ehash_bucket *ehash;
  88. /* Ok, let's try this, I give up, we do need a local binding
  89. * TCP hash as well as the others for fast bind/connect.
  90. */
  91. struct inet_bind_hashbucket *bhash;
  92. int bhash_size;
  93. int ehash_size;
  94. /* All sockets in TCP_LISTEN state will be in here. This is the only
  95. * table where wildcard'd TCP sockets can exist. Hash function here
  96. * is just local port number.
  97. */
  98. struct hlist_head listening_hash[INET_LHTABLE_SIZE];
  99. /* All the above members are written once at bootup and
  100. * never written again _or_ are predominantly read-access.
  101. *
  102. * Now align to a new cache line as all the following members
  103. * are often dirty.
  104. */
  105. rwlock_t lhash_lock ____cacheline_aligned;
  106. atomic_t lhash_users;
  107. wait_queue_head_t lhash_wait;
  108. spinlock_t portalloc_lock;
  109. kmem_cache_t *bind_bucket_cachep;
  110. int port_rover;
  111. };
  112. static inline int inet_ehashfn(const __u32 laddr, const __u16 lport,
  113. const __u32 faddr, const __u16 fport,
  114. const int ehash_size)
  115. {
  116. int h = (laddr ^ lport) ^ (faddr ^ fport);
  117. h ^= h >> 16;
  118. h ^= h >> 8;
  119. return h & (ehash_size - 1);
  120. }
  121. static inline int inet_sk_ehashfn(const struct sock *sk, const int ehash_size)
  122. {
  123. const struct inet_sock *inet = inet_sk(sk);
  124. const __u32 laddr = inet->rcv_saddr;
  125. const __u16 lport = inet->num;
  126. const __u32 faddr = inet->daddr;
  127. const __u16 fport = inet->dport;
  128. return inet_ehashfn(laddr, lport, faddr, fport, ehash_size);
  129. }
  130. extern struct inet_bind_bucket *
  131. inet_bind_bucket_create(kmem_cache_t *cachep,
  132. struct inet_bind_hashbucket *head,
  133. const unsigned short snum);
  134. extern void inet_bind_bucket_destroy(kmem_cache_t *cachep,
  135. struct inet_bind_bucket *tb);
  136. static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
  137. {
  138. return lport & (bhash_size - 1);
  139. }
  140. extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  141. const unsigned short snum);
  142. /* These can have wildcards, don't try too hard. */
  143. static inline int inet_lhashfn(const unsigned short num)
  144. {
  145. return num & (INET_LHTABLE_SIZE - 1);
  146. }
  147. static inline int inet_sk_listen_hashfn(const struct sock *sk)
  148. {
  149. return inet_lhashfn(inet_sk(sk)->num);
  150. }
  151. /* Caller must disable local BH processing. */
  152. static inline void __inet_inherit_port(struct inet_hashinfo *table,
  153. struct sock *sk, struct sock *child)
  154. {
  155. const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size);
  156. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  157. struct inet_bind_bucket *tb;
  158. spin_lock(&head->lock);
  159. tb = inet_sk(sk)->bind_hash;
  160. sk_add_bind_node(child, &tb->owners);
  161. inet_sk(child)->bind_hash = tb;
  162. spin_unlock(&head->lock);
  163. }
  164. static inline void inet_inherit_port(struct inet_hashinfo *table,
  165. struct sock *sk, struct sock *child)
  166. {
  167. local_bh_disable();
  168. __inet_inherit_port(table, sk, child);
  169. local_bh_enable();
  170. }
  171. extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
  172. extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
  173. /*
  174. * - We may sleep inside this lock.
  175. * - If sleeping is not required (or called from BH),
  176. * use plain read_(un)lock(&inet_hashinfo.lhash_lock).
  177. */
  178. static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
  179. {
  180. /* read_lock synchronizes to candidates to writers */
  181. read_lock(&hashinfo->lhash_lock);
  182. atomic_inc(&hashinfo->lhash_users);
  183. read_unlock(&hashinfo->lhash_lock);
  184. }
  185. static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
  186. {
  187. if (atomic_dec_and_test(&hashinfo->lhash_users))
  188. wake_up(&hashinfo->lhash_wait);
  189. }
  190. static inline void __inet_hash(struct inet_hashinfo *hashinfo,
  191. struct sock *sk, const int listen_possible)
  192. {
  193. struct hlist_head *list;
  194. rwlock_t *lock;
  195. BUG_TRAP(sk_unhashed(sk));
  196. if (listen_possible && sk->sk_state == TCP_LISTEN) {
  197. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  198. lock = &hashinfo->lhash_lock;
  199. inet_listen_wlock(hashinfo);
  200. } else {
  201. sk->sk_hashent = inet_sk_ehashfn(sk, hashinfo->ehash_size);
  202. list = &hashinfo->ehash[sk->sk_hashent].chain;
  203. lock = &hashinfo->ehash[sk->sk_hashent].lock;
  204. write_lock(lock);
  205. }
  206. __sk_add_node(sk, list);
  207. sock_prot_inc_use(sk->sk_prot);
  208. write_unlock(lock);
  209. if (listen_possible && sk->sk_state == TCP_LISTEN)
  210. wake_up(&hashinfo->lhash_wait);
  211. }
  212. #endif /* _INET_HASHTABLES_H */