morpheus_network/driver/virtio/
rx.rs

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
//! VirtIO RX logic.
//!
//! Poll-based receive - never block!
//!
//! # Reference
//! NETWORK_IMPL_GUIDE.md ยง4.7

use crate::dma::BufferPool;
use crate::driver::traits::RxError;
use crate::types::{RxResult, VirtioNetHdr, VirtqueueState};

/// Receive a packet via VirtIO.
///
/// # Arguments
/// - `rx_state`: RX virtqueue state
/// - `rx_pool`: RX buffer pool
/// - `out_buffer`: Buffer to copy received frame into
///
/// # Returns
/// - `Ok(Some(len))`: Frame received, `len` bytes copied (without VirtIO header)
/// - `Ok(None)`: No frame available (normal)
/// - `Err(RxError)`: Receive error
///
/// # Contract
/// - MUST return immediately (no blocking)
#[cfg(target_arch = "x86_64")]
pub fn receive(
    rx_state: &mut VirtqueueState,
    rx_pool: &mut BufferPool,
    out_buffer: &mut [u8],
) -> Result<Option<usize>, RxError> {
    use crate::asm::drivers::virtio::rx as asm_rx;

    // Poll via ASM (includes barriers)
    let rx_result = asm_rx::poll(rx_state);

    let result = match rx_result {
        Some(r) => r,
        None => return Ok(None), // No packet available
    };

    // Get buffer (now driver-owned)
    let buf = rx_pool
        .get_mut(result.buffer_idx)
        .ok_or(RxError::DeviceError)?;
    unsafe {
        buf.mark_driver_owned();
    }

    // Calculate frame length (skip 12-byte VirtIO header)
    let frame_len = result.length as usize;
    if frame_len < VirtioNetHdr::SIZE {
        // Invalid - resubmit and report error
        resubmit_buffer(rx_state, rx_pool, result.buffer_idx);
        return Err(RxError::DeviceError);
    }

    let payload_len = frame_len - VirtioNetHdr::SIZE;

    // Check if caller's buffer is large enough
    if payload_len > out_buffer.len() {
        // Frame too large - resubmit our buffer but return error
        resubmit_buffer(rx_state, rx_pool, result.buffer_idx);
        return Err(RxError::BufferTooSmall {
            needed: payload_len,
        });
    }

    // Copy frame (skip VirtIO header)
    out_buffer[..payload_len]
        .copy_from_slice(&buf.as_slice()[VirtioNetHdr::SIZE..VirtioNetHdr::SIZE + payload_len]);

    // Resubmit buffer to RX queue
    resubmit_buffer(rx_state, rx_pool, result.buffer_idx);

    Ok(Some(payload_len))
}

/// Resubmit RX buffer after processing.
///
/// Notifies device immediately to ensure packets keep flowing.
#[cfg(target_arch = "x86_64")]
fn resubmit_buffer(rx_state: &mut VirtqueueState, rx_pool: &mut BufferPool, buf_idx: u16) {
    use crate::asm::drivers::virtio::{notify, rx as asm_rx};

    if let Some(buf) = rx_pool.get_mut(buf_idx) {
        // Mark device-owned before submit
        unsafe {
            buf.mark_device_owned();
        }

        let capacity = buf.capacity() as u16;
        let success = asm_rx::submit(rx_state, buf_idx, capacity);

        if !success {
            // Queue full - this shouldn't happen with proper sizing
            unsafe {
                buf.mark_driver_owned();
            }
            // Buffer will be reclaimed on next refill cycle
        } else {
            // Notify device that buffer is available
            notify::notify(rx_state);
        }
    }
}

/// Refill RX queue with available buffers.
///
/// Call in main loop Phase 1.
#[cfg(target_arch = "x86_64")]
pub fn refill_queue(rx_state: &mut VirtqueueState, rx_pool: &mut BufferPool) {
    use crate::asm::drivers::virtio::{notify, rx as asm_rx};

    let mut submitted = 0;

    while let Some(buf) = rx_pool.alloc() {
        let buf_idx = buf.index();
        let capacity = buf.capacity() as u16;

        // Mark device-owned before submit
        unsafe {
            buf.mark_device_owned();
        }

        let success = asm_rx::submit(rx_state, buf_idx, capacity);

        if !success {
            // Queue full - return buffer and stop
            unsafe {
                buf.mark_driver_owned();
            }
            rx_pool.free(buf_idx);
            break;
        }

        submitted += 1;
    }

    // Notify device if we submitted any buffers
    if submitted > 0 {
        notify::notify(rx_state);
    }
}

/// Pre-fill RX queue during initialization.
///
/// Should be called after queue setup, before DRIVER_OK.
#[cfg(target_arch = "x86_64")]
pub fn prefill_queue(
    rx_state: &mut VirtqueueState,
    rx_pool: &mut BufferPool,
) -> Result<usize, RxError> {
    use crate::asm::drivers::virtio::{notify, rx as asm_rx};

    let mut filled = 0;

    while let Some(buf) = rx_pool.alloc() {
        let buf_idx = buf.index();
        let capacity = buf.capacity() as u16;

        // Mark device-owned before submit
        unsafe {
            buf.mark_device_owned();
        }

        let success = asm_rx::submit(rx_state, buf_idx, capacity);

        if !success {
            // Queue full - return buffer and stop
            unsafe {
                buf.mark_driver_owned();
            }
            rx_pool.free(buf_idx);
            break;
        }

        filled += 1;
    }

    // Notify device
    if filled > 0 {
        notify::notify(rx_state);
    }

    Ok(filled)
}

// Stubs for non-x86_64 platforms
#[cfg(not(target_arch = "x86_64"))]
pub fn receive(
    _rx_state: &mut VirtqueueState,
    _rx_pool: &mut BufferPool,
    _out_buffer: &mut [u8],
) -> Result<Option<usize>, RxError> {
    Ok(None)
}

#[cfg(not(target_arch = "x86_64"))]
pub fn refill_queue(_rx_state: &mut VirtqueueState, _rx_pool: &mut BufferPool) {}

#[cfg(not(target_arch = "x86_64"))]
pub fn prefill_queue(
    _rx_state: &mut VirtqueueState,
    _rx_pool: &mut BufferPool,
) -> Result<usize, RxError> {
    Ok(0)
}