mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
netdevtap: Use common helper function to create unique tap name
Simplify GenerateName/ReserveName for netdevtap by using common functions. Signed-off-by: Shi Lei <shi_lei@massclouds.com> Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
parent
294fd4bd80
commit
c36cad1a31
@ -2692,7 +2692,6 @@ virNetDevTapGetName;
|
|||||||
virNetDevTapGetRealDeviceName;
|
virNetDevTapGetRealDeviceName;
|
||||||
virNetDevTapInterfaceStats;
|
virNetDevTapInterfaceStats;
|
||||||
virNetDevTapReattachBridge;
|
virNetDevTapReattachBridge;
|
||||||
virNetDevTapReserveName;
|
|
||||||
|
|
||||||
|
|
||||||
# util/virnetdevveth.h
|
# util/virnetdevveth.h
|
||||||
|
@ -3396,7 +3396,7 @@ qemuProcessNotifyNets(virDomainDefPtr def)
|
|||||||
case VIR_DOMAIN_NET_TYPE_BRIDGE:
|
case VIR_DOMAIN_NET_TYPE_BRIDGE:
|
||||||
case VIR_DOMAIN_NET_TYPE_NETWORK:
|
case VIR_DOMAIN_NET_TYPE_NETWORK:
|
||||||
case VIR_DOMAIN_NET_TYPE_ETHERNET:
|
case VIR_DOMAIN_NET_TYPE_ETHERNET:
|
||||||
virNetDevTapReserveName(net->ifname);
|
virNetDevReserveName(net->ifname);
|
||||||
break;
|
break;
|
||||||
case VIR_DOMAIN_NET_TYPE_USER:
|
case VIR_DOMAIN_NET_TYPE_USER:
|
||||||
case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
|
case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
|
||||||
|
@ -49,51 +49,11 @@
|
|||||||
#if defined(WITH_GETIFADDRS) && defined(AF_LINK)
|
#if defined(WITH_GETIFADDRS) && defined(AF_LINK)
|
||||||
# include <ifaddrs.h>
|
# include <ifaddrs.h>
|
||||||
#endif
|
#endif
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
VIR_LOG_INIT("util.netdevtap");
|
VIR_LOG_INIT("util.netdevtap");
|
||||||
|
|
||||||
virMutex virNetDevTapCreateMutex = VIR_MUTEX_INITIALIZER;
|
|
||||||
static int virNetDevTapLastID = -1; /* not "unsigned" because callers use %d */
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virNetDevTapReserveName:
|
|
||||||
* @name: name of an existing tap device
|
|
||||||
*
|
|
||||||
* Set the value of virNetDevTapLastID to assure that any new tap
|
|
||||||
* device created with an autogenerated name will use a number higher
|
|
||||||
* than the number in the given tap device name.
|
|
||||||
*
|
|
||||||
* Returns nothing.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
virNetDevTapReserveName(const char *name)
|
|
||||||
{
|
|
||||||
unsigned int id;
|
|
||||||
const char *idstr = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
if (STRPREFIX(name, VIR_NET_GENERATED_VNET_PREFIX)) {
|
|
||||||
|
|
||||||
VIR_INFO("marking device in use: '%s'", name);
|
|
||||||
|
|
||||||
idstr = name + strlen(VIR_NET_GENERATED_VNET_PREFIX);
|
|
||||||
|
|
||||||
if (virStrToLong_ui(idstr, NULL, 10, &id) >= 0) {
|
|
||||||
virMutexLock(&virNetDevTapCreateMutex);
|
|
||||||
|
|
||||||
if (virNetDevTapLastID < (int)id)
|
|
||||||
virNetDevTapLastID = id;
|
|
||||||
|
|
||||||
virMutexUnlock(&virNetDevTapCreateMutex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virNetDevTapGetName:
|
* virNetDevTapGetName:
|
||||||
* @tapfd: a tun/tap file descriptor
|
* @tapfd: a tun/tap file descriptor
|
||||||
@ -183,55 +143,6 @@ virNetDevTapGetRealDeviceName(char *ifname G_GNUC_UNUSED)
|
|||||||
|
|
||||||
|
|
||||||
#ifdef TUNSETIFF
|
#ifdef TUNSETIFF
|
||||||
/**
|
|
||||||
* virNetDevTapGenerateName:
|
|
||||||
* @ifname: pointer to pointer to string containing template
|
|
||||||
*
|
|
||||||
* generate a new (currently unused) name for a new tap device based
|
|
||||||
* on the templace string in @ifname - replace %d with
|
|
||||||
* ++virNetDevTapLastID, 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.
|
|
||||||
*
|
|
||||||
* Returns 0 on success, -1 on failure.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
virNetDevTapGenerateName(char **ifname)
|
|
||||||
{
|
|
||||||
int id;
|
|
||||||
double maxIDd = pow(10, IFNAMSIZ - 1 - strlen(VIR_NET_GENERATED_VNET_PREFIX));
|
|
||||||
int maxID = INT_MAX;
|
|
||||||
int attempts = 0;
|
|
||||||
|
|
||||||
if (maxIDd <= (double)INT_MAX)
|
|
||||||
maxID = (int)maxIDd;
|
|
||||||
|
|
||||||
do {
|
|
||||||
g_autofree char *try = NULL;
|
|
||||||
|
|
||||||
id = ++virNetDevTapLastID;
|
|
||||||
|
|
||||||
/* reset before overflow */
|
|
||||||
if (virNetDevTapLastID >= maxID)
|
|
||||||
virNetDevTapLastID = -1;
|
|
||||||
|
|
||||||
try = g_strdup_printf(*ifname, 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"),
|
|
||||||
VIR_NET_GENERATED_VNET_PREFIX);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virNetDevTapCreate:
|
* virNetDevTapCreate:
|
||||||
* @ifname: the interface name
|
* @ifname: the interface name
|
||||||
@ -263,16 +174,14 @@ int virNetDevTapCreate(char **ifname,
|
|||||||
int ret = -1;
|
int ret = -1;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
|
|
||||||
virMutexLock(&virNetDevTapCreateMutex);
|
|
||||||
|
|
||||||
/* if ifname is "vnet%d", then auto-generate a name for the new
|
/* if ifname is "vnet%d", then auto-generate a name for the new
|
||||||
* device (the kernel could do this for us, but has a bad habit of
|
* device (the kernel could do this for us, but has a bad habit of
|
||||||
* immediately re-using names that have just been released, which
|
* immediately re-using names that have just been released, which
|
||||||
* can lead to race conditions).
|
* can lead to race conditions).
|
||||||
*/
|
* if ifname is just a user-provided name, virNetDevGenerateName
|
||||||
if (STREQ(*ifname, VIR_NET_GENERATED_VNET_PREFIX "%d") &&
|
* leaves it unchanged. */
|
||||||
virNetDevTapGenerateName(ifname) < 0) {
|
if (virNetDevGenerateName(ifname, VIR_NET_DEV_GEN_NAME_VNET) < 0) {
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tunpath)
|
if (!tunpath)
|
||||||
@ -333,7 +242,6 @@ int virNetDevTapCreate(char **ifname,
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
virMutexUnlock(&virNetDevTapCreateMutex);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
while (i--)
|
while (i--)
|
||||||
|
@ -29,10 +29,6 @@
|
|||||||
# define VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP 1
|
# define VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
|
||||||
virNetDevTapReserveName(const char *name)
|
|
||||||
ATTRIBUTE_NONNULL(1);
|
|
||||||
|
|
||||||
int virNetDevTapCreate(char **ifname,
|
int virNetDevTapCreate(char **ifname,
|
||||||
const char *tunpath,
|
const char *tunpath,
|
||||||
int *tapfd,
|
int *tapfd,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user