Skip to contentSkip to conversation

vTPM: add vTPM device for HVM virtual machine

PATCH Superseded Quan Xu 2 participants 4 messages Dec 31, 2014 – Jan 8, 2015

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Quan Xu

author
#0 lore raw

PATCH v2 4/5 vTPM: add vTPM device for HVM virtual machine

---
 tools/libxl/libxl.c          | 62 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c   |  6 +++++
 tools/libxl/libxl_dm.c       | 16 ++++++++++++
 tools/libxl/libxl_internal.h |  3 +++
 4 files changed, 87 insertions(+)
tools/libxl/libxl.c +62 -0
index 18561fb..656d4b0 100644
@@ -2015,6 +2015,10 @@ void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
     flexarray_append(front, "handle");
     flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
 
+    /*for para virtual machine*/
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d", LIBXL_DOMAIN_TYPE_PV));
+
     if (aodev->update_json) {
         lock = libxl__lock_domain_userdata(gc, domid);
         if (!lock) {
@@ -2073,6 +2077,64 @@ out:
     return;
 }
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm)
+{
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device *device;
+    unsigned int rc;
+
+    rc = libxl__device_vtpm_setdefault(gc, vtpm);
+    if (rc) goto out;
+
+    front = flexarray_make(gc, 16, 1);
+    back = flexarray_make(gc, 16, 1);
+
+    if (vtpm->devid == -1) {
+        if ((vtpm->devid = libxl__device_nextid(gc, domid, "vtpm")) < 0) {
+            rc = ERROR_FAIL;
+            goto out;
+        }
+    }
+
+    GCNEW(device);
+    rc = libxl__device_from_vtpm(gc, domid, vtpm, device);
+    if ( rc != 0 ) goto out;
+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, GCSPRINTF("%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, GCSPRINTF("%d", 1));
+    flexarray_append(back, "handle");
+    flexarray_append(back, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(back, "uuid");
+    flexarray_append(back, GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(vtpm->uuid)));
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", 1));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d", LIBXL_DOMAIN_TYPE_HVM));
+
+    libxl__device_generic_add(gc, XBT_NULL, device,
+                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                              libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                              NULL);
+
+    rc = 0;
+out:
+    return;
+}
+
 libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num)
 {
     GC_INIT(ctx);
tools/libxl/libxl_create.c +6 -0
index c6f68fe..b2f61cb 100644
@@ -901,6 +901,12 @@ static void initiate_domain_create(libxl__egc *egc,
             d_config->nics[i].devid = ++last_devid;
     }
 
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM &&
+        d_config->num_vtpms > 0) {
+        ret = libxl__device_vtpm_setdefault(gc, d_config->vtpms);
+        if (ret) goto error_out;
+    }
+
     if (restore_fd >= 0) {
         LOG(DEBUG, "restoring, not running bootloader");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
tools/libxl/libxl_dm.c +16 -0
index 3e191c3..337ac64 100644
@@ -414,6 +414,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     const libxl_device_nic *nics = guest_config->nics;
     const int num_disks = guest_config->num_disks;
     const int num_nics = guest_config->num_nics;
+    const int num_vtpms = guest_config->num_vtpms;
     const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config);
     const libxl_sdl_info *sdl = dm_sdl(guest_config);
     const char *keymap = dm_keymap(guest_config);
@@ -747,6 +748,15 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         abort();
     }
 
