Commit Graph

3500 Commits

Author SHA1 Message Date
Martin Kletzander
d300b54487 util: Check if kernel-provided info is consistent with itself
Just in case someone re-mounted /sys/fs/resctrl with different mount
options (cdp), add a check here.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540780

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-02-05 13:33:13 +01:00
John Ferlan
39b933f197 util: Remove unnecessary initialization
VIR_ALLOC will already initialize, so no need to do it again.
2018-02-02 14:56:19 -05:00
Martin Kletzander
d6e582da80 util: Clear unused part of the map in virBitmapShrink
Some of the other functions depend on the fact that unused bits and longs are
always zero and it's less error-prone to clear it than fix the other functions.
It's enough to zero out one piece of the map since we're calling realloc() to
get rid of the rest (and updating map_len).

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540817

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-02-02 14:51:32 +01:00
Martin Kletzander
679543449c util: Fix possible leak in virResctrlAllocMasksAssign
Found by coverity.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-02-02 14:50:12 +01:00
Chen Hanxiao
bb189c8e8c qemu: Introduce VIR_ERR_DEVICE_MISSING
Add new error code to be able to allow consumers (such as Nova) to be
able to key of a specific error code rather than needing to search the
error message."

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2018-02-01 07:17:01 -05:00
Daniel P. Berrange
5315d20698 util: use union for sockaddr structs to avoid aliasing
Some platforms/toolchains will complain about casting
sockaddr_storage to sockaddr_un because it breaks strict
aliasing rule

../../src/util/virutil.c: In function 'virGetUNIXSocketPath':
../../src/util/virutil.c:2005: error: dereferencing pointer 'un' does break strict-aliasing rules [-Wstrict-aliasing]

Change the code to use a union, in the same way that the
virsocketaddr.h header does.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2018-01-31 17:33:08 +00:00
Daniel P. Berrangé
eef9812827 Fixed virGetUNIXSocketPath stub on Win32
The _() macro was not terminated and an argument needs to be marked as
unused.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2018-01-31 17:10:52 +00:00
Michal Privoznik
7b0ea999fc virUSBDeviceNew: Construct vroot path properly
When starting an LXC container, the /dev entries are created
under temp root (/var/run/libvirt/lxc/$name.dev), relabelled and
then the root is pivoted. However, when it comes to USB devices
which keep path to the device in the structure we need a way to
override the default /dev/usb/... path because we want to work
with the one under temp root. That's what @vroot argument is for
in virUSBDeviceNew. However, what is being passed there is:

  vroot = /var/run/libvirt/lxc/lxc_0.dev/bus/usb

Therefore, constructed path is wrong:

  dev->path = //var/run/libvirt/lxc/lxc_0.dev/bus/usb//dev/bus/usb/002/002

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2018-01-31 17:10:01 +01:00
Daniel P. Berrange
9fe6619d4a util: add virGetUNIXSocketPath helper
When receiving multiple socket FDs from systemd, it is critical to know
what socket address each corresponds to so we can setup the right
protocols on each.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2018-01-31 15:12:53 +00:00
Martin Kletzander
b9ceacba7c util: Extract path formatting into virResctrlAllocDeterminePath
We can use this from more places later, so just a future code de-duplication.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-31 14:51:34 +01:00
Martin Kletzander
859186091c util: Don't overwrite mask in virResctrlAllocFindUnused
Due to confusing naming the pointer to the mask got copied which must not
happen, so use UpdateMask instead of SetMask.  That also means we can get
completely rid of SetMask.

Also don't clear the free bits since it is not used again (leftover from
previous versions).

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-31 14:51:34 +01:00
Martin Kletzander
c39ce914dd util: Use default group's mask for unspecified resctrl allocations
Introduce virResctrlAllocCopyMasks() and use that to initially copy the default
group schemata to the allocation before reserving any parts of the cache.  The
reason for this is that when new group is created the schemata will have unknown
data in it.  If there was previously group with the same CLoS ID, it will have
the previous valies, if not it will have all bits set.  And we need to set all
unspecified (in the XML) allocations to the same one as the default group.

Some non-Linux functions now need to be made public due to this change.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1289368

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-31 14:51:34 +01:00
Martin Kletzander
f6199295a9 util: Add helpers for getting resctrl group allocs
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-31 14:51:34 +01:00
Peter Krempa
f46d6e22f2 util: storage: Parse 'lun' for iSCSI protocol from JSON as string or number
While the QEMU QAPI schema describes 'lun' as a number, the code dealing
with JSON strings does not strictly adhere to this schema and thus
formats the number back as a string. Use the new helper to retrieve both
possibilities.

Note that the formatting code is okay and qemu will accept it as an int.

Tweak also one of the test strings to verify that both formats work
with libvirt.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540290
2018-01-31 12:22:25 +01:00
Peter Krempa
d3da8013cc util: json: Add helper to return string or number properties as string
The helper is useful in cases when the JSON we have to parse may contain
one of the two due to historical reasons and the number value itself
would be stored as a string.
2018-01-31 12:21:39 +01:00
Martin Kletzander
f2e16994f7 util: Don't check if entries under /sys/fs/resctrl/(info/) are directories
We are skipping non-directories under /sys/fs/resctrl/(info/) since those are not
interesting for us.  However in tests it can sometimes happen that ent->d_type
is 0 instead of 4 (DT_DIR) for directories.

I've seen it fail on two machines.  Different machines, different systems, I
cannot reproduce it even using the same setup.  So one of the ways how to work
around this is call stat() on it.  The other one is not checking if it is a
directory since we'll find out eventually when we want to read some files
underneath it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-29 21:40:54 +01:00
Martin Kletzander
e28ccd2643 util: Remove unused variable in virResctrlGetInfo
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-29 21:40:54 +01:00
Martin Kletzander
6899118043 util: Make it possible for virResctrlAllocSetMask to replace existing mask
This wil be used in the future, but it makes sense for now as well.  It makes
sure there is no mask leftover that would leak.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-29 21:40:54 +01:00
Martin Kletzander
ebafc603c1 util: Use "resctrl" instead of "resctrlfs" spelling
Pointed out during review on one or two places, but it actually appears in lot
more places.  So let's be consistent.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-29 21:40:54 +01:00
Martin Kletzander
bd5d07425d util: Check for empty allocation instead of just NULL pointer
When working on the CAT series one of the changes was that the pointer got
allocated in another part of the code, even when resctrl was not available on
the host system.  However this one particular place neglected that so it needs
to be fixed in order to get the proper error message when requesting
<cachetune/> on HW with no support for it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-29 21:40:53 +01:00
Erik Skultety
b7a7912411 build: Fix broken build on FreeBSD and OSX after recent nodedev series
Commits f83c7c88 and 6eb1f2b9 broke the build on FreeBSD and OSX because
of symbols being undefined for those platforms.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-29 17:53:13 +01:00
Erik Skultety
6eb1f2b9d0 util: pci: Introduce virPCIGetMdevTypes helper
This is a replacement for the existing udevPCIGetMdevTypesCap which is
static to the udev backend. This simple helper constructs the sysfs path
from the device's base path for each mdev type and queries the
corresponding attributes of that type.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-29 15:34:30 +01:00
Erik Skultety
3545cbef7f util: mdev: Introduce virMediatedDeviceTypeReadAttrs getter
This should serve as a replacement for the existing udevFillMdevType
which is responsible for fetching the device type's attributes from the
sysfs interface. The problem with the existing solution is that it's
tied to the udev backend.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-29 15:34:30 +01:00
Erik Skultety
c3faa92a1b util: mdev: Introduce virMediatedDeviceType structure
This is later going to replace the existing virNodeDevCapMdevType, since:
1) it's going to couple related stuff in a single module
2) util is supposed to contain helpers that are widely accessible across
the whole repository.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-29 15:34:30 +01:00
Erik Skultety
0674ddd317 util: mdev: Drop some unused symbols/includes from the header
There were some leftovers from early development which never got used.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-29 15:34:30 +01:00
Daniel P. Berrangé
a020ab03fd resctl: stub out functions with Linux-only APIs used
The flock() function and d_type field in struct dirent are not portable
to the mingw platform.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2018-01-25 17:42:36 +00:00
Martin Kletzander
a64c761c27 resctrl: Add functions to work with resctrl allocations
With this commit we finally have a way to read and manipulate basic resctrl
settings.  Locking is done only on exposed functions that read/write from/to
resctrlfs.  Not in functions that are exposed in virresctrlpriv.h as those are
only supposed to be used from tests.

More information about how resctrl works:

  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/x86/intel_rdt_ui.txt

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-25 17:16:08 +01:00
Martin Kletzander
434848d7dc fixup_resctrlinfo
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-25 17:16:08 +01:00
Martin Kletzander
6328e48713 util: Remove now-unneeded resctrl functions
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-25 17:16:08 +01:00
Martin Kletzander
cd572df89b util: Add virResctrlInfo
This will make the current functions obsolete and it will provide more
information to the virresctrl module so that it can be used later.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-25 17:16:08 +01:00
Martin Kletzander
b2211a9e54 Rename virResctrlInfo to virResctrlInfoPerCache
Just to ease the review of following patches.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2018-01-25 17:16:08 +01:00
Daniel P. Berrange
68eed56b2d conf: add support for setting OEM strings SMBIOS data fields
The OEM strings table in SMBIOS allows the vendor to pass arbitrary
strings into the guest OS. This can be used as a way to pass data to an
application like cloud-init, or potentially as an alternative to the
kernel command line for OS installers where you can't modify the install
ISO image to change the kernel args.

As an example, consider if cloud-init and anaconda supported OEM strings
you could use something like

    <oemStrings>
      <entry>cloud-init:ds=nocloud-net;s=http://10.10.0.1:8000/</entry>
      <entry>anaconda:method=http://dl.fedoraproject.org/pub/fedora/linux/releases/25/x86_64/os</entry>
    </oemStrings>

use of a application specific prefix as illustrated above is
recommended, but not mandated, so that an app can reliably identify
which of the many OEM strings are targetted at it.

Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2018-01-25 14:48:56 +00:00
Jim Fehlig
71d56a3979 nodedev: Fix failing to parse PCI address for non-PCI network devices
Commit 8708ca01c added virNetDevSwitchdevFeature() to check if a network
device has Switchdev capabilities. virNetDevSwitchdevFeature() attempts
to retrieve the PCI device associated with the network device, ignoring
non-PCI devices. It does so via the following call chain

  virNetDevSwitchdevFeature()->virNetDevGetPCIDevice()->
  virPCIGetDeviceAddressFromSysfsLink()

For non-PCI network devices (qeth, Xen vif, etc),
virPCIGetDeviceAddressFromSysfsLink() will report an error when
virPCIDeviceAddressParse() fails. virPCIDeviceAddressParse() also
logs an error. After commit 8708ca01c there are now two errors reported
for each non-PCI network device even though the errors are harmless.

To avoid the errors, introduce virNetDevIsPCIDevice() and use it in
virNetDevGetPCIDevice() before attempting to retrieve the associated
PCI device. virNetDevIsPCIDevice() uses the 'subsystem' property of the
device to determine if it is PCI. See the sysfs rules in kernel
documentation for more details

https://www.kernel.org/doc/html/latest/admin-guide/sysfs-rules.html
2018-01-19 09:53:01 -07:00
Bjoern Walk
4be9959b41 util: virsysinfo: parse frequency information on S390
Let's also parse the available processor frequency information on S390
so that it can be utilized by virsh sysinfo:

    # virsh sysinfo

    <sysinfo type='smbios'>
      ...
      <processor>
	<entry name='family'>2964</entry>
	<entry name='manufacturer'>IBM/S390</entry>
	<entry name='version'>00</entry>
	<entry name='max_speed'>5000</entry>
	<entry name='serial_number'>145F07</entry>
      </processor>
      ...
    </sysinfo>

Reviewed-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Signed-off-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2018-01-12 09:21:05 -05:00
Michal Privoznik
72bf14d345 util: Introduce virStringListMerge
For two string lists merge one into the other one.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Bjoern Walk
ae68dbffe9 util: virhostcpu: parse frequency information on S390
Since kernel version 4.7, processor frequency information is available
on S390. Let's adjust the parser so this information shows up for virsh
nodeinfo:

    # virsh nodeinfo
    CPU model:           s390x
    CPU(s):              8
    CPU frequency:       5000 MHz
    CPU socket(s):       1
    Core(s) per socket:  8
    Thread(s) per core:  1
    NUMA cell(s):        1
    Memory size:         16273908 KiB

