Repository navigation
Give lwIP enough reference pbufs for concurrent UDP sends - #1038
Conversation
Every UDP socket send takes one reference pbuf while it runs: lwip_sendto() wraps the caller's data with netbuf_ref(), which allocates a PBUF_REF from the MEMP_PBUF pool. MEMP_NUM_PBUF was 1, so a send that meets another one in flight in another task (the network target sending for the C64, syslog, the ident responder, a stream) finds the pool empty and fails with ERR_MEM, which the socket reports as errno 12 (ENOMEM). The C64 then sees "SEND ERROR: 12" for a datagram it never got out. Raise it to 8. Each entry is one struct pbuf. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The host test builds lwIP's own pbuf.c and memp.c with static pools, as on the device, and the firmware's MEMP_NUM_PBUF, taken from its lwipopts.h. It takes reference pbufs as netbuf_ref() does for each send: one send while another is in flight, four at once, and many rounds to check that the entries come back. The same cases are built with the single entry the firmware had, and that build has to fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@JC-000 Truly excellent PR description. This is exactly the style that I was looking for, clearly showing what was done, why, what other options were considered, highlights of design, and test summaries. Perfect! I have not yet reviewed this, but wanted to mention how pleased I am. |
There was a problem hiding this comment.
Thanks @JC-000 for tracking this down. The diagnosis is right: in this tree lwip_sendto() takes its pbuf from MEMP_PBUF through netbuf_ref() (sockets.c:1701, LWIP_NETIF_TX_SINGLE_PBUF is 0), and no firmware code allocates PBUF_REF/PBUF_ROM itself. Raising the pool to 8 is a small, low-risk change, and 8 matches MEMP_NUM_NETBUF, so the pbuf pool is never the tighter limit for in-flight sends.
I checked it on a U64 Elite, a C64 Ultimate and a U2+L. For each device, "master" is 4ac0608ee and "PR" is that commit plus the two commits of this PR (built as 5501bb23a), both with Log to Syslog Server pointing at a host. Test: net_target_test.py, one session per run, counting SEND ERROR: 12.
| Device | master (MEMP_NUM_PBUF 1) |
PR (MEMP_NUM_PBUF 8) |
|---|---|---|
| U64 Elite, flashed master, PR loaded over JTAG | 1 session: 5 SEND ERROR: 12, suite FAIL at "WRITE still works and reaches the peer" |
1 session: 0, 143 checks OK |
C64 Ultimate, u64ii loaded over JTAG (volatile) |
2 sessions: 1 SEND ERROR: 12 in each. The suite retries that probe, so both report OK |
2 sessions: 0 in each, 143 checks OK |
U2+L in a C64 Ultimate (u2@c64u), Ethernet |
3 sessions: 0, 74 checks OK each | not flashed (see below) |
Host tests: make -C software/io/network/tests passes. With lwipopts.h set back to 1, ref-pool fails (3 tests), and ref-pool-red fails as required. u64, u64ii and u2pl software-only builds succeed on top of current master.
The U2+L did not reproduce the defect on master in three sessions. This suite reaches it through the cartridge route only (74 checks, no native route), so it has fewer overlapping sends. I did not flash the PR onto it, because a green result there could not be told apart from the master result. The change is in shared configuration and I have no reason to expect a different outcome on that target.
Two points on the test are inline. Neither blocks the change. I did not find a defect in the firmware change itself.
Verdict: the firmware change (MEMP_NUM_PBUF 8) is correct and can merge as it stands. Nothing is required of you.
Suggestions (both inline, neither blocks):
refpool_test.cpp:53: the suite guards a minimum of 4 entries, while the comment names five senders. Either derive the count from that list, or state in the comment that the suite only guards the minimum of 4.lwipopts.h:188: note in the comment that 8 is tied toMEMP_NUM_NETBUF, so that raising one does not leave the other as the limit.
| pbuf_free(other); | ||
| } | ||
|
|
||
| TEST(RefPool, FourSendersAtOnce) |
There was a problem hiding this comment.
This and the other two cases check that lwIP's pool hands out N entries, using the local send_ref(). They do not run lwip_sendto(), so what they pin is MEMP_NUM_PBUF >= 4. The comment at the top of the file and in lwipopts.h names at least five UDP senders (network target, syslog, ident responder, data streamer, debug streamer), and the firmware value is 8. If someone lowers the value to 4, this suite still passes while a fifth concurrent sender can fail with ENOMEM again. Either use a count tied to that list (for example the number of senders named in the comment) or say in the comment that the suite only guards the minimum of 4. As written, a passing run reads as covering more than it does.
| * ENOMEM (io/network/tests, refpool). | ||
| */ | ||
| #define MEMP_NUM_PBUF 1 | ||
| #define MEMP_NUM_PBUF 8 |
There was a problem hiding this comment.
One limit remains: more than 8 UDP sends in flight (each also needs a MEMP_NUM_NETBUF, also 8) still returns ENOMEM to the caller, and the network target reports it to the C64 as SEND ERROR: 12. That is acceptable for the senders listed in the comment. It would help to say in the comment that 8 is tied to MEMP_NUM_NETBUF, so that raising one does not leave the other as the limit.
|
@JC-000 thank you for all the careful work on the network stack, including this one and the 20-session hardware runs. To help get it merged, I'm finishing it up on my side and have assigned the PR to myself. I pushed one commit, a merge of current master into your branch ( |
chrisgleissner
left a comment
There was a problem hiding this comment.
@JC-000 thank you for the fix and for the hardware numbers. I have reviewed it again on the current head and I am merging.
Review: the firmware change is one line, MEMP_NUM_PBUF from 1 to 8 in software/network/config/lwipopts.h, with a comment that says why. Every UDP socket send holds one reference pbuf while it runs, so the network target, syslog, the ident responder and the streams can need one each at the same time. The cost is 168 bytes of .bss on every target. CI is green on this head.
Tests: make -C software/io/network/tests passes all suites after the master merge, including ref-pool (3 tests) and sys-arch (4 tests). ref-pool-red fails 3 tests with a single reference pbuf, as required, and ref-pool also fails when software/network/config/lwipopts.h is set back to 1, so the test reads the firmware's value.
Hardware: on 6 November I ran the UCI network target suite with a syslog sink on the Ultimate 64 Elite and the C64 Ultimate against master and against this change. On the Elite, master logged 5 SEND ERROR: 12 and the suite failed; with the change it logged 0 and all 143 checks passed. On the C64 Ultimate, master logged 1 per session and the change logged 0. That run was on the same one-line change. The only difference since then is a merge of master that touches test targets, so I did not repeat it. The U2+L did not reproduce the error on master, so it cannot be shown red there.
Fixes #1035
Summary
Raise
MEMP_NUM_PBUFinsoftware/network/config/lwipopts.hfrom 1 to 8, with a comment saying why, and add a host test that builds lwIP's ownpbuf.c/memp.cand checks that concurrent UDP sends each get their reference pbuf.Why
Every UDP socket send takes one entry from the
MEMP_PBUFpool while it runs:lwip_sendto()wraps the caller's buffer withnetbuf_ref(), which callspbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF)and returnsERR_MEMwhen the pool is empty. With one entry, a second sender in another task finds the pool empty, and the socket reports errno 12 (ENOMEM). With "Log to Syslog Server" set, the syslog task (onesend()per line) races the UCI network target, and the C64 seesSEND ERROR: 12for a datagram that never left. The data streamer also sends UDP from its own task. This does not need WiFi; it is separate from the routing cause in #862.Alternatives
PBUF_REF, so this would be new code to save about 100 bytes.Technical details
software/network/config/lwipopts.h:MEMP_NUM_PBUF 1->8, plus a comment.MEM_LIBC_MALLOC 1puts the header pbuf on the heap, so only thePBUF_REF/PBUF_ROMentry fromMEMP_PBUFis limited by this value.memp_memory_PBUF_basepool grows from 27 to 195 bytes, +168 bytes (7 more entries at 24 bytes each). The end of.bssmoves by +156 bytes onu64(Nios II) and +168 bytes onu64ii(RISC-V);.textand.rodataare unchanged onu64ii. An entry is 24 bytes, not the 20 one gets from the stockstruct pbuf, because this firmware adds acustom_objpointer to every pbuf (LWIP_PBUF_CUSTOM_DATA).software/io/network/tests/refpool_test.cpp(withlwip_refpool/), targetsref-poolandref-pool-redinsoftware/io/network/tests/Makefile. It compiles lwIP's realpbuf.candmemp.cwith static pools, takes the firmware'sMEMP_NUM_PBUFfromlwipopts.h(the build stops if that line disappears), and takes reference pbufs the waynetbuf_ref()does: one send while another is in flight, four at once, and 100 rounds to check that entries come back.ref-pool-redbuilds the same cases with a single entry and succeeds only if the suite fails, aseth-tx-frame-reddoes. No change to the top-levelMakefileor to the CI workflow is needed: the Ethernet framing CI step already runsmake -C software/io/network/tests, whosealltarget now includes both.Tests
Host (CI image
ghcr.io/gideonz/riscv, the onebuild.ymluses), on this branch:make -C software/io/network/tests(the CI step),MEMP_NUM_PBUF 8ref-pool: 3 test(s) passed;ref-pool-red: 3 test(s) failed with a single reference pbuf, as requiredmake -C software/io/network/tests ref-poolwith the firmware'slwipopts.hset back toMEMP_NUM_PBUF 1(RED)refpool_test.cpp:72and the two others)make host_testson this branchmake host_testson master (03c70e6)No Python changed, so the lint gate does not apply.
Firmware:
make u64andmake u64iibuild, exit 0 each (a Nios II build and a RISC-V build of the main application, where lwIP lives; the ESP32 modules are not touched). Sizes as above.Hardware, both over Ethernet with "Log to Syslog Server" set to a host running a UDP syslog sink. Each session is one run of the UCI network target suite's 13 checks (
tests/e2e/io/command_interface/net_target_test.py), 20 back-to-back sessions, counting sessions whose log containsSEND ERROR: 12. Both devices ran work-in-progress builds that also carried our WiFi fixes and #835 (UDP datagrams above 892 bytes), because that is what was on the bench. I did not run plain master on hardware, so I do not claim a plain-master reproduction; the failing path (NetworkTarget::write_socket()->lwip_send()) is the same on master, and a plain smallWRITE_SOCKETalso failed in the loop, so #835 is not needed to trigger it.MEMP_NUM_PBUF 1(tree: master 60f16dc + WiFi fixes + #835)MEMP_NUM_PBUF 8(tree: master a7d85e4 + WiFi fixes + #835 + this change)SEND ERROR: 12SEND ERROR: 12The two trees differ in the master they sit on (the 8 run is two upstream merges later); the change under test is the one line. Control on the C64U with
MEMP_NUM_PBUF 1, changing only the syslog setting and power cycling between: syslog empty, 5 of 5 sessions clean; syslog on,SEND ERROR: 12in 2 of 5 sessions. Heap before and after a full suite was 5225040 B and 5218656 B, with no upward trend across the failing sessions, so this is not a leak.U2+L: not tested. I have no U2+L; the change is in the shared lwIP configuration, so it applies to every target built from
software/network/config/lwipopts.h.Written with Claude Opus 5.5 in Claude Code.