[libvirt] [PATCH 15/33] Move LXC veth.c code into shared utility APIs

Daniel P. Berrange berrange at redhat.com
Thu Nov 3 17:30:11 UTC 2011


From: "Daniel P. Berrange" <berrange at redhat.com>

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
---
 src/Makefile.am                          |    7 +-
 src/lxc/lxc_container.c                  |    2 +-
 src/lxc/lxc_controller.c                 |    3 +-
 src/lxc/lxc_driver.c                     |    2 +-
 src/lxc/veth.h                           |   29 ---------
 src/util/virnetdev.c                     |   81 +++++++++++++++++++++++++
 src/util/virnetdev.h                     |    4 +
 src/{lxc/veth.c => util/virnetdevveth.c} |   95 +++++------------------------
 src/util/virnetdevveth.h                 |   35 +++++++++++
 9 files changed, 144 insertions(+), 114 deletions(-)
 delete mode 100644 src/lxc/veth.h
 rename src/{lxc/veth.c => util/virnetdevveth.c} (74%)
 create mode 100644 src/util/virnetdevveth.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 2faf659..6ffa8c6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -94,6 +94,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
 
@@ -318,14 +319,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 1617117..7163ad8 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -56,7 +56,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 d18055a..d863f38 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 77ac605..ccb0bbf 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 4a66098..0000000
--- 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 <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 */
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index c9fb6a1..5311ae7 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 5324b66..de98553 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 b31ce33..c77e558 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 <dlesko at linux.vnet.ibm.com>
+ *     David L. Leskovec <dlesko at linux.vnet.ibm.com>
+ *     Daniel P. Berrange <berrange at redhat.com>
  */
 
 #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 "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 0000000..0d450f1
--- /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 <dlesko at linux.vnet.ibm.com>
+ *     Daniel P. Berrange <berrange at 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__ */
-- 
1.7.6.4




More information about the libvir-list mailing list