From f92662566e71238bd6559b23fee37b0728ecb8e7 Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Sun, 14 Mar 2021 13:03:50 +0000 Subject: [PATCH] ESP8266: Make copies of incoming packets if under buffer pressure SDK maintains a pool of 5 buffers. If getting close to the limit, we start making copies to avoid pressure. --- src/esp8266/sdk_lwip/src/netif/etharp.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/esp8266/sdk_lwip/src/netif/etharp.c b/src/esp8266/sdk_lwip/src/netif/etharp.c index 7eb328a..241afd4 100755 --- a/src/esp8266/sdk_lwip/src/netif/etharp.c +++ b/src/esp8266/sdk_lwip/src/netif/etharp.c @@ -1299,6 +1299,12 @@ etharp_request(struct netif *netif, ip_addr_t *ipaddr) } #endif /* LWIP_ARP */ +struct wdevctl { + uint16_t num_free_bufs; +}; + +extern struct wdevctl wDevCtrl; + /** * Process received ethernet frames. Using this function instead of directly * calling ip_input and passing ARP frames through etharp in ethernetif_input, @@ -1373,11 +1379,27 @@ ethernet_input(struct pbuf *p, struct netif *netif) LWIP_ASSERT("Can't move over header in packet", 0); goto free_and_return; } else { + /* + * SDK maintains a pool of 5 buffers. The number of free + 1 + * is the first field in the wDevCtrl struct. When it gets down to 1 + * the SDK prints "LmacRxBlk:1" and drops incoming packets. + * If getting close to the limit, we start making copies to avoid pressure. + */ + if (wDevCtrl.num_free_bufs <= 3) { + struct pbuf *p2 = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); + if (p2 != NULL) { + pbuf_copy(p2, p); + pbuf_free(p); + p = p2; + } else { + goto free_and_return; + } + } /* pass to IP layer */ ip_input(p, netif); } break; - + case PP_HTONS(ETHTYPE_ARP): if (!(netif->flags & NETIF_FLAG_ETHARP)) { goto free_and_return;