]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/route/route_temporal.c
Update Subversion to 1.14.0 LTS. See contrib/subversion/CHANGES for a
[FreeBSD/FreeBSD.git] / sys / net / route / route_temporal.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * This file contains code responsible for expiring temporal routes
30  * (typically, redirect-originated) from the route tables.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/rmlock.h>
42 #include <sys/callout.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46 #include <net/route/route_var.h>
47 #include <net/vnet.h>
48
49 /*
50  * Callback returning 1 for the expired routes.
51  * Updates time of the next nearest route expiration as a side effect.
52  */
53 static int
54 expire_route(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
55 {
56         time_t *next_callout;
57
58         if (rt->rt_expire == 0)
59                 return (0);
60
61         if (rt->rt_expire <= time_uptime)
62                 return (1);
63
64         next_callout = (time_t *)arg;
65
66         /*
67          * Update next_callout to determine the next ts to
68          * run the callback at.
69          */
70         if (*next_callout == 0 || *next_callout > rt->rt_expire)
71                 *next_callout = rt->rt_expire;
72
73         return (0);
74 }
75
76 /*
77  * Per-rnh callout function traversing the tree and deleting
78  * expired routes. Calculates next callout run by looking at
79  * the rt_expire time for the remaining temporal routes.
80  */
81 static void
82 expire_callout(void *arg)
83 {
84         struct rib_head *rnh;
85         time_t next_expire;
86         int seconds;
87
88         rnh = (struct rib_head *)arg;
89
90         CURVNET_SET(rnh->rib_vnet);
91         next_expire = 0;
92
93         rib_walk_del(rnh->rib_fibnum, rnh->rib_family, expire_route,
94             (void *)&next_expire, 1);
95
96         RIB_WLOCK(rnh);
97         if (next_expire > 0) {
98                 seconds = (next_expire - time_uptime);
99                 if (seconds < 0)
100                         seconds = 0;
101                 callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
102                     SBT_1MS * 500, expire_callout, rnh, 0);
103                 rnh->next_expire = next_expire;
104         } else {
105                 /*
106                  * Before resetting next_expire, check that tmproutes_update()
107                  * has not kicked in and scheduled another invocation.
108                  */
109                 if (callout_pending(&rnh->expire_callout) == 0)
110                         rnh->next_expire = 0;
111         }
112         RIB_WUNLOCK(rnh);
113         CURVNET_RESTORE();
114 }
115
116 /*
117  * Function responsible for updating the time of the next calllout
118  * w.r.t. new temporal routes insertion.
119  *
120  * Called by the routing code upon adding new temporal route
121  * to the tree. RIB_WLOCK must be held.
122  */
123 void
124 tmproutes_update(struct rib_head *rnh, struct rtentry *rt)
125 {
126         int seconds;
127
128         RIB_WLOCK_ASSERT(rnh);
129
130         if (rnh->next_expire == 0 || rnh->next_expire > rt->rt_expire) {
131                 /*
132                  * Callback is not scheduled, is executing,
133                  * or is scheduled for a later time than we need.
134                  *
135                  * Schedule the one for the current @rt expiration time.
136                  */
137                 seconds = (rt->rt_expire - time_uptime);
138                 if (seconds < 0)
139                         seconds = 0;
140                 callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
141                     SBT_1MS * 500, expire_callout, rnh, 0);
142
143                 rnh->next_expire = rt->rt_expire;
144         }
145 }
146
147 void
148 tmproutes_init(struct rib_head *rh)
149 {
150
151         callout_init(&rh->expire_callout, 1);
152 }
153
154
155 void
156 tmproutes_destroy(struct rib_head *rh)
157 {
158
159         callout_drain(&rh->expire_callout);
160 }
161