[libvirt] [PATCH 5/9] libxl: prevent attaching multiple netdevs with the same MAC

Marek Marczykowski-Górecki marmarek at invisiblethingslab.com
Fri Mar 6 14:36:16 UTC 2015


It will not be possible to detach such device later. Also improve
logging in such cases.

Signed-off-by: Marek Marczykowski-Górecki <marmarek at invisiblethingslab.com>
---
 src/libxl/libxl_driver.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Changes in v4:
 - use virDomainHasNet instead of virDomainNetFindIdx

diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index ce3a99b..1313d2e 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -2787,6 +2787,7 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
     int actualType;
     libxl_device_nic nic;
     int ret = -1;
+    char mac[VIR_MAC_STRING_BUFLEN];
 
     /* preallocate new slot for device */
     if (VIR_REALLOC_N(vm->def->nets, vm->def->nnets + 1) < 0)
@@ -2801,6 +2802,13 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
 
     actualType = virDomainNetGetActualType(net);
 
+    if (virDomainHasNet(vm->def, net)) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                _("device matching mac address %s already exists"),
+                virMacAddrFormat(&net->mac, mac));
+        return -1;
+    }
+
     if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
         /* This is really a "smart hostdev", so it should be attached
          * as a hostdev (the hostdev code will reach over into the
@@ -2879,6 +2887,7 @@ libxlDomainAttachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
     virDomainHostdevDefPtr hostdev;
     virDomainHostdevDefPtr found;
     virDomainHostdevSubsysPCIPtr pcisrc;
+    char mac[VIR_MAC_STRING_BUFLEN];
 
     switch (dev->type) {
         case VIR_DOMAIN_DEVICE_DISK:
@@ -2896,6 +2905,12 @@ libxlDomainAttachDeviceConfig(virDomainDefPtr vmdef, virDomainDeviceDefPtr dev)
 
         case VIR_DOMAIN_DEVICE_NET:
             net = dev->data.net;
+            if (virDomainHasNet(vmdef, net)) {
+                virReportError(VIR_ERR_INVALID_ARG,
+                               _("network device with mac %s already exists."),
+                               virMacAddrFormat(&net->mac, mac));
+                return -1;
+            }
             if (virDomainNetInsert(vmdef, net))
                 return -1;
             dev->data.net = NULL;
-- 
2.1.0




More information about the libvir-list mailing list