fuchsia_storage_benchmarks_lib/filesystems/
pkgdir.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::filesystems::{BlobFilesystem, Blobfs, CacheClearableFilesystem, DeliveryBlob, Fxblob};
use async_trait::async_trait;
use fidl::endpoints::DiscoverableProtocolMarker;
use fidl_fuchsia_io as fio;
use fuchsia_component_test::{Capability, ChildOptions, RealmBuilder, RealmInstance, Ref, Route};
use futures::future::FutureExt;
use std::path::Path;
use storage_benchmarks::{BlockDeviceFactory, Filesystem, FilesystemConfig};
use vfs::directory::entry_container::Directory as _;
/// Config object for starting a `PkgDirInstance`. The `PkgDirInstance` allows blob benchmarks to
/// open and read a blob through its package directory as opposed to talking directly to the
/// filesystem.
#[derive(Clone)]
pub struct PkgDirTest {
    use_fxblob: bool,
}

impl PkgDirTest {
    pub fn new_fxblob() -> Self {
        PkgDirTest { use_fxblob: true }
    }

    pub fn new_blobfs() -> Self {
        PkgDirTest { use_fxblob: false }
    }
}

#[async_trait]
impl FilesystemConfig for PkgDirTest {
    type Filesystem = PkgDirInstance;

    async fn start_filesystem(
        &self,
        block_device_factory: &dyn BlockDeviceFactory,
    ) -> PkgDirInstance {
        let fs = if self.use_fxblob {
            Box::new(Fxblob.start_filesystem(block_device_factory).await) as Box<dyn BlobFilesystem>
        } else {
            Box::new(Blobfs.start_filesystem(block_device_factory).await) as Box<dyn BlobFilesystem>
        };

        let (clone, server_end) = fidl::endpoints::create_proxy::<fio::DirectoryMarker>();
        fs.exposed_dir()
            .clone2(server_end.into_channel().into())
            .expect("connect to blob volume exposed dir");
        let realm = PkgDirRealm::new(self.use_fxblob, clone).await;
        PkgDirInstance { fs, realm, use_fxblob: self.use_fxblob }
    }

    fn name(&self) -> String {
        let fs = if self.use_fxblob { "fxblob" } else { "blobfs" };
        format!("{}-pkgdir", fs)
    }
}

pub struct PkgDirInstance {
    fs: Box<dyn BlobFilesystem>,
    realm: PkgDirRealm,
    use_fxblob: bool,
}

impl PkgDirInstance {
    pub fn pkgdir_proxy(&self) -> fidl_test_pkgdir::PkgDirProxy {
        self.realm
            .realm()
            .root
            .connect_to_protocol_at_exposed_dir::<fidl_test_pkgdir::PkgDirMarker>()
            .unwrap()
    }
}

#[async_trait]
impl Filesystem for PkgDirInstance {
    async fn shutdown(self) {
        self.fs.shutdown_boxed().await
    }

    fn benchmark_dir(&self) -> &Path {
        self.fs.benchmark_dir()
    }
}

#[async_trait]
impl CacheClearableFilesystem for PkgDirInstance {
    async fn clear_cache(&mut self) {
        self.fs.clear_cache().await;
        let (clone, server_end) = fidl::endpoints::create_proxy::<fio::DirectoryMarker>();
        self.fs
            .exposed_dir()
            .clone2(server_end.into_channel().into())
            .expect("connect to blob volume exposed dir");
        self.realm = PkgDirRealm::new(self.use_fxblob, clone).await
    }
}

#[async_trait]
impl BlobFilesystem for PkgDirInstance {
    async fn get_vmo(&self, blob: &DeliveryBlob) -> zx::Vmo {
        self.fs.get_vmo(blob).await
    }

    async fn write_blob(&self, blob: &DeliveryBlob) {
        self.fs.write_blob(blob).await
    }

    fn exposed_dir(&self) -> &fio::DirectoryProxy {
        self.fs.exposed_dir()
    }
}

pub struct PkgDirRealm {
    pub realm: RealmInstance,
}

impl PkgDirRealm {
    pub async fn new(fxblob: bool, exposed_dir: fio::DirectoryProxy) -> Self {
        let builder = RealmBuilder::new().await.unwrap();
        let pkgdir = builder
            .add_child("pkgdir-component", "#meta/pkgdir-component.cm", ChildOptions::new())
            .await
            .unwrap();
        builder.init_mutable_config_from_package(&pkgdir).await.unwrap();
        let exposed_dir = vfs::pseudo_directory! {
            "blob" => vfs::remote::remote_dir(exposed_dir),
        };
        let service_reflector = builder
            .add_local_child(
                "service_reflector",
                move |handles| {
                    let scope = vfs::execution_scope::ExecutionScope::new();
                    let () = exposed_dir.clone().open(
                        scope.clone(),
                        fio::OpenFlags::RIGHT_READABLE,
                        vfs::path::Path::dot(),
                        handles.outgoing_dir.into_channel().into(),
                    );
                    async move {
                        scope.wait().await;
                        Ok(())
                    }
                    .boxed()
                },
                ChildOptions::new(),
            )
            .await
            .unwrap();
        builder.set_config_value(&pkgdir, "use_fxblob", fxblob.into()).await.unwrap();
        builder
            .add_route(
                Route::new()
                    .capability(Capability::protocol::<fidl_test_pkgdir::PkgDirMarker>())
                    .from(&pkgdir)
                    .to(Ref::parent()),
            )
            .await
            .unwrap();
        builder
            .add_route(
                Route::new()
                    .capability(
                        Capability::protocol::<fidl_fuchsia_tracing_provider::RegistryMarker>(),
                    )
                    .from(Ref::parent())
                    .to(&pkgdir),
            )
            .await
            .unwrap();
        builder
            .add_route(
                Route::new()
                    .capability(
                        Capability::directory("blob-exec")
                            .path("/blob/root")
                            .rights(fio::R_STAR_DIR),
                    )
                    .from(&service_reflector)
                    .to(&pkgdir),
            )
            .await
            .unwrap();
        if fxblob {
            builder
                .add_route(
                    Route::new()
                        .capability(
                            Capability::protocol::<fidl_fuchsia_fxfs::BlobReaderMarker>().path(
                                format!(
                                    "/blob/svc/{}",
                                    fidl_fuchsia_fxfs::BlobReaderMarker::PROTOCOL_NAME
                                ),
                            ),
                        )
                        .from(&service_reflector)
                        .to(&pkgdir),
                )
                .await
                .unwrap();
        }
        let realm = builder.build().await.expect("realm build failed");
        Self { realm }
    }

    fn realm(&self) -> &RealmInstance {
        &self.realm
    }
}