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 14, 2024
1 parent e2117d3 commit ee276c8
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,72 @@ 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=SOCKFD,server=on,wait=off.
#[derive(Debug, Default, PartialEq)]
#[allow(dead_code)]
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 listener file descriptor to be passed to qemu
pub fd: File,
// 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>> {
Ok(vec![])
}
}

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

knobs: Knobs,

Expand All @@ -1228,6 +1296,9 @@ impl<'a> QemuCmdLine<'a> {
smp: Smp::new(config),
machine: Machine::new(config),
cpu: Cpu::new(config),
rtc: Rtc::new(),
qmp_sockets: QmpSocket::new()?,

knobs: Knobs::new(config),
devices: Vec::new(),
};
Expand Down Expand Up @@ -1437,6 +1508,10 @@ 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.rtc.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 ee276c8

Please sign in to comment.