src/config/network.rs: implement bond_mode

and rename bond_slaves to slaves to make it compatible with pve.
This commit is contained in:
Dietmar Maurer 2020-05-07 14:07:45 +02:00
parent c2ffc68554
commit bab5d18c3d
5 changed files with 94 additions and 20 deletions

View File

@ -187,7 +187,11 @@ pub fn read_interface(iface: String) -> Result<Value, Error> {
type: bool, type: bool,
optional: true, optional: true,
}, },
bond_slaves: { bond_mode: {
type: LinuxBondMode,
optional: true,
},
slaves: {
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
}, },
@ -212,7 +216,8 @@ pub fn create_interface(
mtu: Option<u64>, mtu: Option<u64>,
bridge_ports: Option<Vec<String>>, bridge_ports: Option<Vec<String>>,
bridge_vlan_aware: Option<bool>, bridge_vlan_aware: Option<bool>,
bond_slaves: Option<Vec<String>>, bond_mode: Option<LinuxBondMode>,
slaves: Option<Vec<String>>,
param: Value, param: Value,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -269,7 +274,8 @@ pub fn create_interface(
if bridge_vlan_aware.is_some() { interface.bridge_vlan_aware = bridge_vlan_aware; } if bridge_vlan_aware.is_some() { interface.bridge_vlan_aware = bridge_vlan_aware; }
} }
NetworkInterfaceType::Bond => { NetworkInterfaceType::Bond => {
if let Some(slaves) = bond_slaves { interface.set_bond_slaves(slaves)?; } if bond_mode.is_some() { interface.bond_mode = bond_mode; }
if let Some(slaves) = slaves { interface.set_bond_slaves(slaves)?; }
} }
_ => bail!("creating network interface type '{:?}' is not supported", interface_type), _ => bail!("creating network interface type '{:?}' is not supported", interface_type),
} }
@ -323,7 +329,7 @@ pub enum DeletableProperty {
/// Delet bridge-vlan-aware flag /// Delet bridge-vlan-aware flag
bridge_vlan_aware, bridge_vlan_aware,
/// Delete bond-slaves (set to 'none') /// Delete bond-slaves (set to 'none')
bond_slaves, slaves,
} }
@ -397,7 +403,11 @@ pub enum DeletableProperty {
type: bool, type: bool,
optional: true, optional: true,
}, },
bond_slaves: { bond_mode: {
type: LinuxBondMode,
optional: true,
},
slaves: {
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
}, },
@ -434,7 +444,8 @@ pub fn update_interface(
mtu: Option<u64>, mtu: Option<u64>,
bridge_ports: Option<Vec<String>>, bridge_ports: Option<Vec<String>>,
bridge_vlan_aware: Option<bool>, bridge_vlan_aware: Option<bool>,
bond_slaves: Option<Vec<String>>, bond_mode: Option<LinuxBondMode>,
slaves: Option<Vec<String>>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
param: Value, param: Value,
@ -476,7 +487,7 @@ pub fn update_interface(
DeletableProperty::autostart => { interface.autostart = false; }, DeletableProperty::autostart => { interface.autostart = false; },
DeletableProperty::bridge_ports => { interface.set_bridge_ports(Vec::new())?; } DeletableProperty::bridge_ports => { interface.set_bridge_ports(Vec::new())?; }
DeletableProperty::bridge_vlan_aware => { interface.bridge_vlan_aware = None; } DeletableProperty::bridge_vlan_aware => { interface.bridge_vlan_aware = None; }
DeletableProperty::bond_slaves => { interface.set_bond_slaves(Vec::new())?; } DeletableProperty::slaves => { interface.set_bond_slaves(Vec::new())?; }
} }
} }
} }
@ -487,7 +498,8 @@ pub fn update_interface(
if mtu.is_some() { interface.mtu = mtu; } if mtu.is_some() { interface.mtu = mtu; }
if let Some(ports) = bridge_ports { interface.set_bridge_ports(ports)?; } if let Some(ports) = bridge_ports { interface.set_bridge_ports(ports)?; }
if bridge_vlan_aware.is_some() { interface.bridge_vlan_aware = bridge_vlan_aware; } if bridge_vlan_aware.is_some() { interface.bridge_vlan_aware = bridge_vlan_aware; }
if let Some(slaves) = bond_slaves { interface.set_bond_slaves(slaves)?; } if let Some(slaves) = slaves { interface.set_bond_slaves(slaves)?; }
if bond_mode.is_some() { interface.bond_mode = bond_mode; }
if let Some(cidr) = cidr { if let Some(cidr) = cidr {
let (_, _, is_v6) = network::parse_cidr(&cidr)?; let (_, _, is_v6) = network::parse_cidr(&cidr)?;

View File

@ -549,6 +549,30 @@ pub enum NetworkConfigMethod {
Loopback, Loopback,
} }
#[api()]
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[allow(non_camel_case_types)]
#[repr(u8)]
/// Linux Bond Mode
pub enum LinuxBondMode {
/// Round-robin policy
balance_rr = 0,
/// Active-backup policy
active_backup = 1,
/// XOR policy
balance_xor = 2,
/// Broadcast policy
broadcast = 3,
/// IEEE 802.3ad Dynamic link aggregation
//#[serde(rename = "802.3ad")]
ieee802_3ad = 4,
/// Adaptive transmit load balancing
balance_tlb = 5,
/// Adaptive load balancing
balance_alb = 6,
}
#[api()] #[api()]
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
@ -642,10 +666,14 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
}, },
bond_slaves: { slaves: {
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
}, },
bond_mode: {
type: LinuxBondMode,
optional: true,
}
} }
)] )]
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -699,7 +727,9 @@ pub struct Interface {
pub bridge_vlan_aware: Option<bool>, pub bridge_vlan_aware: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub bond_slaves: Option<Vec<String>>, pub slaves: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
pub bond_mode: Option<LinuxBondMode>,
} }
// Regression tests // Regression tests

