src/config/network.rs: make it compatible with pve

and depend on proxmox 0.1.26
This commit is contained in:
Dietmar Maurer 2020-05-06 07:51:05 +02:00
parent 5751e49566
commit 7b22acd0c2
12 changed files with 333 additions and 204 deletions

View File

@ -37,7 +37,7 @@ pam = "0.7"
pam-sys = "0.5" pam-sys = "0.5"
percent-encoding = "2.1" percent-encoding = "2.1"
pin-utils = "0.1.0-alpha" pin-utils = "0.1.0-alpha"
proxmox = { version = "0.1.25", features = [ "sortable-macro", "api-macro" ] } proxmox = { version = "0.1.26", features = [ "sortable-macro", "api-macro" ] }
#proxmox = { git = "ssh://gitolite3@proxdev.maurer-it.com/rust/proxmox", version = "0.1.2", features = [ "sortable-macro", "api-macro" ] } #proxmox = { git = "ssh://gitolite3@proxdev.maurer-it.com/rust/proxmox", version = "0.1.2", features = [ "sortable-macro", "api-macro" ] }
#proxmox = { path = "../proxmox/proxmox", features = [ "sortable-macro", "api-macro" ] } #proxmox = { path = "../proxmox/proxmox", features = [ "sortable-macro", "api-macro" ] }
regex = "1.2" regex = "1.2"

View File

