Skip to content

Spike for #807: NET_CMD_WRITE_SOCKET_CHUNK ($16), a payload larger than one command as one datagram - #835

Open
JC-000 wants to merge 8 commits into
GideonZ:masterfrom
JC-000:issue-807-write-socket-chunk
Open

JC-000 wants to merge 8 commits into
GideonZ:masterfrom
JC-000:issue-807-write-socket-chunk

Conversation

@JC-000

@JC-000 JC-000 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

What this is

A spike for #807: NET_CMD_WRITE_SOCKET_CHUNK ($16), which assembles a payload
from several UCI commands and sends it to a socket as one datagram. Today
NET_CMD_WRITE_SOCKET can carry at most 892 bytes, so a larger datagram cannot
be sent at all — splitting it across two commands sends two datagrams, which a
peer expecting one rejects. Receiving already reaches 1472; this makes sending
match.

It is offered as a spike, not for merge as it stands, per @chrisgleissner's note
on #807. The specification it implements is included as
doc/uci_network_chunked_write.md so the design can be reviewed on its own.

Three commits

30a88b4d the E2E tests, which fail on unpatched firmware
8c922633 the firmware that makes them pass
82902682 the specification, as doc/uci_network_chunked_write.md

Evidence

Bench: Ultimate 64 Elite, firmware 3.15, FPGA 124, core 1.4F,
as /v1/info reports it. Both routes, REST and native (6502 agent). The suite
registers 20 scenarios run on both routes — 40 combinations, 20 of them of this
command.

Firmware Result
ed8bc28d, unpatched 20 of 20 chunked combinations fail, every one citing 21,UNKNOWN COMMAND; the 20 pre-existing combinations pass
d33b7802 + this branch 40 of 40 combinations pass, 197 checks

The two rows come from different bases. The branch was rebased from ed8bc28d
onto d33b7802 after the baseline run; the baseline row is a claim about the
absence of this command, and nothing in those seven commits adds it. The spike
row was measured on the base this branch now sits on.

One caveat recorded rather than tidied away: nineteen of the twenty baseline
pre-existing passes came from the suite run. The twentieth,
reset-closes-uci-sockets on the native route, did not — tools/64tass/64tass
was replaced with the tracked Linux binary while the suite was running, so the
native route could not assemble its 6502 agent. It was re-run on its own,
against the same unpatched firmware, and passed with 32 checks.

Baseline run 937 s; patched run 1323 s.

The two properties the command exists for, measured on the wire:

  • a 1472-byte payload carried as 888 + 584 arrived as datagrams [1472] — one
    datagram — the completing chunk replying c0 05, being 1472 little-endian;
  • a 893-byte payload, one byte past what NET_CMD_WRITE_SOCKET can carry,
    arrived as datagrams [893], with nothing on the wire after the first chunk.

Verified by an independent client, and adopted

c64-wireguard implemented this command in its own adapter — a second client on
unrelated code — and ran it on the same bench.

  • Echo sweep 8/8: 888, 889, 891, 892, 893, 1452, 1472 bytes each left as exactly
    one wire datagram of the right length; 1473 refused before reaching the device.
  • §2.1 asserted from a 6510-side debug-bus trace: a non-completing part leaves
    the response count at zero and the buffer untouched, answering 00,OK; only
    the completing part writes the 2-byte total.
  • Standard-MTU tunnel run, 48 MHz, peer left at its default 1420 MTU with no
    per-peer configuration: 60/60 checks, twice. Outbound 888-1472 bytes each
    one datagram; inbound 892, 893, 1452, 1472 bytes received whole.
  • Against Cloudflare WARP's production WireGuard edge: handshake in 48.5 s
    and 48.4 s, ICMP answered through the tunnel, and a 1278-byte DNS response
    received whole over the internet. Sizes above 1280 are capped by WARP's own
    MTU, not by this command.

