2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /************************************************************************
32 * Note: In this file a 'fib' is a "forwarding information base" *
33 * Which is the new name for an in kernel routing (next hop) table. *
34 ***********************************************************************/
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 #include "opt_route.h"
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/kernel.h>
51 #include <sys/domain.h>
52 #include <sys/sysproto.h>
55 #include <net/route.h>
56 #include <net/route/route_var.h>
58 /* Kernel config default option. */
61 #error "ROUTETABLES defined too low"
63 #if ROUTETABLES > RT_MAXFIBS
64 #error "ROUTETABLES defined too big"
66 #define RT_NUMFIBS ROUTETABLES
67 #endif /* ROUTETABLES */
68 /* Initialize to default if not otherwise set. */
73 static void grow_rtables(uint32_t num_fibs);
75 VNET_DEFINE_STATIC(struct sx, rtables_lock);
76 #define V_rtables_lock VNET(rtables_lock)
77 #define RTABLES_LOCK() sx_xlock(&V_rtables_lock)
78 #define RTABLES_UNLOCK() sx_xunlock(&V_rtables_lock)
79 #define RTABLES_LOCK_INIT() sx_init(&V_rtables_lock, "rtables lock")
80 #define RTABLES_LOCK_ASSERT() sx_assert(&V_rtables_lock, SA_LOCKED)
82 VNET_DEFINE_STATIC(struct rib_head **, rt_tables);
83 #define V_rt_tables VNET(rt_tables)
85 VNET_DEFINE(uint32_t, _rt_numfibs) = RT_NUMFIBS;
88 * Handler for net.my_fibnum.
89 * Returns current fib of the process.
92 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
97 fibnum = curthread->td_proc->p_fibnum;
98 error = sysctl_handle_int(oidp, &fibnum, 0, req);
101 SYSCTL_PROC(_net, OID_AUTO, my_fibnum,
102 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
103 &sysctl_my_fibnum, "I",
104 "default FIB of caller");
107 normalize_num_rtables(uint32_t num_rtables)
110 if (num_rtables > RT_MAXFIBS)
111 num_rtables = RT_MAXFIBS;
112 else if (num_rtables == 0)
114 return (num_rtables);
118 * Sets the number of fibs in the current vnet.
119 * Function does not allow shrinking number of rtables.
122 sysctl_fibs(SYSCTL_HANDLER_ARGS)
128 new_fibs = V_rt_numfibs;
129 error = sysctl_handle_32(oidp, &new_fibs, 0, req);
131 new_fibs = normalize_num_rtables(new_fibs);
133 if (new_fibs < V_rt_numfibs)
135 if (new_fibs > V_rt_numfibs)
136 grow_rtables(new_fibs);
142 SYSCTL_PROC(_net, OID_AUTO, fibs,
143 CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
144 NULL, 0, &sysctl_fibs, "IU",
145 "set number of fibs");
148 * Sets fib of a current process.
151 sys_setfib(struct thread *td, struct setfib_args *uap)
155 CURVNET_SET(TD_TO_VNET(td));
156 if (uap->fibnum >= 0 && uap->fibnum < V_rt_numfibs)
157 td->td_proc->p_fibnum = uap->fibnum;
166 * Grows up the number of routing tables in the current fib.
167 * Function creates new index array for all rtables and allocates
168 * remaining routing tables.
171 grow_rtables(uint32_t num_tables)
174 struct rib_head **prnh;
175 struct rib_head **new_rt_tables, **old_rt_tables;
178 RTABLES_LOCK_ASSERT();
180 KASSERT(num_tables >= V_rt_numfibs, ("num_tables(%u) < rt_numfibs(%u)\n",
181 num_tables, V_rt_numfibs));
183 new_rt_tables = mallocarray(num_tables * (AF_MAX + 1), sizeof(void *),
184 M_RTABLE, M_WAITOK | M_ZERO);
187 * Current rt_tables layout:
188 * fib0[af0, af1, af2, .., AF_MAX]fib1[af0, af1, af2, .., Af_MAX]..
189 * this allows to copy existing tables data by using memcpy()
191 if (V_rt_tables != NULL)
192 memcpy(new_rt_tables, V_rt_tables,
193 V_rt_numfibs * (AF_MAX + 1) * sizeof(void *));
195 /* Populate the remainders */
196 for (dom = domains; dom; dom = dom->dom_next) {
197 if (dom->dom_rtattach == NULL)
199 family = dom->dom_family;
200 for (int i = 0; i < num_tables; i++) {
201 prnh = &new_rt_tables[i * (AF_MAX + 1) + family];
204 *prnh = dom->dom_rtattach(i);
206 log(LOG_ERR, "unable to create routing tables for domain %d\n",
212 * Update rtables pointer.
213 * Ensure all writes to new_rt_tables has been completed before
216 atomic_thread_fence_rel();
217 old_rt_tables = V_rt_tables;
218 V_rt_tables = new_rt_tables;
220 /* Wait till all cpus see new pointers */
221 atomic_thread_fence_rel();
222 epoch_wait_preempt(net_epoch_preempt);
224 /* Finally, set number of fibs to a new value */
225 V_rt_numfibs = num_tables;
227 if (old_rt_tables != NULL)
228 free(old_rt_tables, M_RTABLE);
232 vnet_rtables_init(const void *unused __unused)
234 int num_rtables_base;
236 if (IS_DEFAULT_VNET(curvnet)) {
237 num_rtables_base = RT_NUMFIBS;
238 TUNABLE_INT_FETCH("net.fibs", &num_rtables_base);
239 V_rt_numfibs = normalize_num_rtables(num_rtables_base);
248 grow_rtables(V_rt_numfibs);
251 VNET_SYSINIT(vnet_rtables_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
252 vnet_rtables_init, 0);
256 rtables_destroy(const void *unused __unused)
258 struct rib_head *rnh;
263 for (dom = domains; dom; dom = dom->dom_next) {
264 if (dom->dom_rtdetach == NULL)
266 family = dom->dom_family;
267 for (int i = 0; i < V_rt_numfibs; i++) {
268 rnh = rt_tables_get_rnh(i, family);
269 dom->dom_rtdetach(rnh);
275 * dom_rtdetach calls rt_table_destroy(), which
276 * schedules deletion for all rtentries, nexthops and control
277 * structures. Wait for the destruction callbacks to fire.
278 * Note that this should result in freeing all rtentries, but
279 * nexthops deletions will be scheduled for the next epoch run
280 * and will be completed after vnet teardown.
282 epoch_drain_callbacks(net_epoch_preempt);
284 free(V_rt_tables, M_RTABLE);
285 vnet_rtzone_destroy();
287 VNET_SYSUNINIT(rtables_destroy, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
291 static inline struct rib_head *
292 rt_tables_get_rnh_ptr(uint32_t table, sa_family_t family)
294 struct rib_head **prnh;
296 KASSERT(table < V_rt_numfibs,
297 ("%s: table out of bounds (%d < %d)", __func__, table,
299 KASSERT(family < (AF_MAX + 1),
300 ("%s: fam out of bounds (%d < %d)", __func__, family, AF_MAX + 1));
302 /* rnh is [fib=0][af=0]. */
304 /* Get the offset to the requested table and fam. */
305 prnh += table * (AF_MAX + 1) + family;
311 rt_tables_get_rnh(uint32_t table, sa_family_t family)
314 return (rt_tables_get_rnh_ptr(table, family));
318 rt_tables_get_gen(uint32_t table, sa_family_t family)
320 struct rib_head *rnh;
322 rnh = rt_tables_get_rnh_ptr(table, family);
323 KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d family %d",
324 __func__, table, family));
325 return (rnh->rnh_gen);