@ -7,6 +7,7 @@ use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
use crate::config::network; use crate::config::network;
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}; use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
use crate::api2::types::*; use crate::api2::types::*;
use crate::server::{WorkerTask};
#[api( #[api(
input: { input: {
@ -39,9 +40,11 @@ pub fn list_network_devices(
let mut list = Vec::new(); let mut list = Vec::new();
for interface in config.interfaces.values() { for (iface, interface) in config.interfaces.iter() {
if iface == "lo" { continue; } // do not list lo
let mut item: Value = to_value(interface)?; let mut item: Value = to_value(interface)?;
item["digest"] = digest.clone().into(); item["digest"] = digest.clone().into();
item["iface"] = iface.to_string().into();
list.push(item); list.push(item);
} }
@ -59,7 +62,7 @@ pub fn list_network_devices(
node: { node: {
schema: NODE_SCHEMA, schema: NODE_SCHEMA,
}, },
name: { iface: {
schema: NETWORK_INTERFACE_NAME_SCHEMA, schema: NETWORK_INTERFACE_NAME_SCHEMA,
}, },
}, },
@ -73,11 +76,11 @@ pub fn list_network_devices(
}, },
)] )]
/// Read a network interface configuration. /// Read a network interface configuration.
pub fn read_interface(name: String) -> Result<Value, Error> { pub fn read_interface(iface: String) -> Result<Value, Error> {
let (config, digest) = network::config()?; let (config, digest) = network::config()?;
let interface = config.lookup(&name)?; let interface = config.lookup(&iface)?;
let mut data: Value = to_value(interface)?; let mut data: Value = to_value(interface)?;
data["digest"] = proxmox::tools::digest_to_hex(&digest).into(); data["digest"] = proxmox::tools::digest_to_hex(&digest).into();
@ -91,27 +94,29 @@ pub fn read_interface(name: String) -> Result<Value, Error> {
/// Deletable property name /// Deletable property name
pub enum DeletableProperty { pub enum DeletableProperty {
/// Delete the IPv4 address property. /// Delete the IPv4 address property.
address_v4, cidr,
/// Delete the IPv6 address property. /// Delete the IPv6 address property.
address_v6, cidr6,
/// Delete the IPv4 gateway property. /// Delete the IPv4 gateway property.
gateway_v4, gateway,
/// Delete the IPv6 gateway property. /// Delete the IPv6 gateway property.
gateway_v6, gateway6,
/// Delete the whole IPv4 configuration entry. /// Delete the whole IPv4 configuration entry.
method_v4, method,
/// Delete the whole IPv6 configuration entry. /// Delete the whole IPv6 configuration entry.
method_v6, method6,
/// Delete IPv4 comments /// Delete IPv4 comments
comments_v4, comments,
/// Delete IPv6 comments /// Delete IPv6 comments
comments_v6, comments6,
/// Delete mtu. /// Delete mtu.
mtu, mtu,
/// Delete auto flag /// Delete autostart flag
auto, autostart,
/// Delete bridge ports (set to 'none') /// Delete bridge ports (set to 'none')
bridge_ports, bridge_ports,
/// Delet bridge-vlan-aware flag
bridge_vlan_aware,
/// Delete bond-slaves (set to 'none') /// Delete bond-slaves (set to 'none')
bond_slaves, bond_slaves,
} }
@ -124,38 +129,51 @@ pub enum DeletableProperty {
node: { node: {
schema: NODE_SCHEMA, schema: NODE_SCHEMA,
}, },
name: { iface: {
schema: NETWORK_INTERFACE_NAME_SCHEMA, schema: NETWORK_INTERFACE_NAME_SCHEMA,
}, },
auto: { "type": {
description: "Interface type. If specified, need to match the current type.",
type: NetworkInterfaceType,
optional: true,
},
autostart: {
description: "Autostart interface.", description: "Autostart interface.",
type: bool, type: bool,
optional: true, optional: true,
}, },
method_v4: { method: {
type: NetworkConfigMethod, type: NetworkConfigMethod,
optional: true, optional: true,
}, },
method_v6: { method6: {
type: NetworkConfigMethod, type: NetworkConfigMethod,
optional: true, optional: true,
}, },
comments_v4: { comments: {
description: "Comments (inet, may span multiple lines)", description: "Comments (inet, may span multiple lines)",
type: String, type: String,
optional: true, optional: true,
}, },
comments_v6: { comments6: {
description: "Comments (inet5, may span multiple lines)", description: "Comments (inet5, may span multiple lines)",
type: String, type: String,
optional: true, optional: true,
}, },
address: { cidr: {
schema: CIDR_SCHEMA, schema: CIDR_V4_SCHEMA,
optional: true,
},
cidr6: {
schema: CIDR_V6_SCHEMA,
optional: true, optional: true,
}, },
gateway: { gateway: {
schema: IP_SCHEMA, schema: IP_V4_SCHEMA,
optional: true,
},
gateway6: {
schema: IP_V6_SCHEMA,
optional: true, optional: true,
}, },
mtu: { mtu: {
@ -169,6 +187,11 @@ pub enum DeletableProperty {
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
}, },
bridge_vlan_aware: {
description: "Enable bridge vlan support.",
type: bool,
optional: true,
},
bond_slaves: { bond_slaves: {
schema: NETWORK_INTERFACE_LIST_SCHEMA, schema: NETWORK_INTERFACE_LIST_SCHEMA,
optional: true, optional: true,
@ -188,24 +211,28 @@ pub enum DeletableProperty {
}, },
}, },
access: { access: {
permission: &Permission::Privilege(&["system", "network", "interfaces", "{name}"], PRIV_SYS_MODIFY, false), permission: &Permission::Privilege(&["system", "network", "interfaces", "{iface}"], PRIV_SYS_MODIFY, false),
}, },
)] )]
/// Update network interface config. /// Update network interface config.
pub fn update_interface( pub fn update_interface(
name: String, iface: String,
auto: Option<bool>, autostart: Option<bool>,
method_v4: Option<NetworkConfigMethod>, method: Option<NetworkConfigMethod>,
method_v6: Option<NetworkConfigMethod>, method6: Option<NetworkConfigMethod>,
comments_v4: Option<String>, comments: Option<String>,
comments_v6: Option<String>, comments6: Option<String>,
address: Option<String>, cidr: Option<String>,
gateway: Option<String>, gateway: Option<String>,
cidr6: Option<String>,
gateway6: Option<String>,
mtu: Option<u64>, mtu: Option<u64>,
bridge_ports: Option<Vec<String>>, bridge_ports: Option<Vec<String>>,
bridge_vlan_aware: Option<bool>,
bond_slaves: Option<Vec<String>>, bond_slaves: Option<Vec<String>>,
delete: Option<Vec<DeletableProperty>>, delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
param: Value,
) -> Result<(), Error> { ) -> Result<(), Error> {
let _lock = crate::tools::open_file_locked(network::NETWORK_LOCKFILE, std::time::Duration::new(10, 0))?; let _lock = crate::tools::open_file_locked(network::NETWORK_LOCKFILE, std::time::Duration::new(10, 0))?;
@ -218,71 +245,98 @@ pub fn update_interface(
} }
let current_gateway_v4 = config.interfaces.iter() let current_gateway_v4 = config.interfaces.iter()
.find(|(_, interface)| interface.gateway_v4.is_some()) .find(|(_, interface)| interface.gateway.is_some())
.map(|(name, _)| name.to_string()); .map(|(name, _)| name.to_string());
let current_gateway_v6 = config.interfaces.iter() let current_gateway_v6 = config.interfaces.iter()
.find(|(_, interface)| interface.gateway_v4.is_some()) .find(|(_, interface)| interface.gateway6.is_some())
.map(|(name, _)| name.to_string()); .map(|(name, _)| name.to_string());
let interface = config.lookup_mut(&name)?; let interface = config.lookup_mut(&iface)?;
if let Some(interface_type) = param.get("type") {
let interface_type: NetworkInterfaceType = serde_json::from_value(interface_type.clone())?;
if interface_type != interface.interface_type {
bail!("got unexpected interface type ({:?} != {:?})", interface_type, interface.interface_type);
}
}
if let Some(delete) = delete { if let Some(delete) = delete {
for delete_prop in delete { for delete_prop in delete {
match delete_prop { match delete_prop {
DeletableProperty::address_v4 => { interface.cidr_v4 = None; }, DeletableProperty::cidr => { interface.cidr = None; },
DeletableProperty::address_v6 => { interface.cidr_v6 = None; }, DeletableProperty::cidr6 => { interface.cidr6 = None; },
DeletableProperty::gateway_v4 => { interface.gateway_v4 = None; }, DeletableProperty::gateway => { interface.gateway = None; },
DeletableProperty::gateway_v6 => { interface.gateway_v6 = None; }, DeletableProperty::gateway6 => { interface.gateway6 = None; },
DeletableProperty::method_v4 => { interface.method_v4 = None; }, DeletableProperty::method => { interface.method = None; },
DeletableProperty::method_v6 => { interface.method_v6 = None; }, DeletableProperty::method6 => { interface.method6 = None; },
DeletableProperty::comments_v4 => { interface.comments_v4 = None; }, DeletableProperty::comments => { interface.comments = None; },
DeletableProperty::comments_v6 => { interface.comments_v6 = None; }, DeletableProperty::comments6 => { interface.comments6 = None; },
DeletableProperty::mtu => { interface.mtu = None; }, DeletableProperty::mtu => { interface.mtu = None; },
DeletableProperty::auto => { interface.auto = 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::bond_slaves => { interface.set_bond_slaves(Vec::new())?; } DeletableProperty::bond_slaves => { interface.set_bond_slaves(Vec::new())?; }
} }
} }
} }
if let Some(auto) = auto { interface.auto = auto; } if let Some(autostart) = autostart { interface.autostart = autostart; }
if method_v4.is_some() { interface.method_v4 = method_v4; } if method.is_some() { interface.method = method; }
if method_v6.is_some() { interface.method_v6 = method_v6; } if method6.is_some() { interface.method6 = method6; }
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 let Some(slaves) = bond_slaves { interface.set_bond_slaves(slaves)?; } if let Some(slaves) = bond_slaves { interface.set_bond_slaves(slaves)?; }
if let Some(address) = address { if let Some(cidr) = cidr {
let (_, _, is_v6) = network::parse_cidr(&address)?; let (_, _, is_v6) = network::parse_cidr(&cidr)?;
if is_v6 { if is_v6 { bail!("invalid address type (expected IPv4, got IPv6)"); }
interface.cidr_v6 = Some(address); interface.cidr = Some(cidr);
} else {
interface.cidr_v4 = Some(address);
} }
if let Some(cidr6) = cidr6 {
let (_, _, is_v6) = network::parse_cidr(&cidr6)?;
if !is_v6 { bail!("invalid address type (expected IPv6, got IPv4)"); }
interface.cidr6 = Some(cidr6);
} }
if let Some(gateway) = gateway { if let Some(gateway) = gateway {
let is_v6 = gateway.contains(':'); let is_v6 = gateway.contains(':');
if is_v6 { if is_v6 { bail!("invalid address type (expected IPv4, got IPv6)"); }
if let Some(current_gateway_v6) = current_gateway_v6 {
if current_gateway_v6 != name {
bail!("Default IPv6 gateway already exists on interface '{}'", current_gateway_v6);
}
}
interface.gateway_v6 = Some(gateway);
} else {
if let Some(current_gateway_v4) = current_gateway_v4 { if let Some(current_gateway_v4) = current_gateway_v4 {
if current_gateway_v4 != name { if current_gateway_v4 != iface {
bail!("Default IPv4 gateway already exists on interface '{}'", current_gateway_v4); bail!("Default IPv4 gateway already exists on interface '{}'", current_gateway_v4);
} }
} }
interface.gateway_v4 = Some(gateway); interface.gateway = Some(gateway);
}
} }
if comments_v4.is_some() { interface.comments_v4 = comments_v4; } if let Some(gateway6) = gateway6 {
if comments_v6.is_some() { interface.comments_v6 = comments_v6; } let is_v6 = gateway6.contains(':');
if !is_v6 { bail!("invalid address type (expected IPv6, got IPv4)"); }
if let Some(current_gateway_v6) = current_gateway_v6 {
if current_gateway_v6 != iface {
bail!("Default IPv6 gateway already exists on interface '{}'", current_gateway_v6);
}
}
interface.gateway6 = Some(gateway6);
}
if comments.is_some() { interface.comments = comments; }
if comments6.is_some() { interface.comments6 = comments6; }
if interface.cidr.is_some() || interface.gateway.is_some() {
interface.method = Some(NetworkConfigMethod::Static);
} else {
interface.method = Some(NetworkConfigMethod::Manual);
}
if interface.cidr6.is_some() || interface.gateway6.is_some() {
interface.method6 = Some(NetworkConfigMethod::Static);
} else {
interface.method6 = Some(NetworkConfigMethod::Manual);
}
network::save_config(&config)?; network::save_config(&config)?;
@ -296,7 +350,7 @@ pub fn update_interface(
node: { node: {
schema: NODE_SCHEMA, schema: NODE_SCHEMA,
}, },
name: { iface: {
schema: NETWORK_INTERFACE_NAME_SCHEMA, schema: NETWORK_INTERFACE_NAME_SCHEMA,
}, },
digest: { digest: {
@ -306,11 +360,11 @@ pub fn update_interface(
}, },
}, },
access: { access: {
permission: &Permission::Privilege(&["system", "network", "interfaces", "{name}"], PRIV_SYS_MODIFY, false), permission: &Permission::Privilege(&["system", "network", "interfaces", "{iface}"], PRIV_SYS_MODIFY, false),
}, },
)] )]
/// Remove network interface configuration. /// Remove network interface configuration.
pub fn delete_interface(name: String, digest: Option<String>) -> Result<(), Error> { pub fn delete_interface(iface: String, digest: Option<String>) -> Result<(), Error> {
let _lock = crate::tools::open_file_locked(network::NETWORK_LOCKFILE, std::time::Duration::new(10, 0))?; let _lock = crate::tools::open_file_locked(network::NETWORK_LOCKFILE, std::time::Duration::new(10, 0))?;
@ -321,9 +375,9 @@ pub fn delete_interface(name: String, digest: Option<String>) -> Result<(), Erro
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
} }
let _interface = config.lookup(&name)?; // check if interface exists let _interface = config.lookup(&iface)?; // check if interface exists
config.interfaces.remove(&name); config.interfaces.remove(&iface);
network::save_config(&config)?; network::save_config(&config)?;
@ -331,6 +385,7 @@ pub fn delete_interface(name: String, digest: Option<String>) -> Result<(), Erro
} }
#[api( #[api(
protected: true,
input: { input: {
properties: { properties: {
node: { node: {
@ -343,15 +398,23 @@ pub fn delete_interface(name: String, digest: Option<String>) -> Result<(), Erro
}, },
)] )]
/// Reload network configuration (requires ifupdown2). /// Reload network configuration (requires ifupdown2).
pub fn reload_network_config() -> Result<(), Error> { pub async fn reload_network_config(
rpcenv: &mut dyn RpcEnvironment,
) -> Result<String, Error> {
network::assert_ifupdown2_installed()?; network::assert_ifupdown2_installed()?;
let username = rpcenv.get_user().unwrap();
let upid_str = WorkerTask::spawn("srvreload", Some(String::from("networking")), &username.clone(), true, |_worker| async {
let _ = std::fs::rename(network::NETWORK_INTERFACES_NEW_FILENAME, network::NETWORK_INTERFACES_FILENAME); let _ = std::fs::rename(network::NETWORK_INTERFACES_NEW_FILENAME, network::NETWORK_INTERFACES_FILENAME);
network::network_reload()?; network::network_reload()?;
Ok(()) Ok(())
})?;
Ok(upid_str)
} }
#[api( #[api(
@ -383,4 +446,4 @@ pub const ROUTER: Router = Router::new()
.get(&API_METHOD_LIST_NETWORK_DEVICES) .get(&API_METHOD_LIST_NETWORK_DEVICES)
.put(&API_METHOD_RELOAD_NETWORK_CONFIG) .put(&API_METHOD_RELOAD_NETWORK_CONFIG)
.delete(&API_METHOD_REVERT_NETWORK_CONFIG) .delete(&API_METHOD_REVERT_NETWORK_CONFIG)
.match_all("name", &ITEM_ROUTER); .match_all("iface", &ITEM_ROUTER);

