Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions include/zephyr/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct zsock_pollfd {
short revents;
};

typedef uint64_t zsock_eventfd_t;

/* ZSOCK_POLL* values are compatible with Linux */
/** zsock_poll: Poll for readability */
#define ZSOCK_POLLIN 1
Expand Down Expand Up @@ -75,6 +77,12 @@ struct zsock_pollfd {
/** zsock_shutdown: Shut down for both reading and writing */
#define ZSOCK_SHUT_RDWR 2

/** zsock_eventfd **/
#define ZSOCK_EFD_IN_USE 0x1
#define ZSOCK_EFD_SEMAPHORE 0x2
#define ZSOCK_EFD_NONBLOCK O_NONBLOCK
#define ZSOCK_EFD_FLAGS_SET (ZSOCK_EFD_SEMAPHORE | ZSOCK_EFD_NONBLOCK)

/** Protocol level for TLS.
* Here, the same socket protocol level for TLS as in Linux was used.
*/
Expand Down Expand Up @@ -268,6 +276,43 @@ __syscall int zsock_socket(int family, int type, int proto);
*/
__syscall int zsock_socketpair(int family, int type, int proto, int *sv);

/**
* @brief Create an unnamed pair of connected sockets
*
* @details
* @rst
* See `POSIX.1-2017 article
* <https://man7.org/linux/man-pages/man2/eventfd.2.html>`__
* for normative description.
* This function is also exposed as ``eventfd()``
* if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
* @endrst
*/
__syscall int zsock_eventfd(unsigned int initval, int flags);

/**
* @brief Read from an eventfd
*
* If call is successful, the value parameter will have the value 1
*
* @param fd File descriptor
* @param value Pointer for storing the read value
*
* @return 0 on success, -1 on error
*/
__syscall int zsock_eventfd_read(int fd, zsock_eventfd_t *value);

/**
* @brief Write to an eventfd
*
* @param fd File descriptor
* @param value Value to write
*
* @return 0 on success, -1 on error
*/
__syscall int zsock_eventfd_write(int fd, zsock_eventfd_t value);


/**
* @brief Close a network socket
*
Expand Down Expand Up @@ -685,6 +730,8 @@ int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,

#define pollfd zsock_pollfd

#define eventfd_t zsock_eventfd_t

/** POSIX wrapper for @ref zsock_socket */
static inline int socket(int family, int type, int proto)
{
Expand All @@ -697,6 +744,24 @@ static inline int socketpair(int family, int type, int proto, int sv[2])
return zsock_socketpair(family, type, proto, sv);
}

/** POSIX wrapper for @ref zsock_eventfd */
static inline int eventfd(unsigned int initval, int flags)
{
return zsock_eventfd(initval, flags);
}

/** POSIX wrapper for @ref zsock_eventfd_read */
static inline int eventfd_read(int fd, zsock_eventfd_t *value)
{
return zsock_eventfd_read(fd, value);
}

/** POSIX wrapper for @ref zsock_eventfd_write */
static inline int eventfd_write(int fd, zsock_eventfd_t value)
{
return zsock_eventfd_write(fd, value);
}

/** POSIX wrapper for @ref zsock_close */
static inline int close(int sock)
{
Expand Down Expand Up @@ -896,6 +961,15 @@ static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
/** POSIX wrapper for @ref ZSOCK_SHUT_RDWR */
#define SHUT_RDWR ZSOCK_SHUT_RDWR

/** POSIX wrapper for @ref ZSOCK_EFD_IN_USE */
#define EFD_IN_USE ZSOCK_EFD_IN_USE
/** POSIX wrapper for @ref ZSOCK_EFD_SEMAPHORE */
#define EFD_SEMAPHORE ZSOCK_EFD_SEMAPHORE
/** POSIX wrapper for @ref ZSOCK_EFD_NONBLOCK */
#define EFD_NONBLOCK ZSOCK_EFD_NONBLOCK
/** POSIX wrapper for @ref ZSOCK_EFD_FLAGS_SET */
#define EFD_FLAGS_SET ZSOCK_EFD_FLAGS_SET

/** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
#define EAI_BADFLAGS DNS_EAI_BADFLAGS
/** POSIX wrapper for @ref DNS_EAI_NONAME */
Expand Down
102 changes: 0 additions & 102 deletions include/zephyr/posix/sys/eventfd.h

This file was deleted.

1 change: 0 additions & 1 deletion lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC semaphore.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC pthread_key.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c)
add_subdirectory_ifdef(CONFIG_GETOPT getopt)

if(NOT (CONFIG_BOARD_NATIVE_POSIX OR CONFIG_BOARD_NATIVE_POSIX_64BIT))
Expand Down
16 changes: 0 additions & 16 deletions lib/posix/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,3 @@ config APP_LINK_WITH_POSIX_SUBSYS
depends on POSIX_API
help
Add POSIX subsystem header files to the 'app' include path.

config EVENTFD
bool "Support for eventfd"
depends on !ARCH_POSIX
help
Enable support for event file descriptors, eventfd. An eventfd can
be used as an event wait/notify mechanism together with POSIX calls
like read, write and poll.

config EVENTFD_MAX
int "Maximum number of eventfd's"
depends on EVENTFD
default 1
range 1 4096
help
The maximum number of supported event file descriptors.
11 changes: 11 additions & 0 deletions samples/net/sockets/eventfd/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# General config
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_NANO=n

# eventfd() implementation only makes sense for networking APIs.
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_EVENTFD=y
CONFIG_NET_SLIP_TAP=n
CONFIG_TEST_RANDOM_GENERATOR=y
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* This sample application is roughly based on the sample code in Linux
* manpage for eventfd().
*/
#include <sys/eventfd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <net/socket.h>

#define fatal(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
Expand All @@ -31,8 +30,8 @@ void writer(void)
for (j = 1; j < g_argc; j++) {
printf("Writing %s to efd\n", g_argv[j]);
u = strtoull(g_argv[j], NULL, 0);
s = write(efd, &u, sizeof(uint64_t));
if (s != sizeof(uint64_t)) {
s = eventfd_write(efd, u);
if (s != 0) {
fatal("write");
}
}
Expand All @@ -44,21 +43,21 @@ void reader(void)
uint64_t u;
ssize_t s;

sleep(1);
k_sleep(K_MSEC(1000));

printf("About to read\n");
s = read(efd, &u, sizeof(uint64_t));
if (s != sizeof(uint64_t)) {
s = eventfd_read(efd, &u);
if (s != 0) {
fatal("read");
}
printf("Read %llu (0x%llx) from efd\n",
(unsigned long long)u, (unsigned long long)u);
}

int main(int argc, char *argv[])
void main(void)
{
argv = input_argv;
argc = sizeof(input_argv) / sizeof(input_argv[0]);
char **argv = input_argv;
int argc = sizeof(input_argv) / sizeof(input_argv[0]);

if (argc < 2) {
fprintf(stderr, "Usage: %s <num>...\n", argv[0]);
Expand All @@ -77,6 +76,4 @@ int main(int argc, char *argv[])
reader();

printf("Finished\n");

return 0;
}
11 changes: 0 additions & 11 deletions samples/posix/eventfd/prj.conf

This file was deleted.

1 change: 1 addition & 0 deletions subsys/net/lib/sockets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ if(CONFIG_SOCKS)
endif()

zephyr_sources_ifdef(CONFIG_NET_SOCKETPAIR socketpair.c)
zephyr_sources_ifdef(CONFIG_NET_EVENTFD eventfd.c)

zephyr_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
15 changes: 15 additions & 0 deletions subsys/net/lib/sockets/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ config NET_SOCKETPAIR_BUFFER_SIZE
help
Buffer size for socketpair(2)

config NET_EVENTFD
bool "Support for eventfd"
help
Enable support for event file descriptors, eventfd. An eventfd can
be used as an event wait/notify mechanism together with POSIX calls
like read, write and poll.

config NET_EVENTFD_MAX
int "Maximum number of eventfd's"
depends on NET_EVENTFD
default 1
range 1 4096
help
The maximum number of supported event file descriptors.

config NET_SOCKETS_NET_MGMT
bool "Network management socket support [EXPERIMENTAL]"
depends on NET_MGMT_EVENT
Expand Down
Loading