2024-10-01 01:22 AM - edited 2024-10-01 01:29 AM
I am using FreeRTOS and lwIP (callback api) on the STM32F407. I have 1 UDP and 2 TCP ports. After a few hours, the struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL); operation returns NULL. When I checked the ethernetif.c file, I noticed that the ETH_RX_BUFFER_CNT value is statically defined as 12. Do you think I should increase this value? If I increase it, do I also need to modify the ETH_RX_DESC_CNT , ETH_RXBUFNB and etc. values? Below, I’m sharing the current values and the lwipopts file. Thank you in advance for your help
void HAL_ETH_RxAllocateCallback(uint8_t **buff)
{
/* USER CODE BEGIN HAL ETH RxAllocateCallback */
struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL); // THE PROBLEM OCCURS HERE
if (p)
{
/* Get the buff from the struct pbuf address. */
*buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
p->custom_free_function = pbuf_free_custom;
/* Initialize the struct pbuf.
* This must be performed whenever a buffer's allocated because it may be
* changed by lwIP or the app, e.g., pbuf_free decrements ref. */
pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUF_SIZE);
}
else
{
RxAllocStatus = RX_ALLOC_ERROR;
*buff = NULL;
}
/* USER CODE END HAL ETH RxAllocateCallback */
}
/* Definition of the Ethernet driver buffers size and count */
#define ETH_RX_BUF_SIZE 1536 /* buffer size for receive */
#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */
#define ETH_RXBUFNB 4U /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
#define ETH_TXBUFNB 4U /* 4 Tx buffers of size ETH_TX_BUF_SIZE */
/* Exported types ------------------------------------------------------------*/
#ifndef ETH_TX_DESC_CNT
#define ETH_TX_DESC_CNT 4U
#endif /* ETH_TX_DESC_CNT */
#ifndef ETH_RX_DESC_CNT
#define ETH_RX_DESC_CNT 4U
#endif /* ETH_RX_DESC_CNT */
/* Memory Pool Declaration */
#define ETH_RX_BUFFER_CNT 12U
LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
/* STM32CubeMX Specific Parameters (not defined in opt.h) ---------------------*/
/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/
/*----- WITH_RTOS enabled (Since FREERTOS is set) -----*/
#define WITH_RTOS 1
/*----- CHECKSUM_BY_HARDWARE enabled -----*/
#define CHECKSUM_BY_HARDWARE 1
/*-----------------------------------------------------------------------------*/
/* LwIP Stack Parameters (modified compared to initialization value in opt.h) -*/
/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/
/*----- Value in opt.h for LWIP_DHCP: 0 -----*/
#define LWIP_DHCP 1
/*----- Default Value for MEMP_NUM_UDP_PCB: 4 ---*/
#define MEMP_NUM_UDP_PCB 5
/*----- Default Value for MEMP_NUM_TCP_PCB: 5 ---*/
#define MEMP_NUM_TCP_PCB 6
/*----- Value in opt.h for MEM_ALIGNMENT: 1 -----*/
#define MEM_ALIGNMENT 4
/*----- Default Value for MEM_SIZE: 1600 ---*/
#define MEM_SIZE 7000
/*----- Default Value for MEMP_NUM_RAW_PCB: 4 ---*/
#define MEMP_NUM_RAW_PCB 0
/*----- Default Value for MEMP_NUM_TCP_PCB_LISTEN: 8 ---*/
#define MEMP_NUM_TCP_PCB_LISTEN 3
/*----- Default Value for MEMP_NUM_IGMP_GROUP: 8 ---*/
#define MEMP_NUM_IGMP_GROUP 0
/*----- Default Value for MEMP_NUM_SYS_TIMEOUT: 5 ---*/
#define MEMP_NUM_SYS_TIMEOUT 12
/*----- Default Value for MEMP_NUM_NETBUF: 2 ---*/
#define MEMP_NUM_NETBUF 0
/*----- Default Value for MEMP_NUM_NETCONN: 4 ---*/
#define MEMP_NUM_NETCONN 0
/*----- Default Value for MEMP_NUM_TCPIP_MSG_API: 8 ---*/
#define MEMP_NUM_TCPIP_MSG_API 12
/*----- Default Value for MEMP_NUM_SELECT_CB: 4 ---*/
#define MEMP_NUM_SELECT_CB 0
/*----- Default Value for MEMP_NUM_TCPIP_MSG_INPKT: 8 ---*/
#define MEMP_NUM_TCPIP_MSG_INPKT 30
/*----- Default Value for MEMP_NUM_NETDB: 1 ---*/
#define MEMP_NUM_NETDB 0
/*----- Default Value for MEMP_NUM_LOCALHOSTLIST: 1 ---*/
#define MEMP_NUM_LOCALHOSTLIST 0
/*----- Default Value for PBUF_POOL_SIZE: 16 ---*/
#define PBUF_POOL_SIZE 14
/*----- Default Value for PBUF_LINK_HLEN: 14 ---*/
#define PBUF_LINK_HLEN 15
/*----- Default Value for PBUF_POOL_BUFSIZE: 592 ---*/
#define PBUF_POOL_BUFSIZE 1514
/*----- Value in opt.h for LWIP_ETHERNET: LWIP_ARP || PPPOE_SUPPORT -*/
#define LWIP_ETHERNET 1
/*----- Default Value for ETHARP_SUPPORT_STATIC_ENTRIES: 0 ---*/
#define ETHARP_SUPPORT_STATIC_ENTRIES 1
/*----- Value in opt.h for LWIP_DNS_SECURE: (LWIP_DNS_SECURE_RAND_XID | LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT) -*/
#define LWIP_DNS_SECURE 7
/*----- Default Value for TCP_MSS: 536 ---*/
#define TCP_MSS 1460
/*----- Default Value for TCP_SND_BUF: 2920 ---*/
#define TCP_SND_BUF 5840
/*----- Default Value for TCP_SND_QUEUELEN: 17 ---*/
#define TCP_SND_QUEUELEN 16
/*----- Default Value for TCP_WND_UPDATE_THRESHOLD: 1460 ---*/
#define TCP_WND_UPDATE_THRESHOLD 5840
/*----- Value in opt.h for TCPIP_THREAD_STACKSIZE: 0 -----*/
#define TCPIP_THREAD_STACKSIZE 2048
/*----- Value in opt.h for TCPIP_THREAD_PRIO: 1 -----*/
#define TCPIP_THREAD_PRIO osPriorityNormal
/*----- Value in opt.h for TCPIP_MBOX_SIZE: 0 -----*/
#define TCPIP_MBOX_SIZE 25
/*----- Value in opt.h for SLIPIF_THREAD_STACKSIZE: 0 -----*/
#define SLIPIF_THREAD_STACKSIZE 2048
/*----- Value in opt.h for SLIPIF_THREAD_PRIO: 1 -----*/
#define SLIPIF_THREAD_PRIO 3
/*----- Value in opt.h for DEFAULT_THREAD_STACKSIZE: 0 -----*/
#define DEFAULT_THREAD_STACKSIZE 1024
/*----- Value in opt.h for DEFAULT_THREAD_PRIO: 1 -----*/
#define DEFAULT_THREAD_PRIO 3
/*----- Default Value for DEFAULT_RAW_RECVMBOX_SIZE: 0 ---*/
#define DEFAULT_RAW_RECVMBOX_SIZE 25
/*----- Value in opt.h for DEFAULT_UDP_RECVMBOX_SIZE: 0 -----*/
#define DEFAULT_UDP_RECVMBOX_SIZE 25
/*----- Value in opt.h for DEFAULT_TCP_RECVMBOX_SIZE: 0 -----*/
#define DEFAULT_TCP_RECVMBOX_SIZE 25
/*----- Value in opt.h for DEFAULT_ACCEPTMBOX_SIZE: 0 -----*/
#define DEFAULT_ACCEPTMBOX_SIZE 25
/*----- Default Value for LWIP_TCPIP_TIMEOUT: 0 ---*/
#define LWIP_TCPIP_TIMEOUT 1
/*----- Value in opt.h for LWIP_SOCKET: 1 -----*/
#define LWIP_SOCKET 0
/*----- Default Value for LWIP_TCP_KEEPALIVE: 0 ---*/
#define LWIP_TCP_KEEPALIVE 1
/*----- Default Value for LWIP_SO_RCVTIMEO: 0 ---*/
#define LWIP_SO_RCVTIMEO 1
/*----- Value in opt.h for RECV_BUFSIZE_DEFAULT: INT_MAX -----*/
#define RECV_BUFSIZE_DEFAULT 2000000000
/*----- Default Value for SO_REUSE: 0 ---*/
#define SO_REUSE 1
/*----- Default Value for LWIP_SOCKET_SELECT: 1 ---*/
#define LWIP_SOCKET_SELECT 0
/*----- Default Value for LWIP_SOCKET_POLL: 1 ---*/
#define LWIP_SOCKET_POLL 0
/*----- Default Value for LWIP_STATS: 0 ---*/
#define LWIP_STATS 1
/*----- Default Value for LWIP_STATS_DISPLAY: 0 ---*/
#define LWIP_STATS_DISPLAY 1
/*----- Value in opt.h for MIB2_STATS: 0 or SNMP_LWIP_MIB2 -----*/
#define MIB2_STATS 0
/*----- Value in opt.h for CHECKSUM_GEN_IP: 1 -----*/
#define CHECKSUM_GEN_IP 0
/*----- Value in opt.h for CHECKSUM_GEN_UDP: 1 -----*/
#define CHECKSUM_GEN_UDP 0
/*----- Value in opt.h for CHECKSUM_GEN_TCP: 1 -----*/
#define CHECKSUM_GEN_TCP 0
/*----- Value in opt.h for CHECKSUM_GEN_ICMP: 1 -----*/
#define CHECKSUM_GEN_ICMP 0
/*----- Value in opt.h for CHECKSUM_GEN_ICMP6: 1 -----*/
#define CHECKSUM_GEN_ICMP6 0
/*----- Value in opt.h for CHECKSUM_CHECK_IP: 1 -----*/
#define CHECKSUM_CHECK_IP 0
/*----- Value in opt.h for CHECKSUM_CHECK_UDP: 1 -----*/
#define CHECKSUM_CHECK_UDP 0
/*----- Value in opt.h for CHECKSUM_CHECK_TCP: 1 -----*/
#define CHECKSUM_CHECK_TCP 0
/*----- Value in opt.h for CHECKSUM_CHECK_ICMP: 1 -----*/
#define CHECKSUM_CHECK_ICMP 0
/*----- Value in opt.h for CHECKSUM_CHECK_ICMP6: 1 -----*/
#define CHECKSUM_CHECK_ICMP6 0
/*-----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
2024-10-23 08:40 AM
Hello @embedtekin ,
Can you share the version of CubeFirmwareF4 you are using to generate this code?
Also, the problem you're facing with LWIP_MEMPOOL_ALLOC(RX_POOL) returning NULL after a few hours indicates that the RX pool is depleted This can happen if the number of pbufs allocated is insufficient for the network traffic your application is handling. Increasing the ETH_RX_BUFFER_CNT value can help mitigate this issue by providing more buffers for incoming packets.
but I can't imagine such traffic intensive network traffic that would be a bottleneck as well unless you are using a random packet generator to stress test your application.
Regards