Sometimes displaying a integer value as hexadecimal encoded style is more expected for human consumption, such as, offload capability and device flag. This patch introduces some APIs to add 'u32' and 'u64' value as hexadecimal encoded string to array or dictionary.
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 | 72 ++++++++++++++++++++++++++++++++++ lib/telemetry/telemetry_data.c | 46 ++++++++++++++++++++++ lib/telemetry/version.map | 5 +++ 3 files changed, 123 insertions(+) diff --git a/lib/telemetry/rte_telemetry.h b/lib/telemetry/rte_telemetry.h index 5efdafc156..dac87050da 100644 --- a/lib/telemetry/rte_telemetry.h +++ b/lib/telemetry/rte_telemetry.h @@ -170,6 +170,38 @@ int rte_tel_data_add_array_container(struct rte_tel_data *d, struct rte_tel_data *val, int keep); +/** + * Convert a uint32_t 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 x + * The number to be returned in the array as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_array_hex_u32_str(struct rte_tel_data *d, uint32_t x); + +/** + * Convert a uint64_t 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 x + * The number to be returned in the array as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_array_hex_u64_str(struct rte_tel_data *d, uint64_t x); + /** * Add a string value to a dictionary. * The dict must have been started by rte_tel_data_start_dict(). @@ -266,6 +298,46 @@ int rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, struct rte_tel_data *val, int keep); +/** + * Convert a uint32_t to hexadecimal encoded strings and add this string + * to a 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 the value is to be stored under 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 + * @return + * 0 on success, negative errno on error, E2BIG on string truncation of + * either name or value. + */ +__rte_experimental +int rte_tel_data_add_dict_hex_u32_str(struct rte_tel_data *d, + const char *name, uint32_t val); + +/** + * Convert a uint64_t to hexadecimal encoded strings and add this string + * to a 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 the value is to be stored under 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 + * @return + * 0 on success, negative errno on error, E2BIG on string truncation of + * either name or value. + */ +__rte_experimental +int rte_tel_data_add_dict_hex_u64_str(struct rte_tel_data *d, + const char *name, uint64_t val); + /** * 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 f8efc37763..b195b79fa4 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" +/* The string length is equal to (sizeof(uint64_t) * 2 + 3) */ +#define RTE_TEL_HEX_UINT_MAX_STRING_LEN 19 + int rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type) { @@ -126,6 +130,26 @@ rte_tel_data_add_array_container(struct rte_tel_data *d, return 0; } +int +rte_tel_data_add_array_hex_u32_str(struct rte_tel_data *d, uint32_t x) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%x", x); + + return rte_tel_data_add_array_string(d, hex_str); +} + +int +rte_tel_data_add_array_hex_u64_str(struct rte_tel_data *d, uint64_t x) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%"PRIx64"", x); + + return rte_tel_data_add_array_string(d, hex_str); +} + static bool valid_name(const char *name) { @@ -254,6 +278,28 @@ 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_hex_u32_str(struct rte_tel_data *d, const char *name, + uint32_t val) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%x", val); + + return rte_tel_data_add_dict_string(d, name, hex_str); +} + +int +rte_tel_data_add_dict_hex_u64_str(struct rte_tel_data *d, const char *name, + uint64_t val) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%"PRIx64"", val); + + 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 3d1fb15637..994414c47f 100644 --- a/lib/telemetry/version.map +++ b/lib/telemetry/version.map @@ -3,6 +3,11 @@ EXPERIMENTAL { rte_tel_data_add_array_u32; rte_tel_data_add_dict_u32; + rte_tel_data_add_array_hex_u32_str; + rte_tel_data_add_array_hex_u64_str; + rte_tel_data_add_dict_hex_u32_str; + rte_tel_data_add_dict_hex_u64_str; + local: *; }; -- 2.33.0