From 96da7c7f8f50d15e012168c29862693cbf9e2bd1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 06:17:18 -0500 Subject: [PATCH 1/4] ck: virtio-net driver hook and AIENOS_CK_NET gate (QEMU slirp) Bind native/net/aienos_virtio_net.c into the C kernel devices stage: SMMU-confined DMA window first (fail closed without an SMMU), then memory decode + bus master, BAR windows as vnet_ops, vnet_init, bounded ARP + one UDP round trip to the slirp gateway parsed by M6-A, then device reset, bus master off, SMMU stream back to abort. New C-only gate AIENOS_CK_NET (scripts/qemu_ck_net_test.sh) with an in-tree C UDP echo helper (make udp-echo). Host tests for frame building/classification and the fail-closed bind. GATES.md rows 102-106. QEMU does not route virtio-net DMA through the vIOMMU (no ACCESS_PLATFORM), so confinement is programmed, not proven. Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- native/kernel/GATES.md | 10 +- native/kernel/Makefile | 10 +- native/kernel/dev/devices.c | 13 +- native/kernel/dev/net_bind.c | 283 +++++++++++++++++++++++++++ native/kernel/dev/net_bind.h | 45 +++++ native/kernel/dev/net_udp.c | 123 ++++++++++++ native/kernel/dev/net_udp.h | 54 +++++ native/kernel/dev/virtio_net.c | 5 +- native/kernel/dev/virtio_net.h | 13 +- native/kernel/stage.mk | 3 +- native/kernel/svc/tests/stage_test.c | 161 +++++++++++++++ native/kernel/tools/ck_udp_echo.c | 108 ++++++++++ scripts/ck_gates.sh | 43 ++-- scripts/qemu_ck_net_test.sh | 236 ++++++++++++++++++++++ 14 files changed, 1076 insertions(+), 31 deletions(-) create mode 100644 native/kernel/dev/net_bind.c create mode 100644 native/kernel/dev/net_bind.h create mode 100644 native/kernel/dev/net_udp.c create mode 100644 native/kernel/dev/net_udp.h create mode 100644 native/kernel/tools/ck_udp_echo.c create mode 100755 scripts/qemu_ck_net_test.sh diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 3e40e909..f594cc2d 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -20,7 +20,8 @@ Status vocabulary: C gates (one line each from `ck_gates.sh`): `M1` and `M3` (from `scripts/qemu_ck_boot_test.sh`), `M4_NVME`, `M4_STORE`, `ARGUS1_REVOKE` `SMMU`, `NVME_SHUTDOWN` (from `scripts/qemu_ck_store_test.sh`; rows 21-24b, 47-72, 76 and -96-101 were checked against its exact grep patterns), and the NOT_RUN gates +96-101 were checked against its exact grep patterns), `NET` (C-only, from +`scripts/qemu_ck_net_test.sh`; rows 102-106), and the NOT_RUN gates `P2_ARTIFACT`, `M0_ROLLBACK`, `M4_STORE_CRASH`, `M4_CONTINUITY`, `M4_RECOVERY`, `KEYBOARD`. @@ -260,10 +261,15 @@ complete. | 99 | NVMe normal shutdown before bus-master disable (CC.SHN = 01b, then wait CSTS.SHST = 10b) | NVME_SHUTDOWN | DIFFERS (C-only; the Rust kernel has no NVMe shutdown. Boots 1-4 and 6, both geometries: exactly one `nvme: shutdown normal cc=0x..->0x.. csts=0x.. shst=complete waited_us=N` line with CC.SHN read back as 01b and CSTS.SHST as 10b, printed before `dma_gate: nvme bus master revoked`, and no `nvme: shutdown fallback disable` line. Host tests cover CFS, not-ready, already-shut, timeout and the CC.EN = 0 fallback) | | 100 | QEMU device trace shows the shutdown while bus master is on | NVME_SHUTDOWN | DIFFERS (C-only: QEMU `-trace` of `pci_nvme_mmio_shutdown_set` and `pci_cfg_write` on the NVMe command register; the shutdown must land between the guest's BME-set and BME-clear writes) | | 101 | reset path quiesces DMA first; no shutdown without a bound controller | NVME_SHUTDOWN | DIFFERS (C-only: `devices: quiesce before reset nvme=already-released` on every boot (`ck_reset` hook, also covers panic/fault resets); boot 5 safe image: absent `nvme: shutdown normal`) | +| 102 | virtio-net attach behind an SMMU window (`virtio_net: .. caps ok`, `smmu_dma_window: virtio_net only iova=.. len=.. rid=.. stream_id=..`, `dma_gate: virtio_net granted (Confined), bus master on`, `virtio_net: attached .. mac=52:54:00:12:34:56 .. access_platform=no`, ARP reply from the slirp gateway `52:55:0a:00:02:02`) | NET | DIFFERS (C-only; no Rust counterpart: the Rust kernel has no virtio-net queue driver. Boot 1, `iommu=smmuv3`, QEMU user networking (slirp) `restrict=off`, driver `native/net/aienos_virtio_net.c` via `native/kernel/dev/net_bind.c`. NOT proven: the driver does not negotiate VIRTIO_F_ACCESS_PLATFORM, so QEMU does virtio-net DMA without the vIOMMU and the window is programmed but not enforced for this device) | +| 103 | one UDP frame sent (TX) | NET | DIFFERS (C-only: guest `net: udp tx 10.0.2.15:47030 -> 10.0.2.2:47029 .. payload="AIENOS-CK-NET ping nonce=<8 hex>" .. completed=yes` AND the in-tree host helper `native/kernel/tools/ck_udp_echo.c` (`make udp-echo`) logs the same nonce arriving on 127.0.0.1:47029 through slirp) | +| 104 | one UDP frame received (RX) and parsed by M6-A | NET | DIFFERS (C-only: guest `net: udp rx 10.0.2.2:47029 -> 10.0.2.15:47030 .. udp_csum=verified parsed=m6a echo_of_ours=yes payload="AIENOS-CK-NET pong token= echo="`; the token is generated by the script per run, so the line cannot pass without the helper's reply reaching the guest; plus `net: udp round trip ok`, `net: selftest PASS (rc=0)` and `devices: pci=ok nvme=bound virtio_net=selftest-ok`) | +| 105 | virtio-net released after the round trip | NET | DIFFERS (C-only: `virtio_net: device reset status=0x00 (stopped)`, `dma_gate: virtio_net bus master revoked`, `smmu: virtio_net stream 0x.. returned to abort (rc=0)`; line order grant < round trip < revoke < abort) | +| 106 | virtio-net fails closed without an SMMU | NET | DIFFERS (C-only: boot 2 without `iommu=smmuv3`: `dma_gate: virtio_net denied (NoSmmu rc=-1), bus master stays off`, `virtio_net: not bound (SMMU DMA isolation not active)`, absent `dma_gate: virtio_net granted` and `net: udp tx`, and the host helper receives nothing. Host test `stage_test` also checks the fail-closed bind leaves the command register without MEM/BME) | ## Summary counts -Rows 1-101: IDENTICAL 20, DIFFERS 25, NOT_RUN 56 (rows 92-95 have no CK gate at all; rows 96-101 are C-only). Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. +Rows 1-106: IDENTICAL 20, DIFFERS 30, NOT_RUN 56 (rows 92-95 have no CK gate at all; rows 96-106 are C-only). Rows 102-106 were checked against scripts/qemu_ck_net_test.sh. Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. `scripts/trust1_m5_qualify.sh --with-qemu` also runs three of these gates as qemu rows: `ck_m1_boot_qemu` (M1), `ck_store_kernel_qemu` (M4_STORE) and diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 5a38765c..51bca0fc 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -15,6 +15,8 @@ # make sanitize same under ASan + UBSan -> CK_CORE_HOST: PASS # make stage-test | stage-sanitize | stage-free stage host tests (stage.mk) # make print-kcflags flags stage code must be built with +# make udp-echo host UDP echo helper for scripts/qemu_ck_net_test.sh: +# $(OUT)/host/ck_udp_echo (hosted, test tooling only) # # CK_EXTRA_CFLAGS=... extra kernel flags for one-off checks, e.g. # -DCK_SELFTEST_EL2_FAULT (brk at EL2: the EL2 fatal path) @@ -197,8 +199,14 @@ sanitize: $(addprefix $(OUT)/host-san/,$(TESTS)) @fail=0; for t in $^; do ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=print_stacktrace=1 $$t || fail=1; done; \ if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS (sanitize)"; else echo "CK_CORE_HOST: FAIL (sanitize)"; exit 1; fi +# ---- host tool for the AIENOS_CK_NET gate (scripts/qemu_ck_net_test.sh) ---- +udp-echo: $(OUT)/host/ck_udp_echo +$(OUT)/host/ck_udp_echo: $(KDIR)/tools/ck_udp_echo.c + @mkdir -p $(dir $@) + $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -o $@ $< + clean: rm -rf $(OUT) FORCE: -.PHONY: all full test sanitize clean print-kcflags FORCE +.PHONY: all full test sanitize clean print-kcflags udp-echo FORCE diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c index 471fd643..e218cd0d 100644 --- a/native/kernel/dev/devices.c +++ b/native/kernel/dev/devices.c @@ -4,6 +4,7 @@ #include "nvme_bind.h" #include "pci.h" #include "virtio_net.h" +#include "net_bind.h" static pci_system g_pci; static ck_nvme g_nvme; @@ -42,9 +43,14 @@ int ck_stage_devices(void) int nrc = ck_nvme_bind(&g_nvme, &g_pci); if (nrc) ck_dev_nvme_release(); /* a failed bind never keeps DMA */ virtio_pci_caps caps; - int vrc = ck_virtio_net_probe(&g_pci, &caps); + const pci_func *vf = 0; + int vrc = ck_virtio_net_probe(&g_pci, &caps, &vf); + /* virtio-net: SMMU-confined attach plus one bounded UDP round trip; the + * device is always released again before this returns (net_bind.h). A + * net failure is reported, never a devices-stage failure. */ + int netrc = (vrc == 0 && vf) ? ck_net_bind_selftest(vf, &caps) : 1; ck_printf("devices: pci=ok nvme=%s virtio_net=%s\n", g_nvme.bound ? "bound" : "unbound", - vrc == 0 ? "probed" : "caps-refused"); + vrc != 0 ? "caps-refused" : !vf ? "absent" : netrc == 0 ? "selftest-ok" : "selftest-failed"); if (nrc == -1) return 0; /* no NVMe present: not a devices failure; Store reports it */ return nrc; } @@ -55,9 +61,12 @@ int ck_stage_devices(void) * only reports that; after a panic mid-stage it does the full halt order. */ void ck_stage_quiesce(void) { + int net_live = ck_net_live(); + if (net_live) ck_net_release(); int was_live = g_nvme.pf && (g_nvme.bm_on || g_nvme.confined); if (was_live) ck_dev_nvme_release(); ck_printf("devices: quiesce before reset nvme=%s\n", !g_nvme.pf ? "none" : was_live ? "released-now" : "already-released"); + if (net_live) ck_printf("devices: quiesce before reset virtio_net=released-now\n"); } diff --git a/native/kernel/dev/net_bind.c b/native/kernel/dev/net_bind.c new file mode 100644 index 00000000..5700836f --- /dev/null +++ b/native/kernel/dev/net_bind.c @@ -0,0 +1,283 @@ +/* net_bind.c -- virtio-net binding and UDP round trip (see net_bind.h). + * Freestanding. */ +#include "net_bind.h" +#include "ck.h" +#include "net_udp.h" +#include + +#define WIN_N 4 +#define ARP_TRIES 4u +#define ARP_WAIT_US 500000u +#define UDP_TRIES 5u +#define UDP_WAIT_US 1000000u +#define RESET_WAIT_US 100000u + +typedef struct { + const pci_func *pf; + volatile uint8_t *bar_va[PCI_MAX_BARS]; + volatile uint8_t *win[WIN_N]; + vnet_dev dev; + int bm_on, confined; + uint32_t stream_id; +} ck_vnet; + +static ck_vnet g_net; +/* ck_dma_alloc memory is never freed: allocate the shared region once and + * reuse it on a later bind. */ +static uint8_t *g_dma_mem; +static uint64_t g_dma_phys; +static uint8_t frame[VNET_FRAME_MAX]; +static uint8_t rxbuf[VNET_FRAME_MAX]; + +static uint32_t o_read(void *ctx, vnet_window w, uint32_t off, unsigned width) +{ + volatile uint8_t *p = ((ck_vnet *)ctx)->win[w] + off; + if (width == 1) return *p; + if (width == 2) return *(volatile uint16_t *)p; + return *(volatile uint32_t *)p; +} +static void o_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v) +{ + volatile uint8_t *p = ((ck_vnet *)ctx)->win[w] + off; + if (width == 1) *p = (uint8_t)v; + else if (width == 2) *(volatile uint16_t *)p = (uint16_t)v; + else *(volatile uint32_t *)p = v; + ck_mb(); +} +static void o_notify(void *ctx, uint32_t off, uint16_t q) +{ + ck_mb(); /* ring writes visible before the doorbell */ + *(volatile uint16_t *)(((ck_vnet *)ctx)->win[VNET_WIN_NOTIFY] + off) = q; +} + +/* Map the BAR behind one capability window; 0 on a bad BAR or a window + * outside it. */ +static volatile uint8_t *map_window(ck_vnet *n, const virtio_pci_region *r) +{ + if (!r->present || r->bar >= PCI_MAX_BARS) return 0; + const pci_bar *b = &n->pf->bar[r->bar]; + if (b->io || b->addr == 0 || b->size == 0) return 0; + if ((uint64_t)r->offset + r->length > b->size) return 0; + if (!n->bar_va[r->bar]) n->bar_va[r->bar] = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); + return n->bar_va[r->bar] + r->offset; +} + +static void hex8(uint32_t v, char *o) +{ + static const char h[] = "0123456789abcdef"; + for (int i = 7; i >= 0; i--, v >>= 4) o[i] = h[v & 15u]; +} + +static int has_prefix(const uint8_t *b, size_t n, const char *s, size_t sn) +{ + return n >= sn && memcmp(b, s, sn) == 0; +} +static int contains(const uint8_t *b, size_t n, const char *s, size_t sn) +{ + for (size_t i = 0; i + sn <= n; i++) + if (memcmp(b + i, s, sn) == 0) return 1; + return 0; +} + +int ck_net_live(void) { return g_net.pf && (g_net.bm_on || g_net.confined); } + +void ck_net_release(void) +{ + ck_vnet *n = &g_net; + if (!n->pf) return; + if (n->bm_on) { + /* Device reset (status 0) stops the queues; then cut bus mastering. */ + uint32_t st = 0xffu; + if (n->win[VNET_WIN_COMMON]) { + o_write(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1, 0); + uint64_t t0 = ck_time_us(); + while ((st = o_read(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1)) != 0 && ck_time_us() - t0 < RESET_WAIT_US) + ck_udelay(10); + } + ck_printf("virtio_net: device reset status=0x%02x (%s)\n", st, st == 0 ? "stopped" : "TIMEOUT"); + if (pci_bus_master_off(n->pf) == 0) { + n->bm_on = 0; + ck_printf("dma_gate: virtio_net bus master revoked\n"); + } else + ck_printf("dma_gate: virtio_net bus master revoke FAILED (command register still has BME)\n"); + } + if (n->confined) { + int urc = ck_dma_unconfine(n->stream_id); + n->confined = 0; + ck_printf("smmu: virtio_net stream 0x%x %s (rc=%d)\n", n->stream_id, + urc == 0 ? "returned to abort" : "abort FAILED", urc); + } +} + +/* Poll RX until `want` arrives or `us` passes. Answers ARP requests for our + * address on the way. Returns the kind seen (CK_NET_IGNORED on timeout), or + * -1 when the device is broken. */ +static int wait_for(ck_vnet *n, ck_net_peer *p, ck_net_kind want, uint32_t us, size_t *len_out, int *csum, + unsigned *frames) +{ + uint64_t t0 = ck_time_us(); + do { + size_t len = 0; + int got = 0; + vnet_err e = vnet_rx(&n->dev, rxbuf, sizeof rxbuf, &len, &got); + if (e != VNET_OK) { + ck_printf("net: rx error %s\n", vnet_err_name(e)); + return -1; + } + (void)vnet_tx_reclaim(&n->dev, 0); + if (!got) continue; + (*frames)++; + net_mac mac; + net_arp_packet req; + const uint8_t *pl = 0; + size_t pn = 0; + ck_net_kind k = ck_net_classify(p, rxbuf, len, &mac, &req, &pl, &pn, csum); + if (k == CK_NET_ARP_ASK) { + size_t w = ck_net_arp_reply(p, &req, frame, sizeof frame); + if (w && vnet_tx(&n->dev, frame, w) == VNET_OK) + ck_printf("net: arp answered who-has %u.%u.%u.%u\n", p->ip.b[0], p->ip.b[1], p->ip.b[2], p->ip.b[3]); + continue; + } + if (k != (ck_net_kind)want) continue; + if (k == CK_NET_ARP_GW) p->gw_mac = mac; + if (k == CK_NET_UDP_REPLY) { + /* Keep the reply: move the payload to the front of rxbuf. */ + memmove(rxbuf, pl, pn); + *len_out = pn; + } + return (int)k; + } while (ck_time_us() - t0 < us); + return CK_NET_IGNORED; +} + +static int roundtrip(ck_vnet *n) +{ + ck_net_peer p; + memset(&p, 0, sizeof p); + memcpy(p.mac.b, n->dev.mac, 6); + static const net_ipv4 me = {{10, 0, 2, 15}}, gw = {{10, 0, 2, 2}}; + p.ip = me; + p.gw_ip = gw; + p.lport = CK_NET_LOCAL_PORT; + p.rport = CK_NET_ECHO_PORT; + unsigned frames = 0, tries = 0; + size_t dummy = 0; + int csum = 0, k = CK_NET_IGNORED; + + for (tries = 1; tries <= ARP_TRIES && k != CK_NET_ARP_GW; tries++) { + size_t w = ck_net_arp_request(&p, frame, sizeof frame); + if (!w || vnet_tx(&n->dev, frame, w) != VNET_OK) return CK_NET_E_TX; + k = wait_for(n, &p, CK_NET_ARP_GW, ARP_WAIT_US, &dummy, &csum, &frames); + if (k < 0) return CK_NET_E_RX; + } + if (k != CK_NET_ARP_GW) { + ck_printf("net: arp who-has 10.0.2.2 FAIL (no reply after %u tries, frames_seen=%u)\n", ARP_TRIES, frames); + return CK_NET_E_ARP; + } + ck_printf("net: arp who-has 10.0.2.2 tell 10.0.2.15 -> %02x:%02x:%02x:%02x:%02x:%02x tries=%u\n", p.gw_mac.b[0], + p.gw_mac.b[1], p.gw_mac.b[2], p.gw_mac.b[3], p.gw_mac.b[4], p.gw_mac.b[5], tries - 1); + + static char msg[] = "AIENOS-CK-NET ping nonce=00000000"; + hex8((uint32_t)ck_time_us() ^ 0x5eedc0deu, msg + sizeof msg - 9); + const size_t mlen = sizeof msg - 1; + unsigned reclaimed = 0; + k = CK_NET_IGNORED; + for (tries = 1; tries <= UDP_TRIES && k != CK_NET_UDP_REPLY; tries++) { + size_t w = ck_net_udp_frame(&p, (uint16_t)(0x4e00u + tries), (const uint8_t *)msg, mlen, frame, sizeof frame); + vnet_err e = w ? vnet_tx(&n->dev, frame, w) : VNET_E_ARG; + if (e != VNET_OK) { + ck_printf("net: udp tx FAIL (%s)\n", vnet_err_name(e)); + return CK_NET_E_TX; + } + /* The device must hand the TX descriptor back (bounded). */ + uint64_t t0 = ck_time_us(); + unsigned r = 0; + while (ck_time_us() - t0 < UDP_WAIT_US) { + r = 0; + if (vnet_tx_reclaim(&n->dev, &r) != VNET_OK) return CK_NET_E_TX; + reclaimed += r; + if (n->dev.q[VNET_TX_QUEUE].in_flight == 0) break; + } + ck_printf("net: udp tx 10.0.2.15:%u -> 10.0.2.2:%u frame=%u bytes payload=\"%s\" try=%u completed=%s\n", + CK_NET_LOCAL_PORT, CK_NET_ECHO_PORT, (unsigned)w, msg, tries, + n->dev.q[VNET_TX_QUEUE].in_flight == 0 ? "yes" : "NO"); + size_t pn = 0; + k = wait_for(n, &p, CK_NET_UDP_REPLY, UDP_WAIT_US, &pn, &csum, &frames); + if (k < 0) return CK_NET_E_RX; + if (k != CK_NET_UDP_REPLY) continue; + static const char pong[] = "AIENOS-CK-NET pong token="; + char shown[160]; + ck_net_printable(rxbuf, pn, shown, sizeof shown); + int mine = has_prefix(rxbuf, pn, pong, sizeof pong - 1) && contains(rxbuf, pn, msg, mlen); + ck_printf("net: udp rx 10.0.2.2:%u -> 10.0.2.15:%u payload_len=%u udp_csum=%s parsed=m6a echo_of_ours=%s " + "payload=\"%s\"\n", + CK_NET_ECHO_PORT, CK_NET_LOCAL_PORT, (unsigned)pn, csum ? "verified" : "absent", mine ? "yes" : "NO", + shown); + if (!mine) k = CK_NET_IGNORED; /* not the reply to our datagram; keep waiting */ + } + if (k != CK_NET_UDP_REPLY) { + ck_printf("net: udp round trip FAIL (no reply after %u tries, frames_seen=%u)\n", UDP_TRIES, frames); + return CK_NET_E_RX; + } + ck_printf("net: udp round trip ok tx_reclaimed=%u rx_frames=%u\n", reclaimed, frames); + return CK_NET_OK; +} + +int ck_net_bind_selftest(const pci_func *f, const virtio_pci_caps *caps) +{ + ck_vnet *n = &g_net; + memset(n, 0, sizeof *n); + if (!f || !caps) return CK_NET_E_ARG; + n->pf = f; + size_t need = vnet_mem_size(CK_NET_QSIZE); + uint64_t win = ((uint64_t)need + 4095u) & ~(uint64_t)4095u; + if (need && !g_dma_mem) g_dma_mem = ck_dma_alloc((size_t)win, 4096, &g_dma_phys); + uint8_t *mem = need ? g_dma_mem : 0; + uint64_t phys = g_dma_phys; + if (!mem) { + ck_printf("virtio_net: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)win); + return CK_NET_E_DMA; + } + uint32_t rid = ((uint32_t)f->bus << 8) | ((uint32_t)f->dev << 3) | (uint32_t)f->fn; + struct ck_dma_confinement cf; + int src = ck_dma_confine(rid, phys, win, &cf); + if (src != 0) { + ck_printf("dma_gate: virtio_net denied (%s rc=%d), bus master stays off\n", + src == CK_SMMU_ABSENT ? "NoSmmu" : "SmmuNotReady", src); + ck_printf("virtio_net: not bound (SMMU DMA isolation not active)\n"); + return CK_NET_E_DENIED; + } + n->confined = 1; + n->stream_id = cf.stream_id; + ck_printf("smmu_dma_window: virtio_net only iova=0x%llx len=0x%llx rid=0x%x stream_id=0x%x\n", + (unsigned long long)cf.iova, (unsigned long long)cf.len, rid, cf.stream_id); + const virtio_pci_region *r[WIN_N] = {&caps->common_cfg, &caps->notify_cfg, &caps->isr_cfg, &caps->device_cfg}; + for (int i = 0; i < WIN_N; i++) { + n->win[i] = map_window(n, r[i]); + if (!n->win[i] && i != VNET_WIN_ISR) { /* ISR is unused (polled driver) */ + ck_printf("virtio_net: not bound (capability window %d outside its BAR)\n", i); + ck_net_release(); + return CK_NET_E_ARG; + } + } + pci_enable(f, 1); + n->bm_on = 1; + ck_printf("dma_gate: virtio_net granted (Confined), bus master on\n"); + vnet_ops ops = {n, o_read, o_write, o_notify}; + vnet_err e = vnet_init(&n->dev, &ops, caps, mem, phys, need, CK_NET_QSIZE); + if (e != VNET_OK) { + ck_printf("virtio_net: init FAIL (%s)\n", vnet_err_name(e)); + ck_net_release(); + return CK_NET_E_INIT; + } + const uint8_t *m = n->dev.mac; + ck_printf("virtio_net: attached %02x:%02x.%u rid=0x%x qsize=rx%u/tx%u mac=%02x:%02x:%02x:%02x:%02x:%02x " + "features=0x%llx access_platform=no\n", + f->bus, f->dev, f->fn, rid, n->dev.q[VNET_RX_QUEUE].size, n->dev.q[VNET_TX_QUEUE].size, m[0], m[1], m[2], + m[3], m[4], m[5], (unsigned long long)n->dev.features); + int rc = n->dev.has_mac ? roundtrip(n) : CK_NET_E_INIT; + if (!n->dev.has_mac) ck_printf("net: no MAC offered by the device; round trip not attempted\n"); + ck_printf("net: selftest %s (rc=%d)\n", rc == CK_NET_OK ? "PASS" : "FAIL", rc); + ck_net_release(); + return rc; +} diff --git a/native/kernel/dev/net_bind.h b/native/kernel/dev/net_bind.h new file mode 100644 index 00000000..42bda9bf --- /dev/null +++ b/native/kernel/dev/net_bind.h @@ -0,0 +1,45 @@ +/* net_bind.h -- bind native/net's polled virtio-net driver + * (aienos_virtio_net.c) to the virtio-net PCI function through ck.h services + * and run one bounded UDP round trip (QEMU user networking). + * + * DMA rule (same as NVMe, dev/nvme_bind.c): no SMMU confinement means no DMA. + * The driver's one shared region (rings + packet buffers) is allocated first + * and becomes the stream's only SMMU window (ck_dma_confine); only then are + * memory decoding and bus mastering enabled. Without an SMMU, or when the + * SMMU did not come up, the device stays off. There is no unconfined bypass + * for virtio-net, not even in the TEST-ONLY CK_QEMU_UNSAFE_DMA=1 build. + * + * Limit (QEMU): the driver does not negotiate VIRTIO_F_ACCESS_PLATFORM, and + * QEMU's virtio-pci then performs device DMA without the vIOMMU, so the SMMU + * window is programmed but QEMU does not enforce it for this device. The + * kernel prints access_platform=no; the gate does not claim confinement. */ +#ifndef AIENOS_CK_NET_BIND_H +#define AIENOS_CK_NET_BIND_H +#include "aienos_virtio_net.h" +#include "pci.h" + +#define CK_NET_QSIZE 64u + +enum { + CK_NET_OK = 0, + CK_NET_E_ARG = -501, /* bad function, BAR or capability window */ + CK_NET_E_DMA = -502, /* DMA region allocation failed */ + CK_NET_E_DENIED = -503, /* no SMMU confinement: device left off */ + CK_NET_E_INIT = -504, /* vnet_init failed */ + CK_NET_E_ARP = -505, /* no ARP reply from the gateway in time */ + CK_NET_E_TX = -506, /* frame build, post or completion failed */ + CK_NET_E_RX = -507, /* no matching UDP reply in time, or device broken */ +}; + +/* Attach (DMA gate, BAR map, bus master, vnet_init), then ARP for the + * gateway and one UDP datagram to CK_NET_ECHO_PORT and back, every wait + * bounded by ck_time_us. Always leaves the device released (reset, bus + * master off, stream back to abort) before returning. */ +int ck_net_bind_selftest(const pci_func *f, const virtio_pci_caps *caps); + +/* Reset the device, revoke bus mastering and return the SMMU stream to + * abort if still live; prints the dma_gate/smmu lines. Idempotent. */ +void ck_net_release(void); +/* 1 while the device may still DMA (bus master on or stream confined). */ +int ck_net_live(void); +#endif diff --git a/native/kernel/dev/net_udp.c b/native/kernel/dev/net_udp.c new file mode 100644 index 00000000..8b2cb935 --- /dev/null +++ b/native/kernel/dev/net_udp.c @@ -0,0 +1,123 @@ +/* net_udp.c -- UDP round-trip frames (see net_udp.h). Freestanding. */ +#include "net_udp.h" +#include + +static const net_mac k_bcast = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; + +static int mac_eq(const net_mac *a, const net_mac *b) { return memcmp(a->b, b->b, 6) == 0; } +static int ip_eq(net_ipv4 a, net_ipv4 b) { return memcmp(a.b, b.b, 4) == 0; } +static int mac_unicast(const net_mac *m) +{ + static const net_mac zero = {{0, 0, 0, 0, 0, 0}}; + return !(m->b[0] & 1u) && !mac_eq(m, &zero); +} + +static size_t eth_wrap(const net_mac *dst, const net_mac *src, uint16_t type, const uint8_t *pl, size_t n, + uint8_t *out, size_t cap) +{ + net_eth_frame f; + f.destination = *dst; + f.source = *src; + f.ethertype = type; + f.payload = pl; + f.payload_len = n; + size_t w = 0; + return net_eth_build(&f, out, cap, &w) == NET_OK ? w : 0; +} + +static size_t arp_frame(const ck_net_peer *p, uint16_t op, const net_mac *tmac, net_ipv4 tip, + const net_mac *eth_dst, uint8_t *out, size_t cap) +{ + net_arp_packet a; + uint8_t body[64]; + size_t w = 0; + a.operation = op; + a.sender_mac = p->mac; + a.sender_ip = p->ip; + a.target_mac = *tmac; + a.target_ip = tip; + if (net_arp_build(&a, body, sizeof body, &w) != NET_OK) return 0; + return eth_wrap(eth_dst, &p->mac, NET_ETHERTYPE_ARP, body, w, out, cap); +} + +size_t ck_net_arp_request(const ck_net_peer *p, uint8_t *out, size_t cap) +{ + static const net_mac zero = {{0, 0, 0, 0, 0, 0}}; + if (!p || !out) return 0; + return arp_frame(p, NET_ARP_REQUEST, &zero, p->gw_ip, &k_bcast, out, cap); +} + +size_t ck_net_arp_reply(const ck_net_peer *p, const net_arp_packet *req, uint8_t *out, size_t cap) +{ + if (!p || !req || !out || !mac_unicast(&req->sender_mac)) return 0; + return arp_frame(p, NET_ARP_REPLY, &req->sender_mac, req->sender_ip, &req->sender_mac, out, cap); +} + +size_t ck_net_udp_frame(const ck_net_peer *p, uint16_t ip_id, const uint8_t *data, size_t n, uint8_t *out, + size_t cap) +{ + static uint8_t udp[1500], ip[1500]; + if (!p || !out || (n && !data) || !mac_unicast(&p->gw_mac)) return 0; + net_udp_header uh = {p->lport, p->rport}; + size_t uw = 0, iw = 0; + if (net_udp_build(&uh, data, n, p->ip, p->gw_ip, udp, sizeof udp, &uw) != NET_OK) return 0; + net_ipv4_header ih; + ih.dscp_ecn = 0; + ih.identification = ip_id; + ih.ttl = 64; + ih.protocol = NET_IPPROTO_UDP; + ih.source = p->ip; + ih.destination = p->gw_ip; + if (net_ipv4_build(&ih, udp, uw, ip, sizeof ip, &iw) != NET_OK) return 0; + return eth_wrap(&p->gw_mac, &p->mac, NET_ETHERTYPE_IPV4, ip, iw, out, cap); +} + +ck_net_kind ck_net_classify(const ck_net_peer *p, const uint8_t *frame, size_t len, net_mac *mac, + net_arp_packet *arp, const uint8_t **payload, size_t *payload_len, int *csum) +{ + net_eth_frame e; + if (!p || !frame || net_eth_parse(frame, len, &e) != NET_OK) return CK_NET_IGNORED; + if (!mac_eq(&e.destination, &p->mac) && !mac_eq(&e.destination, &k_bcast)) return CK_NET_IGNORED; + if (e.ethertype == NET_ETHERTYPE_ARP) { + net_arp_packet a; + if (net_arp_parse(e.payload, e.payload_len, &a) != NET_OK) return CK_NET_IGNORED; + if (!ip_eq(a.target_ip, p->ip) || !mac_unicast(&a.sender_mac)) return CK_NET_IGNORED; + if (a.operation == NET_ARP_REPLY && ip_eq(a.sender_ip, p->gw_ip) && mac_eq(&a.target_mac, &p->mac) && + mac_eq(&e.destination, &p->mac)) { + if (mac) *mac = a.sender_mac; + return CK_NET_ARP_GW; + } + if (a.operation == NET_ARP_REQUEST) { + if (arp) *arp = a; + return CK_NET_ARP_ASK; + } + return CK_NET_IGNORED; + } + if (e.ethertype != NET_ETHERTYPE_IPV4 || !mac_eq(&e.destination, &p->mac)) return CK_NET_IGNORED; + net_ipv4_header ih; + const uint8_t *ipl = 0; + size_t ipn = 0; + if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ipl, &ipn) != NET_OK) return CK_NET_IGNORED; + if (ih.protocol != NET_IPPROTO_UDP || !ip_eq(ih.source, p->gw_ip) || !ip_eq(ih.destination, p->ip)) + return CK_NET_IGNORED; + net_udp_header uh; + const uint8_t *pl = 0; + size_t pn = 0; + if (net_udp_parse(ipl, ipn, ih.source, ih.destination, &uh, &pl, &pn) != NET_OK) return CK_NET_IGNORED; + if (uh.source_port != p->rport || uh.destination_port != p->lport) return CK_NET_IGNORED; + if (payload) *payload = pl; + if (payload_len) *payload_len = pn; + if (csum) *csum = (ipl[6] | ipl[7]) != 0; + return CK_NET_UDP_REPLY; +} + +void ck_net_printable(const uint8_t *b, size_t n, char *out, size_t cap) +{ + if (!out || !cap) return; + size_t i = 0; + for (; b && i < n && i + 1 < cap; i++) { + uint8_t c = b[i]; + out[i] = (c == '"' || c == '\\') ? '\'' : (c >= 0x20 && c < 0x7f) ? (char)c : '.'; + } + out[i] = 0; +} diff --git a/native/kernel/dev/net_udp.h b/native/kernel/dev/net_udp.h new file mode 100644 index 00000000..5face4c8 --- /dev/null +++ b/native/kernel/dev/net_udp.h @@ -0,0 +1,54 @@ +/* net_udp.h -- the C kernel's UDP round-trip frames over the M6-A protocol + * layer (native/net/aienos_net.h): ARP for the gateway, one UDP datagram + * out, one UDP datagram back. Pure functions over caller buffers (no device, + * no clock), so they are host-tested in svc/tests/stage_test.c. + * + * Addresses are QEMU user networking (slirp) defaults: guest 10.0.2.15, + * gateway/host 10.0.2.2. Slirp forwards a datagram for 10.0.2.2:PORT to the + * host's 127.0.0.1:PORT (scripts/qemu_ck_net_test.sh runs the echo helper + * native/kernel/tools/ck_udp_echo.c there). QEMU is not hardware. */ +#ifndef AIENOS_CK_NET_UDP_H +#define AIENOS_CK_NET_UDP_H +#include +#include +#include "aienos_net.h" + +#define CK_NET_ECHO_PORT 47029u /* host helper port (scripts read this line) */ +#define CK_NET_LOCAL_PORT 47030u /* guest source port */ + +typedef struct { + net_mac mac; /* ours (virtio-net config) */ + net_ipv4 ip; /* ours */ + net_mac gw_mac; /* learned from the ARP reply */ + net_ipv4 gw_ip; + uint16_t lport, rport; +} ck_net_peer; + +/* Frame builders: bytes written, 0 when the buffer is too small or an + * argument is bad. */ +size_t ck_net_arp_request(const ck_net_peer *p, uint8_t *out, size_t cap); +/* ARP reply to `req` (a request for p->ip), sent to its sender. */ +size_t ck_net_arp_reply(const ck_net_peer *p, const net_arp_packet *req, uint8_t *out, size_t cap); +/* Ethernet + IPv4 + UDP from p->ip:lport to p->gw_ip:rport via p->gw_mac. */ +size_t ck_net_udp_frame(const ck_net_peer *p, uint16_t ip_id, const uint8_t *data, size_t n, uint8_t *out, + size_t cap); + +typedef enum { + CK_NET_IGNORED = 0, /* not for us, malformed, or not what we wait for */ + CK_NET_ARP_GW = 1, /* ARP reply from gw_ip to us: *mac = gateway MAC */ + CK_NET_ARP_ASK = 2, /* ARP request for our ip: *arp = the request */ + CK_NET_UDP_REPLY = 3, /* UDP gw_ip:rport -> ip:lport, checksum ok: payload view */ +} ck_net_kind; + +/* Classify one received frame (Ethernet, no FCS). Every field is checked by + * the M6-A parsers; anything else is CK_NET_IGNORED. A gateway MAC that is + * zero, broadcast or multicast is ignored. For CK_NET_UDP_REPLY, *csum = 1 + * when the datagram carried a UDP checksum (verified by net_udp_parse), 0 + * when it carried none (allowed for UDP over IPv4). */ +ck_net_kind ck_net_classify(const ck_net_peer *p, const uint8_t *frame, size_t len, net_mac *mac, + net_arp_packet *arp, const uint8_t **payload, size_t *payload_len, int *csum); + +/* Copy at most cap-1 bytes of `b` as printable ASCII ('.' for anything + * else, '\'' for '"' and '\\'), NUL-terminated. */ +void ck_net_printable(const uint8_t *b, size_t n, char *out, size_t cap); +#endif diff --git a/native/kernel/dev/virtio_net.c b/native/kernel/dev/virtio_net.c index 81fe3107..f3d9ac60 100644 --- a/native/kernel/dev/virtio_net.c +++ b/native/kernel/dev/virtio_net.c @@ -4,8 +4,9 @@ static uint8_t cfg_image[4096]; -int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps) +int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps, const pci_func **func_out) { + if (func_out) *func_out = 0; const pci_func *f = pci_find_id(pci, 0x1af4u, 0x1041u); if (!f) { f = pci_find_id(pci, 0x1af4u, 0x1000u); /* transitional: subsystem 1 = net */ @@ -32,6 +33,6 @@ int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps) f->bus, f->dev, f->fn, caps->common_cfg.bar, caps->common_cfg.offset, caps->notify_cfg.bar, caps->notify_cfg.offset, caps->notify_off_multiplier, caps->isr_cfg.bar, caps->isr_cfg.offset, caps->device_cfg.bar, caps->device_cfg.offset); - ck_printf("virtio_net: not bound (native/net has no virtio queue driver yet; capability parse only)\n"); + if (func_out) *func_out = f; return 0; } diff --git a/native/kernel/dev/virtio_net.h b/native/kernel/dev/virtio_net.h index 024e6336..6f8b980d 100644 --- a/native/kernel/dev/virtio_net.h +++ b/native/kernel/dev/virtio_net.h @@ -1,11 +1,12 @@ -/* virtio_net.h -- virtio-net discovery stage. native/net provides only the - * virtio 1.x PCI capability parser (no queues, no driver), so this stage - * finds the device, parses its capabilities and reports "not bound". */ +/* virtio_net.h -- virtio-net discovery stage: finds the virtio-net PCI + * function and parses its virtio 1.x capabilities (native/net + * aienos_virtio_pci.c). Binding and the data path are dev/net_bind.c. */ #ifndef AIENOS_CK_VIRTIO_NET_H #define AIENOS_CK_VIRTIO_NET_H #include "aienos_virtio_pci.h" #include "pci.h" -/* Returns 0 when no virtio-net exists or its capabilities parsed; the - * parse error code (> 0) otherwise. Never binds, never enables DMA. */ -int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps_out); +/* Returns 0 when no virtio-net exists (*func_out = NULL) or its capabilities + * parsed (*func_out = the function); the parse error code (> 0) otherwise. + * Never enables the device. func_out may be NULL. */ +int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps_out, const pci_func **func_out); #endif diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index e06ea045..6e99b9f8 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -14,6 +14,7 @@ STAGE_OUT ?= /tmp/aienos-ck-stage-$(shell id -u) # Own sources (image). STAGE_OWN_SRCS := $(STAGE_DIR)/dev/pci.c $(STAGE_DIR)/dev/nvme_bind.c $(STAGE_DIR)/dev/nvme_shutdown.c $(STAGE_DIR)/dev/virtio_net.c \ + $(STAGE_DIR)/dev/net_bind.c $(STAGE_DIR)/dev/net_udp.c \ $(STAGE_DIR)/dev/devices.c $(STAGE_DIR)/svc/security.c $(STAGE_DIR)/svc/store_boot.c # Native modules, compiled unmodified. STAGE_NATIVE_SRCS := \ @@ -27,7 +28,7 @@ STAGE_NATIVE_SRCS := \ $(STAGE_NATIVE)/argus/argus_detect.c $(STAGE_NATIVE)/argus/argus_contain.c \ $(STAGE_NATIVE)/argus/bridge/argus_aegis_bridge.c \ $(STAGE_NATIVE)/capability/aienos_capability.c $(STAGE_NATIVE)/capability/aienos_contain.c \ - $(STAGE_NATIVE)/net/aienos_virtio_pci.c + $(STAGE_NATIVE)/net/aienos_virtio_pci.c $(STAGE_NATIVE)/net/aienos_virtio_net.c $(STAGE_NATIVE)/net/aienos_net.c # Kernel-only glue: hosted-header stand-ins for capability/ARGUS + weak mem*. STAGE_KERNEL_SRCS := $(STAGE_DIR)/svc/ck_compat.c diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index 08aa6ad1..19e2b87b 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -16,6 +16,8 @@ #include "pci.h" #include "security.h" #include "store_boot.h" +#include "net_bind.h" +#include "net_udp.h" static int checks, failures; #define CHECK(c) \ @@ -432,6 +434,164 @@ static void test_nvme_shutdown(void) CHECK(ck_nvme_disable(&o, 1000, &r) == CK_NVME_SHUT_GONE && m.cc_writes == 0); CHECK(ck_nvme_disable(0, 1000, &r) == CK_NVME_SHUT_EARG); } +/* ---------------- virtio-net UDP round trip frames + fail-closed bind ---------------- */ + +static ck_net_peer peer_guest(void) +{ + ck_net_peer p; + memset(&p, 0, sizeof p); + static const uint8_t mac[6] = {0x52, 0x54, 0x00, 0x12, 0x34, 0x56}; + memcpy(p.mac.b, mac, 6); + p.ip = (net_ipv4){{10, 0, 2, 15}}; + p.gw_ip = (net_ipv4){{10, 0, 2, 2}}; + p.lport = CK_NET_LOCAL_PORT; + p.rport = CK_NET_ECHO_PORT; + return p; +} +static const net_mac k_gw_mac = {{0x52, 0x55, 0x0a, 0x00, 0x02, 0x02}}; + +/* An ARP frame as the gateway would send it. */ +static size_t gw_arp(uint16_t op, net_mac eth_dst, net_mac smac, net_ipv4 sip, net_mac tmac, net_ipv4 tip, + uint8_t *out, size_t cap) +{ + net_arp_packet a = {op, smac, sip, tmac, tip}; + uint8_t body[64]; + size_t w = 0, fw = 0; + if (net_arp_build(&a, body, sizeof body, &w) != NET_OK) return 0; + net_eth_frame f = {eth_dst, smac, NET_ETHERTYPE_ARP, body, w}; + return net_eth_build(&f, out, cap, &fw) == NET_OK ? fw : 0; +} + +static void test_net_udp(void) +{ + ck_net_peer p = peer_guest(); + static uint8_t f[1600]; + net_eth_frame e; + net_arp_packet a; + net_mac m; + const uint8_t *pl = 0; + size_t pn = 0; + int cs = -1; + + /* ARP request: broadcast, who-has gateway tell us. */ + size_t w = ck_net_arp_request(&p, f, sizeof f); + CHECK(w == 42 && net_eth_parse(f, w, &e) == NET_OK && e.ethertype == NET_ETHERTYPE_ARP); + CHECK(memcmp(e.destination.b, "\xff\xff\xff\xff\xff\xff", 6) == 0); + CHECK(net_arp_parse(e.payload, e.payload_len, &a) == NET_OK && a.operation == NET_ARP_REQUEST && + memcmp(a.target_ip.b, p.gw_ip.b, 4) == 0 && memcmp(a.sender_ip.b, p.ip.b, 4) == 0 && + memcmp(a.sender_mac.b, p.mac.b, 6) == 0); + CHECK(ck_net_arp_request(&p, f, 41) == 0); + + /* ARP reply from the gateway is learned; look-alikes are not. */ + w = gw_arp(NET_ARP_REPLY, p.mac, k_gw_mac, p.gw_ip, p.mac, p.ip, f, sizeof f); + memset(&m, 0, sizeof m); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_ARP_GW && memcmp(m.b, k_gw_mac.b, 6) == 0); + CHECK(ck_net_classify(&p, f, w - 1, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* truncated */ + net_ipv4 other = {{10, 0, 2, 3}}; + w = gw_arp(NET_ARP_REPLY, p.mac, k_gw_mac, other, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* not the gateway */ + net_mac bc = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, zero = {{0}}, mc = {{0x01, 0, 0x5e, 0, 0, 1}}; + w = gw_arp(NET_ARP_REPLY, p.mac, bc, p.gw_ip, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* broadcast sender MAC */ + w = gw_arp(NET_ARP_REPLY, p.mac, zero, p.gw_ip, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* zero sender MAC */ + w = gw_arp(NET_ARP_REPLY, p.mac, mc, p.gw_ip, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* multicast sender MAC */ + w = gw_arp(NET_ARP_REPLY, k_gw_mac, k_gw_mac, p.gw_ip, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* not addressed to us */ + w = gw_arp(NET_ARP_REPLY, bc, k_gw_mac, p.gw_ip, p.mac, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* broadcast reply */ + + /* ARP request for our address is answered to its sender. */ + w = gw_arp(NET_ARP_REQUEST, bc, k_gw_mac, p.gw_ip, zero, p.ip, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_ARP_ASK); + w = ck_net_arp_reply(&p, &a, f, sizeof f); + net_arp_packet r; + CHECK(w == 42 && net_eth_parse(f, w, &e) == NET_OK && memcmp(e.destination.b, k_gw_mac.b, 6) == 0 && + net_arp_parse(e.payload, e.payload_len, &r) == NET_OK && r.operation == NET_ARP_REPLY && + memcmp(r.target_ip.b, p.gw_ip.b, 4) == 0 && memcmp(r.sender_mac.b, p.mac.b, 6) == 0); + w = gw_arp(NET_ARP_REQUEST, bc, k_gw_mac, p.gw_ip, zero, other, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); /* asks for someone else */ + + /* UDP out: refused before the gateway MAC is known, then well formed. */ + static const char ping[] = "AIENOS-CK-NET ping nonce=0123abcd"; + CHECK(ck_net_udp_frame(&p, 1, (const uint8_t *)ping, sizeof ping - 1, f, sizeof f) == 0); + p.gw_mac = k_gw_mac; + w = ck_net_udp_frame(&p, 1, (const uint8_t *)ping, sizeof ping - 1, f, sizeof f); + CHECK(w == 14 + 20 + 8 + sizeof ping - 1); + CHECK(ck_net_udp_frame(&p, 1, f, 1473, f + 0, sizeof f) == 0); /* > 1500-byte IP datagram buffer */ + net_ipv4_header ih; + const uint8_t *ipl = 0; + size_t ipn = 0; + net_udp_header uh; + CHECK(net_eth_parse(f, w, &e) == NET_OK && memcmp(e.destination.b, k_gw_mac.b, 6) == 0 && + net_ipv4_parse(e.payload, e.payload_len, &ih, &ipl, &ipn) == NET_OK && ih.protocol == NET_IPPROTO_UDP && + net_udp_parse(ipl, ipn, p.ip, p.gw_ip, &uh, &pl, &pn) == NET_OK && uh.destination_port == CK_NET_ECHO_PORT && + pn == sizeof ping - 1 && memcmp(pl, ping, pn) == 0); + + /* UDP back: the gateway's reply, built with the roles swapped. */ + ck_net_peer g; + memset(&g, 0, sizeof g); + g.mac = k_gw_mac; + g.ip = p.gw_ip; + g.gw_mac = p.mac; + g.gw_ip = p.ip; + g.lport = CK_NET_ECHO_PORT; + g.rport = CK_NET_LOCAL_PORT; + static const char pong[] = "AIENOS-CK-NET pong token=feedf00d echo=AIENOS-CK-NET ping nonce=0123abcd"; + w = ck_net_udp_frame(&g, 7, (const uint8_t *)pong, sizeof pong - 1, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_UDP_REPLY && cs == 1 && + pn == sizeof pong - 1 && memcmp(pl, pong, pn) == 0); + size_t udp_off = 14 + 20; + f[udp_off + 8] ^= 1; /* corrupt payload: checksum must refuse it */ + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); + f[udp_off + 8] ^= 1; + f[udp_off + 6] = f[udp_off + 7] = 0; /* no checksum: allowed, reported */ + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_UDP_REPLY && cs == 0); + g.lport = 9999; /* wrong source port */ + w = ck_net_udp_frame(&g, 7, (const uint8_t *)pong, sizeof pong - 1, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); + g.lport = CK_NET_ECHO_PORT; + g.ip = other; /* wrong source address */ + w = ck_net_udp_frame(&g, 7, (const uint8_t *)pong, sizeof pong - 1, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); + g.ip = p.gw_ip; + g.gw_mac = k_gw_mac; /* not addressed to our MAC */ + w = ck_net_udp_frame(&g, 7, (const uint8_t *)pong, sizeof pong - 1, f, sizeof f); + CHECK(ck_net_classify(&p, f, w, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); + CHECK(ck_net_classify(&p, f, 13, &m, &a, &pl, &pn, &cs) == CK_NET_IGNORED); + + char s[8]; + ck_net_printable((const uint8_t *)"a\"b\\\x01\x7f" "cdef", 10, s, sizeof s); + CHECK(strcmp(s, "a'b'..c") == 0); + + /* Bind without an SMMU (host shim: CK_SMMU_ABSENT) fails closed: the + * function's command register never gets memory decode or bus master. */ + static uint8_t cfg[4096]; + static pci_func vf; + memset(cfg, 0, sizeof cfg); + memset(&vf, 0, sizeof vf); + vf.vendor = 0x1af4; + vf.device = 0x1041; + vf.cfg = cfg; + vf.bar[4].addr = 0x10000000u; + vf.bar[4].size = 0x4000u; + virtio_pci_caps caps; + memset(&caps, 0, sizeof caps); + caps.common_cfg = (virtio_pci_region){1, 4, 0, 0x1000}; + caps.isr_cfg = (virtio_pci_region){1, 4, 0x1000, 0x1000}; + caps.device_cfg = (virtio_pci_region){1, 4, 0x2000, 0x1000}; + caps.notify_cfg = (virtio_pci_region){1, 4, 0x3000, 0x1000}; + caps.has_notify_off_multiplier = 1; + caps.notify_off_multiplier = 4; + printf(" [virtio-net bind without SMMU, host, not hardware]\n"); + CHECK(ck_net_bind_selftest(&vf, &caps) == CK_NET_E_DENIED); + CHECK((pci_r16(vf.cfg, 4) & 0x6u) == 0); /* no MEM, no BM */ + CHECK(ck_net_live() == 0); + ck_net_release(); /* idempotent */ + CHECK(ck_net_bind_selftest(NULL, &caps) == CK_NET_E_ARG); +} + int main(void) { setvbuf(stdout, NULL, _IOLBF, 0); @@ -442,6 +602,7 @@ int main(void) test_store(); test_security(); test_nvme_shutdown(); + test_net_udp(); printf("CK_STAGE_HOST: %s checks=%d failures=%d\n", failures ? "FAIL" : "PASS", checks, failures); return failures ? 1 : 0; } diff --git a/native/kernel/tools/ck_udp_echo.c b/native/kernel/tools/ck_udp_echo.c new file mode 100644 index 00000000..220e96a9 --- /dev/null +++ b/native/kernel/tools/ck_udp_echo.c @@ -0,0 +1,108 @@ +/* ck_udp_echo.c -- host-side UDP echo helper for scripts/qemu_ck_net_test.sh + * (gate AIENOS_CK_NET). Hosted C (POSIX sockets), test tooling only, never + * in the kernel image. No Python, no nc/socat. + * + * ck_udp_echo PORT TOKEN LOGFILE SECONDS + * + * Binds 127.0.0.1:PORT (QEMU user networking forwards guest datagrams for + * 10.0.2.2:PORT there), writes "ready port=PORT" to LOGFILE, then for SECONDS + * answers every datagram that starts with "AIENOS-CK-NET ping " with + * "AIENOS-CK-NET pong token=TOKEN echo=" + * and logs each one ("rx ..." / "tx ..."). TOKEN (hex, 8..64 chars) is chosen + * by the script per run, so a guest line carrying it proves the reply came + * from this process. Exit 0 at the deadline, 2 usage, 3 socket/bind error. */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PING "AIENOS-CK-NET ping " +#define MAX_ECHO 400 + +static double now_s(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; +} + +static void show(FILE *lg, const unsigned char *b, size_t n) +{ + for (size_t i = 0; i < n; i++) fputc((b[i] >= 0x20 && b[i] < 0x7f && b[i] != '"') ? b[i] : '.', lg); +} + +int main(int argc, char **argv) +{ + if (argc != 5) { + fprintf(stderr, "usage: ck_udp_echo PORT TOKEN LOGFILE SECONDS\n"); + return 2; + } + char *end = 0; + long port = strtol(argv[1], &end, 10); + long secs = strtol(argv[4], 0, 10); + const char *token = argv[2]; + size_t tl = strlen(token); + if (*end || port < 1024 || port > 65535 || secs < 1 || secs > 3600 || tl < 8 || tl > 64 || + strspn(token, "0123456789abcdef") != tl) { + fprintf(stderr, "ck_udp_echo: bad arguments\n"); + return 2; + } + FILE *lg = fopen(argv[3], "w"); + if (!lg) { + perror("ck_udp_echo: log"); + return 2; + } + setvbuf(lg, 0, _IOLBF, 0); + int s = socket(AF_INET, SOCK_DGRAM, 0); + struct sockaddr_in a; + memset(&a, 0, sizeof a); + a.sin_family = AF_INET; + a.sin_port = htons((unsigned short)port); + a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof a) != 0) { + fprintf(lg, "bind failed port=%ld errno=%d (%s)\n", port, errno, strerror(errno)); + fclose(lg); + return 3; + } + fprintf(lg, "ready port=%ld\n", port); + double deadline = now_s() + (double)secs; + unsigned replies = 0; + for (;;) { + double left = deadline - now_s(); + if (left <= 0) break; + struct pollfd pf = {s, POLLIN, 0}; + int pr = poll(&pf, 1, (int)(left * 1000.0) + 1); + if (pr < 0 && errno == EINTR) continue; + if (pr <= 0) continue; + unsigned char in[2048], out[2048]; + struct sockaddr_in from; + socklen_t fl = sizeof from; + ssize_t n = recvfrom(s, in, sizeof in, 0, (struct sockaddr *)&from, &fl); + if (n < 0) continue; + char fa[INET_ADDRSTRLEN] = "?"; + inet_ntop(AF_INET, &from.sin_addr, fa, sizeof fa); + fprintf(lg, "rx from %s:%u len=%zd payload=\"", fa, ntohs(from.sin_port), n); + show(lg, in, (size_t)n); + fprintf(lg, "\"\n"); + if ((size_t)n < sizeof PING - 1 || memcmp(in, PING, sizeof PING - 1) != 0) { + fprintf(lg, "ignored (not a ping)\n"); + continue; + } + int h = snprintf((char *)out, sizeof out, "AIENOS-CK-NET pong token=%s echo=", token); + size_t m = (size_t)n > MAX_ECHO ? MAX_ECHO : (size_t)n; + memcpy(out + h, in, m); + ssize_t sent = sendto(s, out, (size_t)h + m, 0, (struct sockaddr *)&from, fl); + fprintf(lg, "tx to %s:%u len=%zd reply=%u\n", fa, ntohs(from.sin_port), sent, ++replies); + } + fprintf(lg, "done replies=%u\n", replies); + fclose(lg); + close(s); + return 0; +} diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index d980232a..c76b5e24 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -3,11 +3,12 @@ # gates, with a content-addressed receipt. # # It refuses a dirty tree, runs the C kernel QEMU scripts that exist -# (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh), and prints +# (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh, +# scripts/qemu_ck_net_test.sh), and prints # one line per gate: # AIENOS_CK_: PASS|FAIL|NOT_RUN [(reason)] # for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH -# M4_CONTINUITY M4_RECOVERY ARGUS1_REVOKE KEYBOARD. Gates with no C +# M4_CONTINUITY M4_RECOVERY ARGUS1_REVOKE KEYBOARD NET. Gates with no C # implementation print NOT_RUN (MISSING_IMPLEMENTATION: ) and never # run anything. A missing child script, a child that reports NOT_RUN, a child # that prints no verdict line, or a PASS line contradicted by the child's exit @@ -37,6 +38,7 @@ set -uo pipefail # GATE TABLE: gate | source | reason when there is no C implementation # source: boot -> verdict line from scripts/qemu_ck_boot_test.sh # store -> verdict line from scripts/qemu_ck_store_test.sh +# net -> verdict line from scripts/qemu_ck_net_test.sh # missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS # =========================================================================== CK_GATE_TABLE=' @@ -53,8 +55,9 @@ M4_CONTINUITY|missing|no continuity core (ADR 0016 agent identity and memory) in M4_RECOVERY|missing|no Recovery Core (ADR 0006) in the C kernel ARGUS1_REVOKE|store|- KEYBOARD|missing|no xHCI/USB HID keyboard driver in the C kernel +NET|net|- ' -CHILDREN=(boot store) +CHILDREN=(boot store net) # =========================================================================== repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -63,6 +66,7 @@ AAVMF_CODE_FD="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" AAVMF_VARS_FD="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" PHYSICAL_STATEMENT="QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." M4_NVME_PASS_NOTE="SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately" +NET_PASS_NOTE="QEMU user networking (slirp): attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A; QEMU does not route virtio-net DMA through the SMMU (no ACCESS_PLATFORM), so confinement is not proven for it" die() { echo "ck_gates: $*" >&2; exit 2; } json_str() { @@ -90,6 +94,7 @@ set_default_children() { declare -gA child_script=( [boot]="${repo_root}/scripts/qemu_ck_boot_test.sh" [store]="${repo_root}/scripts/qemu_ck_store_test.sh" + [net]="${repo_root}/scripts/qemu_ck_net_test.sh" ) } @@ -191,8 +196,9 @@ evaluate_table() { v="${out%%|*}"; r="${out#*|}" # M4_NVME PASS means the SMMU-confined mode: say so. [[ "${v}" == PASS && "${gate}" == M4_NVME ]] && r="${M4_NVME_PASS_NOTE}" + [[ "${v}" == PASS && "${gate}" == NET ]] && r="${NET_PASS_NOTE}" gate_rows+=("${gate}|${src}|${v}|${r}") - if [[ "${v}" == PASS && "${gate}" == M4_NVME ]]; then + if [[ "${v}" == PASS && ( "${gate}" == M4_NVME || "${gate}" == NET ) ]]; then echo "AIENOS_CK_${gate}: PASS (${r})" elif [[ "${v}" == PASS ]]; then echo "AIENOS_CK_${gate}: PASS" @@ -320,9 +326,9 @@ self_test() { local g for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD; do expect "${g}" NOT_RUN; done } - scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT + scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] (no NET_SCRIPT: missing) scen="$1" - declare -gA child_script=([boot]="$2" [store]="$3") + declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}") run_children 2>/dev/null evaluate_table >"${tmp}/${scen}.out" } @@ -333,21 +339,24 @@ self_test() { "$(fake storeA 0 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS (narrow)' \ 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_NVME_SHUTDOWN: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted confined' \ 'PASS DMA outside the window faulted, page intact, controller still usable (differs)' 'PASS NVMe DMA denied without an SMMU' \ - 'PASS NVMe DMA granted through the TEST-ONLY bypass')" - expect M1 PASS; expect M3 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect_missing_all + 'PASS NVMe DMA granted through the TEST-ONLY bypass')" \ + "$(fake netA 0 'PASS UDP reply received' 'AIENOS_CK_NET: PASS')" + expect M1 PASS; expect M3 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect NET PASS; expect_missing_all count_rows - [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "7/0/6/13" && ${overall} == NOT_ALL_GATES_PASS ]] \ - && ok "A: counts 7/0/6 of 13, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" + [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "8/0/6/14" && ${overall} == NOT_ALL_GATES_PASS ]] \ + && ok "A: counts 8/0/6 of 14, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" [[ "$(nvme_dma_mode)" == "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately" ]] && ok "A: confined DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'" grep -qxF "AIENOS_CK_M4_NVME: PASS (${M4_NVME_PASS_NOTE})" "${tmp}/A.out" \ && ok "A: M4_NVME PASS line carries the confined-mode note" || bad "A: M4_NVME PASS line lacks the note" + grep -qxF "AIENOS_CK_NET: PASS (${NET_PASS_NOTE})" "${tmp}/A.out" \ + && ok "A: NET PASS line carries the QEMU slirp / no-SMMU-enforcement note" || bad "A: NET PASS line lacks the note" grep -qx 'AIENOS_CK_KEYBOARD: NOT_RUN (MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel)' "${tmp}/A.out" \ && ok "A: missing gate prints NOT_RUN (MISSING_IMPLEMENTATION: reason)" || bad "A: KEYBOARD line wrong" - [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 13 ]] && ok "A: exactly 13 verdict lines" || bad "A: verdict line count" + [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 14 ]] && ok "A: exactly 14 verdict lines" || bad "A: verdict line count" # B: boot FAIL; store script missing -> its three gates NOT_RUN, never PASS. scenario B "$(fake bootB 1 'FAIL kernel: alive' 'AIENOS_CK_M3: FAIL' 'AIENOS_CK_M1: FAIL')" "${tmp}/kids/does_not_exist.sh" - expect M1 FAIL; expect M3 FAIL; expect M4_NVME NOT_RUN; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN; expect SMMU NOT_RUN; expect NVME_SHUTDOWN NOT_RUN; expect_missing_all + expect M1 FAIL; expect M3 FAIL; expect M4_NVME NOT_RUN; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN; expect SMMU NOT_RUN; expect NVME_SHUTDOWN NOT_RUN; expect NET NOT_RUN; expect_missing_all [[ "$(nvme_dma_mode)" == "not run" ]] && ok "B: dma mode 'not run' when the store script is missing" || bad "B: dma mode" # C: boot NOT_RUN (quiet flag held, exit 3); store mixed with exit 1. @@ -389,10 +398,10 @@ self_test() { bad "receipt name does not match its content hash" fi if command -v jq >/dev/null; then - jq -e '.physical == "NOT_RUN" and (.gates | length) == 13 and .verdict == "NOT_ALL_GATES_PASS" + jq -e '.physical == "NOT_RUN" and (.gates | length) == 14 and .verdict == "NOT_ALL_GATES_PASS" and ([.gates[] | select(.id == "M1")][0].verdict == "PASS") and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL") - and (.children | length) == 2 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ + and (.children | length) == 3 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong" else echo "SKIP jq not installed; JSON validity not machine-checked" @@ -405,18 +414,18 @@ self_test() { bad "an existing receipt was overwritten or the clash was not refused" fi - # The gate table parses: 13 rows, known sources, reasons only on missing rows. + # The gate table parses: 14 rows, known sources, reasons only on missing rows. local g s w rows=0 tbad=0 while IFS='|' read -r g s w; do [[ -n "${g}" ]] || continue rows=$((rows + 1)) case "${s}" in - boot|store) [[ "${w}" == - ]] || tbad=1 ;; + boot|store|net) [[ "${w}" == - ]] || tbad=1 ;; missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;; *) tbad=1 ;; esac done <<<"${CK_GATE_TABLE}" - [[ ${tbad} == 0 && ${rows} == 13 ]] && ok "gate table: 13 rows well-formed" || bad "gate table malformed (${rows} rows)" + [[ ${tbad} == 0 && ${rows} == 14 ]] && ok "gate table: 14 rows well-formed" || bad "gate table malformed (${rows} rows)" # Dirty-tree refusal, end to end, on a private clone (never this tree). # Both runs stop before any child script (no QEMU). diff --git a/scripts/qemu_ck_net_test.sh b/scripts/qemu_ck_net_test.sh new file mode 100755 index 00000000..c28c7652 --- /dev/null +++ b/scripts/qemu_ck_net_test.sh @@ -0,0 +1,236 @@ +#!/usr/bin/env bash +# Network gate for the AIENOS C kernel: AIENOS_CK_NET (C-only gate; the Rust +# kernel has no virtio-net queue driver, so there is no Rust counterpart). +# Boots the full C image (make full) in QEMU AArch64 with UEFI (AAVMF), an +# NVMe disk (so the boot is the normal full boot) and a virtio-net-pci NIC on +# QEMU user networking (slirp). QEMU is not hardware: a PASS here qualifies +# nothing physical. +# +# The devices stage (native/kernel/dev/net_bind.c) binds native/net's polled +# virtio-net driver: SMMU window for its one DMA region first, then memory +# decode + bus master, vnet_init, then ARP for the slirp gateway 10.0.2.2 and +# one UDP datagram from 10.0.2.15:47030 to 10.0.2.2:47029, then device reset, +# bus master off, SMMU stream back to abort. Slirp forwards 10.0.2.2:47029 to +# the host's 127.0.0.1:47029, where the in-tree C helper +# native/kernel/tools/ck_udp_echo.c (make udp-echo; no Python, no nc/socat) +# answers "AIENOS-CK-NET pong token= echo=". +# +# boot 1 iommu=smmuv3: must pass every M1 check (scripts/lib_ck_m1_checks.sh) +# and show +# (a) attach: caps parsed, SMMU window for virtio_net, "dma_gate: virtio_net +# granted (Confined)", "virtio_net: attached ... mac=52:54:00:12:34:56" +# (QEMU's default NIC MAC), ARP reply from the slirp gateway MAC +# 52:55:0a:00:02:02; +# (b) TX: the guest's "net: udp tx ... completed=yes" line (the device +# returned the TX descriptor) AND the helper's log shows that exact +# datagram (same nonce) arriving from 127.0.0.1; +# (c) RX: the guest's "net: udp rx ... udp_csum=verified parsed=m6a +# echo_of_ours=yes" line carries this run's helper token and the +# guest's own nonce, and the helper logged its reply; +# plus the release order (device reset, bus master revoked, stream back to +# abort, after the round trip) and "devices: ... virtio_net=selftest-ok". +# boot 2 no SMMU, same image: virtio-net DMA must be denied (NoSmmu), no +# grant, no datagram sent, and the helper must log nothing new. +# Not proven (stated, not hidden): the driver does not negotiate +# VIRTIO_F_ACCESS_PLATFORM and QEMU's virtio-pci then does device DMA without +# the vIOMMU, so the SMMU window is programmed but not enforced by QEMU for +# this device (the kernel prints access_platform=no; this script checks it). +# Netdev: -netdev user,ipv6=off,restrict=off (AIENOS_NET_RESTRICT overrides). +# Measured 2026-10-01 with QEMU on the Spark: AIENOS_NET_RESTRICT=on makes the +# gate FAIL (ARP still answers, but the datagram never reaches the helper on +# 127.0.0.1), so restricted slirp cannot carry this round trip; nothing else +# listens for the guest and the run lasts two short boots. +# +# Takes the machine quiet flag itself like the other qemu_ck_* scripts +# (AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG). Final line: AIENOS_CK_NET: +# PASS|FAIL|NOT_RUN. Exit 0 PASS, 1 FAIL, 2 missing tools, 3 NOT_RUN. +# Needs qemu-system-aarch64, AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64) +# and a C compiler. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" + +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" +command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "AIENOS_CK_NET: NOT_RUN"; exit 2; } +[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "AIENOS_CK_NET: NOT_RUN"; exit 2; } + +cross="" +if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" +out="${repo_root}/target/native-kernel" +make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full >/dev/null +make -s -C native/kernel OUT="${out}" udp-echo >/dev/null +efi="${out}/full/BOOTAA64.EFI" +echo_bin="${out}/host/ck_udp_echo" +port="$(sed -nE 's/^#define CK_NET_ECHO_PORT ([0-9]+)u.*/\1/p' native/kernel/dev/net_udp.h)" +lport="$(sed -nE 's/^#define CK_NET_LOCAL_PORT ([0-9]+)u.*/\1/p' native/kernel/dev/net_udp.h)" +[[ -n "${port}" && -n "${lport}" ]] || { echo "FAIL ports not found in native/kernel/dev/net_udp.h"; echo "AIENOS_CK_NET: FAIL"; exit 1; } + +quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" +quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_net_test $$}" +if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then + echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" + echo "AIENOS_CK_NET: NOT_RUN" + exit 3 +fi +own_flag=1 +release_flag() { + if [[ "${own_flag}" == 1 ]]; then + own_flag=0 + if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi + fi +} +top="$(mktemp -d)" +helper_pid="" +cleanup() { + if [[ -n "${helper_pid}" ]]; then kill "${helper_pid}" 2>/dev/null || true; wait "${helper_pid}" 2>/dev/null || true; fi + rm -rf "${top}" + release_flag +} +trap cleanup EXIT + +# Per-run token: the guest can only print it if the helper's reply reached it. +token="$(od -An -N8 -tx1 /dev/urandom | tr -d ' \n')" +[[ "${token}" =~ ^[0-9a-f]{16}$ ]] || { echo "FAIL could not read a random token"; echo "AIENOS_CK_NET: FAIL"; exit 1; } +hlog="${top}/helper.log" +"${echo_bin}" "${port}" "${token}" "${hlog}" "${AIENOS_NET_HELPER_SECONDS:-600}" & +helper_pid=$! +for _ in $(seq 1 50); do + grep -q '^ready port=' "${hlog}" 2>/dev/null && break + kill -0 "${helper_pid}" 2>/dev/null || break + sleep 0.1 +done +if ! grep -q "^ready port=${port}$" "${hlog}" 2>/dev/null; then + echo "NOT_RUN UDP echo helper could not listen on 127.0.0.1:${port}: $(head -c 200 "${hlog}" 2>/dev/null || true)" + echo "AIENOS_CK_NET: NOT_RUN" + exit 3 +fi +echo "PASS UDP echo helper listening on 127.0.0.1:${port} (token ${token})" + +# shellcheck source=scripts/lib_ck_m1_checks.sh +source "${repo_root}/scripts/lib_ck_m1_checks.sh" +img_bytes=67108864 +image="${top}/nvme.img" +truncate -s "${img_bytes}" "${image}" +net_fail=0; m1_fail=0 + +# boot [smmu]: one QEMU boot; serial text in ${work}/serial.txt. +boot() { + work="${top}/$1" + mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" + touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" + cp "${efi}" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" + cp "${vars_fd}" "${work}/vars.fd" + local machine="virt,virtualization=on,gic-version=3" + [[ "${2:-}" != smmu ]] || machine+=",iommu=smmuv3" + set +e + # Issue #61: single-threaded TCG (see qemu_boot_test.sh). + timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \ + -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ + -drive if=pflash,format=raw,file="${work}/vars.fd" \ + -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ + -device virtio-blk-pci,drive=esp \ + -drive if=none,id=nvme0,format=raw,file="${image}" \ + -device nvme,drive=nvme0,serial=aienos-net-test \ + -netdev user,id=n0,ipv6=off,restrict="${AIENOS_NET_RESTRICT:-off}" \ + -device virtio-net-pci,netdev=n0,mac=52:54:00:12:34:56 \ + -device ramfb -display none \ + -serial file:"${work}/serial.log" -no-reboot + qemu_status=$? + set -e + tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" + [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_net_$1.log" + echo "== boot $1: qemu exit ${qemu_status}" + failed=0 + ck_m1_checks + [[ "${failed}" == 0 ]] || m1_fail=1 + failed=0 +} +line_of() { grep -n -- "$1" "${work}/serial.txt" | head -1 | cut -d: -f1; } + +# ---- boot 1: SMMU present, full round trip ---- +boot net-smmu smmu +echo "-- (a) attach" +check "virtio-net capabilities parsed" "^virtio_net: [0-9a-f:.]* caps ok common=bar" +check "SMMU window programmed for the virtio-net DMA region only" \ + "^smmu_dma_window: virtio_net only iova=0x[0-9a-f]* len=0x[0-9a-f]* rid=0x[0-9a-f]* stream_id=0x[0-9a-f]*$" +check "virtio-net DMA granted only after confinement" "^dma_gate: virtio_net granted (Confined), bus master on$" +check "driver attached, QEMU NIC MAC read from the device, ACCESS_PLATFORM not negotiated" \ + "^virtio_net: attached [0-9a-f:.]* rid=0x[0-9a-f]* qsize=rx[0-9]*/tx[0-9]* mac=52:54:00:12:34:56 features=0x[0-9a-f]* access_platform=no$" +check "ARP reply from the slirp gateway (52:55:0a:00:02:02)" \ + "^net: arp who-has 10.0.2.2 tell 10.0.2.15 -> 52:55:0a:00:02:02 tries=[0-9]*$" +echo "-- (b) TX" +tx_re="^net: udp tx 10\.0\.2\.15:${lport} -> 10\.0\.2\.2:${port} frame=[0-9]+ bytes payload=\"(AIENOS-CK-NET ping nonce=[0-9a-f]{8})\" try=[0-9]+ completed=yes$" +tx_line="$(grep -E "${tx_re}" "${work}/serial.txt" | head -1 || true)" +nonce="" +if [[ "${tx_line}" =~ ${tx_re} ]]; then + nonce="${BASH_REMATCH[1]}" + echo "PASS guest sent one UDP frame and the device completed it (${nonce})" +else + echo "FAIL guest UDP TX line with completed=yes"; failed=1 +fi +if [[ -n "${nonce}" ]] && grep -qE "^rx from 127\.0\.0\.1:[0-9]+ len=[0-9]+ payload=\"${nonce}\"$" "${hlog}"; then + echo "PASS host helper received the guest's datagram through slirp (same nonce)" +else + echo "FAIL host helper did not log the guest's datagram"; failed=1 +fi +echo "-- (c) RX" +if grep -qE '^tx to 127\.0\.0\.1:[0-9]+ len=[0-9]+ reply=[0-9]+$' "${hlog}"; then + echo "PASS host helper sent its reply" +else + echo "FAIL host helper reply not logged"; failed=1 +fi +rx_want="^net: udp rx 10\.0\.2\.2:${port} -> 10\.0\.2\.15:${lport} payload_len=[0-9]+ udp_csum=verified parsed=m6a echo_of_ours=yes payload=\"AIENOS-CK-NET pong token=${token} echo=${nonce:-NONE}\"$" +if [[ -n "${nonce}" ]] && grep -qE "${rx_want}" "${work}/serial.txt"; then + echo "PASS guest received and parsed (M6-A, checksum verified) the helper's reply with this run's token" +else + echo "FAIL guest UDP RX line with this run's token and its own nonce"; failed=1 +fi +check "round trip reported" "^net: udp round trip ok tx_reclaimed=[1-9][0-9]* rx_frames=[1-9][0-9]*$" +check "net selftest PASS" "^net: selftest PASS (rc=0)$" +echo "-- release" +check "device reset before the revoke" "^virtio_net: device reset status=0x00 (stopped)$" +check "virtio-net bus master revoked" "^dma_gate: virtio_net bus master revoked$" +check "virtio-net SMMU stream returned to abort" "^smmu: virtio_net stream 0x[0-9a-f]* returned to abort (rc=0)$" +lg="$(line_of '^dma_gate: virtio_net granted (Confined)')"; lt="$(line_of '^net: udp round trip ok')" +lr="$(line_of '^dma_gate: virtio_net bus master revoked$')"; la="$(line_of '^smmu: virtio_net stream .* returned to abort')" +if [[ -n "${lg}" && -n "${lt}" && -n "${lr}" && -n "${la}" && "${lg}" -lt "${lt}" && "${lt}" -lt "${lr}" && "${lr}" -lt "${la}" ]]; then + echo "PASS order: grant < round trip < bus master off < stream abort" +else + echo "FAIL release order (lines ${lg:-none} ${lt:-none} ${lr:-none} ${la:-none})"; failed=1 +fi +check "devices stage summary" "^devices: pci=ok nvme=bound virtio_net=selftest-ok$" +check_absent "no virtio-net failure lines" "^net: .*FAIL\|^virtio_net: .*FAIL\|^virtio_net: not bound" +[[ "${failed}" == 0 ]] || net_fail=1 +failed=0 +helper_rx_after_boot1="$(grep -c '^rx from ' "${hlog}" || true)" + +# ---- boot 2: no SMMU, fail closed ---- +boot net-nosmmu +check "virtio-net DMA denied without an SMMU" "^dma_gate: virtio_net denied (NoSmmu rc=-1), bus master stays off$" +check "virtio-net not bound without an SMMU" "^virtio_net: not bound (SMMU DMA isolation not active)$" +check_absent "no virtio-net grant without an SMMU" "dma_gate: virtio_net granted" +check_absent "no datagram sent without an SMMU" "^net: udp tx" +check "devices stage summary (no SMMU)" "virtio_net=selftest-failed$" +if [[ "$(grep -c '^rx from ' "${hlog}" || true)" == "${helper_rx_after_boot1}" ]]; then + echo "PASS host helper saw nothing from the no-SMMU boot" +else + echo "FAIL host helper received traffic during the no-SMMU boot"; failed=1 +fi +[[ "${failed}" == 0 ]] || net_fail=1 +release_flag + +if [[ "${m1_fail}${net_fail}" != 00 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then + for s in "${top}"/*/serial.txt; do + echo "---- serial console $(basename "$(dirname "${s}")") (net lines) ----" + grep -E '^(virtio_net|net:|dma_gate: virtio|smmu|devices:|stage |report_kind:)' "${s}" | head -40 || true + done + echo "---- helper log ----"; head -20 "${hlog}" || true +fi +[[ "${m1_fail}" != 0 ]] && echo "M1 checks failed on at least one boot: the NET verdict cannot pass" +if [[ "${m1_fail}${net_fail}" == 00 ]]; then echo "AIENOS_CK_NET: PASS"; exit 0; fi +echo "AIENOS_CK_NET: FAIL" +exit 1 From e14f107d0386ce3c42a083b3aed8b22cb329e797 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 06:19:17 -0500 Subject: [PATCH 2/4] evidence: ck_gates receipt with AIENOS_CK_NET PASS (QEMU only) Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- ...2668a18f35f86edeb751221079cb6128c190c.json | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 evidence/ck_gates_614aee9d5525668b062256a706f2668a18f35f86edeb751221079cb6128c190c.json diff --git a/evidence/ck_gates_614aee9d5525668b062256a706f2668a18f35f86edeb751221079cb6128c190c.json b/evidence/ck_gates_614aee9d5525668b062256a706f2668a18f35f86edeb751221079cb6128c190c.json new file mode 100644 index 00000000..2f6a16b8 --- /dev/null +++ b/evidence/ck_gates_614aee9d5525668b062256a706f2668a18f35f86edeb751221079cb6128c190c.json @@ -0,0 +1,41 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-01T11:17:22Z", + "finished_utc": "2026-10-01T11:19:11Z", + "commit": "96da7c7f8f50d15e012168c29862693cbf9e2bd1", + "commit_subject": "ck: virtio-net driver hook and AIENOS_CK_NET gate (QEMU slirp)", + "tree_clean_before": true, + "tree_clean_after": true, + "quiet_flag": "/home/drakestapleton/workspace/.spark-quiet", + "quiet_flag_present_at_start": false, + "host": {"hostname": "spark-b87b", "arch": "aarch64", "kernel": "7.0.0-1019-nvidia", "cpu_model": "Cortex-A725;Cortex-X925"}, + "qemu": {"binary": "/usr/bin/qemu-system-aarch64", "version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)"}, + "firmware": {"aavmf_code": "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd", "aavmf_code_sha256": "4a4cb7f6d8106bb2a7dd8c763fab14b1810152136fc4304e5b728f0043e84f12", "aavmf_vars": "/usr/share/AAVMF/AAVMF_VARS.fd", "aavmf_vars_sha256": "b3b855c5a80310168051164986855692d1bdb06e67619856177965cd87c6774f"}, + "nvme_dma_mode": "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately", + "children": [ + {"name": "boot", "script": "scripts/qemu_ck_boot_test.sh", "present": true, "script_sha256": "64bcb2d1285485b989dc8309ce0b024ea799d3bce13cff22f62924d3ae4b7cce", "exit": "0", "seconds": 6, "log_sha256": "d71d28502a6e0e310ce29c21491158c9e8eb69478386430a5ba8ccd51af92b76", "log_lines": 22}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "b005ed29619d1113536a0686cedafe84fcea95fa30321307ec1d1335ea231925", "exit": "0", "seconds": 90, "log_sha256": "16aefa841e6dbbce611c78c17bc3c502ecba0ab85797d333b4735e0cf82f863b", "log_lines": 533}, + {"name": "net", "script": "scripts/qemu_ck_net_test.sh", "present": true, "script_sha256": "8c32114b65ba41cfbf40aac7468777cf76befa44299380e8ec75e604f66d60c9", "exit": "0", "seconds": 13, "log_sha256": "20a7be1b0933f4a7da7bf21d2116087c8b105987c897bdfbebdfc333e0b63464", "log_lines": 59} + ], + "gates": [ + {"id": "M1", "marker": "AIENOS_CK_M1", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M3", "marker": "AIENOS_CK_M3", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "SMMU", "marker": "AIENOS_CK_SMMU", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "NVME_SHUTDOWN", "marker": "AIENOS_CK_NVME_SHUTDOWN", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "P2_ARTIFACT", "marker": "AIENOS_CK_P2_ARTIFACT", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no signed artifact loader (EL0, W^X, admission receipts) in the C kernel"}, + {"id": "M0_ROLLBACK", "marker": "AIENOS_CK_M0_ROLLBACK", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md)"}, + {"id": "M4_NVME", "marker": "AIENOS_CK_M4_NVME", "source": "store", "verdict": "PASS", "reason": "SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"}, + {"id": "M4_STORE", "marker": "AIENOS_CK_M4_STORE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_STORE_CRASH", "marker": "AIENOS_CK_M4_STORE_CRASH", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no C Store crash/kill campaign (4 KiB or 512 B) and no slot reuse (C Store is append-only)"}, + {"id": "M4_CONTINUITY", "marker": "AIENOS_CK_M4_CONTINUITY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no continuity core (ADR 0016 agent identity and memory) in the C kernel"}, + {"id": "M4_RECOVERY", "marker": "AIENOS_CK_M4_RECOVERY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no Recovery Core (ADR 0006) in the C kernel"}, + {"id": "ARGUS1_REVOKE", "marker": "AIENOS_CK_ARGUS1_REVOKE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "KEYBOARD", "marker": "AIENOS_CK_KEYBOARD", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel"}, + {"id": "NET", "marker": "AIENOS_CK_NET", "source": "net", "verdict": "PASS", "reason": "QEMU user networking (slirp): attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A; QEMU does not route virtio-net DMA through the SMMU (no ACCESS_PLATFORM), so confinement is not proven for it"} + ], + "counts": {"total": 14, "pass": 8, "fail": 0, "not_run": 6}, + "verdict": "NOT_ALL_GATES_PASS", + "parity": "native/kernel/GATES.md", + "physical": "NOT_RUN", + "physical_statement": "QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." +} From e750ea41ccc648ae4b7c4a6e98d452687e9b40f0 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 06:25:19 -0500 Subject: [PATCH 3/4] ck net: review fixes (refuse rebind while live, reset in release order check, comment) Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- native/kernel/GATES.md | 2 +- native/kernel/dev/net_bind.c | 8 +++++++- scripts/qemu_ck_net_test.sh | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index f594cc2d..2e30ac57 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -264,7 +264,7 @@ complete. | 102 | virtio-net attach behind an SMMU window (`virtio_net: .. caps ok`, `smmu_dma_window: virtio_net only iova=.. len=.. rid=.. stream_id=..`, `dma_gate: virtio_net granted (Confined), bus master on`, `virtio_net: attached .. mac=52:54:00:12:34:56 .. access_platform=no`, ARP reply from the slirp gateway `52:55:0a:00:02:02`) | NET | DIFFERS (C-only; no Rust counterpart: the Rust kernel has no virtio-net queue driver. Boot 1, `iommu=smmuv3`, QEMU user networking (slirp) `restrict=off`, driver `native/net/aienos_virtio_net.c` via `native/kernel/dev/net_bind.c`. NOT proven: the driver does not negotiate VIRTIO_F_ACCESS_PLATFORM, so QEMU does virtio-net DMA without the vIOMMU and the window is programmed but not enforced for this device) | | 103 | one UDP frame sent (TX) | NET | DIFFERS (C-only: guest `net: udp tx 10.0.2.15:47030 -> 10.0.2.2:47029 .. payload="AIENOS-CK-NET ping nonce=<8 hex>" .. completed=yes` AND the in-tree host helper `native/kernel/tools/ck_udp_echo.c` (`make udp-echo`) logs the same nonce arriving on 127.0.0.1:47029 through slirp) | | 104 | one UDP frame received (RX) and parsed by M6-A | NET | DIFFERS (C-only: guest `net: udp rx 10.0.2.2:47029 -> 10.0.2.15:47030 .. udp_csum=verified parsed=m6a echo_of_ours=yes payload="AIENOS-CK-NET pong token= echo="`; the token is generated by the script per run, so the line cannot pass without the helper's reply reaching the guest; plus `net: udp round trip ok`, `net: selftest PASS (rc=0)` and `devices: pci=ok nvme=bound virtio_net=selftest-ok`) | -| 105 | virtio-net released after the round trip | NET | DIFFERS (C-only: `virtio_net: device reset status=0x00 (stopped)`, `dma_gate: virtio_net bus master revoked`, `smmu: virtio_net stream 0x.. returned to abort (rc=0)`; line order grant < round trip < revoke < abort) | +| 105 | virtio-net released after the round trip | NET | DIFFERS (C-only: `virtio_net: device reset status=0x00 (stopped)`, `dma_gate: virtio_net bus master revoked`, `smmu: virtio_net stream 0x.. returned to abort (rc=0)`; line order grant < round trip < reset < revoke < abort) | | 106 | virtio-net fails closed without an SMMU | NET | DIFFERS (C-only: boot 2 without `iommu=smmuv3`: `dma_gate: virtio_net denied (NoSmmu rc=-1), bus master stays off`, `virtio_net: not bound (SMMU DMA isolation not active)`, absent `dma_gate: virtio_net granted` and `net: udp tx`, and the host helper receives nothing. Host test `stage_test` also checks the fail-closed bind leaves the command register without MEM/BME) | ## Summary counts diff --git a/native/kernel/dev/net_bind.c b/native/kernel/dev/net_bind.c index 5700836f..db3075cf 100644 --- a/native/kernel/dev/net_bind.c +++ b/native/kernel/dev/net_bind.c @@ -213,7 +213,7 @@ static int roundtrip(ck_vnet *n) "payload=\"%s\"\n", CK_NET_ECHO_PORT, CK_NET_LOCAL_PORT, (unsigned)pn, csum ? "verified" : "absent", mine ? "yes" : "NO", shown); - if (!mine) k = CK_NET_IGNORED; /* not the reply to our datagram; keep waiting */ + if (!mine) k = CK_NET_IGNORED; /* not the reply to our datagram: the next try sends again */ } if (k != CK_NET_UDP_REPLY) { ck_printf("net: udp round trip FAIL (no reply after %u tries, frames_seen=%u)\n", UDP_TRIES, frames); @@ -226,6 +226,12 @@ static int roundtrip(ck_vnet *n) int ck_net_bind_selftest(const pci_func *f, const virtio_pci_caps *caps) { ck_vnet *n = &g_net; + /* Never forget a device that may still do DMA: a live earlier bind + * (bus master on or stream confined) must be released first. */ + if (ck_net_live()) { + ck_printf("virtio_net: not bound (previous bind still live; release it first)\n"); + return CK_NET_E_ARG; + } memset(n, 0, sizeof *n); if (!f || !caps) return CK_NET_E_ARG; n->pf = f; diff --git a/scripts/qemu_ck_net_test.sh b/scripts/qemu_ck_net_test.sh index c28c7652..eb7a0df1 100755 --- a/scripts/qemu_ck_net_test.sh +++ b/scripts/qemu_ck_net_test.sh @@ -195,12 +195,12 @@ echo "-- release" check "device reset before the revoke" "^virtio_net: device reset status=0x00 (stopped)$" check "virtio-net bus master revoked" "^dma_gate: virtio_net bus master revoked$" check "virtio-net SMMU stream returned to abort" "^smmu: virtio_net stream 0x[0-9a-f]* returned to abort (rc=0)$" -lg="$(line_of '^dma_gate: virtio_net granted (Confined)')"; lt="$(line_of '^net: udp round trip ok')" +lg="$(line_of '^dma_gate: virtio_net granted (Confined)')"; lt="$(line_of '^net: udp round trip ok')"; ld="$(line_of '^virtio_net: device reset status=0x00 (stopped)$')" lr="$(line_of '^dma_gate: virtio_net bus master revoked$')"; la="$(line_of '^smmu: virtio_net stream .* returned to abort')" -if [[ -n "${lg}" && -n "${lt}" && -n "${lr}" && -n "${la}" && "${lg}" -lt "${lt}" && "${lt}" -lt "${lr}" && "${lr}" -lt "${la}" ]]; then - echo "PASS order: grant < round trip < bus master off < stream abort" +if [[ -n "${lg}" && -n "${lt}" && -n "${ld}" && -n "${lr}" && -n "${la}" && "${lg}" -lt "${lt}" && "${lt}" -lt "${ld}" && "${ld}" -lt "${lr}" && "${lr}" -lt "${la}" ]]; then + echo "PASS order: grant < round trip < device reset < bus master off < stream abort" else - echo "FAIL release order (lines ${lg:-none} ${lt:-none} ${lr:-none} ${la:-none})"; failed=1 + echo "FAIL release order (lines ${lg:-none} ${lt:-none} ${ld:-none} ${lr:-none} ${la:-none})"; failed=1 fi check "devices stage summary" "^devices: pci=ok nvme=bound virtio_net=selftest-ok$" check_absent "no virtio-net failure lines" "^net: .*FAIL\|^virtio_net: .*FAIL\|^virtio_net: not bound" From 6d10030a5322f094c070653744bde4c7d4168c90 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 06:27:09 -0500 Subject: [PATCH 4/4] evidence: ck_gates receipt after review fixes (AIENOS_CK_NET PASS, QEMU only) Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Opus 5.5 --- ...544e5eab6214508861aa1523fca084945d710.json | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 evidence/ck_gates_f669110b4f18e37e8318e83efbe544e5eab6214508861aa1523fca084945d710.json diff --git a/evidence/ck_gates_f669110b4f18e37e8318e83efbe544e5eab6214508861aa1523fca084945d710.json b/evidence/ck_gates_f669110b4f18e37e8318e83efbe544e5eab6214508861aa1523fca084945d710.json new file mode 100644 index 00000000..8a539847 --- /dev/null +++ b/evidence/ck_gates_f669110b4f18e37e8318e83efbe544e5eab6214508861aa1523fca084945d710.json @@ -0,0 +1,41 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-01T11:25:23Z", + "finished_utc": "2026-10-01T11:27:03Z", + "commit": "e750ea41ccc648ae4b7c4a6e98d452687e9b40f0", + "commit_subject": "ck net: review fixes (refuse rebind while live, reset in release order check, comment)", + "tree_clean_before": true, + "tree_clean_after": true, + "quiet_flag": "/home/drakestapleton/workspace/.spark-quiet", + "quiet_flag_present_at_start": false, + "host": {"hostname": "spark-b87b", "arch": "aarch64", "kernel": "7.0.0-1019-nvidia", "cpu_model": "Cortex-A725;Cortex-X925"}, + "qemu": {"binary": "/usr/bin/qemu-system-aarch64", "version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)"}, + "firmware": {"aavmf_code": "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd", "aavmf_code_sha256": "4a4cb7f6d8106bb2a7dd8c763fab14b1810152136fc4304e5b728f0043e84f12", "aavmf_vars": "/usr/share/AAVMF/AAVMF_VARS.fd", "aavmf_vars_sha256": "b3b855c5a80310168051164986855692d1bdb06e67619856177965cd87c6774f"}, + "nvme_dma_mode": "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately", + "children": [ + {"name": "boot", "script": "scripts/qemu_ck_boot_test.sh", "present": true, "script_sha256": "64bcb2d1285485b989dc8309ce0b024ea799d3bce13cff22f62924d3ae4b7cce", "exit": "0", "seconds": 6, "log_sha256": "8e44e91910af8c9d8d38078090b65ce6399695b5f4d3a03aa3dcfaa236a779b8", "log_lines": 22}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "b005ed29619d1113536a0686cedafe84fcea95fa30321307ec1d1335ea231925", "exit": "0", "seconds": 82, "log_sha256": "a9a109c5f596df5840abf3912788f81462d6ea51ba3d6e35f1ce00fb52a72b0d", "log_lines": 533}, + {"name": "net", "script": "scripts/qemu_ck_net_test.sh", "present": true, "script_sha256": "ff5bdf5246950bc9e825c76ecc541f39460b88c181a948225a7a393f25a71b25", "exit": "0", "seconds": 12, "log_sha256": "3e12d56db7cd6d96cf6e43dc8ad28121313a50a3efce267f3e05ebc83bfc98ce", "log_lines": 59} + ], + "gates": [ + {"id": "M1", "marker": "AIENOS_CK_M1", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M3", "marker": "AIENOS_CK_M3", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "SMMU", "marker": "AIENOS_CK_SMMU", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "NVME_SHUTDOWN", "marker": "AIENOS_CK_NVME_SHUTDOWN", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "P2_ARTIFACT", "marker": "AIENOS_CK_P2_ARTIFACT", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no signed artifact loader (EL0, W^X, admission receipts) in the C kernel"}, + {"id": "M0_ROLLBACK", "marker": "AIENOS_CK_M0_ROLLBACK", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md)"}, + {"id": "M4_NVME", "marker": "AIENOS_CK_M4_NVME", "source": "store", "verdict": "PASS", "reason": "SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"}, + {"id": "M4_STORE", "marker": "AIENOS_CK_M4_STORE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_STORE_CRASH", "marker": "AIENOS_CK_M4_STORE_CRASH", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no C Store crash/kill campaign (4 KiB or 512 B) and no slot reuse (C Store is append-only)"}, + {"id": "M4_CONTINUITY", "marker": "AIENOS_CK_M4_CONTINUITY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no continuity core (ADR 0016 agent identity and memory) in the C kernel"}, + {"id": "M4_RECOVERY", "marker": "AIENOS_CK_M4_RECOVERY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no Recovery Core (ADR 0006) in the C kernel"}, + {"id": "ARGUS1_REVOKE", "marker": "AIENOS_CK_ARGUS1_REVOKE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "KEYBOARD", "marker": "AIENOS_CK_KEYBOARD", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel"}, + {"id": "NET", "marker": "AIENOS_CK_NET", "source": "net", "verdict": "PASS", "reason": "QEMU user networking (slirp): attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A; QEMU does not route virtio-net DMA through the SMMU (no ACCESS_PLATFORM), so confinement is not proven for it"} + ], + "counts": {"total": 14, "pass": 8, "fail": 0, "not_run": 6}, + "verdict": "NOT_ALL_GATES_PASS", + "parity": "native/kernel/GATES.md", + "physical": "NOT_RUN", + "physical_statement": "QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." +}