Reviewed-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Signed-off-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2018-01-10 17:24:11 -05:00
Andrea Bolognani
a63ea8141b util: Don't report CPU frequency for ARM hosts
Some ARM platforms, such as the original Raspberry Pi, report the
CPU frequency in the BogoMIPS field of /proc/cpuinfo, so libvirt
parsed that field and returned it through its API.

However, not only many more boards don't report any value there,
but several - including ARMv8-based server hardware, and even the
more recent Raspberry Pi 3 - use this field as originally intended:
to report the BogoMIPS value instead of the CPU frequency.

Since we have no way of detecting how the field is being used,
it's better to report no information at all rather than something
ludicrous like "your shiny 96-core aarch64 virtualization host's
CPUs are running at a whopping 100 MHz".

Partially-resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1206353

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2018-01-08 14:22:53 +01:00
Andrea Bolognani
6512b0ddc1 util: Improve CPU frequency parsing
Make the parser both more strict, by not ignoring errors reported
by virStrToLong_ui(), and more permissive, by not failing due to
unrelated fields which just happen to have a know prefix and
accepting any amount of whitespace before the numeric value.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2018-01-08 13:48:44 +01:00
Andrea Bolognani
5e07b28a7a util: Print architecture name in /proc/cpuinfo parser
Instead of a generic "your architecture", print the actual
architecture name.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2018-01-08 13:48:44 +01:00
Bjoern Walk
0764fc8ad1 util: virhostcpu: factor out frequency parsing
All different architectures use the same copy-pasted code to parse
processor frequency information from /proc/cpuinfo. Let's extract that
code into a function to avoid repetition.

We now also tolerate if the parsing of /proc/cpuinfo is not successful
and just report a warning instead of bailing out and abandoning the rest
of the CPU information.

Reviewed-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2018-01-08 13:48:44 +01:00
Paolo Bonzini
04502fd54f util: introduce virHostCPUGetMicrocodeVersion
This new API reads host's CPU microcode version from /proc/cpuinfo.

Unfortunately, there is no other way of reading microcode version which
would be usable from both system and session daemon.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2018-01-04 16:52:03 +01:00
Paolo Bonzini
40fc85e796 util: add virFileReadHeaderQuiet wrapper around virFileReadHeaderFD
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2018-01-04 16:52:03 +01:00
Peter Krempa
f06e488d54 util: probe: Add quiet versions of the "PROBE" macro
PROBE macro adds a logging entry, when used in places seeing a lot of
traffic this can cause a significant slowdown.
2018-01-03 15:21:06 +01:00
Chen Hanxiao
c1a6e2fb43 util: virstring: Tweak a few more descriptions.
There are a few more description-related issues that commit @9026d115
forgot to address.

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-03 12:42:53 +01:00
Cédric Bosdonnat
b475a91b77 Add virStringFilterChars() string utility
Add a function to filter a string based on a list of valid characters.
2018-01-03 10:58:16 +01:00
Chen Hanxiao
9026d1152c util: fix the description of virStringSearch
There's no argument named @result, use @matches instead.

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-03 09:54:14 +01:00
Peter Krempa
cd31709351 util: storage: Add helpers to parse and format relPath into privateData
This will be the first private piece of data that will need to be stored
in the XML for some drivers. Add helpers which will do it.
2017-12-14 10:27:30 +01:00
John Ferlan
2114154922 util: Report error if vhost-scsi device file cannot be found
https://bugzilla.redhat.com/show_bug.cgi?id=1523564

If the vhost-scsi device file cannot be found, the generic error

    "error: An error occurred, but the cause is unknown"

is returned.  Let's add a real error message to make it clear
why the failure occurred.
2017-12-13 15:37:30 -05:00
John Ferlan
9e030093f2 util: Fix error path in virSCSIVHostOpenVhostSCSI
We cannot be sure someone initialized the passed *vhostfd and we
certainly don't want or need to be calling VIR_FORCE_CLOSE on what
probably is -1. So let's just return -1 immediately.
2017-12-13 15:37:30 -05:00
Marc Hartmayer
4199c2f221 audit: Log only an info message if audit_level < 2 and audit is not supported
Replace the error message during startup of libvirtd with an info
message if audit_level < 2 and audit is not supported by the
kernel. Audit is not supported by the current kernel if the kernel
does not have audit compiled in or if audit is disabled (e.g. by the
kernel cmdline).

Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-12-13 13:42:16 +01:00
Daniel P. Berrange
917047de61 Update to latest keycodemapdb content
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-12 16:34:12 +00:00
Chen Hanxiao
c416a20db1 virerror: mark VIR_ERR_AGENT_UNSYNCED as DEPRECATED
Since commit 5e5019bf, we've no longer use
VIR_ERR_AGENT_UNSYNCED anymore.
Mark it as DEPRECATED.

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-12-07 14:30:20 +01:00
Andrea Bolognani
77117e18b7 util: gettid() is Linux-specific
The manual page clearly states that

  gettid() is Linux-specific and should not be used in programs
  that are intended to be portable.

Unfortunately, it looks like macOS implemented the functionality
and defined SYS_gettid accordingly, only to deprecate syscall()
altogether with 10.12 (Sierra), released last late year.

To avoid compilation errors, call gettid() on Linux only.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
2017-12-05 11:28:19 +01:00
Peter Krempa
0a1870ddd0 util: storage: Add fields for debug options for disk drivers
Some drive backends allow output of debugging information which can be
configured using properties of the image. Add fields to virStorageSource
which will allow configuring them.
2017-11-23 18:50:04 +01:00
Jiri Denemark
c88ce8ae74 vierror: Define VIR_ERROR_MAX_LENGTH macro
And use it instead of a magic 1024 constant.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
2017-11-23 14:06:40 +01:00
Martin Kletzander
ea7b2a6d04 util: Fix leak in virStringTrimOptionalNewline
Do not access any data if strlen() == 0.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-11-23 11:31:52 +01:00
Martin Kletzander
baca005367 util: Introduce virBitmapShrink
Sometimes the size of the bitmap matters and it might not be guessed correctly
when parsing from some type of input.  For example virBitmapNewData() has Byte
granularity, virBitmapNewString() has nibble granularity and so on.
virBitmapParseUnlimited() can be tricked into creating huge bitmap that's not
needed (e.g.: "0-2,^99999999").  This function provides a way to shrink the
bitmap.  It is not supposed to free any memory.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
449442c34d util: Reintroduce virBitmapSubtract
Already introduced in the past with 9479642fd3, but then renamed to
virBitmapIntersect by a908e9e45e.  This time we'll really use it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
2e5579a43b util: Introduce virBitmapNewString
Our bitmaps can be represented as data (raw bytes for which we have
virBitmapNewData() and virBitmapToData()), human representation (list
of numbers in a string for which we have virBitmapParse() and
virBitmapFormat()) and hexadecimal string (for which we have only
virBitmapToString()).  So let's add the missing complement for the
last one so that we can parse hexadecimal strings.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
fb10602875 util: Don't output too many zeros from virBitmapToString
Truncate the output so that it is only as big as is needed to fit all
the bits, not all the units from the map.  This will be needed in the
future in order to properly format bitmaps for kernel's sysfs files.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
5d893ed6ad util: Rename virBitmapDataToString to virBitmapDataFormat
It is literally only a wrapper around virBitmapNewData() and
virBitmapFormat(), only the naming was wrong since it was introduced.
And because we have virBitmap*String functions where the meaning of
the 'String' is constant, this might confuse someone.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
68d7cc649c util: Rename virBitmapString to virBitmapToString
This follows the virBitmapToData() function and, similarly to
virBitmapNewData(), we'll be able to have virBitmapNewString() later
on without name confusion.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
d127aced0a util: Make prefix optional in virBitampString
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Martin Kletzander
87a8a30d61 util: Introduce virFormatIntPretty
We can't output better memory sizes if we want to be compatible with libvirt
older than the one which introduced /memory/unit, but for new things we can just
output nicer capacity to the user if available.  And this function enables that.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-11-18 10:45:10 +01:00
Viktor Mihajlovski
010f88d5cb numa: avoid failure in nodememstats on non-NUMA systems
libvirt reports a fake NUMA topology in virConnectGetCapabilities
even if built without numactl support. The fake NUMA topology consists
of a single cell representing the host's cpu and memory resources.
Currently this is the case for ARM and s390[x] RPM builds.

A client iterating over NUMA cells obtained via virConnectGetCapabilities
and invoking virNodeGetMemoryStats on them will see an internal failure
"NUMA isn't available on this host" from virNumaGetMaxNode. An example
for such a client is VDSM.

Since the intention seems to be that libvirt always reports at least
a single cell it is necessary to return "fake" node memory statistics
matching the previously reported fake cell in case NUMA isn't supported
on the system.

Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
2017-11-09 17:11:51 -05:00
Jim Fehlig
299e97c4e8 virconf: properly set the end of content
There was a recent report of the xen-xl converter not handling
config files missing an ending newline

https://www.redhat.com/archives/libvir-list/2017-October/msg01353.html

Commit 3cc2a9e0 fixed a similar problem when parsing content of a
file but missed parsing in-memory content. But AFAICT, the better
fix is to properly set the end of the content when initializing the
virConfParserCtxt in virConfParse().

This commit reverts the part of 3cc2a9e0 that appends a newline to
files missing it, and fixes setting the end of content when
initializing virConfParserCtxt. A test is also added to check
parsing in-memory content missing an ending newline.

Signed-off-by: Jim Fehlig <jfehlig@suse.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
2017-11-09 08:04:26 -07:00
Peter Krempa
0a3bae7d22 util: storage: Fix parsing of IPv6 portal address for iSCSI
Split on the last colon and avoid parsing port if the split remainder
contains the closing square bracket, so that IPv6 addresses are
interpreted correctly.
2017-11-08 13:10:40 +01:00
Peter Krempa
90521d0754 storage: Store RBD image name as pool and image name
Similarly to how we store gluster names, split the name into a pool and
image portions when paring the XML and store them separately.
2017-11-07 14:58:04 +01:00
Peter Krempa
bb14d9897b storage: Don't store leading '/' in image name when splitting out volume
Libvirt historically stores storage source path including the volume as
one string in the XML, but that is not really flexible enough when
dealing with the fields in the code. Previously we'd store the slash
separating the two as part of the image name. This was fine for gluster
but it's not necessary and does not scale well when converting other
protocols.

Don't store the slash as part of the path. The resulting change from
absolute to relative path within the gluster driver should be okay,
as the root directory is the default when accessing gluster.
2017-11-07 14:58:04 +01:00
Peter Krempa
e5580888f5 util: Fix condition check in virDiskNameToIndex
Use the more common '< 0' rather than the non-zero check.
2017-11-07 14:29:37 +01:00
Andrea Bolognani
3e7db8d3e8 Remove backslash alignment attempts
Right-aligning backslashes when defining macros or using complex
commands in Makefiles looks cute, but as soon as any changes is
required to the code you end up with either distractingly broken
alignment or unnecessarily big diffs where most of the changes
are just pushing all backslashes a few characters to one side.

Generated using

  $ git grep -El '[[:blank:]][[:blank:]]\\$' | \
    grep -E '*\.([chx]|am|mk)$$' | \
    while read f; do \
      sed -Ei 's/[[:blank:]]*[[:blank:]]\\$/ \\/g' "$f"; \
    done

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
2017-11-03 13:24:12 +01:00
Nikolay Shirokovskiy
05021e727d iohelper: use saferead if later write with O_DIRECT
One of the usecases of iohelper is to read from pipe and write
to file with O_DIRECT. As we read from pipe we can have partial
read and then we fail to write this data because output file
is open with O_DIRECT and buffer size is not aligned.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2017-10-24 10:53:18 +02:00
Julio Faracco
89622ebfad util: Missing 'removeTimeoutImpl' check variable inside virEventRegisterImpl() function.
The function virEventRegisterImpl() checks the attempt to replace the
registered events. But there is a duplicate variable inside the IF statement.
The variable 'removeHandleImpl' was wrongly repeated. One of them needs to be
replaced by 'removeTimeoutImpl'.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2017-10-20 15:15:35 +02:00
John Ferlan
37537a7c64 conf: Add/Allow parsing the encryption in the disk source
Since the virStorageEncryptionPtr encryption; is a member of
 _virStorageSource it really should be allowed to be a subelement
