Commit graph

293 commits

Author SHA1 Message Date
Mark Johnston 44d36c9731 netmap: Use device_set_descf()
No functional change intended.

MFC after:	1 week
2024-06-16 16:37:26 -04:00
Tom Jones 73fdbfb911 netmap: Address errors on memory free in netmap_generic
netmap_generic keeps a pool of mbufs for handling transfers, these mbufs
have an external buffer attached to them.

If some cases other parts of the network stack can chain these mbufs,
when this happens the normal pool destructor function can end up
free'ing the pool mbufs twice:

- A first time if a pool mbuf has been chained with another mbuf when
  its chain is freed
- A second time when its entry in the pool is freed

Additionally, if other parts of the stack demote a pool mbuf its
interface reference will be cleared. In this case we deference a NULL
pointer when trying to free the mbuf through the destructor. Store a
reference to the adapter in ext_arg1 with the destructor callback so we
can find the correct adapter when free'ing a pool mbuf.

This change enables using netmap with epair interfaces.

Reviewed By:	vmaffione
MFC after:	1 week
Relnotes:	yes
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D44371
2024-03-26 09:55:55 +00:00
Mark Johnston 99efa2c88d netmap: Ignore errors in CSB_WRITE()
The CSB_WRITE() and _READ() macros respectively write to and read from
userspace memory and so can in principle fault.  However, we do not
check for errors and will proceed blindly if they fail.  Add assertions
to verify that they do not.

This is in preparation for annotating copyin() and related functions
with __result_use_check.

Reviewed by:	vmaffione
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D43200
2023-12-27 10:13:29 -05:00
Warner Losh fdafd315ad sys: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:24:00 -07:00
Warner Losh 685dc743dc sys: Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:36 -06:00
Warner Losh 71625ec9ad sys: Remove $FreeBSD$: one-line .c comment pattern
Remove /^/[*/]\s*\$FreeBSD\$.*\n/
2023-08-16 11:54:24 -06:00
Warner Losh 2ff63af9b8 sys: Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:18 -06:00
Warner Losh 95ee2897e9 sys: Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:11 -06:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix
2023-05-12 10:44:03 -06:00
Mark Johnston 5f6d37787f netmap: Handle packet batches in generic mode
ifnets are allowed to pass batches of multiple packets to if_input,
linked by the m_nextpkt pointer.  iflib_rxeof() sometimes does this, for
example.  Netmap's generic mode did not handle this and would only
deliver the first packet in the batch, leaking the rest.

PR:		270636
Reviewed by:	vmaffione
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D39426
2023-04-05 17:07:48 -04:00
Mark Johnston ce12afaa6f netmap: Fix queue stalls with generic interfaces
In emulated mode, the FreeBSD netmap port attempts to perform zero-copy
transmission.  This works as follows: the kernel ring is populated with
mbuf headers to which netmap buffers are attached.  When transmitting,
the mbuf refcount is initialized to 2, and when the counter value has
been decremented to 1 netmap infers that the driver has freed the mbuf
and thus transmission is complete.

This scheme does not generalize to the situation where netmap is
attaching to a software interface which may transmit packets among
multiple "queues", as is the case with bridge or lagg interfaces.  In
that case, we would be relying on backing hardware drivers to free
transmitted mbufs promptly, but this isn't guaranteed; a driver may
reasonably defer freeing a small number of transmitted buffers
indefinitely.  If such a buffer ends up at the tail of a netmap transmit
ring, further transmits can end up blocked indefinitely.

Fix the problem by removing the zero-copy scheme (which is also not
implemented in the Linux port of netmap).  Instead, the kernel ring is
populated with regular mbuf clusters into which netmap buffers are
copied by nm_os_generic_xmit_frame().  The refcounting scheme is
preserved, and this lets us avoid allocating a fresh cluster per
transmitted packet in the common case.  If the transmit ring is full, a
callout is used to free the "stuck" mbuf, avoiding the queue deadlock
described above.

Furthermore, when recycling mbuf clusters, be sure to fully reinitialize
the mbuf header instead of simply re-setting M_PKTHDR.  Some software
interfaces, like if_vlan, may set fields in the header which should be
reset before the mbuf is reused.

