From nobody Mon Feb 9 09:16:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1491575932713549.38191753432; Fri, 7 Apr 2017 07:38:52 -0700 (PDT) Received: from localhost ([::1]:51206 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cwV2J-0002rX-F3 for importer@patchew.org; Fri, 07 Apr 2017 10:38:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60456) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cwUuO-0005DX-MD for qemu-devel@nongnu.org; Fri, 07 Apr 2017 10:30:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cwUuL-0002cJ-V9 for qemu-devel@nongnu.org; Fri, 07 Apr 2017 10:30:40 -0400 Received: from mga03.intel.com ([134.134.136.65]:63385) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cwUuL-0002ZL-IS for qemu-devel@nongnu.org; Fri, 07 Apr 2017 10:30:37 -0400 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Apr 2017 07:30:37 -0700 Received: from avallurigigabyte.fi.intel.com ([10.237.72.170]) by fmsmga004.fm.intel.com with ESMTP; 07 Apr 2017 07:30:35 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1491575437; x=1523111437; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=iQMHpNCrTEVxLYAspwMf1cxDgnhCNfr/OdotNxBHXE8=; b=ROeDc9xSUywqXeUGH2a7kgWp5UhewBBQkEK/h4rdJbeiQS2Mxwgd6VjX lsJekQ47/t8iMuwyb26QQm8W6g+asw==; X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.37,165,1488873600"; d="scan'208";a="245879768" From: Amarnath Valluri To: qemu-devel@nongnu.org Date: Fri, 7 Apr 2017 17:30:27 +0300 Message-Id: <1491575431-32170-6-git-send-email-amarnath.valluri@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1491575431-32170-1-git-send-email-amarnath.valluri@intel.com> References: <1491575431-32170-1-git-send-email-amarnath.valluri@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [PATCH v2 5/9] tmp backend: Add new api to read backend TpmInfo X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amarnath Valluri , patrick.ohly@intel.com, marcandre.lureau@gmail.com, stefanb@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" TPM configuration options are backend implementation details and shall not = be part of base TPMBackend object, and these shall not be accessed directly ou= tside of the class, hence added a new interface method, get_tpm_options() to TPMDriverOps., which shall be implemented by the derived classes to return configured tpm options. A new tpm backend api - tpm_backend_query_tpm() which uses _get_tpm_options= () to prepare TpmInfo. Made TPMPassthroughOptions type inherited from newly defined TPMOptions bas= e type. The TPMOptions base type is intentionally left empty and supposed to be inherited by all backend implementations to define their tpm configuration options. Signed-off-by: Amarnath Valluri --- backends/tpm.c | 24 +++++++++++++++++-- hmp.c | 10 ++++---- hw/tpm/tpm_passthrough.c | 57 +++++++++++++++++++++++++++++++++++-----= ---- include/sysemu/tpm_backend.h | 25 +++++++++++++++++-- qapi-schema.json | 41 +++++++++++++++---------------- tpm.c | 32 +------------------------ 6 files changed, 118 insertions(+), 71 deletions(-) diff --git a/backends/tpm.c b/backends/tpm.c index c2be17b..c96f462 100644 --- a/backends/tpm.c +++ b/backends/tpm.c @@ -156,6 +156,28 @@ TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) return k->ops->get_tpm_version(s); } =20 +TPMOptions *tpm_backend_get_tpm_options(TPMBackend *s) +{ + TPMBackendClass *k =3D TPM_BACKEND_GET_CLASS(s); + + assert(k->ops->get_tpm_options); + + return k->ops->get_tpm_options(s); +} + +TPMInfo *tpm_backend_query_tpm(TPMBackend *s) +{ + TPMBackendClass *k =3D TPM_BACKEND_GET_CLASS(s); + TPMInfo *info =3D g_new0(TPMInfo, 1); + + info->type =3D k->ops->type; + info->id =3D g_strdup(s->id); + info->model =3D s->fe_model; + info->options =3D tpm_backend_get_tpm_options(s); + + return info; +} + static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) { TPMBackend *s =3D TPM_BACKEND(obj); @@ -210,8 +232,6 @@ static void tpm_backend_instance_finalize(Object *obj) TPMBackend *s =3D TPM_BACKEND(obj); =20 g_free(s->id); - g_free(s->path); - g_free(s->cancel_path); tpm_backend_thread_end(s); } =20 diff --git a/hmp.c b/hmp.c index edb8970..9caf7c8 100644 --- a/hmp.c +++ b/hmp.c @@ -955,18 +955,18 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict) c, TpmModel_lookup[ti->model]); =20 monitor_printf(mon, " \\ %s: type=3D%s", - ti->id, TpmTypeOptionsKind_lookup[ti->options->type= ]); + ti->id, TpmType_lookup[ti->type]); =20 - switch (ti->options->type) { - case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH: - tpo =3D ti->options->u.passthrough.data; + switch (ti->type) { + case TPM_TYPE_PASSTHROUGH: + tpo =3D (TPMPassthroughOptions *)ti->options; monitor_printf(mon, "%s%s%s%s", tpo->has_path ? ",path=3D" : "", tpo->has_path ? tpo->path : "", tpo->has_cancel_path ? ",cancel-path=3D" : "", tpo->has_cancel_path ? tpo->cancel_path : ""); break; - case TPM_TYPE_OPTIONS_KIND__MAX: + default: break; } monitor_printf(mon, "\n"); diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 0f543bd..71bdf25 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -49,6 +49,7 @@ struct TPMPassthruState { TPMBackend parent; =20 + TPMPassthroughOptions *ops; char *tpm_dev; int tpm_fd; bool tpm_executing; @@ -313,15 +314,14 @@ static TPMVersion tpm_passthrough_get_tpm_version(TPM= Backend *tb) * in Documentation/ABI/stable/sysfs-class-tpm. * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel */ -static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) +static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt) { - TPMPassthruState *tpm_pt =3D TPM_PASSTHROUGH(tb); int fd =3D -1; char *dev; char path[PATH_MAX]; =20 - if (tb->cancel_path) { - fd =3D qemu_open(tb->cancel_path, O_WRONLY); + if (tpm_pt->ops->cancel_path) { + fd =3D qemu_open(tpm_pt->ops->cancel_path, O_WRONLY); if (fd < 0) { error_report("Could not open TPM cancel path : %s", strerror(errno)); @@ -336,7 +336,7 @@ static int tpm_passthrough_open_sysfs_cancel(TPMBackend= *tb) dev) < sizeof(path)) { fd =3D qemu_open(path, O_WRONLY); if (fd >=3D 0) { - tb->cancel_path =3D g_strdup(path); + tpm_pt->ops->cancel_path =3D g_strdup(path); } else { error_report("tpm_passthrough: Could not open TPM cancel " "path %s : %s", path, strerror(errno)); @@ -356,17 +356,24 @@ static int tpm_passthrough_handle_device_opts(QemuOpt= s *opts, TPMBackend *tb) const char *value; =20 value =3D qemu_opt_get(opts, "cancel-path"); - tb->cancel_path =3D g_strdup(value); + if (value) { + tpm_pt->ops->cancel_path =3D g_strdup(value); + tpm_pt->ops->has_cancel_path =3D true; + } else { + tpm_pt->ops->has_cancel_path =3D false; + } =20 value =3D qemu_opt_get(opts, "path"); if (!value) { value =3D TPM_PASSTHROUGH_DEFAULT_DEVICE; + tpm_pt->ops->has_path =3D false; + } else { + tpm_pt->ops->has_path =3D true; } =20 + tpm_pt->ops->path =3D g_strdup(value); tpm_pt->tpm_dev =3D g_strdup(value); =20 - tb->path =3D g_strdup(tpm_pt->tpm_dev); - tpm_pt->tpm_fd =3D qemu_open(tpm_pt->tpm_dev, O_RDWR); if (tpm_pt->tpm_fd < 0) { error_report("Cannot access TPM device using '%s': %s", @@ -387,8 +394,8 @@ static int tpm_passthrough_handle_device_opts(QemuOpts = *opts, TPMBackend *tb) tpm_pt->tpm_fd =3D -1; =20 err_free_parameters: - g_free(tb->path); - tb->path =3D NULL; + g_free(tpm_pt->ops->path); + tpm_pt->ops->path =3D NULL; =20 g_free(tpm_pt->tpm_dev); tpm_pt->tpm_dev =3D NULL; @@ -408,7 +415,7 @@ static TPMBackend *tpm_passthrough_create(QemuOpts *opt= s, const char *id) goto err_exit; } =20 - tpm_pt->cancel_fd =3D tpm_passthrough_open_sysfs_cancel(tb); + tpm_pt->cancel_fd =3D tpm_passthrough_open_sysfs_cancel(tpm_pt); if (tpm_pt->cancel_fd < 0) { goto err_exit; } @@ -430,6 +437,30 @@ static void tpm_passthrough_destroy(TPMBackend *tb) qemu_close(tpm_pt->tpm_fd); qemu_close(tpm_pt->cancel_fd); g_free(tpm_pt->tpm_dev); + + qapi_free_TPMPassthroughOptions(tpm_pt->ops); +} + +static TPMOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb) +{ + TPMPassthruState *tpm_pt =3D TPM_PASSTHROUGH(tb); + TPMPassthroughOptions *ops =3D g_new0(TPMPassthroughOptions, 1); + + if (!ops) { + return NULL; + } + + if (tpm_pt->ops->has_path) { + ops->has_path =3D true; + ops->path =3D g_strdup(tpm_pt->ops->path); + } + + if (tpm_pt->ops->has_cancel_path) { + ops->has_cancel_path =3D true; + ops->cancel_path =3D g_strdup(tpm_pt->ops->cancel_path); + } + + return (TPMOptions *)ops; } =20 static const QemuOptDesc tpm_passthrough_cmdline_opts[] =3D { @@ -460,12 +491,14 @@ static const TPMDriverOps tpm_passthrough_driver =3D { .get_tpm_established_flag =3D tpm_passthrough_get_tpm_established_flag, .reset_tpm_established_flag =3D tpm_passthrough_reset_tpm_established_= flag, .get_tpm_version =3D tpm_passthrough_get_tpm_version, + .get_tpm_options =3D tpm_passthrough_get_tpm_options, }; =20 static void tpm_passthrough_inst_init(Object *obj) { TPMPassthruState *tpm_pt =3D TPM_PASSTHROUGH(obj); =20 + tpm_pt->ops =3D g_new0(TPMPassthroughOptions, 1); tpm_pt->tpm_fd =3D -1; tpm_pt->cancel_fd =3D -1; } @@ -478,6 +511,8 @@ static void tpm_passthrough_inst_finalize(Object *obj) =20 qemu_close(tpm_pt->tpm_fd); qemu_close(tpm_pt->cancel_fd); + + qapi_free_TPMPassthroughOptions(tpm_pt->ops); } =20 static void tpm_passthrough_class_init(ObjectClass *klass, void *data) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index a538a7f..7f4d621 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -48,10 +48,9 @@ struct TPMBackend { GThreadPool *thread_pool; TPMRecvDataCB *recv_data_callback; =20 + /* */ char *id; enum TpmModel fe_model; - char *path; - char *cancel_path; =20 QLIST_ENTRY(TPMBackend) list; }; @@ -98,6 +97,8 @@ struct TPMDriverOps { int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty); =20 TPMVersion (*get_tpm_version)(TPMBackend *t); + + TPMOptions *(*get_tpm_options)(TPMBackend *t); }; =20 =20 @@ -230,6 +231,26 @@ void tpm_backend_open(TPMBackend *s, Error **errp); */ TPMVersion tpm_backend_get_tpm_version(TPMBackend *s); =20 +/** + * tpm_backend_get_tpm_options: + * @s: the backend + * + * Get the backend configuration options + * + * Returns newly allocated TPMOptions + */ +TPMOptions *tpm_backend_get_tpm_options(TPMBackend *s); + +/** + * tpm_backend_query_tpm: + * @s: the backend + * + * Query backend tpm info + * + * Returns newly allocated TPMInfo + */ +TPMInfo *tpm_backend_query_tpm(TPMBackend *s); + TPMBackend *qemu_find_tpm(const char *id); =20 const TPMDriverOps *tpm_get_backend_driver(const char *type); diff --git a/qapi-schema.json b/qapi-schema.json index b921994..3f1ca20 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -5140,6 +5140,16 @@ { 'command': 'query-tpm-types', 'returns': ['TpmType'] } =20 ## +# @TPMOptions: +# +# Base type for TPM options +# +# Since: 2.10 +## +{ 'struct': 'TPMOptions', + 'data': { } } + +## # @TPMPassthroughOptions: # # Information about the TPM passthrough type @@ -5151,20 +5161,9 @@ # # Since: 1.5 ## -{ 'struct': 'TPMPassthroughOptions', 'data': { '*path' : 'str', - '*cancel-path' : 'str'} } +{ 'struct': 'TPMPassthroughOptions', 'base': 'TPMOptions', + 'data': { '*path' : 'str', '*cancel-path' : 'str'} } =20 -## -# @TpmTypeOptions: -# -# A union referencing different TPM backend types' configuration options -# -# @type: 'passthrough' The configuration options for the TPM passthrough t= ype -# -# Since: 1.5 -## -{ 'union': 'TpmTypeOptions', - 'data': { 'passthrough' : 'TPMPassthroughOptions' } } =20 ## # @TPMInfo: @@ -5173,6 +5172,8 @@ # # @id: The Id of the TPM # +# @type: The TPM backend type +# # @model: The TPM frontend model # # @options: The TPM (backend) type configuration options @@ -5181,8 +5182,9 @@ ## { 'struct': 'TPMInfo', 'data': {'id': 'str', + 'type': 'TpmType', 'model': 'TpmModel', - 'options': 'TpmTypeOptions' } } + 'options': 'TPMOptions' } } =20 ## # @query-tpm: @@ -5199,13 +5201,12 @@ # <- { "return": # [ # { "model": "tpm-tis", +# "type": "passthrough", # "options": -# { "type": "passthrough", -# "data": -# { "cancel-path": "/sys/class/misc/tpm0/device/cancel", -# "path": "/dev/tpm0" -# } -# }, +# { +# "cancel-path": "/sys/class/misc/tpm0/device/cancel", +# "path": "/dev/tpm0" +# }, # "id": "tpm0" # } # ] diff --git a/tpm.c b/tpm.c index 0ee021a..1b6b550 100644 --- a/tpm.c +++ b/tpm.c @@ -249,36 +249,6 @@ static const TPMDriverOps *tpm_driver_find_by_type(enu= m TpmType type) return NULL; } =20 -static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv) -{ - TPMInfo *res =3D g_new0(TPMInfo, 1); - TPMPassthroughOptions *tpo; - - res->id =3D g_strdup(drv->id); - res->model =3D drv->fe_model; - res->options =3D g_new0(TpmTypeOptions, 1); - - switch (tpm_backend_get_type(drv)) { - case TPM_TYPE_PASSTHROUGH: - res->options->type =3D TPM_TYPE_OPTIONS_KIND_PASSTHROUGH; - tpo =3D g_new0(TPMPassthroughOptions, 1); - res->options->u.passthrough.data =3D tpo; - if (drv->path) { - tpo->path =3D g_strdup(drv->path); - tpo->has_path =3D true; - } - if (drv->cancel_path) { - tpo->cancel_path =3D g_strdup(drv->cancel_path); - tpo->has_cancel_path =3D true; - } - break; - case TPM_TYPE__MAX: - break; - } - - return res; -} - /* * Walk the list of active TPM backends and collect information about them * following the schema description in qapi-schema.json. @@ -293,7 +263,7 @@ TPMInfoList *qmp_query_tpm(Error **errp) continue; } info =3D g_new0(TPMInfoList, 1); - info->value =3D qmp_query_tpm_inst(drv); + info->value =3D tpm_backend_query_tpm(drv); =20 if (!cur_item) { head =3D cur_item =3D info; --=20 2.7.4