internal.h: Introduce and use VIR_IS_POW2()

This macro checks whether given number is an integer power of
two. At the same time, I've identified two places where we check
for pow2 and I'm replacing them with the macro.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Han Han <hhan@redhat.com>
This commit is contained in:
Michal Privoznik 2020-11-04 19:41:27 +01:00
parent 32217bb709
commit 7fd8e49ef1
3 changed files with 35 additions and 23 deletions

View File

@ -237,6 +237,16 @@
(a) = (a) ^ (b); \
} while (0)
/**
* VIR_IS_POW2:
*
* Returns true if given number is a power of two
*/
#define VIR_IS_POW2(x) \
((x) && !((x) & ((x) - 1)))
/**
* virCheckFlags:
* @supported: an OR'ed set of supported flags

View File

@ -1460,20 +1460,32 @@ qemuValidateDomainDeviceDefNetwork(const virDomainNetDef *net,
return -1;
}
if (net->driver.virtio.rx_queue_size &&
!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_RX_QUEUE_SIZE)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("virtio rx_queue_size option is not supported "
"with this QEMU binary"));
return -1;
if (net->driver.virtio.rx_queue_size) {
if (!VIR_IS_POW2(net->driver.virtio.rx_queue_size)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("rx_queue_size has to be a power of two"));
return -1;
}
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_RX_QUEUE_SIZE)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("virtio rx_queue_size option is not supported "
"with this QEMU binary"));
return -1;
}
}
if (net->driver.virtio.tx_queue_size &&
!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_TX_QUEUE_SIZE)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("virtio tx_queue_size option is not supported "
"with this QEMU binary"));
return -1;
if (net->driver.virtio.tx_queue_size) {
if (!VIR_IS_POW2(net->driver.virtio.tx_queue_size)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("tx_queue_size has to be a power of two"));
return -1;
}
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_TX_QUEUE_SIZE)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("virtio tx_queue_size option is not supported "
"with this QEMU binary"));
return -1;
}
}
if (net->mtu &&
@ -1484,16 +1496,6 @@ qemuValidateDomainDeviceDefNetwork(const virDomainNetDef *net,
return -1;
}
if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("rx_queue_size has to be a power of two"));
return -1;
}
if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("tx_queue_size has to be a power of two"));
return -1;
}
if (qemuValidateDomainVirtioOptions(net->virtio, qemuCaps) < 0)
return -1;
}

View File

@ -89,7 +89,7 @@ double virRandom(void)
*/
uint32_t virRandomInt(uint32_t max)
{
if ((max & (max - 1)) == 0)
if (VIR_IS_POW2(max))
return virRandomBits(__builtin_ffs(max) - 1);
return virRandom() * max;