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 15, 2024
1 parent 9429716 commit dd06917
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ use crate::{kernel_param::KernelParams, Address, HypervisorConfig};

use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use kata_types::config::KATA_PATH;
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::Path;
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";
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,
// for instance MemoryBackendFile. Members of these structs mostly directly
Expand Down Expand Up @@ -1232,19 +1238,66 @@ pub struct QmpSocket {
}

impl QmpSocket {
fn new() -> Result<Vec<Self>> {
let qmp_sockets: Vec<QmpSocket> = Vec::new();
let _proto = MonitorProtocol::new("qmp");
fn new(sid: &str, extra_monitor: bool) -> Result<Vec<Self>> {
let mut qmp_sockets: Vec<QmpSocket> = Vec::new();

// The default QMP socket or called base socket is qmp.sock, its fd will be passed.
let root_path = Path::new(KATA_PATH).join(sid);
let sock_path = root_path.join(QMP_SOCKET_FILE);
let listener = UnixListener::bind(sock_path).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.
qmp_sockets.push(QmpSocket {
protocol: MonitorProtocol::new("qmp"),
fd: Some(sock_file),
name: QMP_SOCKET_FILE.to_owned(),
server: true,
nowait: true,
});

// If extra monitor needed, HMP socket with qmp-extra.sock will be added.
if extra_monitor {
qmp_sockets.push(QmpSocket {
protocol: MonitorProtocol::new("hmp"),
fd: None,
name: root_path.join(QMP_EXTRA_SOCKET_FILE).display().to_string(),
server: true,
nowait: true,
});
}

Ok(qmp_sockets)
}
}

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

let param_qmp = format!("-{}", self.protocol.to_string());
match self.protocol {
// -qmp unix:fd=SOCK_FD,server=on,wait=off
MonitorProtocol::Qmp | MonitorProtocol::QmpPretty => {
params.push(format!("unix:fd={}", self.fd.as_ref().unwrap().as_raw_fd()));
}
// -monitor unix:path=SOCK_PATH,server=on,wait=off
MonitorProtocol::Hmp => {
params.push(format!("unix:path={}", &self.name));
}
}

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

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

Expand Down Expand Up @@ -1299,7 +1352,7 @@ impl<'a> QemuCmdLine<'a> {
smp: Smp::new(config),
machine: Machine::new(config),
cpu: Cpu::new(config),
qmp_sockets: QmpSocket::new()?,
qmp_sockets: QmpSocket::new(id, false)?,
knobs: Knobs::new(config),
devices: Vec::new(),
};
Expand Down

0 comments on commit dd06917

Please sign in to comment.