From patchwork Wed Feb 19 09:37:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11390805 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 08E7E109A for ; Wed, 19 Feb 2020 09:40:04 +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 D8E912464E for ; Wed, 19 Feb 2020 09:40:03 +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="clP69WDS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D8E912464E 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 1j4LoL-00084b-Hj; Wed, 19 Feb 2020 09:38:29 +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 1j4LoJ-00083s-RP for xen-devel@lists.xenproject.org; Wed, 19 Feb 2020 09:38:27 +0000 X-Inumbo-ID: 93c205e2-52fb-11ea-831b-12813bfff9fa Received: from smtp-fw-2101.amazon.com (unknown [72.21.196.25]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 93c205e2-52fb-11ea-831b-12813bfff9fa; Wed, 19 Feb 2020 09:38:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582105106; x=1613641106; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=74QQKc3YhyQJ4+Z87br2uVFdcnsoCtfUsM2gLSTWEDo=; b=clP69WDS13DM3YHSoLJoHek5hCfb75w8rFqtoCHIyXdwBvdl9PG+qYLb 8tXynbc0FjSoHISJmRALt3rw1OV8pYILswGw1hUHHU2KO9rFYr/z8CWDO sRfHLoEBSPFnH00DywBOwiHHqWpkW2U+CNkYlMbFw35eOJyGDBsFdqwVN s=; IronPort-SDR: 0G9CKwmM+wje57Yk0zhMpqrFQDl1RkCFpncZ5NGJQdR4RsRNG4bIBeagC+2ze3YXHEmfJqdqAb qM4j175q/4eg== X-IronPort-AV: E=Sophos;i="5.70,459,1574121600"; d="scan'208";a="17897434" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1e-57e1d233.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP; 19 Feb 2020 09:38:26 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-57e1d233.us-east-1.amazon.com (Postfix) with ESMTPS id D8AB114160C; Wed, 19 Feb 2020 09:38:21 +0000 (UTC) Received: from EX13D32EUC003.ant.amazon.com (10.43.164.24) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Wed, 19 Feb 2020 09:38:08 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUC003.ant.amazon.com (10.43.164.24) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 19 Feb 2020 09:38:07 +0000 Received: from u2f063a87eabd5f.cbg10.amazon.com (10.125.106.135) by mail-relay.amazon.com (10.43.60.234) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Wed, 19 Feb 2020 09:38:05 +0000 From: Paul Durrant To: Date: Wed, 19 Feb 2020 09:37:52 +0000 Message-ID: <20200219093754.2924-5-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200219093754.2924-1-pdurrant@amazon.com> References: <20200219093754.2924-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v6 4/6] libxl: allow creation of domains with a 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: Stefano Stabellini , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Paul Durrant , Jason Andryuk , Ian Jackson , Jan Beulich , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" This patch adds a 'domid' field to libxl_domain_create_info and then modifies libxl__domain_make() to have Xen use that value if it is valid. If the domid value is invalid then Xen will choose the domid, as before, unless the value is the new special RANDOM_DOMID value added to the API. This value instructs libxl__domain_make() to choose a random domid value for Xen to use. If Xen determines that a domid specified to or chosen by libxl__domain_make() co-incides with an existing domain then the create operation will fail. In this case, if RANDOM_DOMID was specified to libxl__domain_make() then a new random value will be chosen and the create operation will be re-tried, otherwise libxl__domain_make() will fail. After Xen has successfully created a new domain, libxl__domain_make() will check whether its domid matches any recently used domid values. If it does then the domain will be destroyed. If the domid used in creation was specified to libxl__domain_make() then it will fail at this point, otherwise the create operation will be re-tried with either a new random or Xen-selected domid value. Signed-off-by: Paul Durrant Acked-by: Ian Jackson --- Cc: Ian Jackson Cc: Wei Liu Cc: Anthony PERARD Cc: Andrew Cooper Cc: George Dunlap Cc: Jan Beulich Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Jason Andryuk v6: - Addressed further comments from Ian v5: - Flattened nested loops v4: - Not added Jason's R-b because of substantial change - Check for recent domid *after* creation - Re-worked commit comment v3: - Added DOMID_MASK definition used to mask randomized values - Use stack variable to avoid assuming endianness v2: - Re-worked to use a value from libxl_domain_create_info --- tools/libxl/libxl.h | 9 +++++ tools/libxl/libxl_create.c | 76 ++++++++++++++++++++++++++++++++++--- tools/libxl/libxl_types.idl | 1 + 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 80ae110a52..35e13428b2 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_CREATEINFO_DOMID + * + * libxl_domain_create_new() and libxl_domain_create_restore() will use + * a domid specified in libxl_domain_create_info(). + */ +#define LIBXL_HAVE_CREATEINFO_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,6 +1536,7 @@ int libxl_ctx_free(libxl_ctx *ctx /* 0 is OK */); /* domain related functions */ #define INVALID_DOMID ~0 +#define RANDOM_DOMID (INVALID_DOMID - 1) /* 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 diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 3a7364e2ac..ccc9e70990 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -600,11 +600,77 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, goto out; } - ret = xc_domain_create(ctx->xch, domid, &create); - if (ret < 0) { - LOGED(ERROR, *domid, "domain creation fail"); - rc = ERROR_FAIL; - goto out; + for (;;) { + uint32_t local_domid; + bool recent; + + if (info->domid == RANDOM_DOMID) { + uint16_t v; + + ret = libxl__random_bytes(gc, (void *)&v, sizeof(v)); + if (ret < 0) + break; + + v &= DOMID_MASK; + if (!libxl_domid_valid_guest(v)) + continue; + + local_domid = v; + } else { + local_domid = info->domid; /* May not be valid */ + } + + ret = xc_domain_create(ctx->xch, &local_domid, &create); + if (ret < 0) { + /* + * If we generated a random domid and creation failed + * because that domid already exists then simply try + * again. + */ + if (errno == EEXIST && info->domid == RANDOM_DOMID) + continue; + + LOGED(ERROR, local_domid, "domain creation fail"); + rc = ERROR_FAIL; + goto out; + } + + /* A new domain now exists */ + *domid = local_domid; + + rc = libxl__is_domid_recent(gc, local_domid, &recent); + if (rc) + goto out; + + /* The domid is not recent, so we're done */ + if (!recent) + break; + + /* + * If the domid was specified then there's no point in + * trying again. + */ + if (libxl_domid_valid_guest(info->domid)) { + LOGED(ERROR, local_domid, "domain id recently used"); + rc = ERROR_FAIL; + goto out; + } + + /* + * The domain is recent and so cannot be used. Clear domid + * here since, if xc_domain_destroy() fails below there is + * little point calling it again in the error path. + */ + *domid = INVALID_DOMID; + + ret = xc_domain_destroy(ctx->xch, local_domid); + if (ret < 0) { + LOGED(ERROR, local_domid, "domain destroy fail"); + rc = ERROR_FAIL; + goto out; + } + + /* The domain was successfully destroyed, so we can try again */ } rc = libxl__arch_domain_save_config(gc, d_config, state, &create); diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl index 7921950f6a..d0d431614f 100644 --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -409,6 +409,7 @@ libxl_domain_create_info = Struct("domain_create_info",[ ("ssidref", uint32), ("ssid_label", string), ("name", string), + ("domid", libxl_domid), ("uuid", libxl_uuid), ("xsdata", libxl_key_value_list), ("platformdata", libxl_key_value_list),