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
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_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
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.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/socket.h>
54 #include <sys/syslog.h>
55 #include <sys/globaldata.h>
56 #include <sys/thread2.h>
59 #include <net/route.h>
60 #include <net/if_var.h>
62 #include <net/if_types.h>
64 #include <net/netmsg2.h>
65 #include <net/netisr2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/ip_flow.h>
71 #define RTPRF_EXPIRING RTF_PROTO3 /* set on routes we manage */
73 struct in_rtqtimo_ctx {
74 struct callout timo_ch;
75 struct netmsg_base timo_nmsg;
76 struct radix_node_head *timo_rnh;
79 static void in_rtqtimo(void *);
81 static struct in_rtqtimo_ctx in_rtqtimo_context[MAXCPU];
82 static struct netmsg_base in_rtqdrain_netmsg[MAXCPU];
85 * Do what we need to do when inserting a route.
87 static struct radix_node *
88 in_addroute(char *key, char *mask, struct radix_node_head *head,
89 struct radix_node *treenodes)
91 struct rtentry *rt = (struct rtentry *)treenodes;
92 struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
93 struct radix_node *ret;
94 struct in_ifaddr_container *iac;
98 * For IP, 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).
102 * For IP, all unicast non-host routes are automatically cloning.
104 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
105 rt->rt_flags |= RTF_MULTICAST;
107 if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST)))
108 rt->rt_flags |= RTF_PRCLONING;
111 * For host routes, we make sure that RTF_BROADCAST
112 * is set for anything that looks like a broadcast address.
113 * This way, we can avoid an expensive call to in_broadcast()
114 * in ip_output() most of the time (because the route passed
115 * to ip_output() is almost always a host route).
117 * For local routes we set RTF_LOCAL allowing various shortcuts.
119 * A cloned network route will point to one of several possible
120 * addresses if an interface has aliases and must be repointed
121 * back to the correct address or arp_rtrequest() will not properly
122 * detect the local ip.
124 if (rt->rt_flags & RTF_HOST) {
125 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
126 rt->rt_flags |= RTF_BROADCAST;
127 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
128 sin->sin_addr.s_addr) {
129 rt->rt_flags |= RTF_LOCAL;
131 LIST_FOREACH(iac, INADDR_HASH(sin->sin_addr.s_addr),
134 if (sin->sin_addr.s_addr ==
135 ia->ia_addr.sin_addr.s_addr) {
136 rt->rt_flags |= RTF_LOCAL;
139 rt->rt_ifa = &ia->ia_ifa;
140 rt->rt_ifp = rt->rt_ifa->ifa_ifp;
147 if (rt->rt_rmx.rmx_mtu == 0 && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
149 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
151 ret = rn_addroute(key, mask, head, treenodes);
152 if (ret == NULL && (rt->rt_flags & RTF_HOST)) {
153 struct rtentry *oldrt;
156 * We are trying to add a host route, but can't.
157 * Find out if it is because of an ARP entry and
160 oldrt = rtpurelookup((struct sockaddr *)sin);
163 if ((oldrt->rt_flags & RTF_LLINFO) &&
164 (oldrt->rt_flags & RTF_HOST) &&
166 oldrt->rt_gateway->sa_family == AF_LINK) {
167 rtrequest(RTM_DELETE, rt_key(oldrt),
168 oldrt->rt_gateway, rt_mask(oldrt),
169 oldrt->rt_flags, NULL);
170 ret = rn_addroute(key, mask, head, treenodes);
176 * If the new route has been created successfully, and it is
177 * not a multicast/broadcast or cloned route, then we will
178 * have to flush the ipflow. Otherwise, we may end up using
183 (RTF_MULTICAST | RTF_BROADCAST | RTF_WASCLONED)) == 0) {
184 ipflow_flush_oncpu();
190 * This code is the inverse of in_closeroute: on first reference, if we
191 * were managing the route, stop doing so and set the expiration timer
194 static struct radix_node *
195 in_matchroute(char *key, struct radix_node_head *head)
197 struct radix_node *rn = rn_match(key, head);
198 struct rtentry *rt = (struct rtentry *)rn;
200 if (rt != NULL && rt->rt_refcnt == 0) { /* this is first reference */
201 if (rt->rt_flags & RTPRF_EXPIRING) {
202 rt->rt_flags &= ~RTPRF_EXPIRING;
203 rt->rt_rmx.rmx_expire = 0;
209 static int rtq_reallyold = 60*60; /* one hour is ``really old'' */
210 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
212 "Default expiration time on cloned routes");
214 static int rtq_minreallyold = 10; /* never automatically crank down to less */
215 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
216 &rtq_minreallyold , 0,
217 "Minimum time to attempt to hold onto cloned routes");
219 static int rtq_toomany = 128; /* 128 cached routes is ``too many'' */
220 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
221 &rtq_toomany , 0, "Upper limit on cloned routes");
224 * On last reference drop, mark the route as belong to us so that it can be
228 in_closeroute(struct radix_node *rn, struct radix_node_head *head)
230 struct rtentry *rt = (struct rtentry *)rn;
232 if (!(rt->rt_flags & RTF_UP))
233 return; /* prophylactic measures */
235 if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
238 if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_EXPIRING)) != RTF_WASCLONED)
242 * As requested by David Greenman:
243 * If rtq_reallyold is 0, just delete the route without
244 * waiting for a timeout cycle to kill it.
246 if (rtq_reallyold != 0) {
247 rt->rt_flags |= RTPRF_EXPIRING;
248 rt->rt_rmx.rmx_expire = time_uptime + rtq_reallyold;
251 * Remove route from the radix tree, but defer deallocation
252 * until we return to rtfree().
254 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
260 struct radix_node_head *rnh;
269 * Get rid of old routes. When draining, this deletes everything, even when
270 * the timeout is not expired yet. When updating, this makes sure that
271 * nothing has a timeout longer than the current value of rtq_reallyold.
274 in_rtqkill(struct radix_node *rn, void *rock)
276 struct rtqk_arg *ap = rock;
277 struct rtentry *rt = (struct rtentry *)rn;
280 if (rt->rt_flags & RTPRF_EXPIRING) {
282 if (ap->draining || rt->rt_rmx.rmx_expire <= time_uptime) {
283 if (rt->rt_refcnt > 0)
284 panic("rtqkill route really not free");
286 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
287 rt_mask(rt), rt->rt_flags, NULL);
289 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
294 (int)(rt->rt_rmx.rmx_expire - time_uptime) >
296 rt->rt_rmx.rmx_expire = time_uptime +
299 ap->nextstop = lmin(ap->nextstop,
300 rt->rt_rmx.rmx_expire);
307 #define RTQ_TIMEOUT 60*10 /* run no less than once every ten minutes */
308 static int rtq_timeout = RTQ_TIMEOUT;
312 * 'last_adjusted_timeout' and 'rtq_reallyold' are _not_ read-only, and
313 * could be changed by all CPUs. However, they are changed at so low
314 * frequency that we could ignore the cache trashing issue and take them
318 in_rtqtimo_dispatch(netmsg_t nmsg)
322 static time_t last_adjusted_timeout = 0;
323 struct in_rtqtimo_ctx *ctx = &in_rtqtimo_context[mycpuid];
324 struct radix_node_head *rnh = ctx->timo_rnh;
328 lwkt_replymsg(&nmsg->lmsg, 0);
331 arg.found = arg.killed = 0;
333 arg.nextstop = time_uptime + rtq_timeout;
334 arg.draining = arg.updating = 0;
335 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
338 * Attempt to be somewhat dynamic about this:
339 * If there are ``too many'' routes sitting around taking up space,
340 * then crank down the timeout, and see if we can't make some more
341 * go away. However, we make sure that we will never adjust more
342 * than once in rtq_timeout seconds, to keep from cranking down too
345 if ((arg.found - arg.killed > rtq_toomany) &&
346 (int)(time_uptime - last_adjusted_timeout) >= rtq_timeout &&
347 rtq_reallyold > rtq_minreallyold) {
348 rtq_reallyold = 2*rtq_reallyold / 3;
349 if (rtq_reallyold < rtq_minreallyold) {
350 rtq_reallyold = rtq_minreallyold;
353 last_adjusted_timeout = time_uptime;
355 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
358 arg.found = arg.killed = 0;
360 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
364 atv.tv_sec = arg.nextstop - time_uptime;
365 if ((int)atv.tv_sec < 1) { /* time shift safety */
367 arg.nextstop = time_uptime + atv.tv_sec;
369 if ((int)atv.tv_sec > rtq_timeout) { /* time shift safety */
370 atv.tv_sec = rtq_timeout;
371 arg.nextstop = time_uptime + atv.tv_sec;
373 callout_reset(&ctx->timo_ch, tvtohz_high(&atv), in_rtqtimo, NULL);
377 in_rtqtimo(void *arg __unused)
380 struct lwkt_msg *lmsg = &in_rtqtimo_context[cpuid].timo_nmsg.lmsg;
383 if (lmsg->ms_flags & MSGF_DONE)
384 lwkt_sendmsg_oncpu(netisr_cpuport(cpuid), lmsg);
389 in_rtqdrain_dispatch(netmsg_t nmsg)
391 struct radix_node_head *rnh = rt_tables[mycpuid][AF_INET];
396 lwkt_replymsg(&nmsg->lmsg, 0);
399 arg.found = arg.killed = 0;
404 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
408 in_rtqdrain_ipi(void *arg __unused)
411 struct lwkt_msg *msg = &in_rtqdrain_netmsg[cpu].lmsg;
414 if (msg->ms_flags & MSGF_DONE)
415 lwkt_sendmsg_oncpu(netisr_cpuport(cpu), msg);
424 CPUMASK_ASSBMASK(mask, ncpus);
425 CPUMASK_ANDMASK(mask, smp_active_mask);
426 if (CPUMASK_TESTNZERO(mask))
427 lwkt_send_ipiq_mask(mask, in_rtqdrain_ipi, NULL);
431 * Initialize our routing tree.
434 in_inithead(void **head, int off)
436 struct radix_node_head *rnh;
437 struct in_rtqtimo_ctx *ctx;
440 if (!rn_inithead(head, rn_cpumaskhead(cpuid), off))
443 if (head != (void **)&rt_tables[cpuid][AF_INET]) /* BOGUS! */
444 return 1; /* only do this for the real routing table */
447 rnh->rnh_addaddr = in_addroute;
448 rnh->rnh_matchaddr = in_matchroute;
449 rnh->rnh_close = in_closeroute;
451 ctx = &in_rtqtimo_context[cpuid];
453 callout_init_mp(&ctx->timo_ch);
454 netmsg_init(&ctx->timo_nmsg, NULL, &netisr_adone_rport, MSGF_PRIORITY,
455 in_rtqtimo_dispatch);
456 netmsg_init(&in_rtqdrain_netmsg[cpuid], NULL, &netisr_adone_rport,
457 MSGF_PRIORITY, in_rtqdrain_dispatch);
459 in_rtqtimo(NULL); /* kick off timeout first time */
464 * This zaps old routes when the interface goes down or interface
465 * address is deleted. In the latter case, it deletes static routes
466 * that point to this address. If we don't do this, we may end up
467 * using the old address in the future. The ones we always want to
468 * get rid of are things like ARP entries, since the user might down
469 * the interface, walk over to a completely different network, and
472 * in_ifadown() is typically called when an interface is being brought
473 * down. We must iterate through all per-cpu route tables and clean
476 struct in_ifadown_arg {
477 struct radix_node_head *rnh;
483 in_ifadownkill(struct radix_node *rn, void *xap)
485 struct in_ifadown_arg *ap = xap;
486 struct rtentry *rt = (struct rtentry *)rn;
489 if (rt->rt_ifa == ap->ifa &&
490 (ap->del || !(rt->rt_flags & RTF_STATIC))) {
492 * We need to disable the automatic prune that happens
493 * in this case in rtrequest() because it will blow
494 * away the pointers that rn_walktree() needs in order
495 * continue our descent. We will end up deleting all
496 * the routes that rtrequest() would have in any case,
497 * so that behavior is not needed there.
499 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
500 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
501 rt_mask(rt), rt->rt_flags, NULL);
503 log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
508 struct netmsg_ifadown {
509 struct netmsg_base base;
515 in_ifadown_dispatch(netmsg_t msg)
517 struct netmsg_ifadown *rmsg = (void *)msg;
518 struct radix_node_head *rnh;
519 struct ifaddr *ifa = rmsg->ifa;
520 struct in_ifadown_arg arg;
525 arg.rnh = rnh = rt_tables[cpu][AF_INET];
528 rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
529 ifa->ifa_flags &= ~IFA_ROUTE;
533 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
535 lwkt_replymsg(&rmsg->base.lmsg, 0);
539 in_ifadown_force(struct ifaddr *ifa, int delete)
541 struct netmsg_ifadown msg;
543 if (ifa->ifa_addr->sa_family != AF_INET)
547 * XXX individual requests are not independantly chained,
548 * which means that the per-cpu route tables will not be
549 * consistent in the middle of the operation. If routes
550 * related to the interface are manipulated while we are
551 * doing this the inconsistancy could trigger a panic.
553 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
554 in_ifadown_dispatch);
557 rt_domsg_global(&msg.base);
563 in_ifadown(struct ifaddr *ifa, int delete)
566 if (ifa->ifa_ifp->if_type == IFT_CARP)
569 return in_ifadown_force(ifa, delete);