Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / netinet / in_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/netinet/in_hostcache.c,v 1.3 1999/08/28 00:49:16 peter Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37
38 #include <net/hostcache.h>
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42
43 #include <netinet/in.h>
44 #include <netinet/in_hostcache.h>
45 #include <netinet/tcp.h>
46 #include <netinet/tcp_timer.h>
47 #include <netinet/tcp_var.h>
48
49 /*
50  * Manage the IP per-host cache (really a thin veneer over the generic
51  * per-host cache code).
52  */
53
54 /* Look up an entry -- can be called from interrupt context. */
55 struct in_hcentry *
56 inhc_lookup(struct sockaddr_in *sin)
57 {
58         struct hcentry *hc;
59
60         hc = hc_get((struct sockaddr *)sin);
61         return ((struct in_hcentry *)hc);
62 }
63
64 /* Look up and possibly create an entry -- must be called from user mode. */
65 struct in_hcentry *
66 inhc_alloc(struct sockaddr_in *sin)
67 {
68         struct in_hcentry *inhc;
69         struct rtentry *rt;
70         int error;
71         /* xxx mutual exclusion for smp */
72
73         inhc = inhc_lookup(sin);
74         if (inhc != 0)
75                 return inhc;
76
77         rt = rtalloc1(inhc->inhc_hc.hc_host, 1, 0);
78         if (rt == 0)
79                 return 0;
80
81         MALLOC(inhc, struct in_hcentry *, sizeof *inhc, M_HOSTCACHE, M_WAITOK);
82         bzero(inhc, sizeof *inhc);
83         inhc->inhc_hc.hc_host = dup_sockaddr((struct sockaddr *)sin, 1);
84         if (in_broadcast(sin->sin_addr, rt->rt_ifp))
85                 inhc->inhc_flags |= INHC_BROADCAST;
86         else if (((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr.s_addr
87                 == sin->sin_addr.s_addr)
88                 inhc->inhc_flags |= INHC_LOCAL;
89         else if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
90                 inhc->inhc_flags |= INHC_MULTICAST;
91         inhc->inhc_pmtu = rt->rt_rmx.rmx_mtu;
92         inhc->inhc_recvpipe = rt->rt_rmx.rmx_recvpipe;
93         inhc->inhc_sendpipe = rt->rt_rmx.rmx_sendpipe;
94         inhc->inhc_ssthresh = rt->rt_rmx.rmx_ssthresh;
95         if (rt->rt_rmx.rmx_locks & RTV_RTT)
96                 inhc->inhc_rttmin = rt->rt_rmx.rmx_rtt
97                         / (RTM_RTTUNIT / TCP_RTT_SCALE);
98         inhc->inhc_hc.hc_rt = rt;
99         error = hc_insert(&inhc->inhc_hc);
100         if (error != 0) {
101                 RTFREE(rt);
102                 FREE(inhc, M_HOSTCACHE);
103                 return 0;
104         }
105         /*
106          * We don't return the structure directly because hc_get() needs
107          * to be allowed to do its own processing.
108          */
109         return (inhc_lookup(sin));
110 }
111
112 /*
113  * This is Van Jacobson's hash function for IPv4 addresses.
114  * It is designed to work with a power-of-two-sized hash table.
115  */
116 static u_long
117 inhc_hash(struct sockaddr *sa, u_long nbuckets)
118 {
119         u_long ip;
120
121         ip = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
122         return ((ip ^ (ip >> 23) ^ (ip >> 17)) & ~(nbuckets - 1));
123 }
124
125 /*
126  * We don't need to do any special work... if there are no references,
127  * as the caller has already ensured, then it's OK to kill.
128  */
129 static int
130 inhc_delete(struct hcentry *hc)
131 {
132         return 0;
133 }
134
135 /*
136  * Return the next increment for the number of buckets in the hash table.
137  * Zero means ``do not bump''.
138  */
139 static u_long
140 inhc_bump(u_long oldsize)
141 {
142         if (oldsize < 512)
143                 return (oldsize << 1);
144         return 0;
145 }
146
147 static struct hccallback inhc_cb = {
148         inhc_hash, inhc_delete, inhc_bump
149 };
150
151 int
152 inhc_init(void)
153 {
154
155         return (hc_init(AF_INET, &inhc_cb, 128, 0));
156 }
157