network-api: remove lazy_static dependency

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
This commit is contained in:
Maximiliano Sandoval 2024-08-14 09:19:57 +02:00 committed by Wolfgang Bumiller
parent 8d5e864bf1
commit dde994ab57
6 changed files with 65 additions and 75 deletions

View File

@ -7,11 +7,11 @@ license.workspace = true
repository.workspace = true repository.workspace = true
exclude.workspace = true exclude.workspace = true
description = "Network Management API implementation" description = "Network Management API implementation"
rust-version.workspace = true
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
const_format.workspace = true const_format.workspace = true
lazy_static.workspace = true
regex.workspace = true regex.workspace = true
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }

View File

@ -1,9 +1,9 @@
use std::fmt; use std::fmt;
use std::sync::LazyLock;
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use proxmox_schema::api; use proxmox_schema::api;
@ -15,11 +15,11 @@ use proxmox_schema::ArraySchema;
use proxmox_schema::Schema; use proxmox_schema::Schema;
use proxmox_schema::StringSchema; use proxmox_schema::StringSchema;
lazy_static! { pub static PHYSICAL_NIC_REGEX: LazyLock<Regex> =
pub static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap(); LazyLock::new(|| Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap());
pub static ref VLAN_INTERFACE_REGEX: Regex = pub static VLAN_INTERFACE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap(); Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap()
} });
pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX); pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX);

View File

