2014-01-23 12:17:05 +04:00
/*
* Copyright ( C ) 2014 Red Hat , Inc .
*
* 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"
2018-12-13 17:53:50 +03:00
# define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
2014-03-11 14:59:58 +04:00
# include "vircommandpriv.h"
2014-01-23 12:17:05 +04:00
# include "virnetdevbandwidth.h"
2022-06-28 11:02:01 +03:00
# include "virnetdevopenvswitch.h"
2014-01-23 12:17:05 +04:00
# include "netdev_bandwidth_conf.c"
# define VIR_FROM_THIS VIR_FROM_NONE
2014-01-27 17:53:00 +04:00
struct testSetStruct {
const char * band ;
2022-06-28 11:02:01 +03:00
const char * exp_cmd_tc ;
const char * exp_cmd_ovs ;
bool ovs ;
const unsigned char * uuid ;
2014-01-27 17:53:00 +04:00
const char * iface ;
const bool hierarchical_class ;
} ;
2021-08-20 16:08:34 +03:00
static int
testVirNetDevBandwidthParse ( virNetDevBandwidth * * var ,
const char * xml )
{
g_autoptr ( xmlDoc ) doc = NULL ;
g_autoptr ( xmlXPathContext ) ctxt = NULL ;
if ( ! xml )
return 0 ;
if ( ! ( doc = virXMLParseStringCtxt ( ( xml ) ,
" bandwidth definition " ,
& ctxt ) ) )
return - 1 ;
return virNetDevBandwidthParse ( var ,
NULL ,
ctxt - > node ,
true ) ;
}
2014-01-23 12:17:05 +04:00
2014-01-27 17:53:00 +04:00
static int
testVirNetDevBandwidthSet ( const void * data )
{
const struct testSetStruct * info = data ;
const char * iface = info - > iface ;
2022-06-28 11:02:01 +03:00
const char * exp_cmd = NULL ;
2021-03-03 13:48:19 +03:00
g_autoptr ( virNetDevBandwidth ) band = NULL ;
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2021-08-20 15:58:13 +03:00
g_autofree char * actual_cmd = NULL ;
2021-04-01 18:54:09 +03:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2014-01-27 17:53:00 +04:00
2021-08-20 16:08:34 +03:00
if ( testVirNetDevBandwidthParse ( & band , info - > band ) < 0 )
return - 1 ;
2014-01-27 17:53:00 +04:00
if ( ! iface )
iface = " eth0 " ;
2021-04-06 11:56:23 +03:00
virCommandSetDryRun ( dryRunToken , & buf , false , false , NULL , NULL ) ;
2014-01-27 17:53:00 +04:00
2022-06-28 11:02:01 +03:00
if ( info - > ovs ) {
exp_cmd = info - > exp_cmd_ovs ;
if ( virNetDevOpenvswitchInterfaceSetQos ( iface , band , info - > uuid , true ) < 0 )
return - 1 ;
} else {
exp_cmd = info - > exp_cmd_tc ;
if ( virNetDevBandwidthSet ( iface , band , info - > hierarchical_class , true ) < 0 )
return - 1 ;
}
2014-01-27 17:53:00 +04:00
if ( ! ( actual_cmd = virBufferContentAndReset ( & buf ) ) ) {
/* This is interesting, no command has been executed.
* Maybe that ' s expected , actually . */
}
tests: Use virTestCompareToString() more
Instead of using:
if (STRNEQ(a, b)) {
virTestDifference(stderr, a, b);
...
}
we can use:
if (virTestCompareToString(a, b) < ) {
...
}
Generated by the following spatch:
@@
expression a, b;
@@
- if (STRNEQ(a, b)) {
+ if (virTestCompareToString(a, b) < 0) {
...
- virTestDifference(stderr, a, b);
...
}
and its variations (STRNEQ_NULLABLE() instead of STRNEQ(), then
in some cases variables passed to STRNEQ() are in reversed order
when compared to virTestCompareToString()).
However, coccinelle failed to recognize the pattern in
testNWFilterEBIPTablesAllTeardown() so I had to fix it manually.
Also, I manually fixed testFormat() in tests/sockettest.c as I
didn't bother writing another spatch rule just for that.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
2022-11-30 11:57:49 +03:00
if ( virTestCompareToString ( exp_cmd , actual_cmd ) < 0 ) {
2021-08-20 16:11:52 +03:00
return - 1 ;
2014-01-27 17:53:00 +04:00
}
2021-08-20 16:11:52 +03:00
return 0 ;
2014-01-27 17:53:00 +04:00
}
2014-01-23 12:17:05 +04:00
static int
mymain ( void )
{
int ret = 0 ;
2022-06-28 11:02:01 +03:00
unsigned char uuid [ VIR_UUID_BUFLEN ] = { 0 } ;
# define VMUUID "c1018351-a229-4209-9faf-42446e0b53e5"
if ( virUUIDParse ( VMUUID , uuid ) < 0 )
return - 1 ;
2014-01-23 12:17:05 +04:00
2022-06-28 11:02:01 +03:00
# define DO_TEST_SET(Band, Exp_cmd_tc, Exp_cmd_ovs, ...) \
2017-11-03 15:09:47 +03:00
do { \
struct testSetStruct data = { . band = Band , \
2022-06-28 11:02:01 +03:00
. exp_cmd_tc = Exp_cmd_tc , \
. exp_cmd_ovs = Exp_cmd_ovs , \
. ovs = false , \
. uuid = uuid , \
2017-11-03 15:09:47 +03:00
__VA_ARGS__ } ; \
2022-06-28 11:02:01 +03:00
if ( virTestRun ( " virNetDevBandwidthSet TC " , \
testVirNetDevBandwidthSet , \
& data ) < 0 ) { \
ret = - 1 ; \
} \
data . ovs = true ; \
if ( virTestRun ( " virNetDevBandwidthSet OVS " , \
2017-11-03 15:09:47 +03:00
testVirNetDevBandwidthSet , \
2022-06-28 11:02:01 +03:00
& data ) < 0 ) { \
2017-11-03 15:09:47 +03:00
ret = - 1 ; \
2022-06-28 11:02:01 +03:00
} \
2014-01-27 17:53:00 +04:00
} while ( 0 )
2022-06-28 11:02:01 +03:00
DO_TEST_SET ( NULL , NULL , NULL ) ;
2014-01-23 12:17:05 +04:00
2022-06-28 11:02:01 +03:00
DO_TEST_SET ( " <bandwidth/> " , NULL , NULL ) ;
2014-01-31 18:04:03 +04:00
2022-06-28 09:41:43 +03:00
DO_TEST_SET ( " <bandwidth> "
" <inbound average='1024'/> "
" </bandwidth> " ,
TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
TC " class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps quantum 87 \n "
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
2022-06-28 11:02:01 +03:00
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n " ,
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find queue 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find qos 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set port eth0 qos=@qos1 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@qos1 create qos type=linux-htb other_config:min-rate=8192000 queues:0=@queue0 'external-ids:vm-id= \" " VMUUID " \" ' "
" 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@queue0 create queue other_config:min-rate=8192000 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set Interface eth0 ingress_policing_rate=0 ingress_policing_burst=0 \n " ) ;
2022-06-28 09:41:43 +03:00
DO_TEST_SET ( " <bandwidth> "
" <outbound average='1024'/> "
" </bandwidth> " ,
TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 ingress \n "
2022-06-28 11:01:54 +03:00
TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
2022-06-28 11:02:01 +03:00
" police rate 1024kbps burst 1024kb mtu 64kb drop flowid :1 \n " ,
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find queue 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find qos 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set Interface eth0 ingress_policing_rate=8192 \n " ) ;
2022-06-28 09:41:43 +03:00
DO_TEST_SET ( " <bandwidth> "
" <inbound average='1' peak='2' floor='3' burst='4'/> "
" <outbound average='5' peak='6' burst='7'/> "
" </bandwidth> " ,
TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
TC " class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb quantum 1 \n "
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n "
TC " qdisc add dev eth0 ingress \n "
2022-06-28 11:01:54 +03:00
TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
2022-06-28 11:02:01 +03:00
" police rate 5kbps burst 7kb mtu 64kb drop flowid :1 \n " ,
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find queue 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find qos 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set port eth0 qos=@qos1 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@qos1 create qos type=linux-htb other_config:min-rate=8000 other_config:burst=32768 other_config:max-rate=16000 "
" queues:0=@queue0 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@queue0 create queue other_config:min-rate=8000 other_config:burst=32768 other_config:max-rate=16000 "
" 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set Interface eth0 ingress_policing_rate=40 ingress_policing_burst=56 \n " ) ;
2022-06-28 09:41:43 +03:00
DO_TEST_SET ( " <bandwidth> "
" <inbound average='4294967295'/> "
" <outbound average='4294967295'/> "
" </bandwidth> " ,
TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
TC " class add dev eth0 parent 1: classid 1:1 htb rate 4294967295kbps quantum 366503875 \n "
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n "
TC " qdisc add dev eth0 ingress \n "
2022-06-28 11:01:54 +03:00
TC " filter add dev eth0 parent ffff: protocol all u32 match "
" u32 0 0 police rate 4294967295kbps burst 4194303kb mtu 64kb "
2022-06-28 11:02:01 +03:00
" drop flowid :1 \n " ,
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find queue 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 --no-heading --columns=_uuid find qos 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set port eth0 qos=@qos1 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@qos1 create qos type=linux-htb other_config:min-rate=34359738360000 "
" queues:0=@queue0 'external-ids:vm-id= \" " VMUUID " \" ' 'external-ids:ifname= \" eth0 \" ' -- "
" --id=@queue0 create queue other_config:min-rate=34359738360000 'external-ids:vm-id= \" " VMUUID " \" ' "
" 'external-ids:ifname= \" eth0 \" ' \n "
OVS_VSCTL " --timeout=5 set Interface eth0 ingress_policing_rate=34359738360 \n " ) ;
2021-03-05 12:13:36 +03:00
2021-03-04 20:52:32 +03:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2014-01-23 12:17:05 +04:00
}
2019-08-21 19:13:16 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " virnetdevbandwidth " ) )