Skip to contentSkip to conversation

Add argument manipulation library

RFC In review Bruce Richardson 4 participants 9 messages Nov 2, 2023 – Jan 24, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Add argument manipulation library

DPDK has traditionally assumed that apps are written where argc/argv parameters are passed directly to rte_eal_init() and then app arguments are handled afterwards, as is done in the DPDK apps and examples.

However, based on other projects, like VPP and OVS, we know that this is often not the case. Instead, the apps build up argument lists internally themselves and pass those to the init function, and DPDK never directly accesses the real argc/argv values.

Therefore, let's make this mode of operation a little easier to use, by adding in an args library to allow an app to dynamically build up an args array and then pass that to EAL init. This library allows things like, for example, initializing a list from argv and then checking if a particular parameter is present, adding it if not. All the basics of such a library are included in patch 1, and I've found it of use myself when writing some simple apps internally using DPDK.

Patches 2 and 3 go a little further than this, and allow for a slightly different use-case, that where an app may want to allow mixing of EAL arguments in with app args. These patches allow the user to query if a particular argument is an EAL arg or not, or a valid app argument. The idea here would be, that an app could go through it's argument list, building up an argument list for EAL init by just picking out the EAL arguments from a full argument list. I'm less convinced of the value of this compared to the basics in patch 1, but I think the idea is interesting so seeking feedback on it.

Bruce Richardson (3):
args: new library to allow easier manipulation of cmdline args
eal: allow export of the cmdline argument parsing options
args: add functions to check parameter validity

 doc/api/doxy-api-index.md           |   1 +
 doc/api/doxy-api.conf.in            |   1 +
 lib/args/args.c                     | 301 ++++++++++++++++++++++++++++
 lib/args/meson.build                |   5 +
 lib/args/rte_args.h                 | 255 +++++++++++++++++++++++
 lib/args/version.map                |  22 ++
 lib/eal/common/eal_common_options.c |   9 +
 lib/eal/include/rte_eal.h           |  14 ++
 lib/eal/version.map                 |   1 +
 lib/meson.build                     |   2 +
 10 files changed, 611 insertions(+)
 create mode 100644 lib/args/args.c
 create mode 100644 lib/args/meson.build
 create mode 100644 lib/args/rte_args.h
 create mode 100644 lib/args/version.map

--
2.39.2

#4 lore raw

RFC 2/3 eal: allow export of the cmdline argument parsing options

Allow other libraries in DPDK to know about the options EAL takes, by exporting the long and short getopt options via an internal-only function.

---
 lib/eal/common/eal_common_options.c |  9 +++++++++
 lib/eal/include/rte_eal.h           | 14 ++++++++++++++
 lib/eal/version.map                 |  1 +
 3 files changed, 24 insertions(+)
lib/eal/common/eal_common_options.c +9 -0
index a6d21f1cba..e26f66ea07 100644
@@ -175,6 +175,15 @@ rte_set_application_usage_hook(rte_usage_hook_t usage_func)
 	return old_func;
 }
 
+__rte_internal
+int
+rte_eal_getopt_params(const char **shortopts, const struct option **longopts)
+{
+	*shortopts = eal_short_options;
+	*longopts = eal_long_options;
+	return 0;
+}
+
 #ifndef RTE_EXEC_ENV_WINDOWS
 static char **eal_args;
 static char **eal_app_args;
lib/eal/include/rte_eal.h +14 -0
index cd318ee141..3bc5447abf 100644
@@ -514,6 +514,20 @@ __rte_internal
 int
 rte_eal_parse_coremask(const char *coremask, int *cores);
 
+struct option;
+/**
+ * @internal
+ * Return the getopt parameters used by EAL when parsing cmdline arguments
+ *
+ * @shortopts
+ *   Pointer for the short options for getopt
+ * @longopts
+ *   Pointer for the longer options used by getopt_long
+ */
+__rte_internal
+int
+rte_eal_getopt_params(const char **shortopts, const struct option **longopts);
+
 #ifdef __cplusplus
 }
 #endif
