Skip to content

Commit

Permalink
ESP8266: Trim TCP segment pbuf before sending
Browse files Browse the repository at this point in the history
Because LWIP_NETIF_TX_SINGLE_PBUF is enabled (and must be enabled,
since lower leel stack can't do scatter/gather), all segments are
preallocated to full MSS, even if the resulting segment is much
smalller. When we are done with the segment and about to send it out,
trim the pbuf memory allocation to exactly the size required.
This is signioficant because pbufs can spend significant time in unacked
queue, and trimming them ASAP reduces peak memory usage when multiple
connections are active.

Signed-off-by: Deomid "rojer" Ryabkov <rojer@rojer.me>
  • Loading branch information
rojer committed Aug 23, 2021
1 parent 1f2af44 commit d42d197
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/esp8266/sdk_lwip/src/core/tcp_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "lwip/snmp.h"
#include "netif/etharp.h"

#include <stdlib.h>
#include <string.h>

#ifdef MEMLEAK_DEBUG
Expand Down Expand Up @@ -1170,6 +1171,24 @@ tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
#endif /* CHECKSUM_GEN_TCP */
TCP_STATS_INC(tcp.xmit);

/*
* If the segment is a single RAM-backed pbuf, trim it to reduce memory usage.
* Note that what we want here is pbuf_realloc but it does not handle the case
* of buffer being relocated (unlikely in practice).
*/
do {
struct pbuf *p = seg->p;
if (p->type != PBUF_RAM || p->next != NULL) break;
uint16_t payload_offset = (((char *) p->payload) - ((char *) p));
uint16_t trimmed_len = LWIP_MEM_ALIGN_SIZE(payload_offset + p->len);
struct pbuf *p2 = (struct pbuf *) realloc(p, trimmed_len);
if (p2 != NULL && p2 != p) {
p2->payload = ((char *) p2) + payload_offset;
seg->p = p2;
seg->tcphdr = p2->payload;
}
} while (0);

#if LWIP_NETIF_HWADDRHINT
ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
IP_PROTO_TCP, &(pcb->addr_hint));
Expand Down

0 comments on commit d42d197

Please sign in to comment.