Skip to content

Commit

Permalink
net: sockets: move select() implementation to zvfs
Browse files Browse the repository at this point in the history
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: Chris Friedt <cfriedt@tenstorrent.com>
  • Loading branch information
cfriedt authored and nashif committed Jun 27, 2024
1 parent 93973e2 commit 49ac191
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 82 deletions.
42 changes: 28 additions & 14 deletions include/zephyr/net/socket_select.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 */

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

#include <zephyr/syscalls/socket_select.h>

/**
* @}
*/
Expand Down
14 changes: 8 additions & 6 deletions include/zephyr/posix/sys/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 18 additions & 1 deletion include/zephyr/sys/fdtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

/* FIXME: For native_posix ssize_t, off_t. */
#include <zephyr/fs/fs.h>
#include <zephyr/sys/mutex.h>
Expand Down Expand Up @@ -207,9 +208,23 @@ struct zvfs_pollfd {

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

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

#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);

struct timespec;
__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().
*
Expand Down Expand Up @@ -239,4 +254,6 @@ enum {
}
#endif

#include <zephyr/syscalls/fdtable.h>

#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */
1 change: 1 addition & 0 deletions lib/os/zvfs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 0 deletions lib/os/zvfs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 49ac191

Please sign in to comment.