@ -2,10 +2,10 @@ use std::collections::HashMap;
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd}; use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd};
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::sync::LazyLock;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use const_format::concatcp; use const_format::concatcp;
use lazy_static::lazy_static;
use nix::ioctl_read_bad; use nix::ioctl_read_bad;
use nix::sys::socket::{socket, AddressFamily, SockFlag, SockType}; use nix::sys::socket::{socket, AddressFamily, SockFlag, SockType};
use regex::Regex; use regex::Regex;
@ -49,16 +49,14 @@ pub static IPV4_REVERSE_MASK: &[&str] = &[
"255.255.255.255", "255.255.255.255",
]; ];
lazy_static! { pub static IPV4_MASK_HASH_LOCALNET: LazyLock<HashMap<&'static str, u8>> = LazyLock::new(|| {
pub static ref IPV4_MASK_HASH_LOCALNET: HashMap<&'static str, u8> = { let mut map = HashMap::new();
let mut map = HashMap::new(); #[allow(clippy::needless_range_loop)]
#[allow(clippy::needless_range_loop)] for i in 0..IPV4_REVERSE_MASK.len() {
for i in 0..IPV4_REVERSE_MASK.len() { map.insert(IPV4_REVERSE_MASK[i], i as u8);
map.insert(IPV4_REVERSE_MASK[i], i as u8); }
} map
map });
};
}
pub fn parse_cidr(cidr: &str) -> Result<(String, u8, bool), Error> { pub fn parse_cidr(cidr: &str) -> Result<(String, u8, bool), Error> {
let (address, mask, is_v6) = parse_address_or_cidr(cidr)?; let (address, mask, is_v6) = parse_address_or_cidr(cidr)?;
@ -93,12 +91,10 @@ pub(crate) fn check_netmask(mask: u8, is_v6: bool) -> Result<(), Error> {
pub(crate) fn parse_address_or_cidr(cidr: &str) -> Result<(String, Option<u8>, bool), Error> { pub(crate) fn parse_address_or_cidr(cidr: &str) -> Result<(String, Option<u8>, bool), Error> {
// NOTE: This is NOT the same regex as in proxmox-schema as this one has capture groups for // NOTE: This is NOT the same regex as in proxmox-schema as this one has capture groups for
// the addresses vs cidr portions! // the addresses vs cidr portions!
lazy_static! { pub static CIDR_V4_REGEX: LazyLock<Regex> =
pub static ref CIDR_V4_REGEX: Regex = LazyLock::new(|| Regex::new(concatcp!(r"^(", IPV4RE_STR, r")(?:/(\d{1,2}))?$")).unwrap());
Regex::new(concatcp!(r"^(", IPV4RE_STR, r")(?:/(\d{1,2}))?$")).unwrap(); pub static CIDR_V6_REGEX: LazyLock<Regex> =
pub static ref CIDR_V6_REGEX: Regex = LazyLock::new(|| Regex::new(concatcp!(r"^(", IPV6RE_STR, r")(?:/(\d{1,3}))?$")).unwrap());
Regex::new(concatcp!(r"^(", IPV6RE_STR, r")(?:/(\d{1,3}))?$")).unwrap();
}
if let Some(caps) = CIDR_V4_REGEX.captures(cidr) { if let Some(caps) = CIDR_V4_REGEX.captures(cidr) {
let address = &caps[1]; let address = &caps[1];
@ -134,9 +130,8 @@ pub(crate) fn get_network_interfaces() -> Result<HashMap<String, bool>, Error> {
ioctl_read_bad!(get_interface_flags, libc::SIOCGIFFLAGS, ifreq); ioctl_read_bad!(get_interface_flags, libc::SIOCGIFFLAGS, ifreq);
lazy_static! { static IFACE_LINE_REGEX: LazyLock<Regex> =
static ref IFACE_LINE_REGEX: Regex = Regex::new(r"^\s*([^:\s]+):").unwrap(); LazyLock::new(|| Regex::new(r"^\s*([^:\s]+):").unwrap());
}
let raw = std::fs::read_to_string(PROC_NET_DEV) let raw = std::fs::read_to_string(PROC_NET_DEV)
.map_err(|err| format_err!("unable to read {} - {}", PROC_NET_DEV, err))?; .map_err(|err| format_err!("unable to read {} - {}", PROC_NET_DEV, err))?;

View File

@ -1,8 +1,7 @@
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::io::BufRead; use std::io::BufRead;
use std::iter::Iterator; use std::iter::Iterator;
use std::sync::LazyLock;
use lazy_static::lazy_static;
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Token { pub enum Token {
@ -33,39 +32,37 @@ pub enum Token {
EOF, EOF,
} }
lazy_static! { static KEYWORDS: LazyLock<HashMap<&'static str, Token>> = LazyLock::new(|| {
static ref KEYWORDS: HashMap<&'static str, Token> = { let mut map = HashMap::new();
let mut map = HashMap::new(); map.insert("address", Token::Address);
map.insert("address", Token::Address); map.insert("auto", Token::Auto);
map.insert("auto", Token::Auto); map.insert("dhcp", Token::DHCP);
map.insert("dhcp", Token::DHCP); map.insert("gateway", Token::Gateway);
map.insert("gateway", Token::Gateway); map.insert("inet", Token::Inet);
map.insert("inet", Token::Inet); map.insert("inet6", Token::Inet6);
map.insert("inet6", Token::Inet6); map.insert("iface", Token::Iface);
map.insert("iface", Token::Iface); map.insert("loopback", Token::Loopback);
map.insert("loopback", Token::Loopback); map.insert("manual", Token::Manual);
map.insert("manual", Token::Manual); 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.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("bridge_vlan_aware", Token::BridgeVlanAware);
map.insert("bridge_vlan_aware", Token::BridgeVlanAware); map.insert("vlan-id", Token::VlanId);
map.insert("vlan-id", Token::VlanId); map.insert("vlan_id", Token::VlanId);
map.insert("vlan_id", Token::VlanId); map.insert("vlan-raw-device", Token::VlanRawDevice);
map.insert("vlan-raw-device", Token::VlanRawDevice); map.insert("vlan_raw_device", Token::VlanRawDevice);
map.insert("vlan_raw_device", Token::VlanRawDevice); 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.insert("bond-primary", Token::BondPrimary);
map.insert("bond-primary", Token::BondPrimary); map.insert("bond_primary", Token::BondPrimary);
map.insert("bond_primary", Token::BondPrimary); map.insert("bond_xmit_hash_policy", Token::BondXmitHashPolicy);
map.insert("bond_xmit_hash_policy", Token::BondXmitHashPolicy); map.insert("bond-xmit-hash-policy", Token::BondXmitHashPolicy);
map.insert("bond-xmit-hash-policy", Token::BondXmitHashPolicy); map
map });
};
}
pub struct Lexer<R> { pub struct Lexer<R> {
input: R, input: R,

View File

@ -6,9 +6,9 @@ pub use helper::{assert_ifupdown2_installed, network_reload, parse_cidr};
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::io::Write; use std::io::Write;
use std::sync::LazyLock;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use serde::de::{value, Deserialize, IntoDeserializer}; use serde::de::{value, Deserialize, IntoDeserializer};
@ -23,11 +23,11 @@ use parser::NetworkParser;
use proxmox_config_digest::ConfigDigest; use proxmox_config_digest::ConfigDigest;
use proxmox_product_config::{open_api_lockfile, replace_system_config, ApiLockGuard}; use proxmox_product_config::{open_api_lockfile, replace_system_config, ApiLockGuard};
lazy_static! { static PHYSICAL_NIC_REGEX: LazyLock<Regex> =
static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap(); LazyLock::new(|| Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap());
static ref VLAN_INTERFACE_REGEX: Regex = static VLAN_INTERFACE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap(); Regex::new(r"^(?P<vlan_raw_device>\S+)\.(?P<vlan_id>\d+)|vlan(?P<vlan_id2>\d+)$").unwrap()
} });
pub fn is_physical_nic(iface: &str) -> bool { pub fn is_physical_nic(iface: &str) -> bool {
PHYSICAL_NIC_REGEX.is_match(iface) PHYSICAL_NIC_REGEX.is_match(iface)
@ -409,9 +409,8 @@ impl NetworkConfig {
/// Check if bridge ports exists /// Check if bridge ports exists
fn check_bridge_ports(&self) -> Result<(), Error> { fn check_bridge_ports(&self) -> Result<(), Error> {
lazy_static! { static VLAN_INTERFACE_REGEX: LazyLock<Regex> =
static ref VLAN_INTERFACE_REGEX: Regex = Regex::new(r"^(\S+)\.(\d+)$").unwrap(); LazyLock::new(|| Regex::new(r"^(\S+)\.(\d+)$").unwrap());
}
for (iface, interface) in self.interfaces.iter() { for (iface, interface) in self.interfaces.iter() {
if let Some(ports) = &interface.bridge_ports { if let Some(ports) = &interface.bridge_ports {

View File

@ -3,9 +3,9 @@ use crate::VLAN_INTERFACE_REGEX;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::io::BufRead; use std::io::BufRead;
use std::iter::{Iterator, Peekable}; use std::iter::{Iterator, Peekable};
use std::sync::LazyLock;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use serde::de::{value, Deserialize, IntoDeserializer}; use serde::de::{value, Deserialize, IntoDeserializer};
@ -551,9 +551,8 @@ impl<R: BufRead> NetworkParser<R> {
} }
} }
lazy_static! { static INTERFACE_ALIAS_REGEX: LazyLock<Regex> =
static ref INTERFACE_ALIAS_REGEX: Regex = Regex::new(r"^\S+:\d+$").unwrap(); LazyLock::new(|| Regex::new(r"^\S+:\d+$").unwrap());
}
if let Some(existing_interfaces) = existing_interfaces { if let Some(existing_interfaces) = existing_interfaces {
for (iface, active) in existing_interfaces.iter() { for (iface, active) in existing_interfaces.iter() {