From patchwork Fri Feb 21 11:20:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396287 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 BC70314E3 for ; Fri, 21 Feb 2020 11:22:13 +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 8390A24672 for ; Fri, 21 Feb 2020 11:22:13 +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="vy0We7ja" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8390A24672 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 1j56Mj-000530-Jy; Fri, 21 Feb 2020 11:21:05 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j56Mi-00052u-D7 for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:04 +0000 X-Inumbo-ID: 3ed701c0-549c-11ea-bc8e-bc764e2007e4 Received: from smtp-fw-9101.amazon.com (unknown [207.171.184.25]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3ed701c0-549c-11ea-bc8e-bc764e2007e4; Fri, 21 Feb 2020 11:21:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582284064; x=1613820064; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OmblxcrSY+biuTqXXgXi966byZ/MlmescutJ3zjWh1s=; b=vy0We7jacXfWwA+qSGhgosZyEhqNPbm7PaMRy0qNsoFV+8kU26ngiVeH 3humJjlPX93W3krCu24eHCHlXhYeMBxToPj908NusurhrtyISkiO13CUV 9JNHBkB8AjPwIKOcLcaLBuPhk0rgOzmJuM4l8N7SUql/r6JGdlH8un/Nn w=; IronPort-SDR: JQL63GKOBQ3VmYd5odiI9HyVJk4xEaIMutKLuWANoEENWBel13ud58rRjrHdmNfuSHQ1toUjb8 cAyXXCCMQCQg== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="18290019" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-74cf8b49.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 21 Feb 2020 11:21:01 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1d-74cf8b49.us-east-1.amazon.com (Postfix) with ESMTPS id 54F21C1E5A; Fri, 21 Feb 2020 11:20:59 +0000 (UTC) Received: from EX13D32EUB002.ant.amazon.com (10.43.166.114) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 21 Feb 2020 11:20:59 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUB002.ant.amazon.com (10.43.166.114) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 21 Feb 2020 11:20:58 +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; Fri, 21 Feb 2020 11:20:57 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:44 +0000 Message-ID: <20200221112049.3077-2-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 1/6] libxl: add infrastructure to track and query 'recent' domids 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" A domid is considered recent if the domain it represents was destroyed less than a specified number of seconds ago. For debugging and/or testing purposes the number can be set using the environment variable LIBXL_DOMID_REUSE_TIMEOUT. If the variable does not exist then a default value of 60s is used. Whenever a domain is destroyed, a time-stamped record will be written into a history file (/var/run/xen/domid-history). To avoid the history file growing too large, any records with time-stamps that indicate that the age of a domid has exceeded the re-use timeout will also be purged. A new utility function, libxl__is_recent_domid(), has been added. This function reads the same history file checking whether a specified domid has a record that does not exceed the re-use timeout. Since this utility function does not write to the file, no records are actually purged by it. NOTE: The history file is purged on boot to it is safe to use CLOCK_MONOTONIC as a time source. Signed-off-by: Paul Durrant Reviewed-by: Ian Jackson --- Cc: Ian Jackson Cc: Wei Liu Cc: Anthony PERARD v7: - Addressed further comments from Ian - Introduced 'struct libxl__domid_history' to hold context v6: _ Addressed further comments from Ian v5: - Re-work file manipulation some more - Add more error checks v4: - Use new generalised libxl__flock - Don't read and write the same file - Use 'recent' rather than 'retired' - Add code into xen-init-dom0 to delete an old history file at boot v2: - New in v2 --- tools/helpers/xen-init-dom0.c | 30 +++++ tools/libxl/libxl.h | 7 ++ tools/libxl/libxl_domain.c | 226 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.c | 10 ++ tools/libxl/libxl_internal.h | 14 +++ 5 files changed, 287 insertions(+) diff --git a/tools/helpers/xen-init-dom0.c b/tools/helpers/xen-init-dom0.c index a1e5729458..56f69ab66f 100644 --- a/tools/helpers/xen-init-dom0.c +++ b/tools/helpers/xen-init-dom0.c @@ -12,6 +12,32 @@ #define DOMNAME_PATH "/local/domain/0/name" #define DOMID_PATH "/local/domain/0/domid" +int clear_domid_history(void) +{ + int rc = 1; + xentoollog_logger_stdiostream *logger; + libxl_ctx *ctx; + + logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0); + if (!logger) + return 1; + + if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, + (xentoollog_logger *)logger)) { + fprintf(stderr, "cannot init libxl context\n"); + goto outlog; + } + + if (!libxl_clear_domid_history(ctx)) + rc = 0; + + libxl_ctx_free(ctx); + +outlog: + xtl_logger_destroy((xentoollog_logger *)logger); + return rc; +} + int main(int argc, char **argv) { int rc; @@ -70,6 +96,10 @@ int main(int argc, char **argv) if (rc) goto out; + rc = clear_domid_history(); + if (rc) + goto out; + /* Write xenstore entries. */ if (!xs_write(xsh, XBT_NULL, DOMID_PATH, "0", strlen("0"))) { fprintf(stderr, "cannot set domid for Dom0\n"); diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index fde8548847..80ae110a52 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -2679,6 +2679,13 @@ static inline int libxl_qemu_monitor_command_0x041200(libxl_ctx *ctx, #include +/* + * This function is for use only during host initialisation. If it is + * invoked on a host with running domains, or concurrent libxl + * processes then the system may malfuntion. + */ +int libxl_clear_domid_history(libxl_ctx *ctx); + #endif /* LIBXL_H */ /* diff --git a/tools/libxl/libxl_domain.c b/tools/libxl/libxl_domain.c index 973fc1434d..8937aeb260 100644 --- a/tools/libxl/libxl_domain.c +++ b/tools/libxl/libxl_domain.c @@ -1268,6 +1268,230 @@ static void dm_destroy_cb(libxl__egc *egc, libxl__devices_destroy(egc, &dis->drs); } +static unsigned int libxl__get_domid_reuse_timeout(void) +{ + const char *env_timeout = getenv("LIBXL_DOMID_REUSE_TIMEOUT"); + + return env_timeout ? strtol(env_timeout, NULL, 0) : + LIBXL_DOMID_REUSE_TIMEOUT; +} + +char *libxl__domid_history_path(libxl__gc *gc, const char *suffix) +{ + return GCSPRINTF("%s/domid-history%s", libxl__run_dir_path(), + suffix ?: ""); +} + +int libxl_clear_domid_history(libxl_ctx *ctx) +{ + GC_INIT(ctx); + char *path; + int rc = ERROR_FAIL; + + path = libxl__domid_history_path(gc, NULL); + if (!path) + goto out; + + if (unlink(path) < 0 && errno != ENOENT) { + LOGE(ERROR, "failed to remove '%s'\n", path); + goto out; + } + + rc = 0; + +out: + GC_FREE; + return rc; +} + +struct libxl__domid_history { + long timeout; + char *path; + FILE *f; + struct timespec ts; +}; + +static void libxl__domid_history_dispose( + struct libxl__domid_history *ctxt) +{ + if (ctxt->f) { + fclose(ctxt->f); + ctxt->f = NULL; + } +} + +static int libxl__open_domid_history(libxl__gc *gc, + struct libxl__domid_history *ctxt) +{ + ctxt->timeout = libxl__get_domid_reuse_timeout(); + ctxt->path = libxl__domid_history_path(gc, NULL); + + ctxt->f = fopen(ctxt->path, "r"); + if (!ctxt->f && errno != ENOENT) { + LOGE(ERROR, "failed to open '%s'", ctxt->path); + return ERROR_FAIL; + } + + if (clock_gettime(CLOCK_MONOTONIC, &ctxt->ts)) { + LOGE(ERROR, "failed to get time"); + libxl__domid_history_dispose(ctxt); + return ERROR_FAIL; + } + + return 0; +} + +static int libxl__close_domid_history(libxl__gc *gc, + struct libxl__domid_history *ctxt) +{ + int r; + + if (!ctxt->f) return 0; + + r = fclose(ctxt->f); + ctxt->f = NULL; + if (r == EOF) { + LOGE(ERROR, "failed to close '%s'", ctxt->path); + return ERROR_FAIL; + } + + return 0; +} + +static int libxl__read_recent(libxl__gc *gc, + struct libxl__domid_history *ctxt, + unsigned long *sec, unsigned int *domid) +{ + if (!ctxt->f) { + *domid = INVALID_DOMID; + return 0; + } + + for (;;) { + int r = fscanf(ctxt->f, "%lu %u", sec, domid); + + if (r == EOF) { + if (ferror(ctxt->f)) { + LOGE(ERROR, "failed to read from '%s'", ctxt->path); + return ERROR_FAIL; + } + + *domid = INVALID_DOMID; + break; + } else if (r == 2 && libxl_domid_valid_guest(*domid) && + ctxt->ts.tv_sec - *sec <= ctxt->timeout) { + break; + } + } + + return 0; +} + +static int libxl__mark_domid_recent(libxl__gc *gc, uint32_t domid) +{ + libxl__flock *lock; + struct libxl__domid_history ctxt; + char *new; + FILE *nf = NULL; + int r, rc; + + lock = libxl__lock_domid_history(gc); + if (!lock) { + LOGED(ERROR, domid, "failed to acquire lock"); + rc = ERROR_FAIL; + goto out; + } + + rc = libxl__open_domid_history(gc, &ctxt); + if (rc) goto out; + + new = libxl__domid_history_path(gc, ".new"); + nf = fopen(new, "a"); + if (!nf) { + LOGED(ERROR, domid, "failed to open '%s'", new); + goto out; + } + + for (;;) { + unsigned long sec; + unsigned int val; + + rc = libxl__read_recent(gc, &ctxt, &sec, &val); + if (rc) goto out; + + if (val == INVALID_DOMID) /* EOF */ + break; + + r = fprintf(nf, "%lu %u\n", sec, val); + if (r < 0) { + LOGED(ERROR, domid, "failed to write to '%s'", new); + goto out; + } + } + + r = fprintf(nf, "%lu %u\n", ctxt.ts.tv_sec, domid); + if (r < 0) { + LOGED(ERROR, domid, "failed to write to '%s'", new); + goto out; + } + + r = fclose(nf); + nf = NULL; + if (r == EOF) { + LOGED(ERROR, domid, "failed to close '%s'", new); + goto out; + } + + rc = libxl__close_domid_history(gc, &ctxt); + if (rc) goto out; + + r = rename(new, ctxt.path); + if (r) { + LOGE(ERROR, "failed to rename '%s' -> '%s'", new, ctxt.path); + return ERROR_FAIL; + } + +out: + if (nf) fclose(nf); + libxl__domid_history_dispose(&ctxt); + if (lock) libxl__unlock_file(lock); + + return rc; +} + +int libxl__is_domid_recent(libxl__gc *gc, uint32_t domid, bool *recent) +{ + struct libxl__domid_history ctxt; + int rc; + + rc = libxl__open_domid_history(gc, &ctxt); + if (rc) goto out; + + *recent = false; + for (;;) { + unsigned long sec; + unsigned int val; + + rc = libxl__read_recent(gc, &ctxt, &sec, &val); + if (rc) goto out; + + if (val == INVALID_DOMID) /* EOF */ + break; + + if (val == domid && ctxt.ts.tv_sec - sec <= ctxt.timeout) { + *recent = true; + break; + } + } + + rc = libxl__close_domid_history(gc, &ctxt); + +out: + libxl__domid_history_dispose(&ctxt); + + return rc; +} + static void devices_destroy_cb(libxl__egc *egc, libxl__devices_remove_state *drs, int rc) @@ -1331,6 +1555,8 @@ static void devices_destroy_cb(libxl__egc *egc, if (!ctx->xch) goto badchild; if (!dis->soft_reset) { + rc = libxl__mark_domid_recent(gc, domid); + if (rc) goto badchild; rc = xc_domain_destroy(ctx->xch, domid); } else { rc = xc_domain_pause(ctx->xch, domid); diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 211236dc99..bbd4c6cba9 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -504,6 +504,16 @@ libxl__flock *libxl__lock_domain_userdata(libxl__gc *gc, uint32_t domid) return lock; } +libxl__flock *libxl__lock_domid_history(libxl__gc *gc) +{ + const char *lockfile; + + lockfile = libxl__domid_history_path(gc, ".lock"); + if (!lockfile) return NULL; + + return libxl__lock_file(gc, lockfile); +} + int libxl__get_domain_configuration(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config) { diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 4936446069..43e5885d1e 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -4263,6 +4263,8 @@ _hidden void libxl__remus_teardown(libxl__egc *egc, _hidden void libxl__remus_restore_setup(libxl__egc *egc, libxl__domain_create_state *dcs); +_hidden char *libxl__domid_history_path(libxl__gc *gc, + const char *suffix); /* * Convenience macros. @@ -4661,6 +4663,7 @@ libxl__flock *libxl__lock_file(libxl__gc *gc, const char *filename); void libxl__unlock_file(libxl__flock *lock); libxl__flock *libxl__lock_domain_userdata(libxl__gc *gc, uint32_t domid); +libxl__flock *libxl__lock_domid_history(libxl__gc *gc); /* * Retrieve / store domain configuration from / to libxl private @@ -4799,6 +4802,17 @@ _hidden int libxl__domain_pvcontrol(libxl__egc *egc, libxl__xswait_state *pvcontrol, domid_t domid, const char *cmd); +/* + * Maximum number of seconds after desctruction then a domid remains + * 'recent'. Recent domids are not allowed to be re-used. This can be + * overidden, for debugging purposes, by the environment variable of the + * same name. + */ +#define LIBXL_DOMID_REUSE_TIMEOUT 60 + +/* Check whether a domid is recent */ +int libxl__is_domid_recent(libxl__gc *gc, uint32_t domid, bool *recent); + #endif /* From patchwork Fri Feb 21 11:20:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396295 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 9A50B14BC for ; Fri, 21 Feb 2020 11:22:24 +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 76F68222C4 for ; Fri, 21 Feb 2020 11:22:24 +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="HvfQK4gq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 76F68222C4 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 1j56Mk-00053C-Tc; Fri, 21 Feb 2020 11:21:06 +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 1j56Mj-00052z-Kk for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:05 +0000 X-Inumbo-ID: 3edcc0b3-549c-11ea-8629-12813bfff9fa Received: from smtp-fw-9101.amazon.com (unknown [207.171.184.25]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 3edcc0b3-549c-11ea-8629-12813bfff9fa; Fri, 21 Feb 2020 11:21:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582284065; x=1613820065; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZYCSbbtCg+dwnwQqUm2sxhA/+YhvOXxlLk0TeDcgRq4=; b=HvfQK4gqTWYEiiHX9jzCHJhHEudpz+IsSOvQhRAbdOgPrgs/DHvL2srK E/ECr+SffQwo15dZJrs5qaMUwJsbkl1tjwM9fRIsiisFGwjndf6UXwx2L 4wYIeYCH2HCKXq1e1zD81XyFqoIBDPgflpL3XC3Ma3IUEyVT0B+u7uaKt g=; IronPort-SDR: e9Zrit2d5MYFG1ciUxjMnP2Qjs5s4sI0MO+Cs6m2hT5VLl1z0j3Wk0F0ZKyrb0BispI9IOg/H3 GXvfpKI/sssA== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="18290025" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-474bcd9f.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 21 Feb 2020 11:21:03 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1d-474bcd9f.us-east-1.amazon.com (Postfix) with ESMTPS id 016C9A173E; Fri, 21 Feb 2020 11:21:01 +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; Fri, 21 Feb 2020 11:21:01 +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; Fri, 21 Feb 2020 11:21:00 +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; Fri, 21 Feb 2020 11:20:58 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:45 +0000 Message-ID: <20200221112049.3077-3-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 2/6] libxl: modify libxl__logv() to only log valid domid values 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" Some code-paths use values other than INVALID_DOMID to indicate an invalid domain id. Specifically, xl will pass a value of 0 when creating/restoring a domain. Therefore modify libxl__logv() to use libxl_domid_valid_guest() as a validity test. Signed-off-by: Paul Durrant Acked-by: Ian Jackson --- Cc: Wei Liu Cc: Anthony PERARD v6: - New in v6 (split out from another patch) --- tools/libxl/libxl_internal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index bbd4c6cba9..d93a75533f 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", From patchwork Fri Feb 21 11:20:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396293 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 749A21892 for ; Fri, 21 Feb 2020 11:22:23 +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 51471222C4 for ; Fri, 21 Feb 2020 11:22:23 +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="sNfadk8N" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 51471222C4 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 1j56N1-00056H-FD; Fri, 21 Feb 2020 11:21:23 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j56Mz-00055s-Nq for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:21 +0000 X-Inumbo-ID: 493e19aa-549c-11ea-aa99-bc764e2007e4 Received: from smtp-fw-9101.amazon.com (unknown [207.171.184.25]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 493e19aa-549c-11ea-aa99-bc764e2007e4; Fri, 21 Feb 2020 11:21:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582284081; x=1613820081; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GB4XwziE0lqzSohlKaFns9MyRRAZWesKJLI6Qeng2ds=; b=sNfadk8N27ld6QL1HJNJX52GuNaE06ZzwbpEWLLagZCDmV4sDZUAOfv8 ajxfS/gke3gVpWBvADmgpjhEuBPPPsrSz8diFwZSBZM/EHc7ezTLEYqQ9 ruUdVOcK/CGCuX69Zv3mh2hVs4KdF4S5WmqA2RErXclGBmCW0KrTjI1Jn 8=; IronPort-SDR: kCIYRKRwxnU/8UNxJMRwnDMpan+xXqe3DoBMzExl37lqZ/rDnUjeCcaRmoI+LTefa0+0Aof6mB 4taxp/OhtB7w== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="18290060" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-2c665b5d.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 21 Feb 2020 11:21:20 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1d-2c665b5d.us-east-1.amazon.com (Postfix) with ESMTPS id E74D3A289D; Fri, 21 Feb 2020 11:21:16 +0000 (UTC) Received: from EX13D32EUC002.ant.amazon.com (10.43.164.94) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 21 Feb 2020 11:21:03 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUC002.ant.amazon.com (10.43.164.94) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 21 Feb 2020 11:21:02 +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; Fri, 21 Feb 2020 11:21:00 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:46 +0000 Message-ID: <20200221112049.3077-4-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 3/6] public/xen.h: add a definition for a 'valid domid' mask 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 , Ian Jackson , Jan Beulich Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" A subsequent patch will modify libxl to allow selection of a random domid value when creating domains. Valid values are limited to a width of 15 bits, so add an appropriate mask definition to the public header. NOTE: It is reasonable for this mask definition to be in a Xen public header rather than in, say, a libxenctrl header since it relates to the validity of a value passed to XEN_DOMCTL_createdomain. This new definition is placed in xen.h rather than domctl.h only to co-locate it with other domid-related defitions. Signed-off-by: Paul Durrant Reviewed-by: Ian Jackson Acked-by: Julien Grall --- Cc: Andrew Cooper Cc: George Dunlap Cc: Jan Beulich Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Wei Liu v6: - New in v6 (split out from another patch) --- xen/include/public/xen.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xen/include/public/xen.h b/xen/include/public/xen.h index d2198dffad..75b1619d0d 100644 --- a/xen/include/public/xen.h +++ b/xen/include/public/xen.h @@ -614,6 +614,9 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t); /* Idle domain. */ #define DOMID_IDLE xen_mk_uint(0x7FFF) +/* Mask for valid domain id values */ +#define DOMID_MASK xen_mk_uint(0x7FFF) + #ifndef __ASSEMBLY__ typedef uint16_t domid_t; From patchwork Fri Feb 21 11:20:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396299 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 D793114BC for ; Fri, 21 Feb 2020 11:22:35 +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 B3BEB222C4 for ; Fri, 21 Feb 2020 11:22:35 +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="ZxfrfTzn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B3BEB222C4 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 1j56NF-0005Co-AD; Fri, 21 Feb 2020 11:21:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j56ND-0005CC-OX for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:35 +0000 X-Inumbo-ID: 51f9209e-549c-11ea-ade5-bc764e2007e4 Received: from smtp-fw-4101.amazon.com (unknown [72.21.198.25]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 51f9209e-549c-11ea-ade5-bc764e2007e4; Fri, 21 Feb 2020 11:21: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=1582284096; x=1613820096; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=x+e+EpgFTOLgWib/5yudT3ryA9rDkDXeWQu2BYMap9g=; b=ZxfrfTznmsox8W+kwoPct1f6Mao1mmJ9EtNkj1b4JXGW8iwVUIP/FlKF RNW+ij8WmXPp0UzjFH73TXP+XLBd0EztQWpaZogx9lQpwKve8aK3DcOgh 6urLBMjSrtLPJRGQjYTXbhTLqrswaa2fojlA/GCnPt/rMj0+IM7t52LLq U=; IronPort-SDR: 7MvmIM0YHAlmGNXbkezaHZXkPcO3Xebrxq0oKHLkMugvOxPKHeeIcsT5YohWEu829aQOMfAv34 unSz61f8OmTg== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="18026508" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-715bee71.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 21 Feb 2020 11:21:24 +0000 Received: from EX13MTAUEA002.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1a-715bee71.us-east-1.amazon.com (Postfix) with ESMTPS id DCE6BA054E; Fri, 21 Feb 2020 11:21:19 +0000 (UTC) Received: from EX13D32EUB001.ant.amazon.com (10.43.166.125) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 21 Feb 2020 11:21:06 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUB001.ant.amazon.com (10.43.166.125) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 21 Feb 2020 11:21:04 +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; Fri, 21 Feb 2020 11:21:02 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:47 +0000 Message-ID: <20200221112049.3077-5-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 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: 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), From patchwork Fri Feb 21 11:20:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396297 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 6E09314BC for ; Fri, 21 Feb 2020 11:22:26 +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 4A884222C4 for ; Fri, 21 Feb 2020 11:22:26 +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="uZvn8PjV" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4A884222C4 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 1j56NG-0005Dr-Kl; Fri, 21 Feb 2020 11:21: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 1j56NF-0005Cj-A8 for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:37 +0000 X-Inumbo-ID: 52020b50-549c-11ea-8629-12813bfff9fa Received: from smtp-fw-6001.amazon.com (unknown [52.95.48.154]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 52020b50-549c-11ea-8629-12813bfff9fa; Fri, 21 Feb 2020 11:21: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=1582284096; x=1613820096; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eSWeDs33Xk0ZJ9WDOyHx4AeQnnVYfVe84mVD3fIk6pA=; b=uZvn8PjV2boi5FbuAJeXzETz6CtEDfRy3muLowYB18I37NCo73QnNbVN yZpk8sDeDnY8FVt9t1qHWTKwKmfOhDA+rKfSMcGgKMWRa1tyvDoCXrTHy JmlrpbLRJucP2/dP3tVSC3S7yjNptbusPhYiunjQqzeQD3/8W8VPEOL0y E=; IronPort-SDR: 3/SpOcYpykbbwtrISJGmq1xHD+av8nsXfQzCwspUzVsDXUjPZgNe00ND03KoxBANVw39SVU1DQ TX/wX/luEmvw== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="19025011" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-57e1d233.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-6001.iad6.amazon.com with ESMTP; 21 Feb 2020 11:21:24 +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 E9C101416DD; Fri, 21 Feb 2020 11:21:22 +0000 (UTC) Received: from EX13D32EUB004.ant.amazon.com (10.43.166.212) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 21 Feb 2020 11:21:07 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUB004.ant.amazon.com (10.43.166.212) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 21 Feb 2020 11:21:06 +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; Fri, 21 Feb 2020 11:21:05 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:48 +0000 Message-ID: <20200221112049.3077-6-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 5/6] xl.conf: introduce 'domid_policy' 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 new global 'domid_policy' configuration option to decide how domain id values are allocated for new domains. It may be set to one of two values: "xen", the default value, will cause an invalid domid value to be passed to do_domain_create() preserving the existing behaviour of having Xen choose the domid value during domain_create(). "random" will cause the special RANDOM_DOMID value to be passed to do_domain_create() such that libxl__domain_make() will select a random domid value. Signed-off-by: Paul Durrant Acked-by: Ian Jackson --- Cc: Wei Liu Cc: Anthony PERARD v2: - New in v2 --- docs/man/xl.conf.5.pod | 10 ++++++++++ tools/examples/xl.conf | 4 ++++ tools/xl/xl.c | 10 ++++++++++ tools/xl/xl.h | 1 + tools/xl/xl_vmcontrol.c | 2 ++ 5 files changed, 27 insertions(+) diff --git a/docs/man/xl.conf.5.pod b/docs/man/xl.conf.5.pod index 207ab3e77a..41ee428744 100644 --- a/docs/man/xl.conf.5.pod +++ b/docs/man/xl.conf.5.pod @@ -45,6 +45,16 @@ The semantics of each C defines which form of C is required. =over 4 +=item B + +Determines how domain-id is set when creating a new domain. + +If set to "xen" then the hypervisor will allocate new domain-id values on a sequential basis. + +If set to "random" then a random domain-id value will be chosen. + +Default: "xen" + =item B If set to "on" then C will automatically reduce the amount of diff --git a/tools/examples/xl.conf b/tools/examples/xl.conf index 0446deb304..95f2f442d3 100644 --- a/tools/examples/xl.conf +++ b/tools/examples/xl.conf @@ -1,5 +1,9 @@ ## Global XL config file ## +# Set domain-id policy. "xen" means that the hypervisor will choose the +# id of a new domain. "random" means that a random value will be chosen. +#domid_policy="xen" + # Control whether dom0 is ballooned down when xen doesn't have enough # free memory to create a domain. "auto" means only balloon if dom0 # starts with all the host's memory. diff --git a/tools/xl/xl.c b/tools/xl/xl.c index 3d4390a46d..2a5ddd4390 100644 --- a/tools/xl/xl.c +++ b/tools/xl/xl.c @@ -54,6 +54,7 @@ int claim_mode = 1; bool progress_use_cr = 0; int max_grant_frames = -1; int max_maptrack_frames = -1; +libxl_domid domid_policy = INVALID_DOMID; xentoollog_level minmsglevel = minmsglevel_default; @@ -228,6 +229,15 @@ static void parse_global_config(const char *configfile, else libxl_bitmap_set_any(&global_pv_affinity_mask); + if (!xlu_cfg_get_string (config, "domid_policy", &buf, 0)) { + if (!strcmp(buf, "xen")) + domid_policy = INVALID_DOMID; + else if (!strcmp(buf, "random")) + domid_policy = RANDOM_DOMID; + else + fprintf(stderr, "invalid domid_policy option"); + } + xlu_cfg_destroy(config); } diff --git a/tools/xl/xl.h b/tools/xl/xl.h index 60bdad8ffb..2b4709efb2 100644 --- a/tools/xl/xl.h +++ b/tools/xl/xl.h @@ -283,6 +283,7 @@ extern int max_maptrack_frames; extern libxl_bitmap global_vm_affinity_mask; extern libxl_bitmap global_hvm_affinity_mask; extern libxl_bitmap global_pv_affinity_mask; +extern libxl_domid domid_policy; enum output_format { OUTPUT_FORMAT_JSON, diff --git a/tools/xl/xl_vmcontrol.c b/tools/xl/xl_vmcontrol.c index e520b1da79..39292acfe6 100644 --- a/tools/xl/xl_vmcontrol.c +++ b/tools/xl/xl_vmcontrol.c @@ -899,6 +899,8 @@ start: autoconnect_console_how = 0; } + d_config.c_info.domid = domid_policy; + if ( restoring ) { libxl_domain_restore_params params; From patchwork Fri Feb 21 11:20:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 11396291 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 CD39314BC for ; Fri, 21 Feb 2020 11:22:22 +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 9E87D222C4 for ; Fri, 21 Feb 2020 11:22:22 +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="qfZnscPR" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9E87D222C4 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 1j56N6-00058P-02; Fri, 21 Feb 2020 11:21:27 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j56N4-00057p-Q4 for xen-devel@lists.xenproject.org; Fri, 21 Feb 2020 11:21:26 +0000 X-Inumbo-ID: 4c75c7f8-549c-11ea-aa99-bc764e2007e4 Received: from smtp-fw-9101.amazon.com (unknown [207.171.184.25]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 4c75c7f8-549c-11ea-aa99-bc764e2007e4; Fri, 21 Feb 2020 11:21:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582284086; x=1613820086; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9KXZNbhpLKSzg5DQpRyF/2xU3IeMvTOHYKHX7w1oahk=; b=qfZnscPRllafAeLvSZQqJnLSo5Faor/LxOcf80IkncxJSXsyZACAMI6s vZKprkyNIMu58YT0MpR0AF014rS2sZQIclt6TA8xHOqZNdC49wC6JSYAX eSjNmIOo9XWyVAYvIplVl8IgXZhybRMi8gGMlhJ5bncqRg3exuc1bmxJ3 0=; IronPort-SDR: q2hxmQN5JiSlrpjw5gNUJWmXTp9TnPxDBFYUh4BbnnLQ5wD5B5gaHEtsHckLbE5arNGf8C/GGY LL0ZG3ktjOyg== X-IronPort-AV: E=Sophos;i="5.70,468,1574121600"; d="scan'208";a="18290070" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-2c665b5d.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 21 Feb 2020 11:21: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-1d-2c665b5d.us-east-1.amazon.com (Postfix) with ESMTPS id 8F698A239D; Fri, 21 Feb 2020 11:21:24 +0000 (UTC) Received: from EX13D32EUB001.ant.amazon.com (10.43.166.125) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 21 Feb 2020 11:21:09 +0000 Received: from EX13MTAUEB002.ant.amazon.com (10.43.60.12) by EX13D32EUB001.ant.amazon.com (10.43.166.125) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Fri, 21 Feb 2020 11:21:08 +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; Fri, 21 Feb 2020 11:21:06 +0000 From: Paul Durrant To: Date: Fri, 21 Feb 2020 11:20:49 +0000 Message-ID: <20200221112049.3077-7-pdurrant@amazon.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200221112049.3077-1-pdurrant@amazon.com> References: <20200221112049.3077-1-pdurrant@amazon.com> MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v7 6/6] xl: allow domid to be preserved on save/restore or migrate 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 '-D' command line option to save and migrate to allow the domain id to be incorporated into the saved domain configuration and hence be preserved. NOTE: Logically it may seem as though preservation of domid should be dealt with by libxl, but the libxl migration stream has no record in which to transfer domid and remote domain creation occurs before the migration stream is parsed. Hence this patch modifies xl rather then libxl. Signed-off-by: Paul Durrant Acked-by: Ian Jackson --- Cc: Wei Liu Cc: Anthony PERARD v5: - Expand the commit comment to say why it is xl being patched rather than libxl v2: - Heavily re-worked based on new libxl_domain_create_info --- docs/man/xl.1.pod.in | 14 ++++++++++++++ tools/xl/xl.h | 1 + tools/xl/xl_cmdtable.c | 6 ++++-- tools/xl/xl_migrate.c | 15 ++++++++++----- tools/xl/xl_saverestore.c | 19 ++++++++++++++----- tools/xl/xl_vmcontrol.c | 3 ++- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/docs/man/xl.1.pod.in b/docs/man/xl.1.pod.in index 33ad2ebd71..09339282e6 100644 --- a/docs/man/xl.1.pod.in +++ b/docs/man/xl.1.pod.in @@ -490,6 +490,13 @@ Display huge (!) amount of debug information during the migration process. Leave the domain on the receive side paused after migration. +=item B<-D> + +Preserve the B in the domain coniguration that is transferred +such that it will be identical on the destination host, unless that +configuration is overridden using the B<-C> option. Note that it is not +possible to use this option for a 'localhost' migration. + =back =item B [I] I I @@ -692,6 +699,13 @@ Leave the domain running after creating the snapshot. Leave the domain paused after creating the snapshot. +=item B<-D> + +Preserve the B in the domain coniguration that is embedded in +the state file such that it will be identical when the domain is restored, +unless that configuration is overridden. (See the B operation +above). + =back =item B [I] diff --git a/tools/xl/xl.h b/tools/xl/xl.h index 2b4709efb2..06569c6c4a 100644 --- a/tools/xl/xl.h +++ b/tools/xl/xl.h @@ -99,6 +99,7 @@ struct save_file_header { #define SAVEFILE_BYTEORDER_VALUE ((uint32_t)0x01020304UL) void save_domain_core_begin(uint32_t domid, + int preserve_domid, const char *override_config_file, uint8_t **config_data_r, int *config_len_r); diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c index 3b302b2f20..08335394e5 100644 --- a/tools/xl/xl_cmdtable.c +++ b/tools/xl/xl_cmdtable.c @@ -153,7 +153,8 @@ struct cmd_spec cmd_table[] = { "[options] []", "-h Print this help.\n" "-c Leave domain running after creating the snapshot.\n" - "-p Leave domain paused after creating the snapshot." + "-p Leave domain paused after creating the snapshot.\n" + "-D Store the domain id in the configration." }, { "migrate", &main_migrate, 0, 1, @@ -167,7 +168,8 @@ struct cmd_spec cmd_table[] = { "-e Do not wait in the background (on ) for the death\n" " of the domain.\n" "--debug Print huge (!) amount of debug during the migration process.\n" - "-p Do not unpause domain after migrating it." + "-p Do not unpause domain after migrating it.\n" + "-D Preserve the domain id" }, { "restore", &main_restore, 0, 1, diff --git a/tools/xl/xl_migrate.c b/tools/xl/xl_migrate.c index 22f0429b84..0813beb801 100644 --- a/tools/xl/xl_migrate.c +++ b/tools/xl/xl_migrate.c @@ -176,7 +176,8 @@ static void migrate_do_preamble(int send_fd, int recv_fd, pid_t child, } -static void migrate_domain(uint32_t domid, const char *rune, int debug, +static void migrate_domain(uint32_t domid, int preserve_domid, + const char *rune, int debug, const char *override_config_file) { pid_t child = -1; @@ -187,7 +188,7 @@ static void migrate_domain(uint32_t domid, const char *rune, int debug, uint8_t *config_data; int config_len, flags = LIBXL_SUSPEND_LIVE; - save_domain_core_begin(domid, override_config_file, + save_domain_core_begin(domid, preserve_domid, override_config_file, &config_data, &config_len); if (!config_len) { @@ -537,13 +538,14 @@ int main_migrate(int argc, char **argv) char *rune = NULL; char *host; int opt, daemonize = 1, monitor = 1, debug = 0, pause_after_migration = 0; + int preserve_domid = 0; static struct option opts[] = { {"debug", 0, 0, 0x100}, {"live", 0, 0, 0x200}, COMMON_LONG_OPTS }; - SWITCH_FOREACH_OPT(opt, "FC:s:ep", opts, "migrate", 2) { + SWITCH_FOREACH_OPT(opt, "FC:s:epD", opts, "migrate", 2) { case 'C': config_filename = optarg; break; @@ -560,6 +562,9 @@ int main_migrate(int argc, char **argv) case 'p': pause_after_migration = 1; break; + case 'D': + preserve_domid = 1; + break; case 0x100: /* --debug */ debug = 1; break; @@ -596,7 +601,7 @@ int main_migrate(int argc, char **argv) pause_after_migration ? " -p" : ""); } - migrate_domain(domid, rune, debug, config_filename); + migrate_domain(domid, preserve_domid, rune, debug, config_filename); return EXIT_SUCCESS; } @@ -716,7 +721,7 @@ int main_remus(int argc, char **argv) } } - save_domain_core_begin(domid, NULL, &config_data, &config_len); + save_domain_core_begin(domid, 0, NULL, &config_data, &config_len); if (!config_len) { fprintf(stderr, "No config file stored for running domain and " diff --git a/tools/xl/xl_saverestore.c b/tools/xl/xl_saverestore.c index 9be033fe65..953d791d1a 100644 --- a/tools/xl/xl_saverestore.c +++ b/tools/xl/xl_saverestore.c @@ -32,6 +32,7 @@ #ifndef LIBXL_HAVE_NO_SUSPEND_RESUME void save_domain_core_begin(uint32_t domid, + int preserve_domid, const char *override_config_file, uint8_t **config_data_r, int *config_len_r) @@ -62,6 +63,8 @@ void save_domain_core_begin(uint32_t domid, fprintf(stderr, "unable to retrieve domain configuration\n"); exit(EXIT_FAILURE); } + + d_config.c_info.domid = preserve_domid ? domid : 0; } config_c = libxl_domain_config_to_json(ctx, &d_config); @@ -120,14 +123,15 @@ void save_domain_core_writeconfig(int fd, const char *source, hdr.optional_data_len); } -static int save_domain(uint32_t domid, const char *filename, int checkpoint, - int leavepaused, const char *override_config_file) +static int save_domain(uint32_t domid, int preserve_domid, + const char *filename, int checkpoint, + int leavepaused, const char *override_config_file) { int fd; uint8_t *config_data; int config_len; - save_domain_core_begin(domid, override_config_file, + save_domain_core_begin(domid, preserve_domid, override_config_file, &config_data, &config_len); if (!config_len) { @@ -236,15 +240,19 @@ int main_save(int argc, char **argv) const char *config_filename = NULL; int checkpoint = 0; int leavepaused = 0; + int preserve_domid = 0; int opt; - SWITCH_FOREACH_OPT(opt, "cp", NULL, "save", 2) { + SWITCH_FOREACH_OPT(opt, "cpD", NULL, "save", 2) { case 'c': checkpoint = 1; break; case 'p': leavepaused = 1; break; + case 'D': + preserve_domid = 1; + break; } if (argc-optind > 3) { @@ -257,7 +265,8 @@ int main_save(int argc, char **argv) if ( argc - optind >= 3 ) config_filename = argv[optind + 2]; - save_domain(domid, filename, checkpoint, leavepaused, config_filename); + save_domain(domid, preserve_domid, filename, checkpoint, leavepaused, + config_filename); return EXIT_SUCCESS; } diff --git a/tools/xl/xl_vmcontrol.c b/tools/xl/xl_vmcontrol.c index 39292acfe6..2e2d427492 100644 --- a/tools/xl/xl_vmcontrol.c +++ b/tools/xl/xl_vmcontrol.c @@ -899,7 +899,8 @@ start: autoconnect_console_how = 0; } - d_config.c_info.domid = domid_policy; + if (!libxl_domid_valid_guest(d_config.c_info.domid)) + d_config.c_info.domid = domid_policy; if ( restoring ) { libxl_domain_restore_params params;