From 86089be928599c3ff6181ab58b0bcf980aa517c1 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 20 Dec 2024 08:16:25 +0100 Subject: [PATCH 1/2] feat(lwip): Hardcode NETIF_API=1 to support POSIX netif API --- components/lwip/CMakeLists.txt | 7 ++----- components/lwip/Kconfig | 8 -------- components/lwip/port/include/lwipopts.h | 9 +-------- components/lwip/port/linux/include/net/if.h | 22 ++++++++++++++++++++- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index 6fb648f5d6..4daec4c224 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -100,11 +100,8 @@ if(CONFIG_LWIP_ENABLE) "port/hooks/lwip_default_hooks.c" "port/debug/lwip_debug.c" "port/sockets_ext.c" - "port/freertos/sys_arch.c") - - if(CONFIG_LWIP_NETIF_API) - list(APPEND srcs "port/if_index.c") - endif() + "port/freertos/sys_arch.c" + "port/if_index.c") if(CONFIG_LWIP_PPP_SUPPORT) list(APPEND srcs diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index bbaa605f93..04b20b1702 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -18,14 +18,6 @@ menu "LWIP" The default name this device will report to other devices on the network. Could be updated at runtime with esp_netif_set_hostname() - config LWIP_NETIF_API - bool "Enable usage of standard POSIX APIs in LWIP" - default n - help - If this feature is enabled, standard POSIX APIs: if_indextoname(), if_nametoindex() - could be used to convert network interface index to name - instead of IDF specific esp-netif APIs (such as esp_netif_get_netif_impl_name()) - config LWIP_TCPIP_TASK_PRIO int "LWIP TCP/IP Task Priority" default 18 diff --git a/components/lwip/port/include/lwipopts.h b/components/lwip/port/include/lwipopts.h index 3a7671472d..b3c9df8d86 100644 --- a/components/lwip/port/include/lwipopts.h +++ b/components/lwip/port/include/lwipopts.h @@ -709,18 +709,11 @@ static inline uint32_t timeout_from_offered(uint32_t lease, uint32_t min) * LWIP_DHCP_DISCOVER_ADD_HOSTNAME==1: include hostname opt in discover packets. * If the hostname is not set in the DISCOVER packet, then some servers might issue * an OFFER with hostname configured and consequently reject the REQUEST with any other hostname. + * LWIP_NETIF_API==1: Support netif APIs (if_nametoindex and if_indextoname) */ #define LWIP_NETIF_HOSTNAME 1 #define LWIP_DHCP_DISCOVER_ADD_HOSTNAME 1 - -/** - * LWIP_NETIF_API==1: Support netif api (in netifapi.c) - */ -#ifdef CONFIG_LWIP_NETIF_API #define LWIP_NETIF_API 1 -#else -#define LWIP_NETIF_API 0 -#endif /** * LWIP_NETIF_STATUS_CALLBACK==1: Support a callback function whenever an interface diff --git a/components/lwip/port/linux/include/net/if.h b/components/lwip/port/linux/include/net/if.h index e3c24eb941..2d980f6e8b 100644 --- a/components/lwip/port/linux/include/net/if.h +++ b/components/lwip/port/linux/include/net/if.h @@ -1,6 +1,26 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #pragma once + +// Need to provide declarations of if_nametoindex and if_indextoname functions +// as we don't want to bring lwip specific defines +// (since we're on linux target and likely using linux tcp/ip stack) + +/** + * @brief Get the interface index for the given interface name. + * @param ifname The interface name. + * @return The interface index. + */ +unsigned int if_nametoindex(const char *ifname); + +/** + * @brief Get the interface name for the given interface index. + * + * @param ifindex The interface index. + * @param ifname The buffer to store the interface name. + * @return char* The interface name. + */ +char *if_indextoname(unsigned int ifindex, char *ifname); From 42d2a131f61fcf2801604e2daf84e5969445bae8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 29 Jan 2025 17:06:23 +0100 Subject: [PATCH 2/2] fix(lwip): Remove if_indextoname() impl in favor of LWIP_NETIF_API ESP-IDF already hardcodes LWIP_NETIF_API=1, so the related netif APIs are defined unconditionally. --- components/lwip/apps/ping/ping_sock.c | 27 ------------------- .../main/tcp_client_multiple.c | 4 --- 2 files changed, 31 deletions(-) diff --git a/components/lwip/apps/ping/ping_sock.c b/components/lwip/apps/ping/ping_sock.c index 827f01e15e..8870eaaae7 100644 --- a/components/lwip/apps/ping/ping_sock.c +++ b/components/lwip/apps/ping/ping_sock.c @@ -26,33 +26,6 @@ #include "ping/ping_sock.h" #include "esp_check.h" -#ifndef CONFIG_LWIP_NETIF_API -// If POSIX NETIF_API not enabled, we need to supply the implementation of if_indextoname() -// using tcpip_api_call() -#include "lwip/priv/tcpip_priv.h" - -struct tcpip_netif_name { - struct tcpip_api_call_data call; - u8_t ifindex; - char *ifname; -}; - -static err_t do_netif_index_to_name(struct tcpip_api_call_data *msg) -{ - struct tcpip_netif_name *params = __containerof(msg, struct tcpip_netif_name, call); - return netif_index_to_name(params->ifindex, params->ifname) ? ERR_OK : ERR_IF; -} - -char *if_indextoname(unsigned int ifindex, char *ifname) -{ - struct tcpip_netif_name params = { .ifindex = ifindex, .ifname = ifname }; - if (tcpip_api_call(do_netif_index_to_name, ¶ms.call) != ERR_OK) { - return NULL; - } - return ifname; -} -#endif // CONFIG_LWIP_NETIF_API == 0 - const static char *TAG = "ping_sock"; #define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + \ diff --git a/examples/protocols/sockets/tcp_client_multi_net/main/tcp_client_multiple.c b/examples/protocols/sockets/tcp_client_multi_net/main/tcp_client_multiple.c index 2c91114600..059a969711 100644 --- a/examples/protocols/sockets/tcp_client_multi_net/main/tcp_client_multiple.c +++ b/examples/protocols/sockets/tcp_client_multi_net/main/tcp_client_multiple.c @@ -46,11 +46,7 @@ static void app_multiple_handle(esp_ip4_addr_t *ip4_addr, esp_netif_t *esp_netif */ #if CONFIG_EXAMPLE_BIND_SOCKET_TO_NETIF_NAME struct ifreq ifr; -#if !CONFIG_LWIP_NETIF_API - esp_netif_get_netif_impl_name(esp_netif, ifr.ifr_name); -#else if_indextoname(esp_netif_get_netif_impl_index(esp_netif), ifr.ifr_name); -#endif int ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(struct ifreq)); if (ret < 0) { ESP_LOGE(TAG, "\"%s\" Unable to bind socket to specified interface: errno %d", netif_name, errno);