Skip to main content

aleph_types/message/execution/
environment.rs

1use crate::chain::Address;
2use crate::item_hash::ItemHash;
3use memsizes::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, skip_serializing_if = "Option::is_none")]
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(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, skip_serializing_if = "Option::is_none")]
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    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub vendor: Option<Vendor>,
90    /// CPU features required by the virtual machine. Examples: 'sev', 'sev_es', 'sev_snp'.
91    pub features: Vec<CpuFeature>,
92}
93
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95/// GPU device class. See <https://admin.pci-ids.ucw.cz/read/PD/03>.
96pub enum GpuDeviceClass {
97    #[serde(rename = "0300")]
98    VgaCompatibleController,
99    #[serde(rename = "0302")]
100    _3DController,
101}
102
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct GpuProperties {
105    /// GPU vendor name.
106    pub vendor: String,
107    /// GPU vendor card name.
108    pub device_name: String,
109    /// GPU device class. See <https://admin.pci-ids.ucw.cz/read/PD/03>.
110    pub device_class: GpuDeviceClass,
111    /// GPU vendor & device IDs.
112    pub device_id: String,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub enum Hypervisor {
117    #[serde(rename = "firecracker")]
118    Firecracker,
119    #[serde(rename = "qemu")]
120    Qemu,
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
124pub struct FunctionEnvironment {
125    #[serde(default)]
126    pub reproducible: bool,
127    #[serde(default)]
128    pub internet: bool,
129    #[serde(default)]
130    pub aleph_api: bool,
131    #[serde(default)]
132    pub shared_cache: bool,
133}
134
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub enum AmdSevPolicy {
137    /// Debugging of the guest is disallowed.
138    NoDebug = 0b1,
139    /// Sharing keys with other guests is disallowed.
140    NoKeySharing = 0b10,
141    /// SEV-ES is required.
142    SevEs = 0b100,
143    /// Sending the guest to another platform is disallowed.
144    NoSend = 0b1000,
145    /// The guest must not be transmitted to another platform not in the domain.
146    Domain = 0b10000,
147    /// The guest must not be transmitted to another platform that is not SEV capable.
148    Sev = 0b100000,
149}
150
151fn default_amd_sev_policy() -> u32 {
152    AmdSevPolicy::NoDebug as u32
153}
154
155#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
156pub struct TrustedExecutionEnvironment {
157    /// OVMF firmware to use.
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    pub firmware: Option<ItemHash>,
160    /// SEV Policy. The default value is 0x01 for SEV without debugging.
161    #[serde(default = "default_amd_sev_policy")]
162    pub policy: u32,
163}
164
165#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub struct InstanceEnvironment {
167    #[serde(default)]
168    pub internet: bool,
169    #[serde(default)]
170    pub aleph_api: bool,
171    /// Hypervisor to use. Default is Qemu.
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub hypervisor: Option<Hypervisor>,
174    /// Trusted Execution Environment properties. Defaults to no TEE.
175    #[serde(default, skip_serializing_if = "Option::is_none")]
176    pub trusted_execution: Option<TrustedExecutionEnvironment>,
177    // The following fields are kept for retro-compatibility.
178    #[serde(default)]
179    pub reproducible: bool,
180    #[serde(default)]
181    pub shared_cache: bool,
182}
183
184#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
185pub struct NodeRequirements {
186    /// Address of the node owner.
187    #[serde(default, skip_serializing_if = "Option::is_none")]
188    pub owner: Option<Address>,
189    /// Node address must match this regular expression.
190    #[serde(default, skip_serializing_if = "Option::is_none")]
191    pub address_regex: Option<String>,
192    /// Hash of the compute resource node that must be used.
193    #[serde(default, skip_serializing_if = "Option::is_none")]
194    pub node_hash: Option<String>,
195    /// Terms and conditions of this CRN.
196    #[serde(default, skip_serializing_if = "Option::is_none")]
197    pub terms_and_conditions: Option<ItemHash>,
198}
199
200#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
201pub struct HostRequirements {
202    /// Required CPU properties.
203    #[serde(default, skip_serializing_if = "Option::is_none")]
204    pub cpu: Option<CpuProperties>,
205    /// Required Compute Resource Node properties.
206    #[serde(default, skip_serializing_if = "Option::is_none")]
207    pub node: Option<NodeRequirements>,
208    /// GPUs needed to pass-through from the host.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    pub gpu: Option<Vec<GpuProperties>>,
211}