1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
11 #include <rte_metrics.h>
12 #include <rte_common.h>
13 #include <rte_metrics_telemetry.h>
15 #include "rte_telemetry_internal.h"
16 #include "rte_telemetry_parser.h"
18 typedef int (*command_func)(struct telemetry_impl *, int, json_t *);
20 struct rte_telemetry_command {
26 rte_telemetry_command_clients(struct telemetry_impl *telemetry, int action,
31 if (telemetry == NULL) {
32 TELEMETRY_LOG_ERR("Invalid telemetry argument");
36 if (action != ACTION_DELETE) {
37 TELEMETRY_LOG_WARN("Invalid action for this command");
41 if (!json_is_object(data)) {
42 TELEMETRY_LOG_WARN("Invalid data provided for this command");
46 json_t *client_path = json_object_get(data, "client_path");
47 if (!json_is_string(client_path)) {
48 TELEMETRY_LOG_WARN("Command value is not a string");
52 ret = rte_telemetry_unregister_client(telemetry,
53 json_string_value(client_path));
55 TELEMETRY_LOG_ERR("Could not unregister client");
62 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
64 TELEMETRY_LOG_ERR("Could not send error");
69 rte_telemetry_command_ports(struct telemetry_impl *telemetry, int action,
74 if (telemetry == NULL) {
75 TELEMETRY_LOG_ERR("Invalid telemetry argument");
79 if (!json_is_null(data)) {
80 TELEMETRY_LOG_WARN("Data should be NULL JSON object for 'ports' command");
84 if (action != ACTION_GET) {
85 TELEMETRY_LOG_WARN("Invalid action for this command");
92 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
94 TELEMETRY_LOG_ERR("Could not send error");
99 rte_telemetry_command_ports_details(struct telemetry_impl *telemetry,
100 int action, json_t *data)
102 json_t *value, *port_ids_json = json_object_get(data, "ports");
103 uint64_t num_port_ids = json_array_size(port_ids_json);
104 int ret, port_ids[num_port_ids];
105 RTE_SET_USED(port_ids);
108 if (telemetry == NULL) {
109 TELEMETRY_LOG_ERR("Invalid telemetry argument");
113 if (action != ACTION_GET) {
114 TELEMETRY_LOG_WARN("Invalid action for this command");
118 if (!json_is_object(data)) {
119 TELEMETRY_LOG_WARN("Invalid data provided for this command");
123 if (!json_is_array(port_ids_json)) {
124 TELEMETRY_LOG_WARN("Invalid Port ID array");
128 json_array_foreach(port_ids_json, index, value) {
129 if (!json_is_integer(value)) {
130 TELEMETRY_LOG_WARN("Port ID given is invalid");
133 port_ids[index] = json_integer_value(value);
139 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
141 TELEMETRY_LOG_ERR("Could not send error");
146 rte_telemetry_command_port_stats(struct telemetry_impl *telemetry, int action,
151 if (telemetry == NULL) {
152 TELEMETRY_LOG_ERR("Invalid telemetry argument");
156 if (!json_is_null(data)) {
157 TELEMETRY_LOG_WARN("Data should be NULL JSON object for 'port_stats' command");
161 if (action != ACTION_GET) {
162 TELEMETRY_LOG_WARN("Invalid action for this command");
169 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
171 TELEMETRY_LOG_ERR("Could not send error");
176 rte_telemetry_command_ports_all_stat_values(struct telemetry_impl *telemetry,
177 int action, json_t *data)
180 struct telemetry_encode_param ep;
182 memset(&ep, 0, sizeof(ep));
183 if (telemetry == NULL) {
184 TELEMETRY_LOG_ERR("Invalid telemetry argument");
188 if (action != ACTION_GET) {
189 TELEMETRY_LOG_WARN("Invalid action for this command");
190 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
192 TELEMETRY_LOG_ERR("Could not send error");
196 if (json_is_object(data)) {
197 TELEMETRY_LOG_WARN("Invalid data provided for this command");
198 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
200 TELEMETRY_LOG_ERR("Could not send error");
204 ret = metrics_fns->get_port_stats_ids(&ep);
206 TELEMETRY_LOG_ERR("Could not get ports stat values");
207 ret = rte_telemetry_send_error_response(telemetry, ret);
209 TELEMETRY_LOG_ERR("Could not send error");
213 ret = rte_telemetry_send_ports_stats_values(&ep, telemetry);
215 TELEMETRY_LOG_ERR("Sending ports stats values failed");
223 rte_telemetry_command_global_stat_values(struct telemetry_impl *telemetry,
224 int action, json_t *data)
227 struct telemetry_encode_param ep = { .type = GLOBAL_STATS };
229 if (telemetry == NULL) {
230 TELEMETRY_LOG_ERR("Invalid telemetry argument");
234 if (action != ACTION_GET) {
235 TELEMETRY_LOG_WARN("Invalid action for this command");
236 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
238 TELEMETRY_LOG_ERR("Could not send error");
242 if (json_is_object(data)) {
243 TELEMETRY_LOG_WARN("Invalid data provided for this command");
244 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
246 TELEMETRY_LOG_ERR("Could not send error");
250 ret = rte_telemetry_send_global_stats_values(&ep, telemetry);
252 TELEMETRY_LOG_ERR("Sending global stats values failed");
260 rte_telemetry_command_ports_stats_values_by_name(struct telemetry_impl
261 *telemetry, int action, json_t *data)
264 struct telemetry_encode_param ep;
265 if (telemetry == NULL) {
266 TELEMETRY_LOG_ERR("Invalid telemetry argument");
270 if (action != ACTION_GET) {
271 TELEMETRY_LOG_WARN("Invalid action for this command");
272 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
274 TELEMETRY_LOG_ERR("Could not send error");
278 ret = metrics_fns->extract_data(&ep, data);
280 TELEMETRY_LOG_ERR("Extracting JSON data failed");
281 ret = rte_telemetry_send_error_response(telemetry, ret);
283 TELEMETRY_LOG_ERR("Could not send error");
287 ret = rte_telemetry_send_ports_stats_values(&ep, telemetry);
289 TELEMETRY_LOG_ERR("Sending ports stats values failed");
297 rte_telemetry_parse_command(struct telemetry_impl *telemetry, int action,
298 const char *command, json_t *data)
303 if (telemetry == NULL) {
304 TELEMETRY_LOG_ERR("Invalid telemetry argument");
308 struct rte_telemetry_command commands[] = {
311 .fn = &rte_telemetry_command_clients
315 .fn = &rte_telemetry_command_ports
318 .text = "ports_details",
319 .fn = &rte_telemetry_command_ports_details
322 .text = "port_stats",
323 .fn = &rte_telemetry_command_port_stats
326 .text = "ports_stats_values_by_name",
327 .fn = &rte_telemetry_command_ports_stats_values_by_name
330 .text = "ports_all_stat_values",
331 .fn = &rte_telemetry_command_ports_all_stat_values
334 .text = "global_stat_values",
335 .fn = &rte_telemetry_command_global_stat_values
339 const uint32_t num_commands = RTE_DIM(commands);
341 for (i = 0; i < num_commands; i++) {
342 if (strcmp(command, commands[i].text) == 0) {
343 ret = commands[i].fn(telemetry, action, data);
345 TELEMETRY_LOG_ERR("Command Function for %s failed",
353 TELEMETRY_LOG_WARN("\"%s\" command not found", command);
355 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
357 TELEMETRY_LOG_ERR("Could not send error");
363 rte_telemetry_parse(struct telemetry_impl *telemetry, char *socket_rx_data)
367 json_t *root, *action, *command, *data;
369 if (telemetry == NULL) {
370 TELEMETRY_LOG_ERR("Invalid telemetry argument");
374 root = json_loads(socket_rx_data, 0, &error);
376 TELEMETRY_LOG_WARN("Could not load JSON object from data passed in : %s",
378 ret = rte_telemetry_send_error_response(telemetry, -EPERM);
380 TELEMETRY_LOG_ERR("Could not send error");
382 } else if (!json_is_object(root)) {
383 TELEMETRY_LOG_WARN("JSON Request is not a JSON object");
388 action = json_object_get(root, "action");
389 if (action == NULL) {
390 TELEMETRY_LOG_WARN("Request does not have action field");
392 } else if (!json_is_integer(action)) {
393 TELEMETRY_LOG_WARN("Action value is not an integer");
397 command = json_object_get(root, "command");
398 if (command == NULL) {
399 TELEMETRY_LOG_WARN("Request does not have command field");
401 } else if (!json_is_string(command)) {
402 TELEMETRY_LOG_WARN("Command value is not a string");
406 action_int = json_integer_value(action);
407 if (action_int != ACTION_GET && action_int != ACTION_DELETE) {
408 TELEMETRY_LOG_WARN("Invalid action code");
412 const char *command_string = json_string_value(command);
413 data = json_object_get(root, "data");
415 TELEMETRY_LOG_WARN("Request does not have data field");
419 ret = rte_telemetry_parse_command(telemetry, action_int, command_string,
422 TELEMETRY_LOG_WARN("Could not parse command");
429 ret = rte_telemetry_send_error_response(telemetry, -EINVAL);
431 TELEMETRY_LOG_ERR("Could not send error");