cxm_extract_fw.8: Small mdoc correction.
[dragonfly.git] / sys / netinet / in_rmx.c
blob9fb755c6a821c447169f68a8d0ae66cadef3953a
1 /*
2 * Copyright 1994, 1995 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
14 * warranty.
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.
29 * $FreeBSD: src/sys/netinet/in_rmx.c,v 1.37.2.3 2002/08/09 14:49:23 ru Exp $
30 * $DragonFly: src/sys/netinet/in_rmx.c,v 1.14 2006/04/11 06:59:34 dillon Exp $
34 * This code does two things necessary for the enhanced TCP metrics to
35 * function in a useful manner:
36 * 1) It marks all non-host routes as `cloning', thus ensuring that
37 * every actual reference to such a route actually gets turned
38 * into a reference to a host route to the specific destination
39 * requested.
40 * 2) When such routes lose all their references, it arranges for them
41 * to be deleted in some random collection of circumstances, so that
42 * a large quantity of stale routing data is not kept in kernel memory
43 * indefinitely. See in_rtqtimo() below for the exact mechanism.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/socket.h>
51 #include <sys/mbuf.h>
52 #include <sys/syslog.h>
53 #include <sys/globaldata.h>
54 #include <sys/thread2.h>
56 #include <net/if.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_flow.h>
63 #define RTPRF_EXPIRING RTF_PROTO3 /* set on routes we manage */
65 static struct callout in_rtqtimo_ch[MAXCPU];
68 * Do what we need to do when inserting a route.
70 static struct radix_node *
71 in_addroute(char *key, char *mask, struct radix_node_head *head,
72 struct radix_node *treenodes)
74 struct rtentry *rt = (struct rtentry *)treenodes;
75 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
76 struct radix_node *ret;
79 * For IP, all unicast non-host routes are automatically cloning.
81 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
82 rt->rt_flags |= RTF_MULTICAST;
84 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST)))
85 rt->rt_flags |= RTF_PRCLONING;
88 * A little bit of help for both IP output and input:
89 * For host routes, we make sure that RTF_BROADCAST
90 * is set for anything that looks like a broadcast address.
91 * This way, we can avoid an expensive call to in_broadcast()
92 * in ip_output() most of the time (because the route passed
93 * to ip_output() is almost always a host route).
95 * We also do the same for local addresses, with the thought
96 * that this might one day be used to speed up ip_input().
98 * We also mark routes to multicast addresses as such, because
99 * it's easy to do and might be useful (but this is much more
100 * dubious since it's so easy to inspect the address). (This
101 * is done above.)
103 if (rt->rt_flags & RTF_HOST) {
104 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
105 rt->rt_flags |= RTF_BROADCAST;
106 } else {
107 if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
108 == sin->sin_addr.s_addr)
109 rt->rt_flags |= RTF_LOCAL;
113 if (rt->rt_rmx.rmx_mtu != 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
114 rt->rt_ifp != NULL)
115 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
117 ret = rn_addroute(key, mask, head, treenodes);
118 if (ret == NULL && rt->rt_flags & RTF_HOST) {
119 struct rtentry *oldrt;
122 * We are trying to add a host route, but can't.
123 * Find out if it is because of an ARP entry and
124 * delete it if so.
126 oldrt = rtpurelookup((struct sockaddr *)sin);
127 if (oldrt != NULL) {
128 --oldrt->rt_refcnt;
129 if (oldrt->rt_flags & RTF_LLINFO &&
130 oldrt->rt_flags & RTF_HOST &&
131 oldrt->rt_gateway &&
132 oldrt->rt_gateway->sa_family == AF_LINK) {
133 rtrequest(RTM_DELETE, rt_key(oldrt),
134 oldrt->rt_gateway, rt_mask(oldrt),
135 oldrt->rt_flags, NULL);
136 ret = rn_addroute(key, mask, head, treenodes);
142 * If the new route has been created successfully, and it is
143 * not a multicast/broadcast or cloned route, then we will
144 * have to flush the ipflow. Otherwise, we may end up using
145 * the wrong route.
147 if (ret != NULL &&
148 (rt->rt_flags &
149 (RTF_MULTICAST | RTF_BROADCAST | RTF_WASCLONED)) == 0)
150 ipflow_flush_oncpu();
151 return ret;
155 * This code is the inverse of in_closeroute: on first reference, if we
156 * were managing the route, stop doing so and set the expiration timer
157 * back off again.
159 static struct radix_node *
160 in_matchroute(char *key, struct radix_node_head *head)
162 struct radix_node *rn = rn_match(key, head);
163 struct rtentry *rt = (struct rtentry *)rn;
165 if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
166 if (rt->rt_flags & RTPRF_EXPIRING) {
167 rt->rt_flags &= ~RTPRF_EXPIRING;
168 rt->rt_rmx.rmx_expire = 0;
171 return rn;
174 static int rtq_reallyold = 60*60; /* one hour is ``really old'' */
175 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
176 &rtq_reallyold , 0,
177 "Default expiration time on cloned routes");
179 static int rtq_minreallyold = 10; /* never automatically crank down to less */
180 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
181 &rtq_minreallyold , 0,
182 "Minimum time to attempt to hold onto cloned routes");
184 static int rtq_toomany = 128; /* 128 cached routes is ``too many'' */
185 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
186 &rtq_toomany , 0, "Upper limit on cloned routes");
189 * On last reference drop, mark the route as belong to us so that it can be
190 * timed out.
192 static void
193 in_closeroute(struct radix_node *rn, struct radix_node_head *head)
195 struct rtentry *rt = (struct rtentry *)rn;
197 if (!(rt->rt_flags & RTF_UP))
198 return; /* prophylactic measures */
200 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
201 return;
203 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_EXPIRING)) != RTF_WASCLONED)
204 return;
207 * As requested by David Greenman:
208 * If rtq_reallyold is 0, just delete the route without
209 * waiting for a timeout cycle to kill it.
211 if (rtq_reallyold != 0) {
212 rt->rt_flags |= RTPRF_EXPIRING;
213 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
214 } else {
216 * Remove route from the radix tree, but defer deallocation
217 * until we return to rtfree().
219 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
220 rt->rt_flags, &rt);
224 struct rtqk_arg {
225 struct radix_node_head *rnh;
226 int draining;
227 int killed;
228 int found;
229 int updating;
230 time_t nextstop;
234 * Get rid of old routes. When draining, this deletes everything, even when
235 * the timeout is not expired yet. When updating, this makes sure that
236 * nothing has a timeout longer than the current value of rtq_reallyold.
238 static int
239 in_rtqkill(struct radix_node *rn, void *rock)
241 struct rtqk_arg *ap = rock;
242 struct rtentry *rt = (struct rtentry *)rn;
243 int err;
245 if (rt->rt_flags & RTPRF_EXPIRING) {
246 ap->found++;
247 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
248 if (rt->rt_refcnt > 0)
249 panic("rtqkill route really not free");
251 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
252 rt_mask(rt), rt->rt_flags, NULL);
253 if (err)
254 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
255 else
256 ap->killed++;
257 } else {
258 if (ap->updating &&
259 (rt->rt_rmx.rmx_expire - time_second >
260 rtq_reallyold)) {
261 rt->rt_rmx.rmx_expire = time_second +
262 rtq_reallyold;
264 ap->nextstop = lmin(ap->nextstop,
265 rt->rt_rmx.rmx_expire);
269 return 0;
272 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
273 static int rtq_timeout = RTQ_TIMEOUT;
275 static void
276 in_rtqtimo(void *rock)
278 struct radix_node_head *rnh = rock;
279 struct rtqk_arg arg;
280 struct timeval atv;
281 static time_t last_adjusted_timeout = 0;
283 arg.found = arg.killed = 0;
284 arg.rnh = rnh;
285 arg.nextstop = time_second + rtq_timeout;
286 arg.draining = arg.updating = 0;
287 crit_enter();
288 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
289 crit_exit();
292 * Attempt to be somewhat dynamic about this:
293 * If there are ``too many'' routes sitting around taking up space,
294 * then crank down the timeout, and see if we can't make some more
295 * go away. However, we make sure that we will never adjust more
296 * than once in rtq_timeout seconds, to keep from cranking down too
297 * hard.
299 if ((arg.found - arg.killed > rtq_toomany) &&
300 (time_second - last_adjusted_timeout >= rtq_timeout) &&
301 rtq_reallyold > rtq_minreallyold) {
302 rtq_reallyold = 2*rtq_reallyold / 3;
303 if (rtq_reallyold < rtq_minreallyold) {
304 rtq_reallyold = rtq_minreallyold;
307 last_adjusted_timeout = time_second;
308 #ifdef DIAGNOSTIC
309 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
310 rtq_reallyold);
311 #endif
312 arg.found = arg.killed = 0;
313 arg.updating = 1;
314 crit_enter();
315 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
316 crit_exit();
319 atv.tv_usec = 0;
320 atv.tv_sec = arg.nextstop - time_second;
321 callout_reset(&in_rtqtimo_ch[mycpuid], tvtohz_high(&atv), in_rtqtimo,
322 rock);
325 void
326 in_rtqdrain(void)
328 struct radix_node_head *rnh = rt_tables[mycpuid][AF_INET];
329 struct rtqk_arg arg;
331 arg.found = arg.killed = 0;
332 arg.rnh = rnh;
333 arg.nextstop = 0;
334 arg.draining = 1;
335 arg.updating = 0;
336 crit_enter();
337 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
338 crit_exit();
342 * Initialize our routing tree.
345 in_inithead(void **head, int off)
347 struct radix_node_head *rnh;
349 if (!rn_inithead(head, off))
350 return 0;
352 if (head != (void **)&rt_tables[mycpuid][AF_INET]) /* BOGUS! */
353 return 1; /* only do this for the real routing table */
355 rnh = *head;
356 rnh->rnh_addaddr = in_addroute;
357 rnh->rnh_matchaddr = in_matchroute;
358 rnh->rnh_close = in_closeroute;
359 callout_init(&in_rtqtimo_ch[mycpuid]);
360 in_rtqtimo(rnh); /* kick off timeout first time */
361 return 1;
365 * This zaps old routes when the interface goes down or interface
366 * address is deleted. In the latter case, it deletes static routes
367 * that point to this address. If we don't do this, we may end up
368 * using the old address in the future. The ones we always want to
369 * get rid of are things like ARP entries, since the user might down
370 * the interface, walk over to a completely different network, and
371 * plug back in.
373 * in_ifadown() is typically called when an interface is being brought
374 * down. We must iterate through all per-cpu route tables and clean
375 * them up.
377 struct in_ifadown_arg {
378 struct radix_node_head *rnh;
379 struct ifaddr *ifa;
380 int del;
383 static int
384 in_ifadownkill(struct radix_node *rn, void *xap)
386 struct in_ifadown_arg *ap = xap;
387 struct rtentry *rt = (struct rtentry *)rn;
388 int err;
390 if (rt->rt_ifa == ap->ifa &&
391 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
393 * We need to disable the automatic prune that happens
394 * in this case in rtrequest() because it will blow
395 * away the pointers that rn_walktree() needs in order
396 * continue our descent. We will end up deleting all
397 * the routes that rtrequest() would have in any case,
398 * so that behavior is not needed there.
400 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
401 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
402 rt_mask(rt), rt->rt_flags, NULL);
403 if (err)
404 log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
406 return 0;
410 in_ifadown(struct ifaddr *ifa, int delete)
412 struct in_ifadown_arg arg;
413 struct radix_node_head *rnh;
414 int origcpu;
415 int cpu;
417 if (ifa->ifa_addr->sa_family != AF_INET)
418 return 1;
421 * XXX individual requests are not independantly chained,
422 * which means that the per-cpu route tables will not be
423 * consistent in the middle of the operation. If routes
424 * related to the interface are manipulated while we are
425 * doing this the inconsistancy could trigger a panic.
427 origcpu = mycpuid;
428 for (cpu = 0; cpu < ncpus2; cpu++) {
429 lwkt_migratecpu(cpu);
431 arg.rnh = rnh = rt_tables[cpu][AF_INET];
432 arg.ifa = ifa;
433 arg.del = delete;
434 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
435 ifa->ifa_flags &= ~IFA_ROUTE;
437 lwkt_migratecpu(origcpu);
438 return 0;