common/cnxk: add SSO HWS interface
[dpdk.git] / lib / librte_telemetry / rte_telemetry.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <sched.h>
7
8 #include <rte_compat.h>
9
10 #ifndef _RTE_TELEMETRY_H_
11 #define _RTE_TELEMETRY_H_
12
13 /** Maximum number of telemetry callbacks. */
14 #define TELEMETRY_MAX_CALLBACKS 64
15 /** Maximum length for string used in object. */
16 #define RTE_TEL_MAX_STRING_LEN 64
17 /** Maximum length of string. */
18 #define RTE_TEL_MAX_SINGLE_STRING_LEN 8192
19 /** Maximum number of dictionary entries. */
20 #define RTE_TEL_MAX_DICT_ENTRIES 256
21 /** Maximum number of array entries. */
22 #define RTE_TEL_MAX_ARRAY_ENTRIES 512
23
24 /**
25  * @file
26  *
27  * RTE Telemetry.
28  *
29  * @warning
30  * @b EXPERIMENTAL:
31  * All functions in this file may be changed or removed without prior notice.
32  *
33  * The telemetry library provides a method to retrieve statistics from
34  * DPDK by sending a request message over a socket. DPDK will send
35  * a JSON encoded response containing telemetry data.
36  ***/
37
38 /** opaque structure used internally for managing data from callbacks */
39 struct rte_tel_data;
40
41 /**
42  * The types of data that can be managed in arrays or dicts.
43  * For arrays, this must be specified at creation time, while for
44  * dicts this is specified implicitly each time an element is added
45  * via calling a type-specific function.
46  */
47 enum rte_tel_value_type {
48         RTE_TEL_STRING_VAL, /** a string value */
49         RTE_TEL_INT_VAL,    /** a signed 32-bit int value */
50         RTE_TEL_U64_VAL,    /** an unsigned 64-bit int value */
51         RTE_TEL_CONTAINER, /** a container struct */
52 };
53
54 /**
55  * Start an array of the specified type for returning from a callback
56  *
57  * @param d
58  *   The data structure passed to the callback
59  * @param type
60  *   The type of the array of data
61  * @return
62  *   0 on success, negative errno on error
63  */
64 __rte_experimental
65 int
66 rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type);
67
68 /**
69  * Start a dictionary of values for returning from a callback
70  *
71  * @param d
72  *   The data structure passed to the callback
73  * @return
74  *   0 on success, negative errno on error
75  */
76 __rte_experimental
77 int
78 rte_tel_data_start_dict(struct rte_tel_data *d);
79
80 /**
81  * Set a string for returning from a callback
82  *
83  * @param d
84  *   The data structure passed to the callback
85  * @param str
86  *   The string to be returned in the data structure
87  * @return
88  *   0 on success, negative errno on error, E2BIG on string truncation
89  */
90 __rte_experimental
91 int
92 rte_tel_data_string(struct rte_tel_data *d, const char *str);
93
94 /**
95  * Add a string to an array.
96  * The array must have been started by rte_tel_data_start_array() with
97  * RTE_TEL_STRING_VAL as the type parameter.
98  *
99  * @param d
100  *   The data structure passed to the callback
101  * @param str
102  *   The string to be returned in the array
103  * @return
104  *   0 on success, negative errno on error, E2BIG on string truncation
105  */
106 __rte_experimental
107 int
108 rte_tel_data_add_array_string(struct rte_tel_data *d, const char *str);
109
110 /**
111  * Add an int to an array.
112  * The array must have been started by rte_tel_data_start_array() with
113  * RTE_TEL_INT_VAL as the type parameter.
114  *
115  * @param d
116  *   The data structure passed to the callback
117  * @param x
118  *   The number to be returned in the array
119  * @return
120  *   0 on success, negative errno on error
121  */
122 __rte_experimental
123 int
124 rte_tel_data_add_array_int(struct rte_tel_data *d, int x);
125
126 /**
127  * Add a uint64_t to an array.
128  * The array must have been started by rte_tel_data_start_array() with
129  * RTE_TEL_U64_VAL as the type parameter.
130  *
131  * @param d
132  *   The data structure passed to the callback
133  * @param x
134  *   The number to be returned in the array
135  * @return
136  *   0 on success, negative errno on error
137  */
138 __rte_experimental
139 int
140 rte_tel_data_add_array_u64(struct rte_tel_data *d, uint64_t x);
141
142 /**
143  * Add a container to an array. A container is an existing telemetry data
144  * array. The array the container is to be added to must have been started by
145  * rte_tel_data_start_array() with RTE_TEL_CONTAINER as the type parameter.
146  * The container type must be an array of type uint64_t/int/string.
147  *
148  * @param d
149  *   The data structure passed to the callback
150  * @param val
151  *   The pointer to the container to be stored in the array.
152  * @param keep
153  *   Flag to indicate that the container memory should not be automatically
154  *   freed by the telemetry library once it has finished with the data.
155  *   1 = keep, 0 = free.
156  * @return
157  *   0 on success, negative errno on error
158  */
159 __rte_experimental
160 int
161 rte_tel_data_add_array_container(struct rte_tel_data *d,
162                 struct rte_tel_data *val, int keep);
163
164 /**
165  * Add a string value to a dictionary.
166  * The dict must have been started by rte_tel_data_start_dict().
167  *
168  * @param d
169  *   The data structure passed to the callback
170  * @param name
171  *   The name the value is to be stored under in the dict
172  * @param val
173  *   The string to be stored in the dict
174  * @return
175  *   0 on success, negative errno on error, E2BIG on string truncation of
176  *   either name or value.
177  */
178 __rte_experimental
179 int
180 rte_tel_data_add_dict_string(struct rte_tel_data *d, const char *name,
181                 const char *val);
182
183 /**
184  * Add an int value to a dictionary.
185  * The dict must have been started by rte_tel_data_start_dict().
186  *
187  * @param d
188  *   The data structure passed to the callback
189  * @param name
190  *   The name the value is to be stored under in the dict
191  * @param val
192  *   The number to be stored in the dict
193  * @return
194  *   0 on success, negative errno on error, E2BIG on string truncation of name.
195  */
196 __rte_experimental
197 int
198 rte_tel_data_add_dict_int(struct rte_tel_data *d, const char *name, int val);
199
200 /**
201  * Add a uint64_t value to a dictionary.
202  * The dict must have been started by rte_tel_data_start_dict().
203  *
204  * @param d
205  *   The data structure passed to the callback
206  * @param name
207  *   The name the value is to be stored under in the dict
208  * @param val
209  *   The number to be stored in the dict
210  * @return
211  *   0 on success, negative errno on error, E2BIG on string truncation of name.
212  */
213 __rte_experimental
214 int
215 rte_tel_data_add_dict_u64(struct rte_tel_data *d,
216                 const char *name, uint64_t val);
217
218 /**
219  * Add a container to a dictionary. A container is an existing telemetry data
220  * array. The dict the container is to be added to must have been started by
221  * rte_tel_data_start_dict(). The container must be an array of type
222  * uint64_t/int/string.
223  *
224  * @param d
225  *   The data structure passed to the callback
226  * @param name
227  *   The name the value is to be stored under in the dict.
228  * @param val
229  *   The pointer to the container to be stored in the dict.
230  * @param keep
231  *   Flag to indicate that the container memory should not be automatically
232  *   freed by the telemetry library once it has finished with the data.
233  *   1 = keep, 0 = free.
234  * @return
235  *   0 on success, negative errno on error
236  */
237 __rte_experimental
238 int
239 rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name,
240                 struct rte_tel_data *val, int keep);
241
242 /**
243  * This telemetry callback is used when registering a telemetry command.
244  * It handles getting and formatting information to be returned to telemetry
245  * when requested.
246  *
247  * @param cmd
248  * The cmd that was requested by the client.
249  * @param params
250  * Contains data required by the callback function.
251  * @param info
252  * The information to be returned to the caller.
253  *
254  * @return
255  * Length of buffer used on success.
256  * @return
257  * Negative integer on error.
258  */
259 typedef int (*telemetry_cb)(const char *cmd, const char *params,
260                 struct rte_tel_data *info);
261
262 /**
263  * Used for handling data received over a telemetry socket.
264  *
265  * @param sock_id
266  * ID for the socket to be used by the handler.
267  *
268  * @return
269  * Void.
270  */
271 typedef void * (*handler)(void *sock_id);
272
273 /**
274  * Used when registering a command and callback function with telemetry.
275  *
276  * @param cmd
277  * The command to register with telemetry.
278  * @param fn
279  * Callback function to be called when the command is requested.
280  * @param help
281  * Help text for the command.
282  *
283  * @return
284  *  0 on success.
285  * @return
286  *  -EINVAL for invalid parameters failure.
287  *  @return
288  *  -ENOENT if max callbacks limit has been reached.
289  */
290 __rte_experimental
291 int
292 rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn, const char *help);
293
294
295 /**
296  * Get a pointer to a container with memory allocated. The container is to be
297  * used embedded within an existing telemetry dict/array.
298  *
299  * @return
300  *  Pointer to a container.
301  */
302 __rte_experimental
303 struct rte_tel_data *
304 rte_tel_data_alloc(void);
305
306 /**
307  * @internal
308  * Free a container that has memory allocated.
309  *
310  * @param data
311  *  Pointer to container.
312  *.
313  */
314 __rte_experimental
315 void
316 rte_tel_data_free(struct rte_tel_data *data);
317
318 #endif