2008-05-22 20:18:40 +04:00
# include <config.h>
2008-06-26 13:37:51 +04:00
# ifdef WITH_QEMU
2008-05-16 20:51:30 +04:00
2010-03-09 21:22:22 +03:00
# include "testutilsqemu.h"
2017-07-21 15:24:51 +03:00
# include "testutilshostcpus.h"
2010-03-09 21:22:22 +03:00
# include "testutils.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2010-04-16 10:01:59 +04:00
# include "cpu_conf.h"
2010-04-21 01:22:49 +04:00
# include "qemu / qemu_driver.h"
2010-12-17 19:41:51 +03:00
# include "qemu / qemu_domain.h"
2018-12-13 17:53:50 +03:00
# define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW
2015-09-09 17:03:14 +03:00
# include "qemu / qemu_capspriv.h"
2013-05-03 16:52:21 +04:00
# include "virstring.h"
2017-07-19 18:01:56 +03:00
# include "virfilecache.h"
2020-02-17 00:59:28 +03:00
# include "virutil.h"
2013-05-03 16:52:21 +04:00
# define VIR_FROM_THIS VIR_FROM_QEMU
2008-05-16 20:51:30 +04:00
2021-03-11 10:16:13 +03:00
virCPUDef * cpuDefault ;
virCPUDef * cpuHaswell ;
virCPUDef * cpuPower8 ;
virCPUDef * cpuPower9 ;
2015-03-23 19:19:28 +03:00
2016-12-20 12:10:05 +03:00
2019-12-02 15:19:44 +03:00
static const char * qemu_emulators [ VIR_ARCH_LAST ] = {
[ VIR_ARCH_I686 ] = " /usr/bin/qemu-system-i386 " ,
[ VIR_ARCH_X86_64 ] = " /usr/bin/qemu-system-x86_64 " ,
[ VIR_ARCH_AARCH64 ] = " /usr/bin/qemu-system-aarch64 " ,
[ VIR_ARCH_ARMV7L ] = " /usr/bin/qemu-system-arm " ,
[ VIR_ARCH_PPC64 ] = " /usr/bin/qemu-system-ppc64 " ,
[ VIR_ARCH_PPC ] = " /usr/bin/qemu-system-ppc " ,
[ VIR_ARCH_RISCV32 ] = " /usr/bin/qemu-system-riscv32 " ,
[ VIR_ARCH_RISCV64 ] = " /usr/bin/qemu-system-riscv64 " ,
2020-11-18 20:06:45 +03:00
[ VIR_ARCH_S390X ] = " /usr/bin/qemu-system-s390x " ,
[ VIR_ARCH_SPARC ] = " /usr/bin/qemu-system-sparc " ,
2019-12-02 15:19:44 +03:00
} ;
2009-09-10 14:19:12 +04:00
2019-12-02 15:19:44 +03:00
static const virArch arch_alias [ VIR_ARCH_LAST ] = {
[ VIR_ARCH_PPC64LE ] = VIR_ARCH_PPC64 ,
[ VIR_ARCH_ARMV6L ] = VIR_ARCH_ARMV7L ,
} ;
2009-09-10 14:19:12 +04:00
2019-12-02 15:19:44 +03:00
static const char * const i386_machines [ ] = {
" pc " , " isapc " , NULL
} ;
2020-01-22 19:46:57 +03:00
/**
2021-03-30 16:24:41 +03:00
* Oldest supported qemu - 2.11 supports machine types back to pc - 0.10 .
2020-01-22 19:46:57 +03:00
*/
2020-01-22 12:28:19 +03:00
static const char * const x86_64_machines [ ] = {
2020-01-22 19:46:57 +03:00
" pc " , " isapc " , " q35 " ,
2020-01-23 12:37:08 +03:00
" pc-1.0 " , " pc-1.2 " ,
2020-01-22 19:46:57 +03:00
" pc-i440fx-1.4 " , " pc-i440fx-2.1 " , " pc-i440fx-2.3 " , " pc-i440fx-2.5 " ,
" pc-i440fx-2.6 " , " pc-i440fx-2.9 " , " pc-i440fx-2.12 " ,
" pc-q35-2.3 " , " pc-q35-2.4 " , " pc-q35-2.5 " , " pc-q35-2.7 " , " pc-q35-2.10 " ,
NULL
2019-12-02 15:19:44 +03:00
} ;
static const char * const aarch64_machines [ ] = {
2020-01-22 19:46:57 +03:00
" virt " , " virt-2.6 " , " versatilepb " , NULL
2019-12-02 15:19:44 +03:00
} ;
static const char * const arm_machines [ ] = {
2020-01-22 19:46:57 +03:00
" vexpress-a9 " , " vexpress-a15 " , " versatilepb " , " virt " , NULL
2019-12-02 15:19:44 +03:00
} ;
static const char * const ppc64_machines [ ] = {
" pseries " , NULL
} ;
static const char * const ppc_machines [ ] = {
" g3beige " , " mac99 " , " prep " , " ppce500 " , NULL
} ;
static const char * const riscv32_machines [ ] = {
" spike_v1.10 " , " spike_v1.9.1 " , " sifive_e " , " virt " , " sifive_u " , NULL
} ;
static const char * const riscv64_machines [ ] = {
" spike_v1.10 " , " spike_v1.9.1 " , " sifive_e " , " virt " , " sifive_u " , NULL
} ;
static const char * const s390x_machines [ ] = {
2021-06-11 11:30:31 +03:00
" s390-ccw-virtio " , " s390-ccw " , NULL
2019-12-02 15:19:44 +03:00
} ;
2020-11-18 20:06:45 +03:00
static const char * const sparc_machines [ ] = {
" SS-5 " , " LX " , " SPARCClassic " , " SPARCbook " ,
" SS-10 " , " SS-20 " , " SS-4 " , " SS-600MP " ,
" Voyager " , " leon3_generic " , NULL
} ;
2009-09-10 14:19:12 +04:00
2019-12-02 15:19:44 +03:00
static const char * const * qemu_machines [ VIR_ARCH_LAST ] = {
[ VIR_ARCH_I686 ] = i386_machines ,
2020-01-22 12:28:19 +03:00
[ VIR_ARCH_X86_64 ] = x86_64_machines ,
2019-12-02 15:19:44 +03:00
[ VIR_ARCH_AARCH64 ] = aarch64_machines ,
[ VIR_ARCH_ARMV7L ] = arm_machines ,
[ VIR_ARCH_PPC64 ] = ppc64_machines ,
[ VIR_ARCH_PPC ] = ppc_machines ,
[ VIR_ARCH_RISCV32 ] = riscv32_machines ,
[ VIR_ARCH_RISCV64 ] = riscv64_machines ,
[ VIR_ARCH_S390X ] = s390x_machines ,
2020-11-18 20:06:45 +03:00
[ VIR_ARCH_SPARC ] = sparc_machines ,
2019-12-02 15:19:44 +03:00
} ;
2009-09-10 14:19:12 +04:00
2019-12-02 15:19:44 +03:00
static const char * const * kvm_machines [ VIR_ARCH_LAST ] = {
[ VIR_ARCH_I686 ] = i386_machines ,
2020-01-22 12:28:19 +03:00
[ VIR_ARCH_X86_64 ] = x86_64_machines ,
2019-12-02 15:19:44 +03:00
[ VIR_ARCH_AARCH64 ] = aarch64_machines ,
[ VIR_ARCH_ARMV7L ] = arm_machines ,
[ VIR_ARCH_PPC64 ] = ppc64_machines ,
[ VIR_ARCH_PPC ] = ppc_machines ,
[ VIR_ARCH_RISCV32 ] = riscv32_machines ,
[ VIR_ARCH_RISCV64 ] = riscv64_machines ,
[ VIR_ARCH_S390X ] = s390x_machines ,
} ;
Fix default console type setting
The default console type may vary based on the OS type. ie a Xen
paravirt guests wants a 'xen' console, while a fullvirt guests
wants a 'serial' console.
A plain integer default console type in the capabilities does
not suffice. Instead introduce a callback that is passed the
OS type.
* src/conf/capabilities.h: Use a callback for default console
type
* src/conf/domain_conf.c, src/conf/domain_conf.h: Use callback
for default console type. Add missing LXC/OpenVZ console types.
* src/esx/esx_driver.c, src/libxl/libxl_conf.c,
src/lxc/lxc_conf.c, src/openvz/openvz_conf.c,
src/phyp/phyp_driver.c, src/qemu/qemu_capabilities.c,
src/uml/uml_conf.c, src/vbox/vbox_tmpl.c,
src/vmware/vmware_conf.c, src/xen/xen_hypervisor.c,
src/xenapi/xenapi_driver.c: Set default console type callback
2011-10-20 17:56:20 +04:00
2020-05-25 20:13:43 +03:00
static const char * qemu_default_ram_id [ VIR_ARCH_LAST ] = {
[ VIR_ARCH_I686 ] = " pc.ram " ,
[ VIR_ARCH_X86_64 ] = " pc.ram " ,
[ VIR_ARCH_AARCH64 ] = " mach-virt.ram " ,
[ VIR_ARCH_ARMV7L ] = " vexpress.highmem " ,
[ VIR_ARCH_PPC64 ] = " ppc_spapr.ram " ,
[ VIR_ARCH_PPC ] = " ppc_spapr.ram " ,
2020-11-18 20:06:45 +03:00
[ VIR_ARCH_S390X ] = " s390.ram " ,
[ VIR_ARCH_SPARC ] = " sun4m.ram " ,
2020-05-25 20:13:43 +03:00
} ;
2019-11-26 19:53:53 +03:00
char *
virFindFileInPath ( const char * file )
{
if ( g_str_has_prefix ( file , " qemu-system " ) | |
g_str_equal ( file , " qemu-kvm " ) ) {
return g_strdup_printf ( " /usr/bin/%s " , file ) ;
}
/* Nothing in tests should be relying on real files
* in host OS , so we return NULL to try to force
* an error in such a case
*/
return NULL ;
}
2019-11-29 13:40:39 +03:00
2021-03-11 10:16:13 +03:00
virCapsHostNUMA *
2019-11-29 13:40:39 +03:00
virCapabilitiesHostNUMANewHost ( void )
{
/*
* Build a NUMA topology with cell_id ( NUMA node id
* being 3 ( 0 + 3 ) , 4 ( 1 + 3 ) , 5 and 6
*/
return virTestCapsBuildNUMATopology ( 3 ) ;
}
2016-12-20 01:52:33 +03:00
static int
2021-03-11 10:16:13 +03:00
testQemuAddGuest ( virCaps * caps ,
2019-12-02 15:19:44 +03:00
virArch arch )
2016-12-20 01:52:33 +03:00
{
2019-12-02 15:19:44 +03:00
size_t nmachines ;
2021-03-11 10:16:13 +03:00
virCapsGuestMachine * * machines = NULL ;
virCapsGuest * guest ;
2019-12-02 15:19:44 +03:00
virArch emu_arch = arch ;
2016-12-20 01:52:33 +03:00
2019-12-02 15:19:44 +03:00
if ( arch_alias [ arch ] ! = VIR_ARCH_NONE )
emu_arch = arch_alias [ arch ] ;
2016-12-20 01:52:33 +03:00
2019-12-02 15:19:44 +03:00
if ( qemu_emulators [ emu_arch ] = = NULL )
return 0 ;
2016-12-20 01:52:33 +03:00
2019-12-02 15:19:44 +03:00
nmachines = g_strv_length ( ( gchar * * ) qemu_machines [ emu_arch ] ) ;
machines = virCapabilitiesAllocMachines ( qemu_machines [ emu_arch ] ,
nmachines ) ;
if ( machines = = NULL )
2016-12-20 01:52:33 +03:00
goto error ;
if ( ! ( guest = virCapabilitiesAddGuest ( caps ,
VIR_DOMAIN_OSTYPE_HVM ,
2019-12-02 15:19:44 +03:00
arch ,
qemu_emulators [ emu_arch ] ,
2016-12-20 01:52:33 +03:00
NULL ,
nmachines ,
machines ) ) )
goto error ;
machines = NULL ;
2019-12-02 15:19:44 +03:00
nmachines = 0 ;
if ( arch = = VIR_ARCH_I686 | |
arch = = VIR_ARCH_X86_64 )
virCapabilitiesAddGuestFeature ( guest , VIR_CAPS_GUEST_FEATURE_TYPE_CPUSELECTION ) ;
2016-12-20 01:52:33 +03:00
if ( ! virCapabilitiesAddGuestDomain ( guest ,
VIR_DOMAIN_VIRT_QEMU ,
NULL ,
NULL ,
0 ,
NULL ) )
goto error ;
2020-11-18 20:06:45 +03:00
if ( kvm_machines [ emu_arch ] ! = NULL ) {
nmachines = g_strv_length ( ( char * * ) kvm_machines [ emu_arch ] ) ;
machines = virCapabilitiesAllocMachines ( kvm_machines [ emu_arch ] ,
nmachines ) ;
if ( machines = = NULL )
goto error ;
if ( ! virCapabilitiesAddGuestDomain ( guest ,
VIR_DOMAIN_VIRT_KVM ,
qemu_emulators [ emu_arch ] ,
NULL ,
nmachines ,
machines ) )
goto error ;
}
2016-12-20 01:52:33 +03:00
2018-08-22 12:15:27 +03:00
return 0 ;
error :
virCapabilitiesFreeMachines ( machines , nmachines ) ;
return - 1 ;
}
2013-03-05 19:17:24 +04:00
2021-03-11 10:16:13 +03:00
virCaps * testQemuCapsInit ( void )
2014-03-18 12:13:43 +04:00
{
2021-03-11 10:16:13 +03:00
virCaps * caps ;
2019-12-02 15:19:44 +03:00
size_t i ;
2008-05-16 20:51:30 +04:00
2015-03-23 19:19:28 +03:00
if ( ! ( caps = virCapabilitiesNew ( VIR_ARCH_X86_64 , false , false ) ) )
2008-05-16 20:51:30 +04:00
return NULL ;
2014-09-03 21:06:55 +04:00
/* Add dummy 'none' security_driver. This is equal to setting
* security_driver = " none " in qemu . conf . */
2020-09-23 02:04:17 +03:00
caps - > host . secModels = g_new0 ( virCapsHostSecModel , 1 ) ;
2014-09-03 21:06:55 +04:00
caps - > host . nsecModels = 1 ;
2019-10-20 14:49:46 +03:00
caps - > host . secModels [ 0 ] . model = g_strdup ( " none " ) ;
caps - > host . secModels [ 0 ] . doi = g_strdup ( " 0 " ) ;
2014-09-03 21:06:55 +04:00
2019-11-29 13:40:39 +03:00
if ( ! ( caps - > host . numa = virCapabilitiesHostNUMANewHost ( ) ) )
tests: Create full host NUMA topology in more cases
vircapstest has code to add a full host NUMA topology, that
is, one that includes all information about nodes and CPUs
including IDs; testQemuCapsInit(), which is used to create a
mock virCapsPtr for QEMU tests, however, just fakes it by
setting nnumaCell_max to some number.
While the latter approach has served us well so far, we're
going to need all the information to be filled in soon. In
order to do that, we can just move the existing code from
vircapstest to testutils and, with some renaming and
trivial tweaking, use it as-is.
Interestingly, the NUMA topology generated by the function
is rigged up so that the NUMA nodes aren't (necessarily)
numbered starting from 0, which is a nice way to spot
mistaken assumptions in our codebase.
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2018-04-10 18:42:14 +03:00
goto cleanup ;
2015-07-24 17:06:33 +03:00
2019-12-02 15:19:44 +03:00
for ( i = 0 ; i < VIR_ARCH_LAST ; i + + ) {
if ( testQemuAddGuest ( caps , i ) < 0 )
goto cleanup ;
}
2014-01-02 14:42:56 +04:00
2009-11-30 22:01:31 +03:00
if ( virTestGetDebug ( ) ) {
2020-07-28 22:57:28 +03:00
g_autofree char * caps_str = NULL ;
2009-09-10 14:07:20 +04:00
caps_str = virCapabilitiesFormatXML ( caps ) ;
if ( ! caps_str )
goto cleanup ;
2015-04-23 20:38:00 +03:00
VIR_TEST_DEBUG ( " QEMU driver capabilities: \n %s " , caps_str ) ;
2009-09-10 14:07:20 +04:00
}
2008-05-16 20:51:30 +04:00
return caps ;
2014-03-25 10:53:44 +04:00
cleanup :
2016-08-04 14:16:55 +03:00
caps - > host . cpu = NULL ;
2013-02-01 16:26:18 +04:00
virObjectUnref ( caps ) ;
2008-05-16 20:51:30 +04:00
return NULL ;
}
2013-05-17 14:34:24 +04:00
2016-08-04 14:16:55 +03:00
void
2021-03-11 10:16:13 +03:00
qemuTestSetHostArch ( virQEMUDriver * driver ,
2016-08-04 14:16:55 +03:00
virArch arch )
{
if ( arch = = VIR_ARCH_NONE )
arch = VIR_ARCH_X86_64 ;
2019-11-26 20:51:22 +03:00
virTestHostArch = arch ;
driver - > hostarch = virArchFromHost ( ) ;
driver - > caps - > host . arch = virArchFromHost ( ) ;
2019-11-29 13:40:39 +03:00
qemuTestSetHostCPU ( driver , arch , NULL ) ;
2016-08-04 14:16:55 +03:00
}
void
2021-03-11 10:16:13 +03:00
qemuTestSetHostCPU ( virQEMUDriver * driver ,
2019-11-29 13:40:39 +03:00
virArch arch ,
2021-03-11 10:16:13 +03:00
virCPUDef * cpu )
2016-08-04 14:16:55 +03:00
{
if ( ! cpu ) {
if ( ARCH_IS_X86 ( arch ) )
cpu = cpuDefault ;
2016-09-13 23:27:09 +03:00
else if ( ARCH_IS_PPC64 ( arch ) )
cpu = cpuPower8 ;
2016-08-04 14:16:55 +03:00
}
2019-12-18 20:16:19 +03:00
g_unsetenv ( " VIR_TEST_MOCK_FAKE_HOST_CPU " ) ;
2017-07-21 15:24:51 +03:00
if ( cpu ) {
if ( cpu - > model )
2019-12-18 20:16:19 +03:00
g_setenv ( " VIR_TEST_MOCK_FAKE_HOST_CPU " , cpu - > model , TRUE ) ;
2017-07-21 15:24:51 +03:00
}
2019-11-29 13:40:39 +03:00
if ( driver ) {
if ( cpu )
driver - > caps - > host . arch = cpu - > arch ;
driver - > caps - > host . cpu = cpu ;
virCPUDefFree ( driver - > hostcpu ) ;
if ( cpu )
virCPUDefRef ( cpu ) ;
driver - > hostcpu = cpu ;
}
2016-08-04 14:16:55 +03:00
}
2021-03-11 10:16:13 +03:00
virQEMUCaps *
2018-04-04 10:17:52 +03:00
qemuTestParseCapabilitiesArch ( virArch arch ,
const char * capsFile )
2014-06-27 18:39:27 +04:00
{
2021-08-20 14:35:05 +03:00
g_autoptr ( virQEMUCaps ) qemuCaps = NULL ;
2020-02-21 10:28:13 +03:00
g_autofree char * binary = g_strdup_printf ( " /usr/bin/qemu-system-%s " ,
virArchToString ( arch ) ) ;
2014-06-27 18:39:27 +04:00
2019-12-10 13:23:14 +03:00
if ( ! ( qemuCaps = virQEMUCapsNewBinary ( binary ) ) | |
2020-06-18 16:44:16 +03:00
virQEMUCapsLoadCache ( arch , qemuCaps , capsFile , true ) < 0 )
2021-08-20 14:35:05 +03:00
return NULL ;
2014-06-27 18:39:27 +04:00
2021-08-20 14:35:05 +03:00
return g_steal_pointer ( & qemuCaps ) ;
2014-06-27 18:39:27 +04:00
}
2015-09-15 09:16:02 +03:00
2018-04-04 10:17:52 +03:00
2015-09-15 09:16:02 +03:00
void qemuTestDriverFree ( virQEMUDriver * driver )
{
2015-09-22 17:12:39 +03:00
virMutexDestroy ( & driver - > lock ) ;
2020-05-07 23:31:04 +03:00
if ( driver - > config ) {
virFileDeleteTree ( driver - > config - > stateDir ) ;
virFileDeleteTree ( driver - > config - > configDir ) ;
}
2017-07-19 18:01:56 +03:00
virObjectUnref ( driver - > qemuCapsCache ) ;
2015-09-15 09:16:02 +03:00
virObjectUnref ( driver - > xmlopt ) ;
virObjectUnref ( driver - > caps ) ;
virObjectUnref ( driver - > config ) ;
2016-07-29 19:06:51 +03:00
virObjectUnref ( driver - > securityManager ) ;
2021-07-30 11:34:50 +03:00
virCPUDefFree ( cpuDefault ) ;
virCPUDefFree ( cpuHaswell ) ;
virCPUDefFree ( cpuPower8 ) ;
virCPUDefFree ( cpuPower9 ) ;
2015-09-15 09:16:02 +03:00
}
2021-03-11 10:16:13 +03:00
int qemuTestCapsCacheInsert ( virFileCache * cache ,
virQEMUCaps * caps )
2015-09-09 17:03:14 +03:00
{
2019-12-02 15:19:44 +03:00
size_t i , j ;
for ( i = 0 ; i < G_N_ELEMENTS ( qemu_emulators ) ; i + + ) {
2021-03-11 10:16:13 +03:00
virQEMUCaps * tmpCaps ;
2019-12-02 15:19:44 +03:00
if ( qemu_emulators [ i ] = = NULL )
continue ;
if ( caps ) {
tmpCaps = virQEMUCapsNewCopy ( caps ) ;
} else {
tmpCaps = virQEMUCapsNew ( ) ;
}
2020-01-23 15:23:35 +03:00
2020-11-20 15:39:37 +03:00
if ( ! tmpCaps )
return - 1 ;
2020-01-23 15:23:35 +03:00
if ( ! virQEMUCapsHasMachines ( tmpCaps ) ) {
2020-05-25 20:13:43 +03:00
const char * defaultRAMid = NULL ;
/* default-ram-id appeared in QEMU 5.2.0. Reflect
* this in our capabilities , i . e . set it for new
* enough versions only . */
if ( virQEMUCapsGetVersion ( tmpCaps ) > = 5002000 )
defaultRAMid = qemu_default_ram_id [ i ] ;
2020-01-23 15:23:35 +03:00
virQEMUCapsSetArch ( tmpCaps , i ) ;
2020-05-25 20:13:43 +03:00
2020-01-23 15:23:35 +03:00
for ( j = 0 ; qemu_machines [ i ] [ j ] ! = NULL ; j + + ) {
virQEMUCapsAddMachine ( tmpCaps ,
VIR_DOMAIN_VIRT_QEMU ,
qemu_machines [ i ] [ j ] ,
NULL ,
NULL ,
0 ,
false ,
2020-05-14 14:11:01 +03:00
false ,
2020-05-25 20:13:43 +03:00
true ,
2021-01-22 15:16:23 +03:00
defaultRAMid ,
false ) ;
2020-04-28 12:04:14 +03:00
virQEMUCapsSet ( tmpCaps , QEMU_CAPS_TCG ) ;
2020-01-23 15:23:35 +03:00
}
2020-11-18 20:06:45 +03:00
if ( kvm_machines [ i ] ! = NULL ) {
for ( j = 0 ; kvm_machines [ i ] [ j ] ! = NULL ; j + + ) {
virQEMUCapsAddMachine ( tmpCaps ,
VIR_DOMAIN_VIRT_KVM ,
kvm_machines [ i ] [ j ] ,
NULL ,
NULL ,
0 ,
false ,
2021-01-22 15:16:23 +03:00
false ,
2020-11-18 20:06:45 +03:00
true ,
2021-01-22 15:16:23 +03:00
defaultRAMid ,
false ) ;
2020-11-18 20:06:45 +03:00
virQEMUCapsSet ( tmpCaps , QEMU_CAPS_KVM ) ;
}
2020-01-23 15:23:35 +03:00
}
2019-12-02 15:19:44 +03:00
}
2020-01-23 15:23:35 +03:00
2019-12-02 15:19:44 +03:00
if ( virFileCacheInsertData ( cache , qemu_emulators [ i ] , tmpCaps ) < 0 ) {
2017-04-11 15:02:06 +03:00
virObjectUnref ( tmpCaps ) ;
return - 1 ;
}
}
2015-09-09 17:03:14 +03:00
2017-04-11 15:02:06 +03:00
return 0 ;
2015-09-09 17:03:14 +03:00
}
2017-04-11 15:02:06 +03:00
2020-05-07 23:31:04 +03:00
# define STATEDIRTEMPLATE abs_builddir " / qemustatedir-XXXXXX"
# define CONFIGDIRTEMPLATE abs_builddir " / qemuconfigdir-XXXXXX"
2017-03-06 10:27:23 +03:00
2015-09-15 09:16:02 +03:00
int qemuTestDriverInit ( virQEMUDriver * driver )
{
2021-03-11 10:16:13 +03:00
virSecurityManager * mgr = NULL ;
2020-05-07 23:31:04 +03:00
char statedir [ ] = STATEDIRTEMPLATE ;
char configdir [ ] = CONFIGDIRTEMPLATE ;
2016-03-23 18:19:26 +03:00
2016-01-18 11:11:19 +03:00
memset ( driver , 0 , sizeof ( * driver ) ) ;
2019-11-29 13:40:39 +03:00
if ( ! ( cpuDefault = virCPUDefCopy ( & cpuDefaultData ) ) | |
! ( cpuHaswell = virCPUDefCopy ( & cpuHaswellData ) ) | |
! ( cpuPower8 = virCPUDefCopy ( & cpuPower8Data ) ) | |
! ( cpuPower9 = virCPUDefCopy ( & cpuPower9Data ) ) )
return - 1 ;
2015-09-22 17:12:39 +03:00
if ( virMutexInit ( & driver - > lock ) < 0 )
return - 1 ;
2019-11-26 20:51:22 +03:00
driver - > hostarch = virArchFromHost ( ) ;
2020-05-07 23:31:04 +03:00
driver - > config = virQEMUDriverConfigNew ( false , NULL ) ;
2015-09-15 09:16:02 +03:00
if ( ! driver - > config )
2015-09-22 17:12:39 +03:00
goto error ;
2015-09-15 09:16:02 +03:00
2020-05-07 23:31:04 +03:00
/* Do this early so that qemuTestDriverFree() doesn't see (unlink) the real
* dirs . */
VIR_FREE ( driver - > config - > stateDir ) ;
VIR_FREE ( driver - > config - > configDir ) ;
2016-03-15 15:54:24 +03:00
/* Overwrite some default paths so it's consistent for tests. */
VIR_FREE ( driver - > config - > libDir ) ;
VIR_FREE ( driver - > config - > channelTargetDir ) ;
2019-10-20 14:49:46 +03:00
driver - > config - > libDir = g_strdup ( " /tmp/lib " ) ;
driver - > config - > channelTargetDir = g_strdup ( " /tmp/channel " ) ;
2016-03-15 15:54:24 +03:00
2020-05-07 23:31:04 +03:00
if ( ! g_mkdtemp ( statedir ) ) {
fprintf ( stderr , " Cannot create fake stateDir " ) ;
goto error ;
}
driver - > config - > stateDir = g_strdup ( statedir ) ;
if ( ! g_mkdtemp ( configdir ) ) {
fprintf ( stderr , " Cannot create fake configDir " ) ;
goto error ;
}
driver - > config - > configDir = g_strdup ( configdir ) ;
2015-09-15 09:16:02 +03:00
driver - > caps = testQemuCapsInit ( ) ;
if ( ! driver - > caps )
goto error ;
2015-09-09 17:03:15 +03:00
/* Using /dev/null for libDir and cacheDir automatically produces errors
* upon attempt to use any of them */
2019-04-12 22:21:05 +03:00
driver - > qemuCapsCache = virQEMUCapsCacheNew ( " /dev/null " , " /dev/null " , 0 , 0 ) ;
2015-09-09 17:03:15 +03:00
if ( ! driver - > qemuCapsCache )
goto error ;
2019-11-26 21:57:30 +03:00
driver - > xmlopt = virQEMUDriverCreateXMLConf ( driver , " none " ) ;
2015-09-15 09:16:02 +03:00
if ( ! driver - > xmlopt )
goto error ;
2017-04-11 15:02:06 +03:00
if ( qemuTestCapsCacheInsert ( driver - > qemuCapsCache , NULL ) < 0 )
2015-09-09 17:03:15 +03:00
goto error ;
2018-10-02 16:08:28 +03:00
if ( ! ( mgr = virSecurityManagerNew ( " none " , " qemu " ,
2016-03-23 18:19:26 +03:00
VIR_SECURITY_MANAGER_PRIVILEGED ) ) )
goto error ;
if ( ! ( driver - > securityManager = virSecurityManagerNewStack ( mgr ) ) )
goto error ;
2019-11-29 13:40:39 +03:00
qemuTestSetHostCPU ( driver , driver - > hostarch , NULL ) ;
2015-09-15 09:16:02 +03:00
return 0 ;
error :
2016-03-23 18:19:26 +03:00
virObjectUnref ( mgr ) ;
2015-09-15 09:16:02 +03:00
qemuTestDriverFree ( driver ) ;
return - 1 ;
}
2015-09-09 17:03:14 +03:00
2016-05-10 12:35:43 +03:00
int
2021-03-11 10:16:13 +03:00
testQemuCapsSetGIC ( virQEMUCaps * qemuCaps ,
2016-05-10 12:35:43 +03:00
int gic )
{
virGICCapability * gicCapabilities = NULL ;
size_t ngicCapabilities = 0 ;
2020-09-23 02:04:17 +03:00
gicCapabilities = g_new0 ( virGICCapability , 2 ) ;
2016-05-10 12:35:43 +03:00
# define IMPL_BOTH \
VIR_GIC_IMPLEMENTATION_KERNEL | VIR_GIC_IMPLEMENTATION_EMULATED
if ( gic & GIC_V2 ) {
gicCapabilities [ ngicCapabilities ] . version = VIR_GIC_VERSION_2 ;
gicCapabilities [ ngicCapabilities ] . implementation = IMPL_BOTH ;
ngicCapabilities + + ;
}
if ( gic & GIC_V3 ) {
gicCapabilities [ ngicCapabilities ] . version = VIR_GIC_VERSION_3 ;
gicCapabilities [ ngicCapabilities ] . implementation = IMPL_BOTH ;
ngicCapabilities + + ;
}
# undef IMPL_BOTH
virQEMUCapsSetGICCapabilities ( qemuCaps ,
gicCapabilities , ngicCapabilities ) ;
2020-01-07 00:57:49 +03:00
return 0 ;
2016-05-10 12:35:43 +03:00
}
2008-06-26 13:37:51 +04:00
# endif
2018-04-18 11:47:31 +03:00
char *
2019-04-16 13:26:22 +03:00
testQemuGetLatestCapsForArch ( const char * arch ,
2018-04-18 11:47:31 +03:00
const char * suffix )
{
struct dirent * ent ;
2020-10-26 00:50:51 +03:00
g_autoptr ( DIR ) dir = NULL ;
2018-04-18 11:47:31 +03:00
int rc ;
2020-07-28 22:57:28 +03:00
g_autofree char * fullsuffix = NULL ;
2018-04-18 11:47:31 +03:00
unsigned long maxver = 0 ;
unsigned long ver ;
2020-02-07 17:51:26 +03:00
g_autofree char * maxname = NULL ;
2018-04-18 11:47:31 +03:00
2019-10-22 16:26:14 +03:00
fullsuffix = g_strdup_printf ( " %s.%s " , arch , suffix ) ;
2018-04-18 11:47:31 +03:00
2019-04-16 13:26:22 +03:00
if ( virDirOpen ( & dir , TEST_QEMU_CAPS_PATH ) < 0 )
2020-10-28 00:49:11 +03:00
return NULL ;
2018-04-18 11:47:31 +03:00
2019-04-16 13:26:22 +03:00
while ( ( rc = virDirRead ( dir , & ent , TEST_QEMU_CAPS_PATH ) ) > 0 ) {
2020-07-28 23:01:22 +03:00
g_autofree char * tmp = NULL ;
2018-04-18 11:47:31 +03:00
2019-10-20 14:49:46 +03:00
tmp = g_strdup ( STRSKIP ( ent - > d_name , " caps_ " ) ) ;
2018-04-18 11:47:31 +03:00
2019-10-20 14:49:46 +03:00
if ( ! tmp )
2018-04-18 11:47:31 +03:00
continue ;
2019-03-07 12:18:38 +03:00
if ( ! virStringStripSuffix ( tmp , fullsuffix ) )
2018-04-18 11:47:31 +03:00
continue ;
if ( virParseVersionString ( tmp , & ver , false ) < 0 ) {
2019-05-03 11:31:02 +03:00
VIR_TEST_DEBUG ( " skipping caps file '%s' " , ent - > d_name ) ;
2018-04-18 11:47:31 +03:00
continue ;
}
if ( ver > maxver ) {
2020-02-07 17:51:26 +03:00
g_free ( maxname ) ;
maxname = g_strdup ( ent - > d_name ) ;
2018-04-18 11:47:31 +03:00
maxver = ver ;
}
}
if ( rc < 0 )
2020-10-28 00:49:11 +03:00
return NULL ;
2018-04-18 11:47:31 +03:00
if ( ! maxname ) {
2019-05-03 11:45:58 +03:00
VIR_TEST_VERBOSE ( " failed to find capabilities for '%s' in '%s' " ,
2019-04-16 13:26:22 +03:00
arch , TEST_QEMU_CAPS_PATH ) ;
2020-10-28 00:49:11 +03:00
return NULL ;
2018-04-18 11:47:31 +03:00
}
2020-10-28 00:49:11 +03:00
return g_strdup_printf ( " %s/%s " , TEST_QEMU_CAPS_PATH , maxname ) ;
2018-04-18 11:47:31 +03:00
}
2019-03-07 17:54:55 +03:00
2020-10-22 20:04:18 +03:00
GHashTable *
2019-03-31 19:43:44 +03:00
testQemuGetLatestCaps ( void )
{
const char * archs [ ] = {
" aarch64 " ,
" ppc64 " ,
" riscv64 " ,
" s390x " ,
" x86_64 " ,
} ;
2021-07-05 17:22:08 +03:00
g_autoptr ( GHashTable ) capslatest = virHashNew ( g_free ) ;
2019-03-31 19:43:44 +03:00
size_t i ;
2019-05-03 11:45:58 +03:00
VIR_TEST_VERBOSE ( " " ) ;
2019-03-31 19:43:44 +03:00
2019-10-15 14:55:26 +03:00
for ( i = 0 ; i < G_N_ELEMENTS ( archs ) ; + + i ) {
2019-04-16 13:26:22 +03:00
char * cap = testQemuGetLatestCapsForArch ( archs [ i ] , " xml " ) ;
2019-03-31 19:43:44 +03:00
if ( ! cap | | virHashAddEntry ( capslatest , archs [ i ] , cap ) < 0 )
2021-07-05 17:22:49 +03:00
return NULL ;
2019-03-31 19:43:44 +03:00
2019-05-03 11:45:58 +03:00
VIR_TEST_VERBOSE ( " latest caps for %s: %s " , archs [ i ] , cap ) ;
2019-03-31 19:43:44 +03:00
}
2019-05-03 11:45:58 +03:00
VIR_TEST_VERBOSE ( " " ) ;
2021-07-05 17:22:08 +03:00
return g_steal_pointer ( & capslatest ) ;
2019-03-31 19:43:44 +03:00
}
2019-03-07 17:54:55 +03:00
int
2019-04-16 13:33:14 +03:00
testQemuCapsIterate ( const char * suffix ,
2019-03-07 17:54:55 +03:00
testQemuCapsIterateCallback callback ,
void * opaque )
{
struct dirent * ent ;
2020-10-26 00:50:51 +03:00
g_autoptr ( DIR ) dir = NULL ;
2019-03-07 17:54:55 +03:00
int rc ;
2019-10-25 14:59:46 +03:00
bool fail = false ;
2019-03-07 17:54:55 +03:00
if ( ! callback )
return 0 ;
2019-10-22 19:34:03 +03:00
/* Validate suffix */
if ( ! STRPREFIX ( suffix , " . " ) ) {
VIR_TEST_VERBOSE ( " malformed suffix '%s' " , suffix ) ;
2020-10-28 00:49:11 +03:00
return - 1 ;
2019-10-22 19:34:03 +03:00
}
2019-04-16 13:33:14 +03:00
if ( virDirOpen ( & dir , TEST_QEMU_CAPS_PATH ) < 0 )
2020-10-28 00:49:11 +03:00
return - 1 ;
2019-03-07 17:54:55 +03:00
2019-06-04 21:56:58 +03:00
while ( ( rc = virDirRead ( dir , & ent , TEST_QEMU_CAPS_PATH ) ) > 0 ) {
2019-10-22 19:31:09 +03:00
g_autofree char * tmp = g_strdup ( ent - > d_name ) ;
2019-10-22 17:08:10 +03:00
char * version = NULL ;
2019-03-07 17:54:55 +03:00
char * archName = NULL ;
/* Strip the trailing suffix, moving on if it's not present */
if ( ! virStringStripSuffix ( tmp , suffix ) )
continue ;
2019-10-22 17:08:10 +03:00
/* Strip the leading prefix */
if ( ! ( version = STRSKIP ( tmp , " caps_ " ) ) ) {
VIR_TEST_VERBOSE ( " malformed file name '%s' " , ent - > d_name ) ;
2020-10-28 00:49:11 +03:00
return - 1 ;
2019-10-22 17:08:10 +03:00
}
2019-10-22 19:31:09 +03:00
/* Find the last dot */
if ( ! ( archName = strrchr ( tmp , ' . ' ) ) ) {
VIR_TEST_VERBOSE ( " malformed file name '%s' " , ent - > d_name ) ;
2020-10-28 00:49:11 +03:00
return - 1 ;
2019-10-22 19:31:09 +03:00
}
2019-03-07 17:54:55 +03:00
2019-10-22 17:08:10 +03:00
/* The version number and the architecture name are separated by
* a dot : overwriting that dot with \ 0 results in both being usable
* as independent , null - terminated strings */
2019-03-07 17:54:55 +03:00
archName [ 0 ] = ' \0 ' ;
archName + + ;
2019-10-22 16:44:37 +03:00
/* Run the user-provided callback.
*
* We skip the dot that , as verified earlier , starts the suffix
* to make it nicer to rebuild the original file name from inside
* the callback .
*/
2019-10-22 17:08:10 +03:00
if ( callback ( TEST_QEMU_CAPS_PATH , " caps " , version ,
2019-10-25 14:59:46 +03:00
archName , suffix + 1 , opaque ) < 0 )
fail = true ;
2019-03-07 17:54:55 +03:00
}
2019-10-25 14:59:46 +03:00
if ( rc < 0 | | fail )
2020-10-28 00:49:11 +03:00
return - 1 ;
2019-03-07 17:54:55 +03:00
2020-10-28 00:49:11 +03:00
return 0 ;
2019-03-07 17:54:55 +03:00
}
2019-03-31 18:49:34 +03:00
2021-08-17 16:30:44 +03:00
void
2019-03-31 18:49:34 +03:00
testQemuInfoSetArgs ( struct testQemuInfo * info ,
2021-08-17 17:26:58 +03:00
struct testQemuConf * conf , . . . )
2019-03-31 18:49:34 +03:00
{
va_list argptr ;
testQemuInfoArgName argname ;
int flag ;
2021-08-17 16:30:44 +03:00
if ( ! ( info - > args . fakeCaps = virQEMUCapsNew ( ) ) )
2021-08-16 17:42:18 +03:00
abort ( ) ;
2021-08-17 17:26:58 +03:00
info - > conf = conf ;
2021-08-17 16:30:44 +03:00
info - > args . newargs = true ;
2021-08-17 17:26:58 +03:00
va_start ( argptr , conf ) ;
2021-08-17 15:24:13 +03:00
while ( ( argname = va_arg ( argptr , testQemuInfoArgName ) ) ! = ARG_END ) {
2019-03-31 18:49:34 +03:00
switch ( argname ) {
case ARG_QEMU_CAPS :
2021-08-17 16:30:44 +03:00
info - > args . fakeCapsUsed = true ;
2019-03-31 18:49:34 +03:00
while ( ( flag = va_arg ( argptr , int ) ) < QEMU_CAPS_LAST )
2021-08-17 16:30:44 +03:00
virQEMUCapsSet ( info - > args . fakeCaps , flag ) ;
2019-03-31 18:49:34 +03:00
break ;
case ARG_GIC :
2021-08-17 16:30:44 +03:00
info - > args . gic = va_arg ( argptr , int ) ;
2019-03-31 18:49:34 +03:00
break ;
case ARG_MIGRATE_FROM :
info - > migrateFrom = va_arg ( argptr , char * ) ;
break ;
case ARG_MIGRATE_FD :
info - > migrateFd = va_arg ( argptr , int ) ;
break ;
case ARG_FLAGS :
info - > flags = va_arg ( argptr , int ) ;
break ;
case ARG_PARSEFLAGS :
info - > parseFlags = va_arg ( argptr , int ) ;
break ;
case ARG_CAPS_ARCH :
2021-08-17 16:30:44 +03:00
info - > args . capsarch = va_arg ( argptr , char * ) ;
2019-03-31 18:49:34 +03:00
break ;
case ARG_CAPS_VER :
2021-08-17 16:30:44 +03:00
info - > args . capsver = va_arg ( argptr , char * ) ;
2019-03-31 18:49:34 +03:00
break ;
case ARG_END :
default :
2021-08-17 16:30:44 +03:00
info - > args . invalidarg = true ;
break ;
2019-03-31 18:49:34 +03:00
}
2021-08-17 16:30:44 +03:00
if ( info - > args . invalidarg )
break ;
2019-03-31 18:49:34 +03:00
}
2021-08-17 16:30:44 +03:00
va_end ( argptr ) ;
}
int
testQemuInfoInitArgs ( struct testQemuInfo * info )
{
g_autofree char * capsfile = NULL ;
if ( ! info - > args . newargs )
return 0 ;
info - > args . newargs = false ;
if ( info - > args . invalidarg ) {
fprintf ( stderr , " Invalid agument encountered by 'testQemuInfoSetArgs' \n " ) ;
return - 1 ;
2019-03-31 18:49:34 +03:00
}
2021-08-17 16:30:44 +03:00
if ( ! ! info - > args . capsarch ^ ! ! info - > args . capsver ) {
fprintf ( stderr , " ARG_CAPS_ARCH and ARG_CAPS_VER must be specified together. \n " ) ;
return - 1 ;
}
if ( info - > args . capsarch & & info - > args . capsver ) {
2019-03-31 18:49:34 +03:00
bool stripmachinealiases = false ;
2021-03-11 10:16:13 +03:00
virQEMUCaps * cachedcaps = NULL ;
2019-03-31 18:49:34 +03:00
2021-08-17 16:30:44 +03:00
if ( info - > args . fakeCapsUsed ) {
fprintf ( stderr , " ARG_QEMU_CAPS can not be combined with ARG_CAPS_ARCH or ARG_CAPS_VER \n " ) ;
return - 1 ;
2021-08-16 17:42:18 +03:00
}
2021-08-17 16:30:44 +03:00
info - > arch = virArchFromString ( info - > args . capsarch ) ;
2019-10-24 18:51:42 +03:00
2021-08-17 16:30:44 +03:00
if ( STREQ ( info - > args . capsver , " latest " ) ) {
capsfile = g_strdup ( virHashLookup ( info - > conf - > capslatest , info - > args . capsarch ) ) ;
2019-03-31 18:49:34 +03:00
stripmachinealiases = true ;
2021-02-19 18:31:42 +03:00
} else {
capsfile = g_strdup_printf ( " %s/caps_%s.%s.xml " ,
2021-08-17 16:30:44 +03:00
TEST_QEMU_CAPS_PATH ,
info - > args . capsver ,
info - > args . capsarch ) ;
2021-02-19 18:31:42 +03:00
}
2019-03-31 18:49:34 +03:00
2021-08-17 17:26:58 +03:00
if ( ! g_hash_table_lookup_extended ( info - > conf - > capscache , capsfile , NULL , ( void * * ) & cachedcaps ) ) {
2021-08-16 17:42:18 +03:00
if ( ! ( cachedcaps = qemuTestParseCapabilitiesArch ( info - > arch , capsfile ) ) )
2021-08-17 16:30:44 +03:00
return - 1 ;
2021-02-19 18:46:45 +03:00
2021-08-17 17:26:58 +03:00
g_hash_table_insert ( info - > conf - > capscache , g_strdup ( capsfile ) , cachedcaps ) ;
2021-02-19 18:46:45 +03:00
}
2021-08-16 17:42:18 +03:00
if ( ! ( info - > qemuCaps = virQEMUCapsNewCopy ( cachedcaps ) ) )
2021-08-17 16:30:44 +03:00
return - 1 ;
2019-03-31 18:49:34 +03:00
2021-05-17 11:51:15 +03:00
if ( stripmachinealiases )
2021-08-16 17:42:18 +03:00
virQEMUCapsStripMachineAliases ( info - > qemuCaps ) ;
2021-05-17 11:51:15 +03:00
2019-03-31 18:49:34 +03:00
info - > flags | = FLAG_REAL_CAPS ;
2020-05-15 15:33:10 +03:00
/* provide path to the replies file for schema testing */
capsfile [ strlen ( capsfile ) - 3 ] = ' \0 ' ;
info - > schemafile = g_strdup_printf ( " %sreplies " , capsfile ) ;
2021-08-16 17:42:18 +03:00
} else {
2021-08-17 16:30:44 +03:00
info - > qemuCaps = g_steal_pointer ( & info - > args . fakeCaps ) ;
2019-03-31 18:49:34 +03:00
}
2021-08-17 16:30:44 +03:00
if ( info - > args . gic ! = GIC_NONE & &
testQemuCapsSetGIC ( info - > qemuCaps , info - > args . gic ) < 0 )
return - 1 ;
2019-03-31 18:49:34 +03:00
2021-08-17 16:30:44 +03:00
return 0 ;
2019-03-31 18:49:34 +03:00
}
void
testQemuInfoClear ( struct testQemuInfo * info )
{
VIR_FREE ( info - > infile ) ;
VIR_FREE ( info - > outfile ) ;
2020-05-15 15:33:10 +03:00
VIR_FREE ( info - > schemafile ) ;
2020-09-22 14:57:39 +03:00
VIR_FREE ( info - > errfile ) ;
2019-03-31 18:49:34 +03:00
virObjectUnref ( info - > qemuCaps ) ;
2021-08-17 16:30:44 +03:00
g_clear_pointer ( & info - > args . fakeCaps , virObjectUnref ) ;
2019-03-31 18:49:34 +03:00
}