Skip to contentSkip to conversation

add vtpm support to libxl

PATCH In review Matthew Fioravante 2 participants 2 messages Sep 18, 2012 – Sep 19, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 2/6 add vtpm support to libxl

This patch adds vtpm support to libxl. It adds vtpm parsing to config files and 3 new xl commands:
vtpm-attach
vtpm-detach
vtpm-list

Signed off by: Matthew Fioravante [email protected]

tools/libxl/libxl.c +0 -0
@@ -2027,6 +2027,274 @@ int libxl__device_disk_local_detach(libxl__gc

*gc, libxl_device_disk *disk)
}

/******************************************************************************/
+int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm) +{
+ if(libxl_uuid_is_nil(&vtpm->uuid)) {
+ libxl_uuid_generate(&vtpm->uuid);
+ }
+ return 0;
+}
+
+static int libxl__device_from_vtpm(libxl__gc *gc, uint32_t domid,
+ libxl_device_vtpm *vtpm,
+ libxl__device *device)
+{

+   device->backend_devid   = vtpm->devid;
+   device->backend_domid   = vtpm->backend_domid;
+   device->backend_kind    = LIBXL__DEVICE_KIND_VTPM;
+   device->devid           = vtpm->devid;
+   device->domid           = domid;
+   device->kind            = LIBXL__DEVICE_KIND_VTPM;

+
+ return 0;
+}
+
+int libxl_device_vtpm_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vtpm *vtpm)
+{

+    GC_INIT(ctx);
+    flexarray_t *front;
+    flexarray_t *back;
+    libxl__device device;
+    char *dompath, **l;
+    unsigned int nb, rc;

+
+ rc = libxl__device_vtpm_setdefault(gc, vtpm);
+ if (rc) goto out;
+
+ front = flexarray_make(16, 1);
+ if (!front) {
+ rc = ERROR_NOMEM;
+ goto out;

+    }
+    back = flexarray_make(16, 1);
+    if (!back) {

+ rc = ERROR_NOMEM;
+ goto out;
+ }
+
+ if(vtpm->devid == -1) {
+ if (!(dompath = libxl__xs_get_dompath(gc, domid))) {
+ rc = ERROR_FAIL;
+ goto out_free;
+ }
+ l = libxl__xs_directory(gc, XBT_NULL, libxl__sprintf(gc,
"%s/device/vtpm", dompath), &nb);
+ if(l == NULL || nb == 0) {
+ vtpm->devid = 0;
+ } else {
+ vtpm->devid = strtoul(l[nb - 1], NULL, 10) + 1;
+ }
+ }
+
+ rc = libxl__device_from_vtpm(gc, domid, vtpm, &device);
+ if ( rc != 0 ) goto out_free;
+

+    flexarray_append(back, "frontend-id");
+    flexarray_append(back, libxl__sprintf(gc, "%d", domid));
+    flexarray_append(back, "online");
+    flexarray_append(back, "1");
+    flexarray_append(back, "state");
+    flexarray_append(back, libxl__sprintf(gc, "%d", 1));

+
+ flexarray_append(back, "uuid");
+ flexarray_append(back, libxl__sprintf(gc, LIBXL_UUID_FMT,
LIBXL_UUID_BYTES(vtpm->uuid)));

+    flexarray_append(back, "instance"); /* MAYBE CAN GET RID OF THIS */
+    flexarray_append(back, "0");
+    flexarray_append(back, "pref_instance"); /* MAYBE CAN GET RID OF
THIS */
+    flexarray_append(back, "0");
+    flexarray_append(back, "resume");
+    flexarray_append(back, "False");
+    flexarray_append(back, "ready"); /* MAYBE CAN GET RID OF THIS */
+    flexarray_append(back, "1");
+
+    flexarray_append(front, "backend-id");
+    flexarray_append(front, libxl__sprintf(gc, "%d", vtpm->backend_domid));
+    flexarray_append(front, "state");
+    flexarray_append(front, libxl__sprintf(gc, "%d", 1));
+    flexarray_append(front, "handle");
+    flexarray_append(front, libxl__sprintf(gc, "%d", vtpm->devid));

+
+ 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));
+
+ rc = 0;
+out_free:
+ flexarray_free(back);
+ flexarray_free(front);
+out:
+ GC_FREE;
+ return rc;
+}
+
+int libxl_device_vtpm_remove(libxl_ctx *ctx, uint32_t domid,
+ libxl_device_vtpm *vtpm,
+ const libxl_asyncop_how *ao_how)
+{

+    AO_CREATE(ctx, domid, ao_how);
+    libxl__device device;
+    int rc;

+
+ rc = libxl__device_from_vtpm(gc, domid, vtpm, &device);
+ if (rc != 0) goto out;
+
+ rc = libxl__initiate_device_remove(egc, ao, &device);
+ if (rc) goto out;
+
+ return AO_INPROGRESS;
+
+out:
+ return AO_ABORT(rc);
+}
+
+int libxl_device_vtpm_destroy(libxl_ctx *ctx, uint32_t domid,
+ libxl_device_vtpm *vtpm)
+{

+   GC_INIT(ctx);
+   libxl__device device;
+   int rc;

+
+ rc = libxl__device_from_vtpm(gc, domid, vtpm, &device);
+ if (rc != 0) goto out;
+
+ rc = libxl__device_destroy(gc, &device);
+out:
+ GC_FREE;
+ return rc;
+}
+
+static void libxl__device_vtpm_from_xs_fe(libxl__gc *gc,
+ const char* fe_path,
+ libxl_device_vtpm *vtpm)
+{
+ char* tmp;
+
+ memset(vtpm, 0, sizeof(*vtpm));
+ tmp = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/handle",
fe_path));
+ if (tmp) {
+ vtpm->devid = atoi(tmp);
+ }
+ tmp = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
"%s/backend-id", fe_path));
+ if(tmp) {
+ vtpm->backend_domid = atoi(tmp);
+ }
+ tmp = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/uuid",
fe_path));
+ if(tmp) {
+ libxl_uuid_from_string(&(vtpm->uuid), tmp);
+ }
+}
+
+libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num)
+{
+ GC_INIT(ctx);
+

+    libxl_device_vtpm* vtpms = NULL;
+    char* fe_path = NULL;
+    char** dir = NULL;
+    unsigned int ndirs = 0;

+
+ *num = 0;
+
+ fe_path = libxl__sprintf(gc, "%s/device/vtpm",
libxl__xs_get_dompath(gc, domid));
+ dir = libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs);
+ if(dir) {

+       vtpms = malloc(sizeof(*vtpms) * ndirs);
+       libxl_device_vtpm* vtpm;
+       libxl_device_vtpm* end = vtpms + ndirs;
+       for(vtpm = vtpms; vtpm < end; ++vtpm, ++dir) {

+ const char* path = libxl__sprintf(gc, "%s/%s", fe_path, *dir);
+ libxl__device_vtpm_from_xs_fe(gc, path, vtpm);
+ }
+ }
+ *num = ndirs;
+
+ GC_FREE;
+ return vtpms;
+}
+
+int libxl_device_vtpm_getinfo(libxl_ctx *ctx, uint32_t domid,
+ libxl_device_vtpm *vtpm, libxl_vtpminfo
*vtpminfo)
+{

+    GC_INIT(ctx);
+    char *dompath, *vtpmpath;
+    char *val;
+    int rc = 0;
+   
+    libxl_vtpminfo_init(vtpminfo);
+    dompath = libxl__xs_get_dompath(gc, domid);
+    vtpminfo->devid = vtpm->devid;

+
+ vtpmpath = libxl__sprintf(gc, "%s/device/vtpm/%d", dompath,
vtpminfo->devid);
+ vtpminfo->backend = xs_read(ctx->xsh, XBT_NULL,
+ libxl__sprintf(gc, "%s/backend",
vtpmpath), NULL);
+ if (!vtpminfo->backend) {
+ goto err;
+ }
+ if(!libxl__xs_read(gc, XBT_NULL, vtpminfo->backend)) {
+ goto err;
+ }
+
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
"%s/backend-id", vtpmpath));
+ vtpminfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/state",
vtpmpath));
+ vtpminfo->state = val ? strtoul(val, NULL, 10) : -1;
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
"%s/event-channel", vtpmpath));
+ vtpminfo->evtch = val ? strtoul(val, NULL, 10) : -1;
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
"%s/ring-ref", vtpmpath));
+ vtpminfo->rref = val ? strtoul(val, NULL, 10) : -1;
+ vtpminfo->frontend = xs_read(ctx->xsh, XBT_NULL,
+ libxl__sprintf(gc, "%s/frontend",
vtpminfo->backend), NULL);
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc,
"%s/frontend-id", vtpminfo->backend));
+ vtpminfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
+
+ val = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/uuid",
vtpminfo->backend));
+ if(val == NULL) {
+ LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "%s/uuid does not exist!\n",
vtpminfo->backend);
+ goto err;
+ }
+ if(libxl_uuid_from_string(&(vtpminfo->uuid), val)) {
+ LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "%s/uuid is a malformed uuid??
(%s) Probably a bug!\n", vtpminfo->backend, val);
+ goto err;
+ }
+
+ goto exit;
+err:
+ rc = ERROR_FAIL;
+exit:
+ GC_FREE;
+ return rc;
+}
+
+int libxl_devid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
+ int devid, libxl_device_vtpm *vtpm)
+{

+    libxl_device_vtpm *vtpms;
+    int nb, i;
+    int rc;

+
+ vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
+ if (!vtpms)
+ return ERROR_FAIL;
+

+    memset(vtpm, 0, sizeof (libxl_device_vtpm));
+    rc = 1;
+    for (i = 0; i < nb; ++i) {
+        if(devid == vtpms[i].devid) {
+            vtpm->backend_domid = vtpms[i].backend_domid;
+            vtpm->devid = vtpms[i].devid;
+            libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid);
+            rc = 0;
+            break;

+ }
+ }
+
+ for (i=0; i<nb; i++)
+ libxl_device_vtpm_dispose(&vtpms[i]);
+ free(vtpms);
+ return rc;
+}
+
+ +/******************************************************************************/

