Repository navigation
Prefer Ethernet over WiFi on a shared subnet, and answer on the interface that owns the address (#862) - #1018
Conversation
lwIP returns the first netif in netif_list whose subnet holds the destination. netif_add() prepends, and the WiFi netif is added after the wired one, so with both up on one subnet every reply and every unbound connect left by WiFi, including replies to the Ethernet address. Use LWIP_HOOK_IP4_ROUTE_SRC: a destination that is an own address, or a source address that belongs to an up netif, selects that netif; with no source, a wired netif is preferred over a wireless one among those whose subnet holds the destination. Everything else falls through to lwIP unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t fail route-select runs the selection against real lwIP netif and ip4_addr headers and a hand-built netif_list. route-select-red runs the same cases against a model of lwIP's lookup (the first on-link netif in the list) and has to fail, as eth-tx-frame-red does. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The C64 opens TCP connections and a UDP socket through the UCI network target to ports this host binds; the suite reads the address they come from. With Ethernet and WiFi on one subnet it must be the Ethernet one (GideonZ#862). It skips unless the device has two interfaces with an address on a subnet that also holds this host, so a device with WiFi off cannot pass it vacuously. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A device without a second interface on the host's subnet, or without a command interface, made the suite report OK. It now ends with suite_skip, after the setting is restored. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The tests now assert the interface a packet finally leaves by: lwIP's ip4_route_src() composed from the hook, lwIP's on-link lookup and netif_default. The hook comes from software/network/config/lwipopts.h (its LWIP_HOOK lines are extracted by the Makefile) and the prototype from lwip_hooks.h, so removing or misspelling the hook in the firmware stops the tests from building. network_interface.cc now includes lwip_hooks.h so its definition is checked against that prototype. Correct the comment on why the WiFi netif wins. route-select-red now fails 4 of 10 cases; the two that only differed in what the hook alone returns are gone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…eway Rule 2 sent a reply from a netif's address out of that netif even to a destination off its subnet. With no gateway configured there the packet is dropped, where lwIP's own lookup used the default netif's gateway and worked. Apply the rule only when the destination is on that netif's subnet or the netif has a gateway; otherwise fall through as before the change. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Match each interface's MAC from the network target against the ethernet_mac GET /v1/info reports, instead of assuming interface 0 is the wired one. The suite skips with the reason when none matches. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…without ethernet_mac Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
chrisgleissner
left a comment
There was a problem hiding this comment.
The production diff is small, and using the shared lwIP source-routing hook is an architecturally defensible place to implement this policy. I found one reproducible UDP regression and three test/precondition defects that should be corrected before merging; the inline comments give the triggering conditions and concrete changes.
Validation: the existing network host targets pass (13 routing cases, with the expected five failures in the legacy variant). I additionally compiled the pinned lwIP ip4.c/udp.c and supporting core code with the PR hook to check actual UDP send behavior and NULL-source dispatch, and reproduced the absent-command-interface cleanup failure with the real Uci transport over a machine double. This review did not rebuild/deploy firmware or repeat the hardware captures.
|
Hi @JC-000 , many thanks for having raised this very useful PR! I reviewed it and left some comments. Many thanks for addressing them. |
The test model called the hook for every source, but ip4_route_src() only calls it when the source is a pointer; a NULL source takes the on-link lookup first (with the hook configured, lwIP's only NULL caller of ip4_route() is etharp_add_static_entry()). Compile the pinned ip4.c, netif.c, udp.c and what they need instead of re-stating the composition, with the hook lines of the firmware's lwipopts.h, and add cases that tell a NULL source from a pointer to 0.0.0.0. Model a down netif as the firmware leaves it (NETIF_FLAG_UP cleared, LINK_UP kept, address kept). The multicast branches of ip4.c and udp.c stay compiled out (LWIP_IGMP is 0 in the host configuration). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…dress The local-destination shortcut in route_select() overrode the source: a UDP socket bound to the WiFi address and sent to the Ethernet address got the Ethernet netif, and udp_sendto_if() rejects a netif that does not own the socket's local address with ERR_RTE. Apply the shortcut only with no source or when the source is the destination; a different explicit source goes on to the source-owner rule. Regressions through lwIP's own udp_sendto() for both bind directions and both netif_list orders, and cases for the netif_is_up and netif_is_link_up guards. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…sent An address stays configured when its link goes down, so two addresses on the subnet do not mean two working routes: with WiFi down every connection leaves from Ethernet on unpatched firmware too, and with Ethernet down it cannot be the winner. Read link state from the interface rows of the root menu (GET /v1/machine:menu_screen, opening the menu and closing it again if it was closed) and skip when the rows cannot be read or either link is down. The command interface setting reads Enabled on a C64 Ultimate whose $DF1B-$DF1F all read $FF. The suite skipped there, but its cleanup then tried to bring the absent interface to Idle, which release() cannot see ($FF is Data More), so the skip became a failure after a wait. Track whether the registers answer separately from whether the setting was changed, release only a present interface, and hand the setting back in a finally that runs first. Device-free cases drive the real main(), Uci and the menu_screen decode against a DeviceDouble: a retained WiFi address with WiFi down, a retained Ethernet address with Ethernet down, unreadable rows, a menu that will not open, and an absent interface. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…menu path hand_back() restored the setting first, which on a U64 whose Command Interface was Disabled lets go of the registers ($DF1C then reads $FF; c64.cc:331-334), so release() could never see Idle and every run after the presence check failed. Release first, then restore, as net_target_test.py does. The menu the suite opened to read the link rows is closed again whatever happens while it is open, and the read waits for the interface rows rather than the first frame. DeviceDouble can now (opt-in) honour menu_button and report ethernet_mac, so open, read, close and the whole skip path run device-free, including a U64 whose $DF1C reads $FF while the setting is off. The host test for an Ethernet netif without an address now gives it mask 0 as DHCP does, and the explicit-source route is checked in both list orders. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
failure = failure or hand_back(...) short-circuited, so a run that failed (the RED run on unpatched firmware included) never released the interface or restored the setting. Run the cleanup first and keep its result for later. The steps are nested so an interrupt in one still reaches the rest. The peer bind gets its own check, so a failure there has a verdict. Device-free cases: a failed run reports the routing failure, releases the present interface just before the setting goes home, and restores the setting; a release that never reaches Idle fails a skip, and does not replace the run's own failure; the read waits for the interface rows when the first frame has none. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Thanks, all four were right. I've pushed follow-up commits to the branch and updated the PR description.
|
|
@JC-000 Many thanks for the fast fixes! |
Review summaryI found no defect in the change. It does what #862 and the policy in the issue thread ask for, on all three hardware classes I could test, and it builds on every target I tried. Latency to the Ethernet address drops by 15 to 22% on every route measured. Latency to the WiFi address rises by 5 to 14% on most routes when requests to both addresses are interleaved, and is unchanged within noise when only the WiFi address is used. Details are under "Latency" below. The smaller test and maintainability observations in the first version of this comment are addressed by commits 9e5a4a1 and f22da00, described in a separate comment. The acceptance criteria are taken from #862 and the maintainer's comment there (reply on the interface the request arrived on, otherwise prefer Ethernet), since the PR does not reference any other ticket. What was runFirmware under test: this PR at 409142b, built from a clean checkout. The base for comparison is its merge base, 1947533, built the same way. Host tests (
Other checks:
LatencyThe change decides which interface carries a reply, so the cost of each REST call on each address is a direct measurement of it. Each request opens a new TCP connection. The routes were Ultimate 64 Elite. Three alternating rounds of base, then PR, each flashed by JTAG, 100 rounds per route per round, so 300 samples per route and firmware. Ethernet is 192.168.1.13, WiFi is 192.168.1.70, requests to both addresses interleaved:
All ten Ethernet routes are faster, with 95% intervals of about 2 points or less. Eight of ten WiFi routes are slower with an interval that excludes zero; the 16 KB read is unchanged and the file lookup is within noise. WiFi address measured alone (same three rounds, no requests to the Ethernet address): the median changes are -0.3% to +8.7%, mostly +1% to +4%. Two routes ( Ultimate II+L in the C64 Ultimate, one base run and one PR run of 100 rounds each, interleaved: Ethernet (192.168.1.74) is 5% to 15% faster on every route, WiFi (192.168.1.97) is 3% to 22% slower, with the same pattern as on the Ultimate 64 Elite. A WiFi-only pair on this device was not conclusive: medians within -4% to +26% with the larger transfers showing a higher 95th percentile. This device was measured with fewer repetitions than the Ultimate 64 Elite. Reading the WiFi result. Before the change, replies to requests for the Ethernet address also left through the WiFi module. When Ethernet and WiFi requests alternate, the WiFi radio was therefore busy a few milliseconds before each WiFi request. After the change, only WiFi requests use it. Two checks on the PR firmware are consistent with that, and neither proves it:
The difference is about 2 ms. If it is a radio wake-up cost, the stock firmware only avoided it for WiFi requests that closely followed Ethernet requests, and the WiFi path in steady use is unchanged. A client that uses only the WiFi address sees the same latency as before within the noise of these measurements. Which of these explanations is right is not established. To reproduce: tests/perf/rest_path_latency_perf_test.py -H 192.168.1.13 --address 192.168.1.70 \
--label base --json base.json # flash the base build, then the PR build
tests/perf/rest_path_latency_perf_test.py -H 192.168.1.13 --address 192.168.1.70 \
--label pr --json pr.json
tests/perf/rest_path_latency_perf_test.py --compare base.json pr.jsonRepeat each firmware at least three times and add Not verified
State of the devicesThe Ultimate 64 Elite runs the PR firmware built at 9e5a4a1 (the firmware is identical at f22da00) volatile (JTAG) and returns to its flashed firmware on a power cycle. The Ultimate II+L has that same firmware flashed. The C64 Ultimate was power-cycled through its own menu as the last step of each U2+L flash. Command Interface and the C64 Ultimate settings changed by the suite were restored by the runner. |
…y test; document uci-net-route
Follow-up commits pushed to this branchTwo commits are added on top of 409142b: 9e5a4a1 and f22da00. Both are in the test, documentation and header-readability scope. Neither changes route selection. 9e5a4a1
f22da00
Verification on hardwareItem 1, the only one with a behaviour that can fail on a device:
The previous test's result on the base firmware is the one recorded in the assessment comment; it was run on both devices before the change. The UDP failure on base firmware is shown on hardware for the first time by the new test. Item 2 changes no behaviour, so there is no failing hardware run for it and none is claimed. It was checked by the host tests ( Item 3 and the perf test were checked as described above. |
Fixes #862.
Problem
With Ethernet and WiFi up on the same subnet,
ip4_route()returns the first netif innetif_listwhose subnet holds the destination.netif_add()prepends and the WiFi netif is added after the wired one, so WiFi wins every lookup. Measured on hardware (see the issue thread):Change
LWIP_HOOK_IP4_ROUTE_SRC(the only stock lwIP routing hook that sees the source address), implemented byroute_select()insoftware/io/network/route_select.h. Every rule considers only netifs that are up, have link and have an address. In order:udp_sendto_if()returnsERR_RTE;ip4_route_src(NULL, ...)and a plainip4_route()run lwIP's on-link lookup before the hook, and in lwIP onlyetharp_add_static_entry()routes that way;NULL, and lwIP's own lookup runs as before. Off-link destinations with no source still go tonetif_default; sockets bound withSO_BINDTODEVICEnever reach the hook.This is the policy from the issue thread: a reply goes out the interface its request arrived on; anything the device starts, or anything ambiguous, prefers Ethernet. Moving the WiFi netif to the tail of
netif_listwould fix what the device starts, but would send replies for the WiFi address out of Ethernet. Binding individual sockets, as #993 does for the streamers, leaves every other socket on WiFi.The hook is configured in
software/network/config/lwipopts.h, with its prototype inlwip_hooks.h(shared by all targets). The only other firmware change is a two-function adapter innetwork_interface.ccthat tells the hook which netifs are wireless.Known limits
INADDR_ANYhas no source address when it answers, so its replies take rule 3. In the firmware that is the Ident service (UDP port 64,socket_dma.cc): a unicast Ident request to either address is now answered from the Ethernet address (before, both were answered from the WiFi address). Fixing that needs the receiving address inidentThread, which is a separate change.ip4_inputtries every netif) is answered from the WiFi address, through WiFi. Two kinds of reply follow the arrival port instead and never reach the hook:icmp.c);tcp_rst_netif()intcp_in.c). On a C64 Ultimate, two such RSTs from the WiFi address left by Ethernet.link_downand the connections on that address are aborted.netif_listorders. Before, it was looped back when the destination's netif headednetif_list(.81 to .83 with WiFi first, .83 to .81 with Ethernet first). Nothing in the firmware does this.ERR_OKin all four cases and leaves by the source's netif. Master returnedERR_RTEin the two cases where the source's netif was not at the head ofnetif_list. Like TCP, these datagrams go on the wire rather than being looped back; a routing hook cannot change that.Tests
Host (
make -C software/io/network/tests, run bybuild.yml; passes with clang on macOS and gcc 11 on Ubuntu 22.04):route-select: 19 cases. They run against lwIP's ownip4.c,netif.candudp.c, built with the hook lines of the firmware'slwipopts.h(the build stops if the hook is removed).ip4_route_src()returns. The UDP cases go through the realudp_sendto()for both bind directions and also assert its result and the netif the datagram is handed to, or that it was looped back.netif_listis hand-built, in both orders where the order matters. A down netif is modelled as the firmware leaves it: UP cleared, the link flag and a static address kept.route-select-red: the same cases with lwIP built without the hook. The target fails if they all pass; 9 of 19 fail.Not covered:
network_interface.cc;LWIP_IGMPis 0 in the host build);NETIF_FLAG_BROADCAST, soip4_route()'s point-to-point gateway match is live on the host but not on the device.The hook lines are taken from
lwipopts.hby grep, whatever#ifthey sit in.E2E (
./run-tests --suite uci-net-route <device>, deep profile):ethernet_macfromGET /v1/info.ethernet_mac(firmware before v3.15a), or no interface on the subnet has that MAC;GET /v1/machine:menu_screen; the menu has to be at its root);uci-net-route-preconditionruns the skip and cleanup paths device-free.Rig evidence (U64 Elite, Ethernet 10.43.23.81, WiFi joined on the same /24 as 10.43.23.83, capture on the test host's wired port)
At 5399781,
uci-net-routeof that revision:uci-net-routeAt 409142b, with the suite as in this PR. The Command Interface was Disabled before each run, and the suite put it back to Disabled.
route_select.h, the hook lines and the adapter are as at this head:uci-net-route: PASS, 7 checks. The menu showed Link Up for both interfaces, and all 3950 captured frames from the Ethernet address carry the Ethernet MAC.GET /v1/version, 100 requests per address: 100 of 100 answered on each, each with the MAC of the interface that owns the address.None of these hardware checks sends between the device's own two addresses, so the rule-1 change is covered by the host tests only.
Reply path at 5399781, 100 REST requests per address:
C64 Ultimate, REST check only:
Builds:
u64andu64iibuild (rc=0, including their updater images) from a tree whose firmware sources equal this head's. The last commit that changes firmware is e900999. The U2-family targets were not built here; CI builds them.Written with Claude Sonnet 5.5 in Claude Code; reviewed adversarially by separate Claude Opus 5.5 sessions before posting.
🤖 Generated with Claude Code