Reviewed by:	vmaffione
MFC after:	1 month
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38065
2023-04-05 12:12:30 -04:00
Vincenzo Maffione e2a431a0ff netmap: fix copyin/copyout of nmreq options list
The previous code unsuccesfully attempted to report a precise error for
each option in the user list. Moreover, commit 253b2ec199 broke some
ctrl-api-test (see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=260547).

With this patch we bail out as soon as an unrecoverable error is detected and
we properly check for copy boundaries. EOPNOTSUPP no longer immediately
returns an error, so that any other option in the list may be examined
by the caller code and a precise report of the (un)supported options can
be returned to the user.

With this patch, all ctrl-api-test unit tests pass again.

PR:			260547
Submitted by:		giuseppe.lettieri@unipi.it
Reviewed by:		vmaffione
MFC after:		14 days
2023-03-21 23:23:18 +00:00
Vincenzo Maffione 6c9fe35743 netmap: get rid of save_if_input for emulated adapters
The save_if_input function pointer was meant to save the previous
value of ifp->if_input before replacing it with the emulated
adapter hook.
However, the same pointer value is already stored in the if_input
field of the netmap_adapter struct, to be used for host TX ring processing.

Reuse the netmap_adapter if_input field to simplify the code
and save some space.

MFC after:	14 days
2023-03-14 22:04:23 +00:00
Vincenzo Maffione 22bf2a479f netmap: get rid of WNA() macro
MFC after:	7 days
2023-03-11 17:19:40 +00:00
Mark Johnston 626d1e4a82 netmap: Remove obsolete compatibility defines
No functional change intended.

Reviewed by:	vmaffione
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D39001
2023-03-09 18:16:53 -05:00
Justin Hibbits e330262f34 Mechanically convert netmap(4) to IfAPI
Reviewed by:	vmaffione, zlei
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D37814
2023-02-14 10:21:19 -05:00
Vincenzo Maffione fa3f665542 netmap: drop redundant if_mtu assignment
Reported by:	zlei
MFC after	3 days
2023-02-08 21:05:32 +00:00
Mark Johnston df40e30c97 netmap: Try to count packet drops in emulated mode
Right now we have little visibility into packet drops within netmap.
Start trying to make packet loss issues more visible by counting queue
drops in the transmit path, and in the input path for interfaces running
in emulated mode, where we place received packets in a bounded software
queue that is processed by rxsync.

Reviewed by:	vmaffione
MFC after:	1 week
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38064
2023-01-23 14:42:41 -05:00
Mark Johnston 854b2f302d netmap: Correct a comment
Reviewed by:	vmaffione
MFC after:	1 week
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38063
2023-01-23 14:42:19 -05:00
Mark Johnston 539437c828 netmap: Fix a queue length check in the generic port rx path
The check is ok by default, since the default value of
netmap_generic_ringsize is 1024.  But we should check against the
configured "ring" size.

Reviewed by:	vmaffione
MFC after:	1 week
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38062
2023-01-23 14:41:55 -05:00
Mark Johnston 56c438fcd4 netmap: Tell the compiler to avoid reloading ring indices
Per the removed comments these fields should be loaded only once, since
they can in principle be modified concurrently, though this would be a
violation of the userspace contract with netmap.

No functional change intended.

Reviewed by:	vmaffione
MFC after:	1 week
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38061
2023-01-23 14:41:05 -05:00
Tom Jones 110ce09c90 if_lagg: Allow lagg interfaces to be used with netmap
Reviewed by:	zlei
Sponsored by:	Zenarmor
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D37436
2023-01-13 15:31:58 +00:00
Vincenzo Maffione 95fc11577d netmap: monitor: add missing netmap_adapter_put()
MFC after:	7 days
2022-12-24 16:03:14 +00:00
Vincenzo Maffione 92e8b4a630 debug_put_get: don't crash on null pointers
MFC after:	7 days
2022-12-24 15:32:12 +00:00
Vincenzo Maffione b41818a28a monitor: fix cb restore when monitored adapter unregisters
netmap_monitor_stop() called nm_monitor_none() after the head of
the zero-copy monitors had been reset, thus thinking that there
was nothing left to do.