of the disk <source> for various disk formats:

   Source{File|Dir|Block|Volume}
   SourceProtocol{RBD|ISCSI|NBD|Gluster|Simple|HTTP}

NB: Simple includes sheepdog, ftp, ftps, tftp

That way we can set up to allow the <encryption> element to be
formatted within the disk source, but we still need to be wary
from whence the element was read - see keep track and when it
comes to format the data, ensure it's written in the correct place.

Modify the qemuxml2argvtest to add a parse failure when there is an
<encryption> as a child of <disk> *and* an <encryption> as a child
of <source>.

The virschematest will read the new test files and validate from a
RNG viewpoint things are fine.
2017-10-19 15:32:50 -04:00
John Ferlan
8002d3cb1b conf: Add/Allow parsing the auth in the disk source
Since the virStorageAuthDefPtr auth; is a member of _virStorageSource
it really should be allowed to be a subelement of the disk <source>
for the RBD and iSCSI prototcols. That way we can set up to allow
the <auth> element to be formatted within the disk source.

Since we've allowed the <auth> to be a child of <disk>, we'll need
to keep track of how it was read so that when writing out we'll know
whether to format as child of <disk> or <source>. For the argv2xml
parsing, let's format under <source> as a preference. Do not allow
<auth> to be both a child of <disk> and <source>.

Modify the qemuxml2argvtest to add a parse failure when there is an
<auth> as a child of <disk> *and* an <auth> as a child of <source>.

Add tests to validate that if the <auth> was found in <source>, then
the resulting xml2xml and xml2arg works just fine.  The two new .args
file are exact copies of the non "-source" version of the file.

The virschematest will read the new test files and validate from a
RNG viewpoint things are fine

Update the virstoragefile, virstoragetest, and args2xml file to show
the "preference" to place <auth> as a child of <source>.
2017-10-19 15:26:49 -04:00
John Ferlan
1f38445e43 util: storage: Introduce privateData for _virStorageSource
Introduce the bare necessities to add privateData to _virStorageSource.

Signed-off-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
2017-10-19 18:20:30 +02:00
Erik Skultety
caf26412b6 util: Introduce virFileWaitForExists
Since we have a number of places where we workaround timing issues with
devices, attributes (files in general) not being available at the time
of processing them by calling usleep in a loop for a fixed number of
tries, we could as well have a utility function that would do that.
Therefore we won't have to duplicate this ugly workaround even more.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-10-19 08:54:53 +02:00
Erik Skultety
15a37cdf88 maint: Remove not-so-much informative block commentaries
There were a bunch of commentary blocks that were literally useless in
terms of describing what the code following them does, since most of
them were documenting "the obvious" or it just wouldn't help at all.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-10-18 13:38:59 +02:00
Erik Skultety
b08017ca54 maint: Replace tabs with spaces in all source files in repo
So we have a syntax-check rule to catch all tab indents but it naturally
can't catch tab spacing, i.e. as a delimiter. This patch is a result of
running 'vim -en +retab +wq'
(using tabstop=8 softtabstop=4 shiftwidth=4 expandtab) on each file from
a list generated by the following:
find . -regextype gnu-awk \
         -regex ".*\.(rng|syms|html|s?[ch]|py|pl|php(\.code)?)(\.in)?" \
         | xargs git grep -lP "\t"

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-10-18 13:25:10 +02:00
Peter Krempa
4a5cbba4bc util: storagefile: Track whether a virStorageSource was auto-detected
When formatting an inactive or migratable XML we will need to suppress
backing chain members which were detected from the disk to keep
semantics straight. This means we need to record, whether a
virStorageSource originates from autodetection.
2017-10-18 09:44:29 +02:00
Peter Krempa
a693fdba01 Terminate backing chains explicitly
Express a properly terminated backing chain by putting a
virStorageSource of type VIR_STORAGE_TYPE_NONE in the chain. The newly
used helpers simplify this greatly.

The change fixes a bug as formatting an incomplete backing chain and
parsing it back would end up in expressing a terminated chain since
src->backingStoreRaw was not populated. By relying on the terminator
object this can be now processed appropriately.
2017-10-17 06:19:18 +02:00
Peter Krempa
0a294a8e28 util: storagefile: Add helpers to check presence of backing store
Add helpers that will simplify checking if a backing file is valid or
whether it has backing store. The helper virStorageSourceIsBacking
returns true if the given virStorageSource is a valid backing store
member. virStorageSourceHasBacking returns true if the virStorageSource
has a backing store child.

Adding these functions creates a central points for further refactors.
2017-10-17 06:19:18 +02:00
Peter Krempa
e3fb414e77 util: storagefile: Tolerate NULL path when looking up volume in chain
chain->path may be NULL e.g. for NBD drives, so the check needs to avoid
dereferencing the path in such case
2017-10-17 06:19:18 +02:00
Peter Krempa
9cc8d6ee9e util: storage: use stored index to lookup disks
We can now use the backing store ID directly rather than counting the
number of images seen while looking up images.
2017-10-17 06:19:18 +02:00
Peter Krempa
c6231a6a6c util: storage: Store backing chain index in virStorageSource
The backing store indexes were not bound to the storage sources in any
way. To allow us to bind a given alias to a given storage source we need
to save the index in virStorageSource. The backing store ids are now
generated when detecting the backing chain.

Since we don't re-detect the backing chain after snapshots, the
numbering needs to be fixed there.
2017-10-17 06:19:18 +02:00
Ladi Prosek
5ae2d9c259 hyperv: Escape WQL queries
The code was vulnerable to SQL injection. Likely not a security issue due to
WMI SQL and other constraints but still lame. For example:

  virsh # dominfo \"
  error: failed to get domain '"'
  error: internal error: SOAP fault during enumeration: code 's:Sender', subcode
  'n:CannotProcessFilter', reason 'The data source could not process the filter.
  The filter might be missing or it might be invalid. Change the filter and try
  the request again.  ', detail 'The WS-Management service cannot process the
  request. The WQL query is invalid. '

This commit fixes the Hyper-V driver by escaping all WMI SQL string parameters.

The same command with the fix:

  virsh # dominfo \"
  error: failed to get domain '"'
  error: Domain not found: No domain with name "

Signed-off-by: Ladi Prosek <lprosek@redhat.com>
2017-10-16 10:29:32 -04:00
Daniel P. Berrange
e371b3bf41 Use https:// links for most sites
This adds a rule to require https links for the libvirt, qemu
and kvm websites.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-10-16 10:22:34 +01:00
Jiri Denemark
ef04596d76 util: Introduce virStringListCopy
The API makes a deep copy of a NULL-terminated string list.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-10-16 09:21:51 +02:00
John Ferlan
ac7cc62419 nodedev: Fix missing network devices
Commit id '8708ca01c' added a check to determine whether the NIC had
Switchdev capabilities; however, in doing so inadvertently would cause
network devices without a PCI device to not be added to the node device
database. Thus, network devices having a "computer" as a parent, such
as "net_lo*", "net_virbr*", "net_tun*", "net_vnet*", etc. were not added.

Alter the check to not even check for Switchdev bits if no PCI device found.
2017-10-13 20:29:06 -04:00
John Ferlan
0c691e9806 util: Resolve resource leak
Need to free @groups in the parent on success similar to other
APIs (virFile*) which use virGetGroupList and virFork.

Reported by Coverity.
2017-10-12 18:27:00 -04:00
Marc Hartmayer
5fec1c3a5c util: Fix deadlock across fork()
This commit fixes the deadlock introduced by commit
0980764dee. The call getgrouplist() of
the glibc library isn't safe to be called in between fork and
exec (see commit 75c125641a).

Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Fixes: 0980764dee ("util: share code between virExec and virCommandExec")
Reviewed-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
2017-10-10 09:27:00 +01:00
Marc Hartmayer
2e88eeebb1 util: Add virCommandGetGID and virCommandGetUID
These functions are used by an upcoming commit.

Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
2017-10-10 09:27:00 +01:00
Peter Krempa
d868366fe3 util: Add functions to simplify bool->virTristate(Bool|Switch) assignment
virTristateBoolFromBool and virTristateSwitchFromBool convert a boolean
to the correct enum value.
2017-10-06 08:47:30 +02:00
Michal Privoznik
cea3715b2e QoS: Set classes and filters in proper direction
Similarly to previous patch, for some types of interface domain
and host are on the same side of RX/TX barrier. In that case, we
need to set up the QoS differently. Well, swapped.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-10-05 09:16:05 +02:00
Michal Privoznik
d86fd2402e virNetDevTapInterfaceStats: Allow caller to not swap the statistics
https://bugzilla.redhat.com/show_bug.cgi?id=1497410

The comment in virNetDevTapInterfaceStats() implementation for
Linux states that packets transmitted by domain are received by
the host and vice versa. Well, this is true but not for all types
of interfaces. For instance, for macvtaps when TAP device is
hooked right onto a physical device any packet that domain sends
looks also like a packet sent to the host. Therefore, we should
allow caller to chose if the stats returned should be straight
copy or swapped.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-10-05 09:16:05 +02:00
Michal Privoznik
7e62c4cd26 virmacaddr: Track if MAC address is autogenerated
It will come handy to know if the MAC address was generated (e.g.
during XML parse) or if it was parsed since provided by user in
the XML.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2017-10-04 17:10:12 +02:00
Kothapally Madhu Pavan
65a983eca1 util: Free a pointer in virPolkitCheckAuth
Free DBusMessage pointer in virPolkitCheckAuth

Signed-off-by: Kothapally Madhu Pavan <kmp@linux.vnet.ibm.com>
2017-10-04 11:20:17 +02:00
John Ferlan
5c09486c1e qemu: Introduce qemuDomainPrepareDiskSource
Introduce a function to setup any TLS needs for a disk source.

If there's a configuration or other error setting up the disk source
for TLS, then cause the domain startup to fail.

For VxHS, follow the chardevTLS model where if the src->haveTLS hasn't
been configured, then take the system/global cfg->haveTLS setting for
the storage source *and* mark that we've done so via the tlsFromConfig
setting in storage source.

Next, if we are using TLS, then generate an alias into a virStorageSource
'tlsAlias' field that will be used to create the TLS object and added to
the disk object in order to link the two together for QEMU.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-09-28 09:45:14 -04:00
Ashish Mittal
f170548502 util: Add TLS attributes to virStorageSource
Add an optional virTristateBool haveTLS to virStorageSource to
manage whether a storage source will be using TLS.

Sample XML for a VxHS disk:

<disk type='network' device='disk'>
  <driver name='qemu' type='raw' cache='none'/>
  <source protocol='vxhs' name='eb90327c-8302-4725-9e1b-4e85ed4dc251' tls='yes'>
    <host name='192.168.0.1' port='9999'/>
  </source>
  <target dev='vda' bus='virtio'/>
</disk>

Additionally add a tlsFromConfig boolean to control whether the TLS
setting was due to domain configuration or qemu.conf global setting
in order to decide whether to Format the haveTLS setting for either
a live or saved domain configuration file.

Update the qemuxml2xmltest in order to add a test to show the proper
parsing.

Also update the docs to describe the tls attribute.

Signed-off-by: Ashish Mittal <Ashish.Mittal@veritas.com>
Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-09-28 09:45:14 -04:00
Ján Tomko
959fe7de1f Shrink volume even with ALLOCATE flag
Calling fallocate on the new (smaller) capacity ensures
that the whole file is allocated, but it does not reduce
the file size.

Also call ftruncate after fallocate.

https://bugzilla.redhat.com/show_bug.cgi?id=1366446
2017-09-27 14:40:44 +02:00
Ján Tomko
3f702f5ab1 virStorageFileResize: fallocate the whole capacity
We have been trying to implement the ALLOCATE flag to mean
"the volume should be fully allocated after the resize".

Since commit b0579ed9 we do not allocate from the existing
capacity, but from the existing allocation value.
However this value is a total of all the allocated bytes,
not an offset.

For a sparsely allocated file:
$ perl -e 'print "x"x8192;' > vol1
$ fallocate -p -o 0 -l 4096 vol1
$ virsh vol-info vol1 default
Capacity:       8.00 KiB
Allocation:     4.00 KiB

Treating allocation as an offset would result in an incompletely
allocated file:
$ virsh vol-resize vol1 --pool default 16384 --allocate
Capacity:       16.00 KiB
Allocation:     12.00 KiB

