Commit Graph

28 Commits

Author SHA1 Message Date
世界 b9cf48a827 usbip: drop ctx ceremony from leaf APIs, move ExportHost lifecycle ctx into constructor
Export.{Snapshot,LeaseCheck,DeviceInfo}, ExportHost.{Reconcile,FinishImport},
ImportHost.Start, and all 11 exportLedger methods carried ctx params that
implementations never consumed (linux FinishImport now reaches into h.runCtx
internally). UrbTransaction.{Wait,Cancel} did consume ctx, but every call site
passed context.Background(), forcing the reverse pattern in endpoint_darwin
where e.ctx was already cancelled. Cancel becomes synchronous, Wait reads
under the close(t.done) happens-before. ExportHost's runCtx now derives in
newPlatformExportHost so Start() is just precondition-check (linux
ensureKernelPath / darwin no-op), and the three pre-Start nil defences in
Close/Events fall out.
2026-06-09 10:42:34 +08:00
世界 9c8147f234 usbip: enforce broadcast invariant and clone-on-stale to close recurring races
- Route every reserved-state mutation through withInventoryWrite; the
  lease insert path now broadcasts so extended subscribers see the busy
  transition immediately instead of waiting for the next mutation. Folds
  cleanupExpiredLocked changes into the broadcast decision on every
  IssueLease early-reject path. Rename the field to inventory and force
  read/write/write-quiet sites through dedicated accessors so future
  callers cannot bypass the invariant.
- Mirror the linux clone-then-swap pattern in darwinExportHost.Reconcile
  via cloneDarwinExport, so the ledger's unlocked Snapshot / LeaseCheck
  reads never observe a half-mutated stale flag. Documented as
  docs/adr/0001-export-pointer-immutability.md and on the Export
  interface; both hosts now share the applyStaleClones helper.
- Rewrite 27 if (_, )?err := …; err != nil sites to assign-then-check
  per .claude/rules/go-syntax.md.
- Delete parse / builder / tautological tests forbidden by
  .claude/rules/code-test.md (option/usbip_test.go,
  iso_scheduler_test.go, usbhost_darwin_status_test.go).
- Tag nine more usbip files with linux || (darwin && cgo); fixes
  pre-existing windows / android lint failures because the protocol
  types were only consumed by tagged code.
2026-06-09 10:42:32 +08:00
世界 0eae853c5f usbip: unify linux teardown and handoff lifecycle 2026-06-09 10:42:31 +08:00
世界 37065daccf usbip: modernize loops and drop unused test helpers
Address `golangci-lint` modernize and unused warnings:
- replace map copy loops with `maps.Copy`
- collapse `if x > y { x = y }` clamps to `min`/`max`
- switch `for i := 0; i < n; i++` to `for i := range n`
- use `t.Context()` in the relay handoff test
- delete the unused `removeOnce` field and `openBinaryDevice` helper
2026-06-09 10:42:29 +08:00
世界 1a17ea0ba9 usbip: write OP_REP_IMPORT before kernel handoff; surface darwin re-capture
On Linux the server was closing the userspace conn inside
NewServerDataSession (handoff.Start closes h.conn synchronously in
direct-TCP mode), so the subsequent WriteOpRepImport always wrote to a
closed socket and every default-mode import failed. Split DataSession
into prepare + Start: the server now writes the reply on the live conn
and only then calls session.Start() to hand the wire to the kernel.
Close also closes h.conn now so the prepare-then-Close error path
doesn't leak the fd.

On Darwin FinishImport returned false after successfully re-capturing
a pending replacement for a stale busy export, so the server skipped
reconcileAndBroadcast and the ledger kept the device hidden until
another IOKit topology event. Return true to trigger reconcile; the
next Reconcile fast-paths the matching registryID back into the
snapshot.
2026-06-09 10:42:28 +08:00
世界 14778f5961 usbip: inline single-caller wrappers and drop dead code
Inline thin pass-through helpers (< 10 lines, < 3 non-test callers) and
the writeSysfs path wrappers; delete the test-only vhciUsedPorts plus
its singleflight machinery and the never-called clientAssignment.IsActive.
2026-06-09 10:42:27 +08:00
世界 ea57b1c928 usbip: drop low-quality abstractions and mock-based tests
Remove four private single-impl interfaces (usbEventListener,
darwinUSBHostDeviceWatch, darwinServerDataDevice, darwinEndpointStateMachine)
and both function-table DI seams (usbipOps, darwinServerOps); production calls
the underlying functions directly. Inline ~25 single-call helpers and fuse
subordinate routines into their sole caller. Collapse single-field helper
structs into their underlying types. Fold server_linux.go/server_darwin.go
into host_linux.go/host_darwin.go.