+    /*add vTPM parameters for HVM virtual machine*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        num_vtpms >0) {
+        flexarray_vappend(dm_args, "-tpmdev",
+                          "xenstubdoms,id=xenvtpm0", NULL);
+        flexarray_vappend(dm_args,"-device",
+                          "tpm-tis,tpmdev=xenvtpm0", NULL);
+    }
+
     ram_size = libxl__sizekb_to_mb(b_info->max_memkb - b_info->video_memkb);
     flexarray_append(dm_args, "-m");
     flexarray_append(dm_args, libxl__sprintf(gc, "%"PRId64, ram_size));
@@ -1412,6 +1422,12 @@ retry_transaction:
     spawn->failure_cb = device_model_startup_failed;
     spawn->detached_cb = device_model_detached;
 
+    /* Plug vtpm devices*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        guest_config->num_vtpms > 0){
+        libxl__device_hvm_vtpm_add(gc, domid, guest_config->vtpms);
+    }
+
     rc = libxl__spawn_spawn(egc, spawn);
     if (rc < 0)
         goto out_close;
tools/libxl/libxl_internal.h +3 -0
index 4361421..946b8cf 100644
@@ -2388,6 +2388,9 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
                                    libxl_device_vtpm *vtpm,
                                    libxl__ao_device *aodev);
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm);
+
 /* Internal function to connect a vkb device */
 _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,
                                   libxl_device_vkb *vkb);
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 13 lines · Quan Xu
---
 tools/libxl/libxl.c          | 62 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_create.c   |  6 +++++
 tools/libxl/libxl_dm.c       | 16 ++++++++++++
 tools/libxl/libxl_internal.h |  3 +++
 4 files changed, 87 insertions(+)
tools/libxl/libxl.c +0 -0
index 18561fb..656d4b0 100644

tools/libxl/libxl.c void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, · line 2018

Quoted 4 lines
tools/libxl/libxl.c +1 -0
@@ -2015,6 +2015,10 @@ void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
     flexarray_append(front, "handle");
     flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
 
+    /*for para virtual machine*/

Space after "/*" and before "*/", and capitalise "for".

Please also fix similar issues below.

tools/libxl/libxl.c void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, · line 2021

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d", LIBXL_DOMAIN_TYPE_PV));
+

What is this used for?

Quoted 21 lines
tools/libxl/libxl.c +14 -0
     if (aodev->update_json) {
         lock = libxl__lock_domain_userdata(gc, domid);
         if (!lock) {
@@ -2073,6 +2077,64 @@ out:
     return;
 }
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm)
+{
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device *device;
+    unsigned int rc;
+
+    rc = libxl__device_vtpm_setdefault(gc, vtpm);
+    if (rc) goto out;
+
+    front = flexarray_make(gc, 16, 1);
+    back = flexarray_make(gc, 16, 1);
+

tools/libxl/libxl.c out: · line 2103

Quoted 10 lines
tools/libxl/libxl.c +10 -0
+    if (vtpm->devid == -1) {
+        if ((vtpm->devid = libxl__device_nextid(gc, domid, "vtpm")) < 0) {
+            rc = ERROR_FAIL;
+            goto out;
+        }
+    }
+
+    GCNEW(device);
+    rc = libxl__device_from_vtpm(gc, domid, vtpm, device);
+    if ( rc != 0 ) goto out;

Coding style.

tools/libxl/libxl.c out: · line 2109

Quoted 6 lines
tools/libxl/libxl.c +6 -0
+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, GCSPRINTF("%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, GCSPRINTF("%d", 1));

No need to use GCSPRINT.

tools/libxl/libxl.c out: · line 2114

Quoted 5 lines
tools/libxl/libxl.c +5 -0
+    flexarray_append(back, "handle");
+    flexarray_append(back, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(back, "uuid");
+    flexarray_append(back, GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(vtpm->uuid)));

Line too long.

Quoted 13 lines
tools/libxl/libxl.c +13 -0
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", 1));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d", LIBXL_DOMAIN_TYPE_HVM));
+

tools/libxl/libxl.c out: · line 2137

Quoted 10 lines
tools/libxl/libxl.c +10 -0
+    libxl__device_generic_add(gc, XBT_NULL, device,
+                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                              libxl__xs_kvs_of_flexarray(gc, front, front->count),
+                              NULL);
+
+    rc = 0;
+out:
+    return;
+}
+

There is one major issue with your implementation. You didn't handle vtpm hot-plug and hot-unplug.

I think what you should do is to refactor libxl__device_vtpm_add to call the right helpers (say libxl__device_vtpm_add_{pv,hvm}).

With this approach you don't need to worry about all the internal logic of handling JSON template and locking etc. You might also be able to nuke b_info->num_vtpms you added in previous patches.

Does this make sense?

