aboutsummaryrefslogtreecommitdiff
path: root/sys/net/route/route_tables.c
blob: e2bbd5ea16a28faed8a6689129cfff5365581656 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1980, 1986, 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/************************************************************************
 * Note: In this file a 'fib' is a "forwarding information base"	*
 * Which is the new name for an in kernel routing (next hop) table.	*
 ***********************************************************************/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_route.h"

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/jail.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/sx.h>
#include <sys/domain.h>
#include <sys/sysproto.h>

#include <net/vnet.h>
#include <net/route.h>
#include <net/route/route_var.h>

/* Kernel config default option. */
#ifdef ROUTETABLES
#if ROUTETABLES <= 0
#error "ROUTETABLES defined too low"
#endif
#if ROUTETABLES > RT_MAXFIBS
#error "ROUTETABLES defined too big"
#endif
#define	RT_NUMFIBS	ROUTETABLES
#endif /* ROUTETABLES */
/* Initialize to default if not otherwise set. */
#ifndef	RT_NUMFIBS
#define	RT_NUMFIBS	1
#endif

static void grow_rtables(uint32_t num_fibs);

VNET_DEFINE_STATIC(struct sx, rtables_lock);
#define	V_rtables_lock		VNET(rtables_lock)
#define	RTABLES_LOCK()		sx_xlock(&V_rtables_lock)
#define	RTABLES_UNLOCK()	sx_xunlock(&V_rtables_lock)
#define	RTABLES_LOCK_INIT()	sx_init(&V_rtables_lock, "rtables lock")
#define	RTABLES_LOCK_ASSERT()	sx_assert(&V_rtables_lock, SA_LOCKED)

VNET_DEFINE_STATIC(struct rib_head **, rt_tables);
#define	V_rt_tables	VNET(rt_tables)

VNET_DEFINE(uint32_t, _rt_numfibs) = RT_NUMFIBS;

/*
 * Handler for net.my_fibnum.
 * Returns current fib of the process.
 */
static int
sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
{
        int fibnum;
        int error;

        fibnum = curthread->td_proc->p_fibnum;
        error = sysctl_handle_int(oidp, &fibnum, 0, req);
        return (error);
}
SYSCTL_PROC(_net, OID_AUTO, my_fibnum,
    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
    &sysctl_my_fibnum, "I",
    "default FIB of caller");

static uint32_t
normalize_num_rtables(uint32_t num_rtables)
{

	if (num_rtables > RT_MAXFIBS)
		num_rtables = RT_MAXFIBS;
	else if (num_rtables == 0)
		num_rtables = 1;
	return (num_rtables);
}

/*
 * Sets the number of fibs in the current vnet.
 * Function does not allow shrinking number of rtables.
 */
static int
sysctl_fibs(SYSCTL_HANDLER_ARGS)
{
	uint32_t new_fibs;
	int error;

	RTABLES_LOCK();
	new_fibs = V_rt_numfibs;
	error = sysctl_handle_32(oidp, &new_fibs, 0, req);
	if (error == 0) {
		new_fibs = normalize_num_rtables(new_fibs);

		if (new_fibs < V_rt_numfibs)
			error = ENOTCAPABLE;
		if (new_fibs > V_rt_numfibs)
			grow_rtables(new_fibs);
	}
	RTABLES_UNLOCK();

	return (error);
}
SYSCTL_PROC(_net, OID_AUTO, fibs,
    CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
    NULL, 0, &sysctl_fibs, "IU",
    "set number of fibs");

/*
 * Sets fib of a current process.
 */
int
sys_setfib(struct thread *td, struct setfib_args *uap)
{
	int error = 0;

	CURVNET_SET(TD_TO_VNET(td));
	if (uap->fibnum >= 0 && uap->fibnum < V_rt_numfibs)
		td->td_proc->p_fibnum = uap->fibnum;
	else
		error = EINVAL;
	CURVNET_RESTORE();

	return (error);
}

/*
 * Grows up the number of routing tables in the current fib.
 * Function creates new index array for all rtables and allocates
 *  remaining routing tables.
 */
static void
grow_rtables(uint32_t num_tables)
{
	struct domain *dom;
	struct rib_head **prnh;
	struct rib_head **new_rt_tables, **old_rt_tables;
	int family;

	RTABLES_LOCK_ASSERT();

	KASSERT(num_tables >= V_rt_numfibs, ("num_tables(%u) < rt_numfibs(%u)\n",
				num_tables, V_rt_numfibs));

	new_rt_tables = mallocarray(num_tables * (AF_MAX + 1), sizeof(void *),
	    M_RTABLE, M_WAITOK | M_ZERO);

	if ((num_tables > 1) && (V_rt_add_addr_allfibs == 0))
		printf("WARNING: Adding ifaddrs to all fibs has been turned off "
			"by default. Consider tuning %s if needed\n",
			"net.add_addr_allfibs");

	/*
	 * Current rt_tables layout:
	 * fib0[af0, af1, af2, .., AF_MAX]fib1[af0, af1, af2, .., Af_MAX]..
	 * this allows to copy existing tables data by using memcpy()
	 */
	if (V_rt_tables != NULL)
		memcpy(new_rt_tables, V_rt_tables,
		    V_rt_numfibs * (AF_MAX + 1) * sizeof(void *));

	/* Populate the remainders */
	for (dom = domains; dom; dom = dom->dom_next) {
		if (dom->dom_rtattach == NULL)
			continue;
		family = dom->dom_family;
		for (int i = 0; i < num_tables; i++) {
			prnh = &new_rt_tables[i * (AF_MAX + 1) + family];
			if (*prnh != NULL)
				continue;
			*prnh = dom->dom_rtattach(i);
			if (*prnh == NULL)
				log(LOG_ERR, "unable to create routing tables for domain %d\n",
				    dom->dom_family);
		}
	}

	/*
	 * Update rtables pointer.
	 * Ensure all writes to new_rt_tables has been completed before
	 *  switching pointer.
	 */
	atomic_thread_fence_rel();
	old_rt_tables = V_rt_tables;
	V_rt_tables = new_rt_tables;

	/* Wait till all cpus see new pointers */
	atomic_thread_fence_rel();
	epoch_wait_preempt(net_epoch_preempt);

	/* Finally, set number of fibs to a new value */
	V_rt_numfibs = num_tables;

	if (old_rt_tables != NULL)
		free(old_rt_tables, M_RTABLE);
}

static void
vnet_rtables_init(const void *unused __unused)
{
	int num_rtables_base;

	if (IS_DEFAULT_VNET(curvnet)) {
		num_rtables_base = RT_NUMFIBS;
		TUNABLE_INT_FETCH("net.fibs", &num_rtables_base);
		V_rt_numfibs = normalize_num_rtables(num_rtables_base);
	} else
		V_rt_numfibs = 1;

	vnet_rtzone_init();

	RTABLES_LOCK_INIT();

	RTABLES_LOCK();
	grow_rtables(V_rt_numfibs);
	RTABLES_UNLOCK();
}
VNET_SYSINIT(vnet_rtables_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
    vnet_rtables_init, 0);

#ifdef VIMAGE
static void
rtables_destroy(const void *unused __unused)
{
	struct rib_head *rnh;
	struct domain *dom;
	int family;

	RTABLES_LOCK();
	for (dom = domains; dom; dom = dom->dom_next) {
		if (dom->dom_rtdetach == NULL)
			continue;
		family = dom->dom_family;
		for (int i = 0; i < V_rt_numfibs; i++) {
			rnh = rt_tables_get_rnh(i, family);
			dom->dom_rtdetach(rnh);
		}
	}
	RTABLES_UNLOCK();

	/*
	 * dom_rtdetach calls rt_table_destroy(), which
	 *  schedules deletion for all rtentries, nexthops and control
	 *  structures. Wait for the destruction callbacks to fire.
	 * Note that this should result in freeing all rtentries, but
	 *  nexthops deletions will be scheduled for the next epoch run
	 *  and will be completed after vnet teardown.
	 */
	epoch_drain_callbacks(net_epoch_preempt);

	free(V_rt_tables, M_RTABLE);
	vnet_rtzone_destroy();
}
VNET_SYSUNINIT(rtables_destroy, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
    rtables_destroy, 0);
#endif

static inline struct rib_head *
rt_tables_get_rnh_ptr(uint32_t table, sa_family_t family)
{
	struct rib_head **prnh;

	KASSERT(table < V_rt_numfibs,
	    ("%s: table out of bounds (%d < %d)", __func__, table,
	     V_rt_numfibs));
	KASSERT(family < (AF_MAX + 1),
	    ("%s: fam out of bounds (%d < %d)", __func__, family, AF_MAX + 1));

	/* rnh is [fib=0][af=0]. */
	prnh = V_rt_tables;
	/* Get the offset to the requested table and fam. */
	prnh += table * (AF_MAX + 1) + family;

	return (*prnh);
}

struct rib_head *
rt_tables_get_rnh(uint32_t table, sa_family_t family)
{

	return (rt_tables_get_rnh_ptr(table, family));
}

u_int
rt_tables_get_gen(uint32_t table, sa_family_t family)
{
	struct rib_head *rnh;

	rnh = rt_tables_get_rnh_ptr(table, family);
	KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d family %d",
	    __func__, table, family));
	return (rnh->rnh_gen);
}