Skip to contentSkip to conversation

libxl: Deprivilege qemu

RFC PATCH In review Ian Jackson 6 participants 61 messages Dec 22, 2015 – Apr 28, 2016

lore
Share & tools
lore
#0 lore raw

v6 cover libxl: Deprivilege qemu

This is a new version of Stefano Stabellini's series
[PATCH v5 0/6] libxl: xs_restrict QEMU

I took Stefano's code as a spec for how to interact with qemu, and have reworked the whole series. In particular, I have
- rebased onto staging
- split up some of the larger patches
- restructured the patches so that every intervening version should work
- redone the async task handling
- provided what seem to me to be appropriate configuration options
- shaved a few yaks (although I tried to limit this!)
- fixed the cross-version compatibility
- reduced the new permission grant for the domain to only .../physmap

I have compiled this but I haven't tested it yet.

I think it would be very useful at this stage for others to read the commit messages, internal docs, and important structural elements.

Detailed code review should probably wait until after testing which in turn ought to wait unil we decide what (if any) design and interface changes are needed.

And I don't expect to get any feedback until the new year :-).

Happy holidays all.

Regards,
Ian.

#1 lore raw

PATCH v6 1/28 libxl: Move FILLZERO up in libxl_internal.h

This means that callers in libxl_internal.h can use it (eg, inline functions). No functional change.

---
v6: New patch.

---
 tools/libxl/libxl_internal.h |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/libxl/libxl_internal.h +2 -3
index a556a38..e442996 100644
@@ -121,6 +121,8 @@
 #define MB(_mb)     (_AC(_mb, ULL) << 20)
 #define GB(_gb)     (_AC(_gb, ULL) << 30)
 
+#define FILLZERO LIBXL_FILLZERO
+
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
 #define ROUNDUP(_val, _order)                                           \
@@ -3545,9 +3547,6 @@ _hidden void libxl__domain_suspend_callback(void *data);
     })
 
 
-#define FILLZERO LIBXL_FILLZERO
-
-
 /*
  * All of these assume (or define)
  *    libxl__gc *gc;
Signature · 1 line
1.7.10.4
Acked-by on v6 1/28
#2 lore raw

Re: [PATCH 01/28] libxl: Move FILLZERO up in libxl_internal.h

Quoted 4 lines · Ian Jackson

This means that callers in libxl_internal.h can use it (eg, inline functions).  No functional change.

(could go in now despite RFC-ness of the series as a whole)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

PATCH v6 2/28 libxl: libxl_types_internal.idl: Add Emacs mode comment

In reply to Ian Jackson

---
v6: New patch.

---
 tools/libxl/libxl_types_internal.idl |    2 ++
 1 file changed, 2 insertions(+)
tools/libxl/libxl_types_internal.idl +2 -0
index 5e55685..4397141 100644
@@ -1,3 +1,5 @@
+# -*- python -*-
+
 namespace("libxl__")
 hidden(True)
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v6 3/28 libxl: Provide libxl__dm_support_*

In reply to Ian Jackson

This allows code elsewhere in libxl to find out what options a device model executable supports. This is done by searching the usage message for fixed strings.

Because this involves calling fork, callers must use the async machinery. To make this easier, and to avoid repeatedly invoking the dm, we provide a cache within the libxl__ctx.

No call site yet.

---
v6: Completely rewritten.
Cache is now in ctx only, not in xenstore. We use the proper libxl__ev_child machinery for fork.

---
 tools/libxl/libxl.c          |    4 +
 tools/libxl/libxl_dm.c       |  231 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |   76 ++++++++++++++
 3 files changed, 311 insertions(+)
tools/libxl/libxl.c +4 -0
index 9207621..d96189d 100644
@@ -87,6 +87,8 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,
     ctx->sigchld_selfpipe[1] = -1;
     libxl__ev_fd_init(&ctx->sigchld_selfpipe_efd);
 
+    LIBXL_LIST_INIT(&ctx->dm_support_cache);
+
     /* The mutex is special because we can't idempotently destroy it */
 
     if (libxl__init_recursive_mutex(ctx, &ctx->lock) < 0) {
@@ -208,6 +210,8 @@ int libxl_ctx_free(libxl_ctx *ctx)
     libxl__sigchld_notneeded(gc);
     libxl__pipe_close(ctx->sigchld_selfpipe);
 
+    libxl__dm_support_cache_destroy(gc);
+
     CTX_UNLOCK;
     pthread_mutex_destroy(&ctx->lock);
 
tools/libxl/libxl_dm.c +231 -0

237 lines not on this page · raw message

tools/libxl/libxl_internal.h +76 -0

100 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#6 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Quoted 3 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device model executable supports.  This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

I know libvirt does something similar to this, but they want to support older versions, whereas we at least have the luxury of not caring about versions before the point this code lands.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Ian Campbell writes ("Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*"):

Quoted 7 lines · Ian Jackson
Quoted 3 lines

This allows code elsewhere in libxl to find out what options a device model executable supports.  This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

Not as far as I'm aware. Stefano, could you perhaps put out some feelers ?

Ian.

#8 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

In reply to Ian Campbell
Quoted 10 lines · Ian Campbell
Quoted 3 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device model executable supports. This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

I know libvirt does something similar to this, but they want to support older versions, whereas we at least have the luxury of not caring about versions before the point this code lands.

Since qemu 1.2.0, libvirt has been using the various QMP commands to probe for qemu capabilities, instead of parsing help output.

Regards,
Jim

#9 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Quoted 8 lines · Jim Fehlig
Quoted 7 lines · Ian Campbell
Quoted 3 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device model executable supports.  This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

Quoted 6 lines
Quoted 3 lines

I know libvirt does something similar to this, but they want to support older versions, whereas we at least have the luxury of not caring about versions before the point this code lands.

Since qemu 1.2.0, libvirt has been using the various QMP commands to probe for qemu capabilities, instead of parsing help output.

As in it spawns a qemu specifically to ask the questions and then kills it and starts what it needs _or_ it starts the qemu with minimal command line cfg and then dynamically pokes in the full config via qmp?

Ian.

Quoted 4 lines

Regards,
Jim

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Quoted 12 lines · Ian Campbell
Quoted 11 lines · Jim Fehlig
Quoted 10 lines · Ian Campbell
Quoted 3 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device model executable supports. This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

I know libvirt does something similar to this, but they want to support older versions, whereas we at least have the luxury of not caring about versions before the point this code lands.

Quoted 6 lines
Quoted 2 lines

Since qemu 1.2.0, libvirt has been using the various QMP commands to probe for qemu capabilities, instead of parsing help output.

As in it spawns a qemu specifically to ask the questions and then kills it and starts what it needs _or_ it starts the qemu with minimal command line cfg and then dynamically pokes in the full config via qmp?

The latter. When the qemu driver loads, it starts qemu, pokes it for capabilities via qmp, caches what it finds, then terminates it. The cached capabilities are used until the associated qemu binary is changed/updated.

If interested in peeking at the code, see virQEMUCapsInitQMP() and the functions it calls in $libvirt_src/src/qemu/qemu_capabilities.c. E.g.

virQEMUCapsInitQMP()
-> virQEMUCapsInitQMPMonitor()

    -> virQEMUCapsInitQMPBasic()

Regards,
Jim

#11 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Quoted 21 lines · Jim Fehlig
Quoted 20 lines · Ian Campbell
Quoted 18 lines · Jim Fehlig
Quoted 9 lines · Ian Campbell
Quoted 4 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device
model executable supports.  This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

Quoted 5 lines

I know libvirt does something similar to this, but they want to support
older versions, whereas we at least have the luxury of not caring about
versions before the point this code lands.

Since qemu 1.2.0, libvirt has been using the various QMP commands to probe for
qemu capabilities, instead of parsing help output.

Quoted 7 lines
Quoted 5 lines

As in it spawns a qemu specifically to ask the questions and then kills it
and starts what it needs _or_ it starts the qemu with minimal command line
cfg and then dynamically pokes in the full config via qmp?

The latter.

From the description I think you meant former?

Quoted 10 lines

When the qemu driver loads, it starts qemu, pokes it for
capabilities via qmp, caches what it finds, then terminates it. The cached capabilities are used until the associated qemu binary is changed/updated.

If interested in peeking at the code, see virQEMUCapsInitQMP() and the functions it calls in $libvirt_src/src/qemu/qemu_capabilities.c. E.g.

virQEMUCapsInitQMP()
  -> virQEMUCapsInitQMPMonitor()
    -> virQEMUCapsInitQMPBasic()

Thanks.

When I spoke to Ian J yesterday we decided doing this properly (via QMP as above) would be nice it was out of scope for this series for now. It would make a nice future bit of cleanup though for sure.

Ian.

Quoted 9 lines

Regards,
Jim

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH 03/28] libxl: Provide libxl__dm_support_*

Quoted 21 lines · Ian Campbell
Quoted 20 lines · Jim Fehlig
Quoted 19 lines · Ian Campbell
Quoted 18 lines · Jim Fehlig
Quoted 9 lines · Ian Campbell
Quoted 4 lines · Ian Jackson

This allows code elsewhere in libxl to find out what options a device
model executable supports. This is done by searching the usage message for fixed strings.

Has anyone (ever, not necessarily a Xen person nor in this context) approached upstream QEMU about a machine readable output of some sort?

Quoted 5 lines

I know libvirt does something similar to this, but they want to support
older versions, whereas we at least have the luxury of not caring about
versions before the point this code lands.

Since qemu 1.2.0, libvirt has been using the various QMP commands to probe for
qemu capabilities, instead of parsing help output.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

As in it spawns a qemu specifically to ask the questions and then kills it
and starts what it needs _or_ it starts the qemu with minimal command line
cfg and then dynamically pokes in the full config via qmp?

The latter.

From the description I think you meant former?

Yes :-). Brain thought one way, fingers typed another...

Regards,
Jim

Quoted 27 lines
Quoted 11 lines

When the qemu driver loads, it starts qemu, pokes it for
capabilities via qmp, caches what it finds, then terminates it. The cached capabilities are used until the associated qemu binary is changed/updated.

If interested in peeking at the code, see virQEMUCapsInitQMP() and the functions it calls in $libvirt_src/src/qemu/qemu_capabilities.c. E.g.

virQEMUCapsInitQMP()
-> virQEMUCapsInitQMPMonitor()

    -> virQEMUCapsInitQMPBasic()

Thanks.

When I spoke to Ian J yesterday we decided doing this properly (via QMP as above) would be nice it was out of scope for this series for now. It would make a nice future bit of cleanup though for sure.

Ian.

Quoted 8 lines

Regards,
Jim

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

PATCH v6 4/28 libxl: Invoke libxl__dm_support_*

In reply to Ian Jackson

When creating a domain (including when restoring), invoke the dm_support_check machinery on the principal qemu dm (if applicable).

This involves another introducing function callback boundary in the domcreate control flow; but this is a straightforward one.

Now, libxl__dm_supported is available (for the principal qemu dm) in the rest of the creation path.

No callers of libxl__dm_supported yet. So in this patch we just pointlessly run qemu, collect and scan its usage message, and cache what we understand from the results.

---
v6: New patch.

---
 tools/libxl/libxl_create.c   |   47 ++++++++++++++++++++++++++++++++++--------
 tools/libxl/libxl_internal.h |    1 +
 2 files changed, 39 insertions(+), 9 deletions(-)
