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
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
29 * $FreeBSD: src/sys/netinet/in_hostcache.c,v 1.3 1999/08/28 00:49:16 peter Exp $
30 * $DragonFly: src/sys/netinet/Attic/in_hostcache.c,v 1.5 2005/01/06 17:59:32 hsu Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
39 #include <net/hostcache.h>
41 #include <net/if_var.h>
42 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_hostcache.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcp_timer.h>
48 #include <netinet/tcp_var.h>
51 * Manage the IP per-host cache (really a thin veneer over the generic
52 * per-host cache code).
55 /* Look up an entry -- can be called from interrupt context. */
57 inhc_lookup(struct sockaddr_in *sin)
61 hc = hc_get((struct sockaddr *)sin);
62 return ((struct in_hcentry *)hc);
65 /* Look up and possibly create an entry -- must be called from user mode. */
67 inhc_alloc(struct sockaddr_in *sin)
69 struct in_hcentry *inhc;
72 /* xxx mutual exclusion for smp */
74 inhc = inhc_lookup(sin);
78 rt = rtlookup(inhc->inhc_hc.hc_host);
82 MALLOC(inhc, struct in_hcentry *, sizeof *inhc, M_HOSTCACHE, M_WAITOK);
83 bzero(inhc, sizeof *inhc);
84 inhc->inhc_hc.hc_host = dup_sockaddr((struct sockaddr *)sin);
85 if (in_broadcast(sin->sin_addr, rt->rt_ifp))
86 inhc->inhc_flags |= INHC_BROADCAST;
87 else if (((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr.s_addr
88 == sin->sin_addr.s_addr)
89 inhc->inhc_flags |= INHC_LOCAL;
90 else if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
91 inhc->inhc_flags |= INHC_MULTICAST;
92 inhc->inhc_pmtu = rt->rt_rmx.rmx_mtu;
93 inhc->inhc_recvpipe = rt->rt_rmx.rmx_recvpipe;
94 inhc->inhc_sendpipe = rt->rt_rmx.rmx_sendpipe;
95 inhc->inhc_ssthresh = rt->rt_rmx.rmx_ssthresh;
96 if (rt->rt_rmx.rmx_locks & RTV_RTT)
97 inhc->inhc_rttmin = rt->rt_rmx.rmx_rtt
98 / (RTM_RTTUNIT / TCP_RTT_SCALE);
99 inhc->inhc_hc.hc_rt = rt;
100 error = hc_insert(&inhc->inhc_hc);
103 FREE(inhc, M_HOSTCACHE);
107 * We don't return the structure directly because hc_get() needs
108 * to be allowed to do its own processing.
110 return (inhc_lookup(sin));
114 * This is Van Jacobson's hash function for IPv4 addresses.
115 * It is designed to work with a power-of-two-sized hash table.
118 inhc_hash(struct sockaddr *sa, u_long nbuckets)
122 ip = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
123 return ((ip ^ (ip >> 23) ^ (ip >> 17)) & ~(nbuckets - 1));
127 * We don't need to do any special work... if there are no references,
128 * as the caller has already ensured, then it's OK to kill.
131 inhc_delete(struct hcentry *hc)
137 * Return the next increment for the number of buckets in the hash table.
138 * Zero means ``do not bump''.
141 inhc_bump(u_long oldsize)
144 return (oldsize << 1);
148 static struct hccallback inhc_cb = {
149 inhc_hash, inhc_delete, inhc_bump
156 return (hc_init(AF_INET, &inhc_cb, 128, 0));