lxc: use correct prefix when setting veth IP address

Commit c9a641 (first appearred in 1.2.12) added support for setting
the guest-side IP address of veth devices in lxc domains.
Unfortunately, it hardcoded the assumption that the proper prefix for
any IP address with no explicit prefix in the config should be "24";
that is only correct for class C IPv4 addresses, but not for any other
IPv4 address, nor for any IPv6 address.

The good news is that there is already a function in libvirt that will
determine the proper default prefix for any IP address. This patch
replaces the use of the ill-fated VIR_SOCKET_ADDR_DEFAULT_PREFIX with
calls to virSocketAddrGetIPPrefix().
This commit is contained in:
Laine Stump 2016-04-26 14:27:08 -04:00
parent f03a4a2a96
commit 70a2c7e062
2 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2015 Red Hat, Inc.
* Copyright (C) 2008-2016 Red Hat, Inc.
* Copyright (C) 2008 IBM Corp.
* Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
*
@ -514,12 +514,20 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
for (j = 0; j < netDef->nips; j++) {
virDomainNetIPDefPtr ip = netDef->ips[j];
unsigned int prefix = (ip->prefix > 0) ? ip->prefix :
VIR_SOCKET_ADDR_DEFAULT_PREFIX;
int prefix;
char *ipStr = virSocketAddrFormat(&ip->address);
VIR_DEBUG("Adding IP address '%s/%u' to '%s'",
ipStr, ip->prefix, newname);
if ((prefix = virSocketAddrGetIPPrefix(&ip->address,
NULL, ip->prefix)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to determine prefix for IP address '%s'"),
ipStr);
VIR_FREE(ipStr);
goto error_out;
}
VIR_DEBUG("Adding IP address '%s/%d' to '%s'",
ipStr, prefix, newname);
if (virNetDevSetIPAddress(newname, &ip->address, NULL, prefix) < 0) {
virReportError(VIR_ERR_SYSTEM_ERROR,
_("Failed to set IP address '%s' on %s"),

View File

@ -54,7 +54,6 @@ typedef struct {
# define VIR_SOCKET_ADDR_FAMILY(s) \
((s)->data.sa.sa_family)
# define VIR_SOCKET_ADDR_DEFAULT_PREFIX 24
# define VIR_SOCKET_ADDR_IPV4_ALL "0.0.0.0"
# define VIR_SOCKET_ADDR_IPV6_ALL "::"