FreeBSD/Linux Kernel Cross Reference
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$
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37
38 #include <net/hostcache.h>
39 #include <net/route.h>
40
41 MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "per-host cache structure");
42
43 static struct hctable hctable[AF_MAX];
44 static int hc_timeout_interval = 120;
45 static int hc_maxidle = 1800;
46
47 static int cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2);
48 static void hc_timeout(void *xhct);
49 static void maybe_bump_hash(struct hctable *hct);
50
51 int
52 hc_init(int af, struct hccallback *hccb, int init_nelem, int primes)
53 {
54 struct hctable *hct;
55 struct hchead *heads;
56 u_long nelem;
57
58 hct = &hctable[af];
59 nelem = init_nelem;
60 if (hct->hct_nentries)
61 return 0;
62
63 if (primes) {
64 heads = phashinit(init_nelem, M_HOSTCACHE, &nelem);
65 } else {
66 int i;
67 MALLOC(heads, struct hchead *, nelem * sizeof *heads,
68 M_HOSTCACHE, M_WAITOK);
69 for (i = 0; i < nelem; i++) {
70 LIST_INIT(&heads[i]);
71 }
72 }
73
74 hct->hct_heads = heads;
75 hct->hct_nentries = nelem;
76 hct->hct_primes = primes;
77 timeout(hc_timeout, hct, hc_timeout_interval * hz);
78 return 0;
79 }
80
81 struct hcentry *
82 hc_get(struct sockaddr *sa)
83 {
84 u_long hash;
85 struct hcentry *hc;
86 struct hctable *hct;
87 int s;
88
89 hct = &hctable[sa->sa_family];
90 if (hct->hct_nentries == 0)
91 return 0;
92 hash = hct->hct_cb->hccb_hash(sa, hct->hct_nentries);
93 hc = hct->hct_heads[hash].lh_first;
94 for (; hc; hc = hc->hc_link.le_next) {
95 if (cmpsa(hc->hc_host, sa) == 0)
96 break;
97 }
98 if (hc == 0)
99 return 0;
100 s = splnet();
101 if (hc->hc_rt && (hc->hc_rt->rt_flags & RTF_UP) == 0) {
102 RTFREE(hc->hc_rt);
103 hc->hc_rt = 0;
104 }
105 if (hc->hc_rt == 0) {
106 hc->hc_rt = rtalloc1(hc->hc_host, 1, 0);
107 }
108 hc_ref(hc);
109 splx(s);
110 /* XXX move to front of list? */
111 return hc;
112 }
113
114 void
115 hc_ref(struct hcentry *hc)
116 {
117 int s = splnet();
118 if (hc->hc_refcnt++ == 0) {
119 hc->hc_hct->hct_idle--;
120 hc->hc_hct->hct_active++;
121 }
122 splx(s);
123 }
124
125 void
126 hc_rele(struct hcentry *hc)
127 {
128 int s = splnet();
129 #ifdef DIAGNOSTIC
130 printf("hc_rele: %p: negative refcnt!\n", (void *)hc);
131 #endif
132 hc->hc_refcnt--;
133 if (hc->hc_refcnt == 0) {
134 hc->hc_hct->hct_idle++;
135 hc->hc_hct->hct_active--;
136 hc->hc_idlesince = mono_time; /* XXX right one? */
137 }
138 splx(s);
139 }
140
141 /*
142 * The user is expected to initialize hc_host with the address and everything
143 * else to the appropriate form of `0'.
144 */
145 int
146 hc_insert(struct hcentry *hc)
147 {
148 struct hcentry *hc2;
149 struct hctable *hct;
150 u_long hash;
151 int s;
152
153 hct = &hctable[hc->hc_host->sa_family];
154 hash = hct->hct_cb->hccb_hash(hc->hc_host, hct->hct_nentries);
155
156 hc2 = hct->hct_heads[hash].lh_first;
157 for (; hc2; hc2 = hc2->hc_link.le_next) {
158 if (cmpsa(hc2->hc_host, hc->hc_host) == 0)
159 break;
160 }
161 if (hc2 != 0)
162 return EEXIST;
163 hc->hc_hct = hct;
164 s = splnet();
165 LIST_INSERT_HEAD(&hct->hct_heads[hash], hc, hc_link);
166 hct->hct_idle++;
167 /*
168 * If the table is now more than 75% full, consider bumping it.
169 */
170 if (100 * (hct->hct_idle + hct->hct_active) > 75 * hct->hct_nentries)
171 maybe_bump_hash(hct);
172 splx(s);
173 return 0;
174 }
175
176 /*
177 * It's not clear to me how much sense this makes as an external interface,
178 * since it is expected that the deletion will normally be handled by
179 * the cache timeout.
180 */
181 int
182 hc_delete(struct hcentry *hc)
183 {
184 struct hctable *hct;
185 int error, s;
186
187 if (hc->hc_refcnt > 0)
188 return 0;
189
190 hct = hc->hc_hct;
191 error = hct->hct_cb->hccb_delete(hc);
192 if (error)
193 return 0;
194
195 s = splnet();
196 LIST_REMOVE(hc, hc_link);
197 hc->hc_hct->hct_idle--;
198 splx(s);
199 free(hc, M_HOSTCACHE);
200 return 0;
201 }
202
203 static void
204 hc_timeout(void *xhct)
205 {
206 struct hcentry *hc;
207 struct hctable *hct;
208 int j, s;
209 time_t start;
210
211 hct = xhct;
212 start = mono_time.tv_sec; /* for simplicity */
213
214 if (hct->hct_idle == 0)
215 return;
216 for (j = 0; j < hct->hct_nentries; j++) {
217 for (hc = hct->hct_heads[j].lh_first; hc;
218 hc = hc->hc_link.le_next) {
219 if (hc->hc_refcnt > 0)
220 continue;
221 if (hc->hc_idlesince.tv_sec + hc_maxidle <= start) {
222 if (hct->hct_cb->hccb_delete(hc))
223 continue;
224 s = splnet();
225 LIST_REMOVE(hc, hc_link);
226 hct->hct_idle--;
227 splx(s);
228 }
229 }
230 }
231 /*
232 * Fiddle something here based on tot_idle...
233 */
234 timeout(hc_timeout, xhct, hc_timeout_interval * hz);
235 }
236
237 static int
238 cmpsa(const struct sockaddr *sa1, const struct sockaddr *sa2)
239 {
240 if (sa1->sa_len != sa2->sa_len)
241 return ((int)sa1->sa_len - sa2->sa_len);
242 return bcmp(sa1, sa2, sa1->sa_len);
243 }
244
245 static void
246 maybe_bump_hash(struct hctable *hct)
247 {
248 ; /* XXX fill me in */
249 }
Cache object: b13e77c738666ca21cf0b5e58520d1da
|