inet_hashtables.h 6.3 KB

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