2008-06-26 16:07:48 +00:00
|
|
|
/*
|
2013-05-09 18:59:04 +00:00
|
|
|
* Copyright (C) 2010-2013 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
|
|
|
*
|
|
|
|
* Authors:
|
2011-11-02 16:03:09 +00:00
|
|
|
* David L. Leskovec <dlesko at linux.vnet.ibm.com>
|
|
|
|
* Daniel P. Berrange <berrange@redhat.com>
|
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"
|
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
|
|
|
|
2008-06-26 16:07:48 +00:00
|
|
|
/* Functions */
|
|
|
|
/**
|
2011-11-02 15:53:39 +00:00
|
|
|
* virNetDevVethGetFreeName:
|
2010-08-31 22:04:46 +00:00
|
|
|
* @veth: pointer to store returned name for veth device
|
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
|
|
|
*/
|
2011-11-02 15:53:39 +00:00
|
|
|
static int virNetDevVethGetFreeName(char **veth, int startDev)
|
2008-06-26 16:07:48 +00:00
|
|
|
{
|
2009-04-14 17:51:12 +00:00
|
|
|
int devNum = startDev-1;
|
2010-08-31 22:04:46 +00:00
|
|
|
char *path = NULL;
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Find free from veth%d", startDev);
|
2008-06-26 16:07:48 +00:00
|
|
|
do {
|
2010-08-31 22:04:46 +00:00
|
|
|
VIR_FREE(path);
|
2008-06-26 16:07:48 +00:00
|
|
|
++devNum;
|
2013-07-04 10:17:18 +00:00
|
|
|
if (virAsprintf(&path, "/sys/class/net/veth%d/", devNum) < 0)
|
2010-08-31 22:04:46 +00:00
|
|
|
return -1;
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Probe %s", path);
|
2008-06-26 16:07:48 +00:00
|
|
|
} while (virFileExists(path));
|
2010-08-31 22:04:46 +00:00
|
|
|
VIR_FREE(path);
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (virAsprintf(veth, "veth%d", devNum) < 0)
|
2010-08-31 22:04:46 +00:00
|
|
|
return -1;
|
2011-06-07 13:26:48 +00:00
|
|
|
|
2010-08-31 22:04:46 +00:00
|
|
|
return devNum;
|
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:
|
|
|
|
* 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
|
|
|
{
|
2011-03-17 15:54:24 +00:00
|
|
|
int rc = -1;
|
2008-06-26 16:07:48 +00:00
|
|
|
const char *argv[] = {
|
2010-08-31 22:04:46 +00:00
|
|
|
"ip", "link", "add", NULL, "type", "veth", "peer", "name", NULL, NULL
|
2008-06-26 16:07:48 +00:00
|
|
|
};
|
|
|
|
int vethDev = 0;
|
2010-08-31 22:04:46 +00:00
|
|
|
bool veth1_alloc = false;
|
2011-03-17 15:54:24 +00:00
|
|
|
bool veth2_alloc = false;
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Host: %s guest: %s", NULLSTR(*veth1), NULLSTR(*veth2));
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2010-08-31 22:04:46 +00:00
|
|
|
if (*veth1 == NULL) {
|
2011-11-02 15:53:39 +00:00
|
|
|
if ((vethDev = virNetDevVethGetFreeName(veth1, vethDev)) < 0)
|
2011-03-17 15:54:24 +00:00
|
|
|
goto cleanup;
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Assigned host: %s", *veth1);
|
2010-08-31 22:04:46 +00:00
|
|
|
veth1_alloc = true;
|
2011-06-07 13:26:48 +00:00
|
|
|
vethDev++;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
2010-08-31 22:04:46 +00:00
|
|
|
argv[3] = *veth1;
|
|
|
|
|
2011-03-17 15:54:24 +00:00
|
|
|
while (*veth2 == NULL) {
|
2011-11-02 15:53:39 +00:00
|
|
|
if ((vethDev = virNetDevVethGetFreeName(veth2, vethDev)) < 0) {
|
2010-08-31 22:04:46 +00:00
|
|
|
if (veth1_alloc)
|
|
|
|
VIR_FREE(*veth1);
|
2011-03-17 15:54:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just make sure they didn't accidentally get same name */
|
|
|
|
if (STREQ(*veth1, *veth2)) {
|
|
|
|
vethDev++;
|
|
|
|
VIR_FREE(*veth2);
|
|
|
|
continue;
|
2010-08-31 22:04:46 +00:00
|
|
|
}
|
2011-03-17 15:54:24 +00:00
|
|
|
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Assigned guest: %s", *veth2);
|
2011-03-17 15:54:24 +00:00
|
|
|
veth2_alloc = true;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
2010-08-31 22:04:46 +00:00
|
|
|
argv[8] = *veth2;
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2011-06-07 13:26:48 +00:00
|
|
|
VIR_DEBUG("Create Host: %s guest: %s", *veth1, *veth2);
|
2011-03-08 18:46:29 +00:00
|
|
|
if (virRun(argv, NULL) < 0) {
|
2010-08-31 22:04:46 +00:00
|
|
|
if (veth1_alloc)
|
|
|
|
VIR_FREE(*veth1);
|
2011-03-17 15:54:24 +00:00
|
|
|
if (veth2_alloc)
|
|
|
|
VIR_FREE(*veth2);
|
|
|
|
goto cleanup;
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 15:54:24 +00:00
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2008-06-26 16:07:48 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
|
|
|
const char *argv[] = {"ip", "link", "del", veth, NULL};
|
|
|
|
|
2011-02-16 23:37:57 +00:00
|
|
|
VIR_DEBUG("veth: %s", veth);
|
2008-06-26 16:07:48 +00:00
|
|
|
|
2012-11-27 12:35:49 +00:00
|
|
|
return virRun(argv, NULL);
|
2008-06-26 16:07:48 +00:00
|
|
|
}
|