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