Call fallocate from zero on the whole requested capacity to fully
allocate the file. After that, the volume is fully allocated
after the resize:
$ virsh vol-resize vol1 --pool default 16384 --allocate
$ virsh vol-info vol1 default
Capacity:       16.00 KiB
Allocation:     16.00 KiB
2017-09-27 14:40:44 +02:00
Ján Tomko
5463d95969 use virFileAllocate in virStorageFileResize
Introduce a new function virFileAllocate that will call the
non-destructive variants of safezero, essentially reverting
my commit 1390c268
    safezero: fall back to writing zeroes even when resizing
back to the state as of commit 18f0316
    virstoragefile: Have virStorageFileResize use safezero

This means that _ALLOCATE flag will no longer work on platforms
without the allocate syscalls, but it will not overwrite data
either.
2017-09-27 14:40:44 +02:00
Daniel P. Berrange
32d6c7386d Print hex values with '0x' prefix and octal with '0' in debug messages
Seeing a log message saying 'flags=93' is ambiguous & confusing unless
you happen to know that libvirt always prints flags as hex.  Change our
debug messages so that they always add a '0x' prefix when printing flags,
and '0' prefix when printing mode. A few other misc places gain a '0x'
prefix in error messages too.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-09-25 13:34:53 +01:00
Laine Stump
17825e8ae7 util: Fix stack smashing in virNetDevGetFamilyId
After commit 8708ca01c0 libvirtd consistently aborts with "stack
 smashing detected" when nodedev driver is initialized.

This is caused by nlmsg_parse() being told that its array of nlattr*
has CTRL_CMD_MAX (10) entries, when in fact it is declared to have
CTRL_ATTR_MAX (8) entries. Since all the entries are initialized to
NULL, the result is that nlmsg_parse is overwriting 2*(sizof(nlattr*))
bytes outside the array.

Signed-off-by: Laine Stump <laine@laine.org>
Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2017-09-22 09:17:59 +02:00
John Ferlan
ffdce9b1f1 util: Fix secret generation in virStorageSourceParseRBDColonString
Commit id '5604c056' used the wrong API to generate the
<secret type='%s'..." field. The previous code used the
correct API as was done in commit id '6887af39'. The data
is actually a usage type not an auth type even though the
result is the same.
2017-09-21 15:46:48 -04:00
John Ferlan
2dd024754e util: Move virSecretUsageType to virsecret.h
Move the virSecretUsageType into the util.
2017-09-21 15:46:48 -04:00
Daniel P. Berrange
633b699bfd iohelper: avoid calling read() with misaligned buffers for O_DIRECT
The iohelper currently calls saferead() to get data from the
underlying file. This has a problem with O_DIRECT when hitting
end-of-file. saferead() is asked to read 1MB, but the first
read() it does may return only a few KB, so it'll try another
read() to fill the remaining buffer. Unfortunately the buffer
pointer passed into this 2nd read() is likely not aligned
to the extent that O_DIRECT requires, so rather than seeing
'0' for end-of-file, we'll get -1 + EINVAL due to misaligned
buffer.

The way the iohelper is currently written, it already handles
getting short reads, so there is actually no need to use
saferead() at all. We can simply call read() directly. The
benefit of this is that we can now write() the data immediately
so when we go into the subsequent reads() we'll always have a
correctly aligned buffer.

Technically the file position ought to be aligned for O_DIRECT
too, but this does not appear to matter when at end-of-file.

Tested-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-09-21 08:52:13 +01:00
Ashish Mittal
2a48252bb5 util: storage: Add JSON backing volume parse for VxHS
Add the backing parse and a test case to verify parsing of VxHS
backing storage.

Signed-off-by: Ashish Mittal <Ashish.Mittal@veritas.com>
Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-09-19 21:10:21 -04:00
Ashish Mittal
029c36c981 storage: Introduce VIR_STORAGE_NET_PROTOCOL_VXHS
Add a new virStorageNetProtocol for Veritas HyperScale (VxHS) disks

Signed-off-by: Ashish Mittal <Ashish.Mittal@veritas.com>
Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-09-19 21:10:21 -04:00
Laine Stump
747116e0b9 util: virPCIGetNetName(): use first netdev name when phys_port_id isn't matched
The mlx4 (Mellanox) netdev driver implements the sysfs phys_port_id
file for both VFs and PFs, so you can find the VF netdev plugged into
the same physical port as any given PF netdev by comparing the
contents of phys_port_id of the respective netdevs. That's what
libvirt does when attempting to find the PF netdev for a given VF
netdev (or vice versa).

Most other netdev's drivers don't implement phys_port_id, so the file
is visible in sysfs directory listing, but attempts to read it result
in ENOTSUPP. In these cases, libvirt is unable to read phys_port_id of
either the PF or the VF, so it just returns the first entry in the
PF/VF's list of netdevs.

But we've found that the i40e driver is in between those two
situations - it implements phys_port_id for PF netdevs, but doesn't
implement it for VF netdevs. So libvirt would successfully read the
phys_port_id of the PF netdev, then try to find a VF netdev with
matching phys_port_id, but would fail because phys_port_id is NULL for
all VFs. This would result in a message like the following:

   Could not find network device with phys_port_id '3cfdfe9edc39'
   under PCI device at /sys/class/net/ens4f1/device/virtfn0

To solve this problem in a way that won't break functionality for
anyone else, this patch saves the first netdev name we find for the
device, and returns that if we fail to find a netdev with the desired
phys_port_id.
2017-09-19 20:41:21 -04:00
Ján Tomko
cdd6eb99c2 configure: fix check for DEVLINK_CMD_ESWITCH_GET
Instead of checking for all possible constants that every
kernel header with devlink support should have (and defining
HAVE_DECL_DEVLINK as 1 if any of them is present due to the
way AC_CHECK_DECLS works), only check for DEVLINK_CMD_ESWITCH_GET.

This is the name of the constant since kernel 4.11. Between 4.8
and 4.11, the now deprecated spelling DEVLINK_CMD_ESWITCH_MODE_GET
was used.

Assume DEVLINK_ESWITCH_MODE_SWITCHDEV is available, since it was
introduced along with the deprecated spelling.
2017-09-19 15:25:10 +02:00
Nikolay Shirokovskiy
776b9ac594 iohelper: reduce zero-out in align case
We only need to zero-out bytes that will be written.
May be we even don't need to zero-out at all because
of immediate truncate.
2017-09-19 11:37:24 +02:00
Nikolay Shirokovskiy
f830e371ef iohelper: simplify last direct write alignment
Make alignment of last direct write more straightforward. Using
additionally two flags 'end' and 'shortRead' looks complicated.
2017-09-19 11:37:20 +02:00
Nikolay Shirokovskiy
3b8a0f6ac2 iohelper: drop unused operation length limit 2017-09-19 11:37:15 +02:00
Edan David
8708ca01c0 nodedev: add switchdev to NIC capabilities
Adding functionality to libvirt that will allow querying the interface
for the availability of switchdev Offloading NIC capabilities.

The switchdev mode was introduced in kernel 4.8, the iproute2-devlink
command to retrieve the switchdev NIC feature with command example:

    devlink dev eswitch show pci/0000:03:00.0

This feature is needed for Openstack so we can do a scheduling decision
if the NIC is in Hardware Offload (switchdev) or regular SR-IOV (legacy) mode.
And select the appropriate hypervisors with the requested capability see [1].

[1] - https://specs.openstack.org/openstack/nova-specs/specs/pike/approved/enable-sriov-nic-features.html

Reviewed-by: Laine Stump <laine@laine.org>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-09-18 08:32:24 -04:00
Peter Krempa
8333e7455e util: error: Add helpers for saving and restoring of last error
Some cleanup paths overwrite a usefull error message with a less useful
one and we then try to preserve the original message. The handlers added
in this patch will simplify the operations since they are designed right
for the purpose.
2017-09-15 05:28:21 +02:00
Guido Günther
9c0d66bb07 storagefile: fix defintion vs definition typo 2017-09-11 18:17:22 +02:00
Stefan Berger
dfbb15b754 tpm: Use /dev/null for cancel path if none was found
TPM 2 does not implement sysfs files for cancellation of commands.
We therefore use /dev/null for the cancel path passed to QEMU.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Tested-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
2017-09-07 14:00:46 +02:00
Daniel P. Berrange
5f5c515bbd event: ignore attempts to replace the event loop impl
Although not previously explicitly documented, the expectation for
the libvirt event loop is that an implementation is registered early
in application startup, before calling any libvirt APIs and then
run forever after. Replacing a previously registered event loop is
not safe & subject to races even if virConnectClose has been called
on open handles, due to delayed deregistration of callbacks during
conenction close.

Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-09-04 13:06:24 +01:00
Martin Kletzander
323f7c476e util: Use virBufferCheckError to its full potential.
We can now check for the error and not care about the return value as
it will be properly handled in virBufferContentAndReset() anyway.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-29 11:17:16 +02:00
Martin Kletzander
7addfa195b util: Umark virBufferCheckErrorInternal as ATTRIBUTE_RETURN_CHECK
The function is useful even without using the return value.  And if
needed, the return value can be obtained by other calls as well.  The
potential for clean-up can be seen in the following patch.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-29 11:17:16 +02:00
Erik Skultety
b988f794e3 daemon: logging: Fix --verbose option being ignored by the daemon
Commit 94c465d0 refactored the logging setup phase but introduced an
issue, where the daemon ignores verbose mode when there are no outputs
defined and the default must be used. The problem is that the default
output was determined too early, thus ignoring the potential '--verbose'
option taking effect. This patch postpones the creation of the default
output to the very last moment when nothing else can change. Since the
default output is only created during the init phase, it's safe to leave
the pointer as NULL for a while, but it will be set eventually, thus not
affecting runtime.
Patch also adjusts both the other daemons.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1442947

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-08-28 16:42:13 +02:00
Pavel Hrdina
4e56a3e793 util: introduce virXMLFormatElement helper
This helper allows you to better structurize the code if some element
may or may not contains attributes and/or child elements.

Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-28 14:02:44 +02:00
Pavel Hrdina
ef3ea17f62 util: introduce virBufferSetChildIndent macro
Reviewed-by: John Ferlan <jferlan@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-28 14:02:34 +02:00
Peter Krempa
c0ce84ca98 util: string: Introduce virStringHasChars
The helper returns true if a string contains any of the given chars.
virStringHasControlChars can be reimplemented using that helper.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-18 14:20:37 +02:00
Pavel Hrdina
561726cd7f util: introduce virXMLNodeContentString
It's equivalent of calling virXPathString("string(.)", ctxt) but it
doesn't have to use the XPath resolving and parsing.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-17 15:42:23 +02:00
Pavel Hrdina
827cf58d50 util: introduce virXMLPropStringLimit
The virXMLPropStringLimit is an equivalent of virXPathStringLimit
which should be preferred if you already have a XML dom node or
if you need to parse more than one property.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-17 15:42:08 +02:00
John Ferlan
070b6f7f55 network: Move and rename networkMacMgrFileName
Move networkMacMgrFileName into src/util/virmacmap.c and rename to
virMacMapFileName. We're about to move some more MacMgr processing
files into virnetworkobj and it doesn't make sense to have this helper
in the driver or in virnetworkobj.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-16 14:17:57 -04:00
John Ferlan
d3e17259e1 util: Add object checking for virObject{Ref|Unref}
Rather than assuming that what's passed to virObject{Ref|Unref}
would be a virObjectPtr as long as it's not NULL, let's do the
similar checks virObjectIsClass in order to prevent a possible
increment or decrement to some field at the obj->u.s.refs offset.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
dfa0efbb77 util: Add magic number check for object validity
The virObjectIsClass API has only ever checked object validity
based on if the @obj is not NULL and it was derived from some class.
While this has worked well in general, there is one additional
check that could be made prior to calling virClassIsDerivedFrom
which loops through the classes checking the magic number against
the klass expected magic number.

If by chance a non virObject is passed, rather than assuming the
void * @obj is a _virObject and thus offsetting to obj->klass,
obj->magic, and obj->parent, let's check that the void * @obj
has at least the "base part" of the magic number in the right
place and generate a more specific VIR_WARN message if not.

There are many consumers to virObjectIsClass, include the locking
primitives virObject{Lock|Unlock}, virObjectRWLock{Read|Write},
and virObjectRWUnlock. For those callers, the locking call will
not fail, but it also will not attempt a virMutex* call which
will "most likely" fail since the &obj->lock is used.

