diff mbox series

[ndctl,06/37] cxl/list: Support multiple memdev device name filter arguments

Message ID 164298554075.3021641.17678360870961637912.stgit@dwillia2-desk3.amr.corp.intel.com
State New, archived
Headers show
Series cxl: Full topology enumeration | expand

Commit Message

Dan Williams Jan. 24, 2022, 12:52 a.m. UTC
Similar to 'ndctl list', allow for a syntax like:

    cxl list -m "$(seq -s ' ' 2 5)"

...to filter the output to just those 4 memdevs.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 Documentation/cxl/cxl-list.txt |   21 ++++++++++++++++++++-
 cxl/filter.c                   |   38 +++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
index c8d10fb5ef86..686e0ea768f0 100644
--- a/Documentation/cxl/cxl-list.txt
+++ b/Documentation/cxl/cxl-list.txt
@@ -30,7 +30,7 @@  OPTIONS
 -------
 -m::
 --memdev=::
-	Specify a cxl memory device name to filter the listing. For example:
+	Specify CXL memory device name(s), or device id(s), to filter the listing. For example:
 ----
 # cxl list --memdev=mem0
 {
@@ -38,6 +38,25 @@  OPTIONS
   "pmem_size":268435456,
   "ram_size":0,
 }
+
+# cxl list -m "0 mem1 2"
+[
+  {
+    "memdev":"mem0",
+    "pmem_size":268435456,
+    "ram_size":0
+  },
+  {
+    "memdev":"mem2",
+    "pmem_size":268435456,
+    "ram_size":268435456
+  },
+  {
+    "memdev":"mem1",
+    "pmem_size":268435456,
+    "ram_size":268435456
+  }
+]
 ----
 
 -M::
diff --git a/cxl/filter.c b/cxl/filter.c
index 21322ed4b4d0..efafaf5a3197 100644
--- a/cxl/filter.c
+++ b/cxl/filter.c
@@ -2,24 +2,40 @@ 
 // Copyright (C) 2015-2020 Intel Corporation. All rights reserved.
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <cxl/libcxl.h>
 #include "filter.h"
 
 struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
-                                         const char *ident)
+					  const char *__ident)
 {
-       int memdev_id;
+	char *ident, *save;
+	const char *name;
+	int memdev_id;
 
-       if (!ident || strcmp(ident, "all") == 0)
-               return memdev;
+	if (!__ident)
+		return memdev;
 
-       if (strcmp(ident, cxl_memdev_get_devname(memdev)) == 0)
-               return memdev;
+	ident = strdup(__ident);
+	if (!ident)
+		return NULL;
 
-       if ((sscanf(ident, "%d", &memdev_id) == 1
-                       || sscanf(ident, "mem%d", &memdev_id) == 1)
-                       && cxl_memdev_get_id(memdev) == memdev_id)
-               return memdev;
+	for (name = strtok_r(ident, " ", &save); name;
+	     name = strtok_r(NULL, " ", &save)) {
+		if (strcmp(name, "all") == 0)
+			break;
 
-       return NULL;
+		if ((sscanf(name, "%d", &memdev_id) == 1 ||
+		     sscanf(name, "mem%d", &memdev_id) == 1) &&
+		    cxl_memdev_get_id(memdev) == memdev_id)
+			break;
+
+		if (strcmp(name, cxl_memdev_get_devname(memdev)) == 0)
+			break;
+	}
+
+	free(ident);
+	if (name)
+		return memdev;
+	return NULL;
 }