From fa97004faf03240a1446321a0547eb0ee697e0d5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 22 Jul 2022 10:14:37 +0200 Subject: [PATCH 1/4] lwip: Support for linux target Implement linux port layer and reuse the original FreeRTOS layer that's compiled and used on linux target as well, by means of FreeRTOS simulator. --- .gitlab/ci/host-test.yml | 18 ++++++ components/esp_netif/CMakeLists.txt | 12 ++-- .../linux/stubs/include/machine/endian.h | 8 +++ components/esp_netif/lwip/esp_netif_lwip.c | 1 + .../esp_netif/lwip/esp_netif_lwip_defaults.c | 3 + components/lwip/CMakeLists.txt | 47 +++++++++----- components/lwip/apps/dhcpserver/dhcpserver.c | 2 +- components/lwip/apps/ping/ping.c | 1 + components/lwip/apps/ping/ping_sock.c | 3 +- components/lwip/apps/sntp/sntp.c | 1 + .../lwip/port/{esp32 => }/debug/lwip_debug.c | 18 ++---- components/lwip/port/esp32/include/arch/cc.h | 32 +--------- .../lwip/port/esp32/include/arch/perf.h | 32 +--------- .../lwip/port/esp32/include/arch/vfs_lwip.h | 18 ++---- .../lwip/port/esp32/include/arpa/inet.h | 20 ------ .../port/esp32/include/debug/lwip_debug.h | 33 ---------- components/lwip/port/esp32/include/netdb.h | 48 --------------- .../lwip/port/esp32/include/netinet/in.h | 22 ------- .../lwip/port/esp32/include/netinet/tcp.h | 21 ------- .../lwip/port/esp32/include/sys/socket.h | 30 +++------ components/lwip/port/esp32/no_vfs_syscalls.c | 18 ++---- .../include/arch/sys_arch.h | 35 +---------- .../lwip/port/{esp32 => }/freertos/sys_arch.c | 38 +++--------- .../{esp32 => }/hooks/lwip_default_hooks.c | 0 .../port/{esp32 => }/hooks/tcp_isn_default.c | 6 +- components/lwip/port/include/arpa/inet.h | 12 ++++ .../lwip/port/include/debug/lwip_debug.h | 25 ++++++++ .../{esp32 => }/include/lwip_default_hooks.h | 0 .../lwip/port/{esp32 => }/include/lwipopts.h | 4 -- components/lwip/port/include/netdb.h | 29 +++++++++ .../{esp32 => }/include/netif/dhcp_state.h | 0 components/lwip/port/include/netinet/in.h | 14 +++++ components/lwip/port/include/netinet/tcp.h | 13 ++++ .../include/sntp/sntp_get_set_time.h | 18 ++---- .../port/{esp32 => }/include/sockets_ext.h | 2 +- components/lwip/port/linux/include/arch/cc.h | 61 +++++++++++++++++++ .../lwip/port/linux/include/arch/vfs_lwip.h | 8 +++ components/lwip/port/linux/include/net/if.h | 6 ++ .../lwip/port/linux/include/sys/fcntl.h | 12 ++++ .../lwip/port/linux/include/sys/socket.h | 13 ++++ .../lwip/port/{esp32 => }/sockets_ext.c | 2 +- .../lwip/test_afl_host/.build-test-rules.yml | 5 ++ components/lwip/test_afl_host/CMakeLists.txt | 1 + components/lwip/test_afl_host/Makefile | 16 ++++- components/lwip/test_afl_host/README.md | 4 +- components/lwip/test_afl_host/esp32_mock.c | 3 +- components/lwip/test_afl_host/esp32_mock.h | 35 ----------- components/lwip/test_afl_host/esp_attr.h | 8 --- components/lwip/test_afl_host/esp_task.h | 0 .../lwip/test_afl_host/sdkconfig.defaults | 2 + components/pthread/CMakeLists.txt | 10 +++ docs/en/api-reference/storage/vfs.rst | 2 +- docs/zh_CN/api-reference/storage/vfs.rst | 2 +- .../env_caps/linux/Kconfig.env_caps | 15 +++++ .../sockets/tcp_client/CMakeLists.txt | 2 +- tools/ci/check_copyright_config.yaml | 1 + tools/ci/check_copyright_ignore.txt | 17 ------ tools/ci/executable-list.txt | 1 + tools/mocks/startup/CMakeLists.txt | 3 + tools/mocks/startup/startup_mock.c | 35 +++++++++++ 60 files changed, 408 insertions(+), 440 deletions(-) create mode 100644 components/esp_netif/linux/stubs/include/machine/endian.h rename components/lwip/port/{esp32 => }/debug/lwip_debug.c (91%) delete mode 100644 components/lwip/port/esp32/include/arpa/inet.h delete mode 100644 components/lwip/port/esp32/include/debug/lwip_debug.h delete mode 100644 components/lwip/port/esp32/include/netdb.h delete mode 100644 components/lwip/port/esp32/include/netinet/in.h delete mode 100644 components/lwip/port/esp32/include/netinet/tcp.h rename components/lwip/port/{esp32 => freertos}/include/arch/sys_arch.h (61%) rename components/lwip/port/{esp32 => }/freertos/sys_arch.c (88%) rename components/lwip/port/{esp32 => }/hooks/lwip_default_hooks.c (100%) rename components/lwip/port/{esp32 => }/hooks/tcp_isn_default.c (98%) create mode 100644 components/lwip/port/include/arpa/inet.h create mode 100644 components/lwip/port/include/debug/lwip_debug.h rename components/lwip/port/{esp32 => }/include/lwip_default_hooks.h (100%) rename components/lwip/port/{esp32 => }/include/lwipopts.h (99%) create mode 100644 components/lwip/port/include/netdb.h rename components/lwip/port/{esp32 => }/include/netif/dhcp_state.h (100%) create mode 100644 components/lwip/port/include/netinet/in.h create mode 100644 components/lwip/port/include/netinet/tcp.h rename components/lwip/port/{esp32 => }/include/sntp/sntp_get_set_time.h (59%) rename components/lwip/port/{esp32 => }/include/sockets_ext.h (88%) create mode 100644 components/lwip/port/linux/include/arch/cc.h create mode 100644 components/lwip/port/linux/include/arch/vfs_lwip.h create mode 100644 components/lwip/port/linux/include/net/if.h create mode 100644 components/lwip/port/linux/include/sys/fcntl.h create mode 100644 components/lwip/port/linux/include/sys/socket.h rename components/lwip/port/{esp32 => }/sockets_ext.c (98%) create mode 100644 components/lwip/test_afl_host/.build-test-rules.yml delete mode 100644 components/lwip/test_afl_host/esp32_mock.h create mode 100644 components/lwip/test_afl_host/esp_task.h create mode 100644 examples/common_components/env_caps/linux/Kconfig.env_caps create mode 100644 tools/mocks/startup/CMakeLists.txt create mode 100644 tools/mocks/startup/startup_mock.c diff --git a/.gitlab/ci/host-test.yml b/.gitlab/ci/host-test.yml index 40b1ac0064..58eb7f2e3b 100644 --- a/.gitlab/ci/host-test.yml +++ b/.gitlab/ci/host-test.yml @@ -336,6 +336,24 @@ test_mqtt_on_host: - idf.py build - LSAN_OPTIONS=verbosity=1:log_threads=1 build/host_mqtt_client_test.elf +test_sockets_on_host: + extends: .host_test_template + script: + # test the tcp-client example with system sockets + - cd ${IDF_PATH}/examples/protocols/sockets/tcp_client + - echo 'CONFIG_EXAMPLE_IPV4_ADDR="127.0.0.1"' >> sdkconfig.defaults + - idf.py --preview set-target linux + - idf.py build + - timeout 5 ./build/tcp_client.elf >test.log || true + - grep "Socket unable to connect" test.log + # test the udp-client example with lwip sockets + - cd ${IDF_PATH}/examples/protocols/sockets/udp_client + - echo 'CONFIG_EXAMPLE_IPV4_ADDR="127.0.0.1"' >> sdkconfig.defaults + - idf.py --preview set-target linux + - idf.py build + - timeout 5 ./build/udp_client.elf >test.log || true + - grep "Message sent" test.log + test_eh_frame_parser: extends: .host_test_template script: diff --git a/components/esp_netif/CMakeLists.txt b/components/esp_netif/CMakeLists.txt index b0b678db5c..091d07daa7 100644 --- a/components/esp_netif/CMakeLists.txt +++ b/components/esp_netif/CMakeLists.txt @@ -1,11 +1,5 @@ idf_build_get_property(target IDF_TARGET) -if(${target} STREQUAL "linux") - # Header only library for linux - idf_component_register(INCLUDE_DIRS include) - return() -endif() - set(srcs_lwip "lwip/esp_netif_lwip.c" "lwip/esp_netif_sntp.c" @@ -23,6 +17,12 @@ set(srcs set(include_dirs "include") set(priv_include_dirs "private_include") +idf_build_get_property(target IDF_TARGET) +if(${target} STREQUAL "linux") + list(APPEND include_dirs + "linux/stubs/include") +endif() + if(CONFIG_PPP_SUPPORT) list(APPEND srcs_lwip lwip/esp_netif_lwip_ppp.c) endif() diff --git a/components/esp_netif/linux/stubs/include/machine/endian.h b/components/esp_netif/linux/stubs/include/machine/endian.h new file mode 100644 index 0000000000..120152bdab --- /dev/null +++ b/components/esp_netif/linux/stubs/include/machine/endian.h @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include_next diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 54d14733ae..38426c7123 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -16,6 +16,7 @@ #include "esp_netif.h" #include "esp_netif_private.h" #include "esp_random.h" +#include "esp_system.h" #include "lwip/tcpip.h" #include "lwip/dhcp.h" diff --git a/components/esp_netif/lwip/esp_netif_lwip_defaults.c b/components/esp_netif/lwip/esp_netif_lwip_defaults.c index 97009d3d57..444ca0fee9 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_defaults.c +++ b/components/esp_netif/lwip/esp_netif_lwip_defaults.c @@ -10,6 +10,7 @@ #if defined(CONFIG_PPP_SUPPORT) #include "esp_netif_lwip_ppp.h" #endif +#if defined(CONFIG_ESP_NETIF_TCPIP_LWIP) #if CONFIG_ESP_NETIF_BRIDGE_EN #include "netif/bridgeif.h" @@ -64,3 +65,5 @@ const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_ppp = &s_n const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_eth = &s_eth_netif_config; const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_sta = &s_wifi_netif_config_sta; const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_ap = &s_wifi_netif_config_ap; + +#endif /*CONFIG_ESP_NETIF_TCPIP_LWIP*/ diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index 170e59478c..eafc963be5 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -1,10 +1,18 @@ +idf_build_get_property(target IDF_TARGET) +if(NOT ${target} STREQUAL "linux") + # ESP platform targets share the same port folder + set(target esp32) +endif() + set(include_dirs include include/apps include/apps/sntp lwip/src/include - port/esp32/include - port/esp32/include/arch + port/include + port/freertos/include/ + port/${target}/include + port/${target}/include/arch ) set(srcs @@ -85,11 +93,11 @@ set(srcs "lwip/src/netif/ppp/upap.c" "lwip/src/netif/ppp/utils.c" "lwip/src/netif/ppp/vj.c" - "port/esp32/hooks/tcp_isn_default.c" - "port/esp32/hooks/lwip_default_hooks.c" - "port/esp32/debug/lwip_debug.c" - "port/esp32/freertos/sys_arch.c" - "port/esp32/sockets_ext.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") if(CONFIG_LWIP_PPP_SUPPORT) list(APPEND srcs @@ -125,10 +133,15 @@ if(CONFIG_LWIP_PPP_SUPPORT) "lwip/src/netif/ppp/polarssl/sha1.c") endif() -if(CONFIG_VFS_SUPPORT_IO) - list(APPEND srcs "port/esp32/vfs_lwip.c") -else() - list(APPEND srcs "port/esp32/no_vfs_syscalls.c") +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() endif() if(CONFIG_LWIP_ICMP) @@ -147,9 +160,9 @@ if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) endif() idf_component_register(SRCS "${srcs}" - INCLUDE_DIRS "${include_dirs}" - LDFRAGMENTS linker.lf - PRIV_REQUIRES vfs) + INCLUDE_DIRS ${include_dirs} + 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) @@ -188,3 +201,9 @@ if(CONFIG_LWIP_DHCP_RESTORE_LAST_IP) endif() target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") + +if(${target} STREQUAL "linux") + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(${COMPONENT_LIB} PRIVATE Threads::Threads) +endif() diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index b839f553eb..1475b49c1a 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -3,9 +3,9 @@ * * SPDX-License-Identifier: Apache-2.0 */ -//#include "esp_common.h" #include #include +#include #include "lwip/dhcp.h" #include "lwip/err.h" #include "lwip/pbuf.h" diff --git a/components/lwip/apps/ping/ping.c b/components/lwip/apps/ping/ping.c index 8f954d5a7a..21c39ac504 100644 --- a/components/lwip/apps/ping/ping.c +++ b/components/lwip/apps/ping/ping.c @@ -55,6 +55,7 @@ #if PING_USE_SOCKETS #include "lwip/sockets.h" #include "lwip/inet.h" +#include "esp_task.h" #include "ping/ping_sock.h" #endif /* PING_USE_SOCKETS */ diff --git a/components/lwip/apps/ping/ping_sock.c b/components/lwip/apps/ping/ping_sock.c index afb0308e62..b85692781c 100644 --- a/components/lwip/apps/ping/ping_sock.c +++ b/components/lwip/apps/ping/ping_sock.c @@ -6,6 +6,7 @@ #include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lwip/opt.h" @@ -231,7 +232,7 @@ esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_c /* set ICMP type and code field */ ep->packet_hdr->code = 0; /* ping id should be unique, treat task handle as ping ID */ - ep->packet_hdr->id = ((uint32_t)ep->ping_task_hdl) & 0xFFFF; + ep->packet_hdr->id = ((intptr_t)ep->ping_task_hdl) & 0xFFFF; /* fill the additional data buffer with some data */ char *d = (char *)(ep->packet_hdr) + sizeof(struct icmp_echo_hdr); for (uint32_t i = 0; i < config->data_size; i++) { diff --git a/components/lwip/apps/sntp/sntp.c b/components/lwip/apps/sntp/sntp.c index f959e9ba54..f4a5c9d9fd 100644 --- a/components/lwip/apps/sntp/sntp.c +++ b/components/lwip/apps/sntp/sntp.c @@ -15,6 +15,7 @@ #undef SNTP_OPMODE_POLL #include "lwip/apps/sntp.h" #include "lwip/tcpip.h" +#include "esp_macros.h" static const char *TAG = "sntp"; diff --git a/components/lwip/port/esp32/debug/lwip_debug.c b/components/lwip/port/debug/lwip_debug.c similarity index 91% rename from components/lwip/port/esp32/debug/lwip_debug.c rename to components/lwip/port/debug/lwip_debug.c index d611bfc9bd..d9be733c54 100644 --- a/components/lwip/port/esp32/debug/lwip_debug.c +++ b/components/lwip/port/debug/lwip_debug.c @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// 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. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "debug/lwip_debug.h" #include "lwip/api.h" diff --git a/components/lwip/port/esp32/include/arch/cc.h b/components/lwip/port/esp32/include/arch/cc.h index 7adf085643..dac407a194 100644 --- a/components/lwip/port/esp32/include/arch/cc.h +++ b/components/lwip/port/esp32/include/arch/cc.h @@ -1,35 +1,9 @@ /* - * Copyright (c) 2001, Swedish Institute of Computer Science. - * All rights reserved. + * SPDX-FileCopyrightText: 2001 Swedish Institute of Computer Science * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels + * SPDX-License-Identifier: BSD-3-Clause * + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD */ #ifndef __ARCH_CC_H__ #define __ARCH_CC_H__ diff --git a/components/lwip/port/esp32/include/arch/perf.h b/components/lwip/port/esp32/include/arch/perf.h index 4b9687be2c..fc6262342f 100644 --- a/components/lwip/port/esp32/include/arch/perf.h +++ b/components/lwip/port/esp32/include/arch/perf.h @@ -1,35 +1,7 @@ /* - * Copyright (c) 2001, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels + * SPDX-FileCopyrightText: 2001 Swedish Institute of Computer Science * + * SPDX-License-Identifier: BSD-3-Clause */ #ifndef __PERF_H__ #define __PERF_H__ diff --git a/components/lwip/port/esp32/include/arch/vfs_lwip.h b/components/lwip/port/esp32/include/arch/vfs_lwip.h index 8a037c6ff2..f86716c879 100644 --- a/components/lwip/port/esp32/include/arch/vfs_lwip.h +++ b/components/lwip/port/esp32/include/arch/vfs_lwip.h @@ -1,16 +1,8 @@ -// Copyright 2017 Espressif Systems (Shanghai) PTE LTD -// -// 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. +/* + * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifdef __cplusplus extern "C" { diff --git a/components/lwip/port/esp32/include/arpa/inet.h b/components/lwip/port/esp32/include/arpa/inet.h deleted file mode 100644 index 94c6c17ed5..0000000000 --- a/components/lwip/port/esp32/include/arpa/inet.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// 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. - -#ifndef INET_H_ -#define INET_H_ - -#include "lwip/inet.h" - -#endif /* INET_H_ */ diff --git a/components/lwip/port/esp32/include/debug/lwip_debug.h b/components/lwip/port/esp32/include/debug/lwip_debug.h deleted file mode 100644 index 9f349db69b..0000000000 --- a/components/lwip/port/esp32/include/debug/lwip_debug.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// 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. - - -#ifndef _LWIP_DEBUG_H -#define _LWIP_DEBUG_H - -#ifdef __cplusplus -extern "C" { -#endif - -void dbg_lwip_tcp_pcb_show(void); -void dbg_lwip_udp_pcb_show(void); -void dbg_lwip_tcp_rxtx_show(void); -void dbg_lwip_udp_rxtx_show(void); -void dbg_lwip_mem_cnt_show(void); - -#ifdef __cplusplus -} -#endif - -#endif // _LWIP_DEBUG_H diff --git a/components/lwip/port/esp32/include/netdb.h b/components/lwip/port/esp32/include/netdb.h deleted file mode 100644 index 3054fdca26..0000000000 --- a/components/lwip/port/esp32/include/netdb.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @file - * This file is a posix wrapper for lwip/netdb.h. - */ - -/* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - */ - -#include "lwip/netdb.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef ESP_PLATFORM -int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, - char *host, socklen_t hostlen, - char *serv, socklen_t servlen, int flags); - -#endif - -#ifdef __cplusplus -} -#endif diff --git a/components/lwip/port/esp32/include/netinet/in.h b/components/lwip/port/esp32/include/netinet/in.h deleted file mode 100644 index 7eaec63342..0000000000 --- a/components/lwip/port/esp32/include/netinet/in.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// 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. - -#ifndef IN_H_ -#define IN_H_ - -#include "lwip/inet.h" - -#define IN6_IS_ADDR_MULTICAST(a) IN_MULTICAST(a) - -#endif /* IN_H_ */ diff --git a/components/lwip/port/esp32/include/netinet/tcp.h b/components/lwip/port/esp32/include/netinet/tcp.h deleted file mode 100644 index a4d4d97699..0000000000 --- a/components/lwip/port/esp32/include/netinet/tcp.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// Copyright 2020 Francesco Giancane -// -// 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. - -#ifndef _NETINET_TCP_H -#define _NETINET_TCP_H - -#include "lwip/tcp.h" - -#endif /* _NETINET_TCP_H */ diff --git a/components/lwip/port/esp32/include/sys/socket.h b/components/lwip/port/esp32/include/sys/socket.h index 5ea4db4ab8..45a6d018d5 100644 --- a/components/lwip/port/esp32/include/sys/socket.h +++ b/components/lwip/port/esp32/include/sys/socket.h @@ -2,33 +2,15 @@ * @file * This file is a posix wrapper for lwip/sockets.h. */ - /* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * SPDX-FileCopyrightText: 2001-2004 Swedish Institute of Computer Science * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. + * SPDX-License-Identifier: BSD-3-Clause * + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD */ +#ifndef LWIP_HDR_SYS_SOCKETS_H +#define LWIP_HDR_SYS_SOCKETS_H #include "lwip/sockets.h" /* @@ -36,3 +18,5 @@ while for ESP32 port is defined in net/if.h */ #include + +#endif /* LWIP_HDR_SYS_SOCKETS_H */ diff --git a/components/lwip/port/esp32/no_vfs_syscalls.c b/components/lwip/port/esp32/no_vfs_syscalls.c index 668499c4b2..69fa1576fb 100644 --- a/components/lwip/port/esp32/no_vfs_syscalls.c +++ b/components/lwip/port/esp32/no_vfs_syscalls.c @@ -1,16 +1,8 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// 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. +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include diff --git a/components/lwip/port/esp32/include/arch/sys_arch.h b/components/lwip/port/freertos/include/arch/sys_arch.h similarity index 61% rename from components/lwip/port/esp32/include/arch/sys_arch.h rename to components/lwip/port/freertos/include/arch/sys_arch.h index 1cec1e01b7..ba85471397 100644 --- a/components/lwip/port/esp32/include/arch/sys_arch.h +++ b/components/lwip/port/freertos/include/arch/sys_arch.h @@ -1,46 +1,17 @@ /* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. + * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels + * SPDX-License-Identifier: BSD-3-Clause * + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD */ - #ifndef __SYS_ARCH_H__ #define __SYS_ARCH_H__ -#ifdef __linux__ -#include "esp32_mock.h" -#else #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "freertos/semphr.h" -#endif // __linux__ #ifdef __cplusplus extern "C" { diff --git a/components/lwip/port/esp32/freertos/sys_arch.c b/components/lwip/port/freertos/sys_arch.c similarity index 88% rename from components/lwip/port/esp32/freertos/sys_arch.c rename to components/lwip/port/freertos/sys_arch.c index fa9c1ef0fa..91291f0dc8 100644 --- a/components/lwip/port/esp32/freertos/sys_arch.c +++ b/components/lwip/port/freertos/sys_arch.c @@ -1,38 +1,18 @@ /* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. + * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels + * SPDX-License-Identifier: BSD-3-Clause * + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD */ /* lwIP includes. */ #include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" #include "lwip/debug.h" #include "lwip/def.h" #include "lwip/sys.h" @@ -40,12 +20,8 @@ #include "lwip/stats.h" #include "arch/sys_arch.h" #include "arch/vfs_lwip.h" -#ifdef __linux__ -#include "esp32_mock.h" -#else // __linux__ #include "esp_log.h" #include "esp_compiler.h" -#endif // __linux__ static const char* TAG = "lwip_arch"; diff --git a/components/lwip/port/esp32/hooks/lwip_default_hooks.c b/components/lwip/port/hooks/lwip_default_hooks.c similarity index 100% rename from components/lwip/port/esp32/hooks/lwip_default_hooks.c rename to components/lwip/port/hooks/lwip_default_hooks.c diff --git a/components/lwip/port/esp32/hooks/tcp_isn_default.c b/components/lwip/port/hooks/tcp_isn_default.c similarity index 98% rename from components/lwip/port/esp32/hooks/tcp_isn_default.c rename to components/lwip/port/hooks/tcp_isn_default.c index 14a2f5681e..32e0dcd7ad 100644 --- a/components/lwip/port/esp32/hooks/tcp_isn_default.c +++ b/components/lwip/port/hooks/tcp_isn_default.c @@ -1,3 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ /** * @file * @@ -75,7 +80,6 @@ #include "lwip/sys.h" #include #include "esp_rom_md5.h" -#include "esp_memory_utils.h" #ifdef CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT diff --git a/components/lwip/port/include/arpa/inet.h b/components/lwip/port/include/arpa/inet.h new file mode 100644 index 0000000000..751bf44ed9 --- /dev/null +++ b/components/lwip/port/include/arpa/inet.h @@ -0,0 +1,12 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef INET_H_ +#define INET_H_ + +#include "lwip/inet.h" + +#endif /* INET_H_ */ diff --git a/components/lwip/port/include/debug/lwip_debug.h b/components/lwip/port/include/debug/lwip_debug.h new file mode 100644 index 0000000000..f39d608c1a --- /dev/null +++ b/components/lwip/port/include/debug/lwip_debug.h @@ -0,0 +1,25 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#ifndef _LWIP_DEBUG_H +#define _LWIP_DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif + +void dbg_lwip_tcp_pcb_show(void); +void dbg_lwip_udp_pcb_show(void); +void dbg_lwip_tcp_rxtx_show(void); +void dbg_lwip_udp_rxtx_show(void); +void dbg_lwip_mem_cnt_show(void); + +#ifdef __cplusplus +} +#endif + +#endif // _LWIP_DEBUG_H diff --git a/components/lwip/port/esp32/include/lwip_default_hooks.h b/components/lwip/port/include/lwip_default_hooks.h similarity index 100% rename from components/lwip/port/esp32/include/lwip_default_hooks.h rename to components/lwip/port/include/lwip_default_hooks.h diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/include/lwipopts.h similarity index 99% rename from components/lwip/port/esp32/include/lwipopts.h rename to components/lwip/port/include/lwipopts.h index b1e4d0ed06..481951ff70 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/include/lwipopts.h @@ -17,12 +17,8 @@ #include #include #include -#ifdef __linux__ -#include "esp32_mock.h" -#else #include "esp_task.h" #include "esp_random.h" -#endif // __linux__ #include "sdkconfig.h" #include "sntp/sntp_get_set_time.h" #include "sockets_ext.h" diff --git a/components/lwip/port/include/netdb.h b/components/lwip/port/include/netdb.h new file mode 100644 index 0000000000..c8cb26389c --- /dev/null +++ b/components/lwip/port/include/netdb.h @@ -0,0 +1,29 @@ +/** + * @file + * This file is a posix wrapper for lwip/netdb.h. + */ + +/* + * SPDX-FileCopyrightText: 2001-2004 Swedish Institute of Computer Science + * + * SPDX-License-Identifier: BSD-3-Clause + * + * SPDX-FileContributor: 2018-2022 Espressif Systems (Shanghai) CO LTD + */ + +#include "lwip/netdb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef ESP_PLATFORM +int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, + char *host, socklen_t hostlen, + char *serv, socklen_t servlen, int flags); + +#endif + +#ifdef __cplusplus +} +#endif diff --git a/components/lwip/port/esp32/include/netif/dhcp_state.h b/components/lwip/port/include/netif/dhcp_state.h similarity index 100% rename from components/lwip/port/esp32/include/netif/dhcp_state.h rename to components/lwip/port/include/netif/dhcp_state.h diff --git a/components/lwip/port/include/netinet/in.h b/components/lwip/port/include/netinet/in.h new file mode 100644 index 0000000000..e8f01bf918 --- /dev/null +++ b/components/lwip/port/include/netinet/in.h @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef IN_H_ +#define IN_H_ + +#include "lwip/inet.h" + +#define IN6_IS_ADDR_MULTICAST(a) IN_MULTICAST(a) + +#endif /* IN_H_ */ diff --git a/components/lwip/port/include/netinet/tcp.h b/components/lwip/port/include/netinet/tcp.h new file mode 100644 index 0000000000..4f73a7d3fb --- /dev/null +++ b/components/lwip/port/include/netinet/tcp.h @@ -0,0 +1,13 @@ +/* + * SPDX-FileCopyrightText: 2015-2016 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020 Francesco Giancane + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _NETINET_TCP_H +#define _NETINET_TCP_H + +#include "lwip/tcp.h" + +#endif /* _NETINET_TCP_H */ diff --git a/components/lwip/port/esp32/include/sntp/sntp_get_set_time.h b/components/lwip/port/include/sntp/sntp_get_set_time.h similarity index 59% rename from components/lwip/port/esp32/include/sntp/sntp_get_set_time.h rename to components/lwip/port/include/sntp/sntp_get_set_time.h index 1991620804..987a280d78 100644 --- a/components/lwip/port/esp32/include/sntp/sntp_get_set_time.h +++ b/components/lwip/port/include/sntp/sntp_get_set_time.h @@ -1,16 +1,8 @@ -// Copyright 2021 Espressif Systems (Shanghai) PTE LTD -// -// 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. +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef __SNTP_GET_SET_TIME_H__ #define __SNTP_GET_SET_TIME_H__ diff --git a/components/lwip/port/esp32/include/sockets_ext.h b/components/lwip/port/include/sockets_ext.h similarity index 88% rename from components/lwip/port/esp32/include/sockets_ext.h rename to components/lwip/port/include/sockets_ext.h index 48ffcf61e1..2d575c6fa5 100644 --- a/components/lwip/port/esp32/include/sockets_ext.h +++ b/components/lwip/port/include/sockets_ext.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/lwip/port/linux/include/arch/cc.h b/components/lwip/port/linux/include/arch/cc.h new file mode 100644 index 0000000000..07d7b73c42 --- /dev/null +++ b/components/lwip/port/linux/include/arch/cc.h @@ -0,0 +1,61 @@ +/* + * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science + * + * SPDX-License-Identifier: BSD-3-Clause + * + * SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD + */ +#ifndef LWIP_ARCH_CC_H +#define LWIP_ARCH_CC_H + +/* see https://sourceforge.net/p/predef/wiki/OperatingSystems/ */ +#if defined __ANDROID__ +#define LWIP_UNIX_ANDROID +#elif defined __linux__ +#define LWIP_UNIX_LINUX +#elif defined __APPLE__ +#define LWIP_UNIX_MACH +#elif defined __OpenBSD__ +#define LWIP_UNIX_OPENBSD +#elif defined __CYGWIN__ +#define LWIP_UNIX_CYGWIN +#elif defined __GNU__ +#define LWIP_UNIX_HURD +#endif + +#define LWIP_TIMEVAL_PRIVATE 0 +#include +#include "esp_linux_helper.h" + +#define LWIP_ERRNO_INCLUDE + +#if defined(LWIP_UNIX_LINUX) || defined(LWIP_UNIX_HURD) +#define LWIP_ERRNO_STDINCLUDE 1 +#endif + +/* different handling for unit test, normally not needed */ +#ifdef LWIP_NOASSERT_ON_ERROR +#define LWIP_ERROR(message, expression, handler) do { if (!(expression)) { \ + handler;}} while(0) +#endif + +#if defined(LWIP_UNIX_ANDROID) && defined(FD_SET) +typedef __kernel_fd_set fd_set; +#endif + +#if defined(LWIP_UNIX_MACH) +/* sys/types.h and signal.h bring in Darwin byte order macros. pull the + header here and disable LwIP's version so that apps still can get + the macros via LwIP headers and use system headers */ +#include +#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS +#endif + +struct sio_status_s; +typedef struct sio_status_s sio_status_t; +#define sio_fd_t sio_status_t* +#define __sio_fd_t_defined + +typedef unsigned int sys_prot_t; + +#endif /* LWIP_ARCH_CC_H */ diff --git a/components/lwip/port/linux/include/arch/vfs_lwip.h b/components/lwip/port/linux/include/arch/vfs_lwip.h new file mode 100644 index 0000000000..79de75600f --- /dev/null +++ b/components/lwip/port/linux/include/arch/vfs_lwip.h @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +static inline void esp_vfs_lwip_sockets_register(void) {} diff --git a/components/lwip/port/linux/include/net/if.h b/components/lwip/port/linux/include/net/if.h new file mode 100644 index 0000000000..e3c24eb941 --- /dev/null +++ b/components/lwip/port/linux/include/net/if.h @@ -0,0 +1,6 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once diff --git a/components/lwip/port/linux/include/sys/fcntl.h b/components/lwip/port/linux/include/sys/fcntl.h new file mode 100644 index 0000000000..1b3a261fa8 --- /dev/null +++ b/components/lwip/port/linux/include/sys/fcntl.h @@ -0,0 +1,12 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#ifdef LWIP_HDR_ESP_LWIPOPTS_H +// ignore when included from lwipopts.h since lwip provides all necessary definitions +#else +// otherwise include system fcntl +#include_next +#endif diff --git a/components/lwip/port/linux/include/sys/socket.h b/components/lwip/port/linux/include/sys/socket.h new file mode 100644 index 0000000000..b2e6a03e09 --- /dev/null +++ b/components/lwip/port/linux/include/sys/socket.h @@ -0,0 +1,13 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef LWIP_HDR_LINUX_SYS_SOCKETS_H +#define LWIP_HDR_LINUX_SYS_SOCKETS_H +/* Include lwip sockets by default */ +#include "lwip/sockets.h" +#else +/* Otherwise use system sockets if LWIP_HDR_LINUX_SYS_SOCKETS_H already defined */ +#include_next +#endif /* LWIP_HDR_LINUX_SYS_SOCKETS_H */ diff --git a/components/lwip/port/esp32/sockets_ext.c b/components/lwip/port/sockets_ext.c similarity index 98% rename from components/lwip/port/esp32/sockets_ext.c rename to components/lwip/port/sockets_ext.c index fb1454b21b..22e63cdd78 100644 --- a/components/lwip/port/esp32/sockets_ext.c +++ b/components/lwip/port/sockets_ext.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/lwip/test_afl_host/.build-test-rules.yml b/components/lwip/test_afl_host/.build-test-rules.yml new file mode 100644 index 0000000000..e27405999e --- /dev/null +++ b/components/lwip/test_afl_host/.build-test-rules.yml @@ -0,0 +1,5 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/lwip/test_afl_host: + enable: + - if: IDF_TARGET == "linux" diff --git a/components/lwip/test_afl_host/CMakeLists.txt b/components/lwip/test_afl_host/CMakeLists.txt index 5891b6162b..f52de94b90 100644 --- a/components/lwip/test_afl_host/CMakeLists.txt +++ b/components/lwip/test_afl_host/CMakeLists.txt @@ -2,6 +2,7 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) +set(COMPONENTS lwip) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(fuzz_test_lwip) diff --git a/components/lwip/test_afl_host/Makefile b/components/lwip/test_afl_host/Makefile index 3dfac00899..e8fcda9877 100644 --- a/components/lwip/test_afl_host/Makefile +++ b/components/lwip/test_afl_host/Makefile @@ -1,4 +1,7 @@ LWIP_COMPONENT_DIR=../ +FREERTOS_COMPONENT_DIR=$(LWIP_COMPONENT_DIR)/../freertos +COMPONENT_DIR=$(LWIP_COMPONENT_DIR)/../ +MOCKS_DIR=$(LWIP_COMPONENT_DIR)/../../tools/mocks CFLAGS=-D IDF_VER=\"v3.1\" \ -DESP_PLATFORM \ @@ -37,8 +40,14 @@ INC_DIRS=-I . \ -I $(LWIP_COMPONENT_DIR)/lwip/src/include \ -I $(LWIP_COMPONENT_DIR)/lwip/src/include/netif \ -I $(LWIP_COMPONENT_DIR)/lwip/src/include/posix \ - -I $(LWIP_COMPONENT_DIR)/lwip/src/include/posix \ - -I $(LWIP_COMPONENT_DIR)/port/esp32/include + -I $(LWIP_COMPONENT_DIR)/port/include \ + -I $(LWIP_COMPONENT_DIR)/port/linux/include \ + -I $(LWIP_COMPONENT_DIR)/port/freertos/include \ + -I $(FREERTOS_COMPONENT_DIR)/FreeRTOS-Kernel/include \ + -I $(FREERTOS_COMPONENT_DIR)/esp_additions/include/freertos \ + -I $(FREERTOS_COMPONENT_DIR)/FreeRTOS-Kernel/portable/linux/include \ + -I $(COMPONENT_DIR)/esp_hw_support/include \ + -I $(COMPONENT_DIR)/linux/include TEST_NAME=test FUZZ=afl-fuzz @@ -95,7 +104,8 @@ dhcpserver.o: ../apps/dhcpserver/dhcpserver.c $(GEN_CFG) .PHONY: $(GEN_CFG) $(GEN_CFG): - $(IDF_PATH)/tools/idf.py reconfigure +# Run reconfiguration without potential AFL in PATHs + PATH=$(subst $(AFL_PATH):,/:,$(PATH)) idf.py reconfigure $(TEST_NAME): $(OBJECTS) @echo "[LD] $@" diff --git a/components/lwip/test_afl_host/README.md b/components/lwip/test_afl_host/README.md index 953be4341f..81ec59e3aa 100644 --- a/components/lwip/test_afl_host/README.md +++ b/components/lwip/test_afl_host/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | Linux | +| ----------------- | ----- | ## Introduction This test uses [american fuzzy lop](http://lcamtuf.coredump.cx/afl/) to mangle real dns, dhcp client, dhcp server packets and look for exceptions caused by the parser. diff --git a/components/lwip/test_afl_host/esp32_mock.c b/components/lwip/test_afl_host/esp32_mock.c index 960da97462..7f24fe03e8 100644 --- a/components/lwip/test_afl_host/esp32_mock.c +++ b/components/lwip/test_afl_host/esp32_mock.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -17,7 +17,6 @@ #include "lwip/timeouts.h" #include "lwip/udp.h" #include "lwip/timeouts.h" -#include "esp32_mock.h" #include "no_warn_host.h" #define ESP_OK 0 diff --git a/components/lwip/test_afl_host/esp32_mock.h b/components/lwip/test_afl_host/esp32_mock.h deleted file mode 100644 index d384acd013..0000000000 --- a/components/lwip/test_afl_host/esp32_mock.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef _ESP32_MOCK_H_ -#define _ESP32_MOCK_H_ - -#include -#include - -/* ------------------------------------------------- ESP32 Port Mock --------------------------------------------------- - * - * ------------------------------------------------------------------------------------------------------------------ */ - -// --------------------- lwipopts.h ------------------------ - -#define ESP_TASK_TCPIP_STACK -#define ESP_TASK_TCPIP_PRIO - -uint32_t esp_random(void); - -// --------------------- sys_arch.h ------------------------ - -// Required to get linux assert.h to work ??? -#define __ASSERT_FUNC __ASSERT_FUNCTION - -typedef void * SemaphoreHandle_t; -typedef void * TaskHandle_t; -typedef void * QueueHandle_t; - -#define vTaskDelay(ms) usleep((m)*0) - -#endif // _ESP32_MOCK_H_ diff --git a/components/lwip/test_afl_host/esp_attr.h b/components/lwip/test_afl_host/esp_attr.h index 3044f875be..40794b5655 100644 --- a/components/lwip/test_afl_host/esp_attr.h +++ b/components/lwip/test_afl_host/esp_attr.h @@ -1,10 +1,2 @@ #pragma once -#define _ESP_NETIF_SUPPRESS_LEGACY_WARNING_ -#define __ARCH_CC_H__ -#define __XTENSA_API_H__ #define IRAM_ATTR -#define FLAG_ATTR(TYPE) -#define SSIZE_MAX INT_MAX -#undef assert -#define assert(x) -#define sys_prot_t int diff --git a/components/lwip/test_afl_host/esp_task.h b/components/lwip/test_afl_host/esp_task.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/lwip/test_afl_host/sdkconfig.defaults b/components/lwip/test_afl_host/sdkconfig.defaults index ee235abe9f..2b408fc611 100644 --- a/components/lwip/test_afl_host/sdkconfig.defaults +++ b/components/lwip/test_afl_host/sdkconfig.defaults @@ -2,3 +2,5 @@ CONFIG_LWIP_TCPIP_CORE_LOCKING=n CONFIG_LWIP_CHECK_THREAD_SAFETY=n CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=n CONFIG_FREERTOS_SMP=n +CONFIG_IDF_TARGET="linux" +CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n diff --git a/components/pthread/CMakeLists.txt b/components/pthread/CMakeLists.txt index fb66f7ca5a..1b1d5f2a8a 100644 --- a/components/pthread/CMakeLists.txt +++ b/components/pthread/CMakeLists.txt @@ -1,3 +1,13 @@ +idf_build_get_property(target IDF_TARGET) +if(${target} STREQUAL "linux") + # Make pthread component an empty interface lib referencing host pthread for Linux target + idf_component_register() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(${COMPONENT_LIB} INTERFACE Threads::Threads) + return() +endif() + set(sources "pthread.c" "pthread_cond_var.c" "pthread_local_storage.c" diff --git a/docs/en/api-reference/storage/vfs.rst b/docs/en/api-reference/storage/vfs.rst index 8a963f0cde..8b01a1723d 100644 --- a/docs/en/api-reference/storage/vfs.rst +++ b/docs/en/api-reference/storage/vfs.rst @@ -137,7 +137,7 @@ A socket VFS driver needs to be registered with the following functions defined: :cpp:func:`stop_socket_select_isr` has the same functionality as :cpp:func:`stop_socket_select` but it can be used from ISR. -Please see :component_file:`lwip/port/esp32/vfs_lwip.c` for a reference socket driver implementation using LWIP. +Please see :component_file:`lwip/port/esp32xx/vfs_lwip.c` for a reference socket driver implementation using LWIP. .. note:: If you use :cpp:func:`select` for socket file descriptors only then you can disable the :ref:`CONFIG_VFS_SUPPORT_SELECT` option to reduce the code size and improve performance. diff --git a/docs/zh_CN/api-reference/storage/vfs.rst b/docs/zh_CN/api-reference/storage/vfs.rst index 86b9045513..b59a3fdfa3 100644 --- a/docs/zh_CN/api-reference/storage/vfs.rst +++ b/docs/zh_CN/api-reference/storage/vfs.rst @@ -137,7 +137,7 @@ VFS 组件支持通过 :cpp:func:`select` 进行同步输入/输出多路复用 :cpp:func:`stop_socket_select_isr` 与 :cpp:func:`stop_socket_select` 的作用相似,但是前者可在 ISR 中使用。 -请参考 :component_file:`lwip/port/esp32/vfs_lwip.c` 以了解使用 LWIP 的套接字驱动参考实现。 +请参考 :component_file:`lwip/port/esp32xx/vfs_lwip.c` 以了解使用 LWIP 的套接字驱动参考实现。 .. note:: 如果 :cpp:func:`select` 用于套接字文件描述符,您可以禁用 :ref:`CONFIG_VFS_SUPPORT_SELECT` 选项来减少代码量,提高性能。 diff --git a/examples/common_components/env_caps/linux/Kconfig.env_caps b/examples/common_components/env_caps/linux/Kconfig.env_caps new file mode 100644 index 0000000000..b2b5a139be --- /dev/null +++ b/examples/common_components/env_caps/linux/Kconfig.env_caps @@ -0,0 +1,15 @@ +config ENV_GPIO_RANGE_MIN + int + default 0 + +config ENV_GPIO_RANGE_MAX + int + default 0 + +config ENV_GPIO_IN_RANGE_MAX + int + default ENV_GPIO_RANGE_MAX + +config ENV_GPIO_OUT_RANGE_MAX + int + default ENV_GPIO_RANGE_MAX diff --git a/examples/protocols/sockets/tcp_client/CMakeLists.txt b/examples/protocols/sockets/tcp_client/CMakeLists.txt index 52a0c94544..0fd7e2e047 100644 --- a/examples/protocols/sockets/tcp_client/CMakeLists.txt +++ b/examples/protocols/sockets/tcp_client/CMakeLists.txt @@ -2,7 +2,7 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -if(${IDF_TARGET} STREQUAL "linux") +if("${IDF_TARGET}" STREQUAL "linux") set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/" "$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs") set(COMPONENTS main) diff --git a/tools/ci/check_copyright_config.yaml b/tools/ci/check_copyright_config.yaml index ebb96be819..947efe9651 100644 --- a/tools/ci/check_copyright_config.yaml +++ b/tools/ci/check_copyright_config.yaml @@ -64,6 +64,7 @@ lwip_component: include: - 'components/lwip/**' - 'components/esp_netif/lwip/**' + - 'examples/common_components/protocol_examples_tapif_io/lwip' allowed_licenses: - Apache-2.0 - BSD-3-Clause diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 464764cb3f..0eba6d561c 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -711,23 +711,6 @@ components/lwip/apps/ping/ping.c components/lwip/include/apps/dhcpserver/dhcpserver_options.h components/lwip/include/apps/esp_ping.h components/lwip/include/apps/ping/ping.h -components/lwip/port/esp32/debug/lwip_debug.c -components/lwip/port/esp32/freertos/sys_arch.c -components/lwip/port/esp32/hooks/tcp_isn_default.c -components/lwip/port/esp32/include/arch/cc.h -components/lwip/port/esp32/include/arch/perf.h -components/lwip/port/esp32/include/arch/sys_arch.h -components/lwip/port/esp32/include/arch/vfs_lwip.h -components/lwip/port/esp32/include/arpa/inet.h -components/lwip/port/esp32/include/debug/lwip_debug.h -components/lwip/port/esp32/include/netdb.h -components/lwip/port/esp32/include/netif/ethernetif.h -components/lwip/port/esp32/include/netif/openthreadif.h -components/lwip/port/esp32/include/netinet/in.h -components/lwip/port/esp32/include/netinet/tcp.h -components/lwip/port/esp32/include/sntp/sntp_get_set_time.h -components/lwip/port/esp32/include/sys/socket.h -components/lwip/port/esp32/no_vfs_syscalls.c components/lwip/test_afl_host/dhcp_di.h components/lwip/test_afl_host/dhcpserver_di.h components/lwip/test_afl_host/dns_di.h diff --git a/tools/ci/executable-list.txt b/tools/ci/executable-list.txt index 4d9779c679..ff243481d6 100644 --- a/tools/ci/executable-list.txt +++ b/tools/ci/executable-list.txt @@ -41,6 +41,7 @@ examples/build_system/cmake/idf_as_lib/run-esp32h4.sh examples/build_system/cmake/idf_as_lib/run-esp32s2.sh examples/build_system/cmake/idf_as_lib/run-esp32s3.sh examples/build_system/cmake/idf_as_lib/run.sh +examples/common_components/protocol_examples_tapif_io/make_tap_netif examples/storage/parttool/parttool_example.py examples/storage/parttool/parttool_example.sh examples/system/ota/otatool/get_running_partition.py diff --git a/tools/mocks/startup/CMakeLists.txt b/tools/mocks/startup/CMakeLists.txt new file mode 100644 index 0000000000..1baae46a9c --- /dev/null +++ b/tools/mocks/startup/CMakeLists.txt @@ -0,0 +1,3 @@ +# This is a manual mock that supplies `main()` if FreeRTOS is mocked +idf_component_register(SRCS "startup_mock.c" + REQUIRES main esp_event) diff --git a/tools/mocks/startup/startup_mock.c b/tools/mocks/startup/startup_mock.c new file mode 100644 index 0000000000..8e99a55c3a --- /dev/null +++ b/tools/mocks/startup/startup_mock.c @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "esp_err.h" +#include "Mockqueue.h" +#include "Mocktask.h" +#include "Mockesp_event.h" +#include + +extern void app_main(void); + +int main(int argc, char **argv) +{ + int queue; + setbuf(stdout, NULL); + + // Mocks are used only as workarounds to build this application + // without FreeRTOS simulator. + // The code below presets the mocks to ignore and return + xQueueSemaphoreTake_IgnoreAndReturn(true); + xQueueGenericSend_IgnoreAndReturn(true); + vQueueDelete_Ignore(); + xQueueCreateMutex_IgnoreAndReturn((QueueHandle_t)&queue); + xTaskGetTickCount_IgnoreAndReturn(0); + xQueueGenericCreate_IgnoreAndReturn((QueueHandle_t)&queue); + xTaskCreatePinnedToCore_IgnoreAndReturn((BaseType_t) &queue); + esp_event_loop_create_default_IgnoreAndReturn(ESP_OK); + xQueueGiveMutexRecursive_IgnoreAndReturn(true); + app_main(); + return 0; +} From 332e4902b44c5282205a59c99a99a0816dc5f572 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 31 Jan 2023 08:15:33 +0100 Subject: [PATCH 2/4] lwip: Rename esp32 port layer to esp32xx As it reflects all chips from esp32-xx family, not only esp32 --- components/lwip/CMakeLists.txt | 2 +- components/lwip/port/{esp32 => esp32xx}/include/arch/cc.h | 0 components/lwip/port/{esp32 => esp32xx}/include/arch/perf.h | 0 components/lwip/port/{esp32 => esp32xx}/include/arch/vfs_lwip.h | 0 components/lwip/port/{esp32 => esp32xx}/include/sys/socket.h | 0 components/lwip/port/{esp32 => esp32xx}/netif/dhcp_state.c | 0 components/lwip/port/{esp32 => esp32xx}/no_vfs_syscalls.c | 0 components/lwip/port/{esp32 => esp32xx}/vfs_lwip.c | 0 8 files changed, 1 insertion(+), 1 deletion(-) rename components/lwip/port/{esp32 => esp32xx}/include/arch/cc.h (100%) rename components/lwip/port/{esp32 => esp32xx}/include/arch/perf.h (100%) rename components/lwip/port/{esp32 => esp32xx}/include/arch/vfs_lwip.h (100%) rename components/lwip/port/{esp32 => esp32xx}/include/sys/socket.h (100%) rename components/lwip/port/{esp32 => esp32xx}/netif/dhcp_state.c (100%) rename components/lwip/port/{esp32 => esp32xx}/no_vfs_syscalls.c (100%) rename components/lwip/port/{esp32 => esp32xx}/vfs_lwip.c (100%) diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index eafc963be5..22ceb1de84 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -1,7 +1,7 @@ idf_build_get_property(target IDF_TARGET) if(NOT ${target} STREQUAL "linux") # ESP platform targets share the same port folder - set(target esp32) + set(target esp32xx) endif() set(include_dirs diff --git a/components/lwip/port/esp32/include/arch/cc.h b/components/lwip/port/esp32xx/include/arch/cc.h similarity index 100% rename from components/lwip/port/esp32/include/arch/cc.h rename to components/lwip/port/esp32xx/include/arch/cc.h diff --git a/components/lwip/port/esp32/include/arch/perf.h b/components/lwip/port/esp32xx/include/arch/perf.h similarity index 100% rename from components/lwip/port/esp32/include/arch/perf.h rename to components/lwip/port/esp32xx/include/arch/perf.h diff --git a/components/lwip/port/esp32/include/arch/vfs_lwip.h b/components/lwip/port/esp32xx/include/arch/vfs_lwip.h similarity index 100% rename from components/lwip/port/esp32/include/arch/vfs_lwip.h rename to components/lwip/port/esp32xx/include/arch/vfs_lwip.h diff --git a/components/lwip/port/esp32/include/sys/socket.h b/components/lwip/port/esp32xx/include/sys/socket.h similarity index 100% rename from components/lwip/port/esp32/include/sys/socket.h rename to components/lwip/port/esp32xx/include/sys/socket.h diff --git a/components/lwip/port/esp32/netif/dhcp_state.c b/components/lwip/port/esp32xx/netif/dhcp_state.c similarity index 100% rename from components/lwip/port/esp32/netif/dhcp_state.c rename to components/lwip/port/esp32xx/netif/dhcp_state.c diff --git a/components/lwip/port/esp32/no_vfs_syscalls.c b/components/lwip/port/esp32xx/no_vfs_syscalls.c similarity index 100% rename from components/lwip/port/esp32/no_vfs_syscalls.c rename to components/lwip/port/esp32xx/no_vfs_syscalls.c diff --git a/components/lwip/port/esp32/vfs_lwip.c b/components/lwip/port/esp32xx/vfs_lwip.c similarity index 100% rename from components/lwip/port/esp32/vfs_lwip.c rename to components/lwip/port/esp32xx/vfs_lwip.c From 854e16feb31c117ff890c3e80230e727db6d8606 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 31 Jan 2023 08:29:00 +0100 Subject: [PATCH 3/4] examples: Add common linux component tapif_io That can be used with linux target on lwip to pass packets from lwip to linux host networking stack, e.g. routing the trafic to internet. --- .../protocol_examples_common/CMakeLists.txt | 10 ++ .../Kconfig.projbuild | 2 + .../protocol_examples_tapif_io/CMakeLists.txt | 9 + .../Kconfig.projbuild | 44 +++++ .../protocol_examples_tapif_io/README.md | 154 ++++++++++++++++++ .../include/protocol_examples_common.h | 32 ++++ .../include/tapio.h | 27 +++ .../protocol_examples_tapif_io/linux/tapio.c | 135 +++++++++++++++ .../linux_connect.c | 51 ++++++ .../protocol_examples_tapif_io/lwip/tapif.c | 100 ++++++++++++ .../protocol_examples_tapif_io/lwip/tapif.h | 25 +++ .../protocol_examples_tapif_io/make_tap_netif | 4 + 12 files changed, 593 insertions(+) create mode 100644 examples/common_components/protocol_examples_tapif_io/CMakeLists.txt create mode 100644 examples/common_components/protocol_examples_tapif_io/Kconfig.projbuild create mode 100644 examples/common_components/protocol_examples_tapif_io/README.md create mode 100644 examples/common_components/protocol_examples_tapif_io/include/protocol_examples_common.h create mode 100644 examples/common_components/protocol_examples_tapif_io/include/tapio.h create mode 100644 examples/common_components/protocol_examples_tapif_io/linux/tapio.c create mode 100644 examples/common_components/protocol_examples_tapif_io/linux_connect.c create mode 100644 examples/common_components/protocol_examples_tapif_io/lwip/tapif.c create mode 100644 examples/common_components/protocol_examples_tapif_io/lwip/tapif.h create mode 100755 examples/common_components/protocol_examples_tapif_io/make_tap_netif diff --git a/examples/common_components/protocol_examples_common/CMakeLists.txt b/examples/common_components/protocol_examples_common/CMakeLists.txt index 15981d027c..2a7352d5fd 100644 --- a/examples/common_components/protocol_examples_common/CMakeLists.txt +++ b/examples/common_components/protocol_examples_common/CMakeLists.txt @@ -1,3 +1,13 @@ +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + # Header only library for linux + + idf_component_register(INCLUDE_DIRS include + PRIV_REQUIRES tapif_io) + return() +endif() + set(srcs "stdin_out.c" "addr_from_stdin.c" "connect.c" diff --git a/examples/common_components/protocol_examples_common/Kconfig.projbuild b/examples/common_components/protocol_examples_common/Kconfig.projbuild index ed382457d7..f839bb5e21 100644 --- a/examples/common_components/protocol_examples_common/Kconfig.projbuild +++ b/examples/common_components/protocol_examples_common/Kconfig.projbuild @@ -4,6 +4,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_WIFI bool "connect using WiFi interface" + depends on !IDF_TARGET_LINUX default y help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. @@ -117,6 +118,7 @@ menu "Example Connection Configuration" config EXAMPLE_CONNECT_ETHERNET bool "connect using Ethernet interface" + depends on !IDF_TARGET_LINUX default n help Protocol examples can use Wi-Fi and/or Ethernet to connect to the network. diff --git a/examples/common_components/protocol_examples_tapif_io/CMakeLists.txt b/examples/common_components/protocol_examples_tapif_io/CMakeLists.txt new file mode 100644 index 0000000000..140acc5d9d --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/CMakeLists.txt @@ -0,0 +1,9 @@ +idf_build_get_property(target IDF_TARGET) + +if(${target} STREQUAL "linux") + idf_component_register(INCLUDE_DIRS include + SRCS linux/tapio.c linux_connect.c lwip/tapif.c + PRIV_REQUIRES esp_netif lwip) +else() + message(FATAL_ERROR "This component is currently only supported for linux target") +endif() diff --git a/examples/common_components/protocol_examples_tapif_io/Kconfig.projbuild b/examples/common_components/protocol_examples_tapif_io/Kconfig.projbuild new file mode 100644 index 0000000000..7c842a3c41 --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/Kconfig.projbuild @@ -0,0 +1,44 @@ +menu "Example Connection Configuration" + + config EXAMPLE_CONNECT_LWIP_TAPIF + bool "connect using lwip to linux tap interface" + depends on IDF_TARGET_LINUX && ESP_NETIF_TCPIP_LWIP + default n + + if EXAMPLE_CONNECT_LWIP_TAPIF + config EXAMPLE_CONNECT_TAPIF_IP_ADDR + string "Static IP address" + default "192.168.5.100" + help + Set static IP address. + + config EXAMPLE_CONNECT_TAPIF_NETMASK + string "Static netmask address" + default "255.255.255.0" + help + Set static netmask address. + + config EXAMPLE_CONNECT_TAPIF_GW + string "Static gateway address" + default "192.168.5.1" + help + Set static gateway address. + + config EXAMPLE_CONNECT_TAPIF_OUT_LOSS + int "Percentage of packets to be dropped on transmission" + default 0 + range 0 100 + help + Set non-zero number simulate packet loss when sending data. + Number represents probability between 0 and 100% + + config EXAMPLE_CONNECT_TAPIF_IN_LOSS + int "Percentage of packets to be dropped on reception" + default 0 + range 0 100 + help + Set non-zero number simulate packet loss when receiving data. + Number represents probability between 0 and 100% + endif + +endmenu diff --git a/examples/common_components/protocol_examples_tapif_io/README.md b/examples/common_components/protocol_examples_tapif_io/README.md new file mode 100644 index 0000000000..33d7d31fad --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/README.md @@ -0,0 +1,154 @@ +# tapif-io Component + +This component implements a tap networking interface that provides connectivity to host network using `tuntap` interface in Linux. +It could be used to route lwip traffic to host side network, typically when working with the **Linux target**. + +## How to use this component + +### Usage of the API + +1) Add the path to this component to the `EXTRA_COMPONENT_DIRS` in your project makefile +```cmake +list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/common_components/tapif_io") +``` +2) Include lwip and linux side of the configuration +```cpp +#include "esp_netif.h" // esp-netif +#include "tapio.h" // esp-netif's driver side +#include "lwip/tapif.h" // esp-netif's network stack side +``` +3) Configure the esp-netif + a) setup the linux tap I/O config +```cpp + esp_netif_driver_ifconfig_t driver_cfg = { + .handle = tapio_create(), + .transmit = tapio_output, + }; +``` + + b) configure the lwip netif for the tap interface +```cpp + struct esp_netif_netstack_config stack_cfg = { + .lwip = { + .init_fn = lwip_tapif_init, + .input_fn = lwip_tapif_input, + } + }; +``` + + c) configure the esp-netif basic parameters +```cpp + esp_netif_inherent_config_t base_cfg = { + .if_key = "TAP", // unique name of the interface + .flags = ESP_NETIF_FLAG_AUTOUP, // no dhcp client, starts when it's set up + .ip_info = &ip_info, // add static IP info + .route_prio = 100 // priority for setting default gateway + }; +``` + + +4) Initialize and attach the esp_netif to the I/O handle +```cpp + esp_netif_t *tap_netif = esp_netif_new(&cfg); + esp_netif_attach(tap_netif, driver_cfg.handle); +``` + +### Host side networking + +1) Create a new tun/tap interface type named `tap0` + a) You can run the script `./make_tap_netif` + b) Update the IP address of the interface to correspond to the configured static IP in previous step + +2) Start the application and send/receive the packets via `tap0` interface + * it is possible to create server or client test application listening or connecting to this interface. + * it is also possible to route these packets to external network (using routing rules or simply by ip forwarding if using the same subnet) + +#### Common networking/routing examples + +##### Isolated internal connection + +Is useful to experiment with one interface with no intention to connect to internet or external facilities. +Typically, when we want to create a server listening on the `tap0` interface and run a client in lwip, e.g. the default `tcp_client` socket example in IDF. +* Create the tap interface using `./make_tap_netif` and set the IP address **not to overlap** with any other IPv4 network range (e.g. `ip addr add 192.168.5.1/24 dev tap0`) +* Configure the `tapif_io` component to use static address from that range (e.g. `192.168.5.x`) +* Configure the `tcp_client` example to connect to the tap interface IP address (e.g. `192.168.5.1`) +* Execute a tcp server listening on the tap interface and the configured port (e.g. `nc -l 3333`) +* Build and run the `tcp_client` example to send and receive data between the server created in the previous step. + +##### Connecting to the external network using IP forwarding + +This allows using full-featured network facilities of your host network, but a care must be taken to the selected IP addresses to avoid potential conflicts. +* Set the IP address of the `tap0` interface from the range used by your host system's default gateway (e.g. `ip addr add 192.168.0.123/24 dev tap0`, assuming the default netif is `eth0` with IP range of `192.168.0.x` and this address doesn't overlap with any other IP address in this network) +* Configure the `tapif_io` with another address from the same range, e.g. +```text +CONFIG_EXAMPLE_CONNECT_TAPIF_IP_ADDR="192.168.0.100" +CONFIG_EXAMPLE_CONNECT_TAPIF_NETMASK="255.255.255.0" +CONFIG_EXAMPLE_CONNECT_TAPIF_GW="192.168.0.1" +``` +assuming that the default gateway of your host network is configured to `192.168.0.1` +* Build and run the lwip example to interact with the host network, e.g, to send an HTTP request to a publicly available http server (if the server is reachable from your host network) + +(Note, that the IP forwarding must be enabled in the host system: +```bash +echo 1 > /proc/sys/net/ipv4/ip_forward +``` +) + +##### Routing the internal interface to the host network with IP tables + +Uses an isolated interface with routing and NAT-ing between interfaces +* Configure the `tap0` interface address **not to overlap** with any other IPv4 network range. +* Setup `MASQUERADE` target to route network traffic between `tap0` and your default network interface (`eth0` in the below example). +```bash +sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE +sudo iptables -A FORWARD -i eth0 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT +sudo iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT +``` + +##### Using DHCP + +It's also possible to configure the lwip interface to use DHCP client (common setup for most default network interfaces, such as Ethernet or WiFi station) +and set up a DHCP server on the host machine to assign the IP address dynamically. + +1) **Configure and set the `esp-netif` up** + +* Same as in [API usage](#Usage-of-the-API), but update the base esp-netif config `3c)` to enable DHCP client +```cpp + esp_netif_inherent_config_t base_cfg = { + .if_key = "TAP", + .flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_EVENT_IP_MODIFIED | ESP_NETIF_FLAG_AUTOUP), + .route_prio = 100 + }; +``` +* After starting the netif, tell the lwIP that we're connected +```cpp + esp_netif_action_connected(tap_netif, 0, 0, 0); +``` +* Wait for the IP address to be assigned. +This could be implemented as a wait loop below, as the esp-event currently doesn't support IP events on Linux target. +```cpp + esp_netif_ip_info_t ip_info = {}; + while (ip_info.ip.addr == 0) { + ESP_LOGI("tap-init", "No IP assigned, waiting..."); + usleep(1000000); + esp_netif_get_ip_info(tap_netif, &ip_info); + } + ESP_LOGI("tap-init", "Assigned IP address:"IPSTR ",", IP2STR(&ip_info.ip)); +``` + +2) **Configure forwarding/routing** if needed based on the previous sections. + +3) **Configure the DHCP server on the host machine** + +Example for `isc-dhcp-server` + +```bash +INTERFACES="tap0"; +authoritative; + +subnet 192.168.5.0 netmask 255.255.255.0 { + range 192.168.5.2 192.168.5.200; + option routers 192.168.5.1; + option domain-name-servers 8.8.8.8; +} +``` diff --git a/examples/common_components/protocol_examples_tapif_io/include/protocol_examples_common.h b/examples/common_components/protocol_examples_tapif_io/include/protocol_examples_common.h new file mode 100644 index 0000000000..3ebfd2abeb --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/include/protocol_examples_common.h @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* Common functions for protocol examples, to establish tap interface connection + * For linux target + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. + */ +#pragma once +#include "esp_err.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Configure and connect, wait for IP + * + * @return ESP_OK on successful connection + */ +esp_err_t example_connect(void); + + +#ifdef __cplusplus +} +#endif diff --git a/examples/common_components/protocol_examples_tapif_io/include/tapio.h b/examples/common_components/protocol_examples_tapif_io/include/tapio.h new file mode 100644 index 0000000000..f646d7583d --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/include/tapio.h @@ -0,0 +1,27 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "esp_err.h" +#include "esp_netif.h" + +/** + * @brief Creates tapio layer as a driver interface to esp-netif + * + * @warning Implemented as singleton, can use only one tapio in the system! + * + * @return pointer to the tapio driver handle + */ +void *tapio_create(void); + +/** + * @brief esp-netif driver I/O output path + * + * @param h Driver's handle + * @param buffer Data to output + * @param len Data size + * @return ESP_OK on success + */ +esp_err_t tapio_output(void *h, void *buffer, size_t len); diff --git a/examples/common_components/protocol_examples_tapif_io/linux/tapio.c b/examples/common_components/protocol_examples_tapif_io/linux/tapio.c new file mode 100644 index 0000000000..215ed8521c --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/linux/tapio.c @@ -0,0 +1,135 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "esp_err.h" +#include "esp_log.h" +#include +#include "esp_netif.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "errno.h" + +#define LWIP_HDR_LINUX_SYS_SOCKETS_H + +#include +#include +#include + +#define DEVTAP "/dev/net/tun" +#define DEVTAP_NAME "tap0" + + +typedef struct tap_io { + esp_netif_driver_base_t base; + int fd; +} tap_io_t; + +static const char *TAG = "tap-netif"; + +static void tapio_input_task(void *arg) +{ + tap_io_t *io = arg; + fd_set fdset; + int ret; + while (1) { + FD_ZERO(&fdset); + FD_SET(io->fd, &fdset); + + /* Wait for a packet to arrive. */ + ret = select(io->fd + 1, &fdset, NULL, NULL, NULL); + + if (ret == 1) { + /* Handle incoming packet. */ + ssize_t readlen; + char buf[1518]; /* max packet size including VLAN excluding CRC */ + + /* Obtain the size of the packet and put it into the "len" + variable. */ + readlen = read(io->fd, buf, sizeof(buf)); + if (readlen < 0) { + ESP_LOGE(TAG, "Failed to read from tap fd: returned %ld", readlen); + exit(1); + } +#if CONFIG_EXAMPLE_CONNECT_TAPIF_IN_LOSS + if (((double)rand()/(double)RAND_MAX) < ((double)CONFIG_EXAMPLE_CONNECT_TAPIF_IN_LOSS)/100.0) { + ESP_LOGW(TAG, "Simulated packet drop on input"); + continue; + } +#endif + esp_netif_receive(io->base.netif, buf, readlen, NULL); + } else if (ret == -1) { + if (errno == EINTR /* Interrupted system call (used by FreeRTOS simulated interrupts) */) { + vTaskDelay(1); // yield to the FreeRTOS simulator + } else { + ESP_LOGE(TAG, "tapif_thread: select() error(%d), %s", errno, strerror(errno)); + } + } + } +} + +static esp_err_t tapio_start(esp_netif_t *esp_netif, void *arg) +{ + tap_io_t *io = arg; + io->base.netif = esp_netif; + esp_netif_action_start(esp_netif, 0, 0, 0); + + sys_thread_new("tapio_rx", tapio_input_task, io, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); + + return ESP_OK; +} + +void *tapio_create(void) +{ + static tap_io_t tap_io = {}; + tap_io.base.post_attach = tapio_start; + tap_io.fd = open(DEVTAP, O_RDWR); + if (tap_io.fd == -1) { + ESP_LOGE(TAG, "Cannot open tap device %s", DEVTAP); + return NULL; + } + + struct ifreq ifr = {}; + memset(&ifr, 0, sizeof(ifr)); + + strncpy(ifr.ifr_name, DEVTAP_NAME, sizeof(ifr.ifr_name)); + ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; /* ensure \0 termination */ + ifr.ifr_flags = IFF_TAP|IFF_NO_PI; + if (ioctl(tap_io.fd, TUNSETIFF, (void *) &ifr) < 0) { + ESP_LOGE(TAG, "Cannot configure ioctl(TUNSETIFF) for \"%s\"", DEVTAP); + return NULL; + } + + + return &tap_io; +} + +esp_err_t tapio_output(void *h, void *buffer, size_t len) +{ + tap_io_t *io = h; + ssize_t written; + +#if CONFIG_EXAMPLE_CONNECT_TAPIF_OUT_LOSS + if (((double)rand()/(double)RAND_MAX) < ((double)CONFIG_EXAMPLE_CONNECT_TAPIF_OUT_LOSS)/100.0) { + ESP_LOGW(TAG, "Simulated packet drop on output"); + return ESP_OK; /* ESP_OK because we simulate packet loss on cable */ + } +#endif + + /* signal that packet should be sent(); */ + written = write(io->fd, buffer, len); + if (written < len) { + ESP_LOGE(TAG, "Failed to write from tap fd: returned %ld", written); + return ESP_FAIL; + } + return ESP_OK; +} diff --git a/examples/common_components/protocol_examples_tapif_io/linux_connect.c b/examples/common_components/protocol_examples_tapif_io/linux_connect.c new file mode 100644 index 0000000000..150b2bf737 --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/linux_connect.c @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "esp_err.h" +#include "esp_netif.h" // esp-netif +#include "tapio.h" // esp-netif's driver side +#include "lwip/tapif.h" // esp-netif's network stack side + +esp_err_t example_connect(void) +{ +#if CONFIG_EXAMPLE_CONNECT_LWIP_TAPIF + // configure linux tapio + esp_netif_driver_ifconfig_t driver_cfg = { + .handle = tapio_create(), + .transmit = tapio_output, + }; + // configure lwip netif for the tapif + struct esp_netif_netstack_config stack_cfg = { + .lwip = { + .init_fn = lwip_tapif_init, + .input_fn = lwip_tapif_input, + } + }; + // configure inherent esp-netif parameters + esp_netif_ip_info_t ip_info = {}; + ip_info.ip.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_IP_ADDR); + ip_info.netmask.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_NETMASK); + ip_info.gw.addr = ipaddr_addr(CONFIG_EXAMPLE_CONNECT_TAPIF_GW); + + esp_netif_inherent_config_t base_cfg = { + .if_key = "TAP", + .flags = ESP_NETIF_FLAG_AUTOUP, + .ip_info = &ip_info, + .route_prio = 100 + }; + + // put all configs together + esp_netif_config_t cfg = { + .base = &base_cfg, + .driver = &driver_cfg, + .stack = &stack_cfg + }; + + // create the interface and attach it to the tapio-handle + esp_netif_t *tap_netif = esp_netif_new(&cfg); + esp_netif_attach(tap_netif, driver_cfg.handle); +#endif // EXAMPLE_CONNECT_LWIP_TAPIF + return ESP_OK; +} diff --git a/examples/common_components/protocol_examples_tapif_io/lwip/tapif.c b/examples/common_components/protocol_examples_tapif_io/lwip/tapif.c new file mode 100644 index 0000000000..a59f70a9f8 --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/lwip/tapif.c @@ -0,0 +1,100 @@ +/* + * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science + * + * SPDX-License-Identifier: BSD-3-Clause + * + * SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD + */ +#include +#include "lwip/opt.h" +#include "lwip/pbuf.h" +#include "lwip/snmp.h" +#include "lwip/ethip6.h" +#include "netif/etharp.h" + +#include "esp_netif.h" +#include "esp_netif_net_stack.h" + +#define IFNAME0 't' +#define IFNAME1 'p' + +static err_t +low_level_output(struct netif *netif, struct pbuf *p) +{ + esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + char buf[1518]; /* max packet size including VLAN excluding CRC */ + + if (p->tot_len > sizeof(buf)) { + MIB2_STATS_NETIF_INC(netif, ifoutdiscards); + LWIP_DEBUGF(NETIF_DEBUG, ("tapif: packet too large")); + return ERR_IF; + } + + /* initiate transfer(); */ + pbuf_copy_partial(p, buf, p->tot_len, 0); + + int ret = esp_netif_transmit(esp_netif, buf, p->tot_len); + /* Check error */ + if (likely(ret == ESP_OK)) { + return ERR_OK; + } + if (ret == ESP_ERR_NO_MEM) { + return ERR_MEM; + } + return ERR_IF; +} + +static void +low_level_init(struct netif *netif) +{ + + /* Obtain MAC address from network interface. */ + netif->hwaddr[0] = 0x02; + netif->hwaddr[1] = 0x12; + netif->hwaddr[2] = 0x34; + netif->hwaddr[3] = 0x56; + netif->hwaddr[4] = 0x78; + netif->hwaddr[5] = 0xab; + netif->hwaddr_len = 6; + + /* device capabilities */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP; +} + +err_t lwip_tapif_init(struct netif *netif) +{ + LWIP_ASSERT("Tried to initialize tapif with NULL netif", netif != NULL); + netif->name[0] = IFNAME0; + netif->name[1] = IFNAME1; +#if LWIP_IPV4 + netif->output = etharp_output; +#endif /* LWIP_IPV4 */ +#if LWIP_IPV6 + netif->output_ip6 = ethip6_output; +#endif /* LWIP_IPV6 */ + netif->linkoutput = low_level_output; + netif->mtu = 1500; + + low_level_init(netif); + netif_set_link_up(netif); + return ERR_OK; +} + +void lwip_tapif_input(void *h, void *buffer, size_t len, void *l2_buff) +{ + struct netif *netif = h; + struct pbuf *p; + LWIP_ASSERT("running tapif input with NULL netif", netif != NULL); + + p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); + if (p == NULL) { + return; + } + memcpy(p->payload, buffer, len); + + /* full packet send to tcpip_thread to process */ + if (unlikely(netif->input(p, netif) != ERR_OK)) { + LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: IP input error\n")); + pbuf_free(p); + } +} diff --git a/examples/common_components/protocol_examples_tapif_io/lwip/tapif.h b/examples/common_components/protocol_examples_tapif_io/lwip/tapif.h new file mode 100644 index 0000000000..3062985670 --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/lwip/tapif.h @@ -0,0 +1,25 @@ +/* + * SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science + * + * SPDX-License-Identifier: BSD-3-Clause + * + * SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD + */ +#pragma once +#include "lwip/esp_netif_net_stack.h" + +/** + * @brief lwip netif init API + * @param netif pointer to lwip's netif + * @return ERR_OK on success + */ +err_t lwip_tapif_init(struct netif *netif); + +/** + * @brief Input data path + * @param h pointer to network stack handle (stuct netif* in our case) + * @param buffer Data + * @param len Data size + * @param l2_buff Data L2 buffer + */ +void lwip_tapif_input(void *h, void *buffer, size_t len, void *l2_buff); diff --git a/examples/common_components/protocol_examples_tapif_io/make_tap_netif b/examples/common_components/protocol_examples_tapif_io/make_tap_netif new file mode 100755 index 0000000000..3857edb657 --- /dev/null +++ b/examples/common_components/protocol_examples_tapif_io/make_tap_netif @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +sudo ip tuntap add dev tap0 mode tap user `whoami` +sudo ip link set tap0 up +sudo ip addr add 192.168.5.1/24 dev tap0 From 5d04ebab51856fefe9396071f4723d49c48f2d84 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 31 Jan 2023 08:30:21 +0100 Subject: [PATCH 4/4] examples: Update socket udp_client to support lwip on linux This uses FreeRTOS simulator and lwip on linux, with options to use tap-io commponent for host network interactions. --- .../sockets/udp_client/CMakeLists.txt | 11 ++++-- .../protocols/sockets/udp_client/README.md | 37 +++++++++++++++++++ .../sockets/udp_client/main/udp_client.c | 4 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/examples/protocols/sockets/udp_client/CMakeLists.txt b/examples/protocols/sockets/udp_client/CMakeLists.txt index ec722eef53..b9b38c6afb 100644 --- a/examples/protocols/sockets/udp_client/CMakeLists.txt +++ b/examples/protocols/sockets/udp_client/CMakeLists.txt @@ -2,9 +2,14 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +if("${IDF_TARGET}" STREQUAL "linux") + # This example uses an extra component with common functionality for lwip's port on linux target + set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_tapif_io) + set(COMPONENTS main esp_netif protocol_examples_tapif_io startup esp_hw_support esp_system nvs_flash) +else() + # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection on ESP target + set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +endif() include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(udp_client) diff --git a/examples/protocols/sockets/udp_client/README.md b/examples/protocols/sockets/udp_client/README.md index bdcee22108..cd8b39cf24 100644 --- a/examples/protocols/sockets/udp_client/README.md +++ b/examples/protocols/sockets/udp_client/README.md @@ -72,3 +72,40 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui ## Troubleshooting Start server first, to receive data sent from the client (application). + +## Running the example for Linux target + +This example could be executed on host system, using lwIP port for linux and FreeRTOS simulator on linux. The socket API used in this example directly calls lwIP implementation. Follow the steps below to configure, build and run the example on linux operating system. + +1. First configure the target (please note that using linux target is currently available only in `preview` stage) +``` +idf.py --preview set-target linux +``` +2. Configure the project +``` +idf.py menuconfig +``` +Choose connection capabilities in `Example Connection Configuration` menu: + +* By default, the `example_connect()` function returns as a no-op, expecting that the connection is already available. This option is preferred when we don't have to interact with outside networking layers and use only lwIP internal interface, such as loopback netif (`lo`). +* If you want to connect lwIP network interface to the host system networking, set `EXAMPLE_CONNECT_LWIP_TAPIF`. + * Configure the interface address information (IP address, GW address and netmask). + * Create a host network interface named `tap0` of *TAP* type. You can use the `./make_tap_netif` script located in the `tapif_io` component directory. + * Optionally set input or output packet loss rate to simulate loosing data of physical interfaces. + * Note about the host side networking: + * This example uses static IP address configured in `tapif_io` component configuration. + * Use the IP ranges that do not overlap with any other IP range of the host system. + * Make sure that the same IP range is configured in `tap0` interface created by tge `./make_tap_netif` script. + * You can leave the defaults in the script and `tapif_io` settings unless any other host network interface uses `192.168.5.x` range. + * Read more about host-side networking in the [`tapif_io` component documentation](../../../common_components/protocol_examples_tapif_io/README.md). + +3. Generate partition table for the application: +``` +idf.by partition-table +``` + +4. Build and run the example the usual way (Note that the flash step is left out) +``` +idf.by build +idf.py monitor +``` diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index 268f6e65da..f3e92f9fb9 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -12,7 +12,6 @@ #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" -#include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -23,7 +22,10 @@ #include "lwip/sockets.h" #include "lwip/sys.h" #include + +#ifdef CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN #include "addr_from_stdin.h" +#endif #if defined(CONFIG_EXAMPLE_IPV4) #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR