]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_hostcache.c
0ff6a40705055a512ebc6c0f61c3b176c68b58be
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1997 Massachusetts Institute of Technology
25 * Permission to use, copy, modify, and distribute this software and
26 * its documentation for any purpose and without fee is hereby
27 * granted, provided that both the above copyright notice and this
28 * permission notice appear in all copies, that both the above
29 * copyright notice and this permission notice appear in all
30 * supporting documentation, and that the name of M.I.T. not be used
31 * in advertising or publicity pertaining to distribution of the
32 * software without specific, written prior permission. M.I.T. makes
33 * no representations about the suitability of this software for any
34 * purpose. It is provided "as is" without express or implied
37 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
38 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
41 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <net/hostcache.h>
60 #include <net/if_var.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_hostcache.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
70 * Manage the IP per-host cache (really a thin veneer over the generic
71 * per-host cache code).
74 /* Look up an entry -- can be called from interrupt context. */
76 inhc_lookup(struct sockaddr_in
*sin
)
80 hc
= hc_get((struct sockaddr
*)sin
);
81 return ((struct in_hcentry
*)hc
);
84 /* Look up and possibly create an entry -- must be called from user mode. */
86 inhc_alloc(struct sockaddr_in
*sin
)
88 struct in_hcentry
*inhc
;
91 /* xxx mutual exclusion for smp */
93 inhc
= inhc_lookup(sin
);
97 rt
= rtalloc1(inhc
->inhc_hc
.hc_host
, 1, 0);
101 MALLOC(inhc
, struct in_hcentry
*, sizeof *inhc
, M_HOSTCACHE
, M_WAITOK
);
104 bzero(inhc
, sizeof *inhc
);
105 inhc
->inhc_hc
.hc_host
= dup_sockaddr((struct sockaddr
*)sin
, 1);
106 if (in_broadcast(sin
->sin_addr
, rt
->rt_ifp
))
107 inhc
->inhc_flags
|= INHC_BROADCAST
;
108 else if (((struct sockaddr_in
*)rt
->rt_ifa
->ifa_addr
)->sin_addr
.s_addr
109 == sin
->sin_addr
.s_addr
)
110 inhc
->inhc_flags
|= INHC_LOCAL
;
111 else if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)))
112 inhc
->inhc_flags
|= INHC_MULTICAST
;
113 inhc
->inhc_pmtu
= rt
->rt_rmx
.rmx_mtu
;
114 inhc
->inhc_recvpipe
= rt
->rt_rmx
.rmx_recvpipe
;
115 inhc
->inhc_sendpipe
= rt
->rt_rmx
.rmx_sendpipe
;
116 inhc
->inhc_ssthresh
= rt
->rt_rmx
.rmx_ssthresh
;
117 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
118 inhc
->inhc_rttmin
= rt
->rt_rmx
.rmx_rtt
119 / (RTM_RTTUNIT
/ TCP_RTT_SCALE
);
120 inhc
->inhc_hc
.hc_rt
= rt
;
121 error
= hc_insert(&inhc
->inhc_hc
);
124 FREE(inhc
, M_HOSTCACHE
);
128 * We don't return the structure directly because hc_get() needs
129 * to be allowed to do its own processing.
131 return (inhc_lookup(sin
));
135 * This is Van Jacobson's hash function for IPv4 addresses.
136 * It is designed to work with a power-of-two-sized hash table.
139 inhc_hash(struct sockaddr
*sa
, u_long nbuckets
)
143 ip
= ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
144 return ((ip
^ (ip
>> 23) ^ (ip
>> 17)) & ~(nbuckets
- 1));
148 * We don't need to do any special work... if there are no references,
149 * as the caller has already ensured, then it's OK to kill.
152 inhc_delete(struct hcentry
*hc
)
158 * Return the next increment for the number of buckets in the hash table.
159 * Zero means ``do not bump''.
162 inhc_bump(u_long oldsize
)
165 return (oldsize
<< 1);
169 static struct hccallback inhc_cb
= {
170 inhc_hash
, inhc_delete
, inhc_bump
177 return (hc_init(AF_INET
, &inhc_cb
, 128, 0));