inet_hashtables.h 13 KB

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