Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Answer execute(final PlugNicCommand command, final LibvirtComputingResour
* controllers, balloon), in which case the next free slot above them is used.
*
* @return the slot to assign, or {@code null} to let libvirt auto-assign (domain XML unavailable
* or unparseable, or no free slot left).
* or unparseable, PCI Express root complex (q35), or no free slot left).
*/
protected Integer findNextAvailablePciSlot(final Domain vm, final List<InterfaceDef> pluggedNics) {
try {
Expand All @@ -139,6 +139,12 @@ protected Integer findNextAvailablePciSlot(final Domain vm, final List<Interface
logger.debug("Domain XML unavailable, letting libvirt auto-assign PCI slot");
return null;
}
if (hasPcieRootComplex(domXml)) {
// q35: bus 0 is the PCIe root complex, which takes no hot-plugged device. Libvirt puts the NIC on the lowest
// free hot-pluggable root port, which is above the ports used at start.
logger.debug("Domain uses a PCI Express root complex, letting libvirt choose a free root port for the NIC");
return null;
}
final Set<Integer> usedSlots = getUsedPciSlots(domXml);
if (usedSlots == null) {
return null;
Expand All @@ -154,6 +160,28 @@ protected Integer findNextAvailablePciSlot(final Domain vm, final List<Interface
}
}

/**
* True when the domain XML has a {@code pcie-root} controller (q35). Its bus 0 does not support hot-plug, so pinning a
* hot-plugged NIC to a bus 0 slot would make libvirt refuse the attach. Returns {@code false} if the XML cannot be parsed,
* which keeps the slot selection below unchanged.
*/
protected boolean hasPcieRootComplex(final String domXml) {
try {
final DocumentBuilder builder = ParserUtils.getSaferDocumentBuilderFactory().newDocumentBuilder();
final Document doc = builder.parse(new InputSource(new StringReader(domXml)));
final NodeList controllers = doc.getElementsByTagName("controller");
for (int i = 0; i < controllers.getLength(); i++) {
final Element controller = (Element) controllers.item(i);
if ("pci".equals(controller.getAttribute("type")) && "pcie-root".equals(controller.getAttribute("model"))) {
return true;
}
}
} catch (final ParserConfigurationException | SAXException | IOException e) {
logger.warn("Failed to parse domain XML to find the PCI root controller", e);
}
return false;
}

/**
* Collects the slot numbers of every {@code <address type='pci' .../>} element in the domain XML,
* whichever device or bus they belong to. Returns {@code null} if the XML cannot be parsed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package com.cloud.hypervisor.kvm.resource.wrapper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import java.util.Arrays;
Expand Down Expand Up @@ -81,6 +83,30 @@ public class LibvirtPlugNicCommandWrapperTest {
+ " </devices>\n"
+ "</domain>\n";

/**
* q35 layout as libvirt reports it: NICs sit in slot 0 behind pcie-root-port controllers on bus 0.
*/
private static final String Q35_DOMAIN_XML =
"<domain type='kvm'>\n"
+ " <name>i-2-43-VM</name>\n"
+ " <devices>\n"
+ " <controller type='pci' index='0' model='pcie-root'/>\n"
+ " <controller type='pci' index='1' model='pcie-root-port'>\n"
+ " <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0' multifunction='on'/>\n"
+ " </controller>\n"
+ " <controller type='pci' index='2' model='pcie-root-port'>\n"
+ " <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x1'/>\n"
+ " </controller>\n"
+ " <interface type='bridge'>\n"
+ " <mac address='02:00:7c:98:00:03'/>\n"
+ " <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>\n"
+ " </interface>\n"
+ " <video>\n"
+ " <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>\n"
+ " </video>\n"
+ " </devices>\n"
+ "</domain>\n";

@Mock
private Domain domain;

Expand Down Expand Up @@ -139,6 +165,20 @@ public void findNextAvailablePciSlotFallsBackWhenLibvirtFails() throws LibvirtEx
assertNull(wrapper.findNextAvailablePciSlot(domain, nicsFromXml()));
}

@Test
public void hasPcieRootComplexDetectsQ35Domains() {
assertTrue(wrapper.hasPcieRootComplex(Q35_DOMAIN_XML));
assertFalse(wrapper.hasPcieRootComplex(DOMAIN_XML));
assertFalse(wrapper.hasPcieRootComplex("<domain><devices><interface>"));
}

@Test
public void findNextAvailablePciSlotLetsLibvirtChooseOnQ35() throws LibvirtException {
when(domain.getXMLDesc(0)).thenReturn(Q35_DOMAIN_XML);
// pinning bus 0 would put the NIC on the root complex, which libvirt refuses for hot-plug
assertNull(wrapper.findNextAvailablePciSlot(domain, Collections.singletonList(nicAtSlot(0x00))));
}

@Test
public void findNextAvailablePciSlotFallsBackWhenBusIsFull() throws LibvirtException {
when(domain.getXMLDesc(0)).thenReturn(DOMAIN_XML);
Expand Down
Loading