fix(esp_eth): improved SPI Ethernet _alloc_recv_buf error handling

This commit is contained in:
Ondrej Kosta 2024-02-13 17:17:34 +01:00
parent 7380f96017
commit 41b67e1031
7 changed files with 369 additions and 107 deletions

View File

@ -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 * 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; emac_dm9051_t *emac = (emac_dm9051_t *)arg;
uint8_t status = 0; uint8_t status = 0;
esp_err_t ret;
while (1) { while (1) {
// check if the task receives any notification // check if the task receives any notification
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no 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 */ /* define max expected frame len */
uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint32_t frame_len = ETH_MAX_PACKET_SIZE;
uint8_t *buffer; uint8_t *buffer;
dm9051_alloc_recv_buf(emac, &buffer, &frame_len); if ((ret = dm9051_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) {
/* we have memory to receive the frame of maximal size previously defined */ if (buffer != NULL) {
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; uint32_t buf_len = DM9051_ETH_MAC_RX_BUF_SIZE_AUTO;
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) { 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); dm9051_flush_recv_frame(emac);
free(buffer); 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 { } else if (frame_len) {
ESP_LOGE(TAG, "frame read from module failed"); ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer);
dm9051_flush_recv_frame(emac);
free(buffer);
} }
/* if allocation failed and there is a waiting frame */ } else if (ret == ESP_ERR_NO_MEM) {
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "no mem for receive buffer");
dm9051_flush_recv_frame(emac); dm9051_flush_recv_frame(emac);
} else {
ESP_LOGE(TAG, "unexpected error 0x%x", ret);
} }
} while (emac->packets_remain); } while (emac->packets_remain);
} }

View File

@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
* SPDX-FileContributor: 2021-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2021-2024 Espressif Systems (Shanghai) CO LTD
*/ */
#include <string.h> #include <string.h>
@ -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) static void emac_ksz8851snl_task(void *arg)
{ {
emac_ksz8851snl_t *emac = (emac_ksz8851snl_t *)arg; emac_ksz8851snl_t *emac = (emac_ksz8851snl_t *)arg;
esp_err_t ret;
while (1) { while (1) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
@ -742,31 +743,35 @@ static void emac_ksz8851snl_task(void *arg)
/* define max expected frame len */ /* define max expected frame len */
uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint32_t frame_len = ETH_MAX_PACKET_SIZE;
uint8_t *buffer; uint8_t *buffer;
emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len); if ((ret = emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) {
/* we have memory to receive the frame of maximal size previously defined */ if (buffer != NULL) {
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; uint32_t buf_len = KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO;
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) { 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); emac_ksz8851_flush_recv_queue(emac);
free(buffer); 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 { } else if (frame_len) {
ESP_LOGE(TAG, "frame read from module failed"); ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer);
emac_ksz8851_flush_recv_queue(emac);
free(buffer);
} }
/* if allocation failed and there is a waiting frame */ } else if (ret == ESP_ERR_NO_MEM) {
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "no mem for receive buffer");
emac_ksz8851_flush_recv_queue(emac); emac_ksz8851_flush_recv_queue(emac);
} else {
ESP_LOGE(TAG, "unexpected error 0x%x", ret);
} }
} }
ksz8851_write_reg(emac, KSZ8851_IER, ier); ksz8851_write_reg(emac, KSZ8851_IER, ier);

View File

