Commit Graph

15 Commits

Author SHA1 Message Date
世界 dcea94559d usbip: fix E.New(fmt.Sprintf) and banned if-init error checks
Replace E.New(fmt.Sprintf("...0x%04x", v)) with variadic E.New(..., fmt.Sprintf("0x%04x", v)) so plain text and integer args flow through F.ToString (sing-box/common/stun/stun.go precedent). Hoist three sysfs_linux.go if-init error checks per .claude/rules/go-syntax.md ("assign first, then check"); two of them also shadowed the outer err.
2026-06-09 10:42:34 +08:00
世界 8799b10b9a usbip: defer darwin RET_UNLINK until completion; route VHCI through primary; close lease-consume race
Three independent correctness bugs that all violated "decide before
publishing": each surface (wire reply, sysfs path, broadcast state)
committed to a state the code later had to contradict.

darwin: switch IOUSBHostPipe abort to IOUSBHostAbortOptionSynchronous and
add the EP-0 path via abortDeviceRequestsWithOption (previously a silent
no-op for control transfers). After abort returns, await a per-pending
drained channel before writing RET_UNLINK so the late RetSubmit
suppression in finishSubmit has actually taken effect — the drained
channel is allocated lazily by markSubmitUnlinked, so the hot submit
path is unchanged.

linux: collapse the per-controller VHCI abstraction. The kernel exposes
attach/detach/status/status.N only on vhci_hcd.0 with globally unique
port numbers; the previous code wrote vhci_hcd.N/attach for N>0 and
silently failed past the primary controller's port range. Glob status*
on the primary, key reservations on bare port int, and carry the
status-suffix as a diagnostic-only secondary index for Description().

export_ledger: ConsumeLeaseAndReserve no longer publishes busy=true and
then conditionally rolls it back. Read seq under fast first, then make
every decision (including the lease.Generation check) inside one slow
critical section before any busy mutation. This removes the window
where a concurrent BroadcastIfChanged could observe transient busy and
poison l.state with no corrective broadcast on rollback.
2026-06-09 10:42:30 +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
世界 cb9ec99e04 usbip: drain linux stub status on finish-import; iterate vhci controllers 2026-06-09 10:42:29 +08:00
世界 843032af68 usbip: fix darwin re-enum loss, devlist serial drop, control downgrade
Three independent regressions that silently degraded the USB/IP feature:

- Darwin: when a busy device re-enumerated (same busid, new IOUSBHost
  registry ID), Reconcile marked the export stale but never captured
  the replacement, and nothing reconciled after FinishImport — the
  device only reappeared on an unrelated future topology event. Now
  Reconcile records the pending registry ID on the stale entry,
  FinishImport rehydrates inline, and the server triggers a
  reconcileAndBroadcast after release as a safety net.

- DEVLIST: SerialString and entryDeviceKey expected a "\0serial=..."
  trailer in the 256-byte Path field, but encodePathField never wrote
  one, so serial-based matching silently failed against plain usbipd
  peers and against sing-box peers that fell back to DEVLIST polling.
  encodePathField now writes the trailer (wire-compatible with kernel
  usbip-utils, which NUL-terminate the path) and ReadOpRepDevListBody
  populates DeviceEntry.Serial on decode.

- Control: any write/read I/O failure during the CONTROL handshake was
  wrapped as errControlUnsupported, so a single TCP RST or 5s deadline
  on startup permanently downgraded the client to 5-second DEVLIST
  polling. Now only io.EOF on the ACK read and frame-validation
  failures stay unsupported; other I/O failures retry with
  exponential backoff (1s→30s) up to 3 consecutive transients before
  committing to the polling fallback.
2026-06-09 10:42:28 +08:00
世界 a40b8217b3 usbip: inline thin wrappers and drop pass-through darwin session
Apply the <10-LoC / <3-non-test-caller heuristic across the package:
inline writeControlFrame, removeLeaseWaiter, vhciAttach, vhciHubForSpeed,
applyControlSnapshot, applyRemoteEntries, shouldRetryBusID, entrySerial,
and the trivial darwin/linux import-host constructors at their call
sites.

Delete the darwinClientSession wrapper -- darwinVirtualController already
satisfies DataSession, so attach the Description() method to it directly
and return the controller from darwinImportHost.Attach.

Fold the 11-line data_session.go into host.go beside AttachedSession,
which embeds DataSession.
2026-06-09 10:42:27 +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
世界 d690a17780 Fix USB/IP import lease and attach races 2026-06-09 10:42:26 +08:00
世界 09491ff209 Cleanup codes 2026-06-09 10:42:25 +08:00
世界 0e34a2d088 Fix usbip host bind recovery 2026-06-09 10:42:25 +08:00
世界 0de8058c5f usbip: address review findings 2026-06-09 10:42:24 +08:00
世界 5e8c224be3 Add usbip tests and auto-load drivers 2026-06-09 10:42:20 +08:00
世界 323b406971 Fix USB/IP re-enumeration and export availability 2026-06-09 10:42:20 +08:00
世界 bb1ab8080a Add Linux usbip client and server services 2026-06-09 10:42:19 +08:00