int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic) {

tools/libxl/libxl.h +2 -1
@@ -477,13 +477,14 @@ typedef struct {
     libxl_domain_create_info c_info;
     libxl_domain_build_info b_info;
 
-    int num_disks, num_vifs, num_pcidevs, num_vfbs, num_vkbs;
+    int num_disks, num_vifs, num_pcidevs, num_vfbs, num_vkbs, num_vtpms;
 
     libxl_device_disk *disks;
     libxl_device_nic *vifs;
     libxl_device_pci *pcidevs;
     libxl_device_vfb *vfbs;
     libxl_device_vkb *vkbs;
+    libxl_device_vtpm *vtpms;
 
     libxl_action_on_shutdown on_poweroff;
     libxl_action_on_shutdown on_reboot;
@@ -679,6 +680,16 @@ libxl_device_nic *libxl_device_nic_list(libxl_ctx

*ctx, uint32_t domid, int *num
int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
libxl_device_nic *nic, libxl_nicinfo
*nicinfo);

+/* Virtual TPMs */
+int libxl_device_vtpm_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vtpm *vtpm);
+int libxl_device_vtpm_remove(libxl_ctx *ctx, uint32_t domid,
+ libxl_device_vtpm *vtpm,
+ const libxl_asyncop_how *ao_how);
+int libxl_device_vtpm_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vtpm *vtpm);
+libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *num);
+int libxl_device_vtpm_getinfo(libxl_ctx *ctx, uint32_t domid,
+ libxl_device_vtpm *vtpm, libxl_vtpminfo
*vtpminfo);
+
/* Keyboard */
int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid,
libxl_device_vkb *vkb);
int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,