Quoted 33 lines
tools/libxl/libxl.c +0 -0
 libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num)
 {
     GC_INIT(ctx);
tools/libxl/libxl_create.c +6 -0
index c6f68fe..b2f61cb 100644
@@ -901,6 +901,12 @@ static void initiate_domain_create(libxl__egc *egc,
             d_config->nics[i].devid = ++last_devid;
     }
 
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM &&
+        d_config->num_vtpms > 0) {
+        ret = libxl__device_vtpm_setdefault(gc, d_config->vtpms);
+        if (ret) goto error_out;
+    }
+
     if (restore_fd >= 0) {
         LOG(DEBUG, "restoring, not running bootloader");
         domcreate_bootloader_done(egc, &dcs->bl, 0);
tools/libxl/libxl_dm.c +1 -0
index 3e191c3..337ac64 100644
@@ -414,6 +414,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     const libxl_device_nic *nics = guest_config->nics;
     const int num_disks = guest_config->num_disks;
     const int num_nics = guest_config->num_nics;
+    const int num_vtpms = guest_config->num_vtpms;
     const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config);
     const libxl_sdl_info *sdl = dm_sdl(guest_config);
     const char *keymap = dm_keymap(guest_config);

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 753

Quoted 6 lines
tools/libxl/libxl_dm.c +3 -0
@@ -747,6 +748,15 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         abort();
     }
 
+    /*add vTPM parameters for HVM virtual machine*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        num_vtpms >0) {

"> 0".

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 755

Quoted 2 lines
tools/libxl/libxl_dm.c +2 -0
+        flexarray_vappend(dm_args, "-tpmdev",
+                          "xenstubdoms,id=xenvtpm0", NULL);

Hardcoded id? I think we need to be more flexible on this.

Wei.

Quoted 36 lines
tools/libxl/libxl_dm.c +10 -0
+        flexarray_vappend(dm_args,"-device",
+                          "tpm-tis,tpmdev=xenvtpm0", NULL);
+    }
+
     ram_size = libxl__sizekb_to_mb(b_info->max_memkb - b_info->video_memkb);
     flexarray_append(dm_args, "-m");
     flexarray_append(dm_args, libxl__sprintf(gc, "%"PRId64, ram_size));
@@ -1412,6 +1422,12 @@ retry_transaction:
     spawn->failure_cb = device_model_startup_failed;
     spawn->detached_cb = device_model_detached;
 
+    /* Plug vtpm devices*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        guest_config->num_vtpms > 0){
+        libxl__device_hvm_vtpm_add(gc, domid, guest_config->vtpms);
+    }
+
     rc = libxl__spawn_spawn(egc, spawn);
     if (rc < 0)
         goto out_close;
tools/libxl/libxl_internal.h +3 -0
index 4361421..946b8cf 100644
@@ -2388,6 +2388,9 @@ _hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid,
                                    libxl_device_vtpm *vtpm,
                                    libxl__ao_device *aodev);
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm);
+
 /* Internal function to connect a vkb device */
 _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,
                                   libxl_device_vkb *vkb);
Signature · 1 line
1.8.3.2
author
#2 lore raw
Quoted 26 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: Monday, January 05, 2015 9:14 PM
To: Xu, Quan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2 4/5] vTPM: add vTPM device for HVM virtual machine

Quoted 3 lines · Quan Xu
---
 tools/libxl/libxl.c          | 62

++++++++++++++++++++++++++++++++++++++++++++

