Abort if the device doesn't exist.
[dragonfly.git] / sys / net / hostcache.c
bloba49a46e8c87c2e7c19c70cad9415b10073fe27d3
1 /*
2 * Copyright 1997 Massachusetts Institute of Technology
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.
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.
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.8 2006/09/05 00:55:46 dillon Exp $
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 #include <sys/thread2.h>
40 #include <net/hostcache.h>
41 #include <net/route.h>
43 MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "per-host cache structure");
45 static struct hctable hctable[AF_MAX];
46 static int hc_timeout_interval = 120;
47 static int hc_maxidle = 1800;
48 static struct callout hc_timeout_h;
50 static int cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2);
51 static void hc_timeout(void *xhct);
52 static void maybe_bump_hash(struct hctable *hct);
54 int
55 hc_init(int af, struct hccallback *hccb, int init_nelem, int primes)
57 struct hctable *hct;
58 struct hchead *heads;
59 u_long nelem;
61 hct = &hctable[af];
62 nelem = init_nelem;
63 if (hct->hct_nentries)
64 return 0;
66 if (primes) {
67 heads = phashinit(init_nelem, M_HOSTCACHE, &nelem);
68 } else {
69 int i;
70 MALLOC(heads, struct hchead *, nelem * sizeof *heads,
71 M_HOSTCACHE, M_WAITOK);
72 for (i = 0; i < nelem; i++) {
73 LIST_INIT(&heads[i]);
77 hct->hct_heads = heads;
78 hct->hct_nentries = nelem;
79 hct->hct_primes = primes;
80 callout_init(&hc_timeout_h);
81 callout_reset(&hc_timeout_h, hc_timeout_interval * hz, hc_timeout, hct);
82 return 0;
85 struct hcentry *
86 hc_get(struct sockaddr *sa)
88 u_long hash;
89 struct hcentry *hc;
90 struct hctable *hct;
92 hct = &hctable[sa->sa_family];
93 if (hct->hct_nentries == 0)
94 return 0;
95 hash = hct->hct_cb->hccb_hash(sa, hct->hct_nentries);
96 hc = hct->hct_heads[hash].lh_first;
97 for (; hc; hc = hc->hc_link.le_next) {
98 if (cmpsa(hc->hc_host, sa) == 0)
99 break;
101 if (hc == NULL)
102 return NULL;
103 crit_enter();
104 if (hc->hc_rt && !(hc->hc_rt->rt_flags & RTF_UP)) {
105 RTFREE(hc->hc_rt);
106 hc->hc_rt = NULL;
108 if (hc->hc_rt == NULL) {
109 hc->hc_rt = rtlookup(hc->hc_host);
111 hc_ref(hc);
112 crit_exit();
113 /* XXX move to front of list? */
114 return hc;
117 void
118 hc_ref(struct hcentry *hc)
120 crit_enter();
121 if (hc->hc_refcnt++ == 0) {
122 hc->hc_hct->hct_idle--;
123 hc->hc_hct->hct_active++;
125 crit_exit();
128 void
129 hc_rele(struct hcentry *hc)
131 crit_enter();
132 #ifdef DIAGNOSTIC
133 printf("hc_rele: %p: negative refcnt!\n", (void *)hc);
134 #endif
135 hc->hc_refcnt--;
136 if (hc->hc_refcnt == 0) {
137 hc->hc_hct->hct_idle++;
138 hc->hc_hct->hct_active--;
139 hc->hc_idlesince = mono_time; /* XXX right one? */
141 crit_exit();
145 * The user is expected to initialize hc_host with the address and everything
146 * else to the appropriate form of `0'.
149 hc_insert(struct hcentry *hc)
151 struct hcentry *hc2;
152 struct hctable *hct;
153 u_long hash;
155 hct = &hctable[hc->hc_host->sa_family];
156 hash = hct->hct_cb->hccb_hash(hc->hc_host, hct->hct_nentries);
158 hc2 = hct->hct_heads[hash].lh_first;
159 for (; hc2; hc2 = hc2->hc_link.le_next) {
160 if (cmpsa(hc2->hc_host, hc->hc_host) == 0)
161 break;
163 if (hc2 != NULL)
164 return EEXIST;
165 hc->hc_hct = hct;
166 crit_enter();
167 LIST_INSERT_HEAD(&hct->hct_heads[hash], hc, hc_link);
168 hct->hct_idle++;
170 * If the table is now more than 75% full, consider bumping it.
172 if (100 * (hct->hct_idle + hct->hct_active) > 75 * hct->hct_nentries)
173 maybe_bump_hash(hct);
174 crit_exit();
175 return 0;
179 * It's not clear to me how much sense this makes as an external interface,
180 * since it is expected that the deletion will normally be handled by
181 * the cache timeout.
184 hc_delete(struct hcentry *hc)
186 struct hctable *hct;
187 int error;
189 if (hc->hc_refcnt > 0)
190 return 0;
192 hct = hc->hc_hct;
193 error = hct->hct_cb->hccb_delete(hc);
194 if (error)
195 return 0;
197 crit_enter();
198 LIST_REMOVE(hc, hc_link);
199 hc->hc_hct->hct_idle--;
200 crit_exit();
201 kfree(hc, M_HOSTCACHE);
202 return 0;
205 static void
206 hc_timeout(void *xhct)
208 struct hcentry *hc;
209 struct hctable *hct;
210 int j;
211 time_t start;
213 hct = xhct;
214 start = mono_time.tv_sec; /* for simplicity */
216 if (hct->hct_idle == 0)
217 return;
218 for (j = 0; j < hct->hct_nentries; j++) {
219 for (hc = hct->hct_heads[j].lh_first; hc;
220 hc = hc->hc_link.le_next) {
221 if (hc->hc_refcnt > 0)
222 continue;
223 if (hc->hc_idlesince.tv_sec + hc_maxidle <= start) {
224 if (hct->hct_cb->hccb_delete(hc))
225 continue;
226 crit_enter();
227 LIST_REMOVE(hc, hc_link);
228 hct->hct_idle--;
229 crit_exit();
234 * Fiddle something here based on tot_idle...
236 callout_reset(&hc_timeout_h, hc_timeout_interval * hz,
237 hc_timeout, xhct);
240 static int
241 cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2)
243 if (sa1->sa_len != sa2->sa_len)
244 return ((int)sa1->sa_len - sa2->sa_len);
245 return bcmp(sa1, sa2, sa1->sa_len);
248 static void
249 maybe_bump_hash(struct hctable *hct)
251 ; /* XXX fill me in */