Index: libvirt-macvtap/src/util/macvtap.h
===================================================================
--- libvirt-macvtap.orig/src/util/macvtap.h
+++ libvirt-macvtap/src/util/macvtap.h
@@ -36,7 +36,8 @@ int openMacvtapTap(virConnectPtr conn,
                    char **res_ifname);
 
 int delMacvtapByMACAddress(const unsigned char *macaddress,
-                           int *hasBusyDev);
+                           const char *linkdev,
+                           int checkTapInUse);
 
 #endif /* WITH_MACVTAP */
 
Index: libvirt-macvtap/src/util/macvtap.c
===================================================================
--- libvirt-macvtap.orig/src/util/macvtap.c
+++ libvirt-macvtap/src/util/macvtap.c
@@ -633,7 +633,8 @@ macvtapModeFromInt(enum virDomainNetdevM
 }
 
 
-/* openMacvtapTap:
+/**
+ * openMacvtapTap:
  * Create an instance of a macvtap device and open its tap character
  * device.
  * @conn: Pointer to virConnect object
@@ -714,7 +715,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,26 +729,99 @@ 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.
+/**
+ * isSameNetDev:
+ * @sockfd : socket filedescriptor to use for the interface ioctl
+ * @ifname : the name of the interface
+ * @macaddress : the MAC address the interface is supposed to have
+ * @linkdevidx : The index of the link device
+ *
+ * Returns 1 if the interface has the given MAC address and link
+ * device interface index, 0 if this is not the case. Returns a negative
+ * errno value in case of error.
+ *
+ * Determine whether a given network interface, described by its name,
+ * has the same MAC address and link device index as the one given.
+ */
+static int
+isSameNetDev(int sockfd,
+             const char *ifname,
+             const unsigned char *macaddress, int linkdevidx)
+{
+    struct ifreq ifr;
+    char path[64];
+    int ifindex;
+    FILE *file;
+
+    if (virStrncpy(ifr.ifr_name, ifname, strlen(ifname),
+                   sizeof(ifr.ifr_name)) == NULL)
+        return -EINVAL;
+
+    if (ioctl(sockfd, SIOCGIFHWADDR, (char *)&ifr) >= 0) {
+        if (memcmp(&ifr.ifr_hwaddr.sa_data[0], macaddress, 6) != 0)
+            return 0;
+    } else
+        return -errno;
+
+    if (snprintf(path, sizeof(path),
+                 "/sys/class/net/%s/iflink", ifname) >= sizeof(path)) {
+        virReportSystemError(EINVAL,
+                             "%s",
+                             _("buffer for iflink path is too small"));
+        return -EINVAL;
+    }
+
+    file = fopen(path, "r");
+
+    if (!file) {
+        virReportSystemError(errno,
+                             _("cannot open macvtap file %s to determine "
+                               "link device interface index"), path);
+        return -errno;
+    }
+
+    if (fscanf(file, "%d", &ifindex) != 1) {
+        virReportSystemError(errno,
+                             "%s",_("cannot determine macvtap's link device "
+                             "interface index"));
+        fclose(file);
+        return 0;
+    }
+
+    fclose(file);
+
+    return (linkdevidx == ifindex);
+}
+
+/**
+ * delMacvtpaByMACAddress:
  * @macaddress : Pointer to 6 bytes holding the MAC address of the
  *    macvtap device(s) to destroy
+ * @linkdev : The lower link device
+ * @checkTapInUse : Check whether the associated tap is in use.
  *
- * Returns 0 in case of success, negative value in case of error.
+ * Returns 0 in case of success, 1 in case a macvtap device with the given
+ * linkdev is busy, or -1 in case of general 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)
+                       const char *linkdev,
+                       int checkTapInUse)
 {
-    struct ifreq ifr;
+    int rc = 0;
     FILE *file;
     char *ifname, *pos;
     char buffer[1024];
     off_t oldpos = 0;
     int tapfd;
+    int linkdevidx;
 
-    *hasBusyDev = 0;
+    if (getIfIndex(NULL, linkdev, &linkdevidx) != 0)
+        return -1;
 
     file = fopen("/proc/net/dev", "r");
 
@@ -770,20 +844,20 @@ delMacvtapByMACAddress(const unsigned ch
             ifname++;
         if ((pos = strchr(ifname, ':')) != NULL) {
             pos[0] = 0;
-            if (virStrncpy(ifr.ifr_name, ifname, strlen(ifname),
-                           sizeof(ifr.ifr_name)) == NULL)
-                continue;
-            if (ioctl(sock, SIOCGIFHWADDR, (char *)&ifr) >= 0) {
-                if (memcmp(&ifr.ifr_hwaddr.sa_data[0], macaddress, 6) == 0) {
+            if (isSameNetDev(sock, ifname, macaddress, linkdevidx) > 0) {
+                int tearit = 1;
+                if (checkTapInUse) {
                     tapfd = openTap(ifname, 0);
-                    if (tapfd > 0) {
+                    if (!(tapfd >= 0)) {
+                        rc = 1;
+                        tearit = 0;
+                    } else
                         close(tapfd);
-                        ifUp(ifname, 0);
-                        if (link_del("macvtap", ifname) == 0)
-                            fseeko(file, oldpos, SEEK_SET);
-                    } else {
-                        *hasBusyDev = 1;
-                    }
+                }
+                if (tearit) {
+                    ifUp(ifname, 0);
+                    if (link_del("macvtap", ifname) == 0)
+                        fseeko(file, oldpos, SEEK_SET);
                 }
             }
         }
@@ -794,7 +868,7 @@ delMacvtapByMACAddress(const unsigned ch
 sock_err:
     fclose(file);
 
-    return 0;
+    return rc;
 }
 
 #endif
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,16 @@ 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) {
+            delMacvtapByMACAddress(net->mac, net->data.direct.linkdev, 0);
+        }
+    }
+#endif
+
 retry:
     if ((ret = qemuRemoveCgroup(driver, vm, 0)) < 0) {
         if (ret == -EBUSY && (retries++ < 5)) {
@@ -6354,8 +6353,7 @@ qemudDomainDetachNetDevice(struct qemud_
 
 #if WITH_MACVTAP
     if (detach->type == VIR_DOMAIN_NET_TYPE_DIRECT) {
-        int dummy;
-        delMacvtapByMACAddress(detach->mac, &dummy);
+        delMacvtapByMACAddress(detach->mac, detach->data.direct.linkdev, 0);
     }
 #endif
 
Index: libvirt-macvtap/src/qemu/qemu_conf.c
===================================================================
--- libvirt-macvtap.orig/src/qemu/qemu_conf.c
+++ libvirt-macvtap/src/qemu/qemu_conf.c
@@ -1439,11 +1439,13 @@ qemudPhysIfaceConnect(virConnectPtr conn
     int rc;
 #if WITH_MACVTAP
     char *res_ifname = NULL;
-    int hasBusyDev = 0;
 
-    delMacvtapByMACAddress(net->mac, &hasBusyDev);
+    rc = delMacvtapByMACAddress(net->mac, linkdev, 1);
 
-    if (hasBusyDev) {
+    if (rc < 0)
+        return -1;
+
+    if (rc != 0) {
         virReportSystemError(errno, "%s",
                              _("A macvtap with the same MAC address is in use"));
         return -1;