In order to avoid some possible future wrap on the 0xCAFExxxx
value, add a check during initialization that some new class
won't cause the wrap. Should be good for a few years at least!

It is still left up to the caller to handle the failed API calls
just as it would be if it passed a NULL opaque pointer anyobj.
2017-08-14 21:41:59 -04:00
John Ferlan
19f4395230 util: Create common error path for invalid object
If virObjectIsClass fails "internally" to virobject.c, create a
macro to generate the VIR_WARN describing what the problem is.
Also improve the checks and message a bit to indicate which was
the failure - whether the obj was NULL or just not the right class

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
045d712c4b util: Introduce and use virObjectRWUnlock
Rather than overload virObjectUnlock as commit id '77f4593b' has
done, create a separate virObjectRWUnlock API that will force the
consumers to make the proper decision regarding unlocking the
RWLock's. Similar to the RWLockRead and RWLockWrite, use the
virObjectGetRWLockableObj helper. This restores the virObjectUnlock
code to using the virObjectGetLockableObj.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
bf09f002b1 util: Introduce virObjectGetRWLockableObj
Introduce a helper to handle the error path more cleanly. The same
as virObjectGetLockableObj in order to essentially follow the original
logic of commit 'b545f65d' to ensure that the input argument at least
has some validity before using.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
8b03a609dd util: Only have virObjectLock handle virObjectLockable
Now that virObjectRWLockWrite exists to handle the virObjectRWLockable
objects, let's restore virObjectLock to only handle virObjectLockable
class locks. There still exists the possibility that the input @anyobj
isn't a valid object and the resource isn't truly locked, but that
also exists before commit id '77f4593b'.

This also restores some logic that commit id '77f4593b' removed
with respect to a common code path that commit id '10c2bb2b' had
introduced as virObjectGetLockableObj. This code path merely does
the same checks as the original virObjectLock commit 'b545f65d',
but in callable/reusable helper to ensure the @obj at least has
some validity before using.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
908b33644e util: Introduce and use virObjectRWLockWrite
Instead of making virObjectLock be the entry point for two
different types of locks, let's create a virObjectRWLockWrite API
which will only handle the virObjectRWLockableClass objects.

Use the new virObjectRWLockWrite for the virdomainobjlist code
in order to handle the Add, Remove, Rename, and Load operations
that need to be very synchronous.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
John Ferlan
99a72b3eb4 util: Rename virObjectLockRead to virObjectRWLockRead
Since the class it represents is based on virObjectRWLockableClass
and in order to make sure we differentiate just in case anyone somehow
believes they could use virObjectLockRead for a virObjectLockableClass,
let's rename the API to use the RW in the name. Besides the RW locks
refer to pthread_rwlock_{init|rdlock|wrlock|unlock|destroy} while the
other locks refer to pthread_mutex_{init|lock|unlock|destroy}.

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-08-14 21:41:59 -04:00
Pavel Hrdina
5bd8a1c5d8 util: introduce virXMLNodeNameEqual
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-08-14 14:31:52 +02:00
Martin Kletzander
a045317680 util: Rename virResctrl to virResctrlInfo
This way later patches can add another structures with virResctrl
prefix without the meaning being even more confusing than it needs to
be.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-14 10:01:12 +02:00
Martin Kletzander
7c4b4f8905 util: Make virResctrlGetCacheControlType() behave like other functions
That means that returning negative values means error and non-negative
values differ in meaning, but are all successful.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-14 10:01:12 +02:00
Martin Kletzander
af4270400a Move resctrl-related code from conf/capabilities to util/virresctrl
It doesn't access anything from conf/ and ti will be needed to use
from other util/ places.  This split makes the separation clearer.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-14 10:01:12 +02:00
Martin Kletzander
62146d8532 virxml: Fix indentation
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-08-14 10:01:12 +02:00
Laine Stump
f5bc8b5436 util: eliminate superfluous saveVlan check in virNetDevSetNetConfig()
Commit 81fb440b further qualified an if statement by adding the
boolean saveVlan to the condition. Coverity pointed out that this
change in the logic eliminated the need to check saveVlan in an
argument to virAsprintf().
2017-08-13 23:07:13 -04:00
Laine Stump
83074cc917 util: fix improper assignment of return value in virHostdevReadNetConfig()
Commit 9a94af6d restructured virHostdevReadNetConfig() so that it
would manually set ret = 0 after successfully reading the device's
config, but Coverity pointed out that "ret = 0" was erroneously placed
outside of an "else" clause, meaning that the the value of ret set in
the "if" clause was unnecessarily and incorrectly overwritten.

This patch moves ret = 0 into the else clause, which should silence
Coverity.
2017-08-13 23:07:13 -04:00
Laine Stump
489a937eb4 util: check for PF online status earlier in guest startup
When using a VF from an SRIOV-capable network card in a guest (either
in macvtap passthrough mode, or via VFIO PCI device assignment), The
associated PF netdev must be online in order for the VF to be usable
by the guest. The guest, however, is not able to change the state of
the PF. And libvirt *could* set the PF online as needed, but that
could lead to the host receiving unexpected IPv6 traffic (since the
default for an unconfigured interface is to participate in IPv6
autoconf). For this reason, before assigning a VF to a guest, libvirt
verifies that the related PF netdev is online - if it isn't, then we
log an error and don't allow the guest startup to continue.

Until now, this check was done during virNetDevSetNetConfig(). This
works nicely because the same function is called both for macvtap
passthrough and for VFIO device assignment. But in the case of VFIO,
the VF has already been unbound from its netdev driver by the time we
get to virNetDevSetNetConfig(), and in the case of dual port Mellanox
NICs that have their VFs setup in single port mode, the *only* way to
determine the proper PF netdev to query for online status is via the
"phys_port_id" file that is in the VF netdev's sysfs directory. *BUT*
if we've unbound the VF from the netdev driver, then it doesn't *have*
a netdev sysfs directory.

So, in order to check the correct PF netdev for online status, this
patch moved the check earlier in the setup, into
virNetDevSaveNetConfig(), which is called *before* unbinding the VF
from its netdev driver.

(Note that this implies that if you are using VFIO device assignment
for the VFs of a Mellanox NIC that has the VFs programmed in single
port mode, you must let the VFs be bound to their net driver and use
"managed='yes'" in the device definition. To be more specific, this is
only true if the VFs in single port mode are using port *2* of the PF
- if the VFs are using only port 1, then the correct PF netdev will be
arrived at by default/chance))

  This resolves: https://bugzilla.redhat.com/267191
2017-08-11 19:13:33 -04:00
Laine Stump
9a08168301 util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.

This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).

To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-11 19:09:49 -04:00
Laine Stump
b67eaa6351 util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.

These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.

When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)

When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.

Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.

(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')

By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.

When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-11 19:05:20 -04:00
Laine Stump
39d136b67b util: match phys_port_id when converting PF-netdev to/from VF-netdev
This patch updates functions in netdev.c to pay attention to
phys_port_id. It uses the new function virNetDevGetPhysPortID() to
learn the phys_port_id of a VF or PF, then sends that info to
virPCIGetNetName(), which has newly been modified to take an optional
phys_port_id.
2017-08-11 18:55:25 -04:00
Laine Stump
b3b5aa75ed util: make virPCIGetNetName() more versatile
A single PCI device may have multiple netdevs associated with it. Each
of those netdevs will have a different phys_port_id entry in
sysfs. This patch modifies virPCIGetNetName() to allow selecting one
of the potential many netdevs in two different ways:

1) by setting the "idx" argument, the caller can select the 1st (0),
2nd (1), etc. netdev from the PCI device's net subdirectory.

2) If the physPortID arg is set (to a null-terminated string) then
virPCIGetNetName() returns the netdev that has that phys_port_id in
the sysfs file of the same name in the netdev's directory.
2017-08-11 18:35:09 -04:00
Laine Stump
0dc67e6d2d util: Fix const'ness of 1st arg to virPCIGetNetName()
The first arg isn't modified in the function, so it should be const.
2017-08-11 18:30:14 -04:00
Laine Stump
48f33bb5df util: new function virNetDevGetPhysPortID()
On Linux each network device *can* (but not necessarily *does*) have
an attribute called phys_port_id which can be read from the file of
that name in the netdev's sysfs directory. The examples I've seen have
been a many-digit hexadecimal number (as an ASCII string).

This value can be useful when a single PCI device is associated with
multiple netdevs (e.g a dual port Mellanox SR-IOV NIC - this card has
a single PCI Physical Function (PF), and that PF has two netdevs
associated with it (the "net" subdirectory of the PF in sysfs has two
links rather than the usual single link to a netdev directory). Each
of the PF netdevs has a different phys_port_id. The Virtual Functions
(VF) are similar - the PF (a PCI device) has "n" VFs (also each of
these is a PCI device), each VF has two netdevs, and each of the VF
netdevs points back to the VF PCI device (with the "device" entry in
its sysfs directory) as well as having a phys_port_id matching the PF
netdev it is associated with.

virNetDevGetPhysPortID() simply attempts to read the phys_port_id for
the given netdev and return it to the caller. If this particular
netdev driver doesn't support phys_port_id, it returns NULL (*not* a
NULL-terminated string, but a NULL pointer) but still counts it as a
success.
2017-08-11 18:25:00 -04:00
Ján Tomko
e9f3222705 introduce virConfReadString
Rewrite virConfReadMem to take a null-terminated string.
All the callers were calling strlen on it anyway.
2017-08-08 12:19:17 +02:00
Peter Krempa
0b1ecf7b53 util: hash: Make virHashCodeGen mockable
Export the function from the util module so that dynamic linking can
override it.
2017-08-03 09:49:15 +02:00
Peter Krempa
8982f3ab20 util: hash: Include stdbool.h in the header file
The functions declared in virhash.h return bool, but stdbool.h was not
included.
2017-08-03 09:49:15 +02:00
Michal Privoznik
3e609bf4e4 virCgroupValidateMachineGroup: Don't free @machinename
We are given a string in @machinename, we never allocate it, just
merely use it for reading. We should not free it otherwise it
leads to double free:

==32191== Thread 17:
==32191== Invalid free() / delete / delete[] / realloc()
==32191==    at 0x4C2D1A0: free (vg_replace_malloc.c:530)
==32191==    by 0x54BBB84: virFree (viralloc.c:582)
==32191==    by 0x2BC04499: qemuProcessStop (qemu_process.c:6313)
==32191==    by 0x2BC500FF: processMonitorEOFEvent (qemu_driver.c:4724)
==32191==    by 0x2BC502FC: qemuProcessEventHandler (qemu_driver.c:4769)
==32191==    by 0x5550640: virThreadPoolWorker (virthreadpool.c:167)
==32191==    by 0x554FBCF: virThreadHelper (virthread.c:206)
==32191==    by 0x8F913D3: start_thread (in /lib64/libpthread-2.23.so)
==32191==    by 0x928DE3C: clone (in /lib64/libc-2.23.so)
==32191==  Address 0x31893d70 is 0 bytes inside a block of size 1,100 free'd
==32191==    at 0x4C2D1A0: free (vg_replace_malloc.c:530)
==32191==    by 0x54BBB84: virFree (viralloc.c:582)
==32191==    by 0x54C1936: virCgroupValidateMachineGroup (vircgroup.c:343)
==32191==    by 0x54C4B29: virCgroupNewDetectMachine (vircgroup.c:1550)
==32191==    by 0x2BBDDA29: qemuConnectCgroup (qemu_cgroup.c:972)
==32191==    by 0x2BC05DA7: qemuProcessReconnect (qemu_process.c:6822)
==32191==    by 0x554FBCF: virThreadHelper (virthread.c:206)
==32191==    by 0x8F913D3: start_thread (in /lib64/libpthread-2.23.so)
==32191==    by 0x928DE3C: clone (in /lib64/libc-2.23.so)
==32191==  Block was alloc'd at
==32191==    at 0x4C2BE80: malloc (vg_replace_malloc.c:298)
==32191==    by 0x4C2E35F: realloc (vg_replace_malloc.c:785)
==32191==    by 0x54BB492: virReallocN (viralloc.c:245)
==32191==    by 0x54BEDF2: virBufferGrow (virbuffer.c:150)
==32191==    by 0x54BF3B9: virBufferVasprintf (virbuffer.c:408)
==32191==    by 0x54BF324: virBufferAsprintf (virbuffer.c:381)
==32191==    by 0x55BB271: virDomainGenerateMachineName (domain_conf.c:27078)
==32191==    by 0x2BBD5B8F: qemuDomainGetMachineName (qemu_domain.c:9595)
==32191==    by 0x2BBDD9B4: qemuConnectCgroup (qemu_cgroup.c:966)
==32191==    by 0x2BC05DA7: qemuProcessReconnect (qemu_process.c:6822)
==32191==    by 0x554FBCF: virThreadHelper (virthread.c:206)
==32191==    by 0x8F913D3: start_thread (in /lib64/libpthread-2.23.so)