tools/libxl/libxl.c void libxl__device_vtpm_add(libxl__egc *egc,

Quoted 10 lines
 tools/libxl/libxl_create.c   |  6 +++++
 tools/libxl/libxl_dm.c       | 16 ++++++++++++
 tools/libxl/libxl_internal.h |  3 +++
 4 files changed, 87 insertions(+)
tools/libxl/libxl.c index renamed from tools/libxl/libxl.c +0 -0

18561fb..656d4b0 100644

tools/libxl/libxl.c +0 -0
@@ -2015,6 +2015,10 @@ void libxl__device_vtpm_add(libxl__egc *egc,

uint32_t domid,

Quoted 6 lines

tools/libxl/libxl.c void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, · line 2018

Quoted 4 lines
tools/libxl/libxl.c +1 -0
     flexarray_append(front, "handle");
     flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
 
+    /*for para virtual machine*/

Space after "/*" and before "*/", and capitalise "for".

Thanks Wei.
I will modify it in v3.

Quoted 8 lines

Please also fix similar issues below.

tools/libxl/libxl.c void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, · line 2021

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d", LIBXL_DOMAIN_TYPE_PV));
+

What is this used for?

The domain-type is the flag bit for Domain ID when bind event channel port. It will be deleted in v3.

Quoted 25 lines
Quoted 25 lines
tools/libxl/libxl.c +17 -0
     if (aodev->update_json) {
         lock = libxl__lock_domain_userdata(gc, domid);
         if (!lock) {
@@ -2073,6 +2077,64 @@ out:
     return;
 }
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm) {
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device *device;
+    unsigned int rc;
+
+    rc = libxl__device_vtpm_setdefault(gc, vtpm);
+    if (rc) goto out;
+
+    front = flexarray_make(gc, 16, 1);
+    back = flexarray_make(gc, 16, 1);
+
+    if (vtpm->devid == -1) {
+        if ((vtpm->devid = libxl__device_nextid(gc, domid, "vtpm")) < 0) {
+            rc = ERROR_FAIL;
+            goto out;
Quoted 8 lines

tools/libxl/libxl.c out: · line 2103

Quoted 6 lines
tools/libxl/libxl.c +6 -0
+        }
+    }
+
+    GCNEW(device);
+    rc = libxl__device_from_vtpm(gc, domid, vtpm, device);
+    if ( rc != 0 ) goto out;

Coding style.

I will modify it in v3.

Quoted 9 lines

tools/libxl/libxl.c out: · line 2109

Quoted 7 lines
tools/libxl/libxl.c +6 -0
+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, GCSPRINTF("%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, GCSPRINTF("%d", 1));

No need to use GCSPRINT.

Ditto

Quoted 10 lines

tools/libxl/libxl.c out:

Quoted 7 lines
tools/libxl/libxl.c +6 -0
+    flexarray_append(back, "handle");
+    flexarray_append(back, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(back, "uuid");
+    flexarray_append(back, GCSPRINTF(LIBXL_UUID_FMT,
+ LIBXL_UUID_BYTES(vtpm->uuid)));

Line too long.

Ditto

Quoted 19 lines

tools/libxl/libxl.c out:

Quoted 12 lines
tools/libxl/libxl.c +12 -0
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", 1));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d",

LIBXL_DOMAIN_TYPE_HVM));

tools/libxl/libxl.c out:

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+
+    libxl__device_generic_add(gc, XBT_NULL, device,
+                              libxl__xs_kvs_of_flexarray(gc, back,

back->count),

tools/libxl/libxl.c out: · line 2130

Quoted 1 line
tools/libxl/libxl.c +1 -0
+                              libxl__xs_kvs_of_flexarray(gc, front,

front->count),

Quoted 10 lines

tools/libxl/libxl.c out: · line 2137

Quoted 7 lines
tools/libxl/libxl.c +7 -0
+                              NULL);
+
+    rc = 0;
+out:
+    return;
+}
+

There is one major issue with your implementation. You didn't handle vtpm hot-plug and hot-unplug.

Yes, host-plug/host-unplug is an issue. I have no more idea for this. Could you share Some host-plug/host-unplug QEMU device mode? Then I can implement it as the next plan.

Quoted 9 lines

I think what you should do is to refactor libxl__device_vtpm_add to call the right helpers (say libxl__device_vtpm_add_{pv,hvm}).

With this approach you don't need to worry about all the internal logic of handling JSON template and locking etc. You might also be able to nuke b_info->num_vtpms you added in previous patches.

Does this make sense?

Yes, make sense.

Quoted 38 lines

tools/libxl/libxl_dm.c static char **

Quoted 26 lines
tools/libxl/libxl.c +0 -0
 libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t
 domid, int *num)  {
     GC_INIT(ctx);
tools/libxl/libxl_create.c +6 -0
index c6f68fe..b2f61cb 100644
@@ -901,6 +901,12 @@ static void initiate_domain_create(libxl__egc *egc,
             d_config->nics[i].devid = ++last_devid;
     }
 
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM &&
+        d_config->num_vtpms > 0) {
+        ret = libxl__device_vtpm_setdefault(gc, d_config->vtpms);
+        if (ret) goto error_out;
+    }
+
     if (restore_fd >= 0) {
         LOG(DEBUG, "restoring, not running bootloader");
         domcreate_bootloader_done(egc, &dcs->bl, 0); diff --git

a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c index 3e191c3..337ac64 100644

tools/libxl/libxl_dm.c +0 -0
@@ -414,6 +414,7 @@ static char **

libxl__build_device_model_args_new(libxl__gc *gc,

Quoted 11 lines
tools/libxl/libxl_dm.c +1 -0
     const libxl_device_nic *nics = guest_config->nics;
     const int num_disks = guest_config->num_disks;
     const int num_nics = guest_config->num_nics;
+    const int num_vtpms = guest_config->num_vtpms;
     const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config);
     const libxl_sdl_info *sdl = dm_sdl(guest_config);
     const char *keymap = dm_keymap(guest_config); @@ -747,6 +748,15

@@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,

         abort();
     }
Quoted 5 lines

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 753

Quoted 3 lines
tools/libxl/libxl_dm.c +3 -0
+    /*add vTPM parameters for HVM virtual machine*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        num_vtpms >0) {

"> 0".

I will modify it in v3.

Quoted 5 lines

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 755

Quoted 3 lines
tools/libxl/libxl_dm.c +2 -0
+        flexarray_vappend(dm_args, "-tpmdev",
+                          "xenstubdoms,id=xenvtpm0", NULL);

Hardcoded id? I think we need to be more flexible on this.

It is not hardcoded id.
Just make sure id=xenvtpm0 is the same as tpmdev=xenvtpm0, in next 2 functions. flexarray_vappend(dm_args, "-tpmdev",

               "xenstubdoms,id=xenvtpm0", NULL);
flexarray_vappend(dm_args,"-device",
                "tpm-tis,tpmdev=xenvtpm0", NULL);

I can change it to:

flexarray_vappend(dm_args, "-tpmdev",

                libxl__sprintf(gc, " xenstubdoms,id=xenvtpm%d", guest_domid, NULL);
flexarray_vappend(dm_args,"-device",
                libxl__sprintf(gc, " tpm-tis,tpmdev=xenvtpm%d",NULL);

thanks
Quan

Quoted 41 lines

Wei.

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc,

Quoted 5 lines
tools/libxl/libxl_dm.c +4 -0
+        flexarray_vappend(dm_args,"-device",
+                          "tpm-tis,tpmdev=xenvtpm0", NULL);
+    }
+
     ram_size = libxl__sizekb_to_mb(b_info->max_memkb -

b_info->video_memkb);

tools/libxl/libxl_internal.h _hidden void libxl__device_vtpm_add(libxl__egc

Quoted 20 lines
     flexarray_append(dm_args, "-m");
     flexarray_append(dm_args, libxl__sprintf(gc, "%"PRId64,
ram_size)); @@ -1412,6 +1422,12 @@ retry_transaction:
     spawn->failure_cb = device_model_startup_failed;
     spawn->detached_cb = device_model_detached;
tools/libxl/libxl_dm.c +6 -0
+    /* Plug vtpm devices*/
+    if (b_info->type == LIBXL_DOMAIN_TYPE_HVM &&
+        guest_config->num_vtpms > 0){
+        libxl__device_hvm_vtpm_add(gc, domid, guest_config->vtpms);
+    }
+
     rc = libxl__spawn_spawn(egc, spawn);
     if (rc < 0)
         goto out_close;
(quoted diff) +0 -0

b/tools/libxl/libxl_internal.h index 4361421..946b8cf 100644

tools/libxl/libxl_internal.h +0 -0
@@ -2388,6 +2388,9 @@ _hidden void libxl__device_vtpm_add(libxl__egc

*egc, uint32_t domid,

Quoted 11 lines
tools/libxl/libxl_internal.h +3 -1
                                    libxl_device_vtpm *vtpm,
                                    libxl__ao_device *aodev);
 
+void libxl__device_hvm_vtpm_add(libxl__gc *gc, uint32_t domid,
+                                libxl_device_vtpm *vtpm);
+
 /* Internal function to connect a vkb device */  _hidden int
 libxl__device_vkb_add(libxl__gc *gc, uint32_t domid,
                                   libxl_device_vkb *vkb);
--

1.8.3.2

#3 lore raw

On Thu, Jan 08, 2015 at 09:44:11AM +0000, Xu, Quan wrote:
[...]

Quoted 31 lines
Quoted 20 lines

tools/libxl/libxl.c out:

Quoted 13 lines
tools/libxl/libxl.c +12 -0
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, GCSPRINTF("%d", 1));
+    flexarray_append(front, "handle");
+    flexarray_append(front, GCSPRINTF("%d", vtpm->devid));
+
+    flexarray_append(front, "domain-type");
+    flexarray_append(front, GCSPRINTF("%d",

LIBXL_DOMAIN_TYPE_HVM));

