xref: /dpdk/lib/telemetry/telemetry_data.c (revision d2671e64)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2020 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #undef RTE_USE_LIBBSD
699a2dd95SBruce Richardson #include <rte_string_fns.h>
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include "telemetry_data.h"
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson int
rte_tel_data_start_array(struct rte_tel_data * d,enum rte_tel_value_type type)1199a2dd95SBruce Richardson rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type)
1299a2dd95SBruce Richardson {
1399a2dd95SBruce Richardson 	enum tel_container_types array_types[] = {
1499a2dd95SBruce Richardson 			RTE_TEL_ARRAY_STRING, /* RTE_TEL_STRING_VAL = 0 */
1599a2dd95SBruce Richardson 			RTE_TEL_ARRAY_INT,    /* RTE_TEL_INT_VAL = 1 */
1699a2dd95SBruce Richardson 			RTE_TEL_ARRAY_U64,    /* RTE_TEL_u64_VAL = 2 */
1799a2dd95SBruce Richardson 			RTE_TEL_ARRAY_CONTAINER, /* RTE_TEL_CONTAINER = 3 */
1899a2dd95SBruce Richardson 	};
1999a2dd95SBruce Richardson 	d->type = array_types[type];
2099a2dd95SBruce Richardson 	d->data_len = 0;
2199a2dd95SBruce Richardson 	return 0;
2299a2dd95SBruce Richardson }
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson int
rte_tel_data_start_dict(struct rte_tel_data * d)2599a2dd95SBruce Richardson rte_tel_data_start_dict(struct rte_tel_data *d)
2699a2dd95SBruce Richardson {
2799a2dd95SBruce Richardson 	d->type = RTE_TEL_DICT;
2899a2dd95SBruce Richardson 	d->data_len = 0;
2999a2dd95SBruce Richardson 	return 0;
3099a2dd95SBruce Richardson }
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson int
rte_tel_data_string(struct rte_tel_data * d,const char * str)3399a2dd95SBruce Richardson rte_tel_data_string(struct rte_tel_data *d, const char *str)
3499a2dd95SBruce Richardson {
3599a2dd95SBruce Richardson 	d->type = RTE_TEL_STRING;
3699a2dd95SBruce Richardson 	d->data_len = strlcpy(d->data.str, str, sizeof(d->data.str));
3799a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
3899a2dd95SBruce Richardson 		d->data_len = RTE_TEL_MAX_SINGLE_STRING_LEN - 1;
3999a2dd95SBruce Richardson 		return E2BIG; /* not necessarily and error, just truncation */
4099a2dd95SBruce Richardson 	}
4199a2dd95SBruce Richardson 	return 0;
4299a2dd95SBruce Richardson }
4399a2dd95SBruce Richardson 
4499a2dd95SBruce Richardson int
rte_tel_data_add_array_string(struct rte_tel_data * d,const char * str)4599a2dd95SBruce Richardson rte_tel_data_add_array_string(struct rte_tel_data *d, const char *str)
4699a2dd95SBruce Richardson {
4799a2dd95SBruce Richardson 	if (d->type != RTE_TEL_ARRAY_STRING)
4899a2dd95SBruce Richardson 		return -EINVAL;
4999a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
5099a2dd95SBruce Richardson 		return -ENOSPC;
5199a2dd95SBruce Richardson 	const size_t bytes = strlcpy(d->data.array[d->data_len++].sval,
5299a2dd95SBruce Richardson 			str, RTE_TEL_MAX_STRING_LEN);
5399a2dd95SBruce Richardson 	return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
5499a2dd95SBruce Richardson }
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson int
rte_tel_data_add_array_int(struct rte_tel_data * d,int x)5799a2dd95SBruce Richardson rte_tel_data_add_array_int(struct rte_tel_data *d, int x)
5899a2dd95SBruce Richardson {
5999a2dd95SBruce Richardson 	if (d->type != RTE_TEL_ARRAY_INT)
6099a2dd95SBruce Richardson 		return -EINVAL;
6199a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
6299a2dd95SBruce Richardson 		return -ENOSPC;
6399a2dd95SBruce Richardson 	d->data.array[d->data_len++].ival = x;
6499a2dd95SBruce Richardson 	return 0;
6599a2dd95SBruce Richardson }
6699a2dd95SBruce Richardson 
6799a2dd95SBruce Richardson int
rte_tel_data_add_array_u64(struct rte_tel_data * d,uint64_t x)6899a2dd95SBruce Richardson rte_tel_data_add_array_u64(struct rte_tel_data *d, uint64_t x)
6999a2dd95SBruce Richardson {
7099a2dd95SBruce Richardson 	if (d->type != RTE_TEL_ARRAY_U64)
7199a2dd95SBruce Richardson 		return -EINVAL;
7299a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
7399a2dd95SBruce Richardson 		return -ENOSPC;
7499a2dd95SBruce Richardson 	d->data.array[d->data_len++].u64val = x;
7599a2dd95SBruce Richardson 	return 0;
7699a2dd95SBruce Richardson }
7799a2dd95SBruce Richardson 
7899a2dd95SBruce Richardson int
rte_tel_data_add_array_container(struct rte_tel_data * d,struct rte_tel_data * val,int keep)7999a2dd95SBruce Richardson rte_tel_data_add_array_container(struct rte_tel_data *d,
8099a2dd95SBruce Richardson 		struct rte_tel_data *val, int keep)
8199a2dd95SBruce Richardson {
8299a2dd95SBruce Richardson 	if (d->type != RTE_TEL_ARRAY_CONTAINER ||
8399a2dd95SBruce Richardson 			(val->type != RTE_TEL_ARRAY_U64
8499a2dd95SBruce Richardson 			&& val->type != RTE_TEL_ARRAY_INT
8599a2dd95SBruce Richardson 			&& val->type != RTE_TEL_ARRAY_STRING))
8699a2dd95SBruce Richardson 		return -EINVAL;
8799a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
8899a2dd95SBruce Richardson 		return -ENOSPC;
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson 	d->data.array[d->data_len].container.data = val;
9199a2dd95SBruce Richardson 	d->data.array[d->data_len++].container.keep = !!keep;
9299a2dd95SBruce Richardson 	return 0;
9399a2dd95SBruce Richardson }
9499a2dd95SBruce Richardson 
9599a2dd95SBruce Richardson int
rte_tel_data_add_dict_string(struct rte_tel_data * d,const char * name,const char * val)9699a2dd95SBruce Richardson rte_tel_data_add_dict_string(struct rte_tel_data *d, const char *name,
9799a2dd95SBruce Richardson 		const char *val)
9899a2dd95SBruce Richardson {
9999a2dd95SBruce Richardson 	struct tel_dict_entry *e = &d->data.dict[d->data_len];
10099a2dd95SBruce Richardson 	size_t nbytes, vbytes;
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	if (d->type != RTE_TEL_DICT)
10399a2dd95SBruce Richardson 		return -EINVAL;
10499a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
10599a2dd95SBruce Richardson 		return -ENOSPC;
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson 	d->data_len++;
10899a2dd95SBruce Richardson 	e->type = RTE_TEL_STRING_VAL;
10999a2dd95SBruce Richardson 	vbytes = strlcpy(e->value.sval, val, RTE_TEL_MAX_STRING_LEN);
11099a2dd95SBruce Richardson 	nbytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
11199a2dd95SBruce Richardson 	if (vbytes >= RTE_TEL_MAX_STRING_LEN ||
11299a2dd95SBruce Richardson 			nbytes >= RTE_TEL_MAX_STRING_LEN)
11399a2dd95SBruce Richardson 		return E2BIG;
11499a2dd95SBruce Richardson 	return 0;
11599a2dd95SBruce Richardson }
11699a2dd95SBruce Richardson 
11799a2dd95SBruce Richardson int
rte_tel_data_add_dict_int(struct rte_tel_data * d,const char * name,int val)11899a2dd95SBruce Richardson rte_tel_data_add_dict_int(struct rte_tel_data *d, const char *name, int val)
11999a2dd95SBruce Richardson {
12099a2dd95SBruce Richardson 	struct tel_dict_entry *e = &d->data.dict[d->data_len];
12199a2dd95SBruce Richardson 	if (d->type != RTE_TEL_DICT)
12299a2dd95SBruce Richardson 		return -EINVAL;
12399a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
12499a2dd95SBruce Richardson 		return -ENOSPC;
12599a2dd95SBruce Richardson 
12699a2dd95SBruce Richardson 	d->data_len++;
12799a2dd95SBruce Richardson 	e->type = RTE_TEL_INT_VAL;
12899a2dd95SBruce Richardson 	e->value.ival = val;
12999a2dd95SBruce Richardson 	const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
13099a2dd95SBruce Richardson 	return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
13199a2dd95SBruce Richardson }
13299a2dd95SBruce Richardson 
13399a2dd95SBruce Richardson int
rte_tel_data_add_dict_u64(struct rte_tel_data * d,const char * name,uint64_t val)13499a2dd95SBruce Richardson rte_tel_data_add_dict_u64(struct rte_tel_data *d,
13599a2dd95SBruce Richardson 		const char *name, uint64_t val)
13699a2dd95SBruce Richardson {
13799a2dd95SBruce Richardson 	struct tel_dict_entry *e = &d->data.dict[d->data_len];
13899a2dd95SBruce Richardson 	if (d->type != RTE_TEL_DICT)
13999a2dd95SBruce Richardson 		return -EINVAL;
14099a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
14199a2dd95SBruce Richardson 		return -ENOSPC;
14299a2dd95SBruce Richardson 
14399a2dd95SBruce Richardson 	d->data_len++;
14499a2dd95SBruce Richardson 	e->type = RTE_TEL_U64_VAL;
14599a2dd95SBruce Richardson 	e->value.u64val = val;
14699a2dd95SBruce Richardson 	const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
14799a2dd95SBruce Richardson 	return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
14899a2dd95SBruce Richardson }
14999a2dd95SBruce Richardson 
15099a2dd95SBruce Richardson int
rte_tel_data_add_dict_container(struct rte_tel_data * d,const char * name,struct rte_tel_data * val,int keep)15199a2dd95SBruce Richardson rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name,
15299a2dd95SBruce Richardson 		struct rte_tel_data *val, int keep)
15399a2dd95SBruce Richardson {
15499a2dd95SBruce Richardson 	struct tel_dict_entry *e = &d->data.dict[d->data_len];
15599a2dd95SBruce Richardson 
15699a2dd95SBruce Richardson 	if (d->type != RTE_TEL_DICT || (val->type != RTE_TEL_ARRAY_U64
15799a2dd95SBruce Richardson 			&& val->type != RTE_TEL_ARRAY_INT
158*d2671e64SRadu Nicolau 			&& val->type != RTE_TEL_ARRAY_STRING
159*d2671e64SRadu Nicolau 			&& val->type != RTE_TEL_DICT))
16099a2dd95SBruce Richardson 		return -EINVAL;
16199a2dd95SBruce Richardson 	if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
16299a2dd95SBruce Richardson 		return -ENOSPC;
16399a2dd95SBruce Richardson 
16499a2dd95SBruce Richardson 	d->data_len++;
16599a2dd95SBruce Richardson 	e->type = RTE_TEL_CONTAINER;
16699a2dd95SBruce Richardson 	e->value.container.data = val;
16799a2dd95SBruce Richardson 	e->value.container.keep = !!keep;
16899a2dd95SBruce Richardson 	const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
16999a2dd95SBruce Richardson 	return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
17099a2dd95SBruce Richardson }
17199a2dd95SBruce Richardson 
17299a2dd95SBruce Richardson struct rte_tel_data *
rte_tel_data_alloc(void)17399a2dd95SBruce Richardson rte_tel_data_alloc(void)
17499a2dd95SBruce Richardson {
17599a2dd95SBruce Richardson 	return malloc(sizeof(struct rte_tel_data));
17699a2dd95SBruce Richardson }
17799a2dd95SBruce Richardson 
17899a2dd95SBruce Richardson void
rte_tel_data_free(struct rte_tel_data * data)17999a2dd95SBruce Richardson rte_tel_data_free(struct rte_tel_data *data)
18099a2dd95SBruce Richardson {
18199a2dd95SBruce Richardson 	free(data);
18299a2dd95SBruce Richardson }
183