View File

@ -2,6 +2,7 @@ use std::io::{Write};
use std::collections::{HashSet, HashMap}; use std::collections::{HashSet, HashMap};
use anyhow::{Error, format_err, bail}; use anyhow::{Error, format_err, bail};
use serde::de::{value, IntoDeserializer, Deserialize};
use proxmox::tools::{fs::replace_file, fs::CreateOptions}; use proxmox::tools::{fs::replace_file, fs::CreateOptions};
@ -14,7 +15,24 @@ pub use lexer::*;
mod parser; mod parser;
pub use parser::*; pub use parser::*;
use crate::api2::types::{Interface, NetworkConfigMethod, NetworkInterfaceType}; use crate::api2::types::{Interface, NetworkConfigMethod, NetworkInterfaceType, LinuxBondMode};
pub fn bond_mode_from_str(s: &str) -> Result<LinuxBondMode, Error> {
LinuxBondMode::deserialize(s.into_deserializer())
.map_err(|_: value::Error| format_err!("invalid bond_mode '{}'", s))
}
pub fn bond_mode_to_str(mode: LinuxBondMode) -> &'static str {
match mode {
LinuxBondMode::balance_rr => "balance-rr",
LinuxBondMode::active_backup => "active-backup",
LinuxBondMode::balance_xor => "balance-xor",
LinuxBondMode::broadcast => "broadcast",
LinuxBondMode::ieee802_3ad => "802.3ad",
LinuxBondMode::balance_tlb => "balance-tlb",
LinuxBondMode::balance_alb => "balance-alb",
}
}
impl Interface { impl Interface {
@ -37,7 +55,8 @@ impl Interface {
mtu: None, mtu: None,
bridge_ports: None, bridge_ports: None,
bridge_vlan_aware: None, bridge_vlan_aware: None,
bond_slaves: None, slaves: None,
bond_mode: None,
} }
} }
@ -116,7 +135,7 @@ impl Interface {
if self.interface_type != NetworkInterfaceType::Bond { if self.interface_type != NetworkInterfaceType::Bond {
bail!("interface '{}' is no bond (type is {:?})", self.name, self.interface_type); bail!("interface '{}' is no bond (type is {:?})", self.name, self.interface_type);
} }
self.bond_slaves = Some(slaves); self.slaves = Some(slaves);
Ok(()) Ok(())
} }
@ -137,7 +156,10 @@ impl Interface {
} }
} }
NetworkInterfaceType::Bond => { NetworkInterfaceType::Bond => {
if let Some(ref slaves) = self.bond_slaves { let mode = self.bond_mode.unwrap_or(LinuxBondMode::balance_rr);
writeln!(w, "\tbond-mode {}", bond_mode_to_str(mode))?;
if let Some(ref slaves) = self.slaves {
if slaves.is_empty() { if slaves.is_empty() {
writeln!(w, "\tbond-slaves none")?; writeln!(w, "\tbond-slaves none")?;
} else { } else {
@ -226,7 +248,8 @@ impl Interface {
mtu: _mtu, mtu: _mtu,
bridge_ports: _bridge_ports, bridge_ports: _bridge_ports,
bridge_vlan_aware: _bridge_vlan_aware, bridge_vlan_aware: _bridge_vlan_aware,
bond_slaves: _bond_slaves, slaves: _slaves,
bond_mode: _bond_mode,
} => { } => {
method == method6 method == method6
&& comments.is_none() && comments.is_none()
@ -264,20 +287,20 @@ impl Interface {
} }
return Ok(()); return Ok(());
} }
if let Some(method) = self.method { if let Some(method) = self.method {
writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?; writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?;
self.write_iface_attributes_v4(w, method)?; self.write_iface_attributes_v4(w, method)?;
self.write_iface_attributes(w)?; self.write_iface_attributes(w)?;
writeln!(w)?; writeln!(w)?;
} }
if let Some(method6) = self.method6 { if let Some(method6) = self.method6 {
let mut skip_v6 = false; // avoid empty inet6 manual entry let mut skip_v6 = false; // avoid empty inet6 manual entry
if self.method.is_some() && method6 == NetworkConfigMethod::Manual { if self.method.is_some() && method6 == NetworkConfigMethod::Manual {
if self.comments6.is_none() && self.options6.is_empty() { skip_v6 = true; } if self.comments6.is_none() && self.options6.is_empty() { skip_v6 = true; }
} }
if !skip_v6 { if !skip_v6 {
writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method6))?; writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method6))?;
self.write_iface_attributes_v6(w, method6)?; self.write_iface_attributes_v6(w, method6)?;

View File

@ -25,6 +25,7 @@ pub enum Token {
BridgePorts, BridgePorts,
BridgeVlanAware, BridgeVlanAware,
BondSlaves, BondSlaves,
BondMode,
EOF, EOF,
} }
@ -49,6 +50,8 @@ lazy_static! {
map.insert("bridge_vlan_aware", Token::BridgeVlanAware); map.insert("bridge_vlan_aware", Token::BridgeVlanAware);
map.insert("bond-slaves", Token::BondSlaves); map.insert("bond-slaves", Token::BondSlaves);
map.insert("bond_slaves", Token::BondSlaves); map.insert("bond_slaves", Token::BondSlaves);
map.insert("bond-mode", Token::BondMode);
map.insert("bond_mode", Token::BondMode);
map map
}; };
} }

