Commit b3d3d4ff authored by Chris Friedt's avatar Chris Friedt Committed by Henrik Brix Andersen
Browse files

net: sockets: move select() implementation to zvfs



Move the implementation of zsock_select() to zvfs_select(). This
allows other types of file descriptors to also make use of
select() functionality even when the network subsystem is not
enabled.

Additionally, it partially removes a dependency cycle between
posix and networking by moving functionality into a mutual
dependency.

Signed-off-by: default avatarChris Friedt <cfriedt@tenstorrent.com>
parent 881dc1fa
Loading
Loading
Loading
Loading
+28 −14
Original line number Diff line number Diff line
@@ -19,17 +19,18 @@
 * @{
 */

#include <time.h>

#include <zephyr/toolchain.h>
#include <zephyr/net/socket_types.h>
#include <zephyr/sys/fdtable.h>

#ifdef __cplusplus
extern "C" {
#endif

/** Socket file descriptor set. */
typedef struct zsock_fd_set {
	uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
} zsock_fd_set;
typedef struct zvfs_fd_set zsock_fd_set;

/**
 * @brief Legacy function to poll multiple sockets for events
@@ -47,13 +48,16 @@ typedef struct zsock_fd_set {
 * it may conflict with generic POSIX ``select()`` function).
 * @endrst
 */
__syscall int zsock_select(int nfds, zsock_fd_set *readfds,
			   zsock_fd_set *writefds,
			   zsock_fd_set *exceptfds,
			   struct zsock_timeval *timeout);
static inline int zsock_select(int nfds, zsock_fd_set *readfds, zsock_fd_set *writefds,
			       zsock_fd_set *exceptfds, struct zsock_timeval *timeout)
{
	struct timeval;

	return zvfs_select(nfds, readfds, writefds, exceptfds, (struct timeval *)timeout);
}

/** Number of file descriptors which can be added to zsock_fd_set */
#define ZSOCK_FD_SETSIZE (sizeof(((zsock_fd_set *)0)->bitset) * 8)
#define ZSOCK_FD_SETSIZE ZVFS_FD_SETSIZE

/**
 * @brief Initialize (clear) fd_set
@@ -67,7 +71,10 @@ __syscall int zsock_select(int nfds, zsock_fd_set *readfds,
 * if :kconfig:option:`CONFIG_POSIX_API` is defined.
 * @endrst
 */
void ZSOCK_FD_ZERO(zsock_fd_set *set);
static inline void ZSOCK_FD_ZERO(zsock_fd_set *set)
{
	ZVFS_FD_ZERO(set);
}

/**
 * @brief Check whether socket is a member of fd_set
@@ -81,7 +88,10 @@ void ZSOCK_FD_ZERO(zsock_fd_set *set);
 * if :kconfig:option:`CONFIG_POSIX_API` is defined.
 * @endrst
 */
int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
static inline int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set)
{
	return ZVFS_FD_ISSET(fd, set);
}

/**
 * @brief Remove socket from fd_set
@@ -95,7 +105,10 @@ int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
 * if :kconfig:option:`CONFIG_POSIX_API` is defined.
 * @endrst
 */
void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
static inline void ZSOCK_FD_CLR(int fd, zsock_fd_set *set)
{
	ZVFS_FD_CLR(fd, set);
}

/**
 * @brief Add socket to fd_set
@@ -109,7 +122,10 @@ void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
 * if :kconfig:option:`CONFIG_POSIX_API` is defined.
 * @endrst
 */
void ZSOCK_FD_SET(int fd, zsock_fd_set *set);
static inline void ZSOCK_FD_SET(int fd, zsock_fd_set *set)
{
	ZVFS_FD_SET(fd, set);
}

/** @cond INTERNAL_HIDDEN */

@@ -153,8 +169,6 @@ static inline void FD_SET(int fd, zsock_fd_set *set)
}
#endif

#include <zephyr/syscalls/socket_select.h>

/**
 * @}
 */
+8 −6
Original line number Diff line number Diff line
@@ -13,16 +13,18 @@
extern "C" {
#endif

#undef fd_set
#define fd_set zsock_fd_set
#define FD_SETSIZE ZSOCK_FD_SETSIZE
#define FD_ZERO ZSOCK_FD_ZERO
#define FD_SET ZSOCK_FD_SET
#define FD_CLR ZSOCK_FD_CLR
#define FD_ISSET ZSOCK_FD_ISSET

#define FD_SETSIZE ZVFS_FD_SETSIZE

struct timeval;

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);

#ifdef __cplusplus
}
+22 −2
Original line number Diff line number Diff line
@@ -7,9 +7,10 @@
#define ZEPHYR_INCLUDE_SYS_FDTABLE_H_

#include <stdarg.h>
#include <sys/types.h>
#include <time.h>

/* FIXME: For native_posix ssize_t, off_t. */
#include <zephyr/fs/fs.h>
#include <sys/types.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>

@@ -207,6 +208,23 @@ struct zvfs_pollfd {

__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);

struct zvfs_fd_set {
	uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
};

/** @brief Number of file descriptors which can be added @ref zvfs_fd_set */
#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8)

void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);

__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
			  struct zvfs_fd_set *ZRESTRICT writefds,
			  struct zvfs_fd_set *ZRESTRICT errorfds,
			  const struct timeval *ZRESTRICT timeout);

/**
 * Request codes for fd_op_vtable.ioctl().
 *
@@ -236,4 +254,6 @@ enum {
}
#endif

#include <zephyr/syscalls/fdtable.h>

#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */
+1 −0
Original line number Diff line number Diff line
@@ -3,3 +3,4 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_ZVFS_EVENTFD zvfs_eventfd.c)
zephyr_library_sources_ifdef(CONFIG_ZVFS_POLL zvfs_poll.c)
zephyr_library_sources_ifdef(CONFIG_ZVFS_SELECT zvfs_select.c)
+5 −0
Original line number Diff line number Diff line
@@ -49,6 +49,11 @@ config ZVFS_POLL_MAX
	help
	  Maximum number of entries supported for poll() call.

config ZVFS_SELECT
	bool "ZVFS select"
	help
	  Enable support for zvfs_select().

endif # ZVFS_POLL

endif # ZVFS
Loading