From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C2BBA43700; Fri, 15 Dec 2023 18:26:48 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 57A6343378; Fri, 15 Dec 2023 18:26:48 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by mails.dpdk.org (Postfix) with ESMTP id E23D243377 for ; Fri, 15 Dec 2023 18:26:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702661207; x=1734197207; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Jl8Upv9c+THq3E/6rtRbdpy3u7DSwZOWMBllB/HvJfw=; b=h2u00kOxhAfZaqqVf6zVR/N5nUhlmU4cCu1TYvHx0WS4hBYWWxfeimHG Eh/D5AgK3tB3jIUfuVAV3S+8Q/jGlhBM0+GwhaubA3g/TVpYrTQbpddCq 1yuHf7TfXClvT+cVYpj/pFK9QG0D0n4XJWEf+EZicjymmRhPSGTm6KT79 PC47UY2HYz9ww6Nkk/iak+BnZnXOQV+YdQnhfbaCgLxDmiczZqiuuOXy0 C3uAdrAnErI0Jqak76+YwrA0BUQtAnPM0aptoSwQTKY1SMbnTN1XCS4L2 CiHDpSvX5qAkxdmomjsGfJv7JosOK1UT/cwmvwH1++mWekSNGf0ZlAeqV A==; X-IronPort-AV: E=McAfee;i="6600,9927,10925"; a="2474230" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="2474230" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 09:26:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="22914238" Received: from unknown (HELO silpixa00400630.ir.intel.com) ([10.237.213.151]) by orviesa001.jf.intel.com with ESMTP; 15 Dec 2023 09:26:44 -0800 From: Euan Bourke To: dev@dpdk.org Cc: Euan Bourke , Thomas Monjalon , Bruce Richardson Subject: [PATCH v4 1/8] arg_parser: new library for command line parsing Date: Fri, 15 Dec 2023 17:26:25 +0000 Message-Id: <20231215172632.3102502-2-euan.bourke@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231215172632.3102502-1-euan.bourke@intel.com> References: <20231215172632.3102502-1-euan.bourke@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add a new library to make it easier for eal and other libraries to parse command line arguments. The first function in this library is one to parse a corelist string into an array of individual core ids. The function will then return the total number of cores described in the corelist. Signed-off-by: Euan Bourke --- .mailmap | 1 + MAINTAINERS | 4 ++ doc/api/doxy-api-index.md | 1 + doc/api/doxy-api.conf.in | 1 + lib/arg_parser/arg_parser.c | 108 ++++++++++++++++++++++++++++++++ lib/arg_parser/meson.build | 7 +++ lib/arg_parser/rte_arg_parser.h | 66 +++++++++++++++++++ lib/arg_parser/version.map | 10 +++ lib/meson.build | 2 + 9 files changed, 200 insertions(+) create mode 100644 lib/arg_parser/arg_parser.c create mode 100644 lib/arg_parser/meson.build create mode 100644 lib/arg_parser/rte_arg_parser.h create mode 100644 lib/arg_parser/version.map diff --git a/.mailmap b/.mailmap index ab0742a382..528bc68a30 100644 --- a/.mailmap +++ b/.mailmap @@ -379,6 +379,7 @@ Eric Zhang Erik Gabriel Carrillo Erik Ziegenbalg Erlu Chen +Euan Bourke Eugenio PĂ©rez Eugeny Parshutin Evan Swanson diff --git a/MAINTAINERS b/MAINTAINERS index 0d1c8126e3..68ef5ba14b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1756,6 +1756,10 @@ M: Nithin Dabilpuram M: Pavan Nikhilesh F: lib/node/ +Argument parsing +M: Bruce Richardson +F: lib/arg_parser/ + Test Applications ----------------- diff --git a/doc/api/doxy-api-index.md b/doc/api/doxy-api-index.md index a6a768bd7c..743d3b6773 100644 --- a/doc/api/doxy-api-index.md +++ b/doc/api/doxy-api-index.md @@ -215,6 +215,7 @@ The public API headers are grouped by topics: [udp4_input_node](@ref rte_node_udp4_input_api.h) - **basic**: + [argument parsing](@ref rte_arg_parser.h), [bitops](@ref rte_bitops.h), [approx fraction](@ref rte_approx.h), [random](@ref rte_random.h), diff --git a/doc/api/doxy-api.conf.in b/doc/api/doxy-api.conf.in index e94c9e4e46..05718ba6ed 100644 --- a/doc/api/doxy-api.conf.in +++ b/doc/api/doxy-api.conf.in @@ -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/arg_parser \ @TOPDIR@/lib/bbdev \ @TOPDIR@/lib/bitratestats \ @TOPDIR@/lib/bpf \ diff --git a/lib/arg_parser/arg_parser.c b/lib/arg_parser/arg_parser.c new file mode 100644 index 0000000000..d8324a27b1 --- /dev/null +++ b/lib/arg_parser/arg_parser.c @@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Intel Corporation + */ + +#include +#include +#include +#include +#include + +#include +#include + + +struct core_bits { + uint8_t bits[(UINT16_MAX + 1) / CHAR_BIT]; + uint16_t max_bit_set; + uint16_t min_bit_set; + uint32_t total_bits_set; +}; + +static inline bool +get_core_bit(struct core_bits *mask, uint16_t idx) +{ + return !!(mask->bits[idx / CHAR_BIT] & (1 << (idx % CHAR_BIT))); +} + +static inline void +set_core_bit(struct core_bits *mask, uint16_t idx) +{ + if (get_core_bit(mask, idx)) + return; + + mask->bits[idx / CHAR_BIT] |= 1 << (idx % CHAR_BIT); + /* Update min and max bit if its first time setting a bit */ + if (++(mask->total_bits_set) == 1) { + mask->min_bit_set = idx; + mask->max_bit_set = idx; + return; + } + + if (idx > mask->max_bit_set) + mask->max_bit_set = idx; + + if (idx < mask->min_bit_set) + mask->min_bit_set = idx; +} + +static inline uint32_t +corebits_to_array(struct core_bits *mask, uint16_t *cores, size_t max_cores) +{ + uint32_t count = 0; + for (uint32_t i = mask->min_bit_set; i <= mask->max_bit_set && count < max_cores; i++) { + if (get_core_bit(mask, i)) + cores[count++] = i; + } + return mask->total_bits_set; +} + + +int +rte_arg_parse_corelist(const char *corelist, uint16_t *cores, uint32_t cores_len) +{ + struct core_bits mask = {0}; + int32_t min = -1; + char *end = NULL; + + min = -1; + do { + int64_t idx; + int32_t max; + + while (isblank(*corelist)) + corelist++; + if (!isdigit(*corelist)) + return -EINVAL; + + errno = 0; + idx = strtol(corelist, &end, 10); + if (errno || end == NULL || idx > UINT16_MAX) + return -EINVAL; + while (isblank(*end)) + end++; + if (*end == '-') + min = idx; + + else if (*end == ',' || *end == '\0') { + /* Check for first number in range */ + if (min == -1) + min = max = idx; + /* Swap if min is larger than max */ + else if (min > idx) { + max = min; + min = idx; + } else /* Normal case */ + max = idx; + + for (; min <= max; min++) + set_core_bit(&mask, min); + + min = -1; + } else + return -EINVAL; + corelist = end + 1; + } while (*end != '\0'); + + return corebits_to_array(&mask, cores, cores_len); +} diff --git a/lib/arg_parser/meson.build b/lib/arg_parser/meson.build new file mode 100644 index 0000000000..6ee228bd69 --- /dev/null +++ b/lib/arg_parser/meson.build @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2023 Intel Corporation + +sources = files('arg_parser.c') +headers = files('rte_arg_parser.h') + +includes += global_inc diff --git a/lib/arg_parser/rte_arg_parser.h b/lib/arg_parser/rte_arg_parser.h new file mode 100644 index 0000000000..a1ef428b95 --- /dev/null +++ b/lib/arg_parser/rte_arg_parser.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Intel Corporation + */ + +#ifndef _RTE_ARG_PARSER_H_ +#define _RTE_ARG_PARSER_H_ + +/** + * @file + * + * RTE Argument Parsing API + * + * The argument parsing API is a collection of functions to help parse + * command line arguments. The API takes a string input and will return + * it to the user in a more usable format. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include + + +/** + * Convert a string describing a list of core ids into an array of core ids. + * + * On success, the passed array is filled with the core ids present in the list up + * to the "cores_len", and the number of unique cores present in the "corelist" + * is returned. + * For example, passing a 1-3,6 "corelist" results in an array of [1, 2, 3, 6] + * and would return 4. + * + * NOTE: if the length of the input array is insufficient to hold the number of core ids + * in "corelist" the input array is filled to capacity but the return value is the + * number of elements which would have been written to the array, had enough space been + * available. [This is similar to the behaviour of the snprintf function]. Because of + * this, the number of core values in the "corelist" may be determined by calling the + * function with a NULL array pointer and array length given as 0. + * + * @param corelist + * Input string describing a list of core ids. + * @param cores + * An array where to store the core ids. + * Array can be NULL if "cores_len" is 0. + * @param cores_len + * The length of the "cores" array. + * If the size is smaller than that needed to hold all cores from "corelist", + * only "cores_len" elements will be written to the array. + * @return + * n: the number of unique cores present in "corelist". + * -EINVAL if the string was invalid. + * NOTE: if n > "cores_len", then only "cores_len" elements in the "cores" array are valid. + */ +__rte_experimental +int +rte_arg_parse_corelist(const char *corelist, uint16_t *cores, uint32_t cores_len); + + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_ARG_PARSER_H_ */ diff --git a/lib/arg_parser/version.map b/lib/arg_parser/version.map new file mode 100644 index 0000000000..b0caaac569 --- /dev/null +++ b/lib/arg_parser/version.map @@ -0,0 +1,10 @@ +DPDK_24 { + local: *; +}; + +EXPERIMENTAL { + global: + + # added in 24.03 + rte_arg_parse_corelist; +}; diff --git a/lib/meson.build b/lib/meson.build index 6c143ce5a6..db9e769033 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -11,6 +11,7 @@ libraries = [ 'log', 'kvargs', # eal depends on kvargs + 'arg_parser', 'telemetry', # basic info querying 'eal', # everything depends on eal 'ring', @@ -72,6 +73,7 @@ if is_ms_compiler 'log', 'kvargs', 'telemetry', + 'arg_parser', ] endif -- 2.34.1