interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / tests / vnet_test.c

617 lines · 30299 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Lane 26 hosted tests: virtio PCI caps against real QEMU 8.2.2 config-space
18 * dumps (tests/fixtures/), malformed caps, and the virtio-net split-virtqueue
19 * driver against a simulated device in C that also plays hostile. Ends with
20 * an M6-A UDP round trip through the TX and RX queues.
21 * Not proven here: a real device, real DMA, interrupts, QEMU slirp. */
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "../aienos_net.h"
27#include "../aienos_virtio_net.h"
28#include "../aienos_virtio_pci.h"
29
30static int failures, checks;
31#define CHECK(c) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0)
32#define EQ(a, b) CHECK((long long)(a) == (long long)(b))
33
34static uint64_t rng = 0x26;
35static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 11); }
36
37/* ---------------- capability parsing ---------------- */
38static int load(const char *path, uint8_t *b, size_t n)
39{
40 FILE *f = fopen(path, "rb");
41 if (!f) return 0;
42 size_t got = fread(b, 1, n, f);
43 int extra = fgetc(f) != EOF;
44 fclose(f);
45 return got == n && !extra;
46}
47static 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)); }
48
49static uint8_t qemu_trans[256], qemu_modern[256];
50
51static void test_qemu_caps(void)
52{
53 virtio_pci_caps r;
54 CHECK(load("tests/fixtures/qemu822_virtio_net_pci_transitional.cfg", qemu_trans, 256));
55 CHECK(load("tests/fixtures/qemu822_virtio_net_pci_modern.cfg", qemu_modern, 256));
56 EQ(qemu_trans[2] | qemu_trans[3] << 8, 0x1000);
57 EQ(qemu_modern[2] | qemu_modern[3] << 8, 0x1041);
58 const uint8_t *imgs[2] = {qemu_trans, qemu_modern};
59 for (int k = 0; k < 2; k++) {
60 const uint8_t *c = imgs[k];
61 /* the dump contains a PCI_CFG cap (type 5) with bar 0, offset 0, length 0 at 0x84 */
62 EQ(c[0x84], 9); EQ(c[0x87], 5); EQ(c[0x86], 20); EQ(c[0x90] | c[0x91] | c[0x92] | c[0x93], 0);
63 EQ(virtio_pci_parse_caps(c, 256, &r), VIRTIO_PCI_OK);
64 CHECK(r.common_cfg.present && r.common_cfg.bar == 4 && r.common_cfg.offset == 0 && r.common_cfg.length == 0x1000);
65 CHECK(r.isr_cfg.present && r.isr_cfg.bar == 4 && r.isr_cfg.offset == 0x1000);
66 CHECK(r.device_cfg.present && r.device_cfg.bar == 4 && r.device_cfg.offset == 0x2000);
67 CHECK(r.notify_cfg.present && r.notify_cfg.bar == 4 && r.notify_cfg.offset == 0x3000 && r.notify_cfg.length == 0x1000);
68 CHECK(r.has_notify_off_multiplier && r.notify_off_multiplier == 4);
69 uint8_t big[4096] = {0};
70 memcpy(big, c, 256);
71 EQ(virtio_pci_parse_caps(big, 4096, &r), VIRTIO_PCI_OK);
72 }
73 /* malformed variants of the real image stay refused */
74 uint8_t x[256];
75 memcpy(x, qemu_modern, 256); put32(x, 0x4c, 0); /* zero-length COMMON_CFG */
76 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
77 memcpy(x, qemu_modern, 256); put32(x, 0x7c, 0); /* zero-length NOTIFY_CFG */
78 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
79 memcpy(x, qemu_modern, 256); put32(x, 0x5c, 0); /* zero-length ISR_CFG */
80 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
81 memcpy(x, qemu_modern, 256); put32(x, 0x6c, 0); /* zero-length DEVICE_CFG */
82 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
83 memcpy(x, qemu_modern, 256); x[0x44] = 6; /* COMMON_CFG bad BAR */
84 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
85 memcpy(x, qemu_modern, 256); put32(x, 0x78, 0xfffff800); /* NOTIFY offset+length overflows */
86 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_REGION);
87 memcpy(x, qemu_modern, 256); x[0x86] = 12; /* PCI_CFG cap shorter than 16 */
88 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY);
89 memcpy(x, qemu_modern, 256); x[0x34] = 0xfc; x[0xfc] = 9; x[0xfd] = 0; /* cap past config space */
90 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_SHORT_CAPABILITY);
91 memcpy(x, qemu_modern, 256); x[0x41] = 0x98; /* loop back to MSI-X */
92 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP);
93 memcpy(x, qemu_modern, 256); x[0x85] = 0x84; /* PCI_CFG self loop */
94 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_CAPABILITY_LOOP);
95 memcpy(x, qemu_modern, 256); x[0x63] = 1; /* DEVICE_CFG retyped as a 2nd COMMON */
96 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_DUPLICATE_CAPABILITY);
97 memcpy(x, qemu_modern, 256); x[0x99] = 0x30; /* pointer into the header */
98 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_INVALID_POINTER);
99 /* PCI_CFG (and any other unmapped type) is ignored even with a junk region */
100 memcpy(x, qemu_modern, 256); x[0x88] = 7; put32(x, 0x8c, 0xffffffff); put32(x, 0x90, 5);
101 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK);
102 memcpy(x, qemu_modern, 256); x[0x87] = 9; /* VENDOR_CFG-type, length 0 */
103 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK);
104 /* required caps missing: the parser reports them absent, vnet_init refuses */
105 memcpy(x, qemu_modern, 256); x[0x73] = 0; /* NOTIFY retyped to 0 */
106 EQ(virtio_pci_parse_caps(x, 256, &r), VIRTIO_PCI_OK);
107 CHECK(!r.notify_cfg.present); /* vnet_init then refuses: test_init, VNET_E_CAPS */
108}
109
110/* ---------------- simulated device ---------------- */
111typedef struct {
112 uint16_t max, size, enable, noff;
113 uint64_t desc, avail, used;
114 uint16_t last_avail, used_idx;
115} simq;
116
117typedef struct {
118 uint8_t *mem; uint64_t dma; size_t mem_len;
119 uint64_t dev_features, drv_features;
120 uint32_t dfsel, gfsel;
121 uint8_t status, cfggen;
122 uint16_t numq, qsel;
123 simq q[2];
124 uint8_t mac[6];
125 uint32_t mult;
126 /* knobs */
127 int refuse_features, reset_stuck, cfggen_unstable, preenabled;
128 uint32_t noff_override;
129 /* observations */
130 unsigned notifies[2], faults, status_writes;
131 uint8_t wire[8][2048]; size_t wire_len[8]; unsigned nwire;
132} sim;
133
134static uint8_t *bus(sim *s, uint64_t a, size_t n)
135{
136 if (a < s->dma || a - s->dma > s->mem_len || n > s->mem_len - (a - s->dma)) { s->faults++; return NULL; }
137 return s->mem + (a - s->dma);
138}
139static uint16_t g16(const uint8_t *p) { uint16_t v; memcpy(&v, p, 2); return v; }
140static uint32_t g32(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v; }
141static uint64_t g64(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v; }
142static void s16(uint8_t *p, uint16_t v) { memcpy(p, &v, 2); }
143static void s32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); }
144
145static uint32_t sim_read(void *ctx, vnet_window w, uint32_t off, unsigned width)
146{
147 sim *s = ctx;
148 if (w == VNET_WIN_DEVICE) return off < 6 && width == 1 ? s->mac[off] : 0;
149 if (w != VNET_WIN_COMMON) return 0;
150 simq *q = &s->q[s->qsel < 2 ? s->qsel : 0];
151 switch (off) {
152 case VNET_CC_DF: return (uint32_t)(s->dev_features >> (s->dfsel ? 32 : 0));
153 case VNET_CC_NUMQ: return s->numq;
154 case VNET_CC_STATUS: return s->reset_stuck ? 1 : s->status;
155 case VNET_CC_CFGGEN: return s->cfggen_unstable ? s->cfggen++ : s->cfggen;
156 case VNET_CC_Q_SIZE: return s->qsel < 2 ? q->size : 0;
157 case VNET_CC_Q_ENABLE: return s->qsel < 2 ? q->enable : 0;
158 case VNET_CC_Q_NOFF: return s->noff_override ? s->noff_override : q->noff;
159 }
160 return 0;
161}
162static void sim_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v)
163{
164 sim *s = ctx;
165 (void)width;
166 if (w != VNET_WIN_COMMON) { s->faults++; return; }
167 simq *q = &s->q[s->qsel < 2 ? s->qsel : 0];
168 switch (off) {
169 case VNET_CC_DFSELECT: s->dfsel = v; break;
170 case VNET_CC_GFSELECT: s->gfsel = v; break;
171 case VNET_CC_GF:
172 if (s->gfsel) s->drv_features = (s->drv_features & 0xffffffffull) | (uint64_t)v << 32;
173 else s->drv_features = (s->drv_features & ~0xffffffffull) | v;
174 break;
175 case VNET_CC_STATUS:
176 s->status_writes++;
177 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; }
178 if ((v & VNET_S_FEATURES_OK) && !(s->status & VNET_S_FEATURES_OK)) {
179 if (s->refuse_features || (s->drv_features & ~s->dev_features)) v &= ~VNET_S_FEATURES_OK;
180 }
181 s->status = (uint8_t)v;
182 break;
183 case VNET_CC_Q_SELECT: s->qsel = (uint16_t)v; break;
184 case VNET_CC_Q_SIZE: if (v > q->max) s->faults++; q->size = (uint16_t)v; break;
185 case VNET_CC_Q_ENABLE: q->enable = (uint16_t)v; break;
186 case VNET_CC_Q_DESCLO: q->desc = (q->desc & ~0xffffffffull) | v; break;
187 case VNET_CC_Q_DESCHI: q->desc = (q->desc & 0xffffffffull) | (uint64_t)v << 32; break;
188 case VNET_CC_Q_AVAILLO: q->avail = (q->avail & ~0xffffffffull) | v; break;
189 case VNET_CC_Q_AVAILHI: q->avail = (q->avail & 0xffffffffull) | (uint64_t)v << 32; break;
190 case VNET_CC_Q_USEDLO: q->used = (q->used & ~0xffffffffull) | v; break;
191 case VNET_CC_Q_USEDHI: q->used = (q->used & 0xffffffffull) | (uint64_t)v << 32; break;
192 default: break;
193 }
194}
195static void sim_notify(void *ctx, uint32_t off, uint16_t queue)
196{
197 sim *s = ctx;
198 if (!(s->status & VNET_S_DRIVER_OK) || queue > 1 || off != (uint32_t)s->q[queue].noff * s->mult) s->faults++;
199 else s->notifies[queue]++;
200}
201static const vnet_ops sim_ops = {0, sim_read, sim_write, sim_notify};
202
203static void sim_reset(sim *s, uint8_t *mem, uint64_t dma, size_t len)
204{
205 memset(s, 0, sizeof *s);
206 s->mem = mem; s->dma = dma; s->mem_len = len;
207 s->dev_features = (1ull << VNET_F_VERSION_1) | (1ull << VNET_F_MAC);
208 s->numq = 3; s->mult = 4;
209 s->q[0].max = s->q[1].max = 256;
210 s->q[0].noff = 0; s->q[1].noff = 1;
211 for (int i = 0; i < 6; i++) s->mac[i] = (uint8_t)(0x52 + i);
212}
213
214static void push_used(sim *s, int qi, uint32_t id, uint32_t len)
215{
216 simq *q = &s->q[qi];
217 uint8_t *u = bus(s, q->used, 6 + 8u * q->size);
218 if (!u) return;
219 uint8_t *e = u + 4 + 8u * (q->used_idx % q->size);
220 s32(e, id); s32(e + 4, len);
221 q->used_idx++;
222 __atomic_thread_fence(__ATOMIC_RELEASE);
223 s16(u + 2, q->used_idx);
224}
225
226/* Read the next available descriptor of queue qi; checks every field. */
227static int pop_avail(sim *s, int qi, uint16_t *id, uint8_t **buf, uint32_t *len, uint16_t *flags)
228{
229 simq *q = &s->q[qi];
230 uint8_t *a = bus(s, q->avail, 6 + 2u * q->size);
231 if (!a || !q->enable) return 0;
232 if (g16(a + 2) == q->last_avail) return 0;
233 if ((uint16_t)(g16(a + 2) - q->last_avail) > q->size) { s->faults++; return 0; }
234 *id = g16(a + 4 + 2u * (q->last_avail % q->size));
235 q->last_avail++;
236 if (*id >= q->size) { s->faults++; return 0; }
237 uint8_t *d = bus(s, q->desc + 16u * *id, 16);
238 if (!d) return 0;
239 *len = g32(d + 8); *flags = g16(d + 12);
240 if (*flags & 1u) s->faults++; /* NEXT: the driver never chains */
241 *buf = bus(s, g64(d), *len);
242 return *buf != NULL;
243}
244
245/* Device transmits everything pending on TX (completes with len 0). */
246static unsigned sim_tx(sim *s)
247{
248 unsigned n = 0; uint16_t id, fl; uint8_t *b; uint32_t len;
249 while (pop_avail(s, 1, &id, &b, &len, &fl)) {
250 if ((fl & VNET_DESC_F_WRITE) || len < VNET_HDR_LEN || len > VNET_BUF_LEN) s->faults++;
251 for (unsigned i = 0; i < VNET_HDR_LEN; i++) if (b[i]) s->faults++;
252 unsigned slot = s->nwire++ % 8;
253 s->wire_len[slot] = len - VNET_HDR_LEN;
254 memcpy(s->wire[slot], b + VNET_HDR_LEN, len - VNET_HDR_LEN);
255 push_used(s, 1, id, 0);
256 n++;
257 }
258 return n;
259}
260/* Device receives one frame into the next RX buffer. */
261static int sim_rx(sim *s, const uint8_t *f, size_t n)
262{
263 uint16_t id, fl; uint8_t *b; uint32_t len;
264 if (!pop_avail(s, 0, &id, &b, &len, &fl)) return 0;
265 if (!(fl & VNET_DESC_F_WRITE) || len < VNET_HDR_LEN + n) { s->faults++; return 0; }
266 memset(b, 0, VNET_HDR_LEN); s16(b + 10, 1);
267 memcpy(b + VNET_HDR_LEN, f, n);
268 push_used(s, 0, id, (uint32_t)(VNET_HDR_LEN + n));
269 return 1;
270}
271
272/* every descriptor in both tables lies inside the shared region */
273static void check_descs(const vnet_dev *d)
274{
275 for (int qi = 0; qi < 2; qi++) {
276 const vnet_queue *q = &d->q[qi];
277 for (unsigned i = 0; i < q->size; i++) {
278 const uint8_t *e = d->mem + q->desc_off + 16u * i;
279 uint64_t a = g64(e); uint32_t l = g32(e + 8);
280 if (l == 0) continue;
281 CHECK(a >= d->dma && a - d->dma <= d->mem_len && l <= d->mem_len - (a - d->dma));
282 CHECK(l <= VNET_BUF_LEN);
283 }
284 }
285}
286
287static uint8_t region[1 << 20] __attribute__((aligned(4096)));
288#define DMA 0x80000000ull
289
290static int bring_up(vnet_dev *d, sim *s, uint16_t qsize)
291{
292 vnet_ops o = sim_ops; o.ctx = s;
293 virtio_pci_caps c;
294 if (virtio_pci_parse_caps(qemu_modern, 256, &c) != VIRTIO_PCI_OK) return -1;
295 return vnet_init(d, &o, &c, region, DMA, sizeof region, qsize);
296}
297
298static void test_init(void)
299{
300 sim s; vnet_dev d;
301 EQ(vnet_mem_size(3), 0); EQ(vnet_mem_size(1), 0); EQ(vnet_mem_size(512), 0);
302 CHECK(vnet_mem_size(256) <= sizeof region);
303
304 sim_reset(&s, region, DMA, sizeof region);
305 s.dev_features = ~0ull; /* offers everything, incl. packed, indirect, ACCESS_PLATFORM */
306 EQ(bring_up(&d, &s, 256), VNET_OK);
307 /* ACCESS_PLATFORM is accepted when offered (virtio 1.x 6.1 SHOULD) */
308 EQ(s.drv_features, (1ull << VNET_F_VERSION_1) | (1ull << VNET_F_MAC) | (1ull << VNET_F_ACCESS_PLATFORM));
309 EQ(d.features, s.drv_features);
310 EQ(s.status, VNET_S_ACK | VNET_S_DRIVER | VNET_S_FEATURES_OK | VNET_S_DRIVER_OK);
311 CHECK(d.has_mac && d.mac[0] == 0x52 && d.mac[5] == 0x57);
312 EQ(d.q[0].size, 256); EQ(d.q[1].size, 256);
313 EQ(s.q[0].enable, 1); EQ(s.q[1].enable, 1);
314 EQ(s.notifies[0], 1); EQ(s.faults, 0);
315 check_descs(&d);
316
317 /* without MAC */
318 sim_reset(&s, region, DMA, sizeof region);
319 s.dev_features = 1ull << VNET_F_VERSION_1;
320 EQ(bring_up(&d, &s, 8), VNET_OK);
321 CHECK(!d.has_mac); EQ(s.drv_features, 1ull << VNET_F_VERSION_1);
322
323 /* the device offers less: largest power of two at or below */
324 sim_reset(&s, region, DMA, sizeof region);
325 s.q[0].max = 100; s.q[1].max = 64;
326 EQ(bring_up(&d, &s, 256), VNET_OK);
327 EQ(d.q[0].size, 64); EQ(d.q[1].size, 64); EQ(s.faults, 0);
328
329 struct { const char *what; int knob; vnet_err want; } cases[] = {
330 {"no VERSION_1 (legacy-only device)", 1, VNET_E_NO_VERSION_1},
331 {"FEATURES_OK refused", 2, VNET_E_FEATURES},
332 {"reset never completes", 3, VNET_E_RESET_TIMEOUT},
333 {"one queue only", 4, VNET_E_QUEUES},
334 {"queue size 0", 5, VNET_E_QUEUES},
335 {"queue size 1", 6, VNET_E_QUEUES},
336 {"queue already enabled", 7, VNET_E_QUEUES},
337 {"notify offset outside window", 8, VNET_E_NOTIFY},
338 {"config generation never settles", 9, VNET_E_CONFIG},
339 };
340 for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
341 sim_reset(&s, region, DMA, sizeof region);
342 switch (cases[i].knob) {
343 case 1: s.dev_features = 1ull << VNET_F_MAC; break;
344 case 2: s.refuse_features = 1; break;
345 case 3: s.reset_stuck = 1; break;
346 case 4: s.numq = 1; break;
347 case 5: s.q[1].max = 0; break;
348 case 6: s.q[0].max = 1; break;
349 case 7: s.preenabled = 1; break;
350 case 8: s.noff_override = 0x400; break; /* 0x400 * 4 + 2 > 0x1000 */
351 case 9: s.cfggen_unstable = 1; break;
352 }
353 vnet_err e = bring_up(&d, &s, 16);
354 if (e != cases[i].want) fprintf(stderr, "init case '%s': got %s\n", cases[i].what, vnet_err_name(e));
355 EQ(e, cases[i].want);
356 CHECK(d.broken);
357 if (cases[i].knob != 3) CHECK(s.status & VNET_S_FAILED);
358 CHECK(!(s.status & VNET_S_DRIVER_OK));
359 EQ(s.notifies[0] + s.notifies[1], 0);
360 uint8_t f[64] = {0};
361 EQ(vnet_tx(&d, f, 60), VNET_E_BROKEN);
362 }
363
364 /* argument and memory refusals */
365 vnet_ops o = sim_ops; o.ctx = &s;
366 virtio_pci_caps c, bad;
367 EQ(virtio_pci_parse_caps(qemu_modern, 256, &c), VIRTIO_PCI_OK);
368 sim_reset(&s, region, DMA, sizeof region);
369 EQ(vnet_init(&d, &o, &c, region, DMA, sizeof region, 6), VNET_E_ARG);
370 EQ(vnet_init(&d, &o, &c, region, DMA, vnet_mem_size(16) - 1, 16), VNET_E_MEM);
371 EQ(vnet_init(&d, &o, &c, region + 8, DMA, sizeof region - 8, 16), VNET_E_MEM);
372 EQ(vnet_init(&d, &o, &c, region, DMA + 4, sizeof region, 16), VNET_E_MEM);
373 EQ(vnet_init(&d, &o, &c, region, UINT64_MAX - 0xfff, sizeof region, 16), VNET_E_MEM);
374 bad = c; bad.notify_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
375 bad = c; bad.has_notify_off_multiplier = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
376 bad = c; bad.common_cfg.length = 0x37; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
377 bad = c; bad.isr_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
378 bad = c; bad.device_cfg.present = 0; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
379 bad = c; bad.device_cfg.length = 4; EQ(vnet_init(&d, &o, &bad, region, DMA, sizeof region, 16), VNET_E_CAPS);
380 EQ(s.notifies[0] + s.notifies[1], 0);
381 EQ(vnet_init_flags(&d, &o, &c, region, DMA, sizeof region, 16, 2), VNET_E_ARG); /* unknown flag */
382 EQ(s.notifies[0] + s.notifies[1], 0);
383}
384
385/* VIRTIO_F_ACCESS_PLATFORM (bit 33): accepted when offered; with
386 * VNET_INIT_REQUIRE_ACCESS_PLATFORM a device that does not offer it is refused
387 * before FEATURES_OK (fail closed: it would DMA around the IOMMU). */
388static void test_access_platform(void)
389{
390 sim s; vnet_dev d;
391 vnet_ops o = sim_ops; o.ctx = &s;
392 virtio_pci_caps c;
393 EQ(virtio_pci_parse_caps(qemu_modern, 256, &c), VIRTIO_PCI_OK);
394 const uint64_t ap = 1ull << VNET_F_ACCESS_PLATFORM, v1 = 1ull << VNET_F_VERSION_1, mac = 1ull << VNET_F_MAC;
395
396 /* not offered, not required: works as before, bit not written */
397 sim_reset(&s, region, DMA, sizeof region);
398 EQ(vnet_init_flags(&d, &o, &c, region, DMA, sizeof region, 16, 0), VNET_OK);
399 EQ(s.drv_features, v1 | mac); CHECK(!(d.features & ap));
400
401 /* offered, not required: accepted */
402 sim_reset(&s, region, DMA, sizeof region);
403 s.dev_features = v1 | mac | ap;
404 EQ(vnet_init(&d, &o, &c, region, DMA, sizeof region, 16), VNET_OK);
405 EQ(s.drv_features, v1 | mac | ap); CHECK(d.features & ap);
406
407 /* offered and required: accepted, device runs */
408 sim_reset(&s, region, DMA, sizeof region);
409 s.dev_features = v1 | mac | ap;
410 EQ(vnet_init_flags(&d, &o, &c, region, DMA, sizeof region, 16, VNET_INIT_REQUIRE_ACCESS_PLATFORM), VNET_OK);
411 EQ(s.drv_features, v1 | mac | ap); EQ(d.features, v1 | mac | ap); CHECK(!d.broken);
412 EQ(s.status, VNET_S_ACK | VNET_S_DRIVER | VNET_S_FEATURES_OK | VNET_S_DRIVER_OK);
413 uint8_t f[60] = {0};
414 EQ(vnet_tx(&d, f, sizeof f), VNET_OK);
415 EQ(sim_tx(&s), 1);
416
417 /* required but not offered (QEMU without iommu_platform=on): refused before
418 * FEATURES_OK, FAILED set, no feature bits written, no queue, no notify */
419 const uint64_t offers[] = {v1 | mac, v1, ~0ull & ~ap};
420 for (size_t i = 0; i < sizeof offers / sizeof offers[0]; i++) {
421 sim_reset(&s, region, DMA, sizeof region);
422 s.dev_features = offers[i];
423 EQ(vnet_init_flags(&d, &o, &c, region, DMA, sizeof region, 16, VNET_INIT_REQUIRE_ACCESS_PLATFORM),
424 VNET_E_NO_ACCESS_PLATFORM);
425 CHECK(d.broken);
426 CHECK(s.status & VNET_S_FAILED);
427 CHECK(!(s.status & (VNET_S_FEATURES_OK | VNET_S_DRIVER_OK)));
428 EQ(s.drv_features, 0);
429 EQ(s.q[0].enable + s.q[1].enable, 0);
430 EQ(s.notifies[0] + s.notifies[1], 0);
431 EQ(vnet_tx(&d, f, sizeof f), VNET_E_BROKEN);
432 }
433 /* a legacy-only device is still refused as NoVersion1 first */
434 sim_reset(&s, region, DMA, sizeof region);
435 s.dev_features = mac | ap;
436 EQ(vnet_init_flags(&d, &o, &c, region, DMA, sizeof region, 16, VNET_INIT_REQUIRE_ACCESS_PLATFORM),
437 VNET_E_NO_VERSION_1);
438 CHECK(strcmp(vnet_err_name(VNET_E_NO_ACCESS_PLATFORM), "NoAccessPlatform") == 0);
439}
440
441static void test_datapath(void)
442{
443 sim s; vnet_dev d;
444 sim_reset(&s, region, DMA, sizeof region);
445 EQ(bring_up(&d, &s, 4), VNET_OK);
446 uint8_t f[VNET_FRAME_MAX], out[VNET_FRAME_MAX];
447 size_t n; int got; unsigned rc;
448 for (size_t i = 0; i < sizeof f; i++) f[i] = (uint8_t)(i * 7 + 1);
449 EQ(vnet_tx(&d, f, 13), VNET_E_ARG);
450 EQ(vnet_tx(&d, f, VNET_FRAME_MAX + 1), VNET_E_ARG);
451 EQ(vnet_rx(&d, out, VNET_FRAME_MAX - 1, &n, &got), VNET_E_ARG);
452 EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_OK); EQ(got, 0);
453 /* fill TX, then full, then reclaim */
454 for (int i = 0; i < 4; i++) EQ(vnet_tx(&d, f, 60 + (size_t)i), VNET_OK);
455 EQ(vnet_tx(&d, f, 60), VNET_E_FULL);
456 EQ(s.notifies[1], 4);
457 EQ(sim_tx(&s), 4);
458 EQ(s.wire_len[3], 63); CHECK(memcmp(s.wire[3], f, 63) == 0);
459 EQ(vnet_tx_reclaim(&d, &rc), VNET_OK); EQ(rc, 4);
460 EQ(vnet_tx(&d, f, VNET_FRAME_MAX), VNET_OK);
461 EQ(sim_tx(&s), 1);
462 EQ(s.wire_len[4], VNET_FRAME_MAX); CHECK(memcmp(s.wire[4], f, VNET_FRAME_MAX) == 0);
463 /* 70,000 frames each way: 16-bit ring indices wrap */
464 unsigned bad = 0;
465 for (unsigned i = 0; i < 70000; i++) {
466 size_t len = 14 + (i * 131u) % (VNET_FRAME_MAX - 13);
467 f[0] = (uint8_t)i; f[len - 1] = (uint8_t)(i >> 8);
468 if (!sim_rx(&s, f, len)) bad++;
469 if (vnet_rx(&d, out, sizeof out, &n, &got) != VNET_OK || !got || n != len || memcmp(out, f, len)) bad++;
470 if (vnet_tx_reclaim(&d, NULL) != VNET_OK || vnet_tx(&d, f, len) != VNET_OK || sim_tx(&s) != 1) bad++;
471 if (s.wire_len[(s.nwire - 1) % 8] != len || memcmp(s.wire[(s.nwire - 1) % 8], f, len)) bad++;
472 }
473 EQ(bad, 0);
474 /* 16-bit used index: 70000 completions wrap once, residue 70000 mod 2^16. */
475 EQ(s.q[0].used_idx, (uint16_t)70000u); /* wrapped */
476 EQ(s.faults, 0);
477 check_descs(&d);
478}
479
480/* hostile completions: each must be refused and leave the device broken */
481static void test_hostile(void)
482{
483 sim s; vnet_dev d;
484 uint8_t f[128] = {0}, out[VNET_FRAME_MAX];
485 size_t n; int got;
486 for (int k = 0; k < 9; k++) {
487 sim_reset(&s, region, DMA, sizeof region);
488 EQ(bring_up(&d, &s, 8), VNET_OK);
489 vnet_err e = VNET_OK;
490 switch (k) {
491 case 0: push_used(&s, 0, 8, 60); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* id == size */
492 case 1: push_used(&s, 0, 0xffffffffu, 60); e = vnet_rx(&d, out, sizeof out, &n, &got); break;
493 case 2: push_used(&s, 0, 3, VNET_BUF_LEN + 1); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* oversized */
494 case 3: push_used(&s, 0, 3, VNET_HDR_LEN - 1); e = vnet_rx(&d, out, sizeof out, &n, &got); break; /* short */
495 case 4: { /* used idx runs ahead of everything posted */
496 uint8_t *u = region + d.q[0].used_off; s16(u + 2, 9);
497 e = vnet_rx(&d, out, sizeof out, &n, &got); break; }
498 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 */
499 case 6: vnet_tx(&d, f, 60); vnet_tx(&d, f, 60); push_used(&s, 1, 0, 0); push_used(&s, 1, 0, 0);
500 e = vnet_tx_reclaim(&d, NULL); break; /* double completion */
501 case 7: push_used(&s, 1, 0, 0); e = vnet_tx_reclaim(&d, NULL); break; /* completion with nothing posted */
502 case 8: vnet_tx(&d, f, 60); push_used(&s, 1, 0, 0x10000); e = vnet_tx_reclaim(&d, NULL); break; /* TX len oversized */
503 }
504 if (e != VNET_E_DEVICE) fprintf(stderr, "hostile case %d: got %s\n", k, vnet_err_name(e));
505 EQ(e, VNET_E_DEVICE);
506 EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_E_BROKEN);
507 EQ(vnet_tx(&d, f, 60), VNET_E_BROKEN);
508 EQ(vnet_tx_reclaim(&d, NULL), VNET_E_BROKEN);
509 }
510 /* a hostile device rewriting the descriptor table cannot steer the driver:
511 re-posted descriptors are rebuilt from the id */
512 sim_reset(&s, region, DMA, sizeof region);
513 EQ(bring_up(&d, &s, 8), VNET_OK);
514 memset(region + d.q[0].desc_off, 0xa5, 16u * 8);
515 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); }
516 check_descs(&d);
517
518 /* seeded random hostile used-ring traffic: never crashes, never delivers
519 more than a frame, never writes a descriptor outside the region */
520 unsigned delivered = 0, refused = 0;
521 for (unsigned round = 0; round < 2000; round++) {
522 sim_reset(&s, region, DMA, sizeof region);
523 if (bring_up(&d, &s, (uint16_t)(2u << (rnd() % 7))) != VNET_OK) { CHECK(0); continue; }
524 for (unsigned step = 0; step < 40 && !d.broken; step++) {
525 uint32_t r = rnd();
526 switch (r % 6) {
527 case 0: sim_rx(&s, f, 14 + rnd() % 100); break;
528 case 1: vnet_tx(&d, f, 14 + rnd() % 100); break;
529 case 2: sim_tx(&s); break;
530 case 3: push_used(&s, (int)(rnd() % 2), rnd() % (d.q[0].size + 2), rnd() % 2 ? rnd() % (VNET_BUF_LEN + 4) : 60); break;
531 case 4: { uint8_t *u = region + d.q[rnd() % 2].used_off; s16(u + 2, (uint16_t)(g16(u + 2) + rnd() % 3)); break; }
532 default: break;
533 }
534 vnet_err e = vnet_rx(&d, out, sizeof out, &n, &got);
535 if (e == VNET_OK && got) { delivered++; CHECK(n <= VNET_FRAME_MAX); }
536 if (e == VNET_OK) e = vnet_tx_reclaim(&d, NULL);
537 if (e != VNET_OK) { CHECK(e == VNET_E_DEVICE && d.broken); refused++; }
538 check_descs(&d);
539 CHECK(d.q[0].in_flight <= d.q[0].size && d.q[1].in_flight <= d.q[1].size);
540 }
541 }
542 CHECK(delivered > 1000 && refused > 500);
543 printf("vnet_test: hostile fuzz delivered=%u refused=%u\n", delivered, refused);
544}
545
546/* M6-A UDP frame out through TX, reply in through RX, parsed by M6-A */
547static void test_m6a_udp(void)
548{
549 sim s; vnet_dev d;
550 sim_reset(&s, region, DMA, sizeof region);
551 EQ(bring_up(&d, &s, 16), VNET_OK);
552 net_mac me, peer = {{0x02, 0, 0, 0, 0, 2}};
553 memcpy(me.b, d.mac, 6);
554 net_ipv4 ip_me = {{10, 0, 2, 15}}, ip_peer = {{10, 0, 2, 2}};
555 const char *msg = "aienos lane26 ping";
556 uint8_t udp[256], ip[512], frame[VNET_FRAME_MAX];
557 size_t ul, il, fl;
558 net_udp_header uh = {40000, 7};
559 EQ(net_udp_build(&uh, (const uint8_t *)msg, strlen(msg), ip_me, ip_peer, udp, sizeof udp, &ul), NET_OK);
560 net_ipv4_header ih = {0, 1, 64, NET_IPPROTO_UDP, ip_me, ip_peer};
561 EQ(net_ipv4_build(&ih, udp, ul, ip, sizeof ip, &il), NET_OK);
562 net_eth_frame ef = {peer, me, NET_ETHERTYPE_IPV4, ip, il};
563 EQ(net_eth_build(&ef, frame, sizeof frame, &fl), NET_OK);
564 EQ(vnet_tx(&d, frame, fl), VNET_OK);
565 EQ(sim_tx(&s), 1);
566 EQ(s.wire_len[0], fl); CHECK(memcmp(s.wire[0], frame, fl) == 0);
567
568 /* the device side parses what went out with M6-A and answers */
569 net_eth_frame pe; net_ipv4_header pih; net_udp_header puh;
570 const uint8_t *pl, *data; size_t pll, dl;
571 EQ(net_eth_parse(s.wire[0], s.wire_len[0], &pe), NET_OK);
572 EQ(net_ipv4_parse(pe.payload, pe.payload_len, &pih, &pl, &pll), NET_OK);
573 EQ(net_udp_parse(pl, pll, pih.source, pih.destination, &puh, &data, &dl), NET_OK);
574 CHECK(dl == strlen(msg) && memcmp(data, msg, dl) == 0);
575 const char *rep = "pong from simulated device";
576 net_udp_header rh = {puh.destination_port, puh.source_port};
577 EQ(net_udp_build(&rh, (const uint8_t *)rep, strlen(rep), ip_peer, ip_me, udp, sizeof udp, &ul), NET_OK);
578 net_ipv4_header rih = {0, 9, 64, NET_IPPROTO_UDP, ip_peer, ip_me};
579 EQ(net_ipv4_build(&rih, udp, ul, ip, sizeof ip, &il), NET_OK);
580 net_eth_frame ref = {me, peer, NET_ETHERTYPE_IPV4, ip, il};
581 EQ(net_eth_build(&ref, frame, sizeof frame, &fl), NET_OK);
582 EQ(sim_rx(&s, frame, fl), 1);
583
584 uint8_t out[VNET_FRAME_MAX]; size_t n; int got;
585 EQ(vnet_rx(&d, out, sizeof out, &n, &got), VNET_OK); EQ(got, 1); EQ(n, fl);
586 EQ(net_eth_parse(out, n, &pe), NET_OK);
587 CHECK(memcmp(pe.destination.b, d.mac, 6) == 0);
588 EQ(net_ipv4_parse(pe.payload, pe.payload_len, &pih, &pl, &pll), NET_OK);
589 CHECK(memcmp(pih.destination.b, ip_me.b, 4) == 0);
590 EQ(net_udp_parse(pl, pll, pih.source, pih.destination, &puh, &data, &dl), NET_OK);
591 EQ(puh.destination_port, 40000); EQ(puh.source_port, 7);
592 CHECK(dl == strlen(rep) && memcmp(data, rep, dl) == 0);
593 EQ(vnet_tx_reclaim(&d, NULL), VNET_OK);
594 EQ(s.faults, 0);
595 int ok = failures == 0;
596 printf("VNET_M6A_UDP_ROUNDTRIP_SIM: %s\n", ok ? "PASS" : "FAIL");
597}
598
599int main(void)
600{
601 test_qemu_caps();
602 int caps_ok = failures == 0;
603 printf("VIRTIO_PCI_QEMU822_CAPS: %s (real QEMU 8.2.2 virtio-net-pci config dumps, 0x1000 + 0x1041)\n",
604 caps_ok ? "PASS" : "FAIL");
605 test_init();
606 int ap_before = failures;
607 test_access_platform();
608 printf("VNET_ACCESS_PLATFORM_SIM: %s (bit 33 accepted when offered; required + not offered refused before FEATURES_OK)\n",
609 failures == ap_before ? "PASS" : "FAIL");
610 test_datapath();
611 test_hostile();
612 test_m6a_udp();
613 printf("vnet_test: %d checks, %d failures\n", checks, failures);
614 printf("VNET_SIM_DATAPATH: %s (simulated device only; no real device, DMA, IRQ or QEMU round trip)\n",
615 failures ? "FAIL" : "PASS");
616 return failures ? 1 : 0;
617}