remove experimental tags from all symbol definitions
[dpdk.git] / lib / librte_telemetry / rte_telemetry.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <pthread.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <jansson.h>
11
12 #include <rte_eal.h>
13 #include <rte_ethdev.h>
14 #include <rte_metrics.h>
15 #include <rte_option.h>
16 #include <rte_string_fns.h>
17
18 #include "rte_telemetry.h"
19 #include "rte_telemetry_internal.h"
20 #include "rte_telemetry_parser.h"
21 #include "rte_telemetry_parser_test.h"
22 #include "rte_telemetry_socket_tests.h"
23
24 #define BUF_SIZE 1024
25 #define ACTION_POST 1
26 #define SLEEP_TIME 10
27
28 #define SELFTEST_VALID_CLIENT "/var/run/dpdk/valid_client"
29 #define SELFTEST_INVALID_CLIENT "/var/run/dpdk/invalid_client"
30 #define SOCKET_TEST_CLIENT_PATH "/var/run/dpdk/client"
31
32 static telemetry_impl *static_telemetry;
33
34 struct telemetry_message_test {
35         char *test_name;
36         int (*test_func_ptr)(struct telemetry_impl *telemetry, int fd);
37 };
38
39 struct json_data {
40         char *status_code;
41         char *data;
42         int port;
43         char *stat_name;
44         int stat_value;
45 };
46
47 static void
48 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
49 {
50         snprintf(socket_path, size, "%s/telemetry", rte_eal_get_runtime_dir());
51 }
52
53 int32_t
54 rte_telemetry_is_port_active(int port_id)
55 {
56         int ret;
57
58         ret = rte_eth_find_next(port_id);
59         if (ret == port_id)
60                 return 1;
61
62         TELEMETRY_LOG_ERR("port_id: %d is invalid, not active",
63                 port_id);
64
65         return 0;
66 }
67
68 static int32_t
69 rte_telemetry_update_metrics_ethdev(struct telemetry_impl *telemetry,
70         uint16_t port_id, int reg_start_index)
71 {
72         int ret, num_xstats, i;
73         struct rte_eth_xstat *eth_xstats;
74
75         if (!rte_eth_dev_is_valid_port(port_id)) {
76                 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
77                 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
78                 if (ret < 0)
79                         TELEMETRY_LOG_ERR("Could not send error");
80                 return -1;
81         }
82
83         ret = rte_telemetry_is_port_active(port_id);
84         if (ret < 1) {
85                 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
86                 if (ret < 0)
87                         TELEMETRY_LOG_ERR("Could not send error");
88                 return -1;
89         }
90
91         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
92         if (num_xstats < 0) {
93                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
94                                 num_xstats);
95                 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
96                 if (ret < 0)
97                         TELEMETRY_LOG_ERR("Could not send error");
98                 return -1;
99         }
100
101         eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
102         if (eth_xstats == NULL) {
103                 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
104                 ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
105                 if (ret < 0)
106                         TELEMETRY_LOG_ERR("Could not send error");
107                 return -1;
108         }
109
110         ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
111         if (ret < 0 || ret > num_xstats) {
112                 free(eth_xstats);
113                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
114                                 port_id, num_xstats, ret);
115                 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
116                 if (ret < 0)
117                         TELEMETRY_LOG_ERR("Could not send error");
118                 return -1;
119         }
120
121         uint64_t xstats_values[num_xstats];
122         for (i = 0; i < num_xstats; i++)
123                 xstats_values[i] = eth_xstats[i].value;
124
125         ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
126                         num_xstats);
127         if (ret < 0) {
128                 TELEMETRY_LOG_ERR("Could not update metrics values");
129                 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
130                 if (ret < 0)
131                         TELEMETRY_LOG_ERR("Could not send error");
132                 free(eth_xstats);
133                 return -1;
134         }
135
136         free(eth_xstats);
137         return 0;
138 }
139
140 int32_t
141 rte_telemetry_write_to_socket(struct telemetry_impl *telemetry,
142         const char *json_string)
143 {
144         int ret;
145
146         if (telemetry == NULL) {
147                 TELEMETRY_LOG_ERR("Could not initialise TELEMETRY_API");
148                 return -1;
149         }
150
151         if (telemetry->request_client == NULL) {
152                 TELEMETRY_LOG_ERR("No client has been chosen to write to");
153                 return -1;
154         }
155
156         if (json_string == NULL) {
157                 TELEMETRY_LOG_ERR("Invalid JSON string!");
158                 return -1;
159         }
160
161         ret = send(telemetry->request_client->fd,
162                         json_string, strlen(json_string), 0);
163         if (ret < 0) {
164                 TELEMETRY_LOG_ERR("Failed to write to socket for client: %s",
165                                 telemetry->request_client->file_path);
166                 return -1;
167         }
168
169         return 0;
170 }
171
172 int32_t
173 rte_telemetry_send_error_response(struct telemetry_impl *telemetry,
174         int error_type)
175 {
176         int ret;
177         const char *status_code, *json_buffer;
178         json_t *root;
179
180         if (error_type == -EPERM)
181                 status_code = "Status Error: Unknown";
182         else if (error_type == -EINVAL)
183                 status_code = "Status Error: Invalid Argument 404";
184         else if (error_type == -ENOMEM)
185                 status_code = "Status Error: Memory Allocation Error";
186         else {
187                 TELEMETRY_LOG_ERR("Invalid error type");
188                 return -EINVAL;
189         }
190
191         root = json_object();
192
193         if (root == NULL) {
194                 TELEMETRY_LOG_ERR("Could not create root JSON object");
195                 return -EPERM;
196         }
197
198         ret = json_object_set_new(root, "status_code", json_string(status_code));
199         if (ret < 0) {
200                 TELEMETRY_LOG_ERR("Status code field cannot be set");
201                 json_decref(root);
202                 return -EPERM;
203         }
204
205         ret = json_object_set_new(root, "data", json_null());
206         if (ret < 0) {
207                 TELEMETRY_LOG_ERR("Data field cannot be set");
208                 json_decref(root);
209                 return -EPERM;
210         }
211
212         json_buffer = json_dumps(root, 0);
213         json_decref(root);
214
215         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
216         if (ret < 0) {
217                 TELEMETRY_LOG_ERR("Could not write to socket");
218                 return -EPERM;
219         }
220
221         return 0;
222 }
223
224 static int
225 rte_telemetry_get_metrics(struct telemetry_impl *telemetry, uint32_t port_id,
226         struct rte_metric_value *metrics, struct rte_metric_name *names,
227         int num_metrics)
228 {
229         int ret, num_values;
230
231         if (num_metrics < 0) {
232                 TELEMETRY_LOG_ERR("Invalid metrics count");
233                 goto einval_fail;
234         } else if (num_metrics == 0) {
235                 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
236                 goto eperm_fail;
237         }
238
239         if (metrics == NULL) {
240                 TELEMETRY_LOG_ERR("Metrics must be initialised.");
241                 goto einval_fail;
242         }
243
244         if (names == NULL) {
245                 TELEMETRY_LOG_ERR("Names must be initialised.");
246                 goto einval_fail;
247         }
248
249         ret = rte_metrics_get_names(names, num_metrics);
250         if (ret < 0 || ret > num_metrics) {
251                 TELEMETRY_LOG_ERR("Cannot get metrics names");
252                 goto eperm_fail;
253         }
254
255         num_values = rte_metrics_get_values(port_id, NULL, 0);
256         ret = rte_metrics_get_values(port_id, metrics, num_values);
257         if (ret < 0 || ret > num_values) {
258                 TELEMETRY_LOG_ERR("Cannot get metrics values");
259                 goto eperm_fail;
260         }
261
262         return 0;
263
264 eperm_fail:
265         ret = rte_telemetry_send_error_response(telemetry, -EPERM);
266         if (ret < 0)
267                 TELEMETRY_LOG_ERR("Could not send error");
268         return -1;
269
270 einval_fail:
271         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
272         if (ret < 0)
273                 TELEMETRY_LOG_ERR("Could not send error");
274         return -1;
275
276 }
277
278 static int32_t
279 rte_telemetry_json_format_stat(struct telemetry_impl *telemetry, json_t *stats,
280         const char *metric_name, uint64_t metric_value)
281 {
282         int ret;
283         json_t *stat = json_object();
284
285         if (stat == NULL) {
286                 TELEMETRY_LOG_ERR("Could not create stat JSON object");
287                 goto eperm_fail;
288         }
289
290         ret = json_object_set_new(stat, "name", json_string(metric_name));
291         if (ret < 0) {
292                 TELEMETRY_LOG_ERR("Stat Name field cannot be set");
293                 goto eperm_fail;
294         }
295
296         ret = json_object_set_new(stat, "value", json_integer(metric_value));
297         if (ret < 0) {
298                 TELEMETRY_LOG_ERR("Stat Value field cannot be set");
299                 goto eperm_fail;
300         }
301
302         ret = json_array_append_new(stats, stat);
303         if (ret < 0) {
304                 TELEMETRY_LOG_ERR("Stat cannot be added to stats json array");
305                 goto eperm_fail;
306         }
307
308         return 0;
309
310 eperm_fail:
311         ret = rte_telemetry_send_error_response(telemetry, -EPERM);
312         if (ret < 0)
313                 TELEMETRY_LOG_ERR("Could not send error");
314         return -1;
315
316 }
317
318 static int32_t
319 rte_telemetry_json_format_port(struct telemetry_impl *telemetry,
320         uint32_t port_id, json_t *ports, uint32_t *metric_ids,
321         int num_metric_ids)
322 {
323         struct rte_metric_value *metrics = 0;
324         struct rte_metric_name *names = 0;
325         int num_metrics, ret, err_ret;
326         json_t *port, *stats;
327         int i;
328
329         num_metrics = rte_metrics_get_names(NULL, 0);
330         if (num_metrics < 0) {
331                 TELEMETRY_LOG_ERR("Cannot get metrics count");
332                 goto einval_fail;
333         } else if (num_metrics == 0) {
334                 TELEMETRY_LOG_ERR("No metrics to display (none have been registered)");
335                 goto eperm_fail;
336         }
337
338         metrics = malloc(sizeof(struct rte_metric_value) * num_metrics);
339         names = malloc(sizeof(struct rte_metric_name) * num_metrics);
340         if (metrics == NULL || names == NULL) {
341                 TELEMETRY_LOG_ERR("Cannot allocate memory");
342                 free(metrics);
343                 free(names);
344
345                 err_ret = rte_telemetry_send_error_response(telemetry, -ENOMEM);
346                 if (err_ret < 0)
347                         TELEMETRY_LOG_ERR("Could not send error");
348                 return -1;
349         }
350
351         ret  = rte_telemetry_get_metrics(telemetry, port_id, metrics, names,
352                 num_metrics);
353         if (ret < 0) {
354                 free(metrics);
355                 free(names);
356                 TELEMETRY_LOG_ERR("rte_telemetry_get_metrics failed");
357                 return -1;
358         }
359
360         port = json_object();
361         stats = json_array();
362         if (port == NULL || stats == NULL) {
363                 TELEMETRY_LOG_ERR("Could not create port/stats JSON objects");
364                 goto eperm_fail;
365         }
366
367         ret = json_object_set_new(port, "port", json_integer(port_id));
368         if (ret < 0) {
369                 TELEMETRY_LOG_ERR("Port field cannot be set");
370                 goto eperm_fail;
371         }
372
373         for (i = 0; i < num_metric_ids; i++) {
374                 int metric_id = metric_ids[i];
375                 int metric_index = -1;
376                 int metric_name_key = -1;
377                 int32_t j;
378                 uint64_t metric_value;
379
380                 if (metric_id >= num_metrics) {
381                         TELEMETRY_LOG_ERR("Metric_id: %d is not valid",
382                                         metric_id);
383                         goto einval_fail;
384                 }
385
386                 for (j = 0; j < num_metrics; j++) {
387                         if (metrics[j].key == metric_id) {
388                                 metric_name_key = metrics[j].key;
389                                 metric_index = j;
390                                 break;
391                         }
392                 }
393
394                 const char *metric_name = names[metric_name_key].name;
395                 metric_value = metrics[metric_index].value;
396
397                 if (metric_name_key < 0 || metric_index < 0) {
398                         TELEMETRY_LOG_ERR("Could not get metric name/index");
399                         goto eperm_fail;
400                 }
401
402                 ret = rte_telemetry_json_format_stat(telemetry, stats,
403                         metric_name, metric_value);
404                 if (ret < 0) {
405                         TELEMETRY_LOG_ERR("Format stat with id: %u failed",
406                                         metric_id);
407                         free(metrics);
408                         free(names);
409                         return -1;
410                 }
411         }
412
413         if (json_array_size(stats) == 0)
414                 ret = json_object_set_new(port, "stats", json_null());
415         else
416                 ret = json_object_set_new(port, "stats", stats);
417
418         if (ret < 0) {
419                 TELEMETRY_LOG_ERR("Stats object cannot be set");
420                 goto eperm_fail;
421         }
422
423         ret = json_array_append_new(ports, port);
424         if (ret < 0) {
425                 TELEMETRY_LOG_ERR("Port object cannot be added to ports array");
426                 goto eperm_fail;
427         }
428
429         free(metrics);
430         free(names);
431         return 0;
432
433 eperm_fail:
434         free(metrics);
435         free(names);
436         ret = rte_telemetry_send_error_response(telemetry, -EPERM);
437         if (ret < 0)
438                 TELEMETRY_LOG_ERR("Could not send error");
439         return -1;
440
441 einval_fail:
442         free(metrics);
443         free(names);
444         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
445         if (ret < 0)
446                 TELEMETRY_LOG_ERR("Could not send error");
447         return -1;
448 }
449
450 static int32_t
451 rte_telemetry_encode_json_format(struct telemetry_impl *telemetry,
452         struct telemetry_encode_param *ep, char **json_buffer)
453 {
454         int ret;
455         json_t *root, *ports;
456         int i;
457         uint32_t port_id;
458         int num_port_ids;
459         int num_metric_ids;
460
461         ports = json_array();
462         if (ports == NULL) {
463                 TELEMETRY_LOG_ERR("Could not create ports JSON array");
464                 goto eperm_fail;
465         }
466
467         if (ep->type == PORT_STATS) {
468                 num_port_ids = ep->pp.num_port_ids;
469                 num_metric_ids = ep->pp.num_metric_ids;
470
471                 if (num_port_ids <= 0 || num_metric_ids <= 0) {
472                         TELEMETRY_LOG_ERR("Please provide port and metric ids to query");
473                         goto einval_fail;
474                 }
475
476                 for (i = 0; i < num_port_ids; i++) {
477                         port_id = ep->pp.port_ids[i];
478                         if (!rte_eth_dev_is_valid_port(port_id)) {
479                                 TELEMETRY_LOG_ERR("Port: %d invalid",
480                                                         port_id);
481                                 goto einval_fail;
482                         }
483                 }
484
485                 for (i = 0; i < num_port_ids; i++) {
486                         port_id = ep->pp.port_ids[i];
487                         ret = rte_telemetry_json_format_port(telemetry,
488                                         port_id, ports, &ep->pp.metric_ids[0],
489                                         num_metric_ids);
490                         if (ret < 0) {
491                                 TELEMETRY_LOG_ERR("Format port in JSON failed");
492                                 return -1;
493                         }
494                 }
495         } else if (ep->type == GLOBAL_STATS) {
496                 /* Request Global Metrics */
497                 ret = rte_telemetry_json_format_port(telemetry,
498                                 RTE_METRICS_GLOBAL,
499                                 ports, &ep->gp.metric_ids[0],
500                                 ep->gp.num_metric_ids);
501                 if (ret < 0) {
502                         TELEMETRY_LOG_ERR(" Request Global Metrics Failed");
503                         return -1;
504                 }
505         } else {
506                 TELEMETRY_LOG_ERR(" Invalid metrics type in encode params");
507                 goto einval_fail;
508         }
509
510         root = json_object();
511         if (root == NULL) {
512                 TELEMETRY_LOG_ERR("Could not create root JSON object");
513                 goto eperm_fail;
514         }
515
516         ret = json_object_set_new(root, "status_code",
517                 json_string("Status OK: 200"));
518         if (ret < 0) {
519                 TELEMETRY_LOG_ERR("Status code field cannot be set");
520                 goto eperm_fail;
521         }
522
523         ret = json_object_set_new(root, "data", ports);
524         if (ret < 0) {
525                 TELEMETRY_LOG_ERR("Data field cannot be set");
526                 goto eperm_fail;
527         }
528
529         *json_buffer = json_dumps(root, JSON_INDENT(2));
530         json_decref(root);
531         return 0;
532
533 eperm_fail:
534         ret = rte_telemetry_send_error_response(telemetry, -EPERM);
535         if (ret < 0)
536                 TELEMETRY_LOG_ERR("Could not send error");
537         return -1;
538
539 einval_fail:
540         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
541         if (ret < 0)
542                 TELEMETRY_LOG_ERR("Could not send error");
543         return -1;
544 }
545
546 int32_t
547 rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
548         struct telemetry_impl *telemetry)
549 {
550         int ret;
551         char *json_buffer = NULL;
552
553         if (telemetry == NULL) {
554                 TELEMETRY_LOG_ERR("Invalid telemetry argument");
555                 return -1;
556         }
557
558         if (ep->gp.num_metric_ids < 0) {
559                 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
560                 goto einval_fail;
561         }
562
563         ret = rte_telemetry_encode_json_format(telemetry, ep,
564                 &json_buffer);
565         if (ret < 0) {
566                 TELEMETRY_LOG_ERR("JSON encode function failed");
567                 return -1;
568         }
569
570         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
571         if (ret < 0) {
572                 TELEMETRY_LOG_ERR("Could not write to socket");
573                 return -1;
574         }
575
576         return 0;
577
578 einval_fail:
579         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
580         if (ret < 0)
581                 TELEMETRY_LOG_ERR("Could not send error");
582         return -1;
583 }
584
585 int32_t
586 rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
587         struct telemetry_impl *telemetry)
588 {
589         int ret;
590         char *json_buffer = NULL;
591         uint32_t port_id;
592         int i;
593
594         if (telemetry == NULL) {
595                 TELEMETRY_LOG_ERR("Invalid telemetry argument");
596                 return -1;
597         }
598
599         if (ep == NULL) {
600                 TELEMETRY_LOG_ERR("Invalid encode param argument");
601                 goto einval_fail;
602         }
603
604         if (ep->pp.num_metric_ids < 0) {
605                 TELEMETRY_LOG_ERR("Invalid num_metric_ids, must be positive");
606                 goto einval_fail;
607         }
608
609         if (ep->pp.num_port_ids < 0) {
610                 TELEMETRY_LOG_ERR("Invalid num_port_ids, must be positive");
611                 goto einval_fail;
612         }
613
614         for (i = 0; i < ep->pp.num_port_ids; i++) {
615                 port_id = ep->pp.port_ids[i];
616                 if (!rte_eth_dev_is_valid_port(port_id)) {
617                         TELEMETRY_LOG_ERR("Port: %d invalid", port_id);
618                         goto einval_fail;
619                 }
620
621                 ret = rte_telemetry_update_metrics_ethdev(telemetry,
622                                 port_id, telemetry->reg_index[i]);
623                 if (ret < 0) {
624                         TELEMETRY_LOG_ERR("Failed to update ethdev metrics");
625                         return -1;
626                 }
627         }
628
629         ret = rte_telemetry_encode_json_format(telemetry, ep, &json_buffer);
630         if (ret < 0) {
631                 TELEMETRY_LOG_ERR("JSON encode function failed");
632                 return -1;
633         }
634
635         ret = rte_telemetry_write_to_socket(telemetry, json_buffer);
636         if (ret < 0) {
637                 TELEMETRY_LOG_ERR("Could not write to socket");
638                 return -1;
639         }
640
641         return 0;
642
643 einval_fail:
644         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
645         if (ret < 0)
646                 TELEMETRY_LOG_ERR("Could not send error");
647         return -1;
648 }
649
650
651 static int32_t
652 rte_telemetry_reg_ethdev_to_metrics(uint16_t port_id)
653 {
654         int ret, num_xstats, ret_val, i;
655         struct rte_eth_xstat *eth_xstats = NULL;
656         struct rte_eth_xstat_name *eth_xstats_names = NULL;
657
658         if (!rte_eth_dev_is_valid_port(port_id)) {
659                 TELEMETRY_LOG_ERR("port_id: %d is invalid", port_id);
660                 return -EINVAL;
661         }
662
663         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
664         if (num_xstats < 0) {
665                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
666                                 port_id, num_xstats);
667                 return -EPERM;
668         }
669
670         eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
671         if (eth_xstats == NULL) {
672                 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats");
673                 return -ENOMEM;
674         }
675
676         ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
677         const char *xstats_names[num_xstats];
678         eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * num_xstats);
679         if (ret < 0 || ret > num_xstats) {
680                 TELEMETRY_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
681                                 port_id, num_xstats, ret);
682                 ret_val = -EPERM;
683                 goto free_xstats;
684         }
685
686         if (eth_xstats_names == NULL) {
687                 TELEMETRY_LOG_ERR("Failed to malloc memory for xstats_names");
688                 ret_val = -ENOMEM;
689                 goto free_xstats;
690         }
691
692         ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
693         if (ret < 0 || ret > num_xstats) {
694                 TELEMETRY_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
695                                 port_id, num_xstats, ret);
696                 ret_val = -EPERM;
697                 goto free_xstats;
698         }
699
700         for (i = 0; i < num_xstats; i++)
701                 xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
702
703         ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
704         if (ret_val < 0) {
705                 TELEMETRY_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
706                 ret_val = -1;
707                 goto free_xstats;
708         }
709
710         goto free_xstats;
711
712 free_xstats:
713         free(eth_xstats);
714         free(eth_xstats_names);
715         return ret_val;
716 }
717
718 static int32_t
719 rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
720 {
721         struct driver_index {
722                 const void *dev_ops;
723                 int reg_index;
724         } drv_idx[RTE_MAX_ETHPORTS];
725         int nb_drv_idx = 0;
726         uint16_t pid;
727         int ret;
728         int selftest = 0;
729
730         RTE_ETH_FOREACH_DEV(pid) {
731                 int i;
732                 /* Different device types have different numbers of stats, so
733                  * first check if the stats for this type of device have
734                  * already been registered
735                  */
736                 for (i = 0; i < nb_drv_idx; i++) {
737                         if (rte_eth_devices[pid].dev_ops == drv_idx[i].dev_ops) {
738                                 telemetry->reg_index[pid] = drv_idx[i].reg_index;
739                                 break;
740                         }
741                 }
742                 if (i < nb_drv_idx)
743                         continue; /* we found a match, go to next port */
744
745                 /* No match, register a new set of xstats for this port */
746                 ret = rte_telemetry_reg_ethdev_to_metrics(pid);
747                 if (ret < 0) {
748                         TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
749                         return -1;
750                 }
751                 telemetry->reg_index[pid] = ret;
752                 drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
753                 drv_idx[nb_drv_idx].reg_index = ret;
754                 nb_drv_idx++;
755         }
756
757         telemetry->metrics_register_done = 1;
758         if (selftest) {
759                 ret = rte_telemetry_socket_messaging_testing(telemetry->reg_index[0],
760                                 telemetry->server_fd);
761                 if (ret < 0)
762                         return -1;
763
764                 ret = rte_telemetry_parser_test(telemetry);
765                 if (ret < 0) {
766                         TELEMETRY_LOG_ERR("Parser Tests Failed");
767                         return -1;
768                 }
769
770                 TELEMETRY_LOG_INFO("Success - All Parser Tests Passed");
771         }
772
773         return 0;
774 }
775
776 static int32_t
777 rte_telemetry_read_client(struct telemetry_impl *telemetry)
778 {
779         char buf[BUF_SIZE];
780         int ret, buffer_read;
781
782         buffer_read = read(telemetry->accept_fd, buf, BUF_SIZE-1);
783
784         if (buffer_read == -1) {
785                 TELEMETRY_LOG_ERR("Read error");
786                 return -1;
787         } else if (buffer_read == 0) {
788                 goto close_socket;
789         } else {
790                 buf[buffer_read] = '\0';
791                 ret = rte_telemetry_parse_client_message(telemetry, buf);
792                 if (ret < 0)
793                         TELEMETRY_LOG_WARN("Parse message failed");
794                 goto close_socket;
795         }
796
797 close_socket:
798         if (close(telemetry->accept_fd) < 0) {
799                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
800                 free(telemetry);
801                 return -EPERM;
802         }
803         telemetry->accept_fd = 0;
804
805         return 0;
806 }
807
808 static int32_t
809 rte_telemetry_accept_new_client(struct telemetry_impl *telemetry)
810 {
811         int ret;
812
813         if (telemetry->accept_fd <= 0) {
814                 ret = listen(telemetry->server_fd, 1);
815                 if (ret < 0) {
816                         TELEMETRY_LOG_ERR("Listening error with server fd");
817                         return -1;
818                 }
819
820                 telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
821                 if (telemetry->accept_fd >= 0 &&
822                         telemetry->metrics_register_done == 0) {
823                         ret = rte_telemetry_initial_accept(telemetry);
824                         if (ret < 0) {
825                                 TELEMETRY_LOG_ERR("Failed to run initial configurations/tests");
826                                 return -1;
827                         }
828                 }
829         } else {
830                 ret = rte_telemetry_read_client(telemetry);
831                 if (ret < 0) {
832                         TELEMETRY_LOG_ERR("Failed to read socket buffer");
833                         return -1;
834                 }
835         }
836
837         return 0;
838 }
839
840 static int32_t
841 rte_telemetry_read_client_sockets(struct telemetry_impl *telemetry)
842 {
843         int ret;
844         telemetry_client *client;
845         char client_buf[BUF_SIZE];
846         int bytes;
847
848         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
849                 bytes = read(client->fd, client_buf, BUF_SIZE-1);
850
851                 if (bytes > 0) {
852                         client_buf[bytes] = '\0';
853                         telemetry->request_client = client;
854                         ret = rte_telemetry_parse(telemetry, client_buf);
855                         if (ret < 0) {
856                                 TELEMETRY_LOG_WARN("Parse socket input failed: %i",
857                                                 ret);
858                                 return -1;
859                         }
860                 }
861         }
862
863         return 0;
864 }
865
866 static int32_t
867 rte_telemetry_run(void *userdata)
868 {
869         int ret;
870         struct telemetry_impl *telemetry = userdata;
871
872         if (telemetry == NULL) {
873                 TELEMETRY_LOG_WARN("TELEMETRY could not be initialised");
874                 return -1;
875         }
876
877         ret = rte_telemetry_accept_new_client(telemetry);
878         if (ret < 0) {
879                 TELEMETRY_LOG_ERR("Accept and read new client failed");
880                 return -1;
881         }
882
883         ret = rte_telemetry_read_client_sockets(telemetry);
884         if (ret < 0) {
885                 TELEMETRY_LOG_ERR("Client socket read failed");
886                 return -1;
887         }
888
889         return 0;
890 }
891
892 static void
893 *rte_telemetry_run_thread_func(void *userdata)
894 {
895         int ret;
896         struct telemetry_impl *telemetry = userdata;
897
898         if (telemetry == NULL) {
899                 TELEMETRY_LOG_ERR("%s passed a NULL instance", __func__);
900                 pthread_exit(0);
901         }
902
903         while (telemetry->thread_status) {
904                 rte_telemetry_run(telemetry);
905                 ret = usleep(SLEEP_TIME);
906                 if (ret < 0)
907                         TELEMETRY_LOG_ERR("Calling thread could not be put to sleep");
908         }
909         pthread_exit(0);
910 }
911
912 static int32_t
913 rte_telemetry_set_socket_nonblock(int fd)
914 {
915         int flags;
916
917         if (fd < 0) {
918                 TELEMETRY_LOG_ERR("Invalid fd provided");
919                 return -1;
920         }
921
922         flags = fcntl(fd, F_GETFL, 0);
923         if (flags < 0)
924                 flags = 0;
925
926         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
927 }
928
929 static int32_t
930 rte_telemetry_create_socket(struct telemetry_impl *telemetry)
931 {
932         int ret;
933         struct sockaddr_un addr;
934         char socket_path[BUF_SIZE];
935
936         if (telemetry == NULL)
937                 return -1;
938
939         telemetry->server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
940         if (telemetry->server_fd == -1) {
941                 TELEMETRY_LOG_ERR("Failed to open socket");
942                 return -1;
943         }
944
945         ret  = rte_telemetry_set_socket_nonblock(telemetry->server_fd);
946         if (ret < 0) {
947                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
948                 goto close_socket;
949         }
950
951         addr.sun_family = AF_UNIX;
952         rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
953         strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
954         unlink(socket_path);
955
956         if (bind(telemetry->server_fd, (struct sockaddr *)&addr,
957                 sizeof(addr)) < 0) {
958                 TELEMETRY_LOG_ERR("Socket binding error");
959                 goto close_socket;
960         }
961
962         return 0;
963
964 close_socket:
965         if (close(telemetry->server_fd) < 0) {
966                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
967                 return -EPERM;
968         }
969
970         return -1;
971 }
972
973 int32_t
974 rte_telemetry_init()
975 {
976         int ret;
977         pthread_attr_t attr;
978         const char *telemetry_ctrl_thread = "telemetry";
979
980         if (static_telemetry) {
981                 TELEMETRY_LOG_WARN("TELEMETRY structure already initialised");
982                 return -EALREADY;
983         }
984
985         static_telemetry = calloc(1, sizeof(struct telemetry_impl));
986         if (static_telemetry == NULL) {
987                 TELEMETRY_LOG_ERR("Memory could not be allocated");
988                 return -ENOMEM;
989         }
990
991         static_telemetry->socket_id = rte_socket_id();
992         rte_metrics_init(static_telemetry->socket_id);
993
994         ret = pthread_attr_init(&attr);
995         if (ret != 0) {
996                 TELEMETRY_LOG_ERR("Pthread attribute init failed");
997                 return -EPERM;
998         }
999
1000         ret = rte_telemetry_create_socket(static_telemetry);
1001         if (ret < 0) {
1002                 ret = rte_telemetry_cleanup();
1003                 if (ret < 0)
1004                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1005                 return -EPERM;
1006         }
1007         TAILQ_INIT(&static_telemetry->client_list_head);
1008
1009         ret = rte_ctrl_thread_create(&static_telemetry->thread_id,
1010                 telemetry_ctrl_thread, &attr, rte_telemetry_run_thread_func,
1011                 (void *)static_telemetry);
1012         static_telemetry->thread_status = 1;
1013
1014         if (ret < 0) {
1015                 ret = rte_telemetry_cleanup();
1016                 if (ret < 0)
1017                         TELEMETRY_LOG_ERR("TELEMETRY cleanup failed");
1018                 return -EPERM;
1019         }
1020
1021         return 0;
1022 }
1023
1024 static int32_t
1025 rte_telemetry_client_cleanup(struct telemetry_client *client)
1026 {
1027         int ret;
1028
1029         ret = close(client->fd);
1030         free(client->file_path);
1031         free(client);
1032
1033         if (ret < 0) {
1034                 TELEMETRY_LOG_ERR("Close client socket failed");
1035                 return -EPERM;
1036         }
1037
1038         return 0;
1039 }
1040
1041 int32_t
1042 rte_telemetry_cleanup(void)
1043 {
1044         int ret;
1045         struct telemetry_impl *telemetry = static_telemetry;
1046         telemetry_client *client, *temp_client;
1047
1048         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1049                 temp_client) {
1050                 TAILQ_REMOVE(&telemetry->client_list_head, client, client_list);
1051                 ret = rte_telemetry_client_cleanup(client);
1052                 if (ret < 0) {
1053                         TELEMETRY_LOG_ERR("Client cleanup failed");
1054                         return -EPERM;
1055                 }
1056         }
1057
1058         ret = close(telemetry->server_fd);
1059         if (ret < 0) {
1060                 TELEMETRY_LOG_ERR("Close TELEMETRY socket failed");
1061                 free(telemetry);
1062                 return -EPERM;
1063         }
1064
1065         telemetry->thread_status = 0;
1066         pthread_join(telemetry->thread_id, NULL);
1067         free(telemetry);
1068         static_telemetry = NULL;
1069
1070         return 0;
1071 }
1072
1073 int32_t
1074 rte_telemetry_unregister_client(struct telemetry_impl *telemetry,
1075         const char *client_path)
1076 {
1077         int ret;
1078         telemetry_client *client, *temp_client;
1079
1080         if (telemetry == NULL) {
1081                 TELEMETRY_LOG_WARN("TELEMETRY is not initialised");
1082                 return -ENODEV;
1083         }
1084
1085         if (client_path == NULL) {
1086                 TELEMETRY_LOG_ERR("Invalid client path");
1087                 goto einval_fail;
1088         }
1089
1090         if (TAILQ_EMPTY(&telemetry->client_list_head)) {
1091                 TELEMETRY_LOG_ERR("There are no clients currently registered");
1092                 return -EPERM;
1093         }
1094
1095         TAILQ_FOREACH_SAFE(client, &telemetry->client_list_head, client_list,
1096                         temp_client) {
1097                 if (strcmp(client_path, client->file_path) == 0) {
1098                         TAILQ_REMOVE(&telemetry->client_list_head, client,
1099                                 client_list);
1100                         ret = rte_telemetry_client_cleanup(client);
1101
1102                         if (ret < 0) {
1103                                 TELEMETRY_LOG_ERR("Client cleanup failed");
1104                                 return -EPERM;
1105                         }
1106
1107                         return 0;
1108                 }
1109         }
1110
1111         TELEMETRY_LOG_WARN("Couldn't find client, possibly not registered yet.");
1112         return -1;
1113
1114 einval_fail:
1115         ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
1116         if (ret < 0)
1117                 TELEMETRY_LOG_ERR("Could not send error");
1118         return -EINVAL;
1119 }
1120
1121 int32_t
1122 rte_telemetry_register_client(struct telemetry_impl *telemetry,
1123         const char *client_path)
1124 {
1125         int ret, fd;
1126         struct sockaddr_un addrs;
1127
1128         if (telemetry == NULL) {
1129                 TELEMETRY_LOG_ERR("Could not initialize TELEMETRY API");
1130                 return -ENODEV;
1131         }
1132
1133         if (client_path == NULL) {
1134                 TELEMETRY_LOG_ERR("Invalid client path");
1135                 return -EINVAL;
1136         }
1137
1138         telemetry_client *client;
1139         TAILQ_FOREACH(client, &telemetry->client_list_head, client_list) {
1140                 if (strcmp(client_path, client->file_path) == 0) {
1141                         TELEMETRY_LOG_WARN("'%s' already registered",
1142                                         client_path);
1143                         return -EINVAL;
1144                 }
1145         }
1146
1147         fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1148         if (fd == -1) {
1149                 TELEMETRY_LOG_ERR("Client socket error");
1150                 return -EACCES;
1151         }
1152
1153         ret = rte_telemetry_set_socket_nonblock(fd);
1154         if (ret < 0) {
1155                 TELEMETRY_LOG_ERR("Could not set socket to NONBLOCK");
1156                 return -EPERM;
1157         }
1158
1159         addrs.sun_family = AF_UNIX;
1160         strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1161         telemetry_client *new_client = malloc(sizeof(telemetry_client));
1162         new_client->file_path = strdup(client_path);
1163         new_client->fd = fd;
1164
1165         if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
1166                 TELEMETRY_LOG_ERR("TELEMETRY client connect to %s didn't work",
1167                                 client_path);
1168                 ret = rte_telemetry_client_cleanup(new_client);
1169                 if (ret < 0) {
1170                         TELEMETRY_LOG_ERR("Client cleanup failed");
1171                         return -EPERM;
1172                 }
1173                 return -EINVAL;
1174         }
1175
1176         TAILQ_INSERT_HEAD(&telemetry->client_list_head, new_client, client_list);
1177
1178         return 0;
1179 }
1180
1181 int32_t
1182 rte_telemetry_parse_client_message(struct telemetry_impl *telemetry, char *buf)
1183 {
1184         int ret, action_int;
1185         json_error_t error;
1186         json_t *root = json_loads(buf, 0, &error);
1187
1188         if (root == NULL) {
1189                 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
1190                                 error.text);
1191                 goto fail;
1192         } else if (!json_is_object(root)) {
1193                 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
1194                 goto fail;
1195         }
1196
1197         json_t *action = json_object_get(root, "action");
1198         if (action == NULL) {
1199                 TELEMETRY_LOG_WARN("Request does not have action field");
1200                 goto fail;
1201         } else if (!json_is_integer(action)) {
1202                 TELEMETRY_LOG_WARN("Action value is not an integer");
1203                 goto fail;
1204         }
1205
1206         json_t *command = json_object_get(root, "command");
1207         if (command == NULL) {
1208                 TELEMETRY_LOG_WARN("Request does not have command field");
1209                 goto fail;
1210         } else if (!json_is_string(command)) {
1211                 TELEMETRY_LOG_WARN("Command value is not a string");
1212                 goto fail;
1213         }
1214
1215         action_int = json_integer_value(action);
1216         if (action_int != ACTION_POST) {
1217                 TELEMETRY_LOG_WARN("Invalid action code");
1218                 goto fail;
1219         }
1220
1221         if (strcmp(json_string_value(command), "clients") != 0) {
1222                 TELEMETRY_LOG_WARN("Invalid command");
1223                 goto fail;
1224         }
1225
1226         json_t *data = json_object_get(root, "data");
1227         if (data == NULL) {
1228                 TELEMETRY_LOG_WARN("Request does not have data field");
1229                 goto fail;
1230         }
1231
1232         json_t *client_path = json_object_get(data, "client_path");
1233         if (client_path == NULL) {
1234                 TELEMETRY_LOG_WARN("Request does not have client_path field");
1235                 goto fail;
1236         }
1237
1238         if (!json_is_string(client_path)) {
1239                 TELEMETRY_LOG_WARN("Client_path value is not a string");
1240                 goto fail;
1241         }
1242
1243         ret = rte_telemetry_register_client(telemetry,
1244                         json_string_value(client_path));
1245         if (ret < 0) {
1246                 TELEMETRY_LOG_ERR("Could not register client");
1247                 telemetry->register_fail_count++;
1248                 goto fail;
1249         }
1250
1251         return 0;
1252
1253 fail:
1254         TELEMETRY_LOG_WARN("Client attempted to register with invalid message");
1255         json_decref(root);
1256         return -1;
1257 }
1258
1259 int32_t
1260 rte_telemetry_dummy_client_socket(const char *valid_client_path)
1261 {
1262         int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1263         struct sockaddr_un addr = {0};
1264
1265         if (sockfd < 0) {
1266                 TELEMETRY_LOG_ERR("Test socket creation failure");
1267                 return -1;
1268         }
1269
1270         addr.sun_family = AF_UNIX;
1271         strlcpy(addr.sun_path, valid_client_path, sizeof(addr.sun_path));
1272         unlink(valid_client_path);
1273
1274         if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1275                 TELEMETRY_LOG_ERR("Test socket binding failure");
1276                 return -1;
1277         }
1278
1279         if (listen(sockfd, 1) < 0) {
1280                 TELEMETRY_LOG_ERR("Listen failure");
1281                 return -1;
1282         }
1283
1284         return sockfd;
1285 }
1286
1287 int32_t
1288 rte_telemetry_selftest(void)
1289 {
1290         const char *invalid_client_path = SELFTEST_INVALID_CLIENT;
1291         const char *valid_client_path = SELFTEST_VALID_CLIENT;
1292         int ret, sockfd;
1293
1294         TELEMETRY_LOG_INFO("Selftest");
1295
1296         ret = rte_telemetry_init();
1297         if (ret < 0) {
1298                 TELEMETRY_LOG_ERR("Valid initialisation test failed");
1299                 return -1;
1300         }
1301
1302         TELEMETRY_LOG_INFO("Success - Valid initialisation test passed");
1303
1304         ret = rte_telemetry_init();
1305         if (ret != -EALREADY) {
1306                 TELEMETRY_LOG_ERR("Invalid initialisation test failed");
1307                 return -1;
1308         }
1309
1310         TELEMETRY_LOG_INFO("Success - Invalid initialisation test passed");
1311
1312         ret = rte_telemetry_unregister_client(static_telemetry,
1313                         invalid_client_path);
1314         if (ret != -EPERM) {
1315                 TELEMETRY_LOG_ERR("Invalid unregister test failed");
1316                 return -1;
1317         }
1318
1319         TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1320
1321         sockfd = rte_telemetry_dummy_client_socket(valid_client_path);
1322         if (sockfd < 0) {
1323                 TELEMETRY_LOG_ERR("Test socket creation failed");
1324                 return -1;
1325         }
1326
1327         ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1328         if (ret != 0) {
1329                 TELEMETRY_LOG_ERR("Valid register test failed: %i", ret);
1330                 return -1;
1331         }
1332
1333         accept(sockfd, NULL, NULL);
1334         TELEMETRY_LOG_INFO("Success - Valid register test passed");
1335
1336         ret = rte_telemetry_register_client(static_telemetry, valid_client_path);
1337         if (ret != -EINVAL) {
1338                 TELEMETRY_LOG_ERR("Invalid register test failed: %i", ret);
1339                 return -1;
1340         }
1341
1342         TELEMETRY_LOG_INFO("Success - Invalid register test passed");
1343
1344         ret = rte_telemetry_unregister_client(static_telemetry,
1345                 invalid_client_path);
1346         if (ret != -1) {
1347                 TELEMETRY_LOG_ERR("Invalid unregister test failed: %i", ret);
1348                 return -1;
1349         }
1350
1351         TELEMETRY_LOG_INFO("Success - Invalid unregister test passed");
1352
1353         ret = rte_telemetry_unregister_client(static_telemetry, valid_client_path);
1354         if (ret != 0) {
1355                 TELEMETRY_LOG_ERR("Valid unregister test failed: %i", ret);
1356                 return -1;
1357         }
1358
1359         TELEMETRY_LOG_INFO("Success - Valid unregister test passed");
1360
1361         ret = rte_telemetry_cleanup();
1362         if (ret < 0) {
1363                 TELEMETRY_LOG_ERR("Cleanup test failed");
1364                 return -1;
1365         }
1366
1367         TELEMETRY_LOG_INFO("Success - Valid cleanup test passed");
1368
1369         return 0;
1370 }
1371
1372 int32_t
1373 rte_telemetry_socket_messaging_testing(int index, int socket)
1374 {
1375         struct telemetry_impl *telemetry = calloc(1, sizeof(telemetry_impl));
1376         int fd, bad_send_fd, send_fd, bad_fd, bad_recv_fd, recv_fd, ret;
1377
1378         if (telemetry == NULL) {
1379                 TELEMETRY_LOG_ERR("Could not initialize Telemetry API");
1380                 return -1;
1381         }
1382
1383         telemetry->server_fd = socket;
1384         telemetry->reg_index[0] = index;
1385         TELEMETRY_LOG_INFO("Beginning Telemetry socket message Selftest");
1386         rte_telemetry_socket_test_setup(telemetry, &send_fd, &recv_fd);
1387         TELEMETRY_LOG_INFO("Register valid client test");
1388
1389         ret = rte_telemetry_socket_register_test(telemetry, &fd, send_fd,
1390                 recv_fd);
1391         if (ret < 0) {
1392                 TELEMETRY_LOG_ERR("Register valid client test failed!");
1393                 free(telemetry);
1394                 return -1;
1395         }
1396
1397         TELEMETRY_LOG_INFO("Success - Register valid client test passed!");
1398
1399         TELEMETRY_LOG_INFO("Register invalid/same client test");
1400         ret = rte_telemetry_socket_test_setup(telemetry, &bad_send_fd,
1401                 &bad_recv_fd);
1402         ret = rte_telemetry_socket_register_test(telemetry, &bad_fd,
1403                 bad_send_fd, bad_recv_fd);
1404         if (!ret) {
1405                 TELEMETRY_LOG_ERR("Register invalid/same client test failed!");
1406                 free(telemetry);
1407                 return -1;
1408         }
1409
1410         TELEMETRY_LOG_INFO("Success - Register invalid/same client test passed!");
1411
1412         ret = rte_telemetry_json_socket_message_test(telemetry, fd);
1413         if (ret < 0) {
1414                 free(telemetry);
1415                 return -1;
1416         }
1417
1418         free(telemetry);
1419         return 0;
1420 }
1421
1422 int32_t
1423 rte_telemetry_socket_register_test(struct telemetry_impl *telemetry, int *fd,
1424         int send_fd, int recv_fd)
1425 {
1426         int ret;
1427         char good_req_string[BUF_SIZE];
1428
1429         snprintf(good_req_string, sizeof(good_req_string),
1430         "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\""
1431                 ":\"%s\"}}", SOCKET_TEST_CLIENT_PATH);
1432
1433         listen(recv_fd, 1);
1434
1435         ret = send(send_fd, good_req_string, strlen(good_req_string), 0);
1436         if (ret < 0) {
1437                 TELEMETRY_LOG_ERR("Could not send message over socket");
1438                 return -1;
1439         }
1440
1441         rte_telemetry_run(telemetry);
1442
1443         if (telemetry->register_fail_count != 0)
1444                 return -1;
1445
1446         *fd = accept(recv_fd, NULL, NULL);
1447
1448         return 0;
1449 }
1450
1451 int32_t
1452 rte_telemetry_socket_test_setup(struct telemetry_impl *telemetry, int *send_fd,
1453         int *recv_fd)
1454 {
1455         int ret;
1456         const char *client_path = SOCKET_TEST_CLIENT_PATH;
1457         char socket_path[BUF_SIZE];
1458         struct sockaddr_un addr = {0};
1459         struct sockaddr_un addrs = {0};
1460         *send_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1461         *recv_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1462
1463         listen(telemetry->server_fd, 5);
1464         addr.sun_family = AF_UNIX;
1465         rte_telemetry_get_runtime_dir(socket_path, sizeof(socket_path));
1466         strlcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
1467
1468         ret = connect(*send_fd, (struct sockaddr *) &addr, sizeof(addr));
1469         if (ret < 0) {
1470                 TELEMETRY_LOG_ERR("Could not connect socket");
1471                 return -1;
1472         }
1473
1474         telemetry->accept_fd = accept(telemetry->server_fd, NULL, NULL);
1475
1476         addrs.sun_family = AF_UNIX;
1477         strlcpy(addrs.sun_path, client_path, sizeof(addrs.sun_path));
1478         unlink(client_path);
1479
1480         ret = bind(*recv_fd, (struct sockaddr *)&addrs, sizeof(addrs));
1481         if (ret < 0) {
1482                 TELEMETRY_LOG_ERR("Could not bind socket");
1483                 return -1;
1484         }
1485
1486         return 0;
1487 }
1488
1489 static int32_t
1490 rte_telemetry_stat_parse(char *buf, struct json_data *json_data_struct)
1491 {
1492         json_error_t error;
1493         json_t *root = json_loads(buf, 0, &error);
1494         int arraylen, i;
1495         json_t *status, *dataArray, *port, *stats, *name, *value, *dataArrayObj,
1496                *statsArrayObj;
1497
1498         stats = NULL;
1499         port = NULL;
1500         name = NULL;
1501
1502         if (buf == NULL) {
1503                 TELEMETRY_LOG_ERR("JSON message is NULL");
1504                 return -EINVAL;
1505         }
1506
1507         if (root == NULL) {
1508                 TELEMETRY_LOG_ERR("Could not load JSON object from data passed in : %s",
1509                                 error.text);
1510                 return -EPERM;
1511         } else if (!json_is_object(root)) {
1512                 TELEMETRY_LOG_ERR("JSON Request is not a JSON object");
1513                 json_decref(root);
1514                 return -EINVAL;
1515         }
1516
1517         status = json_object_get(root, "status_code");
1518         if (!status) {
1519                 TELEMETRY_LOG_ERR("Request does not have status field");
1520                 return -EINVAL;
1521         } else if (!json_is_string(status)) {
1522                 TELEMETRY_LOG_ERR("Status value is not a string");
1523                 return -EINVAL;
1524         }
1525
1526         json_data_struct->status_code = strdup(json_string_value(status));
1527
1528         dataArray = json_object_get(root, "data");
1529         if (dataArray == NULL) {
1530                 TELEMETRY_LOG_ERR("Request does not have data field");
1531                 return -EINVAL;
1532         }
1533
1534         arraylen = json_array_size(dataArray);
1535         if (arraylen == 0) {
1536                 json_data_struct->data = "null";
1537                 return -EINVAL;
1538         }
1539
1540         for (i = 0; i < arraylen; i++) {
1541                 dataArrayObj = json_array_get(dataArray, i);
1542                 port = json_object_get(dataArrayObj, "port");
1543                 stats = json_object_get(dataArrayObj, "stats");
1544         }
1545
1546         if (port == NULL) {
1547                 TELEMETRY_LOG_ERR("Request does not have port field");
1548                 return -EINVAL;
1549         }
1550
1551         if (!json_is_integer(port)) {
1552                 TELEMETRY_LOG_ERR("Port value is not an integer");
1553                 return -EINVAL;
1554         }
1555
1556         json_data_struct->port = json_integer_value(port);
1557
1558         if (stats == NULL) {
1559                 TELEMETRY_LOG_ERR("Request does not have stats field");
1560                 return -EINVAL;
1561         }
1562
1563         arraylen = json_array_size(stats);
1564         for (i = 0; i < arraylen; i++) {
1565                 statsArrayObj = json_array_get(stats, i);
1566                 name = json_object_get(statsArrayObj, "name");
1567                 value = json_object_get(statsArrayObj, "value");
1568         }
1569
1570         if (name == NULL) {
1571                 TELEMETRY_LOG_ERR("Request does not have name field");
1572                 return -EINVAL;
1573         }
1574
1575         if (!json_is_string(name)) {
1576                 TELEMETRY_LOG_ERR("Stat name value is not a string");
1577                 return -EINVAL;
1578         }
1579
1580         json_data_struct->stat_name = strdup(json_string_value(name));
1581
1582         if (value == NULL) {
1583                 TELEMETRY_LOG_ERR("Request does not have value field");
1584                 return -EINVAL;
1585         }
1586
1587         if (!json_is_integer(value)) {
1588                 TELEMETRY_LOG_ERR("Stat value is not an integer");
1589                 return -EINVAL;
1590         }
1591
1592         json_data_struct->stat_value = json_integer_value(value);
1593
1594         return 0;
1595 }
1596
1597 static void
1598 rte_telemetry_free_test_data(struct json_data *data)
1599 {
1600         free(data->status_code);
1601         free(data->stat_name);
1602         free(data);
1603 }
1604
1605 int32_t
1606 rte_telemetry_valid_json_test(struct telemetry_impl *telemetry, int fd)
1607 {
1608         int ret;
1609         int port = 0;
1610         int value = 0;
1611         int fail_count = 0;
1612         int buffer_read = 0;
1613         char buf[BUF_SIZE];
1614         struct json_data *data_struct;
1615         errno = 0;
1616         const char *status = "Status OK: 200";
1617         const char *name = "rx_good_packets";
1618         const char *valid_json_message = "{\"action\":0,\"command\":"
1619         "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1620         ":[0],\"stats\":[\"rx_good_packets\"]}}";
1621
1622         ret = send(fd, valid_json_message, strlen(valid_json_message), 0);
1623         if (ret < 0) {
1624                 TELEMETRY_LOG_ERR("Could not send message over socket");
1625                 return -1;
1626         }
1627
1628         rte_telemetry_run(telemetry);
1629         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1630
1631         if (buffer_read == -1) {
1632                 TELEMETRY_LOG_ERR("Read error");
1633                 return -1;
1634         }
1635
1636         buf[buffer_read] = '\0';
1637         data_struct = calloc(1, sizeof(struct json_data));
1638         ret = rte_telemetry_stat_parse(buf, data_struct);
1639
1640         if (ret < 0) {
1641                 TELEMETRY_LOG_ERR("Could not parse stats");
1642                 fail_count++;
1643         }
1644
1645         if (strcmp(data_struct->status_code, status) != 0) {
1646                 TELEMETRY_LOG_ERR("Status code is invalid");
1647                 fail_count++;
1648         }
1649
1650         if (data_struct->port != port) {
1651                 TELEMETRY_LOG_ERR("Port is invalid");
1652                 fail_count++;
1653         }
1654
1655         if (strcmp(data_struct->stat_name, name) != 0) {
1656                 TELEMETRY_LOG_ERR("Stat name is invalid");
1657                 fail_count++;
1658         }
1659
1660         if (data_struct->stat_value != value) {
1661                 TELEMETRY_LOG_ERR("Stat value is invalid");
1662                 fail_count++;
1663         }
1664
1665         rte_telemetry_free_test_data(data_struct);
1666         if (fail_count > 0)
1667                 return -1;
1668
1669         TELEMETRY_LOG_INFO("Success - Passed valid JSON message test passed");
1670
1671         return 0;
1672 }
1673
1674 int32_t
1675 rte_telemetry_invalid_json_test(struct telemetry_impl *telemetry, int fd)
1676 {
1677         int ret;
1678         char buf[BUF_SIZE];
1679         int fail_count = 0;
1680         const char *invalid_json = "{]";
1681         const char *status = "Status Error: Unknown";
1682         const char *data = "null";
1683         struct json_data *data_struct;
1684         int buffer_read = 0;
1685         errno = 0;
1686
1687         ret = send(fd, invalid_json, strlen(invalid_json), 0);
1688         if (ret < 0) {
1689                 TELEMETRY_LOG_ERR("Could not send message over socket");
1690                 return -1;
1691         }
1692
1693         rte_telemetry_run(telemetry);
1694         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1695
1696         if (buffer_read == -1) {
1697                 TELEMETRY_LOG_ERR("Read error");
1698                 return -1;
1699         }
1700
1701         buf[buffer_read] = '\0';
1702
1703         data_struct = calloc(1, sizeof(struct json_data));
1704         ret = rte_telemetry_stat_parse(buf, data_struct);
1705
1706         if (ret < 0)
1707                 TELEMETRY_LOG_ERR("Could not parse stats");
1708
1709         if (strcmp(data_struct->status_code, status) != 0) {
1710                 TELEMETRY_LOG_ERR("Status code is invalid");
1711                 fail_count++;
1712         }
1713
1714         if (strcmp(data_struct->data, data) != 0) {
1715                 TELEMETRY_LOG_ERR("Data status is invalid");
1716                 fail_count++;
1717         }
1718
1719         rte_telemetry_free_test_data(data_struct);
1720         if (fail_count > 0)
1721                 return -1;
1722
1723         TELEMETRY_LOG_INFO("Success - Passed invalid JSON message test");
1724
1725         return 0;
1726 }
1727
1728 int32_t
1729 rte_telemetry_json_contents_test(struct telemetry_impl *telemetry, int fd)
1730 {
1731         int ret;
1732         char buf[BUF_SIZE];
1733         int fail_count = 0;
1734         char *status = "Status Error: Invalid Argument 404";
1735         char *data = "null";
1736         struct json_data *data_struct;
1737         const char *invalid_contents = "{\"action\":0,\"command\":"
1738         "\"ports_stats_values_by_name\",\"data\":{\"ports\""
1739         ":[0],\"stats\":[\"some_invalid_param\","
1740         "\"another_invalid_param\"]}}";
1741         int buffer_read = 0;
1742         errno = 0;
1743
1744         ret = send(fd, invalid_contents, strlen(invalid_contents), 0);
1745         if (ret < 0) {
1746                 TELEMETRY_LOG_ERR("Could not send message over socket");
1747                 return -1;
1748         }
1749
1750         rte_telemetry_run(telemetry);
1751         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1752
1753         if (buffer_read == -1) {
1754                 TELEMETRY_LOG_ERR("Read error");
1755                 return -1;
1756         }
1757
1758         buf[buffer_read] = '\0';
1759         data_struct = calloc(1, sizeof(struct json_data));
1760         ret = rte_telemetry_stat_parse(buf, data_struct);
1761
1762         if (ret < 0)
1763                 TELEMETRY_LOG_ERR("Could not parse stats");
1764
1765         if (strcmp(data_struct->status_code, status) != 0) {
1766                 TELEMETRY_LOG_ERR("Status code is invalid");
1767                 fail_count++;
1768         }
1769
1770         if (strcmp(data_struct->data, data) != 0) {
1771                 TELEMETRY_LOG_ERR("Data status is invalid");
1772                 fail_count++;
1773         }
1774
1775         rte_telemetry_free_test_data(data_struct);
1776         if (fail_count > 0)
1777                 return -1;
1778
1779         TELEMETRY_LOG_INFO("Success - Passed invalid JSON content test");
1780
1781         return 0;
1782 }
1783
1784 int32_t
1785 rte_telemetry_json_empty_test(struct telemetry_impl *telemetry, int fd)
1786 {
1787         int ret;
1788         char buf[BUF_SIZE];
1789         int fail_count = 0;
1790         const char *status = "Status Error: Invalid Argument 404";
1791         char *data = "null";
1792         struct json_data *data_struct;
1793         const char *empty_json  = "{}";
1794         int buffer_read = 0;
1795         errno = 0;
1796
1797         ret = (send(fd, empty_json, strlen(empty_json), 0));
1798         if (ret < 0) {
1799                 TELEMETRY_LOG_ERR("Could not send message over socket");
1800                 return -1;
1801         }
1802
1803         rte_telemetry_run(telemetry);
1804         buffer_read = recv(fd, buf, BUF_SIZE-1, 0);
1805
1806         if (buffer_read == -1) {
1807                 TELEMETRY_LOG_ERR("Read error");
1808                 return -1;
1809         }
1810
1811         buf[buffer_read] = '\0';
1812         data_struct = calloc(1, sizeof(struct json_data));
1813         ret = rte_telemetry_stat_parse(buf, data_struct);
1814
1815         if (ret < 0)
1816                 TELEMETRY_LOG_ERR("Could not parse stats");
1817
1818         if (strcmp(data_struct->status_code, status) != 0) {
1819                 TELEMETRY_LOG_ERR("Status code is invalid");
1820                 fail_count++;
1821         }
1822
1823         if (strcmp(data_struct->data, data) != 0) {
1824                 TELEMETRY_LOG_ERR("Data status is invalid");
1825                 fail_count++;
1826         }
1827
1828         rte_telemetry_free_test_data(data_struct);
1829
1830         if (fail_count > 0)
1831                 return -1;
1832
1833         TELEMETRY_LOG_INFO("Success - Passed JSON empty message test");
1834
1835         return 0;
1836 }
1837
1838 int32_t
1839 rte_telemetry_json_socket_message_test(struct telemetry_impl *telemetry, int fd)
1840 {
1841         uint16_t i;
1842         int ret, fail_count;
1843
1844         fail_count = 0;
1845         struct telemetry_message_test socket_json_tests[] = {
1846                 {.test_name = "Invalid JSON test",
1847                         .test_func_ptr = rte_telemetry_invalid_json_test},
1848                 {.test_name = "Valid JSON test",
1849                         .test_func_ptr = rte_telemetry_valid_json_test},
1850                 {.test_name = "JSON contents test",
1851                         .test_func_ptr = rte_telemetry_json_contents_test},
1852                 {.test_name = "JSON empty tests",
1853                         .test_func_ptr = rte_telemetry_json_empty_test}
1854                 };
1855
1856 #define NUM_TESTS RTE_DIM(socket_json_tests)
1857
1858         for (i = 0; i < NUM_TESTS; i++) {
1859                 TELEMETRY_LOG_INFO("%s", socket_json_tests[i].test_name);
1860                 ret = (socket_json_tests[i].test_func_ptr)
1861                         (telemetry, fd);
1862                 if (ret < 0) {
1863                         TELEMETRY_LOG_ERR("%s failed",
1864                                         socket_json_tests[i].test_name);
1865                         fail_count++;
1866                 }
1867         }
1868
1869         if (fail_count > 0) {
1870                 TELEMETRY_LOG_ERR("Failed %i JSON socket message test(s)",
1871                                 fail_count);
1872                 return -1;
1873         }
1874
1875         TELEMETRY_LOG_INFO("Success - All JSON tests passed");
1876
1877         return 0;
1878 }
1879
1880 int telemetry_log_level;
1881
1882 static struct rte_option option = {
1883         .name = "telemetry",
1884         .usage = "Enable telemetry backend",
1885         .cb = &rte_telemetry_init,
1886         .enabled = 0
1887 };
1888
1889 RTE_INIT(rte_telemetry_register)
1890 {
1891         telemetry_log_level = rte_log_register("lib.telemetry");
1892         if (telemetry_log_level >= 0)
1893                 rte_log_set_level(telemetry_log_level, RTE_LOG_ERR);
1894
1895         rte_option_register(&option);
1896 }