storvsp/
resolver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Resolver for a SCSI controller.

use super::StorageDevice;
use crate::ScsiController;
use crate::ScsiControllerDisk;
use crate::ScsiControllerState;
use crate::ScsiPathInUse;
use anyhow::Context;
use async_trait::async_trait;
use futures::StreamExt;
use pal_async::task::Spawn;
use scsi_core::ResolveScsiDeviceHandleParams;
use std::sync::Arc;
use std::sync::Weak;
use storvsp_resources::ScsiControllerHandle;
use storvsp_resources::ScsiControllerRequest;
use storvsp_resources::ScsiDeviceAndPath;
use storvsp_resources::ScsiPath;
use thiserror::Error;
use vm_resource::AsyncResolveResource;
use vm_resource::ResolveError;
use vm_resource::ResourceResolver;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::VmbusDeviceHandleKind;
use vmbus_channel::resources::ResolveVmbusDeviceHandleParams;
use vmbus_channel::resources::ResolvedVmbusDevice;
use vmcore::vm_task::VmTaskDriverSource;

/// The resolver for [`ScsiControllerHandle`].
pub struct StorvspResolver;

declare_static_async_resolver! {
    StorvspResolver,
    (VmbusDeviceHandleKind, ScsiControllerHandle),
}

/// An error returned by [`StorvspResolver`].
#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    ScsiPathInUse(ScsiPathInUse),
    #[error("failed to resolve scsi device at {path}")]
    Device {
        path: ScsiPath,
        #[source]
        source: ResolveError,
    },
}

#[async_trait]
impl AsyncResolveResource<VmbusDeviceHandleKind, ScsiControllerHandle> for StorvspResolver {
    type Output = ResolvedVmbusDevice;
    type Error = Error;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: ScsiControllerHandle,
        input: ResolveVmbusDeviceHandleParams<'_>,
    ) -> Result<Self::Output, Self::Error> {
        let controller = ScsiController::new();
        let device = StorageDevice::build_scsi(
            input.driver_source,
            &controller,
            resource.instance_id,
            resource.max_sub_channel_count,
            resource.io_queue_depth.unwrap_or(256),
        );

        for ScsiDeviceAndPath { path, device } in resource.devices {
            let device = resolver
                .resolve(
                    device,
                    ResolveScsiDeviceHandleParams {
                        driver_source: input.driver_source,
                    },
                )
                .await
                .map_err(|err| Error::Device { path, source: err })?;

            controller
                .attach(path, ScsiControllerDisk { disk: device.0 })
                .map_err(Error::ScsiPathInUse)?;
        }

        let driver = input.driver_source.simple();
        if let Some(requests) = resource.requests {
            driver
                .spawn(
                    "storvsp-requests",
                    handle_requests(
                        input.driver_source.clone(),
                        Arc::downgrade(&controller.state),
                        resolver.clone(),
                        requests,
                    ),
                )
                .detach();
        }

        Ok(device.into())
    }
}

async fn handle_requests(
    driver_source: VmTaskDriverSource,
    state: Weak<ScsiControllerState>,
    resolver: ResourceResolver,
    mut requests: mesh::Receiver<ScsiControllerRequest>,
) {
    while let Some(req) = requests.next().await {
        match req {
            ScsiControllerRequest::AddDevice(rpc) => {
                rpc.handle_failable(async |ScsiDeviceAndPath { path, device }| {
                    let device = resolver
                        .resolve(
                            device,
                            ResolveScsiDeviceHandleParams {
                                driver_source: &driver_source,
                            },
                        )
                        .await
                        .context("failed to resolve media")?;

                    if let Some(state) = state.upgrade() {
                        ScsiController { state }
                            .attach(path, ScsiControllerDisk::new(device.0))
                            .context("failed to attach device")?;
                    }
                    anyhow::Ok(())
                })
                .await
            }
            ScsiControllerRequest::RemoveDevice(rpc) => rpc.handle_failable_sync(|path| {
                if let Some(state) = state.upgrade() {
                    ScsiController { state }
                        .remove(path)
                        .context("failed to remove device")?;
                }
                anyhow::Ok(())
            }),
        }
    }
}