blob: 06cbc6f689c5d161bab70c2dfecd1100428729c4 [file] [log] [blame]
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -07001/*
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 * Generic INET transport hashtables
7 *
8 * Authors: Lotsa people, from code originally in tcp
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#include <linux/config.h>
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070017#include <linux/module.h>
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070018#include <linux/sched.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070019#include <linux/slab.h>
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070020#include <linux/wait.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070021
22#include <net/inet_hashtables.h>
23
24/*
25 * Allocate and initialize a new local port bind bucket.
26 * The bindhash mutex for snum's hash chain must be held here.
27 */
28struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
29 struct inet_bind_hashbucket *head,
30 const unsigned short snum)
31{
32 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
33
34 if (tb != NULL) {
35 tb->port = snum;
36 tb->fastreuse = 0;
37 INIT_HLIST_HEAD(&tb->owners);
38 hlist_add_head(&tb->node, &head->chain);
39 }
40 return tb;
41}
42
43EXPORT_SYMBOL(inet_bind_bucket_create);
44
45/*
46 * Caller must hold hashbucket lock for this tb with local BH disabled
47 */
48void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
49{
50 if (hlist_empty(&tb->owners)) {
51 __hlist_del(&tb->node);
52 kmem_cache_free(cachep, tb);
53 }
54}
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070055
56void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
57 const unsigned short snum)
58{
59 struct inet_sock *inet = inet_sk(sk);
60 inet->num = snum;
61 sk_add_bind_node(sk, &tb->owners);
62 inet->bind_hash = tb;
63}
64
65EXPORT_SYMBOL(inet_bind_hash);
66
67/*
68 * Get rid of any references to a local port held by the given sock.
69 */
70static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
71{
72 struct inet_sock *inet = inet_sk(sk);
73 const int bhash = inet_bhashfn(inet->num, hashinfo->bhash_size);
74 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
75 struct inet_bind_bucket *tb;
76
77 spin_lock(&head->lock);
78 tb = inet->bind_hash;
79 __sk_del_bind_node(sk);
80 inet->bind_hash = NULL;
81 inet->num = 0;
82 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
83 spin_unlock(&head->lock);
84}
85
86void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
87{
88 local_bh_disable();
89 __inet_put_port(hashinfo, sk);
90 local_bh_enable();
91}
92
93EXPORT_SYMBOL(inet_put_port);
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070094
95/*
96 * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
97 * Look, when several writers sleep and reader wakes them up, all but one
98 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
99 * this, _but_ remember, it adds useless work on UP machines (wake up each
100 * exclusive lock release). It should be ifdefed really.
101 */
102void inet_listen_wlock(struct inet_hashinfo *hashinfo)
103{
104 write_lock(&hashinfo->lhash_lock);
105
106 if (atomic_read(&hashinfo->lhash_users)) {
107 DEFINE_WAIT(wait);
108
109 for (;;) {
110 prepare_to_wait_exclusive(&hashinfo->lhash_wait,
111 &wait, TASK_UNINTERRUPTIBLE);
112 if (!atomic_read(&hashinfo->lhash_users))
113 break;
114 write_unlock_bh(&hashinfo->lhash_lock);
115 schedule();
116 write_lock_bh(&hashinfo->lhash_lock);
117 }
118
119 finish_wait(&hashinfo->lhash_wait, &wait);
120 }
121}
122
123EXPORT_SYMBOL(inet_listen_wlock);