-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[netdog] Add vlan structs for network configuration
This adds the stuctures to specify a vlan in net.toml. This allows deserialization and serialization of vlan structures along with some tests.
- Loading branch information
Showing
9 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use super::validate_addressing; | ||
use super::{Dhcp4ConfigV1, Dhcp6ConfigV1, Result, Validate}; | ||
use crate::interface_name::InterfaceName; | ||
use crate::net_config::devices::generate_addressing_validation; | ||
use crate::net_config::{RouteV1, StaticConfigV1}; | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
#[serde(remote = "Self")] | ||
pub(crate) struct NetVlanV1 { | ||
pub(crate) primary: Option<bool>, | ||
pub(crate) dhcp4: Option<Dhcp4ConfigV1>, | ||
pub(crate) dhcp6: Option<Dhcp6ConfigV1>, | ||
pub(crate) static4: Option<StaticConfigV1>, | ||
pub(crate) static6: Option<StaticConfigV1>, | ||
#[serde(rename = "route")] | ||
pub(crate) routes: Option<Vec<RouteV1>>, | ||
kind: String, | ||
pub(crate) device: InterfaceName, | ||
pub(crate) id: u16, | ||
} | ||
|
||
impl<'de> Deserialize<'de> for NetVlanV1 { | ||
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let this = Self::deserialize(deserializer)?; | ||
|
||
if this.kind.to_lowercase().as_str() != "vlan" { | ||
return Err(D::Error::custom(format!( | ||
"kind of '{}' does not match 'vlan'", | ||
this.kind.as_str() | ||
))); | ||
} | ||
|
||
// Validate its a valid vlan id - 0-4095 | ||
if this.id > 4095 { | ||
return Err(D::Error::custom( | ||
"invalid vlan ID specified, must be between 0-4095", | ||
)); | ||
} | ||
|
||
Ok(this) | ||
} | ||
} | ||
|
||
impl Validate for NetVlanV1 { | ||
fn validate(&self) -> Result<()> { | ||
validate_addressing(self)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Generate the traits for IP Address validation | ||
generate_addressing_validation!(&NetVlanV1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::interface_name::InterfaceName; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, Serialize, PartialEq)] | ||
pub(crate) struct WickedVlanTag { | ||
#[serde(rename = "$unflatten=device")] | ||
device: InterfaceName, | ||
#[serde(rename = "$unflatten=tag")] | ||
id: u16, | ||
} | ||
|
||
impl WickedVlanTag { | ||
pub(crate) fn new(device: InterfaceName, id: u16) -> Self { | ||
WickedVlanTag { device, id } | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sources/api/netdog/test_data/net_config/vlan/missing_kind.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version = {{version}} | ||
|
||
[vlan42] | ||
device = "eno1" | ||
id = 42 | ||
dhcp4 = true |
36 changes: 36 additions & 0 deletions
36
sources/api/netdog/test_data/net_config/vlan/net_config.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
version = {{version}} | ||
|
||
# Basic vlan dhcp | ||
[vlan2] | ||
kind = "vlan" | ||
device = "eno1" | ||
id = 2 | ||
dhcp4 = true | ||
|
||
[vlan3] | ||
kind = "vlan" | ||
device = "eno2" | ||
id = 3 | ||
dhcp6 = true | ||
|
||
# Static4 | ||
[vlan4] | ||
kind = "vlan" | ||
device = "eno3" | ||
id = 4 | ||
|
||
[vlan4.static4] | ||
addresses = ["10.0.0.9/24"] | ||
|
||
# two devices on same vlan | ||
[firstvlan10] | ||
kind = "vlan" | ||
device = "eno4" | ||
id = 10 | ||
dhcp4 = true | ||
|
||
[secondvlan10] | ||
kind = "vlan" | ||
device = "eno5" | ||
id = 10 | ||
dhcp4 = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version = {{version}} | ||
|
||
[vlan42] | ||
kind = "vlan" | ||
id = 42 | ||
dhcp4 = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version = {{version}} | ||
|
||
[vlan6] | ||
kind = "vlan" | ||
device = "eno1" | ||
dhcp4 = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version = {{version}} | ||
|
||
[vlan6] | ||
kind = "vlan" | ||
device = "eno1" | ||
id = 5000 | ||
dhcp4 = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<interface><name>mystaticvlan</name><control><mode>boot</mode><link-detection><require-link></require-link></link-detection></control><ipv4:static><address><local>192.168.1.100/24</local></address></ipv4:static><vlan><device>eno1000</device><tag>42</tag></vlan></interface> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<interface><name>myvlan</name><control><mode>boot</mode><link-detection><require-link></require-link></link-detection></control><ipv4:dhcp><enabled>true</enabled></ipv4:dhcp><vlan><device>eno1</device><tag>42</tag></vlan></interface> |