Index: libvirt-macvtap/src/qemu/qemu_driver.c
===================================================================
--- libvirt-macvtap.orig/src/qemu/qemu_driver.c
+++ libvirt-macvtap/src/qemu/qemu_driver.c
@@ -2931,17 +2931,6 @@ static void qemudShutdownVMDaemon(struct
         }
     }
 
-#if WITH_MACVTAP
-    def = vm->def;
-    for (i = 0; i < def->nnets; i++) {
-        virDomainNetDefPtr net = def->nets[i];
-        if (net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
-            int dummy;
-            delMacvtapByMACAddress(net->mac, &dummy);
-        }
-    }
-#endif
-
     if (virKillProcess(vm->pid, 0) == 0 &&
         virKillProcess(vm->pid, SIGTERM) < 0)
         virReportSystemError(errno,
@@ -2988,6 +2977,17 @@ static void qemudShutdownVMDaemon(struct
 
     qemuDomainReAttachHostDevices(driver, vm->def);
 
+#if WITH_MACVTAP
+    def = vm->def;
+    for (i = 0; i < def->nnets; i++) {
+        virDomainNetDefPtr net = def->nets[i];
+        if (net->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
+            tearMacvtapByMACAddress(net->mac);
+        }
+    }
+#endif
+
+    retries = 0;
 retry:
     if ((ret = qemuRemoveCgroup(driver, vm, 0)) < 0) {
         if (ret == -EBUSY && (retries++ < 5)) {
Index: libvirt-macvtap/src/util/macvtap.h
===================================================================
--- libvirt-macvtap.orig/src/util/macvtap.h
+++ libvirt-macvtap/src/util/macvtap.h
@@ -38,6 +38,8 @@ int openMacvtapTap(virConnectPtr conn,
 int delMacvtapByMACAddress(const unsigned char *macaddress,
                            int *hasBusyDev);
 
+int tearMacvtapByMACAddress(const unsigned char *macaddress);
+
 #endif /* WITH_MACVTAP */
 
 #define MACVTAP_MODE_PRIVATE_STR  "private"
Index: libvirt-macvtap/src/util/macvtap.c
===================================================================
--- libvirt-macvtap.orig/src/util/macvtap.c
+++ libvirt-macvtap/src/util/macvtap.c
@@ -714,7 +714,7 @@ create_name:
 
     rc = openTap(cr_ifname, 10);
 
-    if (rc > 0)
+    if (rc >= 0)
         *res_ifname = strdup(cr_ifname);
     else
         goto link_del_exit;
@@ -728,17 +728,25 @@ link_del_exit:
 }
 
 
-/* Delete a macvtap type of interface given the MAC address. This
- * function will delete all macvtap type of interfaces that have the
- * given MAC address.
+/**
+ * _delMacvtapByMACAddress:
  * @macaddress : Pointer to 6 bytes holding the MAC address of the
  *    macvtap device(s) to destroy
+ * @checkTapInUse : whether to check that the tap device is in use.
+ * @hasBusyDev : If an interface was busy, it will be reported in this
+ *    variable as '1'. This requires that checkTapInUse is set to '1'.
  *
  * Returns 0 in case of success, negative value in case of error.
+ * Internal function to delete a macvtap type of interface given the MAC
+ * address. This function will delete all macvtap type of interfaces that
+ * have the given MAC address. Depending on the value of checkTapInUse, it
+ * will be tested whether a macvtap is currently in use before it is deleted.
+ *
  */
-int
-delMacvtapByMACAddress(const unsigned char *macaddress,
-                       int *hasBusyDev)
+static int
+_delMacvtapByMACAddress(const unsigned char *macaddress,
+                        int checkTapInUse,
+                        int *hasBusyDev)
 {
     struct ifreq ifr;
     FILE *file;
@@ -746,8 +754,10 @@ delMacvtapByMACAddress(const unsigned ch
     char buffer[1024];
     off_t oldpos = 0;
     int tapfd;
+    int tearit;
 
-    *hasBusyDev = 0;
+    if (checkTapInUse)
+        *hasBusyDev = 0;
 
     file = fopen("/proc/net/dev", "r");
 
@@ -775,14 +785,18 @@ delMacvtapByMACAddress(const unsigned ch
                 continue;
             if (ioctl(sock, SIOCGIFHWADDR, (char *)&ifr) >= 0) {
                 if (memcmp(&ifr.ifr_hwaddr.sa_data[0], macaddress, 6) == 0) {
-                    tapfd = openTap(ifname, 0);
-                    if (tapfd > 0) {
-                        close(tapfd);
+                    tearit = 1;
+                    if (checkTapInUse) {
+                        tapfd = openTap(ifname, 0);
+                        if (!(tapfd >= 0)) {
+                            *hasBusyDev = 1;
+                            tearit = 0;
+                        }
+                    }
+                    if (tearit) {
                         ifUp(ifname, 0);
                         if (link_del("macvtap", ifname) == 0)
                             fseeko(file, oldpos, SEEK_SET);
-                    } else {
-                        *hasBusyDev = 1;
                     }
                 }
             }
@@ -797,4 +811,43 @@ sock_err:
     return 0;
 }
 
+
+/**
+ * delMacvtapByMACAddress:
+ * @macaddress : Pointer to 6 bytes holding the MAC address of the
+ *    macvtap device(s) to destroy
+ * @hasBusyDev : If an interface was busy, it will be reported in this
+ *    variable as '1'.
+ *
+ * Returns 0 in case of success, negative value in case of error.
+ *
+ * Delete a macvtap type of interface given the MAC address. This
+ * function will delete all macvtap type of interfaces that have the
+ * given MAC address.
+ */
+int
+delMacvtapByMACAddress(const unsigned char *macaddress,
+                       int *hasBusyDev)
+{
+    return _delMacvtapByMACAddress(macaddress, 1, hasBusyDev);
+}
+
+
+/**
+ * tearMacvtapByMACAddress:
+ * @macaddress : Pointer to 6 bytes holding the MAC address of the
+ *    macvtap device(s) to destroy
+ *
+ * Returns 0 in case of success, negative value in case of error.
+ *
+ * Delete a macvtap type of interface given the MAC address. This
+ * function will delete all macvtap type of interfaces that have the
+ * given MAC address independent of whether anyone is in use or not.
+ */
+int
+tearMacvtapByMACAddress(const unsigned char *macaddress)
+{
+    return _delMacvtapByMACAddress(macaddress, 0, NULL);
+}
+
 #endif
Index: libvirt-macvtap/src/libvirt_macvtap.syms
===================================================================
--- libvirt-macvtap.orig/src/libvirt_macvtap.syms
+++ libvirt-macvtap/src/libvirt_macvtap.syms
@@ -3,3 +3,4 @@
 # macvtap.h
 openMacvtapTap;
 delMacvtapByMACAddress;
+tearMacvtapByMACAddress;
