source: rtems/cpukit/libnetworking/netinet/in_rmx.c @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

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