xref: /dpdk/doc/guides/prog_guide/telemetry_lib.rst (revision 684ba6265dd24a45e0d797b26a834e8c7bb76716)
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
1998ffdfbcSCiara PowerCreating Callback Functions
2098ffdfbcSCiara Power---------------------------
2198ffdfbcSCiara Power
2298ffdfbcSCiara Power
2398ffdfbcSCiara PowerFunction Type
2498ffdfbcSCiara Power~~~~~~~~~~~~~
2598ffdfbcSCiara Power
2698ffdfbcSCiara PowerWhen creating a callback function in a library/app, it must be of the following type:
2798ffdfbcSCiara Power
2898ffdfbcSCiara Power.. code-block:: c
2998ffdfbcSCiara Power
3098ffdfbcSCiara Power   typedef int (*telemetry_cb)(const char *cmd, const char *params,
3198ffdfbcSCiara Power           struct rte_tel_data *info);
3298ffdfbcSCiara Power
3398ffdfbcSCiara PowerAn example callback function is shown below:
3498ffdfbcSCiara Power
3598ffdfbcSCiara Power.. code-block:: c
3698ffdfbcSCiara Power
3798ffdfbcSCiara Power   static int
3898ffdfbcSCiara Power   handle_example_cmd(const char *cmd __rte_unused, const char *params __rte_unused,
3998ffdfbcSCiara Power           struct rte_tel_data *d)
4098ffdfbcSCiara Power
4198ffdfbcSCiara PowerFor more detail on the callback function parameters, please refer to the
4298ffdfbcSCiara Power`definition in the API doc
4398ffdfbcSCiara Power<https://doc.dpdk.org/api/rte__telemetry_8h.html#a41dc74d561442bb6184ee6dd1f9b5bcc>`_
4498ffdfbcSCiara Power
4598ffdfbcSCiara Power**Example Callback**
4698ffdfbcSCiara Power
4798ffdfbcSCiara PowerThis callback is an example of handling multiple commands in one callback,
4898ffdfbcSCiara Powerand also shows the use of params which holds a port ID. The ``params`` input needs
4998ffdfbcSCiara Powerto be validated and converted to the required integer type for port ID. The ``cmd``
5098ffdfbcSCiara Powerparameter is then used in a comparison to decide which command was requested,
5198ffdfbcSCiara Powerwhich will decide what port information should fill the ``rte_tel_data`` structure.
5298ffdfbcSCiara Power
5398ffdfbcSCiara Power.. code-block:: c
5498ffdfbcSCiara Power
5598ffdfbcSCiara Power   int
5698ffdfbcSCiara Power   handle_cmd_request(const char *cmd, const char *params,
5798ffdfbcSCiara Power         struct rte_tel_data *d)
5898ffdfbcSCiara Power   {
5998ffdfbcSCiara Power      int port_id, used = 0;
6098ffdfbcSCiara Power
6198ffdfbcSCiara Power      if (params == NULL || strlen(params) == 0 || !isdigit(*params))
6298ffdfbcSCiara Power         return -1;
6398ffdfbcSCiara Power
6498ffdfbcSCiara Power      port_id = atoi(params);
6598ffdfbcSCiara Power      if (!rte_eth_dev_is_valid_port(port_id))
6698ffdfbcSCiara Power         return -1;
6798ffdfbcSCiara Power
6898ffdfbcSCiara Power      if (strcmp(cmd, "/cmd_1") == 0)
6998ffdfbcSCiara Power         /* Build up port data requested for command 1 */
7098ffdfbcSCiara Power      else
7198ffdfbcSCiara Power         /* Build up port data requested for command 2 */
7298ffdfbcSCiara Power
7398ffdfbcSCiara Power       return used;
7498ffdfbcSCiara Power   }
7598ffdfbcSCiara Power
7698ffdfbcSCiara Power
7798ffdfbcSCiara PowerFormatting Data
7898ffdfbcSCiara Power~~~~~~~~~~~~~~~
7998ffdfbcSCiara Power
8098ffdfbcSCiara PowerThe callback function provided by the library must format its telemetry
8198ffdfbcSCiara Powerinformation in the required data format. The Telemetry library provides a data
8298ffdfbcSCiara Powerutilities API to build up the data structure with the required information.
8398ffdfbcSCiara PowerThe telemetry library is then responsible for formatting the data structure
8498ffdfbcSCiara Powerinto a JSON response before sending to the client.
8598ffdfbcSCiara Power
8698ffdfbcSCiara Power
8798ffdfbcSCiara PowerArray Data
8898ffdfbcSCiara Power^^^^^^^^^^
8998ffdfbcSCiara Power
9098ffdfbcSCiara PowerSome data will need to be formatted in a list structure. For example, if a
9198ffdfbcSCiara Powercallback needs to return five integer values in the data response, it can be
9298ffdfbcSCiara Powerconstructed using the following functions to build up the list:
9398ffdfbcSCiara Power
9498ffdfbcSCiara Power.. code-block:: c
9598ffdfbcSCiara Power
9698ffdfbcSCiara Power   rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
9798ffdfbcSCiara Power       for(i = 0; i < 5; i++)
9898ffdfbcSCiara Power           rte_tel_data_add_array_int(d, i);
9998ffdfbcSCiara Power
10098ffdfbcSCiara PowerThe resulting response to the client shows the list data provided above
10198ffdfbcSCiara Powerby the handler function in the library/app, placed in a JSON reply by telemetry::
10298ffdfbcSCiara Power
10398ffdfbcSCiara Power    {"/example_lib/five_ints": [0, 1, 2, 3, 4]}
10498ffdfbcSCiara Power
10598ffdfbcSCiara Power
10698ffdfbcSCiara PowerDictionary Data
10798ffdfbcSCiara Power^^^^^^^^^^^^^^^
10898ffdfbcSCiara Power
10998ffdfbcSCiara PowerFor data that needs to be structured in a dictionary with key/value pairs,
11098ffdfbcSCiara Powerthe data utilities API can also be used. For example, some information about
11198ffdfbcSCiara Powera brownie recipe is constructed in the callback function shown below:
11298ffdfbcSCiara Power
11398ffdfbcSCiara Power.. code-block:: c
11498ffdfbcSCiara Power
11598ffdfbcSCiara Power   rte_tel_data_start_dict(d);
11698ffdfbcSCiara Power   rte_tel_data_add_dict_string(d, "Recipe", "Brownies");
11798ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Prep time (mins)", 25);
11898ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Cooking time (mins)", 30);
11998ffdfbcSCiara Power   rte_tel_data_add_dict_int(d, "Serves", 16);
12098ffdfbcSCiara Power
12198ffdfbcSCiara PowerThe resulting response to the client shows the key/value data provided above
12298ffdfbcSCiara Powerby the handler function in telemetry, placed in a JSON reply by telemetry::
12398ffdfbcSCiara Power
12498ffdfbcSCiara Power    {"/example_lib/brownie_recipe": {"Recipe": "Brownies", "Prep time (mins)": 25,
12598ffdfbcSCiara Power      "Cooking time (mins)": 30, "Serves": 16}}
12698ffdfbcSCiara Power
12798ffdfbcSCiara Power
12898ffdfbcSCiara PowerString Data
12998ffdfbcSCiara Power^^^^^^^^^^^
13098ffdfbcSCiara Power
13198ffdfbcSCiara PowerTelemetry also supports single string data.
13298ffdfbcSCiara PowerThe data utilities API can again be used for this, see the example below.
13398ffdfbcSCiara Power
13498ffdfbcSCiara Power.. code-block:: c
13598ffdfbcSCiara Power
13698ffdfbcSCiara Power   rte_tel_data_string(d, "This is an example string");
13798ffdfbcSCiara Power
13898ffdfbcSCiara PowerGiving the following response to the client::
13998ffdfbcSCiara Power
14098ffdfbcSCiara Power    {"/example_lib/string_example": "This is an example string"}
14198ffdfbcSCiara Power
14298ffdfbcSCiara PowerFor more information on the range of data functions available in the API,
143*684ba626SThomas Monjalonplease refer to the `API doc <https://doc.dpdk.org/api/rte__telemetry_8h.html>`_
14498ffdfbcSCiara Power
14598ffdfbcSCiara 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
15398ffdfbcSCiara Powercommand. An example command being registered is shown below:
15424cd1b52SCiara Power
15524cd1b52SCiara Power.. code-block:: c
15624cd1b52SCiara Power
15798ffdfbcSCiara Power    rte_telemetry_register_cmd("/example_lib/string_example", handle_string,
15898ffdfbcSCiara Power            "Returns an example string. Takes no parameters");
15924cd1b52SCiara Power
16024cd1b52SCiara Power
16198ffdfbcSCiara PowerUsing Commands
16298ffdfbcSCiara Power--------------
16324cd1b52SCiara Power
16498ffdfbcSCiara PowerTo use commands, with a DPDK app running (e.g. testpmd), use the
16598ffdfbcSCiara Power``dpdk-telemetry.py`` script.
16698ffdfbcSCiara PowerFor details on its use, see the :doc:`../howto/telemetry`.
167