2020-04-20 12:15:57 +00:00
|
|
|
use std::io::{Write};
|
2020-05-08 09:15:00 +00:00
|
|
|
use std::collections::{HashSet, HashMap, BTreeMap};
|
2020-04-20 12:15:57 +00:00
|
|
|
|
2020-04-22 08:54:07 +00:00
|
|
|
use anyhow::{Error, format_err, bail};
|
2020-05-07 12:07:45 +00:00
|
|
|
use serde::de::{value, IntoDeserializer, Deserialize};
|
2020-05-08 13:51:47 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use regex::Regex;
|
2020-04-20 12:15:57 +00:00
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
mod helper;
|
2020-04-20 16:10:15 +00:00
|
|
|
pub use helper::*;
|
2020-04-20 12:15:57 +00:00
|
|
|
|
|
|
|
mod lexer;
|
|
|
|
pub use lexer::*;
|
|
|
|
|
|
|
|
mod parser;
|
|
|
|
pub use parser::*;
|
|
|
|
|
2020-05-07 12:07:45 +00:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 12:15:57 +00:00
|
|
|
|
|
|
|
impl Interface {
|
|
|
|
|
2020-04-20 16:10:15 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-04-21 09:06:22 +00:00
|
|
|
Self {
|
2020-04-20 12:15:57 +00:00
|
|
|
name,
|
2020-04-23 06:45:03 +00:00
|
|
|
interface_type: NetworkInterfaceType::Unknown,
|
2020-05-06 05:51:05 +00:00
|
|
|
autostart: false,
|
2020-04-21 09:46:56 +00:00
|
|
|
active: false,
|
2020-05-06 05:51:05 +00:00
|
|
|
method: None,
|
|
|
|
method6: None,
|
|
|
|
cidr: None,
|
|
|
|
gateway: None,
|
|
|
|
cidr6: None,
|
|
|
|
gateway6: None,
|
|
|
|
options: Vec::new(),
|
|
|
|
options6: Vec::new(),
|
|
|
|
comments: None,
|
|
|
|
comments6: None,
|
2020-04-23 05:07:14 +00:00
|
|
|
mtu: None,
|
2020-04-23 07:24:17 +00:00
|
|
|
bridge_ports: None,
|
2020-05-06 05:51:05 +00:00
|
|
|
bridge_vlan_aware: None,
|
2020-05-07 12:07:45 +00:00
|
|
|
slaves: None,
|
|
|
|
bond_mode: None,
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn set_method_v4(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.method.is_none() {
|
|
|
|
self.method = Some(method);
|
2020-04-20 16:10:15 +00:00
|
|
|
} else {
|
|
|
|
bail!("inet configuration method already set.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn set_method_v6(&mut self, method: NetworkConfigMethod) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.method6.is_none() {
|
|
|
|
self.method6 = Some(method);
|
2020-04-20 16:10:15 +00:00
|
|
|
} else {
|
|
|
|
bail!("inet6 configuration method already set.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:45:13 +00:00
|
|
|
fn set_cidr_v4(&mut self, address: String) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.cidr.is_none() {
|
|
|
|
self.cidr = Some(address);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 address.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_gateway_v4(&mut self, gateway: String) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.gateway.is_none() {
|
|
|
|
self.gateway = Some(gateway);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 gateway.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:45:13 +00:00
|
|
|
fn set_cidr_v6(&mut self, address: String) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.cidr6.is_none() {
|
|
|
|
self.cidr6 = Some(address);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv6 address.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_gateway_v6(&mut self, gateway: String) -> Result<(), Error> {
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.gateway6.is_none() {
|
|
|
|
self.gateway6 = Some(gateway);
|
2020-04-20 12:15:57 +00:00
|
|
|
} else {
|
|
|
|
bail!("duplicate IPv4 gateway.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:43:38 +00:00
|
|
|
fn set_interface_type(&mut self, interface_type: NetworkInterfaceType) -> Result<(), Error> {
|
|
|
|
if self.interface_type == NetworkInterfaceType::Unknown {
|
|
|
|
self.interface_type = interface_type;
|
|
|
|
} else if self.interface_type != interface_type {
|
|
|
|
bail!("interface type already defined - cannot change from {:?} to {:?}", self.interface_type, interface_type);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:21:27 +00:00
|
|
|
pub(crate) fn set_bridge_ports(&mut self, ports: Vec<String>) -> Result<(), Error> {
|
|
|
|
if self.interface_type != NetworkInterfaceType::Bridge {
|
|
|
|
bail!("interface '{}' is no bridge (type is {:?})", self.name, self.interface_type);
|
|
|
|
}
|
|
|
|
self.bridge_ports = Some(ports);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn set_bond_slaves(&mut self, slaves: Vec<String>) -> Result<(), Error> {
|
|
|
|
if self.interface_type != NetworkInterfaceType::Bond {
|
|
|
|
bail!("interface '{}' is no bond (type is {:?})", self.name, self.interface_type);
|
|
|
|
}
|
2020-05-07 12:07:45 +00:00
|
|
|
self.slaves = Some(slaves);
|
2020-04-23 09:21:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
/// Write attributes not dependening on address family
|
|
|
|
fn write_iface_attributes(&self, w: &mut dyn Write) -> Result<(), Error> {
|
2020-04-23 07:24:17 +00:00
|
|
|
|
2020-05-08 07:58:03 +00:00
|
|
|
static EMPTY_LIST: Vec<String> = Vec::new();
|
|
|
|
|
2020-04-23 07:24:17 +00:00
|
|
|
match self.interface_type {
|
|
|
|
NetworkInterfaceType::Bridge => {
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(true) = self.bridge_vlan_aware {
|
|
|
|
writeln!(w, "\tbridge-vlan-aware yes")?;
|
|
|
|
}
|
2020-05-08 07:58:03 +00:00
|
|
|
let ports = self.bridge_ports.as_ref().unwrap_or(&EMPTY_LIST);
|
|
|
|
if ports.is_empty() {
|
|
|
|
writeln!(w, "\tbridge-ports none")?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, "\tbridge-ports {}", ports.join(" "))?;
|
2020-04-23 07:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:31:10 +00:00
|
|
|
NetworkInterfaceType::Bond => {
|
2020-05-07 12:07:45 +00:00
|
|
|
let mode = self.bond_mode.unwrap_or(LinuxBondMode::balance_rr);
|
|
|
|
writeln!(w, "\tbond-mode {}", bond_mode_to_str(mode))?;
|
|
|
|
|
2020-05-08 07:58:03 +00:00
|
|
|
let slaves = self.slaves.as_ref().unwrap_or(&EMPTY_LIST);
|
|
|
|
if slaves.is_empty() {
|
|
|
|
writeln!(w, "\tbond-slaves none")?;
|
|
|
|
} else {
|
|
|
|
writeln!(w, "\tbond-slaves {}", slaves.join(" "))?;
|
2020-04-23 07:31:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:24:17 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
if let Some(mtu) = self.mtu {
|
2020-05-06 05:51:05 +00:00
|
|
|
writeln!(w, "\tmtu {}", mtu)?;
|
2020-04-23 05:07:14 +00:00
|
|
|
}
|
2020-04-23 07:24:17 +00:00
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write attributes dependening on address family inet (IPv4)
|
2020-04-22 10:42:09 +00:00
|
|
|
fn write_iface_attributes_v4(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
|
|
|
|
if method == NetworkConfigMethod::Static {
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(address) = &self.cidr {
|
|
|
|
writeln!(w, "\taddress {}", address)?;
|
2020-04-22 10:42:09 +00:00
|
|
|
}
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(gateway) = &self.gateway {
|
|
|
|
writeln!(w, "\tgateway {}", gateway)?;
|
2020-04-22 10:42:09 +00:00
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
2020-04-22 10:42:09 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
for option in &self.options {
|
|
|
|
writeln!(w, "\t{}", option)?;
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(ref comments) = self.comments {
|
2020-04-24 05:46:08 +00:00
|
|
|
for comment in comments.lines() {
|
|
|
|
writeln!(w, "#{}", comment)?;
|
|
|
|
}
|
2020-04-23 13:53:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:07:14 +00:00
|
|
|
/// Write attributes dependening on address family inet6 (IPv6)
|
2020-04-22 10:42:09 +00:00
|
|
|
fn write_iface_attributes_v6(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
|
|
|
|
if method == NetworkConfigMethod::Static {
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(address) = &self.cidr6 {
|
|
|
|
writeln!(w, "\taddress {}", address)?;
|
2020-04-22 10:42:09 +00:00
|
|
|
}
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(gateway) = &self.gateway6 {
|
|
|
|
writeln!(w, "\tgateway {}", gateway)?;
|
2020-04-22 10:42:09 +00:00
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
2020-04-22 10:42:09 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
for option in &self.options6 {
|
|
|
|
writeln!(w, "\t{}", option)?;
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(ref comments) = self.comments6 {
|
2020-04-24 05:46:08 +00:00
|
|
|
for comment in comments.lines() {
|
|
|
|
writeln!(w, "#{}", comment)?;
|
|
|
|
}
|
2020-04-23 13:53:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_iface(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
|
2020-04-21 15:17:57 +00:00
|
|
|
fn method_to_str(method: NetworkConfigMethod) -> &'static str {
|
2020-04-21 09:06:22 +00:00
|
|
|
match method {
|
2020-04-21 15:17:57 +00:00
|
|
|
NetworkConfigMethod::Static => "static",
|
|
|
|
NetworkConfigMethod::Loopback => "loopback",
|
|
|
|
NetworkConfigMethod::Manual => "manual",
|
|
|
|
NetworkConfigMethod::DHCP => "dhcp",
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.method.is_none() && self.method6.is_none() { return Ok(()); }
|
2020-04-22 10:42:09 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if self.autostart {
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w, "auto {}", self.name)?;
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(method) = self.method {
|
|
|
|
writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?;
|
|
|
|
self.write_iface_attributes_v4(w, method)?;
|
|
|
|
self.write_iface_attributes(w)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
}
|
2020-05-07 12:07:45 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
if let Some(method6) = self.method6 {
|
|
|
|
let mut skip_v6 = false; // avoid empty inet6 manual entry
|
|
|
|
if self.method.is_some() && method6 == NetworkConfigMethod::Manual {
|
|
|
|
if self.comments6.is_none() && self.options6.is_empty() { skip_v6 = true; }
|
2020-04-21 09:06:22 +00:00
|
|
|
}
|
2020-05-07 12:07:45 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
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
|
2020-04-23 13:53:48 +00:00
|
|
|
self.write_iface_attributes(w)?;
|
|
|
|
}
|
2020-04-21 09:06:22 +00:00
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 05:51:05 +00:00
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum NetworkOrderEntry {
|
|
|
|
Iface(String),
|
|
|
|
Comment(String),
|
|
|
|
Option(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NetworkConfig {
|
2020-05-08 09:15:00 +00:00
|
|
|
pub interfaces: BTreeMap<String, Interface>,
|
2020-04-20 12:15:57 +00:00
|
|
|
order: Vec<NetworkOrderEntry>,
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:55:22 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
impl TryFrom<NetworkConfig> for String {
|
|
|
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(config: NetworkConfig) -> Result<Self, Self::Error> {
|
|
|
|
let mut output = Vec::new();
|
|
|
|
config.write_config(&mut output)?;
|
|
|
|
let res = String::from_utf8(output)?;
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
impl NetworkConfig {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2020-05-08 09:15:00 +00:00
|
|
|
interfaces: BTreeMap::new(),
|
2020-04-20 12:15:57 +00:00
|
|
|
order: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 08:54:07 +00:00
|
|
|
pub fn lookup(&self, name: &str) -> Result<&Interface, Error> {
|
|
|
|
let interface = self.interfaces.get(name).ok_or_else(|| {
|
|
|
|
format_err!("interface '{}' does not exist.", name)
|
|
|
|
})?;
|
|
|
|
Ok(interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lookup_mut(&mut self, name: &str) -> Result<&mut Interface, Error> {
|
|
|
|
let interface = self.interfaces.get_mut(name).ok_or_else(|| {
|
|
|
|
format_err!("interface '{}' does not exist.", name)
|
|
|
|
})?;
|
|
|
|
Ok(interface)
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:15:00 +00:00
|
|
|
/// Check if ports are used only once
|
|
|
|
pub fn check_port_usage(&self) -> Result<(), Error> {
|
|
|
|
let mut used_ports = HashMap::new();
|
|
|
|
let mut check_port_usage = |iface, ports: &Vec<String>| {
|
|
|
|
for port in ports.iter() {
|
|
|
|
if let Some(prev_iface) = used_ports.get(port) {
|
|
|
|
bail!("iface '{}' port '{}' is already used on interface '{}'",
|
|
|
|
iface, port, prev_iface);
|
|
|
|
}
|
|
|
|
used_ports.insert(port.to_string(), iface);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
for (iface, interface) in self.interfaces.iter() {
|
|
|
|
if let Some(ports) = &interface.bridge_ports { check_port_usage(iface, ports)?; }
|
|
|
|
if let Some(slaves) = &interface.slaves { check_port_usage(iface, slaves)?; }
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:31:38 +00:00
|
|
|
/// Check if child mtu is less or equal than parent mtu
|
|
|
|
pub fn check_mtu(&self, parent_name: &str, child_name: &str) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let parent = self.interfaces.get(parent_name)
|
|
|
|
.ok_or(format_err!("check_mtu - missing parent interface '{}'", parent_name))?;
|
|
|
|
let child = self.interfaces.get(child_name)
|
|
|
|
.ok_or(format_err!("check_mtu - missing child interface '{}'", child_name))?;
|
|
|
|
|
|
|
|
let child_mtu = match child.mtu {
|
|
|
|
Some(mtu) => mtu,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let parent_mtu = match parent.mtu {
|
|
|
|
Some(mtu) => mtu,
|
|
|
|
None => {
|
|
|
|
if parent.interface_type == NetworkInterfaceType::Bond {
|
|
|
|
child_mtu
|
|
|
|
} else {
|
|
|
|
1500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if parent_mtu < child_mtu {
|
|
|
|
bail!("interface '{}' - mtu {} is lower than '{}' - mtu {}\n",
|
|
|
|
parent_name, parent_mtu, child_name, child_mtu);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if bond slaves exists
|
|
|
|
pub fn check_bond_slaves(&self) -> Result<(), Error> {
|
|
|
|
for (iface, interface) in self.interfaces.iter() {
|
|
|
|
if let Some(slaves) = &interface.slaves {
|
|
|
|
for slave in slaves.iter() {
|
|
|
|
match self.interfaces.get(slave) {
|
|
|
|
Some(entry) => {
|
|
|
|
if entry.interface_type != NetworkInterfaceType::Eth {
|
|
|
|
bail!("bond '{}' - wrong interface type on slave '{}' ({:?} != {:?})",
|
|
|
|
iface, slave, entry.interface_type, NetworkInterfaceType::Eth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("bond '{}' - unable to find slave '{}'", iface, slave);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.check_mtu(iface, slave)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if bridge ports exists
|
|
|
|
pub fn check_bridge_ports(&self) -> Result<(), Error> {
|
2020-05-08 13:51:47 +00:00
|
|
|
lazy_static!{
|
|
|
|
static ref VLAN_INTERFACE_REGEX: Regex = Regex::new(r"^(\S+)\.(\d+)$").unwrap();
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:31:38 +00:00
|
|
|
for (iface, interface) in self.interfaces.iter() {
|
|
|
|
if let Some(ports) = &interface.bridge_ports {
|
|
|
|
for port in ports.iter() {
|
2020-05-08 13:51:47 +00:00
|
|
|
let captures = VLAN_INTERFACE_REGEX.captures(port);
|
|
|
|
let port = if let Some(ref caps) = captures { &caps[1] } else { port.as_str() };
|
2020-05-08 12:31:38 +00:00
|
|
|
if !self.interfaces.contains_key(port) {
|
|
|
|
bail!("bridge '{}' - unable to find port '{}'", iface, port);
|
|
|
|
}
|
|
|
|
self.check_mtu(iface, port)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
pub fn write_config(&self, w: &mut dyn Write) -> Result<(), Error> {
|
|
|
|
|
2020-05-08 09:15:00 +00:00
|
|
|
self.check_port_usage()?;
|
2020-05-08 12:31:38 +00:00
|
|
|
self.check_bond_slaves()?;
|
|
|
|
self.check_bridge_ports()?;
|
2020-05-08 09:15:00 +00:00
|
|
|
|
2020-04-20 12:15:57 +00:00
|
|
|
let mut done = HashSet::new();
|
|
|
|
|
|
|
|
let mut last_entry_was_comment = false;
|
|
|
|
|
|
|
|
for entry in self.order.iter() {
|
2020-04-21 09:06:22 +00:00
|
|
|
match entry {
|
2020-04-20 12:15:57 +00:00
|
|
|
NetworkOrderEntry::Comment(comment) => {
|
|
|
|
writeln!(w, "#{}", comment)?;
|
|
|
|
last_entry_was_comment = true;
|
|
|
|
}
|
|
|
|
NetworkOrderEntry::Option(option) => {
|
|
|
|
if last_entry_was_comment { writeln!(w)?; }
|
|
|
|
last_entry_was_comment = false;
|
|
|
|
writeln!(w, "{}", option)?;
|
|
|
|
writeln!(w)?;
|
|
|
|
}
|
|
|
|
NetworkOrderEntry::Iface(name) => {
|
|
|
|
let interface = match self.interfaces.get(name) {
|
|
|
|
Some(interface) => interface,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if last_entry_was_comment { writeln!(w)?; }
|
|
|
|
last_entry_was_comment = false;
|
|
|
|
|
|
|
|
if done.contains(name) { continue; }
|
|
|
|
done.insert(name);
|
|
|
|
|
2020-04-21 09:06:22 +00:00
|
|
|
interface.write_iface(w)?;
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name, interface) in &self.interfaces {
|
|
|
|
if done.contains(name) { continue; }
|
2020-04-21 09:06:22 +00:00
|
|
|
interface.write_iface(w)?;
|
2020-04-20 12:15:57 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
pub const NETWORK_INTERFACES_FILENAME: &str = "/etc/network/interfaces";
|
2020-04-23 05:19:29 +00:00
|
|
|
pub const NETWORK_INTERFACES_NEW_FILENAME: &str = "/etc/network/interfaces.new";
|
2020-04-21 10:55:33 +00:00
|
|
|
pub const NETWORK_LOCKFILE: &str = "/var/lock/pve-network.lck";
|
|
|
|
|
2020-04-23 05:19:29 +00:00
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
pub fn config() -> Result<(NetworkConfig, [u8;32]), Error> {
|
2020-04-23 05:19:29 +00:00
|
|
|
let content = std::fs::read(NETWORK_INTERFACES_NEW_FILENAME)
|
|
|
|
.or_else(|err| {
|
2020-04-21 10:55:33 +00:00
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
2020-04-23 05:19:29 +00:00
|
|
|
std::fs::read(NETWORK_INTERFACES_FILENAME)
|
|
|
|
.or_else(|err| {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
Ok(Vec::new())
|
|
|
|
} else {
|
|
|
|
bail!("unable to read '{}' - {}", NETWORK_INTERFACES_FILENAME, err);
|
|
|
|
}
|
|
|
|
})
|
2020-04-21 10:55:33 +00:00
|
|
|
} else {
|
2020-04-23 05:19:29 +00:00
|
|
|
bail!("unable to read '{}' - {}", NETWORK_INTERFACES_NEW_FILENAME, err);
|
2020-04-21 10:55:33 +00:00
|
|
|
}
|
2020-04-23 05:19:29 +00:00
|
|
|
})?;
|
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(&content);
|
|
|
|
|
2020-04-24 10:55:22 +00:00
|
|
|
let existing_interfaces = get_network_interfaces()?;
|
2020-04-21 10:55:33 +00:00
|
|
|
let mut parser = NetworkParser::new(&content[..]);
|
2020-04-24 10:55:22 +00:00
|
|
|
let data = parser.parse_interfaces(Some(&existing_interfaces))?;
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
2020-04-24 07:55:46 +00:00
|
|
|
pub fn changes() -> Result<String, Error> {
|
|
|
|
|
|
|
|
if !std::path::Path::new(NETWORK_INTERFACES_NEW_FILENAME).exists() {
|
|
|
|
return Ok(String::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
compute_file_diff(NETWORK_INTERFACES_FILENAME, NETWORK_INTERFACES_NEW_FILENAME)
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:55:33 +00:00
|
|
|
pub fn save_config(config: &NetworkConfig) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut raw = Vec::new();
|
|
|
|
config.write_config(&mut raw)?;
|
|
|
|
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)=root, others(r)
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(nix::unistd::Gid::from_raw(0));
|
|
|
|
|
2020-04-23 05:19:29 +00:00
|
|
|
replace_file(NETWORK_INTERFACES_NEW_FILENAME, &raw, options)?;
|
2020-04-21 10:55:33 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-22 08:54:07 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_interface_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.interfaces.keys().map(|id| id.to_string()).collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 10:55:22 +00:00
|
|
|
|
2020-05-08 15:28:04 +00:00
|
|
|
|
|
|
|
pub fn complete_port_list(arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
let mut ports = Vec::new();
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => {
|
|
|
|
for (iface, interface) in data.interfaces.iter() {
|
|
|
|
if interface.interface_type == NetworkInterfaceType::Eth {
|
|
|
|
ports.push(iface.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => return vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let arg = arg.clone().trim();
|
|
|
|
let prefix = if let Some(idx) = arg.rfind(",") { &arg[..idx+1] } else { "" };
|
|
|
|
ports.iter().map(|port| format!("{}{}", prefix, port)).collect()
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:55:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
|
|
|
|
use anyhow::{Error};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_network_config_create_lo_1() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let input = "";
|
|
|
|
|
|
|
|
let mut parser = NetworkParser::new(&input.as_bytes()[..]);
|
|
|
|
|
|
|
|
let config = parser.parse_interfaces(None)?;
|
|
|
|
|
|
|
|
let output = String::try_from(config)?;
|
|
|
|
|
|
|
|
let expected = "auto lo\niface lo inet loopback\n\n";
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
|
|
|
// run again using output as input
|
|
|
|
let mut parser = NetworkParser::new(&output.as_bytes()[..]);
|
|
|
|
|
|
|
|
let config = parser.parse_interfaces(None)?;
|
|
|
|
|
|
|
|
let output = String::try_from(config)?;
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_network_config_create_lo_2() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let input = "#c1\n\n#c2\n\niface test inet manual\n";
|
|
|
|
|
|
|
|
let mut parser = NetworkParser::new(&input.as_bytes()[..]);
|
|
|
|
|
|
|
|
let config = parser.parse_interfaces(None)?;
|
|
|
|
|
|
|
|
let output = String::try_from(config)?;
|
|
|
|
|
|
|
|
// Note: loopback should be added in front of other interfaces
|
|
|
|
let expected = "#c1\n#c2\n\nauto lo\niface lo inet loopback\n\niface test inet manual\n\n";
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|