telemetry: introduce backward compatibility
authorCiara Power <ciara.power@intel.com>
Thu, 30 Apr 2020 16:01:32 +0000 (17:01 +0100)
committerThomas Monjalon <thomas@monjalon.net>
Sun, 10 May 2020 22:37:15 +0000 (00:37 +0200)
The new telemetry will now open a socket using the old telemetry path,
to ensure backward compatibility. This is not yet initialised, as it
would clash with the existing telemetry, to be removed in a later patch.
This means that both old and new telemetry socket interfaces are
handled in a common way.

Signed-off-by: Ciara Power <ciara.power@intel.com>
Reviewed-by: Keith Wiles <keith.wiles@intel.com>
lib/librte_metrics/rte_metrics_telemetry.c
lib/librte_telemetry/Makefile
lib/librte_telemetry/meson.build
lib/librte_telemetry/rte_telemetry_legacy.h [new file with mode: 0644]
lib/librte_telemetry/rte_telemetry_version.map
lib/librte_telemetry/telemetry.c
lib/librte_telemetry/telemetry_legacy.c [new file with mode: 0644]

index a207a19..164256c 100644 (file)
@@ -8,6 +8,7 @@
 #include <rte_string_fns.h>
 #ifdef RTE_LIBRTE_TELEMETRY
 #include <rte_telemetry_internal.h>
+#include <rte_telemetry_legacy.h>
 #endif
 
 #include "rte_metrics.h"
@@ -419,6 +420,112 @@ rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
        return 0;
 }
 
+static int
+rte_metrics_tel_initial_metrics_setup(void)
+{
+       int ret;
+       rte_metrics_init(rte_socket_id());
+
+       if (!tel_met_data.metrics_register_done) {
+               ret = rte_metrics_tel_reg_all_ethdev(
+                       &tel_met_data.metrics_register_done,
+                       tel_met_data.reg_index);
+               if (ret < 0)
+                       return ret;
+       }
+       return 0;
+}
+
+static int
+handle_ports_all_stats_values(const char *cmd __rte_unused,
+               const char *params __rte_unused,
+               char *buffer, int buf_len)
+{
+       struct telemetry_encode_param ep;
+       int ret, used = 0;
+       char *json_buffer = NULL;
+
+       ret = rte_metrics_tel_initial_metrics_setup();
+       if (ret < 0)
+               return ret;
+
+       memset(&ep, 0, sizeof(ep));
+       ret = rte_metrics_tel_get_port_stats_ids(&ep);
+       if (ret < 0)
+               return ret;
+
+       ret = rte_metrics_tel_get_ports_stats_json(&ep, tel_met_data.reg_index,
+                       &json_buffer);
+       if (ret < 0)
+               return ret;
+
+       used += strlcpy(buffer, json_buffer, buf_len);
+       return used;
+}
+
+static int
+handle_global_stats_values(const char *cmd __rte_unused,
+               const char *params __rte_unused,
+               char *buffer, int buf_len)
+{
+       char *json_buffer = NULL;
+       struct telemetry_encode_param ep = { .type = GLOBAL_STATS };
+       int ret, used = 0;
+
+       ret = rte_metrics_tel_initial_metrics_setup();
+       if (ret < 0)
+               return ret;
+
+       ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
+       if (ret < 0) {
+               METRICS_LOG_ERR("JSON encode function failed");
+               return ret;
+       }
+       used += strlcpy(buffer, json_buffer, buf_len);
+       return used;
+}
+
+static int
+handle_ports_stats_values_by_name(const char *cmd __rte_unused,
+               const char *params,
+               char *buffer, int buf_len)
+{
+       char *json_buffer = NULL;
+       struct telemetry_encode_param ep;
+       int ret, used = 0;
+       json_t *data;
+       json_error_t error;
+
+       ret = rte_metrics_tel_initial_metrics_setup();
+       if (ret < 0)
+               return ret;
+
+       data = json_loads(params, 0, &error);
+       if (!data) {
+               METRICS_LOG_WARN("Could not load JSON object from data passed in : %s",
+                               error.text);
+               return -EPERM;
+       } else if (!json_is_object(data)) {
+               METRICS_LOG_WARN("JSON Request data is not a JSON object");
+               json_decref(data);
+               return -EINVAL;
+       }
+
+       ret = rte_metrics_tel_extract_data(&ep, data);
+       if (ret < 0) {
+               METRICS_LOG_ERR("Extract data function failed");
+               return ret;
+       }
+
+       ret = rte_metrics_tel_encode_json_format(&ep, &json_buffer);
+       if (ret < 0) {
+               METRICS_LOG_ERR("JSON encode function failed");
+               return ret;
+       }
+       used += strlcpy(buffer, json_buffer, buf_len);
+       return used;
+}
+
 RTE_INIT(metrics_ctor)
 {
 #ifdef RTE_LIBRTE_TELEMETRY
@@ -430,6 +537,12 @@ RTE_INIT(metrics_ctor)
                .extract_data = rte_metrics_tel_extract_data
        };
        rte_telemetry_set_metrics_fns(&fns); /* assign them to telemetry lib */