Moreover, make the @machinename 'const char *' to mark it
explicitly that we are not changing the passed string.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-08-01 11:37:51 +02:00
Peter Krempa
c61d169327 util: storagefile: rename 'nodebacking' to 'nodestorage' in virStorageSource
Make it less confusing by naming the field which refers to the storage
object as 'nodestorage'.

Reviewed-by: Eric Blake <eblake@redhat.com>
2017-07-27 09:44:05 +02:00
Peter Krempa
3c60388591 util: buffer: Add virBufferStrcatVArgs
Split out the worker loop into a separate function and export it.

Reviewed-by: Eric Blake <eblake@redhat.com>
2017-07-27 09:31:14 +02:00
Pavel Hrdina
ac3eb2ab24 util: introduce virFileCache
The new virFileCache will nicely handle the caching logic for any data
that we would like to cache.  For each type of data we will just need
to implement few handlers that will take care of creating, validating,
loading and saving the cached data.

The cached data must be an instance of virObject.

Currently we cache QEMU capabilities which will start using
virFileCache.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
2017-07-26 15:31:25 +02:00
Martin Kletzander
eaf2c9f891 Move machineName generation from virsystemd into domain_conf
It is more related to a domain as we might use it even when there is
no systemd and it does not use any dbus/systemd functions.  In order
not to use code from conf/ in util/ pass machineName in cgroups code
as a parameter.  That also fixes a leak of machineName in the lxc
driver and cleans up and de-duplicates some code.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-07-25 17:02:27 +02:00
Michal Privoznik
2d3c7122c8 Revert "virthread: Introduce virRWLockInitPreferWriter"
This reverts commit 328bd24443.

As it turns out, this is not portable and very Linux & glibc
specific. Worse, this may lead to not starving writers on Linux
but everywhere else. Revert this and if the starvation occurs
resolve it.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
2017-07-25 10:56:03 +02:00
Michal Privoznik
77f4593b09 virobject: Introduce virObjectRWLockable
Up until now we only had virObjectLockable which uses mutexes for
mutually excluding each other in critical section. Well, this is
not enough. Future work will require RW locks so we might as well
have virObjectRWLockable which is introduced here.

Moreover, polymorphism is introduced to our code for the first
time. Yay! More specifically, virObjectLock will grab a write
lock, virObjectLockRead will grab a read lock then (what a
surprise right?). This has great advantage that an object can be
made derived from virObjectRWLockable in a single line and still
continue functioning properly (mutexes can be viewed as grabbing
write locks only). Then just those critical sections that can
grab a read lock need fixing. Therefore the resulting change is
going to be way smaller.

In order to avoid writer starvation, the object initializes RW
lock that prefers writers.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
2017-07-24 15:54:06 +02:00
Michal Privoznik
328bd24443 virthread: Introduce virRWLockInitPreferWriter
We already have virRWLockInit. But this uses pthread defaults
which prefer reader to initialize the RW lock. This may lead to
writer starvation. Therefore we need to have the counterpart that
prefers writers. Now, according to the
pthread_rwlockattr_setkind_np() man page setting
PTHREAD_RWLOCK_PREFER_WRITER_NP attribute is no-op. Therefore we
need to use PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
attribute. So much for good enum value names.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
2017-07-24 15:54:06 +02:00
Peter Krempa
97ea8da183 virStorageNetHostDef: Turn @port into integer
Currently, @port is type of string. Well, that's overkill and
waste of memory. Port is always an integer. Use it as such.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-07-24 10:55:44 +02:00
Peter Krempa
8444419f8c util: storage: fill in default ports when parsing backing chain
Similarly to when parsing XML we need to fill in default ports for the
backing chain. This was missed in commit 5bda835466
2017-07-24 10:55:43 +02:00
Peter Krempa
1f920b9f02 util: uri: Convert port number to unsigned integer
Negative ports don't make sense so use a unsigned integer.
2017-07-24 10:55:43 +02:00
Peter Krempa
e8b69016b1 qemu: command: Rename and move qemuNetworkDriveGetPort
Move it to virstring.c and improve it to parse and validate ports. New
name is virStringParsePort.
2017-07-24 10:55:20 +02:00
Peter Krempa
a908e9e45e util: bitmap: Modify virBitmapSubtract to virBitmapIntersect
Since virBitmapSubtract is unused modify it to perform bitmap
intersection.
2017-07-20 16:14:50 +02:00
Antoine Millet
e484cb3eca Handle hotplug change on VLAN configuration using OVS
A new function virNetDevOpenvswitchUpdateVlan has been created to instruct
OVS of the changes. qemuDomainChangeNet has been modified to handle the
update of the VLAN configuration for a running guest and rely on
virNetDevOpenvswitchUpdateVlan to do the actual update if needed.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-07-20 15:15:03 +02:00
Antoine Millet
695611f99e virnetdevopenvswitch: Move OVS VLAN configuration to a separate function
This piece of code is going to be reused. So move it out to a
separate function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-07-20 15:15:03 +02:00
Pavel Hrdina
38e516a524 util/virhash: add name parameter to virHashSearch
While searching for an element using a function it may be
desirable to know the element key for future operation.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
2017-07-20 14:02:14 +02:00
Michal Privoznik
8ae82e676a virFileInData: Report an error if unable to reposition file
The purpose of this function is to tell if the current position
in given FD is in data section or a hole and how much bytes there
is remaining until the end of the section. This is achieved by
couple of lseeks(). The most important part is that we reposition
the FD back, so that the position is unchanged from the caller
POV. And until now the final lseek() back to the original
position was done with no check for errors. And I was convinced
that that's okay since nothing can go wrong. However, review
feedback from a related series persuaded me, that it's better to
be safe than sorry. Therefore, lets check if the final lseek()
succeeded and if it doesn't report an error.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-07-19 09:46:17 +02:00
Peter Krempa
9756884d14 conf: Pre-fill default ports when parsing network disk sources
Fill them in right away rather than having to figure out at runtime
whether they are necessary or not.

virStorageSourceNetworkDefaultPort does not need to be exported any
more.
2017-07-14 16:05:46 +02:00
Peter Krempa
5bda835466 util: storage: Fill in default ports for gluster and iscsi
Our documentation provides them, so the helper should return them.
2017-07-14 16:05:46 +02:00
Peter Krempa
34ffc2ff41 util: Extract helper to retrieve default port for network protocol
Make the stuff hardcoded in qemu a global helper so that other parts of
the code can determine the default port too.
2017-07-14 16:05:46 +02:00
Daniel P. Berrange
407a281a8e Revert "Prevent more compiler optimization of mockable functions"
This reverts commit e4b980c853.

When a binary links against a .a archive (as opposed to a shared library),
any symbols which are marked as 'weak' get silently dropped. As a result
when the binary later runs, those 'weak' functions have an address of
0x0 and thus crash when run.

This happened with virtlogd and virtlockd because they don't link to
libvirt.so, but instead just libvirt_util.a and libvirt_rpc.a. The
virRandomBits symbols was weak and so left out of the virtlogd &
virtlockd binaries, despite being required by virHashTable functions.

Various other binaries like libvirt_lxc, libvirt_iohelper, etc also
link directly to .a files instead of libvirt.so, so are potentially
at risk of dropping symbols leading to a later runtime crash.

This is normal linker behaviour because a weak symbol is not treated
as undefined, so nothing forces it to be pulled in from the .a You
have to force the linker to pull in weak symbols using -u$SYMNAME
which is not a practical approach.

This risk is silent bad linkage that affects runtime behaviour is
not acceptable for a fix that was merely trying to fix the test
suite. So stop using __weak__ again.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-07-13 13:07:06 +01:00
Martin Kletzander
1701ba6fdc util: Don't leak linksrc in vircgroup
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-07-13 13:14:23 +02:00
Juan Hernandez
dacd160d74 Avoid hidden cgroup mount points
Currently the scan of the /proc/mounts file used to find cgroup mount
points doesn't take into account that mount points may hidden by other
mount points. For, example in certain Kubernetes environments the
/proc/mounts contains the following lines:

  cgroup /sys/fs/cgroup/net_prio,net_cls cgroup ...
  tmpfs /sys/fs/cgroup tmpfs ...
  cgroup /sys/fs/cgroup/net_cls,net_prio cgroup ...

In this particular environment the first mount point is hidden by the
second one. The correct mount point is the third one, but libvirt will
never process it because it only checks the first mount point for each
controller (net_cls in this case). So libvirt will try to use the first
mount point, which doesn't actually exist, and the complete detection
process will fail.

To avoid that issue this patch changes the virCgroupDetectMountsFromFile
function so that when there are duplicates it takes the information from
the last line in /proc/mounts. This requires removing the previous
explicit condition to skip duplicates, and adding code to free the
memory used by the processing of duplicated lines.

Related-To: https://bugzilla.redhat.com/1468214
Related-To: https://github.com/kubevirt/libvirt/issues/4
Signed-off-by: Juan Hernandez <jhernand@redhat.com>
2017-07-13 09:37:52 +02:00
Daniel P. Berrange
e4b980c853 Prevent more compiler optimization of mockable functions
Currently all mockable functions are annotated with the 'noinline'
attribute. This is insufficient to guarantee that a function can
be reliably mocked with an LD_PRELOAD. The C language spec allows
the compiler to assume there is only a single implementation of
each function. It can thus do things like propagating constant
return values into the caller at compile time, or creating
multiple specialized copies of the function body each optimized
for a different caller. To prevent these optimizations we must
also set the 'noclone' and 'weak' attributes.

This fixes the test suite when libvirt.so is built with CLang
with optimization enabled.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-07-11 13:57:12 +01:00
Daniel P. Berrange
d8f8c7a83d Remove network constants out of internal.h
The HOST_NAME_MAX, INET_ADDRSTRLEN and VIR_LOOPBACK_IPV4_ADDR
constants are only used by a handful of files, so are better
kept in virsocketaddr.h or the source file that uses them.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-07-11 13:57:11 +01:00
Peter Krempa
6d7cdec63d util: storage: Always deflatten JSON pseudo-protocol objects
Now that the JSON deflattener is working sanely we can always attempt
the deflattening so that we can then parse the tree as expected.
2017-07-11 14:23:08 +02:00
Peter Krempa
428d175206 util: json: Recursively deflatten objects virJSONValueObjectDeflatten
If a value of the first level object contains more objects needing
deflattening which would be wrapped in an actual object the function
would not recurse into them.

By this simple addition we can fully deflatten the objects.
2017-07-11 14:20:05 +02:00
Peter Krempa
d40f4b3e67 util: json: Properly implement JSON deflattening
As it turns out sometimes users pass in an arbitrarily nested structure
e.g. for the qemu backing chains JSON pseudo protocol. This new
implementation deflattens now a single object fully even with nested
keys.

Additionally it's not necessary now to stick with the "file." prefix for
the properties.
2017-07-11 14:13:35 +02:00
Peter Krempa
f43b7d60d8 util: json: Don't remove the 'file' subobject when deflattening
Currently the function would deflatten the object by dropping the 'file'
prefix from the attributes. This does not really scale well or adhere to
the documentation.

Until we refactor the worker to properly deflatten everything we at
least simulate it by adding the "file" wrapper object back.
2017-07-11 14:10:31 +02:00
Peter Krempa
de75de7c97 util: Move JSON object deflattening code to json utility file
The code will become more universal so it makes more sense for it to
live with the rest of the JSON functions.
2017-07-11 14:02:28 +02:00
Peter Krempa
cadd96b3ea util: json: Add virJSONValueIsObject
Allows testing whether a virJSONValue is an object.
2017-07-11 14:02:28 +02:00
Cédric Bosdonnat
0980764dee util: share code between virExec and virCommandExec
virCommand is a version of virExec that doesn't fork, however it is
just calling execve and doesn't honors setting uid/gid and pwd.

