From patchwork Fri Dec 15 17:26:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Euan Bourke X-Patchwork-Id: 135234 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1CB3F43700; Fri, 15 Dec 2023 18:26:58 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D3E2B4338F; Fri, 15 Dec 2023 18:26:51 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by mails.dpdk.org (Postfix) with ESMTP id CB73C43399 for ; Fri, 15 Dec 2023 18:26:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702661210; x=1734197210; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BlKltLXAHkZnuOHB8DzlXHBUilkZroPUNFyag8fVs9w=; b=htl2PlpIzBpbry48TTyZy0GMHajiSg/itGWUIMaaDtjymYIjCUtcFSqS XTcPCC2MFN3aVi1N4IIRdqtCn4sDv8KV0m2qzLb9CJwr9lc2NNIriyE/h 8NFk5IYS7WEW2+zBtt3by4lvx2fIvd2giP0lYee1ymX6+YRf1MV4i0n3L kh3qmhIPH+7qrqwktUSRexH8DrGsrI8vatKJLEvFN6nvxvNfuK7nwp9HB 57kV9em6IxjKcuTDhW+6gw5OQhvreZZ1WPorXl8O0DUH9HexjudSp51MD lBF9mcbf3OWEtvuaPkE8FfPo0X/v8z324PE9XdLbPg2o8ehvo6zAAMINj A==; X-IronPort-AV: E=McAfee;i="6600,9927,10925"; a="2474237" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="2474237" 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:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="22914244" Received: from unknown (HELO silpixa00400630.ir.intel.com) ([10.237.213.151]) by orviesa001.jf.intel.com with ESMTP; 15 Dec 2023 09:26:48 -0800 From: Euan Bourke To: dev@dpdk.org Cc: Euan Bourke , Bruce Richardson Subject: [PATCH v4 2/8] arg_parser: add new coremask parsing API Date: Fri, 15 Dec 2023 17:26:26 +0000 Message-Id: <20231215172632.3102502-3-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 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 new coremask parsing API. This API behaves similarly to the corelist parsing API, taking a coremask string, a cores array and a cores_len int. Parsing the coremask string, filling its values into the cores array up to cores_len. The API also returns a 'count' which corresponds to the total number of cores in the coremask string. Signed-off-by: Euan Bourke --- lib/arg_parser/arg_parser.c | 51 +++++++++++++++++++++++++++++++++ lib/arg_parser/rte_arg_parser.h | 34 ++++++++++++++++++++++ lib/arg_parser/version.map | 1 + 3 files changed, 86 insertions(+) diff --git a/lib/arg_parser/arg_parser.c b/lib/arg_parser/arg_parser.c index d8324a27b1..8d86a7b618 100644 --- a/lib/arg_parser/arg_parser.c +++ b/lib/arg_parser/arg_parser.c @@ -11,6 +11,9 @@ #include #include +#define BITS_PER_HEX 4 +#define MAX_COREMASK_SIZE ((UINT16_MAX + 1) / BITS_PER_HEX) + struct core_bits { uint8_t bits[(UINT16_MAX + 1) / CHAR_BIT]; @@ -57,6 +60,15 @@ corebits_to_array(struct core_bits *mask, uint16_t *cores, size_t max_cores) return mask->total_bits_set; } +static int xdigit2val(unsigned char c) +{ + if (isdigit(c)) + return c - '0'; + else if (isupper(c)) + return c - 'A' + 10; + else + return c - 'a' + 10; +} int rte_arg_parse_corelist(const char *corelist, uint16_t *cores, uint32_t cores_len) @@ -106,3 +118,42 @@ rte_arg_parse_corelist(const char *corelist, uint16_t *cores, uint32_t cores_len return corebits_to_array(&mask, cores, cores_len); } + +int +rte_arg_parse_coremask(const char *coremask, uint16_t *cores, uint32_t cores_len) +{ + struct core_bits mask = {0}; + + /* Remove all blank characters ahead and after . + * Remove 0x/0X if exists. + */ + while (isblank(*coremask)) + coremask++; + if (coremask[0] == '0' && ((coremask[1] == 'x') || (coremask[1] == 'X'))) + coremask += 2; + + int32_t i = strlen(coremask); + while ((i > 0) && isblank(coremask[i - 1])) + i--; + if (i == 0 || i > MAX_COREMASK_SIZE) + return -EINVAL; + + uint32_t idx = 0; + + for (i = i - 1; i >= 0; i--) { + int val; + char c = coremask[i]; + + if (isxdigit(c) == 0) + return -EINVAL; + + val = xdigit2val(c); + + for (uint8_t j = 0; j < BITS_PER_HEX; j++, idx++) { + if ((1 << j) & val) + set_core_bit(&mask, idx); + } + } + + return corebits_to_array(&mask, cores, cores_len); +} diff --git a/lib/arg_parser/rte_arg_parser.h b/lib/arg_parser/rte_arg_parser.h index a1ef428b95..49d7daa204 100644 --- a/lib/arg_parser/rte_arg_parser.h +++ b/lib/arg_parser/rte_arg_parser.h @@ -58,6 +58,40 @@ __rte_experimental int rte_arg_parse_corelist(const char *corelist, uint16_t *cores, uint32_t cores_len); +/** + * Convert a string describing a bitmask of core ids into an array of core ids. + * + * On success, the passed array is filled with the core ids present in the + * bitmask up to the "cores_len", and the number of unique cores present in the + * "coremask" is returned. + * For example, passing a 0xA "coremask" results in an array of [1, 3] + * and would return 2. + * + * NOTE: if the length of the input array is insufficient to hold the number of core ids + * in "coremask" 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 "coremask" may be determined by calling the + * function with a NULL array pointer and array length given as 0. + * + * @param coremask + * A string containing a bitmask 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 "coremask", + * only "cores_len" elements will be written to the array. + * @return + * n: the number of unique cores present in "coremask". + * -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_coremask(const char *coremask, uint16_t *cores, uint32_t cores_len); + #ifdef __cplusplus } diff --git a/lib/arg_parser/version.map b/lib/arg_parser/version.map index b0caaac569..b44d4b02b7 100644 --- a/lib/arg_parser/version.map +++ b/lib/arg_parser/version.map @@ -7,4 +7,5 @@ EXPERIMENTAL { # added in 24.03 rte_arg_parse_corelist; + rte_arg_parse_coremask; };