From 41b67e10312b6ba24e96366c78432294ba8e2490 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Tue, 13 Feb 2024 17:17:34 +0100 Subject: [PATCH] fix(esp_eth): improved SPI Ethernet _alloc_recv_buf error handling --- components/esp_eth/src/esp_eth_mac_dm9051.c | 45 ++-- .../esp_eth/src/esp_eth_mac_ksz8851snl.c | 45 ++-- components/esp_eth/src/esp_eth_mac_w5500.c | 50 ++-- .../test_apps/main/esp_eth_test_common.c | 14 +- .../esp_eth/test_apps/main/esp_eth_test_hal.c | 34 ++- .../esp_eth/test_apps/main/esp_eth_test_l2.c | 225 +++++++++++++++--- .../esp_eth/test_apps/pytest_esp_eth.py | 63 ++++- 7 files changed, 369 insertions(+), 107 deletions(-) diff --git a/components/esp_eth/src/esp_eth_mac_dm9051.c b/components/esp_eth/src/esp_eth_mac_dm9051.c index f4c4a50494..8cf0837452 100644 --- a/components/esp_eth/src/esp_eth_mac_dm9051.c +++ b/components/esp_eth/src/esp_eth_mac_dm9051.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -817,6 +817,7 @@ static void emac_dm9051_task(void *arg) { emac_dm9051_t *emac = (emac_dm9051_t *)arg; uint8_t status = 0; + esp_err_t ret; while (1) { // check if the task receives any notification if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ... @@ -832,31 +833,35 @@ static void emac_dm9051_task(void *arg) /* define max expected frame len */ uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint8_t *buffer; - dm9051_alloc_recv_buf(emac, &buffer, &frame_len); - /* we have memory to receive the frame of maximal size previously defined */ - if (buffer != NULL) { - uint32_t buf_len = DM9051_ETH_MAC_RX_BUF_SIZE_AUTO; - if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { - if (buf_len == 0) { + if ((ret = dm9051_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) { + if (buffer != NULL) { + /* we have memory to receive the frame of maximal size previously defined */ + uint32_t buf_len = DM9051_ETH_MAC_RX_BUF_SIZE_AUTO; + if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { + if (buf_len == 0) { + dm9051_flush_recv_frame(emac); + free(buffer); + } else if (frame_len > buf_len) { + ESP_LOGE(TAG, "received frame was truncated"); + free(buffer); + } else { + ESP_LOGD(TAG, "receive len=%u", buf_len); + /* pass the buffer to stack (e.g. TCP/IP layer) */ + emac->eth->stack_input(emac->eth, buffer, buf_len); + } + } else { + ESP_LOGE(TAG, "frame read from module failed"); dm9051_flush_recv_frame(emac); free(buffer); - } else if (frame_len > buf_len) { - ESP_LOGE(TAG, "received frame was truncated"); - free(buffer); - } else { - ESP_LOGD(TAG, "receive len=%u", buf_len); - /* pass the buffer to stack (e.g. TCP/IP layer) */ - emac->eth->stack_input(emac->eth, buffer, buf_len); } - } else { - ESP_LOGE(TAG, "frame read from module failed"); - dm9051_flush_recv_frame(emac); - free(buffer); + } else if (frame_len) { + ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer); } - /* if allocation failed and there is a waiting frame */ - } else if (frame_len) { + } else if (ret == ESP_ERR_NO_MEM) { ESP_LOGE(TAG, "no mem for receive buffer"); dm9051_flush_recv_frame(emac); + } else { + ESP_LOGE(TAG, "unexpected error 0x%x", ret); } } while (emac->packets_remain); } diff --git a/components/esp_eth/src/esp_eth_mac_ksz8851snl.c b/components/esp_eth/src/esp_eth_mac_ksz8851snl.c index 473195a83b..f6921c66f4 100644 --- a/components/esp_eth/src/esp_eth_mac_ksz8851snl.c +++ b/components/esp_eth/src/esp_eth_mac_ksz8851snl.c @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: MIT * - * SPDX-FileContributor: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2021-2024 Espressif Systems (Shanghai) CO LTD */ #include @@ -689,6 +689,7 @@ static esp_err_t emac_ksz8851_set_peer_pause_ability(esp_eth_mac_t *mac, uint32_ static void emac_ksz8851snl_task(void *arg) { emac_ksz8851snl_t *emac = (emac_ksz8851snl_t *)arg; + esp_err_t ret; while (1) { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); @@ -742,31 +743,35 @@ static void emac_ksz8851snl_task(void *arg) /* define max expected frame len */ uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint8_t *buffer; - emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len); - /* we have memory to receive the frame of maximal size previously defined */ - if (buffer != NULL) { - uint32_t buf_len = KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO; - if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { - if (buf_len == 0) { + if ((ret = emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) { + if (buffer != NULL) { + /* we have memory to receive the frame of maximal size previously defined */ + uint32_t buf_len = KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO; + if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { + if (buf_len == 0) { + emac_ksz8851_flush_recv_queue(emac); + free(buffer); + } else if (frame_len > buf_len) { + ESP_LOGE(TAG, "received frame was truncated"); + free(buffer); + } else { + ESP_LOGD(TAG, "receive len=%u", buf_len); + /* pass the buffer to stack (e.g. TCP/IP layer) */ + emac->eth->stack_input(emac->eth, buffer, buf_len); + } + } else { + ESP_LOGE(TAG, "frame read from module failed"); emac_ksz8851_flush_recv_queue(emac); free(buffer); - } else if (frame_len > buf_len) { - ESP_LOGE(TAG, "received frame was truncated"); - free(buffer); - } else { - ESP_LOGD(TAG, "receive len=%u", buf_len); - /* pass the buffer to stack (e.g. TCP/IP layer) */ - emac->eth->stack_input(emac->eth, buffer, buf_len); } - } else { - ESP_LOGE(TAG, "frame read from module failed"); - emac_ksz8851_flush_recv_queue(emac); - free(buffer); + } else if (frame_len) { + ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer); } - /* if allocation failed and there is a waiting frame */ - } else if (frame_len) { + } else if (ret == ESP_ERR_NO_MEM) { ESP_LOGE(TAG, "no mem for receive buffer"); emac_ksz8851_flush_recv_queue(emac); + } else { + ESP_LOGE(TAG, "unexpected error 0x%x", ret); } } ksz8851_write_reg(emac, KSZ8851_IER, ier); diff --git a/components/esp_eth/src/esp_eth_mac_w5500.c b/components/esp_eth/src/esp_eth_mac_w5500.c index dbb5a9ba74..bc4356015a 100644 --- a/components/esp_eth/src/esp_eth_mac_w5500.c +++ b/components/esp_eth/src/esp_eth_mac_w5500.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -677,7 +677,7 @@ static esp_err_t emac_w5500_receive(esp_eth_mac_t *mac, uint8_t *buf, uint32_t * remain_bytes -= rx_len + 2; emac->packets_remain = remain_bytes > 0; - *length = rx_len; + *length = copy_len; return ret; err: *length = 0; @@ -700,12 +700,13 @@ static esp_err_t emac_w5500_flush_recv_frame(emac_w5500_t *emac) // read head first ESP_GOTO_ON_ERROR(w5500_read_buffer(emac, &rx_len, sizeof(rx_len), offset), err, TAG, "read frame header failed"); // update read pointer - offset = rx_len; + rx_len = __builtin_bswap16(rx_len); + offset += rx_len; + offset = __builtin_bswap16(offset); ESP_GOTO_ON_ERROR(w5500_write(emac, W5500_REG_SOCK_RX_RD(0), &offset, sizeof(offset)), err, TAG, "write RX RD failed"); /* issue RECV command */ ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_RECV, 100), err, TAG, "issue RECV command failed"); // check if there're more data need to process - rx_len = __builtin_bswap16(rx_len); remain_bytes -= rx_len; emac->packets_remain = remain_bytes > 0; } @@ -731,6 +732,7 @@ static void emac_w5500_task(void *arg) uint8_t *buffer = NULL; uint32_t frame_len = 0; uint32_t buf_len = 0; + esp_err_t ret; while (1) { /* check if the task receives any notification */ if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ... @@ -747,29 +749,33 @@ static void emac_w5500_task(void *arg) do { /* define max expected frame len */ frame_len = ETH_MAX_PACKET_SIZE; - emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len); - /* we have memory to receive the frame of maximal size previously defined */ - if (buffer != NULL) { - buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO; - if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { - if (buf_len == 0) { - free(buffer); - } else if (frame_len > buf_len) { - ESP_LOGE(TAG, "received frame was truncated"); - free(buffer); + if ((ret = emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) { + if (buffer != NULL) { + /* we have memory to receive the frame of maximal size previously defined */ + buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO; + if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { + if (buf_len == 0) { + free(buffer); + } else if (frame_len > buf_len) { + ESP_LOGE(TAG, "received frame was truncated"); + free(buffer); + } else { + ESP_LOGD(TAG, "receive len=%u", buf_len); + /* pass the buffer to stack (e.g. TCP/IP layer) */ + emac->eth->stack_input(emac->eth, buffer, buf_len); + } } else { - ESP_LOGD(TAG, "receive len=%u", buf_len); - /* pass the buffer to stack (e.g. TCP/IP layer) */ - emac->eth->stack_input(emac->eth, buffer, buf_len); + ESP_LOGE(TAG, "frame read from module failed"); + free(buffer); } - } else { - ESP_LOGE(TAG, "frame read from module failed"); - free(buffer); + } else if (frame_len) { + ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer); } - /* if allocation failed and there is a waiting frame */ - } else if (frame_len) { + } else if (ret == ESP_ERR_NO_MEM) { ESP_LOGE(TAG, "no mem for receive buffer"); emac_w5500_flush_recv_frame(emac); + } else { + ESP_LOGE(TAG, "unexpected error 0x%x", ret); } } while (emac->packets_remain); } diff --git a/components/esp_eth/test_apps/main/esp_eth_test_common.c b/components/esp_eth/test_apps/main/esp_eth_test_common.c index 69f5577abd..20469e58b5 100644 --- a/components/esp_eth/test_apps/main/esp_eth_test_common.c +++ b/components/esp_eth/test_apps/main/esp_eth_test_common.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -96,23 +96,31 @@ esp_eth_phy_t *phy_init(eth_phy_config_t *phy_config) phy_config->phy_addr = ESP_ETH_PHY_ADDR_AUTO; #if CONFIG_TARGET_ETH_PHY_DEVICE_IP101 phy = esp_eth_phy_new_ip101(phy_config); + ESP_LOGI(TAG, "DUT PHY: IP101"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720 phy = esp_eth_phy_new_lan87xx(phy_config); + ESP_LOGI(TAG, "DUT PHY: LAN8720"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8041 phy = esp_eth_phy_new_ksz80xx(phy_config); + ESP_LOGI(TAG, "DUT PHY: KSZ8041"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_RTL8201 phy = esp_eth_phy_new_rtl8201(phy_config); + ESP_LOGI(TAG, "DUT PHY: RTL8201"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_DP83848 phy = esp_eth_phy_new_dp83848(phy_config); + ESP_LOGI(TAG, "DUT PHY: DP83848"); #endif // CONFIG_TARGET_ETH_PHY_DEVICE_IP101 #elif CONFIG_TARGET_USE_SPI_ETHERNET phy_config->reset_gpio_num = -1; #if CONFIG_TARGET_ETH_PHY_DEVICE_W5500 phy = esp_eth_phy_new_w5500(phy_config); + ESP_LOGI(TAG, "DUT PHY: W5500"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL phy = esp_eth_phy_new_ksz8851snl(phy_config); + ESP_LOGI(TAG, "DUT PHY: KSZ8851SNL"); #elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051 phy = esp_eth_phy_new_dm9051(phy_config); + ESP_LOGI(TAG, "DUT PHY: DM9051"); #endif // CONFIG_TARGET_ETH_PHY_DEVICE_W5500 #endif // CONFIG_TARGET_USE_INTERNAL_ETHERNET return phy; @@ -145,14 +153,18 @@ void eth_event_handler(void *arg, esp_event_base_t event_base, EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; switch (event_id) { case ETHERNET_EVENT_CONNECTED: + ESP_LOGI(TAG, "Ethernet Link Up"); xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT); break; case ETHERNET_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "Ethernet Link Down"); break; case ETHERNET_EVENT_START: + ESP_LOGI(TAG, "Ethernet Started"); xEventGroupSetBits(eth_event_group, ETH_START_BIT); break; case ETHERNET_EVENT_STOP: + ESP_LOGI(TAG, "Ethernet Stopped"); xEventGroupSetBits(eth_event_group, ETH_STOP_BIT); break; default: diff --git a/components/esp_eth/test_apps/main/esp_eth_test_hal.c b/components/esp_eth/test_apps/main/esp_eth_test_hal.c index 89f0f959f4..19db8e1a6a 100644 --- a/components/esp_eth/test_apps/main/esp_eth_test_hal.c +++ b/components/esp_eth/test_apps/main/esp_eth_test_hal.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -14,6 +14,10 @@ #define ETHERTYPE_TX_MULTI_2 0x2223 // frame transmitted via emac_hal_transmit_multiple_buf_frame (2 buffers) #define ETHERTYPE_TX_MULTI_3 0x2224 // frame transmitted via emac_hal_transmit_multiple_buf_frame (3 buffers) +#define MINIMUM_TEST_FRAME_SIZE 64 + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + static const char *TAG = "esp32_eth_test_hal"; typedef struct @@ -123,8 +127,32 @@ TEST_CASE("hal receive/transmit", "[emac_hal]") test_pkt->data[i] = i & 0xFF; } - // verify that HAL driver correctly processes frame from EMAC descriptors - uint16_t transmit_size = CONFIG_ETH_DMA_BUFFER_SIZE; + uint16_t transmit_size; + + ESP_LOGI(TAG, "Verify DMA descriptors are returned back to owner"); + // find if Rx or Tx buffer number is bigger and work with bigger number + uint32_t config_eth_dma_max_buffer_num = MAX(CONFIG_ETH_DMA_RX_BUFFER_NUM, CONFIG_ETH_DMA_TX_BUFFER_NUM); + // start with short frames since EMAC Rx FIFO may be different of size for different chips => it may help with following fail isolation + for (int32_t i = 0; i < config_eth_dma_max_buffer_num*2; i++) { + transmit_size = MINIMUM_TEST_FRAME_SIZE; + ESP_LOGI(TAG, "transmit frame size: %" PRIu16 ", i = %" PRIi32, transmit_size, i); + recv_info.expected_size = transmit_size; + TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); + TEST_ASSERT(xSemaphoreTake(recv_info.mutex, pdMS_TO_TICKS(500))); + } + + ESP_LOGI(TAG, "Verify that we are able to transmit/receive all frame sizes"); + // iteration over different sizes may help with fail isolation + for (int i = 1; (MINIMUM_TEST_FRAME_SIZE *i) < ETH_MAX_PAYLOAD_LEN; i++) { + transmit_size = MINIMUM_TEST_FRAME_SIZE * i; + ESP_LOGI(TAG, "transmit frame size: %" PRIu16, transmit_size); + recv_info.expected_size = transmit_size; + TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); + TEST_ASSERT(xSemaphoreTake(recv_info.mutex, pdMS_TO_TICKS(500))); + } + + ESP_LOGI(TAG, "Verify that DMA driver correctly processes frame from EMAC descriptors at boundary conditions"); + transmit_size = CONFIG_ETH_DMA_BUFFER_SIZE; ESP_LOGI(TAG, "transmit frame size: %" PRIu16, transmit_size); recv_info.expected_size = transmit_size; TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); diff --git a/components/esp_eth/test_apps/main/esp_eth_test_l2.c b/components/esp_eth/test_apps/main/esp_eth_test_l2.c index a174e5574b..7ac62c809f 100644 --- a/components/esp_eth/test_apps/main/esp_eth_test_l2.c +++ b/components/esp_eth/test_apps/main/esp_eth_test_l2.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -27,6 +27,11 @@ #define POKE_RESP 0xFB #define DUMMY_TRAFFIC 0xFF +#define W5500_RX_MEM_SIZE (0x4000) +#define DM9051_RX_MEM_SIZE (0x4000) +#define KSZ8851SNL_RX_MEM_SIZE (0x3000) + +static const char *TAG = "esp32_eth_test_l2"; typedef struct { EventGroupHandle_t eth_event_group; @@ -38,8 +43,9 @@ typedef struct bool check_rx_data; } recv_info_t; +static recv_info_t s_recv_info; -esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv) { +static esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv) { recv_info_t *recv_info = (recv_info_t*)priv; EventGroupHandle_t eth_event_group = recv_info->eth_event_group; emac_frame_t *pkt = (emac_frame_t *)buffer; @@ -72,7 +78,7 @@ esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t } } else if (ntohs(pkt->proto) == TEST_CTRL_ETH_TYPE) { // control packet if (pkt->data[0] == POKE_RESP) { - memcpy(recv_info->dst_mac_addr, pkt->dest, ETH_ADDR_LEN); + memcpy(recv_info->dst_mac_addr, pkt->src, ETH_ADDR_LEN); // test PC source MAC addr is destination for us printf("Poke response received\n"); xEventGroupSetBits(eth_event_group, ETH_POKE_RESP_RECV_BIT); } @@ -88,7 +94,7 @@ esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t * has been established. I.e. if DUT is connected in network with a switch, even if link is indicated up, * it may take some time the switch starts forwarding the associated port (e.g. it runs RSTP at first). */ -void poke_and_wait(esp_eth_handle_t eth_handle, void *data, uint16_t size, EventGroupHandle_t eth_event_group) +void poke_and_wait(esp_eth_handle_t eth_handle, void *data, uint16_t size, uint8_t *dst_mac_addr, EventGroupHandle_t eth_event_group) { // create a control frame to control test flow between the UT and the Python test script emac_frame_t *ctrl_pkt = calloc(1, 60); @@ -109,6 +115,9 @@ void poke_and_wait(esp_eth_handle_t eth_handle, void *data, uint16_t size, Event EventBits_t bits = xEventGroupWaitBits(eth_event_group, ETH_POKE_RESP_RECV_BIT, true, true, pdMS_TO_TICKS(WAIT_AFTER_CONN_MS)); if ((bits & ETH_POKE_RESP_RECV_BIT) == ETH_POKE_RESP_RECV_BIT) { + if (dst_mac_addr != NULL) { + memcpy(dst_mac_addr, s_recv_info.dst_mac_addr, ETH_ADDR_LEN); + } break; } } @@ -134,13 +143,12 @@ TEST_CASE("ethernet broadcast transmit", "[ethernet_l2]") TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_state_group)); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); TEST_ASSERT(eth_event_rx_group != NULL); - recv_info_t recv_info = { - .eth_event_group = eth_event_rx_group, - .check_rx_data = false, - .unicast_rx_cnt = 0, - .multicast_rx_cnt = 0, - .brdcast_rx_cnt = 0 - }; + + s_recv_info.eth_event_group = eth_event_rx_group; + s_recv_info.check_rx_data = false; + s_recv_info.unicast_rx_cnt = 0; + s_recv_info.multicast_rx_cnt = 0; + s_recv_info.brdcast_rx_cnt = 0; uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; TEST_ESP_OK(mac->get_addr(mac, local_mac_addr)); @@ -148,7 +156,7 @@ TEST_CASE("ethernet broadcast transmit", "[ethernet_l2]") printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2], local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]); - TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &recv_info)); + TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &s_recv_info)); TEST_ESP_OK(esp_eth_start(eth_handle)); // start Ethernet driver state machine EventBits_t bits = 0; @@ -156,7 +164,7 @@ TEST_CASE("ethernet broadcast transmit", "[ethernet_l2]") TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); // if DUT is connected in network with switch: even if link is indicated up, it may take some time the switch // starts switching the associated port (e.g. it runs RSTP at first) - poke_and_wait(eth_handle, NULL, 0, eth_event_rx_group); + poke_and_wait(eth_handle, NULL, 0, NULL, eth_event_rx_group); emac_frame_t *pkt = malloc(1024); pkt->proto = htons(TEST_ETH_TYPE); @@ -199,13 +207,12 @@ TEST_CASE("ethernet recv_pkt", "[ethernet_l2]") TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_state_group)); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); TEST_ASSERT(eth_event_rx_group != NULL); - recv_info_t recv_info = { - .eth_event_group = eth_event_rx_group, - .check_rx_data = true, - .unicast_rx_cnt = 0, - .multicast_rx_cnt = 0, - .brdcast_rx_cnt = 0 - }; + + s_recv_info.eth_event_group = eth_event_rx_group; + s_recv_info.check_rx_data = true; + s_recv_info.unicast_rx_cnt = 0; + s_recv_info.multicast_rx_cnt = 0; + s_recv_info.brdcast_rx_cnt = 0; uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; TEST_ESP_OK(mac->get_addr(mac, local_mac_addr)); @@ -213,7 +220,7 @@ TEST_CASE("ethernet recv_pkt", "[ethernet_l2]") printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2], local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]); - TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &recv_info)); + TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &s_recv_info)); TEST_ESP_OK(esp_eth_start(eth_handle)); // start Ethernet driver state machine EventBits_t bits = 0; @@ -221,7 +228,7 @@ TEST_CASE("ethernet recv_pkt", "[ethernet_l2]") TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); // if DUT is connected in network with switch: even if link is indicated up, it may take some time the switch // starts switching the associated port (e.g. it runs RSTP at first) - poke_and_wait(eth_handle, NULL, 0, eth_event_rx_group); + poke_and_wait(eth_handle, NULL, 0, NULL, eth_event_rx_group); bits = 0; bits = xEventGroupWaitBits(eth_event_rx_group, ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT, @@ -271,26 +278,25 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_state_group)); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); TEST_ASSERT(eth_event_rx_group != NULL); - recv_info_t recv_info = { - .eth_event_group = eth_event_rx_group, - .check_rx_data = false, - .unicast_rx_cnt = 0, - .multicast_rx_cnt = 0, - .brdcast_rx_cnt = 0 - }; + + s_recv_info.eth_event_group = eth_event_rx_group; + s_recv_info.check_rx_data = false; + s_recv_info.unicast_rx_cnt = 0; + s_recv_info.multicast_rx_cnt = 0; + s_recv_info.brdcast_rx_cnt = 0; uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; + uint8_t dest_mac_addr[ETH_ADDR_LEN] = {}; TEST_ESP_OK(mac->get_addr(mac, local_mac_addr)); // test app will parse the DUT MAC from this line of log output printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2], local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]); - TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &recv_info)); + TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &s_recv_info)); // create dummy data packet used for traffic generation emac_frame_t *pkt = calloc(1, 1500); pkt->proto = htons(TEST_ETH_TYPE); - memcpy(pkt->dest, recv_info.dst_mac_addr, ETH_ADDR_LEN); memcpy(pkt->src, local_mac_addr, ETH_ADDR_LEN); printf("EMAC start/stop stress test under heavy Tx traffic\n"); @@ -301,7 +307,8 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); // at first, check that Tx/Rx path works as expected by poking the test script // this also serves as main PASS/FAIL criteria - poke_and_wait(eth_handle, &tx_i, sizeof(tx_i), eth_event_rx_group); + poke_and_wait(eth_handle, &tx_i, sizeof(tx_i), dest_mac_addr, eth_event_rx_group); + memcpy(pkt->dest, dest_mac_addr, ETH_ADDR_LEN); // *** SPI Ethernet modules deviation *** // Rationale: Transmit errors are expected only for internal EMAC since it is possible to try to queue more @@ -319,7 +326,6 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" TEST_ESP_OK(esp_eth_stop(eth_handle)); bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000)); TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); - printf("Ethernet stopped\n"); } printf("EMAC start/stop stress test under heavy Rx traffic\n"); @@ -328,11 +334,11 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" TEST_ESP_OK(esp_eth_start(eth_handle)); // start Ethernet driver state machine bits = xEventGroupWaitBits(eth_event_state_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000)); TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); - poke_and_wait(eth_handle, &rx_i, sizeof(rx_i), eth_event_rx_group); + poke_and_wait(eth_handle, &rx_i, sizeof(rx_i), NULL, eth_event_rx_group); // wait for dummy traffic xEventGroupClearBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT); - recv_info.unicast_rx_cnt = 0; + s_recv_info.unicast_rx_cnt = 0; bits = xEventGroupWaitBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT, true, true, pdMS_TO_TICKS(3000)); TEST_ASSERT((bits & ETH_UNICAST_RECV_BIT) == ETH_UNICAST_RECV_BIT); @@ -341,9 +347,8 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" TEST_ESP_OK(esp_eth_stop(eth_handle)); bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000)); TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); - printf("Recv packets: %d\n", recv_info.unicast_rx_cnt); - TEST_ASSERT_GREATER_THAN_INT32(0, recv_info.unicast_rx_cnt); - printf("Ethernet stopped\n"); + printf("Recv packets: %d\n", s_recv_info.unicast_rx_cnt); + TEST_ASSERT_GREATER_THAN_INT32(0, s_recv_info.unicast_rx_cnt); } free(pkt); @@ -361,3 +366,147 @@ TEST_CASE("ethernet start/stop stress test under heavy traffic", "[ethernet_l2]" vEventGroupDelete(eth_event_rx_group); vEventGroupDelete(eth_event_state_group); } + +#define MAX_HEAP_ALLOCATION_POINTERS (20) +TEST_CASE("heap utilization", "[ethernet_l2]") +{ + esp_eth_mac_t *mac = mac_init(NULL, NULL); + TEST_ASSERT_NOT_NULL(mac); + esp_eth_phy_t *phy = phy_init(NULL); + TEST_ASSERT_NOT_NULL(phy); + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration + esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver + TEST_ESP_OK(esp_eth_driver_install(&config, ð_handle)); // install driver + TEST_ASSERT_NOT_NULL(eth_handle); + extra_eth_config(eth_handle); + + TEST_ESP_OK(esp_event_loop_create_default()); + EventBits_t bits = 0; + EventGroupHandle_t eth_event_state_group = xEventGroupCreate(); + TEST_ASSERT(eth_event_state_group != NULL); + TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_state_group)); + EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); + TEST_ASSERT(eth_event_rx_group != NULL); + + s_recv_info.eth_event_group = eth_event_rx_group; + s_recv_info.check_rx_data = false; + s_recv_info.unicast_rx_cnt = 0; + s_recv_info.multicast_rx_cnt = 0; + s_recv_info.brdcast_rx_cnt = 0; + + uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; + TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, local_mac_addr)); + // test app will parse the DUT MAC from this line of log output + printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2], + local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]); + + TEST_ESP_OK(esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, &s_recv_info)); + +// *** W5500 deviation *** +// Rationale: W5500 SPI Ethernet module does not support internal loopback +#if !CONFIG_TARGET_ETH_PHY_DEVICE_W5500 + // --------------------------------------- + // Loopback greatly simplifies the test !! + // --------------------------------------- + bool loopback_en = true; + TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_S_PHY_LOOPBACK, &loopback_en)); +#endif + + // start the driver + TEST_ESP_OK(esp_eth_start(eth_handle)); + // wait for connection start + bits = xEventGroupWaitBits(eth_event_state_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT); + // wait for connection establish + bits = xEventGroupWaitBits(eth_event_state_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); + + // create test frame + emac_frame_t *test_pkt = calloc(1, ETH_MAX_PACKET_SIZE); + test_pkt->proto = htons(TEST_ETH_TYPE); + memcpy(test_pkt->dest, local_mac_addr, ETH_ADDR_LEN); // our addr so the frame is not filtered at loopback by MAC + memcpy(test_pkt->src, local_mac_addr, ETH_ADDR_LEN); + // fill with data + for (int i = 0; i < ETH_MAX_PAYLOAD_LEN; i++) { + test_pkt->data[i] = i & 0xFF; + } + +// *** W5500 deviation *** +// Rationale: W5500 SPI Ethernet module does not support internal loopback so we need to loop frames back at test PC side +#if CONFIG_TARGET_ETH_PHY_DEVICE_W5500 + uint8_t dest_mac_addr[ETH_ADDR_LEN] = {}; + poke_and_wait(eth_handle, NULL, 0, dest_mac_addr, eth_event_rx_group); + memcpy(test_pkt->dest, dest_mac_addr, ETH_ADDR_LEN); // overwrite destination address with test PC addr +#endif + + uint16_t transmit_size; + size_t free_heap = 0; + uint8_t *memory_p[MAX_HEAP_ALLOCATION_POINTERS] = { 0 }; + int32_t mem_block; + ESP_LOGI(TAG, "Allocate all heap"); + for (mem_block = 0; mem_block < MAX_HEAP_ALLOCATION_POINTERS; mem_block++) { + free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT); + ESP_LOGD(TAG, "free heap: %i B", free_heap); + memory_p[mem_block] = malloc(free_heap); + if (free_heap < 1024) { + break; + } + } + free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT); + ESP_LOGI(TAG, "remaining free heap: %i B", free_heap); + TEST_ASSERT_LESS_OR_EQUAL_INT(1024, free_heap); + transmit_size = ETH_MAX_PAYLOAD_LEN; + ESP_LOGI(TAG, "Verify that the driver is able to recover from `no mem` error"); + + // define number of iteration to fill device internal buffer (if driver's flush function didn't work as expected) + int32_t max_i = 10; // default value will be overwritten by module specific value +// *** Ethernet modules deviation *** +// Rationale: Each Ethernet module has different size of Rx buffer +#if CONFIG_TARGET_USE_INTERNAL_ETHERNET + max_i = CONFIG_ETH_DMA_RX_BUFFER_NUM + 2; +#elif CONFIG_TARGET_ETH_PHY_DEVICE_W5500 + max_i = W5500_RX_MEM_SIZE / ETH_MAX_PACKET_SIZE + 2; +#elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051 + max_i = DM9051_RX_MEM_SIZE / ETH_MAX_PACKET_SIZE + 2; +#elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL + max_i = KSZ8851SNL_RX_MEM_SIZE / ETH_MAX_PACKET_SIZE + 2; +#endif + + for (int32_t i = 0; i < max_i; i++) { // be sure to fill all the descriptors + ESP_LOGI(TAG, "transmit frame size: %" PRIu16 ", i = %" PRIi32, transmit_size, i); + xEventGroupClearBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT); + s_recv_info.brdcast_rx_cnt = 0; + TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); + // wait for dummy traffic + bits = xEventGroupWaitBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT, true, true, pdMS_TO_TICKS(200)); + TEST_ASSERT(bits == 0); // we don't received the frame due to "no mem" + } + ESP_LOGI(TAG, "Free previously allocated heap"); + while(mem_block > 0) { + free(memory_p[mem_block]); + mem_block--; + } + free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT); + ESP_LOGI(TAG, "free heap: %i B", free_heap); + for (int32_t i = 0; i < max_i; i++) { + ESP_LOGD(TAG, "transmit frame size: %" PRIu16 ", i = %" PRIi32, transmit_size, i); + xEventGroupClearBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT); + s_recv_info.brdcast_rx_cnt = 0; + TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); + // wait for dummy traffic + bits = xEventGroupWaitBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT, true, true, pdMS_TO_TICKS(200)); + TEST_ASSERT((bits & ETH_UNICAST_RECV_BIT) == ETH_UNICAST_RECV_BIT); // now, we should be able to receive frames again + } + + free(test_pkt); + TEST_ESP_OK(esp_eth_stop(eth_handle)); + + TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); + TEST_ESP_OK(esp_event_loop_delete_default()); + TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle)); + phy->del(phy); + mac->del(mac); + extra_cleanup(); + vEventGroupDelete(eth_event_rx_group); + vEventGroupDelete(eth_event_state_group); +} diff --git a/components/esp_eth/test_apps/pytest_esp_eth.py b/components/esp_eth/test_apps/pytest_esp_eth.py index 921636f441..4c4a98af50 100644 --- a/components/esp_eth/test_apps/pytest_esp_eth.py +++ b/components/esp_eth/test_apps/pytest_esp_eth.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 import contextlib @@ -100,6 +100,25 @@ class EthTestIntf(object): except Exception as e: raise e + def eth_loopback(self, mac: str, pipe_rcv:connection.Connection) -> None: + with self.configure_eth_if(self.eth_type) as so: + so.settimeout(30) + try: + while pipe_rcv.poll() is not True: + try: + eth_frame = Ether(so.recv(1522)) + except Exception as e: + raise e + if mac == eth_frame.src: + eth_frame.dst = eth_frame.src + eth_frame.src = so.getsockname()[4] + so.send(raw(eth_frame)) + else: + logging.warning('Received frame from unexpected source') + logging.warning('Source MAC %s', eth_frame.src) + except Exception as e: + raise e + def ethernet_test(dut: IdfDut) -> None: dut.run_all_single_board_cases(group='ethernet', timeout=980) @@ -163,7 +182,7 @@ def ethernet_l2_test(dut: IdfDut) -> None: # Start/stop under heavy Tx traffic for tx_i in range(10): target_if.recv_resp_poke(dut_mac, tx_i) - dut.expect_exact('Ethernet stopped') + dut.expect_exact('Ethernet Stopped') for rx_i in range(10): target_if.recv_resp_poke(dut_mac, rx_i) @@ -171,7 +190,7 @@ def ethernet_l2_test(dut: IdfDut) -> None: pipe_rcv, pipe_send = Pipe(False) tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, )) tx_proc.start() - dut.expect_exact('Ethernet stopped') + dut.expect_exact('Ethernet Stopped') pipe_send.send(0) # just send some dummy data tx_proc.join(5) if tx_proc.exitcode is None: @@ -180,6 +199,36 @@ def ethernet_l2_test(dut: IdfDut) -> None: dut.expect_unity_test_output(extra_before=res.group(1)) +def ethernet_heap_alloc_test(dut: IdfDut) -> None: + target_if = EthTestIntf(ETH_TYPE) + + dut.expect_exact('Press ENTER to see the list of tests') + dut.write('\n') + dut.expect_exact('Enter test for running.') + dut.write('"heap utilization"') + res = dut.expect(r'DUT PHY: (\w+)') + dut_phy = res.group(1).decode('utf-8') + # W5500 does not support internal loopback, we need to loopback for it + if 'W5500' in dut_phy: + logging.info('Starting loopback server...') + res = dut.expect( + r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})' + ) + dut_mac = res.group(1).decode('utf-8') + pipe_rcv, pipe_send = Pipe(False) + loopback_proc = Process(target=target_if.eth_loopback, args=(dut_mac, pipe_rcv, )) + loopback_proc.start() + + target_if.recv_resp_poke(mac=dut_mac) + dut.expect_exact('Ethernet Stopped') + pipe_send.send(0) # just send some dummy data + loopback_proc.join(5) + if loopback_proc.exitcode is None: + loopback_proc.terminate() + + dut.expect_unity_test_output() + + # ----------- IP101 ----------- @pytest.mark.esp32 @pytest.mark.ethernet @@ -200,6 +249,8 @@ def test_esp_ethernet(dut: IdfDut) -> None: ], indirect=True) def test_esp_emac_hal(dut: IdfDut) -> None: ethernet_int_emac_hal_test(dut) + dut.serial.hard_reset() + ethernet_heap_alloc_test(dut) @pytest.mark.esp32 @@ -274,6 +325,8 @@ def test_esp_eth_w5500(dut: IdfDut) -> None: ethernet_test(dut) dut.serial.hard_reset() ethernet_l2_test(dut) + dut.serial.hard_reset() + ethernet_heap_alloc_test(dut) # ----------- KSZ8851SNL ----------- @@ -287,6 +340,8 @@ def test_esp_eth_ksz8851snl(dut: IdfDut) -> None: ethernet_test(dut) dut.serial.hard_reset() ethernet_l2_test(dut) + dut.serial.hard_reset() + ethernet_heap_alloc_test(dut) # ----------- DM9051 ----------- @@ -300,3 +355,5 @@ def test_esp_eth_dm9051(dut: IdfDut) -> None: ethernet_test(dut) dut.serial.hard_reset() ethernet_l2_test(dut) + dut.serial.hard_reset() + ethernet_heap_alloc_test(dut)