Memory bandwidth is precious in embedded systems. In our EmbedOS TCP/IP stack, we were seeing the network layer account for 40% of total CPU time on an ARM Cortex-M4 running at 168MHz — most of it in memcpy calls copying packet data between the DMA buffer and the socket buffer.
The Problem: Three Copies Where Zero Are Needed
In a naive TCP implementation, an incoming packet goes: NIC DMA buffer → driver RX ring → IP layer buffer → TCP layer buffer → application buffer. That's three copies. For a 1500-byte Ethernet frame, that's 4,500 bytes moved on every receive.
The Solution: Scatter-Gather DMA + pbuf Chain
We replaced our contiguous buffer model with a pbuf chain (borrowed from lwIP) and added scatter-gather DMA support in the Ethernet driver. Now the NIC writes packet fragments directly into pre-allocated pbuf nodes, and those same nodes are passed up the stack to the application without any copying.
Results
Peak TCP throughput increased from 38 Mbit/s to 94 Mbit/s on the STM32H7. CPU utilisation for the network stack dropped from 40% to 16%. Latency for small packets (< 200 bytes) decreased by 31%.
0 Comments
Leave a Comment