[libvirt] [PATCH] qemu: change the name of tap device for a tap and bridge network

ZhiPeng Lu lu.zhipeng at zte.com.cn
Fri Apr 28 09:23:19 UTC 2017


Creating tap device and adding the device to bridge are not atomic operation.
Similarly deleting tap device and removing it from bridge are not atomic operation.
The Problem occurs when two vms start and shutdown. When one vm with the nic
named "vnet0" stopping, it deleted tap device but not removing port from bridge.
At this time, another vm created the tap device named "vnet0" and added port to the
same bridge. Then, the first vm deleted the tap device from the same bridge.
Finally, the tap device of the second vm don't attached to the bridge.
So, we can add domid to vm's nic name. For example, the vm's domid is 1 and vnet0
is renamed to vnet1.0.

Signed-off-by: ZhiPeng Lu <lu.zhipeng at zte.com.cn>
---
 src/qemu/qemu_interface.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c
index d0850c0..17d40a7 100644
--- a/src/qemu/qemu_interface.c
+++ b/src/qemu/qemu_interface.c
@@ -512,6 +512,7 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def,
     bool template_ifname = false;
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
     const char *tunpath = "/dev/net/tun";
+    char *domIdPlusIndex = NULL;
 
     if (net->backend.tap) {
         tunpath = net->backend.tap;
@@ -531,8 +532,13 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def,
         STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
         strchr(net->ifname, '%')) {
         VIR_FREE(net->ifname);
-        if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0)
+        if (virAsprintf(&domIdPlusIndex, "%s%d.%s",
+           VIR_NET_GENERATED_PREFIX, def->id, "%d") < 0) {
+            goto cleanup;
+        }
+        if (VIR_STRDUP(net->ifname, domIdPlusIndex) < 0) {
             goto cleanup;
+        }
         /* avoid exposing vnet%d in getXMLDesc or error outputs */
         template_ifname = true;
     }
@@ -594,6 +600,7 @@ qemuInterfaceBridgeConnect(virDomainDefPtr def,
     ret = 0;
 
  cleanup:
+    VIR_FREE(domIdPlusIndex);
     if (ret < 0) {
         size_t i;
         for (i = 0; i < *tapfdSize && tapfd[i] >= 0; i++)
-- 
1.8.3.1





More information about the libvir-list mailing list