All of lore.kernel.org
 help / color / mirror / Atom feed
From: Haozhong Zhang <haozhong.zhang@intel.com>
To: xen-devel@lists.xen.org
Cc: Konrad Rzeszutek Wilk <konrad@darnok.org>,
	Dan Williams <dan.j.williams@intel.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	Wei Liu <wei.liu2@citrix.com>,
	Haozhong Zhang <haozhong.zhang@intel.com>
Subject: [RFC XEN PATCH v2 15/15] tools/misc: add xen-ndctl
Date: Mon, 20 Mar 2017 08:09:49 +0800	[thread overview]
Message-ID: <20170320000949.24675-16-haozhong.zhang@intel.com> (raw)
In-Reply-To: <20170320000949.24675-1-haozhong.zhang@intel.com>

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 <haozhong.zhang@intel.com>
---
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>
---
 .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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <xenctrl.h>
+
+#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  = "<smfn> <emfn> [<mgmt_smfn> <mgmt_emfn>]",
+        .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,
+                "<command> [args]\n\n"
+                "List all supported commands by 'xen-ndctl list-cmds'\n"
+                "Get help of a command by 'xen-ndctl help <command>'\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:
+ */
-- 
2.12.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-03-20  0:09 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-20  0:09 [RFC XEN PATCH v2 00/15] Add vNVDIMM support to HVM domains Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 01/15] xen/common: add Kconfig item for pmem support Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 02/15] xen: probe pmem regions via ACPI NFIT Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 03/15] xen/x86: allow customizing locations of extended frametable & M2P Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 04/15] xen/x86: add XEN_SYSCTL_nvdimm_pmem_setup to setup host pmem Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 05/15] xen/x86: add XENMEM_populate_pmem_map to map host pmem pages to HVM domain Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 06/15] tools: reserve guest memory for ACPI from device model Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 07/15] tools/libacpi: expose the minimum alignment used by mem_ops.alloc Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 08/15] tools/libacpi: add callback acpi_ctxt.p2v to get a pointer from physical address Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 09/15] tools/libacpi: add callbacks to access XenStore Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 10/15] tools/libacpi: add a simple AML builder Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 11/15] tools/libacpi: load ACPI built by the device model Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 12/15] tools/libxl: build qemu options from xl vNVDIMM configs Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 13/15] tools/libxl: add support to map host pmem device to guests Haozhong Zhang
2017-03-20  0:09 ` [RFC XEN PATCH v2 14/15] tools/libxl: initiate pmem mapping via qmp callback Haozhong Zhang
2017-03-20  0:09 ` Haozhong Zhang [this message]
2017-03-30  4:11   ` [RFC XEN PATCH v2 15/15] tools/misc: add xen-ndctl Dan Williams
2017-03-30  7:58     ` Haozhong Zhang
2017-04-01 11:55       ` Konrad Rzeszutek Wilk
2017-03-30  4:20 ` [RFC XEN PATCH v2 00/15] Add vNVDIMM support to HVM domains Dan Williams
2017-03-30  8:21   ` Haozhong Zhang
2017-03-30 16:01     ` Dan Williams
2017-04-01 11:54       ` Konrad Rzeszutek Wilk
2017-04-01 15:45         ` Dan Williams
2017-04-04 17:00           ` Konrad Rzeszutek Wilk
2017-04-04 17:16             ` Dan Williams
2017-04-04 17:34               ` Konrad Rzeszutek Wilk
2017-04-04 17:59                 ` Dan Williams
2017-04-04 18:05                   ` Konrad Rzeszutek Wilk
2017-04-04 18:59                     ` Dan Williams
2017-04-11 17:48                       ` Konrad Rzeszutek Wilk
2017-04-01 12:24 ` Konrad Rzeszutek Wilk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170320000949.24675-16-haozhong.zhang@intel.com \
    --to=haozhong.zhang@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=konrad@darnok.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.