xref: /dpdk/doc/guides/prog_guide/telemetry_lib.rst (revision 98ffdfbcf1ea8f4705fa3dacd6d9ae94d3705733)
124cd1b52SCiara Power..  SPDX-License-Identifier: BSD-3-Clause
224cd1b52SCiara Power    Copyright(c) 2020 Intel Corporation.
324cd1b52SCiara Power
424cd1b52SCiara PowerTelemetry Library
524cd1b52SCiara Power=================
624cd1b52SCiara Power
724cd1b52SCiara PowerThe Telemetry library provides an interface to retrieve information from a
824cd1b52SCiara Powervariety of DPDK libraries. The library provides this information via socket
924cd1b52SCiara Powerconnection, taking requests from a connected client and replying with the JSON
1024cd1b52SCiara Powerresponse containing the requested telemetry information.
1124cd1b52SCiara Power
1224cd1b52SCiara PowerTelemetry is enabled to run by default when running a DPDK application, and the
1324cd1b52SCiara Powertelemetry information from enabled libraries is made available. Libraries are
1424cd1b52SCiara Powerresponsible for registering their own commands, and providing the callback
1524cd1b52SCiara Powerfunction that will format the library specific stats into the correct data
1624cd1b52SCiara Powerformat, when requested.
1724cd1b52SCiara Power
1824cd1b52SCiara Power
19*98ffdfbcSCiara PowerCreating Callback Functions
20*98ffdfbcSCiara Power---------------------------
21*98ffdfbcSCiara Power
22*98ffdfbcSCiara Power
23*98ffdfbcSCiara PowerFunction Type
24*98ffdfbcSCiara Power~~~~~~~~~~~~~
25*98ffdfbcSCiara Power
26*98ffdfbcSCiara PowerWhen creating a callback function in a library/app, it must be of the following type:
27*98ffdfbcSCiara Power
28*98ffdfbcSCiara Power.. code-block:: c
29*98ffdfbcSCiara Power
30*98ffdfbcSCiara Power   typedef int (*telemetry_cb)(const char *cmd, const char *params,
31*98ffdfbcSCiara Power           struct rte_tel_data *info);
32*98ffdfbcSCiara Power
33*98ffdfbcSCiara PowerAn example callback function is shown below:
34*98ffdfbcSCiara Power
35*98ffdfbcSCiara Power.. code-block:: c
36*98ffdfbcSCiara Power
37*98ffdfbcSCiara Power   static int
38*98ffdfbcSCiara Power   handle_example_cmd(const char *cmd __rte_unused, const char *params __rte_unused,
39*98ffdfbcSCiara Power           struct rte_tel_data *d)
40*98ffdfbcSCiara Power
41*98ffdfbcSCiara PowerFor more detail on the callback function parameters, please refer to the
42*98ffdfbcSCiara Power`definition in the API doc
43*98ffdfbcSCiara Power<https://doc.dpdk.org/api/rte__telemetry_8h.html#a41dc74d561442bb6184ee6dd1f9b5bcc>`_
44*98ffdfbcSCiara Power
45*98ffdfbcSCiara Power**Example Callback**
46*98ffdfbcSCiara Power
47*98ffdfbcSCiara PowerThis callback is an example of handling multiple commands in one callback,
48*98ffdfbcSCiara Powerand also shows the use of params which holds a port ID. The ``params`` input needs
49*98ffdfbcSCiara Powerto be validated and converted to the required integer type for port ID. The ``cmd``
50*98ffdfbcSCiara Powerparameter is then used in a comparison to decide which command was requested,
51*98ffdfbcSCiara Powerwhich will decide what port information should fill the ``rte_tel_data`` structure.
52*98ffdfbcSCiara Power
53*98ffdfbcSCiara Power.. code-block:: c
54*98ffdfbcSCiara Power
55*98ffdfbcSCiara Power   int
56*98ffdfbcSCiara Power   handle_cmd_request(const char *cmd, const char *params,
57*98ffdfbcSCiara Power         struct rte_tel_data *d)
58*98ffdfbcSCiara Power   {
59*98ffdfbcSCiara Power      int port_id, used = 0;
60*98ffdfbcSCiara Power
61*98ffdfbcSCiara Power      if (params == NULL || strlen(params) == 0 || !isdigit(*params))
62*98ffdfbcSCiara Power         return -1;
63*98ffdfbcSCiara Power
64*98ffdfbcSCiara Power      port_id = atoi(params);
65*98ffdfbcSCiara Power      if (!rte_eth_dev_is_valid_port(port_id))
66*98ffdfbcSCiara Power         return -1;
67*98ffdfbcSCiara Power
68*98ffdfbcSCiara Power      if (strcmp(cmd, "/cmd_1") == 0)
69*98ffdfbcSCiara Power         /* Build up port data requested for command 1 */
70*98ffdfbcSCiara Power      else
71*98ffdfbcSCiara Power         /* Build up port data requested for command 2 */
72*98ffdfbcSCiara Power
73*98ffdfbcSCiara Power       return used;
74*98ffdfbcSCiara Power   }
75*98ffdfbcSCiara Power
76*98ffdfbcSCiara Power
77*98ffdfbcSCiara PowerFormatting Data
78*98ffdfbcSCiara Power~~~~~~~~~~~~~~~
79*98ffdfbcSCiara Power
80*98ffdfbcSCiara PowerThe callback function provided by the library must format its telemetry
81*98ffdfbcSCiara Powerinformation in the required data format. The Telemetry library provides a data
82*98ffdfbcSCiara Powerutilities API to build up the data structure with the required information.
83*98ffdfbcSCiara PowerThe telemetry library is then responsible for formatting the data structure
84*98ffdfbcSCiara Powerinto a JSON response before sending to the client.
85*98ffdfbcSCiara Power
86*98ffdfbcSCiara Power
87*98ffdfbcSCiara PowerArray Data
88*98ffdfbcSCiara Power^^^^^^^^^^
89*98ffdfbcSCiara Power
90*98ffdfbcSCiara PowerSome data will need to be formatted in a list structure. For example, if a
91*98ffdfbcSCiara Powercallback needs to return five integer values in the data response, it can be
92*98ffdfbcSCiara Powerconstructed using the following functions to build up the list:
93*98ffdfbcSCiara Power
94*98ffdfbcSCiara Power.. code-block:: c
95*98ffdfbcSCiara Power
96*98ffdfbcSCiara Power   rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
97*98ffdfbcSCiara Power       for(i = 0; i < 5; i++)
98*98ffdfbcSCiara Power           rte_tel_data_add_array_int(d, i);
99*98ffdfbcSCiara Power
100*98ffdfbcSCiara PowerThe resulting response to the client shows the list data provided above
101*98ffdfbcSCiara Powerby the handler function in the library/app, placed in a JSON reply by telemetry::
102*98ffdfbcSCiara Power
103*98ffdfbcSCiara Power    {"/example_lib/five_ints": [0, 1, 2, 3, 4]}
104*98ffdfbcSCiara Power
105*98ffdfbcSCiara Power
106*98ffdfbcSCiara PowerDictionary Data
107*98ffdfbcSCiara Power^^^^^^^^^^^^^^^
108*98ffdfbcSCiara Power
109*98ffdfbcSCiara PowerFor data that needs to be structured in a dictionary with key/value pairs,
110*98ffdfbcSCiara Powerthe data utilities API can also be used. For example, some information about
111*98ffdfbcSCiara Powera brownie recipe is constructed in the callback function shown below:
112*98ffdfbcSCiara Power
113*98ffdfbcSCiara Power.. code-block:: c
114*98ffdfbcSCiara Power
115*98ffdfbcSCiara Power   rte_tel_data_start_dict(d);
116*98ffdfbcSCiara Power   rte_tel_data_add_dict_string(d, "Recipe", "Brownies");
117*98ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Prep time (mins)", 25);
118*98ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Cooking time (mins)", 30);
119*98ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Serves", 16);
120*98ffdfbcSCiara Power
121*98ffdfbcSCiara PowerThe resulting response to the client shows the key/value data provided above
122*98ffdfbcSCiara Powerby the handler function in telemetry, placed in a JSON reply by telemetry::
123*98ffdfbcSCiara Power
124*98ffdfbcSCiara Power    {"/example_lib/brownie_recipe": {"Recipe": "Brownies", "Prep time (mins)": 25,
125*98ffdfbcSCiara Power      "Cooking time (mins)": 30, "Serves": 16}}
126*98ffdfbcSCiara Power
127*98ffdfbcSCiara Power
128*98ffdfbcSCiara PowerString Data
129*98ffdfbcSCiara Power^^^^^^^^^^^
130*98ffdfbcSCiara Power
131*98ffdfbcSCiara PowerTelemetry also supports single string data.
132*98ffdfbcSCiara PowerThe data utilities API can again be used for this, see the example below.
133*98ffdfbcSCiara Power
134*98ffdfbcSCiara Power.. code-block:: c
135*98ffdfbcSCiara Power
136*98ffdfbcSCiara Power   rte_tel_data_string(d, "This is an example string");
137*98ffdfbcSCiara Power
138*98ffdfbcSCiara PowerGiving the following response to the client::
139*98ffdfbcSCiara Power
140*98ffdfbcSCiara Power    {"/example_lib/string_example": "This is an example string"}
141*98ffdfbcSCiara Power
142*98ffdfbcSCiara PowerFor more information on the range of data functions available in the API,
143*98ffdfbcSCiara Powerplease refer to the `API doc <https://doc.dpdk.org/api-20.05/rte__telemetry_8h.html>`_
144*98ffdfbcSCiara Power
145*98ffdfbcSCiara Power
14624cd1b52SCiara PowerRegistering Commands
14724cd1b52SCiara Power--------------------
14824cd1b52SCiara Power
14924cd1b52SCiara PowerLibraries and applications must register commands to make their information
15024cd1b52SCiara Poweravailable via the Telemetry library. This involves providing a string command
1511474a341SCiara Powerin the required format ("/library/command"), the callback function that
1521474a341SCiara Powerwill handle formatting the information when required, and help text for the
153*98ffdfbcSCiara Powercommand. An example command being registered is shown below:
15424cd1b52SCiara Power
15524cd1b52SCiara Power.. code-block:: c
15624cd1b52SCiara Power
157*98ffdfbcSCiara Power    rte_telemetry_register_cmd("/example_lib/string_example", handle_string,
158*98ffdfbcSCiara Power            "Returns an example string. Takes no parameters");
15924cd1b52SCiara Power
16024cd1b52SCiara Power
161*98ffdfbcSCiara PowerUsing Commands
162*98ffdfbcSCiara Power--------------
16324cd1b52SCiara Power
164*98ffdfbcSCiara PowerTo use commands, with a DPDK app running (e.g. testpmd), use the
165*98ffdfbcSCiara Power``dpdk-telemetry.py`` script.
166*98ffdfbcSCiara PowerFor details on its use, see the :doc:`../howto/telemetry`.
167