+       rte_telemetry_legacy_register("ports_all_stat_values", DATA_NOT_REQ,
+                       handle_ports_all_stats_values);
+       rte_telemetry_legacy_register("global_stat_values", DATA_NOT_REQ,
+                       handle_global_stats_values);
+       rte_telemetry_legacy_register("ports_stats_values_by_name", DATA_REQ,
+                       handle_ports_stats_values_by_name);
 #endif
        metrics_log_level = rte_log_register("lib.metrics");
        if (metrics_log_level >= 0)
index 5457d1e..260dce3 100644 (file)
@@ -25,6 +25,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += rte_telemetry_parser.c
 SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += rte_telemetry_parser_test.c
 SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += telemetry.c
 SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += telemetry_data.c
+SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += telemetry_legacy.c
 
 # export include files
 SYMLINK-$(CONFIG_RTE_LIBRTE_TELEMETRY)-include := rte_telemetry.h
index 8092693..5bcef6c 100644 (file)
@@ -4,7 +4,7 @@
 includes = [global_inc]
 
 sources = files('rte_telemetry.c', 'rte_telemetry_parser.c', 'rte_telemetry_parser_test.c',
-       'telemetry.c', 'telemetry_data.c')
+       'telemetry.c', 'telemetry_data.c', 'telemetry_legacy.c')
 headers = files('rte_telemetry.h', 'rte_telemetry_internal.h', 'rte_telemetry_parser.h')
 includes += include_directories('../librte_metrics')
 
