[09/12] telemetry: introduce telemetry backward compatibility
diff mbox series

Message ID 20200319171907.60891-10-ciara.power@intel.com
State Superseded, archived
Delegated to: David Marchand
Headers show
Series
  • update and simplify telemetry library.
Related show

Checks

Context Check Description
ci/Intel-compilation fail Compilation issues
ci/checkpatch success coding style OK

Commit Message

Ciara Power March 19, 2020, 5:19 p.m. UTC
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>
---
 lib/librte_metrics/Makefile                   |   2 +-
 lib/librte_metrics/meson.build                |   1 +
 lib/librte_metrics/rte_metrics_telemetry.c    | 113 +++++++++
 lib/librte_telemetry/Makefile                 |   1 +
 lib/librte_telemetry/meson.build              |   4 +-
 lib/librte_telemetry/rte_telemetry.h          |   1 +
 lib/librte_telemetry/rte_telemetry_legacy.h   |  48 ++++
 .../rte_telemetry_version.map                 |   2 +
 lib/librte_telemetry/telemetry.c              |  32 +++
 lib/librte_telemetry/telemetry_legacy.c       | 226 ++++++++++++++++++
 10 files changed, 427 insertions(+), 3 deletions(-)
 create mode 100644 lib/librte_telemetry/rte_telemetry_legacy.h
 create mode 100644 lib/librte_telemetry/telemetry_legacy.c

Patch
diff mbox series

diff --git a/lib/librte_metrics/Makefile b/lib/librte_metrics/Makefile
index fa762ba37..c8b9b143d 100644
--- a/lib/librte_metrics/Makefile
+++ b/lib/librte_metrics/Makefile
@@ -21,7 +21,7 @@  ifneq ($(JANSSON_LINK),)
 SRCS-y += rte_metrics_telemetry.c
 SYMLINK-$(CONFIG_RTE_LIBRTE_METRICS)-include += rte_metrics_telemetry.h
 
-LDLIBS += -lrte_ethdev
+LDLIBS += -lrte_ethdev -lrte_telemetry
 LDLIBS += $(JANSSON_LINK)
 
 CFLAGS += $(JANSSON_CFLAGS)
diff --git a/lib/librte_metrics/meson.build b/lib/librte_metrics/meson.build
index d116857e2..1a7cbf29c 100644
--- a/lib/librte_metrics/meson.build
+++ b/lib/librte_metrics/meson.build
@@ -11,4 +11,5 @@  if jansson.found()
 	sources += files('rte_metrics_telemetry.c')
 	headers = files('rte_metrics_telemetry.h')
 	deps += ['ethdev', 'telemetry']
+	includes += include_directories('../librte_telemetry')
 endif
diff --git a/lib/librte_metrics/rte_metrics_telemetry.c b/lib/librte_metrics/rte_metrics_telemetry.c
index 31d505551..49de9d74b 100644
--- a/lib/librte_metrics/rte_metrics_telemetry.c
+++ b/lib/librte_metrics/rte_metrics_telemetry.c
@@ -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"
@@ -417,6 +418,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
@@ -428,6 +535,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)
diff --git a/lib/librte_telemetry/Makefile b/lib/librte_telemetry/Makefile
index b34fe90d4..e2af886b6 100644
--- a/lib/librte_telemetry/Makefile
+++ b/lib/librte_telemetry/Makefile
@@ -26,6 +26,7 @@  SRCS-$(CONFIG_RTE_LIBRTE_TELEMETRY) := rte_telemetry.c
 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_legacy.c
 
 # export include files
 SYMLINK-$(CONFIG_RTE_LIBRTE_TELEMETRY)-include := rte_telemetry.h
diff --git a/lib/librte_telemetry/meson.build b/lib/librte_telemetry/meson.build
index 49c947120..caca32b2e 100644
--- a/lib/librte_telemetry/meson.build
+++ b/lib/librte_telemetry/meson.build
@@ -4,8 +4,8 @@ 
 includes = [global_inc]
 includes += include_directories('../librte_eal/common/include/arch/' + arch_subdir)
 
