From: Daniel P. Berrange Date: Wed, 2 Nov 2011 16:03:09 +0000 (+0000) Subject: Move LXC veth.c code into shared utility APIs X-Git-Tag: v0.9.8-rc1~193 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=428cffb1e7b30a560c83024a04ef97873d312125;p=thirdparty%2Flibvirt.git 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 --- diff --git a/src/Makefile.am b/src/Makefile.am index e0979ebfcc..fbe4c4d714 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -96,6 +96,7 @@ UTIL_SOURCES = \ util/virnetdevbandwidth.h util/virnetdevbandwidth.c \ util/virnetdevbridge.h util/virnetdevbridge.c \ util/virnetdevtap.h util/virnetdevtap.c \ + util/virnetdevveth.h util/virnetdevveth.c \ util/virnetdevvportprofile.h util/virnetdevvportprofile.c \ util/virsocketaddr.h util/virsocketaddr.c @@ -320,14 +321,12 @@ endif LXC_DRIVER_SOURCES = \ lxc/lxc_conf.c lxc/lxc_conf.h \ lxc/lxc_container.c lxc/lxc_container.h \ - lxc/lxc_driver.c lxc/lxc_driver.h \ - lxc/veth.c lxc/veth.h + lxc/lxc_driver.c lxc/lxc_driver.h LXC_CONTROLLER_SOURCES = \ lxc/lxc_conf.c lxc/lxc_conf.h \ lxc/lxc_container.c lxc/lxc_container.h \ - lxc/lxc_controller.c \ - lxc/veth.c lxc/veth.h + lxc/lxc_controller.c SECURITY_DRIVER_APPARMOR_HELPER_SOURCES = \ security/virt-aa-helper.c diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 137a07ccb7..1e7803abce 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -55,7 +55,7 @@ #include "lxc_container.h" #include "util.h" #include "memory.h" -#include "veth.h" +#include "virnetdevveth.h" #include "uuid.h" #include "virfile.h" #include "command.h" diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index dcf78f9af6..40047f098a 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -54,7 +54,8 @@ #include "lxc_conf.h" #include "lxc_container.h" -#include "veth.h" +#include "virnetdev.h" +#include "virnetdevveth.h" #include "memory.h" #include "util.h" #include "virfile.h" diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index dc20d6eff4..ccd8bad99a 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -44,7 +44,7 @@ #include "memory.h" #include "util.h" #include "virnetdevbridge.h" -#include "veth.h" +#include "virnetdevveth.h" #include "nodeinfo.h" #include "uuid.h" #include "stats_linux.h" diff --git a/src/lxc/veth.h b/src/lxc/veth.h deleted file mode 100644 index 4a66098731..0000000000 --- a/src/lxc/veth.h +++ /dev/null @@ -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 - */ - -#ifndef VETH_H -# define VETH_H - -# include -# 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 */ diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 1651cb30de..28aa1fb10d 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -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 /** * virNetDevSetOnline: diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 219196b18b..0b0db84908 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -59,5 +59,9 @@ int virNetDevSetMTUFromDevice(const char *ifname, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; int virNetDevGetMTU(const char *ifname) 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__ */ diff --git a/src/lxc/veth.c b/src/util/virnetdevveth.c similarity index 74% rename from src/lxc/veth.c rename to src/util/virnetdevveth.c index b31ce33edf..c77e55837f 100644 --- a/src/lxc/veth.c +++ b/src/util/virnetdevveth.c @@ -1,32 +1,35 @@ /* - * veth.c: Tools for managing veth pairs - * * Copyright (C) 2010-2011 Red Hat, Inc. * 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: - * David L. Leskovec + * David L. Leskovec + * Daniel P. Berrange */ #include -#include -#include -#include -#include -#include -#include #include -#include "veth.h" -#include "internal.h" -#include "logging.h" +#include "virnetdevveth.h" #include "memory.h" +#include "logging.h" #include "command.h" #include "virterror_internal.h" -#include "virfile.h" #define VIR_FROM_THIS VIR_FROM_NONE @@ -184,67 +187,3 @@ int virNetDevVethDelete(const char *veth) 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; -} diff --git a/src/util/virnetdevveth.h b/src/util/virnetdevveth.h new file mode 100644 index 0000000000..0d450f17be --- /dev/null +++ b/src/util/virnetdevveth.h @@ -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 + * Daniel P. Berrange + */ + +#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__ */