MFC after:	7 days
2022-12-24 15:28:45 +00:00
Vincenzo Maffione 3da494d324 netmap: drop compatibility FreeBSD code
Netmap users on FreeBSD are not supposed to import code from the
github netmap repository anymore. They should use the code that
is available in the src repo. We can therefore drop the compatibility
code.

MFC after:	7 days
2022-12-24 14:36:31 +00:00
Vincenzo Maffione 4ad57c7afa netmap_update_config: update na->name to cope with reconfigurations
MFC after:	1 week
2022-12-03 18:12:04 +00:00
Elliott Mitchell 21cc0918c7 sys: Nuke double-semicolons
A distinct number of double-semicolons have ended up in FreeBSD.  Take a
pass at getting rid of many of these harmless typos.

Reviewed by: emaste, rrs
Pull Request: https://github.com/freebsd/freebsd-src/pull/609
Differential Revision: https://reviews.freebsd.org/D31716
2022-11-02 09:34:20 -06:00
Gordon Bergling 591a9b5ea3 netmap(4): Remove a double word in a source code comment
- s/the the/the/

MFC after:	3 days
2022-09-04 13:42:48 +02:00
Dimitry Andric 8bfedf5852 Fix unused variable warning in if_re_netmap.h
With clang 15, the following -Werror warning is produced:

    sys/dev/netmap/if_re_netmap.h:179:8: error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
            u_int n;
                  ^

The 'n' variable appears to have been a debugging aid that has never
been used for anything, so remove it.

MFC after:	3 days
2022-07-21 21:52:39 +02:00
John Baldwin a9424e0f06 netmap: Remove unused devclass arguments to DRIVER_MODULE. 2022-05-10 10:21:39 -07:00
John Baldwin 6d2a46f0ab netmap: Move more_txspace and some_recvd under #ifdef SYNC_KLOOP_POLL. 2022-04-08 17:30:14 -07:00
Vincenzo Maffione 3937299165 netmap: Fix TOCTOU vulnerability in nmreq_copyin
The total size of the user-provided nmreq was first computed and then
trusted during the copyin. This might lead to kernel memory corruption
and escape from jails/containers.

Reported by: Lucas Leong (@_wmliang_) of Trend Micro Zero Day Initiative
Security: CVE-2022-23084
MFC after:	3 days
2022-03-16 06:58:50 +00:00
Vincenzo Maffione 694ea59c70 netmap: Fix integer overflow in nmreq_copyin
An unsanitized field in an option could be abused, causing an integer
overflow followed by kernel memory corruption. This might be used
to escape jails/containers.

Reported by: Reno Robert and Lucas Leong (@_wmliang_) of Trend Micro
Zero Day Initiative
Security: CVE-2022-23085
2022-03-16 06:57:54 +00:00
Vincenzo Maffione dd6ab49a9a netmap: add a tunable for the maximum number of VALE switches
The new dev.netmap.max_bridges sysctl tunable can be set in
loader.conf(5) to change the default maximum number of VALE
switches that can be created. Current defaults is 8.

MFC after:	2 weeks
2022-03-06 17:29:44 +00:00
Vincenzo Maffione 09a1893398 netmap: fix refcount bug in netmap allocator
Symptom: when a single extmem memory region is provided to netmap
multiple times, for multiple interfaces, the memory region is
never released by netmap once all the existing file descriptors
are closed.

Fix the relevant condition in netmap_mem_drop(): release the memory
when the last user of netmap_adapter is gone, rather then when
the last user of netmap_mem_d is gone.

MFC after:	2 weeks
2022-03-06 16:39:16 +00:00
Vincenzo Maffione 3e3314a8b7 netmap: fix uint32_t overflow in pool size calculation
MFC after:	1 week
2021-09-26 13:56:33 +00:00
Vincenzo Maffione 6127ce9d91 netmap: monitor: support offsets in copy mode 2021-09-26 13:52:16 +00:00
Vincenzo Maffione 98399ab06f netmap: import changes from upstream
- make sure rings are disabled during resets
 - introduce netmap_update_hostrings_mode(), with support
   for multiple host rings
 - always initialize ni_bufs_head in netmap_if
      ni_bufs_head was not properly initialized when no external buffers were
      requestedx and contained the ni_bufs_head from the last request. This
      was causing spurious buffer frees when alternating between apps that
      used external buffers and apps that did not use them.
 - check na validitity under lock on detach
 - netmap_mem: fix leak on error path
 - nm_dispatch: fix compilation on Raspberry Pi