tools/libxl/libxl_create.c +38 -9
index 261816a..1af7066 100644
@@ -719,6 +719,8 @@ static void remus_checkpoint_stream_done(
  */
 
 /* Event callbacks, in this order: */
+static void domcreate_dm_support_checked(libxl__egc *egc,
+    libxl__dm_support_check_state *checking, int rc);
 static void domcreate_devmodel_started(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss,
                                        int rc);
@@ -774,7 +776,7 @@ static void initiate_domain_create(libxl__egc *egc,
     /* convenience aliases */
     libxl_domain_config *const d_config = dcs->guest_config;
     libxl__domain_build_state *const state = &dcs->build_state;
-    const int restore_fd = dcs->restore_fd;
+    const libxl_domain_build_info *b_info = &d_config->b_info;
 
     domid = dcs->domid_soft_reset;
 
@@ -911,10 +913,6 @@ static void initiate_domain_create(libxl__egc *egc,
         }
     }
 
-    dcs->bl.ao = ao;
-    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
@@ -942,6 +940,41 @@ static void initiate_domain_create(libxl__egc *egc,
             d_config->nics[i].devid = ++last_devid;
     }
 
+    if (b_info->type != LIBXL_DOMAIN_TYPE_HVM ||
+        libxl_defbool_val(b_info->device_model_stubdomain)) {
+        domcreate_dm_support_checked(egc, &dcs->dm_support_check, 0);
+    } else {
+        dcs->dm_support_check.ao = ao;
+        dcs->dm_support_check.dm = libxl__domain_device_model(gc, b_info);
+        dcs->dm_support_check.callback = domcreate_dm_support_checked;
+        libxl__dm_support_check_start(&dcs->dm_support_check);
+    }
+
+error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_dm_support_checked(libxl__egc *egc,
+    libxl__dm_support_check_state *checking, int rc)
+{
+    libxl__domain_create_state *dcs =
+        CONTAINER_OF(checking, *dcs, dm_support_check);
+    STATE_AO_GC(dcs->ao);
+    
+    /* convenience aliases */
+    libxl_domain_config *const d_config = dcs->guest_config;
+    const int restore_fd = dcs->restore_fd;
+
+    if (rc) {
+        domcreate_complete(egc, dcs, rc);
+        return;
+    }
+
+    dcs->bl.ao = ao;
+    libxl_device_disk *bootdisk =
+        d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
+
     if (restore_fd >= 0 || dcs->domid_soft_reset != INVALID_DOMID) {
         LOG(DEBUG, "restoring, not running bootloader");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
@@ -959,10 +992,6 @@ static void initiate_domain_create(libxl__egc *egc,
         libxl__bootloader_run(egc, &dcs->bl);
     }
     return;
-
-error_out:
-    assert(ret);
-    domcreate_complete(egc, dcs, ret);
 }
 
 static void domcreate_bootloader_console_available(libxl__egc *egc,
tools/libxl/libxl_internal.h +1 -0
index f963ad6..fd6ef61 100644
@@ -3510,6 +3510,7 @@ struct libxl__domain_create_state {
     /* private to domain_create */
     int guest_domid;
     libxl__domain_build_state build_state;
+    libxl__dm_support_check_state dm_support_check;
     libxl__bootloader_state bl;
     libxl__stub_dm_spawn_state dmss;
         /* If we're not doing stubdom, we use only dmss.dm,
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH v6 5/28 libxl: libxl__spawn_stub_dm: Introduce `dmpath'

In reply to Ian Jackson

Combine two identical calls to libxl__device_model_xs_path, for clarity.

No functional change.

---
v6: New patch.

---
 tools/libxl/libxl_dm.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/libxl/libxl_dm.c +4 -4
index 675e859..6f5fe45 100644
@@ -1557,11 +1557,11 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     perm[1].perms = XS_PERM_READ;
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
-    xs_mkdir(ctx->xsh, t,
-             libxl__device_model_xs_path(gc, dm_domid, guest_domid, ""));
+    const char *dmpath = libxl__device_model_xs_path(gc,
+            dm_domid, guest_domid, "");
+    xs_mkdir(ctx->xsh, t, dmpath);
     xs_set_permissions(ctx->xsh, t,
-                       libxl__device_model_xs_path(gc, dm_domid,
-                                                   guest_domid, ""),
+                       dmpath,
                        perm, ARRAY_SIZE(perm));
     if (!xs_transaction_end(ctx->xsh, t, 0))
         if (errno == EAGAIN)
Signature · 1 line
1.7.10.4
#15 lore raw

PATCH v6 6/28 libxl: qemu_pci_*: Introduce DMPATH local macro, twice

In reply to Ian Jackson

This condenses a number of repetetive calls to libxl__device_model_xs_path. No functional change.

---
v6: New patch.

---
 tools/libxl/libxl_pci.c |   24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
tools/libxl/libxl_pci.c +17 -7
index dc10cb7..2c7a2c6 100644
@@ -966,10 +966,13 @@ static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     char *state, *vdevfn;
     uint32_t dm_domid;
 
+#define DMPATH(suffix) \
+    libxl__device_model_xs_path(gc, dm_domid, domid, (suffix))
+
     dm_domid = libxl_get_stubdom_id(CTX, domid);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+    path = DMPATH("/state");
     state = libxl__xs_read(gc, XBT_NULL, path);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/parameter");
+    path = DMPATH("/parameter");
     if (pcidev->vdevfn) {
         libxl__xs_printf(gc, XBT_NULL, path, PCI_BDF_VDEVFN","PCI_OPTIONS,
                          pcidev->domain, pcidev->bus, pcidev->dev,
@@ -984,9 +987,9 @@ static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     libxl__qemu_traditional_cmd(gc, domid, "pci-ins");
     rc = libxl__wait_for_device_model_deprecated(gc, domid, NULL, NULL,
                                       pci_ins_check, state);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/parameter");
+    path = DMPATH("/parameter");
     vdevfn = libxl__xs_read(gc, XBT_NULL, path);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+    path = DMPATH("/state");
     if ( rc < 0 )
         LOG(ERROR, "qemu refused to add device: %s", vdevfn);
     else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 ) {
@@ -995,6 +998,8 @@ static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     }
     xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
 
+#undef DMPATH
+
     return rc;
 }
 
@@ -1307,9 +1312,12 @@ static int qemu_pci_remove_xenstore(libxl__gc *gc, uint32_t domid,
 
     dm_domid = libxl_get_stubdom_id(CTX, domid);
 
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+#define DMPATH(suffix) \
+    libxl__device_model_xs_path(gc, dm_domid, domid, suffix)
+
+    path = DMPATH("/state");
     state = libxl__xs_read(gc, XBT_NULL, path);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/parameter");
+    path = DMPATH("/parameter");
     libxl__xs_printf(gc, XBT_NULL, path, PCI_BDF, pcidev->domain,
                      pcidev->bus, pcidev->dev, pcidev->func);
 
@@ -1327,9 +1335,11 @@ static int qemu_pci_remove_xenstore(libxl__gc *gc, uint32_t domid,
             return ERROR_FAIL;
         }
     }
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+    path = DMPATH("/state");
     xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
 
+#undef DMPATH
+
     return 0;
 }
 
Signature · 1 line
1.7.10.4
#16 lore raw

PATCH v6 7/28 libxl: libxl__device_model_xs_path: Add emulator_id parameter

In reply to Ian Jackson

We are going to want to talk to two different qemus for some domains. This will involve a different xenstore path for the two qemus, so libxl__device_model_xs_path will need to take a parameter to say which.

Most call sites will want to talk to the main (device model, `DM') emulator. So introduce that parameter now, currently always passing EMUID_DM, which is defined in a dummy way that ensures the actual argument is EMUID_DM.

No functional change.

---
v6: Largely rewritten, but in some sense (at least conceptually)
based on a similar concept in v5.

---
 tools/libxl/libxl_device.c      |    3 ++-
 tools/libxl/libxl_dm.c          |   11 ++++++-----
 tools/libxl/libxl_dom.c         |   12 +++++++-----
 tools/libxl/libxl_dom_suspend.c |    3 ++-
 tools/libxl/libxl_internal.c    |    4 +++-
 tools/libxl/libxl_internal.h    |    9 ++++++---
 tools/libxl/libxl_pci.c         |    4 ++--
 7 files changed, 28 insertions(+), 18 deletions(-)
tools/libxl/libxl_device.c +2 -1
index 8bb5e93..22665e4 100644
@@ -1183,7 +1183,8 @@ int libxl__wait_for_device_model_deprecated(libxl__gc *gc,
     char *path;
     uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
 
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+    path = libxl__device_model_xs_path(gc, dm_domid, domid,
+                                       EMUID_DM, "/state");
     return libxl__xenstore_child_wait_deprecated(gc, domid,
                                      LIBXL_DEVICE_MODEL_START_TIMEOUT,
                                      "Device Model", path, state, spawning,
tools/libxl/libxl_dm.c +6 -5
index 6f5fe45..ea82e11 100644
@@ -1558,7 +1558,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
     const char *dmpath = libxl__device_model_xs_path(gc,
-            dm_domid, guest_domid, "");
+            dm_domid, guest_domid, EMUID_DM, "");
     xs_mkdir(ctx->xsh, t, dmpath);
     xs_set_permissions(ctx->xsh, t,
                        dmpath,
@@ -1724,7 +1724,7 @@ static void stubdom_pvqemu_cb(libxl__egc *egc,
                                   dm_domid, sdss->dm.guest_domid);
     sdss->xswait.path =
         libxl__device_model_xs_path(gc, dm_domid, sdss->dm.guest_domid,
-                                    "/state");
+                                    EMUID_DM, "/state");
     sdss->xswait.timeout_ms = LIBXL_STUBDOM_START_TIMEOUT * 1000;
     sdss->xswait.callback = stubdom_xswait_cb;
     rc = libxl__xswait_start(gc, &sdss->xswait);
@@ -1831,7 +1831,8 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss)
         free(path);
     }
 
-    path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID, domid, "");
+    path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
+                                       domid, EMUID_DM, "");
     xs_mkdir(ctx->xsh, XBT_NULL, path);
 
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
@@ -1886,7 +1887,7 @@ retry_transaction:
 
     spawn->what = GCSPRINTF("domain %d device model", domid);
     spawn->xspath = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                                domid, "/state");
+                                                domid, EMUID_DM, "/state");
     spawn->timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000;
     spawn->pidpath = GCSPRINTF("%s/image/device-model-pid", dom_path);
     spawn->midproc_cb = libxl__spawn_record_pid;
@@ -2099,7 +2100,7 @@ out:
 int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
 {
     char *path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                             domid, "");
+                                             domid, EMUID_DM, "");
     if (!xs_rm(CTX->xsh, XBT_NULL, path))
         LOG(ERROR, "xs_rm failed for %s", path);
     /* We should try to destroy the device model anyway. */
tools/libxl/libxl_dom.c +7 -5
index 47971a9..6ded9c1 100644
@@ -1103,7 +1103,8 @@ int libxl__qemu_traditional_cmd(libxl__gc *gc, uint32_t domid,
 {
     char *path = NULL;
     uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
-    path = libxl__device_model_xs_path(gc, dm_domid, domid, "/command");
+    path = libxl__device_model_xs_path(gc, dm_domid,
+                                       domid, EMUID_DM, "/command");
     return libxl__xs_printf(gc, XBT_NULL, path, "%s", cmd);
 }
 
@@ -1134,7 +1135,8 @@ int libxl__restore_emulator_xenstore_data(libxl__domain_create_state *dcs,
 
     const uint32_t domid = dcs->guest_domid;
     const uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
-    const char *xs_root = libxl__device_model_xs_path(gc, dm_domid, domid, "");
+    const char *xs_root = libxl__device_model_xs_path(gc, dm_domid,
+                                                      domid, EMUID_DM, "");
 
     while (next < end) {
         key = next;
@@ -1225,9 +1227,9 @@ static void domain_suspend_switch_qemu_xen_traditional_logdirty
     if (!lds->cmd_path) {
         uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
         lds->cmd_path = libxl__device_model_xs_path(gc, dm_domid, domid,
-                                                    "/logdirty/cmd");
+                                              EMUID_DM, "/logdirty/cmd");
         lds->ret_path = libxl__device_model_xs_path(gc, dm_domid, domid,
-                                                    "/logdirty/ret");
+                                              EMUID_DM, "/logdirty/ret");
     }
     lds->cmd = enable ? "enable" : "disable";
 
@@ -1442,7 +1444,7 @@ int libxl__save_emulator_xenstore_data(libxl__domain_suspend_state *dss,
     const uint32_t domid = dss->domid;
     const uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
 
-    xs_root = libxl__device_model_xs_path(gc, dm_domid, domid, "");
+    xs_root = libxl__device_model_xs_path(gc, dm_domid, domid, EMUID_DM, "");
 
     entries = libxl__xs_directory(gc, 0, GCSPRINTF("%s/physmap", xs_root),
                                   &nr_entries);
tools/libxl/libxl_dom_suspend.c +2 -1
index 3313ad1..d25fb59 100644
@@ -383,7 +383,8 @@ int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid)
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL: {
         uint32_t dm_domid = libxl_get_stubdom_id(CTX, domid);
 
-        path = libxl__device_model_xs_path(gc, dm_domid, domid, "/state");
+        path = libxl__device_model_xs_path(gc, dm_domid, domid,
+                                           EMUID_DM, "/state");
         state = libxl__xs_read(gc, XBT_NULL, path);
         if (state != NULL && !strcmp(state, "paused")) {
             libxl__qemu_traditional_cmd(gc, domid, "continue");
tools/libxl/libxl_internal.c +3 -1
index 328046b..ec93797 100644
@@ -554,7 +554,9 @@ void libxl__update_domain_configuration(libxl__gc *gc,
 }
 
 char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
-                                  uint32_t domid, const char *format,  ...)
+                                  uint32_t domid,
+                                  struct dummy_qemu_emulator_id emuid,
+                                  const char *format,  ...)
 {
     char *s, *fmt;
     va_list ap;
tools/libxl/libxl_internal.h +6 -3
index fd6ef61..ea56cda 100644
@@ -1955,9 +1955,12 @@ _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc_opt, const char *s);
 _hidden int libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
   /* Return the system-wide default device model */
 _hidden libxl_device_model_version libxl__default_device_model(libxl__gc *gc);
-_hidden char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
-                                          uint32_t domid,
-                                          const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
+
+static const struct dummy_qemu_emulator_id { int x; } EMUID_DM;
+_hidden char *libxl__device_model_xs_path(libxl__gc *gc,
+        uint32_t dm_domid,
+        uint32_t domid, struct dummy_qemu_emulator_id,
+        const char *format, ...) PRINTF_ATTRIBUTE(5, 6);
 
 /*
  * Calling context and GC for event-generating functions:
tools/libxl/libxl_pci.c +2 -2
index 2c7a2c6..86b8cf3 100644
@@ -967,7 +967,7 @@ static int qemu_pci_add_xenstore(libxl__gc *gc, uint32_t domid,
     uint32_t dm_domid;
 
 #define DMPATH(suffix) \
-    libxl__device_model_xs_path(gc, dm_domid, domid, (suffix))
+    libxl__device_model_xs_path(gc, dm_domid, domid, EMUID_DM, (suffix))
 
     dm_domid = libxl_get_stubdom_id(CTX, domid);
     path = DMPATH("/state");
@@ -1313,7 +1313,7 @@ static int qemu_pci_remove_xenstore(libxl__gc *gc, uint32_t domid,
     dm_domid = libxl_get_stubdom_id(CTX, domid);
 
 #define DMPATH(suffix) \
-    libxl__device_model_xs_path(gc, dm_domid, domid, suffix)
+    libxl__device_model_xs_path(gc, dm_domid, domid, EMUID_DM, suffix)
 
     path = DMPATH("/state");
     state = libxl__xs_read(gc, XBT_NULL, path);
Signature · 1 line
1.7.10.4
#17 lore raw

PATCH v6 8/28 libxl: libxl__destroy_domid: Bring dm destruction together

In reply to Ian Jackson

Change the order of operations so that the dm determination and destruction occur together.

The functional change is that the xenstore reads relating to the dm determination now occur after the pci devices have been removed and the domain has been paused. This should not have any visible effect.

---
v6: New patch.

---
 tools/libxl/libxl.c |   25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
tools/libxl/libxl.c +13 -12
index d96189d..1ac5d81 100644
@@ -1621,6 +1621,19 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
         goto out;
     }
 
+    if (libxl__device_pci_destroy_all(gc, domid) < 0)
+        LOG(ERROR, "pci shutdown failed for domid %d", domid);
+    rc = xc_domain_pause(ctx->xch, domid);
+    if (rc < 0) {
+        LOGEV(ERROR, rc, "xc_domain_pause failed for %d", domid);
+    }
+
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    if (!dom_path) {
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
     switch (libxl__domain_type(gc, domid)) {
     case LIBXL_DOMAIN_TYPE_HVM:
         if (libxl_get_stubdom_id(CTX, domid)) {
@@ -1639,18 +1652,6 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
         abort();
     }
 
-    dom_path = libxl__xs_get_dompath(gc, domid);
-    if (!dom_path) {
-        rc = ERROR_FAIL;
-        goto out;
-    }
-
-    if (libxl__device_pci_destroy_all(gc, domid) < 0)
-        LOG(ERROR, "pci shutdown failed for domid %d", domid);
-    rc = xc_domain_pause(ctx->xch, domid);
-    if (rc < 0) {
-        LOGEV(ERROR, rc, "xc_domain_pause failed for %d", domid);
-    }
     if (dm_present) {
         if (libxl__destroy_device_model(gc, domid) < 0)
             LOG(ERROR, "libxl__destroy_device_model failed for %d", domid);
Signature · 1 line
1.7.10.4
#18 lore raw

PATCH v6 9/28 libxl: Move some error handling and cleanup into libxl__destroy_device_model

In reply to Ian Jackson

Move
- the error log message
- the call to libxl__qmp_cleanup
into libxl__destroy_device_model from the one call site in libxl__destroy_domid.

We are going to want to call libxl__destroy_device_model more than once, and this code would otherwise need to be repeated for the other call site(s).

The call site now completely ignores the error, sadly. But I am not intending in this series to try to undertake destruction error-handling cleanup as well.

No functional change other than slight change to the error message.

---
v6: New patch.

---
 tools/libxl/libxl.c    |    4 +---
 tools/libxl/libxl_dm.c |   10 +++++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +1 -3
index 1ac5d81..5414649 100644
@@ -1653,10 +1653,8 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
     }
 
     if (dm_present) {
-        if (libxl__destroy_device_model(gc, domid) < 0)
-            LOG(ERROR, "libxl__destroy_device_model failed for %d", domid);
+        libxl__destroy_device_model(gc, domid);
 
-        libxl__qmp_cleanup(gc, domid);
     }
     dis->drs.ao = ao;
     dis->drs.domid = domid;
tools/libxl/libxl_dm.c +9 -1
index ea82e11..b85b377 100644
@@ -2099,13 +2099,21 @@ out:
 
 int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
 {
+    int rc;
+
     char *path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
                                              domid, EMUID_DM, "");
     if (!xs_rm(CTX->xsh, XBT_NULL, path))
         LOG(ERROR, "xs_rm failed for %s", path);
     /* We should try to destroy the device model anyway. */
-    return kill_device_model(gc,
+    rc = kill_device_model(gc,
                 GCSPRINTF("/local/domain/%d/image/device-model-pid", domid));
+    if (rc)
+        LOG(ERROR, "libxl__destroy_device_model failed for %d",
+            domid);
+
+    libxl__qmp_cleanup(gc, domid);
+    return rc;
 }
 
 int libxl__need_xenpv_qemu(libxl__gc *gc,
Signature · 1 line
1.7.10.4
#19 lore raw

PATCH v6 10/28 libxl: kill_device_model: Silently tolerate ENOENT on pid xs path

In reply to Ian Jackson

If the pid is absent in xenstore, this means there is no such DM. Do not complain about this, then. This allows us to simplify call sites. Do so for libxl__destroy_domid.

---
v6: New patch.

---
 tools/libxl/libxl.c    |    4 +---
 tools/libxl/libxl_dm.c |    7 ++++++-
 2 files changed, 7 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +1 -3
index 5414649..2a0c092 100644
@@ -1606,7 +1606,6 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
     libxl_ctx *ctx = CTX;
     uint32_t domid = dis->domid;
     char *dom_path;
-    char *pid;
     int rc, dm_present;
 
     libxl__ev_child_init(&dis->destroyer);
@@ -1642,8 +1641,7 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
         }
         /* fall through */
     case LIBXL_DOMAIN_TYPE_PV:
-        pid = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("/local/domain/%d/image/device-model-pid", domid));
-        dm_present = (pid != NULL);
+        dm_present = 1;
         break;
     case LIBXL_DOMAIN_TYPE_INVALID:
         rc = ERROR_FAIL;
tools/libxl/libxl_dm.c +6 -1
index b85b377..593f3e6 100644
@@ -2053,11 +2053,16 @@ static int kill_device_model(libxl__gc *gc, const char *xs_path_pid)
     int ret, pid;
 
     ret = libxl__xs_read_checked(gc, XBT_NULL, xs_path_pid, &xs_pid);
-    if (ret || !xs_pid) {
+    if (ret) {
         LOG(ERROR, "unable to find device model pid in %s", xs_path_pid);
         ret = ret ? : ERROR_FAIL;
         goto out;
     }
+    if (!xs_pid) {
+        /* Jolly good */
+        ret = 0;
+        goto out;
+    }
     pid = atoi(xs_pid);
 
     ret = kill(pid, SIGHUP);
Signature · 1 line
1.7.10.4
#20 lore raw

PATCH v6 11/28 libxl: emuids: Pass correct emuid to internal functions

In reply to Ian Jackson

Provide EMUID_PV and use it appropriately. We split our qemus into two:

EMUID_DM does actual emulation. EMUID_PV is for PV backends. Most places relate to emulation and can simply pass EMUID_DM.

Creation is a notable exception, where we pass the emuid as a new parameter to the spawn functions.

There is no overall functional change, because the ultimate consumer, libxl__device_model_xs_path, does not pay any attention to the parameter yet, and because we still need to plumb the emuid through to the qemu argument construction.

Also, domain destruction does not yet pass the right emuid.

---
v6: Largely rewritten, but in some sense (at least conceptually)
based on a similar concept in v5.

---
 tools/libxl/libxl_create.c   |   11 ++++++-----
 tools/libxl/libxl_dm.c       |   16 +++++++++-------
 tools/libxl/libxl_internal.c |    3 +--
 tools/libxl/libxl_internal.h |   15 +++++++++++----
 4 files changed, 27 insertions(+), 18 deletions(-)
tools/libxl/libxl_create.c +6 -5
index 1af7066..41ece5a 100644
@@ -1309,10 +1309,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         libxl_device_vkb_dispose(&vkb);
 
         dcs->dmss.dm.guest_domid = domid;
-        if (libxl_defbool_val(d_config->b_info.device_model_stubdomain))
-            libxl__spawn_stub_dm(egc, &dcs->dmss);
-        else
-            libxl__spawn_local_dm(egc, &dcs->dmss.dm);
+        if (libxl_defbool_val(d_config->b_info.device_model_stubdomain)) {
+            libxl__spawn_stub_dm(egc, &dcs->dmss, EMUID_DM);
+        } else {
+            libxl__spawn_local_dm(egc, &dcs->dmss.dm, EMUID_DM);
+        }
 
         /*
          * Handle the domain's (and the related stubdomain's) access to
@@ -1348,7 +1349,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
 
         if (need_qemu) {
             dcs->dmss.dm.guest_domid = domid;
-            libxl__spawn_local_dm(egc, &dcs->dmss.dm);
+            libxl__spawn_local_dm(egc, &dcs->dmss.dm, EMUID_PV);
             return;
         } else {
             assert(!dcs->dmss.dm.guest_domid);
tools/libxl/libxl_dm.c +9 -7
index 593f3e6..1e1dfa0 100644
@@ -1434,7 +1434,8 @@ char *libxl__stub_dm_name(libxl__gc *gc, const char *guest_name)
     return GCSPRINTF("%s-dm", guest_name);
 }
 
-void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
+void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss,
+                          int emuid)
 {
     STATE_AO_GC(sdss->dm.spawn.ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
@@ -1558,7 +1559,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
     const char *dmpath = libxl__device_model_xs_path(gc,
-            dm_domid, guest_domid, EMUID_DM, "");
+            dm_domid, guest_domid, emuid, "");
     xs_mkdir(ctx->xsh, t, dmpath);
     xs_set_permissions(ctx->xsh, t,
                        dmpath,
@@ -1668,7 +1669,7 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
     sdss->pvqemu.build_state = &sdss->dm_state;
     sdss->pvqemu.callback = spawn_stubdom_pvqemu_cb;
 
-    libxl__spawn_local_dm(egc, &sdss->pvqemu);
+    libxl__spawn_local_dm(egc, &sdss->pvqemu, EMUID_PV);
 
     return;
 
@@ -1724,7 +1725,7 @@ static void stubdom_pvqemu_cb(libxl__egc *egc,
                                   dm_domid, sdss->dm.guest_domid);
     sdss->xswait.path =
         libxl__device_model_xs_path(gc, dm_domid, sdss->dm.guest_domid,
-                                    EMUID_DM, "/state");
+                                    EMUID_PV, "/state");
     sdss->xswait.timeout_ms = LIBXL_STUBDOM_START_TIMEOUT * 1000;
     sdss->xswait.callback = stubdom_xswait_cb;
     rc = libxl__xswait_start(gc, &sdss->xswait);
@@ -1771,7 +1772,8 @@ static void device_model_spawn_outcome(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss,
                                        int rc);
 
-void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss)
+void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss,
+                           int emuid)
 {
     /* convenience aliases */
     const int domid = dmss->guest_domid;
@@ -1832,7 +1834,7 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     }
 
     path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                       domid, EMUID_DM, "");
+                                       domid, emuid, "");
     xs_mkdir(ctx->xsh, XBT_NULL, path);
 
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
@@ -1887,7 +1889,7 @@ retry_transaction:
 
     spawn->what = GCSPRINTF("domain %d device model", domid);
     spawn->xspath = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                                domid, EMUID_DM, "/state");
+                                                domid, emuid, "/state");
     spawn->timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000;
     spawn->pidpath = GCSPRINTF("%s/image/device-model-pid", dom_path);
     spawn->midproc_cb = libxl__spawn_record_pid;
