Skip to content

Experimental iOS USB 3 host-mode transport via usbmuxd (for full-frame models) - #32

Open
moltenglue wants to merge 5 commits into
zoompilot:mainfrom
moltenglue:usbmuxd-transport
Open

moltenglue wants to merge 5 commits into
zoompilot:mainfrom
moltenglue:usbmuxd-transport

Conversation

@moltenglue

@moltenglue moltenglue commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Why this matters for Full Frame models

Today, iOS links put the iPhone in USB Host mode and the comma in composite USB Gadget mode (u_ether CDC-NCM). Even at best on USB 3 SuperSpeed with every AGNOS kernel optimization enabled (UDP JFRM datagrams, core-6 RPS receive steering, NOTRACK conntrack bypass, and 4 MB socket buffers):

  • CDC-NCM best case (UDP JFRM, 393 KB): ~4.3 ms per frame (jetlink/protocol.py, docs/link-protocol.md).
  • CDC-NCM over TCP (393 KB): ~7.35–7.7 ms per frame (~272 TCP segments), degrading to 12.4 ms mean (36–47 ms p90) if receive interrupts contend with real-time threads, plus ~5 ms more on USB 2 (480 Mbps).
  • 4 MiB Full Frame bottleneck (~10.7x larger than 393 KB): Scaling CDC-NCM's best-case ~4.3 ms u_ether stack cost linearly to a ~4 MiB full-frame payload (openpilot 0.11.2+) approaches ~45+ ms on transport alone—consuming nearly the entire 46–50 ms per-frame driving budget before model inference starts.