That work is merged (c64-wireguard PR #112), so the first consumer has
shipped support rather than holding a branch.

Byte weight

Measured baseline vs patched at ed8bc28d with make u2_rv_swonly:

Size Free Free %
ed8bc28d 743.1 KiB 48.9 KiB 6.2 %
+ this branch 743.6 KiB 48.4 KiB 6.1 %

+504 bytes on the U2 application image, of a 792 KiB partition. The U64 image
grows by 524 bytes and its size gate stays PASS at 20.5 % free.

The one design decision worth arguing about

Each chunk carries an explicit offset rather than being positioned by an
accumulator the target keeps. It costs two bytes per chunk, taking the largest
chunk from 890 to 888. It buys the failure of two silent-corruption paths that
the implicit design has:

  • A C64 reset does not reset the command interface. With an implicit accumulator
    a payload left in progress is silently continued by the next program's chunks,
    and the handle does not save you — lwip's alloc_socket is a first-fit from
    zero, so the low descriptors are reissued and a stale handle names a live
    socket.
  • NET_CMD_READ_SOCKET between two chunks discards the accumulation, and every
    command still answers 00,OK, so a client cannot see that it happened.

Appendix B of the specification sets both out with the code they rest on.

Command number

0x12 is not free. xlar54/ultimateii-dos-lib defines the TCP listener commands
at 0x12-0x15 and its u-echoserver.c sample sends $03 $12. This uses
0x16. See Appendix C.

Not in this PR

While splitting write_socket I found that NET_CMD_WRITE_SOCKET has no
minimum-length guard: a two-byte command $03 $11 gives length = -1, which
reaches lwip_send's size_t as 0xFFFFFFFF. It predates this work and is
reachable without ever sending $16, so it is filed separately rather than
bundled here.

🤖 Generated with Claude Code

@JC-000

JC-000 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased onto 883f608d, re-measured, unchanged in content. The code patch is byte-identical to what was reviewed; only Appendix D moved.

uci-net-target at d33b7802: 40/40 combinations, 197 checks. At 883f608d: 40/40, 197 checks. Both UI transports, 0 failures, via ./run-tests --profile deep -s uci-net-target. The bench moved FPGA 124 → 125 between the runs, so that is the same result on different gateware rather than a repeated one.

Separately, the c64-wireguard lane ran its live Cloudflare WARP interop test against this image (a474a7ed, FPGA 125): 4 handshakes reached ACTIVE, including 2 rekeys with hs_timestamp strictly increasing, plus ping, chat and a 1278 B / ancount 15 DNS reply through the tunnel — the chunked $16 send path against a real peer, end to end, matching its reference on the previous image. Reproducible with seed 775774406; PRG sha256 d4dfb981…5d41e262 (msg_port 9999) and 1920eb23…b47ada9b (msg_port 53), both BACKEND=uci REU=0 UCI_CHUNKED_WRITE=1.

Neither result alone separates a firmware fault from a WireGuard one; together they say the rebase changed nothing on either side. Both are claims of parity with the previous image, not a general clearance of 883f608d.

JC-000 and others added 3 commits September 4, 2026 05:24
WRITE_SOCKET sends whatever one command carries, and a command carries at
most 895 bytes because command_protocol.vhd stops command_pointer on the
last byte of the buffer. A UDP payload above 892 therefore leaves as
several datagrams, which a peer expecting one rejects.

Six scenarios for a WRITE_SOCKET_CHUNK that announces a total, carries an
explicit offset per chunk, and sends once when the payload is whole: one
datagram rather than several, the 1472 ceiling accepted and 1473 refused,
chunks refused for a wrong offset, total or handle, commands too short to
hold a header, and a payload in progress discarded by an abort and by a
C64 reset.

The oracles were checked against a simulator of the contract with the
defence under test removed, one variant at a time, so each fails for its
own defect rather than passing because the firmware cannot do the wrong
thing. Peer.collect_udp watches the wire for a whole settle window: the
reply to a write is a byte count, and 1000 is 1000 whether it left as one
datagram or as two.

Red on test-merge d2acbd4: 12 of 12 scenario/route combinations fail with
21,UNKNOWN COMMAND, and the 20 pre-existing combinations pass.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
NET_CMD_WRITE_SOCKET_CHUNK ($16) takes a payload across several commands
and hands it to lwip once, so a datagram up to 1472 bytes leaves whole
where WRITE_SOCKET could carry only 892.

    $03 $16 <HANDLE> <OFFSET_LSB> <OFFSET_MSB> <TOTAL_LSB> <TOTAL_MSB> <DATA>

The offset is on the wire rather than accumulated silently. A chunk that
names a position nothing is holding is refused, which is what a client
sees when its payload was discarded by a reset or by any other command
between chunks, and what a duplicated chunk becomes. An implicit counter
would instead have opened a fresh payload and spliced the two together in
the next datagram, reporting 00,OK throughout.

$16 rather than the next free $12: xlar54/ultimateii-dos-lib defines TCP
listener commands at $12 to $15 and ships a sample that sends $03 $12, so
those numbers are spoken for whether or not this firmware implements them.

WRITE_SOCKET and the completing chunk now share send_to_socket(), so both
report a send identically and neither can bypass the ownership gate that
GideonZ#814 added. No new status codes: a total above 1472 is 82, a chunk that
does not fit the payload in progress is 81, and a send error is 12.

Green on a U64 Elite, core 1.4E: 32 of 32 scenario/route combinations
pass, 177 checks, against the same tests that fail 12 of 12 on d2acbd4.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The proposal NET_CMD_WRITE_SOCKET_CHUNK implements, written as the
"Ultimate Command Interface - Network Target" documents are: sections 1
and 2 state constants and required behaviour, and every "because" lives
in an appendix with the file and line it rests on.

The appendices record what was measured and what was not, including the
properties the bench cannot show and the one scenario that cannot fail
on this tree for the reason it exists.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@JC-000
JC-000 force-pushed the issue-807-write-socket-chunk branch from 1653b0a to 9cd2a98 Compare September 4, 2026 10:53
# Conflicts:
#	tests/e2e/io/command_interface/net_target_test.py
@Grrrolf

Grrrolf commented Sep 5, 2026

Copy link
Copy Markdown

unique id 601A96

Be careful with sharing this type of information. It could be considered PII when it can be linked to you as a person.

@chrisgleissner chrisgleissner added 3.16 Targeted for release 3.16. enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. labels Sep 9, 2026
@chrisgleissner
chrisgleissner changed the base branch from test-merge to master September 13, 2026 12:42
@chrisgleissner

Copy link
Copy Markdown
Collaborator

Hi @JC-000 ,

just an FYI - the target branch for all future PRs should be master. More details in #616 (comment)

I have already amended this PR here.

Thanks

@JC-000

JC-000 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Rgr, I saw the change but wasn't sure what your flow was so I didn't know if you were graduating the PR or I had missed the original target.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
JC-000 and others added 3 commits November 12, 2026 13:35
…nZ#835)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

# Conflicts:
#	software/io/network/network_target.cc
#	software/io/network/network_target.h
…tomically

IDENTIFY, GET_INTERFACE_COUNT, GET_IPADDR and GET_NETADDR are answered on
the UCI Server task and never reach execute(), so they no longer ended a
payload in progress: chunk, IDENTIFY, completing chunk was accepted and sent.
They now set write_chunk_stale before they answer.

execute() read and then cleared write_chunk_stale in two steps; the UCI
Server task, which runs at a higher priority, could set it in between and
the word was lost. The swap is now inside a critical section.

New host tests: a payload ended by IDENTIFY (failed before this change), by an
abort and by a C64 reset is never completed by the next chunk.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The line numbers now match the worker-task network target, the reset is said
to close the sockets on the worker, and a TCP send that fails after a partial
send is noted to report -1, so restarting the payload at offset zero sends
those bytes again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Merged the current master into this branch. Master now runs socket commands on a worker task and answers socket-free commands on the UCI task, so the chunk handling moved onto the worker. An abort or a C64 reset now marks a partly built payload stale; the next command drops it and never completes it. Quick commands such as IDENTIFY mark it stale too, so any other command still ends a chunked write. Two commits on top of the merge do this, each with a host test that failed first: the quick-command rule, and an atomic read-and-clear of the flag. The doc's line references are updated. It also notes that a TCP send that fails after a partial send reports -1, so restarting the payload at offset 0 would resend bytes. make host_tests passes and the U64 and U64-II firmware build. On an Ultimate 64 Elite running this head (ebc39d8), net_target_test.py --test all --route all passes on both routes, including the chunked-write scenarios, the abort and reset discards, and chunk, IDENTIFY, completing chunk giving 81,INVALID PARAMS with no datagram.

Written with Claude Sonnet 5.5 and Claude Opus 5.5 in Claude Code.

@chrisgleissner chrisgleissner added the network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.16 Targeted for release 3.16. 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. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants