2017-06-01 05:45:05 +03:00
|
|
|
/* $NetBSD: rtbl.c,v 1.7 2017/06/01 02:45:14 chs Exp $ */
|
2011-03-31 23:40:51 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998, 2008, 2011 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Kevin M. Lahey of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* 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 project 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 PROJECT 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 PROJECT 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @(#)route.c 8.3 (Berkeley) 1/9/95
|
|
|
|
*/
|
|
|
|
|
2015-08-25 01:21:26 +03:00
|
|
|
#if defined(_KERNEL) && defined(_KERNEL_OPT)
|
2011-03-31 23:40:51 +04:00
|
|
|
#include "opt_route.h"
|
2015-08-25 01:21:26 +03:00
|
|
|
#endif /* _KERNEL && _KERNEL_OPT */
|
2011-03-31 23:40:51 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2017-06-01 05:45:05 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: rtbl.c,v 1.7 2017/06/01 02:45:14 chs Exp $");
|
2011-03-31 23:40:51 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/callout.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/kauth.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/raw_cb.h>
|
|
|
|
|
|
|
|
static rtbl_t *rt_tables[AF_MAX+1];
|
|
|
|
|
|
|
|
int
|
|
|
|
rt_inithead(rtbl_t **tp, int off)
|
|
|
|
{
|
|
|
|
rtbl_t *t;
|
|
|
|
if (*tp != NULL)
|
|
|
|
return 1;
|
2017-06-01 05:45:05 +03:00
|
|
|
t = kmem_alloc(sizeof(*t), KM_SLEEP);
|
2011-03-31 23:40:51 +04:00
|
|
|
*tp = t;
|
|
|
|
return rn_inithead0(&t->t_rnh, off);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rtentry *
|
|
|
|
rt_matchaddr(rtbl_t *t, const struct sockaddr *dst)
|
|
|
|
{
|
|
|
|
struct radix_node_head *rnh = &t->t_rnh;
|
|
|
|
struct radix_node *rn;
|
|
|
|
|
|
|
|
rn = rnh->rnh_matchaddr(dst, rnh);
|
|
|
|
if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0)
|
|
|
|
return NULL;
|
|
|
|
return (struct rtentry *)rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rt_addaddr(rtbl_t *t, struct rtentry *rt, const struct sockaddr *netmask)
|
|
|
|
{
|
|
|
|
struct radix_node_head *rnh = &t->t_rnh;
|
|
|
|
struct radix_node *rn;
|
|
|
|
|
|
|
|
rn = rnh->rnh_addaddr(rt_getkey(rt), netmask, rnh, rt->rt_nodes);
|
|
|
|
|
|
|
|
return (rn == NULL) ? EEXIST : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rtentry *
|
|
|
|
rt_lookup(rtbl_t *t, const struct sockaddr *dst, const struct sockaddr *netmask)
|
|
|
|
{
|
|
|
|
struct radix_node_head *rnh = &t->t_rnh;
|
|
|
|
struct radix_node *rn;
|
|
|
|
|
|
|
|
rn = rnh->rnh_lookup(dst, netmask, rnh);
|
|
|
|
if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0)
|
|
|
|
return NULL;
|
|
|
|
return (struct rtentry *)rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rtentry *
|
|
|
|
rt_deladdr(rtbl_t *t, const struct sockaddr *dst,
|
|
|
|
const struct sockaddr *netmask)
|
|
|
|
{
|
|
|
|
struct radix_node_head *rnh = &t->t_rnh;
|
|
|
|
struct radix_node *rn;
|
|
|
|
|
|
|
|
if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
|
|
|
|
panic("%s", __func__);
|
|
|
|
return (struct rtentry *)rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rt_walktree_visitor(struct radix_node *rn, void *v)
|
|
|
|
{
|
|
|
|
struct rtwalk *rw = (struct rtwalk *)v;
|
|
|
|
|
|
|
|
return (*rw->rw_f)((struct rtentry *)rn, rw->rw_v);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
Make the routing table and rtcaches MP-safe
See the following descriptions for details.
Proposed on tech-kern and tech-net
Overview
--------
We protect the routing table with a rwock and protect
rtcaches with another rwlock. Each rtentry is protected
from being freed or updated via reference counting and psref.
Global rwlocks
--------------
There are two rwlocks; one for the routing table (rt_lock) and
the other for rtcaches (rtcache_lock). rtcache_lock covers
all existing rtcaches; there may have room for optimizations
(future work).
The locking order is rtcache_lock first and rt_lock is next.
rtentry references
------------------
References to an rtentry is managed with reference counting
and psref. Either of the two mechanisms is used depending on
where a rtentry is obtained. Reference counting is used when
we obtain a rtentry from the routing table directly via
rtalloc1 and rtrequest{,1} while psref is used when we obtain
a rtentry from a rtcache via rtcache_* APIs. In both cases,
a caller can sleep/block with holding an obtained rtentry.
The reasons why we use two different mechanisms are (i) only
using reference counting hurts the performance due to atomic
instructions (rtcache case) (ii) ease of implementation;
applying psref to APIs such rtaloc1 and rtrequest{,1} requires
additional works (adding a local variable and an argument).
We will finally migrate to use only psref but we can do it
when we have a lockless routing table alternative.
Reference counting for rtentry
------------------------------
rt_refcnt now doesn't count permanent references such as for
rt_timers and rtcaches, instead it is used only for temporal
references when obtaining a rtentry via rtalloc1 and rtrequest{,1}.
We can do so because destroying a rtentry always involves
removing references of rt_timers and rtcaches to the rtentry
and we don't need to track such references. This also makes
it easy to wait for readers to release references on deleting
or updating a rtentry, i.e., we can simply wait until the
reference counter is 0 or 1. (If there are permanent references
the counter can be arbitrary.)
rt_ref increments a reference counter of a rtentry and rt_unref
decrements it. rt_ref is called inside APIs (rtalloc1 and
rtrequest{,1} so users don't need to care about it while
users must call rt_unref to an obtained rtentry after using it.
rtfree is removed and we use rt_unref and rt_free instead.
rt_unref now just decrements the counter of a given rtentry
and rt_free just tries to destroy a given rtentry.
See the next section for destructions of rtentries by rt_free.
Destructions of rtentries
-------------------------
We destroy a rtentry only when we call rtrequst{,1}(RTM_DELETE);
the original implementation can destroy in any rtfree where it's
the last reference. If we use reference counting or psref, it's
easy to understand if the place that a rtentry is destroyed is
fixed.
rt_free waits for references to a given rtentry to be released
before actually destroying the rtentry. rt_free uses a condition
variable (cv_wait) (and psref_target_destroy for psref) to wait.
Unfortunately rtrequst{,1}(RTM_DELETE) can be called in softint
that we cannot use cv_wait. In that case, we have to defer the
destruction to a workqueue.
rtentry#rt_cv, rtentry#rt_psref and global variables
(see rt_free_global) are added to conduct the procedure.
Updates of rtentries
--------------------
One difficulty to use refcnt/psref instead of rwlock for rtentry
is updates of rtentries. We need an additional mechanism to
prevent readers from seeing inconsistency of a rtentry being
updated.
We introduce RTF_UPDATING flag to rtentries that are updating.
While the flag is set to a rtentry, users cannot acquire the
rtentry. By doing so, we avoid users to see inconsistent
rtentries.
There are two options when a user tries to acquire a rtentry
with the RTF_UPDATING flag; if a user runs in softint context
the user fails to acquire a rtentry (NULL is returned).
Otherwise a user waits until the update completes by waiting
on cv.
The procedure of a updater is simpler to destruction of
a rtentry. Wait on cv (and psref) and after all readers left,
proceed with the update.
Global variables (see rt_update_global) are added to conduct
the procedure.
Currently we apply the mechanism to only RTM_CHANGE in
rtsock.c. We would have to apply other codes. See
"Known issues" section.
psref for rtentry
-----------------
When we obtain a rtentry from a rtcache via rtcache_* APIs,
psref is used to reference to the rtentry.
rtcache_ref acquires a reference to a rtentry with psref
and rtcache_unref releases the reference after using it.
rtcache_ref is called inside rtcache_* APIs and users don't
need to take care of it while users must call rtcache_unref
to release the reference.
struct psref and int bound that is needed for psref is
embedded into struct route. By doing so we don't need to
add local variables and additional argument to APIs.
However this adds another constraint to psref other than
reference counting one's; holding a reference of an rtentry
via a rtcache is allowed by just one caller at the same time.
So we must not acquire a rtentry via a rtcache twice and
avoid a recursive use of a rtcache. And also a rtcache must
be arranged to be used by a LWP/softint at the same time
somehow. For IP forwarding case, we have per-CPU rtcaches
used in softint so the constraint is guaranteed. For a h
rtcache of a PCB case, the constraint is guaranteed by the
solock of each PCB. Any other cases (pf, ipf, stf and ipsec)
are currently guaranteed by only the existence of the global
locks (softnet_lock and/or KERNEL_LOCK). If we've found the
cases that we cannot guarantee the constraint, we would need
to introduce other rtcache APIs that use simple reference
counting.
psref of rtcache is created with IPL_SOFTNET and so rtcache
shouldn't used at an IPL higher than IPL_SOFTNET.
Note that rtcache_free is used to invalidate a given rtcache.
We don't need another care by my change; just keep them as
they are.
Performance impact
------------------
When NET_MPSAFE is disabled the performance drop is 3% while
when it's enabled the drop is increased to 11%. The difference
comes from that currently we don't take any global locks and
don't use psref if NET_MPSAFE is disabled.
We can optimize the performance of the case of NET_MPSAFE
on by reducing lookups of rtcache that uses psref;
currently we do two lookups but we should be able to trim
one of two. This is a future work.
Known issues
------------
There are two known issues to be solved; one is that
a caller of rtrequest(RTM_ADD) may change rtentry (see rtinit).
We need to prevent new references during the update. Or
we may be able to remove the code (perhaps, need more
investigations).
The other is rtredirect that updates a rtentry. We need
to apply our update mechanism, however it's not easy because
rtredirect is called in softint and we cannot apply our
mechanism simply. One solution is to defer rtredirect to
a workqueue but it requires some code restructuring.
2016-12-12 06:55:57 +03:00
|
|
|
rtbl_walktree(sa_family_t family, int (*f)(struct rtentry *, void *), void *v)
|
2011-03-31 23:40:51 +04:00
|
|
|
{
|
|
|
|
rtbl_t *t = rt_tables[family];
|
|
|
|
struct rtwalk rw;
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rw.rw_f = f;
|
|
|
|
rw.rw_v = v;
|
|
|
|
|
|
|
|
return rn_walktree(&t->t_rnh, rt_walktree_visitor, &rw);
|
|
|
|
}
|
|
|
|
|
2016-11-15 04:50:06 +03:00
|
|
|
struct rtentry *
|
|
|
|
rtbl_search_matched_entry(sa_family_t family,
|
|
|
|
int (*f)(struct rtentry *, void *), void *v)
|
|
|
|
{
|
|
|
|
rtbl_t *t = rt_tables[family];
|
|
|
|
struct rtwalk rw;
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rw.rw_f = f;
|
|
|
|
rw.rw_v = v;
|
|
|
|
|
|
|
|
return (struct rtentry *)
|
|
|
|
rn_search_matched(&t->t_rnh, rt_walktree_visitor, &rw);
|
|
|
|
}
|
|
|
|
|
2011-03-31 23:40:51 +04:00
|
|
|
rtbl_t *
|
|
|
|
rt_gettable(sa_family_t af)
|
|
|
|
{
|
|
|
|
if (af >= __arraycount(rt_tables))
|
|
|
|
return NULL;
|
|
|
|
return rt_tables[af];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtbl_init(void)
|
|
|
|
{
|
|
|
|
struct domain *dom;
|
|
|
|
DOMAIN_FOREACH(dom)
|
|
|
|
if (dom->dom_rtattach)
|
|
|
|
dom->dom_rtattach(&rt_tables[dom->dom_family],
|
|
|
|
dom->dom_rtoffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rt_assert_inactive(const struct rtentry *rt)
|
|
|
|
{
|
|
|
|
if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
|
|
|
|
panic ("rtfree 2");
|
|
|
|
}
|
2016-04-11 12:21:18 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
rt_refines(const struct sockaddr *m_sa, const struct sockaddr *n_sa)
|
|
|
|
{
|
|
|
|
|
|
|
|
return rn_refines(m_sa, n_sa);
|
|
|
|
}
|