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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
pub mod error;

use crate::error::{Error, Result};
use log::{debug, trace};
use pci::PciBdf;
use tokio::time::timeout;
use tx::ClientTransaction;
use xenplatform::boot::BootSetupPlatform;
use xenplatform::domain::{BaseDomainConfig, BaseDomainManager, CreatedDomain};

use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use xencall::XenCall;
use xenstore::{XsdClient, XsdInterface};

pub mod pci;
pub mod tx;

#[derive(Clone)]
pub struct XenClient<P: BootSetupPlatform> {
    pub store: XsdClient,
    pub call: XenCall,
    domain_manager: Arc<BaseDomainManager<P>>,
}

#[derive(Clone, Debug)]
pub struct BlockDeviceRef {
    pub path: String,
    pub major: u32,
    pub minor: u32,
}

#[derive(Clone, Debug)]
pub struct DomainDisk {
    pub vdev: String,
    pub block: BlockDeviceRef,
    pub writable: bool,
}

#[derive(Clone, Debug)]
pub struct DomainFilesystem {
    pub path: String,
    pub tag: String,
}

#[derive(Clone, Debug)]
pub struct DomainNetworkInterface {
    pub mac: String,
    pub mtu: u32,
    pub bridge: Option<String>,
    pub script: Option<String>,
}

#[derive(Clone, Debug)]
pub struct DomainChannel {
    pub typ: String,
    pub initialized: bool,
}

#[derive(Clone, Debug)]
pub struct DomainEventChannel {
    pub name: String,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum DomainPciRdmReservePolicy {
    Invalid,
    #[default]
    Strict,
    Relaxed,
}

impl DomainPciRdmReservePolicy {
    pub fn to_option_str(&self) -> &str {
        match self {
            DomainPciRdmReservePolicy::Invalid => "-1",
            DomainPciRdmReservePolicy::Strict => "0",
            DomainPciRdmReservePolicy::Relaxed => "1",
        }
    }
}

#[derive(Clone, Debug)]
pub struct DomainPciDevice {
    pub bdf: PciBdf,
    pub permissive: bool,
    pub msi_translate: bool,
    pub power_management: bool,
    pub rdm_reserve_policy: DomainPciRdmReservePolicy,
}

#[derive(Clone, Debug)]
pub struct DomainConfig {
    pub base: BaseDomainConfig,
    pub backend_domid: u32,
    pub name: String,
    pub disks: Vec<DomainDisk>,
    pub swap_console_backend: Option<String>,
    pub channels: Vec<DomainChannel>,
    pub vifs: Vec<DomainNetworkInterface>,
    pub filesystems: Vec<DomainFilesystem>,
    pub pcis: Vec<DomainPciDevice>,
    pub extra_keys: Vec<(String, String)>,
    pub extra_rw_paths: Vec<String>,
}

#[derive(Debug)]
pub struct CreatedChannel {
    pub ring_ref: u64,
    pub evtchn: u32,
}

#[allow(clippy::too_many_arguments)]
impl<P: BootSetupPlatform> XenClient<P> {
    pub async fn new(current_domid: u32, platform: P) -> Result<XenClient<P>> {
        let store = XsdClient::open().await?;
        let call: XenCall = XenCall::open(current_domid)?;
        let domain_manager = BaseDomainManager::new(call.clone(), platform).await?;
        Ok(XenClient {
            store,
            call,
            domain_manager: Arc::new(domain_manager),
        })
    }

    pub async fn create(&self, config: &DomainConfig) -> Result<CreatedDomain> {
        let created = self.domain_manager.create(config.base.clone()).await?;
        match self.init(created.domid, config, &created).await {
            Ok(_) => Ok(created),
            Err(err) => {
                // ignore since destroying a domain is best
                // effort when an error occurs
                let _ = self.domain_manager.destroy(created.domid).await;
                Err(err)
            }
        }
    }

    pub async fn transaction(&self, domid: u32, backend_domid: u32) -> Result<ClientTransaction> {
        ClientTransaction::new(&self.store, domid, backend_domid).await
    }

