[libvirt] [PATCH 14/33] Rename the LXC veth management APIs and delete duplicated APIs

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


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

The src/lxc/veth.c file contains APIs for managing veth devices,
but some of the APIs duplicate stuff from src/util/virnetdev.h.
Delete thed duplicate APIs and rename the remaining ones to
follow virNetDevVethXXXX

* src/lxc/veth.c, src/lxc/veth.h: Rename APIs & delete duplicates
* src/lxc/lxc_container.c, src/lxc/lxc_controller.c,
  src/lxc/lxc_driver.c: Update for API renaming
---
 po/POTFILES.in           |    1 -
 src/lxc/lxc_container.c  |    7 ++-
 src/lxc/lxc_controller.c |    4 +-
 src/lxc/lxc_driver.c     |   19 +++-----
 src/lxc/veth.c           |  109 ++++++++-------------------------------------
 src/lxc/veth.h           |   20 +++-----
 6 files changed, 42 insertions(+), 118 deletions(-)

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 1665d2d..810cf68 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -42,7 +42,6 @@ src/lxc/lxc_container.c
 src/lxc/lxc_conf.c
 src/lxc/lxc_controller.c
 src/lxc/lxc_driver.c
-src/lxc/veth.c
 src/libxl/libxl_driver.c
 src/libxl/libxl_conf.c
 src/network/bridge_driver.c
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index c4e5f28..1617117 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -60,6 +60,7 @@
 #include "uuid.h"
 #include "virfile.h"
 #include "command.h"
+#include "virnetdev.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -269,12 +270,12 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths,
         }
 
         VIR_DEBUG("Renaming %s to %s", veths[i], newname);
-        rc = setInterfaceName(veths[i], newname);
+        rc = virNetDevSetName(veths[i], newname);
         if (rc < 0)
             goto error_out;
 
         VIR_DEBUG("Enabling %s", newname);
-        rc = vethInterfaceUpOrDown(newname, 1);
+        rc = virNetDevSetOnline(newname, true);
         if (rc < 0)
             goto error_out;
 
@@ -283,7 +284,7 @@ static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths,
 
     /* enable lo device only if there were other net devices */
     if (veths)