tools/libxl/libxl_create.c +0 -0
@@ -54,6 +54,10 @@ void libxl_domain_config_dispose(libxl_domain_config

*d_config)

         libxl_device_vkb_dispose(&d_config->vkbs[i]);
     free(d_config->vkbs);

+ for (i=0; i<d_config->num_vtpms; i++)
+ libxl_device_vtpm_dispose(&d_config->vtpms[i]);
+ free(d_config->vtpms);
+

     libxl_domain_create_info_dispose(&d_config->c_info);
     libxl_domain_build_info_dispose(&d_config->b_info);
 }
@@ -732,6 +736,15 @@ static void domcreate_bootloader_done(libxl__egc *egc,
             goto error_out;
         }
     }

+ for (i = 0; i < d_config->num_vtpms; i++) {
+ ret = libxl_device_vtpm_add(ctx, domid, &d_config->vtpms[i]);
+ if (ret) {
+ LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+ "cannot add vtpm %d to domain: %d", i, ret);
+ ret = ERROR_FAIL;
+ goto error_out;
+ }
+ }

     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
     {
tools/libxl/libxl_dm.c +5 -0
@@ -811,6 +811,11 @@ retry_transaction:
         if (ret)
             goto out_free;
     }
+    for (i = 0; i < dm_config->num_vtpms; i++) {
+        ret = libxl_device_vtpm_add(ctx, dm_domid, &dm_config->vtpms[i]);
+        if (ret)
+            goto out_free;
+    }
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
         goto out_free;
