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 15, 2024
1 parent e2117d3 commit 9429716
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 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 @@ -1178,6 +1178,76 @@ 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)]
pub struct QmpSocket {
// protocol to be used on the socket.
pub protocol: MonitorProtocol,
// QMP unix socket fd to be passed to qemu
pub fd: Option<File>,
// QMP unix socket name to be passed to qemu
pub name: String,
// server tells if this is a server socket.
pub server: bool,
// nowait tells if qemu should block waiting for a client to connect.
pub nowait: bool,
}

impl QmpSocket {
fn new() -> Result<Vec<Self>> {
let qmp_sockets: Vec<QmpSocket> = Vec::new();
let _proto = MonitorProtocol::new("qmp");
Ok(qmp_sockets)
}
}

#[async_trait]
impl ToQemuParams for QmpSocket {
async fn qemu_params(&self) -> Result<Vec<String>> {
let params: Vec<String> = Vec::new();

Ok(vec![params.join(",")])
}
}

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

knobs: Knobs,

Expand All @@ -1228,6 +1299,7 @@ impl<'a> QemuCmdLine<'a> {
smp: Smp::new(config),
machine: Machine::new(config),
cpu: Cpu::new(config),
qmp_sockets: QmpSocket::new()?,
knobs: Knobs::new(config),
devices: Vec::new(),
};
Expand Down Expand Up @@ -1437,6 +1509,9 @@ 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?);
for qmp_socket in &self.qmp_sockets {
result.append(&mut qmp_socket.qemu_params().await?);
}

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

0 comments on commit 9429716

Please sign in to comment.