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 / aienos_virtio_net.h

180 lines · 8251 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/* virtio-net (virtio 1.x, split virtqueues) driver core, polled, hosted-testable.
18 *
19 * The Rust reference crates/aienos-kernel/src/virtio_net.rs only parses PCI
20 * capabilities; it has no queue code. This data path is written from the
21 * virtio 1.x specification (sections 2.6 split virtqueues, 4.1.4 PCI
22 * capabilities, 5.1 network device) and is new C, not a port.
23 *
24 * Device access goes through vnet_ops only: register reads/writes inside the
25 * four mapped capability windows and the queue notify. No heap, no clock, no
26 * I/O of its own. All rings and packet buffers live in ONE caller-provided
27 * region (mem, len) that the device sees at bus address `dma`. The driver
28 * builds every descriptor itself from the descriptor id (never reads a
29 * descriptor back from shared memory) and checks it lies inside the region.
30 * Anything a device writes is untrusted: a used-ring entry with an id out of
31 * range, an id not outstanding, a length larger than the buffer, a used index
32 * that runs ahead of what was posted, or a short RX completion marks the
33 * device broken (sticky); the caller must reset and re-init.
34 *
35 * Features: VIRTIO_F_VERSION_1 is required; VIRTIO_NET_F_MAC and
36 * VIRTIO_F_ACCESS_PLATFORM are accepted when offered (virtio 1.x 6.1: a driver
37 * SHOULD accept ACCESS_PLATFORM; with it the device DMAs through the
38 * platform IOMMU, so `dma` below is an IOMMU bus address). With
39 * VNET_INIT_REQUIRE_ACCESS_PLATFORM the driver refuses (fail closed, before
40 * FEATURES_OK) a device that does not offer it: an IOMMU-confined caller
41 * must never run a device that would bypass the IOMMU. Every other feature
42 * is declined (no packed ring, no indirect descriptors, no event index, no
43 * offloads, no mergeable RX buffers, no control queue). One descriptor per packet; the 12-byte
44 * virtio 1.x net header precedes each frame. Interrupts are not used. */
45#ifndef AIENOS_VIRTIO_NET_H
46#define AIENOS_VIRTIO_NET_H
47
48#include <stddef.h>
49#include <stdint.h>
50
51#include "aienos_virtio_pci.h"
52
53#define VNET_QUEUE_MAX 256u /* largest queue size the driver uses */
54#define VNET_QUEUE_MIN 2u
55#define VNET_HDR_LEN 12u /* struct virtio_net_hdr incl. num_buffers */
56#define VNET_FRAME_MAX 1514u /* Ethernet II without FCS, no VLAN tag */
57#define VNET_BUF_LEN (VNET_HDR_LEN + VNET_FRAME_MAX)
58#define VNET_BUF_STRIDE 1536u
59
60#define VNET_F_MAC 5u
61#define VNET_F_VERSION_1 32u
62#define VNET_F_ACCESS_PLATFORM 33u /* VIRTIO_F_ACCESS_PLATFORM (was IOMMU_PLATFORM) */
63
64/* vnet_init_flags flags */
65#define VNET_INIT_REQUIRE_ACCESS_PLATFORM 1u
66
67/* virtio common configuration (virtio 1.x 4.1.4.3), byte offsets */
68#define VNET_CC_DFSELECT 0x00u
69#define VNET_CC_DF 0x04u
70#define VNET_CC_GFSELECT 0x08u
71#define VNET_CC_GF 0x0cu
72#define VNET_CC_MSIX 0x10u
73#define VNET_CC_NUMQ 0x12u
74#define VNET_CC_STATUS 0x14u
75#define VNET_CC_CFGGEN 0x15u
76#define VNET_CC_Q_SELECT 0x16u
77#define VNET_CC_Q_SIZE 0x18u
78#define VNET_CC_Q_MSIX 0x1au
79#define VNET_CC_Q_ENABLE 0x1cu
80#define VNET_CC_Q_NOFF 0x1eu
81#define VNET_CC_Q_DESCLO 0x20u
82#define VNET_CC_Q_DESCHI 0x24u
83#define VNET_CC_Q_AVAILLO 0x28u
84#define VNET_CC_Q_AVAILHI 0x2cu
85#define VNET_CC_Q_USEDLO 0x30u
86#define VNET_CC_Q_USEDHI 0x34u
87#define VNET_CC_LEN 0x38u
88
89#define VNET_S_ACK 1u
90#define VNET_S_DRIVER 2u
91#define VNET_S_DRIVER_OK 4u
92#define VNET_S_FEATURES_OK 8u
93#define VNET_S_NEEDS_RESET 64u
94#define VNET_S_FAILED 128u
95
96#define VNET_DESC_F_WRITE 2u
97#define VNET_AVAIL_F_NO_INTERRUPT 1u
98
99enum { VNET_RX_QUEUE = 0, VNET_TX_QUEUE = 1 };
100typedef enum { VNET_WIN_COMMON = 0, VNET_WIN_NOTIFY = 1, VNET_WIN_ISR = 2, VNET_WIN_DEVICE = 3 } vnet_window;
101
102typedef struct {
103 void *ctx;
104 /* width is 1, 2 or 4; off + width is already checked against the window */
105 uint32_t (*read)(void *ctx, vnet_window win, uint32_t off, unsigned width);
106 void (*write)(void *ctx, vnet_window win, uint32_t off, unsigned width, uint32_t val);
107 /* write `queue` (16 bit) at byte `off` of the notify window (checked) */
108 void (*notify)(void *ctx, uint32_t off, uint16_t queue);
109} vnet_ops;
110
111typedef enum {
112 VNET_OK = 0,
113 VNET_E_ARG = 1, /* NULL, bad queue size, frame too long, buffer too small */
114 VNET_E_CAPS = 2, /* a required capability missing or too short */
115 VNET_E_MEM = 3, /* region too small, misaligned, or bus range wraps */
116 VNET_E_RESET_TIMEOUT = 4, /* device never read back status 0 */
117 VNET_E_NO_VERSION_1 = 5, /* device does not offer VIRTIO_F_VERSION_1 */
118 VNET_E_FEATURES = 6, /* device refused FEATURES_OK */
119 VNET_E_QUEUES = 7, /* fewer than 2 queues, size 0, or queue already enabled */
120 VNET_E_NOTIFY = 8, /* notify offset outside the notify window */
121 VNET_E_CONFIG = 9, /* device config generation never settled */
122 VNET_E_FULL = 10, /* no free TX descriptor (reclaim first) */
123 VNET_E_DEVICE = 11, /* hostile or broken device completion; device is now broken */
124 VNET_E_BROKEN = 12, /* sticky: an earlier VNET_E_DEVICE; reset and re-init */
125 VNET_E_NO_ACCESS_PLATFORM = 13, /* required (flag) but not offered by the device */
126} vnet_err;
127
128typedef struct {
129 uint16_t size; /* negotiated, power of two, <= VNET_QUEUE_MAX */
130 uint16_t avail_idx; /* driver shadow of avail->idx */
131 uint16_t last_used; /* next used-ring slot to consume */
132 uint16_t in_flight; /* descriptors posted and not yet completed */
133 uint32_t notify_off; /* byte offset in the notify window */
134 size_t desc_off, avail_off, used_off, buf_off; /* offsets into mem */
135 uint8_t posted[VNET_QUEUE_MAX];
136} vnet_queue;
137
138typedef struct {
139 vnet_ops ops;
140 virtio_pci_caps caps;
141 uint8_t *mem;
142 uint64_t dma;
143 size_t mem_len;
144 uint64_t features; /* accepted feature bits */
145 uint8_t has_mac;
146 uint8_t mac[6];
147 uint8_t broken;
148 vnet_queue q[2]; /* VNET_RX_QUEUE, VNET_TX_QUEUE */
149} vnet_dev;
150
151/* Bytes of shared region needed for two queues of `qsize` (power of two in
152 * [VNET_QUEUE_MIN, VNET_QUEUE_MAX]); 0 if qsize is invalid. */
153size_t vnet_mem_size(uint16_t qsize);
154
155/* Reset and bring up the device: feature negotiation, both queues, all RX
156 * buffers posted, DRIVER_OK. mem and dma must be 16-byte aligned; mem_len >=
157 * vnet_mem_size(qsize). The device may offer a smaller queue: the driver then
158 * uses the largest power of two it offers (>= VNET_QUEUE_MIN). On failure the
159 * driver writes FAILED to the device status when it got that far. */
160vnet_err vnet_init(vnet_dev *d, const vnet_ops *ops, const virtio_pci_caps *caps,
161 void *mem, uint64_t dma, size_t mem_len, uint16_t qsize);
162/* vnet_init with flags (VNET_INIT_REQUIRE_ACCESS_PLATFORM); vnet_init is flags 0. */
163vnet_err vnet_init_flags(vnet_dev *d, const vnet_ops *ops, const virtio_pci_caps *caps, void *mem, uint64_t dma,
164 size_t mem_len, uint16_t qsize, uint32_t flags);
165
166/* Copy one Ethernet frame (14..VNET_FRAME_MAX bytes, no FCS) into a free TX
167 * buffer behind a zero virtio-net header, post it and notify. */
168vnet_err vnet_tx(vnet_dev *d, const uint8_t *frame, size_t len);
169
170/* Consume TX completions; *reclaimed (optional) gets the count. */
171vnet_err vnet_tx_reclaim(vnet_dev *d, unsigned *reclaimed);
172
173/* Take at most one received frame: copies it (header stripped) to out (cap >=
174 * VNET_FRAME_MAX), sets *len, sets *got to 1, and re-posts the buffer. *got 0:
175 * nothing pending. */
176vnet_err vnet_rx(vnet_dev *d, uint8_t *out, size_t cap, size_t *len, int *got);
177
178const char *vnet_err_name(vnet_err e);
179
180#endif