This commit extrac those pieces from virExec() to a virExecCommon()
function that is called from both virExec() and virCommandExec().
2017-07-11 10:41:24 +02:00
Michal Privoznik
0fe4aa149f fdstream: Report error from the I/O thread
Problem with our error reporting is that the error object is a
thread local variable. That means if there's an error reported
within the I/O thread it gets logged and everything, but later
when the event loop aborts the stream it doesn't see the original
error. So we are left with some generic error. We can do better
if we copy the error message between the threads.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-07-11 08:41:01 +02:00
Michal Privoznik
3a2ca2fbe4 virfdstream: Check for thread error more frequently
When the I/O thread quits (e.g. due to an I/O error, lseek()
error, whatever), any subsequent virFDStream API should return
error too. Moreover, when invoking stream event callback, we must
set the VIR_STREAM_EVENT_ERROR flag so that the callback knows
something bad happened.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-07-11 08:40:13 +02:00
Peter Krempa
d077fbc221 util: netdevbridge: Refactor error handling in virNetDevBridgeCreate
Replace the switch statement with a simpler if statement. This also
removes the fallthrough path that coverity was complaining about.
2017-06-28 15:27:17 +02:00
Martin Kletzander
8110b4e073 util: Extract locale-related fixes into separate functions
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
2017-06-26 09:20:08 +02:00
John Ferlan
5431055d2b util: Force reading of meta data to get encryption capacity value
https://bugzilla.redhat.com/show_bug.cgi?id=1371892

As it turns out the volume create, build, and refresh path was not peeking
at the meta data, so immediately after a create operation the value displayed
for capacity was still incorrect. However, if a pool refresh was done the
correct value was fetched as a result of a meta data peek.

The reason is it seems historically if the file type is RAW then peeking
at the file just took the physical value for the capacity. However, since
we know if it's an encrypted file, then peeking at the meta data will be
required in order to get a true capacity value.

So check for encryption in the source and if present, use the meta data
in order to fill in the capacity value and set the payload_offset.
2017-06-24 06:43:25 -04:00
Michal Privoznik
edaf135657 virNetDevOpenvswitchInterfaceStats: Be more forgiving when fetching stats
https://bugzilla.redhat.com/show_bug.cgi?id=1461270

When fetching stats for a vhost-user type of interface, we run
couple of ovs-vsctl commands and parse their output. However, not
all stats exist at all times, for instance "rx_dropped" or
"tx_errors" can be missing. Thing is, we ask for a bulk of
statistics and if one of them is missing an error is reported
instead of returning the rest. Since we ignore errors, we fail to
set statistics. Fix this by asking for each piece alone.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-06-23 11:51:23 +02:00
John Ferlan
10c2bb2b19 util: Introduce virObjectGetLockableObj
Split out the object fetch in virObject{Lock|Unlock} into a helper

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-06-22 09:34:40 -04:00
John Ferlan
209a95e354 util: Formatting cleanups to virobject API
Alter to use more recent formatting guidelines

Signed-off-by: John Ferlan <jferlan@redhat.com>
2017-06-22 09:34:40 -04:00
Martin Kletzander
c9d1e5951c util: Move locale.h include from virutil to virstring
Commit 5c54d29aae forgot to do that when moving the only function
using it and it broke the build on some platforms.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-06-22 14:30:27 +02:00
Julio Faracco
96a9b9a7f0 util: fix locale problem with virStrToDouble().
This commit fixes a locale problem with locales that use comma as a mantissa
separator. Example: 12.34 en_US = 12,34 pt_BR. Since strtod() is a non-safe
function, virStrToDouble() will have problems to parse double numbers from
kernel settings and other double numbers from static files (XMLs, JSONs, etc).

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1457634
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1457481

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
2017-06-22 11:30:20 +02:00
Julio Faracco
5c54d29aae util: moving virDoubleToStr() from virutil to virstring.
The function virDoubleToStr() is defined in virutil.* and virStrToDouble() is
defined in virstring.*. Joining both functions into the same file makes more
sense.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
2017-06-22 11:30:20 +02:00
Peter Krempa
296a53313f util: storage: Make @backingFormat optional in virStorageFileGetMetadataInternal
Some callers don't need to know the backing format. Make the argument
optional by using a dummy int if NULL is passed.
2017-06-20 16:50:26 +02:00
Peter Krempa
e4c3eff70e util: storage: Export virStorageIsRelative 2017-06-20 13:25:55 +02:00
Peter Krempa
b16133b114 util: storage: adapt to changes in JSON format for sheepdog
Since qemu 2.9 the options changed from a monolithic string into fine
grained options for the json pseudo-protocol object.
2017-06-20 08:40:18 +02:00
Peter Krempa
ea2c418ac3 util: storage: adapt to changes in JSON format for ssh
Since qemu 2.9 the options changed from a monolithic string into fine
grained options for the json pseudo-protocol object.
2017-06-20 08:40:18 +02:00
Peter Krempa
4fac5a1935 util: storage: adapt to changes in JSON format for ceph/rbd
Since qemu 2.9 the options changed from a monolithic string into fine
grained options for the json pseudo-protocol object.
2017-06-20 08:40:18 +02:00
Peter Krempa
35d23f90b2 util: storage: adapt to changes in JSON format for NBD
Since 2.9 the host and port for NBD are no longer directly under the
json pseudo-protocol object, but rather belong to a sub-object called
'server'.
2017-06-20 08:40:18 +02:00
Peter Krempa
b24bc54080 util: storage: Add JSON parser for new options in iSCSI protocol
Starting from qemu 2.9, more granular options are supported. Add parser
for the relevant bits.

With this patch libvirt is able to parse the host and target IQN of from
the JSON pseudo-protocol specification.

This corresponds to BlockdevOptionsIscsi in qemu qapi.
2017-06-20 08:40:18 +02:00
Peter Krempa
299aff7e0c util: storage: Report errors when source host data is missing
Merge the reporting of the missing source host data into the parser
functions so that callers don't have to do it separately.
2017-06-20 08:40:18 +02:00
Peter Krempa
49ed98a457 util: storage: Split out parsing of TCP network host from JSON pseudoprotocol
Few backing protocols support only TCP. Split out the function which
will correspond to parsing qemu's InetSocketAddressBase.
2017-06-20 08:40:18 +02:00
Peter Krempa
1f915d40a2 util: storage: Add support for type 'inet' in virStorageSourceParseBackingJSONSocketAddress
'SocketAddress' structure was changed to contain 'inet' instead of
'tcp' since qemu commit c5f1ae3ae7b. Existing entries have a backward
compatibility layer.

Libvirt will parse 'inet' and 'tcp' as equivalents.
2017-06-20 08:40:18 +02:00
Peter Krempa
6402f402d4 util: storage: make virStorageSourceParseBackingJSONGlusterHost universal
The same json strucutre is used for NBD and sheepdog volumes for
specifying of the host. Rename the function and fix up error messages to
be more universal.
2017-06-19 15:50:29 +02:00
Peter Krempa
506b80c84e util: storage: Add missing return to virStorageSourceParseBackingJSONGluster
If the number of servers is not expected the code would report an error
but would not return failure.
2017-06-19 15:50:29 +02:00
Peter Krempa
236e1f7e8c util: storage: Output parsed network backing store string to debug log 2017-06-19 15:50:29 +02:00
Martin Kletzander
307a205e25 qemu: Allow live-updates of coalesce settings
Change the settings from qemuDomainUpdateDeviceLive() as otherwise the
call would succeed even though nothing has changed.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1414627

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-06-16 10:18:35 +02:00
Daniel P. Berrange
5e9ca5508d Use sys/uio.h for writev()
With glibc >= 2.25.90 writev() is only available if you explicitly
include sys/uio.h. This matches the documented requirements, but
older glibc and other *NIX pulled in writev indirectly so the bug
wasn't noticed previously.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-06-14 15:01:42 +01:00
Marc Hartmayer
adf846d3c9 Use ATTRIBUTE_FALLTHROUGH
Use ATTRIBUTE_FALLTHROUGH, introduced by commit
5d84f5961b, instead of comments to
indicate that the fall through is an intentional behavior.

Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.vnet.ibm.com>
Reviewed-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2017-06-12 19:11:30 -04:00
Pavel Hrdina
9fd816ed33 Revert "util: virqemu: introduce virQEMUBuildBufferEscape"
This reverts commit 22b02f4492.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2017-06-12 12:45:42 +02:00
Michal Privoznik
f3908d8557 virNetDevOpenvswitchGetVhostuserIfname: Fix off by one error
https://bugzilla.redhat.com/show_bug.cgi?id=1459091

We try to get the last element of the passed path by calling
strrch(path, '/'). However, the pointer that strrchr() returns
points at the slash, We want string that starts right after that.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-06-08 15:02:23 +02:00
Michal Privoznik
5004f121bc virFDStreamThread: Make sure we won't exceed @length
There's a problem with current streams after I switched them from
iohelper to thread implementation. Previously, iohelper made sure
not to exceed specified @length resulting in the pipe EOF
appearing at the exact right moment (the pipe was used to tunnel
the data from the iohelper to the daemon). Anyway, when switching
to thread I had to write the I/O code from scratch. Whilst doing
that I took an inspiration from the iohelper code, but since the
usage of pipe switched to slightly different meaning, there was
no 1:1 relationship between the codes.

Moreover, after introducing VIR_FDSTREAM_MSG_TYPE_HOLE, the
condition that should made sure we won't exceed @length was
completely wrong.

The fix is to:

a) account for holes for @length
b) cap not just data sections but holes too (if @length would be
exceeded)

For this purpose, the condition needs to be brought closer to the
code that handles holes and data sections.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-06-05 17:00:48 +02:00
Julio Faracco
4fd5c2fbce util: remove dead code inside virstoragefile
The host address or the socket path have already been checked at the
begining of the function virStorageSourceParseNBDColonString(). So,
when the parameter is not a unix socket, there is no reason to check
the address again because if it does not exists, the logic will fail
in the first IF conditional.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
2017-06-02 09:39:10 +02:00
Julio Faracco
54aee01d87 util: fix wrong comparison inside virStoragePermsCopy()
VIR_STRDUP returns -1 if the string copy was not successful. So, the
current comparison/logic is throwing an error when VIR_STRDUP() returns
1. Only when source is NULL, it is considering as a success which is
not right.

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
2017-06-02 09:37:44 +02:00
Nitesh Konkar
4ae0f65669 util: hostcpu: Correctly report total number of vcpus in virHostCPUGetMap
Callers expect the return value to be the total number of vcpus in the
host (including offline vcpus). The refactor in c67e04e25f
broke this assumption by using virHostCPUGetOnlineBitmap which only
creates a bitmap long enough to hold the last online vcpu.

Report the full number of host vcpus by returning value from
virHostCPUGetCount().

Signed-off-by: Nitesh Konkar <nitkon12@linux.vnet.ibm.com>
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
2017-05-30 10:42:28 +02:00
Roman Bogorodskiy
981e2c7097 util: fix virfcp build on non-Linux
- Include virerror.h for virReportSystemError
 - Rename stub functions to match original function names
2017-05-26 20:00:51 +04:00
Bjoern Walk
c47a3b130d util: helper functions for fibre channel devices
We will need some convenient helper functions for managing sysfs-entries
for fibre channel-backed devices. Let's implement them and make them
available in the private API.

Signed-off-by: Bjoern Walk <bwalk@linux.vnet.ibm.com>
2017-05-26 10:44:05 -04:00
Yi Wang
9f4c39f309 util: fix memory leak in virSocketAddrFormatFull
The @ipv6_host allocated in virAsprintf may be lost when virAsprintf
addrstr failed.

