source: rtems/cpukit/libnetworking/netinet/in_rmx.c @ b25b88e7

4.104.11
Last change on this file since b25b88e7 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2010 at 5:50:29 AM

Add HAVE_CONFIG_H support to let files receive configure defines.

  • Property mode set to 100644
File size: 11.1 KB
Line 
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$
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#ifdef HAVE_CONFIG_H
46#include "config.h"
47#endif
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/sysctl.h>
53#include <rtems/bsd/sys/queue.h>
54#include <sys/socket.h>
55#include <sys/socketvar.h>
56#include <sys/mbuf.h>
57#include <sys/syslog.h>
58
59#include <net/if.h>
60#include <net/route.h>
61#include <netinet/in.h>
62#include <netinet/in_systm.h>
63#include <netinet/in_var.h>
64
65#include <netinet/ip.h>
66#include <netinet/ip_var.h>
67
68#include <netinet/tcp.h>
69#include <netinet/tcp_seq.h>
70#include <netinet/tcp_timer.h>
71#include <netinet/tcp_var.h>
72
73extern int      in_inithead(void **head, int off);
74
75#define RTPRF_OURS              RTF_PROTO3      /* set on routes we manage */
76
77/*
78 * Do what we need to do when inserting a route.
79 */
80static struct radix_node *
81in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
82            struct radix_node *treenodes)
83{
84        struct rtentry *rt = (struct rtentry *)treenodes;
85        struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
86        struct radix_node *ret;
87
88        /*
89         * For IP, all unicast non-host routes are automatically cloning.
90         */
91        if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
92                rt->rt_flags |= RTF_MULTICAST;
93
94        if(!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST))) {
95                rt->rt_flags |= RTF_PRCLONING;
96        }
97
98        /*
99         * A little bit of help for both IP output and input:
100         *   For host routes, we make sure that RTF_BROADCAST
101         *   is set for anything that looks like a broadcast address.
102         *   This way, we can avoid an expensive call to in_broadcast()
103         *   in ip_output() most of the time (because the route passed
104         *   to ip_output() is almost always a host route).
105         *
106         *   We also do the same for local addresses, with the thought
107         *   that this might one day be used to speed up ip_input().
108         *
109         * We also mark routes to multicast addresses as such, because
110         * it's easy to do and might be useful (but this is much more
111         * dubious since it's so easy to inspect the address).  (This
112         * is done above.)
113         */
114        if (rt->rt_flags & RTF_HOST) {
115                if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
116                        rt->rt_flags |= RTF_BROADCAST;
117                } else {
118#define satosin(sa) ((struct sockaddr_in *)sa)
119                        if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr
120                            == sin->sin_addr.s_addr)
121                                rt->rt_flags |= RTF_LOCAL;
122#undef satosin
123                }
124        }
125
126        /*
127         * We also specify a send and receive pipe size for every
128         * route added, to help TCP a bit.  TCP doesn't actually
129         * want a true pipe size, which would be prohibitive in memory
130         * costs and is hard to compute anyway; it simply uses these
131         * values to size its buffers.  So, we fill them in with the
132         * same values that TCP would have used anyway, and allow the
133         * installing program or the link layer to override these values
134         * as it sees fit.  This will hopefully allow TCP more
135         * opportunities to save its ssthresh value.
136         */
137        if (!rt->rt_rmx.rmx_sendpipe && !(rt->rt_rmx.rmx_locks & RTV_SPIPE))
138                rt->rt_rmx.rmx_sendpipe = tcp_sendspace;
139
140        if (!rt->rt_rmx.rmx_recvpipe && !(rt->rt_rmx.rmx_locks & RTV_RPIPE))
141                rt->rt_rmx.rmx_recvpipe = tcp_recvspace;
142
143        if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) 
144            && rt->rt_ifp)
145                rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
146
147        ret = rn_addroute(v_arg, n_arg, head, treenodes);
148        if (ret == NULL && rt->rt_flags & RTF_HOST) {
149                struct rtentry *rt2;
150                /*
151                 * We are trying to add a host route, but can't.
152                 * Find out if it is because of an
153                 * ARP entry and delete it if so.
154                 */
155                rt2 = rtalloc1((struct sockaddr *)sin, 0,
156                                RTF_CLONING | RTF_PRCLONING);
157                if (rt2) {
158                        if (rt2->rt_flags & RTF_LLINFO &&
159                                rt2->rt_flags & RTF_HOST &&
160                                rt2->rt_gateway &&
161                                rt2->rt_gateway->sa_family == AF_LINK) {
162                                rtrequest(RTM_DELETE,
163                                          (struct sockaddr *)rt_key(rt2),
164                                          rt2->rt_gateway,
165                                          rt_mask(rt2), rt2->rt_flags, 0);
166                                ret = rn_addroute(v_arg, n_arg, head,
167                                        treenodes);
168                        }
169                        RTFREE(rt2);
170                }
171        }
172        return ret;
173}
174
175/*
176 * This code is the inverse of in_clsroute: on first reference, if we
177 * were managing the route, stop doing so and set the expiration timer
178 * back off again.
179 */
180static struct radix_node *
181in_matroute(void *v_arg, struct radix_node_head *head)
182{
183        struct radix_node *rn = rn_match(v_arg, head);
184        struct rtentry *rt = (struct rtentry *)rn;
185
186        if(rt && rt->rt_refcnt == 0) { /* this is first reference */
187                if(rt->rt_flags & RTPRF_OURS) {
188                        rt->rt_flags &= ~RTPRF_OURS;
189                        rt->rt_rmx.rmx_expire = 0;
190                }
191        }
192        return rn;
193}
194
195static int rtq_reallyold = 60*60;
196        /* one hour is ``really old'' */
197SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire,
198        CTLFLAG_RW, &rtq_reallyold , 0, "");
199                                   
200static int rtq_minreallyold = 10;
201        /* never automatically crank down to less */
202SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire,
203        CTLFLAG_RW, &rtq_minreallyold , 0, "");
204                                   
205static int rtq_toomany = 128;
206        /* 128 cached routes is ``too many'' */
207SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache,
208        CTLFLAG_RW, &rtq_toomany , 0, "");
209                                   
210
211/*
212 * On last reference drop, mark the route as belong to us so that it can be
213 * timed out.
214 */
215static void
216in_clsroute(struct radix_node *rn, struct radix_node_head *head)
217{
218        struct rtentry *rt = (struct rtentry *)rn;
219
220        if(!(rt->rt_flags & RTF_UP))
221                return;         /* prophylactic measures */
222
223        if((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
224                return;
225
226        if((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS))
227           != RTF_WASCLONED)
228                return;
229
230        /*
231         * As requested by David Greenman:
232         * If rtq_reallyold is 0, just delete the route without
233         * waiting for a timeout cycle to kill it.
234         */
235        if(rtq_reallyold != 0) {
236                rt->rt_flags |= RTPRF_OURS;
237                rt->rt_rmx.rmx_expire = rtems_bsdnet_seconds_since_boot() + rtq_reallyold;
238        } else {
239                rtrequest(RTM_DELETE,
240                          (struct sockaddr *)rt_key(rt),
241                          rt->rt_gateway, rt_mask(rt),
242                          rt->rt_flags, 0);
243        }
244}
245
246struct rtqk_arg {
247        struct radix_node_head *rnh;
248        int draining;
249        int killed;
250        int found;
251        int updating;
252        time_t nextstop;
253};
254
255/*
256 * Get rid of old routes.  When draining, this deletes everything, even when
257 * the timeout is not expired yet.  When updating, this makes sure that
258 * nothing has a timeout longer than the current value of rtq_reallyold.
259 */
260static int
261in_rtqkill(struct radix_node *rn, void *rock)
262{
263        struct rtqk_arg *ap = rock;
264        struct rtentry *rt = (struct rtentry *)rn;
265        int err;
266
267        if(rt->rt_flags & RTPRF_OURS) {
268                ap->found++;
269
270                if(ap->draining || rt->rt_rmx.rmx_expire <= rtems_bsdnet_seconds_since_boot()) {
271                        if(rt->rt_refcnt > 0)
272                                panic("rtqkill route really not free");
273
274                        err = rtrequest(RTM_DELETE,
275                                        (struct sockaddr *)rt_key(rt),
276                                        rt->rt_gateway, rt_mask(rt),
277                                        rt->rt_flags, 0);
278                        if(err) {
279                                log(LOG_WARNING, "in_rtqkill: error %d\n", err);
280                        } else {
281                                ap->killed++;
282                        }
283                } else {
284                        if(ap->updating
285                           && (rt->rt_rmx.rmx_expire - rtems_bsdnet_seconds_since_boot()
286                               > rtq_reallyold)) {
287                                rt->rt_rmx.rmx_expire = rtems_bsdnet_seconds_since_boot()
288                                        + rtq_reallyold;
289                        }
290                        ap->nextstop = lmin(ap->nextstop,
291                                            rt->rt_rmx.rmx_expire);
292                }
293        }
294
295        return 0;
296}
297
298#define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
299static int rtq_timeout = RTQ_TIMEOUT;
300
301static void
302in_rtqtimo(void *rock)
303{
304        struct radix_node_head *rnh = rock;
305        struct rtqk_arg arg;
306        struct timeval atv;
307        static time_t last_adjusted_timeout = 0;
308        int s;
309
310        arg.found = arg.killed = 0;
311        arg.rnh = rnh;
312        arg.nextstop = rtems_bsdnet_seconds_since_boot() + rtq_timeout;
313        arg.draining = arg.updating = 0;
314        s = splnet();
315        rnh->rnh_walktree(rnh, in_rtqkill, &arg);
316        splx(s);
317
318        /*
319         * Attempt to be somewhat dynamic about this:
320         * If there are ``too many'' routes sitting around taking up space,
321         * then crank down the timeout, and see if we can't make some more
322         * go away.  However, we make sure that we will never adjust more
323         * than once in rtq_timeout seconds, to keep from cranking down too
324         * hard.
325         */
326        if((arg.found - arg.killed > rtq_toomany)
327           && (rtems_bsdnet_seconds_since_boot() - last_adjusted_timeout >= rtq_timeout)
328           && rtq_reallyold > rtq_minreallyold) {
329                rtq_reallyold = 2*rtq_reallyold / 3;
330                if(rtq_reallyold < rtq_minreallyold) {
331                        rtq_reallyold = rtq_minreallyold;
332                }
333
334                last_adjusted_timeout = rtems_bsdnet_seconds_since_boot();
335#ifdef DIAGNOSTIC
336                log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
337                    rtq_reallyold);
338#endif
339                arg.found = arg.killed = 0;
340                arg.updating = 1;
341                s = splnet();
342                rnh->rnh_walktree(rnh, in_rtqkill, &arg);
343                splx(s);
344        }
345
346        atv.tv_usec = 0;
347        atv.tv_sec = arg.nextstop;
348        timeout(in_rtqtimo, rock, hzto(&atv));
349}
350
351void
352in_rtqdrain(void)
353{
354        struct radix_node_head *rnh = rt_tables[AF_INET];
355        struct rtqk_arg arg;
356        int s;
357        arg.found = arg.killed = 0;
358        arg.rnh = rnh;
359        arg.nextstop = 0;
360        arg.draining = 1;
361        arg.updating = 0;
362        s = splnet();
363        rnh->rnh_walktree(rnh, in_rtqkill, &arg);
364        splx(s);
365}
366
367/*
368 * Initialize our routing tree.
369 */
370int
371in_inithead(void **head, int off)
372{
373        struct radix_node_head *rnh;
374
375        if(!rn_inithead(head, off))
376                return 0;
377
378        if(head != (void **)&rt_tables[AF_INET]) /* BOGUS! */
379                return 1;       /* only do this for the real routing table */
380
381        rnh = *head;
382        rnh->rnh_addaddr = in_addroute;
383        rnh->rnh_matchaddr = in_matroute;
384        rnh->rnh_close = in_clsroute;
385        in_rtqtimo(rnh);        /* kick off timeout first time */
386        return 1;
387}
Note: See TracBrowser for help on using the repository browser.