Skip to content

Commit 044a94f

Browse files
pdgendtkartben
authored andcommitted
net: lib: coap: coap_server: Use eventfd instead of socket pair
Convert the socket poll logic to use a more lightweight eventfd file descriptor instead of a socket pair. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent 338371e commit 044a94f

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

subsys/net/lib/coap/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ config COAP_SERVER
166166
bool "CoAP server support [EXPERIMENTAL]"
167167
select EXPERIMENTAL
168168
select NET_SOCKETS
169-
select NET_SOCKETPAIR
169+
select ZVFS
170+
select ZVFS_EVENTFD
170171
help
171172
This option enables the API for CoAP-services to register resources.
172173

subsys/net/lib/coap/coap_server.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ LOG_MODULE_DECLARE(net_coap, CONFIG_COAP_LOG_LEVEL);
1616
#include <zephyr/net/coap_link_format.h>
1717
#include <zephyr/net/coap_mgmt.h>
1818
#include <zephyr/net/coap_service.h>
19-
#include <zephyr/posix/fcntl.h>
19+
#include <zephyr/sys/fdtable.h>
20+
#include <zephyr/zvfs/eventfd.h>
2021

2122
#if defined(CONFIG_NET_TC_THREAD_COOPERATIVE)
2223
/* Lowest priority cooperative thread */
@@ -38,7 +39,7 @@ LOG_MODULE_DECLARE(net_coap, CONFIG_COAP_LOG_LEVEL);
3839
BUILD_ASSERT(CONFIG_ZVFS_POLL_MAX > 0, "CONFIG_ZVFS_POLL_MAX can't be 0");
3940

4041
static K_MUTEX_DEFINE(lock);
41-
static int control_socks[2];
42+
static int control_sock;
4243

4344
#if defined(CONFIG_COAP_SERVER_PENDING_ALLOCATOR_STATIC)
4445
K_MEM_SLAB_DEFINE_STATIC(pending_data, CONFIG_COAP_SERVER_MESSAGE_SIZE,
@@ -370,7 +371,7 @@ static int coap_server_poll_timeout(void)
370371

371372
static void coap_server_update_services(void)
372373
{
373-
if (zsock_send(control_socks[1], &(char){0}, 1, 0) < 0) {
374+
if (zvfs_eventfd_write(control_sock, 1)) {
374375
LOG_ERR("Failed to notify server thread (%d)", errno);
375376
}
376377
}
@@ -468,7 +469,7 @@ int coap_service_start(const struct coap_service *service)
468469
goto end;
469470
}
470471

471-
ret = zsock_fcntl(service->data->sock_fd, F_SETFL, O_NONBLOCK);
472+
ret = zsock_fcntl(service->data->sock_fd, ZVFS_F_SETFL, ZVFS_O_NONBLOCK);
472473
if (ret < 0) {
473474
ret = -errno;
474475
goto close;
@@ -763,25 +764,12 @@ static void coap_server_thread(void *p1, void *p2, void *p3)
763764
ARG_UNUSED(p2);
764765
ARG_UNUSED(p3);
765766

766-
/* Create a socket pair to wake zsock_poll */
767-
ret = zsock_socketpair(AF_UNIX, SOCK_STREAM, 0, control_socks);
768-
if (ret < 0) {
769-
LOG_ERR("Failed to create socket pair (%d)", ret);
767+
control_sock = zvfs_eventfd(0, ZVFS_EFD_NONBLOCK);
768+
if (control_sock < 0) {
769+
LOG_ERR("Failed to create event fd (%d)", -errno);
770770
return;
771771
}
772772

773-
for (int i = 0; i < 2; ++i) {
774-
ret = zsock_fcntl(control_socks[i], F_SETFL, O_NONBLOCK);
775-
776-
if (ret < 0) {
777-
zsock_close(control_socks[0]);
778-
zsock_close(control_socks[1]);
779-
780-
LOG_ERR("Failed to set socket pair [%d] non-blocking (%d)", i, ret);
781-
return;
782-
}
783-
}
784-
785773
COAP_SERVICE_FOREACH(svc) {
786774
if (svc->flags & COAP_SERVICE_AUTOSTART) {
787775
ret = coap_service_start(svc);
@@ -810,9 +798,9 @@ static void coap_server_thread(void *p1, void *p2, void *p3)
810798
sock_nfds++;
811799
}
812800

813-
/* Add socket pair FD to allow wake up */
801+
/* Add event FD to allow wake up */
814802
if (sock_nfds < MAX_POLL_FD) {
815-
sock_fds[sock_nfds].fd = control_socks[0];
803+
sock_fds[sock_nfds].fd = control_sock;
816804
sock_fds[sock_nfds].events = ZSOCK_POLLIN;
817805
sock_fds[sock_nfds].revents = 0;
818806
sock_nfds++;
@@ -828,11 +816,11 @@ static void coap_server_thread(void *p1, void *p2, void *p3)
828816

829817
for (int i = 0; i < sock_nfds; ++i) {
830818
/* Check the wake up event */
831-
if (sock_fds[i].fd == control_socks[0] &&
819+
if (sock_fds[i].fd == control_sock &&
832820
sock_fds[i].revents & ZSOCK_POLLIN) {
833-
char tmp;
821+
zvfs_eventfd_t tmp;
834822

835-
zsock_recv(sock_fds[i].fd, &tmp, 1, 0);
823+
zvfs_eventfd_read(sock_fds[i].fd, &tmp);
836824
continue;
837825
}
838826

0 commit comments

Comments
 (0)