Commit Graph

2531 Commits

Author SHA1 Message Date
ozaki-r
19c4d830db Protect mtudisc and redirect stuffs of icmp/icmp6 with mutex
We have to run pr_init of icmp and icmp6 prior to tcp and tcp6 ones
for mutex initialization.
2017-02-13 07:18:20 +00:00
ozaki-r
10400e2a88 Use IFQ_LOCK instead of splnet for if_snd 2017-02-13 04:06:39 +00:00
ozaki-r
b070ee09f7 Replace splnet with splsoftnet 2017-02-13 04:05:58 +00:00
roy
a3139fb2e1 Allow Unicast Poll from RFC 1122 to bypass DaD checking. 2017-02-11 15:37:30 +00:00
ozaki-r
57c38b2894 Add missing NULL checks for m_get_rcvif 2017-02-07 02:38:08 +00:00
ozaki-r
589739056f Defer some pr_input to workqueue
pr_input is currently called in softint. Some pr_input such as ICMP, ICMPv6
and CARP can add/delete/update IP addresses and routing table entries. For
example, icmp6_redirect_input updates an a routing table entry and
nd6_ra_input may delete an IP address.

Basically such operations shouldn't be done in softint. That aside, we have
a reason to avoid the situation; psz/psref waits cannot be used in softint,
however they are required to work in such pr_input in the MP-safe world.

The change implements the workqueue pr_input framework called wqinput which
provides a means to defer pr_input of a protocol to workqueue easily.
Currently icmp_input, icmp6_input, carp_proto_input and carp6_proto_input
are deferred to workqueue by the framework.

Proposed and discussed on tech-kern and tech-net
2017-02-02 02:52:10 +00:00
ozaki-r
9e8d969cf0 Tweak softnet_lock and NET_MPSAFE
- Don't hold softnet_lock in some functions if NET_MPSAFE
- Add softnet_lock to sysctl_net_inet_icmp_redirtimeout
- Add softnet_lock to expire_upcalls of ip_mroute.c
- Restore softnet_lock for in{,6}_pcbpurgeif{,0} if NET_MPSAFE
- Mark some softnet_lock for future work
2017-01-24 07:09:24 +00:00
ozaki-r
c26964ba3f Replace some splnet with splsoftnet 2017-01-23 10:19:03 +00:00
ozaki-r
14cc93cb28 Get rid of splnet for pool(9)
We don't need it anymore.
2017-01-23 09:14:24 +00:00
maxv
416bf33c7c Add some checks, mostly same as in_arpinput. 2017-01-21 11:07:46 +00:00
maxv
a87e9a2b62 Make sure the protocol address length equals that of IPv4. Also, make sure
the hardware address length equals that of the interface we received the
packet on. Otherwise a packet could easily set them both to zero and make
the kernel read beyond the allocated mbuf, which is terrible.

Note: for the latter we drop the packet instead of replying, since it is
malformed.

Note: I also added an ugly hack in CARP, since it apparently expects at
least six bytes.
2017-01-20 19:21:01 +00:00
maxv
cb01df4fa5 Style 2017-01-20 17:50:52 +00:00
maxv
0b9f08e68e Reput a nullcheck that was mistakenly removed in rev1.204. ar_hrd is
packet-controlled.
2017-01-20 17:45:42 +00:00
ozaki-r
fc198510fe Fix build w/ SCTP and w/o SCTP_DEBUG 2017-01-17 01:24:44 +00:00
christos
35561f6b22 ip6_sprintf -> IN6_PRINT so that we pass the size. 2017-01-16 15:44:46 +00:00
christos
fcb36c6a50 really, use. 2017-01-16 15:44:05 +00:00
christos
f068397dd4 rename arplog -> ARPLOG to make it clear that it is a macro and tuck-in the
buffer used for address formatting.
2017-01-16 15:14:16 +00:00
ryo
28f4c24cc2 Make ip6_sprintf(), in_fmtaddr(), lla_snprintf() and icmp6_redirect_diag() mpsafe.
Reviewed by ozaki-r@
2017-01-16 07:33:36 +00:00
ozaki-r
2b82ef9b8f Get rid of unnecessary header inclusions 2017-01-11 13:08:29 +00:00
christos
1aeddccb19 add a couple of lint comments. 2017-01-10 20:32:27 +00:00
knakahara
23e409fe79 avoid double rtcache_unref().
reviewed by ozaki-r@n.o.
2017-01-10 07:39:52 +00:00
knakahara
cc189cdb90 remove unnecessary conversion.
gif_softc->gif_pdst is already valid sockaddr.
2017-01-06 03:25:13 +00:00
martin
68f5a34706 Fix optlen calculation for the SACK block - 2 bytes too few were
calculated, causing corruption in PR kern/51767.
2017-01-04 15:09:37 +00:00
kre
c6fa5aa928 Remove redundant tests: if optlen === 0, then optlen % 4 != 2 (it is 0)
so there is no need to test both.
2017-01-04 12:35:14 +00:00
christos
106cdf0378 use symbolic constants; no functional change. 2017-01-03 20:59:32 +00:00
christos
0a91c122c2 put it the way we had it before; since we check for the resulting size after
we added the extra space we can be equal to the size of the buffer.
2017-01-03 15:07:59 +00:00
christos
9a6f404c92 fix off-by-one 2017-01-03 13:09:33 +00:00
christos
2bddf11345 - You can't just call the pfil hook to remove an address before an address
is removed! Hold a reference instead, remove it, and then free it.
- GC iatoifa()
2017-01-02 23:00:25 +00:00
skrll
4c7fdffb38 Restore behaviour to pre- tcp_congctl.c:1.18 for SACK. Further analysis
of the change is required.

OK kefren@

PR/51753 tcp SACK causes SSH disconnect
2017-01-02 09:29:38 +00:00
christos
5e3bbad550 make sure that the reset label is defined without TCP_SIGNATURE. 2017-01-02 02:38:54 +00:00
christos
65dfd4cc7e Fix TCP signature code:
1. pack options more tightly instead of being generous with no/op
2. put TCP_SIGNATURE option before SACK
3. fix computation of options length, by deferring it
XXX: Really we should move the options setting code in one place instead
of having two copies one for input and one for output.
XXX: tcp_optlen/tcp_hdrsiz need to be fixed; they were wrong before too.
2017-01-02 01:18:42 +00:00
christos
64f28c9b37 remove ancient ipsec code, and don't conditionalize tcp signatures on ipsec_used 2016-12-31 22:46:46 +00:00
ryo
30456e82a3 In the case of SIOCDIFADDR, call pfil_run_addrhooks before release ia. 2016-12-31 09:41:05 +00:00
ozaki-r
b79bd95d27 Use ether_ifattach in carp_clone_create instead of C&P code
carp_clone_destroy calls ether_ifdetach so not calling ether_ifattach is
inconsistent. If we add something pair of initialization and destruction
to ether_ifattach and ether_ifdetach (e.g., mutex_init/mutex_destroy),
ether_ifdetach of carp_clone_destroy won't work. So use ether_ifattach.

In order to do so, make ether_ifattach accept the 2nd argument (lla) as
NULL to allow carp to initialize its link level address by itself.
2016-12-28 07:26:24 +00:00
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