Skip to contentSkip to conversation

libxl: Auto-assign NIC devids in initiate_domain_create

PATCH Applied Stefan Bader 3 participants 5 messages Jan 9, 2014 – Mar 14, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: Auto-assign NIC devids in initiate_domain_create

This appeared to be working on a quick test with a caller leaving all devids unset when starting an HVM guest and one that sets them all. Possible optimazations (maybe nice to have but probably not important):
1. something more complicated to find gaps in devids
2. limit auto-assignment in initiate_domain_create to HVM domains

-Stefan

Quoted 1 line

From bafc8f62ee3e3175ec4d978bceba4b5f891a597d Mon Sep 17 00:00:00 2001

From: Stefan Bader <[email protected]>
Date: Wed, 8 Jan 2014 18:26:59 +0100
Subject: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create

This will change initiate_domain_create to walk through NIC definitions and automatically assign devids to those which have not assigned one. The devids are needed later in domcreate_launch_dm (for HVM domains using emulated NICs). The command string for starting the device-model has those ids as part of its arguments.
Assignment of devids in the hotplug case is handled by libxl_device_nic_add but that would be called too late in the startup case.
I also moved the call to libxl__device_nic_setdefault here as this seems to be the only path leading there and avoids doing the loop a third time. The two loops are trying to handle a case where the caller sets some devids (not sure that should be valid) but leaves some unset.

---
 tools/libxl/libxl_create.c |   35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
tools/libxl/libxl_create.c +24 -11
index e03bb55..543e0c8 100644
@@ -706,6 +706,7 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_ctx *ctx = libxl__gc_owner(gc);
     uint32_t domid;
     int i, ret;
+    size_t last_devid = -1;
 
     /* convenience aliases */
     libxl_domain_config *const d_config = dcs->guest_config;
@@ -746,6 +747,29 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_device_disk *bootdisk =
         d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
 
+    /*
+     * The devid has to be set before launching the device model. For the
+     * hotplug case this is done in libxl_device_nic_add but on domain
+     * creation this is called too late.
+     * Make two runs over configured NICs in order to avoid duplicate IDs
+     * in case the caller partially assigned IDs.
+     */
+    for (i = 0; i < d_config->num_nics; i++) {
+        /* We have to init the nic here, because we still haven't
+         * called libxl_device_nic_add when domcreate_launch_dm gets called,
+         * but qemu needs the nic information to be complete.
+         */
+        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid);
+        if (ret) goto error_out;
+
+        if (d_config->nics[i].devid > last_devid)
+            last_devid = d_config->nics[i].devid;
+    }
+    for (i = 0; i < d_config->num_nics; i++) {
+        if (d_config->nics[i].devid < 0)
+            d_config->nics[i].devid = ++last_devid;
+    }
+
     if (restore_fd >= 0) {
         LOG(DEBUG, "restoring, not running bootloader\n");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
@@ -1058,17 +1082,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         }
     }
 
-
-
-    for (i = 0; i < d_config->num_nics; i++) {
-        /* We have to init the nic here, because we still haven't
-         * called libxl_device_nic_add at this point, but qemu needs
-         * the nic information to be complete.
-         */
-        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid);
-        if (ret)
-            goto error_out;
-    }
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
     {
Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 26 lines · Stefan Bader

This appeared to be working on a quick test with a caller leaving all devids unset when starting an HVM guest and one that sets them all. Possible optimazations (maybe nice to have but probably not important):
1. something more complicated to find gaps in devids
2. limit auto-assignment in initiate_domain_create to HVM domains

-Stefan

From bafc8f62ee3e3175ec4d978bceba4b5f891a597d Mon Sep 17 00:00:00 2001
From: Stefan Bader <[email protected]>
Date: Wed, 8 Jan 2014 18:26:59 +0100
Subject: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create

This will change initiate_domain_create to walk through NIC definitions and automatically assign devids to those which have not assigned one. The devids are needed later in domcreate_launch_dm (for HVM domains using emulated NICs). The command string for starting the device-model has those ids as part of its arguments.
Assignment of devids in the hotplug case is handled by libxl_device_nic_add but that would be called too late in the startup case.
I also moved the call to libxl__device_nic_setdefault here as this seems to be the only path leading there and avoids doing the loop a third time. The two loops are trying to handle a case where the caller sets some devids (not sure that should be valid) but leaves some unset.

Quoted 1 line

I think from a release point of view we should take this since it is a bug fix to the API which at least libvirt has tripped over (although libvirt has worked around it, others may not have done so).

Ian J: Does that make sense?

Quoted 64 lines
---
 tools/libxl/libxl_create.c |   35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
tools/libxl/libxl_create.c +24 -11
index e03bb55..543e0c8 100644
@@ -706,6 +706,7 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_ctx *ctx = libxl__gc_owner(gc);
     uint32_t domid;
     int i, ret;
+    size_t last_devid = -1;
 
     /* convenience aliases */
     libxl_domain_config *const d_config = dcs->guest_config;
@@ -746,6 +747,29 @@ static void initiate_domain_create(libxl__egc *egc,
     libxl_device_disk *bootdisk =
         d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
 
+    /*
+     * The devid has to be set before launching the device model. For the
+     * hotplug case this is done in libxl_device_nic_add but on domain
+     * creation this is called too late.
+     * Make two runs over configured NICs in order to avoid duplicate IDs
+     * in case the caller partially assigned IDs.
+     */
+    for (i = 0; i < d_config->num_nics; i++) {
+        /* We have to init the nic here, because we still haven't
+         * called libxl_device_nic_add when domcreate_launch_dm gets called,
+         * but qemu needs the nic information to be complete.
+         */
+        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid);
+        if (ret) goto error_out;
+
+        if (d_config->nics[i].devid > last_devid)
+            last_devid = d_config->nics[i].devid;
+    }
+    for (i = 0; i < d_config->num_nics; i++) {
+        if (d_config->nics[i].devid < 0)
+            d_config->nics[i].devid = ++last_devid;
+    }
+
     if (restore_fd >= 0) {
         LOG(DEBUG, "restoring, not running bootloader\n");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
@@ -1058,17 +1082,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         }
     }
 
