Skip to main content

aleph_types/message/execution/
environment.rs

1use crate::chain::Address;
2use crate::item_hash::ItemHash;
3use crate::memory_size::{MemorySize, MiB};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct FunctionTriggers {
8    /// Route HTTP requests to the program.
9    pub http: bool,
10    #[serde(default)]
11    pub persistent: Option<bool>,
12}
13
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15pub enum NetworkProtocol {
16    #[serde(rename = "tcp")]
17    Tcp,
18    #[serde(rename = "udp")]
19    Udp,
20}
21
22fn default_tcp() -> NetworkProtocol {
23    NetworkProtocol::Tcp
24}
25
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27pub struct Port(u16);
28
29/// IPv4 port to forward from a randomly assigned port on the host to the VM.
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct PublishedPort {
32    #[serde(default = "default_tcp")]
33    protocol: NetworkProtocol,
34    /// Port to expose on the guest.
35    port: Port,
36}
37
38fn default_vcpus() -> u32 {
39    1
40}
41
42fn default_memory() -> MiB {
43    MiB::from_units(128)
44}
45
46fn default_seconds() -> u32 {
47    1
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub struct MachineResources {
52    #[serde(default = "default_vcpus")]
53    pub vcpus: u32,
54    #[serde(default = "default_memory")]
55    pub memory: MiB,
56    #[serde(default = "default_seconds")]
57    pub seconds: u32,
58    /// Guest IPv4 ports to map to open ports on the host.
59    #[serde(default)]
60    pub published_ports: Option<Vec<PublishedPort>>,
61}
62
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub enum Architecture {
65    #[serde(rename = "x86_64")]
66    X86_64,
67    #[serde(rename = "arm64")]
68    Arm64,
69}
70
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72pub enum Vendor {
73    #[serde(rename = "AuthenticAMD")]
74    Amd,
75    #[serde(rename = "GenuineIntel")]
76    Intel,
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80/// CPU features required by the virtual machine. Examples: 'sev', 'sev_es', 'sev_snp'.
81pub struct CpuFeature(String);
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84pub struct CpuProperties {
85    /// CPU architecture.
86    pub architecture: Architecture,
87    /// CPU vendor.
88    pub vendor: Option<Vendor>,
89    /// CPU features required by the virtual machine. Examples: 'sev', 'sev_es', 'sev_snp'.
90    pub features: Vec<CpuFeature>,
91}
92
93#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
94/// GPU device class. See <https://admin.pci-ids.ucw.cz/read/PD/03>.
95pub enum GpuDeviceClass {
96    #[serde(rename = "0300")]
97    VgaCompatibleController,
98    #[serde(rename = "0302")]
99    _3DController,
100}
101
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103pub struct GpuProperties {
104    /// GPU vendor name.
105    pub vendor: String,
106    /// GPU vendor card name.
107    pub device_name: String,
108    /// GPU device class. See <https://admin.pci-ids.ucw.cz/read/PD/03>.
109    pub device_class: GpuDeviceClass,
110    /// GPU vendor & device IDs.
111    pub device_id: String,
112}
113
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
115pub enum Hypervisor {
116    #[serde(rename = "firecracker")]
117    Firecracker,
118    #[serde(rename = "qemu")]
119    Qemu,
120}
121
122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
123pub struct FunctionEnvironment {
124    #[serde(default)]
125    pub reproducible: bool,
126    #[serde(default)]
127    pub internet: bool,
128    #[serde(default)]
129    pub aleph_api: bool,
130    #[serde(default)]
131    pub shared_cache: bool,
132}
133
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135pub enum AmdSevPolicy {
136    /// Debugging of the guest is disallowed.
137    NoDebug = 0b1,
138    /// Sharing keys with other guests is disallowed.
139    NoKeySharing = 0b10,
140    /// SEV-ES is required.
141    SevEs = 0b100,
142    /// Sending the guest to another platform is disallowed.
143    NoSend = 0b1000,
144    /// The guest must not be transmitted to another platform not in the domain.
145    Domain = 0b10000,
146    /// The guest must not be transmitted to another platform that is not SEV capable.
147    Sev = 0b100000,
148}
149
150fn default_amd_sev_policy() -> u32 {
151    AmdSevPolicy::NoDebug as u32
152}
153
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
155pub struct TrustedExecutionEnvironment {
156    /// OVMF firmware to use.
157    pub firmware: Option<ItemHash>,
158    /// SEV Policy. The default value is 0x01 for SEV without debugging.
159    #[serde(default = "default_amd_sev_policy")]
160    pub policy: u32,
161}
162
163#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
164pub struct InstanceEnvironment {
165    #[serde(default)]
166    pub internet: bool,
167    #[serde(default)]
168    pub aleph_api: bool,
169    /// Hypervisor to use. Default is Qemu.
170    pub hypervisor: Option<Hypervisor>,
171    /// Trusted Execution Environment properties. Defaults to no TEE.
172    pub trusted_execution: Option<TrustedExecutionEnvironment>,
173    // The following fields are kept for retro-compatibility.
174    #[serde(default)]
175    pub reproducible: bool,
176    #[serde(default)]
177    pub shared_cache: bool,
178}
179
180#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
181pub struct NodeRequirements {
182    /// Address of the node owner.
183    pub owner: Option<Address>,
184    /// Node address must match this regular expression.
185    pub address_regex: Option<String>,
186    /// Hash of the compute resource node that must be used.
187    pub node_hash: Option<String>,
188    /// Terms and conditions of this CRN.
189    pub terms_and_conditions: Option<ItemHash>,
190}
191
192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
193pub struct HostRequirements {
194    /// Required CPU properties.
195    pub cpu: Option<CpuProperties>,
196    /// Required Compute Resource Node properties.
197    pub node: Option<NodeRequirements>,
198    /// GPUs needed to pass-through from the host.
199    pub gpu: Option<Vec<GpuProperties>>,
200}