Skip to content

zephyr: Use k_fifo instead of socketpair for IPC #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025

Conversation

jukkar
Copy link
Member

@jukkar jukkar commented May 22, 2025

As socketpair uses almost 10kb of memory in AP mode, and almost 7kb in STA mode, try to lower RAM consumption by getting rid of socketpair as a IPC between wpa_supplicant and hostapd related threads. The commit uses k_fifo instead and just sends a pointer between threads. This also avoids some memory copy of the data.

@jukkar
Copy link
Member Author

jukkar commented May 22, 2025

In my test setup it saved 6528 bytes RAM and 3060 bytes ROM in STA mode compared to current upstream version that uses socketpair.

@jukkar jukkar changed the title [WIP] zephyr: Use k_fifo instead of socketpair for IPC zephyr: Use k_fifo instead of socketpair for IPC May 23, 2025
@jukkar
Copy link
Member Author

jukkar commented May 23, 2025

Added initial support for hostapd which does not use socketpair.
I am seeing savings for RAM 8832 bytes and ROM 3068 bytes if both STA and AP mode (with hostapd) is enabled.

@jukkar jukkar marked this pull request as ready for review May 23, 2025 12:40
@jukkar jukkar force-pushed the devel/remove-socketpair branch 2 times, most recently from ab5167a to 12306a0 Compare May 26, 2025 15:24
@jukkar jukkar force-pushed the devel/remove-socketpair branch from 12306a0 to 9d5eaf8 Compare June 3, 2025 07:30
intptr_t fifo;

