util: replace macvtap name reservation bitmap with a simple counter

There have been some reports that, due to libvirt always trying to
assign the lowest numbered macvtap / tap device name possible, a new
guest would sometimes be started using the same tap device name as
previously used by another guest that is in the process of being
destroyed *as the new guest is starting.

In some cases this has led to, for example, the old guest's
qemuProcessStop() code deleting a port from an OVS switch that had
just been re-added by the new guest (because the port name is based on
only the device name using the port). Similar problems can happen (and
I believe have) with nwfilter rules and bandwidth rules (which are
both instantiated based on the name of the tap device).

A couple patches have been previously proposed to change the ordering
of startup and shutdown processing, or to put a mutex around
everything related to the tap/macvtap device name usage, but in the
end no matter what you do there will still be possible holes, because
the device could be deleted outside libvirt's control (for example,
regular tap devices are automatically deleted when the qemu process
terminates, and that isn't always initiated by libvirt but could
instead happen completely asynchronously - libvirt then has no control
over the ordering of shutdown operations, and no opportunity to
protect it with a mutex.)

But this only happens if a new device is created at the same time as
one is being deleted. We can effectively eliminate the chance of this
happening if we end the practice of always looking for the lowest
numbered available device name, and instead just keep an integer that
is incremented each time we need a new device name. At some point it
will need to wrap back around to 0 (in order to avoid the IFNAMSIZ 15
character limit if nothing else), and we can't guarantee that the new
name really will be the *least* recently used name, but "math"
suggests that it will be *much* less common that we'll try to re-use
the *most* recently used name.

This patch implements such a counter for macvtap/macvlan, replacing
the existing, and much more complicated, "ID reservation" system. The
counter is set according to whatever macvtap/macvlan devices are
already in use by guests when libvirtd is started, incremented each
time a new device name is needed, and wraps back to 0 when either
INT_MAX is reached, or when the resulting device name would be longer
than IFNAMSIZ-1 characters (which actually is what happens when the
template for the device name is "maccvtap%d"). The result is that no
macvtap name will be re-used until the host has created (and possibly
destroyed) 99,999,999 devices.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Laine Stump 2020-08-23 14:57:19 -04:00
parent b546b48344
commit d7f38beb2e
6 changed files with 155 additions and 281 deletions

View File

@ -2642,7 +2642,6 @@ virNetDevMacVLanDelete;
virNetDevMacVLanDeleteWithVPortProfile; virNetDevMacVLanDeleteWithVPortProfile;
virNetDevMacVLanIsMacvtap; virNetDevMacVLanIsMacvtap;
virNetDevMacVLanModeTypeFromString; virNetDevMacVLanModeTypeFromString;
virNetDevMacVLanReleaseName;
virNetDevMacVLanReserveName; virNetDevMacVLanReserveName;
virNetDevMacVLanRestartWithVPortProfile; virNetDevMacVLanRestartWithVPortProfile;
virNetDevMacVLanTapOpen; virNetDevMacVLanTapOpen;

View File

