xref: /freebsd/sys/netinet/in_rmx.c (revision d82e286489da73321a47e329d98a98817b0438b6)
1 /*
2  * Copyright 1994, 1995 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  * $Id: in_rmx.c,v 1.16 1995/09/18 15:51:32 wollman Exp $
30  */
31 
32 /*
33  * This code does two things necessary for the enhanced TCP metrics to
34  * function in a useful manner:
35  *  1) It marks all non-host routes as `cloning', thus ensuring that
36  *     every actual reference to such a route actually gets turned
37  *     into a reference to a host route to the specific destination
38  *     requested.
39  *  2) When such routes lose all their references, it arranges for them
40  *     to be deleted in some random collection of circumstances, so that
41  *     a large quantity of stale routing data is not kept in kernel memory
42  *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/mbuf.h>
52 #include <sys/syslog.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_seq.h>
65 #include <netinet/tcp_timer.h>
66 #include <netinet/tcp_var.h>
67 #ifndef MTUDISC
68 #include <netinet/tcpip.h>
69 #endif /* not MTUDISC */
70 
71 #define RTPRF_OURS		RTF_PROTO3	/* set on routes we manage */
72 
73 /*
74  * Do what we need to do when inserting a route.
75  */
76 static struct radix_node *
77 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
78 	    struct radix_node *treenodes)
79 {
80 	struct rtentry *rt = (struct rtentry *)treenodes;
81 	struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
82 
83 	/*
84 	 * For IP, all unicast non-host routes are automatically cloning.
85 	 */
86 	if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING))) {
87 		if(!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
88 			rt->rt_flags |= RTF_PRCLONING;
89 		}
90 	}
91 
92 	/*
93 	 * We also specify a send and receive pipe size for every
94 	 * route added, to help TCP a bit.  TCP doesn't actually
95 	 * want a true pipe size, which would be prohibitive in memory
96 	 * costs and is hard to compute anyway; it simply uses these
97 	 * values to size its buffers.  So, we fill them in with the
98 	 * same values that TCP would have used anyway, and allow the
99 	 * installing program or the link layer to override these values
100 	 * as it sees fit.  This will hopefully allow TCP more
101 	 * opportunities to save its ssthresh value.
102 	 */
103 	if (!rt->rt_rmx.rmx_sendpipe && !(rt->rt_rmx.rmx_locks & RTV_SPIPE))
104 		rt->rt_rmx.rmx_sendpipe = tcp_sendspace;
105 
106 	if (!rt->rt_rmx.rmx_recvpipe && !(rt->rt_rmx.rmx_locks & RTV_RPIPE))
107 		rt->rt_rmx.rmx_recvpipe = tcp_recvspace;
108 
109 #ifndef MTUDISC
110 	/*
111 	 * Finally, set an MTU, again duplicating logic in TCP.
112 	 * The in_localaddr() business will go away when we have
113 	 * proper PMTU discovery.
114 	 */
115 #endif /* not MTUDISC */
116 	if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU)
117 	    && rt->rt_ifp)
118 #ifndef MTUDISC
119 		rt->rt_rmx.rmx_mtu = (in_localaddr(sin->sin_addr)
120 				      ? rt->rt_ifp->if_mtu
121 				      : tcp_mssdflt + sizeof(struct tcpiphdr));
122 #else /* MTUDISC */
123 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
124 #endif /* MTUDISC */
125 
126 	return rn_addroute(v_arg, n_arg, head, treenodes);
127 }
128 
129 /*
130  * This code is the inverse of in_clsroute: on first reference, if we
131  * were managing the route, stop doing so and set the expiration timer
132  * back off again.
133  */
134 static struct radix_node *
135 in_matroute(void *v_arg, struct radix_node_head *head)
136 {
137 	struct radix_node *rn = rn_match(v_arg, head);
138 	struct rtentry *rt = (struct rtentry *)rn;
139 
140 	if(rt && rt->rt_refcnt == 0) { /* this is first reference */
141 		if(rt->rt_flags & RTPRF_OURS) {
142 			rt->rt_flags &= ~RTPRF_OURS;
143 			rt->rt_rmx.rmx_expire = 0;
144 		}
145 	}
146 	return rn;
147 }
148 
149 /* MIB variables: net.inet.ip.{rtexpire,rtmaxcache,rtminexpire}. */
150 int rtq_reallyold = 60*60;	/* one hour is ``really old'' */
151 int rtq_toomany = 128;		/* 128 cached routes is ``too many'' */
152 int rtq_minreallyold = 10;	/* never automatically crank down to less */
153 
154 /*
155  * On last reference drop, mark the route as belong to us so that it can be
156  * timed out.
157  */
158 static void
159 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
160 {
161 	struct rtentry *rt = (struct rtentry *)rn;
162 
163 	if(!(rt->rt_flags & RTF_UP))
164 		return;		/* prophylactic measures */
165 
166 	if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
167 		return;
168 
169 	if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
170 	   != RTF_WASCLONED)
171 		return;
172 
173 	/*
174 	 * As requested by David Greenman:
175 	 * If rtq_reallyold is 0, just delete the route without
176 	 * waiting for a timeout cycle to kill it.
177 	 */
178 	if(rtq_reallyold != 0) {
179 		rt->rt_flags |= RTPRF_OURS;
180 		rt->rt_rmx.rmx_expire = time.tv_sec + rtq_reallyold;
181 	} else {
182 		rtrequest(RTM_DELETE,
183 			  (struct sockaddr *)rt_key(rt),
184 			  rt->rt_gateway, rt_mask(rt),
185 			  rt->rt_flags, 0);
186 	}
187 }
188 
189 struct rtqk_arg {
190 	struct radix_node_head *rnh;
191 	int draining;
192 	int killed;
193 	int found;
194 	int updating;
195 	time_t nextstop;
196 };
197 
198 /*
199  * Get rid of old routes.  When draining, this deletes everything, even when
200  * the timeout is not expired yet.  When updating, this makes sure that
201  * nothing has a timeout longer than the current value of rtq_reallyold.
202  */
203 static int
204 in_rtqkill(struct radix_node *rn, void *rock)
205 {
206 	struct rtqk_arg *ap = rock;
207 	struct rtentry *rt = (struct rtentry *)rn;
208 	int err;
209 
210 	if(rt->rt_flags & RTPRF_OURS) {
211 		ap->found++;
212 
213 		if(ap->draining || rt->rt_rmx.rmx_expire <= time.tv_sec) {
214 			if(rt->rt_refcnt > 0)
215 				panic("rtqkill route really not free");
216 
217 			err = rtrequest(RTM_DELETE,
218 					(struct sockaddr *)rt_key(rt),
219 					rt->rt_gateway, rt_mask(rt),
220 					rt->rt_flags, 0);
221 			if(err) {
222 				log(LOG_WARNING, "in_rtqkill: error %d\n", err);
223 			} else {
224 				ap->killed++;
225 			}
226 		} else {
227 			if(ap->updating
228 			   && (rt->rt_rmx.rmx_expire - time.tv_sec
229 			       > rtq_reallyold)) {
230 				rt->rt_rmx.rmx_expire = time.tv_sec
231 					+ rtq_reallyold;
232 			}
233 			ap->nextstop = lmin(ap->nextstop,
234 					    rt->rt_rmx.rmx_expire);
235 		}
236 	}
237 
238 	return 0;
239 }
240 
241 #define RTQ_TIMEOUT	60*10	/* run no less than once every ten minutes */
242 int rtq_timeout = RTQ_TIMEOUT;
243 
244 static void
245 in_rtqtimo(void *rock)
246 {
247 	struct radix_node_head *rnh = rock;
248 	struct rtqk_arg arg;
249 	struct timeval atv;
250 	static time_t last_adjusted_timeout = 0;
251 	int s;
252 
253 	arg.found = arg.killed = 0;
254 	arg.rnh = rnh;
255 	arg.nextstop = time.tv_sec + rtq_timeout;
256 	arg.draining = arg.updating = 0;
257 	s = splnet();
258 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
259 	splx(s);
260 
261 	/*
262 	 * Attempt to be somewhat dynamic about this:
263 	 * If there are ``too many'' routes sitting around taking up space,
264 	 * then crank down the timeout, and see if we can't make some more
265 	 * go away.  However, we make sure that we will never adjust more
266 	 * than once in rtq_timeout seconds, to keep from cranking down too
267 	 * hard.
268 	 */
269 	if((arg.found - arg.killed > rtq_toomany)
270 	   && (time.tv_sec - last_adjusted_timeout >= rtq_timeout)
271 	   && rtq_reallyold > rtq_minreallyold) {
272 		rtq_reallyold = 2*rtq_reallyold / 3;
273 		if(rtq_reallyold < rtq_minreallyold) {
274 			rtq_reallyold = rtq_minreallyold;
275 		}
276 
277 		last_adjusted_timeout = time.tv_sec;
278 		log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
279 		    rtq_reallyold);
280 		arg.found = arg.killed = 0;
281 		arg.updating = 1;
282 		s = splnet();
283 		rnh->rnh_walktree(rnh, in_rtqkill, &arg);
284 		splx(s);
285 	}
286 
287 	atv.tv_usec = 0;
288 	atv.tv_sec = arg.nextstop;
289 	timeout(in_rtqtimo, rock, hzto(&atv));
290 }
291 
292 void
293 in_rtqdrain(void)
294 {
295 	struct radix_node_head *rnh = rt_tables[AF_INET];
296 	struct rtqk_arg arg;
297 	int s;
298 	arg.found = arg.killed = 0;
299 	arg.rnh = rnh;
300 	arg.nextstop = 0;
301 	arg.draining = 1;
302 	arg.updating = 0;
303 	s = splnet();
304 	rnh->rnh_walktree(rnh, in_rtqkill, &arg);
305 	splx(s);
306 }
307 
308 /*
309  * Initialize our routing tree.
310  */
311 int
312 in_inithead(void **head, int off)
313 {
314 	struct radix_node_head *rnh;
315 
316 	if(!rn_inithead(head, off))
317 		return 0;
318 
319 	if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
320 		return 1;	/* only do this for the real routing table */
321 
322 	rnh = *head;
323 	rnh->rnh_addaddr = in_addroute;
324 	rnh->rnh_matchaddr = in_matroute;
325 	rnh->rnh_close = in_clsroute;
326 	in_rtqtimo(rnh);	/* kick off timeout first time */
327 	return 1;
328 }
329 
330