From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 059711FF0AE for ; Tue, 27 Oct 2026 09:58:32 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A173421821; Tue, 27 Oct 2026 09:56:32 +0100 (CET) From: Hannes Laimer To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-perl-rs v3 18/45] pve-rs: sdn: microseg: bind the data plane Date: Tue, 27 Oct 2026 09:55:00 +0100 Message-ID: <20261027085527.702738-19-h.laimer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261027085527.702738-1-h.laimer@proxmox.com> References: <20261027085527.702738-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1793091352968 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.336 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: AT3WB2AGR6NEJ7WRF3XT3A76GH2QLEVF X-Message-ID-Hash: AT3WB2AGR6NEJ7WRF3XT3A76GH2QLEVF X-MailFrom: h.laimer@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Expose the policy lock to Perl, with the full apply of the running config and the apply for one guest NIC at plug time as its methods. Signed-off-by: Hannes Laimer --- pve-rs/Cargo.toml | 2 + pve-rs/Makefile | 2 + pve-rs/debian/control | 1 + pve-rs/src/bindings/sdn/microseg.rs | 105 ++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index 5ae908249b8c..b2b0a983981a 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -38,6 +38,7 @@ proxmox-frr = { version = "0.5.1" } proxmox-http = { version = "1.0.2", features = ["client-sync", "client-trait"] } proxmox-http-error = "1" proxmox-log = "1" +proxmox-microseg = "0.1" proxmox-network-types = "1.1.2" proxmox-notify = { version = "1", features = ["pve-context"] } proxmox-oci = "0.2.1" @@ -79,6 +80,7 @@ proxmox-wireguard = { version = "0.1.2" } # proxmox-log = { path = "../../proxmox/proxmox-log" } # proxmox-login = { path = "../../proxmox/proxmox-login" } # proxmox-metrics = { path = "../../proxmox/proxmox-metrics" } +# proxmox-microseg = { path = "../../proxmox-ve-rs/proxmox-microseg" } # proxmox-network-api = { path = "../../proxmox/proxmox-network-api" } # proxmox-network-types = { path = "../../proxmox/proxmox-network-types" } # proxmox-notify = { path = "../../proxmox/proxmox-notify" } diff --git a/pve-rs/Makefile b/pve-rs/Makefile index fc199ae5c9b8..ea0f4570198c 100644 --- a/pve-rs/Makefile +++ b/pve-rs/Makefile @@ -34,6 +34,8 @@ PERLMOD_PACKAGES := \ PVE::RS::ResourceScheduling::Dynamic \ PVE::RS::SDN::Fabrics \ PVE::RS::SDN::Microseg \ + PVE::RS::SDN::Microseg::ExclusiveLock \ + PVE::RS::SDN::Microseg::SharedLock \ PVE::RS::SDN::PrefixLists \ PVE::RS::SDN::RouteMaps \ PVE::RS::SDN::WireGuard::PrivateKeys \ diff --git a/pve-rs/debian/control b/pve-rs/debian/control index 70367dc54504..5600e01fcbd6 100644 --- a/pve-rs/debian/control +++ b/pve-rs/debian/control @@ -26,6 +26,7 @@ Build-Depends: debhelper-compat (= 13), librust-proxmox-http-1+default-dev (>= 1.0.2-~~), librust-proxmox-http-error-1+default-dev, librust-proxmox-log-1+default-dev, + librust-proxmox-microseg-0.1+default-dev, librust-proxmox-network-types-1+default-dev (>= 1.1.2-~~), librust-proxmox-notify-1+default-dev, librust-proxmox-notify-1+pve-context-dev, diff --git a/pve-rs/src/bindings/sdn/microseg.rs b/pve-rs/src/bindings/sdn/microseg.rs index 5deaf5189aec..7300376460e5 100644 --- a/pve-rs/src/bindings/sdn/microseg.rs +++ b/pve-rs/src/bindings/sdn/microseg.rs @@ -240,4 +240,109 @@ pub mod pve_rs_sdn_microseg { pub fn delete(#[try_from_ref] this: &PerlMicrosegConfig, id: &str) -> Result<(), Error> { change(this, |entries| microseg::api::delete(entries, id)) } + + pub(super) fn running_config_from( + raw: Option<&[u8]>, + ) -> Result, Error> { + match raw { + // pmxcfs yields the empty string for a zero-length file and the Perl reader of the + // same file treats that as an empty config. + Some(raw) if !raw.is_empty() => std::str::from_utf8(raw) + .map_err(Error::from) + .and_then(proxmox_microseg::parse_running_config) + .context("parse /etc/pve/sdn/.running-config"), + _ => Ok(Some(MicrosegRunningConfig::default())), + } + } +} + +#[perlmod::package(name = "PVE::RS::SDN::Microseg::SharedLock", lib = "pve_rs")] +pub mod pve_rs_sdn_microseg_shared_lock { + //! The `PVE::RS::SDN::Microseg::SharedLock` package. + //! + //! The policy lock in shared mode. A plug takes it before reading the running config and the + //! guest, and holds it until the NIC is placed. Released when the object goes away. + + use anyhow::Error; + use perlmod::Value; + use proxmox_microseg::SharedLock; + use proxmox_ve_config::sdn::microseg::GuestInfo; + + use super::pve_rs_sdn_microseg::running_config_from; + + pub struct PerlSharedLock { + lock: SharedLock, + } + + perlmod::declare_magic!(Box : &PerlSharedLock as "PVE::RS::SDN::Microseg::SharedLock"); + + /// Class method: Take the lock. Fails when a full pass holds it past the lock timeout. + #[export] + pub fn new(#[raw] class: Value) -> Result { + let lock = proxmox_microseg::lock_shared()?; + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlSharedLock { lock }))) + } + + /// Method: Enforce the microseg policy on the guest NIC `iface`, `raw` as for the full pass. + /// Returns false for a running config without a microseg block. Fails where only a full pass + /// can place the NIC. + #[export] + pub fn apply_iface( + #[try_from_ref] this: &PerlSharedLock, + raw: Option<&[u8]>, + guest: GuestInfo, + iface: &str, + firewall_bridge: bool, + ) -> Result { + let Some(cfg) = running_config_from(raw)? else { + return Ok(false); + }; + proxmox_microseg::apply_iface(&this.lock, &cfg, &guest, iface, firewall_bridge)?; + Ok(true) + } +} + +#[perlmod::package(name = "PVE::RS::SDN::Microseg::ExclusiveLock", lib = "pve_rs")] +pub mod pve_rs_sdn_microseg_exclusive_lock { + //! The `PVE::RS::SDN::Microseg::ExclusiveLock` package. + //! + //! The policy lock in exclusive mode. A full pass takes it before reading the running config + //! and the inventory, and holds it until the node is synced. Released when the object goes + //! away. + + use anyhow::Error; + use perlmod::Value; + use proxmox_microseg::ExclusiveLock; + use proxmox_ve_config::sdn::microseg::GuestInfo; + + use super::pve_rs_sdn_microseg::running_config_from; + + pub struct PerlExclusiveLock { + lock: ExclusiveLock, + } + + perlmod::declare_magic!(Box : &PerlExclusiveLock as "PVE::RS::SDN::Microseg::ExclusiveLock"); + + /// Class method: Take the lock. Fails when plugs or a pass hold it past the lock timeout. + #[export] + pub fn new(#[raw] class: Value) -> Result { + let lock = proxmox_microseg::lock_exclusive()?; + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlExclusiveLock { lock }))) + } + + /// Method: Enforce the microseg policy on this whole node. `raw` is the content of + /// `/etc/pve/sdn/.running-config`, `undef` or empty before the first commit. Returns false + /// and leaves the node untouched for a running config without a microseg block. + #[export] + pub fn apply( + #[try_from_ref] this: &PerlExclusiveLock, + raw: Option<&[u8]>, + inventory: Vec, + ) -> Result { + let Some(cfg) = running_config_from(raw)? else { + return Ok(false); + }; + proxmox_microseg::apply(&this.lock, &cfg, &inventory)?; + Ok(true) + } } -- 2.47.3