drivers: net: nsos: add F_GETFL and F_SETFL support

This allows to use Zephyr TLS subsystem with Native Sim offloaded sockets.

Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
This commit is contained in:
Marcin Niestroj 2024-02-22 15:01:05 +01:00 committed by Carles Cufí
parent 4317cd4576
commit 483c41d209
6 changed files with 158 additions and 0 deletions

View file

@ -15,9 +15,11 @@ if(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
${ZEPHYR_BASE}/subsys/net/lib/sockets
)
zephyr_library_sources(nsos_errno.c)
zephyr_library_sources(nsos_fcntl.c)
zephyr_library_sources(nsos_netdb.c)
zephyr_library_sources(nsos_sockets.c)
target_sources(native_simulator INTERFACE nsos_adapt.c)
target_sources(native_simulator INTERFACE nsos_errno.c)
target_sources(native_simulator INTERFACE nsos_fcntl.c)
target_sources(native_simulator INTERFACE nsos_netdb.c)
endif()

View file

@ -110,6 +110,9 @@ int nsos_adapt_recvfrom(int fd, void *buf, size_t len, int flags,
struct nsos_mid_sockaddr *addr, size_t *addrlen);
int nsos_adapt_fcntl_getfl(int fd);
int nsos_adapt_fcntl_setfl(int fd, int flags);
int nsos_adapt_getaddrinfo(const char *node, const char *service,
const struct nsos_mid_addrinfo *hints,
struct nsos_mid_addrinfo **res,

View file

@ -11,6 +11,7 @@
*/
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
@ -21,6 +22,7 @@
#include "nsos.h"
#include "nsos_errno.h"
#include "nsos_fcntl.h"
#include "nsos_netdb.h"
#include "nsi_tracing.h"
@ -596,3 +598,24 @@ void nsos_adapt_freeaddrinfo(struct nsos_mid_addrinfo *res_mid)
freeaddrinfo(wrap->addrinfo);
free(wrap);
}
int nsos_adapt_fcntl_getfl(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
return fl_to_nsos_mid(flags);
}
int nsos_adapt_fcntl_setfl(int fd, int flags)
{
int ret;
ret = fcntl(fd, F_SETFL, fl_from_nsos_mid(flags));
if (ret < 0) {
return -errno_to_nsos_mid(errno);
}
return 0;
}

84
drivers/net/nsos_fcntl.c Normal file
View file

@ -0,0 +1,84 @@
/**
* Copyright (c) 2023-2024 Marcin Niestroj
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* fcntl.h related code common to Zephyr (top: nsos_sockets.c) and Linux
* (bottom: nsos_adapt.c).
*
* It is needed by both sides to share the same macro definitions/values
* (prefixed with NSOS_MID_), which is not possible to achieve with two separate
* standard libc libraries, since they use different values for the same
* symbols.
*/
#include <fcntl.h>
#include "nsos_errno.h"
#include "nsos_fcntl.h"
#include <stdbool.h>
static int fl_to_nsos_mid_(int flags, bool strict)
{
int flags_mid = 0;
#define TO_NSOS_MID(_flag) \
if (flags & (_flag)) { \
flags &= ~(_flag); \
flags_mid |= NSOS_MID_ ## _flag; \
}
TO_NSOS_MID(O_RDONLY);
TO_NSOS_MID(O_WRONLY);
TO_NSOS_MID(O_RDWR);
TO_NSOS_MID(O_APPEND);
TO_NSOS_MID(O_EXCL);
TO_NSOS_MID(O_NONBLOCK);
#undef TO_NSOS_MID
if (strict && flags != 0) {
return -NSOS_MID_EINVAL;
}
return flags_mid;
}
int fl_to_nsos_mid(int flags)
{
return fl_to_nsos_mid_(flags, false);
}
int fl_to_nsos_mid_strict(int flags)
{
return fl_to_nsos_mid_(flags, true);
}
int fl_from_nsos_mid(int flags_mid)
{
int flags = 0;
#define FROM_NSOS_MID(_flag) \
if (flags_mid & NSOS_MID_ ## _flag) { \
flags_mid &= ~NSOS_MID_ ## _flag; \
flags |= _flag; \
}
FROM_NSOS_MID(O_RDONLY);
FROM_NSOS_MID(O_WRONLY);
FROM_NSOS_MID(O_RDWR);
FROM_NSOS_MID(O_APPEND);
FROM_NSOS_MID(O_EXCL);
FROM_NSOS_MID(O_NONBLOCK);
#undef FROM_NSOS_MID
return flags;
}

22
drivers/net/nsos_fcntl.h Normal file
View file

@ -0,0 +1,22 @@
/**
* Copyright (c) 2023-2024 Marcin Niestroj
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __DRIVERS_NET_NSOS_FCNTL_H__
#define __DRIVERS_NET_NSOS_FCNTL_H__
#define NSOS_MID_O_RDONLY 00
#define NSOS_MID_O_WRONLY 01
#define NSOS_MID_O_RDWR 02
#define NSOS_MID_O_APPEND 0x0400
#define NSOS_MID_O_EXCL 0x0800
#define NSOS_MID_O_NONBLOCK 0x4000
int fl_to_nsos_mid(int flags);
int fl_to_nsos_mid_strict(int flags);
int fl_from_nsos_mid(int flags);
#endif /* __DRIVERS_NET_NSOS_FCNTL_H__ */

View file

@ -18,11 +18,13 @@
#include <zephyr/net/net_ip.h>
#include <zephyr/net/offloaded_netdev.h>
#include <zephyr/net/socket_offload.h>
#include <zephyr/posix/fcntl.h>
#include <zephyr/sys/fdtable.h>
#include "sockets_internal.h"
#include "nsos.h"
#include "nsos_errno.h"
#include "nsos_fcntl.h"
#include "nsos_netdb.h"
#include "nsi_host_trampolines.h"
@ -227,6 +229,28 @@ static int nsos_ioctl(void *obj, unsigned int request, va_list args)
case ZFD_IOCTL_POLL_OFFLOAD:
return -EOPNOTSUPP;
case F_GETFL: {
int flags;
flags = nsos_adapt_fcntl_getfl(OBJ_TO_FD(obj));
return fl_from_nsos_mid(flags);
}
case F_SETFL: {
int flags = va_arg(args, int);
int ret;
ret = fl_to_nsos_mid_strict(flags);
if (ret < 0) {
return -errno_from_nsos_mid(-ret);
}
ret = nsos_adapt_fcntl_setfl(OBJ_TO_FD(obj), flags);
return -errno_from_nsos_mid(-ret);
}
}
return -EINVAL;