2014-06-25 15:24:53 +04:00
/*
* Copyright ( C ) Red Hat , Inc . 2014
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*/
# include <config.h>
# include "testutils.h"
# include "domain_capabilities.h"
2019-04-04 13:42:14 +03:00
# include "virfilewrapper.h"
# include "configmake.h"
2014-06-25 15:24:53 +04:00
# define VIR_FROM_THIS VIR_FROM_NONE
2019-03-18 19:40:03 +03:00
# if WITH_QEMU || WITH_BHYVE
2019-10-14 15:13:31 +03:00
static int G_GNUC_NULL_TERMINATED
2014-09-17 03:52:54 +04:00
fillStringValues ( virDomainCapsStringValuesPtr values , . . . )
{
int ret = 0 ;
va_list list ;
const char * str ;
va_start ( list , values ) ;
while ( ( str = va_arg ( list , const char * ) ) ) {
2019-10-20 14:49:46 +03:00
if ( VIR_REALLOC_N ( values - > values , values - > nvalues + 1 ) < 0 ) {
2014-09-17 03:52:54 +04:00
ret = - 1 ;
break ;
}
2019-10-20 14:49:46 +03:00
values - > values [ values - > nvalues ] = g_strdup ( str ) ;
2014-09-17 03:52:54 +04:00
values - > nvalues + + ;
}
va_end ( list ) ;
return ret ;
}
2019-03-18 19:40:03 +03:00
# endif /* WITH_QEMU || WITH_BHYVE */
2014-09-17 03:52:54 +04:00
2016-04-25 15:20:58 +03:00
# if WITH_QEMU
2014-06-25 20:39:29 +04:00
# include "testutilsqemu.h"
2017-07-21 15:24:51 +03:00
# include "testutilshostcpus.h"
2016-12-18 22:22:24 +03:00
2016-04-22 23:22:30 +03:00
static int
2019-11-29 13:40:39 +03:00
fakeHostCPU ( virArch arch )
2016-04-22 23:22:30 +03:00
{
2019-12-11 12:13:15 +03:00
g_autoptr ( virCPUDef ) cpu = NULL ;
2016-04-22 23:22:30 +03:00
2017-07-21 15:24:51 +03:00
if ( ! ( cpu = testUtilsHostCpusGetDefForArch ( arch ) ) ) {
2016-04-22 23:22:30 +03:00
virReportError ( VIR_ERR_INTERNAL_ERROR ,
" cannot fake host CPU for arch %s " ,
virArchToString ( arch ) ) ;
return - 1 ;
}
2019-11-29 13:40:39 +03:00
qemuTestSetHostCPU ( NULL , arch , cpu ) ;
2016-04-22 23:22:30 +03:00
return 0 ;
}
2014-09-17 13:33:35 +04:00
static int
2014-06-25 20:39:29 +04:00
fillQemuCaps ( virDomainCapsPtr domCaps ,
2016-04-25 15:20:58 +03:00
const char * name ,
2016-05-10 20:59:48 +03:00
const char * arch ,
2016-04-28 19:01:18 +03:00
const char * machine ,
2016-04-25 15:20:58 +03:00
virQEMUDriverConfigPtr cfg )
2014-06-25 20:39:29 +04:00
{
2016-04-25 15:20:58 +03:00
int ret = - 1 ;
char * path = NULL ;
virQEMUCapsPtr qemuCaps = NULL ;
2015-01-21 21:38:57 +03:00
virDomainCapsLoaderPtr loader = & domCaps - > os . loader ;
2019-10-24 09:52:21 +03:00
virDomainVirtType virtType ;
2014-06-25 20:39:29 +04:00
2019-11-29 13:40:39 +03:00
if ( fakeHostCPU ( domCaps - > arch ) < 0 )
2016-06-15 15:35:18 +03:00
goto cleanup ;
2019-10-22 16:26:14 +03:00
path = g_strdup_printf ( " %s/%s.%s.xml " , TEST_QEMU_CAPS_PATH , name , arch ) ;
2019-11-29 13:40:39 +03:00
if ( ! ( qemuCaps = qemuTestParseCapabilitiesArch ( domCaps - > arch , path ) ) )
2016-04-25 15:20:58 +03:00
goto cleanup ;
2019-10-24 09:52:21 +03:00
if ( virQEMUCapsGet ( qemuCaps , QEMU_CAPS_KVM ) )
virtType = VIR_DOMAIN_VIRT_KVM ;
else
virtType = VIR_DOMAIN_VIRT_QEMU ;
2017-12-06 16:56:54 +03:00
if ( machine ) {
VIR_FREE ( domCaps - > machine ) ;
2019-10-24 09:52:21 +03:00
domCaps - > machine = g_strdup ( virQEMUCapsGetCanonicalMachine ( qemuCaps , virtType , machine ) ) ;
2017-12-06 16:56:54 +03:00
}
2016-04-28 19:01:18 +03:00
2019-10-18 16:08:21 +03:00
if ( ! domCaps - > machine )
2019-10-24 09:52:21 +03:00
domCaps - > machine = g_strdup ( virQEMUCapsGetPreferredMachine ( qemuCaps , virtType ) ) ;
2016-04-28 19:01:18 +03:00
2019-11-29 13:40:39 +03:00
if ( virQEMUCapsFillDomainCaps ( qemuCaps , domCaps - > arch , domCaps ,
2019-04-04 13:42:14 +03:00
false ,
2016-05-18 01:45:27 +03:00
cfg - > firmwares ,
2016-06-27 16:12:34 +03:00
cfg - > nfirmwares ) < 0 )
2016-04-25 15:20:58 +03:00
goto cleanup ;
2014-06-25 20:39:29 +04:00
2019-05-03 16:25:07 +03:00
/* The function above tries to query host's VFIO capabilities by calling
2014-06-25 20:39:29 +04:00
* qemuHostdevHostSupportsPassthroughVFIO ( ) which , however , can ' t be
* successfully mocked as they are not exposed as internal APIs . Therefore ,
* instead of mocking set the expected values here by hand . */
VIR_DOMAIN_CAPS_ENUM_SET ( domCaps - > hostdev . pciBackend ,
VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT ,
VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO ) ;
2014-09-17 19:17:03 +04:00
2016-04-25 15:20:58 +03:00
/* As of f05b6a918e28 we are expecting to see OVMF_CODE.fd file which
* may not exists everywhere . */
2015-01-21 21:38:57 +03:00
while ( loader - > values . nvalues )
VIR_FREE ( loader - > values . values [ - - loader - > values . nvalues ] ) ;
if ( fillStringValues ( & loader - > values ,
2015-01-21 21:44:43 +03:00
" /usr/share/AAVMF/AAVMF_CODE.fd " ,
2017-07-20 22:56:55 +03:00
" /usr/share/AAVMF/AAVMF32_CODE.fd " ,
2015-01-21 21:38:57 +03:00
" /usr/share/OVMF/OVMF_CODE.fd " ,
NULL ) < 0 )
2014-06-25 15:24:53 +04:00
goto cleanup ;
2016-04-25 15:20:58 +03:00
ret = 0 ;
2014-06-25 15:24:53 +04:00
cleanup :
2016-04-25 15:20:58 +03:00
virObjectUnref ( qemuCaps ) ;
VIR_FREE ( path ) ;
2014-09-17 03:52:54 +04:00
return ret ;
2014-06-25 15:24:53 +04:00
}
2016-04-25 15:20:58 +03:00
# endif /* WITH_QEMU */
2016-05-17 00:21:59 +03:00
# ifdef WITH_LIBXL
# include "testutilsxen.h"
static int
fillXenCaps ( virDomainCapsPtr domCaps )
{
virFirmwarePtr * firmwares ;
int ret = - 1 ;
if ( VIR_ALLOC_N ( firmwares , 2 ) < 0 )
return ret ;
if ( VIR_ALLOC ( firmwares [ 0 ] ) < 0 | | VIR_ALLOC ( firmwares [ 1 ] ) < 0 )
goto cleanup ;
2019-10-20 14:49:46 +03:00
firmwares [ 0 ] - > name = g_strdup ( " /usr/lib/xen/boot/hvmloader " ) ;
firmwares [ 1 ] - > name = g_strdup ( " /usr/lib/xen/boot/ovmf.bin " ) ;
2016-05-17 00:21:59 +03:00
if ( libxlMakeDomainCapabilities ( domCaps , firmwares , 2 ) < 0 )
goto cleanup ;
ret = 0 ;
cleanup :
virFirmwareFreeList ( firmwares , 2 ) ;
return ret ;
}
# endif /* WITH_LIBXL */
2017-03-18 23:17:13 +03:00
# ifdef WITH_BHYVE
# include "bhyve / bhyve_capabilities.h"
static int
fillBhyveCaps ( virDomainCapsPtr domCaps , unsigned int * bhyve_caps )
{
virDomainCapsStringValuesPtr firmwares = NULL ;
int ret = - 1 ;
if ( VIR_ALLOC ( firmwares ) < 0 )
return - 1 ;
if ( fillStringValues ( firmwares , " /foo/bar " , " /foo/baz " , NULL ) < 0 )
goto cleanup ;
if ( virBhyveDomainCapsFill ( domCaps , * bhyve_caps , firmwares ) < 0 )
goto cleanup ;
ret = 0 ;
cleanup :
VIR_FREE ( firmwares ) ;
return ret ;
}
# endif /* WITH_BHYVE */
2016-05-17 00:21:59 +03:00
2016-04-25 15:20:58 +03:00
enum testCapsType {
CAPS_NONE ,
CAPS_QEMU ,
2016-05-17 00:21:59 +03:00
CAPS_LIBXL ,
2017-03-18 23:17:13 +03:00
CAPS_BHYVE ,
2016-04-25 15:20:58 +03:00
} ;
2014-06-25 15:24:53 +04:00
2016-04-25 15:20:58 +03:00
struct testData {
const char * name ;
const char * emulator ;
2014-06-25 15:24:53 +04:00
const char * machine ;
2016-05-10 20:59:48 +03:00
const char * arch ;
2014-06-25 15:24:53 +04:00
virDomainVirtType type ;
2016-04-25 15:20:58 +03:00
enum testCapsType capsType ;
const char * capsName ;
void * capsOpaque ;
2014-06-25 15:24:53 +04:00
} ;
static int
test_virDomainCapsFormat ( const void * opaque )
{
2016-04-25 15:20:58 +03:00
const struct testData * data = opaque ;
2014-06-25 15:24:53 +04:00
virDomainCapsPtr domCaps = NULL ;
char * path = NULL ;
char * domCapsXML = NULL ;
int ret = - 1 ;
2019-10-22 16:26:14 +03:00
path = g_strdup_printf ( " %s/domaincapsdata/%s.xml " , abs_srcdir , data - > name ) ;
2014-06-25 15:24:53 +04:00
2016-05-10 20:59:48 +03:00
if ( ! ( domCaps = virDomainCapsNew ( data - > emulator , data - > machine ,
virArchFromString ( data - > arch ) ,
2016-04-25 15:20:58 +03:00
data - > type ) ) )
2014-06-25 15:24:53 +04:00
goto cleanup ;
2016-04-25 15:20:58 +03:00
switch ( data - > capsType ) {
case CAPS_NONE :
break ;
case CAPS_QEMU :
# if WITH_QEMU
2016-04-28 19:01:18 +03:00
if ( fillQemuCaps ( domCaps , data - > capsName , data - > arch , data - > machine ,
2016-04-25 15:19:49 +03:00
data - > capsOpaque ) < 0 )
2016-04-25 15:20:58 +03:00
goto cleanup ;
2016-05-17 00:21:59 +03:00
# endif
break ;
case CAPS_LIBXL :
# if WITH_LIBXL
if ( fillXenCaps ( domCaps ) < 0 )
goto cleanup ;
2017-03-18 23:17:13 +03:00
# endif
break ;
case CAPS_BHYVE :
# if WITH_BHYVE
if ( fillBhyveCaps ( domCaps , data - > capsOpaque ) < 0 )
goto cleanup ;
2016-04-25 15:20:58 +03:00
# endif
break ;
}
2014-06-25 15:24:53 +04:00
if ( ! ( domCapsXML = virDomainCapsFormat ( domCaps ) ) )
goto cleanup ;
2016-05-26 18:01:53 +03:00
if ( virTestCompareToFile ( domCapsXML , path ) < 0 )
2014-06-25 15:24:53 +04:00
goto cleanup ;
ret = 0 ;
cleanup :
VIR_FREE ( domCapsXML ) ;
VIR_FREE ( path ) ;
virObjectUnref ( domCaps ) ;
return ret ;
}
2019-10-24 20:31:08 +03:00
# if WITH_QEMU
2019-10-22 17:27:21 +03:00
static int
2019-10-22 18:16:26 +03:00
doTestQemuInternal ( const char * version ,
const char * machine ,
const char * arch ,
virDomainVirtType type ,
void * opaque )
2019-10-22 17:27:21 +03:00
{
g_autofree char * name = NULL ;
2019-10-22 17:56:40 +03:00
g_autofree char * capsName = NULL ;
g_autofree char * emulator = NULL ;
2019-10-22 17:27:21 +03:00
2019-10-22 17:56:40 +03:00
name = g_strdup_printf ( " qemu_%s%s%s%s.%s " ,
version ,
( type = = VIR_DOMAIN_VIRT_QEMU ? " -tcg " : " " ) ,
( machine ? " - " : " " ) , ( machine ? machine : " " ) ,
arch ) ;
capsName = g_strdup_printf ( " caps_%s " , version ) ;
emulator = g_strdup_printf ( " /usr/bin/qemu-system-%s " , arch ) ;
2019-10-22 17:27:21 +03:00
2020-08-03 18:32:22 +03:00
VIR_WARNINGS_NO_DECLARATION_AFTER_STATEMENT
2019-10-22 17:27:21 +03:00
struct testData data = {
. name = name ,
. emulator = emulator ,
. machine = machine ,
. arch = arch ,
. type = type ,
. capsType = CAPS_QEMU ,
. capsName = capsName ,
. capsOpaque = opaque ,
} ;
2020-08-03 18:32:22 +03:00
VIR_WARNINGS_RESET
2019-10-22 17:27:21 +03:00
if ( virTestRun ( name , test_virDomainCapsFormat , & data ) < 0 )
return - 1 ;
return 0 ;
}
2019-10-22 18:16:26 +03:00
static int
2019-10-22 18:31:54 +03:00
doTestQemu ( const char * inputDir G_GNUC_UNUSED ,
const char * prefix G_GNUC_UNUSED ,
const char * version ,
2019-10-22 18:16:26 +03:00
const char * arch ,
2019-10-22 18:31:54 +03:00
const char * suffix G_GNUC_UNUSED ,
2019-10-22 18:16:26 +03:00
void * opaque )
{
2019-10-25 14:59:46 +03:00
int ret = 0 ;
2019-10-22 18:16:26 +03:00
if ( STREQ ( arch , " x86_64 " ) ) {
/* For x86_64 we test three combinations:
*
* - KVM with default machine
* - KVM with Q35 machine
* - TCG with default machine
*/
if ( doTestQemuInternal ( version , NULL , arch ,
2019-10-25 14:59:46 +03:00
VIR_DOMAIN_VIRT_KVM , opaque ) < 0 )
ret = - 1 ;
if ( doTestQemuInternal ( version , " q35 " , arch ,
VIR_DOMAIN_VIRT_KVM , opaque ) < 0 )
ret = - 1 ;
if ( doTestQemuInternal ( version , NULL , arch ,
VIR_DOMAIN_VIRT_QEMU , opaque ) < 0 )
ret = - 1 ;
2019-10-22 18:16:26 +03:00
} else if ( STREQ ( arch , " aarch64 " ) ) {
/* For aarch64 we test two combinations:
*
* - KVM with default machine
* - KVM with virt machine
*/
if ( doTestQemuInternal ( version , NULL , arch ,
2019-10-25 14:59:46 +03:00
VIR_DOMAIN_VIRT_KVM , opaque ) < 0 )
ret = - 1 ;
if ( doTestQemuInternal ( version , " virt " , arch ,
VIR_DOMAIN_VIRT_KVM , opaque ) < 0 )
ret = - 1 ;
2019-10-22 18:16:26 +03:00
} else if ( STRPREFIX ( arch , " riscv " ) ) {
/* Unfortunately we have to skip RISC-V at the moment */
return 0 ;
} else {
if ( doTestQemuInternal ( version , NULL , arch ,
VIR_DOMAIN_VIRT_KVM , opaque ) < 0 )
2019-10-25 14:59:46 +03:00
ret = - 1 ;
2019-10-22 18:16:26 +03:00
}
2019-10-25 14:59:46 +03:00
return ret ;
2019-10-22 18:16:26 +03:00
}
2019-10-24 20:31:08 +03:00
# endif
2014-06-25 15:24:53 +04:00
static int
mymain ( void )
{
int ret = 0 ;
2017-03-18 23:17:13 +03:00
# if WITH_BHYVE
unsigned int bhyve_caps = 0 ;
# endif
2016-04-25 15:20:58 +03:00
# if WITH_QEMU
2020-09-04 09:59:08 +03:00
g_autoptr ( virQEMUDriverConfig ) cfg = virQEMUDriverConfigNew ( false , NULL ) ;
2014-09-17 03:52:54 +04:00
2015-01-26 19:09:36 +03:00
if ( ! cfg )
return EXIT_FAILURE ;
2016-04-25 15:20:58 +03:00
# endif
2017-11-03 15:09:47 +03:00
# define DO_TEST(Name, Emulator, Machine, Arch, Type, CapsType) \
do { \
struct testData data = { \
. name = Name , \
. emulator = Emulator , \
. machine = Machine , \
. arch = Arch , \
. type = Type , \
. capsType = CapsType , \
} ; \
if ( virTestRun ( Name , test_virDomainCapsFormat , & data ) < 0 ) \
ret = - 1 ; \
2016-04-25 15:20:58 +03:00
} while ( 0 )
2015-01-26 19:09:36 +03:00
2017-11-03 15:09:47 +03:00
# define DO_TEST_LIBXL(Name, Emulator, Machine, Arch, Type) \
do { \
struct testData data = { \
. name = Name , \
. emulator = Emulator , \
. machine = Machine , \
. arch = Arch , \
. type = Type , \
. capsType = CAPS_LIBXL , \
} ; \
if ( virTestRun ( Name , test_virDomainCapsFormat , & data ) < 0 ) \
ret = - 1 ; \
2016-05-17 00:21:59 +03:00
} while ( 0 )
2017-03-18 23:17:13 +03:00
# define DO_TEST_BHYVE(Name, Emulator, BhyveCaps, Type) \
2017-11-03 15:09:47 +03:00
do { \
char * name = NULL ; \
2019-10-22 16:26:14 +03:00
name = g_strdup_printf ( " bhyve_%s.x86_64 " , Name ) ; \
2017-11-03 15:09:47 +03:00
struct testData data = { \
. name = name , \
. emulator = Emulator , \
. arch = " x86_64 " , \
. type = Type , \
. capsType = CAPS_BHYVE , \
. capsOpaque = BhyveCaps , \
} ; \
if ( virTestRun ( name , test_virDomainCapsFormat , & data ) < 0 ) \
ret = - 1 ; \
VIR_FREE ( name ) ; \
2017-03-18 23:17:13 +03:00
} while ( 0 )
2019-02-19 20:44:34 +03:00
DO_TEST ( " empty " , " /bin/emulatorbin " , " my-machine-type " ,
" x86_64 " , VIR_DOMAIN_VIRT_KVM , CAPS_NONE ) ;
2016-04-25 15:20:58 +03:00
# if WITH_QEMU
2019-04-04 13:42:14 +03:00
virFileWrapperAddPrefix ( SYSCONFDIR " /qemu/firmware " ,
abs_srcdir " /qemufirmwaredata/etc/qemu/firmware " ) ;
virFileWrapperAddPrefix ( PREFIX " /share/qemu/firmware " ,
abs_srcdir " /qemufirmwaredata/usr/share/qemu/firmware " ) ;
virFileWrapperAddPrefix ( " /home/user/.config/qemu/firmware " ,
abs_srcdir " /qemufirmwaredata/home/user/.config/qemu/firmware " ) ;
2019-10-22 18:31:54 +03:00
if ( testQemuCapsIterate ( " .xml " , doTestQemu , cfg ) < 0 )
2019-10-25 14:59:46 +03:00
ret = - 1 ;
2018-03-23 15:38:32 +03:00
2019-10-22 18:31:54 +03:00
/*
* Run " tests/qemucapsprobe /path/to/qemu/binary >foo.replies "
* to generate updated or new * . replies data files .
*
* If you manually edit replies files you can run
* " tests/qemucapsfixreplies foo.replies " to fix the replies ids .
*
* Once a replies file has been generated and tweaked if necessary ,
* you can drop it into tests / qemucapabilitiesdata / ( with a sensible
* name - look at what ' s already there for inspiration ) and test
* programs will automatically pick it up .
*
* To generate the corresponding output files after a new replies
2020-08-06 16:34:53 +03:00
* file has been added , run " VIR_TEST_REGENERATE_OUTPUT=1 ninja test " .
2019-10-22 18:31:54 +03:00
*/
2019-10-10 16:38:37 +03:00
2019-04-10 16:08:46 +03:00
virFileWrapperClearPrefixes ( ) ;
2019-04-04 13:42:14 +03:00
2014-06-25 20:39:29 +04:00
# endif /* WITH_QEMU */
2016-05-17 00:21:59 +03:00
# if WITH_LIBXL
2019-02-08 20:00:22 +03:00
DO_TEST_LIBXL ( " libxl-xenpv " , " /usr/bin/qemu-system-x86_64 " ,
2016-05-17 00:21:59 +03:00
" xenpv " , " x86_64 " , VIR_DOMAIN_VIRT_XEN ) ;
2019-02-08 20:00:22 +03:00
DO_TEST_LIBXL ( " libxl-xenfv " , " /usr/bin/qemu-system-x86_64 " ,
2016-05-17 00:21:59 +03:00
" xenfv " , " x86_64 " , VIR_DOMAIN_VIRT_XEN ) ;
# endif /* WITH_LIBXL */
2017-03-18 23:17:13 +03:00
# if WITH_BHYVE
DO_TEST_BHYVE ( " basic " , " /usr/sbin/bhyve " , & bhyve_caps , VIR_DOMAIN_VIRT_BHYVE ) ;
bhyve_caps | = BHYVE_CAP_LPC_BOOTROM ;
DO_TEST_BHYVE ( " uefi " , " /usr/sbin/bhyve " , & bhyve_caps , VIR_DOMAIN_VIRT_BHYVE ) ;
bhyve_caps | = BHYVE_CAP_FBUF ;
DO_TEST_BHYVE ( " fbuf " , " /usr/sbin/bhyve " , & bhyve_caps , VIR_DOMAIN_VIRT_BHYVE ) ;
# endif /* WITH_BHYVE */
2014-06-25 15:24:53 +04:00
return ret ;
}
2017-03-08 15:32:46 +03:00
# if WITH_QEMU
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain ,
2019-08-21 19:13:16 +03:00
VIR_TEST_MOCK ( " domaincaps " ) ,
VIR_TEST_MOCK ( " qemucpu " ) )
2017-03-08 15:32:46 +03:00
# else
2019-08-21 19:13:16 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " domaincaps " ) )
2017-03-08 15:32:46 +03:00
# endif