Skip to content

Commit

Permalink
[ESP32] move route_hook to platform layer (#24365)
Browse files Browse the repository at this point in the history
* [ESP32] move route_hook to platform layer

* [ESP32] use linked list to store the route table
  • Loading branch information
chshu authored and pull[bot] committed Apr 6, 2023
1 parent 39c2d0e commit 31fb917
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 89 deletions.
1 change: 0 additions & 1 deletion examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/linux/src"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-minimal-app/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
Expand Down
1 change: 0 additions & 1 deletion examples/bridge-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/on-off-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
PRIV_REQUIRES chip QRCode bt)
Expand Down
1 change: 0 additions & 1 deletion examples/light-switch-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/light-switch-app/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
Expand Down
1 change: 0 additions & 1 deletion examples/lighting-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/src"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
Expand Down
3 changes: 1 addition & 2 deletions examples/lock-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lock-app/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server"
Expand Down
1 change: 0 additions & 1 deletion examples/ota-provider-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-provider"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
EXCLUDE_SRCS
Expand Down
3 changes: 1 addition & 2 deletions examples/ota-requestor-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
)
Expand Down
5 changes: 0 additions & 5 deletions examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "esp_err.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "route_hook/esp_route_hook.h"
#include <app-common/zap-generated/attribute-id.h>
#include <app/server/Dnssd.h>
#include <app/util/util.h>
Expand Down Expand Up @@ -121,10 +120,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
// newly selected address.
chip::app::DnssdServer::Instance().StartServer();
}
if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)
{
ESP_ERROR_CHECK(esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")));
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
)
Expand Down
4 changes: 4 additions & 0 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ static_library("ESP32") {
"ConnectivityManagerImpl_WiFi.cpp",
"NetworkCommissioningDriver.cpp",
"NetworkCommissioningDriver.h",
"route_hook/ESP32RouteHook.c",
"route_hook/ESP32RouteHook.h",
"route_hook/ESP32RouteTable.c",
"route_hook/ESP32RouteTable.h",
]
if (chip_mdns == "platform") {
sources += [
Expand Down
3 changes: 3 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <platform/DiagnosticDataProvider.h>
#include <platform/ESP32/ESP32Utils.h>
#include <platform/ESP32/NetworkCommissioningDriver.h>
#include <platform/ESP32/route_hook/ESP32RouteHook.h>
#include <platform/internal/BLEManager.h>

#include "esp_event.h"
Expand Down Expand Up @@ -1094,6 +1095,8 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t &
event.Type = DeviceEventType::kInterfaceIpAddressChanged;
event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV6_Assigned;
PlatformMgr().PostEventOrDie(&event);

esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
}

} // namespace DeviceLayer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "esp_route_hook.h"
#include <platform/ESP32/route_hook/ESP32RouteHook.h>

#include <platform/ESP32/route_hook/ESP32RouteTable.h>
#include <stdint.h>
#include <string.h>

#include "esp_check.h"
#include "esp_err.h"
#include "esp_netif.h"
#include "esp_route_table.h"

#include "lwip/icmp6.h"
#include "lwip/mld6.h"
Expand All @@ -26,8 +26,6 @@
#define PIO_FLAG_ON_LINK (1 << 7)
#define PIO_FLAG_AUTO_CONFIG (1 << 6)

#define TAG "ROUTE_HOOK"

typedef struct esp_route_hook_t
{
struct netif * netif;
Expand Down Expand Up @@ -137,12 +135,12 @@ static uint8_t icmp6_raw_recv_handler(void * arg, struct raw_pcb * pcb, struct p

if (p->tot_len != p->len)
{
ESP_LOGW(TAG, "Ignore segmented ICMP packet");
ESP_LOGI(TAG, "Ignore segmented ICMP packet");
return 0;
}
if (p->tot_len <= sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
{
ESP_LOGW(TAG, "Ignore invalid ICMP packet");
ESP_LOGI(TAG, "Ignore invalid ICMP packet");
return 0;
}
if (!ip6_addr_islinklocal(&dest) && !ip6_addr_isallnodes_linklocal(&dest) && !ip6_addr_isallrouters_linklocal(&dest))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "esp_route_table.h"
#include <platform/ESP32/route_hook/ESP32RouteTable.h>

#include <string.h>

Expand All @@ -14,48 +14,53 @@
#include "lwip/netif.h"
#include "lwip/timeouts.h"

#define MAX_RIO_ROUTE 20
#define MAX_RIO_TIMEOUT UINT32_MAX / (1000 * 4) // lwIP defined reasonable timeout value

#define TAG "ROUTE_HOOK"

static esp_route_entry_t s_route_entries[MAX_RIO_ROUTE];
static esp_route_entry_t * s_route_entries = NULL;

static esp_route_entry_t * find_route_entry(const esp_route_entry_t * route_entry)
{
for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++)
for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next)
{
if (s_route_entries[i].netif == NULL)
{
break;
}
if (s_route_entries[i].netif == route_entry->netif && s_route_entries[i].prefix_length == route_entry->prefix_length &&
memcmp(s_route_entries[i].gateway.addr, route_entry->gateway.addr, sizeof(route_entry->gateway.addr)) == 0 &&
memcmp(s_route_entries[i].prefix.addr, route_entry->prefix.addr, route_entry->prefix_length / 8) == 0)
if (iter->netif == route_entry->netif && iter->prefix_length == route_entry->prefix_length &&
memcmp(iter->gateway.addr, route_entry->gateway.addr, sizeof(route_entry->gateway.addr)) == 0 &&
memcmp(iter->prefix.addr, route_entry->prefix.addr, route_entry->prefix_length / 8) == 0)
{
return &s_route_entries[i];
return iter;
}
}

return NULL;
}

static esp_route_entry_t * find_empty_route_entry(void)
static esp_err_t remove_route_entry(esp_route_entry_t * route_entry)
{
for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++)
if (s_route_entries == route_entry)
{
s_route_entries = s_route_entries->next;
free(route_entry);
return ESP_OK;
}

for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next)
{
if (s_route_entries[i].netif == NULL)
if (iter->next == route_entry)
{
return &s_route_entries[i];
iter->next = route_entry->next;
free(route_entry);
return ESP_OK;
}
}
return NULL;

ESP_LOGW(TAG, "The given route entry is not found");
return ESP_ERR_NOT_FOUND;
}

