From patchwork Mon Mar 20 00:09:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9632865 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DA7EA6020B for ; Mon, 20 Mar 2017 00:15:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE7D327F8C for ; Mon, 20 Mar 2017 00:15:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C358D2807E; Mon, 20 Mar 2017 00:15:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2EA7427F8C for ; Mon, 20 Mar 2017 00:15:39 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cpkx8-0007DQ-D7; Mon, 20 Mar 2017 00:13:38 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cpkx6-00078f-OJ for xen-devel@lists.xen.org; Mon, 20 Mar 2017 00:13:36 +0000 Received: from [85.158.137.68] by server-4.bemta-3.messagelabs.com id EE/5D-03705-0BE1FC85; Mon, 20 Mar 2017 00:13:36 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpjkeJIrShJLcpLzFFi42I5YG5SrLtO7ny EwfEZBhZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bD973MBX2OFev+bmFvYOzS62Lk5GARuMUk ceixWxcjF4eQwHRGiSMffjKBJCQEeCWOLJvB2sXIAWT7Sfzth6rpZZSY0NsKVsMmoC+x4vFBV hBbREBa4trny4wgRcwCZxklpq9pZQdJCAtYS3y4cp4NYpuqxLztW5lBhvIK2EncWS4MsUte4s LVUywgNidQeM2l22CtQgK2EvtvTWObwMi3gJFhFaNGcWpRWWqRrqGJXlJRZnpGSW5iZo6uoYG xXm5qcXFiempOYlKxXnJ+7iZGYJgwAMEOxhXbPQ8xSnIwKYnylv84ESHEl5SfUpmRWJwRX1Sa k1p8iFGGg0NJgveI0skIIcGi1PTUirTMHGDAwqQlOHiURHhzQdK8xQWJucWZ6RCpU4y6HHNm7 37DJMSSl5+XKiXOewGkSACkKKM0D24ELHouMcpKCfMyAh0lxFOQWpSbWYIq/4pRnINRSZh3M8 gUnsy8ErhNr4COYAI64u2HEyBHlCQipKQaGHUiIj4GBRoFHv0r1ZicaBS3UKF5z7SP8umWr+/ +jnquI8Uf+LEkfLFgmODzPadlWjM2H+VY3fh02U3eTyfSkpX7p7HpZ23JTsjjMApRXTz1fK1x qE2YodLlH+6nzr19dmpK3ql1qwrmadU5p7snLY42dryhkPxXRLmhOvX/KQbhPx+ufE9mU2Ipz kg01GIuKk4EAMSwF/yZAgAA X-Env-Sender: haozhong.zhang@intel.com X-Msg-Ref: server-8.tower-31.messagelabs.com!1489968802!91050058!6 X-Originating-IP: [192.55.52.115] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 57543 invoked from network); 20 Mar 2017 00:13:34 -0000 Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by server-8.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 20 Mar 2017 00:13:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1489968814; x=1521504814; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=wPJNhNEGpZj1dl16BTzgR6bScGTjcHrn9r7smiR+1r8=; b=XfaiIuq+7zlvtR+NmSPjcZWvhuNd1j2+0MNNcPHKGdJO0KKmOUKHIBtK 1CqB4T1aRzC1BhYoLTd0znKcvGe3FA==; Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Mar 2017 17:13:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,191,1486454400"; d="scan'208";a="78799259" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.153]) by fmsmga006.fm.intel.com with ESMTP; 19 Mar 2017 17:13:32 -0700 From: Haozhong Zhang To: xen-devel@lists.xen.org Date: Mon, 20 Mar 2017 08:09:49 +0800 Message-Id: <20170320000949.24675-16-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20170320000949.24675-1-haozhong.zhang@intel.com> References: <20170320000949.24675-1-haozhong.zhang@intel.com> Cc: Konrad Rzeszutek Wilk , Dan Williams , Ian Jackson , Wei Liu , Haozhong Zhang Subject: [Xen-devel] [RFC XEN PATCH v2 15/15] tools/misc: add xen-ndctl X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP xen-ndctl is a tool for users in Dom0 to setup the host pmem with Xen hypervisor. It's used to specify the storage, which is either the regular RAM or a pmem range, to manage the specified pmem. Signed-off-by: Haozhong Zhang --- Cc: Ian Jackson Cc: Wei Liu --- .gitignore | 1 + tools/misc/Makefile | 4 + tools/misc/xen-ndctl.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+) diff --git a/.gitignore b/.gitignore index 4567de7a59..1af7cda8bd 100644 --- a/.gitignore +++ b/.gitignore @@ -206,6 +206,7 @@ tools/misc/xen-hvmctx tools/misc/xenlockprof tools/misc/lowmemd tools/misc/xencov +tools/misc/xen-ndctl tools/pkg-config/* tools/qemu-xen-build tools/xentrace/xenalyze diff --git a/tools/misc/Makefile b/tools/misc/Makefile index 8152f7bfb5..86d89f1577 100644 --- a/tools/misc/Makefile +++ b/tools/misc/Makefile @@ -31,6 +31,7 @@ INSTALL_SBIN += xenperf INSTALL_SBIN += xenpm INSTALL_SBIN += xenwatchdogd INSTALL_SBIN += xen-livepatch +INSTALL_SBIN += xen-ndctl INSTALL_SBIN += $(INSTALL_SBIN-y) # Everything to be installed in a private bin/ @@ -108,4 +109,7 @@ xen-lowmemd: xen-lowmemd.o xencov: xencov.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) +xen-ndctl: xen-ndctl.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) + -include $(DEPS) diff --git a/tools/misc/xen-ndctl.c b/tools/misc/xen-ndctl.c index e69de29bb2..f556a31dcf 100644 --- a/tools/misc/xen-ndctl.c +++ b/tools/misc/xen-ndctl.c @@ -0,0 +1,227 @@ +/* + * xen-ndctl.c + * + * Xen NVDIMM management tool + * + * Copyright (C) 2017, Intel Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) + +static xc_interface *xch; + +static int handle_help(int argc, char *argv[]); +static int handle_list_cmds(int argc, char *argv[]); +static int handle_setup(int argc, char *argv[]); + +static const struct +{ + const char *name; + const char *syntax; + const char *help; + int (*handler)(int argc, char *argv[]); + bool need_xc; +} cmds[] = +{ + { + .name = "help", + .syntax = "[command]", + .help = "Show this message or the help message of 'command'.\n" + "Use command 'list-cmds' to list all supported commands.\n", + .handler = handle_help, + }, + + { + .name = "list-cmds", + .syntax = "", + .help = "List all supported commands\n", + .handler = handle_list_cmds, + }, + + { + .name = "setup", + .syntax = " [ ]", + .help = "Setup Xen hypervisor to prepare for mapping host pmem pages\n" + "from MFN 'smfn' to 'emfn'. If 'mgmt_smfn' and 'mgmt_emfn' are\n" + "specified, host pmem pages from MFN 'mgmt_smfn' and 'mgmt_emfn'\n" + "will be used by Xen hypervisor to manage this mapping.\n", + .handler = handle_setup, + .need_xc = true, + }, +}; + +static void show_help(const char *cmd) +{ + int nr = ARRAY_SIZE(cmds); + unsigned int i; + + fprintf(stderr, "Usage: xen-ndctl "); + + if ( !cmd ) + { + fprintf(stderr, + " [args]\n\n" + "List all supported commands by 'xen-ndctl list-cmds'\n" + "Get help of a command by 'xen-ndctl help '\n"); + return; + } + + for ( i = 0; i < nr; i++ ) + if ( !strcmp(cmd, cmds[i].name) ) + { + fprintf(stderr, "%s %s\n\n%s", + cmds[i].name, cmds[i].syntax, cmds[i].help); + break; + } + + if ( i == nr ) + fprintf(stderr, "Unsupported command '%s'. " + "List all supported commands by 'xen-ndctl list-cmds'.\n", + cmd); +} + +static int handle_unrecognized_arguments(const char *cmd) +{ + fprintf(stderr, "Unrecognized arguments\n\n"); + show_help(cmd); + return -EINVAL; +} + +static int handle_help(int argc, char *argv[]) +{ + if ( argc == 1 ) + show_help(NULL); + else if ( argc == 2 ) + show_help(argv[1]); + else + return handle_unrecognized_arguments(argv[0]); + + return 0; +} + +static int handle_list_cmds(int argc, char *argv[]) +{ + unsigned int i; + + if ( argc > 1 ) + return handle_unrecognized_arguments(argv[0]); + + for ( i = 0; i < ARRAY_SIZE(cmds); i++ ) + fprintf(stderr, "%s\n", cmds[i].name); + + return 0; +} + +static int handle_setup(int argc, char *argv[]) +{ + unsigned long smfn, emfn, mgmt_smfn = INVALID_MFN, mgmt_emfn = INVALID_MFN; + + if ( argc != 3 /* xen-ndctl setup smfn emfn */ && + argc != 5 /* xen-ndctl setup smfn emfn mgmt_smfn mgmt_emfn */ ) + return handle_unrecognized_arguments(argv[0]); + +#define strtoul_check_overflow(str, ret) \ + do { \ + (ret) = strtoul((str), NULL, 0); \ + if ( errno == ERANGE ) \ + { \ + fprintf(stderr, "MFN '%s' overflow\n", (str)); \ + return -errno; \ + } \ + } while (0) + + strtoul_check_overflow(argv[1], smfn); + strtoul_check_overflow(argv[2], emfn); + + if ( argc == 5 ) + { + strtoul_check_overflow(argv[3], mgmt_smfn); + strtoul_check_overflow(argv[4], mgmt_emfn); + } + +#undef strtoul_check_overflow + + return xc_nvdimm_pmem_setup(xch, smfn, emfn, mgmt_smfn, mgmt_emfn); +} + +int main(int argc, char *argv[]) +{ + unsigned int i; + int nr_cmds = ARRAY_SIZE(cmds), rc = 0; + const char *cmd; + + if ( argc <= 1 ) + { + show_help(NULL); + return 0; + } + + cmd = argv[1]; + + for ( i = 0; i < nr_cmds; i++ ) + if ( !strcmp(cmd, cmds[i].name) ) + { + if ( cmds[i].need_xc ) + { + xch = xc_interface_open(0, 0, 0); + if ( !xch ) + { + rc = -errno; + fprintf(stderr, "failed to get xc handler\n"); + break; + } + } + rc = cmds[i].handler(argc - 1, &argv[1]); + if ( rc ) + fprintf(stderr, "%s failed: %s\n", cmds[i].name, strerror(-rc)); + break; + } + + if ( i == nr_cmds ) + { + fprintf(stderr, "Unsupported command '%s'. " + "List all supported commands by 'xen-ndctl list-cmds'.\n", + cmd); + rc = -ENOSYS; + } + + if ( xch ) + xc_interface_close(xch); + + return rc; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */