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
//! VLAN (802.1Q) connection configuration.
//!
//! This module provides types for configuring VLAN connections over a parent
//! Ethernet or other interface.
use super::error::ConnectionError;
/// VLAN connection configuration.
///
/// Configures a VLAN (802.1Q) virtual interface on top of a parent device.
/// VLANs allow you to segment network traffic on a single physical interface
/// into multiple logical networks.
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// // Basic VLAN on eth0 with ID 100
/// let config = VlanConfig::new("eth0", 100);
///
/// // VLAN with custom interface name and priority mapping
/// let config = VlanConfig::new("eth0", 200)
/// .with_interface_name("vlan200")
/// .with_mtu(1500)
/// .with_connection_name("Office VLAN");
/// ```
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct VlanConfig {
/// Parent interface name (e.g., "eth0", "enp3s0").
pub parent: String,
/// VLAN ID (1-4094).
pub id: u16,
/// Optional name for the VLAN interface.
/// Defaults to `{parent}.{id}` (e.g., "eth0.100").
pub interface_name: Option<String>,
/// Optional human-readable connection name.
/// Defaults to "VLAN {id} on {parent}".
pub connection_name: Option<String>,
/// Optional MTU for the VLAN interface.
pub mtu: Option<u32>,
/// VLAN flags (bitmask).
/// - 0x1: Reorder headers (default on)
/// - 0x2: GVRP (GARP VLAN Registration Protocol)
/// - 0x4: Loose binding (don't fail if parent is down)
/// - 0x8: MVRP (Multiple VLAN Registration Protocol)
pub flags: Option<u32>,
/// Ingress priority mapping (802.1p to Linux priority).
/// Format: "from:to" pairs, e.g., vec!["0:0", "1:1", "2:2"]
pub ingress_priority_map: Option<Vec<String>>,
/// Egress priority mapping (Linux priority to 802.1p).
/// Format: "from:to" pairs, e.g., vec!["0:0", "1:1", "2:2"]
pub egress_priority_map: Option<Vec<String>>,
}
impl VlanConfig {
/// Creates a new VLAN configuration.
///
/// # Arguments
///
/// * `parent` - Parent interface name (e.g., "eth0")
/// * `id` - VLAN ID (1-4094)
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100);
/// assert_eq!(config.parent, "eth0");
/// assert_eq!(config.id, 100);
/// ```
#[must_use]
pub fn new(parent: impl Into<String>, id: u16) -> Self {
Self {
parent: parent.into(),
id,
interface_name: None,
connection_name: None,
mtu: None,
flags: None,
ingress_priority_map: None,
egress_priority_map: None,
}
}
/// Sets a custom interface name for the VLAN device.
///
/// By default, the interface is named `{parent}.{id}` (e.g., "eth0.100").
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100)
/// .with_interface_name("office-vlan");
/// ```
#[must_use]
pub fn with_interface_name(mut self, name: impl Into<String>) -> Self {
self.interface_name = Some(name.into());
self
}
/// Sets a human-readable connection name.
///
/// This is the name shown in NetworkManager's connection list.
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100)
/// .with_connection_name("Office Network");
/// ```
#[must_use]
pub fn with_connection_name(mut self, name: impl Into<String>) -> Self {
self.connection_name = Some(name.into());
self
}
/// Sets the MTU (Maximum Transmission Unit) for the VLAN interface.
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100)
/// .with_mtu(1496); // Account for VLAN header
/// ```
#[must_use]
pub fn with_mtu(mut self, mtu: u32) -> Self {
self.mtu = Some(mtu);
self
}
/// Sets VLAN flags.
///
/// Flags:
/// - `0x1`: Reorder headers (default, recommended)
/// - `0x2`: GVRP (GARP VLAN Registration Protocol)
/// - `0x4`: Loose binding (don't fail if parent is down)
/// - `0x8`: MVRP (Multiple VLAN Registration Protocol)
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// // Enable loose binding (allow VLAN even if parent is down)
/// let config = VlanConfig::new("eth0", 100)
/// .with_flags(0x1 | 0x4);
/// ```
#[must_use]
pub fn with_flags(mut self, flags: u32) -> Self {
self.flags = Some(flags);
self
}
/// Sets ingress priority mapping (802.1p priority to Linux skb priority).
///
/// Each entry maps an incoming 802.1p priority (0-7) to a Linux priority.
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100)
/// .with_ingress_priority_map(vec!["0:0", "1:1", "2:2"]);
/// ```
#[must_use]
pub fn with_ingress_priority_map(mut self, map: Vec<impl Into<String>>) -> Self {
self.ingress_priority_map = Some(map.into_iter().map(Into::into).collect());
self
}
/// Sets egress priority mapping (Linux skb priority to 802.1p priority).
///
/// Each entry maps an outgoing Linux priority to an 802.1p priority (0-7).
///
/// # Examples
///
/// ```rust
/// use nmrs::VlanConfig;
///
/// let config = VlanConfig::new("eth0", 100)
/// .with_egress_priority_map(vec!["0:0", "1:1", "2:2"]);
/// ```
#[must_use]
pub fn with_egress_priority_map(mut self, map: Vec<impl Into<String>>) -> Self {
self.egress_priority_map = Some(map.into_iter().map(Into::into).collect());
self
}
/// Returns the effective interface name.
///
/// Returns the custom interface name if set, otherwise `{parent}.{id}`.
#[must_use]
pub fn effective_interface_name(&self) -> String {
self.interface_name
.clone()
.unwrap_or_else(|| format!("{}.{}", self.parent, self.id))
}
/// Returns the effective connection name.
///
/// Returns the custom connection name if set, otherwise "VLAN {id} on {parent}".
#[must_use]
pub fn effective_connection_name(&self) -> String {
self.connection_name
.clone()
.unwrap_or_else(|| format!("VLAN {} on {}", self.id, self.parent))
}
/// Validates the VLAN configuration.
///
/// # Errors
///
/// Returns `ConnectionError::InvalidVlanId` if the VLAN ID is out of range (1-4094).
/// Returns `ConnectionError::InvalidInput` if the parent interface name is empty.
pub fn validate(&self) -> Result<(), ConnectionError> {
if self.id == 0 || self.id > 4094 {
return Err(ConnectionError::InvalidVlanId { id: self.id });
}
if self.parent.is_empty() {
return Err(ConnectionError::InvalidInput {
field: "parent".to_string(),
reason: "parent interface name cannot be empty".to_string(),
});
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_creates_basic_config() {
let config = VlanConfig::new("eth0", 100);
assert_eq!(config.parent, "eth0");
assert_eq!(config.id, 100);
assert!(config.interface_name.is_none());
assert!(config.connection_name.is_none());
}
#[test]
fn effective_interface_name_default() {
let config = VlanConfig::new("eth0", 100);
assert_eq!(config.effective_interface_name(), "eth0.100");
}
#[test]
fn effective_interface_name_custom() {
let config = VlanConfig::new("eth0", 100).with_interface_name("office-vlan");
assert_eq!(config.effective_interface_name(), "office-vlan");
}
#[test]
fn effective_connection_name_default() {
let config = VlanConfig::new("eth0", 100);
assert_eq!(config.effective_connection_name(), "VLAN 100 on eth0");
}
#[test]
fn effective_connection_name_custom() {
let config = VlanConfig::new("eth0", 100).with_connection_name("Office Network");
assert_eq!(config.effective_connection_name(), "Office Network");
}
#[test]
fn builder_methods_chain() {
let config = VlanConfig::new("enp3s0", 200)
.with_interface_name("vlan200")
.with_connection_name("Server VLAN")
.with_mtu(1496)
.with_flags(0x5)
.with_ingress_priority_map(vec!["0:0", "1:1"])
.with_egress_priority_map(vec!["0:0", "1:1"]);
assert_eq!(config.parent, "enp3s0");
assert_eq!(config.id, 200);
assert_eq!(config.interface_name, Some("vlan200".to_string()));
assert_eq!(config.connection_name, Some("Server VLAN".to_string()));
assert_eq!(config.mtu, Some(1496));
assert_eq!(config.flags, Some(0x5));
assert_eq!(
config.ingress_priority_map,
Some(vec!["0:0".to_string(), "1:1".to_string()])
);
}
#[test]
fn validate_rejects_zero_id() {
let config = VlanConfig::new("eth0", 0);
assert!(matches!(
config.validate().unwrap_err(),
ConnectionError::InvalidVlanId { id: 0 }
));
}
#[test]
fn validate_rejects_id_over_4094() {
let config = VlanConfig::new("eth0", 4095);
assert!(matches!(
config.validate().unwrap_err(),
ConnectionError::InvalidVlanId { id: 4095 }
));
}
#[test]
fn validate_rejects_empty_parent() {
let config = VlanConfig::new("", 100);
assert!(matches!(
config.validate().unwrap_err(),
ConnectionError::InvalidInput { field, reason }
if field == "parent" && reason == "parent interface name cannot be empty"
));
}
#[test]
fn validate_accepts_valid_config() {
let config = VlanConfig::new("eth0", 100);
config.validate().unwrap();
let config = VlanConfig::new("eth0", 1);
config.validate().unwrap();
let config = VlanConfig::new("eth0", 4094);
config.validate().unwrap();
}
}