View File

@ -557,9 +557,7 @@ pub enum NetworkInterfaceType {
/// Loopback /// Loopback
Loopback, Loopback,
/// Physical Ethernet device /// Physical Ethernet device
Ethernet, Eth,
/// Name looks like a physical ethernet device, but device is not found
Vanished,
/// Linux Bridge /// Linux Bridge
Bridge, Bridge,
/// Linux Bond /// Linux Bond
@ -587,18 +585,34 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
name: { name: {
schema: NETWORK_INTERFACE_NAME_SCHEMA, schema: NETWORK_INTERFACE_NAME_SCHEMA,
}, },
interface_type: { "type": {
type: NetworkInterfaceType, type: NetworkInterfaceType,
}, },
method_v4: { method: {
type: NetworkConfigMethod, type: NetworkConfigMethod,
optional: true, optional: true,
}, },
method_v6: { method6: {
type: NetworkConfigMethod, type: NetworkConfigMethod,
optional: true, optional: true,
}, },
options_v4: { cidr: {
schema: CIDR_V4_SCHEMA,
optional: true,
},
cidr6: {
schema: CIDR_V6_SCHEMA,
optional: true,
},
gateway: {
schema: IP_V4_SCHEMA,
optional: true,
},
gateway6: {
schema: IP_V6_SCHEMA,
optional: true,
},
options: {
description: "Option list (inet)", description: "Option list (inet)",
type: Array, type: Array,
items: { items: {
@ -606,7 +620,7 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
type: String, type: String,
}, },
}, },
options_v6: { options6: {
description: "Option list (inet6)", description: "Option list (inet6)",
type: Array, type: Array,
items: { items: {
@ -614,12 +628,12 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
type: String, type: String,
}, },
}, },
comments_v4: { comments: {
description: "Comments (inet, may span multiple lines)", description: "Comments (inet, may span multiple lines)",
type: String, type: String,
optional: true, optional: true,
}, },
comments_v6: { comments6: {
description: "Comments (inet6, may span multiple lines)", description: "Comments (inet6, may span multiple lines)",
type: String, type: String,
optional: true, optional: true,
@ -638,39 +652,41 @@ pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = ArraySchema::new(
/// Network Interface configuration /// Network Interface configuration
pub struct Interface { pub struct Interface {
/// Autostart interface /// Autostart interface
pub auto: bool, #[serde(rename = "autostart")]
pub autostart: bool,
/// Interface is active (UP) /// Interface is active (UP)
pub active: bool, pub active: bool,
/// Interface name /// Interface name
pub name: String, pub name: String,
/// Interface type /// Interface type
#[serde(rename = "type")]
pub interface_type: NetworkInterfaceType, pub interface_type: NetworkInterfaceType,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub method_v4: Option<NetworkConfigMethod>, pub method: Option<NetworkConfigMethod>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub method_v6: Option<NetworkConfigMethod>, pub method6: Option<NetworkConfigMethod>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// IPv4 address with netmask /// IPv4 address with netmask
pub cidr_v4: Option<String>, pub cidr: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// IPv4 gateway /// IPv4 gateway
pub gateway_v4: Option<String>, pub gateway: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// IPv6 address with netmask /// IPv6 address with netmask
pub cidr_v6: Option<String>, pub cidr6: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// IPv6 gateway /// IPv6 gateway
pub gateway_v6: Option<String>, pub gateway6: Option<String>,
#[serde(skip_serializing_if="Vec::is_empty")] #[serde(skip_serializing_if="Vec::is_empty")]
pub options_v4: Vec<String>, pub options: Vec<String>,
#[serde(skip_serializing_if="Vec::is_empty")] #[serde(skip_serializing_if="Vec::is_empty")]
pub options_v6: Vec<String>, pub options6: Vec<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub comments_v4: Option<String>, pub comments: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub comments_v6: Option<String>, pub comments6: Option<String>,
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
/// Maximum Transmission Unit /// Maximum Transmission Unit
@ -678,6 +694,9 @@ pub struct Interface {
#[serde(skip_serializing_if="Option::is_none")] #[serde(skip_serializing_if="Option::is_none")]
pub bridge_ports: Option<Vec<String>>, pub bridge_ports: Option<Vec<String>>,
/// Enable bridge vlan support.
#[serde(skip_serializing_if="Option::is_none")]
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 bond_slaves: Option<Vec<String>>,

View File

@ -140,7 +140,9 @@ impl Shell {
continue; continue;
} }
}; };
let _ = handle_command(helper.cmd_def(), "", args, None);
let rpcenv = CliEnvironment::new();
let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
self.rl.add_history_entry(line); self.rl.add_history_entry(line);
self.update_prompt()?; self.update_prompt()?;
} }

View File

@ -83,7 +83,8 @@ fn main() -> Result<(), Error> {
let args = shellword_split(&line)?; let args = shellword_split(&line)?;
let _ = handle_command(helper.cmd_def(), "", args, None); let rpcenv = CliEnvironment::new();
let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
rl.add_history_entry(line); rl.add_history_entry(line);
} }

View File

@ -2421,7 +2421,8 @@ fn main() {
.insert("catalog", catalog_mgmt_cli()) .insert("catalog", catalog_mgmt_cli())
.insert("task", task_mgmt_cli()); .insert("task", task_mgmt_cli());
run_cli_command(cmd_def, Some(|future| { let rpcenv = CliEnvironment::new();
run_cli_command(cmd_def, rpcenv, Some(|future| {
proxmox_backup::tools::runtime::main(future) proxmox_backup::tools::runtime::main(future)
})); }));
} }

