From: Matthew Fioravante Subject: Re: [PATCH xm/xl enhancements for vptm 6/6] add vtpm support to libxl Date: Tue, 25 Sep 2012 12:11:55 -0400 Message-ID: <5061D7CB.3020707@jhuapl.edu> References: <505CBEB4.8060105@jhuapl.edu> <1348570160.3452.191.camel@zakaz.uk.xensource.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1254954324962400588==" Return-path: In-Reply-To: <1348570160.3452.191.camel@zakaz.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Campbell Cc: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a cryptographically signed message in MIME format. --===============1254954324962400588== Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms020408040900080808080009" This is a cryptographically signed message in MIME format. --------------ms020408040900080808080009 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 09/25/2012 06:49 AM, Ian Campbell wrote: > On Fri, 2012-09-21 at 20:23 +0100, Matthew Fioravante wrote: >> Add support for vtpm=3D["VTPM_SPEC",...] to domain config files. Also = add >> commands vtpm-attach, vtpm-list, and vtpm-detach. >> >> Signed off by Matthew Fioravante matthew.fioravante@jhuapl.edu >> >> --- >> Changes since previous: >> * Rebased to latest xen >> * Updated xl.cfg and xl manpages >> >> diff --git a/docs/man/xl.cfg.pod.5 b/docs/man/xl.cfg.pod.5 >> --- a/docs/man/xl.cfg.pod.5 >> +++ b/docs/man/xl.cfg.pod.5 >> @@ -298,6 +298,35 @@ Specifies the networking provision (both emulated= >> network adapters, >> and Xen virtual interfaces) to provided to the guest. See >> F. >> =20 >> +=3Ditem B >> + >> +Specifies the virtual trusted platform module to be > can there be more than one? There can be more than one. I'm not sure if anyone would ever do this but the capability is there. I don't see any good reason to restrict it. Also linux usually calls your tpm /dev/tpm0, so I guess its theoretically possible to have 2 or more tpms. > >> +provided to the guest. Please see F >> +for more details. >> + >> +Each B is a comma-separated list of C >> +settings, from the following list: >> + >> +=3Dover 4 >> + >> +=3Ditem C >> + >> +Specify the backend domain name of id. This value must be >> +set if you are using the vtpm domain model. If this domain >> +is a guest, the backend should be set to the vtpm domain name. >> +If this domain is a vtpm, the backend should be set to the >> +vtpm manager domain name. The default value is domain 0, >> +which should be used if you are running the vtpm process model. > I had a look in docs/misc/vtpm.txt but didn't see anything which > explained "vtpm process model" vs "vtpm manager domain" vs "vtpm > domain". I suppose that's part of the future doc work you were talking > about ;-) Yes, that vtpm.txt is very old. > >> + >> +=3Ditem C >> + >> +Specify the uuid of this vtpm device. The uuid is used to uniquely >> +identify the vtpm device. You can create one using the uuidgen >> +program on unix systems. If left unspecified, a new uuid >> +will be randomly generated everytime the domain boots. > ^missing space here Thanks fixed below >> + >> +=3Dback >> + >> =3Ditem B >> =20 >> Specifies the paravirtual framebuffer devices which should be supplie= d >> [..] >> diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c >> --- a/tools/libxl/libxl.c >> +++ b/tools/libxl/libxl.c > [...] >> +static void domcreate_attach_vtpms(libxl__egc *egc, libxl__multidev >> *multidev, int ret) { >> + libxl__domain_create_state *dcs =3D CONTAINER_OF(multidev, *dcs, >> multidev); >> + STATE_AO_GC(dcs->ao); >> + int domid =3D dcs->guest_domid; >> + >> + libxl_domain_config* const d_config =3D dcs->guest_config; >> + >> + if(ret) { >> + LOG(ERROR, "unable to add nic devices"); >> + goto error_out; >> + } >> + >> + /* Plug nic interfaces */ > You mean vtpms here. Also fixed >> +int main_vtpmdetach(int argc, char **argv) >> +{ >> + uint32_t domid; >> + int opt, rc=3D0; >> + libxl_device_vtpm vtpm; >> + libxl_uuid uuid; >> + >> + if ((opt =3D def_getopt(argc, argv, "", "vtpm-detach", 2)) !=3D -= 1) >> + return opt; >> + >> + domid =3D find_domain(argv[optind]); >> + >> + if ( libxl_uuid_from_string(&uuid, argv[optind+1])) { > Why does vtpm use/need UUID's for identification rather than just a > domid+devid like other device types? > > Is the UUID used for something more than identification? It uses uuids for long term identification. Each time the vtpm saves its data to a block device its encrypted with a key. That key is sent to the vtpm manager for secure storage. When the vtpm boots it requests the key from the manager. The uuid is used to tell the manager which vtpm this is. dom/devid cannot be used because it changes on every boot of the vm. > > Ian. > Signed off by Matthew Fioravante matthew.fioravante@jhuapl.edu --- Changes since previous: * Rebased to latest xen * Updated xl.cfg and xl manpages * Fixed typos in docs and comments diff --git a/docs/man/xl.cfg.pod.5 b/docs/man/xl.cfg.pod.5 --- a/docs/man/xl.cfg.pod.5 +++ b/docs/man/xl.cfg.pod.5 @@ -298,6 +298,35 @@ Specifies the networking provision (both emulated ne= twork adapters, and Xen virtual interfaces) to provided to the guest. See F. =20 +=3Ditem B + +Specifies the virtual trusted platform module to be +provided to the guest. Please see F +for more details. + +Each B is a comma-separated list of C +settings, from the following list: + +=3Dover 4 + +=3Ditem C + +Specify the backend domain name of id. This value must be +set if you are using the vtpm domain model. If this domain +is a guest, the backend should be set to the vtpm domain name. +If this domain is a vtpm, the backend should be set to the +vtpm manager domain name. The default value is domain 0, +which should be used if you are running the vtpm process model. + +=3Ditem C + +Specify the uuid of this vtpm device. The uuid is used to uniquely +identify the vtpm device. You can create one using the uuidgen +program on unix systems. If left unspecified, a new uuid +will be randomly generated every time the domain boots. + +=3Dback + =3Ditem B =20 Specifies the paravirtual framebuffer devices which should be supplied diff --git a/docs/man/xl.pod.1 b/docs/man/xl.pod.1 --- a/docs/man/xl.pod.1 +++ b/docs/man/xl.pod.1 @@ -1101,6 +1101,31 @@ List virtual network interfaces for a domain. =20 =3Dback =20 +=3Dhead2 VTPM DEVICES + +=3Dover 4 + +=3Ditem B I I + +Creates a new vtpm device in the domain specified by I. +I describes the device to attach, using the same format as = the +B string in the domain config file. See L for +more information. + +=3Ditem B I I + +Removes the vtpm device from the domain specified by I. +I is the numeric device id given to the virtual trusted +platform module device. You will need to run B to determin= e that number. +Alternatively the I of the vtpm can be used to +select the virtual device to detach. + +=3Ditem B I + +List virtual trusted platform modules for a domain. + +=3Dback + =3Dhead1 PCI PASS-THROUGH =20 =3Dover 4 diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1726,6 +1726,246 @@ out: } =20 /***********************************************************************= *******/ +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 =3D vtpm->devid; + device->backend_domid =3D vtpm->backend_domid; + device->backend_kind =3D LIBXL__DEVICE_KIND_VTPM; + device->devid =3D vtpm->devid; + device->domid =3D domid; + device->kind =3D LIBXL__DEVICE_KIND_VTPM; + + return 0; +} + +void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, + libxl_device_vtpm *vtpm, + libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + flexarray_t *front; + flexarray_t *back; + libxl__device *device; + char *dompath, **l; + unsigned int nb, rc; + + rc =3D libxl__device_vtpm_setdefault(gc, vtpm); + if (rc) goto out; + + front =3D flexarray_make(16, 1); + if (!front) { + rc =3D ERROR_NOMEM; + goto out; + } + back =3D flexarray_make(16, 1); + if (!back) { + rc =3D ERROR_NOMEM; + goto out; + } + + if(vtpm->devid =3D=3D -1) { + if (!(dompath =3D libxl__xs_get_dompath(gc, domid))) { + rc =3D ERROR_FAIL; + goto out_free; + } + l =3D libxl__xs_directory(gc, XBT_NULL, libxl__sprintf(gc, "%s/d= evice/vtpm", dompath), &nb); + if(l =3D=3D NULL || nb =3D=3D 0) { + vtpm->devid =3D 0; + } else { + vtpm->devid =3D strtoul(l[nb - 1], NULL, 10) + 1; + } + } + + GCNEW(device); + rc =3D libxl__device_from_vtpm(gc, domid, vtpm, device); + if ( rc !=3D 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 THI= S */ + 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)); + + aodev->dev =3D device; + aodev->action =3D DEVICE_CONNECT; + libxl__wait_device_connection(egc, aodev); + + rc =3D 0; +out_free: + flexarray_free(back); + flexarray_free(front); +out: + aodev->rc =3D rc; + if(rc) aodev->callback(egc, aodev); + return; +} + +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 =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/handle", = fe_path)); + if (tmp) { + vtpm->devid =3D atoi(tmp); + } + tmp =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/backend-i= d", fe_path)); + if(tmp) { + vtpm->backend_domid =3D atoi(tmp); + } + tmp =3D 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 =3D NULL; + char* fe_path =3D NULL; + char** dir =3D NULL; + unsigned int ndirs =3D 0; + + *num =3D 0; + + fe_path =3D libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompa= th(gc, domid)); + dir =3D libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs); + if(dir) { + vtpms =3D malloc(sizeof(*vtpms) * ndirs); + libxl_device_vtpm* vtpm; + libxl_device_vtpm* end =3D vtpms + ndirs; + for(vtpm =3D vtpms; vtpm < end; ++vtpm, ++dir) { + const char* path =3D libxl__sprintf(gc, "%s/%s", fe_path, *dir= ); + libxl__device_vtpm_from_xs_fe(gc, path, vtpm); + } + } + *num =3D 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 =3D 0; + + libxl_vtpminfo_init(vtpminfo); + dompath =3D libxl__xs_get_dompath(gc, domid); + vtpminfo->devid =3D vtpm->devid; + + vtpmpath =3D libxl__sprintf(gc, "%s/device/vtpm/%d", dompath, vtpmin= fo->devid); + vtpminfo->backend =3D xs_read(ctx->xsh, XBT_NULL, + libxl__sprintf(gc, "%s/backend", vtpmpat= h), NULL); + if (!vtpminfo->backend) { + goto err; + } + if(!libxl__xs_read(gc, XBT_NULL, vtpminfo->backend)) { + goto err; + } + + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/backend-= id", vtpmpath)); + vtpminfo->backend_id =3D val ? strtoul(val, NULL, 10) : -1; + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/state", = vtpmpath)); + vtpminfo->state =3D val ? strtoul(val, NULL, 10) : -1; + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/event-ch= annel", vtpmpath)); + vtpminfo->evtch =3D val ? strtoul(val, NULL, 10) : -1; + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/ring-ref= ", vtpmpath)); + vtpminfo->rref =3D val ? strtoul(val, NULL, 10) : -1; + vtpminfo->frontend =3D xs_read(ctx->xsh, XBT_NULL, + libxl__sprintf(gc, "%s/frontend", vtpmi= nfo->backend), NULL); + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/frontend= -id", vtpminfo->backend)); + vtpminfo->frontend_id =3D val ? strtoul(val, NULL, 10) : -1; + + val =3D libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/uuid", v= tpminfo->backend)); + if(val =3D=3D NULL) { + LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "%s/uuid does not exist!\n", vt= pminfo->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 =3D 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 =3D libxl_device_vtpm_list(ctx, domid, &nb); + if (!vtpms) + return ERROR_FAIL; + + memset(vtpm, 0, sizeof (libxl_device_vtpm)); + rc =3D 1; + for (i =3D 0; i < nb; ++i) { + if(devid =3D=3D vtpms[i].devid) { + vtpm->backend_domid =3D vtpms[i].backend_domid; + vtpm->devid =3D vtpms[i].devid; + libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid); + rc =3D 0; + break; + } + } + + for (i=3D0; ivkbs[i]); free(d_config->vkbs); =20 + for (i=3D0; inum_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); } @@ -601,6 +605,8 @@ static void domcreate_bootloader_done(libxl__egc *egc= , static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *aodevs= , int ret); =20 +static void domcreate_attach_vtpms(libxl__egc *egc, libxl__multidev *mul= tidev, + int ret); static void domcreate_attach_pci(libxl__egc *egc, libxl__multidev *aodev= s, int ret); =20 @@ -1084,13 +1090,13 @@ static void domcreate_devmodel_started(libxl__egc= *egc, if (d_config->num_nics > 0) { /* Attach nics */ libxl__multidev_begin(ao, &dcs->multidev); - dcs->multidev.callback =3D domcreate_attach_pci; + dcs->multidev.callback =3D domcreate_attach_vtpms; libxl__add_nics(egc, ao, domid, d_config, &dcs->multidev); libxl__multidev_prepared(egc, &dcs->multidev, 0); return; } =20 - domcreate_attach_pci(egc, &dcs->multidev, 0); + domcreate_attach_vtpms(egc, &dcs->multidev, 0); return; =20 error_out: @@ -1098,6 +1104,36 @@ error_out: domcreate_complete(egc, dcs, ret); } =20 +static void domcreate_attach_vtpms(libxl__egc *egc, libxl__multidev *mul= tidev, int ret) { + libxl__domain_create_state *dcs =3D CONTAINER_OF(multidev, *dcs, mult= idev); + STATE_AO_GC(dcs->ao); + int domid =3D dcs->guest_domid; + + libxl_domain_config* const d_config =3D dcs->guest_config; + + if(ret) { + LOG(ERROR, "unable to add nic devices"); + goto error_out; + } + + /* Plug vtpm devices */ + if (d_config->num_vtpms > 0) { + /* Attach vtpms */ + libxl__multidev_begin(ao, &dcs->multidev); + dcs->multidev.callback =3D domcreate_attach_pci; + libxl__add_vtpms(egc, ao, domid, d_config, &dcs->multidev); + libxl__multidev_prepared(egc, &dcs->multidev, 0); + return; + } + + domcreate_attach_pci(egc, multidev, 0); + return; + +error_out: + assert(ret); + domcreate_complete(egc, dcs, ret); +} + static void domcreate_attach_pci(libxl__egc *egc, libxl__multidev *multi= dev, int ret) { @@ -1111,7 +1147,7 @@ static void domcreate_attach_pci(libxl__egc *egc, l= ibxl__multidev *multidev, libxl_domain_config *const d_config =3D dcs->guest_config; =20 if (ret) { - LOG(ERROR, "unable to add nic devices"); + LOG(ERROR, "unable to add vtpm devices"); goto error_out; } =20 diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -515,6 +515,7 @@ void libxl__multidev_prepared(libxl__egc *egc, =20 DEFINE_DEVICES_ADD(disk) DEFINE_DEVICES_ADD(nic) +DEFINE_DEVICES_ADD(vtpm) =20 #undef DEFINE_DEVICES_ADD =20 diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -954,6 +954,7 @@ _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, uint32_t domid); +_hidden int libxl__device_vtpm_setdefault(libxl__gc *gc, libxl_device_vt= pm *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); @@ -1975,6 +1976,10 @@ _hidden void libxl__device_nic_add(libxl__egc *egc= , uint32_t domid, libxl_device_nic *nic, libxl__ao_device *aodev); =20 +_hidden void libxl__device_vtpm_add(libxl__egc *egc, uint32_t domid, + libxl_device_vtpm *vtpm, + libxl__ao_device *aodev); + /* Internal function to connect a vkb device */ _hidden int libxl__device_vkb_add(libxl__gc *gc, uint32_t domid, libxl_device_vkb *vkb); @@ -2407,6 +2412,10 @@ _hidden void libxl__add_nics(libxl__egc *egc, libx= l__ao *ao, uint32_t domid, libxl_domain_config *d_config, libxl__multidev *multidev); =20 +_hidden void libxl__add_vtpms(libxl__egc *egc, libxl__ao *ao, uint32_t d= omid, + libxl_domain_config *d_config, + libxl__multidev *multidev); + /*----- device model creation -----*/ =20 /* First layer; wraps libxl__spawn_spawn. */ diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -395,6 +395,12 @@ libxl_device_pci =3D Struct("device_pci", [ ("permissive", bool), ]) =20 +libxl_device_vtpm =3D Struct("device_vtpm", [ + ("backend_domid", libxl_domid), + ("devid", libxl_devid), + ("uuid", libxl_uuid), +]) + libxl_diskinfo =3D Struct("diskinfo", [ ("backend", string), ("backend_id", uint32), @@ -418,6 +424,18 @@ libxl_nicinfo =3D Struct("nicinfo", [ ("rref_rx", integer), ], dir=3DDIR_OUT) =20 +libxl_vtpminfo =3D 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=3DDIR_OUT) + libxl_vcpuinfo =3D Struct("vcpuinfo", [ ("vcpuid", uint32), ("cpu", uint32), diff --git a/tools/libxl/libxl_types_internal.idl b/tools/libxl/libxl_typ= es_internal.idl --- a/tools/libxl/libxl_types_internal.idl +++ b/tools/libxl/libxl_types_internal.idl @@ -19,6 +19,7 @@ libxl__device_kind =3D Enumeration("device_kind", [ (5, "VFB"), (6, "VKBD"), (7, "CONSOLE"), + (8, "VTPM"), ]) =20 libxl__console_backend =3D Enumeration("console_backend", [ diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -463,6 +463,35 @@ int libxl_pipe(libxl_ctx *ctx, int pipes[2]) return 0; } =20 +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 =3D libxl_device_vtpm_list(ctx, domid, &nb); + if (!vtpms) + return ERROR_FAIL; + + memset(vtpm, 0, sizeof (libxl_device_vtpm)); + rc =3D 1; + for (i =3D 0; i < nb; ++i) { + if(!libxl_uuid_compare(uuid, &vtpms[i].uuid)) { + vtpm->backend_domid =3D vtpms[i].backend_domid; + vtpm->devid =3D vtpms[i].devid; + libxl_uuid_copy(&vtpm->uuid, &vtpms[i].uuid); + rc =3D 0; + break; + } + } + + for (i=3D0; inum_vtpms =3D 0; + d_config->vtpms =3D NULL; + while ((buf =3D xlu_cfg_get_listitem (vtpms, d_config->num_vtpms= )) !=3D NULL) { + libxl_device_vtpm *vtpm; + char * buf2 =3D strdup(buf); + char *p, *p2; + + d_config->vtpms =3D (libxl_device_vtpm *) realloc(d_config->= vtpms, sizeof(libxl_device_vtpm) * (d_config->num_vtpms+1)); + vtpm =3D d_config->vtpms + d_config->num_vtpms; + libxl_device_vtpm_init(vtpm); + vtpm->devid =3D d_config->num_vtpms; + + p =3D strtok(buf2, ","); + if (!p) + goto skip_vtpm; + do { + while(*p =3D=3D ' ') + ++p; + if ((p2 =3D strchr(p, '=3D')) =3D=3D NULL) + break; + *p2 =3D '\0'; + if (!strcmp(p, "backend")) { + if(domain_qualifier_to_domid(p2 + 1, &(vtpm->backend= _domid), 0)) + { + fprintf(stderr, "Specified backend domain does n= ot exist, defaulting to Dom0\n"); + vtpm->backend_domid =3D 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 =3D strtok(NULL, ",")) !=3D NULL); +skip_vtpm: + free(buf2); + d_config->num_vtpms++; + } + } + if (!xlu_cfg_get_list (config, "vif", &nics, 0, 0)) { d_config->num_nics =3D 0; d_config->nics =3D NULL; @@ -1070,7 +1111,7 @@ static void parse_config_data(const char *config_so= urce, =20 p =3D strtok(buf2, ","); if (!p) - goto skip; + goto skip_nic; do { while (*p =3D=3D ' ') p++; @@ -1134,7 +1175,7 @@ static void parse_config_data(const char *config_so= urce, fprintf(stderr, "the accel parameter for vifs is cur= rently not supported\n"); } } while ((p =3D strtok(NULL, ",")) !=3D NULL); -skip: +skip_nic: free(buf2); d_config->num_nics++; } @@ -5570,6 +5611,131 @@ int main_blockdetach(int argc, char **argv) return rc; } =20 +int main_vtpmattach(int argc, char **argv) +{ + int opt; + libxl_device_vtpm vtpm; + char *oparg; + unsigned int val; + uint32_t domid; + + if ((opt =3D def_getopt(argc, argv, "", "vtpm-attach", 1)) !=3D -1) + return opt; + + if (domain_qualifier_to_domid(argv[optind], &domid, 0) < 0) { + fprintf(stderr, "%s is an invalid domain identifier\n", argv[opt= ind]); + return 1; + } + ++optind; + + libxl_device_vtpm_init(&vtpm); + for (argv +=3D optind, argc -=3D optind; 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 =3D 0; + } + vtpm.backend_domid =3D val; + } else { + fprintf(stderr, "unrecognized argument `%s'\n", *argv); + return 1; + } + } + + if(dryrun_only) { + char* json =3D 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, 0)) { + 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 =3D def_getopt(argc, argv, "", "vtpm-list", 1)) !=3D -1) + return opt; + + /* Idx BE UUID Hdl Sta evch rref BE-path */ + printf("%-3s %-2s %-36s %-6s %-5s %-6s %-5s %-10s\n", + "Idx", "BE", "Uuid", "handle", "state", "evt-ch", "ring-ref",= "BE-path"); + for (argv +=3D optind, argc -=3D optind; argc > 0; --argc, ++argv) {= + uint32_t domid; + if (domain_qualifier_to_domid(*argv, &domid, 0) < 0) { + fprintf(stderr, "%s is an invalid domain identifier\n", *arg= v); + continue; + } + if (!(vtpms =3D libxl_device_vtpm_list(ctx, domid, &nb))) { + continue; + } + for (i =3D 0; i < nb; ++i) { + if(!libxl_device_vtpm_getinfo(ctx, domid, &vtpms[i], &vtpminf= o)) { + /* Idx BE UUID Hdl Sta evch rref BE-= path*/ + printf("%-3d %-2d " LIBXL_UUID_FMT " %6d %5d %6d %8d %-30s= \n", + vtpminfo.devid, vtpminfo.backend_id, + LIBXL_UUID_BYTES(vtpminfo.uuid), + 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) +{ + uint32_t domid; + int opt, rc=3D0; + libxl_device_vtpm vtpm; + libxl_uuid uuid; + + if ((opt =3D def_getopt(argc, argv, "", "vtpm-detach", 2)) !=3D -1) + return opt; + + domid =3D find_domain(argv[optind]); + + 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; + } + } + rc =3D libxl_device_vtpm_remove(ctx, domid, &vtpm, 0); + if (rc) { + fprintf(stderr, "libxl_device_vtpm_remove failed.\n"); + } + libxl_device_vtpm_dispose(&vtpm); + return rc; +} + + static char *uptime_to_string(unsigned long uptime, int short_mode) { int sec, min, hour, day; diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -338,6 +338,21 @@ struct cmd_spec cmd_table[] =3D { "Destroy a domain's virtual block device", " ", }, + { "vtpm-attach", + &main_vtpmattach, 0, 1, + "Create a new virtual TPM device", + " [uuid=3D] [backend=3D]", + }, + { "vtpm-list", + &main_vtpmlist, 0, 0, + "List virtual TPM devices for a domain", + "", + }, + { "vtpm-detach", + &main_vtpmdetach, 0, 1, + "Destroy a domain's virtual TPM device", + " ", + }, { "uptime", &main_uptime, 0, 0, "Print uptime for all/some domains", --------------ms020408040900080808080009 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIDyjCC A8YwggMvoAMCAQICBD/xyf0wDQYJKoZIhvcNAQEFBQAwLzELMAkGA1UEBhMCVVMxDzANBgNV BAoTBkpIVUFQTDEPMA0GA1UECxMGQklTRENBMB4XDTEwMDYxMTE4MjIwNloXDTEzMDYxMTE4 NTIwNlowZjELMAkGA1UEBhMCVVMxDzANBgNVBAoTBkpIVUFQTDEPMA0GA1UECxMGUGVvcGxl MTUwFgYDVQQLEw9WUE5Hcm91cC1CSVNEQ0EwGwYDVQQDExRNYXR0aGV3IEUgRmlvcmF2YW50 ZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAnpbwVSP6o1Nb5lcW7dd3yTo9iBJdi7qz 4nANOMFPK7JOy5npKN1iiousl28U/scUJES55gPwAWYJK3uVyQAsA4adgDKi5DoD1UHDQEwp bY7iHLJeq0NPr4BqYNqnCFPbE6HC8zSJrr4qKn+gVUQT39SIFqdiIPJwZL8FYTRQ/zsCAwEA AaOCAbYwggGyMAsGA1UdDwQEAwIHgDArBgNVHRAEJDAigA8yMDEwMDYxMTE4MjIwNlqBDzIw MTIwNzE3MjI1MjA2WjAbBg0rBgEEAbMlCwMBAQEBBAoWCGZpb3JhbWUxMBsGDSsGAQQBsyUL AwEBAQIEChIIMDAxMDQyNjEwWAYJYIZIAYb6ax4BBEsMSVRoZSBwcml2YXRlIGtleSBjb3Jy ZXNwb25kaW5nIHRvIHRoaXMgY2VydGlmaWNhdGUgbWF5IGhhdmUgYmVlbiBleHBvcnRlZC4w KAYDVR0RBCEwH4EdTWF0dGhldy5GaW9yYXZhbnRlQGpodWFwbC5lZHUwUgYDVR0fBEswSTBH oEWgQ6RBMD8xCzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNVBAsTBkJJU0RD QTEOMAwGA1UEAxMFQ1JMNTYwHwYDVR0jBBgwFoAUCDUpmxH52EU2CyWmF2EJMB1yqeswHQYD VR0OBBYEFO6LYxg6r9wHZ+zdQtBHn1dZ/YTNMAkGA1UdEwQCMAAwGQYJKoZIhvZ9B0EABAww ChsEVjcuMQMCBLAwDQYJKoZIhvcNAQEFBQADgYEAJO9HQh4YNChVLzuZqK5ARJARD8JoujGZ fdo75quvg2jXFQe2sEjvLnxJZgm/pv8fdZakq48CWwjYHKuvIp7sDjTEsQfo+y7SpN/N2NvJ WU5SqfK1VgYtNLRRoGJUB5Q1aZ+Dg95g3kqpyfpUMISJL8IKVLtJVfN4fggFVUYZ9wwxggGr MIIBpwIBATA3MC8xCzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNVBAsTBkJJ U0RDQQIEP/HJ/TAJBgUrDgMCGgUAoIHLMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJ KoZIhvcNAQkFMQ8XDTEyMDkyNTE2MTE1NVowIwYJKoZIhvcNAQkEMRYEFEITSUQL2rhd8Cjb W8dPpxpMLqLTMGwGCSqGSIb3DQEJDzFfMF0wCwYJYIZIAWUDBAEqMAsGCWCGSAFlAwQBAjAK BggqhkiG9w0DBzAOBggqhkiG9w0DAgICAIAwDQYIKoZIhvcNAwICAUAwBwYFKw4DAgcwDQYI KoZIhvcNAwICASgwDQYJKoZIhvcNAQEBBQAEgYBqvpq5U7WCOF0P30eDi89MNSj1tUxQvW1B bVSKk26zlDNj3ADh7YdBISYtec3neLyXK3v6DcEuQozlZ3P1sc7QUPf58OEQN5znsWdTRyiW 4XJlM6u+CMMQ/SwjOF5M3/YpDUK6JrQoteHRi7QyzPmi5VmnrMq5Sk9dEKfXcSd02wAAAAAA AA== --------------ms020408040900080808080009-- --===============1254954324962400588== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --===============1254954324962400588==--