From a12b05ceecedbeca4767e9b9bbdd527fa9ebbc55 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 03:45:29 -0500 Subject: [PATCH 1/2] native/net: accept real QEMU virtio-net-pci caps; add virtio-net split-virtqueue driver core (hosted) Lane 26. The PCI capability walk refused any vendor cap with a zero-length region before checking its type, so the legal VIRTIO_PCI_CAP_PCI_CFG (cfg_type 5, bar 0 / offset 0 / length 0) present on every real QEMU virtio-net-pci device made the whole device InvalidRegion. Confirmed on config space dumped from real QEMU 8.2.2 via qtest (no guest), transitional 0x1000 and modern 0x1041; fixtures and the dump script are in tests/fixtures. Region checks now apply to cfg_type 1..4 only (unchanged for those). The pinned Rust differential still runs via a reference-compat build; the default build gets a C-only pin and diff-check proves the 1,622 differing records were all reference InvalidRegion. aienos_virtio_net.{h,c}: polled virtio 1.x split-virtqueue virtio-net core written from the spec (the Rust reference has no queue code): VERSION_1 required, MAC optional, all else declined; rings and buffers in one caller region; descriptors rebuilt from the id and bounds-checked; hostile used-ring ids, lengths and indices mark the device broken (sticky). Device access only through vnet_ops. tests/vnet_test.c (make test + make sanitize): QEMU caps + 15 malformed variants, init refusals, 70k-frame wrap, hostile device (directed + seeded fuzz), M6-A UDP round trip through TX and RX queues against a simulated device. Not proven: real device, DMA, IRQ, QEMU slirp round trip. Review: codex unavailable (usage limit); Gemini (agy, plan mode): APPROVE, no findings. Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- native/net/Makefile | 36 +- native/net/README.md | 71 ++- native/net/aienos_virtio_net.c | 332 +++++++++++ native/net/aienos_virtio_net.h | 151 +++++ native/net/aienos_virtio_pci.c | 14 +- native/net/receipt.sh | 9 +- .../fixtures/dump_qemu_virtio_net_cfg.sh | 28 + .../qemu822_virtio_net_pci_modern.cfg | Bin 0 -> 256 bytes .../qemu822_virtio_net_pci_transitional.cfg | Bin 0 -> 256 bytes native/net/tests/vnet_test.c | 536 ++++++++++++++++++ 10 files changed, 1163 insertions(+), 14 deletions(-) create mode 100644 native/net/aienos_virtio_net.c create mode 100644 native/net/aienos_virtio_net.h create mode 100644 native/net/tests/fixtures/dump_qemu_virtio_net_cfg.sh create mode 100644 native/net/tests/fixtures/qemu822_virtio_net_pci_modern.cfg create mode 100644 native/net/tests/fixtures/qemu822_virtio_net_pci_transitional.cfg create mode 100644 native/net/tests/vnet_test.c diff --git a/native/net/Makefile b/native/net/Makefile index 47d63ee5..84718e4d 100644 --- a/native/net/Makefile +++ b/native/net/Makefile @@ -4,7 +4,8 @@ # Hosted only: native binding waits for a C kernel, production keys for M5. # # make build out/libaienos_net.a -# make test protocol tests + control-transport loss/recovery + lib-checks +# make test protocol tests + control-transport loss/recovery + virtio-net +# (QEMU config dumps, simulated hostile device) + lib-checks # + diff-check (pinned C/Rust-reference agreement digest) # make sanitize the same tests under ASan+UBSan (Linux) # make receipt run everything on a clean tree, write receipts/.txt @@ -27,8 +28,14 @@ ITERS ?= 200000 DIFF_SEED = 0xa1e5 DIFF_COUNT = 200000 DIFF_DIGEST = 9ddf33a859ade2d92d0e86a213d29d682708b0ac308239a7b6f003dbf9a5cdfe - -SRCS = aienos_net.c aienos_virtio_pci.c aienos_ctl.c x25519.c aienos_sec.c +# The same corpus through the default build. It differs from the Rust digest +# only on records the reference refused as InvalidRegion, because the C parser +# no longer region-checks vendor caps of cfg_type outside 1..4 (QEMU PCI_CFG, +# Lane 26). The Rust digest above is still checked with a reference-compat +# build (out/net_diff_ref). C-only pin, recorded 2026-10-01; no Rust run. +DIFF_DIGEST_FIXED = 034151b28953e23cbcdc2b805885ea88887b3da52e46369e8c01bd5b276241cb + +SRCS = aienos_net.c aienos_virtio_pci.c aienos_virtio_net.c aienos_ctl.c x25519.c aienos_sec.c # M6-B links (never edits) the in-house AES-GCM-SIV/HMAC and Ed25519 code. CRYPTO_SRCS = ../crypto/aes.c ../crypto/polyval.c ../crypto/gcm_siv.c ../crypto/hmac.c ../crypto/ct.c SIG_SRCS = ../sig/ed25519.c ../sig/sha512.c ../sig/ct.c @@ -64,16 +71,21 @@ $(OUT)/ctl_test: tests/ctl_test.c $(LIB) $(CC) $(CFLAGS) -o $@ $< $(LIB) $(OUT)/net_diff: tests/net_diff.c $(LIB) $(CC) $(CFLAGS) -o $@ $< $(LIB) +$(OUT)/vnet_test: tests/vnet_test.c $(LIB) + $(CC) $(CFLAGS) -o $@ $< $(LIB) +$(OUT)/net_diff_ref: tests/net_diff.c $(SRCS) $(DEP_SRCS) $(wildcard *.h) | $(OUT) + $(CC) $(CFLAGS) -DAIENOS_VIRTIO_PCI_REFERENCE_COMPAT -o $@ tests/net_diff.c $(SRCS) $(DEP_SRCS) $(OUT)/x25519_test: tests/x25519_test.c $(LIB) $(CC) $(CFLAGS) -o $@ $< $(LIB) $(OUT)/sec_test: tests/sec_test.c $(LIB) $(CC) $(CFLAGS) -o $@ $< $(LIB) -test: lib-checks $(OUT)/net_test $(OUT)/ctl_test $(OUT)/x25519_test $(OUT)/sec_test diff-check +test: lib-checks $(OUT)/net_test $(OUT)/ctl_test $(OUT)/vnet_test $(OUT)/x25519_test $(OUT)/sec_test diff-check ./$(OUT)/net_test $(SEED) $(ITERS) ./$(OUT)/ctl_test ./$(OUT)/x25519_test ./$(OUT)/sec_test + ./$(OUT)/vnet_test # Protocol objects must not allocate, read a clock or do their own I/O. lib-checks: $(OBJS) @@ -86,16 +98,24 @@ lib-checks: $(OBJS) @grep -q "NOT-FOR-PRODUCTION" aienos_sec.c || { echo "lib-checks FAIL: M6-B TEST label missing"; exit 1; } @echo "lib-checks PASS (no heap, clock, I/O or ambient randomness; TEST identity only)" -diff-corpus: $(OUT)/net_diff +diff-corpus: $(OUT)/net_diff $(OUT)/net_diff_ref ./$(OUT)/net_diff gen $(DIFF_SEED) $(DIFF_COUNT) > $(OUT)/diff_corpus.bin ./$(OUT)/net_diff run < $(OUT)/diff_corpus.bin > $(OUT)/diff_c.txt + ./$(OUT)/net_diff_ref run < $(OUT)/diff_corpus.bin > $(OUT)/diff_ref.txt @echo "corpus $$(sha256sum $(OUT)/diff_corpus.bin | cut -d' ' -f1)" @echo "c-out $$(sha256sum $(OUT)/diff_c.txt | cut -d' ' -f1)" diff-check: diff-corpus - @got=$$(sha256sum $(OUT)/diff_c.txt | cut -d' ' -f1); \ - if [ "$$got" = "$(DIFF_DIGEST)" ]; then echo "NET_RUST_DIFFERENTIAL_PINNED: PASS ($(DIFF_COUNT) records)"; \ + @got=$$(sha256sum $(OUT)/diff_ref.txt | cut -d' ' -f1); \ + if [ "$$got" = "$(DIFF_DIGEST)" ]; then echo "NET_RUST_DIFFERENTIAL_PINNED: PASS ($(DIFF_COUNT) records, reference-compat build)"; \ else echo "NET_RUST_DIFFERENTIAL_PINNED: FAIL (got $$got, pinned $(DIFF_DIGEST))"; exit 1; fi + @got=$$(sha256sum $(OUT)/diff_c.txt | cut -d' ' -f1); \ + if [ "$$got" = "$(DIFF_DIGEST_FIXED)" ]; then echo "NET_DIFFERENTIAL_FIXED_PINNED: PASS (C-only pin)"; \ + else echo "NET_DIFFERENTIAL_FIXED_PINNED: FAIL (got $$got, pinned $(DIFF_DIGEST_FIXED))"; exit 1; fi + @paste -d'\t' $(OUT)/diff_ref.txt $(OUT)/diff_c.txt | awk -F'\t' \ + '$$1 != $$2 { d++; if ($$1 != "V Err InvalidRegion") bad++ } \ + END { if (NR != $(DIFF_COUNT) || bad) { print "NET_DIFFERENTIAL_DIVERGENCE: FAIL (" bad+0 " records differ outside the PCI_CFG fix)"; exit 1 } \ + print "NET_DIFFERENTIAL_DIVERGENCE: PASS (" d+0 " records differ, all reference InvalidRegion)" }' sanitize: | $(OUT) ifeq ($(shell uname -s),Linux) @@ -110,6 +130,8 @@ ifeq ($(shell uname -s),Linux) $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/sec_test_asan tests/sec_test.c $(SRCS) $(DEP_SRCS) ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/x25519_test_asan ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/sec_test_asan + $(CC) $(CFLAGS) $(SANFLAGS) -o $(OUT)/vnet_test_asan tests/vnet_test.c $(SRCS) $(DEP_SRCS) + ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./$(OUT)/vnet_test_asan @echo "NET_SANITIZE: PASS" else @echo "sanitize: Linux only" diff --git a/native/net/README.md b/native/net/README.md index 344f392e..c1bfcd18 100644 --- a/native/net/README.md +++ b/native/net/README.md @@ -7,7 +7,8 @@ control-transport stub. No Rust, no Python, no heap, no outside library. | File | What it is | |---|---| | `aienos_net.{h,c}` | Ethernet II, ARP + fixed ARP cache, IPv4 (no reassembly), ICMP echo, UDP. Bounded parsers; error order matches the reference. | -| `aienos_virtio_pci.{h,c}` | virtio 1.x PCI vendor-capability walk over a config image (no device access). | +| `aienos_virtio_pci.{h,c}` | virtio 1.x PCI vendor-capability walk over a config image (no device access). Accepts the real QEMU virtio-net-pci layout (see Lane 26 below). | +| `aienos_virtio_net.{h,c}` | Lane 26: virtio-net driver core, split virtqueues, polled. New C written from the virtio 1.x spec (the Rust reference has no queue code). Device access only through a small ops struct. | | `aienos_ctl.{h,c}` | Control-transport stub: authenticated frames, ordered exactly-once delivery, retransmit, link-down. **TEST identity only.** The frame format is specified in the header. | | `x25519.{h,c}` | M6-B: X25519 (RFC 7748), constant-time ladder, small-order outputs refused. | | `aienos_sec.{h,c}` | M6-B: mutually authenticated secure control transport over the M6-A UDP framing. **TEST identities only.** The wire format is specified in the header. | @@ -42,8 +43,8 @@ control-transport stub. No Rust, no Python, no heap, no outside library. ## Not claimed (deferred, with the dependency) -- Native binding (virtio-net queues, real NIC, interrupts): needs a C kernel. - No C kernel exists yet; the aienos port order is an open operator decision. +- Native binding (real NIC, interrupts, real DMA): the virtio-net queue code now + exists as hosted code (Lane 26, below) but nothing in the C kernel calls it yet. - Production identity and keys: needs M5 (key hierarchy, TRUST-1). The M6-B secure transport below exists as hosted code but runs on TEST keys only. The stub refuses every identity kind except TEST, whose key is derived from a public label and so protects nothing against an attacker. @@ -194,3 +195,67 @@ verify}`, which carries a 32-byte tag. The two connect like this: signature means widening the Fabric tag to 64 bytes (an omega change). - **Nothing is built yet.** No adapter exists in either repository. This section is the interface plan only. + +## Lane 26: virtio-net PCI fix and virtqueue data path (hosted) + +**The attach bug.** The capability walk refused any vendor capability whose +region had length 0, before looking at its type. Real QEMU virtio-net-pci +always carries a `VIRTIO_PCI_CAP_PCI_CFG` (cfg_type 5) capability with bar 0, +offset 0, length 0, which the virtio 1.x spec allows, so every real QEMU +virtio-net device was refused with `InvalidRegion`. Confirmed on real config +space dumped from QEMU 8.2.2 (no guest: the qtest protocol reads the ECAM +window while the CPU is paused; `tests/fixtures/dump_qemu_virtio_net_cfg.sh` +re-dumps it). Fix: the BAR / zero-length / overflow checks now apply to the +four structures the driver maps (cfg_type 1..4) only; other cfg types are +ignored, as the spec requires. The Rust reference has the same bug (left +unedited: no Rust work). + +The pinned Rust differential still runs, against a reference-compat build +(`-DAIENOS_VIRTIO_PCI_REFERENCE_COMPAT`, test-only). The default build has its +own C-only pin, and `make diff-check` proves the two differ only on records +the reference refused as `InvalidRegion` (1,622 of 200,000). + +**The data path** (`aienos_virtio_net`): reset, ACK, DRIVER; VERSION_1 +required, MAC accepted if offered, every other feature declined; FEATURES_OK +read back; queue 0 RX and queue 1 TX, power-of-two size up to 256 (downsized to +what the device offers); notify offset checked against the notify window; all +RX buffers posted before DRIVER_OK; polled (no MSI-X vector, NO_INTERRUPT). +Rings and buffers live in one caller region; descriptors are rebuilt from the +id on every post and checked inside the region; nothing the device writes is +trusted. A used entry with an id out of range or not outstanding, a length +over the buffer, a short RX completion, or a used index running ahead marks the +device broken (sticky) until re-init. + +**Tests** (`tests/vnet_test.c`, in `make test` and `make sanitize`): +- `VIRTIO_PCI_QEMU822_CAPS`: both real QEMU dumps (transitional 0x1000, modern + 0x1041) parse to the expected windows (BAR 4, common 0x0, ISR 0x1000, device + 0x2000, notify 0x3000, multiplier 4); 15 malformed variants of the real image + stay refused (zero-length common/notify/ISR/device, bad BAR, offset overflow, + short cap, cap past config space, loops, duplicate, bad pointer). +- Init against a simulated device in C: feature masking when the device offers + all 64 bits; refusals for no VERSION_1, FEATURES_OK refused, stuck reset, one + queue, queue size 0/1, pre-enabled queue, notify offset outside the window, + unstable config generation, missing/short caps, small/misaligned/wrapping + memory. No notify before DRIVER_OK. +- Data path: TX full/reclaim, max-size frames, 70,000 frames each way so the + 16-bit ring indices wrap; the device model checks every descriptor it reads. +- Hostile device: 9 directed cases plus a 2,000-round seeded fuzz of forged + used entries and indices; each refusal is sticky; a device scribbling over + the descriptor table cannot steer the driver. +- `VNET_M6A_UDP_ROUNDTRIP_SIM`: a UDP frame built by M6-A leaves through the TX + queue byte-exact; the simulated device answers with a frame placed in the RX + queue, and M6-A parses it (Ethernet, IPv4, UDP checksum, payload). + +**Not proven (do not read more into the PASS lines):** no real device, no real +DMA or IOMMU/SMMU mapping, no interrupts, no QEMU slirp round trip, no +cache-coherence or MMIO ordering on real hardware, no physical NIC. The only +real-QEMU evidence is the static config space. A real QEMU end-to-end needs a +guest kernel that drives the device; this module is hosted and the kernel +binding belongs to the native kernel lane. + +**Kernel hook (for the native kernel).** After `ck_virtio_net_probe` accepts +the caps: add `aienos_virtio_net.c` to the stage sources, enable memory space +and bus mastering, map the BARs named by the caps, supply `vnet_ops` (volatile +MMIO at BAR + cap offset + off; notify = 16-bit write at the notify window + +off), give a DMA-visible region of `vnet_mem_size(qsize)` bytes with its bus +address, and call `vnet_init`; then poll `vnet_rx` / `vnet_tx_reclaim`. diff --git a/native/net/aienos_virtio_net.c b/native/net/aienos_virtio_net.c new file mode 100644 index 00000000..a1b4d7f9 --- /dev/null +++ b/native/net/aienos_virtio_net.c @@ -0,0 +1,332 @@ +/* See aienos_virtio_net.h. */ +#include "aienos_virtio_net.h" + +#include + +#if !defined(__BYTE_ORDER__) || __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ +#error "virtio 1.x rings are little-endian; this driver assumes a little-endian CPU" +#endif + +#define VNET_RESET_POLLS 100000u +#define VNET_CFGGEN_TRIES 8u +#define ALIGN_UP(x, a) (((x) + ((a) - 1)) & ~(size_t)((a) - 1)) + +const char *vnet_err_name(vnet_err e) +{ + switch (e) { + case VNET_OK: return "Ok"; + case VNET_E_ARG: return "Arg"; + case VNET_E_CAPS: return "Caps"; + case VNET_E_MEM: return "Mem"; + case VNET_E_RESET_TIMEOUT: return "ResetTimeout"; + case VNET_E_NO_VERSION_1: return "NoVersion1"; + case VNET_E_FEATURES: return "FeaturesRefused"; + case VNET_E_QUEUES: return "Queues"; + case VNET_E_NOTIFY: return "NotifyOffset"; + case VNET_E_CONFIG: return "ConfigGeneration"; + case VNET_E_FULL: return "Full"; + case VNET_E_DEVICE: return "HostileDevice"; + case VNET_E_BROKEN: return "Broken"; + } + return "Unknown"; +} + +static int pow2_ok(uint32_t n) { return n >= VNET_QUEUE_MIN && n <= VNET_QUEUE_MAX && (n & (n - 1)) == 0; } + +/* Layout of one queue sized for qsize, starting at base; returns the end. */ +static size_t queue_layout(vnet_queue *q, size_t base, uint32_t qsize) +{ + size_t at = ALIGN_UP(base, 64); + q->desc_off = at; at += 16u * qsize; + at = ALIGN_UP(at, 64); + q->avail_off = at; at += 6u + 2u * qsize; + at = ALIGN_UP(at, 64); + q->used_off = at; at += 6u + 8u * qsize; + at = ALIGN_UP(at, 64); + q->buf_off = at; at += (size_t)VNET_BUF_STRIDE * qsize; + return at; +} + +size_t vnet_mem_size(uint16_t qsize) +{ + if (!pow2_ok(qsize)) return 0; + vnet_queue q; + return queue_layout(&q, queue_layout(&q, 0, qsize), qsize); +} + +/* ---- register access, bounded by the parsed capability windows ---- */ +static const virtio_pci_region *win_region(const vnet_dev *d, vnet_window w) +{ + switch (w) { + case VNET_WIN_COMMON: return &d->caps.common_cfg; + case VNET_WIN_NOTIFY: return &d->caps.notify_cfg; + case VNET_WIN_ISR: return &d->caps.isr_cfg; + case VNET_WIN_DEVICE: return &d->caps.device_cfg; + } + return NULL; +} +static int win_ok(const vnet_dev *d, vnet_window w, uint32_t off, unsigned width) +{ + const virtio_pci_region *r = win_region(d, w); + return r && r->present && (uint64_t)off + width <= r->length; +} +/* Callers only pass constant offsets that vnet_init checked against the + * window lengths up front; the check here is a second line. */ +static uint32_t rd(vnet_dev *d, vnet_window w, uint32_t off, unsigned width) +{ + if (!win_ok(d, w, off, width)) return 0; + return d->ops.read(d->ops.ctx, w, off, width); +} +static void wr(vnet_dev *d, vnet_window w, uint32_t off, unsigned width, uint32_t v) +{ + if (win_ok(d, w, off, width)) d->ops.write(d->ops.ctx, w, off, width, v); +} +static void wr64(vnet_dev *d, uint32_t lo_off, uint64_t v) +{ + wr(d, VNET_WIN_COMMON, lo_off, 4, (uint32_t)v); + wr(d, VNET_WIN_COMMON, lo_off + 4, 4, (uint32_t)(v >> 32)); +} + +/* ---- shared ring memory ---- */ +static int in_region(const vnet_dev *d, size_t off, size_t len) +{ + return off <= d->mem_len && len <= d->mem_len - off; +} +static void put16(uint8_t *p, uint16_t v) { memcpy(p, &v, 2); } +static void put32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); } +static void put64(uint8_t *p, uint64_t v) { memcpy(p, &v, 8); } + +/* Write descriptor `id` of queue q for its own buffer. Refuses (returns 0) + * when the buffer would leave the region (cannot happen for a correctly + * sized region; kept as a guard). */ +static int set_desc(vnet_dev *d, vnet_queue *q, uint16_t id, uint32_t len, uint16_t flags) +{ + size_t boff = q->buf_off + (size_t)id * VNET_BUF_STRIDE; + if (id >= q->size || len > VNET_BUF_LEN || !in_region(d, boff, len)) return 0; + uint8_t *e = d->mem + q->desc_off + 16u * id; + put64(e, d->dma + boff); + put32(e + 8, len); + put16(e + 12, flags); + put16(e + 14, 0); + return 1; +} + +static void avail_push(vnet_dev *d, vnet_queue *q, uint16_t id) +{ + uint8_t *a = d->mem + q->avail_off; + put16(a + 4 + 2u * (q->avail_idx & (q->size - 1)), id); + q->avail_idx++; + q->posted[id] = 1; + q->in_flight++; + __atomic_thread_fence(__ATOMIC_RELEASE); /* ring entry before idx */ + __atomic_store_n((uint16_t *)(void *)(a + 2), q->avail_idx, __ATOMIC_RELEASE); +} + +static void kick(vnet_dev *d, vnet_queue *q, uint16_t index) +{ + __atomic_thread_fence(__ATOMIC_SEQ_CST); /* idx visible before the notify */ + d->ops.notify(d->ops.ctx, q->notify_off, index); +} + +static vnet_err mark_broken(vnet_dev *d) +{ + d->broken = 1; + return VNET_E_DEVICE; +} + +/* Take the next used entry, validating everything the device wrote. *got 0 + * when nothing is pending. limit = most bytes the device may report written. */ +static vnet_err used_next(vnet_dev *d, vnet_queue *q, uint32_t limit, uint16_t *id_out, + uint32_t *len_out, int *got) +{ + *got = 0; + uint8_t *u = d->mem + q->used_off; + uint16_t idx = __atomic_load_n((uint16_t *)(void *)(u + 2), __ATOMIC_ACQUIRE); + uint16_t pending = (uint16_t)(idx - q->last_used); + if (pending == 0) return VNET_OK; + /* more completions than posted. Defense in depth: the posted[] check + * below also catches every case the tests construct. */ + if (pending > q->in_flight) return mark_broken(d); + const volatile uint8_t *e = u + 4 + 8u * (q->last_used & (q->size - 1)); + uint32_t id = 0, len = 0; + for (int i = 0; i < 4; i++) { id |= (uint32_t)e[i] << (8 * i); len |= (uint32_t)e[4 + i] << (8 * i); } + if (id >= q->size || !q->posted[id] || len > limit) return mark_broken(d); + q->posted[id] = 0; + q->in_flight--; + q->last_used++; + *id_out = (uint16_t)id; + *len_out = len; + *got = 1; + return VNET_OK; +} + +static vnet_err fail(vnet_dev *d, vnet_err e) +{ + uint32_t s = rd(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1); + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, (s | VNET_S_FAILED) & 0xffu); + return e; +} + +static vnet_err setup_queue(vnet_dev *d, uint16_t index, uint32_t want) +{ + vnet_queue *q = &d->q[index]; + wr(d, VNET_WIN_COMMON, VNET_CC_Q_SELECT, 2, index); + uint32_t max = rd(d, VNET_WIN_COMMON, VNET_CC_Q_SIZE, 2); + if (max == 0 || rd(d, VNET_WIN_COMMON, VNET_CC_Q_ENABLE, 2) != 0) return VNET_E_QUEUES; + uint32_t n = want; + while (n > max) n >>= 1; + if (n < VNET_QUEUE_MIN) return VNET_E_QUEUES; + q->size = (uint16_t)n; + wr(d, VNET_WIN_COMMON, VNET_CC_Q_SIZE, 2, n); + wr(d, VNET_WIN_COMMON, VNET_CC_Q_MSIX, 2, 0xffffu); /* polled: no vector */ + uint64_t noff = (uint64_t)rd(d, VNET_WIN_COMMON, VNET_CC_Q_NOFF, 2) * d->caps.notify_off_multiplier; + if (noff + 2 > d->caps.notify_cfg.length) return VNET_E_NOTIFY; + q->notify_off = (uint32_t)noff; + wr64(d, VNET_CC_Q_DESCLO, d->dma + q->desc_off); + wr64(d, VNET_CC_Q_AVAILLO, d->dma + q->avail_off); + wr64(d, VNET_CC_Q_USEDLO, d->dma + q->used_off); + put16(d->mem + q->avail_off, VNET_AVAIL_F_NO_INTERRUPT); + wr(d, VNET_WIN_COMMON, VNET_CC_Q_ENABLE, 2, 1); + return VNET_OK; +} + +vnet_err vnet_init(vnet_dev *d, const vnet_ops *ops, const virtio_pci_caps *caps, + void *mem, uint64_t dma, size_t mem_len, uint16_t qsize) +{ + if (!d) return VNET_E_ARG; + memset(d, 0, sizeof *d); + d->broken = 1; /* until init succeeds */ + if (!ops || !ops->read || !ops->write || !ops->notify || !caps || !mem || !pow2_ok(qsize)) + return VNET_E_ARG; + /* required windows: common (whole structure), notify (+ multiplier), isr, device */ + if (!caps->common_cfg.present || caps->common_cfg.length < VNET_CC_LEN || !caps->notify_cfg.present || + !caps->has_notify_off_multiplier || caps->notify_cfg.length < 2 || !caps->isr_cfg.present || + !caps->device_cfg.present) + return VNET_E_CAPS; + size_t need = vnet_mem_size(qsize); + if (mem_len < need || ((uintptr_t)mem & 15u) || (dma & 15u) || dma > UINT64_MAX - mem_len) + return VNET_E_MEM; + d->ops = *ops; + d->caps = *caps; + d->mem = mem; + d->dma = dma; + d->mem_len = mem_len; + memset(mem, 0, need); + size_t end = queue_layout(&d->q[VNET_RX_QUEUE], 0, qsize); + queue_layout(&d->q[VNET_TX_QUEUE], end, qsize); + + /* 3.1.1: reset, ACKNOWLEDGE, DRIVER */ + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, 0); + unsigned polls = 0; + while (rd(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1) != 0) + if (++polls >= VNET_RESET_POLLS) return VNET_E_RESET_TIMEOUT; + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, VNET_S_ACK); + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, VNET_S_ACK | VNET_S_DRIVER); + + /* features */ + wr(d, VNET_WIN_COMMON, VNET_CC_DFSELECT, 4, 0); + uint64_t offered = rd(d, VNET_WIN_COMMON, VNET_CC_DF, 4); + wr(d, VNET_WIN_COMMON, VNET_CC_DFSELECT, 4, 1); + offered |= (uint64_t)rd(d, VNET_WIN_COMMON, VNET_CC_DF, 4) << 32; + if (!(offered >> VNET_F_VERSION_1 & 1)) return fail(d, VNET_E_NO_VERSION_1); + uint64_t accept = (1ull << VNET_F_VERSION_1) | (offered & (1ull << VNET_F_MAC)); + if (accept >> VNET_F_MAC & 1) { + if (caps->device_cfg.length < 6) return fail(d, VNET_E_CAPS); + } + wr(d, VNET_WIN_COMMON, VNET_CC_GFSELECT, 4, 0); + wr(d, VNET_WIN_COMMON, VNET_CC_GF, 4, (uint32_t)accept); + wr(d, VNET_WIN_COMMON, VNET_CC_GFSELECT, 4, 1); + wr(d, VNET_WIN_COMMON, VNET_CC_GF, 4, (uint32_t)(accept >> 32)); + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, VNET_S_ACK | VNET_S_DRIVER | VNET_S_FEATURES_OK); + if (!(rd(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1) & VNET_S_FEATURES_OK)) return fail(d, VNET_E_FEATURES); + d->features = accept; + + /* queues: 0 = receiveq1, 1 = transmitq1 */ + wr(d, VNET_WIN_COMMON, VNET_CC_MSIX, 2, 0xffffu); + if (rd(d, VNET_WIN_COMMON, VNET_CC_NUMQ, 2) < 2) return fail(d, VNET_E_QUEUES); + for (uint16_t i = 0; i < 2; i++) { + vnet_err e = setup_queue(d, i, qsize); + if (e) return fail(d, e); + } + + /* MAC, read under a stable config generation */ + if (accept >> VNET_F_MAC & 1) { + unsigned t = 0; + for (;; t++) { + if (t >= VNET_CFGGEN_TRIES) return fail(d, VNET_E_CONFIG); + uint32_t g0 = rd(d, VNET_WIN_COMMON, VNET_CC_CFGGEN, 1); + for (uint32_t i = 0; i < 6; i++) d->mac[i] = (uint8_t)rd(d, VNET_WIN_DEVICE, i, 1); + if (rd(d, VNET_WIN_COMMON, VNET_CC_CFGGEN, 1) == g0) break; + } + d->has_mac = 1; + } + + /* post every RX buffer, then DRIVER_OK, then the first notify */ + vnet_queue *rx = &d->q[VNET_RX_QUEUE]; + for (uint16_t id = 0; id < rx->size; id++) { + if (!set_desc(d, rx, id, VNET_BUF_LEN, VNET_DESC_F_WRITE)) return fail(d, VNET_E_MEM); + avail_push(d, rx, id); + } + wr(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1, + VNET_S_ACK | VNET_S_DRIVER | VNET_S_FEATURES_OK | VNET_S_DRIVER_OK); + uint32_t s = rd(d, VNET_WIN_COMMON, VNET_CC_STATUS, 1); + if ((s & (VNET_S_NEEDS_RESET | VNET_S_FAILED)) || !(s & VNET_S_DRIVER_OK)) return fail(d, VNET_E_DEVICE); + d->broken = 0; + kick(d, rx, VNET_RX_QUEUE); + return VNET_OK; +} + +vnet_err vnet_tx_reclaim(vnet_dev *d, unsigned *reclaimed) +{ + if (reclaimed) *reclaimed = 0; + if (!d) return VNET_E_ARG; + if (d->broken) return VNET_E_BROKEN; + vnet_queue *q = &d->q[VNET_TX_QUEUE]; + for (;;) { + uint16_t id; uint32_t len; int got; + /* TX buffers are device-readable only (QEMU reports 0 written); a + * report above one buffer length is hostile. */ + vnet_err e = used_next(d, q, VNET_BUF_LEN, &id, &len, &got); + if (e || !got) return e; + if (reclaimed) (*reclaimed)++; + } +} + +vnet_err vnet_tx(vnet_dev *d, const uint8_t *frame, size_t len) +{ + if (!d || !frame || len < 14 || len > VNET_FRAME_MAX) return VNET_E_ARG; + if (d->broken) return VNET_E_BROKEN; + vnet_queue *q = &d->q[VNET_TX_QUEUE]; + if (q->in_flight >= q->size) return VNET_E_FULL; + uint16_t id = 0; + while (id < q->size && q->posted[id]) id++; + if (id >= q->size) return VNET_E_FULL; + if (!set_desc(d, q, id, (uint32_t)(VNET_HDR_LEN + len), 0)) return VNET_E_MEM; + uint8_t *b = d->mem + q->buf_off + (size_t)id * VNET_BUF_STRIDE; + memset(b, 0, VNET_HDR_LEN); /* flags 0, GSO_NONE: no offloads negotiated */ + memcpy(b + VNET_HDR_LEN, frame, len); + avail_push(d, q, id); + kick(d, q, VNET_TX_QUEUE); + return VNET_OK; +} + +vnet_err vnet_rx(vnet_dev *d, uint8_t *out, size_t cap, size_t *len, int *got) +{ + if (got) *got = 0; + if (len) *len = 0; + if (!d || !out || !len || !got || cap < VNET_FRAME_MAX) return VNET_E_ARG; + if (d->broken) return VNET_E_BROKEN; + vnet_queue *q = &d->q[VNET_RX_QUEUE]; + uint16_t id; uint32_t n; int have; + vnet_err e = used_next(d, q, VNET_BUF_LEN, &id, &n, &have); + if (e || !have) return e; + if (n < VNET_HDR_LEN) return mark_broken(d); /* every RX completion carries the header */ + size_t flen = n - VNET_HDR_LEN; /* <= VNET_FRAME_MAX by the limit above */ + memcpy(out, d->mem + q->buf_off + (size_t)id * VNET_BUF_STRIDE + VNET_HDR_LEN, flen); + if (!set_desc(d, q, id, VNET_BUF_LEN, VNET_DESC_F_WRITE)) return mark_broken(d); + avail_push(d, q, id); + kick(d, q, VNET_RX_QUEUE); + *len = flen; + *got = 1; + return VNET_OK; +} diff --git a/native/net/aienos_virtio_net.h b/native/net/aienos_virtio_net.h new file mode 100644 index 00000000..caa39d6e --- /dev/null +++ b/native/net/aienos_virtio_net.h @@ -0,0 +1,151 @@ +/* virtio-net (virtio 1.x, split virtqueues) driver core, polled, hosted-testable. + * + * The Rust reference crates/aienos-kernel/src/virtio_net.rs only parses PCI + * capabilities; it has no queue code. This data path is written from the + * virtio 1.x specification (sections 2.6 split virtqueues, 4.1.4 PCI + * capabilities, 5.1 network device) and is new C, not a port. + * + * Device access goes through vnet_ops only: register reads/writes inside the + * four mapped capability windows and the queue notify. No heap, no clock, no + * I/O of its own. All rings and packet buffers live in ONE caller-provided + * region (mem, len) that the device sees at bus address `dma`. The driver + * builds every descriptor itself from the descriptor id (never reads a + * descriptor back from shared memory) and checks it lies inside the region. + * Anything a device writes is untrusted: a used-ring entry with an id out of + * range, an id not outstanding, a length larger than the buffer, a used index + * that runs ahead of what was posted, or a short RX completion marks the + * device broken (sticky); the caller must reset and re-init. + * + * Features: VIRTIO_F_VERSION_1 is required; VIRTIO_NET_F_MAC is accepted when + * offered; every other feature is declined (no packed ring, no indirect + * descriptors, no event index, no offloads, no mergeable RX buffers, no + * control queue, no ACCESS_PLATFORM). One descriptor per packet; the 12-byte + * virtio 1.x net header precedes each frame. Interrupts are not used. */ +#ifndef AIENOS_VIRTIO_NET_H +#define AIENOS_VIRTIO_NET_H + +#include +#include + +#include "aienos_virtio_pci.h" + +#define VNET_QUEUE_MAX 256u /* largest queue size the driver uses */ +#define VNET_QUEUE_MIN 2u +#define VNET_HDR_LEN 12u /* struct virtio_net_hdr incl. num_buffers */ +#define VNET_FRAME_MAX 1514u /* Ethernet II without FCS, no VLAN tag */ +#define VNET_BUF_LEN (VNET_HDR_LEN + VNET_FRAME_MAX) +#define VNET_BUF_STRIDE 1536u + +#define VNET_F_MAC 5u +#define VNET_F_VERSION_1 32u + +/* virtio common configuration (virtio 1.x 4.1.4.3), byte offsets */ +#define VNET_CC_DFSELECT 0x00u +#define VNET_CC_DF 0x04u +#define VNET_CC_GFSELECT 0x08u +#define VNET_CC_GF 0x0cu +#define VNET_CC_MSIX 0x10u +#define VNET_CC_NUMQ 0x12u +#define VNET_CC_STATUS 0x14u +#define VNET_CC_CFGGEN 0x15u +#define VNET_CC_Q_SELECT 0x16u +#define VNET_CC_Q_SIZE 0x18u +#define VNET_CC_Q_MSIX 0x1au +#define VNET_CC_Q_ENABLE 0x1cu +#define VNET_CC_Q_NOFF 0x1eu +#define VNET_CC_Q_DESCLO 0x20u +#define VNET_CC_Q_DESCHI 0x24u +#define VNET_CC_Q_AVAILLO 0x28u +#define VNET_CC_Q_AVAILHI 0x2cu +#define VNET_CC_Q_USEDLO 0x30u +#define VNET_CC_Q_USEDHI 0x34u +#define VNET_CC_LEN 0x38u + +#define VNET_S_ACK 1u +#define VNET_S_DRIVER 2u +#define VNET_S_DRIVER_OK 4u +#define VNET_S_FEATURES_OK 8u +#define VNET_S_NEEDS_RESET 64u +#define VNET_S_FAILED 128u + +#define VNET_DESC_F_WRITE 2u +#define VNET_AVAIL_F_NO_INTERRUPT 1u + +enum { VNET_RX_QUEUE = 0, VNET_TX_QUEUE = 1 }; +typedef enum { VNET_WIN_COMMON = 0, VNET_WIN_NOTIFY = 1, VNET_WIN_ISR = 2, VNET_WIN_DEVICE = 3 } vnet_window; + +typedef struct { + void *ctx; + /* width is 1, 2 or 4; off + width is already checked against the window */ + uint32_t (*read)(void *ctx, vnet_window win, uint32_t off, unsigned width); + void (*write)(void *ctx, vnet_window win, uint32_t off, unsigned width, uint32_t val); + /* write `queue` (16 bit) at byte `off` of the notify window (checked) */ + void (*notify)(void *ctx, uint32_t off, uint16_t queue); +} vnet_ops; + +typedef enum { + VNET_OK = 0, + VNET_E_ARG = 1, /* NULL, bad queue size, frame too long, buffer too small */ + VNET_E_CAPS = 2, /* a required capability missing or too short */ + VNET_E_MEM = 3, /* region too small, misaligned, or bus range wraps */ + VNET_E_RESET_TIMEOUT = 4, /* device never read back status 0 */ + VNET_E_NO_VERSION_1 = 5, /* device does not offer VIRTIO_F_VERSION_1 */ + VNET_E_FEATURES = 6, /* device refused FEATURES_OK */ + VNET_E_QUEUES = 7, /* fewer than 2 queues, size 0, or queue already enabled */ + VNET_E_NOTIFY = 8, /* notify offset outside the notify window */ + VNET_E_CONFIG = 9, /* device config generation never settled */ + VNET_E_FULL = 10, /* no free TX descriptor (reclaim first) */ + VNET_E_DEVICE = 11, /* hostile or broken device completion; device is now broken */ + VNET_E_BROKEN = 12, /* sticky: an earlier VNET_E_DEVICE; reset and re-init */ +} vnet_err; + +typedef struct { + uint16_t size; /* negotiated, power of two, <= VNET_QUEUE_MAX */ + uint16_t avail_idx; /* driver shadow of avail->idx */ + uint16_t last_used; /* next used-ring slot to consume */ + uint16_t in_flight; /* descriptors posted and not yet completed */ + uint32_t notify_off; /* byte offset in the notify window */ + size_t desc_off, avail_off, used_off, buf_off; /* offsets into mem */ + uint8_t posted[VNET_QUEUE_MAX]; +} vnet_queue; + +typedef struct { + vnet_ops ops; + virtio_pci_caps caps; + uint8_t *mem; + uint64_t dma; + size_t mem_len; + uint64_t features; /* accepted feature bits */ + uint8_t has_mac; + uint8_t mac[6]; + uint8_t broken; + vnet_queue q[2]; /* VNET_RX_QUEUE, VNET_TX_QUEUE */ +} vnet_dev; + +/* Bytes of shared region needed for two queues of `qsize` (power of two in + * [VNET_QUEUE_MIN, VNET_QUEUE_MAX]); 0 if qsize is invalid. */ +size_t vnet_mem_size(uint16_t qsize); + +/* Reset and bring up the device: feature negotiation, both queues, all RX + * buffers posted, DRIVER_OK. mem and dma must be 16-byte aligned; mem_len >= + * vnet_mem_size(qsize). The device may offer a smaller queue: the driver then + * uses the largest power of two it offers (>= VNET_QUEUE_MIN). On failure the + * driver writes FAILED to the device status when it got that far. */ +vnet_err vnet_init(vnet_dev *d, const vnet_ops *ops, const virtio_pci_caps *caps, + void *mem, uint64_t dma, size_t mem_len, uint16_t qsize); + +/* Copy one Ethernet frame (14..VNET_FRAME_MAX bytes, no FCS) into a free TX + * buffer behind a zero virtio-net header, post it and notify. */ +vnet_err vnet_tx(vnet_dev *d, const uint8_t *frame, size_t len); + +/* Consume TX completions; *reclaimed (optional) gets the count. */ +vnet_err vnet_tx_reclaim(vnet_dev *d, unsigned *reclaimed); + +/* Take at most one received frame: copies it (header stripped) to out (cap >= + * VNET_FRAME_MAX), sets *len, sets *got to 1, and re-posts the buffer. *got 0: + * nothing pending. */ +vnet_err vnet_rx(vnet_dev *d, uint8_t *out, size_t cap, size_t *len, int *got); + +const char *vnet_err_name(vnet_err e); + +#endif diff --git a/native/net/aienos_virtio_pci.c b/native/net/aienos_virtio_pci.c index 397cde2f..a62706c2 100644 --- a/native/net/aienos_virtio_pci.c +++ b/native/net/aienos_virtio_pci.c @@ -60,7 +60,19 @@ virtio_pci_err virtio_pci_parse_caps(const uint8_t *config, size_t len, virtio_p if (cap_len < minimum || index + cap_len > len) return VIRTIO_PCI_SHORT_CAPABILITY; virtio_pci_region r = {1, config[index + 4], le32(config + index + 8), le32(config + index + 12)}; - if (r.bar > 5 || r.length == 0 || r.offset > UINT32_MAX - r.length) + /* Region checks apply only to the structures this driver maps + * (cfg_type 1..4). VIRTIO_PCI_CAP_PCI_CFG (5) legally carries + * bar 0 / offset 0 / length 0 (real QEMU virtio-net-pci does), and + * the spec tells drivers to ignore cfg types they do not use. The + * Rust reference checked every vendor cap and so refused real + * QEMU; AIENOS_VIRTIO_PCI_REFERENCE_COMPAT restores that order for + * the pinned Rust differential only (tests/net_diff.c). */ +#ifdef AIENOS_VIRTIO_PCI_REFERENCE_COMPAT + int mapped = 1; +#else + int mapped = kind >= 1 && kind <= 4; +#endif + if (mapped && (r.bar > 5 || r.length == 0 || r.offset > UINT32_MAX - r.length)) return VIRTIO_PCI_INVALID_REGION; virtio_pci_err e = VIRTIO_PCI_OK; switch (kind) { diff --git a/native/net/receipt.sh b/native/net/receipt.sh index 7a8e6289..1cc938a8 100755 --- a/native/net/receipt.sh +++ b/native/net/receipt.sh @@ -29,7 +29,7 @@ grep -q "^AIENOS_NET_SECURE_TESTS: PASS" "$log/sanitize.txt" || secure=FAIL grep -q "^X25519_RFC7748: PASS" "$log/test.txt" || secure=FAIL live="NET_RUST_DIFFERENTIAL_LIVE: NOT_RUN (no RUSTDIFF driver given; pinned digest checked)" if [ -n "${RUSTDIFF:-}" ]; then - if "$RUSTDIFF" < out/diff_corpus.bin > "$log/rust.txt" && cmp -s "$log/rust.txt" out/diff_c.txt; then + if "$RUSTDIFF" < out/diff_corpus.bin > "$log/rust.txt" && cmp -s "$log/rust.txt" out/diff_ref.txt; then live="NET_RUST_DIFFERENTIAL_LIVE: PASS (driver sha256 $(sha256sum "$RUSTDIFF" | cut -d' ' -f1))" else live="NET_RUST_DIFFERENTIAL_LIVE: FAIL"; status=FAIL @@ -48,10 +48,13 @@ body="$log/body.txt" echo "scope M6-B: hosted protocol code only; no real keys, no timing measurement, no formal protocol analysis" echo "sources:" for f in aienos_net.c aienos_net.h aienos_virtio_pci.c aienos_virtio_pci.h aienos_ctl.c aienos_ctl.h \ + aienos_virtio_net.c aienos_virtio_net.h \ x25519.c x25519.h aienos_sec.c aienos_sec.h ../argus/sha256.c \ ../crypto/aes.c ../crypto/polyval.c ../crypto/gcm_siv.c ../crypto/hmac.c ../crypto/ct.c \ ../sig/ed25519.c ../sig/sha512.c ../sig/ct.c \ - tests/net_test.c tests/ctl_test.c tests/net_diff.c tests/x25519_test.c tests/sec_test.c \ + tests/net_test.c tests/ctl_test.c tests/net_diff.c tests/x25519_test.c tests/sec_test.c tests/vnet_test.c \ + tests/fixtures/qemu822_virtio_net_pci_transitional.cfg tests/fixtures/qemu822_virtio_net_pci_modern.cfg \ + tests/fixtures/dump_qemu_virtio_net_cfg.sh \ Makefile receipt.sh; do echo " $(sha256sum "$f" | cut -d' ' -f1) native/net/$f" done @@ -61,7 +64,7 @@ body="$log/body.txt" done echo "differential corpus: $(sha256sum out/diff_corpus.bin | cut -d' ' -f1)" echo "results:" - grep -hE '^(net_test|ctl_test|lib-checks|NET_|CTL_|X25519_|AIENOS_NET_| [a-z+0-9-]+ +(done|link_down))' "$log/test.txt" "$log/sanitize.txt" "$log/mutants.txt" | sed 's/^/ /' + grep -hE '^(net_test|ctl_test|vnet_test|lib-checks|NET_|CTL_|VNET_|VIRTIO_|X25519_|AIENOS_NET_| [a-z+0-9-]+ +(done|link_down))' "$log/test.txt" "$log/sanitize.txt" "$log/mutants.txt" | sed 's/^/ /' echo " $live" echo "deferred (not claimed): native binding -> needs a C kernel (no-Rust rule; port order open);" echo " production identity and keys -> needs M5 key hierarchy (TRUST-1); M6-B runs on TEST keys only;" diff --git a/native/net/tests/fixtures/dump_qemu_virtio_net_cfg.sh b/native/net/tests/fixtures/dump_qemu_virtio_net_cfg.sh new file mode 100644 index 00000000..9997b749 --- /dev/null +++ b/native/net/tests/fixtures/dump_qemu_virtio_net_cfg.sh @@ -0,0 +1,28 @@ +#!/bin/sh +# Re-dump the 256-byte PCI config space of a real QEMU virtio-net-pci device, +# with no guest: QEMU's qtest protocol reads the ECAM window directly while the +# CPU stays paused (-S). Recorded with QEMU 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18), +# machine "virt", device at 00:01.0, highmem ECAM base 0x4010000000. +# transitional: default on the virt root bus (device id 0x1000) +# modern: disable-legacy=on (device id 0x1041) +# Single instance, nice'd; refuses to start if another QEMU runs. +# Usage: sh dump_qemu_virtio_net_cfg.sh transitional|modern > out.cfg +set -eu +case "${1:-}" in +transitional) extra="" ;; +modern) extra=",disable-legacy=on" ;; +*) echo "usage: $0 transitional|modern" >&2; exit 2 ;; +esac +if pgrep -x 'qemu-system-.*' >/dev/null 2>&1 || pgrep -f '^qemu-system' >/dev/null 2>&1; then + echo "another QEMU is running; wait for it" >&2; exit 3 +fi +o=0 +cmds=$(while [ $o -lt 256 ]; do printf 'readl 0x%x\n' $((0x4010008000 + o)); o=$((o + 4)); done) +printf '%s\n' "$cmds" | +timeout 30 nice -n 19 qemu-system-aarch64 -machine virt -accel tcg -S -qtest stdio \ + -display none -nodefaults -netdev user,id=n0 \ + -device "virtio-net-pci,addr=1,netdev=n0$extra" 2>/dev/null | +grep '^OK 0x' | while read -r _ h; do + x=$((h)) + printf "$(printf '\\%03o\\%03o\\%03o\\%03o' $((x & 255)) $((x >> 8 & 255)) $((x >> 16 & 255)) $((x >> 24 & 255)))" +done diff --git a/native/net/tests/fixtures/qemu822_virtio_net_pci_modern.cfg b/native/net/tests/fixtures/qemu822_virtio_net_pci_modern.cfg new file mode 100644 index 0000000000000000000000000000000000000000..17f4684dc084fc69b935109e260da7c3359501d0 GIT binary patch literal 256 zcmew& +#include +#include + +#include "../aienos_net.h" +#include "../aienos_virtio_net.h" +#include "../aienos_virtio_pci.h" + +static int failures, checks; +#define CHECK(c) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0) +#define EQ(a, b) CHECK((long long)(a) == (long long)(b)) + +static uint64_t rng = 0x26; +static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 11); } + +/* ---------------- capability parsing ---------------- */ +static int load(const char *path, uint8_t *b, size_t n) +{ + FILE *f = fopen(path, "rb"); + if (!f) return 0; + size_t got = fread(b, 1, n, f); + int extra = fgetc(f) != EOF; + fclose(f); + return got == n && !extra; +} +static void put32(uint8_t *c, size_t at, uint32_t v) { for (int i = 0; i < 4; i++) c[at + i] = (uint8_t)(v >> (8 * i)); } + +static uint8_t qemu_trans[256], qemu_modern[256]; + +static void test_qemu_caps(void) +{ + virtio_pci_caps r; + CHECK(load("tests/fixtures/qemu822_virtio_net_pci_transitional.cfg", qemu_trans, 256)); + CHECK(load("tests/fixtures/qemu822_virtio_net_pci_modern.cfg", qemu_modern, 256)); + EQ(qemu_trans[2] | qemu_trans[3] << 8, 0x1000); + EQ(qemu_modern[2] | qemu_modern[3] << 8, 0x1041); + const uint8_t *imgs[2] = {qemu_trans, qemu_modern}; + for (int k = 0; k < 2; k++) { + const uint8_t *c = imgs[k]; + /* the dump contains a PCI_CFG cap (type 5) with bar 0, offset 0, length 0 at 0x84 */ + EQ(c[0x84], 9); EQ(c[0x87], 5); EQ(c[0x86], 20); EQ(c[0x90] | c[0x91] | c[0x92] | c[0x93], 0); + EQ(virtio_pci_parse_caps(c, 256, &r), VIRTIO_PCI_OK); + CHECK(r.common_cfg.present && r.common_cfg.bar == 4 && r.common_cfg.offset == 0 && r.common_cfg.length == 0x1000); + CHECK(r.isr_cfg.present && r.isr_cfg.bar == 4 && r.isr_cfg.offset == 0x1000); + CHECK(r.device_cfg.present && r.device_cfg.bar == 4 && r.device_cfg.offset == 0x2000); + CHECK(r.notify_cfg.present && r.notify_cfg.bar == 4 && r.notify_cfg.offset == 0x3000 && r.notify_cfg.length == 0x1000); + CHECK(r.has_notify_off_multiplier && r.notify_off_multiplier == 4); + uint8_t big[4096] = {0}; + memcpy(big, c, 256); + EQ(virtio_pci_parse_caps(big, 4096, &r), VIRTIO_PCI_OK); + } + /* malformed variants of the real image stay refused */ + uint8_t x[256]; + memcpy(x, qemu_modern, 256); put32(x, 0x4c, 0); /* zero-length COMMON_CFG */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); put32(x, 0x7c, 0); /* zero-length NOTIFY_CFG */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); put32(x, 0x5c, 0); /* zero-length ISR_CFG */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); put32(x, 0x6c, 0); /* zero-length DEVICE_CFG */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); x[0x44] = 6; /* COMMON_CFG bad BAR */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); put32(x, 0x78, 0xfffff800); /* NOTIFY offset+length overflows */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION); + memcpy(x, qemu_modern, 256); x[0x86] = 12; /* PCI_CFG cap shorter than 16 */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY); + memcpy(x, qemu_modern, 256); x[0x34] = 0xfc; x[0xfc] = 9; x[0xfd] = 0; /* cap past config space */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY); + memcpy(x, qemu_modern, 256); x[0x41] = 0x98; /* loop back to MSI-X */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP); + memcpy(x, qemu_modern, 256); x[0x85] = 0x84; /* PCI_CFG self loop */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP); + memcpy(x, qemu_modern, 256); x[0x63] = 1; /* DEVICE_CFG retyped as a 2nd COMMON */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_DUPLICATE_CAPABILITY); + memcpy(x, qemu_modern, 256); x[0x99] = 0x30; /* pointer into the header */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_POINTER); + /* PCI_CFG (and any other unmapped type) is ignored even with a junk region */ + memcpy(x, qemu_modern, 256); x[0x88] = 7; put32(x, 0x8c, 0xffffffff); put32(x, 0x90, 5); + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK); + memcpy(x, qemu_modern, 256); x[0x87] = 9; /* VENDOR_CFG-type, length 0 */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK); + /* required caps missing: the parser reports them absent, vnet_init refuses */ + memcpy(x, qemu_modern, 256); x[0x73] = 0; /* NOTIFY retyped to 0 */ + EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK); + CHECK(!r.notify_cfg.present); /* vnet_init then refuses: test_init, VNET_E_CAPS */ +} + +/* ---------------- simulated device ---------------- */ +typedef struct { + uint16_t max, size, enable, noff; + uint64_t desc, avail, used; + uint16_t last_avail, used_idx; +} simq; + +typedef struct { + uint8_t *mem; uint64_t dma; size_t mem_len; + uint64_t dev_features, drv_features; + uint32_t dfsel, gfsel; + uint8_t status, cfggen; + uint16_t numq, qsel; + simq q[2]; + uint8_t mac[6]; + uint32_t mult; + /* knobs */ + int refuse_features, reset_stuck, cfggen_unstable, preenabled; + uint32_t noff_override; + /* observations */ + unsigned notifies[2], faults, status_writes; + uint8_t wire[8][2048]; size_t wire_len[8]; unsigned nwire; +} sim; + +static uint8_t *bus(sim *s, uint64_t a, size_t n) +{ + if (a < s->dma || a - s->dma > s->mem_len || n > s->mem_len - (a - s->dma)) { s->faults++; return NULL; } + return s->mem + (a - s->dma); +} +static uint16_t g16(const uint8_t *p) { uint16_t v; memcpy(&v, p, 2); return v; } +static uint32_t g32(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v; } +static uint64_t g64(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v; } +static void s16(uint8_t *p, uint16_t v) { memcpy(p, &v, 2); } +static void s32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); } + +static uint32_t sim_read(void *ctx, vnet_window w, uint32_t off, unsigned width) +{ + sim *s = ctx; + if (w == VNET_WIN_DEVICE) return off < 6 && width == 1 ? s->mac[off] : 0; + if (w != VNET_WIN_COMMON) return 0; + simq *q = &s->q[s->qsel < 2 ? s->qsel : 0]; + switch (off) { + case VNET_CC_DF: return (uint32_t)(s->dev_features >> (s->dfsel ? 32 : 0)); + case VNET_CC_NUMQ: return s->numq; + case VNET_CC_STATUS: return s->reset_stuck ? 1 : s->status; + case VNET_CC_CFGGEN: return s->cfggen_unstable ? s->cfggen++ : s->cfggen; + case VNET_CC_Q_SIZE: return s->qsel < 2 ? q->size : 0; + case VNET_CC_Q_ENABLE: return s->qsel < 2 ? q->enable : 0; + case VNET_CC_Q_NOFF: return s->noff_override ? s->noff_override : q->noff; + } + return 0; +} +static void sim_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v) +{ + sim *s = ctx; + (void)width; + if (w != VNET_WIN_COMMON) { s->faults++; return; } + simq *q = &s->q[s->qsel < 2 ? s->qsel : 0]; + switch (off) { + case VNET_CC_DFSELECT: s->dfsel = v; break; + case VNET_CC_GFSELECT: s->gfsel = v; break; + case VNET_CC_GF: + if (s->gfsel) s->drv_features = (s->drv_features & 0xffffffffull) | (uint64_t)v << 32; + else s->drv_features = (s->drv_features & ~0xffffffffull) | v; + break; + case VNET_CC_STATUS: + s->status_writes++; + if (v == 0) { s->status = 0; for (int i = 0; i < 2; i++) { s->q[i].enable = s->preenabled; s->q[i].size = s->q[i].max; s->q[i].last_avail = s->q[i].used_idx = 0; } break; } + if ((v & VNET_S_FEATURES_OK) && !(s->status & VNET_S_FEATURES_OK)) { + if (s->refuse_features || (s->drv_features & ~s->dev_features)) v &= ~VNET_S_FEATURES_OK; + } + s->status = (uint8_t)v; + break; + case VNET_CC_Q_SELECT: s->qsel = (uint16_t)v; break; + case VNET_CC_Q_SIZE: if (v > q->max) s->faults++; q->size = (uint16_t)v; break; + case VNET_CC_Q_ENABLE: q->enable = (uint16_t)v; break; + case VNET_CC_Q_DESCLO: q->desc = (q->desc & ~0xffffffffull) | v; break; + case VNET_CC_Q_DESCHI: q->desc = (q->desc & 0xffffffffull) | (uint64_t)v << 32; break; + case VNET_CC_Q_AVAILLO: q->avail = (q->avail & ~0xffffffffull) | v; break; + case VNET_CC_Q_AVAILHI: q->avail = (q->avail & 0xffffffffull) | (uint64_t)v << 32; break; + case VNET_CC_Q_USEDLO: q->used = (q->used & ~0xffffffffull) | v; break; + case VNET_CC_Q_USEDHI: q->used = (q->used & 0xffffffffull) | (uint64_t)v << 32; break; + default: break; + } +} +static void sim_notify(void *ctx, uint32_t off, uint16_t queue) +{ + sim *s = ctx; + if (!(s->status & VNET_S_DRIVER_OK) || queue > 1 || off != (uint32_t)s->q[queue].noff * s->mult) s->faults++; + else s->notifies[queue]++; +} +static const vnet_ops sim_ops = {0, sim_read, sim_write, sim_notify}; + +static void sim_reset(sim *s, uint8_t *mem, uint64_t dma, size_t len) +{ + memset(s, 0, sizeof *s); + s->mem = mem; s->dma = dma; s->mem_len = len; + s->dev_features = (1ull << VNET_F_VERSION_1) | (1ull << VNET_F_MAC); + s->numq = 3; s->mult = 4; + s->q[0].max = s->q[1].max = 256; + s->q[0].noff = 0; s->q[1].noff = 1; + for (int i = 0; i < 6; i++) s->mac[i] = (uint8_t)(0x52 + i); +} + +static void push_used(sim *s, int qi, uint32_t id, uint32_t len) +{ + simq *q = &s->q[qi]; + uint8_t *u = bus(s, q->used, 6 + 8u * q->size); + if (!u) return; + uint8_t *e = u + 4 + 8u * (q->used_idx % q->size); + s32(e, id); s32(e + 4, len); + q->used_idx++; + __atomic_thread_fence(__ATOMIC_RELEASE); + s16(u + 2, q->used_idx); +} + +/* Read the next available descriptor of queue qi; checks every field. */ +static int pop_avail(sim *s, int qi, uint16_t *id, uint8_t **buf, uint32_t *len, uint16_t *flags) +{ + simq *q = &s->q[qi]; + uint8_t *a = bus(s, q->avail, 6 + 2u * q->size); + if (!a || !q->enable) return 0; + if (g16(a + 2) == q->last_avail) return 0; + if ((uint16_t)(g16(a + 2) - q->last_avail) > q->size) { s->faults++; return 0; } + *id = g16(a + 4 + 2u * (q->last_avail % q->size)); + q->last_avail++; + if (*id >= q->size) { s->faults++; return 0; } + uint8_t *d = bus(s, q->desc + 16u * *id, 16); + if (!d) return 0; + *len = g32(d + 8); *flags = g16(d + 12); + if (*flags & 1u) s->faults++; /* NEXT: the driver never chains */ + *buf = bus(s, g64(d), *len); + return *buf != NULL; +} + +/* Device transmits everything pending on TX (completes with len 0). */ +static unsigned sim_tx(sim *s) +{ + unsigned n = 0; uint16_t id, fl; uint8_t *b; uint32_t len; + while (pop_avail(s, 1, &id, &b, &len, &fl)) { + if ((fl & VNET_DESC_F_WRITE) || len < VNET_HDR_LEN || len > VNET_BUF_LEN) s->faults++; + for (unsigned i = 0; i < VNET_HDR_LEN; i++) if (b[i]) s->faults++; + unsigned slot = s->nwire++ % 8; + s->wire_len[slot] = len - VNET_HDR_LEN; + memcpy(s->wire[slot], b + VNET_HDR_LEN, len - VNET_HDR_LEN); + push_used(s, 1, id, 0); + n++; + } + return n; +} +/* Device receives one frame into the next RX buffer. */ +static int sim_rx(sim *s, const uint8_t *f, size_t n) +{ + uint16_t id, fl; uint8_t *b; uint32_t len; + if (!pop_avail(s, 0, &id, &b, &len, &fl)) return 0; + if (!(fl & VNET_DESC_F_WRITE) || len < VNET_HDR_LEN + n) { s->faults++; return 0; } + memset(b, 0, VNET_HDR_LEN); s16(b + 10, 1); + memcpy(b + VNET_HDR_LEN, f, n); + push_used(s, 0, id, (uint32_t)(VNET_HDR_LEN + n)); + return 1; +} + +/* every descriptor in both tables lies inside the shared region */ +static void check_descs(const vnet_dev *d) +{ + for (int qi = 0; qi < 2; qi++) { + const vnet_queue *q = &d->q[qi]; + for (unsigned i = 0; i < q->size; i++) { + const uint8_t *e = d->mem + q->desc_off + 16u * i; + uint64_t a = g64(e); uint32_t l = g32(e + 8); + if (l == 0) continue; + CHECK(a >= d->dma && a - d->dma <= d->mem_len && l <= d->mem_len - (a - d->dma)); + CHECK(l <= VNET_BUF_LEN); + } + } +} + +static uint8_t region[1 << 20] __attribute__((aligned(4096))); +#define DMA 0x80000000ull + +static int bring_up(vnet_dev *d, sim *s, uint16_t qsize) +{ + vnet_ops o = sim_ops; o.ctx = s; + virtio_pci_caps c; + if (virtio_pci_parse_caps(qemu_modern, 256, &c) != VIRTIO_PCI_OK) return -1; + return vnet_init(d, &o, &c, region, DMA, sizeof region, qsize); +} + +static void test_init(void) +{ + sim s; vnet_dev d; + EQ(vnet_mem_size(3), 0); EQ(vnet_mem_size(1), 0); EQ(vnet_mem_size(512), 0); + CHECK(vnet_mem_size(256) <= sizeof region); + + sim_reset(&s, region, DMA, sizeof region); + s.dev_features = ~0ull; /* offers everything, incl. packed, indirect, ACCESS_PLATFORM */ + EQ(bring_up(&d, &s, 256), VNET_OK); + EQ(s.drv_features, (1ull << VNET_F_VERSION_1) | (1ull << VNET_F_MAC)); + EQ(s.status, VNET_S_ACK | VNET_S_DRIVER | VNET_S_FEATURES_OK | VNET_S_DRIVER_OK); + CHECK(d.has_mac && d.mac[0] == 0x52 && d.mac[5] == 0x57); + EQ(d.q[0].size, 256); EQ(d.q[1].size, 256); + EQ(s.q[0].enable, 1); EQ(s.q[1].enable, 1); + EQ(s.notifies[0], 1); EQ(s.faults, 0); + check_descs(&d); + + /* without MAC */ + sim_reset(&s, region, DMA, sizeof region); + s.dev_features = 1ull << VNET_F_VERSION_1; + EQ(bring_up(&d, &s, 8), VNET_OK); + CHECK(!d.has_mac); EQ(s.drv_features, 1ull << VNET_F_VERSION_1); + + /* the device offers less: largest power of two at or below */ + sim_reset(&s, region, DMA, sizeof region); + s.q[0].max = 100; s.q[1].max = 64; + EQ(bring_up(&d, &s, 256), VNET_OK); + EQ(d.q[0].size, 64); EQ(d.q[1].size, 64); EQ(s.faults, 0); + + struct { const char *what; int knob; vnet_err want; } cases[] = { + {"no VERSION_1 (legacy-only device)", 1, VNET_E_NO_VERSION_1}, + {"FEATURES_OK refused", 2, VNET_E_FEATURES}, + {"reset never completes", 3, VNET_E_RESET_TIMEOUT}, + {"one queue only", 4, VNET_E_QUEUES}, + {"queue size 0", 5, VNET_E_QUEUES}, + {"queue size 1", 6, VNET_E_QUEUES}, + {"queue already enabled", 7, VNET_E_QUEUES}, + {"notify offset outside window", 8, VNET_E_NOTIFY}, + {"config generation never settles", 9, VNET_E_CONFIG}, + }; + for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) { + sim_reset(&s, region, DMA, sizeof region); + switch (cases[i].knob) { + case 1: s.dev_features = 1ull << VNET_F_MAC; break; + case 2: s.refuse_features = 1; break; + case 3: s.reset_stuck = 1; break; + case 4: s.numq = 1; break; + case 5: s.q[1].max = 0; break; + case 6: s.q[0].max = 1; break; + case 7: s.preenabled = 1; break; + case 8: s.noff_override = 0x400; break; /* 0x400 * 4 + 2 > 0x1000 */ + case 9: s.cfggen_unstable = 1; break; + } + vnet_err e = bring_up(&d, &s, 16); + if (e != cases[i].want) fprintf(stderr, "init case '%s': got %s\n", cases[i].what, vnet_err_name(e)); + EQ(e, cases[i].want); + CHECK(d.broken); + if (cases[i].knob != 3) CHECK(s.status & VNET_S_FAILED); + CHECK(!(s.status & VNET_S_DRIVER_OK)); + EQ(s.notifies[0] + s.notifies[1], 0); + uint8_t f[64] = {0}; + EQ(vnet_tx(&d, f, 60), VNET_E_BROKEN); + } + + /* argument and memory refusals */ + vnet_ops o = sim_ops; o.ctx = &s; + virtio_pci_caps c, bad; + EQ(virtio_pci_parse_caps(qemu_modern, 256, &c), VIRTIO_PCI_OK); + sim_reset(&s, region, DMA, sizeof region); + EQ(vnet_init(&d, &o, &c, region, DMA, sizeof region, 6), VNET_E_ARG); + EQ(vnet_init(&d, &o, &c, region, DMA, vnet_mem_size(16) - 1, 16), VNET_E_MEM); + EQ(vnet_init(&d, &o, &c, region + 8, DMA, sizeof region - 8, 16), VNET_E_MEM); + EQ(vnet_init(&d, &o, &c, region, DMA + 4, sizeof region, 16), VNET_E_MEM); + EQ(vnet_init(&d, &o, &c, region, UINT64_MAX - 0xfff, sizeof region, 16), VNET_E_MEM); + bad = c; bad.notify_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + bad = c; bad.has_notify_off_multiplier = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + bad = c; bad.common_cfg.length = 0x37; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + bad = c; bad.isr_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + bad = c; bad.device_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + bad = c; bad.device_cfg.length = 4; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS); + EQ(s.notifies[0] + s.notifies[1], 0); +} + +static void test_datapath(void) +{ + sim s; vnet_dev d; + sim_reset(&s, region, DMA, sizeof region); + EQ(bring_up(&d, &s, 4), VNET_OK); + uint8_t f[VNET_FRAME_MAX], out[VNET_FRAME_MAX]; + size_t n; int got; unsigned rc; + for (size_t i = 0; i < sizeof f; i++) f[i] = (uint8_t)(i * 7 + 1); + EQ(vnet_tx(&d, f, 13), VNET_E_ARG); + EQ(vnet_tx(&d, f, VNET_FRAME_MAX + 1), VNET_E_ARG); + EQ(vnet_rx(&d, out, VNET_FRAME_MAX - 1, &n, &got), VNET_E_ARG); + EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_OK); EQ(got, 0); + /* fill TX, then full, then reclaim */ + for (int i = 0; i < 4; i++) EQ(vnet_tx(&d, f, 60 + (size_t)i), VNET_OK); + EQ(vnet_tx(&d, f, 60), VNET_E_FULL); + EQ(s.notifies[1], 4); + EQ(sim_tx(&s), 4); + EQ(s.wire_len[3], 63); CHECK(memcmp(s.wire[3], f, 63) == 0); + EQ(vnet_tx_reclaim(&d, &rc), VNET_OK); EQ(rc, 4); + EQ(vnet_tx(&d, f, VNET_FRAME_MAX), VNET_OK); + EQ(sim_tx(&s), 1); + EQ(s.wire_len[4], VNET_FRAME_MAX); CHECK(memcmp(s.wire[4], f, VNET_FRAME_MAX) == 0); + /* 70,000 frames each way: 16-bit ring indices wrap */ + unsigned bad = 0; + for (unsigned i = 0; i < 70000; i++) { + size_t len = 14 + (i * 131u) % (VNET_FRAME_MAX - 13); + f[0] = (uint8_t)i; f[len - 1] = (uint8_t)(i >> 8); + if (!sim_rx(&s, f, len)) bad++; + if (vnet_rx(&d, out, sizeof out, &n, &got) != VNET_OK || !got || n != len || memcmp(out, f, len)) bad++; + if (vnet_tx_reclaim(&d, NULL) != VNET_OK || vnet_tx(&d, f, len) != VNET_OK || sim_tx(&s) != 1) bad++; + if (s.wire_len[(s.nwire - 1) % 8] != len || memcmp(s.wire[(s.nwire - 1) % 8], f, len)) bad++; + } + EQ(bad, 0); + CHECK(s.q[0].used_idx < 70000u); /* wrapped */ + EQ(s.faults, 0); + check_descs(&d); +} + +/* hostile completions: each must be refused and leave the device broken */ +static void test_hostile(void) +{ + sim s; vnet_dev d; + uint8_t f[128] = {0}, out[VNET_FRAME_MAX]; + size_t n; int got; + for (int k = 0; k < 9; k++) { + sim_reset(&s, region, DMA, sizeof region); + EQ(bring_up(&d, &s, 8), VNET_OK); + vnet_err e = VNET_OK; + switch (k) { + case 0: push_used(&s, 0, 8, 60); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* id == size */ + case 1: push_used(&s, 0, 0xffffffffu, 60); e = vnet_rx(&d, out, sizeof out, &n, &got); break; + case 2: push_used(&s, 0, 3, VNET_BUF_LEN + 1); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* oversized */ + case 3: push_used(&s, 0, 3, VNET_HDR_LEN - 1); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* short */ + case 4: { /* used idx runs ahead of everything posted */ + uint8_t *u = region + d.q[0].used_off; s16(u + 2, 9); + e = vnet_rx(&d, out, sizeof out, &n, &got); break; } + case 5: e = vnet_tx(&d, f, 60); push_used(&s, 1, 1, 0); if (!e) e = vnet_tx_reclaim(&d, NULL); break; /* never posted */ + case 6: vnet_tx(&d, f, 60); vnet_tx(&d, f, 60); push_used(&s, 1, 0, 0); push_used(&s, 1, 0, 0); + e = vnet_tx_reclaim(&d, NULL); break; /* double completion */ + case 7: push_used(&s, 1, 0, 0); e = vnet_tx_reclaim(&d, NULL); break; /* completion with nothing posted */ + case 8: vnet_tx(&d, f, 60); push_used(&s, 1, 0, 0x10000); e = vnet_tx_reclaim(&d, NULL); break; /* TX len oversized */ + } + if (e != VNET_E_DEVICE) fprintf(stderr, "hostile case %d: got %s\n", k, vnet_err_name(e)); + EQ(e, VNET_E_DEVICE); + EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_E_BROKEN); + EQ(vnet_tx(&d, f, 60), VNET_E_BROKEN); + EQ(vnet_tx_reclaim(&d, NULL), VNET_E_BROKEN); + } + /* a hostile device rewriting the descriptor table cannot steer the driver: + re-posted descriptors are rebuilt from the id */ + sim_reset(&s, region, DMA, sizeof region); + EQ(bring_up(&d, &s, 8), VNET_OK); + memset(region + d.q[0].desc_off, 0xa5, 16u * 8); + for (int i = 0; i < 8; i++) { push_used(&s, 0, (uint32_t)i, 60); EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_OK); EQ(got, 1); EQ(n, 48); } + check_descs(&d); + + /* seeded random hostile used-ring traffic: never crashes, never delivers + more than a frame, never writes a descriptor outside the region */ + unsigned delivered = 0, refused = 0; + for (unsigned round = 0; round < 2000; round++) { + sim_reset(&s, region, DMA, sizeof region); + if (bring_up(&d, &s, (uint16_t)(2u << (rnd() % 7))) != VNET_OK) { CHECK(0); continue; } + for (unsigned step = 0; step < 40 && !d.broken; step++) { + uint32_t r = rnd(); + switch (r % 6) { + case 0: sim_rx(&s, f, 14 + rnd() % 100); break; + case 1: vnet_tx(&d, f, 14 + rnd() % 100); break; + case 2: sim_tx(&s); break; + case 3: push_used(&s, (int)(rnd() % 2), rnd() % (d.q[0].size + 2), rnd() % 2 ? rnd() % (VNET_BUF_LEN + 4) : 60); break; + case 4: { uint8_t *u = region + d.q[rnd() % 2].used_off; s16(u + 2, (uint16_t)(g16(u + 2) + rnd() % 3)); break; } + default: break; + } + vnet_err e = vnet_rx(&d, out, sizeof out, &n, &got); + if (e == VNET_OK && got) { delivered++; CHECK(n <= VNET_FRAME_MAX); } + if (e == VNET_OK) e = vnet_tx_reclaim(&d, NULL); + if (e != VNET_OK) { CHECK(e == VNET_E_DEVICE && d.broken); refused++; } + check_descs(&d); + CHECK(d.q[0].in_flight <= d.q[0].size && d.q[1].in_flight <= d.q[1].size); + } + } + CHECK(delivered > 1000 && refused > 500); + printf("vnet_test: hostile fuzz delivered=%u refused=%u\n", delivered, refused); +} + +/* M6-A UDP frame out through TX, reply in through RX, parsed by M6-A */ +static void test_m6a_udp(void) +{ + sim s; vnet_dev d; + sim_reset(&s, region, DMA, sizeof region); + EQ(bring_up(&d, &s, 16), VNET_OK); + net_mac me, peer = {{0x02, 0, 0, 0, 0, 2}}; + memcpy(me.b, d.mac, 6); + net_ipv4 ip_me = {{10, 0, 2, 15}}, ip_peer = {{10, 0, 2, 2}}; + const char *msg = "aienos lane26 ping"; + uint8_t udp[256], ip[512], frame[VNET_FRAME_MAX]; + size_t ul, il, fl; + net_udp_header uh = {40000, 7}; + EQ(net_udp_build(&uh, (const uint8_t *)msg, strlen(msg), ip_me, ip_peer, udp, sizeof udp, &ul), NET_OK); + net_ipv4_header ih = {0, 1, 64, NET_IPPROTO_UDP, ip_me, ip_peer}; + EQ(net_ipv4_build(&ih, udp, ul, ip, sizeof ip, &il), NET_OK); + net_eth_frame ef = {peer, me, NET_ETHERTYPE_IPV4, ip, il}; + EQ(net_eth_build(&ef, frame, sizeof frame, &fl), NET_OK); + EQ(vnet_tx(&d, frame, fl), VNET_OK); + EQ(sim_tx(&s), 1); + EQ(s.wire_len[0], fl); CHECK(memcmp(s.wire[0], frame, fl) == 0); + + /* the device side parses what went out with M6-A and answers */ + net_eth_frame pe; net_ipv4_header pih; net_udp_header puh; + const uint8_t *pl, *data; size_t pll, dl; + EQ(net_eth_parse(s.wire[0], s.wire_len[0], &pe), NET_OK); + EQ(net_ipv4_parse(pe.payload, pe.payload_len, &pih, &pl, &pll), NET_OK); + EQ(net_udp_parse(pl, pll, pih.source, pih.destination, &puh, &data, &dl), NET_OK); + CHECK(dl == strlen(msg) && memcmp(data, msg, dl) == 0); + const char *rep = "pong from simulated device"; + net_udp_header rh = {puh.destination_port, puh.source_port}; + EQ(net_udp_build(&rh, (const uint8_t *)rep, strlen(rep), ip_peer, ip_me, udp, sizeof udp, &ul), NET_OK); + net_ipv4_header rih = {0, 9, 64, NET_IPPROTO_UDP, ip_peer, ip_me}; + EQ(net_ipv4_build(&rih, udp, ul, ip, sizeof ip, &il), NET_OK); + net_eth_frame ref = {me, peer, NET_ETHERTYPE_IPV4, ip, il}; + EQ(net_eth_build(&ref, frame, sizeof frame, &fl), NET_OK); + EQ(sim_rx(&s, frame, fl), 1); + + uint8_t out[VNET_FRAME_MAX]; size_t n; int got; + EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_OK); EQ(got, 1); EQ(n, fl); + EQ(net_eth_parse(out, n, &pe), NET_OK); + CHECK(memcmp(pe.destination.b, d.mac, 6) == 0); + EQ(net_ipv4_parse(pe.payload, pe.payload_len, &pih, &pl, &pll), NET_OK); + CHECK(memcmp(pih.destination.b, ip_me.b, 4) == 0); + EQ(net_udp_parse(pl, pll, pih.source, pih.destination, &puh, &data, &dl), NET_OK); + EQ(puh.destination_port, 40000); EQ(puh.source_port, 7); + CHECK(dl == strlen(rep) && memcmp(data, rep, dl) == 0); + EQ(vnet_tx_reclaim(&d, NULL), VNET_OK); + EQ(s.faults, 0); + int ok = failures == 0; + printf("VNET_M6A_UDP_ROUNDTRIP_SIM: %s\n", ok ? "PASS" : "FAIL"); +} + +int main(void) +{ + test_qemu_caps(); + int caps_ok = failures == 0; + printf("VIRTIO_PCI_QEMU822_CAPS: %s (real QEMU 8.2.2 virtio-net-pci config dumps, 0x1000 + 0x1041)\n", + caps_ok ? "PASS" : "FAIL"); + test_init(); + test_datapath(); + test_hostile(); + test_m6a_udp(); + printf("vnet_test: %d checks, %d failures\n", checks, failures); + printf("VNET_SIM_DATAPATH: %s (simulated device only; no real device, DMA, IRQ or QEMU round trip)\n", + failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +} From dd2e479f1c0c91695f0a9a44a926bb171e4c98af Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 03:51:38 -0500 Subject: [PATCH 2/2] native/net: Lane 26 host receipt for a12b05c (M6A_NET_HOST PASS, hosted only) Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- ...130f513ead5c8e9b68e91b1179d0af9978e95b.txt | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 native/net/receipts/m6a_net_host_8359cd99c5244f9df9f3786eb8130f513ead5c8e9b68e91b1179d0af9978e95b.txt diff --git a/native/net/receipts/m6a_net_host_8359cd99c5244f9df9f3786eb8130f513ead5c8e9b68e91b1179d0af9978e95b.txt b/native/net/receipts/m6a_net_host_8359cd99c5244f9df9f3786eb8130f513ead5c8e9b68e91b1179d0af9978e95b.txt new file mode 100644 index 00000000..75bb6ba5 --- /dev/null +++ b/native/net/receipts/m6a_net_host_8359cd99c5244f9df9f3786eb8130f513ead5c8e9b68e91b1179d0af9978e95b.txt @@ -0,0 +1,92 @@ +AIENOS M6-A hosted C network stack + M6-B secure transport receipt +commit: a12b05ceecedbeca4767e9b9bbdd527fa9ebbc55 +host: Linux 7.0.0-1019-nvidia aarch64 +cc: cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +scope: host tests only (no QEMU, no hardware, no native binding, TEST identity only) +scope M6-B: hosted protocol code only; no real keys, no timing measurement, no formal protocol analysis +sources: + 9fe48c2a25f7872089d7495cc31860203647e109c097e708ec21f6d7f7a2a19a native/net/aienos_net.c + 61d3843a95a143ef546f00f2f6e460abc3e337bc8fb15d88d28df6bd644c9d4b native/net/aienos_net.h + 5badaedfcf9015b3a2a239a958e305464982c1ab1d92f18b5f614b5e881dd691 native/net/aienos_virtio_pci.c + 93cad17472829580c121777e574f1993ee7acd9ce45138f50dde33f92e55136b native/net/aienos_virtio_pci.h + 1d5117f0cdf31d4686d4354f01a9100034ddab2b1feeb5c805db45d89992eca6 native/net/aienos_ctl.c + ccdba3e1611b646329d6ce67a4ff661f4ad85601ca13a505b362259170c8b92a native/net/aienos_ctl.h + 245c6965f5eece970432e28ad5d565c1bbd8f912cf9adef75581e2a9c743a153 native/net/aienos_virtio_net.c + 14655c05cdf931b15fe1eb28d9e72c2afed3e5a6c15ba254cef1abd2e4b8aa24 native/net/aienos_virtio_net.h + 5e0e002018b4a4fd696a5d2a23302a2019dd74b72ad47b7adfd1236c3c5c5282 native/net/x25519.c + 213335960f2f1cc365a66d7981c0075b993b132139eea5396c113662951ff445 native/net/x25519.h + 2ae2942144db438c39744c3b4c20bbe57706190ee354c7a1938daa9932f47467 native/net/aienos_sec.c + ff92be9bec46e03f58013c7877b3a11d915704dc9579992ad6e3a820c56f1898 native/net/aienos_sec.h + 716b6d0e4e15a1c5696252d831f7e2cf2a287f2e64f7a2417f6f4908eefc56bd native/net/../argus/sha256.c + 4600b408ea501f3b5cb0a9f833c9300ffd970e359c2bee2e60f93e1ce244d973 native/net/../crypto/aes.c + f4b4f71c40687a4bfa5a00e61c5eb06d46edb5b526e041c920a4bfbd616d9c35 native/net/../crypto/polyval.c + 394bc53984b516c3567ecaf2c108ed2a98b143ac658bc47a87b04787aa25c7be native/net/../crypto/gcm_siv.c + 3f774149c3e734dc85911431cb21e40e4e4ed65caea66536795ed5327fef563f native/net/../crypto/hmac.c + 3c72a3ac1e00550cba4c91339ea670520884e21fd3edaa34008535c768fbc75f native/net/../crypto/ct.c + 33714a7d8605cda4b13c13d7d2f75e6116bafebe95af4c30a11b8e04d82f3026 native/net/../sig/ed25519.c + 110c6322c699ce77011906486aa5ff3cb8becdede12534d20efb92711b90ba22 native/net/../sig/sha512.c + 59b7d18636e7acfc4f5f78d7c6abd7f7dd52cc45a342fb4888be6b4c83413035 native/net/../sig/ct.c + 151bc87ef10b31cb69f8493c30258cccdc7a37011ce662f782c95bacadfe8dea native/net/tests/net_test.c + 9a104222986172cd99b84304fad598cfeeef07b641d2bb2239ca46bbf47093f6 native/net/tests/ctl_test.c + eecd1179afe1c907c6d1d53aa01dd750a705938dc88e8a3ad176568bf8e4b1e3 native/net/tests/net_diff.c + f8bd1c4cfdfcb96e03316dd0535f5d021ce013c3a3e864fb43cb627ba0dc9c5b native/net/tests/x25519_test.c + ff80bbaba6c38139c78a2f0e6f92e184fc964946eb7df9ee915adfcdcb79ff7b native/net/tests/sec_test.c + c18244c51a977e7a0497389baf19351d3796b03c44d7b4db19b1eb948f3f97fa native/net/tests/vnet_test.c + e4b2b588c1e9feb1fc2d2a9c1b33556d92e2acfbba06c38b95f2cb675f72498c native/net/tests/fixtures/qemu822_virtio_net_pci_transitional.cfg + 0a794099f3bcdea39a851a0f484911c4ce91b790e39ef66546a8a3dee5f8a8d0 native/net/tests/fixtures/qemu822_virtio_net_pci_modern.cfg + 7eb402f9d3fb2d920ec272954e4ef7507dc39093209f1e8d98bf659cb15632bd native/net/tests/fixtures/dump_qemu_virtio_net_cfg.sh + 89df8da60616d1d8e996189c1e4fd92e5af8bb2056512d58c75dac3f9da449cc native/net/Makefile + 2f7d6f32b16ef8b6e6f05a09065020518100eb6b36c8445e765be89d755a1235 native/net/receipt.sh +rust reference (spec, unmodified): + 4fa8265a900fb62d78d0a35bed9420dfc449e2aa20e74f5f5f3cd884c17cd449 crates/aienos-kernel/src/net.rs + b66117d6dd7b4fc534d9943559c96cd5297da337e07ac2699aad87ae76acd264 crates/aienos-kernel/src/virtio_net.rs +differential corpus: 88e88c492ed54d87e3f458cd7e2f0dd0d063a853357dc5a49fcd0d942ddb8134 +results: + lib-checks PASS (no heap, clock, I/O or ambient randomness; TEST identity only) + NET_RUST_DIFFERENTIAL_PINNED: PASS (200000 records, reference-compat build) + NET_DIFFERENTIAL_FIXED_PINNED: PASS (C-only pin) + NET_DIFFERENTIAL_DIVERGENCE: PASS (1622 records differ, all reference InvalidRegion) + net_test seed=0x6a5e11 iters=200000 checks=398624 failures=0 + NET_PROTOCOL_HOST: PASS + clean done=1 ticks=63 retx=0/0 dup=0/0 gap=0/0 badtag=0/0 malformed=0/0 transcript=0365b2fe4b2ccc0a + loss30 done=1 ticks=326 retx=181/159 dup=7/4 gap=90/107 badtag=0/0 malformed=0/0 transcript=fcf753c7ec485c53 + loss+dup+reorder done=1 ticks=633 retx=171/190 dup=22/23 gap=142/151 badtag=0/0 malformed=0/0 transcript=617bdd0a7410d59b + corrupt20 done=1 ticks=231 retx=121/129 dup=18/14 gap=73/67 badtag=37/40 malformed=7/8 transcript=9b44fe14e4fbfc53 + everything done=1 ticks=851 retx=243/150 dup=13/8 gap=92/150 badtag=20/26 malformed=4/4 transcript=a92bca84a176256b + blackout-recover done=1 ticks=483 retx=103/120 dup=0/0 gap=90/67 badtag=0/0 malformed=0/0 transcript=e47a0b838ca48036 + blackout-dead link_down at tick 31 after 3 retransmissions + ctl_test checks=21690 failures=0 + CTL_TRANSPORT_LOSS_RECOVERY: PASS + X25519_RFC7748: PASS (149 checks, 0 failures, 1000 iterations) + AIENOS_NET_SECURE_TESTS: PASS (57248 checks, 0 failures) + VIRTIO_PCI_QEMU822_CAPS: PASS (real QEMU 8.2.2 virtio-net-pci config dumps, 0x1000 + 0x1041) + vnet_test: hostile fuzz delivered=1929 refused=1999 + VNET_M6A_UDP_ROUNDTRIP_SIM: PASS + vnet_test: 789204 checks, 0 failures + VNET_SIM_DATAPATH: PASS (simulated device only; no real device, DMA, IRQ or QEMU round trip) + net_test seed=0x6a5e11 iters=50000 checks=99821 failures=0 + NET_PROTOCOL_HOST: PASS + clean done=1 ticks=63 retx=0/0 dup=0/0 gap=0/0 badtag=0/0 malformed=0/0 transcript=0365b2fe4b2ccc0a + loss30 done=1 ticks=326 retx=181/159 dup=7/4 gap=90/107 badtag=0/0 malformed=0/0 transcript=fcf753c7ec485c53 + loss+dup+reorder done=1 ticks=633 retx=171/190 dup=22/23 gap=142/151 badtag=0/0 malformed=0/0 transcript=617bdd0a7410d59b + corrupt20 done=1 ticks=231 retx=121/129 dup=18/14 gap=73/67 badtag=37/40 malformed=7/8 transcript=9b44fe14e4fbfc53 + everything done=1 ticks=851 retx=243/150 dup=13/8 gap=92/150 badtag=20/26 malformed=4/4 transcript=a92bca84a176256b + blackout-recover done=1 ticks=483 retx=103/120 dup=0/0 gap=90/67 badtag=0/0 malformed=0/0 transcript=e47a0b838ca48036 + blackout-dead link_down at tick 31 after 3 retransmissions + ctl_test checks=21690 failures=0 + CTL_TRANSPORT_LOSS_RECOVERY: PASS + X25519_RFC7748: PASS (149 checks, 0 failures, 1000 iterations) + AIENOS_NET_SECURE_TESTS: PASS (57248 checks, 0 failures) + VIRTIO_PCI_QEMU822_CAPS: PASS (real QEMU 8.2.2 virtio-net-pci config dumps, 0x1000 + 0x1041) + vnet_test: hostile fuzz delivered=1929 refused=1999 + VNET_M6A_UDP_ROUNDTRIP_SIM: PASS + vnet_test: 789204 checks, 0 failures + VNET_SIM_DATAPATH: PASS (simulated device only; no real device, DMA, IRQ or QEMU round trip) + NET_SANITIZE: PASS + NET_SECURE_MUTANTS: 30/30 killed + NET_RUST_DIFFERENTIAL_LIVE: NOT_RUN (no RUSTDIFF driver given; pinned digest checked) +deferred (not claimed): native binding -> needs a C kernel (no-Rust rule; port order open); + production identity and keys -> needs M5 key hierarchy (TRUST-1); M6-B runs on TEST keys only; + physical NIC qualification -> needs Machine 1 wave after TRUST-1 Gate 7. +M6A_NET_HOST: PASS +AIENOS_NET_SECURE: PASS