inet_hashtables.h 13 KB

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