Skip to content
Merged
41 changes: 40 additions & 1 deletion .github/instructions/qemu.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,46 @@ from `net/filter*.c` (QEMU master and 11.1.2) and `test/lab/link-impairment/REPO
and QEMU logs nothing. `status` and `queue` switch it at run time. It is 100 % or nothing.
Give each filter its own `null` chardev with a unique id: QEMU 11.1.2 refuses a second
redirector on a chardev already in use. One port's both-ways blackhole needs only one
filter and sink (`queue=all`).
filter and sink (`queue=all`). quickchr exposes it as `fault()` / `set --fault` (#369):
`chardev-add null,id=fault<N>-sink`, then
`object_add filter-redirector,id=fault<N>,netdev=net<N>,queue=rx|tx|all,outdev=fault<N>-sink,position=head`,
with direction changes done by `qom-set /objects/fault<N> queue …`, and clearing by
`object_del` and then `chardev-remove`.
- **HMP replies for the filter and chardev commands** (QEMU 11.1.2): success is an empty
reply for `chardev-add`, `object_add`, `qom-set`, `object_del` and `chardev-remove`.
Refusals are one `Error: …` line: a duplicate id is `attempt to add duplicate property
'<id>' to object (type 'container')` (chardevs and objects have separate namespaces);
an unknown netdev is `Parameter 'netdev' expects a network backend id`; removing a
chardev a filter still uses is `Chardev '<id>' is busy`; a missing one is `Chardev
'<id>' not found` or `object '<id>' not found`. `qom-get /objects/<id> queue` prints the
value as JSON (`"rx"`), or `Device '/objects/<id>' not found`. `qom-list /objects`
and `qom-list /chardevs` print `<id> (child<filter-redirector>)` /
`<id> (child<chardev-null>)` lines. `info network` prints every property of each filter
under its netdev's `filters:` line (` - fault1: type=filter-redirector,vnet_hdr_support=false,indev=,outdev=fault1-sink,position=head,status=on,insert=behind,netdev=net1,queue=all`),
so one command reads every port's state at one instant. quickchr's `fault()` and
`faults()` use it: one monitor command can't interleave with commands from another
connection, though a batch of several commands from another connection can be seen
half-applied.
- **Filter order: the head is the wire side.** QEMU walks a netdev's filters head to tail
for frames delivered to the guest and tail to head for frames the guest sends.
`info network` lists each netdev's filters in chain order, with `position=` and
`queue=`. Filters from `-object` exist before any `object_add`, which appends at the tail
unless given `position=head` (or `position=id=<filter>,insert=before|behind`). Measured
with a boot `filter-dump` and an `object_add`ed `position=head` blackhole on one NIC
(QEMU 11.1.2, KVM, CHR 7.25.2): the capture held the LACPDUs the guest sent into an
egress drop, and none of the peer's under an ingress drop
(`test/integration/link-fault.test.ts`).
- **Windows: the monitor stopped answering after a raw `status off` on a live redirector.**
On windows-latest (QEMU 11.1.0, TCG, `listen-connect` links, two CHRs with an LACP bond),
`qom-set /objects/fault2 status off` on a `queue=all` blackhole succeeded, and so did the
next monitor session. The connection after that was accepted but got no prompt within
5 s, nor within 30 s, in 3 of 3 runs (46782987405, 46784568862, 46795212755). The same
runs passed when the drop was lifted with `object_del`. A probe on an idle CHR with no
traffic on the link saw no hang, including after `status off` (102 connections, all
under 10 ms; run 46794639948). The mechanism is unknown. quickchr never sets `status`
on its fault filters, and `link-fault.test.ts` skips this step on Windows.
- **`info network` doesn't show carrier.** After `set_link <id> off` its netdev line is
unchanged (QEMU 11.1.2), so HMP has no read-back for link state.
- **A `filter-redirector` loop is QEMU's only per-frame hook.** `outdev` writes each frame to
a socket chardev (4-byte big-endian length + frame, the `stream` netdev framing) and
consumes it; `indev` reads frames back and sends them on from the filter's position. Use
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ odd/even-minor scheme chose the tag; it was retired in 0.5.1.)

### Added

- 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
dropped while both ends keep `running=yes`, as with a half-failed optic. `link()`
can't make that fault. QEMU's `filter-redirector` into a `null` chardev is added
and removed at run time, with no root, helper process or reboot, and nothing is
persisted. `chr.faults()`, `list <m>` and `list <m> --json` (`faults`) read the
faults in place back from QEMU. A capture on the same NIC shows the guest's side of
the drop.

- Per-NIC packet capture (#338) with QEMU's `filter-dump`: a NIC option
`capture: true | { start?: "on" | "off"; dir?: string }` (CLI `--add-network
<spec>,capture=on|off,capture-dir=<path>`), set at creation and persisted. Every QEMU
Expand Down
62 changes: 62 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,68 @@ spans and `loadvm` still shift the virtual clock (R2); that is documented, not c
because the only fix is re-creating the filter, which truncates it. Analysis helpers stay out
of quickchr (`tikoci/tzsp-sniff`).

### A NIC fault is a filter added at run time, at the head of the chain (#369)

`fault()` / `set --fault` is a typed front for the blackhole that #339's research
found (`test/lab/link-impairment/REPORT.md`): a `filter-redirector` with only an
`outdev`, on a `null` chardev. It drops every frame one way and leaves carrier up on
both ends, which `link()` can't do. It follows `link()` in everything else: `etherN`
addressing, validate-all-then-apply, the machine lock (op `fault`), any HMP reply text
is a failure, the first failure stops the batch and names what applied, nothing
persisted, and refused next to other `set` flags.

**One filter and one sink per port, switched in place.** `fault<N>` on netdev `net<N>`,
with its own chardev `fault<N>-sink` (a chardev takes one frontend). The direction is
the filter's `queue` (`egress`=`rx`, `ingress`=`tx`, `both`=`all`, named from the
netdev's side), and `qom-set … queue` changes it per frame. So egress → both is one
command with no moment where frames pass, and a port never has two filters to keep
consistent. Clearing is `object_del` and then `chardev-remove`.

**State is read from QEMU, never stored.** A change starts, under the lock, from one
`info network` (every netdev's filters with type, `netdev`, `outdev` and `queue`) and one
`qom-list /chardevs` (ids with types). A `fault<N>` counts as quickchr's only if it is a
`filter-redirector` on `net<N>` writing to `fault<N>-sink` with no `indev`. Anything else
holding the id, such as a `qemuArgs` filter that happens to be named `fault1`, is refused
with `STATE_ERROR` and never reported, changed or deleted. The plan from the snapshot is
zero to three commands, so a repeat is a no-op. A sink an earlier failure left behind (a
`null` chardev no filter uses) is removed before a new one is added, and by a clear, so
retrying a clear whose `chardev-remove` failed finishes it. A filter QEMU refuses after
its sink was added gets the sink removed again. `faults()` and `list <m>` read one
`info network` and `qom-list /chardevs` too, reading the filters on both sides of the
chardevs and retrying until the two agree and every fault filter's sink is listed. QEMU
refuses to remove a chardev a filter uses, so a missing sink means the reads came from
different moments, even when a batch and its undo (A → B → A) left both filter reads
identical. That makes the pair coherent against quickchr's own writers without a lock. One `info network`
reads every port at one instant, without taking the machine lock, so a read never waits
behind a lock holder. The record is checked again afterwards, so a machine removed,
replaced or restarted during the read is an error, not a stale answer. `fault()` itself
takes the lock, so a `start --fg` session (which holds it for its whole run) refuses
`--fault` from another shell, as it does `--link`. Each operation runs over one monitor
connection (`withMonitorSession()`) with the usual 5 s per-command budget. The ids
`fault<N>` and `fault<N>-sink` are reserved; an unused `null` chardev with a sink's id is
taken for one an earlier failure left behind. A batch is applied port by port, so a read in the middle of one can show
its first ports changed and not the rest. A filter that matches but writes to a sink that
isn't a `null` chardev, or one switched off outside `fault()` (`status=off`), counts as
not quickchr's: it is refused and not reported. HMP has no read-back for carrier
(`info network` doesn't show `set_link`), so status says "carrier untouched", not "up".

**Added at run time, unlike capture.** A capture filter is never `object_add`ed
because its timestamps would take a different base (#338). A drop has no such
property, and a fault that existed from boot would need persisted desired state (#58).

**At the head of the chain, so a capture is the guest's view.** QEMU walks a netdev's
filters head to tail for frames delivered to the guest and tail to head for frames it
sends, so the head is the wire side. Boot filters (`cap<N>`) are created first, and
`object_add` appends at the tail by default, which would have put the drop between the
guest and its capture: egress drops invisible, ingress drops captured. `position=head`
puts it on the wire side. A capture then shows what the guest sent, dropped or not, and
only what reached it, like a mirror port on the guest. The far end's capture shows what
crossed. `test/integration/link-fault.test.ts` checks both directions.

**Only the blackhole.** Partial loss, delay, jitter and rate need a process outside
QEMU, which would be its own repo (decided 2026-12-01 on #369). `filter-buffer` and
virtio-net `tx=timer` were rejected in the research.

### Background is not detachment, and `--bg` never claimed to be

`spawnQemu()` backgrounds QEMU and calls `unref()`. That is enough for the parent to
Expand Down
66 changes: 65 additions & 1 deletion MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ flags that were asked for so it can be run as given.
| `--receive <spec>` | Reserve a guest→host receive port (repeatable). Running or stopped; no restart. §7 "Receive slots". |
| `--no-receive <name>` | Drop a receive slot by name (repeatable). Applied before `--receive`. |
| `--link <etherN>=up\|down` | Drop or restore carrier on this machine's own NIC (repeatable). Running only; not combinable with the other flags. §7 "Link state". |
| `--capture <etherN>=on\|off` | Switch packet capture on a NIC created with `capture` (repeatable). Running only; not combinable with the other flags. §7 "Packet capture". |
| `--fault <etherN>=egress\|ingress\|both\|none` | Drop every frame one way (or both) on this machine's NIC, with carrier kept up; `none` clears it (repeatable). Running only; not combinable with the other flags. §7 "Faults that keep carrier". |

**Device-mode restarts the machine.** RouterOS only applies a device-mode change
across a power cycle, and quickchr owns the power button — which is why this lives
Expand Down Expand Up @@ -1158,7 +1160,7 @@ redact before logging or attaching to bug reports.
| `installDeps` | boolean | — | **Deprecated, ignored.** Never installed anything; run `quickchr doctor` for install hints |
| `dryRun` | boolean | `false` | Print QEMU command and exit |
| `timeoutExtra` | number | `0` | Extra **milliseconds** added to boot timeout |
| `qemuArgs` | string[] | — | Extra QEMU argv elements, one entry each, appended verbatim last. Recorded by `add()`; a `start()` value replaces stored args for that boot only |
| `qemuArgs` | string[] | — | Extra QEMU argv elements, one entry each, appended verbatim last. Recorded by `add()`; a `start()` value replaces stored args for that boot only. `[]` means not given, so the stored args are used; `set --clear-qemu-args` drops them (#370) |
| `frozen` | boolean | `false` | Boot frozen at reset with a gdbstub; resume with `continue()`. Refused with provisioning or `background: false` |
| `onProgress` | `(msg) => void` | `console.log` | `[debug]` lines only when `QUICKCHR_DEBUG=1` |

Expand Down Expand Up @@ -1650,6 +1652,9 @@ quickchr list sw1 --json | jq .captures # { "ether2": "/…/caps/sw1-ether2-2

What a capture shows, and doesn't:

- **A NIC with a fault (`--fault`) captures from the guest's side of it**: what
the guest sent, dropped or not, and only what actually reached it. Capture the
far end to see what crossed.
- **A NIC whose link is down captures nothing**, in either direction: QEMU drops
the frame before the filter. A silent member (`set --link etherN=down`) shows
in its far end's capture, where its peer's frames keep going out.
Expand Down Expand Up @@ -1752,6 +1757,65 @@ await chr.link({ ether2: "up", ether3: "down" });

`examples/mlag/` uses both forms for its cable pulls and silent members.

### Faults that keep carrier (`set --fault`, `fault()`)

Drop every frame one way, or both ways, on a running machine's own NIC while
carrier stays up on both ends: a half-failed optic or SFP. `--link` can't make
that fault, because it drops carrier. It is QEMU's `filter-redirector` into a
`null` chardev, added and removed over the monitor, with no root, helper
process or reboot:

```sh
quickchr set lab-a --fault ether3=egress # frames lab-a sends on ether3 vanish
quickchr list lab-a # Fault: ether3 dropping egress (carrier untouched)
quickchr set lab-a --fault ether3=both --fault ether2=ingress
quickchr set lab-a --fault ether3=none --fault ether2=none # clear
```

```ts
await chr.fault("ether3", { drop: "egress" });
await chr.fault({ ether3: { drop: "both" }, ether2: null }); // null clears
await chr.faults(); // { ether3: { drop: "both" } }
```

- **Directions are named from the guest.** `egress` is what this machine sends
on the port, `ingress` what is delivered to it, `both` is both. Ports are
`etherN`, as for `--link`.
- **Nothing tells either end.** Both keep `running=yes`, RouterOS logs nothing,
and only protocol timeouts notice. With an egress drop on one member of an
802.3ad bond (`lacp-rate=1sec`), the far end took the member out of its bond
within 3–8 s (local KVM and CI runners), and the near end followed a few seconds
later once the far end's LACPDUs reported it out of sync. Clearing the drop
brought both back within about 2 s.
- **All or nothing.** Every frame in that direction is dropped. QEMU can't drop
a percentage, delay or rate-limit frames on its own (`test/lab/link-impairment/REPORT.md`).
- **After an `ingress` or `both` drop is cleared**, the first few frames can
still be lost while the peer re-resolves ARP: its requests went unanswered
during the drop.
- **Changes are in place.** Setting a port to what it has is a no-op, and a
change of direction switches the same filter, so frames don't get through in
between. `list <m>` (and `faults` in `list <m> --json`) reads them back from QEMU.
- **Runtime only.** Nothing is saved. A new QEMU launch (`start`, or a
device-mode power cycle) starts with no fault; a guest reboot inside the same
QEMU keeps it. `--fault` can't be combined with the other `set` flags.
- **A capture on the same NIC shows the guest's side** (§ "Packet capture"):
what the guest sent, dropped or not, and only what reached it.
- **`fault<N>` and `fault<N>-sink` are quickchr's names** for the filter and the
sink chardev on the N+1th NIC (`ether2` is `fault1`). Don't give `--qemu-arg`
objects or chardevs those ids: a filter, or a non-null or in-use chardev, holding
one is refused, but an unused `null` chardev named `fault<N>-sink` is taken for a
sink left behind and removed.
- **Don't switch a fault filter off through `monitor()`** (`qom-set … status off`).
quickchr then neither reports nor changes it, and on Windows QEMU's monitor stopped
answering after it while the link carried traffic. Clear a fault with `--fault …=none`.
- **A foreground session (`start --fg`) holds the machine lock**, as for `--link`,
so `--fault` from another shell is refused with `MACHINE_LOCKED` while it runs.
- **Validated first, then applied in order**, under the machine lock, like
`--link`. A failure stops the batch and names the ports already changed. If
QEMU refuses part of one port's setup, quickchr tries to undo the rest. If QEMU
refuses that too, the error names what was left, and the next `--fault` on that
port tries to remove it again.

### Named L2 sockets

`quickchr networks sockets create lab-sw1` registers a link in
Expand Down
16 changes: 10 additions & 6 deletions src/cli/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const SET_ONLY_FLAGS = [
"device-mode", "device-mode-enable", "device-mode-disable",
"qemu-arg", "clear-qemu-args",
"receive", "no-receive",
"link", "capture",
"link", "capture", "fault",
] as const;

export const ADD_FLAGS: readonly string[] = CREATE_FLAGS;
Expand Down Expand Up @@ -90,8 +90,8 @@ export const VALUE_FLAGS: ReadonlySet<string> = new Set([
"qemu-arg",
// exec / qga / console
"via", "user", "password", "timeout", "path", "data", "script",
// set --license / --package / --receive / --link / --capture
"account", "account-file", "password-file", "level", "package", "no-receive", "link", "capture",
// set --license / --package / --receive / --link / --capture / --fault
"account", "account-file", "password-file", "level", "package", "no-receive", "link", "capture", "fault",
// networks sockets create
"mode", "port", "group",
// completions / logs
Expand Down Expand Up @@ -141,7 +141,7 @@ export function identityFlagError(argv: readonly string[]): string | undefined {
}

/** Every occurrence of a repeatable `<port>=<state>` flag, in order, read from the raw
* arguments: `--link` (#334) and `--capture` (#338).
* arguments: `--link` (#334), `--capture` (#338) and `--fault` (#369).
*
* `parseFlags` merges repeats, and a bare occurrence (`true`) or `--no-<flag>` (`false`)
* is overwritten by any later valued one — so `--link ether2 --link --link ether3=up`
Expand All @@ -150,9 +150,13 @@ export function identityFlagError(argv: readonly string[]): string | undefined {
* next argument unless it starts with `--`. Stops at `--`. */
export function portAssignmentFlagValues(
argv: readonly string[],
name: "link" | "capture",
name: "link" | "capture" | "fault",
): { values: string[]; error?: string | undefined } {
const example = name === "link" ? { form: "<port>=up|down", sample: "ether2=down", up: "ether2=up" } : { form: "<port>=on|off", sample: "ether2=on", up: "ether2=on" };
const example = {
link: { form: "<port>=up|down", sample: "ether2=down", up: "ether2=up" },
capture: { form: "<port>=on|off", sample: "ether2=on", up: "ether2=on" },
fault: { form: "<port>=egress|ingress|both|none", sample: "ether3=egress", up: "ether3=none" },
}[name];
const values: string[] = [];
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
Expand Down
Loading
Loading