-sources = files('rte_telemetry.c', 'rte_telemetry_parser.c', 'rte_telemetry_parser_test.c',
-	'telemetry.c')
+sources = files('rte_telemetry.c','rte_telemetry_parser.c', 'rte_telemetry_parser_test.c',
+	'telemetry.c', 'telemetry_legacy.c')
 headers = files('rte_telemetry.h', 'rte_telemetry_internal.h', 'rte_telemetry_parser.h')
 cflags += '-DALLOW_EXPERIMENTAL_API'
 includes += include_directories('../librte_metrics')
diff --git a/lib/librte_telemetry/rte_telemetry.h b/lib/librte_telemetry/rte_telemetry.h
index 060613117..13ce235cb 100644
--- a/lib/librte_telemetry/rte_telemetry.h
+++ b/lib/librte_telemetry/rte_telemetry.h
@@ -125,4 +125,5 @@  int rte_telemetry_register_cmd(const char *cmd, telemetry_cb fn);
  */
 __rte_experimental
 int rte_telemetry_new_init(void);
+
 #endif
diff --git a/lib/librte_telemetry/rte_telemetry_legacy.h b/lib/librte_telemetry/rte_telemetry_legacy.h
new file mode 100644
index 000000000..66c4981b3
--- /dev/null
+++ b/lib/librte_telemetry/rte_telemetry_legacy.h
@@ -0,0 +1,48 @@ 
+/* 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"
+
+enum rte_telemetry_legacy_data_req {
+	DATA_NOT_REQ = 0,
+	DATA_REQ
+};
+
+extern int num_legacy_callbacks;
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * Used for handling data received over the legacy telemetry socket.
+ *
+ * @return
+ * Void.
+ */
+void *legacy_client_handler(void *sock_id);
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice
+ *
+ * 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_cb fn);
+
+#endif
diff --git a/lib/librte_telemetry/rte_telemetry_version.map b/lib/librte_telemetry/rte_telemetry_version.map
index 831bbd59a..2db0a5a44 100644
--- a/lib/librte_telemetry/rte_telemetry_version.map
+++ b/lib/librte_telemetry/rte_telemetry_version.map
@@ -8,6 +8,8 @@  EXPERIMENTAL {
 	rte_telemetry_set_metrics_fns;
 	rte_telemetry_new_init;
 	rte_telemetry_register_cmd;
+	rte_telemetry_legacy_init;
+	rte_telemetry_legacy_register;
 
 	local: *;
 };
diff --git a/lib/librte_telemetry/telemetry.c b/lib/librte_telemetry/telemetry.c
index 1ad784f59..e77672b48 100644
--- a/lib/librte_telemetry/telemetry.c
+++ b/lib/librte_telemetry/telemetry.c
@@ -15,6 +15,7 @@ 
 #include <rte_spinlock.h>
 
 #include "rte_telemetry.h"
+#include "rte_telemetry_legacy.h"
 
 #define MAX_CMD_LEN 56
 
@@ -36,6 +37,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] = {
@@ -182,6 +184,8 @@  unlink_sockets(void)
 {
 	if (v2_socket.path[0])
 		unlink(v2_socket.path);
+	if (v1_socket.path[0])
+		unlink(v1_socket.path);
 }
 
 static int
@@ -221,6 +225,34 @@  create_socket(char *path)
 	return -1;
 }
 
+static int __rte_unused /* will be used in future commit */
+telemetry_legacy_init(const char *runtime_dir, const char **err_str)
+{
+	pthread_t t_old;
+
+	if (num_legacy_callbacks == 1) {
+		*err_str = "No legacy callbacks - error creating legacy socket";
+		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) {
+		*err_str = telemetry_log_error;
+		return -1;
+	}
+	pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+
+	return 0;
+}
+
 static int
 telemetry_v2_init(const char *runtime_dir, const char **err_str)
 {
diff --git a/lib/librte_telemetry/telemetry_legacy.c b/lib/librte_telemetry/telemetry_legacy.c
new file mode 100644
index 000000000..e7c01525b
--- /dev/null
+++ b/lib/librte_telemetry/telemetry_legacy.c
@@ -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_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_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_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_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));
+	buffer_recv[bytes] = 0;
+
+	while (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));
+		buffer_recv[bytes] = 0;
+	}
+	close(s);
+	return NULL;
+}