Skip to content

Commit

Permalink
runtime-rs: build qemu params for QmpSocket and refine new method
Browse files Browse the repository at this point in the history
Fixies: kata-containers#9603

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

Expand All @@ -21,6 +22,7 @@ const MI_B: u64 = 1024 * 1024;
const GI_B: u64 = 1024 * MI_B;

const QMP_SOCKET_FILE: &str = "qmp.sock";
const QMP_EXTRA_SOCKET_FILE: &str = "qmp-extra.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,
Expand Down Expand Up @@ -1221,14 +1223,12 @@ impl ToString for MonitorProtocol {
}

#[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,
Expand All @@ -1241,13 +1241,35 @@ pub struct QmpSocket {
}

impl QmpSocket {
fn new() -> Result<Self> {
Ok(QmpSocket {
protocol: MonitorProtocol::new("qmp"),
address: QmpSockType::Path(QMP_SOCKET_FILE.into()),
server: true,
nowait: true,
})
fn new(proto: MonitorProtocol) -> Result<Self> {
let qmp_socket = match proto {
MonitorProtocol::Qmp | MonitorProtocol::QmpPretty => {
// let sock_path = root_path.join(QMP_SOCKET_FILE);
let listener =
UnixListener::bind(QMP_SOCKET_FILE).context("unix listener bind failed.")?;
let raw_fd = listener.into_raw_fd();
clear_cloexec(raw_fd).context("clearing unix listenser O_CLOEXEC failed")?;
let sock_file = unsafe { File::from_raw_fd(raw_fd) };
// The default QMP socket or called base socket is qmp.sock.
QmpSocket {
protocol: MonitorProtocol::new("qmp"),
address: QmpSockType::Fd(sock_file),
server: true,
nowait: true,
}
}
MonitorProtocol::Hmp => {
// If extra monitor needed, HMP socket with qmp-extra.sock will be added.
QmpSocket {
protocol: MonitorProtocol::new("hmp"),
address: QmpSockType::Path(PathBuf::from(QMP_EXTRA_SOCKET_FILE)),
server: true,
nowait: true,
}
}
};

Ok(qmp_socket)
}
}

Expand All @@ -1256,7 +1278,23 @@ impl ToQemuParams for QmpSocket {
async fn qemu_params(&self) -> Result<Vec<String>> {
let param_qmp = format!("-{}", self.protocol.to_string());

Ok(vec![param_qmp])
let mut params: Vec<String> = Vec::new();

match &self.address {
// -qmp unix:fd=SOCK_FD,server=on,wait=off
QmpSockType::Fd(f) => params.push(format!("unix:fd={}", f.as_raw_fd())),
// -monitor unix:path=SOCK_PATH,server=on,wait=off
QmpSockType::Path(p) => params.push(format!("unix:path={}", p.display())),
}

if self.server {
params.push("server=on".to_owned());
if self.nowait {
params.push("wait=off".to_owned());
}
}

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

Expand Down Expand Up @@ -1311,7 +1349,7 @@ impl<'a> QemuCmdLine<'a> {
smp: Smp::new(config),
machine: Machine::new(config),
cpu: Cpu::new(config),
qmp_socket: QmpSocket::new()?,
qmp_socket: QmpSocket::new(MonitorProtocol::Qmp)?,
knobs: Knobs::new(config),
devices: Vec::new(),
};
Expand All @@ -1320,11 +1358,22 @@ impl<'a> QemuCmdLine<'a> {
qemu_cmd_line.add_iommu();
}

if config.debug_info.enable_debug && !config.debug_info.dbg_monitor_socket.is_empty() {
qemu_cmd_line.add_monitor(&config.debug_info.dbg_monitor_socket)?;
}

qemu_cmd_line.add_rtc();

Ok(qemu_cmd_line)
}

fn add_monitor(&mut self, proto: &str) -> Result<()> {
let monitor = QmpSocket::new(MonitorProtocol::new(proto))?;
self.devices.push(Box::new(monitor));

Ok(())
}

fn add_rtc(&mut self) {
let rtc = Rtc::new();
self.devices.push(Box::new(rtc));
Expand Down

0 comments on commit bf528f5

Please sign in to comment.