tools/libxl/libxl_internal.h +0 -0
@@ -844,6 +844,7 @@ _hidden int

libxl__domain_build_info_setdefault(libxl__gc *gc,
_hidden int libxl__device_disk_setdefault(libxl__gc *gc,

                                           libxl_device_disk *disk);

_hidden int libxl__device_nic_setdefault(libxl__gc *gc,
libxl_device_nic *nic);
+_hidden int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm);
_hidden int libxl__device_vfb_setdefault(libxl__gc *gc,
libxl_device_vfb *vfb);
_hidden int libxl__device_vkb_setdefault(libxl__gc *gc,
libxl_device_vkb *vkb);
_hidden int libxl__device_pci_setdefault(libxl__gc *gc,
libxl_device_pci *pci);

tools/libxl/libxl_types.idl +18 -0
@@ -370,6 +370,12 @@ libxl_device_pci = Struct("device_pci", [
     ("permissive", bool),
     ])
 
+libxl_device_vtpm = Struct("device_vtpm", [
+    ("backend_domid",    libxl_domid),
+    ("devid",            libxl_devid),
+    ("uuid",             libxl_uuid),
+])
+
 libxl_diskinfo = Struct("diskinfo", [
     ("backend", string),
     ("backend_id", uint32),
@@ -393,6 +399,18 @@ libxl_nicinfo = Struct("nicinfo", [
     ("rref_rx", integer),
     ], dir=DIR_OUT)
 
+libxl_vtpminfo = Struct("vtpminfo", [
+    ("backend", string),
+    ("backend_id", uint32),
+    ("frontend", string),
+    ("frontend_id", uint32),
+    ("devid", libxl_devid),
+    ("state", integer),
+    ("evtch", integer),
+    ("rref", integer),
+    ("uuid", libxl_uuid),
+    ], dir=DIR_OUT)
+
 libxl_vcpuinfo = Struct("vcpuinfo", [
     ("vcpuid", uint32),
     ("cpu", uint32),
(quoted diff) +0 -0

b/tools/libxl/libxl_types_internal.idl

tools/libxl/libxl_types_internal.idl +1 -0
@@ -19,6 +19,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (5, "VFB"),
     (6, "VKBD"),
     (7, "CONSOLE"),
+    (8, "VTPM"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/libxl/libxl_utils.c +29 -0
@@ -454,6 +454,35 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2])
     return 0;
 }
 
+int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
+                            libxl_uuid* uuid, libxl_device_vtpm *vtpm)
+{
+    libxl_device_vtpm *vtpms;
+    int nb, i;
+    int rc;
+
+    vtpms = libxl_device_vtpm_list(ctx, domid, &nb);
+    if (!vtpms)
+        return ERROR_FAIL;
+
+    memset(vtpm, 0, sizeof (libxl_device_vtpm));
+    rc = 1;
+    for (i = 0; i < nb; ++i) {
+        if(!libxl_uuid_compare(uuid, &vtpms[i].uuid)) {
+            vtpm->backend_domid = vtpms[i].backend_domid;
+            vtpm->devid = vtpms[i].devid;
+            libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid);
+            rc = 0;
+            break;
+        }
+    }
+
+    for (i=0; i<nb; i++)
+        libxl_device_vtpm_dispose(&vtpms[i]);
+    free(vtpms);
+    return rc;
+}
+
 int libxl_mac_to_device_nic(libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic)
 {
tools/libxl/libxl_utils.h +0 -0
@@ -63,6 +63,11 @@ int libxl_devid_to_device_nic(libxl_ctx *ctx,

uint32_t domid, int devid,
int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid, const
char *vdev,

                                libxl_device_disk *disk);

+int libxl_uuid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
+ libxl_uuid *uuid, libxl_device_vtpm *vtpm);
+int libxl_devid_to_device_vtpm(libxl_ctx *ctx, uint32_t domid,
+ int devid, libxl_device_vtpm *vtpm);
+
int libxl_cpumap_alloc(libxl_ctx *ctx, libxl_cpumap *cpumap); int libxl_cpumap_test(libxl_cpumap *cpumap, int cpu); void libxl_cpumap_set(libxl_cpumap *cpumap, int cpu);