tools/libxl/libxl_internal.c +1 -2
index ec93797..843fdbf 100644
@@ -554,8 +554,7 @@ void libxl__update_domain_configuration(libxl__gc *gc,
 }
 
 char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
-                                  uint32_t domid,
-                                  struct dummy_qemu_emulator_id emuid,
+                                  uint32_t domid, int emuid,
                                   const char *format,  ...)
 {
     char *s, *fmt;
tools/libxl/libxl_internal.h +11 -4
index ea56cda..71c1e17 100644
@@ -1956,10 +1956,15 @@ _hidden int libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
   /* Return the system-wide default device model */
 _hidden libxl_device_model_version libxl__default_device_model(libxl__gc *gc);
 
-static const struct dummy_qemu_emulator_id { int x; } EMUID_DM;
+enum {
+    EMUID_PV,
+    EMUID_DM,
+    /* NB stubdom and its PV service domain not recorded here */
+};
+
 _hidden char *libxl__device_model_xs_path(libxl__gc *gc,
         uint32_t dm_domid,
-        uint32_t domid, struct dummy_qemu_emulator_id,
+        uint32_t domid, int emuid,
         const char *format, ...) PRINTF_ATTRIBUTE(5, 6);
 
 /*
@@ -3399,7 +3404,8 @@ struct libxl__dm_spawn_state {
     libxl__dm_spawn_cb *callback;
 };
 
-_hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
+_hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*,
+                                   int emuid);
 
 /* Stubdom device models. */
 
@@ -3418,7 +3424,8 @@ typedef struct {
     libxl__xswait_state xswait;
 } libxl__stub_dm_spawn_state;
 
-_hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
+_hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*,
+                                  int emuid);
 
 _hidden char *libxl__stub_dm_name(libxl__gc *gc, const char * guest_name);
 
Signature · 1 line
1.7.10.4
#21 lore raw

PATCH v6 12/28 libxl: Use libxl__device_model_xs_path in libxl__spawn_qdisk_backend

In reply to Ian Jackson

Rather than open-coding the answer. We are going to change this path.

No functional change (other than to very unlikely error paths).

---
v6: Broken out from a portmanteau patch which was in v5, and fixed.

---
 tools/libxl/libxl_dm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libxl/libxl_dm.c +2 -1
index 1e1dfa0..916d12c 100644
@@ -2018,7 +2018,8 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     dmss->build_state->saved_state = 0;
 
     dmss->spawn.what = GCSPRINTF("domain %u Qdisk backend", domid);
-    dmss->spawn.xspath = GCSPRINTF("device-model/%u/state", domid);
+    dmss->spawn.xspath = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
+                                                     domid, EMUID_PV, "/state");
     dmss->spawn.timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000;
     /*
      * We cannot save Qemu pid anywhere in the xenstore guest dir,
Signature · 1 line
1.7.10.4
#22 lore raw

PATCH v6 13/28 libxl: emuids: Record which emuids we have started to create

In reply to Ian Jackson

Record in xenstore which emulator(s) we have started. Right now this information is not used.

The _get_bodgeerrors function is going to be needed in several call sites where errors are currently not handled well. I don't want to try to combine an error handling rework with this series.

---
v6: New patch - storing this information here is a new approach in v6.

---
 docs/misc/xenstore-paths.markdown |    5 +++
 tools/libxl/libxl_create.c        |    2 ++
 tools/libxl/libxl_dm.c            |   61 +++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h      |   14 +++++++++
 4 files changed, 82 insertions(+)
docs/misc/xenstore-paths.markdown +5 -0
index 197bb0f..208b64b 100644
@@ -505,6 +505,11 @@ The guest's virtual time offset from UTC in seconds.
 
 The device model version for a domain.
 
+#### ~/libxl/$DOMID/dm-emuidmap = INTEGER
+
+Which emulator IDs are serving a domain.  This is a bitmask of
+`1u<<EMUID_*` values (see `libxl_internal.h`).
+
 #### /libxl/$DOMID/remus/netbuf/$DEVID/ifb = STRING [n,INTERNAL]
 
 ifb device used by Remus to buffer network output from the associated vif.
tools/libxl/libxl_create.c +2 -0
index 41ece5a..71893c5 100644
@@ -410,6 +410,8 @@ int libxl__domain_build(libxl__gc *gc,
     struct timeval start_time;
     int i, ret;
 
+    state->emuidmap = 0;
+
     ret = libxl__build_pre(gc, domid, d_config, state);
     if (ret)
         goto out;
tools/libxl/libxl_dm.c +61 -0

79 lines not on this page · raw message

tools/libxl/libxl_internal.h +14 -0
index 71c1e17..0194823 100644
@@ -1097,6 +1097,8 @@ typedef struct {
 
     char *saved_state;
 
+    unsigned emuidmap;
+
     libxl__file_reference pv_kernel;
     libxl__file_reference pv_ramdisk;
     const char * pv_cmdline;
@@ -1962,6 +1964,18 @@ enum {
     /* NB stubdom and its PV service domain not recorded here */
 };
 
+_hidden int libxl__dm_emuidmap_add(libxl__gc*, int domid,
+                                   libxl__domain_build_state*, int emuid);
+
+_hidden int libxl__dm_emuidmap_get(libxl__gc*, int domid,
+                                   unsigned *emuidmap_r);
+
+_hidden void libxl__dm_emuidmap_get_bodgeerrors(libxl__gc*, int domid,
+                                             unsigned *emuidmap_r);
+    /* *emuidmap_r is set to a default (but unreliable) value even
+     * if we get an error, which is logged.  Try to avoid calling
+     * this if you can! */
+
 _hidden char *libxl__device_model_xs_path(libxl__gc *gc,
         uint32_t dm_domid,
         uint32_t domid, int emuid,
Signature · 1 line
1.7.10.4
#23 lore raw

PATCH v6 14/28 libxl: emuids: Pass emuid to dm destruction

In reply to Ian Jackson

Rather than recomputing this with a switch() statement, etc., we rely on the emuidmap to tell us which qemus to destroy.

---
v6: New patch. This is a new approach in v6.

squash! libxl: Pass emuid to dm destruction

---
 tools/libxl/libxl.c          |   31 ++++++++++++-------------------
 tools/libxl/libxl_dm.c       |   13 +++++++++----
 tools/libxl/libxl_internal.h |    3 ++-
 3 files changed, 23 insertions(+), 24 deletions(-)
tools/libxl/libxl.c +12 -19
index 2a0c092..dff4478 100644
@@ -1606,7 +1606,8 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
     libxl_ctx *ctx = CTX;
     uint32_t domid = dis->domid;
     char *dom_path;
-    int rc, dm_present;
+    int rc;
+    unsigned emuidmap;
 
     libxl__ev_child_init(&dis->destroyer);
 
@@ -1633,27 +1634,19 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
         goto out;
     }
 
-    switch (libxl__domain_type(gc, domid)) {
-    case LIBXL_DOMAIN_TYPE_HVM:
-        if (libxl_get_stubdom_id(CTX, domid)) {
-            dm_present = 0;
-            break;
-        }
-        /* fall through */
-    case LIBXL_DOMAIN_TYPE_PV:
-        dm_present = 1;
-        break;
-    case LIBXL_DOMAIN_TYPE_INVALID:
-        rc = ERROR_FAIL;
-        goto out;
-    default:
-        abort();
-    }
+    libxl__dm_emuidmap_get_bodgeerrors(gc, domid, &emuidmap);
 
-    if (dm_present) {
-        libxl__destroy_device_model(gc, domid);
+    libxl__destroy_device_model(gc, domid, emuidmap, EMUID_PV);
 
+    if (libxl__domain_type(gc, domid) == LIBXL_DOMAIN_TYPE_HVM &&
+        libxl_get_stubdom_id(CTX, domid)) {
+        /* This is destroyed in libxl__domain_destroy */
+    } else {
+        libxl__destroy_device_model(gc, domid, emuidmap, EMUID_DM);
     }
+
+    libxl__destroy_device_model(gc, domid, emuidmap, EMUID_PV);
+
     dis->drs.ao = ao;
     dis->drs.domid = domid;
     dis->drs.callback = devices_destroy_cb;
tools/libxl/libxl_dm.c +9 -4
index 2bd1eb0..bcae664 100644
@@ -2111,20 +2111,25 @@ out:
     return rc;
 }
 
-int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
+int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid,
+                                unsigned emuidmap, int emuid)
 {
     int rc;
 
+    if (!(emuidmap & (1u << emuid)))
+        return 0;
+
     char *path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                             domid, EMUID_DM, "");
+                                             domid, emuid, "");
     if (!xs_rm(CTX->xsh, XBT_NULL, path))
         LOG(ERROR, "xs_rm failed for %s", path);
+
     /* We should try to destroy the device model anyway. */
     rc = kill_device_model(gc,
                 GCSPRINTF("/local/domain/%d/image/device-model-pid", domid));
     if (rc)
-        LOG(ERROR, "libxl__destroy_device_model failed for %d",
-            domid);
+        LOG(ERROR, "libxl__destroy_device_model failed for %d (emuid=%d)",
+            domid, emuid);
 
     libxl__qmp_cleanup(gc, domid);
     return rc;