MFC after:	2 weeks
2021-08-22 09:31:05 +00:00
Vincenzo Maffione f4a54f4333 netmap: use safer defaults for hwbuf_len
We must make sure that incoming packets will never overflow the netmap
buffers, even when the user is using the offset feature. In the typical
scenario, the netmap buffer is 2KiB and, with an MTU of 1500, there are
~500 bytes available for user offsets.

Unfortunately, some NICs accept incoming packets even when they are
larger then the MTU. This means that the only way to stop DMA from
overflowing the netmap buffers, when offsets are allowed, is to choose
a hardware buffer length which is smaller than the netmap buffer
length. For most NICs and for 2KiB netmap buffers, this means 1024
bytes, which is unconveniently small.

The current code will select the small hardware buf size even when
offsets are not     in use. The main purpose of this change is to
fix this bug by returning to the normal behavior for the no-offsets
case.

At the same time, the patch pushes the handling of the offset case
to the lower level driver code, so that it can be made NIC-specific
(in future patches).
2021-04-18 13:39:15 +00:00
Cy Schubert b51f459a20 wpa: Import wpa_supplicant/hostapd commit f91680c15
This is the April update to vendor/wpa committed upstream
2021/04/07.

This is MFV efec822389.

Suggested by:		philip
Reviewed by:		philip
MFC after:		2 months
Differential Revision:	https://reviews.freebsd.org/D29744
2021-04-17 07:21:12 -07:00
Vincenzo Maffione 13c4641188 netmap: make sure rings are disabled during resets
Explicitly disable ring synchronization before calling
callbacks that may result in a hardware reset.

Before this patch we relied on capturing the down/up events which,
however, may not be issued by all drivers.
2021-04-17 14:02:47 +00:00
Vincenzo Maffione 70275a6735 netmap: don't use linux type struct device *
Such type cannot be used in code that is in common between
FreeBSD and Linux. Use the FreeBSD type instead.

MFC after:	3 days
Reported by:	markj
Differential Revision:	https://reviews.freebsd.org/D29677
2021-04-11 21:13:01 +00:00
Vincenzo Maffione 172c5eb272 netmap: vtnet: remove unused variable
Reported by:	bdragon
2021-04-09 19:33:41 +00:00
Vincenzo Maffione 15dc713ceb netmap: vtnet: add support for netmap offsets
Follow-up change to a6d768d845.
This change adds support for netmap offsets.
2021-04-07 21:32:20 +00:00
Vincenzo Maffione 45c67e8f6b netmap: several typo fixes
No functional changes intended.
2021-04-02 07:01:20 +00:00
Vincenzo Maffione 66671ae589 netmap: fix typo bug in netmap_compute_buf_len 2021-04-02 06:47:28 +00:00
Vincenzo Maffione 660a47cb99 netmap: monitor: add a flag to distinguish packet direction
The netmap monitor intercepts any TX/RX packets on the monitored
port. However, before this change there was no way to tell
whether an intercepted packet was being transmitted or received
on the monitored port.
A TXMON flag in the netmap slot has been added for this purpose.
2021-03-29 16:32:54 +00:00
Vincenzo Maffione a6d768d845 netmap: add kernel support for the "offsets" feature
This feature enables applications to ask netmap to transmit or
receive packets starting at a user-specified offset from the
beginning of the netmap buffer. This is meant to ease those
packet manipulation operations such as pushing or popping packet
headers, that may be useful to implement software switches,
routers and other packet processors.
To use the feature, drivers (e.g., iflib, vtnet, etc.) must have
explicit support. This change does not add support for any driver,
but introduces the necessary kernel changes. However, offsets support
is already included for VALE ports and pipes.
2021-03-29 16:29:01 +00:00
Vincenzo Maffione ee7ffaa2e6 netmap: fix issues in nm_os_extmem_create()
- Call vm_object_reference() before vm_map_lookup_done().
- Use vm_mmap_to_errno() to convert vm_map_* return values to errno.
- Fix memory leak of e->obj.

Reported by:	markj
Reviewed by:	markj
MFC after:	1 week
2021-03-20 17:15:50 +00:00