Commit Graph

209 Commits

Author SHA1 Message Date
ozaki-r
55b7c5b86a Resolve tangled lock dependencies in route.c
This change sweeps remaining lock decisions based on if locked or not by moving
utility functions of rtentry updates from rtsock.c and ensuring holding the
rt_lock.  It also improves the atomicity of a update of a rtentry.
2018-04-12 04:38:13 +00:00
ozaki-r
1ff21b4734 Kill remaining rt->rt_refcnt++ 2018-04-05 03:39:14 +00:00
ozaki-r
e3a1cd4228 Don't take RT_LOCK in DDB
It definitely causes a diagnostic failure if LOCKDEBUG is enabled.
2018-03-23 04:09:41 +00:00
ozaki-r
af8d700fee Prevent rt_free_global.wk from being enqueued to workqueue doubly 2018-01-30 11:01:04 +00:00
ozaki-r
c42d42d7f6 Fix a return value of rt_update_prepare
Callers expect it to be an errno.
2018-01-23 07:20:10 +00:00
ozaki-r
606d8da495 Suppress noisy debugging outputs
Even if DEBUG they are too noisy under load.
2018-01-19 08:01:05 +00:00
christos
219bb927fe Use a queue of deferred entries to delete routes instead of a fixed stack
of 10. Otherwise we can overflow in route deletions from the rexmit timer.
XXX: pullup-8
2018-01-09 19:52:29 +00:00
christos
8b26e6d696 Don't stomp past the end of the array! need __arraycount not sizeof()
Found by chuq, while debugging the sdf.org crashes
XXX: pullup-8
Restructure a bit for readability.
2018-01-05 01:53:15 +00:00
ozaki-r
856a12a96d Synchronize on rtcache_generation with rtlock
It's racy if NET_MPSAFE is enabled.

Pointed out by joerg@
2017-09-25 04:15:33 +00:00
ozaki-r
82d5516b2e Remove the global lock for rtcache
Thanks to removal of LIST_ENTRY of struct route, rtcaches are accessed only by
their users. And in existing usages a rtcache is guranteed to be not accessed
simultaneously. So the rtcache framework doesn't need any exclusion controls
in itself.
2017-09-22 05:05:32 +00:00
ozaki-r
0092eb7df6 Invalidate rtcache based on a global generation counter
The change introduces a global generation counter that is incremented when any
routes have been added or deleted. When a rtcache caches a rtentry into itself,
it also stores a snapshot of the generation counter. If the snapshot equals to
the global counter, the cache is still valid, otherwise invalidated.

One drawback of the change is that all rtcaches of all protocol families are
invalidated when any routes of any protocol families are added or deleted.
If that matters, we should have separate generation counters based on
protocol families.

