kratart: support for krata channels for two-way byte communication

This commit is contained in:
Alex Zenla
2024-03-28 05:31:57 +00:00
parent d4f1ee5521
commit 63f7db6cf4
9 changed files with 484 additions and 132 deletions

View File

@ -275,7 +275,7 @@ impl XenClient {
initrd.as_slice(),
config.max_vcpus,
config.mem_mb,
1 + config.channels.len(),
1,
)?;
boot.boot(&mut arch, &mut state, config.cmdline)?;
xenstore_evtchn = state.store_evtchn;

View File

@ -8,8 +8,8 @@ pub enum Error {
Io(#[from] io::Error),
#[error("failed to read structure")]
StructureReadFailed,
#[error("mmap failed")]
MmapFailed,
#[error("mmap failed: {0}")]
MmapFailed(nix::errno::Errno),
}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -2,10 +2,14 @@ pub mod error;
pub mod sys;
use error::{Error, Result};
use nix::errno::Errno;
use std::{
fs::{File, OpenOptions},
marker::PhantomData,
os::{fd::AsRawFd, raw::c_void},
sync::Arc,
thread::sleep,
time::Duration,
};
use sys::{
AllocGref, DeallocGref, GetOffsetForVaddr, GrantRef, MapGrantRef, SetMaxGrants, UnmapGrantRef,
@ -151,24 +155,28 @@ pub struct GrantTab {
const PAGE_SIZE: usize = 4096;
#[allow(clippy::len_without_is_empty)]
pub struct MappedMemory {
pub struct MappedMemory<'a> {
gnttab: GrantTab,
length: usize,
addr: *mut c_void,
addr: u64,
_ptr: PhantomData<&'a c_void>,
}
impl MappedMemory {
unsafe impl Send for MappedMemory<'_> {}
impl MappedMemory<'_> {
pub fn len(&self) -> usize {
self.length
}
pub fn ptr(&self) -> *mut c_void {
self.addr
self.addr as *mut c_void
}
}
impl Drop for MappedMemory {
impl Drop for MappedMemory<'_> {
fn drop(&mut self) {
let _ = unsafe { munmap(self.addr, self.length) };
let _ = self.gnttab.unmap(self);
}
}
@ -179,12 +187,12 @@ impl GrantTab {
})
}
pub fn map_grant_refs(
pub fn map_grant_refs<'a>(
&self,
refs: Vec<GrantRef>,
read: bool,
write: bool,
) -> Result<MappedMemory> {
) -> Result<MappedMemory<'a>> {
let (index, refs) = self.device.map_grant_ref(refs)?;
unsafe {
let mut flags: i32 = 0;
@ -196,21 +204,39 @@ impl GrantTab {
flags |= PROT_WRITE;
}
let addr = mmap(
std::ptr::null_mut(),
PAGE_SIZE * refs.len(),
flags,
MAP_SHARED,
self.device.handle.as_raw_fd(),
index as i64,
);
if addr == MAP_FAILED {
return Err(Error::MmapFailed);
}
let addr = loop {
let addr = mmap(
std::ptr::null_mut(),
PAGE_SIZE * refs.len(),
flags,
MAP_SHARED,
self.device.handle.as_raw_fd(),
index as i64,
);
let errno = Errno::last();
if addr == MAP_FAILED {
if errno == Errno::EAGAIN {
sleep(Duration::from_micros(1000));
continue;
}
return Err(Error::MmapFailed(errno));
}
break addr;
};
Ok(MappedMemory {
addr,
gnttab: self.clone(),
addr: addr as u64,
length: PAGE_SIZE * refs.len(),
_ptr: PhantomData,
})
}
}
fn unmap(&self, memory: &MappedMemory<'_>) -> Result<()> {
let (offset, count) = self.device.get_offset_for_vaddr(memory.addr)?;
let _ = unsafe { munmap(memory.addr as *mut c_void, memory.length) };
self.device.unmap_grant_ref(offset, count)?;
Ok(())
}
}

View File

