1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-02-03 17:47:15 +03:00

netdevveth: Simplify virNetDevVethCreate by using virNetDevGenerateName

Simplify virNetDevVethCreate by using common GenerateName/ReserveName
functions.

Signed-off-by: Shi Lei <shi_lei@massclouds.com>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Shi Lei 2020-12-14 09:50:35 +08:00 committed by Laine Stump
parent 9b5d741a9d
commit 2dd0fb492f
2 changed files with 32 additions and 106 deletions

View File

@ -308,6 +308,9 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm,
VIR_DEBUG("calling vethCreate()"); VIR_DEBUG("calling vethCreate()");
parentVeth = net->ifname; parentVeth = net->ifname;
if (parentVeth)
virNetDevReserveName(parentVeth);
if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0) if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0)
return NULL; return NULL;
VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth); VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);

View File

@ -32,48 +32,6 @@
VIR_LOG_INIT("util.netdevveth"); VIR_LOG_INIT("util.netdevveth");
/* Functions */
virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER;
static int virNetDevVethExists(int devNum)
{
int ret;
g_autofree char *path = NULL;
path = g_strdup_printf(SYSFS_NET_DIR "vnet%d/", devNum);
ret = virFileExists(path) ? 1 : 0;
VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);
return ret;
}
/**
* virNetDevVethGetFreeNum:
* @startDev: device number to start at (x in vethx)
*
* Looks in /sys/class/net/ to find the first available veth device
* name.
*
* Returns non-negative device number on success or -1 in case of error
*/
static int virNetDevVethGetFreeNum(int startDev)
{
int devNum;
#define MAX_DEV_NUM 65536
for (devNum = startDev; devNum < MAX_DEV_NUM; devNum++) {
int ret = virNetDevVethExists(devNum);
if (ret < 0)
return -1;
if (ret == 0)
return devNum;
}
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("No free veth devices available"));
return -1;
}
/** /**
* virNetDevVethCreate: * virNetDevVethCreate:
@ -102,39 +60,16 @@ static int virNetDevVethGetFreeNum(int startDev)
*/ */
int virNetDevVethCreate(char** veth1, char** veth2) int virNetDevVethCreate(char** veth1, char** veth2)
{ {
int ret = -1; int status;
int vethNum = 0;
size_t i;
/*
* We might race with other containers, but this is reasonably
* unlikely, so don't do too many retries for device creation
*/
virMutexLock(&virNetDevVethCreateMutex);
#define MAX_VETH_RETRIES 10
for (i = 0; i < MAX_VETH_RETRIES; i++) {
g_autofree char *veth1auto = NULL; g_autofree char *veth1auto = NULL;
g_autofree char *veth2auto = NULL; g_autofree char *veth2auto = NULL;
g_autoptr(virCommand) cmd = NULL; g_autoptr(virCommand) cmd = NULL;
int status; if (virNetDevGenerateName(&veth1auto, VIR_NET_DEV_GEN_NAME_VNET) < 0)
if (!*veth1) { return -1;
int veth1num;
if ((veth1num = virNetDevVethGetFreeNum(vethNum)) < 0)
goto cleanup;
veth1auto = g_strdup_printf("vnet%d", veth1num); if (virNetDevGenerateName(&veth2auto, VIR_NET_DEV_GEN_NAME_VNET) < 0)
vethNum = veth1num + 1; return -1;
}
if (!*veth2) {
int veth2num;
if ((veth2num = virNetDevVethGetFreeNum(vethNum)) < 0)
goto cleanup;
veth2auto = g_strdup_printf("vnet%d", veth2num);
vethNum = veth2num + 1;
}
cmd = virCommandNew("ip"); cmd = virCommandNew("ip");
virCommandAddArgList(cmd, "link", "add", virCommandAddArgList(cmd, "link", "add",
@ -143,36 +78,24 @@ int virNetDevVethCreate(char** veth1, char** veth2)
*veth2 ? *veth2 : veth2auto, *veth2 ? *veth2 : veth2auto,
NULL); NULL);
if (virCommandRun(cmd, &status) < 0) if (virCommandRun(cmd, &status) < 0 || status) {
goto cleanup; virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Failed to allocate free veth pair"));
if (status == 0) { return -1;
if (veth1auto) {
*veth1 = veth1auto;
veth1auto = NULL;
}
if (veth2auto) {
*veth2 = veth2auto;
veth2auto = NULL;
}
VIR_DEBUG("Create Host: %s guest: %s", *veth1, *veth2);
ret = 0;
goto cleanup;
} }
VIR_DEBUG("Failed to create veth host: %s guest: %s: %d", VIR_DEBUG("create veth host: %s guest: %s: %d",
*veth1 ? *veth1 : veth1auto, *veth1 ? *veth1 : veth1auto,
*veth2 ? *veth2 : veth2auto, *veth2 ? *veth2 : veth2auto,
status); status);
}
virReportError(VIR_ERR_INTERNAL_ERROR, if (veth1auto)
_("Failed to allocate free veth pair after %d attempts"), *veth1 = g_steal_pointer(&veth1auto);
MAX_VETH_RETRIES); if (veth2auto)
*veth2 = g_steal_pointer(&veth2auto);
cleanup: VIR_DEBUG("Create Host: %s guest: %s", *veth1, *veth2);
virMutexUnlock(&virNetDevVethCreateMutex); return 0;
return ret;
} }
/** /**