From 2e275763162623b59b1872a16c0c9f0f3837f530 Mon Sep 17 00:00:00 2001
From: zhangxinhao <zhangxinhao1@huawei.com>
Date: Tue, 9 May 2023 23:01:36 +0800
Subject: cpu_topo: support for specifying "clusters" in qemu
 comand

Support for cpu topology "clusters" for qemu command parameter "-smp".

Signed-off-by: zhangxinhao <zhangxinhao1@huawei.com>
---
 src/qemu/qemu_capabilities.c             |  2 ++
 src/qemu/qemu_capabilities.h             |  1 +
 src/qemu/qemu_command.c                  |  7 ++---
 tests/qemuxml2argvdata/smp-clusters.args | 30 +++++++++++++++++++++
 tests/qemuxml2argvdata/smp-clusters.xml  | 33 ++++++++++++++++++++++++
 tests/qemuxml2argvtest.c                 |  1 +
 6 files changed, 69 insertions(+), 5 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/smp-clusters.args
 create mode 100644 tests/qemuxml2argvdata/smp-clusters.xml

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 21b477cd4d..19030b2040 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -579,6 +579,7 @@ VIR_ENUM_IMPL(virQEMUCaps,
               /* 365 */
               "calc-dirty-rate",
               "dirtyrate-param.mode",
+              "smp-clusters",
     );
 
 
@@ -3218,6 +3219,7 @@ static struct virQEMUCapsCommandLineProps virQEMUCapsCommandLine[] = {
     { "chardev", "fd", QEMU_CAPS_CHARDEV_FD_PASS },
     { "overcommit", NULL, QEMU_CAPS_OVERCOMMIT },
     { "smp-opts", "dies", QEMU_CAPS_SMP_DIES },
+    { "smp-opts", "clusters", QEMU_CAPS_SMP_CLUSTERS },
 };
 
 static int
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 00682eb52c..43507760c8 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -560,6 +560,7 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */
     /* 365 */
     QEMU_CAPS_CALC_DIRTY_RATE, /* accepts calc-dirty-rate */
     QEMU_CAPS_DIRTYRATE_MODE , /* calc-dirty-rate accepts mode parameter */
+    QEMU_CAPS_SMP_CLUSTERS, /*  -smp clusters= */
 
     QEMU_CAPS_LAST /* this must always be the last item */
 } virQEMUCapsFlags;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 85f95b41c3..dc7fb871e7 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -7335,14 +7335,11 @@ qemuBuildSmpCommandLine(virCommandPtr cmd,
                            _("Only 1 die per socket is supported"));
             return -1;
         }
-        if (def->cpu->clusters != 1) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("Only 1 cluster per dies is supported"));
-            return -1;
-        }
         virBufferAsprintf(&buf, ",sockets=%u", def->cpu->sockets);
         if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SMP_DIES))
             virBufferAsprintf(&buf, ",dies=%u", def->cpu->dies);
+        if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SMP_CLUSTERS))
+            virBufferAsprintf(&buf, ",clusters=%u", def->cpu->clusters);
         virBufferAsprintf(&buf, ",cores=%u", def->cpu->cores);
         virBufferAsprintf(&buf, ",threads=%u", def->cpu->threads);
     } else {
diff --git a/tests/qemuxml2argvdata/smp-clusters.args b/tests/qemuxml2argvdata/smp-clusters.args
new file mode 100644
index 0000000000..ab91ad7218
--- /dev/null
+++ b/tests/qemuxml2argvdata/smp-clusters.args
@@ -0,0 +1,30 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-QEMUGuest1 \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-QEMUGuest1/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-QEMUGuest1/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-QEMUGuest1/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-i386 \
+-name QEMUGuest1 \
+-S \
+-machine pc,usb=off,dump-guest-core=off \
+-accel tcg \
+-m 214 \
+-realtime mlock=off \
+-smp 1,maxcpus=4,sockets=2,clusters=2,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,path=/tmp/lib/domain--1-QEMUGuest1/monitor.sock,\
+server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-usb \
+-drive file=/dev/HostVG/QEMUGuest1,format=raw,if=none,id=drive-ide0-0-0 \
+-device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1
diff --git a/tests/qemuxml2argvdata/smp-clusters.xml b/tests/qemuxml2argvdata/smp-clusters.xml
new file mode 100644
index 0000000000..c1845b8213
--- /dev/null
+++ b/tests/qemuxml2argvdata/smp-clusters.xml
@@ -0,0 +1,33 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static' current='1'>4</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu>
+    <topology sockets='2' clusters='2' cores='1' threads='1'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu-system-i386</emulator>
+    <disk type='block' device='disk'>
+      <driver name='qemu' type='raw'/>
+      <source dev='/dev/HostVG/QEMUGuest1'/>
+      <target dev='hda' bus='ide'/>
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
+    </disk>
+    <controller type='usb' index='0'/>
+    <controller type='ide' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <memballoon model='none'/>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index ec90532b5f..df62dfacb6 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -1828,6 +1828,7 @@ mymain(void)
 
     DO_TEST("smp", NONE);
     DO_TEST("smp-dies", QEMU_CAPS_SMP_DIES);
+    DO_TEST("smp-clusters", QEMU_CAPS_SMP_CLUSTERS);
 
     DO_TEST("iothreads", QEMU_CAPS_OBJECT_IOTHREAD);
     DO_TEST("iothreads-ids", QEMU_CAPS_OBJECT_IOTHREAD);
-- 
2.25.1

