Path: blob/main/devices/src/virtio/vhost_user_backend/block.rs
5394 views
// Copyright 2021 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34mod sys;56use anyhow::Context;7use cros_async::Executor;8use serde::Deserialize;9use serde::Serialize;10use snapshot::AnySnapshot;11pub use sys::start_device as run_block_device;12pub use sys::Options;13use vm_memory::GuestMemory;14use vmm_vhost::message::*;1516use crate::virtio;17use crate::virtio::block::asynchronous::BlockAsync;18use crate::virtio::vhost_user_backend::handler::DeviceRequestHandler;19use crate::virtio::vhost_user_backend::handler::VhostUserDevice;20use crate::virtio::vhost_user_backend::VhostUserDeviceBuilder;21use crate::virtio::VirtioDevice;2223const NUM_QUEUES: u16 = 16;2425struct BlockBackend {26inner: Box<BlockAsync>,2728avail_features: u64,29}3031#[derive(Serialize, Deserialize)]32struct BlockBackendSnapshot {33// `avail_features` don't need to be snapshotted, but they are34// to be used to make sure that the proper features are used on `restore`.35avail_features: u64,36}3738impl VhostUserDeviceBuilder for BlockAsync {39fn build(self: Box<Self>, _ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>> {40let avail_features = self.features() | 1 << VHOST_USER_F_PROTOCOL_FEATURES;41let backend = BlockBackend {42inner: self,43avail_features,44};45let handler = DeviceRequestHandler::new(backend);46Ok(Box::new(handler))47}48}4950impl VhostUserDevice for BlockBackend {51fn max_queue_num(&self) -> usize {52NUM_QUEUES as usize53}5455fn features(&self) -> u64 {56self.avail_features57}5859fn protocol_features(&self) -> VhostUserProtocolFeatures {60VhostUserProtocolFeatures::CONFIG61| VhostUserProtocolFeatures::MQ62| VhostUserProtocolFeatures::BACKEND_REQ63| VhostUserProtocolFeatures::DEVICE_STATE64}6566fn read_config(&self, offset: u64, data: &mut [u8]) {67self.inner.read_config(offset, data)68}6970fn reset(&mut self) {71if let Err(e) = self.inner.reset() {72base::error!("reset failed: {:#}", e);73}74}7576fn start_queue(77&mut self,78idx: usize,79queue: virtio::Queue,80mem: GuestMemory,81) -> anyhow::Result<()> {82self.inner.start_queue(idx, queue, mem)83}8485fn stop_queue(&mut self, idx: usize) -> anyhow::Result<virtio::Queue> {86self.inner.stop_queue(idx)87}8889fn enter_suspended_state(&mut self) -> anyhow::Result<()> {90// TODO: This assumes that `reset` only stops workers which might not be true in the91// future. Consider moving the `reset` code into a `stop_all_workers` method or, maybe,92// make `stop_queue` implicitly stop a worker thread when there is no active queue.93self.inner.reset()?;94Ok(())95}9697fn snapshot(&mut self) -> anyhow::Result<AnySnapshot> {98// The queue states are being snapshotted in the device handler.99AnySnapshot::to_any(BlockBackendSnapshot {100avail_features: self.avail_features,101})102.context("Failed to serialize BlockBackendSnapshot")103}104105fn restore(&mut self, data: AnySnapshot) -> anyhow::Result<()> {106let block_backend_snapshot: BlockBackendSnapshot =107AnySnapshot::from_any(data).context("Failed to deserialize BlockBackendSnapshot")?;108anyhow::ensure!(109self.avail_features == block_backend_snapshot.avail_features,110"Vhost user block restored avail_features do not match. Live: {:?}, snapshot: {:?}",111self.avail_features,112block_backend_snapshot.avail_features,113);114Ok(())115}116}117118119