Skip to content

Give lwIP enough reference pbufs for concurrent UDP sends - #1038

Merged
chrisgleissner merged 3 commits into
GideonZ:masterfrom
JC-000:lwip-refpool/r2
Nov 10, 2026
Merged

chrisgleissner merged 3 commits into
GideonZ:masterfrom
JC-000:lwip-refpool/r2

Conversation

@JC-000

@JC-000 JC-000 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #1035

Summary

Raise MEMP_NUM_PBUF in software/network/config/lwipopts.h from 1 to 8, with a comment saying why, and add a host test that builds lwIP's own pbuf.c/memp.c and checks that concurrent UDP sends each get their reference pbuf.

Why

Every UDP socket send takes one entry from the MEMP_PBUF pool while it runs: lwip_sendto() wraps the caller's buffer with netbuf_ref(), which calls pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF) and returns ERR_MEM when 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 (one send() per line) races the UCI network target, and the C64 sees SEND ERROR: 12 for 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

  • Retry in syslog: fixes only that sender; the network target and the streamer would still lose datagrams.
  • A dedicated pool: lwIP has no per-user pool for PBUF_REF, so this would be new code to save about 100 bytes.
  • 2 or 4: enough for two or three senders; 8 leaves room for sends that overlap for longer (several sockets, a stream running) at a trivial cost.

Technical details

  • software/network/config/lwipopts.h: MEMP_NUM_PBUF 1 -> 8, plus a comment.
  • MEM_LIBC_MALLOC 1 puts the header pbuf on the heap, so only the PBUF_REF/PBUF_ROM entry from MEMP_PBUF is limited by this value.
  • Cost, measured from the linker maps of the main application, master vs this branch: the memp_memory_PBUF_base pool grows from 27 to 195 bytes, +168 bytes (7 more entries at 24 bytes each). The end of .bss moves by +156 bytes on u64 (Nios II) and +168 bytes on u64ii (RISC-V); .text and .rodata are unchanged on u64ii. An entry is 24 bytes, not the 20 one gets from the stock struct pbuf, because this firmware adds a custom_obj pointer to every pbuf (LWIP_PBUF_CUSTOM_DATA).
  • Host test: software/io/network/tests/refpool_test.cpp (with lwip_refpool/), targets ref-pool and ref-pool-red in software/io/network/tests/Makefile. It compiles lwIP's real pbuf.c and memp.c with static pools, takes the firmware's MEMP_NUM_PBUF from lwipopts.h (the build stops if that line disappears), and takes reference pbufs the way netbuf_ref() does: one send while another is in flight, four at once, and 100 rounds to check that entries come back. ref-pool-red builds the same cases with a single entry and succeeds only if the suite fails, as eth-tx-frame-red does. No change to the top-level Makefile or to the CI workflow is needed: the Ethernet framing CI step already runs make -C software/io/network/tests, whose all target now includes both.

Tests

Host (CI image ghcr.io/gideonz/riscv, the one build.yml uses), on this branch:

Run Result
make -C software/io/network/tests (the CI step), MEMP_NUM_PBUF 8 exit 0; ref-pool: 3 test(s) passed; ref-pool-red: 3 test(s) failed with a single reference pbuf, as required
make -C software/io/network/tests ref-pool with the firmware's lwipopts.h set back to MEMP_NUM_PBUF 1 (RED) exit 2; 3 test(s) failed (refpool_test.cpp:72 and the two others)
make host_tests on this branch exit 0
make host_tests on master (03c70e6) exit 0

No Python changed, so the lint gate does not apply.

Firmware: make u64 and make u64ii build, 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 contains SEND 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 small WRITE_SOCKET also failed in the loop, so #835 is not needed to trigger it.

Machine MEMP_NUM_PBUF 1 (tree: master 60f16dc + WiFi fixes + #835) MEMP_NUM_PBUF 8 (tree: master a7d85e4 + WiFi fixes + #835 + this change)
Ultimate 64 Elite (U64E) 13 of 20 sessions with SEND ERROR: 12 0 of 20; every session 13 ok / 0 failed
Ultimate 64 / C64 Ultimate (C64U) 9 of 20 sessions with SEND ERROR: 12 0 of 20; every session 13 ok / 0 failed

The 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: 12 in 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.

JC-000 and others added 2 commits October 30, 2026 14:46
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>
@chrisgleissner chrisgleissner added enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 31, 2026
@chrisgleissner

chrisgleissner commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

@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.

@chrisgleissner chrisgleissner left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

  1. 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.
  2. lwipopts.h:188: note in the comment that 8 is tied to MEMP_NUM_NETBUF, so that raising one does not leave the other as the limit.

pbuf_free(other);
}

TEST(RefPool, FourSendersAtOnce)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thanks for the fix and for the 20-session hardware runs on both machines. I reviewed it and it is ready apart from one thing: #1067 changed software/io/network/tests/Makefile and this branch no longer merges. Please merge or rebase master. I'll merge as soon as it is conflict free.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@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 (2b4462dca). The only conflict was in software/io/network/tests/Makefile: #1067 added the sys-arch targets next to your ref-pool targets, and I kept both. In that directory make now passes all suites, ref-pool 3 tests and sys-arch 4 tests among them, and ref-pool-red and sys-arch-red still fail as required. Nothing else changed. Please say if you would rather handle it differently and I'll revert the commit.

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@chrisgleissner
chrisgleissner merged commit 3db34d2 into GideonZ:master Nov 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lwIP MEMP_NUM_PBUF is 1, so concurrent UDP sends fail with ENOMEM ("SEND ERROR: 12" with syslog on)

2 participants