Signed-off-by: Yi Wang <wang.yi59@zte.com.cn>
Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-05-26 12:15:13 +02:00
Chen Hanxiao
a495e3f9ea util: display leading zeros of USB vendor/product id's in log messages
Many vendor id's and product id's have leading zeros.  We should show
them in the logs.

Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Reviewed-by: Laine Stump <laine@laine.org>
2017-05-24 12:23:59 -04:00
Jim Fehlig
975ea20f85 maint: define a macro for IPv4 loopback address
Use a macro instead of hardcoding "127.0.0.1" throughout the
sources.
2017-05-22 10:20:27 -06:00
Wang King
c4a4c01e6e util: Don't leak @reply in virSystemdGetMachineNameByPID
@reply is a DBusMessage object returned by virDBusCallMethod in
get machine object call path, dereference it before calling
virDBusCallMethod again to get machine name.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-22 06:48:01 +02:00
Michal Privoznik
ab26790f07 virfile: Provide stub for virFileInData
Some older systems (such as RHEL6) lack SEEK_HOLE and SEEK_DATA
which virFileInData relies on. Provide a stub for these systems.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-19 14:02:37 +02:00
Michal Privoznik
0da4a635bc virStream: Forbid negative seeks
Currently, we don't assign any meaning to that. Our current view
on virStream is that it's merely a pipe. And pipes don't support
seeking.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-18 15:05:18 +02:00
Wang King
506acac87f util: Do not leak @handles in stop netlink event service
Commit e3ba4025 introduced srv->handles and VIR_RESIZE_N to allocate
@handles as necessary, but did not free the handles during when calling
virNetlinkEventServiceStop.
2017-05-18 07:26:05 -04:00
Wang King
c886b5d153 util: Deduplicate code in virNetlinkEventServiceStopAll
Commit 15a71e60 introduced the virNetlinkEventServiceStopAll function, and
the code in virNetlinkEventServiceStop is copied to this function, so just
call virNetlinkEventServiceStop instead.
2017-05-18 07:26:05 -04:00
Erik Skultety
3a2a2a7401 mdev: Pass a uuidstr rather than an mdev object to some util functions
Namely, this patch is about virMediatedDeviceGetIOMMUGroup{Dev,Num}
functions. There's no compelling reason why these functions should take
an object, on the contrary, having to create an object every time one
needs to query the IOMMU group number, discarding the object afterwards,
seems odd.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-05-18 12:20:15 +02:00
Marek Marczykowski-Górecki
1128769f9e pci: fix link maximum speed detection
Commit 8e09663 "pci: recognize/report GEN4 (PCIe 4.0) card 16GT/s Link
speed" introduced another speed into enum, but mistakenly also altered
field width, so one bit of link width was included there.

Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
2017-05-18 10:45:58 +02:00
Michal Privoznik
895479647b fdstream: Implement sparse stream
Basically, what is needed here is to introduce new message type
for the messages passed between the event loop callbacks and the
worker thread that does all the I/O. The idea is that instead of
a queue of read buffers we will have a queue where "hole of size
X" messages appear. That way the event loop callbacks can just
check the head of the queue and see if the worker thread is in
data or a hole section and how long the section is.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-18 07:42:13 +02:00
Michal Privoznik
b928d140f4 util: Introduce virFileInData
This function takes a FD and determines whether the current
position is in data section or in a hole. In addition to that,
it also determines how much bytes are there remaining till the
current section ends.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-18 07:42:13 +02:00
Michal Privoznik
07c2399c01 virfdstream: Use messages instead of pipe
One big downside of using the pipe to transfer the data is that
we can really transfer just bare data. No metadata can be carried
through unless some formatted messages are introduced. That would
be quite painful to achieve so let's use a message queue. It's
fairly easy to exchange info between threads now that iohelper is
no longer used.

The reason why we cannot use the FD for plain files directly is
that despite us setting noblock flag on the FD, any
read()/write() blocks regardless (which is a show stopper since
those parts of the code are run from the event loop) and poll()
reports such FD as always readable/writable - even though the
subsequent operation might block.

The pipe is still not gone though. It is used to signal the event
loop that an event occurred (e.g. data is available for reading
in the queue, or vice versa).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2017-05-18 07:42:13 +02:00
Andrea Bolognani
5645badd1f gic: Remove VIR_GIC_VERSION_DEFAULT
The QEMU default is GICv2, and some of the code in libvirt
relies on the exact value. Stop pretending that's not the
case and use GICv2 explicitly where needed.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
2017-05-16 16:48:30 +02:00
Peter Krempa
14789b7ea8 util: conf: Don't log when adding commented out lines
virConfAddEntry spams debug logs even for fully commented out lines.
Skip such messages to avoid:

2017-05-12 12:35:38.867+0000: 10820: debug : virConfAddEntry:241 : Add entry (null) (nil)
2017-05-12 12:35:38.867+0000: 10820: debug : virConfAddEntry:241 : Add entry (null) (nil)
2017-05-12 12:35:38.867+0000: 10820: debug : virConfAddEntry:241 : Add entry (null) (nil)
2017-05-12 12:35:38.867+0000: 10820: debug : virConfAddEntry:241 : Add entry (null) (nil)
2017-05-12 12:35:38.867+0000: 10820: debug : virConfAddEntry:241 : Add entry (null) (nil)
...

This also fixes NULL passed to printf.
2017-05-15 13:51:25 +02:00
Daniel P. Berrange
3213e369c3 Detect VMDK version 3 files
The metadata libvirt cares about is identical for version 3
as for previous versions, so we merely need list the new
version number.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-05-15 12:09:04 +01:00
Pavel Hrdina
0918b84968 util: introduce virBufferEscapeRegex
Add a helper to escape all possible meta-characters used for
POSIX extended regular expressions.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-05-12 16:54:33 +02:00
Pavel Hrdina
f15f155403 util: introduce virStringMatch
Simply tries to match the provided regex on a string and returns
the result.  Useful if caller don't care about the matched substring
and want to just test if some pattern patches a string.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
2017-05-12 16:51:18 +02:00
Daniel P. Berrange
1a77b97c7f Don't inline virStringTrimOptionalNewline
GCC complains that inlining virStringTrimOptionalNewline is not
likely on some platforms:

  cc1: warnings being treated as errors
  ../../src/util/virfile.c: In function 'virFileReadValueBitmap':
  ../../src/util/virstring.h:292: error: inlining failed in call to 'virStringTrimOptionalNewline': call is unlikely and code size would grow [-Winline]
  ../../src/util/virfile.c:3987: error: called from here [-Winline]

Inlining this function is not going to be a measurable performance
benefit either, since the time required to execute it is going to
be dominated by running of strlen() over the string, not by the
function call overhead.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-05-10 09:25:45 +01:00
Martin Kletzander
4082417425 util: Define SYSFS_SYSTEM_PATH unconditionally in virhostcpu
The code is already prepared to handle the non-existence of it.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-05-09 14:17:38 +02:00
Martin Kletzander
7008e10869 util: Remove virsysfs and instead enhance virFileReadValue* functions
It is no longer needed thanks to the great virfilewrapper.c.  And this
way we don't have to add a new set of functions for each prefixed
path.

While on that, add two functions that weren't there before, string and
scaled integer reading ones.  Also increase the length of the string
being read by one to accompany for the optional newline at the
end (i.e. change INT_STRLEN_BOUND to INT_BUFSIZE_BOUND).

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
2017-05-09 13:12:40 +02:00
Erik Skultety
8fc72e1c72 mdev: Cleanup code after commits @daf5081b and @2739a983
So, because mingw is somehow OK with dereferencing a pointer within a
VIR_DEBUG macro, compared to outside of it to which it complained with a
"potential NULL pointer dereference" error (still a false positive), we
can make the code a tiny bit cleaner.

Sighed-by: Erik Skultety <eskultet@redhat.com>
Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-05-09 13:01:37 +02:00
Michal Privoznik
033369c7d9 virPerfEventIsEnabled: Accept NULL @perf
After bdcf6e481 there is a crasher in libvirt. The commit assumes
that priv->perf is always set. That is not true. For inactive
domains, the priv->perf is not allocated as it is set in
qemuProcessLaunch(). Now, usually we differentiate between
accesses to inactive and active definition and it works just
fine. Except for 'domstats'. There priv->perf is accessed without
prior check for domain inactivity. While we could check for that,
more robust solution is to make virPerfEventIsEnabled() accept
NULL.

How to reproduce:
1) ensure you have at least one inactive domain
2) virsh domstats

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
2017-05-04 16:42:25 +02:00
Erik Skultety
574718d366 mdev: Fix mingw build by adding a check for non-NULL pointer
This patch fixes the following MinGW error (although actually being a
false positive):

../../src/util/virmdev.c: In function 'virMediatedDeviceListMarkDevices':
../../src/util/virmdev.c:453:21: error: potential null pointer
dereference [-Werror=null-dereference]
          const char *mdev_path = mdev->path;
                      ^~~~~~~~~

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2017-05-04 13:23:09 +02:00
Erik Skultety
92e30a4dac mdev: Fix daemon crash on domain shutdown after reconnect
The problem resides in virHostdevUpdateActiveMediatedDevices which gets
called during qemuProcessReconnect. The issue here is that
virMediatedDeviceListAdd takes a pointer to the item to be added to the
list to which VIR_APPEND_ELEMENT is used, which also clears the pointer.
However, in this case only the local copy of the pointer got cleared,
leaving the original pointing to valid memory. To sum it up, during
cleanup phase, the original pointer is freed and the daemon crashes
basically any time it would access it.

Backtrace:
0x00007ffff3ccdeba in __strcmp_sse2_unaligned
0x00007ffff72a444a in virMediatedDeviceListFindIndex
0x00007ffff7241446 in virHostdevReAttachMediatedDevices
0x00007fffc60215d9 in qemuHostdevReAttachMediatedDevices
0x00007fffc60216dc in qemuHostdevReAttachDomainDevices
0x00007fffc6046e6f in qemuProcessStop
0x00007fffc6091596 in processMonitorEOFEvent
0x00007fffc6091793 in qemuProcessEventHandler
0x00007ffff7294bf5 in virThreadPoolWorker
0x00007ffff7294184 in virThreadHelper
0x00007ffff3fdc3c4 in start_thread () from /lib64/libpthread.so.0
0x00007ffff3d269cf in clone () from /lib64/libc.so.6

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1446455

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Laine Stump <laine@laine.org>
2017-05-04 08:05:03 +02:00
Erik Skultety
2739a983f2 util: mdev: Use a local variable instead of a direct pointer access
Use a local variable to hold data, rather than accessing the pointer
after calling virMediatedDeviceListAdd (therefore VIR_APPEND_ELEMENT).
Although not causing an issue at the moment, this change is a necessary
prerequisite for tweaking virMediatedDeviceListAdd in a separate patch,
which will take a reference for the source pointer (instead of pointer
value) and will clear it along the way.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Laine Stump <laine@laine.org>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
2017-05-04 07:54:42 +02:00
Daniel P. Berrange
71890992da Fix padding of encrypted data
If we are encoding a block of data that is 16 bytes in length,
we cannot leave it as 16 bytes, we must pad it out to the next
block boundary, 32 bytes. Without this padding, the decoder will
incorrectly treat the last byte of plain text as the padding
length, as it can't distinguish padded from non-padded data.

The problem exhibited itself when using a 16 byte passphrase
for a LUKS volume

  $ virsh secret-set-value 55806c7d-8e93-456f-829b-607d8c198367 \
       $(echo -n 1234567812345678 | base64)
  Secret value set

  $ virsh start demo
  error: Failed to start domain demo
  error: internal error: process exited while connecting to monitor: >>>>>>>>>>Len 16
  2017-05-02T10:35:40.016390Z qemu-system-x86_64: -object \
    secret,id=virtio-disk1-luks-secret0,data=SEtNi5vDUeyseMKHwc1c1Q==,\
    keyid=masterKey0,iv=zm7apUB1A6dPcH53VW960Q==,format=base64: \
    Incorrect number of padding bytes (56) found on decrypted data

Notice how the padding '56' corresponds to the ordinal value of
the character '8'.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2017-05-02 17:27:13 +01:00
Laine Stump
30e672301d util: rename/move VIR_NET_GENERATED_PREFIX to be consistent
... with VIR_NET_GENERATED_MACV???_PREFIX, which is defined in
util/virnetdevmacvlan.h.

Since VIR_NET_GENERATED_PREFIX is used for plain tap devices, it is
renamed to VIR_NET_GENERATED_TAP_PREFIX and moved to virnetdev.h
2017-04-28 09:43:52 -04:00
Laine Stump
a05400ef55 util: make macvtap/macvlan generated name #defines available to other files
MACVTAP_NAME_PREFIX and MACVLAN_NAME_PREFIX could be useful to other
files if they were defined in virnetdevmacvlan.h instead of
virnetdevmacvlan.c, so do that (while slightly renaming them and also
adding yet another #define that chooses between macvlan/macvtap based
on flags).

This is a prerequisite to fix: https://bugzilla.redhat.com/1335798
2017-04-28 09:43:52 -04:00
Michal Privoznik
d111f52c35 iohelper: Remove unused mode
After 1eb6647979 nobody calls the iohelper with 6 arguments.
Everybody uses the other mode. Well, the only user of iohelper
after the previous commit is virFileWrapperFd really.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2017-04-28 14:17:10 +02:00