Add syntactic sugar for proxmox users (behind proxmox feature flag)

This commit is contained in:
PolloLoco
2023-01-14 14:45:09 +01:00
parent 3c35e581cf
commit b5609cb219
3 changed files with 46 additions and 0 deletions

View File

@@ -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 = []

View File

@@ -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<Option<Uuid>> = 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<u64, VgpuProfileOverride<'a>>,
}
#[derive(Deserialize)]
@@ -538,6 +543,17 @@ fn handle_profile_override<C: VgpuConfigLike>(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
}

View File

@@ -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<u64> {
// 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()
}