From patchwork Thu Jan 9 11:48:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11325515 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 0065214B4 for ; Thu, 9 Jan 2020 11:50:03 +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 D076A2075D for ; Thu, 9 Jan 2020 11:50:02 +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="iBP2UCoR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D076A2075D 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 1ipWIo-0000Hq-3S; Thu, 09 Jan 2020 11:48:38 +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 1ipWIn-0000Hi-2s for xen-devel@lists.xenproject.org; Thu, 09 Jan 2020 11:48:37 +0000 X-Inumbo-ID: f7f3dc84-32d5-11ea-b9a8-12813bfff9fa Received: from smtp-fw-6002.amazon.com (unknown [52.95.49.90]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f7f3dc84-32d5-11ea-b9a8-12813bfff9fa; Thu, 09 Jan 2020 11:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1578570516; x=1610106516; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jyxss6pBldF+KsQbXrM8ySia03xzIwXABbl7zka5abQ=; b=iBP2UCoRV0xeGaT+uZON8xUSKllk+QgpWjpmctNnaRHeaRTTHAp3d3ov XEA5kzFTLjEF4mBF9Ia/zWmWBbD7cbqiKwuZ6MXTgxgOr8J5daYJqg9Bo TqUj+QFiwHHAr+j69fwpzpdY88gj0dM30iB6tGW3Qf+l8UTyxJdSQTonm w=; IronPort-SDR: ThuL5gP4b7mPWsD0YSdzY/WOr3EWWGla063m3mi1tohkYGiAUfRyAXUrqLc1ZRH4xU1rgD6aWI Vqr7NrEyhNdA== X-IronPort-AV: E=Sophos;i="5.69,413,1571702400"; d="scan'208";a="10742955" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-27fb8269.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP; 09 Jan 2020 11:48:36 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-27fb8269.us-east-1.amazon.com (Postfix) with ESMTPS id D8FBAA27DD; Thu, 9 Jan 2020 11:48:34 +0000 (UTC) Received: from EX13D32EUB003.ant.amazon.com (10.43.166.165) by EX13MTAUEA001.ant.amazon.com (10.43.61.243) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 9 Jan 2020 11:48:34 +0000 Received: from EX13MTAUWC001.ant.amazon.com (10.43.162.135) by EX13D32EUB003.ant.amazon.com (10.43.166.165) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 9 Jan 2020 11:48:33 +0000 Received: from u2f063a87eabd5f.cbg10.amazon.com (10.125.106.135) by mail-relay.amazon.com (10.43.162.232) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 9 Jan 2020 11:48:30 +0000 From: Paul Durrant To: Date: Thu, 9 Jan 2020 11:48:14 +0000 Message-ID: <20200109114816.2293-5-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200109114816.2293-1-pdurrant@amazon.com> References: <20200109114816.2293-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v2 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: Anthony PERARD , Paul Durrant , Ian Jackson , Wei Liu 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 do_domain_create() to use that value if it is valid. Any valid domid will be checked against the retired domid list before being passed to libxl__domain_make(). 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 select a random domid value, check it for validity, verify it does not match a retired domain, and then pass it to Xen's XEN_DOMCTL_createdomain operation. If Xen determines that it co-incides with an existing domain, a new random value will be selected and the operation will be re-tried. 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 v2: - Re-worked to use a value from libxl_domain_create_info --- tools/libxl/libxl.h | 9 +++++++++ tools/libxl/libxl_create.c | 32 +++++++++++++++++++++++++++++++- tools/libxl/libxl_internal.c | 2 +- tools/libxl/libxl_types.idl | 1 + 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 18c1a2d6bf..7e60ee1c8b 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 1835a5502c..ee76dee364 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -600,9 +600,39 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, goto out; } - ret = xc_domain_create(ctx->xch, domid, &create); + if (libxl_domid_valid_guest(info->domid)) { + *domid = info->domid; + + if (libxl__is_retired_domid(gc, *domid)) { + LOGED(ERROR, *domid, "domain id is retired"); + rc = ERROR_FAIL; + goto out; + } + } else if (info->domid == RANDOM_DOMID) { + *domid = 0; /* Zero-out initial value */ + } + + for (;;) { + if (info->domid == RANDOM_DOMID) { + /* Randomize lower order bytes */ + ret = libxl__random_bytes(gc, (void *)domid, + sizeof(uint16_t)); + if (ret < 0) + break; + + if (!libxl_domid_valid_guest(*domid) || + libxl__is_retired_domid(gc, *domid)) + continue; + } + + ret = xc_domain_create(ctx->xch, domid, &create); + if (ret == 0 || errno != EEXIST || info->domid != RANDOM_DOMID) + break; + } + if (ret < 0) { LOGED(ERROR, *domid, "domain creation fail"); + *domid = INVALID_DOMID; rc = ERROR_FAIL; goto out; } 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", 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),