[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); ++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); } -- 1.6.2

On Tue, Apr 07, 2009 at 04:21:39PM -0500, Serge E. Hallyn wrote:
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.
ACK. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
participants (2)
-
Daniel P. Berrange
-
Serge E. Hallyn