Use a normal malloc() for PCB allocations instead of the really aweful mbuf
[dragonfly.git] / sys / net / hostcache.c
1 /*
2  * Copyright 1997 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/net/hostcache.c,v 1.6.2.1 2002/04/14 21:41:48 luigi Exp $
30  * $DragonFly: src/sys/net/Attic/hostcache.c,v 1.2 2003/06/17 04:28:47 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38
39 #include <net/hostcache.h>
40 #include <net/route.h>
41
42 MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "per-host cache structure");
43
44 static  struct hctable hctable[AF_MAX];
45 static  int hc_timeout_interval = 120;
46 static  int hc_maxidle = 1800;
47
48 static  int cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2);
49 static  void hc_timeout(void *xhct);
50 static  void maybe_bump_hash(struct hctable *hct);
51
52 int
53 hc_init(int af, struct hccallback *hccb, int init_nelem, int primes)
54 {
55         struct hctable *hct;
56         struct hchead *heads;
57         u_long nelem;
58
59         hct = &hctable[af];
60         nelem = init_nelem;
61         if (hct->hct_nentries)
62                 return 0;
63
64         if (primes) {
65                 heads = phashinit(init_nelem, M_HOSTCACHE, &nelem);
66         } else {
67                 int i;
68                 MALLOC(heads, struct hchead *, nelem * sizeof *heads,
69                        M_HOSTCACHE, M_WAITOK);
70                 for (i = 0; i < nelem; i++) {
71                         LIST_INIT(&heads[i]);
72                 }
73         }
74         
75         hct->hct_heads = heads;
76         hct->hct_nentries = nelem;
77         hct->hct_primes = primes;
78         timeout(hc_timeout, hct, hc_timeout_interval * hz);
79         return 0;
80 }
81
82 struct hcentry *
83 hc_get(struct sockaddr *sa)
84 {
85         u_long hash;
86         struct hcentry *hc;
87         struct hctable *hct;
88         int s;
89
90         hct = &hctable[sa->sa_family];
91         if (hct->hct_nentries == 0)
92                 return 0;
93         hash = hct->hct_cb->hccb_hash(sa, hct->hct_nentries);
94         hc = hct->hct_heads[hash].lh_first;
95         for (; hc; hc = hc->hc_link.le_next) {
96                 if (cmpsa(hc->hc_host, sa) == 0)
97                         break;
98         }
99         if (hc == 0)
100                 return 0;
101         s = splnet();
102         if (hc->hc_rt && (hc->hc_rt->rt_flags & RTF_UP) == 0) {
103                 RTFREE(hc->hc_rt);
104                 hc->hc_rt = 0;
105         }
106         if (hc->hc_rt == 0) {
107                 hc->hc_rt = rtalloc1(hc->hc_host, 1, 0);
108         }
109         hc_ref(hc);
110         splx(s);
111         /* XXX move to front of list? */
112         return hc;
113 }
114
115 void
116 hc_ref(struct hcentry *hc)
117 {
118         int s = splnet();
119         if (hc->hc_refcnt++ == 0) {
120                 hc->hc_hct->hct_idle--;
121                 hc->hc_hct->hct_active++;
122         }
123         splx(s);
124 }
125
126 void
127 hc_rele(struct hcentry *hc)
128 {
129         int s = splnet();
130 #ifdef DIAGNOSTIC
131         printf("hc_rele: %p: negative refcnt!\n", (void *)hc);
132 #endif
133         hc->hc_refcnt--;
134         if (hc->hc_refcnt == 0) {
135                 hc->hc_hct->hct_idle++;
136                 hc->hc_hct->hct_active--;
137                 hc->hc_idlesince = mono_time; /* XXX right one? */
138         }
139         splx(s);
140 }
141
142 /*
143  * The user is expected to initialize hc_host with the address and everything
144  * else to the appropriate form of `0'.
145  */
146 int
147 hc_insert(struct hcentry *hc)
148 {
149         struct hcentry *hc2;
150         struct hctable *hct;
151         u_long hash;
152         int s;
153
154         hct = &hctable[hc->hc_host->sa_family];
155         hash = hct->hct_cb->hccb_hash(hc->hc_host, hct->hct_nentries);
156         
157         hc2 = hct->hct_heads[hash].lh_first;
158         for (; hc2; hc2 = hc2->hc_link.le_next) {
159                 if (cmpsa(hc2->hc_host, hc->hc_host) == 0)
160                         break;
161         }
162         if (hc2 != 0)
163                 return EEXIST;
164         hc->hc_hct = hct;
165         s = splnet();
166         LIST_INSERT_HEAD(&hct->hct_heads[hash], hc, hc_link);
167         hct->hct_idle++;
168         /*
169          * If the table is now more than 75% full, consider bumping it.
170          */
171         if (100 * (hct->hct_idle + hct->hct_active) > 75 * hct->hct_nentries)
172                 maybe_bump_hash(hct);
173         splx(s);
174         return 0;
175 }
176
177 /*
178  * It's not clear to me how much sense this makes as an external interface,
179  * since it is expected that the deletion will normally be handled by
180  * the cache timeout.
181  */
182 int
183 hc_delete(struct hcentry *hc)
184 {
185         struct hctable *hct;
186         int error, s;
187
188         if (hc->hc_refcnt > 0)
189                 return 0;
190
191         hct = hc->hc_hct;
192         error = hct->hct_cb->hccb_delete(hc);
193         if (error)
194                 return 0;
195
196         s = splnet();
197         LIST_REMOVE(hc, hc_link);
198         hc->hc_hct->hct_idle--;
199         splx(s);
200         free(hc, M_HOSTCACHE);
201         return 0;
202 }
203
204 static void
205 hc_timeout(void *xhct)
206 {
207         struct hcentry *hc;
208         struct hctable *hct;
209         int j, s;
210         time_t start;
211
212         hct = xhct;
213         start = mono_time.tv_sec; /* for simplicity */
214
215         if (hct->hct_idle == 0)
216                 return;
217         for (j = 0; j < hct->hct_nentries; j++) {
218                 for (hc = hct->hct_heads[j].lh_first; hc; 
219                      hc = hc->hc_link.le_next) {
220                         if (hc->hc_refcnt > 0)
221                                 continue;
222                         if (hc->hc_idlesince.tv_sec + hc_maxidle <= start) {
223                                 if (hct->hct_cb->hccb_delete(hc))
224                                         continue;
225                                 s = splnet();
226                                 LIST_REMOVE(hc, hc_link);
227                                 hct->hct_idle--;
228                                 splx(s);
229                         }
230                 }
231         }
232         /*
233          * Fiddle something here based on tot_idle...
234          */
235         timeout(hc_timeout, xhct, hc_timeout_interval * hz);
236 }
237
238 static int
239 cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2)
240 {
241         if (sa1->sa_len != sa2->sa_len)
242                 return ((int)sa1->sa_len - sa2->sa_len);
243         return bcmp(sa1, sa2, sa1->sa_len);
244 }
245
246 static void
247 maybe_bump_hash(struct hctable *hct)
248 {
249         ;                       /* XXX fill me in */
250 }