1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <sys/socket.h>
14 #include <rte_common.h>
15 #include <rte_tailq.h>
16 #include <rte_string_fns.h>
18 #include "rte_telemetry_parser.h"
32 #define TEST_CLIENT "/var/run/dpdk/test_client"
35 rte_telemetry_create_test_socket(struct telemetry_impl *telemetry,
36 const char *test_client_path)
39 struct sockaddr_un addr = {0};
40 struct telemetry_client *client;
42 if (telemetry == NULL) {
43 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
47 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
49 TELEMETRY_LOG_ERR("Test socket creation failure");
53 addr.sun_family = AF_UNIX;
54 strlcpy(addr.sun_path, test_client_path, sizeof(addr.sun_path));
55 unlink(test_client_path);
57 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
58 TELEMETRY_LOG_ERR("Test socket binding failure");
62 if (listen(sockfd, 1) < 0) {
63 TELEMETRY_LOG_ERR("Listen failure");
67 ret = rte_telemetry_register_client(telemetry, test_client_path);
69 TELEMETRY_LOG_ERR("Register dummy client failed: %i", ret);
73 ret = accept(sockfd, NULL, NULL);
75 TELEMETRY_LOG_ERR("Socket accept failed");
79 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list)
80 telemetry->request_client = client;
86 rte_telemetry_format_port_stat_ids(int *port_ids, int num_port_ids,
87 const char * const *stat_names, int num_stat_names, json_t **data)
91 json_t *stat_names_json_array = NULL;
92 json_t *port_ids_json_array = NULL;
95 if (num_port_ids < 0) {
96 TELEMETRY_LOG_ERR("Port Ids Count invalid");
100 *data = json_object();
102 TELEMETRY_LOG_ERR("Data json object creation failed");
106 port_ids_json_array = json_array();
107 if (port_ids_json_array == NULL) {
108 TELEMETRY_LOG_ERR("port_ids_json_array creation failed");
112 for (i = 0; i < (uint32_t)num_port_ids; i++) {
113 ret = json_array_append(port_ids_json_array,
114 json_integer(port_ids[i]));
116 TELEMETRY_LOG_ERR("JSON array creation failed");
121 ret = json_object_set_new(*data, "ports", port_ids_json_array);
123 TELEMETRY_LOG_ERR("Setting 'ports' value in data object failed");
128 if (num_stat_names < 0) {
129 TELEMETRY_LOG_ERR("Stat Names Count invalid");
133 stat_names_json_array = json_array();
134 if (stat_names_json_array == NULL) {
135 TELEMETRY_LOG_ERR("stat_names_json_array creation failed");
140 for (i = 0; i < (uint32_t)num_stat_names; i++) {
141 ret = json_array_append(stat_names_json_array,
142 json_string(stat_names[i]));
144 TELEMETRY_LOG_ERR("JSON array creation failed");
149 ret = json_object_set_new(*data, "stats", stat_names_json_array);
151 TELEMETRY_LOG_ERR("Setting 'stats' value in data object failed");
161 if (stat_names_json_array)
162 json_decref(stat_names_json_array);
163 if (port_ids_json_array)
164 json_decref(port_ids_json_array);
169 rte_telemetry_create_json_request(int action, char *command,
170 const char *client_path, int *port_ids, int num_port_ids,
171 const char * const *stat_names, int num_stat_names, char **request,
175 json_t *root = json_object();
179 TELEMETRY_LOG_ERR("Could not create root json object");
183 if (inv_choice == INV_ACTION_FIELD) {
184 ret = json_object_set_new(root, "ac--on", json_integer(action));
186 TELEMETRY_LOG_ERR("Setting invalid action field in root object failed");
190 ret = json_object_set_new(root, "action", json_integer(action));
192 TELEMETRY_LOG_ERR("Setting valid action field in root object failed");
197 if (inv_choice == INV_COMMAND_FIELD) {
198 ret = json_object_set_new(root, "co---nd", json_string(command));
200 TELEMETRY_LOG_ERR("Setting invalid command field in root object failed");
204 ret = json_object_set_new(root, "command", json_string(command));
206 TELEMETRY_LOG_ERR("Setting valid command field in root object failed");
213 data = json_object();
215 TELEMETRY_LOG_ERR("Data json object creation failed");
219 ret = json_object_set_new(data, "client_path",
220 json_string(client_path));
222 TELEMETRY_LOG_ERR("Setting valid client_path field in data object failed");
226 } else if (port_ids) {
227 ret = rte_telemetry_format_port_stat_ids(port_ids, num_port_ids,
228 stat_names, num_stat_names, &data);
230 TELEMETRY_LOG_ERR("Formatting Port/Stat arrays failed");
236 if (inv_choice == INV_DATA_FIELD) {
237 ret = json_object_set_new(root, "d--a", data);
239 TELEMETRY_LOG_ERR("Setting invalid data field in data object failed");
243 ret = json_object_set_new(root, "data", data);
245 TELEMETRY_LOG_ERR("Setting valid data field in data object failed");
250 *request = json_dumps(root, 0);
251 if (*request == NULL) {
252 TELEMETRY_LOG_ERR("Converting JSON root object to char* failed");
266 rte_telemetry_send_get_ports_and_stats_request(struct telemetry_impl *telemetry,
267 int action_choice, char *command_choice, int inv_choice)
271 char *client_path_data = NULL;
273 if (telemetry == NULL) {
274 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
279 if (inv_choice == INV_ACTION_VAL)
281 else if (inv_choice == INV_COMMAND_VAL)
282 command_choice = "INVALID_COMMAND";
283 else if (inv_choice == INV_DATA_VAL)
284 client_path_data = "INVALID_DATA";
286 ret = rte_telemetry_create_json_request(action_choice, command_choice,
287 client_path_data, NULL, -1, NULL, -1, &request, inv_choice);
289 TELEMETRY_LOG_ERR("Could not create JSON Request");
293 if (inv_choice == INV_JSON_FORMAT)
296 ret = rte_telemetry_parse(telemetry, request);
298 TELEMETRY_LOG_WARN("Could not parse JSON Request");
306 rte_telemetry_send_get_ports_details_request(struct telemetry_impl *telemetry,
307 int action_choice, int *port_ids, int num_port_ids, int inv_choice)
311 if (telemetry == NULL) {
312 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
316 char *command = "ports_details";
318 if (inv_choice == INV_ACTION_VAL)
320 else if (inv_choice == INV_COMMAND_VAL)
321 command = "INVALID_COMMAND";
322 else if (inv_choice == INV_DATA_VAL)
326 ret = rte_telemetry_create_json_request(action_choice, command, NULL,
327 port_ids, num_port_ids, NULL, -1, &request, inv_choice);
329 TELEMETRY_LOG_ERR("Could not create JSON Request");
333 if (inv_choice == INV_JSON_FORMAT)
336 ret = rte_telemetry_parse(telemetry, request);
338 TELEMETRY_LOG_WARN("Could not parse JSON Request");
346 rte_telemetry_send_stats_values_by_name_request(struct telemetry_impl
347 *telemetry, int action_choice, int *port_ids, int num_port_ids,
348 const char * const *stat_names, int num_stat_names,
353 char *command = "ports_stats_values_by_name";
355 if (telemetry == NULL) {
356 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
360 if (inv_choice == INV_ACTION_VAL)
362 else if (inv_choice == INV_COMMAND_VAL)
363 command = "INVALID_COMMAND";
364 else if (inv_choice == INV_DATA_VAL) {
369 ret = rte_telemetry_create_json_request(action_choice, command, NULL,
370 port_ids, num_port_ids, stat_names, num_stat_names, &request,
373 TELEMETRY_LOG_ERR("Could not create JSON Request");
377 if (inv_choice == INV_JSON_FORMAT)
380 ret = rte_telemetry_parse(telemetry, request);
382 TELEMETRY_LOG_WARN("Could not parse JSON Request");
390 rte_telemetry_send_unreg_request(struct telemetry_impl *telemetry,
391 int action_choice, const char *client_path, int inv_choice)
396 if (telemetry == NULL) {
397 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
401 char *command = "clients";
403 if (inv_choice == INV_ACTION_VAL)
405 else if (inv_choice == INV_COMMAND_VAL)
406 command = "INVALID_COMMAND";
407 else if (inv_choice == INV_DATA_VAL)
410 ret = rte_telemetry_create_json_request(action_choice, command,
411 client_path, NULL, -1, NULL, -1, &request, inv_choice);
413 TELEMETRY_LOG_ERR("Could not create JSON Request");
417 if (inv_choice == INV_JSON_FORMAT)
420 ret = rte_telemetry_parse(telemetry, request);
422 TELEMETRY_LOG_WARN("Could not parse JSON Request");
430 rte_telemetry_parser_test(struct telemetry_impl *telemetry)
433 const char *client_path = TEST_CLIENT;
435 if (telemetry == NULL) {
436 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
440 ret = rte_telemetry_create_test_socket(telemetry, client_path);
442 TELEMETRY_LOG_ERR("Could not create test request client socket");
446 int port_ids[] = {0, 1};
447 int num_port_ids = RTE_DIM(port_ids);
449 static const char * const stat_names[] = {"tx_good_packets",
451 int num_stat_names = RTE_DIM(stat_names);
453 static const char * const test_types[] = {
454 "INVALID ACTION VALUE TESTS",
455 "INVALID COMMAND VALUE TESTS",
456 "INVALID DATA VALUE TESTS",
457 "INVALID ACTION FIELD TESTS",
458 "INVALID COMMAND FIELD TESTS",
459 "INVALID DATA FIELD TESTS",
460 "INVALID JSON FORMAT TESTS",
465 #define NUM_TEST_TYPES (sizeof(test_types)/sizeof(const char * const))
468 for (i = 0; i < NUM_TEST_TYPES; i++) {
469 TELEMETRY_LOG_INFO("%s", test_types[i]);
471 ret = rte_telemetry_send_get_ports_and_stats_request(telemetry,
472 ACTION_GET, "ports", i);
473 if (ret != 0 && i == VALID_REQ) {
474 TELEMETRY_LOG_ERR("Get ports valid test failed");
476 } else if (ret != -1 && i != VALID_REQ) {
477 TELEMETRY_LOG_ERR("Get ports invalid test failed");
481 TELEMETRY_LOG_INFO("Success - Get ports test passed");
483 ret = rte_telemetry_send_get_ports_details_request(telemetry,
484 ACTION_GET, port_ids, num_port_ids, i);
485 if (ret != 0 && i == VALID_REQ) {
486 TELEMETRY_LOG_ERR("Get ports details valid");
488 } else if (ret != -1 && i != VALID_REQ) {
489 TELEMETRY_LOG_ERR("Get ports details invalid");
493 TELEMETRY_LOG_INFO("Success - Get ports details test passed");
495 ret = rte_telemetry_send_get_ports_and_stats_request(telemetry,
496 ACTION_GET, "port_stats", i);
497 if (ret != 0 && i == VALID_REQ) {
498 TELEMETRY_LOG_ERR("Get port stats valid test");
500 } else if (ret != -1 && i != VALID_REQ) {
501 TELEMETRY_LOG_ERR("Get ports stats invalid test failed");
505 TELEMETRY_LOG_INFO("Success - Get ports stats test passed");
507 ret = rte_telemetry_send_stats_values_by_name_request(telemetry,
508 ACTION_GET, port_ids, num_port_ids, stat_names,
510 if (ret != 0 && i == VALID_REQ) {
511 TELEMETRY_LOG_ERR("Get ports stats values by name valid test failed");
513 } else if (ret != -1 && i != VALID_REQ) {
514 TELEMETRY_LOG_ERR("Get ports stats values by name invalid test failed");
518 TELEMETRY_LOG_INFO("Success - Get ports stats values by name test passed");
520 ret = rte_telemetry_send_unreg_request(telemetry, ACTION_DELETE,
522 if (ret != 0 && i == VALID_REQ) {
523 TELEMETRY_LOG_ERR("Deregister valid test failed");
525 } else if (ret != -1 && i != VALID_REQ) {
526 TELEMETRY_LOG_ERR("Deregister invalid test failed");
530 TELEMETRY_LOG_INFO("Success - Deregister test passed");