Skip to content

Commit 96818f4

Browse files
jukkarkartben
authored andcommitted
hostap: Replace wifi event mechanism by k_fifo
Earlier we had socketpair to pass wifi event information from wpa_supplicant side to zephyr adaption layer. This is now replaced by k_fifo to save some RAM. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 3e70425 commit 96818f4

File tree

3 files changed

+92
-87
lines changed

3 files changed

+92
-87
lines changed

modules/hostap/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ config WIFI_NM_WPA_SUPPLICANT
1313
select XSI_SINGLE_PROCESS
1414
select NET_SOCKETS
1515
select NET_SOCKETS_PACKET
16-
select NET_SOCKETPAIR
1716
select NET_L2_WIFI_MGMT
1817
select WIFI_NM
1918
select EXPERIMENTAL
2019
select COMMON_LIBC_MALLOC
20+
select ZVFS
21+
select ZVFS_EVENTFD
2122
help
2223
WPA supplicant as a network management backend for WIFI_NM.
2324

modules/hostap/src/supp_main.c

Lines changed: 90 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LOG_MODULE_REGISTER(wifi_supplicant, CONFIG_WIFI_NM_WPA_SUPPLICANT_LOG_LEVEL);
1111
#include <zephyr/kernel.h>
1212
#include <zephyr/init.h>
1313
#include <poll.h>
14+
#include <zephyr/zvfs/eventfd.h>
1415

