[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] lxc: fix veth off by 1 error

When not specifying a target for veth device, veth.c:getFreeVethName()
is supposed to scan for unused veth devices in /sys/class/net.
However, when it finds one, it bumps the index by one before
returning it.

So, if you have one container running, veth0 is passed into
the container, veth1 is taken and still sitting in /sys/class/net.
When you now start a second container, getFreeVethName() finds
veth0 is unused, but returns 1.  Now container creation dies
becuase /sys/class/net/veth1 exists.

Signed-off-by: Serge Hallyn <serue us ibm com>
 src/veth.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/veth.c b/src/veth.c
index 93173e4..90c1dcb 100644
--- a/src/veth.c
+++ b/src/veth.c
@@ -35,12 +35,12 @@
 static int getFreeVethName(char *veth, int maxLen, int startDev)
     int rc = -1;
-    int devNum = startDev;
+    int devNum = startDev-1;
     char path[PATH_MAX];
     do {
-        snprintf(path, PATH_MAX, "/sys/class/net/veth%d/", devNum);
+        snprintf(path, PATH_MAX, "/sys/class/net/veth%d/", devNum);
     } while (virFileExists(path));
     snprintf(veth, maxLen, "veth%d", devNum);
@@ -97,6 +97,7 @@ int vethCreate(char* veth1, int veth1MaxLen,
     while ((1 > strlen(veth2)) || STREQ(veth1, veth2)) {
         vethDev = getFreeVethName(veth2, veth2MaxLen, vethDev);
+        ++vethDev;
         DEBUG("assigned veth2: %s", veth2);

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]