Skip to content

Commit 78a5114

Browse files
committed
Address comments
1 parent 7d7b664 commit 78a5114

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtGpuDef.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,42 @@ private void generatePciXml(StringBuilder gpuBuilder) {
8282
// Parse the bus address into domain, bus, slot, function. Two input formats are accepted:
8383
// - "dddd:bb:ss.f" full PCI address with domain (e.g. 0000:00:02.0)
8484
// - "bb:ss.f" legacy short BDF; domain defaults to 0000
85-
String domain = "0x0000";
86-
String bus = "0x00";
87-
String slot = "0x00";
88-
String function = "0x0";
85+
// Each segment is parsed as a hex integer and formatted with fixed widths
86+
// (domain: 4 hex digits, bus/slot: 2 hex digits, function: 1 hex digit) to
87+
// produce canonical libvirt XML values regardless of input casing or padding.
88+
int domainVal = 0, busVal = 0, slotVal = 0, funcVal = 0;
8989

9090
if (busAddress != null && !busAddress.isEmpty()) {
9191
String[] parts = busAddress.split(":");
9292
String slotFunction = null;
93-
if (parts.length == 3) {
94-
domain = "0x" + parts[0];
95-
bus = "0x" + parts[1];
96-
slotFunction = parts[2];
97-
} else if (parts.length == 2) {
98-
bus = "0x" + parts[0];
99-
slotFunction = parts[1];
100-
}
101-
if (slotFunction != null) {
102-
String[] slotFunctionParts = slotFunction.split("\\.");
103-
if (slotFunctionParts.length > 0) {
104-
slot = "0x" + slotFunctionParts[0];
105-
if (slotFunctionParts.length > 1) {
106-
function = "0x" + slotFunctionParts[1].trim();
93+
try {
94+
if (parts.length == 3) {
95+
domainVal = Integer.parseInt(parts[0], 16);
96+
busVal = Integer.parseInt(parts[1], 16);
97+
slotFunction = parts[2];
98+
} else if (parts.length == 2) {
99+
busVal = Integer.parseInt(parts[0], 16);
100+
slotFunction = parts[1];
101+
}
102+
if (slotFunction != null) {
103+
String[] slotFunctionParts = slotFunction.split("\\.");
104+
if (slotFunctionParts.length > 0) {
105+
slotVal = Integer.parseInt(slotFunctionParts[0], 16);
106+
if (slotFunctionParts.length > 1) {
107+
funcVal = Integer.parseInt(slotFunctionParts[1].trim(), 16);
108+
}
107109
}
108110
}
111+
} catch (NumberFormatException e) {
112+
// leave all values at 0 (safe defaults)
109113
}
110114
}
111115

116+
String domain = String.format("0x%04x", domainVal);
117+
String bus = String.format("0x%02x", busVal);
118+
String slot = String.format("0x%02x", slotVal);
119+
String function = String.format("0x%x", funcVal);
120+
112121
gpuBuilder.append(" <address domain='").append(domain).append("' bus='").append(bus).append("' slot='")
113122
.append(slot).append("' function='").append(function.trim()).append("'/>\n");
114123
gpuBuilder.append(" </source>\n");

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtGpuDefTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ public void testGpuDef_withFullPciAddressNonZeroDomain() {
156156
}
157157

158158
@Test
159-
public void testGpuDef_withFullPciAddressWideDomain() {
159+
public void testGpuDef_withNvidiaStyleEightDigitDomain() {
160+
// nvidia-smi reports PCI addresses with an 8-digit domain (e.g. "00000001:af:00.1").
161+
// generatePciXml must normalize it to the canonical 4-digit form "0x0001".
160162
LibvirtGpuDef gpuDef = new LibvirtGpuDef();
161163
VgpuTypesInfo pciGpuInfo = new VgpuTypesInfo(
162164
GpuDevice.DeviceType.PCI,
163165
"passthrough",
164166
"passthrough",
165-
"10000:af:00.1",
167+
"00000001:af:00.1",
166168
"10de",
167169
"NVIDIA Corporation",
168170
"1b38",
@@ -172,7 +174,7 @@ public void testGpuDef_withFullPciAddressWideDomain() {
172174

173175
String gpuXml = gpuDef.toString();
174176

175-
assertTrue(gpuXml.contains("<address domain='0x10000' bus='0xaf' slot='0x00' function='0x1'/>"));
177+
assertTrue(gpuXml.contains("<address domain='0x0001' bus='0xaf' slot='0x00' function='0x1'/>"));
176178
}
177179

178180
@Test

0 commit comments

Comments
 (0)