Skip to content

Commit 2318890

Browse files
shubhamdpHunsupJung
authored andcommitted
[ESP32] Enable LWIP_TCPIP_CORE_LOCKING by default and added static assert for the same (project-chip#28798)
* [ESP32] Enable LWIP_TCPIP_CORE_LOCKING by default and acquire lwip locks when initializing route_hook * inet: static assert if LWIP_TCPIP_CORE_LOCKING is disabled Static assert is disabled for ASR and bouffalolab platforms * typo
1 parent 04d2a81 commit 2318890

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

config/esp32/components/chip/Kconfig

+11
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ menu "CHIP Core"
186186
help
187187
Enable this option to use LwIP default IPv6 route hook for Route Information Option(RIO) feature.
188188

189+
config ENABLE_LWIP_THREAD_SAFETY
190+
bool "Enable LwIP Thread safety options"
191+
default y
192+
select LWIP_TCPIP_CORE_LOCKING
193+
select LWIP_CHECK_THREAD_SAFETY
194+
help
195+
CHIP SDK performs LwIP core locking before calling an LwIP API.
196+
To make the calls thread safe we have to enable LWIP_TCPIP_CORE_LOCKING.
197+
Here, we are also enabling LWIP_CHECK_THREAD_SAFETY which will assert when
198+
LwIP code gets called from any other context or without holding the LwIP lock.
199+
189200
endmenu # "Networking Options"
190201

191202
menu "System Options"

src/inet/UDPEndPointImplLwIP.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141

4242
static_assert(LWIP_VERSION_MAJOR > 1, "CHIP requires LwIP 2.0 or later");
4343

44+
#if !(CHIP_DEVICE_LAYER_TARGET_BL602 || CHIP_DEVICE_LAYER_TARGET_BL702 || CHIP_DEVICE_LAYER_TARGET_BL702L || \
45+
CHIP_DEVICE_LAYER_TARGET_ASR)
46+
static_assert(LWIP_TCPIP_CORE_LOCKING, "CHIP requires config LWIP_TCPIP_CORE_LOCKING enabled");
47+
#endif
48+
4449
#if !defined(RAW_FLAGS_MULTICAST_LOOP) || !defined(UDP_FLAGS_MULTICAST_LOOP) || !defined(raw_clear_flags) || \
4550
!defined(raw_set_flags) || !defined(udp_clear_flags) || !defined(udp_set_flags)
4651
#define HAVE_LWIP_MULTICAST_LOOP 0

src/platform/ESP32/route_hook/ESP32RouteHook.c

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "lwip/icmp6.h"
1818
#include "lwip/mld6.h"
1919
#include "lwip/netif.h"
20+
#include "lwip/opt.h"
2021
#include "lwip/prot/icmp6.h"
2122
#include "lwip/prot/ip6.h"
2223
#include "lwip/prot/nd6.h"
2324
#include "lwip/raw.h"
25+
#include "lwip/tcpip.h"
2426

2527
#define HOPLIM_MAX 255
2628
#define PIO_FLAG_ON_LINK (1 << 7)
@@ -155,25 +157,41 @@ esp_err_t esp_route_hook_init(esp_netif_t * netif)
155157
esp_err_t ret = ESP_OK;
156158

157159
ESP_RETURN_ON_FALSE(netif != NULL, ESP_ERR_INVALID_ARG, TAG, "Invalid network interface");
160+
161+
LOCK_TCPIP_CORE();
162+
158163
int netif_idx = esp_netif_get_netif_impl_index(netif);
159164
if (netif_idx < 0 || netif_idx > UINT8_MAX)
160165
{
166+
UNLOCK_TCPIP_CORE();
161167
return ESP_ERR_INVALID_SIZE;
162168
}
163169
lwip_netif = netif_get_by_index((uint8_t) netif_idx);
164-
ESP_RETURN_ON_FALSE(lwip_netif != NULL, ESP_ERR_INVALID_ARG, TAG, "Invalid network interface");
170+
171+
if (lwip_netif == NULL)
172+
{
173+
UNLOCK_TCPIP_CORE();
174+
ESP_LOGE(TAG, "Invalid network interface");
175+
return ESP_ERR_INVALID_ARG;
176+
}
165177

166178
for (esp_route_hook_t * iter = s_hooks; iter != NULL; iter = iter->next)
167179
{
168180
if (iter->netif == lwip_netif)
169181
{
182+
UNLOCK_TCPIP_CORE();
170183
ESP_LOGI(TAG, "Hook already installed on netif, skip...");
171184
return ESP_OK;
172185
}
173186
}
174187

175188
hook = (esp_route_hook_t *) malloc(sizeof(esp_route_hook_t));
176-
ESP_RETURN_ON_FALSE(hook != NULL, ESP_ERR_NO_MEM, TAG, "Cannot allocate hook");
189+
if (hook == NULL)
190+
{
191+
UNLOCK_TCPIP_CORE();
192+
ESP_LOGE(TAG, "Cannot allocate hook");
193+
return ESP_ERR_NO_MEM;
194+
}
177195

178196
ESP_GOTO_ON_FALSE(mld6_joingroup_netif(lwip_netif, ip_2_ip6(&router_group)) == ESP_OK, ESP_FAIL, exit, TAG,
179197
"Failed to join multicast group");
@@ -189,6 +207,9 @@ esp_err_t esp_route_hook_init(esp_netif_t * netif)
189207
s_hooks = hook;
190208

191209
exit:
210+
211+
UNLOCK_TCPIP_CORE();
212+
192213
if (ret != ESP_OK && hook != NULL)
193214
{
194215
free(hook);

0 commit comments

Comments
 (0)