2008-06-26 16:07:48 +00:00
|
|
|
/*
|
2014-02-20 00:32:19 +00:00
|
|
|
* Copyright (C) 2010-2014 Red Hat, Inc.
|
2008-06-26 16:07:48 +00:00
|
|
|
* Copyright IBM Corp. 2008
|
|
|
|
*
|
2011-11-02 16:03:09 +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-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-06-26 16:07:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2010-07-23 17:25:56 +00:00
|
|
|
#include <sys/wait.h>
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2011-11-02 16:03:09 +00:00
|
|
|
#include "virnetdevveth.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "virutil.h"
|
2013-10-02 11:18:49 +00:00
|
|
|
#include "virnetdev.h"
|
2010-07-23 17:25:56 +00:00
|
|
|
|
2011-11-02 15:53:39 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
2010-07-23 17:25:56 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.netdevveth");
|
|
|
|
|
2008-06-26 16:07:48 +00:00
|
|
|
/* Functions */
|
2013-10-02 11:18:49 +00:00
|
|
|
|
2014-03-25 14:54:44 +00:00
|
|
|
virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER;
|
2014-02-25 15:41:07 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
static int virNetDevVethExists(int devNum)
|
|
|
|
{
|
|
|
|
int ret;
|
2018-07-28 18:01:35 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
|
|
|
|
2015-04-15 09:45:47 +00:00
|
|
|
if (virAsprintf(&path, SYSFS_NET_DIR "vnet%d/", devNum) < 0)
|
2013-10-02 11:18:49 +00:00
|
|
|
return -1;
|
|
|
|
ret = virFileExists(path) ? 1 : 0;
|
2013-10-02 11:24:54 +00:00
|
|
|
VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);
|
2013-10-02 11:18:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-26 16:07:48 +00:00
|
|
|
/**
|
2013-10-02 11:18:49 +00:00
|
|
|
* virNetDevVethGetFreeNum:
|
2008-06-26 16:07:48 +00:00
|
|
|
* @startDev: device number to start at (x in vethx)
|
|
|
|
*
|
|
|
|
* Looks in /sys/class/net/ to find the first available veth device
|
|
|
|
* name.
|
|
|
|
*
|
2010-08-31 22:04:46 +00:00
|
|
|
* Returns non-negative device number on success or -1 in case of error
|
2008-06-26 16:07:48 +00:00
|
|
|
*/
|
2013-10-02 11:18:49 +00:00
|
|
|
static int virNetDevVethGetFreeNum(int startDev)
|
2008-06-26 16:07:48 +00:00
|
|
|
{
|
2013-10-02 11:18:49 +00:00
|
|
|
int devNum;
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
#define MAX_DEV_NUM 65536
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
for (devNum = startDev; devNum < MAX_DEV_NUM; devNum++) {
|
|
|
|
int ret = virNetDevVethExists(devNum);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
if (ret == 0)
|
|
|
|
return devNum;
|
|
|
|
}
|
2011-06-07 13:26:48 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("No free veth devices available"));
|
|
|
|
return -1;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-02 15:53:39 +00:00
|
|
|
* virNetDevVethCreate:
|
2010-08-31 22:04:46 +00:00
|
|
|
* @veth1: pointer to name for parent end of veth pair
|
|
|
|
* @veth2: pointer to return name for container end of veth pair
|
2008-06-26 16:07:48 +00:00
|
|
|
*
|
|
|
|
* Creates a veth device pair using the ip command:
|
2014-11-26 08:25:43 +00:00
|
|
|
* ip link add veth1 type veth peer name veth2
|
2010-08-31 22:04:46 +00:00
|
|
|
* If veth1 points to NULL on entry, it will be a valid interface on
|
|
|
|
* return. veth2 should point to NULL on entry.
|
|
|
|
*
|
2008-06-26 16:07:48 +00:00
|
|
|
* NOTE: If veth1 and veth2 names are not specified, ip will auto assign
|
|
|
|
* names. There seems to be two problems here -
|
|
|
|
* 1) There doesn't seem to be a way to determine the names of the
|
|
|
|
* devices that it creates. They show up in ip link show and
|
|
|
|
* under /sys/class/net/ however there is no guarantee that they
|
|
|
|
* are the devices that this process just created.
|
|
|
|
* 2) Once one of the veth devices is moved to another namespace, it
|
|
|
|
* is no longer visible in the parent namespace. This seems to
|
|
|
|
* confuse the name assignment causing it to fail with File exists.
|
2010-08-31 22:04:46 +00:00
|
|
|
* Because of these issues, this function currently allocates names
|
|
|
|
* prior to using the ip command, and returns any allocated names
|
|
|
|
* to the caller.
|
2008-06-26 16:07:48 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
2011-11-02 15:53:39 +00:00
|
|
|
int virNetDevVethCreate(char** veth1, char** veth2)
|
2008-06-26 16:07:48 +00:00
|
|
|
{
|
2013-10-02 11:18:49 +00:00
|
|
|
int ret = -1;
|
|
|
|
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
|
|
|
|
*/
|
2014-02-25 15:41:07 +00:00
|
|
|
virMutexLock(&virNetDevVethCreateMutex);
|
2013-10-02 11:18:49 +00:00
|
|
|
#define MAX_VETH_RETRIES 10
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_VETH_RETRIES; i++) {
|
2018-07-28 18:01:35 +00:00
|
|
|
VIR_AUTOFREE(char *) veth1auto = NULL;
|
|
|
|
VIR_AUTOFREE(char *) veth2auto = NULL;
|
2018-07-28 18:01:36 +00:00
|
|
|
VIR_AUTOPTR(virCommand) cmd = NULL;
|
2018-07-28 18:01:35 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
int status;
|
|
|
|
if (!*veth1) {
|
|
|
|
int veth1num;
|
|
|
|
if ((veth1num = virNetDevVethGetFreeNum(vethNum)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-10-02 11:24:54 +00:00
|
|
|
if (virAsprintf(&veth1auto, "vnet%d", veth1num) < 0)
|
2013-10-02 11:18:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
vethNum = veth1num + 1;
|
|
|
|
}
|
|
|
|
if (!*veth2) {
|
|
|
|
int veth2num;
|
|
|
|
if ((veth2num = virNetDevVethGetFreeNum(vethNum)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-10-02 11:24:54 +00:00
|
|
|
if (virAsprintf(&veth2auto, "vnet%d", veth2num) < 0)
|
2013-10-02 11:18:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
vethNum = veth2num + 1;
|
|
|
|
}
|
|
|
|
|
2013-10-04 10:53:53 +00:00
|
|
|
cmd = virCommandNew("ip");
|
2014-11-26 08:25:43 +00:00
|
|
|
virCommandAddArgList(cmd, "link", "add",
|
2013-10-02 11:18:49 +00:00
|
|
|
*veth1 ? *veth1 : veth1auto,
|
|
|
|
"type", "veth", "peer", "name",
|
|
|
|
*veth2 ? *veth2 : veth2auto,
|
|
|
|
NULL);
|
2010-08-31 22:04:46 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2011-03-17 15:54:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
if (status == 0) {
|
|
|
|
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;
|
2010-08-31 22:04:46 +00:00
|
|
|
}
|
2011-03-17 15:54:24 +00:00
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
VIR_DEBUG("Failed to create veth host: %s guest: %s: %d",
|
|
|
|
*veth1 ? *veth1 : veth1auto,
|
2014-02-20 00:32:19 +00:00
|
|
|
*veth2 ? *veth2 : veth2auto,
|
2013-10-02 11:18:49 +00:00
|
|
|
status);
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 11:18:49 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to allocate free veth pair after %d attempts"),
|
|
|
|
MAX_VETH_RETRIES);
|
2011-03-17 15:54:24 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-02-25 15:41:07 +00:00
|
|
|
virMutexUnlock(&virNetDevVethCreateMutex);
|
2013-10-02 11:18:49 +00:00
|
|
|
return ret;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-02 15:53:39 +00:00
|
|
|
* virNetDevVethDelete:
|
2008-06-26 16:07:48 +00:00
|
|
|
* @veth: name for one end of veth pair
|
|
|
|
*
|
|
|
|
* This will delete both veth devices in a pair. Only one end needs to
|
|
|
|
* be specified. The ip command will identify and delete the other veth
|
|
|
|
* device as well.
|
|
|
|
* ip link del veth
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
2011-11-02 15:53:39 +00:00
|
|
|
int virNetDevVethDelete(const char *veth)
|
2008-06-26 16:07:48 +00:00
|
|
|
{
|
2013-10-02 10:16:14 +00:00
|
|
|
int status;
|
2018-07-28 18:01:36 +00:00
|
|
|
VIR_AUTOPTR(virCommand) cmd = virCommandNewArgList("ip", "link",
|
|
|
|
"del", veth, NULL);
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2013-10-02 10:16:14 +00:00
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2018-07-28 18:01:36 +00:00
|
|
|
return -1;
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2013-10-02 10:16:14 +00:00
|
|
|
if (status != 0) {
|
|
|
|
if (!virNetDevExists(veth)) {
|
|
|
|
VIR_DEBUG("Device %s already deleted (by kernel namespace cleanup)", veth);
|
2018-07-28 18:01:36 +00:00
|
|
|
return 0;
|
2013-10-02 10:16:14 +00:00
|
|
|
}
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to delete veth device %s"), veth);
|
2018-07-28 18:01:36 +00:00
|
|
|
return -1;
|
2013-10-02 10:16:14 +00:00
|
|
|
}
|
2013-10-04 10:53:52 +00:00
|
|
|
|
2018-07-28 18:01:36 +00:00
|
|
|
return 0;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|