diff --git a/proxmox-ve-config/src/firewall/types/address.rs b/proxmox-ve-config/src/firewall/types/address.rs index 3777dc3..9b73d3d 100644 --- a/proxmox-ve-config/src/firewall/types/address.rs +++ b/proxmox-ve-config/src/firewall/types/address.rs @@ -108,6 +108,15 @@ impl From for Cidr { } } +impl From for Cidr { + fn from(value: IpAddr) -> Self { + match value { + IpAddr::V4(addr) => Ipv4Cidr::from(addr).into(), + IpAddr::V6(addr) => Ipv6Cidr::from(addr).into(), + } + } +} + const IPV4_LENGTH: u8 = 32; #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] diff --git a/proxmox-ve-config/src/sdn/ipam.rs b/proxmox-ve-config/src/sdn/ipam.rs index 95e3ba7..598b835 100644 --- a/proxmox-ve-config/src/sdn/ipam.rs +++ b/proxmox-ve-config/src/sdn/ipam.rs @@ -8,7 +8,11 @@ use std::{ use serde::Deserialize; use crate::{ - firewall::types::Cidr, + common::Allowlist, + firewall::types::{ + ipset::{IpsetEntry, IpsetScope}, + Cidr, Ipset, + }, guest::{types::Vmid, vm::MacAddress}, sdn::{SdnNameError, SubnetName, ZoneName}, }; @@ -309,6 +313,40 @@ impl Ipam { } } +impl Ipam { + /// Generates an [`Ipset`] for all guests with at least one entry in the IPAM. + /// + /// # Arguments + /// * `filter` - A [`Allowlist`] for which IPsets should get returned + /// + /// It contains all IPs in all VNets, that a guest has stored in IPAM. + /// Ipset name is of the form `guest-ipam-` + pub fn ipsets(&self, filter: Option<&Allowlist>) -> impl Iterator + '_ { + self.entries + .iter() + .flat_map(|(_, entries)| entries.iter()) + .filter_map(|entry| { + if let IpamData::Vm(data) = &entry.data() { + if filter.is_none_or(|list| list.is_allowed(&data.vmid)) { + return Some(data); + } + } + + None + }) + .fold(HashMap::::new(), |mut acc, entry| { + acc.entry(entry.vmid) + .or_insert_with(|| { + Ipset::from_parts(IpsetScope::Sdn, format!("guest-ipam-{}", entry.vmid)) + }) + .push(IpsetEntry::from(entry.ip)); + + acc + }) + .into_values() + } +} + impl TryFrom for Ipam { type Error = IpamError;