View File

@ -9,7 +9,7 @@ use regex::Regex;
use super::helper::*; use super::helper::*;
use super::lexer::*; use super::lexer::*;
use super::{NetworkConfig, NetworkOrderEntry, Interface, NetworkConfigMethod, NetworkInterfaceType}; use super::{NetworkConfig, NetworkOrderEntry, Interface, NetworkConfigMethod, NetworkInterfaceType, bond_mode_from_str};
pub struct NetworkParser<R: BufRead> { pub struct NetworkParser<R: BufRead> {
input: Peekable<Lexer<R>>, input: Peekable<Lexer<R>>,
@ -236,9 +236,15 @@ impl <R: BufRead> NetworkParser<R> {
Token::BondSlaves => { Token::BondSlaves => {
self.eat(Token::BondSlaves)?; self.eat(Token::BondSlaves)?;
let slaves = self.parse_iface_list()?; let slaves = self.parse_iface_list()?;
interface.bond_slaves = Some(slaves); interface.slaves = Some(slaves);
interface.set_interface_type(NetworkInterfaceType::Bond)?; interface.set_interface_type(NetworkInterfaceType::Bond)?;
} }
Token::BondMode => {
self.eat(Token::BondMode)?;
let mode = self.next_text()?;
interface.bond_mode = Some(bond_mode_from_str(&mode)?);
self.eat(Token::Newline)?;
}
Token::Netmask => bail!("netmask is deprecated and no longer supported"), Token::Netmask => bail!("netmask is deprecated and no longer supported"),
_ => { // parse addon attributes _ => { // parse addon attributes