From b5609cb219e6a6db1677f9de3befe884e545c36e Mon Sep 17 00:00:00 2001 From: PolloLoco Date: Sat, 14 Jan 2023 14:45:09 +0100 Subject: [PATCH 1/3] Add syntactic sugar for proxmox users (behind proxmox feature flag) --- Cargo.toml | 4 ++++ src/lib.rs | 16 ++++++++++++++++ src/utils.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index b4360c1..5926f23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,7 @@ libc = "0.2.102" parking_lot = "0.11.2" serde = { version = "1.0.130", features = ["derive"] } toml = "0.5.8" + +[features] +# Feature flag to enable syntactic sugar for proxmox users +proxmox = [] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e4ebaff..cef78b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,8 @@ use crate::nvidia::error::{ NV_ERR_BUSY_RETRY, NV_ERR_NOT_SUPPORTED, NV_ERR_OBJECT_NOT_FOUND, NV_OK, }; use crate::nvidia::nvos::{Nvos54Parameters, NV_ESC_RM_CONTROL}; +#[cfg(feature = "proxmox")] +use crate::utils::uuid_to_vmid; use crate::uuid::Uuid; static LAST_MDEV_UUID: Mutex> = parking_lot::const_mutex(None); @@ -220,6 +222,9 @@ struct ProfileOverridesConfig<'a> { profile: HashMap<&'a str, VgpuProfileOverride<'a>>, #[serde(borrow, default)] mdev: HashMap<&'a str, VgpuProfileOverride<'a>>, + #[cfg(feature = "proxmox")] + #[serde(borrow, default)] + vm: HashMap>, } #[derive(Deserialize)] @@ -538,6 +543,17 @@ fn handle_profile_override(config: &mut C) -> bool { } } + #[cfg(feature = "proxmox")] + if let Some(vmid) = mdev_uuid.and_then(uuid_to_vmid) { + if let Some(config_override) = config_overrides.vm.get(&vmid) { + info!("Applying proxmox VMID {} profile overrides", vmid); + + if !apply_profile_override(config, &vgpu_type, config_override) { + return false; + } + } + } + true } diff --git a/src/utils.rs b/src/utils.rs index dd091f1..8bbe89e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,8 @@ use std::fmt; +#[cfg(feature = "proxmox")] +use crate::uuid::Uuid; + #[derive(Clone, Copy)] #[repr(C, align(8))] pub struct AlignedU64(pub u64); @@ -17,3 +20,26 @@ impl fmt::LowerHex for AlignedU64 { fmt::LowerHex::fmt(&self.0, f) } } + +#[cfg(feature = "proxmox")] +/// Extracts the VMID from the last segment of a mdev uuid +/// +/// For example, for this uuid 00000000-0000-0000-0000-000000000100 +/// it would extract the number 100 +/// +/// All except the last segment must be zero +pub fn uuid_to_vmid(uuid: Uuid) -> Option { + // Ensure that the first parts of the uuid are only 0 + if uuid.0 != 0 || uuid.1 != 0 || uuid.2 != 0 || uuid.3[0] != 0 || uuid.3[1] != 0 { + return None; + } + + // Format the last segment of the uuid + let s = format!( + "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", + uuid.3[2], uuid.3[3], uuid.3[4], uuid.3[5], uuid.3[6], uuid.3[7] + ); + + // Parse it as a normal decimal number to get the right vm id + s.parse().ok() +} From f139fa775c1e0b05165edc8be14d1aa346ab6f50 Mon Sep 17 00:00:00 2001 From: PolloLoco Date: Sat, 14 Jan 2023 19:14:39 +0100 Subject: [PATCH 2/3] Enable 'proxmox' feature by default --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5926f23..b91295f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ toml = "0.5.8" [features] # Feature flag to enable syntactic sugar for proxmox users -proxmox = [] \ No newline at end of file +proxmox = [] +default = ["proxmox"] \ No newline at end of file From 688747fc845feabb04dfb24df42932aab1e74315 Mon Sep 17 00:00:00 2001 From: PolloLoco Date: Sat, 14 Jan 2023 19:14:49 +0100 Subject: [PATCH 3/3] Formatting --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 8bbe89e..00d5dd8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,13 +21,13 @@ impl fmt::LowerHex for AlignedU64 { } } -#[cfg(feature = "proxmox")] /// Extracts the VMID from the last segment of a mdev uuid /// /// For example, for this uuid 00000000-0000-0000-0000-000000000100 /// it would extract the number 100 /// /// All except the last segment must be zero +#[cfg(feature = "proxmox")] pub fn uuid_to_vmid(uuid: Uuid) -> Option { // Ensure that the first parts of the uuid are only 0 if uuid.0 != 0 || uuid.1 != 0 || uuid.2 != 0 || uuid.3[0] != 0 || uuid.3[1] != 0 {