Commit Graph

2497 Commits

Author SHA1 Message Date
ozaki-r
12da772ecc Fix panic in pfil_run_hooks on bootup
XXX a kernel with pf still fails to boot up. Please someone fix it.
2016-12-27 10:53:11 +00:00
knakahara
52f944e4de pserialize_perform() is required an additionally serialization. see pserialize(9).
ok by ozaki-r@n.o.
2016-12-26 00:30:07 +00:00
maya
4a3120403d Remove extraneous parentheses. no functional change
Appeases clang
2016-12-23 11:11:28 +00:00
knakahara
50b2b110c2 pserialize_perform() is required *after* PSLIST_WRITER_REMOVE. 2016-12-22 04:54:54 +00:00
ozaki-r
dd8638eea5 Move bpf_mtap and if_ipackets++ on Rx of each driver to percpuq if_input
The benefits of the change are:
- We can reduce codes
- We can provide the same behavior between drivers
  - Where/When if_ipackets is counted up
  - Note that some drivers still update packet statistics in their own
    way (periodical update)
- Moved bpf_mtap run in softint
  - This makes it easy to MP-ify bpf

Proposed on tech-kern and tech-net
2016-12-15 09:28:02 +00:00
ozaki-r
e8a9852807 Restore nd6.h inclusion to resolve implicit dependency 2016-12-15 03:54:15 +00:00
knakahara
237f476937 fix race of gif_softc->gif_ro when we send multiple flows over gif on NET_MPSAFE enabled kernel.
make gif_softc->gif_ro percpu as well as ipforward_rt to resolve this race.
and add future TODO comment for etherip(4).
2016-12-14 11:19:15 +00:00
ozaki-r
44375ea93d Remove unnecessary inclusions of nd6.h 2016-12-13 08:29:03 +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
a5540a4a8e Remove unnecessary forward struct declarations 2016-12-11 07:34:14 +00:00
ozaki-r
5baee62c70 Use psref for ip_rtaddr
ip_rtaddr will be sleepable soon. So use psref instead of pserialize.
2016-12-08 06:25:43 +00:00
ozaki-r
4c25fb2f83 Add rtcache_unref to release points of rtentry stemming from rtcache
In the MP-safe world, a rtentry stemming from a rtcache can be freed at any
points. So we need to protect rtentries somehow say by reference couting or
passive references. Regardless of the method, we need to call some release
function of a rtentry after using it.

The change adds a new function rtcache_unref to release a rtentry. At this
point, this function does nothing because for now we don't add a reference
to a rtentry when we get one from a rtcache. We will add something useful
in a further commit.

This change is a part of changes for MP-safe routing table. It is separated
to avoid one big change that makes difficult to debug by bisecting.
2016-12-08 05:16:33 +00:00
knakahara
ec7a5d403a add API to manipulate ifa->ia_hash and ia_hash_pslist_entry, and fix ia_hash_pslist_entry race by using them.
in_ifaddr_lock is required before writing ifa->ia_hash and
ia_hash_pslist_entry to serialize writer processings.

reviewed by ozaki-r@n.o.
2016-12-06 07:01:47 +00:00
knakahara
92613f0abe We must use PSLIST_ENTRY_DESTROY after PSLIST_WRITER_REMOVE and waiting all readers done.
And then, if we want to re-insert the removed pslist element, we need to
call PSLIST_ENTERY_INIT again.

advised by riastradh@n.o and reviewed by ozaki-r@n.o, thanks.
2016-11-18 10:38:55 +00:00
knakahara
2526d8f639 fix: "ifconfig destory" can stalls when "ifconfig" is done parallel.
This problem occurs only if NET_MPSAFE on.

ifconfig destroy side:
    kernel entry point is ifioctl => if_clone_destroy.
    pr_purgeif() acquires softnet_lock, and then ifa_remove() calls
    pserialize_perform() holding softnet_lock.
ifconfig side:
    kernel entry point is socreate.
    pr_attach()(udp_attach_wrapper()) calls sosetlock(). In this call path,
    sosetlock() try to acquire softnet_lock.
These can cause dead lock.
2016-11-18 06:50:04 +00:00
mrg
bbc9acc117 apply a #ifdef INET6 so the previous compiles without INET6. 2016-11-15 22:23:09 +00:00
mlelstv
845a599209 Enforce alignment requirements that are violated in some cases.
For machines that don't need strict alignment (i386,amd64,vax,m68k) this
is a no-op.

Fixes PR kern/50766 but should be improved.
2016-11-15 20:50:28 +00:00
ozaki-r
fe91c59d38 Cleanup/KNF tcp6_mtudisc
No functional change.
2016-11-09 03:33:30 +00:00
roy
7681c3aec3 Don't handle ARP duplication for the unspecified address. 2016-11-05 20:03:15 +00:00
ozaki-r
cf96c34d79 Remove unnecessary argument
No functional change.
2016-10-25 02:45:09 +00:00
ozaki-r
1219daf3b1 Remove unnecessary #ifdef IPSEC
The entire function is already in #ifdef IPSEC.