This change removes LIST_ENTRY from struct route, which fixes a part of
PR kern/52515.
2017-09-21 07:15:34 +00:00
ozaki-r
6b704eed1f Remove unnecessary NULL check of rt_ifp
It's always non-NULL.
2017-09-21 04:44:32 +00:00
ozaki-r
fcbd6bf4fa Drop RTF_LLINFO flag (now it's RTF_LLDATA) from local routes
They don't have llinfo anymore. And also the change fixes unexpected
behavior of ARP proxy.
2017-06-28 04:10:47 +00:00
ozaki-r
3659ef5665 Purge all related L2 caches on removing a route
The change addresses situations similar to PR 51179.
2017-06-22 09:56:48 +00:00
ozaki-r
4f6bbf24ed Fix locking in rtalloc1 (affected only if NET_MPSAFE) 2017-06-22 08:31:54 +00:00
ozaki-r
f4510b11c6 Forbit installing a route which its gateway is unreachable
This change needs a tweak in route_output_change to unbreak route
change commands (e.g., route change -inet6 default -reject).

PR kern/52077 (s-yamaguchi@IIJ and ozaki-r@)
2017-03-24 03:45:02 +00:00
ozaki-r
2b5b2086c8 Tweak and KNF some functions 2017-03-22 07:14:18 +00:00
ozaki-r
390b465f79 Make updating a rtentry in rtinit MP-safe 2017-02-20 04:23:11 +00:00
ozaki-r
77a7c1c676 Make NOMPSAFE comments informative 2017-02-17 04:31:34 +00:00
ozaki-r
39a63b6b81 Ensure that nobody references a rtentry that is passed to rt_setgate 2017-02-10 13:48:06 +00:00
ozaki-r
f1b2f01b14 Fix locking against myself in ifa_ifwithroute_psref
It happened on the path: rtrequest1 => rt_getifa => ifa_ifwithroute_psref.

Reported by ryo@
2017-02-10 13:44:47 +00:00
ozaki-r
8d757e7177 Disable rt_update mechanism by default
This is a workaround for PR kern/51877. Enable again once the issue
is fixed.
2017-01-19 06:58:55 +00:00
ozaki-r
6e752b2732 Fix typo in comments 2017-01-17 07:53:06 +00:00
ozaki-r
2b82ef9b8f Get rid of unnecessary header inclusions 2017-01-11 13:08:29 +00:00
ozaki-r
8be8a178cb Don't call psref_target_destroy unless NET_MPSAFE
We don't need it if NET_MPSAFE off and also it causes lockup
sometimes because of calling it with holding softnet_lock.
2016-12-21 04:01:57 +00:00
ozaki-r
eceb88a68b Fix kernel build with RT_DEBUG and !NET_MPSAFE 2016-12-21 00:33:49 +00:00
ozaki-r
6fb8880601 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 03:55:57 +00:00
ozaki-r
5879478f65 Don't use rt_walktree to delete routes
Some functions use rt_walktree to scan the routing table and delete
matched routes. However, we shouldn't use rt_walktree to delete
routes because rt_walktree is recursive to the routing table (radix
tree) and isn't friendly to MP-ification. rt_walktree allows a caller
to pass a callback function to delete an matched entry. The callback
function is called from an API of the radix tree (rn_walktree) but
also calls an API of the radix tree to delete an entry.

This change adds a new API of the radix tree, rn_search_matched,
which returns a matched entry that is selected by a callback
function passed by a caller and the caller itself deletes the
entry. By using the API, we can avoid the recursive form.
2016-11-15 01:50:06 +00:00
ozaki-r
cf96c34d79 Remove unnecessary argument
No functional change.
2016-10-25 02:45:09 +00:00
ozaki-r
d5985ea7e4 Revert v1.157
We need to hold the rtentry over rtrequest1 for info that dereferences
member variables of the rtentry after rtrequest1.
2016-10-24 03:19:07 +00:00
ozaki-r
01d48c0fd7 Delete rt_timers on RTM_DELETE surely
We want to ensure that a rtentry is referenced by nobody after
RTM_DELETE (except for the caller). However, rt_timer could
have a reference to the rtentry after that.
2016-10-21 10:56:35 +00:00
ozaki-r
951f676f30 Remove unnecessary argument
No functional change.
2016-10-21 10:52:47 +00:00
ozaki-r
302ac4ae0e Make some rt_timer functions and variables static
No functional change.
2016-10-21 09:01:44 +00:00
ozaki-r
e07d22aae6 Avoid temporal dangling reference 2016-10-21 03:04:33 +00:00
ozaki-r
3be3142886 Don't hold global locks if NET_MPSAFE is enabled
If NET_MPSAFE is enabled, don't hold KERNEL_LOCK and softnet_lock in
part of the network stack such as IP forwarding paths. The aim of the
change is to make it easy to test the network stack without the locks
and reduce our local diffs.

By default (i.e., if NET_MPSAFE isn't enabled), the locks are held
as they used to be.

Reviewed by knakahara@
2016-10-18 07:30:30 +00:00
ozaki-r
9b97df78c1 CID 1364759: fix using uninitialized value 2016-08-05 00:52:02 +00:00
ozaki-r
a403cbd4f5 Apply pserialize and psref to struct ifaddr and its variants
This change makes struct ifaddr and its variants (in_ifaddr and in6_ifaddr)
MP-safe by using pserialize and psref. At this moment, pserialize_perform
and psref_target_destroy are disabled because (1) we don't need them
because of softnet_lock (2) they cause a deadlock because of softnet_lock.
So we'll enable them when we remove softnet_lock in the future.
2016-08-01 03:15:30 +00:00
martin
17f84ba4fd Mark the rt_timer callout MPSAFE and move the first reset a few lines
down so the the workqueue is properly prepared (the latter being more
a cosmetical change). Ok: ozaki-r@
2016-07-15 09:25:47 +00:00
hannken
da7d165fe0 rtcache_clear_rtentry: use LIST_FOREACH_SAFE as the element gets
removed from the list.
2016-07-13 09:56:20 +00:00
ozaki-r
dca032f9f4 Run timers in workqueue
Timers (such as nd6_timer) typically free/destroy some data in callout
(softint). If we apply psz/psref for such data, we cannot do free/destroy
process in there because synchronization of psz/psref cannot be used in
softint. So run timer callbacks in workqueue works (normal LWP context).

Doing workqueue_enqueue a work twice (i.e., call workqueue_enqueue before
a previous task is scheduled) isn't allowed. For nd6_timer and
rt_timer_timer, this doesn't happen because callout_reset is called only
from workqueue's work. OTOH, ip{,6}flow_slowtimo's callout can be called
before its work starts and completes because the callout is periodically
called regardless of completion of the work. To avoid such a situation,
add a flag for each protocol; the flag is set true when a work is
enqueued and set false after the work finished. workqueue_enqueue is
called only if the flag is false.

Proposed on tech-net and tech-kern.
2016-07-11 07:37:00 +00:00
msaitoh
8bc54e5be6 KNF. Remove extra spaces. No functional change. 2016-07-07 06:55:38 +00:00
ozaki-r
17b4eb5edd Make sure to free all interface addresses in if_detach
Addresses of an interface (struct ifaddr) have a (reverse) pointer of an
interface object (ifa->ifa_ifp). If the addresses are surely freed when
their interface is destroyed, the pointer is always valid and we don't
need a tweak of replacing the pointer to if_index like mbuf.

In order to make sure the assumption, the following changes are required:
- Deactivate the interface at the firstish of if_detach. This prevents
  in6_unlink_ifa from saving multicast addresses (wrongly)
- Invalidate rtcache(s) and clear a rtentry referencing an address on
  RTM_DELETE. rtcache(s) may delay freeing an address
- Replace callout_stop with callout_halt of DAD timers to ensure stopping
  such timers in if_detach
2016-07-01 05:22:33 +00:00
ozaki-r
2cf7873b92 Constify rtentry of if_output
We no longer need to change rtentry below if_output.

The change makes it clear where rtentries are changed (or not)
and helps forthcoming locking (os psrefing) rtentries.
2016-04-28 00:16:56 +00:00
ozaki-r
3f11155830 Stop using rt_gwroute completely 2016-04-26 09:31:18 +00:00
ozaki-r
9e0f6c5e36 Stop using rt_gwroute on packet sending paths
rt_gwroute of rtentry is a reference to a rtentry of the gateway
for a rtentry with RTF_GATEWAY. That was used by L2 (arp and ndp)
to look up L2 addresses. By separating L2 nexthop caches, we don't
need a route for the purpose and we can stop using rt_gwroute.
By doing so, we can reduce referencing and modifying rtentries,
which makes it easy to apply a lock (and/or psref) to the
routing table and rtentries.

One issue to do this is to keep RTF_REJECT behavior. It seems it
was broken when we moved rtalloc1 things from L2 output routines
(e.g., ether_output) to ip_hresolv_output, but (fortunately?)
it works unexpectedly. What we mistook are:
- RTF_REJECT was checked for any routes in L2 output routines,
  but in ip_hresolv_output it is checked only when the route
  is RTF_GATEWAY
- The RTF_REJECT check wasn't copied to IPv6 (nd6_output)

It seems that rt_gwroute checks hid the mistakes and it looked
work (unexpectedly) and removing rt_gwroute checks unveil the
issue. So we need to fix RTF_REJECT checks in ip_hresolv_output
and also add them to nd6_output.

One more point we have to care is returning an errno; we need
to mimic looutput behavior. Originally RTF_REJECT check was
done either in L2 output routines or in looutput. The latter is
applied when a reject route directs to a loopback interface.
However, now RTF_REJECT check is done before looutput so to keep
the original behavior we need to return an errno which looutput
chooses. Added rt_check_reject_route does such tweaks.
2016-04-26 09:30:01 +00:00
ozaki-r
0c74cec625 Check error of rt_setgate and rt_settag 2016-04-25 14:38:08 +00:00
ozaki-r
156a79f975 Don't rt_setkey twice 2016-04-25 14:30:42 +00:00
ozaki-r
4f0eb37aac ddb: rename show arptab to show routes
show arptab command of ddb is now inappropriate because it actually dumps
routes but arp entries aren't routes anymore. So rename it to show routes
and move the code from if_arp.c to route.c.

ok christos@
2016-04-13 00:47:01 +00:00
ozaki-r
1fcfead163 Remove out-dated comments and unnecessary splsoftnet for pool_{get,put} 2016-04-11 08:26:33 +00:00
christos
c586dc1f9c remove useless cast. 2016-04-07 04:04:47 +00:00