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 71bf685 commit 8bcf53c
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +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::path::PathBuf;
use std::os::fd::{AsRawFd, IntoRawFd};
use std::os::unix::net::UnixListener;
use std::path::{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";
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 @@ -1231,10 +1236,28 @@ pub struct QmpSocket {
}

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

let root_path = Path::new(KATA_PATH).join(sid);

let _default_proto = MonitorProtocol::new("qmp");
// The default QMP socket or called base socket is qmp.sock.
qmp_sockets.push(QmpSocket {
protocol: MonitorProtocol::new("qmp"),
address: root_path.join(QMP_SOCKET_FILE),
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"),
address: root_path.join(QMP_EXTRA_SOCKET_FILE),
server: true,
nowait: true,
});
}

Ok(qmp_sockets)
}
Expand All @@ -1243,9 +1266,33 @@ impl QmpSocket {
#[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 => {
let listener = UnixListener::bind(self.address.clone())
.context("unix listener bind failed.")?;
let raw_fd = listener.into_raw_fd();
clear_cloexec(raw_fd).context("clearing unix listenser O_CLOEXEC failed")?;

params.push(format!("unix:fd={}", raw_fd));
}
// -monitor unix:path=SOCK_PATH,server=on,wait=off
MonitorProtocol::Hmp => {
params.push(format!("unix:path={}", self.address.display().to_string()));
}
}

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 @@ -1300,7 +1347,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 8bcf53c

Please sign in to comment.