No functional change.
2016-10-19 01:13:01 +00:00
ozaki-r
14c3b51523 Set ia to ensure to call ia4_release 2016-10-19 01:10:15 +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
ee138b23cc Avoid double frees of mbuf
May fix one of panicks reported by Tom Ivar Helbekkmo in PR kern/51522
2016-10-18 01:15:20 +00:00
roy
d9119dc54f Implement RFC 5227 2.4 Ongoing Conflict Detection and Address Defence.
If ip_dad_count is 0, then the conflict is just logged and the address
is not marked as duplicated.
2016-10-11 13:59:30 +00:00
roy
dc1455f4fc Remove unused variable. 2016-10-11 13:39:34 +00:00
roy
103ec7fade Mark arprequest static and introduce arpannounce so that gratuitous
ARP requests are only send from valid addresses.
2016-10-11 12:32:30 +00:00
ozaki-r
20491bb993 Fix kernel builds with IFA_STATS 2016-10-11 05:15:01 +00:00
ozaki-r
8f4376cb6f Fix race condition on ifqueue used by traditional netisr
If a underlying network device driver supports MSI/MSI-X, RX interrupts
can be delivered to arbitrary CPUs. This means that Layer 2 subroutines
such as ether_input (softint) and subsequent Layer 3 subroutines (softint)
which are called via traditional netisr can be dispatched on an arbitrary
CPU. Layer 2 subroutines now run without any locks (expected) and so a
Layer 2 subroutine and a Layer 3 subroutine can run in parallel.

There is a shared data between a Layer 2 routine and a Layer 3 routine,
that is ifqueue and IF_ENQUEUE (from L2) and IF_DEQUEUE (from L3) on it
are racy now.

To fix the race condition, use ifqueue#ifq_lock to protect ifqueue
instead of splnet that is meaningless now.

The same race condition exists in route_intr. Fix it as well.

Reviewed by knakahara@
2016-10-03 11:06:06 +00:00
roy
de8590571b Default netmask to /32 for INET on POINTOPOINT links if not specified. 2016-10-01 17:17:20 +00:00
roy
aac6678f54 in_ifscrub is no longer needed. 2016-09-29 15:18:18 +00:00
roy
9288933cf3 Set dstaddr in in_ifinit so that sppp consumers announce the correct
dstaddr in routing messages.
2016-09-29 15:04:17 +00:00
roy
39d33d5a25 When changing an address via in_ifinit, ensure that the old address
is correctly scrubbed.
This allows sppp consumers to announce removal of the old address.
2016-09-29 14:18:38 +00:00
roy
0dbee937df Now that we disallow sending or receiving from invalid addresses,
allow binding to tentative addresses.
2016-09-29 12:19:47 +00:00
roy
8066689d53 Drop UDP packets as well as TCP without error when sending from detached or
tentative addresses.
2016-09-20 14:30:13 +00:00
christos
397177a1eb Dealing with arplog is a bit more complicated... 2016-09-18 02:17:43 +00:00
christos
2d3e3eb6e6 protect arplog with INET 2016-09-17 02:37:59 +00:00
roy
70c02d276f Drop hostIsNew from in_ifinit, let the function work out if the address
has changed.
Sync address flag setup with the IPv6 counterpart.
When scrubbing the address, or setting up the address fails, restore the
old address flags as well as the old address.
2016-09-16 14:17:23 +00:00
roy
68a9e8e0bd Clear IN_IFF_TENTATIVE when stopping DaD here. 2016-09-16 13:47:47 +00:00
roy
26b6b5b9e8 Don't setup DaD for INADDR_ANY 2016-09-16 09:59:45 +00:00
roy
8c6871896f Ensure that packets are sent from a valid address.
If the packet is TCP and the address is detached or tentative then
it's just dropped, otherwise an error is returned.

This is needed because you can bind to a valid address and it can then
become invalid.

This satisfies RFC 4862 section 5.5.4.
2016-09-15 18:25:45 +00:00
roy
bddde31ef2 Allow arplog to be used outside of if_arp.c 2016-09-15 18:17:29 +00:00
christos
959c247a60 revert previous, roy says it breaks DaD. 2016-09-13 15:57:50 +00:00
christos
acab31252a When initializing addresses, reset the interface flags to 0. This fixes
an issue where point to point addresses that started down, and then came
up, were left with stale flags on one side of the point to point link.
2016-09-13 15:41:33 +00:00
christos
647765d084 remove trailing spaces. userland does not catch this? 2016-09-13 00:45:15 +00:00
christos
47afd135ed add bits for address flags 2016-09-13 00:19:28 +00:00
roy
3e6930820d Disallow input to detached addresses because they are not yet valid. 2016-09-07 15:41:44 +00:00
roy
ec17a3e1f4 Refine arplog to be like nd6log. 2016-09-07 13:01:39 +00:00
ozaki-r
43fe10b43b Apply psz/psref to remaining IFADDR_READER_FOREACH
Pointed out by ryo@
2016-09-01 04:27:00 +00:00
ozaki-r
543e39c0d3 Make ipforward_rt and ip6_forward_rt percpu
Sharing one rtcache between CPUs is just a bad idea.

Reviewed by knakahara@
2016-08-31 09:14:47 +00:00