1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
6 #include <rte_string_fns.h>
8 #include "telemetry_data.h"
11 rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type)
13 enum tel_container_types array_types[] = {
14 RTE_TEL_ARRAY_STRING, /* RTE_TEL_STRING_VAL = 0 */
15 RTE_TEL_ARRAY_INT, /* RTE_TEL_INT_VAL = 1 */
16 RTE_TEL_ARRAY_U64, /* RTE_TEL_u64_VAL = 2 */
17 RTE_TEL_ARRAY_CONTAINER, /* RTE_TEL_CONTAINER = 3 */
19 d->type = array_types[type];
25 rte_tel_data_start_dict(struct rte_tel_data *d)
27 d->type = RTE_TEL_DICT;
33 rte_tel_data_string(struct rte_tel_data *d, const char *str)
35 d->type = RTE_TEL_STRING;
36 d->data_len = strlcpy(d->data.str, str, sizeof(d->data.str));
37 if (d->data_len >= RTE_TEL_MAX_SINGLE_STRING_LEN) {
38 d->data_len = RTE_TEL_MAX_SINGLE_STRING_LEN - 1;
39 return E2BIG; /* not necessarily and error, just truncation */
45 rte_tel_data_add_array_string(struct rte_tel_data *d, const char *str)
47 if (d->type != RTE_TEL_ARRAY_STRING)
49 if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
51 const size_t bytes = strlcpy(d->data.array[d->data_len++].sval,
52 str, RTE_TEL_MAX_STRING_LEN);
53 return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
57 rte_tel_data_add_array_int(struct rte_tel_data *d, int x)
59 if (d->type != RTE_TEL_ARRAY_INT)
61 if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
63 d->data.array[d->data_len++].ival = x;
68 rte_tel_data_add_array_u64(struct rte_tel_data *d, uint64_t x)
70 if (d->type != RTE_TEL_ARRAY_U64)
72 if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
74 d->data.array[d->data_len++].u64val = x;
79 rte_tel_data_add_array_container(struct rte_tel_data *d,
80 struct rte_tel_data *val, int keep)
82 if (d->type != RTE_TEL_ARRAY_CONTAINER ||
83 (val->type != RTE_TEL_ARRAY_U64
84 && val->type != RTE_TEL_ARRAY_INT
85 && val->type != RTE_TEL_ARRAY_STRING))
87 if (d->data_len >= RTE_TEL_MAX_ARRAY_ENTRIES)
90 d->data.array[d->data_len].container.data = val;
91 d->data.array[d->data_len++].container.keep = !!keep;
96 rte_tel_data_add_dict_string(struct rte_tel_data *d, const char *name,
99 struct tel_dict_entry *e = &d->data.dict[d->data_len];
100 size_t nbytes, vbytes;
102 if (d->type != RTE_TEL_DICT)
104 if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
108 e->type = RTE_TEL_STRING_VAL;
109 vbytes = strlcpy(e->value.sval, val, RTE_TEL_MAX_STRING_LEN);
110 nbytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
111 if (vbytes >= RTE_TEL_MAX_STRING_LEN ||
112 nbytes >= RTE_TEL_MAX_STRING_LEN)
118 rte_tel_data_add_dict_int(struct rte_tel_data *d, const char *name, int val)
120 struct tel_dict_entry *e = &d->data.dict[d->data_len];
121 if (d->type != RTE_TEL_DICT)
123 if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
127 e->type = RTE_TEL_INT_VAL;
129 const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
130 return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
134 rte_tel_data_add_dict_u64(struct rte_tel_data *d,
135 const char *name, uint64_t val)
137 struct tel_dict_entry *e = &d->data.dict[d->data_len];
138 if (d->type != RTE_TEL_DICT)
140 if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
144 e->type = RTE_TEL_U64_VAL;
145 e->value.u64val = val;
146 const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
147 return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
151 rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name,
152 struct rte_tel_data *val, int keep)
154 struct tel_dict_entry *e = &d->data.dict[d->data_len];
156 if (d->type != RTE_TEL_DICT || (val->type != RTE_TEL_ARRAY_U64
157 && val->type != RTE_TEL_ARRAY_INT
158 && val->type != RTE_TEL_ARRAY_STRING))
160 if (d->data_len >= RTE_TEL_MAX_DICT_ENTRIES)
164 e->type = RTE_TEL_CONTAINER;
165 e->value.container.data = val;
166 e->value.container.keep = !!keep;
167 const size_t bytes = strlcpy(e->name, name, RTE_TEL_MAX_STRING_LEN);
168 return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG;
171 struct rte_tel_data *
172 rte_tel_data_alloc(void)
174 return malloc(sizeof(struct rte_tel_data));
178 rte_tel_data_free(struct rte_tel_data *data)