mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-02-22 19:32:20 +00:00
virtio-devices: Introduce helper for implementing read_config()
Add a helper function to share code between implementations that can use a slice accessible data structure for configuration data. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
5d67338b29
commit
d262540857
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
use crate::{ActivateResult, Error, Queue};
|
use crate::{ActivateResult, Error, Queue};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::io::Write;
|
||||||
use std::num::Wrapping;
|
use std::num::Wrapping;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap, GuestUsize};
|
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap, GuestUsize};
|
||||||
@ -149,6 +150,26 @@ pub trait VirtioDevice: Send {
|
|||||||
fn counters(&self) -> Option<HashMap<&'static str, Wrapping<u64>>> {
|
fn counters(&self) -> Option<HashMap<&'static str, Wrapping<u64>>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper to allow common implementation of read_config
|
||||||
|
fn read_config_from_slice(&self, config: &[u8], offset: u64, mut data: &mut [u8]) {
|
||||||
|
let config_len = config.len() as u64;
|
||||||
|
let data_len = data.len() as u64;
|
||||||
|
if offset + data_len > config_len {
|
||||||
|
error!(
|
||||||
|
"Out-of-bound access to configuration: config_len = {} offset = {:x} length = {} for {}",
|
||||||
|
config_len,
|
||||||
|
offset,
|
||||||
|
data_len,
|
||||||
|
self.device_type()
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Some(end) = offset.checked_add(data.len() as u64) {
|
||||||
|
data.write_all(&config[offset as usize..std::cmp::min(end, config_len) as usize])
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait providing address translation the same way a physical DMA remapping
|
/// Trait providing address translation the same way a physical DMA remapping
|
||||||
|
Loading…
x
Reference in New Issue
Block a user