From 41126d5da4a87320407885cf2a5eedc1c828dfaa Mon Sep 17 00:00:00 2001
From: Simon Rettberg
Date: Mon, 26 Jun 2023 12:19:05 +0200
Subject: [libvirt] Allow setting PCI address on Device level
Devices like video and disk can be PCI Devices and thus have an
tag that defines where on the PCI bus it sits. Move the
according helper functions for getting/setting this address from
HostDevPci up to Device.
---
.../org/openslx/libvirt/domain/device/Device.java | 59 +++++++++++++++++++++-
.../domain/device/HostdevAddressableTarget.java | 4 +-
.../openslx/libvirt/domain/device/HostdevPci.java | 59 +++-------------------
.../openslx/libvirt/domain/device/HostdevUsb.java | 4 +-
4 files changed, 70 insertions(+), 56 deletions(-)
diff --git a/src/main/java/org/openslx/libvirt/domain/device/Device.java b/src/main/java/org/openslx/libvirt/domain/device/Device.java
index 1e0e031..31a54ca 100644
--- a/src/main/java/org/openslx/libvirt/domain/device/Device.java
+++ b/src/main/java/org/openslx/libvirt/domain/device/Device.java
@@ -11,7 +11,7 @@ import org.w3c.dom.Node;
* @author Manuel Bentele
* @version 1.0
*/
-public class Device extends LibvirtXmlNode
+public class Device extends LibvirtXmlNode implements HostdevAddressableTarget
{
/**
* Creates an empty virtual machine device.
@@ -170,6 +170,63 @@ public class Device extends LibvirtXmlNode
}
}
+ /**
+ * Sets the PCI device address for an XML address element selected by a XPath expression.
+ *
+ * @param expression XPath expression to select the XML address element.
+ * @param address PCI device address for the selected XML address element.
+ */
+ protected void setPciAddress( final String expression, final HostdevPciDeviceAddress address )
+ {
+ final String pciDomain = HostdevUtils.appendHexPrefix( address.getPciDomainAsString() );
+ final String pciBus = HostdevUtils.appendHexPrefix( address.getPciBusAsString() );
+ final String pciDevice = HostdevUtils.appendHexPrefix( address.getPciDeviceAsString() );
+ final String pciFunction = HostdevUtils.appendHexPrefix( address.getPciFunctionAsString() );
+
+ this.setXmlElementAttributeValue( expression, "domain", pciDomain );
+ this.setXmlElementAttributeValue( expression, "bus", pciBus );
+ this.setXmlElementAttributeValue( expression, "slot", pciDevice );
+ this.setXmlElementAttributeValue( expression, "function", pciFunction );
+ }
+
+ /**
+ * Returns the PCI device address from an address XML element selected by a XPath expression.
+ *
+ * @param expression XPath expression to select the XML address element.
+ * @return PCI device address from the selected XML address element.
+ */
+ protected HostdevPciDeviceAddress getPciAddress( final String expression )
+ {
+ String pciDomain = this.getXmlElementAttributeValue( expression, "domain" );
+ String pciBus = this.getXmlElementAttributeValue( expression, "bus" );
+ String pciDevice = this.getXmlElementAttributeValue( expression, "slot" );
+ String pciFunction = this.getXmlElementAttributeValue( expression, "function" );
+
+ pciDomain = HostdevUtils.removeHexPrefix( pciDomain );
+ pciBus = HostdevUtils.removeHexPrefix( pciBus );
+ pciDevice = HostdevUtils.removeHexPrefix( pciDevice );
+ pciFunction = HostdevUtils.removeHexPrefix( pciFunction );
+
+ return HostdevPciDeviceAddress.valueOf( pciDomain + ":" + pciBus + ":" + pciDevice + "." + pciFunction );
+ }
+
+ /**
+ * Returns this devices PCI bus address, or null if it doesn't have an explicit one
+ * set, or if the address type isn't PCI.
+ */
+ public HostdevPciDeviceAddress getPciTarget()
+ {
+ if ( !"pci".equals( this.getXmlElementAttributeValue( "address", "type" ) ) )
+ return null;
+ return this.getPciAddress( "address" );
+ }
+
+ public void setPciTarget( HostdevPciDeviceAddress address )
+ {
+ this.setPciAddress( "address", address );
+ this.setXmlElementAttributeValue( "address", "type", "pci" );
+ }
+
/**
* Type of virtual machine devices.
*
diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevAddressableTarget.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevAddressableTarget.java
index 8a6f9a1..ca06065 100644
--- a/src/main/java/org/openslx/libvirt/domain/device/HostdevAddressableTarget.java
+++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevAddressableTarget.java
@@ -15,12 +15,12 @@ public abstract interface HostdevAddressableTarget
*
* @return target of the target device (in the virtual machine).
*/
- public T getTarget();
+ public T getPciTarget();
/**
* Sets the target for the target device (in the virtual machine).
*
* @param target target for the target device (in the virtual machine).
*/
- public void setTarget( T target );
+ public void setPciTarget( T target );
}
diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java
index d2be2d6..1351f26 100644
--- a/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java
+++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevPci.java
@@ -8,8 +8,7 @@ import org.openslx.libvirt.xml.LibvirtXmlNode;
* @author Manuel Bentele
* @version 1.0
*/
-public class HostdevPci extends Hostdev implements HostdevAddressableSource,
- HostdevAddressableTarget
+public class HostdevPci extends Hostdev implements HostdevAddressableSource
{
/**
* Creates an empty hostdev PCI device.
@@ -29,46 +28,6 @@ public class HostdevPci extends Hostdev implements HostdevAddressableSource