Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/instructions/qemu.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ accepted. A successful `set_link` answers with nothing; an unknown id answers
`Error: Device '<id>' not found` (QEMU 11.1.2, 7.25.2, `test/integration/link-state.test.ts`).
quickchr exposes it by port name as `link()` / `set --link` (#334).

### Pausing a guest: monitor `stop` keeps carrier, stops the clock, backs up the peer

HMP `stop` holds the vCPUs; QEMU, its monitor and QGA, and every NIC's carrier stay up,
so a link peer sees a hung box, not a cable pull. Grounded on Linux/KVM, RouterOS
7.25.2, QEMU 11.1.2 (#364); quickchr exposes it as `pause()` / `quickchr pause` (#375).

- **The guest clock stops with it (KVM).** Uptime excludes the pause and the guest
clock falls behind by its length (51 s after a 50 s pause) until NTP corrects it.
No RouterOS reboot at 20, 30, 50 or 60 s, including a 30 s pause straight after
boot (#375).
- **HVF (x86, Intel Mac, QEMU 11.1.1): the first `stop` after a boot does not freeze
guest time.** On the first `cont` the guest sees the whole pause at once: 3 and
10 s were harmless, 20 and 30 s made the kernel fail and RouterOS reboot in the
same QEMU process (same PID, `kernel failure in previous boot`). Every later
`stop` in that boot froze the clock like KVM. Suspected: the TSC keeps counting
across the first stop under HVF and the jump trips the soft-lockup limit;
unconfirmed. Not tested on Apple Silicon/TCG. Follow-up: #377.
- **SIGSTOP of QEMU is a different fault.** Guest time jumps forward on SIGCONT, and
with RouterOS's default `/system/watchdog watchdog-timer=yes` a 20 s SIGSTOP rebooted
RouterOS (`kernel failure in previous boot`); 5 s did not, nor did 20 s with
`watchdog-timer=no`. SIGSTOP also freezes the monitor and QGA.
- **Backpressure reaches the peer on both socket transports.** The peer's NIC toward
the paused VM stops transmitting once the socket buffer fills: about 100 frames on
`dgram` (unix datagram), about 2 MB on `listen-connect` (TCP). The peer's guest then
drops at its queue (`tx-queue-drop`). Its monitor (~2 ms), QGA and other links keep
working. After `cont` the backlog (328 frames on dgram, 1574 on TCP in the probe)
arrives within 0.6 s.
- **The paused VM's own channels stay up, its guest does not answer.** The monitor
works throughout, and the QGA chardev socket (QEMU's) still accepts a connection,
but the agent inside the guest cannot reply, so a QGA call waits out its timeout.
quickchr refuses guest requests on a paused machine instead (`MACHINE_PAUSED`).
- **A failed `loadvm` leaves the VM held** (`paused (restore-vm)`); quickchr's
`snapshot.load()` resumes it with `cont` only if the guest was running before.
- **`system_reset` keeps the process and its sockets.** Same PID, every socket link
kept, RouterOS ready again in 12–23 s on KVM. A reset of a held guest (`stop`, or
`-S`) leaves it in `paused (prelaunch)`; `cont` boots it.

### `filter-dump`: a pcap of one NIC, from QEMU

`-object filter-dump,id=<id>,netdev=net<N>,file=<path>[,maxlen=…][,queue=…][,status=on|off]`
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ odd/even-minor scheme chose the tag; it was retired in 0.5.1.)

### Added

- Pause and in-place reset (#375): `chr.pause()` / `quickchr pause <name>` holds the
guest (QEMU monitor `stop`) while QEMU, its sockets and every NIC's carrier stay up —
a hung switch, not a pulled cable. The guest clock stops too, so RouterOS never sees
the hang. `continue()` / `quickchr continue` resumes it. A new `paused` marker in
machine state (not `frozen`) shows in `list` (`(paused)`), the detail view and
`--json`, and is cleared by `continue`, `reset` and `stop`. While it is set,
`rest()`, `exec()` and `qga()` fail at once with the new `MACHINE_PAUSED` code, and
`shutdown` power-cuts immediately. `chr.reset()` / `quickchr reset <name>
[--no-wait] [-T <s>]` is an in-place power cycle (`system_reset`): same QEMU PID and
sockets, RouterOS cold-boots, REST-ready by default; a paused or frozen guest is
resumed by it. Known limitation: on macOS/HVF (x86), the first pause after a boot
that lasts 20 s or more makes the guest reboot on `continue` (#377).

- One-way drop on a NIC that keeps carrier (#369): `chr.fault("ether3", { drop:
"egress" | "ingress" | "both" })`, `null` to clear (and a batch form), and `quickchr
set <m> --fault etherN=egress|ingress|both|none`. Every frame in that direction is
Expand Down
42 changes: 41 additions & 1 deletion DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ A **receive slot** is a per-machine named port for traffic the guest sends to th
- **Separate from forwards.** Stored as `MachineState.receivePorts`, never in `ports`/`extraPorts`: `buildHostfwdString` would otherwise emit a `hostfwd` for it, and `PortMapping.guest` has no meaning. Because there is no QEMU side, a slot can be added or removed on a running machine (contrast #141).
- **Not in the block, for now.** Ports are explicit or registry defaults (`WELL_KNOWN_RECEIVE_PORTS`, kept apart from `WELL_KNOWN_GUEST_PORTS` so `--forward tzsp` cannot mean the wrong direction). The block has no spare offset (auto extras take `+6+i`; Windows IPC takes +6..+8), and receive slots will **not** draw from the extras pool (decided on #57): with one to four extra slots per block, they would mostly spill into a second block. Registry defaults below 1024 are replaced by an unprivileged stand-in (syslog → 5514) so a listener needs no root; `snmp-trap` is absent because RouterOS has no trap-port setting.
- **Collisions** are checked against every machine's port block (whole block, so Windows IPC at +6..+8 is covered), forwards and receive ports, and against in-flight creations: the lock reservation carries the resolved forward hosts and receive ports. Reservations are read **before** `machine.json` — a creation saves before it releases its reservation, so it cannot be missed in both — and `saveMachine()` writes a uniquely named, exclusively created temp file and renames it, so a reader never sees a truncated file; the replacement keeps the old file's mode (and owner, when root saves), and a failed write or rename removes the temp file (a Windows rename refused by a scanner holding the file is retried by `renameWithRetry` for ~1.6 s, then the save fails with `STATE_ERROR` and leaves the old file — never an in-place write, which could publish a partial record the allocator would skip). An explicit `portBase` is checked offset by offset (all ten) against held ports. Blocks ruled out by a reservation do not count against the allocator's 20 live probes. The allocator also checks where auto extras land (`+6+i`, so a fifth is past the block) against reservations. Explicit forwards are checked against other machines' whole blocks. The block allocator skips a block containing any reserved port, mapped or not; an explicit `portBase` or fixed forward that clashes is a `PORT_CONFLICT`. A clash names the next free port. A change (removals + additions) is validated and saved once, under the machine lock then the allocation lock — creation takes them in the other order but never waits on an existing machine's lock, so this cannot deadlock. The change re-reads `machine.json` after the allocation-lock wait and replaces only `receivePorts`; `license()` runs entirely under the machine lock (renew, verification and record), so neither overwrites the other and a `clean` cannot reset the disk between the guest's verification and the record. Because a save can now fail (`STATE_ERROR`), the first save after a spawn stops the new QEMU on failure (`saveSpawnedState()`), and a status refresh treats its save as best effort.
- **Status refreshes patch, not rewrite.** `list`/`get` marking a dead QEMU stopped take the machine lock fail-fast, re-read `machine.json` and change only `status`/`pid`/`frozen` (`markMachineStopped()`); if another operation holds the lock they skip the write (the holder saves its own state). In turn `acquireLockNow` waits up to 2 s for a `status` holder instead of failing a `start` that races a `list`. So they cannot write back a stale record over a receive change. `set --qemu-arg` still rewrites the whole record without the lock (stopped machines only; pre-existing).
- **Status refreshes patch, not rewrite.** `list`/`get` marking a dead QEMU stopped take the machine lock fail-fast, re-read `machine.json` and change only `status`/`pid`/`frozen`/`paused` (`markMachineStopped()`); if another operation holds the lock they skip the write (the holder saves its own state). In turn `acquireLockNow` waits up to 2 s for a `status` holder instead of failing a `start` that races a `list`. So they cannot write back a stale record over a receive change. `set --qemu-arg` still rewrites the whole record without the lock (stopped machines only; pre-existing).

Current consumers are all first-party tikoci projects (centrs, donny, restraml), so a port-scheme migration can be coordinated across them — but `@tikoci/quickchr` is published and public, so a change to the persisted port contract still needs an issue, a CHANGELOG/docs note, and (for the `ChrInstance`/`StartOptions` surface) a deprecation/migration path, not a silent break.

Expand Down Expand Up @@ -1560,6 +1560,46 @@ is `MACHINE_STOPPED`; a wait that never reaches REST is a genuine
and no `savevm` refusal on prelaunch VMs (snapshot is already tricky business;
QEMU accepts or errors on its own).

## Pause and in-place reset (#375)

Two in-process faults for lab topologies, researched in #364: a guest that hangs
while its NICs keep carrier (a hung switch), and a power cycle that keeps QEMU.

- **`pause()` is monitor `stop`, not SIGSTOP.** `stop` freezes the guest clock, so
RouterOS never sees the hang and its watchdog cannot reboot it; the monitor and
QGA keep answering; and it works the same on Windows. SIGSTOP is a different
fault (a hang the watchdog ends with a reboot) and is not offered.
- **`paused` is its own marker, not `frozen`.** `frozen` means QEMU was launched
with `-S` and a gdbstub and the guest never booted; `paused` means a booted guest
was held. Both are live markers cleared with the pid (`finalizeStoppedState()`,
`markMachineStopped()`, `clean()`, a relaunch). `status` stays `"running"`, as for
`frozen`, so every running-machine check works unchanged. `continue()` clears both.
Comment thread
mobileskyfi marked this conversation as resolved.
Resolved
- **Guest requests fail fast on a paused machine.** `rest()`, `exec()` (every
transport) and `qga()` throw `MACHINE_PAUSED` instead of waiting out their own
timeout, reading the marker from disk so a handle made before a `quickchr pause`
in another process sees it. `shutdown()` power-cuts at once and reports `forced`:
the guest cannot act on `/system/shutdown`. The alternative — documenting the
wait — would make every `exec` against a hung switch in a test cost its timeout.
- **`reset()` is `system_reset` on that machine only.** QEMU, its PID and its sockets
stay up, so no link is re-established. It never touches far-end carrier: a lab
that wants the neighbor to see a cable drop calls `link()` on the peers, as
`examples/mlag`'s power-cut step does. **A reset always ends with RouterOS
booting**: QEMU leaves a held guest in `paused (prelaunch)` after `system_reset`,
so `reset()` resumes it and clears both markers. The other choice (reset but stay
held) has no use a `pause()` after the reset doesn't cover, and would make
`reset()`'s REST wait impossible to keep. A reset whose REST wait times out is a
`BOOT_TIMEOUT` with the usual report, but QEMU is left running: stopping it, as
`continue()`'s timeout does, would pull every link the reset exists to keep.
- **Every QEMU spawn starts with fresh markers.** `buildAndSpawnQemu()` clears
`paused` and sets `frozen` from the launch config before any path can record the
new pid or save the record, so neither a foreground exit nor a still-running
spawn failure can carry the previous process's marker.
- **Monitor replies are confirmed, not assumed.** `pause`, `reset` and `continue`
read `info status` and send `stop`/`cont`/`system_reset` with the final prompt
required; an unrecognised status, a close before the prompt, or an `error…` reply
is `PROCESS_FAILED`, and the `paused` marker is set only after QEMU reports the
guest held.

## Managed macOS networks (#256, decided 2026-11-11)

**Root lives in the network, once; machines stay unprivileged.** Every vmnet mode
Expand Down
Loading