|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [RFC XEN PATCH v4 12/41] tools/xl: add xl command 'pmem-list'
The new xl command 'pmem-list' is used to list the information of PMEM
regions.
Signed-off-by: Haozhong Zhang <haozhong.zhang@xxxxxxxxx>
---
Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Cc: George Dunlap <George.Dunlap@xxxxxxxxxxxxx>
Cc: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
Cc: Jan Beulich <jbeulich@xxxxxxxx>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
Cc: Stefano Stabellini <sstabellini@xxxxxxxxxx>
Cc: Tim Deegan <tim@xxxxxxx>
Cc: Wei Liu <wei.liu2@xxxxxxxxxx>
---
tools/libxl/Makefile | 2 +-
tools/libxl/libxl.h | 20 +++++++
tools/libxl/libxl_nvdimm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++
tools/libxl/libxl_types.idl | 16 +++++
tools/xl/Makefile | 2 +-
tools/xl/xl.h | 1 +
tools/xl/xl_cmdtable.c | 6 ++
tools/xl/xl_nvdimm.c | 92 +++++++++++++++++++++++++++++
8 files changed, 275 insertions(+), 2 deletions(-)
create mode 100644 tools/xl/xl_nvdimm.c
diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index 5a861f72cb..a6f2dbd1cf 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -139,7 +139,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o
libxl_pci.o \
libxl_dom_suspend.o libxl_dom_save.o libxl_usb.o \
libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \
libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \
- libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
+ libxl_9pfs.o libxl_domain.o libxl_vdispl.o
libxl_nvdimm.o \
$(LIBXL_OBJS-y)
LIBXL_OBJS += libxl_genid.o
LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 5e9aed739d..9ce487e79f 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -2304,6 +2304,26 @@ int libxl_psr_cat_get_l3_info(libxl_ctx *ctx,
libxl_psr_cat_info **info,
void libxl_psr_cat_info_list_free(libxl_psr_cat_info *list, int nr);
#endif
+/* NVDIMM */
+
+/*
+ * Get a list of information of PMEM regions of the specified type.
+ *
+ * Parameters:
+ * ctx: libxl contenxt
+ * type: type of the PMEM regions
+ * regions_r: return the information list (one entry per region) on success;
+ * the list is dynamically allocated and shall be freed by callers
+ * nr_r: return the number of entries in regions_r on success
+ *
+ * Return:
+ * 0 on success; otherwise, ERROR_*, and leave errno valid.
+ */
+int libxl_nvdimm_pmem_get_regions(libxl_ctx *ctx,
+ libxl_nvdimm_pmem_region_type type,
+ libxl_nvdimm_pmem_region **regions_r,
+ unsigned int *nr_r);
+
/* misc */
/* Each of these sets or clears the flag according to whether the
diff --git a/tools/libxl/libxl_nvdimm.c b/tools/libxl/libxl_nvdimm.c
index e69de29bb2..70da18f11f 100644
--- a/tools/libxl/libxl_nvdimm.c
+++ b/tools/libxl/libxl_nvdimm.c
@@ -0,0 +1,138 @@
+/*
+ * tools/libxl/libxl_nvdimm.c
+ *
+ * <One line description of the file and what it does>
+ *
+ * Copyright (C) 2017 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms and conditions of the GNU General Public
+ * License, version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xenctrl.h>
+#include <xen-tools/libs.h>
+
+#include "libxl_internal.h"
+
+/*
+ * Convert sizeof(libxl_nvdimm_pmem_*_region) to
+ * sizeof(xen_sysctl_nvdimm_pmem_*_region_t).
+ *
+ * Indexed by LIBXL_NVDIMM_PMEM_REGION_TYPE_*.
+ */
+static size_t xc_pmem_region_struct_size[] = {
+ [LIBXL_NVDIMM_PMEM_REGION_TYPE_RAW] = sizeof(libxl_nvdimm_pmem_raw_region),
+};
+
+static int get_xc_region_type(libxl_nvdimm_pmem_region_type type,
+ uint8_t *xc_type_r)
+{
+ static uint8_t xc_region_types[] = {
+ [LIBXL_NVDIMM_PMEM_REGION_TYPE_RAW] = PMEM_REGION_TYPE_RAW,
+ };
+ static unsigned int nr_types =
+ sizeof(xc_region_types) / sizeof(xc_region_types[0]);
+
+ if (type >= nr_types)
+ return -EINVAL;
+
+ *xc_type_r = xc_region_types[type];
+
+ return 0;
+}
+
+static void copy_from_xc_regions(libxl_nvdimm_pmem_region *tgt_regions,
+ void *src_xc_regions, uint8_t xc_type,
+ unsigned int nr)
+{
+ static size_t offset = offsetof(libxl_nvdimm_pmem_region, u);
+ libxl_nvdimm_pmem_region *tgt = tgt_regions;
+ libxl_nvdimm_pmem_region *end = tgt_regions + nr;
+ void *src = src_xc_regions;
+ size_t size = xc_pmem_region_struct_size[xc_type];
+
+ BUILD_BUG_ON(sizeof(libxl_nvdimm_pmem_raw_region) !=
+ sizeof(xen_sysctl_nvdimm_pmem_raw_region_t));
+
+ while (tgt < end) {
+ memcpy((void *)tgt + offset, src, size);
+ tgt += 1;
+ src += size;
+ }
+}
+
+int libxl_nvdimm_pmem_get_regions(libxl_ctx *ctx,
+ libxl_nvdimm_pmem_region_type type,
+ libxl_nvdimm_pmem_region **regions_r,
+ unsigned int *nr_r)
+{
+ GC_INIT(ctx);
+ libxl_nvdimm_pmem_region *regions;
+ uint8_t xc_type;
+ unsigned int nr;
+ void *xc_regions;
+ int rc = 0, err;
+
+ err = get_xc_region_type(type, &xc_type);
+ if (err) {
+ LOGE(ERROR, "invalid PMEM region type %d required", type);
+ rc = ERROR_INVAL;
+ goto out;
+ }
+
+ err = xc_nvdimm_pmem_get_regions_nr(ctx->xch, xc_type, &nr);
+ if (err) {
+ rc = ERROR_FAIL;
+ goto out;
+ }
+
+ if (!nr) {
+ *nr_r = 0;
+ goto out;
+ }
+
+ xc_regions = libxl__malloc(gc, nr * xc_pmem_region_struct_size[xc_type]);
+ if (!xc_regions) {
+ LOGE(ERROR, "cannot allocate xc buffer for %d regions", nr);
+ err = -ENOMEM;
+ rc = ERROR_NOMEM;
+ goto out;
+ }
+
+ err = xc_nvdimm_pmem_get_regions(ctx->xch, xc_type, xc_regions, &nr);
+ if (err) {
+ LOGE(ERROR, "cannot get information of PMEM regions of type %d, err
%d",
+ type, err);
+ rc = ERROR_FAIL;
+ goto out;
+ }
+
+ regions = libxl__malloc(NOGC, sizeof(*regions) * nr);
+ if (!regions) {
+ LOGE(ERROR, "cannot allocate return buffer for %d regions", nr);
+ err = -ENOMEM;
+ rc = ERROR_NOMEM;
+ goto out;
+ }
+ copy_from_xc_regions(regions, xc_regions, xc_type, nr);
+
+ *regions_r = regions;
+ *nr_r = nr;
+
+ out:
+ GC_FREE;
+
+ if (rc)
+ errno = -err;
+
+ return rc;
+}
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index a239324341..1c7b8998e9 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -1041,3 +1041,19 @@ libxl_psr_cat_info = Struct("psr_cat_info", [
("cbm_len", uint32),
("cdp_enabled", bool),
])
+
+libxl_nvdimm_pmem_region_type = Enumeration("nvdimm_pmem_region_type", [
+ (0, "RAW"),
+ ])
+
+libxl_nvdimm_pmem_raw_region = Struct("nvdimm_pmem_raw_region", [
+ ("smfn", uint64),
+ ("emfn", uint64),
+ ("pxm", uint32),
+ ])
+
+libxl_nvdimm_pmem_region = Struct("nvdimm_pmem_region", [
+ ("u", KeyedUnion(None, libxl_nvdimm_pmem_region_type, "type",
+ [("raw", libxl_nvdimm_pmem_raw_region),
+ ])),
+ ])
diff --git a/tools/xl/Makefile b/tools/xl/Makefile
index a5117ab3fb..0c374b3c2a 100644
--- a/tools/xl/Makefile
+++ b/tools/xl/Makefile
@@ -22,7 +22,7 @@ XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
XL_OBJS += xl_info.o xl_console.o xl_misc.o
XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
-XL_OBJS += xl_vdispl.o
+XL_OBJS += xl_vdispl.o xl_nvdimm.o
$(XL_OBJS): CFLAGS += $(CFLAGS_libxentoollog)
$(XL_OBJS): CFLAGS += $(CFLAGS_XL)
diff --git a/tools/xl/xl.h b/tools/xl/xl.h
index 6b60d1db50..9359a3d9c7 100644
--- a/tools/xl/xl.h
+++ b/tools/xl/xl.h
@@ -210,6 +210,7 @@ int main_psr_cat_cbm_set(int argc, char **argv);
int main_psr_cat_show(int argc, char **argv);
#endif
int main_qemu_monitor_command(int argc, char **argv);
+int main_pmem_list(int argc, char **argv);
void help(const char *command);
diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c
index 5546cf66e7..f525cafcdf 100644
--- a/tools/xl/xl_cmdtable.c
+++ b/tools/xl/xl_cmdtable.c
@@ -616,6 +616,12 @@ struct cmd_spec cmd_table[] = {
"Issue a qemu monitor command to the device model of a domain",
"<Domain> <Command>",
},
+ { "pmem-list",
+ &main_pmem_list, 0, 0,
+ "List PMEM regions of specified types, or all PMEM regions if no type is
specified",
+ "[options]",
+ "-r, --raw List PMEM regions detected by Xen hypervisor\n"
+ },
};
int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);
diff --git a/tools/xl/xl_nvdimm.c b/tools/xl/xl_nvdimm.c
new file mode 100644
index 0000000000..799c76e4c2
--- /dev/null
+++ b/tools/xl/xl_nvdimm.c
@@ -0,0 +1,92 @@
+/*
+ * tools/xl/xl_nvdimm.c
+ *
+ * <One line description of the file and what it does>
+ *
+ * Copyright (C) 2017 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms and conditions of the GNU General Public
+ * License, version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libxl.h>
+
+#include "xl.h"
+#include "xl_utils.h"
+
+typedef void (*show_region_fn_t)(libxl_nvdimm_pmem_region *region,
+ unsigned int idx);
+
+static void show_raw_region(libxl_nvdimm_pmem_region *region, unsigned int idx)
+{
+ libxl_nvdimm_pmem_raw_region *raw = ®ion->u.raw;
+
+ printf(" %u: mfn 0x%lx - 0x%lx, pxm %u\n",
+ idx, raw->smfn, raw->emfn, raw->pxm);
+}
+
+static show_region_fn_t show_region_fn[] = {
+ [LIBXL_NVDIMM_PMEM_REGION_TYPE_RAW] = show_raw_region,
+};
+
+static int list_regions(libxl_nvdimm_pmem_region_type type)
+{
+ int rc;
+ libxl_nvdimm_pmem_region *regions = NULL;
+ unsigned int nr, i;
+
+ rc = libxl_nvdimm_pmem_get_regions(ctx, type, ®ions, &nr);
+ if (rc || !nr)
+ goto out;
+
+ printf("List of %s PMEM regions:\n",
+ libxl_nvdimm_pmem_region_type_to_string(type));
+ for (i = 0; i < nr; i++)
+ show_region_fn[type](®ions[i], i);
+
+ out:
+ if (regions)
+ free(regions);
+
+ if (rc)
+ fprintf(stderr, "Error: pmem-list failed: %s\n", strerror(errno));
+
+ return rc;
+}
+
+int main_pmem_list(int argc, char **argv)
+{
+ static struct option opts[] = {
+ { "raw", 0, 0, 'r' },
+ COMMON_LONG_OPTS
+ };
+
+ bool all = true, raw = false;
+ int opt, ret = 0;
+
+ SWITCH_FOREACH_OPT(opt, "r", opts, "pmem-list", 0) {
+ case 'r':
+ all = false;
+ raw = true;
+ break;
+ }
+
+ if (all || raw)
+ ret = list_regions(LIBXL_NVDIMM_PMEM_REGION_TYPE_RAW);
+
+ return ret;
+}
--
2.15.1
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |