From ac2515e19979aebb9b37db52c10aca8fa39a6227 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Thu, 7 Sep 2023 13:17:02 +0800 Subject: [PATCH] refactor(lwip): Added on/off switch for LwIP stack * This switch allows applications to replace lwip with a different IP stack or just make it build if it is a dependency but not actually needed. --- components/esp-tls/CMakeLists.txt | 2 +- components/lwip/CMakeLists.txt | 360 +++++++++--------- components/lwip/Kconfig | 11 + components/mbedtls/CMakeLists.txt | 3 +- .../sockets/udp_client/sdkconfig.ci.linux | 1 + .../sockets/udp_client/sdkconfig.defaults | 0 .../udp_client/sdkconfig.defaults.linux | 1 + 7 files changed, 199 insertions(+), 179 deletions(-) create mode 100644 examples/protocols/sockets/udp_client/sdkconfig.defaults create mode 100644 examples/protocols/sockets/udp_client/sdkconfig.defaults.linux diff --git a/components/esp-tls/CMakeLists.txt b/components/esp-tls/CMakeLists.txt index b40b249c4c..c8fe0d5a65 100644 --- a/components/esp-tls/CMakeLists.txt +++ b/components/esp-tls/CMakeLists.txt @@ -35,7 +35,7 @@ set_property(TARGET ${lwip} APPEND PROPERTY LINK_INTERFACE_MULTIPLICITY 5) else() # Check if LWIP in the build for linux target to adapt esp-tls compatibility layer idf_build_get_property(build_components BUILD_COMPONENTS) - if("lwip" IN_LIST build_components) + if(CONFIG_LWIP_ENABLE) target_compile_definitions(${COMPONENT_LIB} PRIVATE ESP_TLS_WITH_LWIP=1) endif() endif() diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index ca1e8e71bb..d0526fd4ba 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -1,107 +1,76 @@ idf_build_get_property(target IDF_TARGET) -if(NOT ${target} STREQUAL "linux") - # ESP platform targets share the same port folder - set(target esp32xx) -endif() -set(include_dirs - include - include/apps - include/apps/sntp - lwip/src/include - port/include - port/freertos/include/ - port/${target}/include - port/${target}/include/arch - port/${target}/include/sys - ) +if(CONFIG_LWIP_ENABLE) + if(NOT ${target} STREQUAL "linux") + # ESP platform targets share the same port folder + set(target esp32xx) + endif() -set(srcs - "apps/sntp/sntp.c" - "lwip/src/api/api_lib.c" - "lwip/src/api/api_msg.c" - "lwip/src/api/err.c" - "lwip/src/api/if_api.c" - "lwip/src/api/netbuf.c" - "lwip/src/api/netdb.c" - "lwip/src/api/netifapi.c" - "lwip/src/api/sockets.c" - "lwip/src/api/tcpip.c" - "lwip/src/apps/sntp/sntp.c" - "lwip/src/apps/netbiosns/netbiosns.c" - "lwip/src/core/def.c" - "lwip/src/core/dns.c" - "lwip/src/core/inet_chksum.c" - "lwip/src/core/init.c" - "lwip/src/core/ip.c" - "lwip/src/core/mem.c" - "lwip/src/core/memp.c" - "lwip/src/core/netif.c" - "lwip/src/core/pbuf.c" - "lwip/src/core/raw.c" - "lwip/src/core/stats.c" - "lwip/src/core/sys.c" - "lwip/src/core/tcp.c" - "lwip/src/core/tcp_in.c" - "lwip/src/core/tcp_out.c" - "lwip/src/core/timeouts.c" - "lwip/src/core/udp.c" - "lwip/src/core/ipv4/autoip.c" - "lwip/src/core/ipv4/dhcp.c" - "lwip/src/core/ipv4/etharp.c" - "lwip/src/core/ipv4/icmp.c" - "lwip/src/core/ipv4/igmp.c" - "lwip/src/core/ipv4/ip4.c" - "lwip/src/core/ipv4/ip4_napt.c" - "lwip/src/core/ipv4/ip4_addr.c" - "lwip/src/core/ipv4/ip4_frag.c" - "lwip/src/core/ipv6/dhcp6.c" - "lwip/src/core/ipv6/ethip6.c" - "lwip/src/core/ipv6/icmp6.c" - "lwip/src/core/ipv6/inet6.c" - "lwip/src/core/ipv6/ip6.c" - "lwip/src/core/ipv6/ip6_addr.c" - "lwip/src/core/ipv6/ip6_frag.c" - "lwip/src/core/ipv6/mld6.c" - "lwip/src/core/ipv6/nd6.c" - "lwip/src/netif/ethernet.c" - "lwip/src/netif/bridgeif.c" - "lwip/src/netif/bridgeif_fdb.c" - "lwip/src/netif/slipif.c" - "lwip/src/netif/slipif.c" - "lwip/src/netif/ppp/auth.c" - "lwip/src/netif/ppp/ccp.c" - "lwip/src/netif/ppp/chap-md5.c" - "lwip/src/netif/ppp/chap-new.c" - "lwip/src/netif/ppp/chap_ms.c" - "lwip/src/netif/ppp/demand.c" - "lwip/src/netif/ppp/eap.c" - "lwip/src/netif/ppp/ecp.c" - "lwip/src/netif/ppp/eui64.c" - "lwip/src/netif/ppp/fsm.c" - "lwip/src/netif/ppp/ipcp.c" - "lwip/src/netif/ppp/ipv6cp.c" - "lwip/src/netif/ppp/lcp.c" - "lwip/src/netif/ppp/magic.c" - "lwip/src/netif/ppp/mppe.c" - "lwip/src/netif/ppp/multilink.c" - "lwip/src/netif/ppp/ppp.c" - "lwip/src/netif/ppp/pppapi.c" - "lwip/src/netif/ppp/pppcrypt.c" - "lwip/src/netif/ppp/pppoe.c" - "lwip/src/netif/ppp/pppol2tp.c" - "lwip/src/netif/ppp/pppos.c" - "lwip/src/netif/ppp/upap.c" - "lwip/src/netif/ppp/utils.c" - "lwip/src/netif/ppp/vj.c" - "port/hooks/tcp_isn_default.c" - "port/hooks/lwip_default_hooks.c" - "port/debug/lwip_debug.c" - "port/sockets_ext.c" - "port/freertos/sys_arch.c") + set(include_dirs + include + include/apps + include/apps/sntp + lwip/src/include + port/include + port/freertos/include/ + port/${target}/include + port/${target}/include/arch + port/${target}/include/sys + ) -if(CONFIG_LWIP_PPP_SUPPORT) - list(APPEND srcs + set(srcs + "apps/sntp/sntp.c" + "lwip/src/api/api_lib.c" + "lwip/src/api/api_msg.c" + "lwip/src/api/err.c" + "lwip/src/api/if_api.c" + "lwip/src/api/netbuf.c" + "lwip/src/api/netdb.c" + "lwip/src/api/netifapi.c" + "lwip/src/api/sockets.c" + "lwip/src/api/tcpip.c" + "lwip/src/apps/sntp/sntp.c" + "lwip/src/apps/netbiosns/netbiosns.c" + "lwip/src/core/def.c" + "lwip/src/core/dns.c" + "lwip/src/core/inet_chksum.c" + "lwip/src/core/init.c" + "lwip/src/core/ip.c" + "lwip/src/core/mem.c" + "lwip/src/core/memp.c" + "lwip/src/core/netif.c" + "lwip/src/core/pbuf.c" + "lwip/src/core/raw.c" + "lwip/src/core/stats.c" + "lwip/src/core/sys.c" + "lwip/src/core/tcp.c" + "lwip/src/core/tcp_in.c" + "lwip/src/core/tcp_out.c" + "lwip/src/core/timeouts.c" + "lwip/src/core/udp.c" + "lwip/src/core/ipv4/autoip.c" + "lwip/src/core/ipv4/dhcp.c" + "lwip/src/core/ipv4/etharp.c" + "lwip/src/core/ipv4/icmp.c" + "lwip/src/core/ipv4/igmp.c" + "lwip/src/core/ipv4/ip4.c" + "lwip/src/core/ipv4/ip4_napt.c" + "lwip/src/core/ipv4/ip4_addr.c" + "lwip/src/core/ipv4/ip4_frag.c" + "lwip/src/core/ipv6/dhcp6.c" + "lwip/src/core/ipv6/ethip6.c" + "lwip/src/core/ipv6/icmp6.c" + "lwip/src/core/ipv6/inet6.c" + "lwip/src/core/ipv6/ip6.c" + "lwip/src/core/ipv6/ip6_addr.c" + "lwip/src/core/ipv6/ip6_frag.c" + "lwip/src/core/ipv6/mld6.c" + "lwip/src/core/ipv6/nd6.c" + "lwip/src/netif/ethernet.c" + "lwip/src/netif/bridgeif.c" + "lwip/src/netif/bridgeif_fdb.c" + "lwip/src/netif/slipif.c" + "lwip/src/netif/slipif.c" "lwip/src/netif/ppp/auth.c" "lwip/src/netif/ppp/ccp.c" "lwip/src/netif/ppp/chap-md5.c" @@ -127,40 +96,77 @@ if(CONFIG_LWIP_PPP_SUPPORT) "lwip/src/netif/ppp/upap.c" "lwip/src/netif/ppp/utils.c" "lwip/src/netif/ppp/vj.c" - "lwip/src/netif/ppp/polarssl/arc4.c" - "lwip/src/netif/ppp/polarssl/des.c" - "lwip/src/netif/ppp/polarssl/md4.c" - "lwip/src/netif/ppp/polarssl/md5.c" - "lwip/src/netif/ppp/polarssl/sha1.c") -endif() + "port/hooks/tcp_isn_default.c" + "port/hooks/lwip_default_hooks.c" + "port/debug/lwip_debug.c" + "port/sockets_ext.c" + "port/freertos/sys_arch.c") + + if(CONFIG_LWIP_PPP_SUPPORT) + list(APPEND srcs + "lwip/src/netif/ppp/auth.c" + "lwip/src/netif/ppp/ccp.c" + "lwip/src/netif/ppp/chap-md5.c" + "lwip/src/netif/ppp/chap-new.c" + "lwip/src/netif/ppp/chap_ms.c" + "lwip/src/netif/ppp/demand.c" + "lwip/src/netif/ppp/eap.c" + "lwip/src/netif/ppp/ecp.c" + "lwip/src/netif/ppp/eui64.c" + "lwip/src/netif/ppp/fsm.c" + "lwip/src/netif/ppp/ipcp.c" + "lwip/src/netif/ppp/ipv6cp.c" + "lwip/src/netif/ppp/lcp.c" + "lwip/src/netif/ppp/magic.c" + "lwip/src/netif/ppp/mppe.c" + "lwip/src/netif/ppp/multilink.c" + "lwip/src/netif/ppp/ppp.c" + "lwip/src/netif/ppp/pppapi.c" + "lwip/src/netif/ppp/pppcrypt.c" + "lwip/src/netif/ppp/pppoe.c" + "lwip/src/netif/ppp/pppol2tp.c" + "lwip/src/netif/ppp/pppos.c" + "lwip/src/netif/ppp/upap.c" + "lwip/src/netif/ppp/utils.c" + "lwip/src/netif/ppp/vj.c" + "lwip/src/netif/ppp/polarssl/arc4.c" + "lwip/src/netif/ppp/polarssl/des.c" + "lwip/src/netif/ppp/polarssl/md4.c" + "lwip/src/netif/ppp/polarssl/md5.c" + "lwip/src/netif/ppp/polarssl/sha1.c") + endif() + + if(NOT ${target} STREQUAL "linux") + # Support for vfs and linker fragments only for target builds + set(linker_fragments linker.lf) + if(CONFIG_VFS_SUPPORT_IO) + list(APPEND srcs "port/${target}/vfs_lwip.c") + else() + list(APPEND srcs "port/${target}/no_vfs_syscalls.c") + endif() + else() + # This wraps some posix IO functions to conditionally pass control to lwip + list(APPEND srcs "port/${target}/vfs_lwip.c") + endif() + + if(CONFIG_LWIP_ICMP) + list(APPEND srcs + "apps/ping/esp_ping.c" + "apps/ping/ping.c" + "apps/ping/ping_sock.c") + endif() + + if(CONFIG_LWIP_DHCPS) + list(APPEND srcs "apps/dhcpserver/dhcpserver.c") + endif() + + if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) + list(APPEND srcs "port/esp32xx/netif/dhcp_state.c") + endif() +endif() # CONFIG_LWIP_ENABLE if(NOT ${target} STREQUAL "linux") - # Support for vfs and linker fragments only for target builds set(priv_requires vfs) - set(linker_fragments linker.lf) - if(CONFIG_VFS_SUPPORT_IO) - list(APPEND srcs "port/${target}/vfs_lwip.c") - else() - list(APPEND srcs "port/${target}/no_vfs_syscalls.c") - endif() -else() - # This wraps some posix IO functions to conditionally pass control to lwip - list(APPEND srcs "port/${target}/vfs_lwip.c") -endif() - -if(CONFIG_LWIP_ICMP) - list(APPEND srcs - "apps/ping/esp_ping.c" - "apps/ping/ping.c" - "apps/ping/ping_sock.c") -endif() - -if(CONFIG_LWIP_DHCPS) - list(APPEND srcs "apps/dhcpserver/dhcpserver.c") -endif() - -if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) - list(APPEND srcs "port/esp32xx/netif/dhcp_state.c") endif() idf_component_register(SRCS "${srcs}" @@ -168,53 +174,55 @@ idf_component_register(SRCS "${srcs}" LDFRAGMENTS ${linker_fragments} PRIV_REQUIRES ${priv_requires}) -# lots of LWIP source files evaluate macros that check address of stack variables -target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-address) -target_compile_definitions(${COMPONENT_LIB} PRIVATE ESP_LWIP_COMPONENT_BUILD) +if(CONFIG_LWIP_ENABLE) + # lots of LWIP source files evaluate macros that check address of stack variables + target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-address) + target_compile_definitions(${COMPONENT_LIB} PRIVATE ESP_LWIP_COMPONENT_BUILD) -set_source_files_properties( - lwip/src/netif/ppp/pppos.c - PROPERTIES COMPILE_FLAGS - -Wno-type-limits + set_source_files_properties( + lwip/src/netif/ppp/pppos.c + PROPERTIES COMPILE_FLAGS + -Wno-type-limits + ) + # "comparison is always false due to limited range of data type" warning + # when setting CONFIG_LWIP_TCP_WND_DEFAULT to 65535 + set_source_files_properties( + lwip/src/core/tcp.c + PROPERTIES COMPILE_FLAGS + -Wno-type-limits ) -# "comparison is always false due to limited range of data type" warning -# when setting CONFIG_LWIP_TCP_WND_DEFAULT to 65535 -set_source_files_properties( - lwip/src/core/tcp.c - PROPERTIES COMPILE_FLAGS - -Wno-type-limits -) -# ignore some declaration mismatches -set_source_files_properties( - lwip/src/netif/ppp/chap_ms.c - PROPERTIES COMPILE_FLAGS - -Wno-array-parameter -) + # ignore some declaration mismatches + set_source_files_properties( + lwip/src/netif/ppp/chap_ms.c + PROPERTIES COMPILE_FLAGS + -Wno-array-parameter + ) -if(CONFIG_OPENTHREAD_ENABLED) - idf_component_optional_requires(PRIVATE openthread) -endif() - -if(CONFIG_ETH_ENABLED) - idf_component_optional_requires(PRIVATE esp_eth) -endif() - -if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) - idf_component_optional_requires(PRIVATE nvs_flash) -endif() - -if(${target} STREQUAL "linux") - set(THREADS_PREFER_PTHREAD_FLAG ON) - find_package(Threads REQUIRED) - target_link_libraries(${COMPONENT_LIB} PRIVATE Threads::Threads) - set(WRAP_FUNCTIONS select - read - fcntl - write - close) - foreach(wrap ${WRAP_FUNCTIONS}) - target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}") - target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __wrap_${wrap}") - endforeach() + if(CONFIG_OPENTHREAD_ENABLED) + idf_component_optional_requires(PRIVATE openthread) + endif() + + if(CONFIG_ETH_ENABLED) + idf_component_optional_requires(PRIVATE esp_eth) + endif() + + if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) + idf_component_optional_requires(PRIVATE nvs_flash) + endif() + + if(${target} STREQUAL "linux") + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(${COMPONENT_LIB} PRIVATE Threads::Threads) + set(WRAP_FUNCTIONS select + read + fcntl + write + close) + foreach(wrap ${WRAP_FUNCTIONS}) + target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}") + target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __wrap_${wrap}") + endforeach() + endif() endif() diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index d3be4246d0..cb8f86bd4b 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -1,4 +1,15 @@ menu "LWIP" + config LWIP_ENABLE + bool "Enable LwIP stack" + default y if !IDF_TARGET_LINUX + default n if IDF_TARGET_LINUX + help + Builds normally if selected. Excludes LwIP from build if unselected, even if it is a + dependency of a component or application. + Some applications can switch their IP stacks, e.g., when switching between chip + and Linux targets (LwIP stack vs. Linux IP stack). Since the LwIP dependency cannot + easily be excluded based on a Kconfig option, it has to be a dependency in all cases. + This switch allows the LwIP stack to be built selectively, even if it is a dependency. config LWIP_LOCAL_HOSTNAME string "Local netif hostname" diff --git a/components/mbedtls/CMakeLists.txt b/components/mbedtls/CMakeLists.txt index 1fe4dda01b..b183388cd7 100644 --- a/components/mbedtls/CMakeLists.txt +++ b/components/mbedtls/CMakeLists.txt @@ -134,8 +134,7 @@ endif() # net_sockets.c should only be compiled if BSD socket functions are available. # Do this by checking if lwip component is included into the build. -idf_build_get_property(build_components BUILD_COMPONENTS) -if(lwip IN_LIST build_components) +if(CONFIG_LWIP_ENABLE) list(APPEND mbedtls_target_sources "${COMPONENT_DIR}/port/net_sockets.c") idf_component_get_property(lwip_lib lwip COMPONENT_LIB) target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${lwip_lib}) diff --git a/examples/protocols/sockets/udp_client/sdkconfig.ci.linux b/examples/protocols/sockets/udp_client/sdkconfig.ci.linux index 6f95c91824..bcf15875e6 100644 --- a/examples/protocols/sockets/udp_client/sdkconfig.ci.linux +++ b/examples/protocols/sockets/udp_client/sdkconfig.ci.linux @@ -1,3 +1,4 @@ CONFIG_IDF_TARGET="linux" CONFIG_EXAMPLE_IPV4_ADDR="127.0.0.1" CONFIG_EXAMPLE_CONNECT_LWIP_TAPIF=n +CONFIG_LWIP_ENABLE=y diff --git a/examples/protocols/sockets/udp_client/sdkconfig.defaults b/examples/protocols/sockets/udp_client/sdkconfig.defaults new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/protocols/sockets/udp_client/sdkconfig.defaults.linux b/examples/protocols/sockets/udp_client/sdkconfig.defaults.linux new file mode 100644 index 0000000000..242f3ccd8c --- /dev/null +++ b/examples/protocols/sockets/udp_client/sdkconfig.defaults.linux @@ -0,0 +1 @@ +CONFIG_LWIP_ENABLE=y