@ -367,7 +367,7 @@ libxlReconnectNotifyNets(virDomainDefPtr def)
* impolite. * impolite.
*/ */
if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT) if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); virNetDevMacVLanReserveName(net->ifname);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) { if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
if (!conn && !(conn = virGetConnectNetwork())) if (!conn && !(conn = virGetConnectNetwork()))

View File

@ -1613,7 +1613,7 @@ virLXCProcessReconnectNotifyNets(virDomainDefPtr def)
* impolite. * impolite.
*/ */
if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT) if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); virNetDevMacVLanReserveName(net->ifname);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) { if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
if (!conn && !(conn = virGetConnectNetwork())) if (!conn && !(conn = virGetConnectNetwork()))

View File

@ -3321,7 +3321,7 @@ qemuProcessNotifyNets(virDomainDefPtr def)
* impolite. * impolite.
*/ */
if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT) if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); virNetDevMacVLanReserveName(net->ifname);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) { if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
if (!conn && !(conn = virGetConnectNetwork())) if (!conn && !(conn = virGetConnectNetwork()))

View File

@ -45,6 +45,7 @@ VIR_ENUM_IMPL(virNetDevMacVLanMode,
# include <net/if.h> # include <net/if.h>
# include <linux/if_tun.h> # include <linux/if_tun.h>
# include <math.h>
/* Older kernels lacked this enum value. */ /* Older kernels lacked this enum value. */
# if !HAVE_DECL_MACVLAN_MODE_PASSTHRU # if !HAVE_DECL_MACVLAN_MODE_PASSTHRU
@ -69,211 +70,121 @@ VIR_LOG_INIT("util.netdevmacvlan");
((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \ ((flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? \
VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX) VIR_NET_GENERATED_MACVTAP_PREFIX : VIR_NET_GENERATED_MACVLAN_PREFIX)
# define MACVLAN_MAX_ID 8191
virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER; virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER;
virBitmapPtr macvtapIDs = NULL; static int virNetDevMacVTapLastID = -1;
virBitmapPtr macvlanIDs = NULL; static int virNetDevMacVLanLastID = -1;
static int
virNetDevMacVLanOnceInit(void) static void
virNetDevMacVLanReserveNameInternal(const char *name)
{ {
if (!macvtapIDs && unsigned int id;
!(macvtapIDs = virBitmapNew(MACVLAN_MAX_ID + 1))) const char *idstr = NULL;
return -1; int *lastID = NULL;
if (!macvlanIDs && int len;
!(macvlanIDs = virBitmapNew(MACVLAN_MAX_ID + 1)))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virNetDevMacVLan); if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
lastID = &virNetDevMacVTapLastID;
len = strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
/** } else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
* virNetDevMacVLanReserveID: lastID = &virNetDevMacVTapLastID;
* len = strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
* @id: id 0 - MACVLAN_MAX_ID+1 to reserve (or -1 for "first free") } else {
* @flags: set VIR_NETDEV_MACVLAN_CREATE_WITH_TAP for macvtapN else macvlanN return;
* @quietFail: don't log an error if this name is already in-use
* @nextFree: reserve the next free ID *after* @id rather than @id itself
*
* Reserve the indicated ID in the appropriate bitmap, or find the
* first free ID if @id is -1.
*
* Returns newly reserved ID# on success, or -1 to indicate failure.
*/
static int
virNetDevMacVLanReserveID(int id, unsigned int flags,
bool quietFail, bool nextFree)
{
virBitmapPtr bitmap;
if (virNetDevMacVLanInitialize() < 0)
return -1;
bitmap = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
macvtapIDs : macvlanIDs;
if (id > MACVLAN_MAX_ID) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("can't use name %s%d - out of range 0-%d"),
VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
return -1;
} }
if ((id < 0 || nextFree) && VIR_INFO("marking device in use: '%s'", name);
(id = virBitmapNextClearBit(bitmap, id)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, idstr = name + len;
_("no unused %s names available"),
VIR_NET_GENERATED_PREFIX); if (virStrToLong_ui(idstr, NULL, 10, &id) >= 0) {
return -1; if (*lastID < (int)id)
*lastID = id;
} }
if (virBitmapIsBitSet(bitmap, id)) {
if (quietFail) {
VIR_INFO("couldn't reserve name %s%d - already in use",
VIR_NET_GENERATED_PREFIX, id);
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("couldn't reserve name %s%d - already in use"),
VIR_NET_GENERATED_PREFIX, id);
}
return -1;
}
if (virBitmapSetBit(bitmap, id) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("couldn't mark %s%d as used"),
VIR_NET_GENERATED_PREFIX, id);
return -1;
}
VIR_INFO("reserving device %s%d", VIR_NET_GENERATED_PREFIX, id);
return id;
}
/**
* virNetDevMacVLanReleaseID:
* @id: id 0 - MACVLAN_MAX_ID+1 to release
*
* Returns 0 for success or -1 for failure.
*/
static int
virNetDevMacVLanReleaseID(int id, unsigned int flags)
{
virBitmapPtr bitmap;
if (virNetDevMacVLanInitialize() < 0)
return 0;
bitmap = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ?
macvtapIDs : macvlanIDs;
if (id > MACVLAN_MAX_ID) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("can't free name %s%d - out of range 0-%d"),
VIR_NET_GENERATED_PREFIX, id, MACVLAN_MAX_ID);
return -1;
}
if (id < 0)
return 0;
VIR_INFO("releasing %sdevice %s%d",
virBitmapIsBitSet(bitmap, id) ? "" : "unreserved",
VIR_NET_GENERATED_PREFIX, id);
if (virBitmapClearBit(bitmap, id) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("couldn't mark %s%d as unused"),
VIR_NET_GENERATED_PREFIX, id);
return -1;
}
return 0;
} }
/** /**
* virNetDevMacVLanReserveName: * virNetDevMacVLanReserveName:
* @name: name of an existing macvtap/macvlan device
* *
* @name: already-known name of device * Set the value of virNetDevMacV(Lan|Tap)LastID to assure that any
* @quietFail: don't log an error if this name is already in-use * new device created with an autogenerated name will use a number
* higher than the number in the given device name.
* *
* Extract the device type and id from a macvtap/macvlan device name * Returns nothing.
* and mark the appropriate position as in-use in the appropriate
* bitmap.
*
* Returns reserved ID# on success, -1 on failure, -2 if the name
* doesn't fit the auto-pattern (so not reserveable).
*/ */
int void
virNetDevMacVLanReserveName(const char *name, bool quietFail) virNetDevMacVLanReserveName(const char *name)
{ {
unsigned int id; virMutexLock(&virNetDevMacVLanCreateMutex);
unsigned int flags = 0; virNetDevMacVLanReserveNameInternal(name);
const char *idstr = NULL; virMutexUnlock(&virNetDevMacVLanCreateMutex);
if (virNetDevMacVLanInitialize() < 0)
return -1;
if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) {
idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
} else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
} else {
return -2;
}
if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("couldn't get id value from macvtap device name %s"),
name);
return -1;
}
return virNetDevMacVLanReserveID(id, flags, quietFail, false);
} }
/** /**
* virNetDevMacVLanReleaseName: * virNetDevMacVLanGenerateName:
* @ifname: pointer to pointer to string containing template
* @lastID: counter to add to the template to form the name
* *
* @name: already-known name of device * generate a new (currently unused) name for a new macvtap/macvlan
* device based on the template string in @ifname - replace %d with
* ++(*counter), and keep trying new values until one is found
* that doesn't already exist, or we've tried 10000 different
* names. Once a usable name is found, replace the template with the
* actual name.
* *
* Extract the device type and id from a macvtap/macvlan device name * Returns 0 on success, -1 on failure.
* and mark the appropriate position as in-use in the appropriate
* bitmap.
*
* returns 0 on success, -1 on failure
*/ */
int static int
virNetDevMacVLanReleaseName(const char *name) virNetDevMacVLanGenerateName(char **ifname, unsigned int flags)
{ {
unsigned int id; const char *prefix;
unsigned int flags = 0; const char *iftemplate;
const char *idstr = NULL; int *lastID;
int id;
double maxIDd;
int maxID = INT_MAX;
int attempts = 0;
if (virNetDevMacVLanInitialize() < 0) if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) {
return -1; prefix = VIR_NET_GENERATED_MACVTAP_PREFIX;
iftemplate = VIR_NET_GENERATED_MACVTAP_PREFIX "%d";
if (STRPREFIX(name, VIR_NET_GENERATED_MACVTAP_PREFIX)) { lastID = &virNetDevMacVTapLastID;
idstr = name + strlen(VIR_NET_GENERATED_MACVTAP_PREFIX);
flags |= VIR_NETDEV_MACVLAN_CREATE_WITH_TAP;
} else if (STRPREFIX(name, VIR_NET_GENERATED_MACVLAN_PREFIX)) {
idstr = name + strlen(VIR_NET_GENERATED_MACVLAN_PREFIX);
} else { } else {
return 0; prefix = VIR_NET_GENERATED_MACVLAN_PREFIX;
iftemplate = VIR_NET_GENERATED_MACVLAN_PREFIX "%d";
lastID = &virNetDevMacVLanLastID;
} }
if (virStrToLong_ui(idstr, NULL, 10, &id) < 0) { maxIDd = pow(10, IFNAMSIZ - 1 - strlen(prefix));
virReportError(VIR_ERR_INTERNAL_ERROR, if (maxIDd <= (double)INT_MAX)
_("couldn't get id value from macvtap device name %s"), maxID = (int)maxIDd;
name);
return -1; do {
} g_autofree char *try = NULL;
return virNetDevMacVLanReleaseID(id, flags);
id = ++(*lastID);
/* reset before overflow */
if (*lastID == maxID)
*lastID = -1;
try = g_strdup_printf(iftemplate, id);
if (!virNetDevExists(try)) {
g_free(*ifname);
*ifname = g_steal_pointer(&try);
return 0;
}
} while (++attempts < 10000);
virReportError(VIR_ERR_INTERNAL_ERROR,
_("no unused %s names available"),
*ifname);
return -1;
} }
@ -320,8 +231,7 @@ virNetDevMacVLanCreate(const char *ifname,
const char *type, const char *type,
const virMacAddr *macaddress, const virMacAddr *macaddress,
const char *srcdev, const char *srcdev,
uint32_t macvlan_mode, uint32_t macvlan_mode)
int *retry)
{ {
int error = 0; int error = 0;
int ifindex = 0; int ifindex = 0;
@ -330,7 +240,6 @@ virNetDevMacVLanCreate(const char *ifname,
.mac = macaddress, .mac = macaddress,
}; };
*retry = 0;
if (virNetDevGetIndex(srcdev, &ifindex) < 0) if (virNetDevGetIndex(srcdev, &ifindex) < 0)
return -1; return -1;
@ -338,17 +247,15 @@ virNetDevMacVLanCreate(const char *ifname,
data.ifindex = &ifindex; data.ifindex = &ifindex;
if (virNetlinkNewLink(ifname, type, &data, &error) < 0) { if (virNetlinkNewLink(ifname, type, &data, &error) < 0) {
char macstr[VIR_MAC_STRING_BUFLEN]; char macstr[VIR_MAC_STRING_BUFLEN];
if (error == -EEXIST)
*retry = 1;
else if (error < 0)
virReportSystemError(-error,
_("error creating %s interface %s@%s (%s)"),
type, ifname, srcdev,
virMacAddrFormat(macaddress, macstr));
virReportSystemError(-error,
_("error creating %s interface %s@%s (%s)"),
type, ifname, srcdev,
virMacAddrFormat(macaddress, macstr));
return -1; return -1;
} }
VIR_INFO("created device: '%s'", ifname);
return 0; return 0;
} }
@ -363,6 +270,7 @@ virNetDevMacVLanCreate(const char *ifname,
*/ */
int virNetDevMacVLanDelete(const char *ifname) int virNetDevMacVLanDelete(const char *ifname)
{ {
VIR_INFO("delete device: '%s'", ifname);
return virNetlinkDelLink(ifname, NULL); return virNetlinkDelLink(ifname, NULL);
} }
@ -903,13 +811,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
unsigned int flags) unsigned int flags)
{ {
const char *type = VIR_NET_GENERATED_PREFIX; const char *type = VIR_NET_GENERATED_PREFIX;
const char *pattern = (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) ? g_autofree char *ifname = NULL;
VIR_NET_GENERATED_MACVTAP_PATTERN : VIR_NET_GENERATED_MACVLAN_PATTERN;
int reservedID = -1;
char ifname[IFNAMSIZ];
int retries, do_retry = 0;
uint32_t macvtapMode; uint32_t macvtapMode;
const char *ifnameCreated = NULL;
int vf = -1; int vf = -1;
bool vnet_hdr = flags & VIR_NETDEV_MACVLAN_VNET_HDR; bool vnet_hdr = flags & VIR_NETDEV_MACVLAN_VNET_HDR;
@ -944,6 +847,8 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
return -1; return -1;
} }
virMutexLock(&virNetDevMacVLanCreateMutex);
if (ifnameRequested) { if (ifnameRequested) {
int rc; int rc;
bool isAutoName bool isAutoName
@ -951,97 +856,81 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX)); STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX));
VIR_INFO("Requested macvtap device name: %s", ifnameRequested); VIR_INFO("Requested macvtap device name: %s", ifnameRequested);
virMutexLock(&virNetDevMacVLanCreateMutex);
if ((rc = virNetDevExists(ifnameRequested)) < 0) { if ((rc = virNetDevExists(ifnameRequested)) < 0) {
virMutexUnlock(&virNetDevMacVLanCreateMutex); virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1; return -1;
} }
if (rc) { if (rc) {
if (isAutoName) /* ifnameRequested is already being used */
goto create_name;
virReportSystemError(EEXIST,
_("Unable to create %s device %s"),
type, ifnameRequested);
virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1;
}
if (isAutoName &&
(reservedID = virNetDevMacVLanReserveName(ifnameRequested, true)) < 0) {
reservedID = -1;
goto create_name;
}
if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress, if (!isAutoName) {
linkdev, macvtapMode, &do_retry) < 0) { virReportSystemError(EEXIST,
if (isAutoName) { _("Unable to create device '%s'"),
virNetDevMacVLanReleaseName(ifnameRequested); ifnameRequested);
reservedID = -1; virMutexUnlock(&virNetDevMacVLanCreateMutex);
goto create_name;
}
virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1;
}
/* virNetDevMacVLanCreate() was successful - use this name */
ifnameCreated = ifnameRequested;
create_name:
virMutexUnlock(&virNetDevMacVLanCreateMutex);
}
retries = MACVLAN_MAX_ID;
while (!ifnameCreated && retries) {
virMutexLock(&virNetDevMacVLanCreateMutex);
reservedID = virNetDevMacVLanReserveID(reservedID, flags, false, true);
if (reservedID < 0) {
virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1;
}
g_snprintf(ifname, sizeof(ifname), pattern, reservedID);
if (virNetDevMacVLanCreate(ifname, type, macaddress, linkdev,
macvtapMode, &do_retry) < 0) {
virNetDevMacVLanReleaseID(reservedID, flags);
virMutexUnlock(&virNetDevMacVLanCreateMutex);
if (!do_retry)
return -1; return -1;
VIR_INFO("Device %s wasn't reserved but already existed, skipping", }
ifname); } else {
retries--;
continue; /* ifnameRequested is available. try to open it */
virNetDevMacVLanReserveNameInternal(ifnameRequested);
if (virNetDevMacVLanCreate(ifnameRequested, type, macaddress,
linkdev, macvtapMode) == 0) {
/* virNetDevMacVLanCreate() was successful - use this name */
ifname = g_strdup(ifnameRequested);
} else if (!isAutoName) {
/* coudn't open ifnameRequested, but it wasn't an
* autogenerated named, so there is nothing else to
* try - fail and return.
*/
virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1;
}
} }
ifnameCreated = ifname;
virMutexUnlock(&virNetDevMacVLanCreateMutex);
} }
if (!ifnameCreated) { if (!ifname) {
virReportError(VIR_ERR_INTERNAL_ERROR, /* ifnameRequested was NULL, or it was an already in use
_("Too many unreserved %s devices in use"), * autogenerated name, so now we look for an unused
type); * autogenerated name.
return -1; */
if (virNetDevMacVLanGenerateName(&ifname, flags) < 0 ||
virNetDevMacVLanCreate(ifname, type, macaddress,
linkdev, macvtapMode) < 0) {
virMutexUnlock(&virNetDevMacVLanCreateMutex);
return -1;
}
} }
if (virNetDevVPortProfileAssociate(ifnameCreated, /* all done creating the device */
virMutexUnlock(&virNetDevMacVLanCreateMutex);
if (virNetDevVPortProfileAssociate(ifname,
virtPortProfile, virtPortProfile,
macaddress, macaddress,
linkdev, linkdev,
vf, vf,
vmuuid, vmOp, false) < 0) vmuuid, vmOp, false) < 0) {
goto link_del_exit; goto link_del_exit;
}
if (flags & VIR_NETDEV_MACVLAN_CREATE_IFUP) { if (flags & VIR_NETDEV_MACVLAN_CREATE_IFUP) {
if (virNetDevSetOnline(ifnameCreated, true) < 0) if (virNetDevSetOnline(ifname, true) < 0)
goto disassociate_exit; goto disassociate_exit;
} }
if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) { if (flags & VIR_NETDEV_MACVLAN_CREATE_WITH_TAP) {
if (virNetDevMacVLanTapOpen(ifnameCreated, tapfd, tapfdSize) < 0) if (virNetDevMacVLanTapOpen(ifname, tapfd, tapfdSize) < 0)
goto disassociate_exit; goto disassociate_exit;
if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, vnet_hdr) < 0) if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, vnet_hdr) < 0)
goto disassociate_exit; goto disassociate_exit;
*ifnameResult = g_strdup(ifnameCreated);
} else {
*ifnameResult = g_strdup(ifnameCreated);
} }
if (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_CREATE || if (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_CREATE ||
@ -1050,17 +939,18 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
* a saved image) - migration and libvirtd restart are handled * a saved image) - migration and libvirtd restart are handled
* elsewhere. * elsewhere.
*/ */
if (virNetDevMacVLanVPortProfileRegisterCallback(ifnameCreated, macaddress, if (virNetDevMacVLanVPortProfileRegisterCallback(ifname, macaddress,
linkdev, vmuuid, linkdev, vmuuid,
virtPortProfile, virtPortProfile,
vmOp) < 0) vmOp) < 0)
goto disassociate_exit; goto disassociate_exit;
} }
*ifnameResult = g_steal_pointer(&ifname);
return 0; return 0;
disassociate_exit: disassociate_exit:
ignore_value(virNetDevVPortProfileDisassociate(ifnameCreated, ignore_value(virNetDevVPortProfileDisassociate(ifname,
virtPortProfile, virtPortProfile,
macaddress, macaddress,
linkdev, linkdev,
@ -1070,9 +960,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested,
VIR_FORCE_CLOSE(tapfd[tapfdSize]); VIR_FORCE_CLOSE(tapfd[tapfdSize]);
link_del_exit: link_del_exit:
ignore_value(virNetDevMacVLanDelete(ifnameCreated)); ignore_value(virNetDevMacVLanDelete(ifname));
virNetDevMacVLanReleaseName(ifnameCreated);
return -1; return -1;
} }
@ -1106,7 +994,6 @@ int virNetDevMacVLanDeleteWithVPortProfile(const char *ifname,
ret = -1; ret = -1;
if (virNetDevMacVLanDelete(ifname) < 0) if (virNetDevMacVLanDelete(ifname) < 0)
ret = -1; ret = -1;
virNetDevMacVLanReleaseName(ifname);
} }
if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) { if (mode == VIR_NETDEV_MACVLAN_MODE_PASSTHRU) {
@ -1181,8 +1068,7 @@ int virNetDevMacVLanCreate(const char *ifname G_GNUC_UNUSED,
const char *type G_GNUC_UNUSED, const char *type G_GNUC_UNUSED,
const virMacAddr *macaddress G_GNUC_UNUSED, const virMacAddr *macaddress G_GNUC_UNUSED,
const char *srcdev G_GNUC_UNUSED, const char *srcdev G_GNUC_UNUSED,
uint32_t macvlan_mode G_GNUC_UNUSED, uint32_t macvlan_mode G_GNUC_UNUSED)
int *retry G_GNUC_UNUSED)
{ {
virReportSystemError(ENOSYS, "%s", virReportSystemError(ENOSYS, "%s",
_("Cannot create macvlan devices on this platform")); _("Cannot create macvlan devices on this platform"));
@ -1271,18 +1157,9 @@ int virNetDevMacVLanVPortProfileRegisterCallback(const char *ifname G_GNUC_UNUSE
return -1; return -1;
} }
int virNetDevMacVLanReleaseName(const char *name G_GNUC_UNUSED) void virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED)
{ {
virReportSystemError(ENOSYS, "%s", virReportSystemError(ENOSYS, "%s",
_("Cannot create macvlan devices on this platform")); _("Cannot create macvlan devices on this platform"));
return -1;
}
int virNetDevMacVLanReserveName(const char *name G_GNUC_UNUSED,
bool quietFail G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Cannot create macvlan devices on this platform"));
return -1;
} }
#endif /* ! WITH_MACVTAP */ #endif /* ! WITH_MACVTAP */

View File

@ -54,8 +54,7 @@ typedef enum {
#define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap" #define VIR_NET_GENERATED_MACVTAP_PREFIX "macvtap"
#define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan" #define VIR_NET_GENERATED_MACVLAN_PREFIX "macvlan"
int virNetDevMacVLanReserveName(const char *name, bool quietfail); void virNetDevMacVLanReserveName(const char *name);
int virNetDevMacVLanReleaseName(const char *name);
bool virNetDevMacVLanIsMacvtap(const char *ifname) bool virNetDevMacVLanIsMacvtap(const char *ifname)
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE; ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT G_GNUC_NO_INLINE;
@ -64,8 +63,7 @@ int virNetDevMacVLanCreate(const char *ifname,
const char *type, const char *type,
const virMacAddr *macaddress, const virMacAddr *macaddress,
const char *srcdev, const char *srcdev,
uint32_t macvlan_mode, uint32_t macvlan_mode)
int *retry)
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
G_GNUC_WARN_UNUSED_RESULT; G_GNUC_WARN_UNUSED_RESULT;