@ -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 * 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; remain_bytes -= rx_len + 2;
emac->packets_remain = remain_bytes > 0; emac->packets_remain = remain_bytes > 0;
*length = rx_len; *length = copy_len;
return ret; return ret;
err: err:
*length = 0; *length = 0;
@ -700,12 +700,13 @@ static esp_err_t emac_w5500_flush_recv_frame(emac_w5500_t *emac)
// read head first // read head first
ESP_GOTO_ON_ERROR(w5500_read_buffer(emac, &rx_len, sizeof(rx_len), offset), err, TAG, "read frame header failed"); ESP_GOTO_ON_ERROR(w5500_read_buffer(emac, &rx_len, sizeof(rx_len), offset), err, TAG, "read frame header failed");
// update read pointer // 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"); 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 */ /* issue RECV command */
ESP_GOTO_ON_ERROR(w5500_send_command(emac, W5500_SCR_RECV, 100), err, TAG, "issue RECV command failed"); 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 // check if there're more data need to process
rx_len = __builtin_bswap16(rx_len);
remain_bytes -= rx_len; remain_bytes -= rx_len;
emac->packets_remain = remain_bytes > 0; emac->packets_remain = remain_bytes > 0;
} }
@ -731,6 +732,7 @@ static void emac_w5500_task(void *arg)
uint8_t *buffer = NULL; uint8_t *buffer = NULL;
uint32_t frame_len = 0; uint32_t frame_len = 0;
uint32_t buf_len = 0; uint32_t buf_len = 0;
esp_err_t ret;
while (1) { while (1) {
/* check if the task receives any notification */ /* check if the task receives any notification */
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ... if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) == 0 && // if no notification ...
@ -747,29 +749,33 @@ static void emac_w5500_task(void *arg)
do { do {
/* define max expected frame len */ /* define max expected frame len */
frame_len = ETH_MAX_PACKET_SIZE; frame_len = ETH_MAX_PACKET_SIZE;
emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len); if ((ret = emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len)) == ESP_OK) {
/* we have memory to receive the frame of maximal size previously defined */ if (buffer != NULL) {
if (buffer != NULL) { /* we have memory to receive the frame of maximal size previously defined */
buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO; buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO;
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) { if (buf_len == 0) {
free(buffer); free(buffer);
} else if (frame_len > buf_len) { } else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated"); ESP_LOGE(TAG, "received frame was truncated");
free(buffer); 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 { } else {
ESP_LOGD(TAG, "receive len=%u", buf_len); ESP_LOGE(TAG, "frame read from module failed");
/* pass the buffer to stack (e.g. TCP/IP layer) */ free(buffer);
emac->eth->stack_input(emac->eth, buffer, buf_len);
} }
} else { } else if (frame_len) {
ESP_LOGE(TAG, "frame read from module failed"); ESP_LOGE(TAG, "invalid combination of frame_len(%u) and buffer pointer(%p)", frame_len, buffer);
free(buffer);
} }
/* if allocation failed and there is a waiting frame */ } else if (ret == ESP_ERR_NO_MEM) {
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "no mem for receive buffer");
emac_w5500_flush_recv_frame(emac); emac_w5500_flush_recv_frame(emac);
} else {
ESP_LOGE(TAG, "unexpected error 0x%x", ret);
} }
} while (emac->packets_remain); } while (emac->packets_remain);
} }

View File