tools/libxl/libxl.c out:

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+
+    libxl__device_generic_add(gc, XBT_NULL, device,
+                              libxl__xs_kvs_of_flexarray(gc, back,

back->count),

Quoted 1 line

+ libxl__xs_kvs_of_flexarray(gc, front,

front->count),

Quoted 10 lines

tools/libxl/libxl.c out: · line 2137

Quoted 7 lines
tools/libxl/libxl.c +7 -0
+                              NULL);
+
+    rc = 0;
+out:
+    return;
+}
+

There is one major issue with your implementation. You didn't handle vtpm hot-plug and hot-unplug.

Quoted 3 lines

Yes, host-plug/host-unplug is an issue. I have no more idea for this. Could you share Some host-plug/host-unplug QEMU device mode? Then I can implement it as the next plan.

The hotplug handling logic is generic to both PV and HVM. It's more about maintaining some states in libxl. The locking pattern is documented in libxl_internal.h. Search for json_lock in that file. BTW that file also contains documents for some other libxl internal machinery that you might find useful.

Generally speaking, I don't think you will need to touch hotplug handling logic. If you're aware of it you won't break things by chance.

I will use your specific case as example.

Search for libxl_device_vtpm_add in libxl.c you will see a macro defining that function, which is called in device hotplug path. That function initiates an asynchronous operation (AO), which is documented in libxl_internal.h.