tools/libxl/xl.h +3 -0
@@ -79,6 +79,9 @@ int main_networkdetach(int argc, char **argv);
 int main_blockattach(int argc, char **argv);
 int main_blocklist(int argc, char **argv);
 int main_blockdetach(int argc, char **argv);
+int main_vtpmattach(int argc, char **argv);
+int main_vtpmlist(int argc, char **argv);
+int main_vtpmdetach(int argc, char **argv);
 int main_uptime(int argc, char **argv);
 int main_tmem_list(int argc, char **argv);
 int main_tmem_freeze(int argc, char **argv);
tools/libxl/xl_cmdimpl.c +1 -1
@@ -560,7 +560,7 @@ static void parse_config_data(const char *config_source,
     const char *buf;
     long l;
     XLU_Config *config;
-    XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids;
+    XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms;
     int pci_power_mgmt = 0;
     int pci_msitranslate = 1;
     int pci_permissive = 0;
@@ -911,6 +911,47 @@ static void parse_config_data(const char

*config_source,

         }
     }

+ if (!xlu_cfg_get_list(config, "vtpm", &vtpms, 0, 0)) {

+        d_config->num_vtpms = 0;
+        d_config->vtpms = NULL;
+        while ((buf = xlu_cfg_get_listitem (vtpms,
d_config->num_vtpms)) != NULL) {
+            libxl_device_vtpm *vtpm;
+            char * buf2 = strdup(buf);
+            char *p, *p2;

+
+ d_config->vtpms = (libxl_device_vtpm *)
realloc(d_config->vtpms, sizeof(libxl_device_vtpm) * (d_config->num_vtpms+1));

+            vtpm = d_config->vtpms + d_config->num_vtpms;
+            libxl_device_vtpm_init(vtpm);
+            vtpm->devid = d_config->num_vtpms;

+
+ p = strtok(buf2, ",");
+ if (!p)
+ goto skip_vtpm;
+ do {
+ while(*p == ' ')
+ ++p;
+ if ((p2 = strchr(p, '=')) == NULL)
+ break;
+ *p2 = '\0';
+ if (!strcmp(p, "backend")) {
+ if(domain_qualifier_to_domid(p2 + 1,
&(vtpm->backend_domid), 0))
+ {
+ fprintf(stderr, "Specified backend domain does
not exist, defaulting to Dom0\n");
+ vtpm->backend_domid = 0;
+ }
+ } else if(!strcmp(p, "uuid")) {
+ if( libxl_uuid_from_string(&vtpm->uuid, p2 + 1) ) {
+ fprintf(stderr, "Failed to parse vtpm UUID:
%s\n", p2 + 1);
+ exit(1);
+ }
+ }
+ } while ((p = strtok(NULL, ",")) != NULL);
+skip_vtpm:
+ free(buf2);
+ d_config->num_vtpms++;
+ }
+ }
+

     if (!xlu_cfg_get_list (config, "vif", &nics, 0, 0)) {
         d_config->num_vifs = 0;
         d_config->vifs = NULL;

@@ -936,7 +977,7 @@ static void parse_config_data(const char *config_source,

             p = strtok(buf2, ",");
             if (!p)
-                goto skip;
+                goto skip_nic;
             do {
                 while (*p == ' ')
                     p++;
@@ -993,7 +1034,7 @@ static void parse_config_data(const char
*config_source,
                     fprintf(stderr, "the accel parameter for vifs is
currently not supported\n");
                 }
             } while ((p = strtok(NULL, ",")) != NULL);
-skip:
+skip_nic:
             free(buf2);
             d_config->num_vifs++;
         }
@@ -5361,6 +5402,136 @@ int main_blockdetach(int argc, char **argv)
     return 0;

}

+int main_vtpmattach(int argc, char **argv)
+{

+    int opt;
+    libxl_device_vtpm vtpm;
+    char *oparg;
+    unsigned int val;

+
+ if ((opt = def_getopt(argc, argv, "", "vtpm-attach", 1)) != -1)
+ return opt;
+
+ if (argc-optind > 3) {
+ help("vtpm-attach");
+ return 0;
+ }
+
+ if (domain_qualifier_to_domid(argv[optind], &domid, 0) < 0) {
+ fprintf(stderr, "%s is an invalid domain identifier\n",
argv[optind]);
+ return 1;

+    }
+    libxl_device_vtpm_init(&vtpm);
+    for (argv += optind+1, argc -= optind+1; argc > 0; ++argv, --argc) {

+ if (MATCH_OPTION("uuid", *argv, oparg)) {
+ if(libxl_uuid_from_string(&(vtpm.uuid), oparg)) {
+ fprintf(stderr, "Invalid uuid specified (%s)\n", oparg);
+ return 1;
+ }
+ } else if (MATCH_OPTION("backend", *argv, oparg)) {
+ if(domain_qualifier_to_domid(oparg, &val, 0)) {
+ fprintf(stderr, "Specified backend domain does not
exist, defaulting to Dom0\n");
+ val = 0;
+ }
+ vtpm.backend_domid = val;
+ } else {
+ fprintf(stderr, "unrecognized argument `%s'\n", *argv);
+ return 1;
+ }
+ }
+
+ if(dryrun_only) {

+       char* json = libxl_device_vtpm_to_json(ctx, &vtpm);
+       printf("vtpm: %s\n", json);
+       free(json);
+       libxl_device_vtpm_dispose(&vtpm);
+       if (ferror(stdout) || fflush(stdout)) { perror("stdout");

exit(-1); }
+ return 0;
+ }
+
+ if (libxl_device_vtpm_add(ctx, domid, &vtpm)) {
+ fprintf(stderr, "libxl_device_vtpm_add failed.\n");
+ return 1;

+    }
+    libxl_device_vtpm_dispose(&vtpm);
+    return 0;

+}
+
+int main_vtpmlist(int argc, char **argv)
+{

+    int opt;
+    libxl_device_vtpm *vtpms;
+    libxl_vtpminfo vtpminfo;
+    int nb, i;

+
+ if ((opt = def_getopt(argc, argv, "", "vtpm-list", 0)) != -1)
+ return opt;
+
+ /* Idx BE UUID Hdl Sta evch rref BE-path */
+ printf("%-3s %-2s %-36s %-6s %-5s %-6s %-5s %-30s\n",
+ "Idx", "BE", "Uuid", "handle", "state", "evt-ch",
"ring-ref", "BE-path");
+ for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
+ if (domain_qualifier_to_domid(*argv, &domid, 0) < 0) {
+ fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
+ continue;
+ }
+ if (!(vtpms = libxl_device_vtpm_list(ctx, domid, &nb))) {
+ continue;
+ }
+ for (i = 0; i < nb; ++i) {
+ if(!libxl_device_vtpm_getinfo(ctx, domid, &vtpms[i],
&vtpminfo)) {

+              /* Idx BE */
+              printf("%-3d %-2d ", vtpminfo.devid, vtpminfo.backend_id);
+              /* MAC */
+              printf(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(vtpminfo.uuid));
+              /* Hdl  Sta  evch txr/rxr  BE-path */
+              printf(" %6d %5d %6d %8d %-30s\n",

+ vtpminfo.devid, vtpminfo.state, vtpminfo.evtch,
+ vtpminfo.rref, vtpminfo.backend);
+ libxl_vtpminfo_dispose(&vtpminfo);
+ }
+ libxl_device_vtpm_dispose(&vtpms[i]);
+ }
+ free(vtpms);
+ }
+ return 0;
+}
+
+int main_vtpmdetach(int argc, char **argv)
+{

+    int opt;
+    libxl_device_vtpm vtpm;
+    libxl_uuid uuid;

+
+ if ((opt = def_getopt(argc, argv, "", "vtpm-detach", 2)) != -1)
+ return opt;
+
+ if (domain_qualifier_to_domid(argv[optind], &domid, 0) < 0) {
+ fprintf(stderr, "%s is an invalid domain identifier\n",
argv[optind]);
+ return 1;
+ }
+
+ if ( libxl_uuid_from_string(&uuid, argv[optind+1])) {
+ if (libxl_devid_to_device_vtpm(ctx, domid,
atoi(argv[optind+1]), &vtpm)) {
+ fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
+ return 1;
+ }
+ } else {
+ if (libxl_uuid_to_device_vtpm(ctx, domid, &uuid, &vtpm)) {
+ fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
+ return 1;
+ }
+ }
+ if (libxl_device_vtpm_remove(ctx, domid, &vtpm, 0)) {
+ fprintf(stderr, "libxl_device_vtpm_del failed.\n");
+ return 1;

+    }
+    libxl_device_vtpm_dispose(&vtpm);
+    return 0;

+}
+
+
+
static char *uptime_to_string(unsigned long time, int short_mode)

 {
     int sec, min, hour, day;
tools/libxl/xl_cmdtable.c +15 -0
@@ -338,6 +338,21 @@ struct cmd_spec cmd_table[] = {
       "Destroy a domain's virtual block device",
       "<Domain> <DevId>",
     },
+    { "vtpm-attach",
+      &main_vtpmattach, 0, 1,
+      "Create a new virtual TPM device",
+      "<Domain> [uuid=<uuid>] [backend=<BackDomain>]",
+    },
+    { "vtpm-list",
+      &main_vtpmlist, 0, 0,
+      "List virtual TPM devices for a domain",
+      "<Domain(s)>",
+    },
+    { "vtpm-detach",
+      &main_vtpmdetach, 0, 1,
+      "Destroy a domain's virtual TPM device",
+      "<Domain> <DevId|uuid>",
+    },
     { "uptime",
       &main_uptime, 0, 0,
       "Print uptime for all/some domains",
Signature · 1 line
1.7.4.4

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

#1 lore raw
Quoted 5 lines · Matthew Fioravante

This patch adds vtpm support to libxl. It adds vtpm parsing to config files and 3 new xl commands:
vtpm-attach
vtpm-detach
vtpm-list

Please can you patch docs/man/xl* as appropriate too.

Quoted 24 lines

Signed off by: Matthew Fioravante [email protected]

tools/libxl/libxl.c +14 -0
@@ -2027,6 +2027,274 @@ int libxl__device_disk_local_detach(libxl__gc
 *gc, libxl_device_disk *disk)
 }
 
 /******************************************************************************/
+int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vtpm *vtpm)
+{
+   if(libxl_uuid_is_nil(&vtpm->uuid)) {
+      libxl_uuid_generate(&vtpm->uuid);
+   }
+   return 0;
+}
+
+static int libxl__device_from_vtpm(libxl__gc *gc, uint32_t domid,
+                                   libxl_device_vtpm *vtpm,
+                                   libxl__device *device)
+{
+   device->backend_devid   = vtpm->devid;
+   device->backend_domid   = vtpm->backend_domid;

tools/libxl/libxl.c int libxl__device_disk_local_detach(libxl__gc · line 2054

Quoted 10 lines
tools/libxl/libxl.c +9 -0
+   device->backend_kind    = LIBXL__DEVICE_KIND_VTPM;
+   device->devid           = vtpm->devid;
+   device->domid           = domid;
+   device->kind            = LIBXL__DEVICE_KIND_VTPM;
+
+   return 0;
+}
+
+int libxl_device_vtpm_add(libxl_ctx *ctx, uint32_t domid,
 libxl_device_vtpm *vtpm)

It looks like this patch is pretty badly whitespace damaged.

http://wiki.xen.org/wiki/Submitting_Xen_Patches has some hints on using mercurials patch bomb extension to avoid this sort of thing, and also a link to Linux's doc on how to configure various popular MUAs to not corrupt things.

I skimmed the rest of the patch regardless of that and it looks quite reasonable, thanks.

Ian.