Move LXC veth.c code into shared utility APIs

Move the virNetDevSetName and virNetDevSetNamespace APIs out
of LXC's veth.c and into virnetdev.c.

Move the remaining content of the file to src/util/virnetdevveth.c

* src/lxc/veth.c: Rename to src/util/virnetdevveth.c
* src/lxc/veth.h: Rename to src/util/virnetdevveth.h
* src/util/virnetdev.c, src/util/virnetdev.h: Add
  virNetDevSetName and virNetDevSetNamespace
* src/lxc/lxc_container.c, src/lxc/lxc_controller.c,
  src/lxc/lxc_driver.c: Update include paths
This commit is contained in:
Daniel P. Berrange 2011-11-02 16:03:09 +00:00
parent 29b242ad80
commit 428cffb1e7
9 changed files with 144 additions and 114 deletions

View File

@ -96,6 +96,7 @@ UTIL_SOURCES = \
util/virnetdevbandwidth.h util/virnetdevbandwidth.c \ util/virnetdevbandwidth.h util/virnetdevbandwidth.c \
util/virnetdevbridge.h util/virnetdevbridge.c \ util/virnetdevbridge.h util/virnetdevbridge.c \
util/virnetdevtap.h util/virnetdevtap.c \ util/virnetdevtap.h util/virnetdevtap.c \
util/virnetdevveth.h util/virnetdevveth.c \
util/virnetdevvportprofile.h util/virnetdevvportprofile.c \ util/virnetdevvportprofile.h util/virnetdevvportprofile.c \
util/virsocketaddr.h util/virsocketaddr.c util/virsocketaddr.h util/virsocketaddr.c
@ -320,14 +321,12 @@ endif
LXC_DRIVER_SOURCES = \ LXC_DRIVER_SOURCES = \
lxc/lxc_conf.c lxc/lxc_conf.h \ lxc/lxc_conf.c lxc/lxc_conf.h \
lxc/lxc_container.c lxc/lxc_container.h \ lxc/lxc_container.c lxc/lxc_container.h \
lxc/lxc_driver.c lxc/lxc_driver.h \ lxc/lxc_driver.c lxc/lxc_driver.h
lxc/veth.c lxc/veth.h
LXC_CONTROLLER_SOURCES = \ LXC_CONTROLLER_SOURCES = \
lxc/lxc_conf.c lxc/lxc_conf.h \ lxc/lxc_conf.c lxc/lxc_conf.h \
lxc/lxc_container.c lxc/lxc_container.h \ lxc/lxc_container.c lxc/lxc_container.h \
lxc/lxc_controller.c \ lxc/lxc_controller.c
lxc/veth.c lxc/veth.h
SECURITY_DRIVER_APPARMOR_HELPER_SOURCES = \ SECURITY_DRIVER_APPARMOR_HELPER_SOURCES = \
security/virt-aa-helper.c security/virt-aa-helper.c

View File

@ -55,7 +55,7 @@
#include "lxc_container.h" #include "lxc_container.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "veth.h" #include "virnetdevveth.h"
#include "uuid.h" #include "uuid.h"
#include "virfile.h" #include "virfile.h"
#include "command.h" #include "command.h"

View File

@ -54,7 +54,8 @@
#include "lxc_conf.h" #include "lxc_conf.h"
#include "lxc_container.h" #include "lxc_container.h"
#include "veth.h" #include "virnetdev.h"
#include "virnetdevveth.h"
#include "memory.h" #include "memory.h"
#include "util.h" #include "util.h"
#include "virfile.h" #include "virfile.h"

View File

@ -44,7 +44,7 @@
#include "memory.h" #include "memory.h"
#include "util.h" #include "util.h"
#include "virnetdevbridge.h" #include "virnetdevbridge.h"
#include "veth.h" #include "virnetdevveth.h"
#include "nodeinfo.h" #include "nodeinfo.h"
#include "uuid.h" #include "uuid.h"
#include "stats_linux.h" #include "stats_linux.h"

View File

