From 07d1c765d4cdd345e63cd77504a38646c2a0f015 Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Wed, 11 Sep 2019 15:53:02 -0400 Subject: [PATCH] allow configuring size of virtio queues --- rust/src/virtio/bus.rs | 20 ++++++++++++++++---- rust/src/virtio/config.rs | 11 +++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/rust/src/virtio/bus.rs b/rust/src/virtio/bus.rs index e665796..1b8e36d 100644 --- a/rust/src/virtio/bus.rs +++ b/rust/src/virtio/bus.rs @@ -6,6 +6,7 @@ use super::{VirtioDevice,VirtioDeviceOps,PciIrq}; use super::consts::*; use super::pci::PciBus; use crate::vm::Result; +use std::iter; pub struct VirtioBus { @@ -43,7 +44,7 @@ pub struct VirtioDeviceConfig<'a> { kvm: Kvm, ops: Arc>, mmio: AddressRange, - num_queues: usize, + queue_sizes: Vec, config_size: usize, device_class: u16, features: u64, @@ -61,7 +62,7 @@ impl <'a> VirtioDeviceConfig<'a> { kvm, ops, mmio, - num_queues: 0, + queue_sizes: Vec::new(), config_size: 0, features: 0, device_class: 0x0880, @@ -100,7 +101,11 @@ impl <'a> VirtioDeviceConfig<'a> { } pub fn num_queues(&self) -> usize { - self.num_queues + self.queue_sizes.len() + } + + pub fn queue_sizes(&self) -> &[usize] { + &self.queue_sizes } #[allow(dead_code)] @@ -108,8 +113,15 @@ impl <'a> VirtioDeviceConfig<'a> { self.config_size } + pub fn set_queue_sizes(&mut self, sizes: &[usize]) -> &'a mut VirtioDeviceConfig { + self.queue_sizes.clear(); + self.queue_sizes.extend_from_slice(sizes); + self + } + pub fn set_num_queues(&mut self, n: usize) -> &'a mut VirtioDeviceConfig { - self.num_queues = n; + self.queue_sizes.clear(); + self.queue_sizes.extend(iter::repeat(DEFAULT_QUEUE_SIZE as usize).take(n)); self } diff --git a/rust/src/virtio/config.rs b/rust/src/virtio/config.rs index ab780f6..8ff473b 100644 --- a/rust/src/virtio/config.rs +++ b/rust/src/virtio/config.rs @@ -8,7 +8,6 @@ use super::eventfd::IoEventFd; use super::vring::Vring; use super::virtqueue::InterruptLine; use super::bus::VirtioDeviceConfig; -use super::consts::DEFAULT_QUEUE_SIZE; /// /// Manages a set of virtqueues during device intitialization. @@ -28,7 +27,7 @@ impl VirtQueueConfig { num_queues: dev_config.num_queues(), selected_queue: 0, enabled_features: 0, - vrings: create_vrings(memory,dev_config.num_queues()), + vrings: create_vrings(memory,dev_config.queue_sizes()), interrupt: InterruptLine::from_config(&dev_config)?, events: create_ioeventfds(&dev_config)?, }) @@ -123,10 +122,10 @@ fn create_ioeventfds(conf: &VirtioDeviceConfig) -> Result>> { Ok(v) } -fn create_vrings(memory: &GuestRam, n: usize) -> Vec { - let mut v = Vec::with_capacity(n); - for _ in 0..n { - v.push(Vring::new(memory.clone(), DEFAULT_QUEUE_SIZE)); +fn create_vrings(memory: &GuestRam, queue_sizes: &[usize]) -> Vec { + let mut v = Vec::with_capacity(queue_sizes.len()); + for &sz in queue_sizes { + v.push(Vring::new(memory.clone(), sz as u16)) } v }