@ -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 * 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; phy_config->phy_addr = ESP_ETH_PHY_ADDR_AUTO;
#if CONFIG_TARGET_ETH_PHY_DEVICE_IP101 #if CONFIG_TARGET_ETH_PHY_DEVICE_IP101
phy = esp_eth_phy_new_ip101(phy_config); phy = esp_eth_phy_new_ip101(phy_config);
ESP_LOGI(TAG, "DUT PHY: IP101");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720 #elif CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720
phy = esp_eth_phy_new_lan87xx(phy_config); phy = esp_eth_phy_new_lan87xx(phy_config);
ESP_LOGI(TAG, "DUT PHY: LAN8720");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8041 #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8041
phy = esp_eth_phy_new_ksz80xx(phy_config); phy = esp_eth_phy_new_ksz80xx(phy_config);
ESP_LOGI(TAG, "DUT PHY: KSZ8041");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_RTL8201 #elif CONFIG_TARGET_ETH_PHY_DEVICE_RTL8201
phy = esp_eth_phy_new_rtl8201(phy_config); phy = esp_eth_phy_new_rtl8201(phy_config);
ESP_LOGI(TAG, "DUT PHY: RTL8201");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_DP83848 #elif CONFIG_TARGET_ETH_PHY_DEVICE_DP83848
phy = esp_eth_phy_new_dp83848(phy_config); phy = esp_eth_phy_new_dp83848(phy_config);
ESP_LOGI(TAG, "DUT PHY: DP83848");
#endif // CONFIG_TARGET_ETH_PHY_DEVICE_IP101 #endif // CONFIG_TARGET_ETH_PHY_DEVICE_IP101
#elif CONFIG_TARGET_USE_SPI_ETHERNET #elif CONFIG_TARGET_USE_SPI_ETHERNET
phy_config->reset_gpio_num = -1; phy_config->reset_gpio_num = -1;
#if CONFIG_TARGET_ETH_PHY_DEVICE_W5500 #if CONFIG_TARGET_ETH_PHY_DEVICE_W5500
phy = esp_eth_phy_new_w5500(phy_config); phy = esp_eth_phy_new_w5500(phy_config);
ESP_LOGI(TAG, "DUT PHY: W5500");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL
phy = esp_eth_phy_new_ksz8851snl(phy_config); phy = esp_eth_phy_new_ksz8851snl(phy_config);
ESP_LOGI(TAG, "DUT PHY: KSZ8851SNL");
#elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051 #elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051
phy = esp_eth_phy_new_dm9051(phy_config); phy = esp_eth_phy_new_dm9051(phy_config);
ESP_LOGI(TAG, "DUT PHY: DM9051");
#endif // CONFIG_TARGET_ETH_PHY_DEVICE_W5500 #endif // CONFIG_TARGET_ETH_PHY_DEVICE_W5500
#endif // CONFIG_TARGET_USE_INTERNAL_ETHERNET #endif // CONFIG_TARGET_USE_INTERNAL_ETHERNET
return phy; 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; EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
switch (event_id) { switch (event_id) {
case ETHERNET_EVENT_CONNECTED: case ETHERNET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up");
xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT); xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
break; break;
case ETHERNET_EVENT_DISCONNECTED: case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down");
break; break;
case ETHERNET_EVENT_START: case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
xEventGroupSetBits(eth_event_group, ETH_START_BIT); xEventGroupSetBits(eth_event_group, ETH_START_BIT);
break; break;
case ETHERNET_EVENT_STOP: case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped");
xEventGroupSetBits(eth_event_group, ETH_STOP_BIT); xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
break; break;
default: default:

View File

@ -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 * 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_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 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"; static const char *TAG = "esp32_eth_test_hal";
typedef struct typedef struct
@ -123,8 +127,32 @@ TEST_CASE("hal receive/transmit", "[emac_hal]")
test_pkt->data[i] = i & 0xFF; test_pkt->data[i] = i & 0xFF;
} }
// verify that HAL driver correctly processes frame from EMAC descriptors uint16_t transmit_size;
uint16_t transmit_size = CONFIG_ETH_DMA_BUFFER_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); ESP_LOGI(TAG, "transmit frame size: %" PRIu16, transmit_size);
recv_info.expected_size = transmit_size; recv_info.expected_size = transmit_size;
TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size)); TEST_ESP_OK(esp_eth_transmit(eth_handle, test_pkt, transmit_size));

View File

