Skip to content

Commit 162fdfb

Browse files
committed
devices/mmio: Automatically enable device_idx feature for queues
Instead of each device manually enabling the event_idx for each queue, let's make the transport layer configure the queues properly based on the acknowledged features. If it is necessary to disable this for some specific queue in the future (unlikely) we could an option for this in QueueConfig or the device could override this. Signed-off-by: Matej Hrica <mhrica@redhat.com>
1 parent 69b0a79 commit 162fdfb

6 files changed

Lines changed: 12 additions & 22 deletions

File tree

src/devices/src/virtio/block/device.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,11 @@ impl VirtioDevice for Block {
403403
panic!("virtio_blk: worker thread already exists");
404404
}
405405

406-
let [mut blk_q]: [_; NUM_QUEUES] = queues.try_into().map_err(|_| {
406+
let [blk_q]: [_; NUM_QUEUES] = queues.try_into().map_err(|_| {
407407
error!("Cannot perform activate. Expected {} queue(s)", NUM_QUEUES);
408408
ActivateError::BadActivate
409409
})?;
410410

411-
let event_idx: bool = (self.acked_features & (1 << VIRTIO_RING_F_EVENT_IDX)) != 0;
412-
blk_q.queue.set_event_idx(event_idx);
413-
414411
let disk = match self.disk.take() {
415412
Some(d) => d,
416413
None => DiskProperties::new(

src/devices/src/virtio/fs/device.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,10 @@ impl VirtioDevice for Fs {
169169
panic!("virtio_fs: worker thread already exists");
170170
}
171171

172-
let event_idx: bool = (self.acked_features & (1 << VIRTIO_RING_F_EVENT_IDX)) != 0;
173-
174172
// Extract queues and eventfds from DeviceQueues.
175173
let mut worker_queues = Vec::with_capacity(queues.len());
176174
let mut queue_evts = Vec::with_capacity(queues.len());
177-
for mut dq in queues {
178-
dq.queue.set_event_idx(event_idx);
175+
for dq in queues {
179176
worker_queues.push(dq.queue);
180177
queue_evts.push(dq.event);
181178
}

src/devices/src/virtio/gpu/worker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ impl Worker {
6161
let control_evt = control_q.event.try_clone().unwrap();
6262
// SAFETY: control_evt is valid for the duration of the fcntl calls.
6363
let fd = unsafe { BorrowedFd::borrow_raw(control_evt.as_raw_fd()) };
64-
let flags = OFlag::from_bits_retain(fcntl(fd, FcntlArg::F_GETFL).unwrap()) & !OFlag::O_NONBLOCK;
64+
let flags =
65+
OFlag::from_bits_retain(fcntl(fd, FcntlArg::F_GETFL).unwrap()) & !OFlag::O_NONBLOCK;
6566
fcntl(fd, FcntlArg::F_SETFL(flags)).unwrap();
6667

6768
Self {

src/devices/src/virtio/mmio.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ impl MmioTransport {
310310
.collect();
311311

312312
let mut locked_device = self.locked_device();
313+
let event_idx_enabled =
314+
(locked_device.acked_features() & (1 << VIRTIO_RING_F_EVENT_IDX)) != 0;
315+
for dq in &mut device_queues {
316+
dq.queue.set_event_idx(event_idx_enabled);
317+
}
313318
locked_device
314319
.activate(self.mem.clone(), self.interrupt.clone(), device_queues)
315320
.expect("Failed to activate device");

src/devices/src/virtio/net/device.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,11 @@ impl VirtioDevice for Net {
176176
interrupt: InterruptTransport,
177177
queues: Vec<DeviceQueue>,
178178
) -> ActivateResult {
179-
let [mut rx_q, mut tx_q]: [_; NUM_QUEUES] = queues.try_into().map_err(|_| {
179+
let [rx_q, tx_q]: [_; NUM_QUEUES] = queues.try_into().map_err(|_| {
180180
error!("Cannot perform activate. Expected {} queue(s)", NUM_QUEUES);
181181
ActivateError::BadActivate
182182
})?;
183183

184-
let event_idx: bool = (self.acked_features & (1 << VIRTIO_RING_F_EVENT_IDX)) != 0;
185-
rx_q.queue.set_event_idx(event_idx);
186-
tx_q.queue.set_event_idx(event_idx);
187-
188184
match NetWorker::new(
189185
rx_q,
190186
tx_q,

src/devices/src/virtio/snd/device.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ use std::io::Write;
22
use std::thread::JoinHandle;
33

44
use utils::eventfd::EventFd;
5-
use virtio_bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
65
use vm_memory::{ByteValued, GuestMemoryMmap};
76

87
use super::super::{ActivateError, ActivateResult, DeviceQueue, QueueConfig, VirtioDevice};
98
use super::virtio_sound::VirtioSoundConfig;
109
use super::worker::SndWorker;
11-
use super::{defs, defs::uapi, defs::QUEUE_INDEXES, Error};
10+
use super::{defs, defs::uapi, Error};
1211

1312
use crate::virtio::{DeviceState, InterruptTransport};
1413

@@ -100,7 +99,7 @@ impl VirtioDevice for Snd {
10099
&mut self,
101100
mem: GuestMemoryMmap,
102101
interrupt: InterruptTransport,
103-
mut queues: Vec<DeviceQueue>,
102+
queues: Vec<DeviceQueue>,
104103
) -> ActivateResult {
105104
if self.worker_thread.is_some() {
106105
panic!("virtio_snd: worker thread already exists");
@@ -115,11 +114,6 @@ impl VirtioDevice for Snd {
115114
return Err(ActivateError::BadActivate);
116115
}
117116

118-
let event_idx: bool = (self.acked_features & (1 << VIRTIO_RING_F_EVENT_IDX)) != 0;
119-
for idx in QUEUE_INDEXES {
120-
queues[idx].queue.set_event_idx(event_idx);
121-
}
122-
123117
let worker = SndWorker::new(
124118
queues,
125119
interrupt.clone(),

0 commit comments

Comments
 (0)