Skip to content

Commit 7cbbef1

Browse files
authored
Merge pull request #102 from rcore-os/indirect
Add support for indirect descriptors in VirtQueue with alloc feature
2 parents 9653e4c + 2d3a6cc commit 7cbbef1

File tree

8 files changed

+340
-90
lines changed

8 files changed

+340
-90
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ VirtIO guest drivers in Rust. For **no_std** environment.
3232

3333
| Feature flag | Supported | |
3434
| ---------------------------- | --------- | --------------------------------------- |
35-
| `VIRTIO_F_INDIRECT_DESC` | | Indirect descriptors |
35+
| `VIRTIO_F_INDIRECT_DESC` | | Indirect descriptors |
3636
| `VIRTIO_F_EVENT_IDX` || `avail_event` and `used_event` fields |
3737
| `VIRTIO_F_VERSION_1` | TODO | VirtIO version 1 compliance |
3838
| `VIRTIO_F_ACCESS_PLATFORM` || Limited device access to memory |

src/device/blk.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use zerocopy::{AsBytes, FromBytes};
1111

1212
const QUEUE: u16 = 0;
1313
const QUEUE_SIZE: u16 = 16;
14-
const SUPPORTED_FEATURES: BlkFeature = BlkFeature::RO.union(BlkFeature::FLUSH);
14+
const SUPPORTED_FEATURES: BlkFeature = BlkFeature::RO
15+
.union(BlkFeature::FLUSH)
16+
.union(BlkFeature::RING_INDIRECT_DESC);
1517

1618
/// Driver for a VirtIO block device.
1719
///
@@ -68,7 +70,11 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
6870
};
6971
info!("found a block device of size {}KB", capacity / 2);
7072

71-
let queue = VirtQueue::new(&mut transport, QUEUE)?;
73+
let queue = VirtQueue::new(
74+
&mut transport,
75+
QUEUE,
76+
negotiated_features.contains(BlkFeature::RING_INDIRECT_DESC),
77+
)?;
7278
transport.finish_init();
7379

7480
Ok(VirtIOBlk {
@@ -601,7 +607,7 @@ mod tests {
601607
let transport = FakeTransport {
602608
device_type: DeviceType::Block,
603609
max_queue_size: QUEUE_SIZE.into(),
604-
device_features: 0,
610+
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
605611
config_space: NonNull::from(&mut config_space),
606612
state: state.clone(),
607613
};
@@ -671,7 +677,7 @@ mod tests {
671677
let transport = FakeTransport {
672678
device_type: DeviceType::Block,
673679
max_queue_size: QUEUE_SIZE.into(),
674-
device_features: 0,
680+
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
675681
config_space: NonNull::from(&mut config_space),
676682
state: state.clone(),
677683
};
@@ -746,7 +752,7 @@ mod tests {
746752
let transport = FakeTransport {
747753
device_type: DeviceType::Block,
748754
max_queue_size: QUEUE_SIZE.into(),
749-
device_features: BlkFeature::FLUSH.bits(),
755+
device_features: (BlkFeature::RING_INDIRECT_DESC | BlkFeature::FLUSH).bits(),
750756
config_space: NonNull::from(&mut config_space),
751757
state: state.clone(),
752758
};
@@ -813,7 +819,7 @@ mod tests {
813819
let transport = FakeTransport {
814820
device_type: DeviceType::Block,
815821
max_queue_size: QUEUE_SIZE.into(),
816-
device_features: 0,
822+
device_features: BlkFeature::RING_INDIRECT_DESC.bits(),
817823
config_space: NonNull::from(&mut config_space),
818824
state: state.clone(),
819825
};

src/device/console.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl<H: Hal, T: Transport> VirtIOConsole<H, T> {
7272
(features & supported_features).bits()
7373
});
7474
let config_space = transport.config_space::<Config>()?;
75-
let receiveq = VirtQueue::new(&mut transport, QUEUE_RECEIVEQ_PORT_0)?;
76-
let transmitq = VirtQueue::new(&mut transport, QUEUE_TRANSMITQ_PORT_0)?;
75+
let receiveq = VirtQueue::new(&mut transport, QUEUE_RECEIVEQ_PORT_0, false)?;
76+
let transmitq = VirtQueue::new(&mut transport, QUEUE_TRANSMITQ_PORT_0, false)?;
7777

7878
// Safe because no alignment or initialisation is required for [u8], the DMA buffer is
7979
// dereferenceable, and the lifetime of the reference matches the lifetime of the DMA buffer

src/device/gpu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl<H: Hal, T: Transport> VirtIOGpu<H, T> {
5757
);
5858
}
5959

60-
let control_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT)?;
61-
let cursor_queue = VirtQueue::new(&mut transport, QUEUE_CURSOR)?;
60+
let control_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT, false)?;
61+
let cursor_queue = VirtQueue::new(&mut transport, QUEUE_CURSOR, false)?;
6262

6363
let queue_buf_send = FromBytes::new_box_slice_zeroed(PAGE_SIZE);
6464
let queue_buf_recv = FromBytes::new_box_slice_zeroed(PAGE_SIZE);

src/device/input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl<H: Hal, T: Transport> VirtIOInput<H, T> {
3838

3939
let config = transport.config_space::<Config>()?;
4040

41-
let mut event_queue = VirtQueue::new(&mut transport, QUEUE_EVENT)?;
42-
let status_queue = VirtQueue::new(&mut transport, QUEUE_STATUS)?;
41+
let mut event_queue = VirtQueue::new(&mut transport, QUEUE_EVENT, false)?;
42+
let status_queue = VirtQueue::new(&mut transport, QUEUE_STATUS, false)?;
4343
for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
4444
// Safe because the buffer lasts as long as the queue.
4545
let token = unsafe { event_queue.add(&[], &mut [event.as_bytes_mut()])? };

src/device/net.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl<H: Hal, T: Transport, const QUEUE_SIZE: usize> VirtIONet<H, T, QUEUE_SIZE>
139139
return Err(Error::InvalidParam);
140140
}
141141

142-
let send_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT)?;
143-
let mut recv_queue = VirtQueue::new(&mut transport, QUEUE_RECEIVE)?;
142+
let send_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT, false)?;
143+
let mut recv_queue = VirtQueue::new(&mut transport, QUEUE_RECEIVE, false)?;
144144

145145
const NONE_BUF: Option<RxBuffer> = None;
146146
let mut rx_buffers = [NONE_BUF; QUEUE_SIZE];

src/device/socket/vsock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ impl<H: Hal, T: Transport> VirtIOSocket<H, T> {
257257
};
258258
debug!("guest cid: {guest_cid:?}");
259259

260-
let mut rx = VirtQueue::new(&mut transport, RX_QUEUE_IDX)?;
261-
let tx = VirtQueue::new(&mut transport, TX_QUEUE_IDX)?;
262-
let event = VirtQueue::new(&mut transport, EVENT_QUEUE_IDX)?;
260+
let mut rx = VirtQueue::new(&mut transport, RX_QUEUE_IDX, false)?;
261+
let tx = VirtQueue::new(&mut transport, TX_QUEUE_IDX, false)?;
262+
let event = VirtQueue::new(&mut transport, EVENT_QUEUE_IDX, false)?;
263263

264264
// Allocate and add buffers for the RX queue.
265265
let mut rx_queue_buffers = [null_mut(); QUEUE_SIZE];

0 commit comments

Comments
 (0)