From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id AFCDA1FF0B6 for ; Thu, 12 Nov 2026 13:42:34 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id C3A15216A1; Thu, 12 Nov 2026 13:42:28 +0100 (CET) From: Hannes Laimer To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox-perl-rs v7 02/21] pve-rs: sdn: add bindings for the eBPF DHCP responder Date: Thu, 12 Nov 2026 13:41:42 +0100 Message-ID: <20261112124201.2844423-3-h.laimer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261112124201.2844423-1-h.laimer@proxmox.com> References: <20261112124201.2844423-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: 1794487332617 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.291 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: 4MOSYSSJDZ24VXLDGR3IFM4MGTWZSBP7 X-Message-ID-Hash: 4MOSYSSJDZ24VXLDGR3IFM4MGTWZSBP7 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: The responder answers from records the Perl side builds. They have to be built under the responder's lock. Otherwise a full pass could overwrite a newer record with one it built earlier. So the lock is an object, and every call that takes records is a method of it. Records cannot be passed in without holding the lock. Signed-off-by: Hannes Laimer --- pve-rs/Cargo.toml | 2 + pve-rs/Makefile | 3 + pve-rs/debian/control | 1 + pve-rs/src/bindings/sdn/dhcp.rs | 200 ++++++++++++++++++++++++++++++++ pve-rs/src/bindings/sdn/mod.rs | 1 + 5 files changed, 207 insertions(+) create mode 100644 pve-rs/src/bindings/sdn/dhcp.rs diff --git a/pve-rs/Cargo.toml b/pve-rs/Cargo.toml index f22e3aee5674..66769865d9e6 100644 --- a/pve-rs/Cargo.toml +++ b/pve-rs/Cargo.toml @@ -45,6 +45,7 @@ proxmox-openid = "1.0.2" proxmox-resource-scheduling = "2" proxmox-schema = "5" proxmox-section-config = "3" +proxmox-sdn-dhcp = "0.1" proxmox-sdn-types = "0.2.5" proxmox-shared-cache = "1" proxmox-subscription = "1.0.2" @@ -71,6 +72,7 @@ proxmox-wireguard = { version = "0.1.2" } # proxmox-config-digest = { path = "../../proxmox/proxmox-config-digest" } # proxmox-daemon = { path = "../../proxmox/proxmox-daemon" } # proxmox-dns-api = { path = "../../proxmox/proxmox-dns-api" } +# proxmox-ebpf = { path = "../../proxmox/proxmox-ebpf" } # proxmox-http-error = { path = "../../proxmox/proxmox-http-error" } # proxmox-http = { path = "../../proxmox/proxmox-http" } # proxmox-human-byte = { path = "../../proxmox/proxmox-human-byte" } diff --git a/pve-rs/Makefile b/pve-rs/Makefile index 60f68c7a21be..c597401f789b 100644 --- a/pve-rs/Makefile +++ b/pve-rs/Makefile @@ -33,6 +33,9 @@ PERLMOD_PACKAGES := \ PVE::RS::OpenId \ PVE::RS::ResourceScheduling::Static \ PVE::RS::ResourceScheduling::Dynamic \ + PVE::RS::SDN::Dhcp \ + PVE::RS::SDN::Dhcp::ExclusiveLock \ + PVE::RS::SDN::Dhcp::SharedLock \ PVE::RS::SDN::Fabrics \ PVE::RS::SDN::PrefixLists \ PVE::RS::SDN::RouteMaps \ diff --git a/pve-rs/debian/control b/pve-rs/debian/control index 25b7be8b26e6..fca271543db1 100644 --- a/pve-rs/debian/control +++ b/pve-rs/debian/control @@ -33,6 +33,7 @@ Build-Depends: debhelper-compat (= 13), librust-proxmox-oci-0.2+default-dev (>= 0.2.2-~~), librust-proxmox-openid-1+default-dev (>= 1.0.2-~~), librust-proxmox-resource-scheduling-2+default-dev (>= 2.0.0-~~), + librust-proxmox-sdn-dhcp-0.1+default-dev, librust-proxmox-sdn-types-0.2+default-dev (>= 0.2.5-~~), librust-proxmox-section-config-3+default-dev, librust-proxmox-shared-cache-1+default-dev, diff --git a/pve-rs/src/bindings/sdn/dhcp.rs b/pve-rs/src/bindings/sdn/dhcp.rs new file mode 100644 index 000000000000..09af79486c88 --- /dev/null +++ b/pve-rs/src/bindings/sdn/dhcp.rs @@ -0,0 +1,200 @@ +#[perlmod::package(name = "PVE::RS::SDN::Dhcp", lib = "pve_rs")] +pub mod pve_rs_sdn_dhcp { + //! The `PVE::RS::SDN::Dhcp` package. + //! + //! Bindings for the per-tap eBPF DHCP responder. Interfaces and the records they answer with + //! are handed in here and served directly from the kernel. The calls that change the responder + //! are methods of a lock object, so nothing writes to it without holding the lock in the mode + //! the change needs. + + use std::net::Ipv4Addr; + + use anyhow::Error; + use serde::Deserialize; + + use proxmox_network_types::mac_address::MacAddress; + use proxmox_sdn_dhcp::{Iface, Record}; + + /// One interface's DHCP answer, the address and every option the reply carries. + #[derive(Deserialize)] + pub struct PerlRecord { + ip: Ipv4Addr, + prefixlen: u8, + server_id: Ipv4Addr, + lease: u32, + #[serde(default)] + router: Option, + #[serde(default)] + dns: Option, + #[serde(default)] + mtu: Option, + } + + /// One guest interface to serve, with its MAC and the record it answers with. + #[derive(Deserialize)] + pub struct PerlIface { + name: String, + #[serde(default)] + mac: Option, + #[serde(default)] + record: Option, + } + + impl From for Record { + fn from(r: PerlRecord) -> Self { + Record { + ip: r.ip, + prefixlen: r.prefixlen, + server_id: r.server_id, + lease: r.lease, + router: r.router, + dns: r.dns, + mtu: r.mtu, + } + } + } + + impl From for Iface { + fn from(i: PerlIface) -> Self { + Iface { + name: i.name, + mac: i.mac.map(|mac| mac.octets()), + record: i.record.map(Record::from), + } + } + } + + pub(super) fn to_ifaces(ifaces: Vec) -> Vec { + ifaces.into_iter().map(Iface::from).collect() + } + + /// A tap unplug. The interface is gone or going and takes its record with it. + #[export] + pub fn detach(iface: &str) -> Result<(), Error> { + proxmox_sdn_dhcp::detach(iface) + } +} + +#[perlmod::package(name = "PVE::RS::SDN::Dhcp::SharedLock", lib = "pve_rs")] +pub mod pve_rs_sdn_dhcp_shared_lock { + //! The `PVE::RS::SDN::Dhcp::SharedLock` package. + //! + //! The responder's lock taken shared, what a tap plug and a guest's own record change hold. + //! Released when the object goes away. + + use anyhow::Error; + use perlmod::Value; + + use proxmox_network_types::mac_address::MacAddress; + use proxmox_sdn_dhcp::{Iface, Outcome, Record, SharedLock}; + + use super::pve_rs_sdn_dhcp::{PerlIface, PerlRecord, to_ifaces}; + + /// The lock as Perl holds it. + pub struct PerlSharedLock { + lock: SharedLock, + } + + perlmod::declare_magic!(Box : &PerlSharedLock as "PVE::RS::SDN::Dhcp::SharedLock"); + + /// Class method: take the lock. Plugs hold it next to each other and wait only behind a full + /// pass or a record change. + #[export] + pub fn new(#[raw] class: Value) -> Result { + let lock = proxmox_sdn_dhcp::lock_shared()?; + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlSharedLock { lock }))) + } + + /// Method: a tap plug with the NIC's MAC and the record it answers. Without a record the + /// interface gets a link and no entry. Returns false when nothing is loaded yet. An interface + /// gone already counts as done. + #[export] + pub fn attach( + #[try_from_ref] this: &PerlSharedLock, + iface: String, + mac: Option, + record: Option, + ) -> Result { + let iface = Iface { + name: iface, + mac: mac.map(|mac| mac.octets()), + record: record.map(Record::from), + }; + Ok(this.lock.attach(&iface)? == Outcome::Done) + } + + /// Method: a plug onto a bridge the responder does not serve. The interface loses its link and + /// its entry. + #[export] + pub fn detach(#[try_from_ref] this: &PerlSharedLock, iface: &str) -> Result<(), Error> { + this.lock.detach(iface) + } + + /// Method: a record change for the listed interfaces by a caller holding the guest lock. + /// Returns false when nothing is loaded to write into, so a full pass is due. + #[export] + pub fn update( + #[try_from_ref] this: &PerlSharedLock, + ifaces: Vec, + ) -> Result { + Ok(this.lock.update(&to_ifaces(ifaces))? == Outcome::Done) + } +} + +#[perlmod::package(name = "PVE::RS::SDN::Dhcp::ExclusiveLock", lib = "pve_rs")] +pub mod pve_rs_sdn_dhcp_exclusive_lock { + //! The `PVE::RS::SDN::Dhcp::ExclusiveLock` package. + //! + //! The responder's lock taken exclusively, what a full pass and a record change hold. Released + //! when the object goes away. + + use std::sync::Mutex; + + use anyhow::Error; + use perlmod::Value; + + use proxmox_sdn_dhcp::{ExclusiveLock, Outcome}; + + use super::pve_rs_sdn_dhcp::{PerlIface, to_ifaces}; + + /// The lock as Perl holds it. A full pass and a clear take it mutably, and Perl hands out + /// shared references only. + pub struct PerlExclusiveLock { + lock: Mutex, + } + + perlmod::declare_magic!(Box : &PerlExclusiveLock as "PVE::RS::SDN::Dhcp::ExclusiveLock"); + + /// Class method: take the lock, waiting out every plug in flight. + #[export] + pub fn new(#[raw] class: Value) -> Result { + let lock = Mutex::new(proxmox_sdn_dhcp::lock_exclusive()?); + Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(PerlExclusiveLock { lock }))) + } + + /// Method: the full pass over the guest interfaces to serve. Those found here get a link, and + /// an entry where they have a record. Every other link and entry goes. + #[export] + pub fn apply( + #[try_from_ref] this: &PerlExclusiveLock, + ifaces: Vec, + ) -> Result<(), Error> { + this.lock.lock().unwrap().apply(&to_ifaces(ifaces)) + } + + /// Method: a record change for the listed interfaces. Those not found here are skipped. + /// Returns false when nothing is loaded to write into, so a full pass is due. + #[export] + pub fn update( + #[try_from_ref] this: &PerlExclusiveLock, + ifaces: Vec, + ) -> Result { + Ok(this.lock.lock().unwrap().update(&to_ifaces(ifaces))? == Outcome::Done) + } + + /// Method: detach the responder everywhere and drop its pinned state. + #[export] + pub fn clear(#[try_from_ref] this: &PerlExclusiveLock) -> Result<(), Error> { + this.lock.lock().unwrap().clear() + } +} diff --git a/pve-rs/src/bindings/sdn/mod.rs b/pve-rs/src/bindings/sdn/mod.rs index dcae046a7e46..4b99b8fa10e5 100644 --- a/pve-rs/src/bindings/sdn/mod.rs +++ b/pve-rs/src/bindings/sdn/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod dhcp; pub(crate) mod fabrics; pub(crate) mod prefix_lists; pub(crate) mod route_maps; -- 2.47.3