Add support for using native sockets with the connectivity API. This
allows libraries that use the connectivity API to be tested in an
application with real connectivity, without external hardware.
Signed-off-by: Jordan Yates <jordan@embeint.com>
`nsos_connect_blocking()` is not returning the error as expected. It is
expected to return a negative `NSI_ERRNO_MID_...` value, but it is
returning a positive errno value. `nsos_connect_blocking()` should map
the errno value to a negative `NSI_ERRNO_MID_...`, which `nsos_connect()`
maps back to a -1 return with errno=`ECONNREFUSED`.
the current sequence of events:
- `ECONNREFUSED` occurs during connection
- `nsos_connect_blocking()` returns positive `ECONNREFUSED`
- `nsos_connect()` returns positive `ECONNREFUSED`
- `zsock_connect()` returns positive `ECONNREFUSED`
the expected sequence of events:
- `ECONNREFUSED` occurs during connection
- `nsos_connect_blocking()` returns negative `NSI_ERRNO_MID_ECONNREFUSED`
- `nsos_connect()` returns -1, errno=`ECONNREFUSED`
- `zsock_connect()` returns -1, errno=`ECONNREFUSED`
Signed-off-by: Noah Olson <noah@wavelynx.com>
Now that the native simulator has a common version of the error
conversion let's use that instead of a version specific for the
NSOS code.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Raise the poll signal when the socket is being closed to prevent users
of `zsock_poll` blocking after the socket is no more.
Signed-off-by: Jordan Yates <jordan@embeint.com>
Handle AF_UNIX family sockets for NSOS offloaded driver
Note that the size of struct sockaddr_un is done conditionnaly based on
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS
Also, NET_SOCKADDR_PTR_MAX_SIZE needs to be updated only if
CONFIG_NET_SOCKETS_PACKET is not set. Otherwise, for a AF_PACKET socket,
a struct net_context variable can be corrupted, as local would have be on
16 bytes instead of 20 bytes.
Signed-off-by: Noemie Gillet <ngillet@sequans.com>
There are 2-level switch statements (one switch inside another) which were
not properly terminated with 'break' statements, leading to implicit
fallthrough. Fix that.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
For each of the fdtable.h functions listed below, convert the
z_ prefixed semi-private functions to use the zvfs_ prefix.
ZVFS stands for Zephyr Virtual File System and
is intended to be a common library used by the C library,
POSIX API, Networking, Filesystem, and other areas.
There are already a few functions in fdtable.h that use the
zvfs_ prefix, so this change is mostly about unifying them in
a way that uses a suitable prefix ("namespace") so that it can
be considered a public API.
- z_alloc_fd
- z_fdtable_call_ioctl
- z_finalize_fd
- z_finalize_typed_fd
- z_free_fd
- z_get_fd_obj
- z_get_fd_obj_and_vtable
- z_get_obj_lock_and_cond
- z_reserve_fd
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Fill-in the mode field of the fd_entry so that the
implementation can be made aware that the specific file
descriptors created are sockets.
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Use poll() to wait for connect attempt to complete or timeout. That way
connect() does not block Native Simulator on host syscall.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
So far only a single blocking API could be handled simultaneously, due to
epoll_ctl(..., EPOLL_CTL_ADD, ...) returning -EEXIST when same file
descriptor was added twice.
Follow 'man epoll' advice about using dup() syscall to create a duplicate
file descriptor, which can be used with different events masks. Use such
duplicate for each blocking API except ioctl() (for handling Zephyr poll()
syscall).
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Introduce nsos_poll_if_blocking(), which replaces common code in:
* nsos_accept_with_poll()
* nsos_recvfrom_with_poll()
This will allow to introduce similar behavior for other blocking APIs in
subsequent commits.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
ioctl() API expects error codes to be returned as negative value directly,
instead of using 'errno'. Fix ZFD_IOCTL_POLL_PREPARE handling to respect
that.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
There is a goto statement with conversion using errno_from_nsos_mid(). Use
NSOS_MID_ERRNO instead of ERRNO to return proper error code.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
There is no reason to pass addrlen by pointer, since it is a read-only in
the context of sockaddr_to_nsos_mid().
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
zsock_poll_internal() iterates through all fds to call
ZFD_IOCTL_POLL_UPDATE on them. In cases when -EAGAIN is returned from one
of such syscalls (which happens for example for TLS sockets when in the
middle of a TLS handshake) whole fds array is iterated once again. This
means that ZFD_IOCTL_POLL_UPDATE can be called more than once for a single
preceding ZFD_IOCTL_POLL_PREPARE operation. This resulted in error in
nsos_adapt_poll_remove() call, which was not intended to be called twice.
In case ZFD_IOCTL_POLL_UPDATE is called second time, update 'revents' just
by calling poll() syscall on the host (Linux) side with 0 timeout.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Improve code consistency and fix CONFIG_POSIX_API=n compatibility with use
of ZSOCK_* and DNS_* macros.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
So far only non-blocking accept() and recvfrom() were suported. This patch
implements blocking behavior, with the use of poll(fd, POLLIN) as helper
mechanism.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Use NSI_HW_EVENT() in order to periodically check for events in host
sockets. Whenever there is a socket event ready to be processed by Zephyr,
raise native_sim (newly introduced) CPU interrupt, so that Zephyr driver
can signal readiness with k_poll().
Maintain a list of Zephyr poll() executions in Zephyr context. Iterate
through them whenever there is some event to be processed.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Add driver for 'native_sim' target that implements offloaded socket
networking by the use of host networking stack and wrapped BSD sockets API.
This driver has following advantages over existing networking drivers for
emulated platforms that are already in tree:
* no TUN/TAP use means that no additional setup is required on the host
side:
* possible to use it within unpriviledged Docker containers, either for
development or in CI
* possibility to use and test offloaded sockets
(CONFIG_NET_SOCKETS_OFFLOAD=y) with emulated target, which allows
to increase tests coverage of this feature, without requirement of using
hardware
Native Simulator host libc has different error codes than embedded libc
used by Zephyr. Convert between those.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>