diff options
author | Manuel Bentele | 2021-08-10 15:55:55 +0200 |
---|---|---|
committer | Manuel Bentele | 2021-08-10 15:55:55 +0200 |
commit | e1ed70e29d73a0c1b847af389f41366dcfc6a6f5 (patch) | |
tree | 971f72eaab7b1b4f858843e278db463ca09edb60 | |
parent | [qemu] Do not remove boot order and network interfaces for VM edit (diff) | |
download | master-sync-shared-e1ed70e29d73a0c1b847af389f41366dcfc6a6f5.tar.gz master-sync-shared-e1ed70e29d73a0c1b847af389f41366dcfc6a6f5.tar.xz master-sync-shared-e1ed70e29d73a0c1b847af389f41366dcfc6a6f5.zip |
Add Libvirt domain XML representation for mediated devices (mdev)
4 files changed, 293 insertions, 3 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index 2925ef1..169caff 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -25,6 +25,7 @@ import org.openslx.libvirt.domain.device.Graphics; import org.openslx.libvirt.domain.device.GraphicsSpice; import org.openslx.libvirt.domain.device.GraphicsVnc; import org.openslx.libvirt.domain.device.Hostdev; +import org.openslx.libvirt.domain.device.HostdevMdev; import org.openslx.libvirt.domain.device.HostdevPci; import org.openslx.libvirt.domain.device.HostdevUsb; import org.openslx.libvirt.domain.device.Interface; @@ -922,6 +923,17 @@ public class Domain extends LibvirtXmlDocument } /** + * Returns list of virtual machine mediated hostdev devices specified in the Libvirt domain XML + * document. + * + * @return list of virtual machine mediated hostdev devices. + */ + public ArrayList<HostdevMdev> getHostdevMdevDevices() + { + return Domain.filterDevices( HostdevMdev.class, this.getDevices() ); + } + + /** * Returns list of virtual machine PCI hostdev devices specified in the Libvirt domain XML * document. * @@ -1171,6 +1183,16 @@ public class Domain extends LibvirtXmlDocument } /** + * Adds a virtual machine mediated hostdev device to the Libvirt domain XML document. + * + * @return reference to the added mediated hostdev device if creation was successful. + */ + public HostdevMdev addHostdevMdevDevice() + { + return HostdevMdev.class.cast( this.addDevice( new HostdevMdev() ) ); + } + + /** * Adds a virtual machine PCI hostdev device to the Libvirt domain XML document. * * @return reference to the added PCI hostdev device if creation was successful. diff --git a/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java index 4c6775e..11e74c3 100644 --- a/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java +++ b/src/main/java/org/openslx/libvirt/domain/device/Hostdev.java @@ -76,7 +76,10 @@ public class Hostdev extends Device xmlNode.setXmlElementAttributeValue( "mode", "subsystem" ); - if ( hostdev instanceof HostdevPci ) { + if ( hostdev instanceof HostdevMdev ) { + xmlNode.setXmlElementAttributeValue( "type", Type.MDEV.toString() ); + addedHostdev = HostdevPci.createInstance( xmlNode ); + } else if ( hostdev instanceof HostdevPci ) { xmlNode.setXmlElementAttributeValue( "type", Type.PCI.toString() ); addedHostdev = HostdevPci.createInstance( xmlNode ); } else if ( hostdev instanceof HostdevUsb ) { @@ -103,6 +106,9 @@ public class Hostdev extends Device } switch ( type ) { + case MDEV: + deviceHostdev = HostdevMdev.newInstance( xmlNode ); + break; case PCI: deviceHostdev = HostdevPci.newInstance( xmlNode ); break; @@ -123,8 +129,9 @@ public class Hostdev extends Device enum Type { // @formatter:off - PCI( "pci" ), - USB( "usb" ); + MDEV( "mdev" ), + PCI ( "pci" ), + USB ( "usb" ); // @formatter:on /** diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java new file mode 100644 index 0000000..d25a6eb --- /dev/null +++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdev.java @@ -0,0 +1,162 @@ +package org.openslx.libvirt.domain.device; + +import org.openslx.libvirt.xml.LibvirtXmlNode; + +/** + * A hostdev mediated device node in a Libvirt domain XML document for mediated device passthrough. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class HostdevMdev extends Hostdev implements HostdevAddressableSource<HostdevMdevDeviceAddress> +{ + /** + * Creates an empty hostdev mediated device. + */ + public HostdevMdev() + { + super(); + } + + /** + * Creates a hostdev mediated device representing an existing Libvirt XML hostdev mediated device + * element. + * + * @param xmlNode existing Libvirt XML hostdev mediated device element. + */ + public HostdevMdev( LibvirtXmlNode xmlNode ) + { + super( xmlNode ); + } + + /** + * Checks whether the hostdev mediated device display is on or off. + * + * @return state whether the hostdev mediated device display is on or off. + */ + public boolean isDisplayOn() + { + return this.getXmlElementAttributeValueAsBool( "display" ); + } + + /** + * Sets the state of the hostdev mediated device display. + * + * @param on state whether the hostdev mediated device display is on or off. + */ + public void setDisplayOn( boolean on ) + { + this.setXmlElementAttributeValueOnOff( "display", on ); + } + + /** + * Returns the hostdev mediated device model. + * + * @return hostdev mediated device model. + */ + public Model getModel() + { + final String model = this.getXmlElementAttributeValue( "model" ); + return Model.fromString( model ); + } + + /** + * Sets the hostdev mediated device model. + * + * @param model hostdev mediated device model that is set. + */ + public void setModel( Model model ) + { + this.setXmlElementAttributeValue( "model", model.toString() ); + } + + @Override + public HostdevMdevDeviceAddress getSource() + { + final String mdevDeviceAddress = this.getXmlElementAttributeValue( "source/address", "uuid" ); + return HostdevMdevDeviceAddress.valueOf( mdevDeviceAddress ); + } + + @Override + public void setSource( HostdevMdevDeviceAddress source ) + { + this.setXmlElementAttributeValue( "source/address", "domain", source.getDeviceAddressAsString() ); + } + + /** + * Creates a non-existent hostdev mediated device as Libvirt XML device element. + * + * @param xmlNode Libvirt XML node of the Libvirt XML device that is created. + * @return created hostdev mediated device instance. + */ + public static HostdevPci createInstance( LibvirtXmlNode xmlNode ) + { + return HostdevPci.newInstance( xmlNode ); + } + + /** + * Creates a hostdev mediated device representing an existing Libvirt XML hostdev mediated device + * element. + * + * @param xmlNode existing Libvirt XML hostdev mediated device element. + * @return hostdev mediated device instance. + */ + public static HostdevPci newInstance( LibvirtXmlNode xmlNode ) + { + return new HostdevPci( xmlNode ); + } + + /** + * Model for hostdev mediated device passthrough. + * + * @author Manuel Bentele + * @version 1.0 + */ + enum Model + { + // @formatter:off + VFIO_PCI( "vfio-pci" ), + VFIO_CCW( "vfio-ccw" ), + VFIO_AP ( "vfio-ap" ); + // @formatter:on + + /** + * Name of the hostdev mediated device model. + */ + private String model = null; + + /** + * Creates hostdev mediated device model. + * + * @param type valid name of the hostdev mediated device model in a Libvirt domain XML + * document. + */ + Model( String model ) + { + this.model = model; + } + + @Override + public String toString() + { + return this.model; + } + + /** + * Creates hostdev mediated device model from its name with error check. + * + * @param model name of the hostdev mediated device model in a Libvirt domain XML document. + * @return valid hostdev mediated device model. + */ + public static Model fromString( String model ) + { + for ( Model m : Model.values() ) { + if ( m.model.equalsIgnoreCase( model ) ) { + return m; + } + } + + return null; + } + } +} diff --git a/src/main/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddress.java b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddress.java new file mode 100644 index 0000000..337791d --- /dev/null +++ b/src/main/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddress.java @@ -0,0 +1,99 @@ +package org.openslx.libvirt.domain.device; + +import java.util.UUID; + +/** + * Representation of an address from a mediated device. + * + * @author Manuel Bentele + * @version 1.0 + */ +public class HostdevMdevDeviceAddress +{ + /** + * Address of the hostdev mediated device. + */ + private final UUID deviceAddress; + + /** + * Creates a new mediated device address and sets the address information to the default address + * {@code 00000000-0000-0000-0000-000000000000}. + */ + public HostdevMdevDeviceAddress() + { + this( new UUID( 0, 0 ) ); + } + + /** + * Creates a new mediated device address consisting of a specified mediated device address. + * + * @param mdevDeviceAddress mediated device address. + */ + public HostdevMdevDeviceAddress( UUID mdevDeviceAddress ) + { + this.deviceAddress = mdevDeviceAddress; + } + + /** + * Returns the address of the mediated device. + * + * @return address of the mediated device. + */ + public UUID getDeviceAddress() + { + return this.deviceAddress; + } + + /** + * Returns the address of the mediated device as {@code String}. + * + * @return address of the mediated device as {@code String}. + */ + public String getDeviceAddressAsString() + { + return this.getDeviceAddress().toString(); + } + + /** + * Creates a new mediated device address parsed from a {@link String}. + * + * @param mdevDeviceAddress textual information containing a mediated device address as + * {@link String}. The textual mediated device address should be well-formed according + * to the string representation as described in {@link UUID#toString}. + * + * @return mediated device address instance. + */ + public static HostdevMdevDeviceAddress valueOf( String mdevDeviceAddress ) + { + HostdevMdevDeviceAddress parsedMdevDeviceAddress = null; + + try { + final UUID deviceAddress = UUID.fromString( mdevDeviceAddress ); + parsedMdevDeviceAddress = new HostdevMdevDeviceAddress( deviceAddress ); + } catch ( IllegalArgumentException e ) { + parsedMdevDeviceAddress = null; + } + + return parsedMdevDeviceAddress; + } + + @Override + public boolean equals( Object obj ) + { + if ( obj == null ) { + return false; + } else if ( this.getClass() != obj.getClass() ) { + return false; + } else { + // check if MDEV device addresses are equal + final HostdevMdevDeviceAddress other = HostdevMdevDeviceAddress.class.cast( obj ); + return other.getDeviceAddress().equals( this.getDeviceAddress() ); + } + } + + @Override + public String toString() + { + return this.getDeviceAddressAsString(); + } +} |