Skip to content

Commit f55eef4

Browse files
committed
vhost-user-frontend: Remove unnecessary definition/helpers
Some of the helpers are not required for every user, and can be dropped. Remove them and do minor cleanups. Signed-off-by: Viresh Kumar <[email protected]>
1 parent 1f554d5 commit f55eef4

File tree

5 files changed

+19
-237
lines changed

5 files changed

+19
-237
lines changed

crates/vhost-user-frontend/src/device.rs

+3-53
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
88

99
use crate::{
10-
ActivateError, ActivateResult, Error, GuestMemoryMmap, GuestRegionMmap,
11-
VIRTIO_F_RING_INDIRECT_DESC,
10+
AccessPlatform, ActivateError, ActivateResult, Error, GuestMemoryMmap, GuestRegionMmap,
11+
VirtioDeviceType, VIRTIO_F_RING_INDIRECT_DESC,
1212
};
1313
use libc::EFD_NONBLOCK;
1414
use std::collections::HashMap;
1515
use std::io::Write;
1616
use std::num::Wrapping;
17-
use std::sync::{
18-
atomic::{AtomicBool, Ordering},
19-
Arc, Barrier,
20-
};
17+
use std::sync::{atomic::AtomicBool, Arc, Barrier};
2118
use std::thread;
2219
use virtio_queue::Queue;
2320
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestUsize};
24-
use vm_migration::{MigratableError, Pausable};
25-
use vm_virtio::AccessPlatform;
26-
use vm_virtio::VirtioDeviceType;
2721
use vmm_sys_util::eventfd::EventFd;
2822

