1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <sys/socket.h>
13 #include <rte_metrics.h>
14 #include <rte_option.h>
15 #include <rte_string_fns.h>
16 #include <rte_lcore.h>
18 #include "rte_telemetry.h"
19 #include "rte_telemetry_internal.h"
20 #include "rte_telemetry_parser.h"
21 #include "rte_telemetry_socket_tests.h"
27 #define SELFTEST_VALID_CLIENT "/var/run/dpdk/valid_client"
28 #define SELFTEST_INVALID_CLIENT "/var/run/dpdk/invalid_client"
29 #define SOCKET_TEST_CLIENT_PATH "/var/run/dpdk/client"
31 static telemetry_impl *static_telemetry;
33 struct telemetry_message_test {
34 const char *test_name;
35 int (*test_func_ptr)(struct telemetry_impl *telemetry, int fd);
46 /* global array of functions pointers to metrics library */
47 const struct metrics_functions *metrics_fns;
50 rte_telemetry_set_metrics_fns(const struct metrics_functions *fns)
56 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
58 snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
62 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
63 const char *json_string)
67 if (telemetry == NULL) {
68 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
72 if (telemetry->request_client == NULL) {
73 TELEMETRY_LOG_ERR("No client has been chosen to write to");
77 if (json_string == NULL) {
78 TELEMETRY_LOG_ERR("Invalid JSON string!");
82 ret = send(telemetry->request_client->fd,
83 json_string, strlen(json_string), 0);
85 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
86 telemetry->request_client->file_path);
94 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
98 const char *status_code, *json_buffer;
101 if (error_type == -EPERM)
102 status_code = "Status Error: Unknown";
103 else if (error_type == -EINVAL)
104 status_code = "Status Error: Invalid Argument 404";
105 else if (error_type == -ENOMEM)
106 status_code = "Status Error: Memory Allocation Error";
108 TELEMETRY_LOG_ERR("Invalid error type");
112 root = json_object();
115 TELEMETRY_LOG_ERR("Could not create root JSON object");
119 ret = json_object_set_new(root, "status_code", json_string(status_code));
121 TELEMETRY_LOG_ERR("Status code field cannot be set");
126 ret = json_object_set_new(root, "data", json_null());
128 TELEMETRY_LOG_ERR("Data field cannot be set");
133 json_buffer = json_dumps(root, 0);
136 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
138 TELEMETRY_LOG_ERR("Could not write to socket");
146 rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
147 struct telemetry_impl *telemetry)
150 char *json_buffer = NULL;
152 if (telemetry == NULL) {
153 TELEMETRY_LOG_ERR("Invalid telemetry argument");
157 ret = metrics_fns->encode_json_format(ep, &json_buffer);
159 TELEMETRY_LOG_ERR("JSON encode function failed");
160 ret = rte_telemetry_send_error_response(telemetry, ret);
162 TELEMETRY_LOG_ERR("Could not send error");
166 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
168 TELEMETRY_LOG_ERR("Could not write to socket");
176 rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
177 struct telemetry_impl *telemetry)
180 char *json_buffer = NULL;
182 if (telemetry == NULL) {
183 TELEMETRY_LOG_ERR("Invalid telemetry argument");
188 TELEMETRY_LOG_ERR("Invalid encode param argument");
192 if (ep->pp.num_metric_ids < 0) {
193 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
197 if (ep->pp.num_port_ids < 0) {
198 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
202 ret = metrics_fns->get_ports_stats_json(ep, telemetry->reg_index,
205 TELEMETRY_LOG_ERR("Function for get_ports_stats_json"
207 ret = rte_telemetry_send_error_response(telemetry, ret);
209 TELEMETRY_LOG_ERR("Could not send error");
213 ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
215 TELEMETRY_LOG_ERR("Could not write to socket");
222 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
224 TELEMETRY_LOG_ERR("Could not send error");
229 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
234 ret = metrics_fns->reg_all_ethdev(&telemetry->metrics_register_done,
235 telemetry->reg_index);
237 TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
238 ret = rte_telemetry_send_error_response(telemetry, ret);
240 TELEMETRY_LOG_ERR("Could not send error");
245 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
246 telemetry->server_fd);
250 ret = rte_telemetry_parser_test(telemetry);
252 TELEMETRY_LOG_ERR("Parser Tests Failed");
256 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
263 rte_telemetry_read_client(struct telemetry_impl *telemetry)
266 int ret, buffer_read;
268 buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
270 if (buffer_read == -1) {
271 TELEMETRY_LOG_ERR("Read error");
273 } else if (buffer_read == 0) {
276 buf[buffer_read] = '\0';
277 ret = rte_telemetry_parse_client_message(telemetry, buf);
279 TELEMETRY_LOG_WARN("Parse message failed");
284 if (close(telemetry->accept_fd) < 0) {
285 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
289 telemetry->accept_fd = 0;
295 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
299 if (telemetry->accept_fd <= 0) {
300 ret = listen(telemetry->server_fd, 1);
302 TELEMETRY_LOG_ERR("Listening error with server fd");
306 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
307 if (telemetry->accept_fd >= 0 &&
308 telemetry->metrics_register_done == 0) {
309 ret = rte_telemetry_initial_accept(telemetry);
311 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
316 ret = rte_telemetry_read_client(telemetry);
318 TELEMETRY_LOG_ERR("Failed to read socket buffer");
327 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
330 telemetry_client *client;
331 char client_buf[BUF_SIZE];
334 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
335 bytes = read(client->fd, client_buf, BUF_SIZE-1);
338 client_buf[bytes] = '\0';
339 telemetry->request_client = client;
340 ret = rte_telemetry_parse(telemetry, client_buf);
342 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
353 rte_telemetry_run(void *userdata)
356 struct telemetry_impl *telemetry = userdata;
358 if (telemetry == NULL) {
359 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
363 ret = rte_telemetry_accept_new_client(telemetry);
365 TELEMETRY_LOG_ERR("Accept and read new client failed");
369 ret = rte_telemetry_read_client_sockets(telemetry);
371 TELEMETRY_LOG_ERR("Client socket read failed");
379 *rte_telemetry_run_thread_func(void *userdata)
382 struct telemetry_impl *telemetry = userdata;
384 if (telemetry == NULL) {
385 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
389 while (telemetry->thread_status) {
390 rte_telemetry_run(telemetry);
391 ret = usleep(SLEEP_TIME);
393 TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
399 rte_telemetry_set_socket_nonblock(int fd)
404 TELEMETRY_LOG_ERR("Invalid fd provided");
408 flags = fcntl(fd, F_GETFL, 0);
412 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
416 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
419 struct sockaddr_un addr;
420 char socket_path[BUF_SIZE];
422 if (telemetry == NULL)
425 telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
426 if (telemetry->server_fd == -1) {
427 TELEMETRY_LOG_ERR("Failed to open socket");
431 ret = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
433 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
437 addr.sun_family = AF_UNIX;
438 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
439 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
442 if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
444 TELEMETRY_LOG_ERR("Socket binding error");
451 if (close(telemetry->server_fd) < 0) {
452 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
460 rte_telemetry_init(void)
464 const char *telemetry_ctrl_thread = "telemetry";
466 if (static_telemetry) {
467 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
471 static_telemetry = calloc(1, sizeof(struct telemetry_impl));
472 if (static_telemetry == NULL) {
473 TELEMETRY_LOG_ERR("Memory could not be allocated");
477 static_telemetry->socket_id = rte_socket_id();
479 ret = pthread_attr_init(&attr);
481 TELEMETRY_LOG_ERR("Pthread attribute init failed");
485 ret = rte_telemetry_create_socket(static_telemetry);
487 ret = rte_telemetry_cleanup();
489 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
492 TAILQ_INIT(&static_telemetry->client_list_head);
494 ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
495 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
496 (void *)static_telemetry);
497 static_telemetry->thread_status = 1;
500 ret = rte_telemetry_cleanup();
502 TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
510 rte_telemetry_client_cleanup(struct telemetry_client *client)
514 ret = close(client->fd);
515 free(client->file_path);
519 TELEMETRY_LOG_ERR("Close client socket failed");
527 rte_telemetry_cleanup(void)
530 struct telemetry_impl *telemetry = static_telemetry;
531 telemetry_client *client, *temp_client;
533 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
535 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
536 ret = rte_telemetry_client_cleanup(client);
538 TELEMETRY_LOG_ERR("Client cleanup failed");
543 ret = close(telemetry->server_fd);
545 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
550 telemetry->thread_status = 0;
551 pthread_join(telemetry->thread_id, NULL);
553 static_telemetry = NULL;
559 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
560 const char *client_path)
563 telemetry_client *client, *temp_client;
565 if (telemetry == NULL) {
566 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
570 if (client_path == NULL) {
571 TELEMETRY_LOG_ERR("Invalid client path");
575 if (TAILQ_EMPTY(&telemetry->client_list_head)) {
576 TELEMETRY_LOG_ERR("There are no clients currently registered");
580 TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
582 if (strcmp(client_path, client->file_path) == 0) {
583 TAILQ_REMOVE(&telemetry->client_list_head, client,
585 ret = rte_telemetry_client_cleanup(client);
588 TELEMETRY_LOG_ERR("Client cleanup failed");
596 TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
600 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
602 TELEMETRY_LOG_ERR("Could not send error");
607 rte_telemetry_register_client(struct telemetry_impl *telemetry,
608 const char *client_path)
611 struct sockaddr_un addrs;
613 if (telemetry == NULL) {
614 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
618 if (client_path == NULL) {
619 TELEMETRY_LOG_ERR("Invalid client path");
623 telemetry_client *client;
624 TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
625 if (strcmp(client_path, client->file_path) == 0) {
626 TELEMETRY_LOG_WARN("'%s' already registered",
632 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
634 TELEMETRY_LOG_ERR("Client socket error");
638 ret = rte_telemetry_set_socket_nonblock(fd);
640 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
644 addrs.sun_family = AF_UNIX;
645 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
646 telemetry_client *new_client = malloc(sizeof(telemetry_client));
647 new_client->file_path = strdup(client_path);
650 if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
651 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
653 ret = rte_telemetry_client_cleanup(new_client);
655 TELEMETRY_LOG_ERR("Client cleanup failed");
661 TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
667 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
671 json_t *root = json_loads(buf, 0, &error);
674 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
677 } else if (!json_is_object(root)) {
678 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
682 json_t *action = json_object_get(root, "action");
683 if (action == NULL) {
684 TELEMETRY_LOG_WARN("Request does not have action field");
686 } else if (!json_is_integer(action)) {
687 TELEMETRY_LOG_WARN("Action value is not an integer");
691 json_t *command = json_object_get(root, "command");
692 if (command == NULL) {
693 TELEMETRY_LOG_WARN("Request does not have command field");
695 } else if (!json_is_string(command)) {
696 TELEMETRY_LOG_WARN("Command value is not a string");
700 action_int = json_integer_value(action);
701 if (action_int != ACTION_POST) {
702 TELEMETRY_LOG_WARN("Invalid action code");
706 if (strcmp(json_string_value(command), "clients") != 0) {
707 TELEMETRY_LOG_WARN("Invalid command");
711 json_t *data = json_object_get(root, "data");
713 TELEMETRY_LOG_WARN("Request does not have data field");
717 json_t *client_path = json_object_get(data, "client_path");
718 if (client_path == NULL) {
719 TELEMETRY_LOG_WARN("Request does not have client_path field");
723 if (!json_is_string(client_path)) {
724 TELEMETRY_LOG_WARN("Client_path value is not a string");
728 ret = rte_telemetry_register_client(telemetry,
729 json_string_value(client_path));
731 TELEMETRY_LOG_ERR("Could not register client");
732 telemetry->register_fail_count++;
739 TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
745 rte_telemetry_dummy_client_socket(const char *valid_client_path)
747 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
748 struct sockaddr_un addr = {0};
751 TELEMETRY_LOG_ERR("Test socket creation failure");
755 addr.sun_family = AF_UNIX;
756 strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
757 unlink(valid_client_path);
759 if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
760 TELEMETRY_LOG_ERR("Test socket binding failure");
764 if (listen(sockfd, 1) < 0) {
765 TELEMETRY_LOG_ERR("Listen failure");
773 rte_telemetry_selftest(void)
775 const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
776 const char *valid_client_path = SELFTEST_VALID_CLIENT;
779 TELEMETRY_LOG_INFO("Selftest");
781 ret = rte_telemetry_init();
783 TELEMETRY_LOG_ERR("Valid initialisation test failed");
787 TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
789 ret = rte_telemetry_init();
790 if (ret != -EALREADY) {
791 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
795 TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
797 ret = rte_telemetry_unregister_client(static_telemetry,
798 invalid_client_path);
800 TELEMETRY_LOG_ERR("Invalid unregister test failed");
804 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
806 sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
808 TELEMETRY_LOG_ERR("Test socket creation failed");
812 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
814 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
818 accept(sockfd, NULL, NULL);
819 TELEMETRY_LOG_INFO("Success - Valid register test passed");
821 ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
822 if (ret != -EINVAL) {
823 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
827 TELEMETRY_LOG_INFO("Success - Invalid register test passed");
829 ret = rte_telemetry_unregister_client(static_telemetry,
830 invalid_client_path);
832 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
836 TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
838 ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
840 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
844 TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
846 ret = rte_telemetry_cleanup();
848 TELEMETRY_LOG_ERR("Cleanup test failed");
852 TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
858 rte_telemetry_socket_messaging_testing(int index, int socket)
860 struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
861 int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
863 if (telemetry == NULL) {
864 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
868 telemetry->server_fd = socket;
869 telemetry->reg_index[0] = index;
870 TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
871 rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
872 TELEMETRY_LOG_INFO("Register valid client test");
874 ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
877 TELEMETRY_LOG_ERR("Register valid client test failed!");
882 TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
884 TELEMETRY_LOG_INFO("Register invalid/same client test");
885 ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
887 ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
888 bad_send_fd, bad_recv_fd);
890 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
895 TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
897 ret = rte_telemetry_json_socket_message_test(telemetry, fd);
908 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
909 int send_fd, int recv_fd)
912 char good_req_string[BUF_SIZE];
914 snprintf(good_req_string, sizeof(good_req_string),
915 "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
916 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
920 ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
922 TELEMETRY_LOG_ERR("Could not send message over socket");
926 rte_telemetry_run(telemetry);
928 if (telemetry->register_fail_count != 0)
931 *fd = accept(recv_fd, NULL, NULL);
937 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
941 const char *client_path = SOCKET_TEST_CLIENT_PATH;
942 char socket_path[BUF_SIZE];
943 struct sockaddr_un addr = {0};
944 struct sockaddr_un addrs = {0};
945 *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
946 *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
948 listen(telemetry->server_fd, 5);
949 addr.sun_family = AF_UNIX;
950 rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
951 strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
953 ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
955 TELEMETRY_LOG_ERR("Could not connect socket");
959 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
961 addrs.sun_family = AF_UNIX;
962 strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
965 ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
967 TELEMETRY_LOG_ERR("Could not bind socket");
975 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
978 json_t *root = json_loads(buf, 0, &error);
980 json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
988 TELEMETRY_LOG_ERR("JSON message is NULL");
993 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
996 } else if (!json_is_object(root)) {
997 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1002 status = json_object_get(root, "status_code");
1004 TELEMETRY_LOG_ERR("Request does not have status field");
1006 } else if (!json_is_string(status)) {
1007 TELEMETRY_LOG_ERR("Status value is not a string");
1011 json_data_struct->status_code = strdup(json_string_value(status));
1013 dataArray = json_object_get(root, "data");
1014 if (dataArray == NULL) {
1015 TELEMETRY_LOG_ERR("Request does not have data field");
1019 arraylen = json_array_size(dataArray);
1020 if (arraylen == 0) {
1021 json_data_struct->data = "null";
1025 for (i = 0; i < arraylen; i++) {
1026 dataArrayObj = json_array_get(dataArray, i);
1027 port = json_object_get(dataArrayObj, "port");
1028 stats = json_object_get(dataArrayObj, "stats");
1032 TELEMETRY_LOG_ERR("Request does not have port field");
1036 if (!json_is_integer(port)) {
1037 TELEMETRY_LOG_ERR("Port value is not an integer");
1041 json_data_struct->port = json_integer_value(port);
1043 if (stats == NULL) {
1044 TELEMETRY_LOG_ERR("Request does not have stats field");
1048 arraylen = json_array_size(stats);
1049 for (i = 0; i < arraylen; i++) {
1050 statsArrayObj = json_array_get(stats, i);
1051 name = json_object_get(statsArrayObj, "name");
1052 value = json_object_get(statsArrayObj, "value");
1056 TELEMETRY_LOG_ERR("Request does not have name field");
1060 if (!json_is_string(name)) {
1061 TELEMETRY_LOG_ERR("Stat name value is not a string");
1065 json_data_struct->stat_name = strdup(json_string_value(name));
1067 if (value == NULL) {
1068 TELEMETRY_LOG_ERR("Request does not have value field");
1072 if (!json_is_integer(value)) {
1073 TELEMETRY_LOG_ERR("Stat value is not an integer");
1077 json_data_struct->stat_value = json_integer_value(value);
1083 rte_telemetry_free_test_data(struct json_data *data)
1085 free(data->status_code);
1086 free(data->stat_name);
1091 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1097 int buffer_read = 0;
1099 struct json_data *data_struct;
1101 const char *status = "Status OK: 200";
1102 const char *name = "rx_good_packets";
1103 const char *valid_json_message = "{\"action\":0,\"command\":"
1104 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1105 ":[0],\"stats\":[\"rx_good_packets\"]}}";
1107 ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1109 TELEMETRY_LOG_ERR("Could not send message over socket");
1113 rte_telemetry_run(telemetry);
1114 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1116 if (buffer_read == -1) {
1117 TELEMETRY_LOG_ERR("Read error");
1121 buf[buffer_read] = '\0';
1122 data_struct = calloc(1, sizeof(struct json_data));
1123 ret = rte_telemetry_stat_parse(buf, data_struct);
1126 TELEMETRY_LOG_ERR("Could not parse stats");
1130 if (strcmp(data_struct->status_code, status) != 0) {
1131 TELEMETRY_LOG_ERR("Status code is invalid");
1135 if (data_struct->port != port) {
1136 TELEMETRY_LOG_ERR("Port is invalid");
1140 if (strcmp(data_struct->stat_name, name) != 0) {
1141 TELEMETRY_LOG_ERR("Stat name is invalid");
1145 if (data_struct->stat_value != value) {
1146 TELEMETRY_LOG_ERR("Stat value is invalid");
1150 rte_telemetry_free_test_data(data_struct);
1154 TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1160 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1165 const char *invalid_json = "{]";
1166 const char *status = "Status Error: Unknown";
1167 const char *data = "null";
1168 struct json_data *data_struct;
1169 int buffer_read = 0;
1172 ret = send(fd, invalid_json, strlen(invalid_json), 0);
1174 TELEMETRY_LOG_ERR("Could not send message over socket");
1178 rte_telemetry_run(telemetry);
1179 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1181 if (buffer_read == -1) {
1182 TELEMETRY_LOG_ERR("Read error");
1186 buf[buffer_read] = '\0';
1188 data_struct = calloc(1, sizeof(struct json_data));
1189 ret = rte_telemetry_stat_parse(buf, data_struct);
1192 TELEMETRY_LOG_ERR("Could not parse stats");
1194 if (strcmp(data_struct->status_code, status) != 0) {
1195 TELEMETRY_LOG_ERR("Status code is invalid");
1199 if (strcmp(data_struct->data, data) != 0) {
1200 TELEMETRY_LOG_ERR("Data status is invalid");
1204 rte_telemetry_free_test_data(data_struct);
1208 TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1214 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1219 const char *status = "Status Error: Invalid Argument 404";
1220 const char *data = "null";
1221 struct json_data *data_struct;
1222 const char *invalid_contents = "{\"action\":0,\"command\":"
1223 "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1224 ":[0],\"stats\":[\"some_invalid_param\","
1225 "\"another_invalid_param\"]}}";
1226 int buffer_read = 0;
1229 ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1231 TELEMETRY_LOG_ERR("Could not send message over socket");
1235 rte_telemetry_run(telemetry);
1236 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1238 if (buffer_read == -1) {
1239 TELEMETRY_LOG_ERR("Read error");
1243 buf[buffer_read] = '\0';
1244 data_struct = calloc(1, sizeof(struct json_data));
1245 ret = rte_telemetry_stat_parse(buf, data_struct);
1248 TELEMETRY_LOG_ERR("Could not parse stats");
1250 if (strcmp(data_struct->status_code, status) != 0) {
1251 TELEMETRY_LOG_ERR("Status code is invalid");
1255 if (strcmp(data_struct->data, data) != 0) {
1256 TELEMETRY_LOG_ERR("Data status is invalid");
1260 rte_telemetry_free_test_data(data_struct);
1264 TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1270 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1275 const char *status = "Status Error: Invalid Argument 404";
1276 const char *data = "null";
1277 struct json_data *data_struct;
1278 const char *empty_json = "{}";
1279 int buffer_read = 0;
1282 ret = (send(fd, empty_json, strlen(empty_json), 0));
1284 TELEMETRY_LOG_ERR("Could not send message over socket");
1288 rte_telemetry_run(telemetry);
1289 buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1291 if (buffer_read == -1) {
1292 TELEMETRY_LOG_ERR("Read error");
1296 buf[buffer_read] = '\0';
1297 data_struct = calloc(1, sizeof(struct json_data));
1298 ret = rte_telemetry_stat_parse(buf, data_struct);
1301 TELEMETRY_LOG_ERR("Could not parse stats");
1303 if (strcmp(data_struct->status_code, status) != 0) {
1304 TELEMETRY_LOG_ERR("Status code is invalid");
1308 if (strcmp(data_struct->data, data) != 0) {
1309 TELEMETRY_LOG_ERR("Data status is invalid");
1313 rte_telemetry_free_test_data(data_struct);
1318 TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1324 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1327 int ret, fail_count;
1330 struct telemetry_message_test socket_json_tests[] = {
1331 {.test_name = "Invalid JSON test",
1332 .test_func_ptr = rte_telemetry_invalid_json_test},
1333 {.test_name = "Valid JSON test",
1334 .test_func_ptr = rte_telemetry_valid_json_test},
1335 {.test_name = "JSON contents test",
1336 .test_func_ptr = rte_telemetry_json_contents_test},
1337 {.test_name = "JSON empty tests",
1338 .test_func_ptr = rte_telemetry_json_empty_test}
1341 #define NUM_TESTS RTE_DIM(socket_json_tests)
1343 for (i = 0; i < NUM_TESTS; i++) {
1344 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1345 ret = (socket_json_tests[i].test_func_ptr)
1348 TELEMETRY_LOG_ERR("%s failed",
1349 socket_json_tests[i].test_name);
1354 if (fail_count > 0) {
1355 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1360 TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1365 int telemetry_log_level;
1367 static struct rte_option option = {
1368 .name = "telemetry",
1369 .usage = "Enable telemetry backend",
1370 .cb = &rte_telemetry_init,
1374 RTE_INIT(rte_telemetry_register)
1376 telemetry_log_level = rte_log_register("lib.telemetry");
1377 if (telemetry_log_level >= 0)
1378 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1380 rte_option_register(&option);