lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] why is my stack usage so high for the tx thread hosted by l


From: Peter
Subject: [lwip-users] why is my stack usage so high for the tx thread hosted by lwip?
Date: Sun, 18 Sep 2022 20:11:28 +0100

I will be amazed if you find anybody who knows what most of the
options in the lwipopts.h file do *and* is willing to post it. LWIP is
a typical open source piece of code, unsupported and those who
developed it have moved on to more interesting things many years ago.
So the web is full of desperate people posting everywhere asking
questions like this.

I have spent a chunk of my life this year messing about with this
stuff. LWIP does actually work very well, which is just as well
considering the above!

One thing I found is that the buffer config makes practically no
difference to performance in a typical embedded system (i.e. not a
3GHz PC CPU!). So I was able to achieve a big reduction in the memory
usage.

Below is my lwipopts.h file with comments which I believe to be
correct.

/**

******************************************************************************
  * @file    LwIP/LwIP_HTTP_Server_Netconn_RTOS/Inc/lwipopts.h
  * @author  MCD Application Team
  * @brief   lwIP Options Configuration.

******************************************************************************
*
*       This sort of explains the memory usage
* https://lwip-users.nongnu.narkive.com/dkzkPa8l/lwip-memory-settings
*       https://www.cnblogs.com/shangdawei/p/3494148.html
*       https://lwip.fandom.com/wiki/Tuning_TCP
* https://groups.google.com/g/osdeve_mirror_tcpip_lwip/c/lFYJ7Fw0Cxg
*       ST UM1713 document gives an overview of integrating all this.
*
*
*
*
*       7/7/22  PH      MEM_SIZE set to 5k (was 10k). Only ~1.5k is
used.
*       13/7/22 PH      MEMP_MEM_MALLOC=1, MEM_SIZE=16k.
*       14/7/22 PH      MEMP_MEM_MALLOC=0; 1 was unreliable. 8 x 512
byte buffers now.
*       6/8/22  PH      4 x MTU buffers for RX. Done partly for
EditGetData().
*                               MEM_SIZE=6k. 5k is used - see .map
file for ram_heap address.
*                               Sizes of various static RAM structures
determined experimentally.
*
*
*
*
*
*
*
  */
#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__

/**
 * NO_SYS==1: Provides VERY minimal functionality. Otherwise,
 * use lwIP facilities.
 */
#define NO_SYS                          0

// Flag to make LWIP API thread-safe. The netconn and socket APIs are
claimed
// to be thread-safe anyway. The raw API is never thread-safe.
#define LWIP_TCPIP_CORE_LOCKING         1

// This places more objects into the static block defined by MEM_SIZE.
// Uses mem_malloc/mem_free instead of the lwip pool allocator.
// MEM_SIZE now needs to be increased by about 10k.
// It doesn't magically produce extra memory, and causes crashes.
// There is also a performance loss, apparently. AVOID.
#define MEMP_MEM_MALLOC                         0


//NC: Need for sending PING messages by keepalive
#define LWIP_RAW 1
#define DEFAULT_RAW_RECVMBOX_SIZE 4

/*-----------------------------------------------------------------------------*/

/* LwIP Stack Parameters (modified compared to initialization value in
opt.h) -*/
/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/

/*----- Value in opt.h for LWIP_DNS: 0 -----*/
#define LWIP_DNS 1

/* ---------- Memory options ---------- */
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
   lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
   byte alignment -> define MEM_ALIGNMENT to 2. */
#define MEM_ALIGNMENT           4

// MEM_SIZE: the size of the heap memory. This is a statically
allocated block. You can find it
// in the .map file as the symbol ram_heap and you can see how much of
this RAM gets used.
// If MEMP_MEM_MALLOC=0, this holds just the PBUF_ stuff.
// If MEMP_MEM_MALLOC=1 (which is not reliable) this greatly expands
and needs 16k+.
// Empirically this needs to be big enough for at least 4 x
PBUF_POOL_BUFSIZE.
// 6k yields a good speed and going to 8k+ makes a minimal
improvement. The main
// factor affecting speed in the KDE is the poll period in
ethernetif_input().
// This value also limits the biggest block size sent out by
netconn_write. With the
// MEM_SIZE of 6k, the biggest block netconn_write will send out is
4k.
#define MEM_SIZE                (6*1024)

// MEMP_ structures. Their sizes have been determined experimentally,
by
// increasing them and seeing free RAM changing.

/* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
   sends a lot of data out of ROM (or other static memory), this
   should be set high. */
#define MEMP_NUM_PBUF           5       // each 1 is 20 bytes of
static RAM

/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
   per active UDP "connection". */
#define MEMP_NUM_UDP_PCB        6       // each 1 is 32 bytes of
static RAM

/* MEMP_NUM_TCP_PCB: the number of simultaneously active TCP
   connections. */
#define MEMP_NUM_TCP_PCB        5       // each 1 is 145 bytes of
static RAM

/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
   connections. */
#define MEMP_NUM_TCP_PCB_LISTEN 5       // each 1 is 28 bytes of
static RAM

/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
   segments. */
#define MEMP_NUM_TCP_SEG        8       // each 1 is 20 bytes of
static RAM

/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
   timeouts. */
#define MEMP_NUM_SYS_TIMEOUT    10      // each 1 is 16 bytes of
static RAM


/* ---------- Pbuf dynamically allocated buffer blocks  ---------- */

// These settings are probably for RECEIVE only and make negligible
difference
// to performance, which is dominated by the low_level_input poll
period. These
// PBUFs relate directly to the netconn API netbufs etc.

/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
// Statically allocated, so setting this to 2 frees up another 3k of
RAM
#define PBUF_POOL_SIZE           4