2923
pub enum VirtioInterruptType {
@@ -281,11 +275,6 @@ impl VirtioCommon {
281275
}
282276

283277
pub fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
284-
// We first must resume the virtio thread if it was paused.
285-
if self.pause_evt.take().is_some() {
286-
self.resume().ok()?;
287-
}
288-
289278
if let Some(kill_evt) = self.kill_evt.take() {
290279
// Ignore the result because there is nothing we can do about it.
291280
let _ = kill_evt.write(1);
@@ -317,42 +306,3 @@ impl VirtioCommon {
317306
self.avail_features &= !(1 << VIRTIO_F_RING_INDIRECT_DESC);
318307
}
319308
}
320-
321-
impl Pausable for VirtioCommon {
322-
fn pause(&mut self) -> std::result::Result<(), MigratableError> {
323-
info!(
324-
"Pausing virtio-{}",
325-
VirtioDeviceType::from(self.device_type)
326-
);
327-
self.paused.store(true, Ordering::SeqCst);
328-
if let Some(pause_evt) = &self.pause_evt {
329-
pause_evt
330-
.write(1)
331-
.map_err(|e| MigratableError::Pause(e.into()))?;
332-
333-
// Wait for all threads to acknowledge the pause before going
334-
// any further. This is exclusively performed when pause_evt
335-
// eventfd is Some(), as this means the virtio device has been
336-
// activated. One specific case where the device can be paused
337-
// while it hasn't been yet activated is snapshot/restore.
338-
self.paused_sync.as_ref().unwrap().wait();
339-
}
340-
341-
Ok(())
342-
}
343-
344-
fn resume(&mut self) -> std::result::Result<(), MigratableError> {
345-
info!(
346-
"Resuming virtio-{}",
347-
VirtioDeviceType::from(self.device_type)
348-
);
349-
self.paused.store(false, Ordering::SeqCst);
350-
if let Some(epoll_threads) = &self.epoll_threads {
351-
for t in epoll_threads.iter() {
352-
t.thread().unpark();
353-
}
354-
}
355-
356-
Ok(())
357-
}
358-
}

crates/vhost-user-frontend/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ pub use crate::vhost_user::*;
2424
use std::fmt::{self, Debug};
2525

2626
use virtio_queue::{Queue, QueueT};
27-
use vm_memory::{
28-
bitmap::AtomicBitmap, GuestAddress, GuestMemory,
29-
};
27+
use vm_memory::{bitmap::AtomicBitmap, GuestAddress, GuestMemory};
3028

31-
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
32-
type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>;
33-
type MmapRegion = vm_memory::MmapRegion<AtomicBitmap>;
29+
pub type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
30+
pub type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>;
31+
pub type MmapRegion = vm_memory::MmapRegion<AtomicBitmap>;
3432

3533
const VIRTIO_F_RING_INDIRECT_DESC: u32 = 28;
3634
const VIRTIO_F_RING_EVENT_IDX: u32 = 29;
3735
const VIRTIO_F_VERSION_1: u32 = 32;
36+
#[allow(dead_code)]
3837
const VIRTIO_F_IOMMU_PLATFORM: u32 = 33;
3938
const VIRTIO_F_IN_ORDER: u32 = 35;
4039
const VIRTIO_F_ORDER_PLATFORM: u32 = 36;

crates/vhost-user-frontend/src/thread_helper.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44
//
55

6-
use crate::{
7-
seccomp_filters::{get_seccomp_filter, Thread},
8-
ActivateError,
9-
};
6+
use crate::{get_seccomp_filter, ActivateError, Thread};
107
use seccompiler::{apply_filter, SeccompAction};
118
use std::{
129
panic::AssertUnwindSafe,

crates/vhost-user-frontend/src/vhost_user/mod.rs

+10-153
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,28 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{
5-
ActivateError, EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap,
5+
clone_queue, ActivateError, EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap,
66
GuestRegionMmap, VirtioInterrupt, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IN_ORDER,
77
VIRTIO_F_NOTIFICATION_DATA, VIRTIO_F_ORDER_PLATFORM, VIRTIO_F_RING_EVENT_IDX,
88
VIRTIO_F_RING_INDIRECT_DESC, VIRTIO_F_VERSION_1,
99
};
10-
use anyhow::anyhow;
10+
use std::fmt::Debug;
1111
use std::io;
1212
use std::ops::Deref;
1313
use std::os::unix::io::AsRawFd;
1414
use std::sync::{atomic::AtomicBool, Arc, Barrier, Mutex};
15-
use versionize::Versionize;
1615
use vhost::vhost_user::message::{
1716
VhostUserInflight, VhostUserProtocolFeatures, VhostUserVirtioFeatures,
1817
};
1918
use vhost::vhost_user::{MasterReqHandler, VhostUserMasterReqHandler};
2019
use vhost::Error as VhostError;
2120
use virtio_queue::Error as QueueError;
2221
use virtio_queue::Queue;
23-
use vm_memory::{
24-
mmap::MmapRegionError, Address, Error as MmapError, GuestAddressSpace, GuestMemory,
25-
GuestMemoryAtomic,
26-
};
27-
use vm_migration::{protocol::MemoryRangeTable, MigratableError, Snapshot, VersionMapped};
22+
use vm_memory::{mmap::MmapRegionError, Error as MmapError, GuestAddressSpace, GuestMemoryAtomic};
2823
use vmm_sys_util::eventfd::EventFd;
29-
use vu_common_ctrl::VhostUserHandle;
24+
pub(crate) use vu_common_ctrl::VhostUserHandle;
3025

31-
pub mod blk;
32-
pub mod fs;
33-
pub mod net;
3426
pub mod vu_common_ctrl;
35-
36-
pub use self::blk::Blk;
37-
pub use self::fs::*;
38-
pub use self::net::Net;
3927
pub use self::vu_common_ctrl::VhostUserConfig;
4028

4129
#[derive(Debug)]
@@ -143,7 +131,7 @@ pub enum Error {
143131
/// Could not find the shm log region
144132
MissingShmLogRegion,
145133
}
146-
type Result<T> = std::result::Result<T, Error>;
134+
pub type Result<T> = std::result::Result<T, Error>;
147135

148136
pub const DEFAULT_VIRTIO_FEATURES: u64 = 1 << VIRTIO_F_RING_INDIRECT_DESC
149137
| 1 << VIRTIO_F_RING_EVENT_IDX
@@ -225,7 +213,7 @@ impl<S: VhostUserMasterReqHandler> VhostUserEpollHandler<S> {
225213
self.mem.memory().deref(),
226214
self.queues
227215
.iter()
228-
.map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap()))
216+
.map(|(i, q, e)| (*i, clone_queue(q), e.try_clone().unwrap()))
229217
.collect(),
230218
&self.virtio_interrupt,
231219
self.acked_features,
@@ -323,7 +311,7 @@ impl VhostUserCommon {
323311
&mem.memory(),
324312
queues
325313
.iter()
326-
.map(|(i, q, e)| (*i, vm_virtio::clone_queue(q), e.try_clone().unwrap()))
314+
.map(|(i, q, e)| (*i, clone_queue(q), e.try_clone().unwrap()))
327315
.collect(),
328316
&interrupt_cb,
329317
acked_features,
@@ -378,150 +366,19 @@ impl VhostUserCommon {
378366
&mut self,
379367
guest_memory: &Option<GuestMemoryAtomic<GuestMemoryMmap>>,
380368
region: &Arc<GuestRegionMmap>,
381-
) -> std::result::Result<(), crate::Error> {
369+
) -> std::result::Result<(), Error> {
382370
if let Some(vu) = &self.vu {
383371
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits()
384372
!= 0
385373
{
386-
return vu
387-
.lock()
388-
.unwrap()
389-
.add_memory_region(region)
390-
.map_err(crate::Error::VhostUserAddMemoryRegion);
374+
return vu.lock().unwrap().add_memory_region(region);
391375
} else if let Some(guest_memory) = guest_memory {
392376
return vu
393377
.lock()
394378
.unwrap()
395-
.update_mem_table(guest_memory.memory().deref())
396-
.map_err(crate::Error::VhostUserUpdateMemory);
397-
}
398-
}
399-
Ok(())
400-
}
401-
402-
pub fn pause(&mut self) -> std::result::Result<(), MigratableError> {
403-
if let Some(vu) = &self.vu {
404-
vu.lock().unwrap().pause_vhost_user().map_err(|e| {
405-
MigratableError::Pause(anyhow!("Error pausing vhost-user-blk backend: {:?}", e))
406-
})
407-
} else {
408-
Ok(())
409-
}
410-
}
411-
412-
pub fn resume(&mut self) -> std::result::Result<(), MigratableError> {
413-
if let Some(vu) = &self.vu {
414-
vu.lock().unwrap().resume_vhost_user().map_err(|e| {
415-
MigratableError::Resume(anyhow!("Error resuming vhost-user-blk backend: {:?}", e))
416-
})
417-
} else {
418-
Ok(())
419-
}
420-
}
421-
422-
pub fn snapshot<T>(
423-
&mut self,
424-
id: &str,
425-
state: &T,
426-
) -> std::result::Result<Snapshot, MigratableError>
427-
where
428-
T: Versionize + VersionMapped,
429-
{
430-
let snapshot = Snapshot::new_from_versioned_state(id, state)?;
431-
432-
if self.migration_started {
433-
self.shutdown();
434-
}
435-
436-
Ok(snapshot)
437-
}
438-
439-
pub fn start_dirty_log(
440-
&mut self,
441-
guest_memory: &Option<GuestMemoryAtomic<GuestMemoryMmap>>,
442-
) -> std::result::Result<(), MigratableError> {
443-
if let Some(vu) = &self.vu {
444-
if let Some(guest_memory) = guest_memory {
445-
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
446-
vu.lock()
447-
.unwrap()
448-
.start_dirty_log(last_ram_addr)
449-
.map_err(|e| {
450-
MigratableError::StartDirtyLog(anyhow!(
451-
"Error starting migration for vhost-user backend: {:?}",
452-
e
453-
))
454-
})
455-
} else {
456-
Err(MigratableError::StartDirtyLog(anyhow!(
457-
"Missing guest memory"
458-
)))
379+
.update_mem_table(guest_memory.memory().deref());
459380
}
460-
} else {
461-
Ok(())
462-
}
463-
}
464-
465-
pub fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
466-
if let Some(vu) = &self.vu {
467-
vu.lock().unwrap().stop_dirty_log().map_err(|e| {
468-
MigratableError::StopDirtyLog(anyhow!(
469-
"Error stopping migration for vhost-user backend: {:?}",
470-
e
471-
))
472-
})
473-
} else {
474-
Ok(())
475381
}
476-
}
477-
478-
pub fn dirty_log(
479-
&mut self,
480-
guest_memory: &Option<GuestMemoryAtomic<GuestMemoryMmap>>,
481-
) -> std::result::Result<MemoryRangeTable, MigratableError> {
482-
if let Some(vu) = &self.vu {
483-
if let Some(guest_memory) = guest_memory {
484-
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
485-
vu.lock().unwrap().dirty_log(last_ram_addr).map_err(|e| {
486-
MigratableError::DirtyLog(anyhow!(
487-
"Error retrieving dirty ranges from vhost-user backend: {:?}",
488-
e
489-
))
490-
})
491-
} else {
492-
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
493-
}
494-
} else {
495-
Ok(MemoryRangeTable::default())
496-
}
497-
}
498-
499-
pub fn start_migration(&mut self) -> std::result::Result<(), MigratableError> {
500-
self.migration_started = true;
501-
Ok(())
502-
}
503-
504-
pub fn complete_migration(
505-
&mut self,
506-
kill_evt: Option<EventFd>,
507-
) -> std::result::Result<(), MigratableError> {
508-
self.migration_started = false;
509-
510-
// Make sure the device thread is killed in order to prevent from
511-
// reconnections to the socket.
512-
if let Some(kill_evt) = kill_evt {
513-
kill_evt.write(1).map_err(|e| {
514-
MigratableError::CompleteMigration(anyhow!(
515-
"Error killing vhost-user thread: {:?}",
516-
e
517-
))
518-
})?;
519-
}
520-
521-
// Drop the vhost-user handler to avoid further calls to fail because
522-
// the connection with the backend has been closed.
523-
self.vu = None;
524-
525382
Ok(())
526383
}
527384
}

