summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrcitach <[email protected]>2026-10-15 05:04:15 +0000
committerRbb666 <[email protected]>2026-10-15 13:28:57 +0800
commitefc5a9df829902d2d73fe15e09b18fcccc52e9ad (patch)
tree46dee641523ce1ea5c6e5455f3ea074d40a01f47
parent1e24039cf879539d2ac367c8e6423124787ef23d (diff)
[lwip] configure buffer options and preserve PBUF tailroom
-rw-r--r--components/net/lwip/lwip-2.1.2/src/core/pbuf.c33
-rw-r--r--components/net/lwip/port/lwipopts.h50
2 files changed, 75 insertions, 8 deletions
diff --git a/components/net/lwip/lwip-2.1.2/src/core/pbuf.c b/components/net/lwip/lwip-2.1.2/src/core/pbuf.c
index a209e0ca8f..40071728e4 100644
--- a/components/net/lwip/lwip-2.1.2/src/core/pbuf.c
+++ b/components/net/lwip/lwip-2.1.2/src/core/pbuf.c
@@ -271,7 +271,10 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
break;
}
case PBUF_RAM: {
- u16_t payload_len = (u16_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
+ mem_size_t payload_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(offset) + LWIP_MEM_ALIGN_SIZE(length));
+#if PBUF_PAYLOAD_MARGIN_LEN > 0
+ payload_len += LWIP_MEM_ALIGN_SIZE(PBUF_PAYLOAD_MARGIN_LEN);
+#endif
mem_size_t alloc_len = (mem_size_t)(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF) + payload_len);
/* bug #50040: Check for integer overflow when calculating alloc_len */
@@ -440,9 +443,31 @@ pbuf_realloc(struct pbuf *p, u16_t new_len)
&& ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
#endif /* LWIP_SUPPORT_CUSTOM_PBUF */
) {
- /* reallocate and adjust the length of the pbuf that will be split */
- q = (struct pbuf *)mem_trim(q, (mem_size_t)(((u8_t *)q->payload - (u8_t *)q) + rem_len));
- LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
+ size_t trim_len = (size_t)((u8_t *)q->payload - (u8_t *)q);
+#if PBUF_PAYLOAD_MARGIN_LEN > 0
+ size_t margin_len = LWIP_MEM_ALIGN_SIZE((size_t)PBUF_PAYLOAD_MARGIN_LEN);
+#endif
+ /* Keep the original allocation if the aligned trim size would wrap. */
+ int trim_size_ok = (size_t)rem_len <= (size_t)-1 - trim_len;
+#if PBUF_PAYLOAD_MARGIN_LEN > 0
+ trim_size_ok = trim_size_ok &&
+ margin_len <= (size_t)-1 - trim_len - rem_len;
+#endif
+ if (trim_size_ok) {
+ trim_len += rem_len;
+#if PBUF_PAYLOAD_MARGIN_LEN > 0
+ trim_len += margin_len;
+#endif
+ if (trim_len <= (size_t)((mem_size_t)-1) - (MEM_ALIGNMENT - 1U)) {
+ struct pbuf *trimmed;
+ /* reallocate and adjust the length of the pbuf that will be split */
+ trimmed = (struct pbuf *)mem_trim(q, (mem_size_t)trim_len);
+ LWIP_ASSERT("mem_trim returned q == NULL", trimmed != NULL);
+ if (trimmed != NULL) {
+ q = trimmed;
+ }
+ }
+ }
}
/* adjust length fields for new last pbuf */
q->len = rem_len;
diff --git a/components/net/lwip/port/lwipopts.h b/components/net/lwip/port/lwipopts.h
index 424430c665..7d29d79b6b 100644
--- a/components/net/lwip/port/lwipopts.h
+++ b/components/net/lwip/port/lwipopts.h
@@ -282,14 +282,21 @@
//#define MEM_LIBC_MALLOC 1
//#define MEM_USE_POOLS 1
//#define MEMP_USE_CUSTOM_POOLS 1
-//#define MEM_SIZE (1024*64)
+
+#ifdef RT_LWIP_MEM_SIZE
+#define MEM_SIZE RT_LWIP_MEM_SIZE
+#endif
#define MEMP_MEM_MALLOC 0
/* 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. */
+#ifdef RT_LWIP_PBUF_STRUCT_NUM
+#define MEMP_NUM_PBUF RT_LWIP_PBUF_STRUCT_NUM
+#else
#define MEMP_NUM_PBUF 32 //16
+#endif
/* the number of struct netconns */
#ifdef RT_MEMP_NUM_NETCONN
@@ -323,17 +330,44 @@
* setting in the lwip opts.h
*/
/* MEMP_NUM_NETBUF: the number of struct netbufs. */
-// #define MEMP_NUM_NETBUF 2
+#ifdef RT_LWIP_MEMP_NUM_NETBUF
+#define MEMP_NUM_NETBUF RT_LWIP_MEMP_NUM_NETBUF
+#endif
/* MEMP_NUM_NETCONN: the number of struct netconns. */
// #define MEMP_NUM_NETCONN 4
/* MEMP_NUM_TCPIP_MSG_*: the number of struct tcpip_msg, which is used
for sequential API communication and incoming packets. Used in
src/api/tcpip.c. */
-// #define MEMP_NUM_TCPIP_MSG_API 16
-// #define MEMP_NUM_TCPIP_MSG_INPKT 16
+#ifdef RT_LWIP_TCPIP_MSG_API_NUM
+#define MEMP_NUM_TCPIP_MSG_API RT_LWIP_TCPIP_MSG_API_NUM
+#endif
+#ifdef RT_LWIP_TCPIP_MSG_INPKT_NUM
+#define MEMP_NUM_TCPIP_MSG_INPKT RT_LWIP_TCPIP_MSG_INPKT_NUM
+#endif
/* ---------- Pbuf options ---------- */
+
+/* Optional driver-reserved space before and after a PBUF_RAM payload. */
+#ifdef RT_LWIP_PBUF_LINK_ENCAPSULATION_HLEN
+#define PBUF_LINK_ENCAPSULATION_HLEN RT_LWIP_PBUF_LINK_ENCAPSULATION_HLEN
+#endif
+
+#ifdef RT_LWIP_PBUF_PAYLOAD_MARGIN_LEN
+#define PBUF_PAYLOAD_MARGIN_LEN RT_LWIP_PBUF_PAYLOAD_MARGIN_LEN
+#else
+#define PBUF_PAYLOAD_MARGIN_LEN 0
+#endif
+
+/* Only the bundled lwIP 2.1.2 implements PBUF_RAM tailroom allocation. */
+#if PBUF_PAYLOAD_MARGIN_LEN != 0 && !defined(RT_USING_LWIP212)
+#error "PBUF_PAYLOAD_MARGIN_LEN requires the bundled lwIP 2.1.2 (RT_USING_LWIP212)"
+#endif
+
+#ifdef RT_LWIP_NETIF_TX_SINGLE_PBUF
+#define LWIP_NETIF_TX_SINGLE_PBUF 1
+#endif
+
/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
#ifdef RT_LWIP_PBUF_NUM
#define PBUF_POOL_SIZE RT_LWIP_PBUF_NUM
@@ -415,7 +449,11 @@
#define TCPIP_THREAD_STACKSIZE 4096
#endif
#define TCPIP_THREAD_NAME "tcpip"
+#ifdef RT_LWIP_TCP_RECVMBOX_SIZE
+#define DEFAULT_TCP_RECVMBOX_SIZE RT_LWIP_TCP_RECVMBOX_SIZE
+#else
#define DEFAULT_TCP_RECVMBOX_SIZE 10
+#endif
/* ---------- ARP options ---------- */
#define LWIP_ARP 1
@@ -490,7 +528,11 @@
#define LWIP_UDPLITE 0
#define UDP_TTL 255
+#ifdef RT_LWIP_UDP_RECVMBOX_SIZE
+#define DEFAULT_UDP_RECVMBOX_SIZE RT_LWIP_UDP_RECVMBOX_SIZE
+#else
#define DEFAULT_UDP_RECVMBOX_SIZE 1
+#endif
/* ---------- RAW options ---------- */
#ifdef RT_LWIP_RAW