@ -1,29 +0,0 @@
/*
* veth.h: Interface to tools for managing veth pairs
*
* Copyright (C) 2010 Red Hat, Inc.
* Copyright IBM Corp. 2008
*
* See COPYING.LIB for the License of this software
*
* Authors:
* David L. Leskovec <dlesko at linux.vnet.ibm.com>
*/
#ifndef VETH_H
# define VETH_H
# include <config.h>
# include "internal.h"
/* Function declarations */
int virNetDevVethCreate(char **veth1, char **veth2)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevVethDelete(const char *veth)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetNamespace(const char *ifname, int pidInNs)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetName(const char *ifname, const char *newifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
#endif /* VETH_H */

View File

@ -332,6 +332,87 @@ int virNetDevSetMTUFromDevice(const char *ifname,
} }
/**
* virNetDevSetNamespace:
* @ifname: name of device
* @pidInNs: PID of process in target net namespace
*
* Moves the given device into the target net namespace specified by the given
* pid using this command:
* ip link set @iface netns @pidInNs
*
* Returns 0 on success or -1 in case of error
*/
int virNetDevSetNamespace(const char *ifname, int pidInNs)
{
int rc;
char *pid = NULL;
const char *argv[] = {
"ip", "link", "set", ifname, "netns", NULL, NULL
};
if (virAsprintf(&pid, "%d", pidInNs) == -1) {
virReportOOMError();
return -1;
}
argv[5] = pid;
rc = virRun(argv, NULL);
VIR_FREE(pid);
return rc;
}
#ifdef SIOCSIFNAME
/**
* virNetDevSetName:
* @ifname: name of device
* @newifname: new name of @ifname
*
* Changes the name of the given device.
*
* Returns 0 on success, -1 on error
*/
int virNetDevSetName(const char* ifname, const char *newifname)
{
int fd = -1;
int ret = -1;
struct ifreq ifr;
if ((fd = virNetDevSetupControl(ifname, &ifr)) < 0)
return -1;
if (virStrcpyStatic(ifr.ifr_newname, newifname) == NULL) {
virReportSystemError(ERANGE,
_("Network interface name '%s' is too long"),
newifname);
goto cleanup;
}
if (ioctl(fd, SIOCSIFNAME, &ifr)) {
virReportSystemError(errno,
_("Unable to rename '%s' to '%s'"),
ifname, newifname);
goto cleanup;
}
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
return ret;
}
#else
int virNetDevSetName(const char* ifname, const char *newifname)
{
virReportSystemError(ENOSYS,
_("Cannot rename interface '%s' to '%s' on this platform"),
ifname, newifname);
return -1;
}
#endif
#ifdef SIOCSIFFLAGS #ifdef SIOCSIFFLAGS
/** /**
* virNetDevSetOnline: * virNetDevSetOnline:

View File

@ -59,5 +59,9 @@ int virNetDevSetMTUFromDevice(const char *ifname,
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevGetMTU(const char *ifname) int virNetDevGetMTU(const char *ifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetNamespace(const char *ifname, int pidInNs)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetName(const char *ifname, const char *newifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
#endif /* __VIR_NETDEV_H__ */ #endif /* __VIR_NETDEV_H__ */

View File

@ -1,32 +1,35 @@
/* /*
* veth.c: Tools for managing veth pairs
*
* Copyright (C) 2010-2011 Red Hat, Inc. * Copyright (C) 2010-2011 Red Hat, Inc.
* Copyright IBM Corp. 2008 * Copyright IBM Corp. 2008
* *
* See COPYING.LIB for the License of this software * 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* Authors: * Authors:
* David L. Leskovec <dlesko at linux.vnet.ibm.com> * David L. Leskovec <dlesko at linux.vnet.ibm.com>
* Daniel P. Berrange <berrange@redhat.com>
*/ */
#include <config.h> #include <config.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include "veth.h" #include "virnetdevveth.h"
#include "internal.h"
#include "logging.h"
#include "memory.h" #include "memory.h"
#include "logging.h"
#include "command.h" #include "command.h"
#include "virterror_internal.h" #include "virterror_internal.h"
#include "virfile.h"
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
@ -184,67 +187,3 @@ int virNetDevVethDelete(const char *veth)
return rc; return rc;
} }
/**
* virNetDevSetNamespace:
* @ifname: name of device
* @pidInNs: PID of process in target net namespace
*
* Moves the given device into the target net namespace specified by the given
* pid using this command:
* ip link set @iface netns @pidInNs
*
* Returns 0 on success or -1 in case of error
*/
int virNetDevSetNamespace(const char* ifname, int pidInNs)
{
int rc;
char *pid = NULL;
const char *argv[] = {
"ip", "link", "set", ifname, "netns", NULL, NULL
};
if (virAsprintf(&pid, "%d", pidInNs) == -1) {
virReportOOMError();
return -1;
}
argv[5] = pid;
rc = virRun(argv, NULL);
VIR_FREE(pid);
return rc;
}
/**
* virNetDevSetName:
* @ifname: name of device
* @new: new name of @ifname
*
* Changes the name of the given device.
*
* Returns 0 on success, -1 on failure with errno set.
*/
int virNetDevSetName(const char* ifname, const char* new)
{
struct ifreq ifr;
int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(struct ifreq));
if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL) {
errno = EINVAL;
return -1;
}
if (virStrcpyStatic(ifr.ifr_newname, new) == NULL) {
errno = EINVAL;
return -1;
}
if (ioctl(fd, SIOCSIFNAME, &ifr))
return -1;
return 0;
}

35
src/util/virnetdevveth.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2010-2011 Red Hat, Inc.
* Copyright IBM Corp. 2008
*
* 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* David L. Leskovec <dlesko at linux.vnet.ibm.com>
* Daniel P. Berrange <berrange@redhat.com>
*/
#ifndef __VIR_NETDEV_VETH_H__
# define __VIR_NETDEV_VETH_H__
# include "internal.h"
/* Function declarations */
int virNetDevVethCreate(char **veth1, char **veth2)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevVethDelete(const char *veth)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
#endif /* __VIR_NETDEV_VETH_H__ */