a3s-box-core 2.4.0

Core types, config, and error handling for A3S Box MicroVM runtime
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Platform types for multi-architecture image builds.
//!
//! Represents target OS/architecture pairs used by Buildx-style
//! multi-platform builds and OCI Image Index manifests.

use serde::{Deserialize, Serialize};
use std::fmt;

/// A target platform (OS + architecture).
///
/// Used for multi-platform builds and OCI Image Index entries.
/// Compatible with Docker/OCI platform specification.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Platform {
    /// Operating system (e.g., "linux").
    pub os: String,
    /// CPU architecture (e.g., "amd64", "arm64").
    pub architecture: String,
    /// Optional variant (e.g., "v7" for armv7).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub variant: Option<String>,
}

impl Platform {
    /// Create a new platform.
    pub fn new(os: impl Into<String>, architecture: impl Into<String>) -> Self {
        Self {
            os: os.into(),
            architecture: architecture.into(),
            variant: None,
        }
    }

    /// Create a platform with a variant.
    pub fn with_variant(
        os: impl Into<String>,
        architecture: impl Into<String>,
        variant: impl Into<String>,
    ) -> Self {
        Self {
            os: os.into(),
            architecture: architecture.into(),
            variant: Some(variant.into()),
        }
    }

    /// linux/amd64
    pub fn linux_amd64() -> Self {
        Self::new("linux", "amd64")
    }

    /// linux/arm64
    pub fn linux_arm64() -> Self {
        Self::new("linux", "arm64")
    }

    /// Detect the current host platform.
    pub fn host() -> Self {
        let arch = match std::env::consts::ARCH {
            "x86_64" => "amd64",
            "aarch64" => "arm64",
            other => other,
        };
        let os = match std::env::consts::OS {
            "macos" => "darwin",
            other => other,
        };
        Self::new(os, arch)
    }

    /// Parse a platform string like "linux/amd64" or "linux/arm/v7".
    pub fn parse(s: &str) -> Result<Self, String> {
        let parts: Vec<&str> = s.split('/').collect();
        match parts.len() {
            2 => {
                let arch = normalize_arch(parts[1]);
                Ok(Self::new(parts[0], arch))
            }
            3 => {
                let arch = normalize_arch(parts[1]);
                Ok(Self::with_variant(parts[0], arch, parts[2]))
            }
            _ => Err(format!(
                "Invalid platform '{}': expected 'os/arch' or 'os/arch/variant'",
                s
            )),
        }
    }

    /// Parse a comma-separated list of platforms.
    ///
    /// Example: "linux/amd64,linux/arm64"
    pub fn parse_list(s: &str) -> Result<Vec<Self>, String> {
        s.split(',').map(|p| Self::parse(p.trim())).collect()
    }

    /// Check if this platform matches the host architecture.
    pub fn is_native(&self) -> bool {
        *self == Self::host()
    }

    /// Get the OCI architecture string.
    pub fn oci_arch(&self) -> &str {
        &self.architecture
    }
}

impl fmt::Display for Platform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.os, self.architecture)?;
        if let Some(ref v) = self.variant {
            write!(f, "/{}", v)?;
        }
        Ok(())
    }
}

/// Runtime capabilities exposed by the current host OS.
///
/// This is intentionally separate from [`Platform`], which represents an OCI
/// image platform. Capabilities describe what the host can execute directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlatformCapabilities {
    /// Host OS as reported by Rust.
    pub os: String,
    /// Host architecture in OCI naming.
    pub architecture: String,
    /// VM backend used on this host.
    pub vm_backend: VmBackend,
    /// Host/guest control channel available on this host.
    pub host_guest_channel: HostGuestChannel,
    /// Whether Unix domain sockets are available.
    pub unix_sockets: bool,
    /// Whether Windows named pipes are available.
    pub named_pipes: bool,
    /// Whether the native network proxy is available.
    pub netproxy: bool,
    /// Bridge network backend available on this host.
    pub bridge_network_backend: BridgeNetworkBackend,
    /// Whether user-defined bridge networks can provide guest outbound NAT.
    pub bridge_outbound_nat: bool,
    /// Whether published ports can be bridged on this host.
    pub published_ports: bool,
    /// Whether TEE attestation is available in the native runtime.
    pub tee_attestation: bool,
    /// Whether sealed storage is available in the native runtime.
    pub sealed_storage: bool,
    /// Whether this host can run interactive PTY sessions through the native control channel.
    pub interactive_pty: bool,
}

/// VM backend selected for the current host.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum VmBackend {
    /// Native libkrun-backed VM execution.
    Krun,
    /// Native Windows Hypervisor Platform backend through libkrun.
    Whpx,
    /// No supported VM backend for this host.
    Unsupported,
}

impl fmt::Display for VmBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Krun => write!(f, "krun"),
            Self::Whpx => write!(f, "whpx"),
            Self::Unsupported => write!(f, "unsupported"),
        }
    }
}

/// Host/guest control transport available on the current host.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HostGuestChannel {
    /// Unix domain sockets.
    UnixSocket,
    /// Windows named pipes.
    NamedPipe,
    /// No supported channel.
    Unsupported,
}

