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"
19 #include "rte_telemetry_internal.h"
33 #define TEST_CLIENT "/var/run/dpdk/test_client"
36 rte_telemetry_create_test_socket(struct telemetry_impl *telemetry,
37 const char *test_client_path)
40 struct sockaddr_un addr = {0};
41 struct telemetry_client *client;
43 if (telemetry == NULL) {
44 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
48 sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
50 TELEMETRY_LOG_ERR("Test socket creation failure");
54 addr.sun_family = AF_UNIX;
55 strlcpy(addr.sun_path, test_client_path, sizeof(addr.sun_path));
56 unlink(test_client_path);
58 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
59 TELEMETRY_LOG_ERR("Test socket binding failure");
63 if (listen(sockfd, 1) < 0) {
64 TELEMETRY_LOG_ERR("Listen failure");
68 ret = rte_telemetry_register_client(telemetry, test_client_path);
70 TELEMETRY_LOG_ERR("Register dummy client failed: %i", ret);
74 ret = accept(sockfd, NULL, NULL);
76 TELEMETRY_LOG_ERR("Socket accept failed");
80 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list)
81 telemetry->request_client = client;
87 rte_telemetry_format_port_stat_ids(int *port_ids, int num_port_ids,
88 const char * const *stat_names, int num_stat_names, json_t **data)
92 json_t *stat_names_json_array = NULL;
93 json_t *port_ids_json_array = NULL;
96 if (num_port_ids < 0) {
97 TELEMETRY_LOG_ERR("Port Ids Count invalid");
101 *data = json_object();
103 TELEMETRY_LOG_ERR("Data json object creation failed");
107 port_ids_json_array = json_array();
108 if (port_ids_json_array == NULL) {
109 TELEMETRY_LOG_ERR("port_ids_json_array creation failed");
113 for (i = 0; i < (uint32_t)num_port_ids; i++) {
114 ret = json_array_append(port_ids_json_array,
115 json_integer(port_ids[i]));
117 TELEMETRY_LOG_ERR("JSON array creation failed");
122 ret = json_object_set_new(*data, "ports", port_ids_json_array);
124 TELEMETRY_LOG_ERR("Setting 'ports' value in data object failed");
129 if (num_stat_names < 0) {
130 TELEMETRY_LOG_ERR("Stat Names Count invalid");
134 stat_names_json_array = json_array();
135 if (stat_names_json_array == NULL) {
136 TELEMETRY_LOG_ERR("stat_names_json_array creation failed");
141 for (i = 0; i < (uint32_t)num_stat_names; i++) {
142 ret = json_array_append(stat_names_json_array,
143 json_string(stat_names[i]));
145 TELEMETRY_LOG_ERR("JSON array creation failed");
150 ret = json_object_set_new(*data, "stats", stat_names_json_array);
152 TELEMETRY_LOG_ERR("Setting 'stats' value in data object failed");
162 if (stat_names_json_array)
163 json_decref(stat_names_json_array);
164 if (port_ids_json_array)
165 json_decref(port_ids_json_array);
170 rte_telemetry_create_json_request(int action, const char *command,
171 const char *client_path, int *port_ids, int num_port_ids,
172 const char * const *stat_names, int num_stat_names, char **request,
176 json_t *root = json_object();
180 TELEMETRY_LOG_ERR("Could not create root json object");
184 if (inv_choice == INV_ACTION_FIELD) {
185 ret = json_object_set_new(root, "ac--on", json_integer(action));
187 TELEMETRY_LOG_ERR("Setting invalid action field in root object failed");
191 ret = json_object_set_new(root, "action", json_integer(action));
193 TELEMETRY_LOG_ERR("Setting valid action field in root object failed");
198 if (inv_choice == INV_COMMAND_FIELD) {
199 ret = json_object_set_new(root, "co---nd", json_string(command));
201 TELEMETRY_LOG_ERR("Setting invalid command field in root object failed");
205 ret = json_object_set_new(root, "command", json_string(command));
207 TELEMETRY_LOG_ERR("Setting valid command field in root object failed");
214 data = json_object();
216 TELEMETRY_LOG_ERR("Data json object creation failed");
220 ret = json_object_set_new(data, "client_path",
221 json_string(client_path));
223 TELEMETRY_LOG_ERR("Setting valid client_path field in data object failed");
227 } else if (port_ids) {
228 ret = rte_telemetry_format_port_stat_ids(port_ids, num_port_ids,
229 stat_names, num_stat_names, &data);
231 TELEMETRY_LOG_ERR("Formatting Port/Stat arrays failed");
237 if (inv_choice == INV_DATA_FIELD) {
238 ret = json_object_set_new(root, "d--a", data);
240 TELEMETRY_LOG_ERR("Setting invalid data field in data object failed");
244 ret = json_object_set_new(root, "data", data);
246 TELEMETRY_LOG_ERR("Setting valid data field in data object failed");
251 *request = json_dumps(root, 0);
252 if (*request == NULL) {
253 TELEMETRY_LOG_ERR("Converting JSON root object to char* failed");
267 rte_telemetry_send_get_ports_and_stats_request(struct telemetry_impl *telemetry,
268 int action_choice, const char *command_choice, int inv_choice)
272 const char *client_path_data = NULL;
274 if (telemetry == NULL) {
275 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
280 if (inv_choice == INV_ACTION_VAL)
282 else if (inv_choice == INV_COMMAND_VAL)
283 command_choice = "INVALID_COMMAND";
284 else if (inv_choice == INV_DATA_VAL)
285 client_path_data = "INVALID_DATA";
287 ret = rte_telemetry_create_json_request(action_choice, command_choice,
288 client_path_data, NULL, -1, NULL, -1, &request, inv_choice);
290 TELEMETRY_LOG_ERR("Could not create JSON Request");
294 if (inv_choice == INV_JSON_FORMAT)
297 ret = rte_telemetry_parse(telemetry, request);
299 TELEMETRY_LOG_WARN("Could not parse JSON Request");
307 rte_telemetry_send_get_ports_details_request(struct telemetry_impl *telemetry,
308 int action_choice, int *port_ids, int num_port_ids, int inv_choice)
312 if (telemetry == NULL) {
313 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
317 const char *command = "ports_details";
319 if (inv_choice == INV_ACTION_VAL)
321 else if (inv_choice == INV_COMMAND_VAL)
322 command = "INVALID_COMMAND";
323 else if (inv_choice == INV_DATA_VAL)
327 ret = rte_telemetry_create_json_request(action_choice, command, NULL,
328 port_ids, num_port_ids, NULL, -1, &request, inv_choice);
330 TELEMETRY_LOG_ERR("Could not create JSON Request");
334 if (inv_choice == INV_JSON_FORMAT)
337 ret = rte_telemetry_parse(telemetry, request);
339 TELEMETRY_LOG_WARN("Could not parse JSON Request");
347 rte_telemetry_send_stats_values_by_name_request(struct telemetry_impl
348 *telemetry, int action_choice, int *port_ids, int num_port_ids,
349 const char * const *stat_names, int num_stat_names,
354 const char *command = "ports_stats_values_by_name";
356 if (telemetry == NULL) {
357 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
361 if (inv_choice == INV_ACTION_VAL)
363 else if (inv_choice == INV_COMMAND_VAL)
364 command = "INVALID_COMMAND";
365 else if (inv_choice == INV_DATA_VAL) {
370 ret = rte_telemetry_create_json_request(action_choice, command, NULL,
371 port_ids, num_port_ids, stat_names, num_stat_names, &request,
374 TELEMETRY_LOG_ERR("Could not create JSON Request");
378 if (inv_choice == INV_JSON_FORMAT)
381 ret = rte_telemetry_parse(telemetry, request);
383 TELEMETRY_LOG_WARN("Could not parse JSON Request");
391 rte_telemetry_send_unreg_request(struct telemetry_impl *telemetry,
392 int action_choice, const char *client_path, int inv_choice)
397 if (telemetry == NULL) {
398 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
402 const char *command = "clients";
404 if (inv_choice == INV_ACTION_VAL)
406 else if (inv_choice == INV_COMMAND_VAL)
407 command = "INVALID_COMMAND";
408 else if (inv_choice == INV_DATA_VAL)
411 ret = rte_telemetry_create_json_request(action_choice, command,
412 client_path, NULL, -1, NULL, -1, &request, inv_choice);
414 TELEMETRY_LOG_ERR("Could not create JSON Request");
418 if (inv_choice == INV_JSON_FORMAT)
421 ret = rte_telemetry_parse(telemetry, request);
423 TELEMETRY_LOG_WARN("Could not parse JSON Request");
431 rte_telemetry_parser_test(struct telemetry_impl *telemetry)
434 const char *client_path = TEST_CLIENT;
436 if (telemetry == NULL) {
437 TELEMETRY_LOG_ERR("Telemetry argument has not been initialised");
441 ret = rte_telemetry_create_test_socket(telemetry, client_path);
443 TELEMETRY_LOG_ERR("Could not create test request client socket");
447 int port_ids[] = {0, 1};
448 int num_port_ids = RTE_DIM(port_ids);
450 static const char * const stat_names[] = {"tx_good_packets",
452 int num_stat_names = RTE_DIM(stat_names);
454 static const char * const test_types[] = {
455 "INVALID ACTION VALUE TESTS",
456 "INVALID COMMAND VALUE TESTS",
457 "INVALID DATA VALUE TESTS",
458 "INVALID ACTION FIELD TESTS",
459 "INVALID COMMAND FIELD TESTS",
460 "INVALID DATA FIELD TESTS",
461 "INVALID JSON FORMAT TESTS",
467 for (i = 0; i < RTE_DIM(test_types); i++) {
468 TELEMETRY_LOG_INFO("%s", test_types[i]);
470 ret = rte_telemetry_send_get_ports_and_stats_request(telemetry,
471 ACTION_GET, "ports", i);
472 if (ret != 0 && i == VALID_REQ) {
473 TELEMETRY_LOG_ERR("Get ports valid test failed");
475 } else if (ret != -1 && i != VALID_REQ) {
476 TELEMETRY_LOG_ERR("Get ports invalid test failed");
480 TELEMETRY_LOG_INFO("Success - Get ports test passed");
482 ret = rte_telemetry_send_get_ports_details_request(telemetry,
483 ACTION_GET, port_ids, num_port_ids, i);
484 if (ret != 0 && i == VALID_REQ) {
485 TELEMETRY_LOG_ERR("Get ports details valid");
487 } else if (ret != -1 && i != VALID_REQ) {
488 TELEMETRY_LOG_ERR("Get ports details invalid");
492 TELEMETRY_LOG_INFO("Success - Get ports details test passed");
494 ret = rte_telemetry_send_get_ports_and_stats_request(telemetry,
495 ACTION_GET, "port_stats", i);
496 if (ret != 0 && i == VALID_REQ) {
497 TELEMETRY_LOG_ERR("Get port stats valid test");
499 } else if (ret != -1 && i != VALID_REQ) {
500 TELEMETRY_LOG_ERR("Get ports stats invalid test failed");
504 TELEMETRY_LOG_INFO("Success - Get ports stats test passed");
506 ret = rte_telemetry_send_stats_values_by_name_request(telemetry,
507 ACTION_GET, port_ids, num_port_ids, stat_names,
509 if (ret != 0 && i == VALID_REQ) {
510 TELEMETRY_LOG_ERR("Get ports stats values by name valid test failed");
512 } else if (ret != -1 && i != VALID_REQ) {
513 TELEMETRY_LOG_ERR("Get ports stats values by name invalid test failed");
517 TELEMETRY_LOG_INFO("Success - Get ports stats values by name test passed");
519 ret = rte_telemetry_send_unreg_request(telemetry, ACTION_DELETE,
521 if (ret != 0 && i == VALID_REQ) {
522 TELEMETRY_LOG_ERR("Deregister valid test failed");
524 } else if (ret != -1 && i != VALID_REQ) {
525 TELEMETRY_LOG_ERR("Deregister invalid test failed");
529 TELEMETRY_LOG_INFO("Success - Deregister test passed");