    async fn init(&self, domid: u32, config: &DomainConfig, created: &CreatedDomain) -> Result<()> {
        trace!("xenclient init domid={} domain={:?}", domid, created);
        let transaction = self.transaction(domid, config.backend_domid).await?;
        transaction
            .add_domain_declaration(&config.name, &config.base, created)
            .await?;
        transaction.commit().await?;
        if !self
            .store
            .introduce_domain(domid, created.store_mfn, created.store_evtchn)
            .await?
        {
            return Err(Error::IntroduceDomainFailed);
        }
        let transaction = self.transaction(domid, config.backend_domid).await?;
        transaction
            .add_channel_device(
                created,
                0,
                &DomainChannel {
                    typ: config
                        .swap_console_backend
                        .clone()
                        .unwrap_or("xenconsoled".to_string())
                        .to_string(),
                    initialized: true,
                },
            )
            .await?;

        for (index, channel) in config.channels.iter().enumerate() {
            transaction
                .add_channel_device(created, index + 1, channel)
                .await?;
        }

        for (index, disk) in config.disks.iter().enumerate() {
            transaction.add_vbd_device(index, disk).await?;
        }

        for (index, filesystem) in config.filesystems.iter().enumerate() {
            transaction.add_9pfs_device(index, filesystem).await?;
        }

        for (index, vif) in config.vifs.iter().enumerate() {
            transaction.add_vif_device(index, vif).await?;
        }

        for (index, pci) in config.pcis.iter().enumerate() {
            transaction
                .add_pci_device(&self.call, index, config.pcis.len(), pci)
                .await?;
        }

        for (key, value) in &config.extra_keys {
            transaction.write_key(key, value).await?;
        }

        for key in &config.extra_rw_paths {
            transaction.add_rw_path(key).await?;
        }

        transaction.commit().await?;
        self.call.unpause_domain(domid).await?;
        Ok(())
    }

    pub async fn destroy(&self, domid: u32) -> Result<()> {
        let _ = self.destroy_store(domid).await;
        self.domain_manager.destroy(domid).await?;
        Ok(())
    }

    async fn destroy_store(&self, domid: u32) -> Result<()> {
        let dom_path = self.store.get_domain_path(domid).await?;
        let vm_path = self.store.read_string(&format!("{}/vm", dom_path)).await?;
        if vm_path.is_none() {
            return Err(Error::DomainNonExistent);
        }

        let mut backend_paths: Vec<String> = Vec::new();
        let console_frontend_path = format!("{}/console", dom_path);
        let console_backend_path = self
            .store
            .read_string(format!("{}/backend", console_frontend_path).as_str())
            .await?;

        for device_category in self
            .store
            .list(format!("{}/device", dom_path).as_str())
            .await?
        {
            for device_id in self
                .store
                .list(format!("{}/device/{}", dom_path, device_category).as_str())
                .await?
            {
                let device_path = format!("{}/device/{}/{}", dom_path, device_category, device_id);
                let Some(backend_path) = self
                    .store
                    .read_string(format!("{}/backend", device_path).as_str())
                    .await?
                else {
                    continue;
                };
                backend_paths.push(backend_path);
            }
        }

        for backend in &backend_paths {
            let state_path = format!("{}/state", backend);
            let mut watch = self.store.create_watch(&state_path).await?;
            let online_path = format!("{}/online", backend);
            let tx = self.store.transaction().await?;
            let state = tx.read_string(&state_path).await?.unwrap_or(String::new());
            if state.is_empty() {
                break;
            }
            tx.write_string(&online_path, "0").await?;
            if !state.is_empty() && u32::from_str(&state).unwrap_or(0) != 6 {
                tx.write_string(&state_path, "5").await?;
            }
            self.store.bind_watch(&watch).await?;
            tx.commit().await?;

            let mut count: u32 = 0;
            loop {
                if count >= 3 {
                    debug!("unable to safely destroy backend: {}", backend);
                    break;
                }
                let _ = timeout(Duration::from_secs(1), watch.receiver.recv()).await;
                let state = self
                    .store
                    .read_string(&state_path)
                    .await?
                    .unwrap_or_else(|| "6".to_string());
                let state = i64::from_str(&state).unwrap_or(-1);
                if state == 6 {
                    break;
                }
                count += 1;
            }
        }

        let tx = self.store.transaction().await?;
        let mut backend_removals: Vec<String> = Vec::new();
        backend_removals.extend_from_slice(backend_paths.as_slice());
        if let Some(backend) = console_backend_path {
            backend_removals.push(backend);
        }
        for path in &backend_removals {
            let path = PathBuf::from(path);
            let parent = path.parent().ok_or(Error::PathParentNotFound)?;
            tx.rm(parent.to_str().ok_or(Error::PathStringConversion)?)
                .await?;
        }
        if let Some(vm_path) = vm_path {
            tx.rm(&vm_path).await?;
        }
        tx.rm(&dom_path).await?;
        tx.commit().await?;
        Ok(())
    }
}