@ -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 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -27,6 +27,11 @@
#define POKE_RESP 0xFB #define POKE_RESP 0xFB
#define DUMMY_TRAFFIC 0xFF #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 typedef struct
{ {
EventGroupHandle_t eth_event_group; EventGroupHandle_t eth_event_group;
@ -38,8 +43,9 @@ typedef struct
bool check_rx_data; bool check_rx_data;
} recv_info_t; } 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; recv_info_t *recv_info = (recv_info_t*)priv;
EventGroupHandle_t eth_event_group = recv_info->eth_event_group; EventGroupHandle_t eth_event_group = recv_info->eth_event_group;
emac_frame_t *pkt = (emac_frame_t *)buffer; 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 } else if (ntohs(pkt->proto) == TEST_CTRL_ETH_TYPE) { // control packet
if (pkt->data[0] == POKE_RESP) { 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"); printf("Poke response received\n");
xEventGroupSetBits(eth_event_group, ETH_POKE_RESP_RECV_BIT); 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, * 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). * 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 // create a control frame to control test flow between the UT and the Python test script
emac_frame_t *ctrl_pkt = calloc(1, 60); 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, EventBits_t bits = xEventGroupWaitBits(eth_event_group, ETH_POKE_RESP_RECV_BIT,
true, true, pdMS_TO_TICKS(WAIT_AFTER_CONN_MS)); true, true, pdMS_TO_TICKS(WAIT_AFTER_CONN_MS));
if ((bits & ETH_POKE_RESP_RECV_BIT) == ETH_POKE_RESP_RECV_BIT) { 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; 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, &eth_event_handler, eth_event_state_group)); TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_state_group));
EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate();
TEST_ASSERT(eth_event_rx_group != NULL); TEST_ASSERT(eth_event_rx_group != NULL);
recv_info_t recv_info = {
.eth_event_group = eth_event_rx_group, s_recv_info.eth_event_group = eth_event_rx_group;
.check_rx_data = false, s_recv_info.check_rx_data = false;
.unicast_rx_cnt = 0, s_recv_info.unicast_rx_cnt = 0;
.multicast_rx_cnt = 0, s_recv_info.multicast_rx_cnt = 0;
.brdcast_rx_cnt = 0 s_recv_info.brdcast_rx_cnt = 0;
};
uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; uint8_t local_mac_addr[ETH_ADDR_LEN] = {};
TEST_ESP_OK(mac->get_addr(mac, local_mac_addr)); 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], 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]); 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 TEST_ESP_OK(esp_eth_start(eth_handle)); // start Ethernet driver state machine
EventBits_t bits = 0; EventBits_t bits = 0;
@ -156,7 +164,7 @@ TEST_CASE("ethernet broadcast transmit", "[ethernet_l2]")
TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); 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 // 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) // 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); emac_frame_t *pkt = malloc(1024);
pkt->proto = htons(TEST_ETH_TYPE); 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, &eth_event_handler, eth_event_state_group)); TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_state_group));
EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate();
TEST_ASSERT(eth_event_rx_group != NULL); TEST_ASSERT(eth_event_rx_group != NULL);
recv_info_t recv_info = {
.eth_event_group = eth_event_rx_group, s_recv_info.eth_event_group = eth_event_rx_group;
.check_rx_data = true, s_recv_info.check_rx_data = true;
.unicast_rx_cnt = 0, s_recv_info.unicast_rx_cnt = 0;
.multicast_rx_cnt = 0, s_recv_info.multicast_rx_cnt = 0;
.brdcast_rx_cnt = 0 s_recv_info.brdcast_rx_cnt = 0;
};
uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; uint8_t local_mac_addr[ETH_ADDR_LEN] = {};
TEST_ESP_OK(mac->get_addr(mac, local_mac_addr)); 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], 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]); 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 TEST_ESP_OK(esp_eth_start(eth_handle)); // start Ethernet driver state machine
EventBits_t bits = 0; EventBits_t bits = 0;
@ -221,7 +228,7 @@ TEST_CASE("ethernet recv_pkt", "[ethernet_l2]")
TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); 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 // 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) // 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 = 0;
bits = xEventGroupWaitBits(eth_event_rx_group, ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT, 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, &eth_event_handler, eth_event_state_group)); TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_state_group));
EventGroupHandle_t eth_event_rx_group = xEventGroupCreate(); EventGroupHandle_t eth_event_rx_group = xEventGroupCreate();
TEST_ASSERT(eth_event_rx_group != NULL); TEST_ASSERT(eth_event_rx_group != NULL);
recv_info_t recv_info = {
.eth_event_group = eth_event_rx_group, s_recv_info.eth_event_group = eth_event_rx_group;
.check_rx_data = false, s_recv_info.check_rx_data = false;
.unicast_rx_cnt = 0, s_recv_info.unicast_rx_cnt = 0;
.multicast_rx_cnt = 0, s_recv_info.multicast_rx_cnt = 0;
.brdcast_rx_cnt = 0 s_recv_info.brdcast_rx_cnt = 0;
};
uint8_t local_mac_addr[ETH_ADDR_LEN] = {}; 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_ESP_OK(mac->get_addr(mac, local_mac_addr));
// test app will parse the DUT MAC from this line of log output // 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], 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]); 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 // create dummy data packet used for traffic generation
emac_frame_t *pkt = calloc(1, 1500); emac_frame_t *pkt = calloc(1, 1500);
pkt->proto = htons(TEST_ETH_TYPE); 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); memcpy(pkt->src, local_mac_addr, ETH_ADDR_LEN);
printf("EMAC start/stop stress test under heavy Tx traffic\n"); 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); TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
// at first, check that Tx/Rx path works as expected by poking the test script // at first, check that Tx/Rx path works as expected by poking the test script
// this also serves as main PASS/FAIL criteria // 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 *** // *** SPI Ethernet modules deviation ***
// Rationale: Transmit errors are expected only for internal EMAC since it is possible to try to queue more // 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)); TEST_ESP_OK(esp_eth_stop(eth_handle));
bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000)); bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
printf("Ethernet stopped\n");
} }
printf("EMAC start/stop stress test under heavy Rx traffic\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 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)); bits = xEventGroupWaitBits(eth_event_state_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT); 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 // wait for dummy traffic
xEventGroupClearBits(eth_event_rx_group, ETH_UNICAST_RECV_BIT); 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)); 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); 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)); TEST_ESP_OK(esp_eth_stop(eth_handle));
bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000)); bits = xEventGroupWaitBits(eth_event_state_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
printf("Recv packets: %d\n", recv_info.unicast_rx_cnt); printf("Recv packets: %d\n", s_recv_info.unicast_rx_cnt);
TEST_ASSERT_GREATER_THAN_INT32(0, recv_info.unicast_rx_cnt); TEST_ASSERT_GREATER_THAN_INT32(0, s_recv_info.unicast_rx_cnt);
printf("Ethernet stopped\n");
} }
free(pkt); 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_rx_group);
vEventGroupDelete(eth_event_state_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, &eth_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, &eth_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);
}