Reversing USB roles removes u_ether from the hot path:

  • Comma stays in USB Host mode (its default AUX role) and runs usbmuxd (libusb USB 3 bulk pipes).
  • iPhone / iPad stays in standard USB Device mode and binds a public POSIX loopback listener strictly on 127.0.0.1:5599 (zero USBDriverKit, zero MFi hardware, zero private iOS APIs, and no Local Network permission prompt).
  • Wire-speed headroom: Independent iperf3 benchmarks over usbmuxd on USB 3.2 Gen 2 iPhones (Ethan Huang, alongside tzulunyeh/PhotoTransfer) measure 6.12 Gb/s (~765 MB/s) — ~0.5 ms theoretical wire time for 393 KB and ~5.5 ms for 4 MiB (vs. 303 Mb/s / ~111 ms on Apple's stock USB 2.0 cable).

Design: Simple, Opt-In & Non-Destructive

  1. Opt-in toggle (OFF by default): Gated behind Settings → Connection → Full Frame Experimental Network. Standard Jetlink users, CDC-NCM dialing, jetlink/comma/jetlink-root.sh, and Jetlink protocol v3 (JLNK 32-byte header) remain 100% untouched.
  2. Direct /var/run/usbmuxd tunnel (no iproxy copy hop): jetlink/comma/usbmux_transport.py (UsbmuxClient, UsbmuxTransport, UsbmuxReconnector) speaks usbmuxd's lightweight plist protocol directly in stdlib-only Python and hands the upgraded descriptor to StreamTransport — avoiding double-copying multi-megabyte frames through an extra iproxy process.
  3. Zero per-frame iOS allocations: Reuses JetlinkServer's existing TCPListener (TCPListener.loopback(port:) bound strictly to 127.0.0.1:5599), TCP_NODELAY = 1, 4 MB socket buffers, and pre-allocated FrameReader, while suppressing UDP JFRM over loopback (medium != .usb).
  4. Comma provisioning, trust persistence & A/B Variant B:
    • scripts/comma/jetlink-usbmux.sh: Tears down any active gadget (jetlink-root.sh teardown), restores USB Host mode, persists lockdown trust in /data/jetlink/lockdown, verifies Apple VID 05ac negotiated speed (5000/10000 Mbps vs. 480 Mbps USB 2 warnings), and supports an optional --mode3 flag (USBMUXD_DEFAULT_DEVICE_MODE=3 per Synacktiv) for A/B comparison.
    • scripts/bench_link.py & scripts/verify_parity.py: Added --usbmux and synthetic --payload-kb (e.g. --payload-kb 384 / --payload-kb 4096) support for clean cable-to-cable A/B testing.
    • scripts/upstream/: Tracks libimobiledevice/usbmuxd, commaai/agnos-builder (AGNOS 20.6 / agnos-kernel-sdm845 tici_defconfig), and zoompilot/jetlink contracts so upstream changes are caught before breaking.
    • Documentation & attribution: (https://github.com/moltenglue/jetlink/blob/usbmuxd-transport/docs/usbmux-transport.md) (plus minor compiler/actor warning fixes in Transport.swift and ModelStatusLabel.swift).

Verification

Check / Benchmark Result Scope
tests/test_usbmux_transport.py & tests/test_upstream_usbmux.py 17/17 passed Mocked /var/run/usbmuxd, protocol v3 393 KB round-trip, < 1.5 s reconnect, sysfs speed & pairing diagnostics, stdlib -I isolation, live-compatible upstream contracts
JetlinkKitTests & JetlinkServerTests 223/223 passed LinkEvent.isLoopback, LinkMedium (usbmux), loopbackUsbmuxServer (4 MiB payload + golden replay)
swift-format lint --strict & xcodebuild (iOS device) Clean / Build succeeded Zero warnings in modified files
Live jetlink-server loopback (127.0.0.1, bench_link.py) 384 KiB: p50 0.36 ms · p99 0.56 ms
4096 KiB: p50 0.98 ms · p99 1.71 ms
Local macOS loopback (0/90 & 0/32 frames > 46 ms); excludes physical USB wire latency
Live upstream audit (fetch_usbmux_deps.sh + check_usbmux_deps.py) 13/13 [OK] Verified against usbmuxd (78a75a31) & agnos-builder (v20.6, e14707c7)

Hardware status: All CI-equivalent unit tests, Swift/iOS builds, and macOS 127.0.0.1 loopback benchmarks are verified; live comma 3X/four + powered 10 Gbps USB 3 hub + iPhone A/B hardware benchmarks (--usbmux vs. CDC-NCM) are ready to run next.

Add an experimental USB Host-mode transport ('Full Frame Experimental
Network', off by default) that reverses USB roles between the comma and
an iPhone/iPad to bypass the comma's Linux 4.9 u_ether CDC-NCM gadget
bottleneck (~8 ms on 393 KB frames) for full-frame model testing.

- Leave CDC-NCM, jetlink/comma/jetlink-root.sh, and protocol v3 framing
  completely unchanged when the toggle is off.
- Comma side: scripts/comma/jetlink-usbmux.sh (host mode restore,
  persistent /data/jetlink/lockdown pairing, USB 3/USB 2 speed checks,
  optional --mode3 A/B toggle) and direct stdlib-only UsbmuxClient,
  UsbmuxTransport, UsbmuxReconnector, and diagnostics in
  jetlink/comma/usbmux_transport.py (no iproxy subprocess copy hop).
- iOS / JetlinkKit: gate 127.0.0.1:5599 loopback listening behind
  PhoneSettings.fullFrameExperimentalNetwork ('Full Frame Experimental
  Network' in Settings -> Connection) using public POSIX sockets only
  (TCPListener.loopback, LinkEvent.isLoopback, no Local Network prompt),
  with clear usbmuxd / Trust status readouts.
- Benchmarks & tests: --usbmux and --payload-kb support in bench_link.py
  and verify_parity.py, plus comprehensive Python/Swift unit tests and
  docs/usbmux-transport.md.
- Fix pre-existing deprecation and Swift 6 actor-isolation warnings in
  Transport.swift and ModelStatusLabel.swift.
…link

Track external dependencies that affect USB Host-mode usbmuxd transport
and provide repeatable verification, pin updates, and rebase drift checks:

- scripts/upstream/USBMUX_PINS.json: pin tested commits for usbmuxd,
  commaai/agnos-builder (AGNOS 20.6), agnos-kernel-sdm845, and
  zoompilot/jetlink plus watched_jetlink_paths.
- scripts/upstream/fetch_usbmux_deps.sh: sparse blobless read-only fetch
  of usbmuxd headers/C sources, agnos-builder, and tici_defconfig.
- scripts/upstream/check_usbmux_deps.py: 'check' validates usbmuxd wire
  enums/keys/Mode-3/lockdown contracts and AGNOS kernel/rootfs USB config;
  'update-pins' updates USBMUX_PINS.json safely when compatible;
  'sync-jetlink' reports upstream commits and watched-path overlaps.
- scripts/comma/jetlink-usbmux.sh: add 'preflight' (AGNOS VERSION,
  kernel, /data/jetlink/bin persistence, Mode 3 support check).
- .github/workflows/upstream-usbmux.yml & ci.yml: weekly upstream monitoring
  enabled on moltenglue/jetlink with issue alerting and shellcheck CI coverage.
- tests/test_upstream_usbmux.py & docs/usbmux-transport.md: full unit test
  coverage and documentation.
@zephleggett

Copy link
Copy Markdown
Collaborator

Thanks for this. Host mode is an interesting idea, and it's the only way a Lightning iPhone could ever link by cable.

Can't merge it yet though:

  • No hardware numbers. The 0.36 ms is macOS loopback and the 6.12 Gb/s wasn't measured on a comma. Userspace usbmuxd on the comma's 4.9 kernel could be faster or slower than what we have.
  • We send 393 KB a frame, not 4 MiB. The link is about 4.4 ms today, so the most this saves is ~3 ms.
  • It can't drive. Only the bench scripts open the transport, and setup tears down the gadget the owner rebuilds.
  • It needs a powered hub where a direct cable works now, and usbmuxd isn't on AGNOS. apt-get won't survive an update.

Also:

  • CI is red: lint at tests/test_upstream_usbmux.py:168, and test_comma_hold because bench_link.py lost its module-level JetlinkClient.
  • Please drop scripts/upstream/ and the weekly workflow. That belongs in your fork.
  • Move the toggle to developer settings. In Connection it silently stops the cable dial.
  • Leave out the unrelated Swift edits and the percentile rewrite.

What I'd want to see first: bench_link.py --usbmux --payload-kb 384 on a real comma with a USB 3 iPhone, next to the current link, with comma CPU use. If it wins, a small PR with just the transport, the bench flag and a developer toggle would be easy to take.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants