ttcore_def/
lib.rs

1//!
2//! # 基本类型定义
3//!
4
5use serde::{Deserialize, Serialize};
6use std::{
7    collections::{BTreeMap, HashMap},
8    fmt,
9};
10
11/// VM CPU 默认数量
12pub const CPU_DEFAULT: u32 = 2;
13/// VM MEM 默认容量, 单位: MB
14pub const MEM_DEFAULT: u32 = 1024;
15/// VM DISK 默认容量, 单位: MB
16pub const DISK_DEFAULT: u32 = 40 * 1024;
17
18/// Cli ID
19pub type CliId = String;
20/// Cli ID as `&str`
21pub type CliIdRef = str;
22/// Env ID
23pub type EnvId = String;
24/// Env ID as `&str`
25pub type EnvIdRef = str;
26
27/// 使用 Vm 的 MAC 地址的末尾两段的乘积, 最大值: 256 * 256
28pub type VmId = i32;
29pub type Pid = u32;
30
31/// VM 默认开放的端口(sshd)
32pub const SSH_PORT: u16 = 22;
33/// VM 默认开放的端口(ttrexec-daemon
34pub const TTREXEC_PORT: u16 = 22000;
35
36/// eg: 10.10.123.110
37#[derive(Clone, Default, Debug, Deserialize, Serialize)]
38pub struct Ipv4 {
39    addr: String,
40}
41
42impl Ipv4 {
43    /// create a new one
44    pub fn new(addr: String) -> Ipv4 {
45        Ipv4 { addr }
46    }
47
48    /// convert to string
49    pub fn as_str(&self) -> &str {
50        self.addr.as_str()
51    }
52}
53
54impl fmt::Display for Ipv4 {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{}", &self.addr)
57    }
58}
59
60/// eg: 22
61pub type Port = u16;
62/// Vm 内部视角的端口, 如 80、443 等标准端口
63pub type VmPort = Port;
64/// 外部视角的端口, 如 8080、8443 等 nat 出来的端口
65pub type PubPort = Port;
66
67/// 未来可能支持更多的容器引擎
68/// - [Y] Qemu
69/// - [Y] Bhyve
70/// - [N] FireCracker
71/// - [N] Jail
72/// - [N] Systemd Nspawn
73/// - [N] Firecracker
74#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
75#[non_exhaustive]
76#[allow(missing_docs)]
77#[serde(untagged)]
78pub enum VmKind {
79    Qemu,
80    Bhyve,
81    FireCracker,
82    Unknown,
83}
84
85impl fmt::Display for VmKind {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            VmKind::Qemu => write!(f, "Qemu"),
89            VmKind::Bhyve => write!(f, "Bhyve"),
90            VmKind::FireCracker => write!(f, "FireCracker"),
91            _ => write!(f, "Unknown"),
92        }
93    }
94}
95
96#[cfg(target_os = "linux")]
97impl Default for VmKind {
98    fn default() -> VmKind {
99        VmKind::Qemu
100    }
101}
102
103#[cfg(target_os = "freebsd")]
104impl Default for VmKind {
105    fn default() -> VmKind {
106        VmKind::Bhyve
107    }
108}
109
110#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
111impl Default for VmKind {
112    fn default() -> VmKind {
113        VmKind::Unknown
114    }
115}
116
117/// 元信息, 用于展示
118#[derive(Clone, Debug, Deserialize, Serialize)]
119pub struct EnvMeta {
120    /// 保证全局唯一
121    pub id: EnvId,
122    /// 起始时间设定之后不允许变更
123    pub start_timestamp: u64,
124    /// 结束时间可以变更, 用以控制 Vm 的生命周期
125    pub end_timestamp: u64,
126    /// 内部的 Vm 数量
127    pub vm_cnt: usize,
128    /// 是否处于停止状态
129    /// `tt env stop ...`
130    pub is_stopped: bool,
131}
132
133/// 环境实例的详细信息
134#[derive(Clone, Debug, Deserialize, Serialize)]
135pub struct EnvInfo {
136    /// 保证全局唯一
137    pub id: EnvId,
138    /// 起始时间设定之后不允许变更
139    pub start_timestamp: u64,
140    /// 结束时间可以变更, 用以控制 Vm 的生命周期
141    pub end_timestamp: u64,
142    /// 同一 Env 下所有 Vm 集合
143    pub vm: BTreeMap<VmId, VmInfo>,
144    /// 是否处于停止状态
145    /// `tt env stop ...`
146    pub is_stopped: bool,
147}
148
149/// 以此结构响应客户端请求, 防止触发 Drop 动作
150#[derive(Clone, Debug, Deserialize, Serialize)]
151pub struct VmInfo {
152    /// 系统名称
153    pub os: String,
154    /// CPU 数量
155    pub cpu_num: u32,
156    /// 单位: MB
157    pub mem_size: u32,
158    /// 单位: MB
159    pub disk_size: u32,
160    /// Vm IP 由 VmId 决定, 使用'10.10.x.x/8'网段
161    pub ip: Ipv4,
162    /// 用于 DNAT 的内外端口影射关系,
163    pub port_map: HashMap<VmPort, PubPort>,
164}