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