src/config/network.rs: use a single mtu setting (instead of mtu_v4 and mtu_v6)

This commit is contained in:
Dietmar Maurer 2020-04-23 07:07:14 +02:00
parent 4cb6bd894c
commit 2c18efd902
4 changed files with 29 additions and 41 deletions

View File

@ -90,10 +90,8 @@ pub enum DeletableProperty {
method_v4, method_v4,
/// Delete the whole IPv6 configuration entry. /// Delete the whole IPv6 configuration entry.
method_v6, method_v6,
/// Delete mtu for IPv4. /// Delete mtu.
mtu_v4, mtu,
/// Delete mtu IPv6.
mtu_v6,
/// Delete auto flag /// Delete auto flag
auto, auto,
} }
@ -126,17 +124,12 @@ pub enum DeletableProperty {
schema: IP_SCHEMA, schema: IP_SCHEMA,
optional: true, optional: true,
}, },
mtu_v4: { mtu: {
description: "Maximum Transmission Unit for IPv4.", description: "Maximum Transmission Unit.",
optional: true,
minimum: 46,
maximum: 65535,
},
mtu_v6: {
description: "Maximum Transmission Unit for IPv6.",
optional: true, optional: true,
minimum: 46, minimum: 46,
maximum: 65535, maximum: 65535,
default: 1500,
}, },
delete: { delete: {
description: "List of properties to delete.", description: "List of properties to delete.",
@ -164,8 +157,7 @@ pub fn update_interface(
method_v6: Option<NetworkConfigMethod>, method_v6: Option<NetworkConfigMethod>,
address: Option<String>, address: Option<String>,
gateway: Option<String>, gateway: Option<String>,
mtu_v4: Option<u64>, mtu: Option<u64>,
mtu_v6: Option<u64>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -190,8 +182,7 @@ pub fn update_interface(
DeletableProperty::gateway_v6 => { interface.gateway_v6 = None; }, DeletableProperty::gateway_v6 => { interface.gateway_v6 = None; },
DeletableProperty::method_v4 => { interface.method_v4 = None; }, DeletableProperty::method_v4 => { interface.method_v4 = None; },
DeletableProperty::method_v6 => { interface.method_v6 = None; }, DeletableProperty::method_v6 => { interface.method_v6 = None; },
DeletableProperty::mtu_v4 => { interface.mtu_v4 = None; }, DeletableProperty::mtu => { interface.mtu = None; },
DeletableProperty::mtu_v6 => { interface.mtu_v6 = None; },
DeletableProperty::auto => { interface.auto = false; }, DeletableProperty::auto => { interface.auto = false; },
} }
} }
@ -200,8 +191,7 @@ pub fn update_interface(
if let Some(auto) = auto { interface.auto = auto; } if let Some(auto) = auto { interface.auto = auto; }
if method_v4.is_some() { interface.method_v4 = method_v4; } if method_v4.is_some() { interface.method_v4 = method_v4; }
if method_v6.is_some() { interface.method_v6 = method_v6; } if method_v6.is_some() { interface.method_v6 = method_v6; }
if mtu_v4.is_some() { interface.mtu_v4 = mtu_v4; } if mtu.is_some() { interface.mtu = mtu; }
if mtu_v6.is_some() { interface.mtu_v6 = mtu_v6; }
if let Some(address) = address { if let Some(address) = address {
let (_, _, is_v6) = network::parse_cidr(&address)?; let (_, _, is_v6) = network::parse_cidr(&address)?;

View File

@ -586,17 +586,14 @@ pub struct Interface {
/// IPv6 gateway /// IPv6 gateway
pub gateway_v6: Option<String>, pub gateway_v6: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
/// Maximum Transmission Unit for IPv4
pub mtu_v4: Option<u64>,
#[serde(skip_serializing_if="Option::is_none")]
/// Maximum Transmission Unit for IPv6
pub mtu_v6: Option<u64>,
#[serde(skip_serializing_if="Vec::is_empty")] #[serde(skip_serializing_if="Vec::is_empty")]
pub options_v4: Vec<String>, pub options_v4: Vec<String>,
#[serde(skip_serializing_if="Vec::is_empty")] #[serde(skip_serializing_if="Vec::is_empty")]
pub options_v6: Vec<String>, pub options_v6: Vec<String>,
#[serde(skip_serializing_if="Option::is_none")]
/// Maximum Transmission Unit
pub mtu: Option<u64>,
} }
// Regression tests // Regression tests

View File

@ -30,10 +30,9 @@ impl Interface {
gateway_v4: None, gateway_v4: None,
cidr_v6: None, cidr_v6: None,
gateway_v6: None, gateway_v6: None,
mtu_v4: None,
mtu_v6: None,
options_v4: Vec::new(), options_v4: Vec::new(),
options_v6: Vec::new(), options_v6: Vec::new(),
mtu: None,
} }
} }
@ -99,6 +98,15 @@ impl Interface {
} }
} }
/// Write attributes not dependening on address family
fn write_iface_attributes(&self, w: &mut dyn Write) -> Result<(), Error> {
if let Some(mtu) = self.mtu {
writeln!(w, " mtu {}", mtu)?;
}
Ok(())
}
/// Write attributes dependening on address family inet (IPv4)
fn write_iface_attributes_v4(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> { fn write_iface_attributes_v4(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
if method == NetworkConfigMethod::Static { if method == NetworkConfigMethod::Static {
if let Some(address) = &self.cidr_v4 { if let Some(address) = &self.cidr_v4 {
@ -109,10 +117,6 @@ impl Interface {
} }
} }
if let Some(mtu) = &self.mtu_v4 {
writeln!(w, " mtu {}", mtu)?;
}
for option in &self.options_v4 { for option in &self.options_v4 {
writeln!(w, " {}", option)?; writeln!(w, " {}", option)?;
} }
@ -120,6 +124,7 @@ impl Interface {
Ok(()) Ok(())
} }
/// Write attributes dependening on address family inet6 (IPv6)
fn write_iface_attributes_v6(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> { fn write_iface_attributes_v6(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
if method == NetworkConfigMethod::Static { if method == NetworkConfigMethod::Static {
if let Some(address) = &self.cidr_v6 { if let Some(address) = &self.cidr_v6 {
@ -130,10 +135,6 @@ impl Interface {
} }
} }
if let Some(mtu) = &self.mtu_v6 {
writeln!(w, " mtu {}", mtu)?;
}
for option in &self.options_v6 { for option in &self.options_v6 {
writeln!(w, " {}", option)?; writeln!(w, " {}", option)?;
} }
@ -159,13 +160,13 @@ impl Interface {
} }
if self.method_v4 == self.method_v6 if self.method_v4 == self.method_v6
&& self.mtu_v4 == self.mtu_v6
&& self.options_v4.is_empty() == self.options_v6.is_empty() && self.options_v4.is_empty() == self.options_v6.is_empty()
{ {
if let Some(method) = self.method_v4 { if let Some(method) = self.method_v4 {
writeln!(w, "iface {} {}", self.name, method_to_str(method))?; writeln!(w, "iface {} {}", self.name, method_to_str(method))?;
self.write_iface_attributes_v4(w, method)?; self.write_iface_attributes_v4(w, method)?;
self.write_iface_attributes_v6(w, method)?; self.write_iface_attributes_v6(w, method)?;
self.write_iface_attributes(w)?;
writeln!(w)?; writeln!(w)?;
} }
} else { } else {
@ -179,6 +180,7 @@ impl Interface {
self.write_iface_attributes_v6(w, method)?; self.write_iface_attributes_v6(w, method)?;
writeln!(w)?; writeln!(w)?;
} }
self.write_iface_attributes(w)?;
} }
Ok(()) Ok(())
} }

View File

@ -156,7 +156,7 @@ impl <R: BufRead> NetworkParser<R> {
Ok(()) Ok(())
} }
fn parse_iface_attributes(&mut self, interface: &mut Interface, family_v4: bool, family_v6: bool) -> Result<(), Error> { fn parse_iface_attributes(&mut self, interface: &mut Interface) -> Result<(), Error> {
loop { loop {
match self.peek()? { match self.peek()? {
@ -170,8 +170,7 @@ impl <R: BufRead> NetworkParser<R> {
Token::Gateway => self.parse_iface_gateway(interface)?, Token::Gateway => self.parse_iface_gateway(interface)?,
Token::MTU => { Token::MTU => {
let mtu = self.parse_iface_mtu()?; let mtu = self.parse_iface_mtu()?;
if family_v4 { interface.mtu_v4 = Some(mtu); } interface.mtu = Some(mtu);
if family_v6 { interface.mtu_v6 = Some(mtu); }
} }
Token::Netmask => bail!("netmask is deprecated and no longer supported"), Token::Netmask => bail!("netmask is deprecated and no longer supported"),
_ => { _ => {
@ -221,7 +220,7 @@ impl <R: BufRead> NetworkParser<R> {
interface.set_method_v6(config_method)?; interface.set_method_v6(config_method)?;
} }
if has_attributes { self.parse_iface_attributes(&mut interface, address_family_v4, address_family_v6)?; } if has_attributes { self.parse_iface_attributes(&mut interface)?; }
} else { } else {
let mut interface = Interface::new(iface.clone()); let mut interface = Interface::new(iface.clone());
if address_family_v4 { if address_family_v4 {
@ -231,7 +230,7 @@ impl <R: BufRead> NetworkParser<R> {
interface.set_method_v6(config_method)?; interface.set_method_v6(config_method)?;
} }
if has_attributes { self.parse_iface_attributes(&mut interface, address_family_v4, address_family_v6)?; } if has_attributes { self.parse_iface_attributes(&mut interface)?; }
config.interfaces.insert(interface.name.clone(), interface); config.interfaces.insert(interface.name.clone(), interface);