2012-08-16 15:41:06 +00:00
|
|
|
/*
|
|
|
|
* device_conf.c: device XML handling
|
|
|
|
*
|
conf: allow type='pci' addresses with no address attributes specified
Prior to this, <address type='pci'/> wasn't allowed when parsing
(domain+bus+slot+function needed to be a "valid" PCI address, meaning
that at least one of domain/bus/slot had to be non-0), the RNG
required bus to be specified, and if type was set to PCI when
formatting, domain+bus+slot+function would always be output.
This makes all the address attributes optional during parse and RNG
validation, and suppresses domain+bus+slot+function if domain+bus+slot
are all 0 (NB: if d+b+s are all 0, any value for function is
nonsensical as that will never happen in the real world, and after
the next patch we will always assign a real working address to any
empty PCI address before it is ever output to anywhere).
Note that explicitly setting all attributes to 0 is equivalent to
setting none of them, which is okay, since 0000:00:00 is reserved in
any PCI bus setup, and can't be used anyway.
2016-05-17 18:03:00 +00:00
|
|
|
* Copyright (C) 2006-2016 Red Hat, Inc.
|
2012-08-16 15:41:06 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-08-16 15:41:06 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-08-16 15:41:06 +00:00
|
|
|
#include "datatypes.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 18:13:21 +00:00
|
|
|
#include "virxml.h"
|
2012-12-13 18:01:25 +00:00
|
|
|
#include "viruuid.h"
|
2012-12-04 12:04:07 +00:00
|
|
|
#include "virbuffer.h"
|
2012-08-16 15:41:06 +00:00
|
|
|
#include "device_conf.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2012-08-16 15:41:06 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_DEVICE
|
|
|
|
|
2016-04-03 18:16:51 +00:00
|
|
|
int virPCIDeviceAddressIsValid(virPCIDeviceAddressPtr addr,
|
conf: more useful error message when pci function is out of range
If a pci address had a function number out of range, the error message
would be:
Insufficient specification for PCI address
which is logged by virDevicePCIAddressParseXML() after
virDevicePCIAddressIsValid returns a failure.
This patch enhances virDevicePCIAddressIsValid() to optionally report
the error itself (since it is the place that decides which part of the
address is "invalid"), and uses that feature when calling from
virDevicePCIAddressParseXML(), so that the error will be more useful,
e.g.:
Invalid PCI address function=0x8, must be <= 7
Previously, virDevicePCIAddressIsValid didn't check for the
theoretical limits of domain or bus, only for slot or function. While
adding log messages, we also correct that ommission. (The RNG for PCI
addresses already enforces this limit, which by the way means that we
can't add any negative tests for this - as far as I know our
domainschematest has no provisions for passing XML that is supposed to
fail).
Note that virDevicePCIAddressIsValid() can only check against the
absolute maximum attribute values for *any* possible PCI controller,
not for the actual maximums of the specific controller that this
device is attaching to; fortunately there is later more specific
validation for guest-side PCI addresses when building the set of
assigned PCI addresses. For host-side PCI addresses (e.g. for
<hostdev> and for network device pools), we rely on the error that
will be logged when it is found that the device doesn't actually
exist.
This resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1004596
2015-07-22 15:59:00 +00:00
|
|
|
bool report)
|
2012-08-16 15:41:06 +00:00
|
|
|
{
|
conf: more useful error message when pci function is out of range
If a pci address had a function number out of range, the error message
would be:
Insufficient specification for PCI address
which is logged by virDevicePCIAddressParseXML() after
virDevicePCIAddressIsValid returns a failure.
This patch enhances virDevicePCIAddressIsValid() to optionally report
the error itself (since it is the place that decides which part of the
address is "invalid"), and uses that feature when calling from
virDevicePCIAddressParseXML(), so that the error will be more useful,
e.g.:
Invalid PCI address function=0x8, must be <= 7
Previously, virDevicePCIAddressIsValid didn't check for the
theoretical limits of domain or bus, only for slot or function. While
adding log messages, we also correct that ommission. (The RNG for PCI
addresses already enforces this limit, which by the way means that we
can't add any negative tests for this - as far as I know our
domainschematest has no provisions for passing XML that is supposed to
fail).
Note that virDevicePCIAddressIsValid() can only check against the
absolute maximum attribute values for *any* possible PCI controller,
not for the actual maximums of the specific controller that this
device is attaching to; fortunately there is later more specific
validation for guest-side PCI addresses when building the set of
assigned PCI addresses. For host-side PCI addresses (e.g. for
<hostdev> and for network device pools), we rely on the error that
will be logged when it is found that the device doesn't actually
exist.
This resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1004596
2015-07-22 15:59:00 +00:00
|
|
|
if (addr->domain > 0xFFFF) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address domain='0x%x', "
|
|
|
|
"must be <= 0xFFFF"),
|
|
|
|
addr->domain);
|
2012-08-16 15:41:06 +00:00
|
|
|
return 0;
|
conf: more useful error message when pci function is out of range
If a pci address had a function number out of range, the error message
would be:
Insufficient specification for PCI address
which is logged by virDevicePCIAddressParseXML() after
virDevicePCIAddressIsValid returns a failure.
This patch enhances virDevicePCIAddressIsValid() to optionally report
the error itself (since it is the place that decides which part of the
address is "invalid"), and uses that feature when calling from
virDevicePCIAddressParseXML(), so that the error will be more useful,
e.g.:
Invalid PCI address function=0x8, must be <= 7
Previously, virDevicePCIAddressIsValid didn't check for the
theoretical limits of domain or bus, only for slot or function. While
adding log messages, we also correct that ommission. (The RNG for PCI
addresses already enforces this limit, which by the way means that we
can't add any negative tests for this - as far as I know our
domainschematest has no provisions for passing XML that is supposed to
fail).
Note that virDevicePCIAddressIsValid() can only check against the
absolute maximum attribute values for *any* possible PCI controller,
not for the actual maximums of the specific controller that this
device is attaching to; fortunately there is later more specific
validation for guest-side PCI addresses when building the set of
assigned PCI addresses. For host-side PCI addresses (e.g. for
<hostdev> and for network device pools), we rely on the error that
will be logged when it is found that the device doesn't actually
exist.
This resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1004596
2015-07-22 15:59:00 +00:00
|
|
|
}
|
|
|
|
if (addr->bus > 0xFF) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address bus='0x%x', "
|
|
|
|
"must be <= 0xFF"),
|
|
|
|
addr->bus);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (addr->slot > 0x1F) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address slot='0x%x', "
|
|
|
|
"must be <= 0x1F"),
|
|
|
|
addr->slot);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (addr->function > 7) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address function=0x%x, "
|
|
|
|
"must be <= 7"),
|
|
|
|
addr->function);
|
|
|
|
return 0;
|
|
|
|
}
|
conf: allow type='pci' addresses with no address attributes specified
Prior to this, <address type='pci'/> wasn't allowed when parsing
(domain+bus+slot+function needed to be a "valid" PCI address, meaning
that at least one of domain/bus/slot had to be non-0), the RNG
required bus to be specified, and if type was set to PCI when
formatting, domain+bus+slot+function would always be output.
This makes all the address attributes optional during parse and RNG
validation, and suppresses domain+bus+slot+function if domain+bus+slot
are all 0 (NB: if d+b+s are all 0, any value for function is
nonsensical as that will never happen in the real world, and after
the next patch we will always assign a real working address to any
empty PCI address before it is ever output to anywhere).
Note that explicitly setting all attributes to 0 is equivalent to
setting none of them, which is okay, since 0000:00:00 is reserved in
any PCI bus setup, and can't be used anyway.
2016-05-17 18:03:00 +00:00
|
|
|
if (virPCIDeviceAddressIsEmpty(addr)) {
|
conf: more useful error message when pci function is out of range
If a pci address had a function number out of range, the error message
would be:
Insufficient specification for PCI address
which is logged by virDevicePCIAddressParseXML() after
virDevicePCIAddressIsValid returns a failure.
This patch enhances virDevicePCIAddressIsValid() to optionally report
the error itself (since it is the place that decides which part of the
address is "invalid"), and uses that feature when calling from
virDevicePCIAddressParseXML(), so that the error will be more useful,
e.g.:
Invalid PCI address function=0x8, must be <= 7
Previously, virDevicePCIAddressIsValid didn't check for the
theoretical limits of domain or bus, only for slot or function. While
adding log messages, we also correct that ommission. (The RNG for PCI
addresses already enforces this limit, which by the way means that we
can't add any negative tests for this - as far as I know our
domainschematest has no provisions for passing XML that is supposed to
fail).
Note that virDevicePCIAddressIsValid() can only check against the
absolute maximum attribute values for *any* possible PCI controller,
not for the actual maximums of the specific controller that this
device is attaching to; fortunately there is later more specific
validation for guest-side PCI addresses when building the set of
assigned PCI addresses. For host-side PCI addresses (e.g. for
<hostdev> and for network device pools), we rely on the error that
will be logged when it is found that the device doesn't actually
exist.
This resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1004596
2015-07-22 15:59:00 +00:00
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("Invalid PCI address 0000:00:00, at least "
|
|
|
|
"one of domain, bus, or slot must be > 0"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2012-08-16 15:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2016-04-03 18:16:51 +00:00
|
|
|
virPCIDeviceAddressParseXML(xmlNodePtr node,
|
|
|
|
virPCIDeviceAddressPtr addr)
|
2012-08-16 15:41:06 +00:00
|
|
|
{
|
|
|
|
char *domain, *slot, *bus, *function, *multi;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
|
|
|
|
|
|
domain = virXMLPropString(node, "domain");
|
|
|
|
bus = virXMLPropString(node, "bus");
|
|
|
|
slot = virXMLPropString(node, "slot");
|
|
|
|
function = virXMLPropString(node, "function");
|
|
|
|
multi = virXMLPropString(node, "multifunction");
|
|
|
|
|
|
|
|
if (domain &&
|
2014-12-08 08:27:26 +00:00
|
|
|
virStrToLong_uip(domain, NULL, 0, &addr->domain) < 0) {
|
2012-08-16 15:41:06 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse <address> 'domain' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bus &&
|
2014-12-08 08:27:26 +00:00
|
|
|
virStrToLong_uip(bus, NULL, 0, &addr->bus) < 0) {
|
2012-08-16 15:41:06 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse <address> 'bus' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slot &&
|
2014-12-08 08:27:26 +00:00
|
|
|
virStrToLong_uip(slot, NULL, 0, &addr->slot) < 0) {
|
2012-08-16 15:41:06 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse <address> 'slot' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function &&
|
2014-12-08 08:27:26 +00:00
|
|
|
virStrToLong_uip(function, NULL, 0, &addr->function) < 0) {
|
2012-08-16 15:41:06 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse <address> 'function' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multi &&
|
2014-06-27 15:18:53 +00:00
|
|
|
((addr->multi = virTristateSwitchTypeFromString(multi)) <= 0)) {
|
2012-08-16 15:41:06 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Unknown value '%s' for <address> 'multifunction' attribute"),
|
|
|
|
multi);
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
}
|
conf: allow type='pci' addresses with no address attributes specified
Prior to this, <address type='pci'/> wasn't allowed when parsing
(domain+bus+slot+function needed to be a "valid" PCI address, meaning
that at least one of domain/bus/slot had to be non-0), the RNG
required bus to be specified, and if type was set to PCI when
formatting, domain+bus+slot+function would always be output.
This makes all the address attributes optional during parse and RNG
validation, and suppresses domain+bus+slot+function if domain+bus+slot
are all 0 (NB: if d+b+s are all 0, any value for function is
nonsensical as that will never happen in the real world, and after
the next patch we will always assign a real working address to any
empty PCI address before it is ever output to anywhere).
Note that explicitly setting all attributes to 0 is equivalent to
setting none of them, which is okay, since 0000:00:00 is reserved in
any PCI bus setup, and can't be used anyway.
2016-05-17 18:03:00 +00:00
|
|
|
if (!virPCIDeviceAddressIsEmpty(addr) && !virPCIDeviceAddressIsValid(addr, true))
|
2012-08-16 15:41:06 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:48:31 +00:00
|
|
|
cleanup:
|
2012-08-16 15:41:06 +00:00
|
|
|
VIR_FREE(domain);
|
|
|
|
VIR_FREE(bus);
|
|
|
|
VIR_FREE(slot);
|
|
|
|
VIR_FREE(function);
|
|
|
|
VIR_FREE(multi);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-04-03 18:16:51 +00:00
|
|
|
virPCIDeviceAddressFormat(virBufferPtr buf,
|
|
|
|
virPCIDeviceAddress addr,
|
2012-08-16 15:41:06 +00:00
|
|
|
bool includeTypeInAddr)
|
|
|
|
{
|
|
|
|
virBufferAsprintf(buf, "<address %sdomain='0x%.4x' bus='0x%.2x' "
|
|
|
|
"slot='0x%.2x' function='0x%.1x'/>\n",
|
|
|
|
includeTypeInAddr ? "type='pci' " : "",
|
|
|
|
addr.domain,
|
|
|
|
addr.bus,
|
|
|
|
addr.slot,
|
|
|
|
addr.function);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-16 15:42:14 +00:00
|
|
|
|
2012-10-11 16:34:14 +00:00
|
|
|
bool
|
2016-04-03 18:16:51 +00:00
|
|
|
virPCIDeviceAddressEqual(virPCIDeviceAddress *addr1,
|
|
|
|
virPCIDeviceAddress *addr2)
|
2012-08-16 15:42:14 +00:00
|
|
|
{
|
2012-10-11 16:34:14 +00:00
|
|
|
if (addr1->domain == addr2->domain &&
|
|
|
|
addr1->bus == addr2->bus &&
|
|
|
|
addr1->slot == addr2->slot &&
|
|
|
|
addr1->function == addr2->function) {
|
|
|
|
return true;
|
2012-08-16 15:42:14 +00:00
|
|
|
}
|
2012-10-11 16:34:14 +00:00
|
|
|
return false;
|
2012-08-16 15:42:14 +00:00
|
|
|
}
|
2014-05-07 12:21:35 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virInterfaceLinkParseXML(xmlNodePtr node,
|
2016-06-13 17:06:15 +00:00
|
|
|
virNetDevIfLinkPtr lnk)
|
2014-05-07 12:21:35 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *stateStr, *speedStr;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
stateStr = virXMLPropString(node, "state");
|
|
|
|
speedStr = virXMLPropString(node, "speed");
|
|
|
|
|
|
|
|
if (stateStr) {
|
2016-06-13 17:06:15 +00:00
|
|
|
if ((state = virNetDevIfStateTypeFromString(stateStr)) < 0) {
|
2014-05-07 12:21:35 +00:00
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("unknown link state: %s"),
|
|
|
|
stateStr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
lnk->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (speedStr &&
|
|
|
|
virStrToLong_ui(speedStr, NULL, 10, &lnk->speed) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Unable to parse link speed: %s"),
|
|
|
|
speedStr);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(stateStr);
|
|
|
|
VIR_FREE(speedStr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virInterfaceLinkFormat(virBufferPtr buf,
|
2016-06-13 17:06:15 +00:00
|
|
|
const virNetDevIfLink *lnk)
|
2014-05-07 12:21:35 +00:00
|
|
|
{
|
|
|
|
if (!lnk->speed && !lnk->state) {
|
|
|
|
/* If there's nothing to format, return early. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "<link");
|
|
|
|
if (lnk->speed)
|
|
|
|
virBufferAsprintf(buf, " speed='%u'", lnk->speed);
|
|
|
|
if (lnk->state)
|
|
|
|
virBufferAsprintf(buf, " state='%s'",
|
2016-06-13 17:06:15 +00:00
|
|
|
virNetDevIfStateTypeToString(lnk->state));
|
2014-05-07 12:21:35 +00:00
|
|
|
virBufferAddLit(buf, "/>\n");
|
|
|
|
return 0;
|
|
|
|
}
|