/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
// ** It is better to use 4 x MTU than 8 x 512 because in say
EditGetData() you are **
// ** much more likely to get the whole file header in the first
packet. With 512   **
// ** byte packets the CRLFCRLF marker is only ~64 bytes before the
end of the pkt  **
// ** Same issue in UploadGetData where we need to get the whole
header in.         **
#define PBUF_POOL_BUFSIZE        1500 + PBUF_LINK_ENCAPSULATION_HLEN +
PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN

/* --------------------------------------------------------------- */


/* ---------- TCP options ---------- */
#define LWIP_TCP                1
#define TCP_TTL                 255

/* Controls if TCP should queue segments that arrive out of
   order. Define to 0 if your device is low on memory. */
#define TCP_QUEUE_OOSEQ         0

/* TCP Maximum segment size. */
#define TCP_MSS                 (1500 - 40)       /* TCP_MSS =
(Ethernet MTU - IP header size - TCP header size) */

/* TCP sender buffer space (bytes). */
#define TCP_SND_BUF             (4*TCP_MSS)             // no effect
on static RAM

/*  TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at
least
  as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */

#define TCP_SND_QUEUELEN        (2* TCP_SND_BUF/TCP_MSS)

/* TCP advertised receive window. */
// Should be less than PBUF_POOL_SIZE * (PBUF_POOL_BUFSIZE - protocol
headers)
#define TCP_WND                 (2*TCP_MSS)             // no effect
on static RAM


/* ---------- ICMP options ---------- */
#define LWIP_ICMP               1


/* ---------- DHCP options ---------- */
#define LWIP_DHCP               1


/* ---------- UDP options ---------- */
#define LWIP_UDP                1
#define UDP_TTL                 255


/* ---------- Statistics options ---------- */
#define LWIP_STATS 0

/* ---------- link callback options ---------- */
/* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an
interface
 * whenever the link changes (i.e., link down)
 * 8/2022 this is done from the low_level_input RTOS task.
 */
#define LWIP_NETIF_LINK_CALLBACK        0




/*
   --------------------------------------
   ---------- Checksum options ----------
   --------------------------------------
*/

/* 
The STM32F4xx allows computing and verifying the IP, UDP, TCP and ICMP
checksums by hardware:
 - To use this feature let the following define uncommented.
 - To disable it and process by CPU comment the  the checksum.
*/
#define CHECKSUM_BY_HARDWARE 


#ifdef CHECKSUM_BY_HARDWARE
  /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing
IP packets.*/
  #define CHECKSUM_GEN_IP                 0
  /* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing
UDP packets.*/
  #define CHECKSUM_GEN_UDP                0
  /* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing
TCP packets.*/
  #define CHECKSUM_GEN_TCP                0 
  /* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP
packets.*/
  #define CHECKSUM_CHECK_IP               0
  /* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming
UDP packets.*/
  #define CHECKSUM_CHECK_UDP              0
  /* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming
TCP packets.*/
  #define CHECKSUM_CHECK_TCP              0
  /* CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming
ICMP packets.*/  
  #define CHECKSUM_GEN_ICMP               0
#else
  /* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing
IP packets.*/
  #define CHECKSUM_GEN_IP                 1
  /* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing
UDP packets.*/
  #define CHECKSUM_GEN_UDP                1
  /* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing
TCP packets.*/
  #define CHECKSUM_GEN_TCP                1
  /* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP
packets.*/
  #define CHECKSUM_CHECK_IP               1
  /* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming
UDP packets.*/
  #define CHECKSUM_CHECK_UDP              1
  /* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming
TCP packets.*/
  #define CHECKSUM_CHECK_TCP              1
  /* CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming
ICMP packets.*/  
  #define CHECKSUM_GEN_ICMP               1
#endif


/*
   ----------------------------------------------
   ---------- Sequential layer options ----------
   ----------------------------------------------
*/
/**
 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
 */
#define LWIP_NETCONN                    1

/*
   ------------------------------------
   ---------- Socket options ----------
   ------------------------------------
*/
/**
 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
 */
#define LWIP_SOCKET                     1

/*
   ------------------------------------
   ---------- httpd options ----------
   ------------------------------------
*/
/** Set this to 1 to include "fsdata_custom.c" instead of "fsdata.c"
for the
 * file system (to prevent changing the file included in CVS) */
#define HTTPD_USE_CUSTOM_FSDATA   0

/*
   ---------------------------------
   ---------- OS options ----------
   ---------------------------------
*/

#define TCPIP_THREAD_NAME              "TCP/IP"
#define TCPIP_THREAD_STACKSIZE          4096
#define TCPIP_MBOX_SIZE                 6
#define DEFAULT_UDP_RECVMBOX_SIZE       6
#define DEFAULT_TCP_RECVMBOX_SIZE       6
#define DEFAULT_ACCEPTMBOX_SIZE         6
#define DEFAULT_THREAD_STACKSIZE        512
#define TCPIP_THREAD_PRIO               osPriorityHigh

#define LWIP_DEBUG 1

#define IP_DEBUG LWIP_DBG_ON
#define DHCP_DEBUG LWIP_DBG_OFF
#define UDP_DEBUG LWIP_DBG_ON
#define SOCKET_DEBUG_LWIP_DBG_ON
//#define ICMP_DEBUG LWIP_DBG_ON|LWIP_DBG_TRACE
//#define NETIF_DEBUG LWIP_DBG_OFF
#define LWIP_DBG_TYPES_ON  (LWIP_DBG_TRACE|LWIP_DBG_STATE)
#define LWIP_SO_RCVTIMEO        1
#define LWIP_NETIF_HOSTNAME     1

#define SO_REUSE 1

// Defining these produces various errors
//#define LWIP_IPV6                                             1
//#define LWIP_IPV6_DHCP6                               1

#endif /* __LWIPOPTS_H__ */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]