Skip to content

Commit

Permalink
runtime-rs: add QMP params suppport in cmdline
Browse files Browse the repository at this point in the history
Fixes: kata-containers#9603

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
  • Loading branch information
Apokleos committed May 16, 2024
1 parent 8aac225 commit 0df4db8
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use std::collections::HashMap;
use std::fmt::Display;
use std::fs::{read_to_string, File};
use std::os::fd::AsRawFd;
use std::path::PathBuf;
use tokio;

// These should have been called MiB and GiB for better readability but the
// more fitting names unfortunately generate linter warnings.
const MI_B: u64 = 1024 * 1024;
const GI_B: u64 = 1024 * MI_B;

const QMP_SOCKET_FILE: &str = "qmp.sock";

// The approach taken here is inspired by govmm. We build structs, each
// corresponding to a qemu command line parameter, like Kernel, or a device,
// for instance MemoryBackendFile. Members of these structs mostly directly
Expand Down Expand Up @@ -1178,6 +1181,85 @@ impl ToQemuParams for DeviceIntelIommu {
}
}

// Qemu provides methods and types for managing QEMU instances.
// To manage a qemu instance after it has been launched you need
// to pass the -qmp option during launch requesting the qemu instance
// to create a QMP unix domain manageent socket, e.g.,
// -qmp unix:fd=SOCK_FD,server=on,wait=off.
// -monitor unix:path=SOCK_PATH,server=on,wait=off.
#[derive(Debug, Default, PartialEq)]
pub enum MonitorProtocol {
// Socket using a human-friendly text-based protocol.
Hmp,

// Socket using a richer json-based protocol.
#[default]
Qmp,

// Same as Qmp with pretty json formatting.
QmpPretty,
}

impl MonitorProtocol {
pub fn new(proto: &str) -> Self {
match proto {
"hmp" => MonitorProtocol::Hmp,
"qmp-pretty" => MonitorProtocol::QmpPretty,
_ => MonitorProtocol::Qmp,
}
}
}

impl ToString for MonitorProtocol {
fn to_string(&self) -> String {
match *self {
MonitorProtocol::Hmp => "monitor".to_string(),
MonitorProtocol::QmpPretty => "qmp-pretty".to_string(),
_ => "qmp".to_string(),
}
}
}

#[derive(Debug)]
#[allow(dead_code)]
enum QmpSockType {
Fd(File),
Path(PathBuf),
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct QmpSocket {
// protocol to be used on the socket.
protocol: MonitorProtocol,
// QMP unix socket to be passed to qemu
address: QmpSockType,
// server tells if this is a server socket.
server: bool,
// nowait tells if qemu should block waiting for a client to connect.
nowait: bool,
}

impl QmpSocket {
fn new() -> Result<Self> {
Ok(QmpSocket {
protocol: MonitorProtocol::new("qmp"),
address: QmpSockType::Path(QMP_SOCKET_FILE.into()),
server: true,
nowait: true,
})
}
}

#[async_trait]
impl ToQemuParams for QmpSocket {
async fn qemu_params(&self) -> Result<Vec<String>> {
let param_qmp = format!("-{}", self.protocol.to_string());

Ok(vec![param_qmp])
}
}

fn is_running_in_vm() -> Result<bool> {
let res = read_to_string("/proc/cpuinfo")?
.lines()
Expand Down Expand Up @@ -1212,6 +1294,7 @@ pub struct QemuCmdLine<'a> {
smp: Smp,
machine: Machine,
cpu: Cpu,
qmp_socket: QmpSocket,

knobs: Knobs,

Expand All @@ -1228,6 +1311,7 @@ impl<'a> QemuCmdLine<'a> {
smp: Smp::new(config),
machine: Machine::new(config),
cpu: Cpu::new(config),
qmp_socket: QmpSocket::new()?,
knobs: Knobs::new(config),
devices: Vec::new(),
};
Expand Down Expand Up @@ -1437,6 +1521,7 @@ impl<'a> QemuCmdLine<'a> {
result.append(&mut self.machine.qemu_params().await?);
result.append(&mut self.cpu.qemu_params().await?);
result.append(&mut self.memory.qemu_params().await?);
result.append(&mut self.qmp_socket.qemu_params().await?);

for device in &self.devices {
result.append(&mut device.qemu_params().await?);
Expand Down

0 comments on commit 0df4db8

Please sign in to comment.