-        rc = vethInterfaceUpOrDown("lo", 1);
+        rc = virNetDevSetOnline("lo", true);
 
 error_out:
     VIR_FREE(newname);
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 649ac87..d18055a 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -932,7 +932,7 @@ static int lxcControllerMoveInterfaces(unsigned int nveths,
 {
     unsigned int i;
     for (i = 0 ; i < nveths ; i++)
-        if (moveInterfaceToNetNs(veths[i], container) < 0)
+        if (virNetDevSetNamespace(veths[i], container) < 0)
             return -1;
 
     return 0;
@@ -953,7 +953,7 @@ static int lxcControllerCleanupInterfaces(unsigned int nveths,
 {
     unsigned int i;
     for (i = 0 ; i < nveths ; i++)
-        vethDelete(veths[i]);
+        ignore_value(virNetDevVethDelete(veths[i]));
 
     return 0;
 }
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 2c1154f..77ac605 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -55,6 +55,7 @@
 #include "domain_audit.h"
 #include "domain_nwfilter.h"
 #include "network/bridge_driver.h"
+#include "virnetdev.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -1152,8 +1153,8 @@ static void lxcVmCleanup(lxc_driver_t *driver,
     priv->monitorWatch = -1;
 
     for (i = 0 ; i < vm->def->nnets ; i++) {
-        vethInterfaceUpOrDown(vm->def->nets[i]->ifname, 0);
-        vethDelete(vm->def->nets[i]->ifname);
+        ignore_value(virNetDevSetOnline(vm->def->nets[i]->ifname, false));
+        ignore_value(virNetDevVethDelete(vm->def->nets[i]->ifname));
 
         networkReleaseActualDevice(vm->def->nets[i]);
     }
@@ -1246,7 +1247,7 @@ static int lxcSetupInterfaces(virConnectPtr conn,
 
         VIR_DEBUG("calling vethCreate()");
         parentVeth = def->nets[i]->ifname;
-        if (vethCreate(&parentVeth, &containerVeth) < 0)
+        if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0)
             goto error_exit;
         VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
 
@@ -1262,17 +1263,13 @@ static int lxcSetupInterfaces(virConnectPtr conn,
         (*veths)[(*nveths)] = containerVeth;
         (*nveths)++;
 
-        {
-            char macaddr[VIR_MAC_STRING_BUFLEN];
-            virFormatMacAddr(def->nets[i]->mac, macaddr);
-            if (setMacAddr(containerVeth, macaddr) < 0)
-                goto error_exit;
-        }
+        if (virNetDevSetMAC(containerVeth, def->nets[i]->mac) < 0)
+            goto error_exit;
 
         if (virNetDevBridgeAddPort(bridge, parentVeth) < 0)
             goto error_exit;
 
-        if (vethInterfaceUpOrDown(parentVeth, 1) < 0)
+        if (virNetDevSetOnline(parentVeth, true) < 0)
             goto error_exit;
 
         if (virNetDevBandwidthSet(def->nets[i]->ifname,
@@ -1829,7 +1826,7 @@ cleanup:
     }
     for (i = 0 ; i < nveths ; i++) {
         if (rc != 0)
-            vethDelete(veths[i]);
+            ignore_value(virNetDevVethDelete(veths[i]));
         VIR_FREE(veths[i]);
     }
     if (rc != 0) {
diff --git a/src/lxc/veth.c b/src/lxc/veth.c
index e4db65c..b31ce33 100644
--- a/src/lxc/veth.c
+++ b/src/lxc/veth.c
@@ -28,15 +28,15 @@
 #include "virterror_internal.h"
 #include "virfile.h"
 
-#define VIR_FROM_THIS VIR_FROM_LXC
+#define VIR_FROM_THIS VIR_FROM_NONE
 
-#define vethError(code, ...)                                  \
-    virReportErrorHelper(VIR_FROM_LXC, code, __FILE__,        \
+#define virNetDevvError(code, ...)                                  \
+    virReportErrorHelper(VIR_FROM_NONE, code, __FILE__,             \
                          __FUNCTION__, __LINE__, __VA_ARGS__)
 
 /* Functions */
 /**
- * getFreeVethName:
+ * virNetDevVethGetFreeName:
  * @veth: pointer to store returned name for veth device
  * @startDev: device number to start at (x in vethx)
  *
@@ -45,7 +45,7 @@
  *
  * Returns non-negative device number on success or -1 in case of error
  */
-static int getFreeVethName(char **veth, int startDev)
+static int virNetDevVethGetFreeName(char **veth, int startDev)
 {
     int devNum = startDev-1;
     char *path = NULL;
@@ -71,7 +71,7 @@ static int getFreeVethName(char **veth, int startDev)
 }
 
 /**
- * vethCreate:
+ * virNetDevVethCreate:
  * @veth1: pointer to name for parent end of veth pair
  * @veth2: pointer to return name for container end of veth pair
  *
@@ -95,7 +95,7 @@ static int getFreeVethName(char **veth, int startDev)
  *
  * Returns 0 on success or -1 in case of error
  */
-int vethCreate(char** veth1, char** veth2)
+int virNetDevVethCreate(char** veth1, char** veth2)
 {
     int rc = -1;
     const char *argv[] = {
@@ -108,7 +108,7 @@ int vethCreate(char** veth1, char** veth2)
     VIR_DEBUG("Host: %s guest: %s", NULLSTR(*veth1), NULLSTR(*veth2));
 
     if (*veth1 == NULL) {
-        if ((vethDev = getFreeVethName(veth1, vethDev)) < 0)
+        if ((vethDev = virNetDevVethGetFreeName(veth1, vethDev)) < 0)
             goto cleanup;
         VIR_DEBUG("Assigned host: %s", *veth1);
         veth1_alloc = true;
@@ -117,7 +117,7 @@ int vethCreate(char** veth1, char** veth2)
     argv[3] = *veth1;
 
     while (*veth2 == NULL) {
-        if ((vethDev = getFreeVethName(veth2, vethDev)) < 0) {
+        if ((vethDev = virNetDevVethGetFreeName(veth2, vethDev)) < 0) {
             if (veth1_alloc)
                 VIR_FREE(*veth1);
             goto cleanup;
@@ -151,7 +151,7 @@ cleanup:
 }
 
 /**
- * vethDelete:
+ * virNetDevVethDelete:
  * @veth: name for one end of veth pair
  *
  * This will delete both veth devices in a pair.  Only one end needs to
@@ -161,7 +161,7 @@ cleanup:
  *
  * Returns 0 on success or -1 in case of error
  */
-int vethDelete(const char *veth)
+int virNetDevVethDelete(const char *veth)
 {
     int rc;
     const char *argv[] = {"ip", "link", "del", veth, NULL};
@@ -185,59 +185,10 @@ int vethDelete(const char *veth)
     return rc;
 }
 
-/**
- * vethInterfaceUpOrDown:
- * @veth: name of veth device
- * @upOrDown: 0 => down, 1 => up
- *
- * Enables a veth device using SIOCSIFFLAGS
- *
- * Returns 0 on success, -1 on failure, with errno set
- */
-int vethInterfaceUpOrDown(const char* veth, int upOrDown)
-{
-    struct ifreq ifr;
-    int fd, ret;
-
-    if ((fd = socket(PF_PACKET, SOCK_DGRAM, 0)) == -1)
-        return(-1);
-
-    memset(&ifr, 0, sizeof(struct ifreq));
-
-    if (virStrcpyStatic(ifr.ifr_name, veth) == NULL) {
-        errno = EINVAL;
-        return -1;
-    }
-
-    if ((ret = ioctl(fd, SIOCGIFFLAGS, &ifr)) == 0) {
-        if (upOrDown)
-            ifr.ifr_flags |= IFF_UP;
-        else
-            ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
-
-        ret = ioctl(fd, SIOCSIFFLAGS, &ifr);
-    }
-
-    VIR_FORCE_CLOSE(fd);
-    if (ret == -1)
-        if (upOrDown == 0)
-            /*
-             * Prevent overwriting an error log which may be set
-             * where an actual failure occurs.
-             */
-            VIR_DEBUG("Failed to disable '%s'", veth);
-        else
-            vethError(VIR_ERR_INTERNAL_ERROR,
-                      _("Failed to enable '%s'"), veth);
-    else
-        ret = 0;
-
-    return(ret);
-}
 
 /**
- * moveInterfaceToNetNs:
- * @iface: name of device
+ * 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
@@ -246,12 +197,12 @@ int vethInterfaceUpOrDown(const char* veth, int upOrDown)
  *
  * Returns 0 on success or -1 in case of error
  */
-int moveInterfaceToNetNs(const char* iface, int pidInNs)
+int virNetDevSetNamespace(const char* ifname, int pidInNs)
 {
     int rc;
     char *pid = NULL;
     const char *argv[] = {
-        "ip", "link", "set", iface, "netns", NULL, NULL
+        "ip", "link", "set", ifname, "netns", NULL, NULL
     };
 
     if (virAsprintf(&pid, "%d", pidInNs) == -1) {
@@ -267,42 +218,22 @@ int moveInterfaceToNetNs(const char* iface, int pidInNs)
 }
 
 /**
- * setMacAddr
- * @iface: name of device
- * @macaddr: MAC address to be assigned
- *
- * Changes the MAC address of the given device with the
- * given address using this command:
- *     ip link set @iface address @macaddr
- *
- * Returns 0 on success or -1 in case of error
- */
-int setMacAddr(const char* iface, const char* macaddr)
-{
-    const char *argv[] = {
-        "ip", "link", "set", iface, "address", macaddr, NULL
-    };
-
-    return virRun(argv, NULL);
-}
-
-/**
- * setInterfaceName
- * @iface: name of device
- * @new: new name of @iface
+ * 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 setInterfaceName(const char* iface, const char* new)
+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, iface) == NULL) {
+    if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL) {
         errno = EINVAL;
         return -1;
     }
diff --git a/src/lxc/veth.h b/src/lxc/veth.h
index f50a939..4a66098 100644
--- a/src/lxc/veth.h
+++ b/src/lxc/veth.h
@@ -17,17 +17,13 @@
 # include "internal.h"
 
 /* Function declarations */
-int vethCreate(char** veth1, char** veth2)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
-int vethDelete(const char* veth)
-    ATTRIBUTE_NONNULL(1);
-int vethInterfaceUpOrDown(const char* veth, int upOrDown)
-    ATTRIBUTE_NONNULL(1);
-int moveInterfaceToNetNs(const char *iface, int pidInNs)
-    ATTRIBUTE_NONNULL(1);
-int setMacAddr(const char* iface, const char* macaddr)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
-int setInterfaceName(const char* iface, const char* new)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+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 */
-- 
1.7.6.4




More information about the libvir-list mailing list