2013-10-23 17:44:40 +04:00
/*
2014-03-17 13:38:38 +04:00
* Copyright ( C ) 2013 , 2014 Red Hat , Inc .
2013-10-23 17:44:40 +04:00
*
* 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>
2021-10-20 11:30:32 +03:00
# include "internal.h"
2013-10-23 17:44:40 +04:00
# include "testutils.h"
# ifdef __linux__
# include <sys / types.h>
# include <sys / stat.h>
# include <fcntl.h>
# include <virpci.h>
2021-10-20 11:30:32 +03:00
# include <virpcivpd.h>
2013-10-23 17:44:40 +04:00
# define VIR_FROM_THIS VIR_FROM_NONE
2014-01-15 13:20:55 +04:00
static int
2021-03-11 10:16:13 +03:00
testVirPCIDeviceCheckDriver ( virPCIDevice * dev , const char * expected )
2014-01-15 13:20:55 +04:00
{
2021-09-04 23:37:31 +03:00
g_autofree char * path = NULL ;
g_autofree char * driver = NULL ;
2014-01-15 13:20:55 +04:00
if ( virPCIDeviceGetDriverPathAndName ( dev , & path , & driver ) < 0 )
2021-09-04 23:41:36 +03:00
return - 1 ;
2014-01-15 13:20:55 +04:00
if ( STRNEQ_NULLABLE ( driver , expected ) ) {
virReportError ( VIR_ERR_INTERNAL_ERROR ,
" PCI device %s driver mismatch: %s, expecting %s " ,
virPCIDeviceGetName ( dev ) , NULLSTR ( driver ) ,
NULLSTR ( expected ) ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2014-01-15 13:20:55 +04:00
}
2021-09-04 23:41:36 +03:00
return 0 ;
2014-01-15 13:20:55 +04:00
}
2013-10-23 17:44:40 +04:00
static int
2019-10-14 15:45:03 +03:00
testVirPCIDeviceNew ( const void * opaque G_GNUC_UNUSED )
2013-10-23 17:44:40 +04:00
{
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
2013-10-23 17:44:40 +04:00
const char * devName ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = 0 , . bus = 0 , . slot = 0 , . function = 0 } ;
2013-10-23 17:44:40 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
if ( ! ( dev = virPCIDeviceNew ( & devAddr ) ) )
2013-10-23 17:44:40 +04:00
goto cleanup ;
devName = virPCIDeviceGetName ( dev ) ;
if ( STRNEQ ( devName , " 0000:00:00.0 " ) ) {
virReportError ( VIR_ERR_INTERNAL_ERROR ,
" PCI device name mismatch: %s, expecting %s " ,
devName , " 0000:00:00.0 " ) ;
goto cleanup ;
}
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-10-23 17:44:40 +04:00
virPCIDeviceFree ( dev ) ;
return ret ;
}
2017-11-03 15:09:47 +03:00
# define CHECK_LIST_COUNT(list, cnt) \
if ( ( count = virPCIDeviceListCount ( list ) ) ! = cnt ) { \
virReportError ( VIR_ERR_INTERNAL_ERROR , \
" Unexpected count of items in " # list " : %d, " \
" expecting %zu " , count , ( size_t ) cnt ) ; \
goto cleanup ; \
2013-10-23 19:55:02 +04:00
}
static int
2019-10-14 15:45:03 +03:00
testVirPCIDeviceDetach ( const void * opaque G_GNUC_UNUSED )
2013-10-23 19:55:02 +04:00
{
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev [ ] = { NULL , NULL , NULL } ;
2019-10-15 14:55:26 +03:00
size_t i , nDev = G_N_ELEMENTS ( dev ) ;
2021-08-20 16:53:53 +03:00
g_autoptr ( virPCIDeviceList ) activeDevs = NULL ;
g_autoptr ( virPCIDeviceList ) inactiveDevs = NULL ;
2013-10-23 19:55:02 +04:00
int count ;
2013-11-05 18:07:49 +04:00
if ( ! ( activeDevs = virPCIDeviceListNew ( ) ) | |
2013-10-23 19:55:02 +04:00
! ( inactiveDevs = virPCIDeviceListNew ( ) ) )
goto cleanup ;
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
CHECK_LIST_COUNT ( inactiveDevs , 0 ) ;
2013-11-05 18:07:49 +04:00
for ( i = 0 ; i < nDev ; i + + ) {
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = 0 , . bus = 0 ,
. slot = i + 1 , . function = 0 } ;
if ( ! ( dev [ i ] = virPCIDeviceNew ( & devAddr ) ) )
2013-11-05 18:07:49 +04:00
goto cleanup ;
2013-10-23 19:55:02 +04:00
2019-08-14 13:09:47 +03:00
virPCIDeviceSetStubDriver ( dev [ i ] , VIR_PCI_STUB_DRIVER_VFIO ) ;
2015-10-23 12:54:07 +03:00
2013-11-05 18:07:49 +04:00
if ( virPCIDeviceDetach ( dev [ i ] , activeDevs , inactiveDevs ) < 0 )
goto cleanup ;
2013-10-23 19:55:02 +04:00
2019-08-14 13:09:47 +03:00
if ( testVirPCIDeviceCheckDriver ( dev [ i ] , " vfio-pci " ) < 0 )
2014-01-15 13:20:55 +04:00
goto cleanup ;
2013-11-05 18:07:49 +04:00
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
CHECK_LIST_COUNT ( inactiveDevs , i + 1 ) ;
}
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-11-05 18:07:49 +04:00
for ( i = 0 ; i < nDev ; i + + )
virPCIDeviceFree ( dev [ i ] ) ;
return ret ;
}
2013-10-23 19:55:02 +04:00
2013-11-05 18:07:49 +04:00
static int
2019-10-14 15:45:03 +03:00
testVirPCIDeviceReset ( const void * opaque G_GNUC_UNUSED )
2013-11-05 18:07:49 +04:00
{
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev [ ] = { NULL , NULL , NULL } ;
2019-10-15 14:55:26 +03:00
size_t i , nDev = G_N_ELEMENTS ( dev ) ;
2021-08-20 16:53:53 +03:00
g_autoptr ( virPCIDeviceList ) activeDevs = NULL ;
g_autoptr ( virPCIDeviceList ) inactiveDevs = NULL ;
2013-11-05 18:07:49 +04:00
int count ;
if ( ! ( activeDevs = virPCIDeviceListNew ( ) ) | |
! ( inactiveDevs = virPCIDeviceListNew ( ) ) )
2013-10-31 15:05:20 +04:00
goto cleanup ;
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
2013-11-05 18:07:49 +04:00
CHECK_LIST_COUNT ( inactiveDevs , 0 ) ;
for ( i = 0 ; i < nDev ; i + + ) {
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = 0 , . bus = 0 ,
. slot = i + 1 , . function = 0 } ;
if ( ! ( dev [ i ] = virPCIDeviceNew ( & devAddr ) ) )
2013-11-05 18:07:49 +04:00
goto cleanup ;
2019-08-14 13:09:47 +03:00
virPCIDeviceSetStubDriver ( dev [ i ] , VIR_PCI_STUB_DRIVER_VFIO ) ;
2015-10-23 12:54:07 +03:00
2013-11-05 18:07:49 +04:00
if ( virPCIDeviceReset ( dev [ i ] , activeDevs , inactiveDevs ) < 0 )
goto cleanup ;
}
2013-10-31 15:05:20 +04:00
2013-10-23 19:55:02 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-11-05 18:07:49 +04:00
for ( i = 0 ; i < nDev ; i + + )
virPCIDeviceFree ( dev [ i ] ) ;
2013-10-23 19:55:02 +04:00
return ret ;
}
2013-10-31 14:57:56 +04:00
static int
2019-10-14 15:45:03 +03:00
testVirPCIDeviceReattach ( const void * opaque G_GNUC_UNUSED )
2013-10-31 14:57:56 +04:00
{
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev [ ] = { NULL , NULL , NULL } ;
2019-10-15 14:55:26 +03:00
size_t i , nDev = G_N_ELEMENTS ( dev ) ;
2021-08-20 16:53:53 +03:00
g_autoptr ( virPCIDeviceList ) activeDevs = NULL ;
g_autoptr ( virPCIDeviceList ) inactiveDevs = NULL ;
2013-10-31 14:57:56 +04:00
int count ;
2013-11-05 18:07:49 +04:00
if ( ! ( activeDevs = virPCIDeviceListNew ( ) ) | |
2013-10-31 14:57:56 +04:00
! ( inactiveDevs = virPCIDeviceListNew ( ) ) )
goto cleanup ;
2013-11-05 18:07:49 +04:00
for ( i = 0 ; i < nDev ; i + + ) {
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = 0 , . bus = 0 ,
. slot = i + 1 , . function = 0 } ;
if ( ! ( dev [ i ] = virPCIDeviceNew ( & devAddr ) ) )
2013-11-05 18:07:49 +04:00
goto cleanup ;
2013-10-31 14:57:56 +04:00
2013-11-05 18:07:49 +04:00
if ( virPCIDeviceListAdd ( inactiveDevs , dev [ i ] ) < 0 ) {
virPCIDeviceFree ( dev [ i ] ) ;
goto cleanup ;
}
2013-10-31 14:57:56 +04:00
2013-11-05 18:07:49 +04:00
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
CHECK_LIST_COUNT ( inactiveDevs , i + 1 ) ;
2013-10-31 14:57:56 +04:00
2019-08-14 13:09:47 +03:00
virPCIDeviceSetStubDriver ( dev [ i ] , VIR_PCI_STUB_DRIVER_VFIO ) ;
2013-11-05 18:07:49 +04:00
}
2013-10-31 14:57:56 +04:00
2013-10-31 15:05:20 +04:00
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
2013-11-05 18:07:49 +04:00
CHECK_LIST_COUNT ( inactiveDevs , nDev ) ;
2013-10-31 15:05:20 +04:00
2013-11-05 18:07:49 +04:00
for ( i = 0 ; i < nDev ; i + + ) {
if ( virPCIDeviceReattach ( dev [ i ] , activeDevs , inactiveDevs ) < 0 )
goto cleanup ;
2013-10-31 15:05:20 +04:00
2013-11-05 18:07:49 +04:00
CHECK_LIST_COUNT ( activeDevs , 0 ) ;
CHECK_LIST_COUNT ( inactiveDevs , nDev - i - 1 ) ;
}
2013-10-31 14:57:56 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2013-10-31 14:57:56 +04:00
return ret ;
}
2013-11-05 18:07:49 +04:00
2013-12-24 22:07:27 +04:00
struct testPCIDevData {
unsigned int domain ;
unsigned int bus ;
unsigned int slot ;
unsigned int function ;
2014-01-15 13:20:55 +04:00
const char * driver ;
2013-12-24 22:07:27 +04:00
} ;
static int
testVirPCIDeviceIsAssignable ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
2013-12-24 22:07:27 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
if ( ! ( dev = virPCIDeviceNew ( & devAddr ) ) )
2019-11-12 23:46:29 +03:00
return - 1 ;
2013-12-24 22:07:27 +04:00
if ( virPCIDeviceIsAssignable ( dev , true ) )
ret = 0 ;
virPCIDeviceFree ( dev ) ;
return ret ;
}
2014-01-15 13:20:55 +04:00
static int
testVirPCIDeviceDetachSingle ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
2014-01-15 13:20:55 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
dev = virPCIDeviceNew ( & devAddr ) ;
2014-01-15 13:20:55 +04:00
if ( ! dev )
goto cleanup ;
2019-08-14 13:09:47 +03:00
virPCIDeviceSetStubDriver ( dev , VIR_PCI_STUB_DRIVER_VFIO ) ;
2015-10-23 12:54:07 +03:00
if ( virPCIDeviceDetach ( dev , NULL , NULL ) < 0 )
2014-01-15 13:20:55 +04:00
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2014-01-15 13:20:55 +04:00
virPCIDeviceFree ( dev ) ;
return ret ;
}
static int
testVirPCIDeviceReattachSingle ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
2014-01-15 13:20:55 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
dev = virPCIDeviceNew ( & devAddr ) ;
2014-01-15 13:20:55 +04:00
if ( ! dev )
goto cleanup ;
2016-01-19 17:58:31 +03:00
virPCIDeviceSetUnbindFromStub ( dev , true ) ;
virPCIDeviceSetRemoveSlot ( dev , true ) ;
virPCIDeviceSetReprobe ( dev , true ) ;
2014-01-15 13:20:55 +04:00
if ( virPCIDeviceReattach ( dev , NULL , NULL ) < 0 )
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2014-01-15 13:20:55 +04:00
virPCIDeviceFree ( dev ) ;
return ret ;
}
static int
testVirPCIDeviceCheckDriverTest ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
2014-01-15 13:20:55 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
dev = virPCIDeviceNew ( & devAddr ) ;
2014-01-15 13:20:55 +04:00
if ( ! dev )
goto cleanup ;
if ( testVirPCIDeviceCheckDriver ( dev , data - > driver ) < 0 )
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2014-01-15 13:20:55 +04:00
virPCIDeviceFree ( dev ) ;
return ret ;
}
static int
testVirPCIDeviceUnbind ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virPCIDevice * dev ;
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
2014-01-15 13:20:55 +04:00
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 15:54:28 +03:00
dev = virPCIDeviceNew ( & devAddr ) ;
2014-01-15 13:20:55 +04:00
if ( ! dev )
goto cleanup ;
2015-10-29 18:09:54 +03:00
if ( virPCIDeviceUnbind ( dev ) < 0 )
2014-01-15 13:20:55 +04:00
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2014-01-15 13:20:55 +04:00
virPCIDeviceFree ( dev ) ;
return ret ;
}
2021-10-20 11:30:32 +03:00
static int
testVirPCIDeviceGetVPD ( const void * opaque )
{
const struct testPCIDevData * data = opaque ;
g_autoptr ( virPCIDevice ) dev = NULL ;
virPCIDeviceAddress devAddr = { . domain = data - > domain , . bus = data - > bus ,
. slot = data - > slot , . function = data - > function } ;
g_autoptr ( virPCIVPDResource ) res = NULL ;
dev = virPCIDeviceNew ( & devAddr ) ;
if ( ! dev )
return - 1 ;
res = virPCIDeviceGetVPD ( dev ) ;
/* Only basic checks - full parser validation is done elsewhere. */
if ( res - > ro = = NULL )
return - 1 ;
if ( STRNEQ ( res - > name , " testname " ) ) {
VIR_TEST_DEBUG ( " Unexpected name present in VPD: %s " , res - > name ) ;
return - 1 ;
}
if ( STRNEQ ( res - > ro - > part_number , " 42 " ) ) {
VIR_TEST_DEBUG ( " Unexpected part number value present in VPD: %s " , res - > ro - > part_number ) ;
return - 1 ;
}
return 0 ;
}
2015-12-04 15:38:16 +03:00
# define FAKEROOTDIRTEMPLATE abs_builddir " / fakerootdir-XXXXXX"
2013-11-05 18:07:49 +04:00
2013-10-23 17:44:40 +04:00
static int
mymain ( void )
{
int ret = 0 ;
2021-09-04 23:37:31 +03:00
g_autofree char * fakerootdir = NULL ;
2013-10-23 17:44:40 +04:00
2019-10-20 14:49:46 +03:00
fakerootdir = g_strdup ( FAKEROOTDIRTEMPLATE ) ;
2013-10-23 17:44:40 +04:00
2019-11-14 00:35:47 +03:00
if ( ! g_mkdtemp ( fakerootdir ) ) {
2015-12-04 15:38:16 +03:00
VIR_TEST_DEBUG ( " Cannot create fakerootdir " ) ;
2013-10-23 17:44:40 +04:00
abort ( ) ;
}
2019-12-18 20:16:19 +03:00
g_setenv ( " LIBVIRT_FAKE_ROOT_DIR " , fakerootdir , TRUE ) ;
2013-10-23 17:44:40 +04:00
2017-11-03 15:09:47 +03:00
# define DO_TEST(fnc) \
do { \
if ( virTestRun ( # fnc , fnc , NULL ) < 0 ) \
ret = - 1 ; \
2013-10-23 17:44:40 +04:00
} while ( 0 )
2017-11-03 15:09:47 +03:00
# define DO_TEST_PCI(fnc, domain, bus, slot, function) \
do { \
struct testPCIDevData data = { \
domain , bus , slot , function , NULL \
} ; \
2021-09-04 23:37:31 +03:00
g_autofree char * label = NULL ; \
2019-10-22 16:26:14 +03:00
label = g_strdup_printf ( " %s(%04x:%02x:%02x.%x) " , \
# fnc, domain, bus, slot, function); \
2017-11-03 15:09:47 +03:00
if ( virTestRun ( label , fnc , & data ) < 0 ) \
ret = - 1 ; \
2013-12-24 22:07:27 +04:00
} while ( 0 )
2017-11-03 15:09:47 +03:00
# define DO_TEST_PCI_DRIVER(domain, bus, slot, function, driver) \
do { \
struct testPCIDevData data = { \
domain , bus , slot , function , driver \
} ; \
2021-09-04 23:37:31 +03:00
g_autofree char * label = NULL ; \
2019-10-22 16:26:14 +03:00
label = g_strdup_printf ( " PCI driver %04x:%02x:%02x.%x is %s " , \
domain , bus , slot , function , \
NULLSTR ( driver ) ) ; \
2017-11-03 15:09:47 +03:00
if ( virTestRun ( label , testVirPCIDeviceCheckDriverTest , \
& data ) < 0 ) \
ret = - 1 ; \
2014-01-15 13:20:55 +04:00
} while ( 0 )
/* Changes made to individual devices are persistent and the
* tests often rely on the state set by previous tests .
*/
2013-10-23 17:44:40 +04:00
DO_TEST ( testVirPCIDeviceNew ) ;
2013-10-23 19:55:02 +04:00
DO_TEST ( testVirPCIDeviceDetach ) ;
2013-11-05 18:07:49 +04:00
DO_TEST ( testVirPCIDeviceReset ) ;
2013-10-31 14:57:56 +04:00
DO_TEST ( testVirPCIDeviceReattach ) ;
2013-12-24 22:07:27 +04:00
DO_TEST_PCI ( testVirPCIDeviceIsAssignable , 5 , 0x90 , 1 , 0 ) ;
DO_TEST_PCI ( testVirPCIDeviceIsAssignable , 1 , 1 , 0 , 0 ) ;
2013-10-23 17:44:40 +04:00
2014-01-15 13:20:55 +04:00
/* Reattach a device already bound to non-stub a driver */
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 1 , 0 , " i915 " ) ;
DO_TEST_PCI ( testVirPCIDeviceReattachSingle , 0 , 0x0a , 1 , 0 ) ;
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 1 , 0 , " i915 " ) ;
/* Reattach an unbound device */
DO_TEST_PCI ( testVirPCIDeviceUnbind , 0 , 0x0a , 1 , 0 ) ;
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 1 , 0 , NULL ) ;
DO_TEST_PCI ( testVirPCIDeviceReattachSingle , 0 , 0x0a , 1 , 0 ) ;
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 1 , 0 , " i915 " ) ;
/* Detach an unbound device */
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 2 , 0 , NULL ) ;
DO_TEST_PCI ( testVirPCIDeviceDetachSingle , 0 , 0x0a , 2 , 0 ) ;
2019-08-14 13:09:47 +03:00
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 2 , 0 , " vfio-pci " ) ;
2014-01-15 13:20:55 +04:00
/* Reattach an unknown unbound device */
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 3 , 0 , NULL ) ;
DO_TEST_PCI ( testVirPCIDeviceReattachSingle , 0 , 0x0a , 3 , 0 ) ;
DO_TEST_PCI_DRIVER ( 0 , 0x0a , 3 , 0 , NULL ) ;
2021-10-20 11:30:32 +03:00
DO_TEST_PCI ( testVirPCIDeviceGetVPD , 0 , 0x03 , 0 , 0 ) ;
2013-10-23 17:44:40 +04:00
if ( getenv ( " LIBVIRT_SKIP_CLEANUP " ) = = NULL )
2015-12-04 15:38:16 +03:00
virFileDeleteTree ( fakerootdir ) ;
2013-10-23 17:44:40 +04:00
2014-03-17 13:38:38 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2013-10-23 17:44:40 +04:00
}
2019-08-21 19:13:16 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " virpci " ) )
2013-10-23 17:44:40 +04:00
# else
int
main ( void )
{
return EXIT_AM_SKIP ;
}
# endif