rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | //! Driver for virtio-net: QEMU's paravirtual network card
//! (`-device virtio-net-pci`).
//!
//! It works like virtio-blk (block/virtio_blk.rs: same legacy I/O-port
//! registers, same virtqueues) but with *two* queues:
//! * queue 0, receive: we hand the card empty buffers in advance; when a
//! frame arrives it fills one, puts it in the used ring and (because we
//! asked for it) raises an interrupt.
//! * queue 1, transmit: we fill a buffer with a frame and hand it over.
//! Every buffer starts with a 10-byte `virtio_net_hdr` (checksum offload,
//! segmentation offload...). We negotiate none of those features, so on
//! send it's all zeros and on receive we skip it. After the header comes a
//! plain Ethernet frame: destination MAC, source MAC, type, payload.
//!
//! The only feature we accept is MAC (bit 5): "the device config area holds
//! a MAC address", which is how we learn ours (QEMU: 52:54:00:12:34:56).
//!
//! This file knows nothing about IP: it just moves Ethernet frames. The
//! `smoltcp::phy::Device` impl at the bottom is the glue that lets smoltcp
//! (net/mod.rs) send and receive through it.
//! Reference: virtio spec 1.2 section 5.1 (network device).
use cratememory;
use cratePciDevice;
use Vec;
use ;
use ;
use ;
use Instant;
use PhysAddr;
use Port;
// Legacy virtio PCI registers (offsets into the I/O BAR), as in virtio_blk.
const REG_DEVICE_FEATURES: u16 = 0x00;
const REG_GUEST_FEATURES: u16 = 0x04;
const REG_QUEUE_ADDRESS: u16 = 0x08;
const REG_QUEUE_SIZE: u16 = 0x0c;
const REG_QUEUE_SELECT: u16 = 0x0e;
const REG_QUEUE_NOTIFY: u16 = 0x10;
const REG_DEVICE_STATUS: u16 = 0x12;
pub const REG_ISR_STATUS: u16 = 0x13; // reading it acknowledges the interrupt
const REG_MAC: u16 = 0x14; // device config: 6-byte MAC address
const STATUS_ACKNOWLEDGE: u8 = 1;
const STATUS_DRIVER: u8 = 2;
const STATUS_DRIVER_OK: u8 = 4;
const FEATURE_MAC: u32 = 1 << 5;
const DESC_WRITE: u16 = 2; // device writes into this buffer
const AVAIL_NO_INTERRUPT: u16 = 1;
const QUEUE_RX: u16 = 0;
const QUEUE_TX: u16 = 1;
/// Buffers per queue, and their size: a 10-byte header + a max-size
/// Ethernet frame (1514 bytes) fits in 2 KiB, so 2 buffers per page.
const BUFFERS: usize = 32;
const BUF_SIZE: usize = 2048;
const HEADER_LEN: usize = 10;
/// Largest Ethernet frame without the 4-byte checksum (the card adds it).
pub const MTU: usize = 1514;
/// PCI vendor 0x1af4 (virtio), device 0x1000 = transitional network card.
/// One virtqueue plus its buffers. Descriptor `i` always points at
/// buffer `i`, which keeps the bookkeeping trivial.
/// Packet and byte counters, shown by `ifconfig`.
// --- smoltcp glue -------------------------------------------------------------
//
// smoltcp asks a `Device` for *tokens*: an RxToken is "a received frame you
// may look at", a TxToken is "permission to build one frame to send". This
// lets smoltcp work without copying in zero-copy drivers; ours copies the
// received frame out (simple, and frames are small).
;
;
|