inet_hashtables.h 5.4 KB

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