View File

@ -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 # SPDX-License-Identifier: CC0-1.0
import contextlib import contextlib
@ -100,6 +100,25 @@ class EthTestIntf(object):
except Exception as e: except Exception as e:
raise 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: def ethernet_test(dut: IdfDut) -> None:
dut.run_all_single_board_cases(group='ethernet', timeout=980) 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 # Start/stop under heavy Tx traffic
for tx_i in range(10): for tx_i in range(10):
target_if.recv_resp_poke(dut_mac, tx_i) 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): for rx_i in range(10):
target_if.recv_resp_poke(dut_mac, rx_i) 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) pipe_rcv, pipe_send = Pipe(False)
tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, )) tx_proc = Process(target=target_if.traffic_gen, args=(dut_mac, pipe_rcv, ))
tx_proc.start() tx_proc.start()
dut.expect_exact('Ethernet stopped') dut.expect_exact('Ethernet Stopped')
pipe_send.send(0) # just send some dummy data pipe_send.send(0) # just send some dummy data
tx_proc.join(5) tx_proc.join(5)
if tx_proc.exitcode is None: 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)) 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 ----------- # ----------- IP101 -----------
@pytest.mark.esp32 @pytest.mark.esp32
@pytest.mark.ethernet @pytest.mark.ethernet
@ -200,6 +249,8 @@ def test_esp_ethernet(dut: IdfDut) -> None:
], indirect=True) ], indirect=True)
def test_esp_emac_hal(dut: IdfDut) -> None: def test_esp_emac_hal(dut: IdfDut) -> None:
ethernet_int_emac_hal_test(dut) ethernet_int_emac_hal_test(dut)
dut.serial.hard_reset()
ethernet_heap_alloc_test(dut)
@pytest.mark.esp32 @pytest.mark.esp32
@ -274,6 +325,8 @@ def test_esp_eth_w5500(dut: IdfDut) -> None:
ethernet_test(dut) ethernet_test(dut)
dut.serial.hard_reset() dut.serial.hard_reset()
ethernet_l2_test(dut) ethernet_l2_test(dut)
dut.serial.hard_reset()
ethernet_heap_alloc_test(dut)
# ----------- KSZ8851SNL ----------- # ----------- KSZ8851SNL -----------
@ -287,6 +340,8 @@ def test_esp_eth_ksz8851snl(dut: IdfDut) -> None:
ethernet_test(dut) ethernet_test(dut)
dut.serial.hard_reset() dut.serial.hard_reset()
ethernet_l2_test(dut) ethernet_l2_test(dut)
dut.serial.hard_reset()
ethernet_heap_alloc_test(dut)
# ----------- DM9051 ----------- # ----------- DM9051 -----------
@ -300,3 +355,5 @@ def test_esp_eth_dm9051(dut: IdfDut) -> None:
ethernet_test(dut) ethernet_test(dut)
dut.serial.hard_reset() dut.serial.hard_reset()
ethernet_l2_test(dut) ethernet_l2_test(dut)
dut.serial.hard_reset()
ethernet_heap_alloc_test(dut)