mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-02 12:50:54 +00:00
kratart: support for krata channels for two-way byte communication
This commit is contained in:
parent
d4f1ee5521
commit
63f7db6cf4
@ -2,14 +2,18 @@ use anyhow::Result;
|
|||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use kratart::chan::KrataChannelService;
|
use kratart::chan::KrataChannelService;
|
||||||
use xenevtchn::EventChannel;
|
use xenevtchn::EventChannel;
|
||||||
|
use xengnt::GrantTab;
|
||||||
use xenstore::XsdClient;
|
use xenstore::XsdClient;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||||
|
|
||||||
let mut krata =
|
let mut krata = KrataChannelService::new(
|
||||||
KrataChannelService::new(EventChannel::open().await?, XsdClient::open().await?)?;
|
EventChannel::open().await?,
|
||||||
krata.init().await?;
|
XsdClient::open().await?,
|
||||||
|
GrantTab::open()?,
|
||||||
|
)?;
|
||||||
|
krata.watch().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,118 @@
|
|||||||
use std::collections::HashMap;
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::atomic::{fence, Ordering},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
|
use log::{error, info};
|
||||||
|
use tokio::{
|
||||||
|
select,
|
||||||
|
sync::{
|
||||||
|
broadcast,
|
||||||
|
mpsc::{channel, Receiver, Sender},
|
||||||
|
},
|
||||||
|
task::JoinHandle,
|
||||||
|
time::sleep,
|
||||||
|
};
|
||||||
use xenevtchn::EventChannel;
|
use xenevtchn::EventChannel;
|
||||||
use xengnt::{sys::GrantRef, GrantTab};
|
use xengnt::{sys::GrantRef, GrantTab, MappedMemory};
|
||||||
use xenstore::{XsdClient, XsdInterface};
|
use xenstore::{XsdClient, XsdInterface};
|
||||||
|
|
||||||
|
const KRATA_SINGLE_CHANNEL_QUEUE_LEN: usize = 100;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct XenConsoleInterface {
|
struct XenConsoleInterface {
|
||||||
input: [u8; 1024],
|
input: [u8; XenConsoleInterface::INPUT_SIZE],
|
||||||
output: [u8; 2048],
|
output: [u8; XenConsoleInterface::OUTPUT_SIZE],
|
||||||
in_cons: u32,
|
in_cons: u32,
|
||||||
in_prod: u32,
|
in_prod: u32,
|
||||||
out_cons: u32,
|
out_cons: u32,
|
||||||
out_prod: u32,
|
out_prod: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for XenConsoleInterface {}
|
||||||
|
|
||||||
|
impl XenConsoleInterface {
|
||||||
|
const INPUT_SIZE: usize = 1024;
|
||||||
|
const OUTPUT_SIZE: usize = 2048;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct KrataChannelService {
|
pub struct KrataChannelService {
|
||||||
backends: HashMap<(u32, u32), KrataChannelBackend>,
|
backends: HashMap<(u32, u32), KrataChannelBackend>,
|
||||||
evtchn: EventChannel,
|
evtchn: EventChannel,
|
||||||
store: XsdClient,
|
store: XsdClient,
|
||||||
|
gnttab: GrantTab,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KrataChannelService {
|
impl KrataChannelService {
|
||||||
pub fn new(evtchn: EventChannel, store: XsdClient) -> Result<KrataChannelService> {
|
pub fn new(
|
||||||
|
evtchn: EventChannel,
|
||||||
|
store: XsdClient,
|
||||||
|
gnttab: GrantTab,
|
||||||
|
) -> Result<KrataChannelService> {
|
||||||
Ok(KrataChannelService {
|
Ok(KrataChannelService {
|
||||||
backends: HashMap::new(),
|
backends: HashMap::new(),
|
||||||
evtchn,
|
evtchn,
|
||||||
store,
|
store,
|
||||||
|
gnttab,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init(&mut self) -> Result<()> {
|
pub async fn watch(&mut self) -> Result<()> {
|
||||||
|
self.scan_all_backends().await?;
|
||||||
|
let mut watch_handle = self.store.create_watch().await?;
|
||||||
|
self.store
|
||||||
|
.bind_watch(&watch_handle, "/local/domain/0/backend/console".to_string())
|
||||||
|
.await?;
|
||||||
|
loop {
|
||||||
|
let Some(_) = watch_handle.receiver.recv().await else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
self.scan_all_backends().await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ensure_backend_exists(&mut self, domid: u32, id: u32, path: String) -> Result<()> {
|
||||||
|
if self.backends.contains_key(&(domid, id)) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let Some(frontend_path) = self.store.read_string(format!("{}/frontend", path)).await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let Some(typ) = self
|
||||||
|
.store
|
||||||
|
.read_string(format!("{}/type", frontend_path))
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if typ != "krata-channel" {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let backend = KrataChannelBackend::new(
|
||||||
|
path.clone(),
|
||||||
|
frontend_path.clone(),
|
||||||
|
domid,
|
||||||
|
id,
|
||||||
|
self.store.clone(),
|
||||||
|
self.evtchn.clone(),
|
||||||
|
self.gnttab.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
self.backends.insert((domid, id), backend);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn scan_all_backends(&mut self) -> Result<()> {
|
||||||
let domains = self.store.list("/local/domain/0/backend/console").await?;
|
let domains = self.store.list("/local/domain/0/backend/console").await?;
|
||||||
for domid_string in domains {
|
let mut seen: Vec<(u32, u32)> = Vec::new();
|
||||||
|
for domid_string in &domains {
|
||||||
let domid = domid_string.parse::<u32>()?;
|
let domid = domid_string.parse::<u32>()?;
|
||||||
let domid_path = format!("/local/domain/0/backend/console/{}", domid);
|
let domid_path = format!("/local/domain/0/backend/console/{}", domid);
|
||||||
for id_string in self.store.list(&domid_path).await? {
|
for id_string in self.store.list(&domid_path).await? {
|
||||||
@ -41,118 +121,335 @@ impl KrataChannelService {
|
|||||||
"/local/domain/0/backend/console/{}/{}",
|
"/local/domain/0/backend/console/{}/{}",
|
||||||
domid_string, id_string
|
domid_string, id_string
|
||||||
);
|
);
|
||||||
let Some(frontend_path) = self
|
self.ensure_backend_exists(domid, id, console_path).await?;
|
||||||
.store
|
seen.push((domid, id));
|
||||||
.read_string(format!("{}/frontend", console_path))
|
|
||||||
.await?
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let Some(typ) = self
|
|
||||||
.store
|
|
||||||
.read_string(format!("{}/type", frontend_path))
|
|
||||||
.await?
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if typ != "krata-channel" {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let Some(ring_ref_string) = self
|
|
||||||
.store
|
|
||||||
.read_string(format!("{}/ring-ref", frontend_path))
|
|
||||||
.await?
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(port_string) = self
|
|
||||||
.store
|
|
||||||
.read_string(format!("{}/port", frontend_path))
|
|
||||||
.await?
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let ring_ref = ring_ref_string.parse::<u64>()?;
|
|
||||||
let port = port_string.parse::<u32>()?;
|
|
||||||
let backend = KrataChannelBackend {
|
|
||||||
backend: console_path.clone(),
|
|
||||||
domid,
|
|
||||||
ring_ref,
|
|
||||||
port,
|
|
||||||
store: self.store.clone(),
|
|
||||||
evtchn: self.evtchn.clone(),
|
|
||||||
grant: GrantTab::open()?,
|
|
||||||
};
|
|
||||||
|
|
||||||
backend.init().await?;
|
|
||||||
self.backends.insert((domid, id), backend);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut gone: Vec<(u32, u32)> = Vec::new();
|
||||||
|
for backend in self.backends.keys() {
|
||||||
|
if !seen.contains(backend) {
|
||||||
|
gone.push(*backend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for item in gone {
|
||||||
|
if let Some(backend) = self.backends.remove(&item) {
|
||||||
|
drop(backend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct KrataChannelBackend {
|
||||||
|
pub domid: u32,
|
||||||
|
pub id: u32,
|
||||||
|
pub receiver: Receiver<Vec<u8>>,
|
||||||
|
pub sender: Sender<Vec<u8>>,
|
||||||
|
task: JoinHandle<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for KrataChannelBackend {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.task.abort();
|
||||||
|
info!(
|
||||||
|
"destroyed channel backend for domain {} channel {}",
|
||||||
|
self.domid, self.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KrataChannelBackend {
|
||||||
|
pub async fn new(
|
||||||
|
backend: String,
|
||||||
|
frontend: String,
|
||||||
|
domid: u32,
|
||||||
|
id: u32,
|
||||||
|
store: XsdClient,
|
||||||
|
evtchn: EventChannel,
|
||||||
|
gnttab: GrantTab,
|
||||||
|
) -> Result<KrataChannelBackend> {
|
||||||
|
let processor = KrataChannelBackendProcessor {
|
||||||
|
backend,
|
||||||
|
frontend,
|
||||||
|
domid,
|
||||||
|
id,
|
||||||
|
store,
|
||||||
|
evtchn,
|
||||||
|
gnttab,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (output_sender, output_receiver) = channel(KRATA_SINGLE_CHANNEL_QUEUE_LEN);
|
||||||
|
let (input_sender, input_receiver) = channel(KRATA_SINGLE_CHANNEL_QUEUE_LEN);
|
||||||
|
|
||||||
|
let task = processor.launch(output_sender, input_receiver).await?;
|
||||||
|
Ok(KrataChannelBackend {
|
||||||
|
domid,
|
||||||
|
id,
|
||||||
|
task,
|
||||||
|
receiver: output_receiver,
|
||||||
|
sender: input_sender,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct KrataChannelBackend {
|
pub struct KrataChannelBackendProcessor {
|
||||||
backend: String,
|
backend: String,
|
||||||
|
frontend: String,
|
||||||
|
id: u32,
|
||||||
domid: u32,
|
domid: u32,
|
||||||
ring_ref: u64,
|
|
||||||
port: u32,
|
|
||||||
store: XsdClient,
|
store: XsdClient,
|
||||||
evtchn: EventChannel,
|
evtchn: EventChannel,
|
||||||
grant: GrantTab,
|
gnttab: GrantTab,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KrataChannelBackend {
|
impl KrataChannelBackendProcessor {
|
||||||
pub async fn init(&self) -> Result<()> {
|
async fn init(&self) -> Result<()> {
|
||||||
self.store.write_string(&self.backend, "4").await?;
|
self.store
|
||||||
|
.write_string(format!("{}/state", self.backend), "3")
|
||||||
|
.await?;
|
||||||
|
info!(
|
||||||
|
"created channel backend for domain {} channel {}",
|
||||||
|
self.domid, self.id
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read(&self) -> Result<()> {
|
async fn on_frontend_state_change(&self) -> Result<bool> {
|
||||||
let memory = self.grant.map_grant_refs(
|
let state = self
|
||||||
|
.store
|
||||||
|
.read_string(format!("{}/state", self.backend))
|
||||||
|
.await?
|
||||||
|
.unwrap_or("0".to_string())
|
||||||
|
.parse::<u32>()?;
|
||||||
|
if state == 3 {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn on_self_state_change(&self) -> Result<bool> {
|
||||||
|
let state = self
|
||||||
|
.store
|
||||||
|
.read_string(format!("{}/state", self.backend))
|
||||||
|
.await?
|
||||||
|
.unwrap_or("0".to_string())
|
||||||
|
.parse::<u32>()?;
|
||||||
|
if state == 5 {
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn launch(
|
||||||
|
&self,
|
||||||
|
output_sender: Sender<Vec<u8>>,
|
||||||
|
input_receiver: Receiver<Vec<u8>>,
|
||||||
|
) -> Result<JoinHandle<()>> {
|
||||||
|
let owned = self.clone();
|
||||||
|
Ok(tokio::task::spawn(async move {
|
||||||
|
if let Err(error) = owned.processor(output_sender, input_receiver).await {
|
||||||
|
error!("failed to process krata channel: {}", error);
|
||||||
|
}
|
||||||
|
let _ = owned
|
||||||
|
.store
|
||||||
|
.write_string(format!("{}/state", owned.backend), "6")
|
||||||
|
.await;
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn processor(
|
||||||
|
&self,
|
||||||
|
sender: Sender<Vec<u8>>,
|
||||||
|
mut receiver: Receiver<Vec<u8>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
self.init().await?;
|
||||||
|
let mut frontend_state_change = self.store.create_watch().await?;
|
||||||
|
self.store
|
||||||
|
.bind_watch(&frontend_state_change, format!("{}/state", self.frontend))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let (ring_ref, port) = loop {
|
||||||
|
match frontend_state_change.receiver.recv().await {
|
||||||
|
Some(_) => {
|
||||||
|
if self.on_frontend_state_change().await? {
|
||||||
|
let mut tries = 0;
|
||||||
|
let (ring_ref, port) = loop {
|
||||||
|
let ring_ref = self
|
||||||
|
.store
|
||||||
|
.read_string(format!("{}/ring-ref", self.frontend))
|
||||||
|
.await?;
|
||||||
|
let port = self
|
||||||
|
.store
|
||||||
|
.read_string(format!("{}/port", self.frontend))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if (ring_ref.is_none() || port.is_none()) && tries < 10 {
|
||||||
|
tries += 1;
|
||||||
|
self.store
|
||||||
|
.write_string(format!("{}/state", self.backend), "4")
|
||||||
|
.await?;
|
||||||
|
sleep(Duration::from_millis(250)).await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break (ring_ref, port);
|
||||||
|
};
|
||||||
|
|
||||||
|
if ring_ref.is_none() || port.is_none() {
|
||||||
|
return Err(anyhow!("frontend did not give ring-ref and port"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(ring_ref) = ring_ref.unwrap().parse::<u64>() else {
|
||||||
|
return Err(anyhow!("frontend gave invalid ring-ref"));
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(port) = port.unwrap().parse::<u32>() else {
|
||||||
|
return Err(anyhow!("frontend gave invalid port"));
|
||||||
|
};
|
||||||
|
|
||||||
|
break (ring_ref, port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None => {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.store
|
||||||
|
.write_string(format!("{}/state", self.backend), "4")
|
||||||
|
.await?;
|
||||||
|
let memory = self.gnttab.map_grant_refs(
|
||||||
vec![GrantRef {
|
vec![GrantRef {
|
||||||
domid: self.domid,
|
domid: self.domid,
|
||||||
reference: self.ring_ref as u32,
|
reference: ring_ref as u32,
|
||||||
}],
|
}],
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
)?;
|
)?;
|
||||||
let interface = memory.ptr() as *mut XenConsoleInterface;
|
let mut channel = self.evtchn.bind(self.domid, port).await?;
|
||||||
let mut channel = self.evtchn.bind(self.domid, self.port).await?;
|
unsafe {
|
||||||
unsafe { self.read_buffer(channel.local_port, interface).await? };
|
let buffer = self.read_output_buffer(channel.local_port, &memory).await?;
|
||||||
|
if !buffer.is_empty() {
|
||||||
|
sender.send(buffer).await?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut self_state_change = self.store.create_watch().await?;
|
||||||
|
self.store
|
||||||
|
.bind_watch(&self_state_change, format!("{}/state", self.backend))
|
||||||
|
.await?;
|
||||||
loop {
|
loop {
|
||||||
channel.receiver.recv().await?;
|
select! {
|
||||||
unsafe { self.read_buffer(channel.local_port, interface).await? };
|
x = self_state_change.receiver.recv() => match x {
|
||||||
channel.unmask_sender.send(channel.local_port).await?;
|
Some(_) => {
|
||||||
|
match self.on_self_state_change().await {
|
||||||
|
Err(error) => {
|
||||||
|
error!("failed to process state change for domain {} channel {}: {}", self.domid, self.id, error);
|
||||||
|
},
|
||||||
|
|
||||||
|
Ok(stop) => {
|
||||||
|
if stop {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
None => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
x = receiver.recv() => match x {
|
||||||
|
Some(data) => {
|
||||||
|
let mut index = 0;
|
||||||
|
loop {
|
||||||
|
if index >= data.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let interface = memory.ptr() as *mut XenConsoleInterface;
|
||||||
|
let cons = unsafe { (*interface).in_cons };
|
||||||
|
let mut prod = unsafe { (*interface).in_prod };
|
||||||
|
fence(Ordering::Release);
|
||||||
|
let space = (prod - cons) as usize;
|
||||||
|
if space > XenConsoleInterface::INPUT_SIZE {
|
||||||
|
error!("channel for domid {} has an invalid input space of {}", self.domid, space);
|
||||||
|
}
|
||||||
|
let free = XenConsoleInterface::INPUT_SIZE - space;
|
||||||
|
let want = data.len().min(free);
|
||||||
|
let buffer = &data[index..want];
|
||||||
|
for b in buffer {
|
||||||
|
unsafe { (*interface).input[prod as usize & (XenConsoleInterface::INPUT_SIZE - 1)] = *b; };
|
||||||
|
prod += 1;
|
||||||
|
}
|
||||||
|
fence(Ordering::Release);
|
||||||
|
unsafe { (*interface).in_prod = prod; };
|
||||||
|
self.evtchn.notify(channel.local_port).await?;
|
||||||
|
index += want;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
None => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
x = channel.receiver.recv() => match x {
|
||||||
|
Ok(_) => {
|
||||||
|
unsafe {
|
||||||
|
let buffer = self.read_output_buffer(channel.local_port, &memory).await?;
|
||||||
|
if !buffer.is_empty() {
|
||||||
|
sender.send(buffer).await?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
channel.unmask_sender.send(channel.local_port).await?;
|
||||||
|
},
|
||||||
|
|
||||||
|
Err(error) => {
|
||||||
|
match error {
|
||||||
|
broadcast::error::RecvError::Closed => {
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
return Err(anyhow!("failed to receive event notification: {}", error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async unsafe fn read_buffer(
|
async unsafe fn read_output_buffer<'a>(
|
||||||
&self,
|
&self,
|
||||||
local_port: u32,
|
local_port: u32,
|
||||||
interface: *mut XenConsoleInterface,
|
memory: &MappedMemory<'a>,
|
||||||
) -> Result<()> {
|
) -> Result<Vec<u8>> {
|
||||||
|
let interface = memory.ptr() as *mut XenConsoleInterface;
|
||||||
let mut cons = (*interface).out_cons;
|
let mut cons = (*interface).out_cons;
|
||||||
let prod = (*interface).out_prod;
|
let prod = (*interface).out_prod;
|
||||||
|
fence(Ordering::Release);
|
||||||
let size = prod - cons;
|
let size = prod - cons;
|
||||||
if size == 0 || size > 2048 {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let mut data: Vec<u8> = Vec::new();
|
let mut data: Vec<u8> = Vec::new();
|
||||||
|
if size == 0 || size as usize > XenConsoleInterface::OUTPUT_SIZE {
|
||||||
|
return Ok(data);
|
||||||
|
}
|
||||||
loop {
|
loop {
|
||||||
if cons == prod {
|
if cons == prod {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
data.push((*interface).output[cons as usize]);
|
data.push((*interface).output[cons as usize & (XenConsoleInterface::OUTPUT_SIZE - 1)]);
|
||||||
cons += 1;
|
cons += 1;
|
||||||
}
|
}
|
||||||
|
fence(Ordering::AcqRel);
|
||||||
(*interface).out_cons = cons;
|
(*interface).out_cons = cons;
|
||||||
self.evtchn.notify(local_port).await?;
|
self.evtchn.notify(local_port).await?;
|
||||||
Ok(())
|
Ok(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use krata::launchcfg::{
|
|||||||
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use xenclient::{DomainChannel, DomainConfig, DomainDisk, DomainNetworkInterface};
|
use xenclient::{DomainConfig, DomainDisk, DomainNetworkInterface};
|
||||||
use xenstore::XsdInterface;
|
use xenstore::XsdInterface;
|
||||||
|
|
||||||
use crate::cfgblk::ConfigBlock;
|
use crate::cfgblk::ConfigBlock;
|
||||||
@ -180,10 +180,11 @@ impl GuestLauncher {
|
|||||||
writable: false,
|
writable: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
channels: vec![DomainChannel {
|
// channels: vec![DomainChannel {
|
||||||
typ: "krata-channel".to_string(),
|
// typ: "krata-channel".to_string(),
|
||||||
initialized: true,
|
// initialized: false,
|
||||||
}],
|
// }],
|
||||||
|
channels: vec![],
|
||||||
vifs: vec![DomainNetworkInterface {
|
vifs: vec![DomainNetworkInterface {
|
||||||
mac: &guest_mac_string,
|
mac: &guest_mac_string,
|
||||||
mtu: 1500,
|
mtu: 1500,
|
@ -275,7 +275,7 @@ impl XenClient {
|
|||||||
initrd.as_slice(),
|
initrd.as_slice(),
|
||||||
config.max_vcpus,
|
config.max_vcpus,
|
||||||
config.mem_mb,
|
config.mem_mb,
|
||||||
1 + config.channels.len(),
|
1,
|
||||||
)?;
|
)?;
|
||||||
boot.boot(&mut arch, &mut state, config.cmdline)?;
|
boot.boot(&mut arch, &mut state, config.cmdline)?;
|
||||||
xenstore_evtchn = state.store_evtchn;
|
xenstore_evtchn = state.store_evtchn;
|
||||||
|
@ -8,8 +8,8 @@ pub enum Error {
|
|||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
#[error("failed to read structure")]
|
#[error("failed to read structure")]
|
||||||
StructureReadFailed,
|
StructureReadFailed,
|
||||||
#[error("mmap failed")]
|
#[error("mmap failed: {0}")]
|
||||||
MmapFailed,
|
MmapFailed(nix::errno::Errno),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
@ -2,10 +2,14 @@ pub mod error;
|
|||||||
pub mod sys;
|
pub mod sys;
|
||||||
|
|
||||||
use error::{Error, Result};
|
use error::{Error, Result};
|
||||||
|
use nix::errno::Errno;
|
||||||
use std::{
|
use std::{
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
|
marker::PhantomData,
|
||||||
os::{fd::AsRawFd, raw::c_void},
|
os::{fd::AsRawFd, raw::c_void},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
thread::sleep,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
use sys::{
|
use sys::{
|
||||||
AllocGref, DeallocGref, GetOffsetForVaddr, GrantRef, MapGrantRef, SetMaxGrants, UnmapGrantRef,
|
AllocGref, DeallocGref, GetOffsetForVaddr, GrantRef, MapGrantRef, SetMaxGrants, UnmapGrantRef,
|
||||||
@ -151,24 +155,28 @@ pub struct GrantTab {
|
|||||||
const PAGE_SIZE: usize = 4096;
|
const PAGE_SIZE: usize = 4096;
|
||||||
|
|
||||||
#[allow(clippy::len_without_is_empty)]
|
#[allow(clippy::len_without_is_empty)]
|
||||||
pub struct MappedMemory {
|
pub struct MappedMemory<'a> {
|
||||||
|
gnttab: GrantTab,
|
||||||
length: usize,
|
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 {
|
pub fn len(&self) -> usize {
|
||||||
self.length
|
self.length
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ptr(&self) -> *mut c_void {
|
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) {
|
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,
|
&self,
|
||||||
refs: Vec<GrantRef>,
|
refs: Vec<GrantRef>,
|
||||||
read: bool,
|
read: bool,
|
||||||
write: bool,
|
write: bool,
|
||||||
) -> Result<MappedMemory> {
|
) -> Result<MappedMemory<'a>> {
|
||||||
let (index, refs) = self.device.map_grant_ref(refs)?;
|
let (index, refs) = self.device.map_grant_ref(refs)?;
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut flags: i32 = 0;
|
let mut flags: i32 = 0;
|
||||||
@ -196,21 +204,39 @@ impl GrantTab {
|
|||||||
flags |= PROT_WRITE;
|
flags |= PROT_WRITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
let addr = mmap(
|
let addr = loop {
|
||||||
std::ptr::null_mut(),
|
let addr = mmap(
|
||||||
PAGE_SIZE * refs.len(),
|
std::ptr::null_mut(),
|
||||||
flags,
|
PAGE_SIZE * refs.len(),
|
||||||
MAP_SHARED,
|
flags,
|
||||||
self.device.handle.as_raw_fd(),
|
MAP_SHARED,
|
||||||
index as i64,
|
self.device.handle.as_raw_fd(),
|
||||||
);
|
index as i64,
|
||||||
if addr == MAP_FAILED {
|
);
|
||||||
return Err(Error::MmapFailed);
|
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 {
|
Ok(MappedMemory {
|
||||||
addr,
|
gnttab: self.clone(),
|
||||||
|
addr: addr as u64,
|
||||||
length: PAGE_SIZE * refs.len(),
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ use std::mem::size_of;
|
|||||||
use nix::{ioc, ioctl_readwrite_bad};
|
use nix::{ioc, ioctl_readwrite_bad};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct GrantRef {
|
pub struct GrantRef {
|
||||||
pub domid: u32,
|
pub domid: u32,
|
||||||
pub reference: u32,
|
pub reference: u32,
|
||||||
@ -33,7 +33,7 @@ impl MapGrantRef {
|
|||||||
return None;
|
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) {
|
for i in (4..data.len()).step_by(2) {
|
||||||
let Some(domid) = data.get(i) else {
|
let Some(domid) = data.get(i) else {
|
||||||
break;
|
break;
|
||||||
@ -146,10 +146,10 @@ impl AllocGref {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let index = (*data.get(4)? as u64) << 48
|
let index = (*data.get(4)? as u64)
|
||||||
| (*data.get(5)? as u64) << 32
|
| (*data.get(5)? as u64) << 16
|
||||||
| (*data.get(6)? as u64) << 16
|
| (*data.get(6)? as u64) << 32
|
||||||
| *data.get(7)? as u64;
|
| (*data.get(7)? as u64) << 48;
|
||||||
for i in (8..data.len()).step_by(2) {
|
for i in (8..data.len()).step_by(2) {
|
||||||
let Some(bits_low) = data.get(i) else {
|
let Some(bits_low) = data.get(i) else {
|
||||||
break;
|
break;
|
||||||
@ -157,7 +157,7 @@ impl AllocGref {
|
|||||||
let Some(bits_high) = data.get(i + 1) else {
|
let Some(bits_high) = data.get(i + 1) else {
|
||||||
break;
|
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))
|
Some((index, refs))
|
||||||
}
|
}
|
||||||
|
@ -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 libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::{metadata, File},
|
fs::{metadata, File},
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
net::UnixStream,
|
||||||
select,
|
select,
|
||||||
sync::{
|
sync::{
|
||||||
mpsc::{channel, Receiver, Sender},
|
mpsc::{channel, Receiver, Sender},
|
||||||
@ -19,14 +29,16 @@ use crate::{
|
|||||||
sys::{XsdMessageHeader, XSD_ERROR, XSD_UNWATCH, XSD_WATCH_EVENT},
|
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_PAYLOAD_SIZE: usize = 4096;
|
||||||
const XEN_BUS_MAX_PACKET_SIZE: usize = XsdMessageHeader::SIZE + XEN_BUS_MAX_PAYLOAD_SIZE;
|
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 {
|
for path in XEN_BUS_PATHS {
|
||||||
match metadata(path).await {
|
match metadata(path).await {
|
||||||
Ok(_) => return Some(path),
|
Ok(metadata) => {
|
||||||
|
return Some((path, metadata.file_type().is_socket()));
|
||||||
|
}
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,17 +70,25 @@ pub struct XsdSocket {
|
|||||||
|
|
||||||
impl XsdSocket {
|
impl XsdSocket {
|
||||||
pub async fn open() -> Result<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,
|
Some(path) => path,
|
||||||
None => return Err(Error::BusNotFound),
|
None => return Err(Error::BusNotFound),
|
||||||
};
|
};
|
||||||
|
|
||||||
let file = File::options()
|
let file = if socket {
|
||||||
.read(true)
|
let stream = UnixStream::connect(path).await?;
|
||||||
.write(true)
|
let stream = stream.into_std()?;
|
||||||
.custom_flags(O_NONBLOCK)
|
stream.set_nonblocking(true)?;
|
||||||
.open(path)
|
unsafe { File::from_raw_fd(stream.into_raw_fd()) }
|
||||||
.await?;
|
} else {
|
||||||
|
File::options()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.custom_flags(O_NONBLOCK)
|
||||||
|
.open(path)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
|
||||||
XsdSocket::from_handle(file).await
|
XsdSocket::from_handle(file).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ impl XsPermission {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct XsdWatchHandle {
|
pub struct XsdWatchHandle {
|
||||||
id: u32,
|
pub id: u32,
|
||||||
unwatch_sender: Sender<u32>,
|
unwatch_sender: Sender<u32>,
|
||||||
pub receiver: Receiver<String>,
|
pub receiver: Receiver<String>,
|
||||||
}
|
}
|
||||||
@ -202,7 +202,11 @@ impl XsdClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn bind_watch<P: AsRef<str>>(&self, handle: &XsdWatchHandle, path: P) -> Result<()> {
|
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
|
let _ = self
|
||||||
.socket
|
.socket
|
||||||
.send(0, XSD_WATCH, &[path.as_ref(), &id_string])
|
.send(0, XSD_WATCH, &[path.as_ref(), &id_string])
|
||||||
|
Loading…
Reference in New Issue
Block a user