View File

@ -273,10 +273,10 @@ fn list_network_devices(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Re
fn render_address(_value: &Value, record: &Value) -> Result<String, Error> { fn render_address(_value: &Value, record: &Value) -> Result<String, Error> {
let mut text = String::new(); let mut text = String::new();
if let Some(cidr) = record["cidr_v4"].as_str() { if let Some(cidr) = record["cidr"].as_str() {
text.push_str(cidr); text.push_str(cidr);
} }
if let Some(cidr) = record["cidr_v6"].as_str() { if let Some(cidr) = record["cidr6"].as_str() {
if !text.is_empty() { text.push('\n'); } if !text.is_empty() { text.push('\n'); }
text.push_str(cidr); text.push_str(cidr);
} }
@ -287,10 +287,10 @@ fn list_network_devices(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Re
fn render_gateway(_value: &Value, record: &Value) -> Result<String, Error> { fn render_gateway(_value: &Value, record: &Value) -> Result<String, Error> {
let mut text = String::new(); let mut text = String::new();
if let Some(gateway) = record["gateway_v4"].as_str() { if let Some(gateway) = record["gateway"].as_str() {
text.push_str(gateway); text.push_str(gateway);
} }
if let Some(gateway) = record["gateway_v6"].as_str() { if let Some(gateway) = record["gateway6"].as_str() {
if !text.is_empty() { text.push('\n'); } if !text.is_empty() { text.push('\n'); }
text.push_str(gateway); text.push_str(gateway);
} }
@ -299,13 +299,13 @@ fn list_network_devices(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Re
} }
let options = default_table_format_options() let options = default_table_format_options()
.column(ColumnConfig::new("interface_type").header("type")) .column(ColumnConfig::new("type").header("type"))
.column(ColumnConfig::new("name")) .column(ColumnConfig::new("name"))
.column(ColumnConfig::new("auto")) .column(ColumnConfig::new("autostart"))
.column(ColumnConfig::new("method_v4")) .column(ColumnConfig::new("method"))
.column(ColumnConfig::new("method_v6")) .column(ColumnConfig::new("method6"))
.column(ColumnConfig::new("cidr_v4").header("address").renderer(render_address)) .column(ColumnConfig::new("cidr").header("address").renderer(render_address))
.column(ColumnConfig::new("gateway_v4").header("gateway").renderer(render_gateway)); .column(ColumnConfig::new("gateway").header("gateway").renderer(render_gateway));
format_and_print_result_full(&mut data, info.returns, &output_format, &options); format_and_print_result_full(&mut data, info.returns, &output_format, &options);
@ -347,15 +347,15 @@ fn network_commands() -> CommandLineInterface {
"update", "update",
CliCommand::new(&api2::node::network::API_METHOD_UPDATE_INTERFACE) CliCommand::new(&api2::node::network::API_METHOD_UPDATE_INTERFACE)
.fixed_param("node", String::from("localhost")) .fixed_param("node", String::from("localhost"))
.arg_param(&["name"]) .arg_param(&["iface"])
.completion_cb("name", config::network::complete_interface_name) .completion_cb("iface", config::network::complete_interface_name)
) )
.insert( .insert(
"remove", "remove",
CliCommand::new(&api2::node::network::API_METHOD_DELETE_INTERFACE) CliCommand::new(&api2::node::network::API_METHOD_DELETE_INTERFACE)
.fixed_param("node", String::from("localhost")) .fixed_param("node", String::from("localhost"))
.arg_param(&["name"]) .arg_param(&["iface"])
.completion_cb("name", config::network::complete_interface_name) .completion_cb("iface", config::network::complete_interface_name)
) )
.insert( .insert(
"revert", "revert",
@ -839,7 +839,10 @@ fn main() {
.completion_cb("remote-store", complete_remote_datastore_name) .completion_cb("remote-store", complete_remote_datastore_name)
); );
proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def)); let mut rpcenv = CliEnvironment::new();
rpcenv.set_user(Some(String::from("root@pam")));
proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def, rpcenv));
} }
// shell completion helper // shell completion helper

