mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-08-23 00:11:16 +00:00
virtio-devices: Port devices over to new read_config_from_slice() helper
Using this helper removes lots of duplicated code across the devices. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
d262540857
commit
751a302050
@ -20,9 +20,8 @@ use super::{
|
|||||||
use crate::vm_memory::GuestMemory;
|
use crate::vm_memory::GuestMemory;
|
||||||
use crate::{VirtioInterrupt, VirtioInterruptType};
|
use crate::{VirtioInterrupt, VirtioInterruptType};
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use std::cmp;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
use std::result;
|
use std::result;
|
||||||
@ -471,19 +470,8 @@ impl VirtioDevice for Balloon {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config = self.config.lock().unwrap();
|
self.read_config_from_slice(self.config.lock().unwrap().as_slice(), offset, data);
|
||||||
let config_slice = config.as_slice();
|
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -17,7 +17,6 @@ use crate::VirtioInterrupt;
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use block_util::{build_disk_image_id, Request, RequestType, VirtioBlockConfig};
|
use block_util::{build_disk_image_id, Request, RequestType, VirtioBlockConfig};
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use std::cmp;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||||
@ -504,18 +503,8 @@ impl<T: 'static + DiskFile + Send> VirtioDevice for Block<T> {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
||||||
|
@ -505,20 +505,8 @@ impl VirtioDevice for Console {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config = self.config.lock().unwrap();
|
self.read_config_from_slice(self.config.lock().unwrap().as_slice(), offset, data);
|
||||||
let config_slice = config.as_slice();
|
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -10,11 +10,10 @@ use super::{
|
|||||||
use crate::{DmaRemapping, VirtioInterrupt, VirtioInterruptType};
|
use crate::{DmaRemapping, VirtioInterrupt, VirtioInterruptType};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use std::cmp;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::ops::Bound::Included;
|
use std::ops::Bound::Included;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
@ -968,26 +967,16 @@ impl VirtioDevice for Iommu {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let mut config: Vec<u8> = Vec::new();
|
let mut config: Vec<u8> = Vec::new();
|
||||||
config.extend_from_slice(self.config.as_slice());
|
config.extend_from_slice(self.config.as_slice());
|
||||||
for config_topo_pci_range in self.config_topo_pci_ranges.iter() {
|
for config_topo_pci_range in self.config_topo_pci_ranges.iter() {
|
||||||
config.extend_from_slice(config_topo_pci_range.as_slice());
|
config.extend_from_slice(config_topo_pci_range.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
let config_slice = config.as_slice();
|
self.read_config_from_slice(config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn activate(
|
fn activate(
|
||||||
&mut self,
|
&mut self,
|
||||||
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
||||||
|
@ -21,7 +21,7 @@ use crate::{VirtioInterrupt, VirtioInterruptType};
|
|||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
||||||
use std::result;
|
use std::result;
|
||||||
@ -870,19 +870,8 @@ impl VirtioDevice for Mem {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config = self.config.lock().unwrap();
|
self.read_config_from_slice(self.config.lock().unwrap().as_slice(), offset, data);
|
||||||
let config_slice = config.as_slice();
|
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -20,10 +20,9 @@ use net_util::{
|
|||||||
open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
|
open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
|
||||||
RX_QUEUE_EVENT, RX_TAP_EVENT, TX_QUEUE_EVENT,
|
RX_QUEUE_EVENT, RX_TAP_EVENT, TX_QUEUE_EVENT,
|
||||||
};
|
};
|
||||||
use std::cmp;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io;
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
use std::num::Wrapping;
|
use std::num::Wrapping;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
@ -413,18 +412,8 @@ impl VirtioDevice for Net {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -15,10 +15,9 @@ use crate::{VirtioInterrupt, VirtioInterruptType};
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||||
use std::cmp;
|
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
use std::result;
|
use std::result;
|
||||||
@ -463,19 +462,8 @@ impl VirtioDevice for Pmem {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -9,8 +9,6 @@ use super::{Error, Result};
|
|||||||
use crate::VirtioInterrupt;
|
use crate::VirtioInterrupt;
|
||||||
use block_util::VirtioBlockConfig;
|
use block_util::VirtioBlockConfig;
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use std::cmp;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::result;
|
use std::result;
|
||||||
@ -188,18 +186,8 @@ impl VirtioDevice for Blk {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
||||||
|
@ -10,9 +10,7 @@ use crate::{
|
|||||||
VirtioInterrupt, VirtioSharedMemoryList, VIRTIO_F_VERSION_1,
|
VirtioInterrupt, VirtioSharedMemoryList, VIRTIO_F_VERSION_1,
|
||||||
};
|
};
|
||||||
use libc::{self, c_void, off64_t, pread64, pwrite64, EFD_NONBLOCK};
|
use libc::{self, c_void, off64_t, pread64, pwrite64, EFD_NONBLOCK};
|
||||||
use std::cmp;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
|
||||||
use std::os::unix::io::{AsRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, RawFd};
|
||||||
use std::result;
|
use std::result;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
@ -407,18 +405,8 @@ impl VirtioDevice for Fs {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
@ -13,8 +13,6 @@ use super::{Error, Result};
|
|||||||
use crate::VirtioInterrupt;
|
use crate::VirtioInterrupt;
|
||||||
use libc::EFD_NONBLOCK;
|
use libc::EFD_NONBLOCK;
|
||||||
use net_util::MacAddr;
|
use net_util::MacAddr;
|
||||||
use std::cmp;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::result;
|
use std::result;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
@ -194,18 +192,8 @@ impl VirtioDevice for Net {
|
|||||||
self.acked_features |= v;
|
self.acked_features |= v;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
||||||
let config_slice = self.config.as_slice();
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
||||||
let config_len = config_slice.len() as u64;
|
|
||||||
if offset >= config_len {
|
|
||||||
error!("Failed to read config space");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
||||||
// This write can't fail, offset and end are checked against config_len.
|
|
||||||
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
|
Loading…
Reference in New Issue
Block a user