During domain creation, the code path is a bit different. Take a look at libxl_create.c:domcreate_attach_vtpms. In the end it's still libxl__device_vtpm_add that does the real work.

Again, you're not likely to change all these, but knowing them can help you not break things by chance. And you can also see why I want you to modify libxl__device_vtpm_add.

To play with hotplug and unplug, see `xl --help | grep vtpm'.

Quoted 14 lines
Quoted 9 lines

I think what you should do is to refactor libxl__device_vtpm_add to call the right helpers (say libxl__device_vtpm_add_{pv,hvm}).

With this approach you don't need to worry about all the internal logic of handling JSON template and locking etc. You might also be able to nuke b_info->num_vtpms you added in previous patches.

Does this make sense?

Yes, make sense.

Quoted 1 line

[...]

Quoted 7 lines

It is not hardcoded id.
Just make sure id=xenvtpm0 is the same as tpmdev=xenvtpm0, in next 2 functions. flexarray_vappend(dm_args, "-tpmdev",

               "xenstubdoms,id=xenvtpm0", NULL);
flexarray_vappend(dm_args,"-device",
                "tpm-tis,tpmdev=xenvtpm0", NULL);
Quoted 7 lines

I can change it to:

flexarray_vappend(dm_args, "-tpmdev",

                libxl__sprintf(gc, " xenstubdoms,id=xenvtpm%d", guest_domid, NULL);
flexarray_vappend(dm_args,"-device",
                libxl__sprintf(gc, " tpm-tis,tpmdev=xenvtpm%d",NULL);

Probably you should use that particular vtpm's devid? Given that in theory you can have several vtpms for a guest if you hotplug more than one.

Wei.