xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Roger Pau Monne <roger.pau@citrix.com>
To: xen-devel@lists.xen.org
Cc: Ian Jackson <ian.jackson@eu.citrix.com>,
	Roger Pau Monne <roger.pau@citrix.com>
Subject: [PATCH v10 10/19] libxl: convert libxl_device_nic_add to an async operation
Date: Fri, 20 Jul 2012 15:12:19 +0100	[thread overview]
Message-ID: <1342793548-10239-11-git-send-email-roger.pau@citrix.com> (raw)
In-Reply-To: <1342793548-10239-1-git-send-email-roger.pau@citrix.com>

This patch converts libxl_device_nic_add to an ao operation that
waits for device backend to reach state XenbusStateInitWait and then
marks the operation as completed. This is not really useful now, but
will be used by latter patches that will launch hotplug scripts after
we reached the desired xenbus state.

Calls to libxl_device_nic_add have also been moved to occur after the
device model has been launched, so when hotplug scripts are called
from this functions the interfaces already exists.

As usual, libxl_device_nic_add callers have been modified, and the
internal function libxl__device_disk_add has been used if the call was
inside an already running ao.

Changes since v9:

 * Fixed Ocaml bindings.

Changes since v5:

 * Updated to use the new ao_devices struct.

 * Renamed ao_device in nic_add to aodev.

Changes since v4:

 * Used the macro defined in previous patch to define the generic
   callback to wait for nics to be connected.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c                  |   29 ++++++++++++++-----
 tools/libxl/libxl.h                  |    3 +-
 tools/libxl/libxl_create.c           |   51 ++++++++++++++++++++++++++++-----
 tools/libxl/libxl_device.c           |    2 +
 tools/libxl/libxl_dm.c               |   38 +++++++++++++++++++++++--
 tools/libxl/libxl_internal.h         |    9 ++++++
 tools/libxl/xl_cmdimpl.c             |    2 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |    2 +-
 8 files changed, 114 insertions(+), 22 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index d97add5..d27ac20 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2424,12 +2424,13 @@ static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
-int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
+void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_nic *nic, libxl__ao_device *aodev)
 {
-    GC_INIT(ctx);
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front;
     flexarray_t *back;
-    libxl__device device;
+    libxl__device *device;
     char *dompath, **l;
     unsigned int nb, rc;
 
@@ -2460,7 +2461,8 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
         }
     }
 
-    rc = libxl__device_from_nic(gc, domid, nic, &device);
+    GCNEW(device);
+    rc = libxl__device_from_nic(gc, domid, nic, device);
     if ( rc != 0 ) goto out_free;
 
     flexarray_append(back, "frontend-id");
@@ -2501,6 +2503,9 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
     flexarray_append(back, libxl__strdup(gc, nic->bridge));
     flexarray_append(back, "handle");
     flexarray_append(back, libxl__sprintf(gc, "%d", nic->devid));
+    flexarray_append(back, "type");
+    flexarray_append(back, libxl__strdup(gc,
+                                     libxl_nic_type_to_string(nic->nictype)));
 
     flexarray_append(front, "backend-id");
     flexarray_append(front, libxl__sprintf(gc, "%d", nic->backend_domid));
@@ -2511,18 +2516,22 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
     flexarray_append(front, "mac");
     flexarray_append(front, libxl__sprintf(gc,
                                     LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
-    libxl__device_generic_add(gc, XBT_NULL, &device,
+    libxl__device_generic_add(gc, XBT_NULL, device,
                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
                              libxl__xs_kvs_of_flexarray(gc, front, front->count));
 
-    /* FIXME: wait for plug */
+    aodev->dev = device;
+    aodev->action = DEVICE_CONNECT;
+    libxl__wait_device_connection(egc, aodev);
+
     rc = 0;
 out_free:
     flexarray_free(back);
     flexarray_free(front);
 out:
-    GC_FREE;
-    return rc;
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
 }
 
 static void libxl__device_nic_from_xs_be(libxl__gc *gc,
@@ -3014,6 +3023,7 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 /* Macro for defining device addition functions in a compact way */
 /* The following functions are defined:
  * libxl_device_disk_add
+ * libxl_device_nic_add
  */
 
 #define DEFINE_DEVICE_ADD(type)                                         \
@@ -3037,6 +3047,9 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 /* disk */
 DEFINE_DEVICE_ADD(disk)
 
+/* nic */
+DEFINE_DEVICE_ADD(nic)
+
 #undef DEFINE_DEVICE_ADD
 
 /******************************************************************************/
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index f3161d5..d548090 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -696,7 +696,8 @@ int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
 
 /* Network Interfaces */
-int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
+int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic,
+                         const libxl_asyncop_how *ao_how);
 int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_nic *nic,
                             const libxl_asyncop_how *ao_how);
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 181eb53..36f7366 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -562,6 +562,9 @@ static void domcreate_bootloader_done(libxl__egc *egc,
 static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
                                 int ret);
 
+static void domcreate_attach_pci(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                 int ret);
+
 static void domcreate_console_available(libxl__egc *egc,
                                         libxl__domain_create_state *dcs);
 
@@ -893,13 +896,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
         goto error_out;
     }
     for (i = 0; i < d_config->num_nics; i++) {
-        ret = libxl_device_nic_add(ctx, domid, &d_config->nics[i]);
-        if (ret) {
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
-                       "cannot add nic %d to domain: %d", i, ret);
-            ret = ERROR_FAIL;
-            goto error_out;
-        }
+        /* 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.
+         */
+        libxl__device_nic_setdefault(gc, &d_config->nics[i]);
     }
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
@@ -972,7 +973,6 @@ static void domcreate_devmodel_started(libxl__egc *egc,
 {
     libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.dm);
     STATE_AO_GC(dmss->spawn.ao);
-    int i;
     libxl_ctx *ctx = CTX;
     int domid = dcs->guest_domid;
 
@@ -992,6 +992,41 @@ static void domcreate_devmodel_started(libxl__egc *egc,
         }
     }
 
