Skip to content

Commit

Permalink
[netdog] Add vlan structs for network configuration
Browse files Browse the repository at this point in the history
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
yeazelm committed Dec 13, 2022
1 parent f06897c commit 9118ab0
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sources/api/netdog/src/net_config/devices/vlan.rs
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);
16 changes: 16 additions & 0 deletions sources/api/netdog/src/wicked/vlan.rs
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 }
}
}
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 sources/api/netdog/test_data/net_config/vlan/net_config.toml
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
6 changes: 6 additions & 0 deletions sources/api/netdog/test_data/net_config/vlan/no_device.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = {{version}}

[vlan42]
kind = "vlan"
id = 42
dhcp4 = true
6 changes: 6 additions & 0 deletions sources/api/netdog/test_data/net_config/vlan/no_id.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = {{version}}

[vlan6]
kind = "vlan"
device = "eno1"
dhcp4 = true
7 changes: 7 additions & 0 deletions sources/api/netdog/test_data/net_config/vlan/oob_id.toml
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
1 change: 1 addition & 0 deletions sources/api/netdog/test_data/wicked/mystaticvlan.xml
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>
1 change: 1 addition & 0 deletions sources/api/netdog/test_data/wicked/myvlan.xml
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>

0 comments on commit 9118ab0

Please sign in to comment.