espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.63k stars 7.28k forks source link

Zero-copy `esp_netif_transmit` when prepending headers (IDFGH-13434) #14340

Open redfast00 opened 2 months ago

redfast00 commented 2 months ago

Answers checklist.

General issue report

If you want to make a custom network adaptor with ESP-NETIF, you need to implement three functions:

The signature of the transmit function is esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void *data, size_t len). This function is called by the network stack. The implementation needs to transmit the data frame to the hardware.

A problem I'm currently facing while implementing esp_netif_transmit, is that I need to prepend some headers to the data before transmitting it via DMA. Since I don't control which buffer I'm passed, I can't prepend the headers directly to the data, so I need to allocate a new, bigger buffer, add the headers and copy the data.

Is there a way to control which buffers are passed to esp_netif_transmit? That way I could add some headroom to the buffer.

Alternatively, is there a way to chain buffers before sending them to the 802.11 MAC TX slots? That way, I could have one DMA entry with the headers which points to the next one that has the data.

david-cermak commented 2 months ago

You can use esp_netif_transmit_wrap() and use the last argument as opaque pointer to your DMA buffer struct. the default wlan port passes lwip's pbuf as the network stack buffer (these pbufs support prep-ending and chaining, too)

https://github.com/espressif/esp-idf/blob/8e4454b285ad39881c5bf3f440d8be617a2f18a8/components/esp_netif/lwip/netif/wlanif.c#L92