@ -3,7 +3,7 @@ use std::mem::size_of;
use nix::{ioc, ioctl_readwrite_bad};
#[repr(C)]
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct GrantRef {
pub domid: u32,
pub reference: u32,
@ -33,7 +33,7 @@ impl MapGrantRef {
return None;
}
let index = (*data.get(2)? as u64) << 32 | *data.get(3)? as u64;
let index = (*data.get(2)? as u64) | (*data.get(3)? as u64) << 32;
for i in (4..data.len()).step_by(2) {
let Some(domid) = data.get(i) else {
break;
@ -146,10 +146,10 @@ impl AllocGref {
return None;
}
let index = (*data.get(4)? as u64) << 48
| (*data.get(5)? as u64) << 32
| (*data.get(6)? as u64) << 16
| *data.get(7)? as u64;
let index = (*data.get(4)? as u64)
| (*data.get(5)? as u64) << 16
| (*data.get(6)? as u64) << 32
| (*data.get(7)? as u64) << 48;
for i in (8..data.len()).step_by(2) {
let Some(bits_low) = data.get(i) else {
break;
@ -157,7 +157,7 @@ impl AllocGref {
let Some(bits_high) = data.get(i + 1) else {
break;
};
refs.push((*bits_low as u32) << 16 | *bits_high as u32);
refs.push((*bits_low as u32) | (*bits_high as u32) << 16);
}
Some((index, refs))
}

View File

@ -1,10 +1,20 @@
use std::{collections::HashMap, ffi::CString, io::ErrorKind, os::fd::AsRawFd, sync::Arc};
use std::{
collections::HashMap,
ffi::CString,
io::ErrorKind,
os::{
fd::{AsRawFd, FromRawFd, IntoRawFd},
unix::fs::FileTypeExt,
},
sync::Arc,
};
use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
use log::warn;
use tokio::{
fs::{metadata, File},
io::{AsyncReadExt, AsyncWriteExt},
net::UnixStream,
select,
sync::{
mpsc::{channel, Receiver, Sender},
@ -19,14 +29,16 @@ use crate::{
sys::{XsdMessageHeader, XSD_ERROR, XSD_UNWATCH, XSD_WATCH_EVENT},
};
const XEN_BUS_PATHS: &[&str] = &["/dev/xen/xenbus"];
const XEN_BUS_PATHS: &[&str] = &["/var/run/xenstored/socket", "/dev/xen/xenbus"];
const XEN_BUS_MAX_PAYLOAD_SIZE: usize = 4096;
const XEN_BUS_MAX_PACKET_SIZE: usize = XsdMessageHeader::SIZE + XEN_BUS_MAX_PAYLOAD_SIZE;
async fn find_bus_path() -> Option<&'static str> {
async fn find_bus_path() -> Option<(&'static str, bool)> {
for path in XEN_BUS_PATHS {
match metadata(path).await {
Ok(_) => return Some(path),
Ok(metadata) => {
return Some((path, metadata.file_type().is_socket()));
}
Err(_) => continue,
}
}
@ -58,17 +70,25 @@ pub struct XsdSocket {
impl XsdSocket {
pub async fn open() -> Result<XsdSocket> {
let path = match find_bus_path().await {
let (path, socket) = match find_bus_path().await {
Some(path) => path,
None => return Err(Error::BusNotFound),
};
let file = File::options()
.read(true)
.write(true)
.custom_flags(O_NONBLOCK)
.open(path)
.await?;
let file = if socket {
let stream = UnixStream::connect(path).await?;
let stream = stream.into_std()?;
stream.set_nonblocking(true)?;
unsafe { File::from_raw_fd(stream.into_raw_fd()) }
} else {
File::options()
.read(true)
.write(true)
.custom_flags(O_NONBLOCK)
.open(path)
.await?
};
XsdSocket::from_handle(file).await
}

View File

@ -43,7 +43,7 @@ impl XsPermission {
}
pub struct XsdWatchHandle {
id: u32,
pub id: u32,
unwatch_sender: Sender<u32>,
pub receiver: Receiver<String>,
}
@ -202,7 +202,11 @@ impl XsdClient {
}
pub async fn bind_watch<P: AsRef<str>>(&self, handle: &XsdWatchHandle, path: P) -> Result<()> {
let id_string = handle.id.to_string();
self.bind_watch_id(handle.id, path).await
}
pub async fn bind_watch_id<P: AsRef<str>>(&self, id: u32, path: P) -> Result<()> {
let id_string = id.to_string();
let _ = self
.socket
.send(0, XSD_WATCH, &[path.as_ref(), &id_string])