diff --git a/lib/librte_telemetry/rte_telemetry_legacy.h b/lib/librte_telemetry/rte_telemetry_legacy.h
new file mode 100644 (file)
index 0000000..c83f9a8
--- /dev/null
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2020 Intel Corporation
+ */
+
+#ifndef _RTE_TELEMETRY_LEGACY_H_
+#define _RTE_TELEMETRY_LEGACY_H_
+
+#include <rte_compat.h>
+#include "rte_telemetry.h"
+
+/**
+ * @internal
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+
+ * @file
+ * RTE Telemetry Legacy
+ *
+ ***/
+
+/**
+ * @internal
+ * Value representing if data is required for the command
+ */
+enum rte_telemetry_legacy_data_req {
+       DATA_NOT_REQ = 0,
+       DATA_REQ
+};
+
+/**
+ * This telemetry callback is used when registering a legacy telemetry command.
+ * It handles getting and formatting stats to be returned to telemetry when
+ * requested. Stats up to buf_len in length are put in the buffer.
+ *
+ * @param cmd
+ * The cmd that was requested by the client.
+ * @param params
+ * Contains data required by the callback function.
+ * @param buffer
+ * A buffer to hold the formatted response.
+ * @param buf_len
+ * Length of the buffer.
+ *
+ * @return
+ * Length of buffer used on success.
+ * @return
+ * Negative integer on error.
+ */
+typedef int (*telemetry_legacy_cb)(const char *cmd, const char *params,
+               char *buffer, int buf_len);
+
+/**
+ * @internal
+ * Counter for the number of registered legacy callbacks
+ */
+extern int num_legacy_callbacks;
+
+/**
+ * @internal
+ * Used for handling data received over the legacy telemetry socket.
+ *
+ * @return
+ * Void.
+ */
+void *
+legacy_client_handler(void *sock_id);
+
+/**
+ * @internal
+ *
+ * Used when registering a command and callback function with
+ * telemetry legacy support.
+ *
+ * @return
+ *  0 on success.
+ * @return
+ *  -EINVAL for invalid parameters failure.
+ *  @return
+ *  -ENOENT if max callbacks limit has been reached.
+ */
+__rte_experimental
+int
+rte_telemetry_legacy_register(const char *cmd,
+               enum rte_telemetry_legacy_data_req data_req,
+               telemetry_legacy_cb fn);
+
+#endif
index d383afc..496c59e 100644 (file)
@@ -12,6 +12,7 @@ EXPERIMENTAL {
        rte_tel_data_string;
        rte_telemetry_cleanup;
        rte_telemetry_init;
+       rte_telemetry_legacy_register;
        rte_telemetry_new_init;
        rte_telemetry_parse;
        rte_telemetry_register_cmd;
index 9f4234c..e4b5e35 100644 (file)
@@ -18,6 +18,7 @@
 #include "rte_telemetry.h"
 #include "telemetry_json.h"
 #include "telemetry_data.h"
+#include "rte_telemetry_legacy.h"
 
 #define MAX_CMD_LEN 56
 #define MAX_HELP_LEN 64
@@ -38,6 +39,7 @@ struct socket {
        handler fn;
 };
 static struct socket v2_socket; /* socket for v2 telemetry */
+static struct socket v1_socket; /* socket for v1 telemetry */
 static char telemetry_log_error[1024]; /* Will contain error on init failure */
 /* list of command callbacks, with one command registered by default */
 static struct cmd_callback callbacks[TELEMETRY_MAX_CALLBACKS];
@@ -297,6 +299,8 @@ unlink_sockets(void)
 {
        if (v2_socket.path[0])
                unlink(v2_socket.path);
+       if (v1_socket.path[0])
+               unlink(v1_socket.path);
 }
 
 static int
@@ -336,6 +340,33 @@ error:
        return -1;
 }
 
