1#![allow(unsafe_op_in_unsafe_fn)]
53
54use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};
55
56pub const NET_QUEUE_MAGIC: u32 = 0x514E_5453;
58pub const NET_QUEUE_ABI: u16 = 1;
60
61pub const NET_FRAME_SIZE: usize = 2048;
64pub const NET_RING_SLOTS: usize = 256;
66pub const NET_POOL_SLOTS: usize = 256;
68
69pub const NET_QUEUE_HDR_SIZE: usize = 0x100;
70pub const NET_RX_DESC_OFF: usize = 0x100;
71pub const NET_TX_DESC_OFF: usize = 0x1100;
72pub const NET_STATS_OFF: usize = 0x2100;
73pub const NET_RX_POOL_OFF: usize = 0x3000;
74pub const NET_TX_POOL_OFF: usize = NET_RX_POOL_OFF + NET_POOL_SLOTS * NET_FRAME_SIZE;
75pub const NET_QUEUE_SIZE: usize = NET_TX_POOL_OFF + NET_POOL_SLOTS * NET_FRAME_SIZE;
76
77const _: () = assert!(NET_RING_SLOTS.is_power_of_two());
78const _: () = assert!(NET_POOL_SLOTS % 64 == 0);
79const _: () = assert!(NET_QUEUE_SIZE % 4096 == 0);
80const _: () = assert!(NET_QUEUE_SIZE / 4096 == 0x103);
81
82pub mod fd_flags {
84 pub const EOP: u16 = 1 << 0;
86 pub const SOP: u16 = 1 << 1;
88 pub const CSUM_OK: u16 = 1 << 2;
90 pub const NEED_COMP: u16 = 1 << 3;
92 pub const VLAN: u16 = 1 << 4;
94 pub const TRUNCATED: u16 = 1 << 5;
96}
97
98pub mod features {
101 pub const CSUM_TX: u64 = 1 << 0;
102 pub const CSUM_RX: u64 = 1 << 1;
103 pub const TSO_TX: u64 = 1 << 2;
104 pub const LRO_RX: u64 = 1 << 3;
105 pub const VLAN_TX: u64 = 1 << 4;
106 pub const VLAN_RX: u64 = 1 << 5;
107 pub const RSS: u64 = 1 << 6;
108 pub const DATA_RING: u64 = 1 << 7;
109 pub const ZC_TX: u64 = 1 << 8;
110 pub const ZC_RX: u64 = 1 << 9;
111 pub const MULTI_QUEUE: u64 = 1 << 10;
112 pub const POLLING: u64 = 1 << 11;
113
114 pub const ALL: u64 = CSUM_TX
115 | CSUM_RX
116 | TSO_TX
117 | LRO_RX
118 | VLAN_TX
119 | VLAN_RX
120 | RSS
121 | DATA_RING
122 | ZC_TX
123 | ZC_RX
124 | MULTI_QUEUE
125 | POLLING;
126}
127
128pub mod link_flags {
130 pub const UP: u32 = 1 << 0;
131 pub const FULL_DUPLEX: u32 = 1 << 1;
132 pub const CARRIER: u32 = 1 << 2;
133}
134
135pub mod event {
137 pub const RX_READY: u32 = 1;
138 pub const TX_DONE: u32 = 2;
139 pub const LINK_UP: u32 = 3;
140 pub const LINK_DOWN: u32 = 4;
141 pub const QUEUE_ERROR: u32 = 5;
142 pub const RESET: u32 = 6;
143 pub const DEVICE_ADD: u32 = 7;
144 pub const DEVICE_REMOVE: u32 = 8;
145 pub const QUIESCE: u32 = 9;
146 pub const RESUME: u32 = 10;
147}
148
149#[repr(C)]
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
152pub struct NetFrameDesc {
153 pub slot: u32,
155 pub len: u16,
157 pub flags: u16,
159 pub vlan: u16,
161 pub rss: u16,
163 pub reserved: u32,
165}
166
167const _: () = assert!(core::mem::size_of::<NetFrameDesc>() == 16);
168
169#[repr(C)]
175pub struct NetRing {
176 pub head: AtomicU32,
177 pub tail: AtomicU32,
178 pub size: u32,
179 pub flags: u32,
180}
181
182impl NetRing {
183 pub const fn new(size: u32) -> Self {
184 Self {
185 head: AtomicU32::new(0),
186 tail: AtomicU32::new(0),
187 size,
188 flags: 0,
189 }
190 }
191
192 #[inline]
193 pub fn len(&self) -> u32 {
194 self.tail
195 .load(Ordering::Acquire)
196 .wrapping_sub(self.head.load(Ordering::Acquire))
197 }
198
199 #[inline]
200 pub fn is_empty(&self) -> bool {
201 self.len() == 0
202 }
203
204 #[inline]
209 pub unsafe fn push(&self, descs: *mut NetFrameDesc, desc: NetFrameDesc) -> bool {
210 let tail = self.tail.load(Ordering::Relaxed);
211 let head = self.head.load(Ordering::Acquire);
212 if tail.wrapping_sub(head) >= self.size {
213 return false;
214 }
215 let mask = (self.size - 1) as usize;
216 core::ptr::write(descs.add((tail as usize) & mask), desc);
217 self.tail.store(tail.wrapping_add(1), Ordering::Release);
218 true
219 }
220
221 #[inline]
226 pub unsafe fn pop(&self, descs: *const NetFrameDesc) -> Option<NetFrameDesc> {
227 let head = self.head.load(Ordering::Relaxed);
228 let tail = self.tail.load(Ordering::Acquire);
229 if head == tail {
230 return None;
231 }
232 let mask = (self.size - 1) as usize;
233 let desc = core::ptr::read(descs.add((head as usize) & mask));
234 self.head.store(head.wrapping_add(1), Ordering::Release);
235 Some(desc)
236 }
237}
238
239const _: () = assert!(core::mem::size_of::<NetRing>() == 16);
240
241#[repr(C)]
247pub struct NetQueueHeader {
248 pub magic: u32,
249 pub abi: u16,
250 pub flags: u16,
251 pub qid: u16,
252 pub frame_size: u16,
253 pub slots: u16,
254 pub reserved0: u16,
255 pub features: AtomicU64,
257 pub rx: NetRing,
258 pub tx: NetRing,
259 pub rx_bitmap: [AtomicU64; 4],
261 pub tx_bitmap: [AtomicU64; 4],
262 pub rx_dropped: AtomicU64,
263 pub tx_dropped: AtomicU64,
264 pub irq_count: AtomicU64,
265 pub poll_count: AtomicU64,
266 pub epoch: AtomicU32,
268 pub link: AtomicU32,
270 pub speed_mbps: AtomicU32,
272 pub mtu: AtomicU32,
274 pub reserved: [u8; 64],
275}
276
277const _: () = assert!(core::mem::size_of::<NetQueueHeader>() <= NET_QUEUE_HDR_SIZE);
278
279impl NetQueueHeader {
280 pub unsafe fn init(ptr: *mut Self, qid: u16, features: u64, mtu: u32, epoch: u32) {
286 core::ptr::write(
287 ptr,
288 Self {
289 magic: NET_QUEUE_MAGIC,
290 abi: NET_QUEUE_ABI,
291 flags: 0,
292 qid,
293 frame_size: NET_FRAME_SIZE as u16,
294 slots: NET_RING_SLOTS as u16,
295 reserved0: 0,
296 features: AtomicU64::new(features),
297 rx: NetRing::new(NET_RING_SLOTS as u32),
298 tx: NetRing::new(NET_RING_SLOTS as u32),
299 rx_bitmap: [
300 AtomicU64::new(0),
301 AtomicU64::new(0),
302 AtomicU64::new(0),
303 AtomicU64::new(0),
304 ],
305 tx_bitmap: [
306 AtomicU64::new(0),
307 AtomicU64::new(0),
308 AtomicU64::new(0),
309 AtomicU64::new(0),
310 ],
311 rx_dropped: AtomicU64::new(0),
312 tx_dropped: AtomicU64::new(0),
313 irq_count: AtomicU64::new(0),
314 poll_count: AtomicU64::new(0),
315 epoch: AtomicU32::new(epoch),
316 link: AtomicU32::new(0),
317 speed_mbps: AtomicU32::new(0),
318 mtu: AtomicU32::new(mtu),
319 reserved: [0u8; 64],
320 },
321 );
322 }
323
324 #[inline]
325 pub fn is_valid(&self) -> bool {
326 self.magic == NET_QUEUE_MAGIC
327 && self.abi == NET_QUEUE_ABI
328 && self.frame_size as usize == NET_FRAME_SIZE
329 && self.slots as usize == NET_RING_SLOTS
330 }
331
332 #[inline]
333 pub fn as_ptr(&self) -> *mut u8 {
334 self as *const Self as *mut u8
335 }
336
337 #[inline]
340 pub unsafe fn rx_descs(&self) -> *mut NetFrameDesc {
341 self.as_ptr().add(NET_RX_DESC_OFF) as *mut NetFrameDesc
342 }
343
344 #[inline]
347 pub unsafe fn tx_descs(&self) -> *mut NetFrameDesc {
348 self.as_ptr().add(NET_TX_DESC_OFF) as *mut NetFrameDesc
349 }
350
351 #[inline]
354 pub unsafe fn rx_slot(&self, slot: u32) -> *mut u8 {
355 debug_assert!((slot as usize) < NET_POOL_SLOTS);
356 self.as_ptr()
357 .add(NET_RX_POOL_OFF + slot as usize * self.frame_size as usize)
358 }
359
360 #[inline]
363 pub unsafe fn tx_slot(&self, slot: u32) -> *mut u8 {
364 debug_assert!((slot as usize) < NET_POOL_SLOTS);
365 self.as_ptr()
366 .add(NET_TX_POOL_OFF + slot as usize * self.frame_size as usize)
367 }
368
369 #[inline]
371 pub fn alloc_rx_slot(&self) -> Option<u32> {
372 bitmap_alloc(&self.rx_bitmap)
373 }
374
375 #[inline]
377 pub fn free_rx_slot(&self, slot: u32) {
378 bitmap_free(&self.rx_bitmap, slot);
379 }
380
381 #[inline]
383 pub fn alloc_tx_slot(&self) -> Option<u32> {
384 bitmap_alloc(&self.tx_bitmap)
385 }
386
387 #[inline]
389 pub fn free_tx_slot(&self, slot: u32) {
390 bitmap_free(&self.tx_bitmap, slot);
391 }
392
393 #[inline]
399 pub unsafe fn rx_push(&self, desc: NetFrameDesc) -> bool {
400 self.rx.push(self.rx_descs(), desc)
401 }
402
403 #[inline]
409 pub unsafe fn rx_pop(&self) -> Option<NetFrameDesc> {
410 self.rx.pop(self.rx_descs())
411 }
412
413 #[inline]
419 pub unsafe fn tx_push(&self, desc: NetFrameDesc) -> bool {
420 self.tx.push(self.tx_descs(), desc)
421 }
422
423 #[inline]
429 pub unsafe fn tx_pop(&self) -> Option<NetFrameDesc> {
430 self.tx.pop(self.tx_descs())
431 }
432}
433
434#[inline]
436pub fn bitmap_alloc(words: &[AtomicU64; 4]) -> Option<u32> {
437 for (index, word) in words.iter().enumerate() {
438 loop {
439 let current = word.load(Ordering::Relaxed);
440 if current == u64::MAX {
441 break;
442 }
443 let bit = (!current).trailing_zeros();
444 let mask = 1u64 << bit;
445 if word
446 .compare_exchange_weak(current, current | mask, Ordering::AcqRel, Ordering::Relaxed)
447 .is_ok()
448 {
449 return Some(index as u32 * 64 + bit);
450 }
451 }
452 }
453 None
454}
455
456#[inline]
458pub fn bitmap_free(words: &[AtomicU64; 4], slot: u32) {
459 let index = (slot as usize) / 64;
460 if index >= words.len() {
461 return;
462 }
463 let mask = 1u64 << (slot % 64);
464 words[index].fetch_and(!mask, Ordering::Release);
465}
466
467#[repr(C)]
469#[derive(Debug, Clone, Copy, Default)]
470pub struct NetQueueMap {
471 pub base: u64,
472 pub len: u64,
473 pub qid: u16,
474 pub frame_size: u16,
475 pub slots: u16,
476 pub flags: u16,
477}
478
479#[repr(C)]
481#[derive(Debug, Clone, Copy, Default)]
482pub struct NetLinkInfo {
483 pub mac: [u8; 6],
484 pub _pad: u16,
485 pub mtu: u32,
486 pub flags: u32,
487 pub speed_mbps: u32,
488 pub epoch: u32,
489}
490
491#[repr(C)]
493#[derive(Debug, Clone, Copy, Default)]
494pub struct NetStats {
495 pub rx_packets: u64,
496 pub rx_bytes: u64,
497 pub rx_dropped: u64,
498 pub tx_packets: u64,
499 pub tx_bytes: u64,
500 pub tx_dropped: u64,
501 pub irqs: u64,
502 pub polls: u64,
503}
504
505#[repr(C)]
507#[derive(Debug, Clone, Copy, Default)]
508pub struct NetQueueInfo {
509 pub features: u64,
510 pub qid: u16,
511 pub slots: u16,
512 pub frame_size: u16,
513 pub _pad0: u16,
514 pub epoch: u32,
515 pub mtu: u32,
516 pub stats: NetStats,
517}
518
519#[repr(C)]
521#[derive(Debug, Clone, Copy, Default)]
522pub struct NetEvent {
523 pub kind: u32,
524 pub qid: u16,
525 pub flags: u16,
526 pub epoch: u32,
527 pub _pad: u32,
528 pub data: u64,
529}
530
531#[repr(C)]
533#[derive(Debug, Clone, Copy)]
534pub struct NetFrame {
535 pub addr: u64,
537 pub len: u32,
539 pub flags: u32,
541}