|
| 1 | +use crate::helpers::{ |
| 2 | + CheckGroup, CheckGroupResult, CheckResult, |
| 3 | + CheckResultValue::{Errored, Failed, Passed, Skipped}, |
| 4 | + host_executor::HostNamespaceExecutor, |
| 5 | +}; |
| 6 | +use async_trait::async_trait; |
| 7 | +use futures::{FutureExt, future::join_all}; |
| 8 | +use log::debug; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +const GROUP_IDENTIFIER: &str = "IOMMUChecks"; |
| 12 | +const NAME: &str = "IOMMU Checks"; |
| 13 | + |
| 14 | +pub struct IOMMUChecks { |
| 15 | + host_executor: HostNamespaceExecutor, |
| 16 | +} |
| 17 | + |
| 18 | +#[cfg(target_arch = "x86_64")] |
| 19 | +impl IOMMUChecks { |
| 20 | + pub fn new(host_executor: HostNamespaceExecutor) -> Self { |
| 21 | + IOMMUChecks { host_executor } |
| 22 | + } |
| 23 | + |
| 24 | + /// Run all the checkers asynchronously, then |
| 25 | + /// join and collect the results. |
| 26 | + pub async fn run_all(&self) -> CheckGroupResult { |
| 27 | + let results = join_all([self.check_iommu().boxed()]).await; |
| 28 | + |
| 29 | + let mut group_result = Skipped; |
| 30 | + for res in results.iter() { |
| 31 | + // Set group result to Failed if we failed and aren't already in an Errored state |
| 32 | + if !matches!(group_result, Errored(_)) && matches!(res.result, Failed(_)) { |
| 33 | + group_result = Failed(String::from("group failed")); |
| 34 | + } |
| 35 | + |
| 36 | + if matches!(res.result, Errored(_)) { |
| 37 | + group_result = Errored(String::from("group errored")); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + CheckGroupResult { |
| 42 | + name: NAME.to_string(), |
| 43 | + result: group_result, |
| 44 | + results, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + async fn check_iommu(&self) -> CheckResult { |
| 49 | + let name = String::from("IOMMU Support"); |
| 50 | + |
| 51 | + match self |
| 52 | + .host_executor |
| 53 | + .spawn_in_host_ns(async { |
| 54 | + if Path::new("/sys/firmware/acpi/tables/DMAR").exists() { |
| 55 | + debug!("Found Intel IOMMU"); |
| 56 | + true |
| 57 | + } else if Path::new("/sys/firmware/acpi/tables/IVRS").exists() { |
| 58 | + debug!("Found AMD IOMMU"); |
| 59 | + true |
| 60 | + } else { |
| 61 | + false |
| 62 | + } |
| 63 | + }) |
| 64 | + .await |
| 65 | + { |
| 66 | + Ok(true) => CheckResult::new(&name, Passed), |
| 67 | + Ok(false) => CheckResult::new(&name, Failed("no IOMMU detected".to_string())), |
| 68 | + Err(e) => { |
| 69 | + debug!("Error: {}", e); |
| 70 | + CheckResult::new(&name, Errored(e.to_string())) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// No-op for other archs |
| 77 | +// TODO(bml) arm64 IOMMU?? |
| 78 | +#[cfg(not(target_arch = "x86_64"))] |
| 79 | +impl IOMMUChecks { |
| 80 | + pub fn new(host_executor: HostNamespaceExecutor) -> Self { |
| 81 | + IOMMUChecks { host_executor } |
| 82 | + } |
| 83 | + |
| 84 | + pub async fn run_all(&self) -> CheckGroupResult { |
| 85 | + CheckGroupResult::new(NAME) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[async_trait] |
| 90 | +impl CheckGroup for IOMMUChecks { |
| 91 | + fn id(&self) -> &str { |
| 92 | + GROUP_IDENTIFIER |
| 93 | + } |
| 94 | + |
| 95 | + fn name(&self) -> &str { |
| 96 | + NAME |
| 97 | + } |
| 98 | + |
| 99 | + fn description(&self) -> &str { |
| 100 | + "IOMMU capability checks" |
| 101 | + } |
| 102 | + |
| 103 | + async fn run(&self) -> CheckGroupResult { |
| 104 | + self.run_all().await |
| 105 | + } |
| 106 | +} |
0 commit comments