From patchwork Tue Dec 24 13:04:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11309327 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CBB5D138D for ; Tue, 24 Dec 2019 13:06:00 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A82F920706 for ; Tue, 24 Dec 2019 13:06:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="iD+HQdWO" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A82F920706 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ijjrw-0005Vv-AZ; Tue, 24 Dec 2019 13:05:00 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ijjru-0005Uw-Qm for xen-devel@lists.xenproject.org; Tue, 24 Dec 2019 13:04:58 +0000 X-Inumbo-ID: f53696e2-264d-11ea-97ba-12813bfff9fa Received: from smtp-fw-2101.amazon.com (unknown [72.21.196.25]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f53696e2-264d-11ea-97ba-12813bfff9fa; Tue, 24 Dec 2019 13:04:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1577192686; x=1608728686; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O7fo1fo9xeBdgn1L2IioqSvlYPWpauo6229N3Ii1uj0=; b=iD+HQdWOsz6U54MDRvbZGAWLdbS35RJcVu08aL7zsmm67fBtPpTyYx7t 82h3N+38XluUg+1DvrDK4OQ+DDtGqDJKvi9l53be9lwxf0jOpS3qccOhA PCeRZAB8Bqmoa9Jv1jR8XPQMt7UT8/2C/HUqyKfdTyaD2zgd+0e0vZKp9 M=; IronPort-SDR: hDDH5EVfBK7JPYZcja+wgwzV0xRetLv/T8NyEJ8FveLJK1wOHkgcSMum8r6z61gmcxYSY2GDeZ LnjZdgIXpSqQ== X-IronPort-AV: E=Sophos;i="5.69,351,1571702400"; d="scan'208";a="9936032" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 24 Dec 2019 13:04:46 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-5dd976cd.us-east-1.amazon.com (Postfix) with ESMTPS id B822BA20DC; Tue, 24 Dec 2019 13:04:44 +0000 (UTC) Received: from EX13D32EUC003.ant.amazon.com (10.43.164.24) by EX13MTAUEA001.ant.amazon.com (10.43.61.243) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 24 Dec 2019 13:04:36 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D32EUC003.ant.amazon.com (10.43.164.24) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 24 Dec 2019 13:04:35 +0000 Received: from u2f063a87eabd5f.cbg10.amazon.com (10.125.106.135) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Tue, 24 Dec 2019 13:04:33 +0000 From: Paul Durrant To: Date: Tue, 24 Dec 2019 13:04:15 +0000 Message-ID: <20191224130416.3570-6-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191224130416.3570-1-pdurrant@amazon.com> References: <20191224130416.3570-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH 5/6] libxl: allow creation of domains with specified or random domid X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Anthony PERARD , Paul Durrant , Ian Jackson , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" This patch modifies do_domain_create() to use the value of domid that is passed in. A new 'special value' - RANDOM_DOMID - is added into the API and this, INVALID_DOMID or any valid domid is passed unmodified to libxl__domain_make(). Any other invalid domid value will cause an error. If RANDOM_DOMID is passed in then libxl__domain_make() will use libxl__random_bytes() to choose a domid. If the chosen value is not a valid domid then this step will be repeated. Once a valid value is chosen it will be passed to xc_domain_create() but if this fails with an errno value of EEXIST, a new random value will be chosen and the operation will be retried. If a valid domid is passed in and xc_domain_create() fails with errno set to EEXIST then this will result in a new error value of ERROR_DEVICE_EXISTS being returned from libxl__domain_make(). This is done so that domcreate_complete() can be adjusted so as not to tear down the existing domain that the attempted creation happened to collide with. NOTE: libxl__logv() is also modified to only log valid domid values in messages rather than any domid, valid or otherwise, that is not INVALID_DOMID. Signed-off-by: Paul Durrant --- Cc: Ian Jackson Cc: Wei Liu Cc: Anthony PERARD --- tools/libxl/libxl.h | 12 ++++++++++ tools/libxl/libxl_create.c | 43 +++++++++++++++++++++++++++++------- tools/libxl/libxl_internal.c | 2 +- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 18c1a2d6bf..6e7f5a0241 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -1268,6 +1268,14 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src); */ #define LIBXL_HAVE_DOMAIN_NEED_MEMORY_CONFIG +/* + * LIBXL_HAVE_SPECIFIED_DOMID + * + * libxl_domain_create_new() and libxl_domain_create_restore() will use + * a caller specified domid value. + */ +#define LIBXL_HAVE_SPECIFIED_DOMID + typedef char **libxl_string_list; void libxl_string_list_dispose(libxl_string_list *sl); int libxl_string_list_length(const libxl_string_list *sl); @@ -1528,7 +1536,11 @@ int libxl_ctx_free(libxl_ctx *ctx /* 0 is OK */); /* domain related functions */ #define INVALID_DOMID ~0 +#define RANDOM_DOMID (INVALID_DOMID - 1) +/* On entry a domid == RANDOM_DOMID, a valid random domain id will be + * chosen, otherwise if domid is a valid value then that will be used as + * the domain id */ /* If the result is ERROR_ABORTED, the domain may or may not exist * (in a half-created state). *domid will be valid and will be the * domain id, or INVALID_DOMID, as appropriate */ diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 1835a5502c..1d98567f59 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -555,8 +555,6 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, libxl_domain_create_info *info = &d_config->c_info; libxl_domain_build_info *b_info = &d_config->b_info; - assert(soft_reset || *domid == INVALID_DOMID); - uuid_string = libxl__uuid2string(gc, info->uuid); if (!uuid_string) { rc = ERROR_NOMEM; @@ -571,6 +569,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, .max_grant_frames = b_info->max_grant_frames, .max_maptrack_frames = b_info->max_maptrack_frames, }; + uint32_t in_domid = *domid; if (info->type != LIBXL_DOMAIN_TYPE_PV) { create.flags |= XEN_DOMCTL_CDF_hvm; @@ -600,10 +599,24 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, goto out; } - ret = xc_domain_create(ctx->xch, domid, &create); + for (;;) { + if (in_domid == RANDOM_DOMID) { + ret = libxl__random_bytes(gc, (void *)domid, sizeof(*domid)); + if (ret < 0) + break; + + if (!libxl_domid_valid_guest(*domid)) + continue; + } + + ret = xc_domain_create(ctx->xch, domid, &create); + if (ret == 0 || errno != EEXIST || in_domid != RANDOM_DOMID) + break; + } + if (ret < 0) { - LOGED(ERROR, *domid, "domain creation fail"); - rc = ERROR_FAIL; + LOGED(ERROR, in_domid, "domain creation fail"); + rc = (errno == EEXIST) ? ERROR_DEVICE_EXISTS : ERROR_FAIL; goto out; } @@ -1111,7 +1124,6 @@ static void initiate_domain_create(libxl__egc *egc, if (ret) { LOGD(ERROR, domid, "cannot make domain: %d", ret); dcs->guest_domid = domid; - ret = ERROR_FAIL; goto error_out; } @@ -1752,7 +1764,8 @@ static void domcreate_complete(libxl__egc *egc, if (!rc && d_config->b_info.exec_ssidref) rc = xc_flask_relabel_domain(CTX->xch, dcs->guest_domid, d_config->b_info.exec_ssidref); - bool retain_domain = !rc || rc == ERROR_ABORTED; + bool retain_domain = !rc || rc == ERROR_ABORTED || + rc == ERROR_DEVICE_EXISTS; if (retain_domain) { libxl__domain_userdata_lock *lock; @@ -1845,7 +1858,21 @@ static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config, if (rc < 0) goto out_err; } cdcs->dcs.callback = domain_create_cb; - cdcs->dcs.domid = INVALID_DOMID; + + /* Allow valid and special values */ + switch (*domid) { + case INVALID_DOMID: + case RANDOM_DOMID: + break; + default: + if (libxl_domid_valid_guest(*domid)) + break; + + rc = ERROR_FAIL; + goto out_err; + } + + cdcs->dcs.domid = *domid; cdcs->dcs.soft_reset = false; if (cdcs->dcs.restore_params.checkpointed_stream == diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index ba5637358e..dc6aaa9c9f 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -234,7 +234,7 @@ void libxl__logv(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval, fileline[sizeof(fileline)-1] = 0; domain[0] = 0; - if (domid != INVALID_DOMID) + if (libxl_domid_valid_guest(domid)) snprintf(domain, sizeof(domain), "Domain %"PRIu32":", domid); x: xtl_log(ctx->lg, msglevel, errnoval, "libxl",