impl fmt::Display for HostGuestChannel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnixSocket => write!(f, "unix-socket"),
            Self::NamedPipe => write!(f, "named-pipe"),
            Self::Unsupported => write!(f, "unsupported"),
        }
    }
}

/// User-defined bridge network backend selected for the current host.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum BridgeNetworkBackend {
    /// Linux `passt` backend.
    Passt,
    /// macOS pure-Rust vfkit netproxy backend.
    Netproxy,
    /// No bridge backend is available.
    Unsupported,
}

impl fmt::Display for BridgeNetworkBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Passt => write!(f, "passt"),
            Self::Netproxy => write!(f, "netproxy"),
            Self::Unsupported => write!(f, "unsupported"),
        }
    }
}

impl PlatformCapabilities {
    /// Detect capabilities for the current host.
    pub fn current() -> Self {
        let host = Platform::host();

        Self {
            os: std::env::consts::OS.to_string(),
            architecture: host.architecture,
            vm_backend: current_vm_backend(),
            host_guest_channel: current_host_guest_channel(),
            unix_sockets: cfg!(unix),
            named_pipes: cfg!(windows),
            netproxy: cfg!(target_os = "macos"),
            bridge_network_backend: current_bridge_network_backend(),
            bridge_outbound_nat: cfg!(target_os = "linux"),
            published_ports: cfg!(unix) || cfg!(windows),
            tee_attestation: cfg!(unix),
            sealed_storage: cfg!(unix),
            interactive_pty: cfg!(unix),
        }
    }

    /// Whether the host can run the VM runtime directly.
    pub fn supports_native_vm(&self) -> bool {
        matches!(self.vm_backend, VmBackend::Krun | VmBackend::Whpx)
    }

    /// Whether the host has a supported control channel.
    pub fn supports_host_guest_channel(&self) -> bool {
        self.host_guest_channel != HostGuestChannel::Unsupported
    }

    /// Whether user-defined bridge networking is available.
    pub fn supports_bridge_networking(&self) -> bool {
        self.bridge_network_backend != BridgeNetworkBackend::Unsupported
    }

    /// Human-readable bridge networking mode summary for diagnostics.
    pub fn bridge_networking_summary(&self) -> String {
        match self.bridge_network_backend {
            BridgeNetworkBackend::Passt => {
                "passt (peer networking and outbound NAT supported)".to_string()
            }
            BridgeNetworkBackend::Netproxy => {
                "netproxy (peer networking supported; outbound NAT unsupported)".to_string()
            }
            BridgeNetworkBackend::Unsupported => "unsupported".to_string(),
        }
    }
}

#[cfg(unix)]
fn current_vm_backend() -> VmBackend {
    VmBackend::Krun
}

#[cfg(windows)]
fn current_vm_backend() -> VmBackend {
    VmBackend::Whpx
}

#[cfg(not(any(unix, windows)))]
fn current_vm_backend() -> VmBackend {
    VmBackend::Unsupported
}

#[cfg(unix)]
fn current_host_guest_channel() -> HostGuestChannel {
    HostGuestChannel::UnixSocket
}

#[cfg(windows)]
fn current_host_guest_channel() -> HostGuestChannel {
    HostGuestChannel::NamedPipe
}

#[cfg(not(any(unix, windows)))]
fn current_host_guest_channel() -> HostGuestChannel {
    HostGuestChannel::Unsupported
}

#[cfg(target_os = "linux")]
fn current_bridge_network_backend() -> BridgeNetworkBackend {
    BridgeNetworkBackend::Passt
}

