]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-api-types/src/network.rs
update to first proxmox crate split
[proxmox-backup.git] / pbs-api-types / src / network.rs
1 use serde::{Deserialize, Serialize};
2
3 use proxmox_schema::*;
4
5 use crate::{
6 PROXMOX_SAFE_ID_REGEX,
7 IP_V4_FORMAT, IP_V6_FORMAT, IP_FORMAT,
8 CIDR_V4_FORMAT, CIDR_V6_FORMAT, CIDR_FORMAT,
9 };
10
11 pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat =
12 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
13
14 pub const IP_V4_SCHEMA: Schema =
15 StringSchema::new("IPv4 address.")
16 .format(&IP_V4_FORMAT)
17 .max_length(15)
18 .schema();
19
20 pub const IP_V6_SCHEMA: Schema =
21 StringSchema::new("IPv6 address.")
22 .format(&IP_V6_FORMAT)
23 .max_length(39)
24 .schema();
25
26 pub const IP_SCHEMA: Schema =
27 StringSchema::new("IP (IPv4 or IPv6) address.")
28 .format(&IP_FORMAT)
29 .max_length(39)
30 .schema();
31
32 pub const CIDR_V4_SCHEMA: Schema =
33 StringSchema::new("IPv4 address with netmask (CIDR notation).")
34 .format(&CIDR_V4_FORMAT)
35 .max_length(18)
36 .schema();
37
38 pub const CIDR_V6_SCHEMA: Schema =
39 StringSchema::new("IPv6 address with netmask (CIDR notation).")
40 .format(&CIDR_V6_FORMAT)
41 .max_length(43)
42 .schema();
43
44 pub const CIDR_SCHEMA: Schema =
45 StringSchema::new("IP address (IPv4 or IPv6) with netmask (CIDR notation).")
46 .format(&CIDR_FORMAT)
47 .max_length(43)
48 .schema();
49
50 #[api()]
51 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
52 #[serde(rename_all = "lowercase")]
53 /// Interface configuration method
54 pub enum NetworkConfigMethod {
55 /// Configuration is done manually using other tools
56 Manual,
57 /// Define interfaces with statically allocated addresses.
58 Static,
59 /// Obtain an address via DHCP
60 DHCP,
61 /// Define the loopback interface.
62 Loopback,
63 }
64
65 #[api()]
66 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
67 #[serde(rename_all = "kebab-case")]
68 #[allow(non_camel_case_types)]
69 #[repr(u8)]
70 /// Linux Bond Mode
71 pub enum LinuxBondMode {
72 /// Round-robin policy
73 balance_rr = 0,
74 /// Active-backup policy
75 active_backup = 1,
76 /// XOR policy
77 balance_xor = 2,
78 /// Broadcast policy
79 broadcast = 3,
80 /// IEEE 802.3ad Dynamic link aggregation
81 #[serde(rename = "802.3ad")]
82 ieee802_3ad = 4,
83 /// Adaptive transmit load balancing
84 balance_tlb = 5,
85 /// Adaptive load balancing
86 balance_alb = 6,
87 }
88
89 #[api()]
90 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
91 #[serde(rename_all = "kebab-case")]
92 #[allow(non_camel_case_types)]
93 #[repr(u8)]
94 /// Bond Transmit Hash Policy for LACP (802.3ad)
95 pub enum BondXmitHashPolicy {
96 /// Layer 2
97 layer2 = 0,
98 /// Layer 2+3
99 #[serde(rename = "layer2+3")]
100 layer2_3 = 1,
101 /// Layer 3+4
102 #[serde(rename = "layer3+4")]
103 layer3_4 = 2,
104 }
105
106 #[api()]
107 #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
108 #[serde(rename_all = "lowercase")]
109 /// Network interface type
110 pub enum NetworkInterfaceType {
111 /// Loopback
112 Loopback,
113 /// Physical Ethernet device
114 Eth,
115 /// Linux Bridge
116 Bridge,
117 /// Linux Bond
118 Bond,
119 /// Linux VLAN (eth.10)
120 Vlan,
121 /// Interface Alias (eth:1)
122 Alias,
123 /// Unknown interface type
124 Unknown,
125 }
126
127 pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network interface name.")
128 .format(&NETWORK_INTERFACE_FORMAT)
129 .min_length(1)
130 .max_length(libc::IFNAMSIZ-1)
131 .schema();
132
133 pub const NETWORK_INTERFACE_ARRAY_SCHEMA: Schema = ArraySchema::new(
134 "Network interface list.", &NETWORK_INTERFACE_NAME_SCHEMA)
135 .schema();
136
137 pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = StringSchema::new(
138 "A list of network devices, comma separated.")
139 .format(&ApiStringFormat::PropertyString(&NETWORK_INTERFACE_ARRAY_SCHEMA))
140 .schema();
141
142 #[api(
143 properties: {
144 name: {
145 schema: NETWORK_INTERFACE_NAME_SCHEMA,
146 },
147 "type": {
148 type: NetworkInterfaceType,
149 },
150 method: {
151 type: NetworkConfigMethod,
152 optional: true,
153 },
154 method6: {
155 type: NetworkConfigMethod,
156 optional: true,
157 },
158 cidr: {
159 schema: CIDR_V4_SCHEMA,
160 optional: true,
161 },
162 cidr6: {
163 schema: CIDR_V6_SCHEMA,
164 optional: true,
165 },
166 gateway: {
167 schema: IP_V4_SCHEMA,
168 optional: true,
169 },
170 gateway6: {
171 schema: IP_V6_SCHEMA,
172 optional: true,
173 },
174 options: {
175 description: "Option list (inet)",
176 type: Array,
177 items: {
178 description: "Optional attribute line.",
179 type: String,
180 },
181 },
182 options6: {
183 description: "Option list (inet6)",
184 type: Array,
185 items: {
186 description: "Optional attribute line.",
187 type: String,
188 },
189 },
190 comments: {
191 description: "Comments (inet, may span multiple lines)",
192 type: String,
193 optional: true,
194 },
195 comments6: {
196 description: "Comments (inet6, may span multiple lines)",
197 type: String,
198 optional: true,
199 },
200 bridge_ports: {
201 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
202 optional: true,
203 },
204 slaves: {
205 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
206 optional: true,
207 },
208 bond_mode: {
209 type: LinuxBondMode,
210 optional: true,
211 },
212 "bond-primary": {
213 schema: NETWORK_INTERFACE_NAME_SCHEMA,
214 optional: true,
215 },
216 bond_xmit_hash_policy: {
217 type: BondXmitHashPolicy,
218 optional: true,
219 },
220 }
221 )]
222 #[derive(Debug, Serialize, Deserialize)]
223 /// Network Interface configuration
224 pub struct Interface {
225 /// Autostart interface
226 #[serde(rename = "autostart")]
227 pub autostart: bool,
228 /// Interface is active (UP)
229 pub active: bool,
230 /// Interface name
231 pub name: String,
232 /// Interface type
233 #[serde(rename = "type")]
234 pub interface_type: NetworkInterfaceType,
235 #[serde(skip_serializing_if="Option::is_none")]
236 pub method: Option<NetworkConfigMethod>,
237 #[serde(skip_serializing_if="Option::is_none")]
238 pub method6: Option<NetworkConfigMethod>,
239 #[serde(skip_serializing_if="Option::is_none")]
240 /// IPv4 address with netmask
241 pub cidr: Option<String>,
242 #[serde(skip_serializing_if="Option::is_none")]
243 /// IPv4 gateway
244 pub gateway: Option<String>,
245 #[serde(skip_serializing_if="Option::is_none")]
246 /// IPv6 address with netmask
247 pub cidr6: Option<String>,
248 #[serde(skip_serializing_if="Option::is_none")]
249 /// IPv6 gateway
250 pub gateway6: Option<String>,
251
252 #[serde(skip_serializing_if="Vec::is_empty")]
253 pub options: Vec<String>,
254 #[serde(skip_serializing_if="Vec::is_empty")]
255 pub options6: Vec<String>,
256
257 #[serde(skip_serializing_if="Option::is_none")]
258 pub comments: Option<String>,
259 #[serde(skip_serializing_if="Option::is_none")]
260 pub comments6: Option<String>,
261
262 #[serde(skip_serializing_if="Option::is_none")]
263 /// Maximum Transmission Unit
264 pub mtu: Option<u64>,
265
266 #[serde(skip_serializing_if="Option::is_none")]
267 pub bridge_ports: Option<Vec<String>>,
268 /// Enable bridge vlan support.
269 #[serde(skip_serializing_if="Option::is_none")]
270 pub bridge_vlan_aware: Option<bool>,
271
272 #[serde(skip_serializing_if="Option::is_none")]
273 pub slaves: Option<Vec<String>>,
274 #[serde(skip_serializing_if="Option::is_none")]
275 pub bond_mode: Option<LinuxBondMode>,
276 #[serde(skip_serializing_if="Option::is_none")]
277 #[serde(rename = "bond-primary")]
278 pub bond_primary: Option<String>,
279 pub bond_xmit_hash_policy: Option<BondXmitHashPolicy>,
280 }
281
282 impl Interface {
283 pub fn new(name: String) -> Self {
284 Self {
285 name,
286 interface_type: NetworkInterfaceType::Unknown,
287 autostart: false,
288 active: false,
289 method: None,
290 method6: None,
291 cidr: None,
292 gateway: None,
293 cidr6: None,
294 gateway6: None,
295 options: Vec::new(),
296 options6: Vec::new(),
297 comments: None,
298 comments6: None,
299 mtu: None,
300 bridge_ports: None,
301 bridge_vlan_aware: None,
302 slaves: None,
303 bond_mode: None,
304 bond_primary: None,
305 bond_xmit_hash_policy: None,
306 }
307 }
308 }