lib/eal/version.map +1 -0
index e00a844805..6c0a7145ff 100644
@@ -421,6 +421,7 @@ INTERNAL {
 	rte_bus_register;
 	rte_bus_unregister;
 	rte_eal_get_baseaddr;
+	rte_eal_getopt_params;
 	rte_eal_parse_coremask;
 	rte_firmware_read;
 	rte_intr_allow_others;
Signature · 1 line
2.39.2
#1 lore raw

RFC 1/3 args: new library to allow easier manipulation of cmdline args

In reply to Bruce Richardson

Add a new small library to make it easier for apps to work with cmdline arguments and build up args to use when initializing EAL.

This library is optional, and can be disabled at build time using the disable libraries meson option.

---
 doc/api/doxy-api-index.md |   1 +
 doc/api/doxy-api.conf.in  |   1 +
 lib/args/args.c           | 179 ++++++++++++++++++++++++++++++++++
 lib/args/meson.build      |   5 +
 lib/args/rte_args.h       | 199 ++++++++++++++++++++++++++++++++++++++
 lib/args/version.map      |  19 ++++
 lib/meson.build           |   2 +
 7 files changed, 406 insertions(+)
 create mode 100644 lib/args/args.c
 create mode 100644 lib/args/meson.build
 create mode 100644 lib/args/rte_args.h
 create mode 100644 lib/args/version.map
doc/api/doxy-api-index.md +1 -0
index a6a768bd7c..6ba1bb40e9 100644
@@ -222,6 +222,7 @@ The public API headers are grouped by topics:
   [key/value args](@ref rte_kvargs.h),
   [string](@ref rte_string_fns.h),
   [thread](@ref rte_thread.h)
+  [cmdline args management](@ref rte_args.h),
 
 - **debug**:
   [jobstats](@ref rte_jobstats.h),
doc/api/doxy-api.conf.in +1 -0
index e94c9e4e46..15b540553d 100644
@@ -28,6 +28,7 @@ INPUT                   = @TOPDIR@/doc/api/doxy-api-index.md \
                           @TOPDIR@/lib/eal/include \
                           @TOPDIR@/lib/eal/include/generic \
                           @TOPDIR@/lib/acl \
+                          @TOPDIR@/lib/args \
                           @TOPDIR@/lib/bbdev \
                           @TOPDIR@/lib/bitratestats \
                           @TOPDIR@/lib/bpf \
lib/args/args.c new file +179 -0

179 lines not on this page · raw message

lib/args/meson.build new file +5 -0
new file mode 100644
index 0000000000..997f76a8bb
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2023 Intel Corporation
+
+sources = files('args.c')
+headers = files('rte_args.h')
lib/args/rte_args.h new file +199 -0

199 lines not on this page · raw message

lib/args/version.map new file +19 -0
new file mode 100644
index 0000000000..776bd22b82
@@ -0,0 +1,19 @@
+DPDK_24 {
+	local: *;
+};
+
+EXPERIMENTAL {
+	global:
+
+	# added in 24.03
+	rte_args_add;
+	rte_args_add_argv;
+	rte_args_add_list;
+	rte_args_alloc;
+	rte_args_create;
+	rte_args_eal_init;
+	rte_args_free;
+	rte_args_get_argc;
+	rte_args_get_argv;
+	rte_args_has_arg;
+};
lib/meson.build +2 -0
index 6c143ce5a6..7049a4b00e 100644
@@ -13,6 +13,7 @@ libraries = [
         'kvargs', # eal depends on kvargs
         'telemetry', # basic info querying
         'eal', # everything depends on eal
+        'args',
         'ring',
         'rcu', # rcu depends on ring
         'mempool',
@@ -76,6 +77,7 @@ if is_ms_compiler
 endif
 
 optional_libs = [
+        'args',
         'bbdev',
         'bitratestats',
         'bpf',
Signature · 1 line
2.39.2
#5 lore raw

RFC 3/3 args: add functions to check parameter validity

In reply to Bruce Richardson

Add in functions which can be used to check for valid arguments for EAL or for the application. This can be used to separate out mixed arguments.

---
 lib/args/args.c      | 122 +++++++++++++++++++++++++++++++++++++++++++
 lib/args/rte_args.h  |  56 ++++++++++++++++++++
 lib/args/version.map |   3 ++
 3 files changed, 181 insertions(+)
lib/args/args.c +122 -0
index 80eb6670da..c04faa6323 100644
@@ -22,6 +22,15 @@ struct rte_args {
 
 #define DEFAULT_SIZE_HINT 8
 
+#define INVALID_ARG '?'
+#define VALID_ARG 'x'
+#define PARAM_ARG ':'
+
+static char *app_shortopts;
+static struct option *app_longopts;
+static char *eal_shortopts;
+static struct option *eal_longopts;
+
 struct rte_args *
 rte_args_alloc(uint32_t size_hint)
 {
@@ -177,3 +186,116 @@ rte_args_eal_init(struct rte_args *a)
 
 	return rte_eal_init(argc, argv);
 }
+
+static int
+args_register_opts(const char *shortopts, const struct option *longopts,
+		char **sopt_var, struct option **lopt_var)
+{
+	static struct option emptyopt = {0};
+	struct option *lopts = &emptyopt;
+	char *sopts;
+
+	if (shortopts == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	/* for short options, we need to pre-pend ':' for extra argument reporting */
+	if (shortopts[0] != '\0' && shortopts[0] != ':') {
+		size_t len = strlen(shortopts);
+
+		sopts = malloc(len + 2);
+		if (sopts == NULL) {
+			rte_errno = ENOMEM;
+			return -1;
+		}
+		snprintf(sopts, len + 2, ":%s", shortopts);
+	} else
+		sopts = strdup(shortopts);
+
+	/* for long options, we need to ensure we always return a value, not setting a flag */
+	if (longopts != NULL) {
+		int i, n = 0;
+		while (memcmp(&longopts[n], &emptyopt, sizeof(emptyopt)) != 0)
+			n++;
+		lopts = calloc(n + 1, sizeof(emptyopt));
+		if (lopts == NULL) {
+			free(sopts);
+			rte_errno = ENOMEM;
+			return -1;
+		}
+		for (i = 0; i < n; i++) {
+			lopts[i].name = longopts[i].name;
+			lopts[i].has_arg = longopts[i].has_arg;
+			lopts[i].flag = NULL;
+			lopts[i].val = VALID_ARG;
+		}
+		memset(&lopts[i], 0, sizeof(lopts[i]));
+	}
+
+	if (*sopt_var != NULL)
+		free(*sopt_var);
+	if (*lopt_var != NULL)
+		free(*lopt_var);
+
+	*sopt_var = sopts;
+	*lopt_var = lopts;
+	return 0;
+}
+
+int
+rte_args_register_app_opts(const char *shortopts, const struct option *longopts)
+{
+	return args_register_opts(shortopts, longopts, &app_shortopts, &app_longopts);
+}
+
+static int
+args_is_valid_arg(const char *arg, const char *sopts, const struct option *lopts)
+{
+	const int save_optind = optind,
+			save_opterr = opterr,
+			save_optopt = optopt;
+
+	int ret = getopt_long(1, (void *)&arg, sopts, lopts, NULL);
+	switch (ret) {
+	case INVALID_ARG:
+		ret = 0; break;
+	case VALID_ARG:
+		ret = 1; break;
+	case PARAM_ARG:
+		ret = 2; break;
+	default:
+		ret = -1; break;
+	}
+
+	/* restore saved values */
+	optind = save_optind;
+	opterr = save_opterr;
+	optopt = save_optopt;
+	return ret;
+}
+
+int
+rte_args_is_app_arg(const char *arg)
+{
+	if (app_shortopts == NULL || app_longopts == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	return args_is_valid_arg(arg, app_shortopts, app_longopts);
+}
+
+int
+rte_args_is_eal_arg(const char *arg)
+{
+	if (eal_shortopts == NULL || eal_longopts == NULL) {
+		const struct option *l;
+		const char *s;
+
+		rte_eal_getopt_params(&s, &l);
+		if (args_register_opts(s, l, &eal_shortopts, &eal_longopts) < 0)
+			return -1;
+	}
+	return args_is_valid_arg(arg, eal_shortopts, eal_longopts);;
+}
lib/args/rte_args.h +56 -0

62 lines not on this page · raw message

lib/args/version.map +3 -0
index 776bd22b82..ef80f0d762 100644
@@ -16,4 +16,7 @@ EXPERIMENTAL {
 	rte_args_get_argc;
 	rte_args_get_argv;
 	rte_args_has_arg;
+	rte_args_is_app_arg;
+	rte_args_is_eal_arg;
+	rte_args_register_app_opts;
 };
Signature · 1 line
2.39.2
#7 lore raw
In reply to Bruce Richardson
Quoted 48 lines · On Thu, 2 Nov 2023 17:28:46 +0000 Bruce Richardson…

DPDK has traditionally assumed that apps are written where argc/argv parameters are passed directly to rte_eal_init() and then app arguments are handled afterwards, as is done in the DPDK apps and examples.

However, based on other projects, like VPP and OVS, we know that this is often not the case. Instead, the apps build up argument lists internally themselves and pass those to the init function, and DPDK never directly accesses the real argc/argv values.

Therefore, let's make this mode of operation a little easier to use, by adding in an args library to allow an app to dynamically build up an args array and then pass that to EAL init. This library allows things like, for example, initializing a list from argv and then checking if a particular parameter is present, adding it if not. All the basics of such a library are included in patch 1, and I've found it of use myself when writing some simple apps internally using DPDK.

Patches 2 and 3 go a little further than this, and allow for a slightly different use-case, that where an app may want to allow mixing of EAL arguments in with app args. These patches allow the user to query if a particular argument is an EAL arg or not, or a valid app argument. The idea here would be, that an app could go through it's argument list, building up an argument list for EAL init by just picking out the EAL arguments from a full argument list. I'm less convinced of the value of this compared to the basics in patch 1, but I think the idea is interesting so seeking feedback on it.

Bruce Richardson (3):
args: new library to allow easier manipulation of cmdline args
eal: allow export of the cmdline argument parsing options
args: add functions to check parameter validity

 doc/api/doxy-api-index.md           |   1 +
 doc/api/doxy-api.conf.in            |   1 +
 lib/args/args.c                     | 301 ++++++++++++++++++++++++++++
 lib/args/meson.build                |   5 +
 lib/args/rte_args.h                 | 255 +++++++++++++++++++++++
 lib/args/version.map                |  22 ++
 lib/eal/common/eal_common_options.c |   9 +
 lib/eal/include/rte_eal.h           |  14 ++
 lib/eal/version.map                 |   1 +
 lib/meson.build                     |   2 +
 10 files changed, 611 insertions(+)
 create mode 100644 lib/args/args.c
 create mode 100644 lib/args/meson.build
 create mode 100644 lib/args/rte_args.h
 create mode 100644 lib/args/version.map
Quoted 3 lines

--
2.39.2

It would be good to change rte_eal_init() from:
int rte_eal_init(int argc, char **argv);

to
int rte_eal_init(int argc, char * const *argv);

I.e it should not modify the argv's passed.
This would save unnecessary copy in applications that build synthetic args. Example already exists in dumpcap.

#8 lore raw
Quoted 58 lines · Stephen Hemminger
Quoted 48 lines · On Thu, 2 Nov 2023 17:28:46 +0000 Bruce Richardson…

DPDK has traditionally assumed that apps are written where argc/argv parameters are passed directly to rte_eal_init() and then app arguments are handled afterwards, as is done in the DPDK apps and examples.

However, based on other projects, like VPP and OVS, we know that this is often not the case. Instead, the apps build up argument lists internally themselves and pass those to the init function, and DPDK never directly accesses the real argc/argv values.

Therefore, let's make this mode of operation a little easier to use, by adding in an args library to allow an app to dynamically build up an args array and then pass that to EAL init. This library allows things like, for example, initializing a list from argv and then checking if a particular parameter is present, adding it if not. All the basics of such a library are included in patch 1, and I've found it of use myself when writing some simple apps internally using DPDK.

Patches 2 and 3 go a little further than this, and allow for a slightly different use-case, that where an app may want to allow mixing of EAL arguments in with app args. These patches allow the user to query if a particular argument is an EAL arg or not, or a valid app argument. The idea here would be, that an app could go through it's argument list, building up an argument list for EAL init by just picking out the EAL arguments from a full argument list. I'm less convinced of the value of this compared to the basics in patch 1, but I think the idea is interesting so seeking feedback on it.

Bruce Richardson (3):
args: new library to allow easier manipulation of cmdline args
eal: allow export of the cmdline argument parsing options
args: add functions to check parameter validity

 doc/api/doxy-api-index.md           |   1 +
 doc/api/doxy-api.conf.in            |   1 +
 lib/args/args.c                     | 301 ++++++++++++++++++++++++++++
 lib/args/meson.build                |   5 +
 lib/args/rte_args.h                 | 255 +++++++++++++++++++++++
 lib/args/version.map                |  22 ++
 lib/eal/common/eal_common_options.c |   9 +
 lib/eal/include/rte_eal.h           |  14 ++
 lib/eal/version.map                 |   1 +
 lib/meson.build                     |   2 +
 10 files changed, 611 insertions(+)
 create mode 100644 lib/args/args.c
 create mode 100644 lib/args/meson.build
 create mode 100644 lib/args/rte_args.h
 create mode 100644 lib/args/version.map
Quoted 3 lines

--
2.39.2

It would be good to change rte_eal_init() from:
int rte_eal_init(int argc, char **argv);

Quoted 7 lines

to
int rte_eal_init(int argc, char * const *argv);

I.e it should not modify the argv's passed.
This would save unnecessary copy in applications that build synthetic args. Example already exists in dumpcap.

Pushed up a quick patch proposal for that:
http://patches.dpdk.org/project/dpdk/patch/[email protected]/

#6 lore raw

Re: [24.03 RFC 3/3] args: add functions to check parameter validity

In reply to Bruce Richardson

02/11/2023 18:28, Bruce Richardson:

Quoted 7 lines

Add in functions which can be used to check for valid arguments for EAL or for the application. This can be used to separate out mixed arguments.

---
 lib/args/args.c      | 122 +++++++++++++++++++++++++++++++++++++++++++
 lib/args/rte_args.h  |  56 ++++++++++++++++++++

This is helping with args taken by EAL.
So I think it should be hosted in EAL itself.

#2 lore raw

Re: [24.03 RFC 1/3] args: new library to allow easier manipulation of cmdline args

In reply to Bruce Richardson

02/11/2023 18:28, Bruce Richardson:

Quoted 5 lines

Add a new small library to make it easier for apps to work with cmdline arguments and build up args to use when initializing EAL.

This library is optional, and can be disabled at build time using the disable libraries meson option.

This is an optional helper, so why not.

Another help for applications would be to allow initializing DPDK without the need of passing or building argc/argv arguments. I think we could add new functions rte_eal_init_*(). Example:
rte_eal_init_prepare() rte_eal_init_memory(memory parameters) rte_eal_init_devices(devargs) rte_eal_init_threads()

It should be possible to rebuild rte_eal_init()
using above smaller functions to keep the big old
rte_eal_init with argc/argv for compatibility.

#3 lore raw

RE: [24.03 RFC 1/3] args: new library to allow easier manipulation of cmdline args

<snip>

Quoted 19 lines

02/11/2023 18:28, Bruce Richardson:

Quoted 5 lines

Add a new small library to make it easier for apps to work with cmdline arguments and build up args to use when initializing EAL.

This library is optional, and can be disabled at build time using the disable libraries meson option.

This is an optional helper, so why not.

Another help for applications would be to allow initializing DPDK without the need of passing or building argc/argv arguments.
I think we could add new functions rte_eal_init_*().
Example:
rte_eal_init_prepare()
rte_eal_init_memory(memory parameters) rte_eal_init_devices(devargs)
rte_eal_init_threads()

Quoted 2 lines

It should be possible to rebuild rte_eal_init() using above smaller functions to keep the big old rte_eal_init with argc/argv for compatibility.

We have to ensure these new APIs and argc/argv are in sync in future changes. For ex: if we add a new parameter to the API, we need a new argv. This is not a problem, but we have to do this manually.

Quoted 1 line