#[cfg(target_os = "macos")]
fn current_bridge_network_backend() -> BridgeNetworkBackend {
    BridgeNetworkBackend::Netproxy
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn current_bridge_network_backend() -> BridgeNetworkBackend {
    BridgeNetworkBackend::Unsupported
}

/// Normalize architecture names to OCI conventions.
fn normalize_arch(arch: &str) -> String {
    match arch {
        "x86_64" | "x86-64" => "amd64".to_string(),
        "aarch64" | "arm64v8" => "arm64".to_string(),
        "armhf" | "armv7l" => "arm".to_string(),
        "i386" | "i686" | "x86" => "386".to_string(),
        other => other.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_platform_new() {
        let p = Platform::new("linux", "amd64");
        assert_eq!(p.os, "linux");
        assert_eq!(p.architecture, "amd64");
        assert!(p.variant.is_none());
    }

    #[test]
    fn test_platform_with_variant() {
        let p = Platform::with_variant("linux", "arm", "v7");
        assert_eq!(p.variant, Some("v7".to_string()));
    }

    #[test]
    fn test_platform_display() {
        assert_eq!(Platform::linux_amd64().to_string(), "linux/amd64");
        assert_eq!(Platform::linux_arm64().to_string(), "linux/arm64");
        assert_eq!(
            Platform::with_variant("linux", "arm", "v7").to_string(),
            "linux/arm/v7"
        );
    }

    #[test]
    fn test_platform_parse() {
        let p = Platform::parse("linux/amd64").unwrap();
        assert_eq!(p, Platform::linux_amd64());

        let p = Platform::parse("linux/arm64").unwrap();
        assert_eq!(p, Platform::linux_arm64());

        let p = Platform::parse("linux/arm/v7").unwrap();
        assert_eq!(p.architecture, "arm");
        assert_eq!(p.variant, Some("v7".to_string()));
    }

    #[test]
    fn test_platform_parse_normalizes() {
        let p = Platform::parse("linux/x86_64").unwrap();
        assert_eq!(p.architecture, "amd64");

        let p = Platform::parse("linux/aarch64").unwrap();
        assert_eq!(p.architecture, "arm64");
    }

    #[test]
    fn test_platform_parse_invalid() {
        assert!(Platform::parse("linux").is_err());
        assert!(Platform::parse("a/b/c/d").is_err());
    }

    #[test]
    fn test_platform_parse_list() {
        let platforms = Platform::parse_list("linux/amd64,linux/arm64").unwrap();
        assert_eq!(platforms.len(), 2);
        assert_eq!(platforms[0], Platform::linux_amd64());
        assert_eq!(platforms[1], Platform::linux_arm64());
    }

    #[test]
    fn test_platform_parse_list_with_spaces() {
        let platforms = Platform::parse_list("linux/amd64, linux/arm64").unwrap();
        assert_eq!(platforms.len(), 2);
    }

    #[test]
    fn test_platform_host() {
        let host = Platform::host();
        let expected_os = match std::env::consts::OS {
            "macos" => "darwin",
            other => other,
        };
        assert_eq!(host.os, expected_os);
        assert!(host.architecture == "amd64" || host.architecture == "arm64");
    }

    #[test]
    fn test_platform_is_native() {
        let host = Platform::host();
        assert!(host.is_native());
        // The opposite arch should not be native
        let other = if host.architecture == "amd64" {
            Platform::linux_arm64()
        } else {
            Platform::linux_amd64()
        };
        assert!(!other.is_native());
    }

    #[test]
    fn test_platform_serde_roundtrip() {
        let p = Platform::linux_amd64();
        let json = serde_json::to_string(&p).unwrap();
        let parsed: Platform = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, p);
    }

    #[test]
    fn test_platform_serde_with_variant() {
        let p = Platform::with_variant("linux", "arm", "v7");
        let json = serde_json::to_string(&p).unwrap();
        let parsed: Platform = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, p);
        assert_eq!(parsed.variant, Some("v7".to_string()));
    }

    #[test]
    fn test_normalize_arch() {
        assert_eq!(normalize_arch("x86_64"), "amd64");
        assert_eq!(normalize_arch("aarch64"), "arm64");
        assert_eq!(normalize_arch("armhf"), "arm");
        assert_eq!(normalize_arch("i386"), "386");
        assert_eq!(normalize_arch("riscv64"), "riscv64");
    }

    #[test]
    fn test_platform_equality() {
        assert_eq!(Platform::linux_amd64(), Platform::new("linux", "amd64"));
        assert_ne!(Platform::linux_amd64(), Platform::linux_arm64());
    }

    #[test]
    fn test_platform_capabilities_match_host() {
        let capabilities = PlatformCapabilities::current();
        assert_eq!(capabilities.os, std::env::consts::OS);
        assert!(capabilities.supports_host_guest_channel());

        #[cfg(unix)]
        {
            assert_eq!(capabilities.vm_backend, VmBackend::Krun);
            assert_eq!(
                capabilities.host_guest_channel,
                HostGuestChannel::UnixSocket
            );
            assert!(capabilities.unix_sockets);
            assert!(!capabilities.named_pipes);
            assert!(capabilities.supports_native_vm());
            assert!(capabilities.supports_bridge_networking());
        }

        #[cfg(windows)]
        {
            assert_eq!(capabilities.vm_backend, VmBackend::Whpx);
            assert_eq!(capabilities.host_guest_channel, HostGuestChannel::NamedPipe);
            assert!(!capabilities.unix_sockets);
            assert!(capabilities.named_pipes);
            assert!(capabilities.supports_native_vm());
            assert!(!capabilities.interactive_pty);
        }
    }

    #[test]
    fn test_platform_capability_display_values() {
        assert_eq!(VmBackend::Krun.to_string(), "krun");
        assert_eq!(VmBackend::Whpx.to_string(), "whpx");
        assert_eq!(HostGuestChannel::UnixSocket.to_string(), "unix-socket");
        assert_eq!(HostGuestChannel::NamedPipe.to_string(), "named-pipe");
        assert_eq!(BridgeNetworkBackend::Netproxy.to_string(), "netproxy");
    }

    #[test]
    fn test_bridge_networking_summary_documents_nat_boundary() {
        let mut capabilities = PlatformCapabilities::current();
        capabilities.bridge_network_backend = BridgeNetworkBackend::Netproxy;
        capabilities.bridge_outbound_nat = false;

        let summary = capabilities.bridge_networking_summary();

        assert!(summary.contains("netproxy"));
        assert!(summary.contains("outbound NAT unsupported"));
    }
}