Skip to content

Commit

Permalink
[cherrypick][v1.1-branch] Cherry picking lwip related fixes (#32370)
Browse files Browse the repository at this point in the history
* inet: ScopedLwIPLock for better safety and added locks at necessary places (#28655)

* inet: scoped lwip locks for better safety and add at few more places

* Do not static assert if LWIP_TCPIP_CORE_LOCKING is disabled

* Add scope for locks

* move out the error variable definition to the top

* [ESP32] Enable LWIP_TCPIP_CORE_LOCKING by default and added static assert for the same (#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

* UDPEndPointImplLwIP: Support LWIP_TCPIP_CORE_LOCKING=0 (#29057)

Wrap calls to LwIP APIs in `tcpip_api_call()`, as required.
When `LWIP_TCPIP_CORE_LOCKING` is enabled, this internally becomes `LOCK_TCPIP_CORE/UNLOCK_TCPIP_CORE`
and when it isn't, it posts a message to the TCPIP task to run the function.

Added CHIP stack locking to the UDP receive function.

---------

Co-authored-by: Deomid Ryabkov <rojer@rojer.me>
  • Loading branch information
shubhamdp and rojer authored Mar 12, 2024
1 parent 28733f1 commit 8956bbb
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 99 deletions.
11 changes: 11 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ menu "CHIP Core"
help
Enable this option to start a UDP Endpoint queue filter for mDNS Broadcast packets

config ENABLE_LWIP_THREAD_SAFETY
bool "Enable LwIP Thread safety options"
default y
select LWIP_TCPIP_CORE_LOCKING
select LWIP_CHECK_THREAD_SAFETY
help
CHIP SDK performs LwIP core locking before calling an LwIP API.
To make the calls thread safe we have to enable LWIP_TCPIP_CORE_LOCKING.
Here, we are also enabling LWIP_CHECK_THREAD_SAFETY which will assert when
LwIP code gets called from any other context or without holding the LwIP lock.

endmenu # "Networking Options"

menu "System Options"
Expand Down
1 change: 1 addition & 0 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static_library("inet") {
]

if (chip_system_config_use_lwip) {
sources += [ "EndPointStateLwIP.cpp" ]
public_deps += [ "${chip_root}/src/lwip" ]
}

Expand Down
85 changes: 85 additions & 0 deletions src/inet/EndPointStateLwIP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <inet/EndPointStateLwIP.h>

#include <lwip/sys.h>
#include <lwip/tcpip.h>

#include <platform/LockTracker.h>

namespace chip {
namespace Inet {

err_t EndPointStateLwIP::RunOnTCPIPRet(std::function<err_t()> fn)
{
assertChipStackLockedByCurrentThread();
#if LWIP_TCPIP_CORE_LOCKING
err_t err;
LOCK_TCPIP_CORE();
err = fn();
UNLOCK_TCPIP_CORE();
return err;
#else
// Post a message to the TCPIP task and wait for it to run.
static sys_sem_t sTCPIPSem;
static bool sTCPIPSemInited = false;
if (!sTCPIPSemInited)
{
err_t err = sys_sem_new(&sTCPIPSem, 0);
if (err != ERR_OK)
{
return err;
}
sTCPIPSemInited = true;
}

// tcpip_callback takes a C function pointer, so we can't pass a capturing lambda to it.
// Just store the state the function we pass to it needs in statics.
// This should be safe, since that function will execute before we return and there is no
// re-entry into this method.
static std::function<err_t()> sTCPIPFunction;
static err_t sTCPIPFunctionResult;
VerifyOrDie(sTCPIPFunction == nullptr);

sTCPIPFunction = fn;
const err_t err = tcpip_callback(
[](void * aCtx) {
sTCPIPFunctionResult = sTCPIPFunction();
sys_sem_signal(&sTCPIPSem);
},
nullptr);
if (err != ERR_OK)
{
return err;
}
sys_arch_sem_wait(&sTCPIPSem, 0);
sTCPIPFunction = nullptr;
return sTCPIPFunctionResult;
#endif
}

void EndPointStateLwIP::RunOnTCPIP(std::function<void()> fn)
{
VerifyOrDie(RunOnTCPIPRet([&fn]() {
fn();
return ERR_OK;
}) == ERR_OK);
}

} // namespace Inet
} // namespace chip
7 changes: 6 additions & 1 deletion src/inet/EndPointStateLwIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

#pragma once

#include <inet/EndPointBasis.h>
#include <functional>

#include <inet/EndPointBasis.h>
#include <inet/IPAddress.h>

struct udp_pcb;
Expand All @@ -46,6 +47,10 @@ class DLL_EXPORT EndPointStateLwIP
UDP = 1,
TCP = 2
} mLwIPEndPointType;

// Synchronously runs a function within the TCPIP task's context.
static void RunOnTCPIP(std::function<void()>);
static err_t RunOnTCPIPRet(std::function<err_t()>);
};

} // namespace Inet
Expand Down
7 changes: 7 additions & 0 deletions src/inet/TCPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
#include <lwip/tcp.h>
#include <lwip/tcpip.h>

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

#if !(CHIP_DEVICE_LAYER_TARGET_BL602 || CHIP_DEVICE_LAYER_TARGET_BL702 || CHIP_DEVICE_LAYER_TARGET_BL702L)
// TODO: Update to use RunOnTCPIP.
static_assert(LWIP_TCPIP_CORE_LOCKING, "CHIP requires config LWIP_TCPIP_CORE_LOCKING enabled");
#endif

namespace chip {
namespace Inet {

Expand Down
Loading

0 comments on commit 8956bbb

Please sign in to comment.