tools/libxl/libxl_internal.h +2 -1
index 0194823..4a9003d 100644
@@ -1656,7 +1656,8 @@ _hidden int libxl__wait_for_device_model_deprecated(libxl__gc *gc,
                                                       void *userdata),
                                 void *check_callback_userdata);
 
-_hidden int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid);
+_hidden int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid,
+                                        unsigned emuidmap, int emuid);
 
 _hidden const libxl_vnc_info *libxl__dm_vnc(const libxl_domain_config *g_cfg);
 
Signature · 1 line
1.7.10.4
#24 lore raw

PATCH v6 15/28 libxl: emuids: Pass emuid to device model argument construction

In reply to Ian Jackson

No functional change yet.

---
 tools/libxl/libxl_dm.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/libxl/libxl_dm.c +6 -2
index bcae664..5b56047 100644
@@ -752,6 +752,7 @@ static int libxl__dm_runas_helper(libxl__gc *gc, const char *username)
 
 static int libxl__build_device_model_args_new(libxl__gc *gc,
                                         const char *dm, int guest_domid,
+                                        int emuid,
                                         const libxl_domain_config *guest_config,
                                         char ***args, char ***envs,
                                         const libxl__domain_build_state *state,
@@ -1296,6 +1297,7 @@ end_search:
 
 static int libxl__build_device_model_args(libxl__gc *gc,
                                         const char *dm, int guest_domid,
+                                        int emuid,
                                         const libxl_domain_config *guest_config,
                                         char ***args, char ***envs,
                                         const libxl__domain_build_state *state,
@@ -1313,7 +1315,8 @@ static int libxl__build_device_model_args(libxl__gc *gc,
         assert(dm_state_fd != NULL);
         assert(*dm_state_fd < 0);
         return libxl__build_device_model_args_new(gc, dm,
-                                                  guest_domid, guest_config,
+                                                  guest_domid, emuid,
+                                                  guest_config,
                                                   args, envs,
                                                   state, dm_state_fd);
     default:
@@ -1531,6 +1534,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss,
         goto out;
 
     ret = libxl__build_device_model_args(gc, "stubdom-dm", guest_domid,
+                                         emuid,
                                          guest_config, &args, NULL,
                                          d_state, NULL);
     if (ret) {
@@ -1817,7 +1821,7 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss,
         rc = ERROR_FAIL;
         goto out;
     }
-    rc = libxl__build_device_model_args(gc, dm, domid, guest_config,
+    rc = libxl__build_device_model_args(gc, dm, domid, emuid, guest_config,
                                           &args, &envs, state,
                                           &dm_state_fd);
     if (rc)
Signature · 1 line
1.7.10.4
#25 lore raw

PATCH v6 16/28 libxl: emuids: Provide libxl__dm_xs_path_rel

In reply to Ian Jackson

This gives a relative path, suitable for driver domains. It will be used shortly.

No functional change.

---
v6: New patch

---
 tools/libxl/libxl_internal.c |   10 ++++++++--
 tools/libxl/libxl_internal.h |    4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.c +8 -2
index 843fdbf..d0c98b0 100644
@@ -553,6 +553,12 @@ void libxl__update_domain_configuration(libxl__gc *gc,
     dst->b_info.video_memkb = src->b_info.video_memkb;
 }
 
+char *libxl__dm_xs_path_rel(libxl__gc *gc,
+                            uint32_t domid, int emuid)
+{
+    return GCSPRINTF("device-model/%u", domid);
+}
+
 char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
                                   uint32_t domid, int emuid,
                                   const char *format,  ...)
@@ -560,8 +566,8 @@ char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
     char *s, *fmt;
     va_list ap;
 
-    fmt = GCSPRINTF("/local/domain/%u/device-model/%u%s", dm_domid,
-                    domid, format);
+    fmt = GCSPRINTF("/local/domain/%u/%s%s", dm_domid,
+                    libxl__dm_xs_path_rel(gc, domid, emuid), format);
 
     va_start(ap, format);
     s = libxl__vsprintf(gc, fmt, ap);
tools/libxl/libxl_internal.h +4 -0
index 4a9003d..2248509 100644
@@ -1982,6 +1982,10 @@ _hidden char *libxl__device_model_xs_path(libxl__gc *gc,
         uint32_t domid, int emuid,
         const char *format, ...) PRINTF_ATTRIBUTE(5, 6);
 
+_hidden char *libxl__dm_xs_path_rel(libxl__gc *gc,
+                                    uint32_t domid, int emuid);
+    /* returns relative path (ie assuming we are the toolstack) */
+
 /*
  * Calling context and GC for event-generating functions:
  *
Signature · 1 line
1.7.10.4
#26 lore raw

PATCH v6 17/28 libxl: emuids: Do not open-code device-model/%u in libxl__destroy_qdisk_backend

In reply to Ian Jackson

This is used in a driver domain context and doesn't get the caller's domid. But that's OK, we can now use libxl__dm_xs_path_rel which returns the same value as previously.

No functional change.

---
 tools/libxl/libxl_dm.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_dm.c +1 -1
index 5b56047..3bf1317 100644
@@ -2109,7 +2109,7 @@ int libxl__destroy_qdisk_backend(libxl__gc *gc, uint32_t domid)
 
     libxl__xs_rm_checked(gc, XBT_NULL, pid_path);
     libxl__xs_rm_checked(gc, XBT_NULL,
-                         GCSPRINTF("device-model/%u", domid));
+                         libxl__dm_xs_path_rel(gc, domid, EMUID_PV));
 
 out:
     return rc;
Signature · 1 line
1.7.10.4
#27 lore raw

PATCH v6 18/28 libxl: emuids: Change pid path in xenstore

In reply to Ian Jackson

Introduce libxl__dm_xs_pid_path and use it everywhere.

There is no overall functional change, although there is a change to these internal paths. Also, the paths used are now relative (ie to /local/domain/<domid-containing-device-model>) rather than absolute.

---
v6: Rewritten, actual paths changed, patch title changed
v4: Update xenstore-paths.markdown

---
 docs/misc/xenstore-paths.markdown |   10 +++-------
 tools/libxl/libxl_dm.c            |   11 +++++------
 tools/libxl/libxl_internal.c      |    6 ++++++
 tools/libxl/libxl_internal.h      |    3 +++
 4 files changed, 17 insertions(+), 13 deletions(-)
docs/misc/xenstore-paths.markdown +3 -7
index 208b64b..3bd31af 100644
@@ -143,11 +143,6 @@ The guests name.
 
 The domain's own ID.
 
-#### ~/image/device-model-pid = INTEGER   [INTERNAL]
-
-The process ID of the device model associated with this domain, if it
-has one.
-
 #### ~/cpu/[0-9]+/availability = ("online"|"offline") [PV]
 
 One node for each virtual CPU up to the guest's configured
@@ -463,9 +458,10 @@ in the value supplied by the guest in this case).
 
 Contains the status of the device models running on the domain.
 
-#### ~/libxl/$DOMID/qdisk-backend-pid [w]
+#### ~/libxl/$DOMID/dm-pid-$EMUID [w] = INTEGER   [INTERNAL]
 
-Contains the PIDs of the device models running on the domain.
+Contains the PIDs of the device models running on this domain, to
+service $DOMID.
 
 ## Virtual Machine Paths
 
tools/libxl/libxl_dm.c +5 -6
index 3bf1317..6b0bbed 100644
@@ -1867,7 +1867,7 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss,
     }
 
     const char *dom_path = libxl__xs_get_dompath(gc, domid);
-    spawn->pidpath = GCSPRINTF("%s/%s", dom_path, "image/device-model-pid");
+    spawn->pidpath = libxl__dm_xs_pid_path(gc, domid, emuid);
 
     if (vnc && vnc->passwd) {
         /* This xenstore key will only be used by qemu-xen-traditionnal.
@@ -1901,7 +1901,7 @@ retry_transaction:
     spawn->xspath = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
                                                 domid, emuid, "/state");
     spawn->timeout_ms = LIBXL_DEVICE_MODEL_START_TIMEOUT * 1000;
-    spawn->pidpath = GCSPRINTF("%s/image/device-model-pid", dom_path);
+    spawn->pidpath = libxl__dm_xs_pid_path(gc, domid, emuid);
     spawn->midproc_cb = libxl__spawn_record_pid;
     spawn->confirm_cb = device_model_confirm;
     spawn->failure_cb = device_model_startup_failed;
@@ -2036,7 +2036,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
      * because we will call this from unprivileged driver domains,
      * so save it in the current domain libxl private dir.
      */
-    dmss->spawn.pidpath = GCSPRINTF("libxl/%u/qdisk-backend-pid", domid);
+    dmss->spawn.pidpath = libxl__dm_xs_pid_path(gc, domid, EMUID_PV);
     dmss->spawn.midproc_cb = libxl__spawn_record_pid;
     dmss->spawn.confirm_cb = device_model_confirm;
     dmss->spawn.failure_cb = device_model_startup_failed;
@@ -2101,7 +2101,7 @@ int libxl__destroy_qdisk_backend(libxl__gc *gc, uint32_t domid)
     char *pid_path;
     int rc;
 
-    pid_path = GCSPRINTF("libxl/%u/qdisk-backend-pid", domid);
+    pid_path = libxl__dm_xs_pid_path(gc, domid, EMUID_PV);
 
     rc = kill_device_model(gc, pid_path);
     if (rc)
@@ -2129,8 +2129,7 @@ int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid,
         LOG(ERROR, "xs_rm failed for %s", path);
 
     /* We should try to destroy the device model anyway. */
-    rc = kill_device_model(gc,
-                GCSPRINTF("/local/domain/%d/image/device-model-pid", domid));
+    rc = kill_device_model(gc, libxl__dm_xs_pid_path(gc, domid, emuid));
     if (rc)
         LOG(ERROR, "libxl__destroy_device_model failed for %d (emuid=%d)",
             domid, emuid);
tools/libxl/libxl_internal.c +6 -0
index d0c98b0..30271c9 100644
@@ -576,6 +576,12 @@ char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
     return s;
 }
 
+char *libxl__dm_xs_pid_path(libxl__gc *gc, int guest_domid, int emuid)
+{
+    return GCSPRINTF("libxl/%d/dm-pid-%d", guest_domid, emuid);
+}
+
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_internal.h +3 -0
index 2248509..350ec29 100644
@@ -1986,6 +1986,9 @@ _hidden char *libxl__dm_xs_path_rel(libxl__gc *gc,
                                     uint32_t domid, int emuid);
     /* returns relative path (ie assuming we are the toolstack) */
 
+_hidden char *libxl__dm_xs_pid_path(libxl__gc *gc,
+                                    int guest_domid, int emuid);
+
 /*
  * Calling context and GC for event-generating functions:
  *
Signature · 1 line
1.7.10.4
#28 lore raw

PATCH v6 19/28 libxl: Improve libxl__destroy_device_model

In reply to Ian Jackson

Take some improvements from libxl__destroy_qdisk_backend:
* Use libxl__xs_rm_checked rather than xs_rm
* Remove the pid from xenstore

Then libxl__destroy_qdisk_backend can be made into a simple wrapper around libxl__destroy_device_model.

---
v6: New patch.

---
 tools/libxl/libxl_dm.c |   27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
tools/libxl/libxl_dm.c +9 -18
index 6b0bbed..886ed9c 100644
@@ -2098,21 +2098,12 @@ out:
 /* Helper to destroy a Qdisk backend */
 int libxl__destroy_qdisk_backend(libxl__gc *gc, uint32_t domid)
 {
-    char *pid_path;
-    int rc;
-
-    pid_path = libxl__dm_xs_pid_path(gc, domid, EMUID_PV);
+    unsigned emuidmap;
 
-    rc = kill_device_model(gc, pid_path);
-    if (rc)
-        goto out;
+    libxl__dm_emuidmap_get(gc, domid, &emuidmap);
+    /* better to blunder on if this fails */
 
-    libxl__xs_rm_checked(gc, XBT_NULL, pid_path);
-    libxl__xs_rm_checked(gc, XBT_NULL,
-                         libxl__dm_xs_path_rel(gc, domid, EMUID_PV));
-
-out:
-    return rc;
+    return libxl__destroy_device_model(gc, domid, emuidmap, EMUID_PV);
 }
 
 int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid,
@@ -2123,16 +2114,16 @@ int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid,
     if (!(emuidmap & (1u << emuid)))
         return 0;
 
-    char *path = libxl__device_model_xs_path(gc, LIBXL_TOOLSTACK_DOMID,
-                                             domid, emuid, "");
-    if (!xs_rm(CTX->xsh, XBT_NULL, path))
-        LOG(ERROR, "xs_rm failed for %s", path);
+    const char *control_path = libxl__dm_xs_path_rel(gc, domid, emuid);
+    libxl__xs_rm_checked(gc, XBT_NULL, control_path);
 
     /* We should try to destroy the device model anyway. */
-    rc = kill_device_model(gc, libxl__dm_xs_pid_path(gc, domid, emuid));
+    const char *pid_path = libxl__dm_xs_pid_path(gc, domid, emuid);
+    rc = kill_device_model(gc, pid_path);
     if (rc)
         LOG(ERROR, "libxl__destroy_device_model failed for %d (emuid=%d)",
             domid, emuid);
+    libxl__xs_rm_checked(gc, XBT_NULL, pid_path);
 
     libxl__qmp_cleanup(gc, domid);
     return rc;
Signature · 1 line
1.7.10.4
#29 lore raw

PATCH v6 20/28 libxl: domcreate_dm_support_checked: Introduce `goto out'

In reply to Ian Jackson

We are going to want this shortly.

No functional change.

---
v6: New patch.

---
 tools/libxl/libxl_create.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/libxl/libxl_create.c +5 -4
index 71893c5..b41c6bd 100644
@@ -968,10 +968,7 @@ static void domcreate_dm_support_checked(libxl__egc *egc,
     libxl_domain_config *const d_config = dcs->guest_config;
     const int restore_fd = dcs->restore_fd;
 
-    if (rc) {
-        domcreate_complete(egc, dcs, rc);
-        return;
-    }
+    if (rc) goto out;
 
     dcs->bl.ao = ao;
     libxl_device_disk *bootdisk =
@@ -994,6 +991,10 @@ static void domcreate_dm_support_checked(libxl__egc *egc,
         libxl__bootloader_run(egc, &dcs->bl);
     }
     return;
+
+ out:
+    domcreate_complete(egc, dcs, rc);
+    return;
 }
 
 static void domcreate_bootloader_console_available(libxl__egc *egc,
Signature · 1 line
1.7.10.4
#30 lore raw

PATCH v6 21/28 libxl: dm user: Reject attempts to set user!=root with qemu trad

In reply to Ian Jackson

Previously this option would be silently ignored, which is a potential security problem (introduced in 84f2fd1b "run QEMU as non-root" in xen-unstable only).

CC Stefano Stabellini <[email protected]>

---
v6: New patch.

---
 tools/libxl/libxl_dm.c |    8 ++++++++
 1 file changed, 8 insertions(+)
tools/libxl/libxl_dm.c +8 -0
index 886ed9c..8232981 100644
@@ -415,6 +415,14 @@ static int libxl__build_device_model_args_old(libxl__gc *gc,
     dm_args = flexarray_make(gc, 16, 1);
     dm_envs = flexarray_make(gc, 16, 1);
 
+    if (b_info->device_model_user && /* default is NULL if stubdom */
+        strcmp(b_info->device_model_user,"root")) {
+        LOG(ERROR,
+ "device_model_user != root (%s) not supported by qemu-xen-traditional",
+            b_info->device_model_user);
+        return ERROR_INVAL;
+    }
+
     flexarray_vappend(dm_args, dm,
                       "-d", GCSPRINTF("%d", domid), NULL);
 
Signature · 1 line
1.7.10.4
Acked-by on v6 21/28
#31 lore raw

Re: [PATCH 21/28] libxl: dm user: Reject attempts to set user!=root with qemu trad

Quoted 6 lines · Ian Jackson

Previously this option would be silently ignored, which is a potential security problem (introduced in 84f2fd1b "run QEMU as non-root" in xen-unstable only).

CC Stefano Stabellini <[email protected]>

(could/should go in now despite RFC-ness of the series as a whole, assuming it is as independent as it looks, we really don't want to forget this for 4.7 if the other 27 patches take longer to land)

Quoted 18 lines

---
v6: New patch.
---
 tools/libxl/libxl_dm.c |    8 ++++++++

 1 file changed, 8 insertions(+)
tools/libxl/libxl_dm.c +0 -0
index 886ed9c..8232981 100644
@@ -415,6 +415,14 @@ static int
 libxl__build_device_model_args_old(libxl__gc *gc,
      dm_args = flexarray_make(gc, 16, 1);
      dm_envs = flexarray_make(gc, 16, 1);


+    if (b_info->device_model_user && /* default is NULL if stubdom */ +        strcmp(b_info->device_model_user,"root")) {
+        LOG(ERROR,

Quoted 8 lines

+ "device_model_user != root (%s) not supported by qemu-xen-traditional", +            b_info->device_model_user);
+        return ERROR_INVAL;
+    }
+
     flexarray_vappend(dm_args, dm,
                       "-d", GCSPRINTF("%d", domid), NULL);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#32 lore raw

Re: [PATCH 21/28] libxl: dm user: Reject attempts to set user!=root with qemu trad

Ian Campbell writes ("Re: [PATCH 21/28] libxl: dm user: Reject attempts to set user!=root with qemu trad"):

Quoted 5 lines · Ian Jackson
Quoted 4 lines

Previously this option would be silently ignored, which is a potential security problem (introduced in 84f2fd1b "run QEMU as non-root" in xen-unstable only).

Quoted 7 lines
Quoted 2 lines
CC Stefano Stabellini <[email protected]>

(could/should go in now despite RFC-ness of the series as a whole, assuming it is as independent as it looks, we really don't want to forget this for 4.7 if the other 27 patches take longer to land)

Well, it moved easily to earlier in the series, so I have done that. But there are other things wrong with 84f2fd1b which are sorted out here, too. I think if this series doesn't make 4.7 we may need to revert 84f2fd1b, or at least consider other parts of this series to cherry pick.

Thanks,
Ian.

#33 lore raw

Re: [PATCH 21/28] libxl: dm user: Reject attempts to set user!=root with qemu trad

Quoted 18 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 21/28] libxl: dm user: Reject attempts to set user!=root with qemu trad"):

Quoted 9 lines · Ian Jackson
Quoted 7 lines

Previously this option would be silently ignored, which is a potential
security problem (introduced in 84f2fd1b "run QEMU as non-root" in xen-unstable only).

CC Stefano Stabellini <[email protected]>
Quoted 6 lines

(could/should go in now despite RFC-ness of the series as a whole, assuming
it is as independent as it looks, we really don't want to forget this for
4.7 if the other 27 patches take longer to land)

Quoted 5 lines

Well, it moved easily to earlier in the series, so I have done that. But there are other things wrong with 84f2fd1b which are sorted out here, too.  I think if this series doesn't make 4.7 we may need to revert 84f2fd1b, or at least consider other parts of this series to cherry pick.

Makes sense, I assume you'll either propose that revert of a suitable list of cherry-picks if/when the time comes.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#34 lore raw

PATCH v6 22/28 libxl: dm user: Document the default

In reply to Ian Jackson
CC Stefano Stabellini <[email protected]>

---
v6: New patch

---
 docs/man/xl.cfg.pod.5 |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
docs/man/xl.cfg.pod.5 +2 -1
index 8899f75..dd93725 100644
@@ -1839,7 +1839,8 @@ option to the device-model.
 =item B<device_model_user="username">
 
 Run the device model as user "username", instead of
-xen-qemudepriv-domid$domid or xen-qemudepriv-shared or root.
+xen-qemudepriv-domid$domid or xen-qemudepriv-shared or root.  The
+default is to use the first of these users which exists.
 
 =back
 
Signature · 1 line
1.7.10.4
#36 lore raw

PATCH v6 23/28 libxl: dm user: Move user choice earlier, and fill in config

In reply to Ian Jackson

Move the dm user defaulting from libxl__build_device_model_args_new earlier in domain create. We now do it just after the dm support check has finished.

Convey the result to the dm args constructor in the b_info, ie in the supplied config. So, the application's supplied config is filled in with the chosen user, as for other defaults.

Also:
- slightly improve one of the warning messages
- change the moved code to use rc' (following CODING_STYLE) rather than ret'
- change the moved code to use domid' rather than guest_domid' like most of the rest of domain create
- change the name of the (static) user selection function.

CC Stefano Stabellini <[email protected]>

---
v6: New patch

---
 tools/libxl/libxl_create.c |   84 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_dm.c     |   62 ++------------------------------
 2 files changed, 86 insertions(+), 60 deletions(-)
tools/libxl/libxl_create.c +84 -0
index b41c6bd..be9bb7b 100644
@@ -20,6 +20,8 @@
 #include "libxl_internal.h"
 #include "libxl_arch.h"
 
+#include <pwd.h>
+
 #include <xc_dom.h>
 #include <xenguest.h>
 #include <xen/hvm/hvm_info_table.h>
@@ -388,6 +390,83 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
     return 0;
 }
 
+/* return 1 if the user was found, 0 if it was not, -1 on error */
+static int dm_runas_helper(libxl__gc *gc, const char *username)
+{
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
+    int ret;
+
+    buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (buf_size < 0) {
+        LOGE(ERROR, "sysconf(_SC_GETPW_R_SIZE_MAX) returned error %ld",
+                buf_size);
+        return ERROR_FAIL;
+    }
+
+    while (1) {
+        buf = libxl__realloc(gc, buf, buf_size);
+        ret = getpwnam_r(username, &pwd, buf, buf_size, &user);
+        if (ret == ERANGE) {
+            buf_size += 128;
+            continue;
+        }
+        if (ret != 0)
+            return ERROR_FAIL;
+        if (user != NULL)
+            return 1;
+        return 0;
+    }
+}
+
+static int domcreate_setdefault_dm_user(libxl__gc *gc,
+                                        libxl__domain_create_state *dcs)
+{
+    /* convenience aliases */
+    libxl_domain_config *const d_config = dcs->guest_config;
+    libxl_domain_build_info *const b_info = &d_config->b_info;
+    const uint32_t domid = dcs->guest_domid;
+
+    int rc;
+    const char *user;
+
+    if (b_info->device_model_user)
+        /* already set, good-oh */
+        return 0;
+
+    if (!(b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+          !libxl_defbool_val(b_info->device_model_stubdomain)))
+        /* we're not going to run it anyway */
+        return 0;
+
+    user = GCSPRINTF("%s%d", LIBXL_QEMU_USER_BASE, domid);
+
+    rc = dm_runas_helper(gc, user);
+    if (rc < 0) goto error;
+    if (rc > 0) goto found;
+
+    user = LIBXL_QEMU_USER_SHARED;
+    rc = dm_runas_helper(gc, user);
+    if (rc < 0) goto error;
+    if (rc > 0) {
+        LOG(WARN, "Could not find user %s%d, falling back to %s",
+            LIBXL_QEMU_USER_BASE, domid, LIBXL_QEMU_USER_SHARED);
+        goto found;
+    }
+
+    LOG(WARN, "Could not find user %s%d or %s, starting QEMU as root",
+        LIBXL_QEMU_USER_BASE, domid, LIBXL_QEMU_USER_SHARED);
+    user = "root";
+
+ found:
+    b_info->device_model_user = libxl__strdup(NOGC, user);
+    return 0;
+
+ error:
+    return rc;
+}
+
 static void init_console_info(libxl__gc *gc,
                              libxl__device_console *console,
                              int dev_num)
@@ -970,6 +1049,11 @@ static void domcreate_dm_support_checked(libxl__egc *egc,
 
     if (rc) goto out;
 
+    /* config defaults which depend on dm support etc. */
+
+    rc = domcreate_setdefault_dm_user(gc, dcs);
+    if (rc) goto out;
+
     dcs->bl.ao = ao;
     libxl_device_disk *bootdisk =
         d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
tools/libxl/libxl_dm.c +2 -60

89 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#37 lore raw

PATCH v6 24/28 libxl: dm spawn records rc in state struct rather than passing as argument

In reply to Ian Jackson

This is going to be much more convenient in a moment, when one of the call sites is going to want to run two in parallel.

No functional change.

---
v6: New patch.

---
 tools/libxl/libxl.c          |    4 ++--
 tools/libxl/libxl_create.c   |   14 +++++++-------
 tools/libxl/libxl_dm.c       |   22 +++++++++++++---------
 tools/libxl/libxl_internal.h |    5 +++--
 4 files changed, 25 insertions(+), 20 deletions(-)
tools/libxl/libxl.c +2 -2
index dff4478..3bb6d3b 100644
@@ -4345,10 +4345,10 @@ static void device_complete(libxl__egc *egc, libxl__ao_device *aodev)
     libxl__nested_ao_free(aodev->ao);
 }
 
-static void qdisk_spawn_outcome(libxl__egc *egc, libxl__dm_spawn_state *dmss,
-                                int rc)
+static void qdisk_spawn_outcome(libxl__egc *egc, libxl__dm_spawn_state *dmss)
 {
     STATE_AO_GC(dmss->spawn.ao);
+    int rc = dmss->rc;
 
     LOG(DEBUG, "qdisk backend spawn for domain %u %s", dmss->guest_domid,
                rc ? "failed" : "succeed");
tools/libxl/libxl_create.c +7 -7
index be9bb7b..e67e402 100644
@@ -803,8 +803,7 @@ static void remus_checkpoint_stream_done(
 static void domcreate_dm_support_checked(libxl__egc *egc,
     libxl__dm_support_check_state *checking, int rc);
 static void domcreate_devmodel_started(libxl__egc *egc,
-                                       libxl__dm_spawn_state *dmss,
-                                       int rc);
+                                       libxl__dm_spawn_state *dmss);
 static void domcreate_bootloader_console_available(libxl__egc *egc,
                                                    libxl__bootloader_state *bl);
 static void domcreate_bootloader_done(libxl__egc *egc,
@@ -1387,8 +1386,8 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
 
         if (d_config->b_info.device_model_version ==
             LIBXL_DEVICE_MODEL_VERSION_NONE) {
-            domcreate_devmodel_started(egc, &dcs->dmss.dm, 0);
-            return;
+            dcs->dmss.dm.rc = 0;
+            domcreate_devmodel_started(egc, &dcs->dmss.dm);
         }
 
         libxl_device_vkb_init(&vkb);
@@ -1440,7 +1439,8 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
             return;
         } else {
             assert(!dcs->dmss.dm.guest_domid);
-            domcreate_devmodel_started(egc, &dcs->dmss.dm, 0);
+            dcs->dmss.dm.rc = 0;
+            domcreate_devmodel_started(egc, &dcs->dmss.dm);
             return;
         }
     }
@@ -1456,11 +1456,11 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
 }
 
 static void domcreate_devmodel_started(libxl__egc *egc,
-                                       libxl__dm_spawn_state *dmss,
-                                       int ret)
+                                       libxl__dm_spawn_state *dmss)
 {
     libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.dm);
     STATE_AO_GC(dmss->spawn.ao);
+    int ret = dmss->rc;
     int domid = dcs->guest_domid;
 
     /* convenience aliases */
tools/libxl/libxl_dm.c +13 -9
index 5e1a6cf..58760e7 100644
@@ -1369,8 +1369,7 @@ retry_transaction:
 }
 
 static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
-                                libxl__dm_spawn_state *stubdom_dmss,
-                                int rc);
+                                libxl__dm_spawn_state *stubdom_dmss);
 
 static void spawn_stub_launch_dm(libxl__egc *egc,
                                  libxl__multidev *aodevs, int ret);
@@ -1534,7 +1533,8 @@ retry_transaction:
 
 out:
     assert(ret);
-    spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret);
+    sdss->pvqemu.rc = ret;
+    spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu);
 }
 
 static void spawn_stub_launch_dm(libxl__egc *egc,
@@ -1632,16 +1632,17 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
 
 out:
     assert(ret);
-    spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret);
+    sdss->pvqemu.rc = ret;
+    spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu);
 }
 
 static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
-                                libxl__dm_spawn_state *stubdom_dmss,
-                                int rc)
+                                libxl__dm_spawn_state *stubdom_dmss)
 {
     libxl__stub_dm_spawn_state *sdss =
         CONTAINER_OF(stubdom_dmss, *sdss, pvqemu);
     STATE_AO_GC(sdss->dm.spawn.ao);
+    int rc = stubdom_dmss->rc;
     uint32_t dm_domid = sdss->pvqemu.guest_domid;
     libxl_domain_config *d_config = stubdom_dmss->guest_config;
 
@@ -1712,7 +1713,8 @@ static void stubdom_xswait_cb(libxl__egc *egc, libxl__xswait_state *xswait,
         return;
  out:
     libxl__xswait_stop(gc, xswait);
-    sdss->callback(egc, &sdss->dm, rc);
+    sdss->dm.rc = rc;
+    sdss->callback(egc, &sdss->dm);
 }
 
 /* callbacks passed to libxl__spawn_spawn */
@@ -1929,7 +1931,8 @@ static void device_model_spawn_outcome(libxl__egc *egc,
     }
 
  out:
-    dmss->callback(egc, dmss, rc);
+    dmss->rc = rc;
+    dmss->callback(egc, dmss);
 }
 
 void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
@@ -2005,7 +2008,8 @@ error:
     assert(rc);
     if (logfile_w >= 0) close(logfile_w);
     if (null >= 0) close(null);
-    dmss->callback(egc, dmss, rc);
+    dmss->rc = rc;
+    dmss->callback(egc, dmss);
     return;
 }
 
tools/libxl/libxl_internal.h +3 -2
index 350ec29..82f264a 100644
@@ -3413,12 +3413,13 @@ _hidden void libxl__add_vtpms(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
 
 typedef struct libxl__dm_spawn_state libxl__dm_spawn_state;
 
-typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*,
-                                int rc /* if !0, error was logged */);
+typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*);
 
 struct libxl__dm_spawn_state {
     /* mixed - spawn.ao must be initialised by user; rest is private: */
     libxl__spawn_state spawn;
+    /* filled in by dm spawn just before making callback */
+    int rc; /* if !0, error was logged */
     /* filled in by user, must remain valid: */
     uint32_t guest_domid; /* domain being served */
     libxl_domain_config *guest_config;
Signature · 1 line
1.7.10.4
#38 lore raw

PATCH v6 25/28 libxl: emuids: Perhaps change dm xs control path

In reply to Ian Jackson

We are going to want to run two qemus, which will mean them having different xs control paths. But sometimes we will run only one qemu to do both jobs, in which case there has to be one xs control path, because otherwise the single qemu will only see in xenstore either the HVM DM work to do, or the PV (eg qdisk) backend work to do.

So we need to record whether any particular domain has been set up this way. Use a new emuid flag bit EMUID_SPLIT which just tells us whether we are running two qemus in this way.

If we do set this flag, we pass the emulator_id to qemu, so that qemu and libxl agree on the xenstore path. Obviously this depends on qemu understanding emulator_id, but we don't check that now, because:

We do not actually ever set EMUID_SPLIT, because when we run split qemus we also need to actually run two qemus which means a lot of non-xs-path-related changes which are more readable in a separate patch.

So right now there is no overall functional change.

---
v6: Rewritten.

---
 docs/misc/xenstore-paths.markdown |   11 +++++++++++
 tools/libxl/libxl_dm.c            |   15 +++++++++++++++
 tools/libxl/libxl_internal.c      |   16 +++++++++++++++-
 tools/libxl/libxl_internal.h      |    1 +
 4 files changed, 42 insertions(+), 1 deletion(-)
docs/misc/xenstore-paths.markdown +11 -0
index 3bd31af..1dc54ac 100644
@@ -339,6 +339,17 @@ A PV console backend. Described in [console.txt](console.txt)
 Information relating to device models running in the domain. $DOMID is
 the target domain of the device model.
 
+#### ~/device-model/$DOMID[/$EMULATOR_ID]/* [INTERNAL]
+
+Information relating to device models running in the domain. $DOMID is
+the target domain of the device model.
+
+$EMULATOR_ID indentifies a specific device model instance (multiple
+device model instances for the same domain are possible).  This path
+component is present only if there are (going to be) more than one
+device model for the domain.  See `/libxl/$DOMID/dm-emuidmap` and
+`EMUID_SPLIT` in `libxl_internal.h`.
+
 #### ~/libxl/disable_udev = ("1"|"0") []
 
 Indicates whether device hotplug scripts in this domain should be run
tools/libxl/libxl_dm.c +15 -0
index 58760e7..d805800 100644
@@ -1236,6 +1236,11 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
             flexarray_append(dm_args, "-runas");
             flexarray_append(dm_args, user);
         }
+        if (state->emuidmap & (1u << EMUID_SPLIT)) {
+            flexarray_append(dm_args, "-xenopts");
+            flexarray_append(dm_args,
+                             GCSPRINTF("emulator_id=%u", emuid));
+        }
     }
     flexarray_append(dm_args, NULL);
     *args = (char **) flexarray_contents(dm_args);
@@ -1943,6 +1948,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     const char *dm;
     int logfile_w, null = -1, rc;
     uint32_t domid = dmss->guest_domid;
+    unsigned emuidmap;
 
     /* Always use qemu-xen as device model */
     dm = qemu_xen_path(gc);
@@ -1958,6 +1964,15 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     flexarray_vappend(dm_args, "-monitor", "/dev/null", NULL);
     flexarray_vappend(dm_args, "-serial", "/dev/null", NULL);
     flexarray_vappend(dm_args, "-parallel", "/dev/null", NULL);
+
+    rc = libxl__dm_emuidmap_get(gc, domid, &emuidmap);
+    if (rc) goto error;
+
+    if (emuidmap & (1u << EMUID_SPLIT)) {
+        flexarray_append(dm_args, "-xenopts");
+        flexarray_append(dm_args,
+                GCSPRINTF("emulator_id=%u", EMUID_PV));
+    }
     flexarray_append(dm_args, NULL);
     args = (char **) flexarray_contents(dm_args);
 
tools/libxl/libxl_internal.c +15 -1
index 30271c9..8dc34ad 100644
@@ -556,7 +556,21 @@ void libxl__update_domain_configuration(libxl__gc *gc,
 char *libxl__dm_xs_path_rel(libxl__gc *gc,
                             uint32_t domid, int emuid)
 {
-    return GCSPRINTF("device-model/%u", domid);
+    unsigned emuidmap;
+
+    libxl__dm_emuidmap_get_bodgeerrors(gc, domid, &emuidmap);
+
+    if (!(emuidmap & (1u << emuid))) {
+        LOG(ERROR,
+ "Trying to find path for emulator %d but map is %#x, probable BUG",
+            emuid, emuidmap);
+    }
+
+    if (!(emuidmap & (1u << EMUID_SPLIT))) {
+        return GCSPRINTF("device-model/%u", domid);
+    } else {
+        return GCSPRINTF("device-model/%u/%u", domid, emuid);
+    }
 }
 
 char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,
tools/libxl/libxl_internal.h +1 -0
index 82f264a..02c35e0 100644
@@ -1962,6 +1962,7 @@ _hidden libxl_device_model_version libxl__default_device_model(libxl__gc *gc);
 enum {
     EMUID_PV,
     EMUID_DM,
+    EMUID_SPLIT, /* we will run both PV and DM, put emuid in xs path */
     /* NB stubdom and its PV service domain not recorded here */
 };
 
Signature · 1 line
1.7.10.4
#39 lore raw

Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path

Quoted 9 lines · Ian Jackson

We are going to want to run two qemus, which will mean them having different xs control paths.  But sometimes we will run only one qemu to do both jobs, in which case there has to be one xs control path, because otherwise the single qemu will only see in xenstore either the HVM DM work to do, or the PV (eg qdisk) backend work to do.

So we need to record whether any particular domain has been set up this way.  Use a new emuid flag bit EMUID_SPLIT which just tells us whether we are running two qemus in this way.

So, do I understand correctly that the states (combinations of bits) are:

0 No QEMUs
PV Single QEMU only providing PV backends
DM Single QEMU only providing device emulation
PV|DM Single QEMU providing PV backends and device emulation

SPLIT|PV|DM	A pair of QEMUs, one for PV b/e and one for device emu
SPLIT|PV	Nonsense
SPLIT|DM	Nonsense	

?

Describing SPLIT as an "ID" is a bit odd (it's a kind of meta thing) but I suppose I can see why it is done this way. An alternative might be to have a separate bit for the PV|DM case together, maybe you discarded that possibility though?

Ian.

Quoted 130 lines

If we do set this flag, we pass the emulator_id to qemu, so that qemu and libxl agree on the xenstore path.  Obviously this depends on qemu understanding emulator_id, but we don't check that now, because:

We do not actually ever set EMUID_SPLIT, because when we run split qemus we also need to actually run two qemus which means a lot of non-xs-path-related changes which are more readable in a separate patch.

So right now there is no overall functional change.

---
v6: Rewritten.
---
 docs/misc/xenstore-paths.markdown |   11 +++++++++++ tools/libxl/libxl_dm.c            |   15 +++++++++++++++ tools/libxl/libxl_internal.c      |   16 +++++++++++++++- tools/libxl/libxl_internal.h      |    1 +

 4 files changed, 42 insertions(+), 1 deletion(-)
docs/misc/xenstore- renamed from docs/misc/xenstore-paths.markdown +0 -0

paths.markdown
index 3bd31af..1dc54ac 100644

docs/misc/xenstore-paths.markdown +0 -0
@@ -339,6 +339,17 @@ A PV console backend. Described in
 [console.txt](console.txt)

 Information relating to device models running in the domain. $DOMID is the target domain of the device model.

docs/misc/xenstore-paths.markdown +11 -0
+#### ~/device-model/$DOMID[/$EMULATOR_ID]/* [INTERNAL]
+
+Information relating to device models running in the domain. $DOMID is
+the target domain of the device model.
+
+$EMULATOR_ID indentifies a specific device model instance (multiple
+device model instances for the same domain are possible).  This path
+component is present only if there are (going to be) more than one
+device model for the domain.  See `/libxl/$DOMID/dm-emuidmap` and
+`EMUID_SPLIT` in `libxl_internal.h`.
+

 #### ~/libxl/disable_udev = ("1"|"0") []

 Indicates whether device hotplug scripts in this domain should be run

tools/libxl/libxl_dm.c +5 -0
index 58760e7..d805800 100644
@@ -1236,6 +1236,11 @@ static int
 libxl__build_device_model_args_new(libxl__gc *gc,
              flexarray_append(dm_args, "-runas");
              flexarray_append(dm_args, user);
          }
+        if (state->emuidmap & (1u << EMUID_SPLIT)) {
+            flexarray_append(dm_args, "-xenopts");
+            flexarray_append(dm_args,
+                             GCSPRINTF("emulator_id=%u", emuid));
+        }
      }
      flexarray_append(dm_args, NULL);

     *args = (char **) flexarray_contents(dm_args);

tools/libxl/libxl_dm.c +1 -0
@@ -1943,6 +1948,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc,
 libxl__dm_spawn_state *dmss)
      const char *dm;
      int logfile_w, null = -1, rc;
      uint32_t domid = dmss->guest_domid;
+    unsigned emuidmap;


     /* Always use qemu-xen as device model */
     dm = qemu_xen_path(gc);

tools/libxl/libxl_dm.c +9 -0
@@ -1958,6 +1964,15 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc,
 libxl__dm_spawn_state *dmss)
      flexarray_vappend(dm_args, "-monitor", "/dev/null", NULL);
      flexarray_vappend(dm_args, "-serial", "/dev/null", NULL);
      flexarray_vappend(dm_args, "-parallel", "/dev/null", NULL);
+
+    rc = libxl__dm_emuidmap_get(gc, domid, &emuidmap);
+    if (rc) goto error;
+
+    if (emuidmap & (1u << EMUID_SPLIT)) {
+        flexarray_append(dm_args, "-xenopts");
+        flexarray_append(dm_args,
+                GCSPRINTF("emulator_id=%u", EMUID_PV));
+    }
      flexarray_append(dm_args, NULL);
      args = (char **) flexarray_contents(dm_args);

tools/libxl/libxl_internal.c +15 -1
index 30271c9..8dc34ad 100644
@@ -556,7 +556,21 @@ void libxl__update_domain_configuration(libxl__gc
 *gc,
  char *libxl__dm_xs_path_rel(libxl__gc *gc,
                              uint32_t domid, int emuid)
  {
-    return GCSPRINTF("device-model/%u", domid);
+    unsigned emuidmap;
+
+    libxl__dm_emuidmap_get_bodgeerrors(gc, domid, &emuidmap);
+
+    if (!(emuidmap & (1u << emuid))) {
+        LOG(ERROR,
+ "Trying to find path for emulator %d but map is %#x, probable BUG",
+            emuid, emuidmap);
+    }
+
+    if (!(emuidmap & (1u << EMUID_SPLIT))) {
+        return GCSPRINTF("device-model/%u", domid);
+    } else {
+        return GCSPRINTF("device-model/%u/%u", domid, emuid);
+    }
  }


 char *libxl__device_model_xs_path(libxl__gc *gc, uint32_t dm_domid,

tools/libxl/libxl_internal.h +1 -0
index 82f264a..02c35e0 100644
@@ -1962,6 +1962,7 @@ _hidden libxl_device_model_version
 libxl__default_device_model(libxl__gc *gc);
  enum {
      EMUID_PV,
      EMUID_DM,
+    EMUID_SPLIT, /* we will run both PV and DM, put emuid in xs path */
      /* NB stubdom and its PV service domain not recorded here */
  };
Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#40 lore raw

Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path

Ian Campbell writes ("Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path"):

Quoted 1 line

So, do I understand correctly that the states (combinations of bits) are:

No.

Quoted 4 lines

Describing SPLIT as an "ID" is a bit odd (it's a kind of meta thing) but I suppose I can see why it is done this way. An alternative might be to have a separate bit for the PV|DM case together, maybe you discarded that possibility though?

Perhaps things will be clearer with this comment, which I've just added to add in this patch:

/*
* This EMUID enum is used for several overlapping purposes. *
* Each qemu process has an EMUID, which is either EMUID_PV or
* EMUID_DM.
*
* Operations which talk to a qemu process need to know its emuid so
* that they talk to the right one. (Specifically, so that they find
* the right place in xenstore.) Likewise a qemu process needs to
* be told its emuid for the same reason.
*
* However, qemut and older versions of qemuu do not support emuids.
* In that case for an HVM domain, the same qemu process deals with
* both roles. In this situation the xenstore paths for PV backend
* control do not include the emuid, and therefore we can talk to the
* single qemu via' both' emuids.
*
* In xenstore we record which qemus a domain has. This is necessary
* for correct teardown. And, we also record whether the xenstore
* paths are unified, as discussed above, so that subsequent libxl
* operations can do the right thing. This is what the EMUID_SPLIT
* flag is for.
*
* Overall, the following scenarios are possible:
*
* dm-emuidmap libxl__device_model_xs_path

  *                                         can be passed      control xs paths
  *                                          which emuids?     contain what
  *                                                            to distinguish
  *
  *   0            No QEMU processes               PV             -
  *   PV           PV domain                       PV             -
  *   PV           HVM domain with stub dm         PV; HVM        stubdomid
  *      DM        HVM domain, 1 QEMU              PV; HVM        -
  *   PV|DM|SPLIT  HVM domain, 2 QEMUs privsep     PV; HVM        emuid
  *
  * The following scenarios would be handled correctly but are not set up
  * by current libxl code, and/or are implausible for other reasons:
  *
  *   0            Stub dm only                    HVM            stubdomid
  *   PV|SPLIT     PV domain                       PV             emuid

*/

Ian.

#41 lore raw

Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path

Quoted 14 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path"):

Quoted 2 lines

So, do I understand correctly that the states (combinations of bits) are:

No.

Quoted 6 lines

Describing SPLIT as an "ID" is a bit odd (it's a kind of meta thing) but I
suppose I can see why it is done this way. An alternative might be to have
a separate bit for the PV|DM case together, maybe you discarded that possibility though?

Quoted 2 lines

Perhaps things will be clearer with this comment, which I've just added to add in this patch:

Much thanks. One nit:

Quoted 6 lines


  * In xenstore we record which qemus a domain has.  This is necessary
  * for correct teardown.  And, we also record whether the xenstore
  * paths are unified, as discussed above, so that subsequent libxl
  * operations can do the right thing.  This is what the EMUID_SPLIT
  * flag is for.

EMUID_SPLIT actually records the inverse (i.e. that they are split, rather than unified as the previous text is discussing).

I knew what you meant though, and clarifying it could well make it less obvious I suspect.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#42 lore raw

Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path

Ian Campbell writes ("Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path"):

Quoted 7 lines · Ian Jackson
Quoted 5 lines

  * In xenstore we record which qemus a domain has.  This is necessary
  * for correct teardown.  And, we also record whether the xenstore
  * paths are unified, as discussed above, so that subsequent libxl
  * operations can do the right thing.  This is what the EMUID_SPLIT
  * flag is for.

Quoted 5 lines

EMUID_SPLIT actually records the inverse (i.e. that they are split, rather than unified as the previous text is discussing).

I knew what you meant though, and clarifying it could well make it less obvious I suspect.

Yes. I could explain the reason for the sense of the flag, which is to get the default right, but that seems obvious.

How about:

Quoted 3 lines
Quoted 3 lines

  * In xenstore we record which qemus a domain has.  This is
  * necessary for correct teardown.  And, we also record whether
  * the xenstore paths are split or unified, as discussed above,

                               ++++++++
Quoted 2 lines
Quoted 2 lines

  * so that subsequent libxl operations can do the right
  * thing.  This is what the EMUID_SPLIT flag is for.

?

Ian.

#43 lore raw

Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path

Quoted 22 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 25/28] libxl: emuids: Perhaps change dm xs control path"):

Quoted 8 lines · Ian Jackson
Quoted 6 lines

  * In xenstore we record which qemus a domain has.  This is necessary
  * for correct teardown.  And, we also record whether the xenstore
  * paths are unified, as discussed above, so that subsequent libxl
  * operations can do the right thing.  This is what the EMUID_SPLIT
  * flag is for.

Quoted 6 lines

EMUID_SPLIT actually records the inverse (i.e. that they are split, rather
than unified as the previous text is discussing).

I knew what you meant though, and clarifying it could well make it less obvious I suspect.

Yes.  I could explain the reason for the sense of the flag, which is to get the default right, but that seems obvious.

How about:

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

  * In xenstore we record which qemus a domain has.  This is
  * necessary for correct teardown.  And, we also record whether
  * the xenstore paths are split or unified, as discussed above,

                               ++++++++

Quoted 2 lines
Quoted 2 lines

  * so that subsequent libxl operations can do the right
  * thing.  This is what the EMUID_SPLIT flag is for.

?

Looks good, thanks.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#44 lore raw

PATCH v6 26/28 libxl: spawns two QEMUs for HVM guests

In reply to Ian Jackson

From: Stefano Stabellini <[email protected]>

We actually need to spawn a second QEMU if we want to run qemu as non-root, because the pv backends ought to run as root (or device hotplug may not work).

So in this case, start a second QEMU to provide PV backends in userspace to HVM guests. Use both dcs->dmss.pvqemu and dcs->dmss.dm to keep track of the starting QEMUs.

Only proceed when both QEMUs have started.

And, we only default to running QEMU as non-root if we are going to be able to run split qemus. In particular, it is not safe to run split qemus if they don't support the emulator_id option, because we need to split the xenstore paths too.

---
v6: Split only if trying to run a qemu as non-root
Reorganise changes to dm callbacks
No more dcs in dmss
Use min() to calculate worst rc Explicitly set unused fields of dmss.pvqemu to 0 Change error handling

v3: use dcs->dmss.pvqemu to spawn the second QEMU
keep track of the rc of both QEMUs before proceeding

---
 tools/libxl/libxl_create.c |   72 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
tools/libxl/libxl_create.c +72 -0
index e67e402..59dfcd67 100644
@@ -431,6 +431,8 @@ static int domcreate_setdefault_dm_user(libxl__gc *gc,
     int rc;
     const char *user;
 
+    const char *dm = libxl__domain_device_model(gc, b_info);
+
     if (b_info->device_model_user)
         /* already set, good-oh */
         return 0;
@@ -440,6 +442,15 @@ static int domcreate_setdefault_dm_user(libxl__gc *gc,
         /* we're not going to run it anyway */
         return 0;
 
+    if (!libxl__dm_supported(gc, dm, libxl__dm_support_check__emulator_id)) {
+        /* we don't want to run the pv backends as non-root because
+         * device hotplug will no longer work. */
+        LOG(WARN,
+ "Device model does not support split PV backends, running it as root");
+        user = "root";
+        goto found;
+    }
+
     user = GCSPRINTF("%s%d", LIBXL_QEMU_USER_BASE, domid);
 
     rc = dm_runas_helper(gc, user);
@@ -802,6 +813,14 @@ static void remus_checkpoint_stream_done(
 /* Event callbacks, in this order: */
 static void domcreate_dm_support_checked(libxl__egc *egc,
     libxl__dm_support_check_state *checking, int rc);
+
+static void domcreate_dm_local_split_pv_cb(libxl__egc *egc,
+                                           libxl__dm_spawn_state *dmss);
+static void domcreate_dm_local_split_dm_cb(libxl__egc *egc,
+                                           libxl__dm_spawn_state *dmss);
+static void domcreate_dm_local_split_cb(libxl__egc *egc,
+                                        libxl__domain_create_state *dcs);
+
 static void domcreate_devmodel_started(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss);
 static void domcreate_bootloader_console_available(libxl__egc *egc,
@@ -1044,6 +1063,9 @@ static void domcreate_dm_support_checked(libxl__egc *egc,
     
     /* convenience aliases */
     libxl_domain_config *const d_config = dcs->guest_config;
+    const libxl_domain_build_info *b_info = &d_config->b_info;
+    libxl__domain_build_state *const b_state = &dcs->build_state;
+    const uint32_t domid = dcs->guest_domid;
     const int restore_fd = dcs->restore_fd;
 
     if (rc) goto out;
@@ -1053,6 +1075,15 @@ static void domcreate_dm_support_checked(libxl__egc *egc,
     rc = domcreate_setdefault_dm_user(gc, dcs);
     if (rc) goto out;
 
+    /* run two qemus? */
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM &&
+        !libxl_defbool_val(d_config->b_info.device_model_stubdomain) &&
+        b_info->device_model_user &&
+        strcmp(b_info->device_model_user, "root")) {
+        rc = libxl__dm_emuidmap_add(gc, domid, b_state, EMUID_SPLIT);
+        if (rc) goto out;
+    }
+
     dcs->bl.ao = ao;
     libxl_device_disk *bootdisk =
         d_config->num_disks > 0 ? &d_config->disks[0] : NULL;
@@ -1130,6 +1161,11 @@ static void domcreate_bootloader_done(libxl__egc *egc,
     dcs->dmss.dm.callback = domcreate_devmodel_started;
     dcs->dmss.callback = domcreate_devmodel_started;
 
+    dcs->dmss.pvqemu.spawn.ao = ao;
+    dcs->dmss.pvqemu.guest_domid = domid;
+    dcs->dmss.pvqemu.guest_config = 0;
+    dcs->dmss.pvqemu.build_state = 0;
+
     if (restore_fd < 0 && dcs->domid_soft_reset == INVALID_DOMID) {
         rc = libxl__domain_build(gc, d_config, domid, state);
         domcreate_rebuild_done(egc, dcs, rc);
@@ -1398,6 +1434,16 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
         if (libxl_defbool_val(d_config->b_info.device_model_stubdomain)) {
             libxl__spawn_stub_dm(egc, &dcs->dmss, EMUID_DM);
         } else {
+            if (state->emuidmap & (1u << EMUID_SPLIT)) {
+                dcs->dmss.dm.rc = 1;
+                dcs->dmss.dm.callback = domcreate_dm_local_split_dm_cb;
+
+                dcs->dmss.pvqemu.rc = 1; /* +ve means in progress */
+                dcs->dmss.pvqemu.callback = domcreate_dm_local_split_pv_cb;
+
+                libxl__spawn_qdisk_backend(egc, &dcs->dmss.pvqemu);
+            }
+
             libxl__spawn_local_dm(egc, &dcs->dmss.dm, EMUID_DM);
         }
 
@@ -1455,6 +1501,32 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
     domcreate_complete(egc, dcs, ret);
 }
 
+static void domcreate_dm_local_split_pv_cb(libxl__egc *egc,
+                                           libxl__dm_spawn_state *dmss)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.pvqemu);
+    domcreate_dm_local_split_cb(egc, dcs);
+}
+
+static void domcreate_dm_local_split_dm_cb(libxl__egc *egc,
+                                           libxl__dm_spawn_state *dmss)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.dm);
+    domcreate_dm_local_split_cb(egc, dcs);
+}
+
+static void domcreate_dm_local_split_cb(libxl__egc *egc,
+                                        libxl__domain_create_state *dcs)
+{
+    if (dcs->dmss.dm.rc > 0 ||
+        dcs->dmss.pvqemu.rc > 0)
+        /* something is still in progress */
+        return;
+
+    dcs->dmss.dm.rc = min(dcs->dmss.dm.rc, dcs->dmss.pvqemu.rc);
+    domcreate_devmodel_started(egc, &dcs->dmss.dm);
+}
+
 static void domcreate_devmodel_started(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss)
 {
Signature · 1 line
1.7.10.4
#45 lore raw

Re: [PATCH 26/28] libxl: spawns two QEMUs for HVM guests

Quoted 6 lines · Ian Jackson
tools/libxl/libxl_create.c +0 -0
@@ -440,6 +442,15 @@ static int domcreate_setdefault_dm_user(libxl__gc
 *gc,
          /* we're not going to run it anyway */
          return 0;


+    if (!libxl__dm_supported(gc, dm,

Quoted 8 lines

libxl__dm_support_check__emulator_id)) {
+        /* we don't want to run the pv backends as non-root because +         * device hotplug will no longer work. */
+        LOG(WARN,
+ "Device model does not support split PV backends, running it as root"); +        user = "root";
+        goto found;
+    }

Should this be a fatal error, unless b_info->device_model_user == "root"?

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#46 lore raw

Re: [PATCH 26/28] libxl: spawns two QEMUs for HVM guests

Ian Campbell writes ("Re: [PATCH 26/28] libxl: spawns two QEMUs for HVM guests"):

Quoted 6 lines · Ian Jackson
Quoted 5 lines
(quoted diff) +2 -0
@@ -440,6 +442,15 @@ static int domcreate_setdefault_dm_user(libxl__gc
 *gc,
+    if (!libxl__dm_supported(gc, dm,
 libxl__dm_support_check__emulator_id)) {
+        /* we don't want to run the pv backends as non-root because
Quoted 8 lines

tools/libxl/libxl_create.c static int domcreate_setdefault_dm_user(libxl__gc *gc, · line 452

Quoted 6 lines
tools/libxl/libxl_create.c +6 -0
+         * device hotplug will no longer work. */
+        LOG(WARN,
+ "Device model does not support split PV backends, running it as root");
+        user = "root";
+        goto found;
+    }

Should this be a fatal error, unless b_info->device_model_user == "root"?

This is in the code which sets the default value if the configuration didn't specify one.

Ian.

#47 lore raw

Re: [PATCH 26/28] libxl: spawns two QEMUs for HVM guests

Quoted 20 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 26/28] libxl: spawns two QEMUs for HVM guests"):

Quoted 9 lines · Ian Jackson
Quoted 8 lines
(quoted diff) +0 -0
@@ -440,6 +442,15 @@ static int

domcreate_setdefault_dm_user(libxl__gc
*gc,
+    if (!libxl__dm_supported(gc, dm, libxl__dm_support_check__emulator_id)) {
+        /* we don't want to run the pv backends as non-root because +         * device hotplug will no longer work. */
+        LOG(WARN,

Quoted 8 lines
Quoted 5 lines

+ "Device model does not support split PV backends, running it as root");
+        user = "root";
+        goto found;
+    }

Should this be a fatal error, unless b_info->device_model_user == "root"?

Quoted 2 lines

This is in the code which sets the default value if the configuration didn't specify one.

Oh yes, sorry for the noise.

Quoted 2 lines

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#48 lore raw

PATCH v6 27/28 libxl: Limit qemu physmap entries

In reply to Ian Jackson

Add a maximum limit of physmap entries to save, so that when the guest gets write access to physmap it cannot DOS the toolstack.

---
v6: Split out of xs permissions relaxation patch.

---
 tools/libxl/libxl_dom.c |    7 +++++++
 1 file changed, 7 insertions(+)
tools/libxl/libxl_dom.c +7 -0
index 6ded9c1..60e8f7f 100644
@@ -1431,6 +1431,8 @@ static void append_string(libxl__gc *gc, char **buf, uint32_t *len,
     *len += extralen;
 }
 
+#define MAX_PHYSMAP_ENTRIES 12
+
 int libxl__save_emulator_xenstore_data(libxl__domain_suspend_state *dss,
                                        char **callee_buf,
                                        uint32_t *callee_len)
@@ -1450,6 +1452,11 @@ int libxl__save_emulator_xenstore_data(libxl__domain_suspend_state *dss,
                                   &nr_entries);
     if (!entries || nr_entries == 0) { rc = 0; goto out; }
 
+    if (nr_entries > MAX_PHYSMAP_ENTRIES) {
+        LOG(ERROR, "Max physmap entries reached");
+        return ERROR_FAIL;
+    }
+
     for (i = 0; i < nr_entries; ++i) {
         static const char *const physmap_subkeys[] = {
             "start_addr", "size", "name"
Signature · 1 line
1.7.10.4
#49 lore raw

Re: [PATCH 27/28] libxl: Limit qemu physmap entries

Quoted 5 lines · Ian Jackson

Add a maximum limit of physmap entries to save, so that when the guest gets write access to physmap it cannot DOS the toolstack.

Can we have a reference for where the number 12 comes from please.

With that I think this doesn't need to wait for the rest of the series?

Quoted 25 lines

---
v6: Split out of xs permissions relaxation patch.
---
 tools/libxl/libxl_dom.c |    7 +++++++

 1 file changed, 7 insertions(+)
tools/libxl/libxl_dom.c +0 -0
index 6ded9c1..60e8f7f 100644
@@ -1431,6 +1431,8 @@ static void append_string(libxl__gc *gc, char
 **buf, uint32_t *len,
      *len += extralen;
  }

tools/libxl/libxl_dom.c +2 -0
+#define MAX_PHYSMAP_ENTRIES 12
+

 int libxl__save_emulator_xenstore_data(libxl__domain_suspend_state *dss, char **callee_buf, uint32_t *callee_len)

tools/libxl/libxl_dom.c +0 -0
@@ -1450,6 +1452,11 @@ int
 libxl__save_emulator_xenstore_data(libxl__domain_suspend_state *dss,
                                    &nr_entries);
      if (!entries || nr_entries == 0) { rc = 0; goto out; }

Quoted 8 lines

+    if (nr_entries > MAX_PHYSMAP_ENTRIES) {
+        LOG(ERROR, "Max physmap entries reached"); +        return ERROR_FAIL;
+    }
+
     for (i = 0; i < nr_entries; ++i) {
         static const char *const physmap_subkeys[] = { "start_addr", "size", "name"

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#50 lore raw

PATCH v6 28/28 libxl: xsrestrict QEMU

In reply to Ian Jackson

If QEMU supports xsrestrict, pass xsrestrict=on to it (by default).

XXX We need to do this only if xenstored supports it, and AFAICT there is not a particularly easy way to test this. Should we open a new test xenstore connection to query this information ? We could do this once per libxl ctx.

Allow the user to specify that xsrestrict should be on, in which case if it qemu cannot be depriv'd, we fail.

When qemu is depriv'd it still needs write access to the physmap records in xenstore. It will be running with the privilege of the domain, so we need to allow the domain write access to
/local/domain/$LIBXL_TOOLSTACK_DOMID/device-model/$DOMID/physmap

It doesn't contain any information the guest doesn't already know. However be aware that it still means relaxing a tools/guest interface.

The guest physmap contains the memory layout of the guest. The guest is already in possession of this information. A malicious guest could modify the physmap entries causing QEMU to read wrong information at restore time. QEMU would populate its XenPhysmap list with wrong entries that wouldn't match its own device emulation addresses, leading to a failure in restoring the domain. But it could not compromise the security of the system because the addresses are only used for checks against QEMU's addresses.

Is it dangerous to let the guest write values that later QEMU and libxl read back? Let's dig a bit deeper into the code. QEMU and libxl use very similar functions to parse the physmap entries.

Let's start checking libxl. The function that reads the physmap is libxl__toolstack_save. It calls:

* libxl__xs_directory on /physmap
This is safe.

* libxl__xs_read
If the path is wrong (nothing is there), it returns NULL, and it is handled correctly.

* strtoll on the values read
The values are under guest control but strtoll can handle bad formats. strtoll always returns an unsigned long long. In case of errors, it can return LLONG_MIN or LLONG_MAX. libxl__toolstack_save doesn't check for conversion errors, but it never uses the returned values anywhere. That's OK. libxl__toolstack_restore writes back the values to xenstore.

So in case of errors:

1) libxl__toolstack_save could return early with an error, if the xenstore paths are wrong (nothing is on xenstore)

2) libxl__toolstack_restore could write back to xenstore any unsigned long long, including LLONG_MIN or LLONG_MAX

Either way libxl is fine.

Quoted 1 line

From the QEMU side, the code is very similar and uses strtoll to parse

the entries, that can deal with bad input values. The values are used to avoid memory allocations at restore time for memory that has already been allocated (video memory). If the values are wrong, QEMU will attempt another memory allocation, failing because the maxmem limit has been reached.

Either way QEMU is fine.

There are no other out-of-guest consumers of this information.

---
v6: Merge the xsrestrict and the permissions update patches, so that
the permission relaxation is done only when needed.
Rip out qemu feature checking code, now in new (earlier) patch Rip out emulator_id, now in new (earlier) patch
Provide and document a config option to control restriction. Make the default depend on specified dm user, and on stubdomness. Arrange that when we are going to xsrestrict, we will run two qemus,
as required
Move rwperm initialisaton higher up the relevant function to help
avoid future changes using it uninitialised.
Grant write access on to the physmap subdirectory, and drop
discussion of the rest from the commit message, and adjust the xenstore doc accordingly.
Move the physmap number of entries limit into its own patch.

v5 (permissions):
improve commit message with security details

v4 (permissions):
add error message in case count > MAX_PHYSMAP_ENTRIES add a note to xenstore-paths.markdown about the possible change in
privilege level
only change permissions if xsrestrict is supported

v4 (xsrestrict):
update xenstore-paths.markdown

v3 (xsrestrict):
add emulator_ids
mark as WIP

v3 (permissions):
use LIBXL_TOOLSTACK_DOMID instead of 0 in the commit message update commit message with more info on why it is safe add a limit on the number of physmap entries to save and restore

v2 (permissioins):
fix permissions to actually do what intended
use LIBXL_TOOLSTACK_DOMID instead of 0

---
 docs/man/xl.cfg.pod.5             |   14 ++++++++++
 docs/misc/xenstore-paths.markdown |    7 +++++
 tools/libxl/libxl_create.c        |   51 +++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl_dm.c            |   30 ++++++++++++++++++++--
 tools/libxl/libxl_types.idl       |    1 +
 tools/libxl/xl_cmdimpl.c          |    2 ++
 6 files changed, 101 insertions(+), 4 deletions(-)
docs/man/xl.cfg.pod.5 +14 -0
index dd93725..7c0bac3 100644
@@ -1842,6 +1842,20 @@ Run the device model as user "username", instead of
 xen-qemudepriv-domid$domid or xen-qemudepriv-shared or root.  The
 default is to use the first of these users which exists.
 
+=item B<device_model_restrict=BOOLEAN>
+
+Restrict the device model's access to hypervisor facilities (such as
+xenstore).  The default is true if device_model_user is not `root` and
+the qemu in use supports this option (ie, when the restriction is both
+possible and beneficial), or false otherwise.
+
+If the option is set to true (1) but the restriction is not possible
+or not effective, the domain configuration is rejected.
+
+This option is ignored when a stub domain device model is in use,
+because in that situation the device model is restricted by virtue of
+being in its own domain.
+
 =back
 
 =head2 Keymaps
docs/misc/xenstore-paths.markdown +7 -0
index 1dc54ac..27981de 100644
@@ -339,6 +339,13 @@ A PV console backend. Described in [console.txt](console.txt)
 Information relating to device models running in the domain. $DOMID is
 the target domain of the device model.
 
+#### ~/device-model/$DOMID/physmap [w]
+
+Information about an HVM domain's physical memory map.  This is
+written as well as read by device models which may run at the same
+privilege level of the guest domain.  When the device model ruus with
+system privilege, this path is INTERNAL.
+
 #### ~/device-model/$DOMID[/$EMULATOR_ID]/* [INTERNAL]
 
 Information relating to device models running in the domain. $DOMID is
tools/libxl/libxl_create.c +49 -2

66 lines not on this page · raw message

tools/libxl/libxl_dm.c +28 -2
index d805800..0ee956f 100644
@@ -420,6 +420,14 @@ static int libxl__build_device_model_args_old(libxl__gc *gc,
             b_info->device_model_user);
         return ERROR_INVAL;
     }
+    if (libxl_defbool_val(b_info->device_model_restrict) &&
+        !libxl_defbool_val(b_info->device_model_stubdomain)) {
+        /* The dm support check ought to prevent this, but in case
+         * someone has done something odd, we double check. */
+        LOG(ERROR,
+            "device_model_restrict not supported by qemu-xen-traditional");
+        return ERROR_INVAL;
+    }
 
     flexarray_vappend(dm_args, dm,
                       "-d", GCSPRINTF("%d", domid), NULL);
@@ -1238,8 +1246,14 @@ static int libxl__build_device_model_args_new(libxl__gc *gc,
         }
         if (state->emuidmap & (1u << EMUID_SPLIT)) {
             flexarray_append(dm_args, "-xenopts");
-            flexarray_append(dm_args,
-                             GCSPRINTF("emulator_id=%u", emuid));
+            flexarray_append(dm_args, GCSPRINTF("%semulator_id=%u",
+                    libxl_defbool_val(b_info->device_model_restrict)
+                    ? "xsrestrict=on," : "",
+                                                emuid));
+        } else {
+            /* xsrestrict only makes sense with split qemu because
+             * the pv devices need unrestricted access */
+            assert(!libxl_defbool_val(b_info->device_model_restrict));
         }
     }
     flexarray_append(dm_args, NULL);
@@ -1760,11 +1774,17 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss,
     char **pass_stuff;
     const char *dm;
     int dm_state_fd = -1;
+    struct xs_permissions rwperm[2];
 
     if (libxl_defbool_val(b_info->device_model_stubdomain)) {
         abort();
     }
 
+    rwperm[0].id = LIBXL_TOOLSTACK_DOMID;
+    rwperm[0].perms = XS_PERM_NONE;
+    rwperm[1].id = domid;
+    rwperm[1].perms = XS_PERM_WRITE;
+
     rc = libxl__dm_emuidmap_add(gc, domid, state, emuid);
     if (rc) goto out;
 
@@ -1804,6 +1824,12 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss,
                                        domid, emuid, "");
     xs_mkdir(ctx->xsh, XBT_NULL, path);
 
+    if (libxl_defbool_val(b_info->device_model_restrict)) {
+        rc = libxl__xs_mknod(gc, XBT_NULL, GCSPRINTF("%s/physmap", path),
+                             rwperm, 2);
+        if (rc) goto out;
+    }
+
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
         b_info->device_model_version
         == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL)
tools/libxl/libxl_types.idl +1 -0
index 9658356..d5890a8 100644
@@ -443,6 +443,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("device_model_ssidref", uint32),
     ("device_model_ssid_label", string),
     ("device_model_user", string),
+    ("device_model_restrict", libxl_defbool),
 
     # extra parameters pass directly to qemu, NULL terminated
     ("extra",            libxl_string_list),
tools/libxl/xl_cmdimpl.c +2 -0
index f9933cb..bdef4dc 100644
@@ -2176,6 +2176,8 @@ skip_vfb:
         }
     }
 
+    xlu_cfg_get_defbool(config, "device_model_restrict",
+                        &b_info->device_model_restrict, 0);
 
     xlu_cfg_replace_string (config, "device_model_override",
                             &b_info->device_model, 0);
Signature · 1 line
1.7.10.4
#51 lore raw

Re: [PATCH 28/28] libxl: xsrestrict QEMU

Quoted 6 lines · Ian Jackson

If QEMU supports xsrestrict, pass xsrestrict=on to it (by default).

XXX We need to do this only if xenstored supports it, and AFAICT there is not a particularly easy way to test this.  Should we open a new test xenstore connection to query this information ?  We could do this once per libxl ctx.

Is this because the support is in oxenstored ^ cxenstored?

Otherwise I would argue that the toolstack should expect the xenstored to be at least as new as it is.

Quoted 5 lines

Let's start checking libxl.  The function that reads the physmap is libxl__toolstack_save.  It calls:

* libxl__xs_directory on /physmap
  This is safe.

Potentially very many entries and hence a big return array? Are the guest quotas sufficient to not worry about this?

Quoted 2 lines

Either way libxl is fine.

[...]

Quoted 1 line

Either way QEMU is fine.

Perhaps we should add a code comment to each of these places noting that the values are guest controlled, since it is a bit more unexpected in this case than e.g. frontend dirs.

Quoted 10 lines
docs/misc/xenstore- renamed from docs/misc/xenstore-paths.markdown +0 -0

paths.markdown
index 1dc54ac..27981de 100644

docs/misc/xenstore-paths.markdown +0 -0
@@ -339,6 +339,13 @@ A PV console backend. Described in
 [console.txt](console.txt)

 Information relating to device models running in the domain. $DOMID is the target domain of the device model.

docs/misc/xenstore-paths.markdown A PV console backend. Described in [console.txt](console.txt) · line 346

Quoted 5 lines
docs/misc/xenstore-paths.markdown +5 -0
+#### ~/device-model/$DOMID/physmap [w]
+
+Information about an HVM domain's physical memory map.  This is
+written as well as read by device models which may run at the same
+privilege level of the guest domain.  When the device model ruus with

"runs"

Quoted 1 line

+system privilege, this path is INTERNAL.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#52 lore raw

Re: [PATCH 28/28] libxl: xsrestrict QEMU

Ian Campbell writes ("Re: [PATCH 28/28] libxl: xsrestrict QEMU"):

Quoted 9 lines · Ian Jackson
Quoted 6 lines

If QEMU supports xsrestrict, pass xsrestrict=on to it (by default).

XXX We need to do this only if xenstored supports it, and AFAICT there is not a particularly easy way to test this.  Should we open a new test xenstore connection to query this information ?  We could do this once per libxl ctx.

Is this because the support is in oxenstored ^ cxenstored?

Yes.

Quoted 2 lines

Otherwise I would argue that the toolstack should expect the xenstored to be at least as new as it is.

True.

Quoted 5 lines
Quoted 2 lines

* libxl__xs_directory on /physmap
  This is safe.

Potentially very many entries and hence a big return array? Are the guest quotas sufficient to not worry about this?

Yes, the quota has to deal with that anyway (since the directory is in core in xenstored, anyway).

Quoted 3 lines

Perhaps we should add a code comment to each of these places noting that the values are guest controlled, since it is a bit more unexpected in this case than e.g. frontend dirs.

This is a good idea.

Quoted 5 lines

docs/misc/xenstore-paths.markdown A PV console backend. Described in [console.txt](console.txt) · line 346

Quoted 3 lines
docs/misc/xenstore-paths.markdown +3 -0
+Information about an HVM domain's physical memory map.  This is
+written as well as read by device models which may run at the same
+privilege level of the guest domain.  When the device model ruus with

"runs"

Fixed, thanks.

Ian.

#53 lore raw

Re: [PATCH 28/28] libxl: xsrestrict QEMU

In reply to Ian Jackson
Quoted 7 lines · Ian Jackson

If QEMU supports xsrestrict, pass xsrestrict=on to it (by default).

XXX We need to do this only if xenstored supports it, and AFAICT there is not a particularly easy way to test this. Should we open a new test xenstore connection to query this information ? We could do this once per libxl ctx.

Quoted 7 lines

Allow the user to specify that xsrestrict should be on, in which case if it qemu cannot be depriv'd, we fail.

When qemu is depriv'd it still needs write access to the physmap records in xenstore. It will be running with the privilege of the domain, so we need to allow the domain write access to
/local/domain/$LIBXL_TOOLSTACK_DOMID/device-model/$DOMID/physmap

QEMU also needs to be able to write to "state" under /local/domain/$LIBXL_TOOLSTACK_DOMID/device-model/$DOMID

I guess theoretically it might be possible to restrict the xenstore connection on the QEMU side only after switching to the "running" state, however that's not how it was done in the early implementation at least:

http://marc.info/?i=1433173614-19716-2-git-send-email-stefano.stabellini%40eu.citrix.com

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#54 lore raw
In reply to Ian Jackson
Quoted 14 lines · Ian Jackson

This is a new version of Stefano Stabellini's series
[PATCH v5 0/6] libxl: xs_restrict QEMU

I took Stefano's code as a spec for how to interact with qemu, and have reworked the whole series. In particular, I have
- rebased onto staging
- split up some of the larger patches
- restructured the patches so that every intervening version should work
- redone the async task handling
- provided what seem to me to be appropriate configuration options
- shaved a few yaks (although I tried to limit this!)
- fixed the cross-version compatibility
- reduced the new permission grant for the domain to only .../physmap

One aspect I don't see in this series is how save and restore for multiple emulator context is handled.

Did I miss anything?

Wei.

#55 lore raw
Quoted 16 lines · Wei Liu
Quoted 14 lines · Ian Jackson

This is a new version of Stefano Stabellini's series
[PATCH v5 0/6] libxl: xs_restrict QEMU

I took Stefano's code as a spec for how to interact with qemu, and have reworked the whole series. In particular, I have
- rebased onto staging
- split up some of the larger patches
- restructured the patches so that every intervening version should work
- redone the async task handling
- provided what seem to me to be appropriate configuration options
- shaved a few yaks (although I tried to limit this!)
- fixed the cross-version compatibility
- reduced the new permission grant for the domain to only .../physmap

Quoted 4 lines

One aspect I don't see in this series is how save and restore for multiple emulator context is handled.

Did I miss anything?

With the premise that I didn't have a chance to read the series yet, in the original design there aren't multiple emulators: one qemu instance is the emulator, the other only provides PV backends.

#56 lore raw

Stefano Stabellini writes ("Re: [RFC PATCH v6 00/28] libxl: Deprivilege qemu"):

Quoted 9 lines · Wei Liu
Quoted 4 lines

One aspect I don't see in this series is how save and restore for multiple emulator context is handled.

Did I miss anything?

With the premise that I didn't have a chance to read the series yet, in the original design there aren't multiple emulators: one qemu instance is the emulator, the other only provides PV backends.

Right. And the PV backends don't have any "emulator state" in that sense. They are simply torn down and recreated.

Ian.

#57 lore raw
In reply to Ian Jackson

I take that this series is going to miss 4.7 at this stage, right?

Quoted 23 lines · Ian Jackson

This is a new version of Stefano Stabellini's series
[PATCH v5 0/6] libxl: xs_restrict QEMU

I took Stefano's code as a spec for how to interact with qemu, and have reworked the whole series. In particular, I have
- rebased onto staging
- split up some of the larger patches
- restructured the patches so that every intervening version should work
- redone the async task handling
- provided what seem to me to be appropriate configuration options
- shaved a few yaks (although I tried to limit this!)
- fixed the cross-version compatibility
- reduced the new permission grant for the domain to only .../physmap

I have compiled this but I haven't tested it yet.

I think it would be very useful at this stage for others to read the commit messages, internal docs, and important structural elements.

Detailed code review should probably wait until after testing which in turn ought to wait unil we decide what (if any) design and interface changes are needed.

Quoted 7 lines

And I don't expect to get any feedback until the new year :-).

Happy holidays all.

Regards,
Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#59 lore raw
In reply to Stefano Stabellini

Stefano Stabellini writes ("Re: [RFC PATCH v6 00/28] libxl: Deprivilege qemu"):

Quoted 1 line

I take that this series is going to miss 4.7 at this stage, right?

I'm afraid so. We concluded that a crucial piece - arranging for the necessary access controls on privcmd - was not going to be in place for 4.7.

Without that, this series does not offer much additional security. It is a shame that the code will rot a bit in the meantime as a result of it missing 4.7.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#60 lore raw

Ian Jackson writes ("Re: [RFC PATCH v6 00/28] libxl: Deprivilege qemu"):

Quoted 10 lines

Stefano Stabellini writes ("Re: [RFC PATCH v6 00/28] libxl: Deprivilege qemu"):

Quoted 1 line

I take that this series is going to miss 4.7 at this stage, right?

I'm afraid so. We concluded that a crucial piece - arranging for the necessary access controls on privcmd - was not going to be in place for 4.7.

Without that, this series does not offer much additional security. It is a shame that the code will rot a bit in the meantime as a result of it missing 4.7.

FYI, the Citrix XenServer team is interested in work in this area. I've been having some conversations with them, to see how we can best work together.

Watch this space. I'm hopeful that this work (or something substantially similar) will be ready for Xen 4.8.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel