From 758ba6ab4080bb7638e298e0d9940846a524bae1 Mon Sep 17 00:00:00 2001 From: Abhik Roy Date: Wed, 8 Oct 2025 21:37:23 +1100 Subject: [PATCH] fix: Reorder Ethernet initialization in VLAN example - Moved `ethernet_init_all` to after default event loop creation to ensure the event loop exists before Ethernet driver initialization. --- .../vlan_support/main/vlan_support_main.c | 18 ++++++------ .../network/vlan_support/pytest_vlan_napt.py | 28 ++++++++++++------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/examples/network/vlan_support/main/vlan_support_main.c b/examples/network/vlan_support/main/vlan_support_main.c index dc0cb6cb2e4..e985330b09e 100644 --- a/examples/network/vlan_support/main/vlan_support_main.c +++ b/examples/network/vlan_support/main/vlan_support_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -128,16 +128,8 @@ void app_main(void) s_vlan_event_group = xEventGroupCreate(); static esp_vlan_netifs vlan_netif_list; - - // Initialize Ethernet driver uint8_t eth_port_cnt = 0; esp_eth_handle_t *eth_handle; - ESP_ERROR_CHECK(ethernet_init_all(ð_handle, ð_port_cnt)); - - // Check or multiple ethernet interface - if (1 < eth_port_cnt) { - ESP_LOGW(TAG, "Multiple Ethernet Interface detected: Only the first initialized interface is going to be used."); - } // Initialize TCP/IP network interface (should be called only once in application) ESP_ERROR_CHECK(esp_netif_init()); @@ -145,6 +137,14 @@ void app_main(void) // Create default event loop that running in background ESP_ERROR_CHECK(esp_event_loop_create_default()); + // Initialize Ethernet driver + ESP_ERROR_CHECK(ethernet_init_all(ð_handle, ð_port_cnt)); + + // Check or multiple ethernet interface + if (1 < eth_port_cnt) { + ESP_LOGW(TAG, "Multiple Ethernet Interface detected: Only the first initialized interface is going to be used."); + } + // Register user defined event handlers ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); diff --git a/examples/network/vlan_support/pytest_vlan_napt.py b/examples/network/vlan_support/pytest_vlan_napt.py index c646e5b3700..3e0c1578207 100644 --- a/examples/network/vlan_support/pytest_vlan_napt.py +++ b/examples/network/vlan_support/pytest_vlan_napt.py @@ -4,18 +4,16 @@ import ipaddress import subprocess import threading import time -from typing import Dict -from typing import Union import pytest from pytest_embedded import Dut from pytest_embedded_idf.utils import idf_parametrize from scapy import layers -from scapy.all import AsyncSniffer from scapy.all import ICMP from scapy.all import IP from scapy.all import TCP from scapy.all import UDP +from scapy.all import AsyncSniffer udp_port = 1234 tcp_port = 4321 @@ -95,7 +93,7 @@ def create_config(dut: Dut) -> dict: tmp_ip = ipaddress.IPv4Address(vlanServer_conf['ip']) vlanServer_net_addr = ipaddress.IPv4Address(int(tmp_ip) & int(subnet_mask)) - config: Dict[str, Union[str, dict, dict, str, str, list, list, list, list]] = { + config: dict[str, str | dict | dict | str | str | list | list | list | list] = { # Basic Configurations 'pc_iface': pc_iface, 'vlanClient': vlanClient_conf, @@ -103,10 +101,11 @@ def create_config(dut: Dut) -> dict: 'esp_vlanClient_ip': esp_vlanClient_ip, 'esp_vlanServer_ip': esp_vlanServer_ip, 'vlan_create_cmd_l': [ - f'ip netns add ns_vlanClient', + 'ip netns add ns_vlanClient', f'ip link add link {pc_iface} name {vlanClient_conf["name"]} type vlan id {vlanClient_conf["id"]}', f'ip link set {vlanClient_conf["name"]} netns ns_vlanClient', - f'ip netns exec ns_vlanClient ip addr add {vlanClient_conf["ip"]}/255.255.255.0 dev {vlanClient_conf["name"]}', + f'ip netns exec ns_vlanClient ip addr add {vlanClient_conf["ip"]}/255.255.255.0 ' + f'dev {vlanClient_conf["name"]}', f'ip netns exec ns_vlanClient ip link set dev {vlanClient_conf["name"]} up', f'ip link add link {pc_iface} name {vlanServer_conf["name"]} type vlan id {vlanServer_conf["id"]}', f'ip addr add {vlanServer_conf["ip"]}/255.255.255.0 dev {vlanServer_conf["name"]}', @@ -117,7 +116,7 @@ def create_config(dut: Dut) -> dict: f'ip netns exec ns_vlanClient ip link delete {vlanClient_conf["name"]}', f'ip link set dev {vlanServer_conf["name"]} down', f'ip link delete {vlanServer_conf["name"]}', - f'ip netns delete ns_vlanClient', + 'ip netns delete ns_vlanClient', ], 'set_route_cmd_l': [ f'ip netns exec ns_vlanClient ip route add {vlanServer_net_addr}/24 via {esp_vlanClient_ip}' @@ -148,7 +147,10 @@ def ping_test(config: dict) -> None: ) # Stop sniffing - capture.join(timeout=20) + try: + capture.join(timeout=20) + except OSError as e: + print(f'[ERROR] Packet capture failed: {e}') vlanServer_pkt_list = capture.results clear_network(config) @@ -227,7 +229,10 @@ def udp_test(config: dict) -> None: udp_server_client_comm(config, udp_port) # Stop sniffing - capture.join(timeout=20) + try: + capture.join(timeout=20) + except OSError as e: + print(f'[ERROR] Packet capture failed: {e}') vlanServer_pkt_list = capture.results clear_network(config) @@ -307,7 +312,10 @@ def tcp_test(config: dict) -> None: tcp_server_client_comm(config, tcp_port) # Stop sniffing - capture.join(timeout=20) + try: + capture.join(timeout=20) + except OSError as e: + print(f'[ERROR] Packet capture failed: {e}') vlanServer_pkt_list = capture.results clear_network(config)