static void route_timeout_handler(void * arg)
{
esp_route_entry_t * route = (esp_route_entry_t *) arg;

esp_route_table_remove_route_entry(route);
remove_route_entry(route);
}

esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * route_entry)
Expand All @@ -69,16 +74,21 @@ esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * ro

if (entry == NULL)
{
entry = find_empty_route_entry();
entry = (esp_route_entry_t *) malloc(sizeof(esp_route_entry_t));
if (entry == NULL)
{
ESP_LOGW(TAG, "Cannot allocate route entry");
return NULL;
}

entry->netif = route_entry->netif;
entry->gateway = route_entry->gateway;
ip6_addr_assign_zone(&entry->gateway, IP6_UNICAST, entry->netif);
entry->prefix = route_entry->prefix;
entry->prefix_length = route_entry->prefix_length;

entry->next = s_route_entries;
s_route_entries = entry;
}
else
{
Expand All @@ -90,25 +100,8 @@ esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * ro
{
sys_timeout(entry->lifetime_seconds * 1000, route_timeout_handler, entry);
}
return entry;
}

esp_err_t esp_route_table_remove_route_entry(esp_route_entry_t * route_entry)
{
if (route_entry < &s_route_entries[0] || route_entry > &s_route_entries[LWIP_ARRAYSIZE(s_route_entries)])
{
return ESP_ERR_INVALID_ARG;
}
route_entry->netif = NULL;
for (esp_route_entry_t * moved = route_entry; moved < &s_route_entries[LWIP_ARRAYSIZE(s_route_entries) - 1]; moved++)
{
*moved = *(moved + 1);
if (moved->netif == NULL)
{
break;
}
}
return ESP_OK;
return entry;
}

static inline bool is_better_route(const esp_route_entry_t * lhs, const esp_route_entry_t * rhs)
Expand All @@ -134,15 +127,11 @@ struct netif * lwip_hook_ip6_route(const ip6_addr_t * src, const ip6_addr_t * de
{
esp_route_entry_t * route = NULL;

for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++)
for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next)
{
if (s_route_entries[i].netif == NULL)
{
break;
}
if (route_match(&s_route_entries[i], dest) && is_better_route(&s_route_entries[i], route))
if (route_match(iter, dest) && is_better_route(iter, route))
{
route = &s_route_entries[i];
route = iter;
}
}

Expand All @@ -160,16 +149,11 @@ const ip6_addr_t * lwip_hook_nd6_get_gw(struct netif * netif, const ip6_addr_t *
{
esp_route_entry_t * route = NULL;

for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++)
for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next)
{
if (s_route_entries[i].netif == NULL)
{
break;
}
if (s_route_entries[i].netif == netif && route_match(&s_route_entries[i], dest) &&
is_better_route(&s_route_entries[i], route))
if (iter->netif == netif && route_match(iter, dest) && is_better_route(iter, route))
{
route = &s_route_entries[i];
route = iter;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
extern "C" {
#endif

#define TAG "ROUTE_HOOK"

/**
* @brief Route table entry
*
*/
typedef struct
typedef struct esp_route_entry_t
{
ip6_addr_t prefix;
uint8_t prefix_length;
ip6_addr_t gateway;
int8_t preference;
uint32_t lifetime_seconds;
struct netif * netif;
struct esp_route_entry_t * next;
} esp_route_entry_t;

/**
Expand All @@ -38,18 +41,6 @@ typedef struct
*/
esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * route_entry);

/**
* @brief Removes an entry from the route table
*
* @param[in] route_entry The route entry to be removed
*
* @return
* - ESP_OK
* - ESP_ERR_INVALID_ARG The provided route_entry is not in the route table.
*
*/
esp_err_t esp_route_table_remove_route_entry(esp_route_entry_t * route_entry);

/**
* @brief The lwIP ip6 route hook, called by the lwIP function ip6_route when sending packets.
*
Expand Down

0 comments on commit 31fb917

Please sign in to comment.