Delete change-detector tests per .claude/rules/code-test.md: parse round-trip,
mirror, input-validation, and builder-property suites that never exercised
real syscalls, network, or cgo. Six test files removed; linux_test.go slimmed
to three real-system tests; darwin_integration_test.go retains the six
real-cgo tests. Surviving suite: 16 tests that all exercise real OS APIs or
spawn the official Linux usbip server.

Net: -5100 LoC (~37% of the package).
2026-06-09 10:42:27 +08:00
世界 0a6edefe53 usbip: drop dead code from host-seam refactor
The ExportHost.Reconcile changed bool is computed by both Linux and
Darwin implementations but discarded at the only call site
(reconcileAndBroadcast); the ledger already detects change in
BroadcastIfChanged. linuxImportHost.trackPort had a dead add=true
branch — all callers passed false. darwinExport.Snapshot was setting
StatusReason to the same string as State (tautological). LeaseCheck
for stale captures returned the State name as the reason.

Also trim narrative comments in export_ledger.go that referenced
refactor history or the deleted lease.go file, inline the trivial
checkAvailableUnderLock helper at its only caller, and replace
cloneIsoPackets with slices.Clone.
2026-06-09 10:42:26 +08:00
世界 bee231b9ff usbip: collapse server state into exportLedger
ServerService used to own three mutexes coordinating six pieces of
mutable state (exports, busy, controlState, controlSeq, controlSubs,
LeaseManager). The lock-ordering rule that fixed the import/attach
race in 0c892411a lived only as a comment. Two-mutex (fast/slow)
ledger absorbs all of it; the two mutexes are never held
simultaneously, so the ordering rule becomes a property of the type
instead of a discipline callers must observe. TryReserveForImport
closes the window where two concurrent OpReqImports could both pass
the busy check before either marked busy.
2026-06-09 10:42:26 +08:00
世界 cec39eb00c usbip: unify platform services behind host interfaces
Collapse ServerService and ClientService to one definition each by
hiding Linux vs Darwin behind ExportHost, ImportHost, and Export
seams. Along the way, extract three state machines that previously
lived as scattered fields on the service structs:

- LeaseManager owns its own mutex and closes the
  availability-vs-insert TOCTOU by checking export busy inside Issue
  under the same lock as the insert.
- DataSession gives the three per-import data-plane implementations
  (Linux kernel handoff, Darwin server data session, Darwin virtual
  controller) a uniform Done/Err/Close interface.
- clientAssignment encapsulates the matched/import-all target state
  and exposes ApplyMatched/ApplyAll diffs to ClientService, which
  keeps worker goroutine lifecycle.

Service busy tracking moves off the per-platform serverExport struct
onto ServerService.busy, since it follows the lease/import lifecycle
rather than physical claim/release. linux_test.go is migrated to
construct ServerService and ClientService through the new host
interfaces.
2026-06-09 10:42:26 +08:00
世界 d690a17780 Fix USB/IP import lease and attach races 2026-06-09 10:42:26 +08:00
世界 ff9336435e usbip: fix review findings 2026-06-09 10:42:26 +08:00
世界 35347af96d usbip: use event-driven status updates 2026-06-09 10:42:25 +08:00
世界 09491ff209 Cleanup codes 2026-06-09 10:42:25 +08:00
世界 26bd37a012 Fix USB/IP matched retention and snapshot ordering 2026-06-09 10:42:25 +08:00
世界 0e34a2d088 Fix usbip host bind recovery 2026-06-09 10:42:25 +08:00
世界 9f884487e9 Fix USB/IP handoff and shutdown cleanup 2026-06-09 10:42:24 +08:00
世界 786c7b0138 usbip: harden linux interop teardown 2026-06-09 10:42:24 +08:00
世界 0de8058c5f usbip: address review findings 2026-06-09 10:42:24 +08:00
世界 1b6d586930 fix usbip control resync and darwin bounds 2026-06-09 10:42:23 +08:00
世界 44214c4c55 Fix USB/IP control status updates 2026-06-09 10:42:23 +08:00
世界 0135eaef7c usbip: add extended control protocol 2026-06-09 10:42:23 +08:00
世界 7ab2269998 Fix USB/IP unlink and runtime test gating 2026-06-09 10:42:22 +08:00
世界 1cf178440f usbip: add linux socketpair relay fallback 2026-06-09 10:42:22 +08:00
世界 d408350363 usbip: cover import-all retry refresh 2026-06-09 10:42:21 +08:00
世界 033b905bc6 usbip: harden linux import/export handling 2026-06-09 10:42:21 +08:00
世界 4608c0c656 Add usbip virtual-device interop tests 2026-06-09 10:42:20 +08:00
世界 5e8c224be3 Add usbip tests and auto-load drivers 2026-06-09 10:42:20 +08:00