src/config/network.rs: parse bridge-ports

This commit is contained in:
Dietmar Maurer 2020-04-23 09:24:17 +02:00
parent 02269f3dba
commit 1d9a68c2fc
4 changed files with 55 additions and 0 deletions

View File

@ -550,6 +550,10 @@ pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network int
.max_length(libc::IFNAMSIZ-1) .max_length(libc::IFNAMSIZ-1)
.schema(); .schema();
pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
"Network interface list.", &NETWORK_INTERFACE_NAME_SCHEMA)
.schema();
#[api( #[api(
properties: { properties: {
name: { name: {
@ -582,6 +586,10 @@ pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network int
type: String, type: String,
}, },
}, },
bridge_ports: {
schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true,
},
} }
)] )]
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -620,6 +628,9 @@ pub struct Interface {
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// Maximum Transmission Unit /// Maximum Transmission Unit
pub mtu: Option<u64>, pub mtu: Option<u64>,
#[serde(skip_serializing_if="Option::is_none")]
pub bridge_ports: Option<Vec<String>>,
} }
// Regression tests // Regression tests

View File

@ -33,6 +33,7 @@ impl Interface {
options_v4: Vec::new(), options_v4: Vec::new(),
options_v6: Vec::new(), options_v6: Vec::new(),
mtu: None, mtu: None,
bridge_ports: None,
} }
} }
@ -100,9 +101,24 @@ impl Interface {
/// Write attributes not dependening on address family /// Write attributes not dependening on address family
fn write_iface_attributes(&self, w: &mut dyn Write) -> Result<(), Error> { fn write_iface_attributes(&self, w: &mut dyn Write) -> Result<(), Error> {
match self.interface_type {
NetworkInterfaceType::Bridge => {
if let Some(ref ports) = self.bridge_ports {
if ports.is_empty() {
writeln!(w, " bridge-ports none")?;
} else {
writeln!(w, " bridge-ports {}", ports.join(" "))?;
}
}
}
_ => {}
}
if let Some(mtu) = self.mtu { if let Some(mtu) = self.mtu {
writeln!(w, " mtu {}", mtu)?; writeln!(w, " mtu {}", mtu)?;
} }
Ok(()) Ok(())
} }
@ -161,6 +177,7 @@ impl Interface {
gateway_v4: _gateway_v4, gateway_v4: _gateway_v4,
gateway_v6: _gateway_v6, gateway_v6: _gateway_v6,
mtu: _mtu, mtu: _mtu,
bridge_ports: _bridge_ports,
} => { } => {
method_v4 == method_v6 method_v4 == method_v6
&& options_v4.is_empty() && options_v4.is_empty()

View File

@ -22,6 +22,7 @@ pub enum Token {
Static, Static,
Attribute, Attribute,
MTU, MTU,
BridgePorts,
EOF, EOF,
} }
@ -40,6 +41,8 @@ lazy_static! {
map.insert("netmask", Token::Netmask); map.insert("netmask", Token::Netmask);
map.insert("static", Token::Static); map.insert("static", Token::Static);
map.insert("mtu", Token::MTU); map.insert("mtu", Token::MTU);
map.insert("bridge-ports", Token::BridgePorts);
map.insert("bridge_ports", Token::BridgePorts);
map map
}; };
} }

View File

@ -156,6 +156,24 @@ impl <R: BufRead> NetworkParser<R> {
Ok(()) Ok(())
} }
fn parse_iface_list(&mut self) -> Result<Vec<String>, Error> {
let mut list = Vec::new();
loop {
let (token, text) = self.next()?;
match token {
Token::Newline => break,
Token::Text => {
if &text != "none" {
list.push(text);
}
}
_ => bail!("unable to parse interface list - unexpected token '{:?}'", token),
}
}
Ok(list)
}
fn parse_iface_attributes(&mut self, interface: &mut Interface) -> Result<(), Error> { fn parse_iface_attributes(&mut self, interface: &mut Interface) -> Result<(), Error> {
loop { loop {
@ -172,6 +190,12 @@ impl <R: BufRead> NetworkParser<R> {
let mtu = self.parse_iface_mtu()?; let mtu = self.parse_iface_mtu()?;
interface.mtu = Some(mtu); interface.mtu = Some(mtu);
} }
Token::BridgePorts => {
self.eat(Token::BridgePorts)?;
let ports = self.parse_iface_list()?;
interface.bridge_ports = Some(ports);
interface.interface_type = NetworkInterfaceType::Bridge;
}
Token::Netmask => bail!("netmask is deprecated and no longer supported"), Token::Netmask => bail!("netmask is deprecated and no longer supported"),
_ => { _ => {
self.parse_iface_addon_attribute(interface)?; self.parse_iface_addon_attribute(interface)?;