View File

@ -519,5 +519,6 @@ fn main() {
.completion_cb("archive", tools::complete_file_name) .completion_cb("archive", tools::complete_file_name)
); );
run_cli_command(cmd_def, None); let rpcenv = CliEnvironment::new();
run_cli_command(cmd_def, rpcenv, None);
} }

View File

@ -22,27 +22,28 @@ impl Interface {
Self { Self {
name, name,
interface_type: NetworkInterfaceType::Unknown, interface_type: NetworkInterfaceType::Unknown,
auto: false, autostart: false,
active: false, active: false,
method_v4: None, method: None,
method_v6: None, method6: None,
cidr_v4: None, cidr: None,
gateway_v4: None, gateway: None,
cidr_v6: None, cidr6: None,
gateway_v6: None, gateway6: None,
options_v4: Vec::new(), options: Vec::new(),
options_v6: Vec::new(), options6: Vec::new(),
comments_v4: None, comments: None,
comments_v6: None, comments6: None,
mtu: None, mtu: None,
bridge_ports: None, bridge_ports: None,
bridge_vlan_aware: None,
bond_slaves: None, bond_slaves: None,
} }
} }
fn set_method_v4(&mut self, method: NetworkConfigMethod) -> Result<(), Error> { fn set_method_v4(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
if self.method_v4.is_none() { if self.method.is_none() {
self.method_v4 = Some(method); self.method = Some(method);
} else { } else {
bail!("inet configuration method already set."); bail!("inet configuration method already set.");
} }
@ -50,8 +51,8 @@ impl Interface {
} }
fn set_method_v6(&mut self, method: NetworkConfigMethod) -> Result<(), Error> { fn set_method_v6(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
if self.method_v6.is_none() { if self.method6.is_none() {
self.method_v6 = Some(method); self.method6 = Some(method);
} else { } else {
bail!("inet6 configuration method already set."); bail!("inet6 configuration method already set.");
} }
@ -59,8 +60,8 @@ impl Interface {
} }
fn set_cidr_v4(&mut self, address: String) -> Result<(), Error> { fn set_cidr_v4(&mut self, address: String) -> Result<(), Error> {
if self.cidr_v4.is_none() { if self.cidr.is_none() {
self.cidr_v4 = Some(address); self.cidr = Some(address);
} else { } else {
bail!("duplicate IPv4 address."); bail!("duplicate IPv4 address.");
} }
@ -68,8 +69,8 @@ impl Interface {
} }
fn set_gateway_v4(&mut self, gateway: String) -> Result<(), Error> { fn set_gateway_v4(&mut self, gateway: String) -> Result<(), Error> {
if self.gateway_v4.is_none() { if self.gateway.is_none() {
self.gateway_v4 = Some(gateway); self.gateway = Some(gateway);
} else { } else {
bail!("duplicate IPv4 gateway."); bail!("duplicate IPv4 gateway.");
} }
@ -77,8 +78,8 @@ impl Interface {
} }
fn set_cidr_v6(&mut self, address: String) -> Result<(), Error> { fn set_cidr_v6(&mut self, address: String) -> Result<(), Error> {
if self.cidr_v6.is_none() { if self.cidr6.is_none() {
self.cidr_v6 = Some(address); self.cidr6 = Some(address);
} else { } else {
bail!("duplicate IPv6 address."); bail!("duplicate IPv6 address.");
} }
@ -86,8 +87,8 @@ impl Interface {
} }
fn set_gateway_v6(&mut self, gateway: String) -> Result<(), Error> { fn set_gateway_v6(&mut self, gateway: String) -> Result<(), Error> {
if self.gateway_v6.is_none() { if self.gateway6.is_none() {
self.gateway_v6 = Some(gateway); self.gateway6 = Some(gateway);
} else { } else {
bail!("duplicate IPv4 gateway."); bail!("duplicate IPv4 gateway.");
} }
@ -124,20 +125,23 @@ impl Interface {
match self.interface_type { match self.interface_type {
NetworkInterfaceType::Bridge => { NetworkInterfaceType::Bridge => {
if let Some(true) = self.bridge_vlan_aware {
writeln!(w, "\tbridge-vlan-aware yes")?;
}
if let Some(ref ports) = self.bridge_ports { if let Some(ref ports) = self.bridge_ports {
if ports.is_empty() { if ports.is_empty() {
writeln!(w, " bridge-ports none")?; writeln!(w, "\tbridge-ports none")?;
} else { } else {
writeln!(w, " bridge-ports {}", ports.join(" "))?; writeln!(w, "\tbridge-ports {}", ports.join(" "))?;
} }
} }
} }
NetworkInterfaceType::Bond => { NetworkInterfaceType::Bond => {
if let Some(ref slaves) = self.bond_slaves { if let Some(ref slaves) = self.bond_slaves {
if slaves.is_empty() { if slaves.is_empty() {
writeln!(w, " bond-slaves none")?; writeln!(w, "\tbond-slaves none")?;
} else { } else {
writeln!(w, " bond-slaves {}", slaves.join(" "))?; writeln!(w, "\tbond-slaves {}", slaves.join(" "))?;
} }
} }
} }
@ -145,7 +149,7 @@ impl Interface {
} }
if let Some(mtu) = self.mtu { if let Some(mtu) = self.mtu {
writeln!(w, " mtu {}", mtu)?; writeln!(w, "\tmtu {}", mtu)?;
} }
Ok(()) Ok(())
@ -154,19 +158,19 @@ impl Interface {
/// Write attributes dependening on address family inet (IPv4) /// 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 {
writeln!(w, " address {}", address)?; writeln!(w, "\taddress {}", address)?;
} }
if let Some(gateway) = &self.gateway_v4 { if let Some(gateway) = &self.gateway {
writeln!(w, " gateway {}", gateway)?; writeln!(w, "\tgateway {}", gateway)?;
} }
} }
for option in &self.options_v4 { for option in &self.options {
writeln!(w, " {}", option)?; writeln!(w, "\t{}", option)?;
} }
if let Some(ref comments) = self.comments_v4 { if let Some(ref comments) = self.comments {
for comment in comments.lines() { for comment in comments.lines() {
writeln!(w, "#{}", comment)?; writeln!(w, "#{}", comment)?;
} }
@ -178,19 +182,19 @@ impl Interface {
/// Write attributes dependening on address family inet6 (IPv6) /// 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.cidr6 {
writeln!(w, " address {}", address)?; writeln!(w, "\taddress {}", address)?;
} }
if let Some(gateway) = &self.gateway_v6 { if let Some(gateway) = &self.gateway6 {
writeln!(w, " gateway {}", gateway)?; writeln!(w, "\tgateway {}", gateway)?;
} }
} }
for option in &self.options_v6 { for option in &self.options6 {
writeln!(w, " {}", option)?; writeln!(w, "\t{}", option)?;
} }
if let Some(ref comments) = self.comments_v6 { if let Some(ref comments) = self.comments6 {
for comment in comments.lines() { for comment in comments.lines() {
writeln!(w, "#{}", comment)?; writeln!(w, "#{}", comment)?;
} }
@ -204,30 +208,31 @@ impl Interface {
// Note: use match to make sure we considered all values at compile time // Note: use match to make sure we considered all values at compile time
match self { match self {
Interface { Interface {
method_v4, method,
method_v6, method6,
options_v4, options,
options_v6, options6,
comments_v4, comments,
comments_v6, comments6,
// the rest does not matter // the rest does not matter
name: _name, name: _name,
interface_type: _interface_type, interface_type: _interface_type,
auto: _auto, autostart: _autostart,
active: _active, active: _active,
cidr_v4: _cidr_v4, cidr: _cidr,
cidr_v6: _cidr_v6, cidr6: _cidr6,
gateway_v4: _gateway_v4, gateway: _gateway,
gateway_v6: _gateway_v6, gateway6: _gateway6,
mtu: _mtu, mtu: _mtu,
bridge_ports: _bridge_ports, bridge_ports: _bridge_ports,
bridge_vlan_aware: _bridge_vlan_aware,
bond_slaves: _bond_slaves, bond_slaves: _bond_slaves,
} => { } => {
method_v4 == method_v6 method == method6
&& comments_v4.is_none() && comments.is_none()
&& comments_v6.is_none() && comments6.is_none()
&& options_v4.is_empty() && options.is_empty()
&& options_v6.is_empty() && options6.is_empty()
} }
} }
} }
@ -243,36 +248,46 @@ impl Interface {
} }
} }
if self.method_v4.is_none() && self.method_v6.is_none() { return Ok(()); } if self.method.is_none() && self.method6.is_none() { return Ok(()); }
if self.auto { if self.autostart {
writeln!(w, "auto {}", self.name)?; writeln!(w, "auto {}", self.name)?;
} }
if self.combine_entry() { if self.combine_entry() {
if let Some(method) = self.method_v4 { if let Some(method) = self.method {
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)?; self.write_iface_attributes(w)?;
writeln!(w)?; writeln!(w)?;
} }
} else { return Ok(());
if let Some(method) = self.method_v4 { }
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(method) = self.method_v6 {
writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method))?; if let Some(method6) = self.method6 {
self.write_iface_attributes_v6(w, method)?; let mut skip_v6 = false; // avoid empty inet6 manual entry
if self.method_v4.is_none() { // only write common attributes once if self.method.is_some() && method6 == NetworkConfigMethod::Manual {
if self.comments6.is_none() && self.options6.is_empty() { skip_v6 = true; }
}
if !skip_v6 {
writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method6))?;
self.write_iface_attributes_v6(w, method6)?;
if self.method.is_none() { // only write common attributes once
self.write_iface_attributes(w)?; self.write_iface_attributes(w)?;
} }
writeln!(w)?; writeln!(w)?;
} }
} }
Ok(()) Ok(())
} }
} }