1516
#if !defined(CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_NONE) && !defined(CONFIG_MBEDTLS_ENABLE_HEAP)
1617
#include <mbedtls/platform.h>
@@ -44,6 +45,7 @@ static K_THREAD_STACK_DEFINE(iface_wq_stack, CONFIG_WIFI_NM_WPA_SUPPLICANT_WQ_ST
4445
#include "fst/fst.h"
4546
#include "includes.h"
4647
#include "wpa_cli_zephyr.h"
48+
#include "ctrl_iface_zephyr.h"
4749
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
4850
#include "hostapd.h"
4951
#include "hapd_main.h"
@@ -109,7 +111,8 @@ struct supplicant_context {
109111
struct net_mgmt_event_callback cb;
110112
struct net_if *iface;
111113
char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
112-
int event_socketpair[2];
114+
struct k_fifo fifo;
115+
int sock;
113116
struct k_work iface_work;
114117
struct k_work_q iface_wq;
115118
int (*iface_handler)(struct supplicant_context *ctx, struct net_if *iface);
@@ -139,39 +142,25 @@ struct k_work_q *get_workq(void)
139142
return &get_default_context()->iface_wq;
140143
}
141144

145+
/* found in hostap/wpa_supplicant/ctrl_iface_zephyr.c */
146+
extern int send_data(struct k_fifo *fifo, int sock, const char *buf, size_t len, int flags);
147+
142148
int zephyr_wifi_send_event(const struct wpa_supplicant_event_msg *msg)
143149
{
144150
struct supplicant_context *ctx;
145-
struct pollfd fds[1];
146151
int ret;
147152

148153
/* TODO: Fix this to get the correct container */
149154
ctx = get_default_context();
150155

151-
if (ctx->event_socketpair[1] < 0) {
156+
if (ctx->sock < 0) {
152157
ret = -ENOENT;
153158
goto out;
154159
}
155160

156-
fds[0].fd = ctx->event_socketpair[0];
157-
fds[0].events = POLLOUT;
158-
fds[0].revents = 0;
159-
160-
ret = zsock_poll(fds, 1, WRITE_TIMEOUT);
161-
if (ret < 0) {
162-
ret = -errno;
163-
LOG_ERR("Cannot write event (%d)", ret);
164-
goto out;
165-
}
166-
167-
ret = zsock_send(ctx->event_socketpair[1], msg, sizeof(*msg), 0);
168-
if (ret < 0) {
169-
ret = -errno;
170-
LOG_WRN("Event send failed (%d)", ret);
171-
goto out;
172-
}
173-
174-
if (ret != sizeof(*msg)) {
161+
ret = send_data(&ctx->fifo, ctx->sock,
162+
(const char *)msg, sizeof(*msg), 0);
163+
if (ret != 0) {
175164
ret = -EMSGSIZE;
176165
LOG_WRN("Event partial send (%d)", ret);
177166
goto out;
@@ -562,84 +551,105 @@ static int setup_interface_monitoring(struct supplicant_context *ctx, struct net
562551
static void event_socket_handler(int sock, void *eloop_ctx, void *user_data)
563552
{
564553
struct supplicant_context *ctx = user_data;
565-
struct wpa_supplicant_event_msg msg;
566-
int ret;
554+
struct wpa_supplicant_event_msg event_msg;
555+
struct zephyr_msg *msg;
556+
zvfs_eventfd_t value;
567557

568558
ARG_UNUSED(eloop_ctx);
569-
ARG_UNUSED(ctx);
570559

571-
ret = zsock_recv(sock, &msg, sizeof(msg), 0);
572-
if (ret < 0) {
573-
LOG_ERR("Failed to recv the message (%d)", -errno);
574-
return;
575-
}
560+
do {
561+
zvfs_eventfd_read(sock, &value);
576562

577-
if (ret != sizeof(msg)) {
578-
LOG_ERR("Received incomplete message: got: %d, expected:%d",
579-
ret, sizeof(msg));
580-
return;
581-
}
563+
msg = k_fifo_get(&ctx->fifo, K_NO_WAIT);
564+
if (msg == NULL) {
565+
LOG_ERR("fifo(event): %s", "empty");
566+
return;
567+
}
582568

583-
LOG_DBG("Passing message %d to wpa_supplicant", msg.event);
569+
if (msg->data == NULL) {
570+
LOG_ERR("fifo(event): %s", "no data");
571+
goto out;
572+
}
584573

585-
if (msg.global) {
586-
wpa_supplicant_event_global(msg.ctx, msg.event, msg.data);
574+
if (msg->len != sizeof(event_msg)) {
575+
LOG_ERR("Received incomplete message: got: %d, expected:%d",
576+
msg->len, sizeof(event_msg));
577+
goto out;
578+
}
579+
580+
memcpy(&event_msg, msg->data, sizeof(event_msg));
581+
582+
LOG_DBG("Passing message %d to wpa_supplicant", event_msg.event);
583+
584+
if (event_msg.global) {
585+
wpa_supplicant_event_global(event_msg.ctx, event_msg.event,
586+
event_msg.data);
587587
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
588-
} else if (msg.hostapd) {
589-
hostapd_event(msg.ctx, msg.event, msg.data);
588+
} else if (event_msg.hostapd) {
589+
hostapd_event(event_msg.ctx, event_msg.event, event_msg.data);
590590
#endif
591-
} else {
592-
wpa_supplicant_event(msg.ctx, msg.event, msg.data);
593-
}
594-
595-
if (msg.data) {
596-
union wpa_event_data *data = msg.data;
597-
598-
/* Free up deep copied data */
599-
if (msg.event == EVENT_AUTH) {
600-
os_free((char *)data->auth.ies);
601-
} else if (msg.event == EVENT_RX_MGMT) {
602-
os_free((char *)data->rx_mgmt.frame);
603-
} else if (msg.event == EVENT_TX_STATUS) {
604-
os_free((char *)data->tx_status.data);
605-
} else if (msg.event == EVENT_ASSOC) {
606-
os_free((char *)data->assoc_info.addr);
607-
os_free((char *)data->assoc_info.req_ies);
608-
os_free((char *)data->assoc_info.resp_ies);
609-
os_free((char *)data->assoc_info.resp_frame);
610-
} else if (msg.event == EVENT_ASSOC_REJECT) {
611-
os_free((char *)data->assoc_reject.bssid);
612-
os_free((char *)data->assoc_reject.resp_ies);
613-
} else if (msg.event == EVENT_DEAUTH) {
614-
os_free((char *)data->deauth_info.addr);
615-
os_free((char *)data->deauth_info.ie);
616-
} else if (msg.event == EVENT_DISASSOC) {
617-
os_free((char *)data->disassoc_info.addr);
618-
os_free((char *)data->disassoc_info.ie);
619-
} else if (msg.event == EVENT_UNPROT_DEAUTH) {
620-
os_free((char *)data->unprot_deauth.sa);
621-
os_free((char *)data->unprot_deauth.da);
622-
} else if (msg.event == EVENT_UNPROT_DISASSOC) {
623-
os_free((char *)data->unprot_disassoc.sa);
624-
os_free((char *)data->unprot_disassoc.da);
591+
} else {
592+
wpa_supplicant_event(event_msg.ctx, event_msg.event, event_msg.data);
625593
}
626594

627-
os_free(msg.data);
628-
}
595+
if (event_msg.data) {
596+
union wpa_event_data *data = event_msg.data;
597+
598+
/* Free up deep copied data */
599+
if (event_msg.event == EVENT_AUTH) {
600+
os_free((char *)data->auth.ies);
601+
} else if (event_msg.event == EVENT_RX_MGMT) {
602+
os_free((char *)data->rx_mgmt.frame);
603+
} else if (event_msg.event == EVENT_TX_STATUS) {
604+
os_free((char *)data->tx_status.data);
605+
} else if (event_msg.event == EVENT_ASSOC) {
606+
os_free((char *)data->assoc_info.addr);
607+
os_free((char *)data->assoc_info.req_ies);
608+
os_free((char *)data->assoc_info.resp_ies);
609+
os_free((char *)data->assoc_info.resp_frame);
610+
} else if (event_msg.event == EVENT_ASSOC_REJECT) {
611+
os_free((char *)data->assoc_reject.bssid);
612+
os_free((char *)data->assoc_reject.resp_ies);
613+
} else if (event_msg.event == EVENT_DEAUTH) {
614+
os_free((char *)data->deauth_info.addr);
615+
os_free((char *)data->deauth_info.ie);
616+
} else if (event_msg.event == EVENT_DISASSOC) {
617+
os_free((char *)data->disassoc_info.addr);
618+
os_free((char *)data->disassoc_info.ie);
619+
} else if (event_msg.event == EVENT_UNPROT_DEAUTH) {
620+
os_free((char *)data->unprot_deauth.sa);
621+
os_free((char *)data->unprot_deauth.da);
622+
} else if (event_msg.event == EVENT_UNPROT_DISASSOC) {
623+
os_free((char *)data->unprot_disassoc.sa);
624+
os_free((char *)data->unprot_disassoc.da);
625+
}
626+
627+
os_free(event_msg.data);
628+
}
629+
630+
out:
631+
os_free(msg->data);
632+
os_free(msg);
633+
634+
} while (!k_fifo_is_empty(&ctx->fifo));
629635
}
630636

631637
static int register_supplicant_event_socket(struct supplicant_context *ctx)
632638
{
633639
int ret;
634640

635-
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, ctx->event_socketpair);
641+
ret = zvfs_eventfd(0, ZVFS_EFD_NONBLOCK);
636642
if (ret < 0) {
637643
ret = -errno;
638644
LOG_ERR("Failed to initialize socket (%d)", ret);
639645
return ret;
640646
}
641647

642-
eloop_register_read_sock(ctx->event_socketpair[0], event_socket_handler, NULL, ctx);
648+
ctx->sock = ret;
649+
650+
k_fifo_init(&ctx->fifo);
651+
652+
eloop_register_read_sock(ctx->sock, event_socket_handler, NULL, ctx);
643653

644654
return 0;
645655
}
@@ -707,7 +717,7 @@ static void handler(void)
707717

708718
supplicant_generate_state_event(ctx->if_name, NET_EVENT_SUPPLICANT_CMD_NOT_READY, 0);
709719

710-
eloop_unregister_read_sock(ctx->event_socketpair[0]);
720+
eloop_unregister_read_sock(ctx->sock);
711721

712722
zephyr_global_wpa_ctrl_deinit();
713723

@@ -716,8 +726,7 @@ static void handler(void)
716726
out:
717727
wpa_supplicant_deinit(ctx->supplicant);
718728

719-
zsock_close(ctx->event_socketpair[0]);
720-
zsock_close(ctx->event_socketpair[1]);
729+
close(ctx->sock);
721730

722731
err:
723732
os_free(params.pid_file);

subsys/net/lib/sockets/Kconfig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ if NET_SOCKETPAIR
350350

351351
config NET_SOCKETPAIR_BUFFER_SIZE
352352
int "Size of the intermediate buffer, in bytes"
353-
default 1024 if WIFI_NM_WPA_SUPPLICANT
354353
default 64
355354
range 1 4096
356355
help
@@ -372,8 +371,6 @@ if NET_SOCKETPAIR_STATIC
372371

373372
config NET_SOCKETPAIR_MAX
374373
int "How many socketpairs to pre-allocate"
375-
default 6 if WIFI_NM_HOSTAPD_AP
376-
default 4 if WIFI_NM_WPA_SUPPLICANT
377374
default 1
378375

379376
endif # NET_SOCKETPAIR_STATIC
@@ -382,8 +379,6 @@ if NET_SOCKETPAIR_HEAP
382379

383380
config HEAP_MEM_POOL_ADD_SIZE_SOCKETPAIR
384381
int
385-
default 9136 if WIFI_NM_HOSTAPD_AP
386-
default 6852 if WIFI_NM_WPA_SUPPLICANT
387382
default 296
388383

389384
endif # NET_SOCKETPAIR_HEAP

0 commit comments

Comments
 (0)