crates/vhost-user-frontend/src/vhost_user/vu_common_ctrl.rs

-21
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use virtio_queue::{Descriptor, Queue, QueueT};
2727
use vm_memory::{
2828
Address, Error as MmapError, FileOffset, GuestAddress, GuestMemory, GuestMemoryRegion,
2929
};
30-
use vm_migration::protocol::MemoryRangeTable;
3130
use vmm_sys_util::eventfd::EventFd;
3231

3332
// Size of a dirty page for vhost-user.
@@ -559,26 +558,6 @@ impl VhostUserHandle {
559558

560559
Ok(())
561560
}
562-
563-
pub fn dirty_log(&mut self, last_ram_addr: u64) -> Result<MemoryRangeTable> {
564-
// The log region is updated by creating a new region that is sent to
565-
// the backend. This ensures the backend stops logging to the previous
566-
// region. The previous region is returned and processed to create the
567-
// bitmap representing the dirty pages.
568-
if let Some(region) = self.update_log_base(last_ram_addr)? {
569-
// Be careful with the size, as it was based on u8, meaning we must
570-
// divide it by 8.
571-
let len = region.size() / 8;
572-
let bitmap = unsafe {
573-
// Cast the pointer to u64
574-
let ptr = region.as_ptr() as *const u64;
575-
std::slice::from_raw_parts(ptr, len).to_vec()
576-
};
577-
Ok(MemoryRangeTable::from_bitmap(bitmap, 0, 4096))
578-
} else {
579-
Err(Error::MissingShmLogRegion)
580-
}
581-
}
582561
}
583562

584563
fn memfd_create(name: &ffi::CStr, flags: u32) -> std::result::Result<RawFd, std::io::Error> {

0 commit comments

Comments
 (0)