+static int __rte_unused /* will be used in future commit */
+telemetry_legacy_init(const char *runtime_dir)
+{
+       pthread_t t_old;
+
+       if (num_legacy_callbacks == 1) {
+               snprintf(telemetry_log_error, sizeof(telemetry_log_error),
+                        "No legacy callbacks, legacy socket not created");
+               return -1;
+       }
+
+       v1_socket.fn = legacy_client_handler;
+       if ((size_t) snprintf(v1_socket.path, sizeof(v1_socket.path),
+                       "%s/telemetry", runtime_dir)
+                       >= sizeof(v1_socket.path)) {
+               snprintf(telemetry_log_error, sizeof(telemetry_log_error),
+                               "Error with socket binding, path too long");
+               return -1;
+       }
+       v1_socket.sock = create_socket(v1_socket.path);
+       if (v1_socket.sock < 0)
+               return -1;
+       pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+
+       return 0;
+}
+
 static int
 telemetry_v2_init(const char *runtime_dir)
 {
diff --git a/lib/librte_telemetry/telemetry_legacy.c b/lib/librte_telemetry/telemetry_legacy.c
new file mode 100644 (file)
index 0000000..8e24eb4
--- /dev/null
@@ -0,0 +1,226 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2020 Intel Corporation
+ */
+
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <pthread.h>
+
+/* we won't link against libbsd, so just always use DPDKs-specific strlcpy */
+#undef RTE_USE_LIBBSD
+#include <rte_string_fns.h>
+#include <rte_common.h>
+#include <rte_spinlock.h>
+
+#include "rte_telemetry_legacy.h"
+
+#define MAX_LEN 128
+#define BUF_SIZE 1024
+#define CLIENTS_UNREG_ACTION "\"action\":2"
+#define CLIENTS_CMD "\"command\":\"clients\""
+#define CLIENTS_DATA "\"data\":{\"client_path\":\""
+#define STATS_ACTION "\"action\":0"
+#define DATA_REQ_LABEL "\"data\":"
+#define TELEMETRY_LEGACY_MAX_CALLBACKS 4
+
+
+static int
+register_client(const char *cmd __rte_unused,
+               const char *params __rte_unused,
+               char *buffer, int buf_len);
+
+struct json_command {
+       char action[MAX_LEN];
+       char cmd[MAX_LEN];
+       char data[MAX_LEN];
+       telemetry_legacy_cb fn;
+
+};
+
+struct json_command callbacks[TELEMETRY_LEGACY_MAX_CALLBACKS] = {
+               {
+                       .action = "\"action\":1",
+                       .cmd = CLIENTS_CMD,
+                       .data = CLIENTS_DATA,
+                       .fn = register_client
+               }
+};
+int num_legacy_callbacks = 1;
+static rte_spinlock_t callback_sl = RTE_SPINLOCK_INITIALIZER;
+
+int
+rte_telemetry_legacy_register(const char *cmd,
+               enum rte_telemetry_legacy_data_req data_req,
+               telemetry_legacy_cb fn)
+{
+       if (fn == NULL)
+               return -EINVAL;
+       if (num_legacy_callbacks >= (int) RTE_DIM(callbacks))
+               return -ENOENT;
+
+       rte_spinlock_lock(&callback_sl);
+       strlcpy(callbacks[num_legacy_callbacks].action, STATS_ACTION, MAX_LEN);
+       snprintf(callbacks[num_legacy_callbacks].cmd, MAX_LEN,
+                       "\"command\":\"%s\"", cmd);
+       snprintf(callbacks[num_legacy_callbacks].data, MAX_LEN,
+                       data_req ? "%s{\"" : "%snull",
+                       DATA_REQ_LABEL);
+       callbacks[num_legacy_callbacks].fn = fn;
+       num_legacy_callbacks++;
+       rte_spinlock_unlock(&callback_sl);
+
+       return 0;
+}
+
+static int
+register_client(const char *cmd __rte_unused, const char *params,
+               char *buffer __rte_unused, int buf_len __rte_unused)
+{
+       pthread_t th;
+       char data[BUF_SIZE];
+       int fd;
+       struct sockaddr_un addrs;
+
+       strlcpy(data, strchr(params, ':'), sizeof(data));
+       memcpy(data, &data[strlen(":\"")], strlen(data));
+       *strchr(data, '\"') = 0;
+
+       fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+       addrs.sun_family = AF_UNIX;
+       strlcpy(addrs.sun_path, data, sizeof(addrs.sun_path));
+
+       if (connect(fd, (struct sockaddr *)&addrs, sizeof(addrs)) == -1) {
+               perror("\nClient connection error\n");
+               return -1;
+       }
+       pthread_create(&th, NULL, &legacy_client_handler,
+                       (void *)(uintptr_t)fd);
+       return 0;
+}
+
+static int
+send_error_response(int s, int err)
+{
+       const char *desc;
+       char out_buf[100000];
+
+       switch (err) {
+       case -ENOMEM:
+               desc = "Memory Allocation Error";
+               break;
+       case -EINVAL:
+               desc = "Invalid Argument 404";
+               break;
+       case -EPERM:
+               desc = "Unknown";
+               break;
+       default:
+               /* Default case keeps behaviour of Telemetry library */
+               printf("\nInvalid error type: %d\n", err);
+               return -EINVAL;
+       }
+       int used = snprintf(out_buf, sizeof(out_buf), "{\"status_code\": "
+                       "\"Status Error: %s\", \"data\": null}", desc);
+       if (write(s, out_buf, used) < 0) {
+               perror("Error writing to socket");
+               return -1;
+       }
+       return 0;
+}
+
+static void
+perform_command(telemetry_legacy_cb fn, const char *param, int s)
+{
+       char out_buf[100000];
+       int ret, used = 0;
+
+       ret = fn("", param, out_buf, sizeof(out_buf));
+       if (ret < 0) {
+               ret = send_error_response(s, ret);
+               if (ret < 0)
+                       printf("\nCould not send error response\n");
+               return;
+       }
+       used += ret;
+       if (write(s, out_buf, used) < 0)
+               perror("Error writing to socket");
+}
+
+static int
+parse_client_request(char *buffer, int buf_len, int s)
+{
+       int i;
+       char *data = buffer + buf_len;
+       telemetry_legacy_cb fn = NULL;
+       const char *valid_sep = ",}";
+       if (buffer[0] != '{' || buffer[buf_len - 1] != '}')
+               return -EPERM;
+
+       if (strstr(buffer, CLIENTS_UNREG_ACTION) && strstr(buffer, CLIENTS_CMD)
+                       && strstr(buffer, CLIENTS_DATA))
+               return 0;
+
+       for (i = 0; i < num_legacy_callbacks; i++) {
+               char *action_ptr = strstr(buffer, callbacks[i].action);
+               char *cmd_ptr = strstr(buffer, callbacks[i].cmd);
+               char *data_ptr = strstr(buffer, callbacks[i].data);
+               if (!action_ptr || !cmd_ptr || !data_ptr)
+                       continue;
+
+               char action_sep = action_ptr[strlen(callbacks[i].action)];
+               char cmd_sep = cmd_ptr[strlen(callbacks[i].cmd)];
+               if (!(strchr(valid_sep, action_sep) && strchr(valid_sep,
+                               cmd_sep)))
+                       return -EPERM;
+               char data_sep;
+
+               if (!strchr(data_ptr, '{'))
+                       data_sep = data_ptr[strlen(callbacks[i].data)];
+               else {
+                       char *data_end = strchr(data_ptr, '}');
+                       data = data_ptr + strlen(DATA_REQ_LABEL);
+                       data_sep = data_end[1];
+                       data_end[1] = 0;
+               }
+               if (!strchr(valid_sep, data_sep))
+                       return -EPERM;
+               fn = callbacks[i].fn;
+               break;
+       }
+
+       if (!fn)
+               return -EINVAL;
+       perform_command(fn, data, s);
+       return 0;
+}
+
+void *
+legacy_client_handler(void *sock_id)
+{
+       int s = (int)(uintptr_t)sock_id;
+       int ret;
+       char buffer_recv[BUF_SIZE];
+       /* receive data is not null terminated */
+       int bytes = read(s, buffer_recv, sizeof(buffer_recv));
+
+       while (bytes > 0) {
+               buffer_recv[bytes] = 0;
+               int i, j;
+               char buffer[BUF_SIZE];
+               for (i = 0, j = 0; buffer_recv[i] != '\0'; i++) {
+                       buffer[j] = buffer_recv[i];
+                       j += !isspace(buffer_recv[i]);
+               }
+               buffer[j] = 0;
+               ret = parse_client_request(buffer, j, s);
+               if (ret < 0) {
+                       ret = send_error_response(s, ret);
+                       if (ret < 0)
+                               printf("\nCould not send error response\n");
+               }
+               bytes = read(s, buffer_recv, sizeof(buffer_recv));
+       }
+       close(s);
+       return NULL;
+}