env: Utilize arch specific functionality to get cpu details

Have kata-env call architecture specific function to get cpu details
instead of generic function to get cpu details that works only for
certain architectures. The functionality for cpu details has been fully
implemented for x86_64 and arm architectures, but needs to be
implemented for s390 and powerpc.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
This commit is contained in:
Archana Shinde 2023-04-26 22:01:03 -07:00
parent fb40c71a21
commit 4064192896
7 changed files with 54 additions and 7 deletions

View File

@ -9,6 +9,7 @@ pub use arch_specific::*;
mod arch_specific {
use crate::check;
use crate::types::*;
use crate::utils;
use anyhow::Result;
use std::path::Path;
@ -37,6 +38,34 @@ mod arch_specific {
Ok(())
}
fn normalize_vendor(vendor: &str) -> String {
match vendor {
"0x41" => String::from("ARM Limited"),
_ => String::from("3rd Party Limited"),
}
}
fn normalize_model(model: &str) -> String {
match model {
"8" => String::from("v8"),
"7" | "7M" | "?(12)" | "?(13)" | "?(14)" | "?(15)" | "?(16)" | "?(17)" => {
String::from("v7")
}
"6" | "6TEJ" => String::from("v6"),
"5" | "5T" | "5TE" | "5TEJ" => String::from("v5"),
"4" | "4T" => String::from("v4"),
"3" => String::from("v3"),
_ => String::from("unknown"),
}
}
pub fn get_cpu_details() -> Result<(String, String)> {
let (vendor, model) = utils::get_generic_cpu_details(check::PROC_CPUINFO)?;
let norm_vendor = normalize_vendor(&vendor);
let norm_model = normalize_model(&model);
Ok((norm_vendor, norm_model))
}
pub fn get_checks() -> Option<&'static [CheckItem<'static>]> {
Some(CHECK_LIST)
}

View File

@ -9,6 +9,7 @@ pub use arch_specific::*;
mod arch_specific {
use crate::check;
use crate::utils;
use anyhow::Result;
pub const ARCH_CPU_VENDOR_FIELD: &str = "";
@ -24,6 +25,14 @@ mod arch_specific {
const PEF_SYS_FIRMWARE_DIR: &str = "/sys/firmware/ultravisor/";
pub fn get_cpu_details() -> Result<(String, String)> {
utils::get_generic_cpu_details(check::PROC_CPUINFO)
// TODO: In case of error from get_generic_cpu_details, implement functionality
// to get cpu details specific to powerpc architecture similar
// to the goloang implementation of function getCPUDetails()
}
pub fn available_guest_protection() -> Result<check::GuestProtection, check::ProtectionError> {
if !Uid::effective().is_root() {
return Err(check::ProtectionError::NoPerms);

View File

@ -10,6 +10,7 @@ pub use arch_specific::*;
mod arch_specific {
use crate::check;
use crate::types::*;
use crate::utils;
use anyhow::{anyhow, Result};
use nix::unistd::Uid;
use std::collections::HashMap;
@ -144,6 +145,14 @@ mod arch_specific {
Ok(false)
}
pub fn get_cpu_details() -> Result<(String, String)> {
utils::get_generic_cpu_details(check::PROC_CPUINFO)
// TODO: In case of error from get_generic_cpu_details, implement functionality
// to get cpu details specific to s390x architecture similar
// to the goloang implementation of function getS390xCPUDetails()
}
#[allow(dead_code)]
// Guest protection is not supported on ARM64.
pub fn available_guest_protection() -> Result<check::GuestProtection, check::ProtectionError> {

View File

@ -12,6 +12,7 @@ mod arch_specific {
use crate::check;
use crate::check::{GuestProtection, ProtectionError};
use crate::types::*;
use crate::utils;
use anyhow::{anyhow, Result};
use nix::unistd::Uid;
use std::fs;
@ -109,6 +110,10 @@ mod arch_specific {
Ok(cpu_flags)
}
pub fn get_cpu_details() -> Result<(String, String)> {
utils::get_generic_cpu_details(check::PROC_CPUINFO)
}
pub const TDX_SYS_FIRMWARE_DIR: &str = "/sys/firmware/tdx_seam/";
pub const TDX_CPU_FLAG: &str = "tdx";
pub const SEV_KVM_PARAMETER_PATH: &str = "/sys/module/kvm_amd/parameters/sev";

View File

@ -47,7 +47,6 @@ pub const GENERIC_CPU_MODEL_FIELD: &str = "model name";
#[allow(dead_code)]
pub const PROC_CPUINFO: &str = "/proc/cpuinfo";
#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
fn read_file_contents(file_path: &str) -> Result<String> {
let contents = std::fs::read_to_string(file_path)?;
Ok(contents)
@ -55,7 +54,6 @@ fn read_file_contents(file_path: &str) -> Result<String> {
// get_single_cpu_info returns the contents of the first cpu from
// the specified cpuinfo file by parsing based on a specified delimiter
#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
pub fn get_single_cpu_info(cpu_info_file: &str, substring: &str) -> Result<String> {
let contents = read_file_contents(cpu_info_file)?;

View File

@ -7,7 +7,6 @@
use crate::arch::arch_specific;
use crate::args::EnvArgument;
use crate::check;
use crate::ops::version;
use crate::utils;
use kata_types::config::TomlConfig;
@ -232,7 +231,7 @@ fn get_host_info() -> Result<HostInfo> {
let host_kernel_version = utils::get_kernel_version(utils::PROC_VERSION_FILE)?;
let (host_distro_name, host_distro_version) =
utils::get_distro_details(utils::OS_RELEASE, utils::OS_RELEASE_CLR)?;
let (cpu_vendor, cpu_model) = utils::get_generic_cpu_details(check::PROC_CPUINFO)?;
let (cpu_vendor, cpu_model) = arch_specific::get_cpu_details()?;
let host_distro = DistroInfo {
name: host_distro_name,

View File

@ -5,9 +5,7 @@
#![allow(dead_code)]
#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
use crate::arch::arch_specific;
#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
use crate::check::get_single_cpu_info;
use anyhow::{anyhow, Context, Result};
@ -106,7 +104,7 @@ pub fn get_distro_details(os_release: &str, os_release_clr: &str) -> Result<(Str
Ok((name, version))
}
#[cfg(any(target_arch = "s390x", target_arch = "x86_64"))]
#[cfg(any(target_arch = "s390x", target_arch = "x86_64", target_arch = "aarch64"))]
pub fn get_generic_cpu_details(cpu_info_file: &str) -> Result<(String, String)> {
let cpu_info = get_single_cpu_info(cpu_info_file, "\n\n")?;
let lines = cpu_info.lines();