Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions bin/propolis-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use clap::{Args, Parser, Subcommand};
use futures::{future, SinkExt};
use newtype_uuid::{GenericUuid, TypedUuid, TypedUuidKind, TypedUuidTag};
use propolis_client::instance_spec::{
BlobStorageBackend, Board, Chipset, Component, CrucibleStorageBackend,
I440Fx, InstanceMetadata, InstanceProperties, InstanceSpec,
InstanceSpecGetResponse, NvmeDisk, PciPath, QemuPvpanic,
BlobStorageBackend, Board, Chipset, Component, CpuTopology,
CrucibleStorageBackend, I440Fx, InstanceMetadata, InstanceProperties,
InstanceSpec, InstanceSpecGetResponse, NvmeDisk, PciPath, QemuPvpanic,
ReplacementComponent, SerialPort, SerialPortNumber, SpecKey, VirtioDisk,
};
use propolis_client::support::nvme_serial_from_str;
Expand Down Expand Up @@ -176,7 +176,7 @@ struct VmConfig {

/// Number of vCPUs allocated to instance
#[clap(short = 'c', default_value = "4", action, requires = "config_toml")]
vcpus: u8,
vcpus: u16,

/// Memory allocated to instance (MiB)
#[clap(short, default_value = "1024", action, requires = "config_toml")]
Expand Down Expand Up @@ -332,11 +332,28 @@ impl VmConfig {
.map(|cfg| cfg.hv_interface.clone())
.unwrap_or_default();

let cpu_topology = match parsed_toml
.as_ref()
.map(|cfg| cfg.machine_settings.cpu_topology.as_str())
{
None | Some("single_socket") => CpuTopology::single_socket_smt(),
Some("legacy_single_thread_sockets") => {
CpuTopology::legacy_single_thread_sockets(self.vcpus)
}
Some(other) => {
anyhow::bail!(
"cpu_topology must be omitted, \"single_socket\" \
or \"legacy_single_thread_sockets\". got: {other}"
)
}
Comment on lines +337 to +348

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would sure be nice if this could be an enum, but, i suppose there is probably some reason it cannot?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a particularly great reason. it would want to be a shared enum with -standalone (or duplicated) and I don't know how much any of this specific config plumbing will stick around beyond like the next week (see #1237 (comment) and #1263)

};

let mut spec = InstanceSpec {
board: Board {
chipset: Chipset::I440Fx(I440Fx { enable_pcie }),
cpuid: cpuid_profile,
cpus: self.vcpus,
cpu_topology,
memory_mb: self.memory,
guest_hv_interface,
},
Expand Down
177 changes: 138 additions & 39 deletions bin/propolis-server/src/lib/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::convert::TryInto;
use std::fs::File;
use std::num::{NonZeroU8, NonZeroUsize};
use std::num::{NonZeroU16, NonZeroUsize};
use std::os::unix::fs::FileTypeExt;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -51,11 +51,12 @@ use propolis::intr_pins;
use propolis::vmm::{self, Builder, Machine};
use propolis::vsock::GuestCid;
use propolis_api_types::instance::InstanceProperties;
use propolis_api_types::instance_spec::components::board::TopoAction;
use propolis_api_types::instance_spec::components::board::TopoLeaves;
use propolis_api_types::instance_spec::components::devices::SerialPortNumber;
use propolis_api_types::instance_spec::{self, SpecKey};
use propolis_types::{CpuidIdent, CpuidVendor};
use slog::info;
use strum::IntoEnumIterator;
use thiserror::Error;

// XXX: completely arb for now
Expand Down Expand Up @@ -119,6 +120,9 @@ pub enum MachineInitError {
#[error("failed to start attestation server")]
AttestationServer(#[source] std::io::Error),

#[error("unsupported VM configuration")]
UnsupportedCpuConfig(&'static str),

#[cfg(feature = "falcon")]
#[error("softnpu p9 device missing")]
SoftNpuP9Missing,
Expand All @@ -143,6 +147,30 @@ fn get_spec_guest_ram_limits(spec: &Spec) -> (usize, usize) {
(lowmem, highmem)
}

#[derive(Debug)]
struct TopoActions {
leaves_to_clear: Vec<TopoKind>,
leaves_to_set: Vec<TopoKind>,
}

impl TopoActions {
fn new() -> Self {
Self { leaves_to_clear: Vec::new(), leaves_to_set: Vec::new() }
}
fn update(&mut self, action: TopoAction, leaf: TopoKind) {
match action {
TopoAction::Ignore => {}
TopoAction::Clear => {
self.leaves_to_clear.push(leaf);
}
TopoAction::Specialize => {
self.leaves_to_clear.push(leaf);
self.leaves_to_set.push(leaf);
}
}
}
}

pub fn build_instance(
name: &str,
spec: &Spec,
Expand Down Expand Up @@ -1229,7 +1257,7 @@ impl MachineInitializer<'_> {
fn generate_smbios(
&self,
bootrom_version: &Option<String>,
) -> smbios::TableBytes {
) -> Result<smbios::TableBytes, MachineInitError> {
use smbios::table::{type0, type1, type16, type4};

let rom_size =
Expand Down Expand Up @@ -1337,7 +1365,31 @@ impl MachineInitializer<'_> {
.and_then(|vals| propolis::cpuid::parse_brand_string(vals).ok())
.unwrap_or_default();

let smb_type4 = smbios::table::Type4 {
let num_cpus: u8 = self.spec.board.cpus.try_into().map_err(|_| {
MachineInitError::UnsupportedCpuConfig(
"more CPUs than expressible in Type 4 SMBIOS v2.7 tables",
)
})?;
Comment thread
acfoltzer marked this conversation as resolved.
Resolved

let has_smt = self.spec.board.cpu_topology.has_smt;
let thread_count = num_cpus;
// Note that propolis-server checks more than just has_smt, unlike
// propolis-standalone. Before CPU topologies were configurable, Type 4
// tables happened to omit HARDWARE_THREAD, even if `has_smt` was set.
// Likewise, the core count did not reflect SMT, even if we hoped CPUID
// would. This happened to be fine in an Oxide rack, because the
// initial topology also happened to be single (non-SMT) threads
// per-socket. Conversely, this was wrong on Intel, where the guest
// topology was one socket with SMT, but these details were not
// reflected in the SMBIOS Type 4 table.
//
// At some point we will likely alter the meaning of has_smt in specs so
// that this bool can be taken as-is (see Propolis#1265 at the least)
let non_legacy_smt =
has_smt && !self.spec.board.has_legacy_cpu_topology();
let core_count = if non_legacy_smt { num_cpus / 2 } else { num_cpus };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reject odd number of vCPUs like standalone?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we ought to, yeah! I was thinking about this as enforced by Nexus when sending a topology with actually-meaningful SMT


let mut smb_type4 = smbios::table::Type4 {
proc_type: type4::ProcType::Central,
proc_family,
proc_manufacturer,
Expand All @@ -1346,15 +1398,19 @@ impl MachineInitializer<'_> {
status: type4::ProcStatus::Enabled,
// unknown
proc_upgrade: 0x2,
// make core and thread counts equal for now
core_count: self.spec.board.cpus,
core_enabled: self.spec.board.cpus,
thread_count: self.spec.board.cpus,
core_count,
core_enabled: core_count,
thread_count,
proc_characteristics: type4::Characteristics::IS_64_BIT
| type4::Characteristics::MULTI_CORE,
..Default::default()
};

if non_legacy_smt {
smb_type4.proc_characteristics |=
type4::Characteristics::HARDWARE_THREAD;
}

let memsize_bytes = (self.spec.board.memory_mb as usize) * MB;
let mut smb_type16 = smbios::table::Type16 {
location: type16::Location::SystemBoard,
Expand Down Expand Up @@ -1391,7 +1447,7 @@ impl MachineInitializer<'_> {
smb_tables.add(0x1700.into(), &smb_type17).unwrap();
smb_tables.add(0x3200.into(), &smb_type32).unwrap();

smb_tables.commit()
Ok(smb_tables.commit())
}

fn generate_e820(&self) -> Result<Entry, MachineInitError> {
Expand Down Expand Up @@ -1478,7 +1534,7 @@ impl MachineInitializer<'_> {
fn generate_acpi_tables(
&self,
acpi_variant: acpi::AcpiVariant,
cpus: u8,
cpus: u16,
) -> Result<fwcfg::formats::AcpiTables, MachineInitError> {
let (lowmem, _) = get_spec_guest_ram_limits(self.spec);
let generators: Vec<_> = self
Expand Down Expand Up @@ -1518,7 +1574,7 @@ impl MachineInitializer<'_> {
/// Should not be called before [`Self::initialize_rom()`].
pub fn initialize_fwcfg(
&mut self,
cpus: u8,
cpus: u16,
bootrom_version: &Option<String>,
acpi_variant: acpi::AcpiVariant,
) -> Result<Arc<ramfb::RamFb>, MachineInitError> {
Expand All @@ -1531,7 +1587,7 @@ impl MachineInitializer<'_> {
.map_err(|e| MachineInitError::FwcfgInsertFailed("cpu count", e))?;

let smbios::TableBytes { entry_point, structure_table } =
self.generate_smbios(bootrom_version);
self.generate_smbios(bootrom_version)?;
fwcfg
.insert_named(
"etc/smbios/smbios-tables",
Expand Down Expand Up @@ -1589,11 +1645,65 @@ impl MachineInitializer<'_> {
Ok(ramfb)
}

/// Specifics of the guest vCPU topology are dictated by the VM's virtual
/// platform, which may not include all supported topology leaves.
fn topo_cpuid_actions(&self) -> Result<TopoActions, MachineInitError> {
// This is trying to find a local minima of annoying.. taking a
// `HashSet<TopoKind>` (or two) in the API directly leaves me a bit
// concerned that a caller providing `[Std4, Std4, Ext8, Ext1E]` would
// not easily see the mistake.
//
// The hope is that taking explicit fields helps guard against some
// kinds of errors, at the cost of us having some glue to do here. But
// *this* code is unlikely to change on a whim whereas users of the
// propolis-server API may be more fleeting (such as hand-crafted json
// in ad-hoc use, propolis-cli, etc)
let mut topo_actions = TopoActions::new();

let TopoLeaves { std_4, std_b, std_1f, ext_8, ext_1e } =
self.spec.board.cpu_topology.topo_leaves;

topo_actions.update(std_4, TopoKind::Std4);
topo_actions.update(std_b, TopoKind::StdB);
topo_actions.update(std_1f, TopoKind::Std1F);
topo_actions.update(ext_8, TopoKind::Ext8);
topo_actions.update(ext_1e, TopoKind::Ext1E);
Ok(topo_actions)
}

/// Initialize virtual CPUs by first setting their capabilities, inserting
/// them into the device map, and then, if a kstat sampler is provided,
/// tracking their kstats.
pub async fn initialize_cpus(&mut self) -> Result<(), MachineInitError> {
let hv_interface = self.machine.guest_hv_interface.as_ref();

let cpus = self.spec.board.cpus;
let cpus_per_socket = if self.spec.board.has_legacy_cpu_topology() {
// This .. unfortunate .. lie is temporary to preserve the initial
// virtual platform's topology. The specializer was run with
// num_vcpu set to `board.cpus`, but all topology leaves were left
// unspecialized. The consequence of this is leaf 1 EBX would report
// all processors, and leaf 8000001D would report an L3 shared by
// vCPU-many cores, while other topology information would report a
// single logical processor per socket.
//
// This is a bug and should be fixed in a new rev of the `Initial`
// virtual platform, probably by making leaf 1 a "normal" topology
// leaf like 4, B, and others.
cpus
} else {
let sockets = self.spec.board.cpu_topology.sockets;
if !cpus.is_multiple_of(sockets) {
return Err(MachineInitError::UnsupportedCpuConfig(
"vCPUs do not evenly divide into sockets",
));
}
cpus / sockets
};

let topo_actions = self.topo_cpuid_actions()?;
info!(self.log, "computed cpu topo actions {:?}", topo_actions);

for vcpu in self.machine.vcpus.iter() {
// Report that the guest is running on bhyve.
//
Expand All @@ -1605,33 +1715,22 @@ impl MachineInitializer<'_> {
requests to set hypervisor leaves",
);

// Instead of `TopoKind::supported`, we use an intentionally-reduced
// list of Intel-only leaves for the moment. This is because if we
// specialize leaves used by AMD (or just both vendors), we'll
// change the topology a guest sees.
//
// The initial CPU platform defined in Nexus (Omicron#8728) hews to
// the pre-specialization topology, which won't have leaf B at all.
// Before that is sent, though, we'll see the present-but-zero
// leaves from bhyve, which we would happily specialize into
// something reflecting the guest if requested here. Once
// Omicron#8728 lands and propolis-server receives explicit CPUID
// profiles, we can add AMD leaves here too.
let cpu_topo_leaves = [TopoKind::Std4];

let specialized = propolis::cpuid::Specializer::new()
.with_vcpu_count(
NonZeroU8::new(self.spec.board.cpus).unwrap(),
true,
)
.with_vcpuid(vcpu.id)
.with_cache_topo()
.clear_cpu_topo(TopoKind::iter())
.with_cpu_topo(cpu_topo_leaves.into_iter())
.execute(set)
.map_err(|e| {
MachineInitError::CpuidSpecializationFailed(vcpu.id, e)
})?;
let has_smt = self.spec.board.cpu_topology.has_smt;

let specialized = propolis::cpuid::Specializer::new(
NonZeroU16::new(cpus_per_socket).unwrap(),
)
.with_cpu_topo(
has_smt,
topo_actions.leaves_to_set.clone().into_iter(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
topo_actions.leaves_to_set.clone().into_iter(),
topo_actions.leaves_to_set.iter().copied(),

And similarly for leaves_to_clear below

)
.with_vcpuid(vcpu.id)
.with_cache_topo()
.clear_cpu_topo(topo_actions.leaves_to_clear.clone().into_iter())
.execute(set)
.map_err(|e| {
MachineInitError::CpuidSpecializationFailed(vcpu.id, e)
})?;

info!(self.log, "setting CPUID for vCPU";
"vcpu" => vcpu.id,
Expand Down
Loading
Loading