+    /* Plug nic interfaces */
+    if (d_config->num_nics > 0) {
+        /* Attach nics */
+        dcs->aodevs.size = d_config->num_nics;
+        dcs->aodevs.callback = domcreate_attach_pci;
+        libxl__prepare_ao_devices(ao, &dcs->aodevs);
+        libxl__add_nics(egc, ao, domid, 0, d_config, &dcs->aodevs);
+        return;
+    }
+
+    domcreate_attach_pci(egc, &dcs->aodevs, 0);
+    return;
+
+error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_attach_pci(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                 int ret)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(aodevs, *dcs, aodevs);
+    STATE_AO_GC(dcs->ao);
+    int i;
+    libxl_ctx *ctx = CTX;
+    int domid = dcs->guest_domid;
+
+    /* convenience aliases */
+    libxl_domain_config *const d_config = dcs->guest_config;
+
+    if (ret) {
+        LOG(ERROR, "unable to add nic devices");
+        goto error_out;
+    }
+
     for (i = 0; i < d_config->num_pcidevs; i++)
         libxl__device_pci_add(gc, domid, &d_config->pcidevs[i], 1);
 
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index a935ced..ccbd84a 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -453,6 +453,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
  *
  * The following functions are defined:
  * libxl__add_disks
+ * libxl__add_nics
  */
 
 #define DEFINE_DEVICES_ADD(type)                                               \
@@ -471,6 +472,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
     }
 
 DEFINE_DEVICES_ADD(disk)
+DEFINE_DEVICES_ADD(nic)
 
 #undef DEFINE_DEVICES_ADD
 
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 9a5cb0a..8f5618b 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -697,6 +697,10 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
 static void spawn_stub_launch_dm(libxl__egc *egc,
                                  libxl__ao_devices *aodevs, int ret);
 
+static void stubdom_pvqemu_cb(libxl__egc *egc,
+                              libxl__ao_devices *aodevs,
+                              int rc);
+
 static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc,
                                            libxl__destroy_domid_state *dis,
                                            int rc);
@@ -864,9 +868,11 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
      }
 
     for (i = 0; i < dm_config->num_nics; i++) {
-        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->nics[i]);
-        if (ret)
-            goto out;
+         /* 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.
+         */
+        libxl__device_nic_setdefault(gc, &dm_config->nics[i]);
     }
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
@@ -943,9 +949,35 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
         CONTAINER_OF(stubdom_dmss, *sdss, pvqemu);
     STATE_AO_GC(sdss->dm.spawn.ao);
     uint32_t dm_domid = sdss->pvqemu.guest_domid;
+    libxl_domain_config *d_config = stubdom_dmss->guest_config;
 
     if (rc) goto out;
 
+    if (d_config->num_nics > 0) {
+        sdss->aodevs.size = d_config->num_nics;
+        sdss->aodevs.callback = stubdom_pvqemu_cb;
+        libxl__prepare_ao_devices(ao, &sdss->aodevs);
+        libxl__add_nics(egc, ao, dm_domid, 0, d_config, &sdss->aodevs);
+        return;
+    }
+
+out:
+    stubdom_pvqemu_cb(egc, &sdss->aodevs, rc);
+}
+
+static void stubdom_pvqemu_cb(libxl__egc *egc,
+                              libxl__ao_devices *aodevs,
+                              int rc)
+{
+    libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(aodevs, *sdss, aodevs);
+    STATE_AO_GC(sdss->dm.spawn.ao);
+    uint32_t dm_domid = sdss->pvqemu.guest_domid;
+
+    if (rc) {
+        LOGE(ERROR, "error connecting nics devices");
+        goto out;
+    }
+
     rc = libxl_domain_unpause(CTX, dm_domid);
     if (rc) goto out;
 
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index eb69721..67dfebe 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1893,6 +1893,11 @@ _hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid,
                                     libxl_device_disk *disk,
                                     libxl__ao_device *aodev);
 
