|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use xencall::XenCall; |
| 4 | +pub use xenplatform::domain::PlatformDomainConfig; |
| 5 | +use xenplatform::domain::PlatformDomainInfo; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + error::Result, |
| 9 | + tx::{ |
| 10 | + channel::ChannelDeviceConfig, |
| 11 | + fs9p::Fs9pDeviceConfig, |
| 12 | + pci::PciRootDeviceConfig, |
| 13 | + vbd::VbdDeviceConfig, |
| 14 | + vif::VifDeviceConfig, |
| 15 | + {BlockDeviceResult, DeviceResult}, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +pub struct DomainConfig { |
| 20 | + platform: Option<PlatformDomainConfig>, |
| 21 | + name: Option<String>, |
| 22 | + backend_domid: u32, |
| 23 | + channels: Vec<ChannelDeviceConfig>, |
| 24 | + vifs: Vec<VifDeviceConfig>, |
| 25 | + vbds: Vec<VbdDeviceConfig>, |
| 26 | + fs9ps: Vec<Fs9pDeviceConfig>, |
| 27 | + pci: Option<PciRootDeviceConfig>, |
| 28 | + extra_keys: HashMap<String, String>, |
| 29 | + extra_rw_paths: Vec<String>, |
| 30 | + start: bool, |
| 31 | +} |
| 32 | + |
| 33 | +impl Default for DomainConfig { |
| 34 | + fn default() -> Self { |
| 35 | + Self::new() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl DomainConfig { |
| 40 | + pub fn new() -> Self { |
| 41 | + Self { |
| 42 | + platform: None, |
| 43 | + name: None, |
| 44 | + backend_domid: 0, |
| 45 | + channels: Vec::new(), |
| 46 | + vifs: Vec::new(), |
| 47 | + vbds: Vec::new(), |
| 48 | + fs9ps: Vec::new(), |
| 49 | + pci: None, |
| 50 | + extra_keys: HashMap::new(), |
| 51 | + extra_rw_paths: Vec::new(), |
| 52 | + start: true, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn platform(&mut self, platform: PlatformDomainConfig) -> &mut Self { |
| 57 | + self.platform = Some(platform); |
| 58 | + self |
| 59 | + } |
| 60 | + |
| 61 | + pub fn get_platform(&self) -> &Option<PlatformDomainConfig> { |
| 62 | + &self.platform |
| 63 | + } |
| 64 | + |
| 65 | + pub fn name(&mut self, name: impl AsRef<str>) -> &mut Self { |
| 66 | + self.name = Some(name.as_ref().to_string()); |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + pub fn get_name(&self) -> &Option<String> { |
| 71 | + &self.name |
| 72 | + } |
| 73 | + |
| 74 | + pub fn backend_domid(&mut self, backend_domid: u32) -> &mut Self { |
| 75 | + self.backend_domid = backend_domid; |
| 76 | + self |
| 77 | + } |
| 78 | + |
| 79 | + pub fn get_backend_domid(&self) -> u32 { |
| 80 | + self.backend_domid |
| 81 | + } |
| 82 | + |
| 83 | + pub fn add_channel(&mut self, channel: ChannelDeviceConfig) -> &mut Self { |
| 84 | + self.channels.push(channel); |
| 85 | + self |
| 86 | + } |
| 87 | + |
| 88 | + pub fn get_channels(&self) -> &Vec<ChannelDeviceConfig> { |
| 89 | + &self.channels |
| 90 | + } |
| 91 | + |
| 92 | + pub fn add_vif(&mut self, vif: VifDeviceConfig) -> &mut Self { |
| 93 | + self.vifs.push(vif); |
| 94 | + self |
| 95 | + } |
| 96 | + |
| 97 | + pub fn get_vifs(&self) -> &Vec<VifDeviceConfig> { |
| 98 | + &self.vifs |
| 99 | + } |
| 100 | + |
| 101 | + pub fn add_vbd(&mut self, vbd: VbdDeviceConfig) -> &mut Self { |
| 102 | + self.vbds.push(vbd); |
| 103 | + self |
| 104 | + } |
| 105 | + |
| 106 | + pub fn get_vbds(&self) -> &Vec<VbdDeviceConfig> { |
| 107 | + &self.vbds |
| 108 | + } |
| 109 | + |
| 110 | + pub fn add_fs9p(&mut self, fs9p: Fs9pDeviceConfig) -> &mut Self { |
| 111 | + self.fs9ps.push(fs9p); |
| 112 | + self |
| 113 | + } |
| 114 | + |
| 115 | + pub fn get_fs9ps(&self) -> &Vec<Fs9pDeviceConfig> { |
| 116 | + &self.fs9ps |
| 117 | + } |
| 118 | + |
| 119 | + pub fn pci(&mut self, pci: PciRootDeviceConfig) -> &mut Self { |
| 120 | + self.pci = Some(pci); |
| 121 | + self |
| 122 | + } |
| 123 | + |
| 124 | + pub fn get_pci(&self) -> &Option<PciRootDeviceConfig> { |
| 125 | + &self.pci |
| 126 | + } |
| 127 | + |
| 128 | + pub fn add_extra_key(&mut self, key: impl AsRef<str>, value: impl ToString) -> &mut Self { |
| 129 | + self.extra_keys |
| 130 | + .insert(key.as_ref().to_string(), value.to_string()); |
| 131 | + self |
| 132 | + } |
| 133 | + |
| 134 | + pub fn get_extra_keys(&self) -> &HashMap<String, String> { |
| 135 | + &self.extra_keys |
| 136 | + } |
| 137 | + |
| 138 | + pub fn add_rw_path(&mut self, path: impl AsRef<str>) -> &mut Self { |
| 139 | + self.extra_rw_paths.push(path.as_ref().to_string()); |
| 140 | + self |
| 141 | + } |
| 142 | + |
| 143 | + pub fn get_rw_paths(&self) -> &Vec<String> { |
| 144 | + &self.extra_rw_paths |
| 145 | + } |
| 146 | + |
| 147 | + pub fn start(&mut self, start: bool) -> &mut Self { |
| 148 | + self.start = start; |
| 149 | + self |
| 150 | + } |
| 151 | + |
| 152 | + pub fn get_start(&self) -> bool { |
| 153 | + self.start |
| 154 | + } |
| 155 | + |
| 156 | + pub fn done(self) -> Self { |
| 157 | + self |
| 158 | + } |
| 159 | + |
| 160 | + pub(crate) async fn prepare( |
| 161 | + &mut self, |
| 162 | + domid: u32, |
| 163 | + call: &XenCall, |
| 164 | + platform: &PlatformDomainInfo, |
| 165 | + ) -> Result<()> { |
| 166 | + if let Some(pci) = self.pci.as_mut() { |
| 167 | + pci.prepare(domid, call).await?; |
| 168 | + } |
| 169 | + |
| 170 | + for channel in &mut self.channels { |
| 171 | + channel.prepare(platform).await?; |
| 172 | + } |
| 173 | + |
| 174 | + Ok(()) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +pub struct DomainResult { |
| 179 | + pub platform: PlatformDomainInfo, |
| 180 | + pub channels: Vec<DeviceResult>, |
| 181 | + pub vifs: Vec<DeviceResult>, |
| 182 | + pub vbds: Vec<BlockDeviceResult>, |
| 183 | + pub fs9ps: Vec<DeviceResult>, |
| 184 | + pub pci: Option<DeviceResult>, |
| 185 | +} |
0 commit comments