ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
lwip_fast_select.c
Go to the documentation of this file.
1// Fast socket monitoring for ESP32 and LibreTiny (LwIP >= 2.1.3)
2// Replaces lwip_select() with direct rcvevent reads and FreeRTOS task notifications.
3//
4// This must be a .c file (not .cpp) because:
5// 1. lwip/priv/sockets_priv.h conflicts with C++ compilation units
6// 2. The netconn callback is a C function pointer
7//
8// USE_ESP32 and USE_LIBRETINY platform flags (-D) control compilation of this file.
9// See the guard at the bottom of the header comment for details.
10//
11// Thread safety analysis
12// ======================
13// Three threads interact with this code:
14// 1. Main loop task — calls init, has_data, hook
15// 2. LwIP TCP/IP task — calls event_callback (reads s_original_callback; writes rcvevent
16// via the original callback under SYS_ARCH_PROTECT/UNPROTECT mutex)
17// 3. Background tasks — call wake_main_loop
18//
19// LwIP source references (ESP-IDF v5.5.2, commit 30aaf64524):
20// sockets.c: https://github.com/espressif/esp-idf/blob/30aaf64524/components/lwip/lwip/src/api/sockets.c
21// - event_callback (static, same for all sockets): L327
22// - DEFAULT_SOCKET_EVENTCB = event_callback: L328
23// - tryget_socket_unconn_nouse (direct array lookup): L450
24// - lwip_socket_dbg_get_socket (thin wrapper): L461
25// - All socket types use DEFAULT_SOCKET_EVENTCB: L1741, L1748, L1759
26// - event_callback definition: L2538
27// - SYS_ARCH_PROTECT before rcvevent switch: L2578
28// - sock->rcvevent++ (NETCONN_EVT_RCVPLUS case): L2582
29// - SYS_ARCH_UNPROTECT after switch: L2615
30// sys.h: https://github.com/espressif/esp-idf/blob/30aaf64524/components/lwip/lwip/src/include/lwip/sys.h
31// - SYS_ARCH_PROTECT calls sys_arch_protect(): L495
32// - SYS_ARCH_UNPROTECT calls sys_arch_unprotect(): L506
33// (ESP-IDF implements sys_arch_protect/unprotect as FreeRTOS mutex lock/unlock)
34//
35// Socket slot lifetime
36// ====================
37// This code reads struct lwip_sock fields without SYS_ARCH_PROTECT. The safety
38// argument requires that the slot cannot be freed while we read it.
39//
40// In LwIP, the socket table is a static array and slots are only freed via:
41// lwip_close() -> lwip_close_internal() -> free_socket_free_elements() -> free_socket()
42// The TCP/IP thread does NOT call free_socket(). On link loss, RST, or timeout
43// it frees the TCP PCB and signals the netconn (rcvevent++ to indicate EOF), but
44// the netconn and lwip_sock slot remain allocated until the application calls
45// lwip_close(). ESPHome removes the fd from the monitored set before calling
46// lwip_close().
47//
48// Therefore lwip_socket_dbg_get_socket(fd) plus a volatile read of rcvevent
49// (to prevent compiler reordering or caching) is safe as long as the application
50// is single-writer for close. ESPHome guarantees this by design: all socket
51// create/read/close happens on the main loop. fd numbers are not reused while
52// the slot remains allocated, and the slot remains allocated until lwip_close().
53// Any change in LwIP that allows free_socket() to be called outside lwip_close()
54// would invalidate this assumption.
55//
56// LwIP source references for slot lifetime:
57// sockets.c (same commit as above):
58// - alloc_socket (slot allocation): L419
59// - free_socket (slot deallocation): L384
60// - free_socket_free_elements (called from lwip_close_internal): L393
61// - lwip_close_internal (only caller of free_socket_free_elements): L2355
62// - lwip_close (only caller of lwip_close_internal): L2450
63//
64// Shared state and safety rationale:
65//
66// esphome_main_task_handle (TaskHandle_t, 4 bytes, defined in main_task.c):
67// Written once by main loop in Application::setup(). Read by TCP/IP thread
68// (in callback) and background tasks (in wake).
69// Safe: write-once-then-read pattern. Socket hooks may run before setup(),
70// but the NULL check on esphome_main_task_handle in the callback provides correct
71// degraded behavior — notifications are simply skipped until setup() completes.
72//
73// s_original_callback (netconn_callback, 4-byte function pointer):
74// Written by main loop in hook_socket() (only when NULL — set once).
75// Read by TCP/IP thread in esphome_socket_event_callback().
76// Safe: set-once pattern. The first hook_socket() captures the original callback.
77// All subsequent hooks see it already set and skip the write. The TCP/IP thread
78// only reads this after the callback pointer has been swapped (which happens after
79// the write), so it always sees the initialized value.
80//
81// sock->conn->callback (netconn_callback, 4-byte function pointer):
82// Written by main loop in hook_socket(). Never restored — all LwIP sockets share
83// the same static event_callback (DEFAULT_SOCKET_EVENTCB), so the wrapper stays permanently.
84// Read by TCP/IP thread when invoking the callback.
85// Safe: 32-bit aligned pointer writes are atomic on Xtensa, RISC-V (ESP32),
86// and ARM Cortex-M (LibreTiny). The TCP/IP thread will see either the old or
87// new pointer atomically — never a torn value. Both the wrapper and original
88// callbacks are valid at all times (the wrapper itself calls the original),
89// so either value is correct.
90//
91// sock->rcvevent (s16_t, 2 bytes):
92// Written by TCP/IP thread in event_callback under SYS_ARCH_PROTECT.
93// Read by main loop in has_data() via volatile cast.
94// Safe: SYS_ARCH_UNPROTECT releases a FreeRTOS mutex (ESP32) or resumes the
95// scheduler (LibreTiny), both providing a memory barrier. The volatile cast
96// prevents the compiler from caching the read. Aligned 16-bit reads are
97// single-instruction loads on Xtensa (L16SI), RISC-V (LH), and ARM Cortex-M
98// (LDRH), which cannot produce torn values. On single-core chips (LibreTiny,
99// ESP32-C3/C6/H2) cross-core visibility is not an issue.
100//
101// FreeRTOS task notification value:
102// Written by TCP/IP thread (xTaskNotifyGive in callback) and background tasks
103// (xTaskNotifyGive in wake_main_loop). Read by main loop (ulTaskNotifyTake).
104// Safe: FreeRTOS notification APIs are thread-safe by design (use internal
105// critical sections). Multiple concurrent xTaskNotifyGive calls are safe —
106// the notification count simply increments.
107
108// USE_LWIP_FAST_SELECT is set via -D build flag (not cg.add_define) so it is
109// visible in both .c and .cpp translation units.
110#ifdef USE_LWIP_FAST_SELECT
111
112// LwIP headers must come first — they define netconn_callback, struct lwip_sock, etc.
113#include <lwip/api.h>
114#include <lwip/priv/sockets_priv.h>
115#include <lwip/tcp.h>
116// FreeRTOS include paths differ: ESP-IDF uses freertos/ prefix, LibreTiny does not
117#ifdef USE_ESP32
118#include <freertos/FreeRTOS.h>
119#include <freertos/task.h>
120#else
121#include <FreeRTOS.h>
122#include <task.h>
123#endif
124
127
128#include <stddef.h>
129
130// Compile-time verification of thread safety assumptions.
131// On ESP32 (Xtensa/RISC-V) and LibreTiny (ARM Cortex-M), naturally-aligned
132// reads/writes up to 32 bits are atomic.
133// These asserts ensure our cross-thread shared state meets those requirements.
134
135// Pointer types must fit in a single 32-bit store (atomic write)
136_Static_assert(sizeof(TaskHandle_t) <= 4, "TaskHandle_t must be <= 4 bytes for atomic access");
137_Static_assert(sizeof(netconn_callback) <= 4, "netconn_callback must be <= 4 bytes for atomic access");
138
139// rcvevent must be exactly 2 bytes (s16_t) — the inline in lwip_fast_select.h reads it as int16_t.
140// If lwIP changes this to int or similar, the offset assert would still pass but the load width would be wrong.
141_Static_assert(sizeof(((struct lwip_sock *) 0)->rcvevent) == 2,
142 "rcvevent size changed — update int16_t cast in esphome_lwip_socket_has_data() in lwip_fast_select.h");
143
144// Struct member alignment — natural alignment guarantees atomicity on Xtensa/RISC-V/ARM.
145// Misaligned access would not be atomic even if the size is <= 4 bytes.
146_Static_assert(offsetof(struct netconn, callback) % sizeof(netconn_callback) == 0,
147 "netconn.callback must be naturally aligned for atomic access");
148_Static_assert(offsetof(struct lwip_sock, rcvevent) % sizeof(((struct lwip_sock *) 0)->rcvevent) == 0,
149 "lwip_sock.rcvevent must be naturally aligned for atomic access");
150
151// Verify the hardcoded offset used in the header's inline esphome_lwip_socket_has_data().
152_Static_assert(offsetof(struct lwip_sock, rcvevent) == ESPHOME_LWIP_SOCK_RCVEVENT_OFFSET,
153 "lwip_sock.rcvevent offset changed — update ESPHOME_LWIP_SOCK_RCVEVENT_OFFSET in lwip_fast_select.h");
154
155// Task handle is in main_task.c (esphome_main_task_handle) — shared with wake.h.
156
157// Saved original event_callback pointer — written once in first hook_socket(), read from TCP/IP task.
158static netconn_callback s_original_callback = NULL;
159
160#ifdef USE_OTA_PLATFORM_ESPHOME
161static struct netconn *s_ota_listener_conn = NULL;
163
164void esphome_fast_select_set_ota_listener_sock(struct lwip_sock *sock) {
165 s_ota_listener_conn = (sock != NULL) ? sock->conn : NULL;
166}
167#else
168void esphome_fast_select_set_ota_listener_sock(struct lwip_sock *sock) { (void) sock; }
169#endif
170
171// Wrapper callback: calls original event_callback + notifies main loop task.
172// Called from LwIP's TCP/IP thread when socket events occur (task context, not ISR).
173static void esphome_socket_event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len) {
174 // Call original LwIP event_callback first — updates rcvevent/sendevent/errevent,
175 // signals any select() waiters. This preserves all LwIP behavior.
176 // s_original_callback is always valid here: hook_socket() sets it before swapping
177 // the callback pointer, so this wrapper cannot run until it's initialized.
178 s_original_callback(conn, evt, len);
179 // Wake the main loop task if sleeping in ulTaskNotifyTake().
180 // Only notify on receive events to avoid spurious wakeups from send-ready events.
181 // NETCONN_EVT_ERROR is deliberately omitted: LwIP signals errors via RCVPLUS
182 // (rcvevent++ with a NULL pbuf or error in recvmbox), so error conditions
183 // already wake the main loop through the RCVPLUS path.
184 if (evt == NETCONN_EVT_RCVPLUS) {
185#ifdef USE_OTA_PLATFORM_ESPHOME
186 // Mark OTA pending-enable only for events on its listen socket. MUST happen
187 // before xTaskNotifyGive so the flags are visible when the main task wakes.
188 if (conn == s_ota_listener_conn) {
190 }
191#endif
192 TaskHandle_t task = esphome_main_task_handle;
193 if (task != NULL) {
194 xTaskNotifyGive(task);
195 }
196 }
197}
198
199// lwip_socket_dbg_get_socket() is a thin wrapper around the static
200// tryget_socket_unconn_nouse() — a direct array lookup without the refcount
201// that get_socket()/done_socket() uses. This is safe because:
202// 1. The only path to free_socket() is lwip_close(), called exclusively from the main loop
203// 2. The TCP/IP thread never frees socket slots (see "Socket slot lifetime" above)
204// 3. Both has_data() reads and lwip_close() run on the main loop — no concurrent free
205// If lwip_socket_dbg_get_socket() were ever removed, we could fall back to lwip_select().
206// Returns the sock only if both the sock and its netconn are valid, NULL otherwise.
207static inline struct lwip_sock *get_sock(int fd) {
208 struct lwip_sock *sock = lwip_socket_dbg_get_socket(fd);
209 if (sock == NULL || sock->conn == NULL)
210 return NULL;
211 return sock;
212}
213
214struct lwip_sock *esphome_lwip_get_sock(int fd) {
215 return get_sock(fd);
216}
217
218void esphome_lwip_hook_socket(struct lwip_sock *sock) {
219 // Save original callback once — all LwIP sockets share the same static event_callback
220 // (DEFAULT_SOCKET_EVENTCB in sockets.c, used for SOCK_RAW, SOCK_DGRAM, and SOCK_STREAM).
221 if (s_original_callback == NULL) {
222 s_original_callback = sock->conn->callback;
223 }
224
225 // Replace with our wrapper. Atomic on all supported platforms (32-bit aligned pointer write).
226 // TCP/IP thread sees either old or new pointer — both are valid.
227 sock->conn->callback = esphome_socket_event_callback;
228}
229
230bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable) {
231 if (sock == NULL || sock->conn == NULL)
232 return false;
233 if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_TCP)
234 return false;
235 if (sock->conn->pcb.tcp == NULL)
236 return false;
237 if (enable) {
238 tcp_nagle_disable(sock->conn->pcb.tcp);
239 } else {
240 tcp_nagle_enable(sock->conn->pcb.tcp);
241 }
242 return true;
243}
244
245#endif // USE_LWIP_FAST_SELECT
void esphome_wake_ota_component_any_context(void)
bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable)
Set or clear TCP_NODELAY on a socket's tcp_pcb directly.
void esphome_lwip_hook_socket(struct lwip_sock *sock)
Hook a socket's netconn callback to notify the main loop task on receive events.
struct lwip_sock * esphome_lwip_get_sock(int fd)
Look up a LwIP socket struct from a file descriptor.
void esphome_fast_select_set_ota_listener_sock(struct lwip_sock *sock)
Set the listener netconn that the fast-select callback filters OTA wakes against.
@ ESPHOME_LWIP_SOCK_RCVEVENT_OFFSET
TaskHandle_t esphome_main_task_handle
Main loop task handle and wake helpers — shared between wake.h (C++) and lwip_fast_select....
Definition main_task.c:4
uint32_t len