-
-
-    for (i = 0; i < d_config->num_nics; i++) {
-        /* We have to init the nic here, because we still haven't
-         * called libxl_device_nic_add at this point, but qemu needs
-         * the nic information to be complete.
-         */
-        ret = libxl__device_nic_setdefault(gc, &d_config->nics[i], domid);
-        if (ret)
-            goto error_out;
-    }
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
     {
Acked-by
#2 lore raw

Ian Campbell writes ("Re: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create"):

Quoted 6 lines · Stefan Bader
Quoted 5 lines

From bafc8f62ee3e3175ec4d978bceba4b5f891a597d Mon Sep 17 00:00:00 2001
From: Stefan Bader <[email protected]>
Date: Wed, 8 Jan 2014 18:26:59 +0100
Subject: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create

Quoted 11 lines
Quoted 11 lines

This will change initiate_domain_create to walk through NIC definitions and automatically assign devids to those which have not assigned one. The devids are needed later in domcreate_launch_dm (for HVM domains using emulated NICs). The command string for starting the device-model has those ids as part of its arguments.
Assignment of devids in the hotplug case is handled by libxl_device_nic_add but that would be called too late in the startup case.
I also moved the call to libxl__device_nic_setdefault here as this seems to be the only path leading there and avoids doing the loop a third time. The two loops are trying to handle a case where the caller sets some devids (not sure that should be valid) but leaves some unset.

Thanks. Thanks also for the careful and comprehensive explanation.

Quoted 3 lines
Quoted 1 line
Quoted 5 lines

I think from a release point of view we should take this since it is a bug fix to the API which at least libvirt has tripped over (although libvirt has worked around it, others may not have done so).

Ian J: Does that make sense?

I agree.

Thanks,
Ian.

Applied
#3 lore raw
Quoted 27 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create"):

Quoted 6 lines · Stefan Bader
Quoted 5 lines

From bafc8f62ee3e3175ec4d978bceba4b5f891a597d Mon Sep 17 00:00:00 2001
From: Stefan Bader <[email protected]>
Date: Wed, 8 Jan 2014 18:26:59 +0100
Subject: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create

Quoted 11 lines
Quoted 11 lines

This will change initiate_domain_create to walk through NIC definitions and automatically assign devids to those which have not assigned one. The devids are needed later in domcreate_launch_dm (for HVM domains using emulated NICs). The command string for starting the device-model has those ids as part of its arguments.
Assignment of devids in the hotplug case is handled by libxl_device_nic_add but that would be called too late in the startup case.
I also moved the call to libxl__device_nic_setdefault here as this seems to be the only path leading there and avoids doing the loop a third time. The two loops are trying to handle a case where the caller sets some devids (not sure that should be valid) but leaves some unset.

Thanks. Thanks also for the careful and comprehensive explanation.

Quoted 3 lines
Quoted 1 line
Quoted 7 lines
Quoted 5 lines

I think from a release point of view we should take this since it is a bug fix to the API which at least libvirt has tripped over (although libvirt has worked around it, others may not have done so).

Ian J: Does that make sense?

I agree.

Applied, thanks.

Ian.

#4 lore raw
In reply to Ian Campbell

Ian Campbell writes ("Re: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create"):

Quoted 16 lines · Stefan Bader
Quoted 15 lines

Date: Wed, 8 Jan 2014 18:26:59 +0100
Subject: [PATCH] libxl: Auto-assign NIC devids in initiate_domain_create

This will change initiate_domain_create to walk through NIC definitions and automatically assign devids to those which have not assigned one. The devids are needed later in domcreate_launch_dm (for HVM domains using emulated NICs). The command string for starting the device-model has those ids as part of its arguments.
Assignment of devids in the hotplug case is handled by libxl_device_nic_add but that would be called too late in the startup case.
I also moved the call to libxl__device_nic_setdefault here as this seems to be the only path leading there and avoids doing the loop a third time. The two loops are trying to handle a case where the caller sets some devids (not sure that should be valid) but leaves some unset.

Quoted 3 lines
Quoted 1 line

I have backported this to 4.3. It doesn't apply cleanly to 4.2; I didn't investigate that further.

Thanks,
Ian.