Sometimes displaying a unsigned integer value as hexadecimal encoded style is more expected for human consumption, such as, offload capability and device flag. This patch introduces two APIs to add unsigned integer (can be one of uint8_t, uint16_t, uint32_t and uint64_t type) value as hexadecimal encoded string to array or dictionary. If the 'val_bits' is zero, the value is stored as hexadecimal encoded string without padding zero.
Signed-off-by: Huisong Li <lihuis...@huawei.com> Acked-by: Morten Brørup <m...@smartsharesystems.com> Acked-by: Chengwen Feng <fengcheng...@huawei.com> --- lib/telemetry/rte_telemetry.h | 50 +++++++++++++++++++++++++++++ lib/telemetry/telemetry_data.c | 58 ++++++++++++++++++++++++++++++++++ lib/telemetry/version.map | 9 ++++++ 3 files changed, 117 insertions(+) diff --git a/lib/telemetry/rte_telemetry.h b/lib/telemetry/rte_telemetry.h index 40e9a3bf9d..88b34097b0 100644 --- a/lib/telemetry/rte_telemetry.h +++ b/lib/telemetry/rte_telemetry.h @@ -10,6 +10,7 @@ extern "C" { #endif #include <stdint.h> +#include <rte_compat.h> /** Maximum length for string used in object. */ #define RTE_TEL_MAX_STRING_LEN 128 @@ -20,6 +21,11 @@ extern "C" { /** Maximum number of array entries. */ #define RTE_TEL_MAX_ARRAY_ENTRIES 512 +#define RTE_TEL_U8_BITS 8 +#define RTE_TEL_U16_BITS 16 +#define RTE_TEL_U32_BITS 32 +#define RTE_TEL_U64_BITS 64 + /** * @file * @@ -153,6 +159,27 @@ int rte_tel_data_add_array_container(struct rte_tel_data *d, struct rte_tel_data *val, int keep); +/** + * Convert a unsigned integer to hexadecimal encoded strings and add this string + * to an array. + * The array must have been started by rte_tel_data_start_array() with + * RTE_TEL_STRING_VAL as the type parameter. + * + * @param d + * The data structure passed to the callback + * @param val + * The number to be returned in the array as a hexadecimal encoded strings. + * The type of ''val' can be one of uint8_t, uint16_t, uint32_t and uint64_t. + * @param val_bits + * The total bits of the input 'val'. If val_bits is zero, the value is stored + * in the array as hexadecimal encoded string without padding zero. + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_array_uint_hex(struct rte_tel_data *d, uint64_t val, + uint16_t val_bits); + /** * Add a string value to a dictionary. * The dict must have been started by rte_tel_data_start_dict(). @@ -231,6 +258,29 @@ int rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, struct rte_tel_data *val, int keep); +/** + * Convert a unsigned integer to hexadecimal encoded strings and add this string + * to an dictionary. + * The dict must have been started by rte_tel_data_start_dict(). + * + * @param d + * The data structure passed to the callback + * @param name + * The name of the value is to be stored in the dict + * Must contain only alphanumeric characters or the symbols: '_' or '/' + * @param val + * The number to be stored in the dict as a hexadecimal encoded strings. + * The type of ''val' can be one of uint8_t, uint16_t, uint32_t and uint64_t. + * @param val_bits + * The total bits of the input 'val'. If val_bits is zero, the value is stored + * in the array as hexadecimal encoded string without padding zero. + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_dict_uint_hex(struct rte_tel_data *d, const char *name, + uint64_t val, uint16_t val_bits); + /** * This telemetry callback is used when registering a telemetry command. * It handles getting and formatting information to be returned to telemetry diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c index 080d99aec9..fb2f711d99 100644 --- a/lib/telemetry/telemetry_data.c +++ b/lib/telemetry/telemetry_data.c @@ -4,6 +4,7 @@ #include <errno.h> #include <stdlib.h> +#include <inttypes.h> #undef RTE_USE_LIBBSD #include <stdbool.h> @@ -12,6 +13,9 @@ #include "telemetry_data.h" +#define RTE_TEL_UINT_HEX_STRING_BUFFER_LEN 64 +#define RTE_TEL_UINT_HEX_FORMAT_LEN 16 + int rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type) { @@ -113,6 +117,33 @@ rte_tel_data_add_array_container(struct rte_tel_data *d, return 0; } +int +rte_tel_data_add_array_uint_hex(struct rte_tel_data *d, uint64_t val, + uint16_t val_bits) +{ + char hex_str[RTE_TEL_UINT_HEX_STRING_BUFFER_LEN]; + + switch (val_bits) { + case RTE_TEL_U8_BITS: + sprintf(hex_str, "0x%02"PRIx64"", val); + break; + case RTE_TEL_U16_BITS: + sprintf(hex_str, "0x%04"PRIx64"", val); + break; + case RTE_TEL_U32_BITS: + sprintf(hex_str, "0x%08"PRIx64"", val); + break; + case RTE_TEL_U64_BITS: + sprintf(hex_str, "0x%016"PRIx64"", val); + break; + default: + sprintf(hex_str, "0x%"PRIx64"", val); + break; + } + + return rte_tel_data_add_array_string(d, hex_str); +} + static bool valid_name(const char *name) { @@ -220,6 +251,33 @@ rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG; } +int +rte_tel_data_add_dict_uint_hex(struct rte_tel_data *d, const char *name, + uint64_t val, uint16_t val_bits) +{ + char hex_str[RTE_TEL_UINT_HEX_STRING_BUFFER_LEN]; + + switch (val_bits) { + case RTE_TEL_U8_BITS: + sprintf(hex_str, "0x%02"PRIx64"", val); + break; + case RTE_TEL_U16_BITS: + sprintf(hex_str, "0x%04"PRIx64"", val); + break; + case RTE_TEL_U32_BITS: + sprintf(hex_str, "0x%08"PRIx64"", val); + break; + case RTE_TEL_U64_BITS: + sprintf(hex_str, "0x%016"PRIx64"", val); + break; + default: + sprintf(hex_str, "0x%"PRIx64"", val); + break; + } + + return rte_tel_data_add_dict_string(d, name, hex_str); +} + struct rte_tel_data * rte_tel_data_alloc(void) { diff --git a/lib/telemetry/version.map b/lib/telemetry/version.map index 9794f9ea20..951bd63974 100644 --- a/lib/telemetry/version.map +++ b/lib/telemetry/version.map @@ -1,3 +1,12 @@ +EXPERIMENTAL { + global: + + rte_tel_data_add_array_uint_hex; + rte_tel_data_add_dict_uint_hex; + + local: *; +}; + DPDK_23 { global: -- 2.33.0