/**
* Dynamic data to send. The receiver is responsible for freeing the data.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why const data was removed?Without const, dynamic data doesn't make much send, we can just call i t data.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It complicated my debugging about the heap corruption and was a bit early premature optimization. We can bring it back when everything is working smoothly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can do it in a subsequent PR.

goto fail;
}

priv->sock_pair[0] = ret;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we rename this? As we are removing select NET_SOCKETPAIR, this might confuse the users, as it's basically two separate sockets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can certainly rename it, I did not want to do too many changes so it looks still a bit like socketpair even if it is not.

@jukkar jukkar force-pushed the devel/remove-socketpair branch from 9d5eaf8 to fa3f7da Compare June 6, 2025 14:47
@jukkar jukkar force-pushed the devel/remove-socketpair branch from fa3f7da to 141aa5c Compare June 9, 2025 06:29
@krish2718
Copy link
Collaborator

@MaochenWang1 can you please review?

@MaochenWang1
Copy link
Collaborator

Hi @jukkar is this feature targeted for v4.2 release?
Sorry for delay response, busy handling downstream issues.

@krish2718
Copy link
Collaborator

Hi @jukkar is this feature targeted for v4.2 release? Sorry for delay response, busy handling downstream issues.

It would be good to get this in for 4.2, as it says RAM.

@MaochenWang1
Copy link
Collaborator

Hi @jukkar is this feature targeted for v4.2 release? Sorry for delay response, busy handling downstream issues.

It would be good to get this in for 4.2, as it says RAM.

but I saw issues on RW612, and don't have time to deep research it
image

@krish2718
Copy link
Collaborator

Same build command and hal_nxp as zephyrproject-rtos/zephyr#90317 (comment) ?

@MaochenWang1
Copy link
Collaborator

Same build command and hal_nxp as zephyrproject-rtos/zephyr#90317 (comment) ?

use the latest codebase, the latest hal_nxp

@jukkar
Copy link
Member Author

jukkar commented Jun 24, 2025

@MaochenWang1 I just tried frdm_rw612 board and see no issues when connecting to either OPEN mode AP or to WPA3-SAE-HNP. I used the same command line option as you.

@MaochenWang1
Copy link
Collaborator

@MaochenWang1 I just tried frdm_rw612 board and see no issues when connecting to either OPEN mode AP or to WPA3-SAE-HNP. I used the same command line option as you.

Sorry, I have some problem to build the latest upstream, as it requires python3.12, still fixing it.

@MaochenWang1
Copy link
Collaborator

I use the TOT of upstream, and apply Jukkar's patch for zephyr and hostap repo, there is still wpa3 connect issue:
image
the build cmd:
west build -p always -b rd_rw612_bga samples/net/wifi/shell -d wifi -DEXTRA_CONF_FILE="nxp/overlay_rw612.conf nxp/overlay_hostap_rw612.conf"

@MaochenWang1
Copy link
Collaborator

I tried the frdm_rw612 board, and have the same issue.

@MaochenWang1
Copy link
Collaborator

Hi @jukkar can we hold this feature after v4.2?

@jukkar
Copy link
Member Author

jukkar commented Jun 25, 2025

I have version

uart:~$ wifi version
Wi-Fi Driver Version: v1.3.r51.z_up.p12
Wi-Fi Firmware Version: rw610w-V2, IMU, FP99, 18.99.6.p24, PVE_FIX 1

@jukkar
Copy link
Member Author

jukkar commented Jun 25, 2025

Hi @jukkar can we hold this feature after v4.2?

If you cannot verify the functionality, then we have to postpone this.

I am just wondering what could cause the issues in your setup as everything works ok for my NXP board. Things work ok also for Nordic wifi test setup.

@MaochenWang1
Copy link
Collaborator

I have version

uart:~$ wifi version
Wi-Fi Driver Version: v1.3.r51.z_up.p12
Wi-Fi Firmware Version: rw610w-V2, IMU, FP99, 18.99.6.p24, PVE_FIX 1

Hi jukkar, your FW version is kind of old. please remove modules/hal/nxp/zephyr/blobs/rw61x, and run west blobs fetch hal_nxp again. and please show me your console log of the successful connection?

@jukkar
Copy link
Member Author

jukkar commented Jun 26, 2025

@MaochenWang1 here is a log to connecting to open AP

uart:~$ wifi version
Wi-Fi Driver Version: v1.3.r51.z_up.p12
Wi-Fi Firmware Version: rw610w-V2, IMU, FP99, 18.99.6.p41, PVE_FIX 1
uart:~$ wifi scan
Scan requested
uart:~$ 
Num  | SSID                             (len) | Chan (Band)   | RSSI | Security             | BSSID             | MFP     
1    | WiTi                             4     | 8    (2.4GHz) | -55  | OPEN                 | 00:00:00:00:00:70 | Disable 
2    | WiTi                             4     | 149  (5GHz  ) | -62  | WPA2-PSK             | DC:1E:A0:39:9B:BD | Disable 
Scan request done
uart:~$ wifi connect -s WiTi -b 2 -k 0
Connection requested
uart:~$ [00:06:52.519,684] <inf> wpa_supp: ml: SME: Trying to authenticate with 00:00:00:00:00:70 (SSID='WiTi' freq=2447 MHz)
uart:~$ [00:06:52.550,586] <inf> wpa_supp: ml: Trying to associate with 00:00:00:00:00:70 (SSID='WiTi' freq=2447 MHz)
uart:~$ [00:06:52.595,560] <inf> wpa_supp: ml: Associated with 00:00:00:00:00:70
uart:~$ [00:06:52.605,601] <inf> wpa_supp: ml: CTRL-EVENT-CONNECTED - Connection to 00:00:00:00:00:70 completed [id=0 id_str=]
uart:~$ Connected
uart:~$ [00:06:52.622,697] <inf> wpa_supp: ml: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
uart:~$ [00:06:52.632,970] <dbg> nxp_wifi: nxp_wifi_wlan_event_callback: WLAN: received event 2
uart:~$ [00:06:52.645,312] <dbg> nxp_wifi: nxp_wifi_wlan_event_callback: WLAN: associated to nxp_wlan_network
uart:~$ [00:06:55.799,293] <inf> net_dhcpv4: Received: 192.168.200.223
uart:~$ net iface
Hostname: test
Default interface: 1


Interface ml (0x10044090) (WiFi) [1]
===============================
Link addr : C0:95:DA:01:6E:99
MTU       : 1500
Flags     : AUTO_START,IPv4,IPv6
Device    : ml (0x180a2c5c)
Status    : oper=UP, admin=UP, carrier=ON
Ethernet capabilities supported:
IPv6 unicast addresses (max 2):
	fe80::c295:daff:fe01:6e99 autoconf preferred infinite
	fd97:d912:d520:0:c295:daff:fe01:6e99 autoconf preferred infinite
IPv6 multicast addresses (max 3):
	ff02::1
	ff02::1:ff01:6e99
IPv6 prefixes (max 2):
	fd97:d912:d520::/64 infinite
IPv6 hop limit           : 64
IPv6 base reachable time : 30000
IPv6 reachable time      : 32351
IPv6 retransmit timer    : 0
DHCPv6 state             : disabled
IPv4 unicast addresses (max 1):
	192.168.200.223/255.255.255.0 DHCP preferred
IPv4 multicast addresses (max 2):
	224.0.0.1
IPv4 gateway : 192.168.200.1
DHCPv4 lease time : 43200
DHCPv4 renew time : 21600
DHCPv4 server     : 192.168.200.1
DHCPv4 requested  : 192.168.200.223
DHCPv4 state      : bound
DHCPv4 attempts   : 1
DHCPv4 state      : bound
uart:~$ 

@jukkar
Copy link
Member Author

jukkar commented Jun 26, 2025

Compilation for my above test was done like this:

west build -p \
-d ~/zephyrproject/build/wifi-shell \
~/zephyrproject/zephyr/samples/net/wifi/shell/ \
-b frdm_rw612 \
-S wifi-ip \
-DEXTRA_CONF_FILE="nxp/overlay_rw612.conf nxp/overlay_hostap_rw612.conf"
-- \
-DCONFIG_WIFI_LOG_LEVEL_DBG=y \
-DCONFIG_NET_DHCPV4=y \
-DCONFIG_NET_CONFIG_SETTINGS=n \
-DCONFIG_NET_TCP_IPV6_ND_REACHABILITY_HINT=y \
-DCONFIG_NET_STATISTICS=n \
-DCONFIG_LOG_MODE_IMMEDIATE=y \
-DCONFIG_NET_HOSTNAME_ENABLE=y \
-DCONFIG_NET_HOSTNAME="\"test\"" \
-DCONFIG_NET_PKT_RX_COUNT=40 \
-DCONFIG_NET_PKT_TX_COUNT=40 \
-DCONFIG_NET_BUF_RX_COUNT=160 \
-DCONFIG_NET_BUF_TX_COUNT=160 \
-DCONFIG_OBJ_CORE=y \
-DCONFIG_NET_SOCKETS_OBJ_CORE=y \
-DCONFIG_NET_BUF_POOL_USAGE=y \
-DCONFIG_NET_IF_MAX_IPV4_COUNT=2 \
-DCONFIG_NET_IF_MAX_IPV6_COUNT=2 \
-DCONFIG_WIFI_NM_WPA_SUPPLICANT_LOG_LEVEL_DBG=n \
-DCONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_LEVEL=0

jukkar added 2 commits June 26, 2025 16:24
As socketpair uses almost 10kb of memory in AP mode, and almost
7kb in STA mode, try to lower RAM consumption by getting rid of
socketpair as a IPC between wpa_supplicant and hostapd related
threads. The commit uses k_fifo instead and just sends a pointer
between threads. This also avoids some memory copy of the data.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Replace socketpair by k_fifo for internal IPC between different
hostap threads. This will save both RAM and ROM as we can avoid
large buffers that are needed by socketpair.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
@jukkar jukkar force-pushed the devel/remove-socketpair branch from 141aa5c to e97f328 Compare June 26, 2025 13:24
@jukkar
Copy link
Member Author

jukkar commented Jun 26, 2025

  • Changed zvfs_eventfd to be non-blocking to make sure we read all the events

@jukkar jukkar merged commit bc5d22f into zephyrproject-rtos:main Jun 27, 2025
@jukkar jukkar deleted the devel/remove-socketpair branch June 27, 2025 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants