mirror of
https://github.com/edera-dev/krata.git
synced 2026-04-02 21:00:17 +00:00
feat(xen): update xenclient and xenplatform to the latest structure (#433)
This commit is contained in:
89
crates/xen/xenclient/src/tx/channel.rs
Normal file
89
crates/xen/xenclient/src/tx/channel.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use xenplatform::domain::PlatformDomainInfo;
|
||||
|
||||
use super::{DeviceConfig, DeviceDescription, DeviceResult, XenTransaction};
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct ChannelDeviceConfig {
|
||||
backend_type: String,
|
||||
default_console: bool,
|
||||
default_console_options: Option<(u32, u64)>,
|
||||
backend_initialized: bool,
|
||||
}
|
||||
|
||||
impl Default for ChannelDeviceConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ChannelDeviceConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
backend_type: "console".to_string(),
|
||||
default_console: false,
|
||||
default_console_options: None,
|
||||
backend_initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_type(&mut self, backend_type: impl AsRef<str>) -> &mut Self {
|
||||
self.backend_type = backend_type.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn default_console(&mut self) -> &mut Self {
|
||||
self.default_console = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn backend_initialized(&mut self) -> &mut Self {
|
||||
self.backend_initialized = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn prepare(&mut self, platform: &PlatformDomainInfo) -> Result<()> {
|
||||
if self.default_console {
|
||||
self.default_console_options = Some((platform.console_evtchn, platform.console_mfn));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DeviceConfig for ChannelDeviceConfig {
|
||||
type Result = DeviceResult;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<DeviceResult> {
|
||||
let id = tx.assign_next_devid().await?;
|
||||
let mut device = DeviceDescription::new("console", &self.backend_type);
|
||||
device
|
||||
.add_backend_bool("online", true)
|
||||
.add_backend_item("protocol", "vt100")
|
||||
.add_backend_item("type", &self.backend_type)
|
||||
.add_backend_item("state", if self.backend_initialized { 4 } else { 1 });
|
||||
|
||||
if self.default_console {
|
||||
device.special_frontend_path("console");
|
||||
let (port, ring_ref) = self
|
||||
.default_console_options
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::ParameterMissing("default_console_options"))?;
|
||||
device
|
||||
.add_frontend_item("port", port)
|
||||
.add_frontend_item("ring-ref", ring_ref);
|
||||
}
|
||||
|
||||
device
|
||||
.add_frontend_item("limit", 1048576)
|
||||
.add_frontend_item("output", "pty")
|
||||
.add_frontend_item("tty", "")
|
||||
.add_frontend_item("type", &self.backend_type)
|
||||
.add_frontend_item("state", 1);
|
||||
tx.add_device(id, device).await?;
|
||||
Ok(DeviceResult { id })
|
||||
}
|
||||
}
|
||||
78
crates/xen/xenclient/src/tx/fs9p.rs
Normal file
78
crates/xen/xenclient/src/tx/fs9p.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use super::{DeviceConfig, DeviceDescription, DeviceResult, XenTransaction};
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct Fs9pDeviceConfig {
|
||||
backend_type: String,
|
||||
security_model: String,
|
||||
path: Option<String>,
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Fs9pDeviceConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Fs9pDeviceConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
backend_type: "9pfs".to_string(),
|
||||
security_model: "none".to_string(),
|
||||
path: None,
|
||||
tag: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_type(&mut self, backend_type: impl AsRef<str>) -> &mut Self {
|
||||
self.backend_type = backend_type.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn security_model(&mut self, security_model: impl AsRef<str>) -> &mut Self {
|
||||
self.security_model = security_model.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn path(&mut self, path: impl AsRef<str>) -> &mut Self {
|
||||
self.path = Some(path.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn tag(&mut self, tag: impl AsRef<str>) -> &mut Self {
|
||||
self.tag = Some(tag.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DeviceConfig for Fs9pDeviceConfig {
|
||||
type Result = DeviceResult;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<DeviceResult> {
|
||||
let id = tx.assign_next_devid().await?;
|
||||
let path = self
|
||||
.path
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::ParameterMissing("path"))?;
|
||||
let tag = self
|
||||
.tag
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::ParameterMissing("tag"))?;
|
||||
let mut device = DeviceDescription::new("9pfs", &self.backend_type);
|
||||
device
|
||||
.add_backend_bool("online", true)
|
||||
.add_backend_item("state", 1)
|
||||
.add_backend_item("path", path)
|
||||
.add_backend_item("security_model", &self.security_model);
|
||||
device
|
||||
.add_frontend_item("state", 1)
|
||||
.add_frontend_item("tag", tag);
|
||||
tx.add_device(id, device).await?;
|
||||
Ok(DeviceResult { id })
|
||||
}
|
||||
}
|
||||
425
crates/xen/xenclient/src/tx/mod.rs
Normal file
425
crates/xen/xenclient/src/tx/mod.rs
Normal file
@@ -0,0 +1,425 @@
|
||||
pub mod channel;
|
||||
pub mod fs9p;
|
||||
pub mod pci;
|
||||
pub mod vbd;
|
||||
pub mod vif;
|
||||
|
||||
use crate::{
|
||||
devalloc::DeviceIdAllocator,
|
||||
error::{Error, Result},
|
||||
};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use tokio::sync::Mutex;
|
||||
use xenplatform::domain::{PlatformDomainConfig, PlatformDomainInfo};
|
||||
use xenstore::{
|
||||
XsPermission, XsdClient, XsdInterface, XsdTransaction, XS_PERM_NONE, XS_PERM_READ,
|
||||
XS_PERM_READ_WRITE,
|
||||
};
|
||||
|
||||
pub struct XenTransaction {
|
||||
frontend_domid: u32,
|
||||
frontend_dom_path: String,
|
||||
backend_domid: u32,
|
||||
backend_dom_path: String,
|
||||
blkalloc: Arc<Mutex<DeviceIdAllocator>>,
|
||||
devalloc: Arc<Mutex<DeviceIdAllocator>>,
|
||||
tx: XsdTransaction,
|
||||
abort: bool,
|
||||
}
|
||||
|
||||
impl XenTransaction {
|
||||
pub async fn new(store: &XsdClient, frontend_domid: u32, backend_domid: u32) -> Result<Self> {
|
||||
let frontend_dom_path = store.get_domain_path(frontend_domid).await?;
|
||||
let backend_dom_path = store.get_domain_path(backend_domid).await?;
|
||||
let tx = store.transaction().await?;
|
||||
|
||||
let devalloc = XenTransaction::load_id_allocator(&tx, "devid", &frontend_dom_path).await?;
|
||||
let blkalloc = XenTransaction::load_id_allocator(&tx, "blkid", &frontend_dom_path).await?;
|
||||
|
||||
Ok(XenTransaction {
|
||||
frontend_domid,
|
||||
frontend_dom_path,
|
||||
backend_domid,
|
||||
backend_dom_path,
|
||||
tx,
|
||||
devalloc: Arc::new(Mutex::new(devalloc)),
|
||||
blkalloc: Arc::new(Mutex::new(blkalloc)),
|
||||
abort: true,
|
||||
})
|
||||
}
|
||||
|
||||
async fn load_id_allocator(
|
||||
tx: &XsdTransaction,
|
||||
allocator_type: &str,
|
||||
frontend_dom_path: &str,
|
||||
) -> Result<DeviceIdAllocator> {
|
||||
let state = tx
|
||||
.read(format!(
|
||||
"{}/{}-alloc-state",
|
||||
frontend_dom_path, allocator_type
|
||||
))
|
||||
.await?;
|
||||
let allocator = state
|
||||
.and_then(|state| DeviceIdAllocator::deserialize(&state))
|
||||
.unwrap_or_else(DeviceIdAllocator::new);
|
||||
Ok(allocator)
|
||||
}
|
||||
|
||||
pub async fn assign_next_devid(&self) -> Result<u64> {
|
||||
self.devalloc
|
||||
.lock()
|
||||
.await
|
||||
.allocate()
|
||||
.ok_or(Error::DevIdExhausted)
|
||||
.map(|x| x as u64)
|
||||
}
|
||||
|
||||
pub async fn assign_next_blkidx(&self) -> Result<u32> {
|
||||
self.blkalloc
|
||||
.lock()
|
||||
.await
|
||||
.allocate()
|
||||
.ok_or(Error::DevIdExhausted)
|
||||
}
|
||||
|
||||
pub async fn release_devid(&self, devid: u64) -> Result<()> {
|
||||
self.devalloc.lock().await.release(devid as u32);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn release_blkid(&self, blkid: u32) -> Result<()> {
|
||||
self.blkalloc.lock().await.release(blkid);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write(
|
||||
&self,
|
||||
key: impl AsRef<str>,
|
||||
value: impl AsRef<str>,
|
||||
perms: Option<&[XsPermission]>,
|
||||
) -> Result<()> {
|
||||
let path = format!("{}/{}", self.frontend_dom_path, key.as_ref());
|
||||
if let Some(perms) = perms {
|
||||
self.tx.mknod(&path, perms).await?;
|
||||
}
|
||||
|
||||
// empty string is written by mknod, if perms is set we can skip it.
|
||||
if perms.is_none() || perms.is_some() && !value.as_ref().is_empty() {
|
||||
self.tx.write_string(path, value.as_ref()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_domain_declaration(
|
||||
&self,
|
||||
name: Option<impl AsRef<str>>,
|
||||
platform: &PlatformDomainConfig,
|
||||
created: &PlatformDomainInfo,
|
||||
) -> Result<()> {
|
||||
let vm_path = format!("/vm/{}", platform.uuid);
|
||||
let ro_perm = &[
|
||||
XsPermission {
|
||||
id: 0,
|
||||
perms: XS_PERM_NONE,
|
||||
},
|
||||
XsPermission {
|
||||
id: self.frontend_domid,
|
||||
perms: XS_PERM_READ,
|
||||
},
|
||||
];
|
||||
|
||||
let no_perm = &[XsPermission {
|
||||
id: 0,
|
||||
perms: XS_PERM_NONE,
|
||||
}];
|
||||
|
||||
let rw_perm = &[XsPermission {
|
||||
id: self.frontend_domid,
|
||||
perms: XS_PERM_READ_WRITE,
|
||||
}];
|
||||
|
||||
self.tx.rm(&self.frontend_dom_path).await?;
|
||||
self.tx.mknod(&self.frontend_dom_path, ro_perm).await?;
|
||||
|
||||
self.tx.rm(&vm_path).await?;
|
||||
self.tx.mknod(&vm_path, no_perm).await?;
|
||||
self.tx
|
||||
.write_string(format!("{}/uuid", vm_path), &platform.uuid.to_string())
|
||||
.await?;
|
||||
|
||||
self.write("vm", &vm_path, None).await?;
|
||||
self.write("cpu", "", Some(ro_perm)).await?;
|
||||
self.write("memory", "", Some(ro_perm)).await?;
|
||||
self.write("control", "", Some(ro_perm)).await?;
|
||||
self.write("control/shutdown", "", Some(rw_perm)).await?;
|
||||
self.write("control/feature-poweroff", "", Some(rw_perm))
|
||||
.await?;
|
||||
self.write("control/feature-reboot", "", Some(rw_perm))
|
||||
.await?;
|
||||
self.write("control/feature-suspend", "", Some(rw_perm))
|
||||
.await?;
|
||||
self.write("control/sysrq", "", Some(rw_perm)).await?;
|
||||
self.write("data", "", Some(rw_perm)).await?;
|
||||
self.write("drivers", "", Some(rw_perm)).await?;
|
||||
self.write("feature", "", Some(rw_perm)).await?;
|
||||
self.write("attr", "", Some(rw_perm)).await?;
|
||||
self.write("error", "", Some(rw_perm)).await?;
|
||||
self.write("uuid", platform.uuid.to_string(), Some(ro_perm))
|
||||
.await?;
|
||||
if let Some(name) = name {
|
||||
self.write("name", name.as_ref(), Some(ro_perm)).await?;
|
||||
}
|
||||
self.write(
|
||||
"memory/static-max",
|
||||
(platform.resources.max_memory_mb * 1024).to_string(),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
self.write(
|
||||
"memory/target",
|
||||
(platform.resources.assigned_memory_mb * 1024).to_string(),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
self.write("memory/videoram", "0", None).await?;
|
||||
self.write("domid", self.frontend_domid.to_string(), None)
|
||||
.await?;
|
||||
self.write("type", "PV", None).await?;
|
||||
self.write("store/port", created.store_evtchn.to_string(), None)
|
||||
.await?;
|
||||
self.write("store/ring-ref", created.store_mfn.to_string(), None)
|
||||
.await?;
|
||||
for i in 0..platform.resources.max_vcpus {
|
||||
let path = format!("{}/cpu/{}", self.frontend_dom_path, i);
|
||||
self.tx.mkdir(&path).await?;
|
||||
self.tx.set_perms(&path, ro_perm).await?;
|
||||
let path = format!("{}/cpu/{}/availability", self.frontend_dom_path, i);
|
||||
self.tx
|
||||
.write_string(
|
||||
&path,
|
||||
if i < platform.resources.assigned_vcpus {
|
||||
"online"
|
||||
} else {
|
||||
"offline"
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
self.tx.set_perms(&path, ro_perm).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_device(&self, id: u64, device: DeviceDescription) -> Result<()> {
|
||||
let frontend_path = if let Some(ref special_frontend_path) = device.special_frontend_path {
|
||||
format!("{}/{}", self.frontend_dom_path, special_frontend_path)
|
||||
} else {
|
||||
format!(
|
||||
"{}/device/{}/{}",
|
||||
self.frontend_dom_path, device.frontend_type, id
|
||||
)
|
||||
};
|
||||
let backend_path = format!(
|
||||
"{}/backend/{}/{}/{}",
|
||||
self.backend_dom_path, device.backend_type, self.frontend_domid, id
|
||||
);
|
||||
|
||||
let frontend_perms = &[
|
||||
XsPermission {
|
||||
id: self.frontend_domid,
|
||||
perms: XS_PERM_READ_WRITE,
|
||||
},
|
||||
XsPermission {
|
||||
id: self.backend_domid,
|
||||
perms: XS_PERM_READ,
|
||||
},
|
||||
];
|
||||
|
||||
let backend_perms = &[
|
||||
XsPermission {
|
||||
id: self.backend_domid,
|
||||
perms: XS_PERM_READ_WRITE,
|
||||
},
|
||||
XsPermission {
|
||||
id: self.frontend_domid,
|
||||
perms: XS_PERM_READ,
|
||||
},
|
||||
];
|
||||
|
||||
self.tx.mknod(&frontend_path, frontend_perms).await?;
|
||||
self.tx.mknod(&backend_path, backend_perms).await?;
|
||||
|
||||
for (key, value) in &device.backend_items {
|
||||
let path = format!("{}/{}", backend_path, key);
|
||||
self.tx.write_string(&path, value).await?;
|
||||
}
|
||||
|
||||
self.tx
|
||||
.write_string(format!("{}/frontend", backend_path), &frontend_path)
|
||||
.await?;
|
||||
self.tx
|
||||
.write_string(
|
||||
format!("{}/frontend-id", backend_path),
|
||||
&self.frontend_domid.to_string(),
|
||||
)
|
||||
.await?;
|
||||
for (key, value) in &device.frontend_items {
|
||||
let path = format!("{}/{}", frontend_path, key);
|
||||
self.tx.write_string(&path, value).await?;
|
||||
if device.special_frontend_path.is_none() {
|
||||
self.tx.set_perms(&path, frontend_perms).await?;
|
||||
}
|
||||
}
|
||||
self.tx
|
||||
.write_string(format!("{}/backend", frontend_path), &backend_path)
|
||||
.await?;
|
||||
self.tx
|
||||
.write_string(
|
||||
format!("{}/backend-id", frontend_path),
|
||||
&self.backend_domid.to_string(),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_rw_path(&self, key: impl AsRef<str>) -> Result<()> {
|
||||
let rw_perm = &[XsPermission {
|
||||
id: self.frontend_domid,
|
||||
perms: XS_PERM_READ_WRITE,
|
||||
}];
|
||||
|
||||
self.tx
|
||||
.mknod(
|
||||
&format!("{}/{}", self.frontend_dom_path, key.as_ref()),
|
||||
rw_perm,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn before_commit(&self) -> Result<()> {
|
||||
let devid_allocator_state = self.devalloc.lock().await.serialize();
|
||||
let blkid_allocator_state = self.blkalloc.lock().await.serialize();
|
||||
self.tx
|
||||
.write(
|
||||
format!("{}/devid-alloc-state", self.frontend_dom_path),
|
||||
devid_allocator_state,
|
||||
)
|
||||
.await?;
|
||||
self.tx
|
||||
.write(
|
||||
format!("{}/blkid-alloc-state", self.frontend_dom_path),
|
||||
blkid_allocator_state,
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn maybe_commit(mut self) -> Result<bool> {
|
||||
self.abort = false;
|
||||
self.before_commit().await?;
|
||||
Ok(self.tx.maybe_commit().await?)
|
||||
}
|
||||
|
||||
pub async fn commit(mut self) -> Result<()> {
|
||||
self.abort = false;
|
||||
self.before_commit().await?;
|
||||
self.tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for XenTransaction {
|
||||
fn drop(&mut self) {
|
||||
if !self.abort {
|
||||
return;
|
||||
}
|
||||
let tx = self.tx.clone();
|
||||
tokio::task::spawn(async move {
|
||||
let _ = tx.abort().await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DeviceDescription {
|
||||
frontend_type: String,
|
||||
backend_type: String,
|
||||
special_frontend_path: Option<String>,
|
||||
frontend_items: HashMap<String, String>,
|
||||
backend_items: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl DeviceDescription {
|
||||
pub fn new(frontend_type: impl AsRef<str>, backend_type: impl AsRef<str>) -> Self {
|
||||
Self {
|
||||
frontend_type: frontend_type.as_ref().to_string(),
|
||||
backend_type: backend_type.as_ref().to_string(),
|
||||
special_frontend_path: None,
|
||||
frontend_items: HashMap::new(),
|
||||
backend_items: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn special_frontend_path(&mut self, path: impl AsRef<str>) -> &mut Self {
|
||||
self.special_frontend_path = Some(path.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_frontend_item(&mut self, key: impl AsRef<str>, value: impl ToString) -> &mut Self {
|
||||
self.frontend_items
|
||||
.insert(key.as_ref().to_string(), value.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_backend_item(&mut self, key: impl AsRef<str>, value: impl ToString) -> &mut Self {
|
||||
self.backend_items
|
||||
.insert(key.as_ref().to_string(), value.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_frontend_bool(&mut self, key: impl AsRef<str>, value: bool) -> &mut Self {
|
||||
self.add_frontend_item(key, if value { "1" } else { "0" })
|
||||
}
|
||||
|
||||
pub fn add_backend_bool(&mut self, key: impl AsRef<str>, value: bool) -> &mut Self {
|
||||
self.add_backend_item(key, if value { "1" } else { "0" })
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DeviceResult {
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BlockDeviceResult {
|
||||
pub id: u64,
|
||||
pub idx: u32,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait DeviceConfig {
|
||||
type Result;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<Self::Result>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BlockDeviceRef {
|
||||
pub path: String,
|
||||
pub major: u32,
|
||||
pub minor: u32,
|
||||
}
|
||||
|
||||
impl BlockDeviceRef {
|
||||
pub fn new(path: impl AsRef<str>, major: u32, minor: u32) -> Self {
|
||||
Self {
|
||||
path: path.as_ref().to_string(),
|
||||
major,
|
||||
minor,
|
||||
}
|
||||
}
|
||||
}
|
||||
194
crates/xen/xenclient/src/tx/pci.rs
Normal file
194
crates/xen/xenclient/src/tx/pci.rs
Normal file
@@ -0,0 +1,194 @@
|
||||
use super::{DeviceConfig, DeviceDescription, DeviceResult, XenTransaction};
|
||||
use crate::{
|
||||
error::{Error, Result},
|
||||
pci::{PciBdf, XenPciBackend},
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
use xencall::{sys::DOMCTL_DEV_RDM_RELAXED, XenCall};
|
||||
use xenplatform::sys::XEN_PAGE_SHIFT;
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub enum PciRdmReservePolicy {
|
||||
Invalid,
|
||||
#[default]
|
||||
Strict,
|
||||
Relaxed,
|
||||
}
|
||||
|
||||
impl PciRdmReservePolicy {
|
||||
pub fn to_option_str(&self) -> &str {
|
||||
match self {
|
||||
PciRdmReservePolicy::Invalid => "-1",
|
||||
PciRdmReservePolicy::Strict => "0",
|
||||
PciRdmReservePolicy::Relaxed => "1",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PciDeviceConfig {
|
||||
bdf: PciBdf,
|
||||
rdm_reserve_policy: PciRdmReservePolicy,
|
||||
permissive: bool,
|
||||
msi_translate: bool,
|
||||
power_management: bool,
|
||||
}
|
||||
|
||||
pub struct PciRootDeviceConfig {
|
||||
backend_type: String,
|
||||
devices: Vec<PciDeviceConfig>,
|
||||
}
|
||||
|
||||
impl PciDeviceConfig {
|
||||
pub fn new(bdf: PciBdf) -> Self {
|
||||
Self {
|
||||
bdf,
|
||||
rdm_reserve_policy: PciRdmReservePolicy::Strict,
|
||||
permissive: false,
|
||||
msi_translate: false,
|
||||
power_management: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rdm_reserve_policy(&mut self, rdm_reserve_policy: PciRdmReservePolicy) -> &mut Self {
|
||||
self.rdm_reserve_policy = rdm_reserve_policy;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn permissive(&mut self, permissive: bool) -> &mut Self {
|
||||
self.permissive = permissive;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn msi_translate(&mut self, msi_translate: bool) -> &mut Self {
|
||||
self.msi_translate = msi_translate;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn power_management(&mut self, power_management: bool) -> &mut Self {
|
||||
self.power_management = power_management;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PciRootDeviceConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl PciRootDeviceConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
backend_type: "pci".to_string(),
|
||||
devices: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_type(&mut self, backend_type: impl AsRef<str>) -> &mut Self {
|
||||
self.backend_type = backend_type.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_device(&mut self, device: PciDeviceConfig) -> &mut Self {
|
||||
self.devices.push(device);
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn prepare(&self, domid: u32, call: &XenCall) -> Result<()> {
|
||||
for device in &self.devices {
|
||||
let backend = XenPciBackend::new();
|
||||
if !backend.is_assigned(&device.bdf).await? {
|
||||
return Err(Error::PciDeviceNotAssignable(device.bdf));
|
||||
}
|
||||
let resources = backend.read_resources(&device.bdf).await?;
|
||||
for resource in resources {
|
||||
if resource.is_bar_io() {
|
||||
call.ioport_permission(
|
||||
domid,
|
||||
resource.start as u32,
|
||||
resource.size() as u32,
|
||||
true,
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
call.iomem_permission(
|
||||
domid,
|
||||
resource.start >> XEN_PAGE_SHIFT,
|
||||
(resource.size() + (XEN_PAGE_SHIFT - 1)) >> XEN_PAGE_SHIFT,
|
||||
true,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(irq) = backend.read_irq(&device.bdf).await? {
|
||||
let irq = call.map_pirq(domid, irq as isize, None).await?;
|
||||
call.irq_permission(domid, irq, true).await?;
|
||||
}
|
||||
|
||||
backend.reset(&device.bdf).await?;
|
||||
|
||||
call.assign_device(
|
||||
domid,
|
||||
device.bdf.encode(),
|
||||
if device.rdm_reserve_policy == PciRdmReservePolicy::Relaxed {
|
||||
DOMCTL_DEV_RDM_RELAXED
|
||||
} else {
|
||||
0
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
if device.permissive {
|
||||
backend.enable_permissive(&device.bdf).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DeviceConfig for PciRootDeviceConfig {
|
||||
type Result = DeviceResult;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<DeviceResult> {
|
||||
let id = tx.assign_next_devid().await?;
|
||||
let mut device = DeviceDescription::new("pci", &self.backend_type);
|
||||
device
|
||||
.add_backend_bool("online", true)
|
||||
.add_backend_item("state", 1)
|
||||
.add_backend_item("num_devs", self.devices.len());
|
||||
|
||||
for (index, pci) in self.devices.iter().enumerate() {
|
||||
let mut options = IndexMap::new();
|
||||
options.insert("permissive", if pci.permissive { "1" } else { "0" });
|
||||
options.insert("rdm_policy", pci.rdm_reserve_policy.to_option_str());
|
||||
options.insert("msitranslate", if pci.msi_translate { "1" } else { "0" });
|
||||
let options = options
|
||||
.into_iter()
|
||||
.map(|(key, value)| format!("{}={}", key, value))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
device
|
||||
.add_backend_item(format!("key-{}", index), pci.bdf.to_string())
|
||||
.add_backend_item(format!("dev-{}", index), pci.bdf.to_string())
|
||||
.add_backend_item(format!("opts-{}", index), options);
|
||||
|
||||
if let Some(vdefn) = pci.bdf.vdefn {
|
||||
device.add_backend_item(format!("vdefn-{}", index), format!("{:#x}", vdefn));
|
||||
}
|
||||
}
|
||||
|
||||
device.add_frontend_item("state", 1);
|
||||
tx.add_device(id, device).await?;
|
||||
Ok(DeviceResult { id })
|
||||
}
|
||||
}
|
||||
131
crates/xen/xenclient/src/tx/vbd.rs
Normal file
131
crates/xen/xenclient/src/tx/vbd.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use super::{BlockDeviceRef, BlockDeviceResult, DeviceConfig, DeviceDescription, XenTransaction};
|
||||
use crate::{
|
||||
error::{Error, Result},
|
||||
util::vbd_blkidx_to_disk_name,
|
||||
};
|
||||
|
||||
pub struct VbdDeviceConfig {
|
||||
backend_type: String,
|
||||
removable: bool,
|
||||
bootable: bool,
|
||||
writable: bool,
|
||||
discard: bool,
|
||||
trusted: bool,
|
||||
block_device: Option<BlockDeviceRef>,
|
||||
}
|
||||
|
||||
impl Default for VbdDeviceConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl VbdDeviceConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
backend_type: "vbd".to_string(),
|
||||
removable: false,
|
||||
bootable: true,
|
||||
writable: false,
|
||||
discard: false,
|
||||
trusted: true,
|
||||
block_device: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_type(&mut self, backend_type: impl AsRef<str>) -> &mut Self {
|
||||
self.backend_type = backend_type.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn removable(&mut self, removable: bool) -> &mut Self {
|
||||
self.removable = removable;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn bootable(&mut self, bootable: bool) -> &mut Self {
|
||||
self.bootable = bootable;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn writable(&mut self, writable: bool) -> &mut Self {
|
||||
self.writable = writable;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn discard(&mut self, discard: bool) -> &mut Self {
|
||||
self.discard = discard;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn trusted(&mut self, trusted: bool) -> &mut Self {
|
||||
self.trusted = trusted;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn block_device(&mut self, block_device: BlockDeviceRef) -> &mut Self {
|
||||
self.block_device = Some(block_device);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DeviceConfig for VbdDeviceConfig {
|
||||
type Result = BlockDeviceResult;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<BlockDeviceResult> {
|
||||
let id = tx.assign_next_devid().await?;
|
||||
let idx = tx.assign_next_blkidx().await?;
|
||||
let vdev = vbd_blkidx_to_disk_name(idx)?;
|
||||
let block_device = self
|
||||
.block_device
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::ParameterMissing("block device"))?;
|
||||
|
||||
let mut device = DeviceDescription::new("vbd", &self.backend_type);
|
||||
device
|
||||
.add_backend_item("online", 1)
|
||||
.add_backend_bool("removable", self.removable)
|
||||
.add_backend_bool("bootable", self.bootable)
|
||||
.add_backend_item("type", "phy")
|
||||
.add_backend_item("device-type", "disk")
|
||||
.add_backend_item("discard-enable", self.discard)
|
||||
.add_backend_item("specification", "xen")
|
||||
.add_backend_item("physical-device-path", &block_device.path)
|
||||
.add_backend_item("mode", if self.writable { "w" } else { "r" })
|
||||
.add_backend_item(
|
||||
"physical-device",
|
||||
format!("{:02x}:{:02x}", block_device.major, block_device.minor),
|
||||
)
|
||||
.add_backend_item("dev", &vdev)
|
||||
.add_backend_item("state", 1);
|
||||
|
||||
// we should use standard virtual-device support for first few block devices.
|
||||
// the kernel warns when you use ext for indexes 5 or less, due to
|
||||
// potential id overlapping.
|
||||
let (vdev, vd_key) = if idx <= 5 {
|
||||
// shift by 4 as partition count is 16
|
||||
((202 << 8) | (idx as u64 * 16u64), "virtual-device")
|
||||
} else {
|
||||
// this is silly but 256 is the number of partitions
|
||||
// multiply the index by that to get the actual id
|
||||
((1u64 << 28u64) + (idx as u64) * 256, "virtual-device-ext")
|
||||
};
|
||||
|
||||
device
|
||||
.add_frontend_item(vd_key, vdev)
|
||||
.add_frontend_item("state", 1)
|
||||
.add_frontend_item("device-type", "disk")
|
||||
.add_frontend_bool("trusted", self.trusted)
|
||||
.add_frontend_item("protocol", "x86_64-abi")
|
||||
.add_frontend_item("x-index", idx);
|
||||
|
||||
tx.add_device(id, device).await?;
|
||||
|
||||
Ok(BlockDeviceResult { id, idx })
|
||||
}
|
||||
}
|
||||
112
crates/xen/xenclient/src/tx/vif.rs
Normal file
112
crates/xen/xenclient/src/tx/vif.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use super::{DeviceConfig, DeviceDescription, DeviceResult, XenTransaction};
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct VifDeviceConfig {
|
||||
backend_type: String,
|
||||
mac: Option<String>,
|
||||
mtu: Option<u32>,
|
||||
script: Option<String>,
|
||||
bridge: Option<String>,
|
||||
trusted: bool,
|
||||
}
|
||||
|
||||
impl Default for VifDeviceConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl VifDeviceConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
backend_type: "vif".to_string(),
|
||||
mac: None,
|
||||
mtu: None,
|
||||
script: None,
|
||||
bridge: None,
|
||||
trusted: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_type(&mut self, backend_type: impl AsRef<str>) -> &mut Self {
|
||||
self.backend_type = backend_type.as_ref().to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn mac(&mut self, mac: impl AsRef<str>) -> &mut Self {
|
||||
self.mac = Some(mac.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn mtu(&mut self, mtu: u32) -> &mut Self {
|
||||
self.mtu = Some(mtu);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn script(&mut self, script: impl AsRef<str>) -> &mut Self {
|
||||
self.script = Some(script.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn bridge(&mut self, bridge: impl AsRef<str>) -> &mut Self {
|
||||
self.bridge = Some(bridge.as_ref().to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn trusted(&mut self, trusted: bool) -> &mut Self {
|
||||
self.trusted = trusted;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn done(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DeviceConfig for VifDeviceConfig {
|
||||
type Result = DeviceResult;
|
||||
|
||||
async fn add_to_transaction(&self, tx: &XenTransaction) -> Result<DeviceResult> {
|
||||
let id = tx.assign_next_devid().await?;
|
||||
let mac = self
|
||||
.mac
|
||||
.as_ref()
|
||||
.ok_or_else(|| Error::ParameterMissing("mac address"))?;
|
||||
let mtu = self
|
||||
.mtu
|
||||
.ok_or_else(|| Error::ParameterMissing("mtu"))?
|
||||
.to_string();
|
||||
let mut device = DeviceDescription::new("vif", &self.backend_type);
|
||||
device
|
||||
.add_backend_item("online", 1)
|
||||
.add_backend_item("state", 1)
|
||||
.add_backend_item("mac", mac)
|
||||
.add_backend_item("mtu", &mtu)
|
||||
.add_backend_item("type", "vif")
|
||||
.add_backend_item("handle", id);
|
||||
|
||||
if let Some(bridge) = self.bridge.as_ref() {
|
||||
device.add_backend_item("bridge", bridge);
|
||||
}
|
||||
|
||||
if let Some(script) = self.script.as_ref() {
|
||||
device
|
||||
.add_backend_item("script", script)
|
||||
.add_backend_item("hotplug-status", "");
|
||||
} else {
|
||||
device
|
||||
.add_backend_item("script", "")
|
||||
.add_backend_item("hotplug-status", "connected");
|
||||
}
|
||||
|
||||
device
|
||||
.add_frontend_item("state", 1)
|
||||
.add_frontend_item("mac", mac)
|
||||
.add_frontend_item("mtu", &mtu)
|
||||
.add_frontend_bool("trusted", self.trusted);
|
||||
|
||||
tx.add_device(id, device.done()).await?;
|
||||
Ok(DeviceResult { id })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user