View File

@ -23,6 +23,7 @@ pub enum Token {
Attribute, Attribute,
MTU, MTU,
BridgePorts, BridgePorts,
BridgeVlanAware,
BondSlaves, BondSlaves,
EOF, EOF,
} }
@ -44,6 +45,8 @@ lazy_static! {
map.insert("mtu", Token::MTU); map.insert("mtu", Token::MTU);
map.insert("bridge-ports", Token::BridgePorts); map.insert("bridge-ports", Token::BridgePorts);
map.insert("bridge_ports", Token::BridgePorts); map.insert("bridge_ports", Token::BridgePorts);
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 map

View File

@ -137,6 +137,21 @@ impl <R: BufRead> NetworkParser<R> {
Ok(mtu) Ok(mtu)
} }
fn parse_yes_no(&mut self) -> Result<bool, Error> {
let text = self.next_text()?;
let value = match text.to_lowercase().as_str() {
"yes" => true,
"no" => false,
_ => {
bail!("unable to bool value '{}' - (expected yes/no)", text);
}
};
self.eat(Token::Newline)?;
Ok(value)
}
fn parse_to_eol(&mut self) -> Result<String, Error> { fn parse_to_eol(&mut self) -> Result<String, Error> {
let mut line = String::new(); let mut line = String::new();
loop { loop {
@ -182,15 +197,15 @@ impl <R: BufRead> NetworkParser<R> {
Token::Comment => { Token::Comment => {
let comment = self.eat(Token::Comment)?; let comment = self.eat(Token::Comment)?;
if !address_family_v4 && address_family_v6 { if !address_family_v4 && address_family_v6 {
let mut comments = interface.comments_v6.take().unwrap_or(String::new()); let mut comments = interface.comments6.take().unwrap_or(String::new());
if !comments.is_empty() { comments.push('\n'); } if !comments.is_empty() { comments.push('\n'); }
comments.push_str(&comment); comments.push_str(&comment);
interface.comments_v6 = Some(comments); interface.comments6 = Some(comments);
} else { } else {
let mut comments = interface.comments_v4.take().unwrap_or(String::new()); let mut comments = interface.comments.take().unwrap_or(String::new());
if !comments.is_empty() { comments.push('\n'); } if !comments.is_empty() { comments.push('\n'); }
comments.push_str(&comment); comments.push_str(&comment);
interface.comments_v4 = Some(comments); interface.comments = Some(comments);
} }
self.eat(Token::Newline)?; self.eat(Token::Newline)?;
continue; continue;
@ -207,6 +222,11 @@ 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::BridgeVlanAware => {
self.eat(Token::BridgeVlanAware)?;
let bridge_vlan_aware = self.parse_yes_no()?;
interface.bridge_vlan_aware = Some(bridge_vlan_aware);
}
Token::BridgePorts => { Token::BridgePorts => {
self.eat(Token::BridgePorts)?; self.eat(Token::BridgePorts)?;
let ports = self.parse_iface_list()?; let ports = self.parse_iface_list()?;
@ -225,9 +245,9 @@ impl <R: BufRead> NetworkParser<R> {
let option = self.parse_to_eol()?; let option = self.parse_to_eol()?;
if !option.is_empty() { if !option.is_empty() {
if !address_family_v4 && address_family_v6 { if !address_family_v4 && address_family_v6 {
interface.options_v6.push(option); interface.options6.push(option);
} else { } else {
interface.options_v4.push(option); interface.options.push(option);
} }
}; };
}, },
@ -335,7 +355,7 @@ impl <R: BufRead> NetworkParser<R> {
for iface in auto_flag.iter() { for iface in auto_flag.iter() {
if let Some(interface) = config.interfaces.get_mut(iface) { if let Some(interface) = config.interfaces.get_mut(iface) {
interface.auto = true; interface.autostart = true;
} }
} }
@ -349,13 +369,13 @@ impl <R: BufRead> NetworkParser<R> {
for (iface, active) in existing_interfaces.iter() { for (iface, active) in existing_interfaces.iter() {
if let Some(interface) = config.interfaces.get_mut(iface) { if let Some(interface) = config.interfaces.get_mut(iface) {
interface.active = *active; interface.active = *active;
if interface.interface_type == NetworkInterfaceType::Unknown { if interface.interface_type == NetworkInterfaceType::Unknown && PHYSICAL_NIC_REGEX.is_match(iface) {
interface.interface_type = NetworkInterfaceType::Ethernet; interface.interface_type = NetworkInterfaceType::Eth;
} }
} else if PHYSICAL_NIC_REGEX.is_match(iface) { // also add all physical NICs } else if PHYSICAL_NIC_REGEX.is_match(iface) { // also add all physical NICs
let mut interface = Interface::new(iface.clone()); let mut interface = Interface::new(iface.clone());
interface.set_method_v4(NetworkConfigMethod::Manual)?; interface.set_method_v4(NetworkConfigMethod::Manual)?;
interface.interface_type = NetworkInterfaceType::Ethernet; interface.interface_type = NetworkInterfaceType::Eth;
interface.active = *active; interface.active = *active;
config.interfaces.insert(interface.name.clone(), interface); config.interfaces.insert(interface.name.clone(), interface);
config.order.push(NetworkOrderEntry::Iface(iface.to_string())); config.order.push(NetworkOrderEntry::Iface(iface.to_string()));
@ -378,7 +398,7 @@ impl <R: BufRead> NetworkParser<R> {
continue; continue;
} }
if PHYSICAL_NIC_REGEX.is_match(name) { if PHYSICAL_NIC_REGEX.is_match(name) {
interface.interface_type = NetworkInterfaceType::Vanished; interface.interface_type = NetworkInterfaceType::Eth;
continue; continue;
} }
} }
@ -387,7 +407,7 @@ impl <R: BufRead> NetworkParser<R> {
let mut interface = Interface::new(String::from("lo")); let mut interface = Interface::new(String::from("lo"));
interface.set_method_v4(NetworkConfigMethod::Loopback)?; interface.set_method_v4(NetworkConfigMethod::Loopback)?;
interface.interface_type = NetworkInterfaceType::Loopback; interface.interface_type = NetworkInterfaceType::Loopback;
interface.auto = true; interface.autostart = true;
config.interfaces.insert(interface.name.clone(), interface); config.interfaces.insert(interface.name.clone(), interface);
// Note: insert 'lo' as first interface after initial comments // Note: insert 'lo' as first interface after initial comments

View File

@ -30,7 +30,8 @@ Ext.define('PBS.SystemConfiguration', {
minHeight: 200, minHeight: 200,
title: gettext('Interfaces'), title: gettext('Interfaces'),
xtype: 'proxmoxNodeNetworkView', xtype: 'proxmoxNodeNetworkView',
types: ['bond'], showApplyBtn: true,
types: ['bond', 'bridge', 'vlan'],
nodename: 'localhost' nodename: 'localhost'
}, },
{ {