xref: /freebsd/sys/net/route/route_temporal.c (revision 9768746ba83efa02837c5b9c66348db6e900208f)
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/ck.h>
42 #include <sys/rmlock.h>
43 #include <sys/callout.h>
44 
45 #include <net/if.h>
46 #include <net/route.h>
47 #include <net/route/route_ctl.h>
48 #include <net/route/route_var.h>
49 #include <net/vnet.h>
50 
51 /*
52  * Callback returning 1 for the expired routes.
53  * Updates time of the next nearest route expiration as a side effect.
54  */
55 static int
56 expire_route(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
57 {
58 	uint32_t nh_expire = nhop_get_expire(nh);
59 	time_t *next_callout;
60 
61 	if (nh_expire == 0)
62 		return (0);
63 
64 	if (nh_expire <= time_uptime)
65 		return (1);
66 
67 	next_callout = (time_t *)arg;
68 
69 	/*
70 	 * Update next_callout to determine the next ts to
71 	 * run the callback at.
72 	 */
73 	if (*next_callout == 0 || *next_callout > nh_expire)
74 		*next_callout = nh_expire;
75 
76 	return (0);
77 }
78 
79 /*
80  * Per-rnh callout function traversing the tree and deleting
81  * expired routes. Calculates next callout run by looking at
82  * the nh_expire time for the remaining temporal routes.
83  */
84 static void
85 expire_callout(void *arg)
86 {
87 	struct rib_head *rnh;
88 	time_t next_expire;
89 	int seconds;
90 
91 	rnh = (struct rib_head *)arg;
92 
93 	CURVNET_SET(rnh->rib_vnet);
94 	next_expire = 0;
95 
96 	rib_walk_del(rnh->rib_fibnum, rnh->rib_family, expire_route,
97 	    (void *)&next_expire, 1);
98 
99 	RIB_WLOCK(rnh);
100 	if (next_expire > 0) {
101 		seconds = (next_expire - time_uptime);
102 		if (seconds < 0)
103 			seconds = 0;
104 		callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
105 		    SBT_1MS * 500, expire_callout, rnh, 0);
106 		rnh->next_expire = next_expire;
107 	} else {
108 		/*
109 		 * Before resetting next_expire, check that tmproutes_update()
110 		 * has not kicked in and scheduled another invocation.
111 		 */
112 		if (callout_pending(&rnh->expire_callout) == 0)
113 			rnh->next_expire = 0;
114 	}
115 	RIB_WUNLOCK(rnh);
116 	CURVNET_RESTORE();
117 }
118 
119 /*
120  * Function responsible for updating the time of the next calllout
121  * w.r.t. new temporal routes insertion.
122  *
123  * Called by the routing code upon adding new temporal route
124  * to the tree. RIB_WLOCK must be held.
125  */
126 void
127 tmproutes_update(struct rib_head *rnh, struct rtentry *rt, struct nhop_object *nh)
128 {
129 	int seconds;
130 	uint32_t nh_expire = nhop_get_expire(nh);
131 
132 	RIB_WLOCK_ASSERT(rnh);
133 
134 	if (rnh->next_expire == 0 || rnh->next_expire > nh_expire) {
135 		/*
136 		 * Callback is not scheduled, is executing,
137 		 * or is scheduled for a later time than we need.
138 		 *
139 		 * Schedule the one for the current @rt expiration time.
140 		 */
141 		seconds = (nh_expire - time_uptime);
142 		if (seconds < 0)
143 			seconds = 0;
144 		callout_reset_sbt(&rnh->expire_callout, SBT_1S * seconds,
145 		    SBT_1MS * 500, expire_callout, rnh, 0);
146 
147 		rnh->next_expire = nh_expire;
148 	}
149 }
150 
151 void
152 tmproutes_init(struct rib_head *rh)
153 {
154 
155 	callout_init(&rh->expire_callout, 1);
156 }
157 
158 void
159 tmproutes_destroy(struct rib_head *rh)
160 {
161 
162 	callout_drain(&rh->expire_callout);
163 }
164