diff mbox series

[ndctl,4/7] daxctl: add basic config parsing support

Message ID 20210831090459.2306727-5-vishal.l.verma@intel.com (mailing list archive)
State New, archived
Headers show
Series Policy based reconfiguration for daxctl | expand

Commit Message

Verma, Vishal L Aug. 31, 2021, 9:04 a.m. UTC
Add support similar to ndctl and libndctl for parsing config files. This
allows storing a config file path/list in the daxctl_ctx, and adds APIs
for setting and retrieving it.

Cc: QI Fuli <qi.fuli@fujitsu.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 daxctl/lib/libdaxctl.c   | 37 +++++++++++++++++++++++++++++++++++++
 daxctl/libdaxctl.h       |  2 ++
 daxctl/Makefile.am       |  1 +
 daxctl/lib/Makefile.am   |  4 ++++
 daxctl/lib/libdaxctl.sym |  2 ++
 5 files changed, 46 insertions(+)

Comments

qi.fuli@fujitsu.com Sept. 2, 2021, 12:19 p.m. UTC | #1
> Subject: [ndctl PATCH 4/7] daxctl: add basic config parsing support
> 
> Add support similar to ndctl and libndctl for parsing config files. This allows
> storing a config file path/list in the daxctl_ctx, and adds APIs for setting and
> retrieving it.
> 
> Cc: QI Fuli <qi.fuli@fujitsu.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
>  daxctl/lib/libdaxctl.c   | 37
> +++++++++++++++++++++++++++++++++++++
>  daxctl/libdaxctl.h       |  2 ++
>  daxctl/Makefile.am       |  1 +
>  daxctl/lib/Makefile.am   |  4 ++++
>  daxctl/lib/libdaxctl.sym |  2 ++
>  5 files changed, 46 insertions(+)
> 
> diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c index 860bd9c..659d2fe
> 100644
> --- a/daxctl/lib/libdaxctl.c
> +++ b/daxctl/lib/libdaxctl.c
> @@ -17,6 +17,8 @@
>  #include <util/log.h>
>  #include <util/sysfs.h>
>  #include <util/iomem.h>
> +#include <util/strbuf.h>
> +#include <util/parse-configs.h>
>  #include <daxctl/libdaxctl.h>
>  #include "libdaxctl-private.h"
> 
> @@ -37,6 +39,7 @@ struct daxctl_ctx {
>  	struct log_ctx ctx;
>  	int refcount;
>  	void *userdata;
> +	const char *configs;
>  	int regions_init;
>  	struct list_head regions;
>  	struct kmod_ctx *kmod_ctx;
> @@ -68,6 +71,40 @@ DAXCTL_EXPORT void daxctl_set_userdata(struct
> daxctl_ctx *ctx, void *userdata)
>  	ctx->userdata = userdata;
>  }
> 
> +DAXCTL_EXPORT void daxctl_set_configs(struct daxctl_ctx **ctx, char
> +*conf_dir) {
> +	struct dirent **namelist;
> +	struct strbuf value = STRBUF_INIT;
> +	int rc;
> +
> +	if ((!ctx) || (!conf_dir))
> +		return;
> +
> +	rc = scandir(conf_dir, &namelist, filter_conf_files, alphasort);
> +	if (rc == -1) {
> +		perror("scandir");
> +		return;
> +	}
> +
> +	while (rc--) {
> +		if (value.len)
> +			strbuf_addstr(&value, " ");
> +		strbuf_addstr(&value, conf_dir);
> +		strbuf_addstr(&value, "/");
> +		strbuf_addstr(&value, namelist[rc]->d_name);
> +		free(namelist[rc]);
> +	}
> +	(*ctx)->configs = strbuf_detach(&value, NULL);
> +	free(namelist);
> +}
> +
> +DAXCTL_EXPORT const char *daxctl_get_configs(struct daxctl_ctx *ctx) {
> +	if (ctx == NULL)
> +		return NULL;
> +	return ctx->configs;
> +}
> +
>  /**
>   * daxctl_new - instantiate a new library context
>   * @ctx: context to establish
> diff --git a/daxctl/libdaxctl.h b/daxctl/libdaxctl.h index 683ae9c..9388f85 100644
> --- a/daxctl/libdaxctl.h
> +++ b/daxctl/libdaxctl.h
> @@ -28,6 +28,8 @@ int daxctl_get_log_priority(struct daxctl_ctx *ctx);  void
> daxctl_set_log_priority(struct daxctl_ctx *ctx, int priority);  void
> daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata);  void
> *daxctl_get_userdata(struct daxctl_ctx *ctx);
> +void daxctl_set_configs(struct daxctl_ctx **ctx, char *conf_dir); const
> +char *daxctl_get_configs(struct daxctl_ctx *ctx);
> 
>  struct daxctl_region;
>  struct daxctl_region *daxctl_new_region(struct daxctl_ctx *ctx, int id, diff --git
> a/daxctl/Makefile.am b/daxctl/Makefile.am index 9b1313a..a9845a0 100644
> --- a/daxctl/Makefile.am
> +++ b/daxctl/Makefile.am
> @@ -10,6 +10,7 @@ config.h: $(srcdir)/Makefile.am
>  		"$(daxctl_modprobe_datadir)/$(daxctl_modprobe_data)"' >>$@
> && \
>  	echo '#define DAXCTL_MODPROBE_INSTALL \
>  		"$(sysconfdir)/modprobe.d/$(daxctl_modprobe_data)"' >>$@
> +	$(AM_V_GEN) echo '#define DAXCTL_CONF_DIR  "$(ndctl_confdir)"'
> >>$@
> 
>  daxctl_SOURCES =\
>  		daxctl.c \
> diff --git a/daxctl/lib/Makefile.am b/daxctl/lib/Makefile.am index
> db2351e..7a53598 100644
> --- a/daxctl/lib/Makefile.am
> +++ b/daxctl/lib/Makefile.am
> @@ -13,6 +13,10 @@ libdaxctl_la_SOURCES =\
>  	../../util/iomem.h \
>  	../../util/sysfs.c \
>  	../../util/sysfs.h \
> +	../../util/strbuf.h \
> +	../../util/strbuf.c \
> +	../../util/wrapper.c \
> +	../../util/usage.c \
>  	../../util/log.c \
>  	../../util/log.h \
>  	../../util/parse-configs.h \
> diff --git a/daxctl/lib/libdaxctl.sym b/daxctl/lib/libdaxctl.sym index
> a13e93d..190b605 100644
> --- a/daxctl/lib/libdaxctl.sym
> +++ b/daxctl/lib/libdaxctl.sym
> @@ -96,4 +96,6 @@ LIBDAXCTL_9 {
>  global:
>  	daxctl_dev_will_auto_online_memory;
>  	daxctl_dev_has_online_memory;
> +	daxctl_set_configs;
> +	daxctl_get_configs;
>  } LIBDAXCTL_8;
> --
> 2.31.1

Looks good to me.
Thank you very much.

QI
Dan Williams Sept. 16, 2021, 10:58 p.m. UTC | #2
On Tue, Aug 31, 2021 at 2:05 AM Vishal Verma <vishal.l.verma@intel.com> wrote:
>
> Add support similar to ndctl and libndctl for parsing config files. This
> allows storing a config file path/list in the daxctl_ctx, and adds APIs
> for setting and retrieving it.
>
> Cc: QI Fuli <qi.fuli@fujitsu.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
>  daxctl/lib/libdaxctl.c   | 37 +++++++++++++++++++++++++++++++++++++
>  daxctl/libdaxctl.h       |  2 ++
>  daxctl/Makefile.am       |  1 +
>  daxctl/lib/Makefile.am   |  4 ++++
>  daxctl/lib/libdaxctl.sym |  2 ++
>  5 files changed, 46 insertions(+)
>
> diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
> index 860bd9c..659d2fe 100644
> --- a/daxctl/lib/libdaxctl.c
> +++ b/daxctl/lib/libdaxctl.c
> @@ -17,6 +17,8 @@
>  #include <util/log.h>
>  #include <util/sysfs.h>
>  #include <util/iomem.h>
> +#include <util/strbuf.h>
> +#include <util/parse-configs.h>
>  #include <daxctl/libdaxctl.h>
>  #include "libdaxctl-private.h"
>
> @@ -37,6 +39,7 @@ struct daxctl_ctx {
>         struct log_ctx ctx;
>         int refcount;
>         void *userdata;
> +       const char *configs;
>         int regions_init;
>         struct list_head regions;
>         struct kmod_ctx *kmod_ctx;
> @@ -68,6 +71,40 @@ DAXCTL_EXPORT void daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata)
>         ctx->userdata = userdata;
>  }
>
> +DAXCTL_EXPORT void daxctl_set_configs(struct daxctl_ctx **ctx, char *conf_dir)
> +{
> +       struct dirent **namelist;
> +       struct strbuf value = STRBUF_INIT;
> +       int rc;
> +
> +       if ((!ctx) || (!conf_dir))
> +               return;
> +
> +       rc = scandir(conf_dir, &namelist, filter_conf_files, alphasort);
> +       if (rc == -1) {
> +               perror("scandir");
> +               return;
> +       }
> +
> +       while (rc--) {
> +               if (value.len)
> +                       strbuf_addstr(&value, " ");
> +               strbuf_addstr(&value, conf_dir);
> +               strbuf_addstr(&value, "/");
> +               strbuf_addstr(&value, namelist[rc]->d_name);
> +               free(namelist[rc]);
> +       }
> +       (*ctx)->configs = strbuf_detach(&value, NULL);
> +       free(namelist);
> +}
> +
> +DAXCTL_EXPORT const char *daxctl_get_configs(struct daxctl_ctx *ctx)
> +{
> +       if (ctx == NULL)
> +               return NULL;
> +       return ctx->configs;
> +}
> +
>  /**
>   * daxctl_new - instantiate a new library context
>   * @ctx: context to establish
> diff --git a/daxctl/libdaxctl.h b/daxctl/libdaxctl.h
> index 683ae9c..9388f85 100644
> --- a/daxctl/libdaxctl.h
> +++ b/daxctl/libdaxctl.h
> @@ -28,6 +28,8 @@ int daxctl_get_log_priority(struct daxctl_ctx *ctx);
>  void daxctl_set_log_priority(struct daxctl_ctx *ctx, int priority);
>  void daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata);
>  void *daxctl_get_userdata(struct daxctl_ctx *ctx);
> +void daxctl_set_configs(struct daxctl_ctx **ctx, char *conf_dir);
> +const char *daxctl_get_configs(struct daxctl_ctx *ctx);
>
>  struct daxctl_region;
>  struct daxctl_region *daxctl_new_region(struct daxctl_ctx *ctx, int id,
> diff --git a/daxctl/Makefile.am b/daxctl/Makefile.am
> index 9b1313a..a9845a0 100644
> --- a/daxctl/Makefile.am
> +++ b/daxctl/Makefile.am
> @@ -10,6 +10,7 @@ config.h: $(srcdir)/Makefile.am
>                 "$(daxctl_modprobe_datadir)/$(daxctl_modprobe_data)"' >>$@ && \
>         echo '#define DAXCTL_MODPROBE_INSTALL \
>                 "$(sysconfdir)/modprobe.d/$(daxctl_modprobe_data)"' >>$@
> +       $(AM_V_GEN) echo '#define DAXCTL_CONF_DIR  "$(ndctl_confdir)"' >>$@

This gets back to my namespace question about collisions between
daxctl, ndctl, and cxl-cli conf snippets. I think they should each get
their own directory in /etc, then we don't need to encode any prefixes
into section names. What do you think?
Verma, Vishal L Nov. 17, 2021, 11:17 p.m. UTC | #3
On Thu, 2021-09-16 at 15:58 -0700, Dan Williams wrote:
> On Tue, Aug 31, 2021 at 2:05 AM Vishal Verma <vishal.l.verma@intel.com> wrote:
> > 
> > Add support similar to ndctl and libndctl for parsing config files. This
> > allows storing a config file path/list in the daxctl_ctx, and adds APIs
> > for setting and retrieving it.
> > 
> > Cc: QI Fuli <qi.fuli@fujitsu.com>
> > Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> > ---
> >  daxctl/lib/libdaxctl.c   | 37 +++++++++++++++++++++++++++++++++++++
> >  daxctl/libdaxctl.h       |  2 ++
> >  daxctl/Makefile.am       |  1 +
> >  daxctl/lib/Makefile.am   |  4 ++++
> >  daxctl/lib/libdaxctl.sym |  2 ++
> >  5 files changed, 46 insertions(+)
> > 
[snip]
> > 
> > diff --git a/daxctl/Makefile.am b/daxctl/Makefile.am
> > index 9b1313a..a9845a0 100644
> > --- a/daxctl/Makefile.am
> > +++ b/daxctl/Makefile.am
> > @@ -10,6 +10,7 @@ config.h: $(srcdir)/Makefile.am
> >                 "$(daxctl_modprobe_datadir)/$(daxctl_modprobe_data)"' >>$@ && \
> >         echo '#define DAXCTL_MODPROBE_INSTALL \
> >                 "$(sysconfdir)/modprobe.d/$(daxctl_modprobe_data)"' >>$@
> > +       $(AM_V_GEN) echo '#define DAXCTL_CONF_DIR  "$(ndctl_confdir)"' >>$@
> 
> This gets back to my namespace question about collisions between
> daxctl, ndctl, and cxl-cli conf snippets. I think they should each get
> their own directory in /etc, then we don't need to encode any prefixes
> into section names. What do you think?

Yep splitting config directories sounds cleaner - I'll change this.
diff mbox series

Patch

diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index 860bd9c..659d2fe 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -17,6 +17,8 @@ 
 #include <util/log.h>
 #include <util/sysfs.h>
 #include <util/iomem.h>
+#include <util/strbuf.h>
+#include <util/parse-configs.h>
 #include <daxctl/libdaxctl.h>
 #include "libdaxctl-private.h"
 
@@ -37,6 +39,7 @@  struct daxctl_ctx {
 	struct log_ctx ctx;
 	int refcount;
 	void *userdata;
+	const char *configs;
 	int regions_init;
 	struct list_head regions;
 	struct kmod_ctx *kmod_ctx;
@@ -68,6 +71,40 @@  DAXCTL_EXPORT void daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata)
 	ctx->userdata = userdata;
 }
 
+DAXCTL_EXPORT void daxctl_set_configs(struct daxctl_ctx **ctx, char *conf_dir)
+{
+	struct dirent **namelist;
+	struct strbuf value = STRBUF_INIT;
+	int rc;
+
+	if ((!ctx) || (!conf_dir))
+		return;
+
+	rc = scandir(conf_dir, &namelist, filter_conf_files, alphasort);
+	if (rc == -1) {
+		perror("scandir");
+		return;
+	}
+
+	while (rc--) {
+		if (value.len)
+			strbuf_addstr(&value, " ");
+		strbuf_addstr(&value, conf_dir);
+		strbuf_addstr(&value, "/");
+		strbuf_addstr(&value, namelist[rc]->d_name);
+		free(namelist[rc]);
+	}
+	(*ctx)->configs = strbuf_detach(&value, NULL);
+	free(namelist);
+}
+
+DAXCTL_EXPORT const char *daxctl_get_configs(struct daxctl_ctx *ctx)
+{
+	if (ctx == NULL)
+		return NULL;
+	return ctx->configs;
+}
+
 /**
  * daxctl_new - instantiate a new library context
  * @ctx: context to establish
diff --git a/daxctl/libdaxctl.h b/daxctl/libdaxctl.h
index 683ae9c..9388f85 100644
--- a/daxctl/libdaxctl.h
+++ b/daxctl/libdaxctl.h
@@ -28,6 +28,8 @@  int daxctl_get_log_priority(struct daxctl_ctx *ctx);
 void daxctl_set_log_priority(struct daxctl_ctx *ctx, int priority);
 void daxctl_set_userdata(struct daxctl_ctx *ctx, void *userdata);
 void *daxctl_get_userdata(struct daxctl_ctx *ctx);
+void daxctl_set_configs(struct daxctl_ctx **ctx, char *conf_dir);
+const char *daxctl_get_configs(struct daxctl_ctx *ctx);
 
 struct daxctl_region;
 struct daxctl_region *daxctl_new_region(struct daxctl_ctx *ctx, int id,
diff --git a/daxctl/Makefile.am b/daxctl/Makefile.am
index 9b1313a..a9845a0 100644
--- a/daxctl/Makefile.am
+++ b/daxctl/Makefile.am
@@ -10,6 +10,7 @@  config.h: $(srcdir)/Makefile.am
 		"$(daxctl_modprobe_datadir)/$(daxctl_modprobe_data)"' >>$@ && \
 	echo '#define DAXCTL_MODPROBE_INSTALL \
 		"$(sysconfdir)/modprobe.d/$(daxctl_modprobe_data)"' >>$@
+	$(AM_V_GEN) echo '#define DAXCTL_CONF_DIR  "$(ndctl_confdir)"' >>$@
 
 daxctl_SOURCES =\
 		daxctl.c \
diff --git a/daxctl/lib/Makefile.am b/daxctl/lib/Makefile.am
index db2351e..7a53598 100644
--- a/daxctl/lib/Makefile.am
+++ b/daxctl/lib/Makefile.am
@@ -13,6 +13,10 @@  libdaxctl_la_SOURCES =\
 	../../util/iomem.h \
 	../../util/sysfs.c \
 	../../util/sysfs.h \
+	../../util/strbuf.h \
+	../../util/strbuf.c \
+	../../util/wrapper.c \
+	../../util/usage.c \
 	../../util/log.c \
 	../../util/log.h \
 	../../util/parse-configs.h \
diff --git a/daxctl/lib/libdaxctl.sym b/daxctl/lib/libdaxctl.sym
index a13e93d..190b605 100644
--- a/daxctl/lib/libdaxctl.sym
+++ b/daxctl/lib/libdaxctl.sym
@@ -96,4 +96,6 @@  LIBDAXCTL_9 {
 global:
 	daxctl_dev_will_auto_online_memory;
 	daxctl_dev_has_online_memory;
+	daxctl_set_configs;
+	daxctl_get_configs;
 } LIBDAXCTL_8;