2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * This file contains code responsible for expiring temporal routes
30 * (typically, redirect-originated) from the route tables.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
42 #include <sys/rmlock.h>
43 #include <sys/callout.h>
46 #include <net/route.h>
47 #include <net/route/route_ctl.h>
48 #include <net/route/route_var.h>
52 * Callback returning 1 for the expired routes.
53 * Updates time of the next nearest route expiration as a side effect.
56 expire_route(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
60 if (rt->rt_expire == 0)
63 if (rt->rt_expire <= time_uptime)
66 next_callout = (time_t *)arg;
69 * Update next_callout to determine the next ts to
70 * run the callback at.
72 if (*next_callout == 0 || *next_callout > rt->rt_expire)
73 *next_callout = rt->rt_expire;
79 * Per-rnh callout function traversing the tree and deleting
80 * expired routes. Calculates next callout run by looking at
81 * the rt_expire time for the remaining temporal routes.
84 expire_callout(void *arg)
90 rnh = (struct rib_head *)arg;
92 CURVNET_SET(rnh->rib_vnet);
95 rib_walk_del(rnh->rib_fibnum, rnh->rib_family, expire_route,
96 (void *)&next_expire, 1);
99 if (next_expire > 0) {
100 seconds = (next_expire - time_uptime);
103 callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
104 SBT_1MS * 500, expire_callout, rnh, 0);
105 rnh->next_expire = next_expire;
108 * Before resetting next_expire, check that tmproutes_update()
109 * has not kicked in and scheduled another invocation.
111 if (callout_pending(&rnh->expire_callout) == 0)
112 rnh->next_expire = 0;
119 * Function responsible for updating the time of the next calllout
120 * w.r.t. new temporal routes insertion.
122 * Called by the routing code upon adding new temporal route
123 * to the tree. RIB_WLOCK must be held.
126 tmproutes_update(struct rib_head *rnh, struct rtentry *rt)
130 RIB_WLOCK_ASSERT(rnh);
132 if (rnh->next_expire == 0 || rnh->next_expire > rt->rt_expire) {
134 * Callback is not scheduled, is executing,
135 * or is scheduled for a later time than we need.
137 * Schedule the one for the current @rt expiration time.
139 seconds = (rt->rt_expire - time_uptime);
142 callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
143 SBT_1MS * 500, expire_callout, rnh, 0);
145 rnh->next_expire = rt->rt_expire;
150 tmproutes_init(struct rib_head *rh)
153 callout_init(&rh->expire_callout, 1);
157 tmproutes_destroy(struct rib_head *rh)
160 callout_drain(&rh->expire_callout);