+/* AO operation to connect a nic device */
+_hidden void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                                   libxl_device_nic *nic,
+                                   libxl__ao_device *aodev);
+
 /* Waits for the passed device to reach state XenbusStateInitWait.
  * This is not really useful by itself, but is important when executing
  * hotplug scripts, since we need to be sure the device is in the correct
@@ -2286,6 +2291,10 @@ _hidden void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
                               int start, libxl_domain_config *d_config,
                               libxl__ao_devices *aodevs);
 
+_hidden void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                             int start, libxl_domain_config *d_config,
+                             libxl__ao_devices *aodevs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index f27e811..183dd99 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -5244,7 +5244,7 @@ int main_networkattach(int argc, char **argv)
         return 0;
     }
 
-    if (libxl_device_nic_add(ctx, domid, &nic)) {
+    if (libxl_device_nic_add(ctx, domid, &nic, 0)) {
         fprintf(stderr, "libxl_device_nic_add failed.\n");
         return 1;
     }
diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xenlight_stubs.c
index a158351..0e9c65e 100644
--- a/tools/ocaml/libs/xl/xenlight_stubs.c
+++ b/tools/ocaml/libs/xl/xenlight_stubs.c
@@ -281,7 +281,7 @@ value stub_xl_device_nic_add(value info, value domid)
 	device_nic_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_nic_add(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_nic_add(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("nic_add", &lg);
 	FREE_CTX();
-- 
1.7.7.5 (Apple Git-26)

  parent reply	other threads:[~2012-07-20 14:12 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-20 14:12 [PATCH v10 01/19] execute hotplug scripts from libxl v10 Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 01/19] libxl: check backend state before setting it to "closing" Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 02/19] libxl: change ao_device_remove to ao_device Roger Pau Monne
2012-07-20 16:35   ` Ian Jackson
2012-07-20 14:12 ` [PATCH v10 03/19] libxl: move device model creation prototypes Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 04/19] libxl: convert libxl_domain_destroy to an async op Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 05/19] libxl: move bootloader data strucutres and prototypes Roger Pau Monne
2012-07-20 17:21   ` Ian Jackson
2012-07-20 14:12 ` [PATCH v10 06/19] libxl: refactor disk addition to take a helper Roger Pau Monne
2012-07-20 17:24   ` Ian Jackson
2012-07-20 14:12 ` [PATCH v10 07/19] libxl: convert libxl__device_disk_local_attach to an async op Roger Pau Monne
2012-07-20 18:03   ` Ian Jackson
2012-07-23 11:32     ` Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 08/19] libxl: rename vifs to nics Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 09/19] libxl: convert libxl_device_disk_add to an async op Roger Pau Monne
2012-07-20 18:04   ` Ian Jackson
2012-07-20 14:12 ` Roger Pau Monne [this message]
2012-07-20 14:12 ` [PATCH v10 11/19] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 12/19] libxl: rename _IOEMU nic type to VIF_IOEMU Roger Pau Monne
2012-07-20 16:34   ` Ian Campbell
2012-07-20 16:39     ` Ian Campbell
2012-07-20 14:12 ` [PATCH v10 13/19] libxl: set correct nic type depending on the guest Roger Pau Monne
2012-07-20 18:06   ` Ian Jackson
2012-07-20 14:12 ` [PATCH v10 14/19] libxl: use libxl__xs_path_cleanup on device_destroy Roger Pau Monne
2012-07-20 15:38   ` Ian Campbell
2012-07-20 14:12 ` [PATCH v10 15/19] libxl: call hotplug scripts for disk devices from libxl Roger Pau Monne
2012-07-20 15:20   ` Ian Campbell
2012-07-20 14:12 ` [PATCH v10 16/19] libxl: call hotplug scripts for nic " Roger Pau Monne
2012-07-20 16:01   ` Ian Campbell
2012-07-20 16:12     ` Ian Campbell
2012-07-20 16:36     ` Ian Campbell
2012-07-23  9:22     ` Roger Pau Monne
2012-07-20 16:03   ` Ian Campbell
2012-07-20 14:12 ` [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation Roger Pau Monne
2012-07-20 18:08   ` Ian Jackson
2012-07-23  8:58     ` Roger Pau Monne
2012-07-23  9:26       ` Ian Campbell
2012-07-23 11:44       ` Ian Jackson
2012-07-20 14:12 ` [PATCH v10 18/19] libxl: convert libxl_device_vfb_add " Roger Pau Monne
2012-07-20 18:11   ` Ian Jackson
2012-07-23 11:58     ` Roger Pau Monne
2012-07-20 14:12 ` [PATCH v10 19/19] xl: main_blockdetach don't call destroy if remove succeeds Roger Pau Monne
2012-07-20 14:55   ` Ian Campbell
2012-07-23 12:17 ` [PATCH v10 01/19] execute hotplug scripts from libxl v10 Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1342793548-10239-11-git-send-email-roger.pau@citrix.com \
    --to=roger.pau@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).