|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | use crate::{
|
5 |
| - ActivateError, EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap, |
| 5 | + clone_queue, ActivateError, EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap, |
6 | 6 | GuestRegionMmap, VirtioInterrupt, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IN_ORDER,
|
7 | 7 | VIRTIO_F_NOTIFICATION_DATA, VIRTIO_F_ORDER_PLATFORM, VIRTIO_F_RING_EVENT_IDX,
|
8 | 8 | VIRTIO_F_RING_INDIRECT_DESC, VIRTIO_F_VERSION_1,
|
9 | 9 | };
|
10 |
| -use anyhow::anyhow; |
| 10 | +use std::fmt::Debug; |
11 | 11 | use std::io;
|
12 | 12 | use std::ops::Deref;
|
13 | 13 | use std::os::unix::io::AsRawFd;
|
14 | 14 | use std::sync::{atomic::AtomicBool, Arc, Barrier, Mutex};
|
15 |
| -use versionize::Versionize; |
16 | 15 | use vhost::vhost_user::message::{
|
17 | 16 | VhostUserInflight, VhostUserProtocolFeatures, VhostUserVirtioFeatures,
|
18 | 17 | };
|
19 | 18 | use vhost::vhost_user::{MasterReqHandler, VhostUserMasterReqHandler};
|
20 | 19 | use vhost::Error as VhostError;
|
21 | 20 | use virtio_queue::Error as QueueError;
|
22 | 21 | 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}; |
28 | 23 | use vmm_sys_util::eventfd::EventFd;
|
29 |
| -use vu_common_ctrl::VhostUserHandle; |
| 24 | +pub(crate) use vu_common_ctrl::VhostUserHandle; |
30 | 25 |
|
31 |
| -pub mod blk; |
32 |
| -pub mod fs; |
33 |
| -pub mod net; |
34 | 26 | pub mod vu_common_ctrl;
|
35 |
| - |
36 |
| -pub use self::blk::Blk; |
37 |
| -pub use self::fs::*; |
38 |
| -pub use self::net::Net; |
39 | 27 | pub use self::vu_common_ctrl::VhostUserConfig;
|
40 | 28 |
|
41 | 29 | #[derive(Debug)]
|
@@ -143,7 +131,7 @@ pub enum Error {
|
143 | 131 | /// Could not find the shm log region
|
144 | 132 | MissingShmLogRegion,
|
145 | 133 | }
|
146 |
| -type Result<T> = std::result::Result<T, Error>; |
| 134 | +pub type Result<T> = std::result::Result<T, Error>; |
147 | 135 |
|
148 | 136 | pub const DEFAULT_VIRTIO_FEATURES: u64 = 1 << VIRTIO_F_RING_INDIRECT_DESC
|
149 | 137 | | 1 << VIRTIO_F_RING_EVENT_IDX
|
@@ -225,7 +213,7 @@ impl<S: VhostUserMasterReqHandler> VhostUserEpollHandler<S> {
|
225 | 213 | self.mem.memory().deref(),
|
226 | 214 | self.queues
|
227 | 215 | .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())) |
229 | 217 | .collect(),
|
230 | 218 | &self.virtio_interrupt,
|
231 | 219 | self.acked_features,
|
@@ -323,7 +311,7 @@ impl VhostUserCommon {
|
323 | 311 | &mem.memory(),
|
324 | 312 | queues
|
325 | 313 | .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())) |
327 | 315 | .collect(),
|
328 | 316 | &interrupt_cb,
|
329 | 317 | acked_features,
|
@@ -378,150 +366,19 @@ impl VhostUserCommon {
|
378 | 366 | &mut self,
|
379 | 367 | guest_memory: &Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
380 | 368 | region: &Arc<GuestRegionMmap>,
|
381 |
| - ) -> std::result::Result<(), crate::Error> { |
| 369 | + ) -> std::result::Result<(), Error> { |
382 | 370 | if let Some(vu) = &self.vu {
|
383 | 371 | if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits()
|
384 | 372 | != 0
|
385 | 373 | {
|
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); |
391 | 375 | } else if let Some(guest_memory) = guest_memory {
|
392 | 376 | return vu
|
393 | 377 | .lock()
|
394 | 378 | .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()); |
459 | 380 | }
|
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(()) |
475 | 381 | }
|
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 |
| - |
525 | 382 | Ok(())
|
526 | 383 | }
|
527 | 384 | }
|
0 commit comments