naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame 0002-ss-pretty-print-BPF-socket-local-storage.patch

eca1b9
From e3ecf048579afd7a673700d04893671627e85d57 Mon Sep 17 00:00:00 2001
eca1b9
From: Quentin Deslandes <qde@naccy.de>
eca1b9
Date: Wed, 21 Feb 2024 16:16:20 +0100
eca1b9
Subject: [PATCH] ss: pretty-print BPF socket-local storage
eca1b9
eca1b9
ss is able to print the map ID(s) for which a given socket has BPF
eca1b9
socket-local storage defined (using --bpf-maps or --bpf-map-id=). However,
eca1b9
the actual content of the map remains hidden.
eca1b9
eca1b9
This change aims to pretty-print the socket-local storage content following
eca1b9
the socket details, similar to what `bpftool map dump` would do. The exact
eca1b9
output format is inspired by drgn, while the BTF data processing is similar
eca1b9
to bpftool's.
eca1b9
eca1b9
ss will use libbpf's btf_dump__dump_type_data() to ease pretty-printing
eca1b9
of binary data. This requires out_bpf_sk_storage_print_fn() as a print
eca1b9
callback function used by btf_dump__dump_type_data(). vout() is also
eca1b9
introduced, which is similar to out() but accepts a va_list as
eca1b9
parameter.
eca1b9
eca1b9
ss' output remains unchanged unless --bpf-maps or --bpf-map-id= is used,
eca1b9
in which case each socket containing BPF local storage will be followed by
eca1b9
the content of the storage before the next socket's info is displayed.
eca1b9
eca1b9
Signed-off-by: Quentin Deslandes <qde@naccy.de>
eca1b9
Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
eca1b9
Signed-off-by: David Ahern <dsahern@kernel.org>
eca1b9
---
eca1b9
 misc/ss.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++----
eca1b9
 1 file changed, 137 insertions(+), 11 deletions(-)
eca1b9
eca1b9
diff --git a/misc/ss.c b/misc/ss.c
eca1b9
index 2c7e7c58..3ebac132 100644
eca1b9
--- a/misc/ss.c
eca1b9
+++ b/misc/ss.c
eca1b9
@@ -61,7 +61,9 @@
eca1b9
 #define ENABLE_BPF_SKSTORAGE_SUPPORT
eca1b9
 
eca1b9
 #include <bpf/bpf.h>
eca1b9
+#include <bpf/btf.h>
eca1b9
 #include <bpf/libbpf.h>
eca1b9
+#include <linux/btf.h>
eca1b9
 
eca1b9
 #if (LIBBPF_MAJOR_VERSION == 0) && (LIBBPF_MINOR_VERSION < 5)
eca1b9
 #warning "libbpf version 0.5 or later is required, disabling BPF socket-local storage support"
eca1b9
@@ -1053,11 +1055,10 @@ static int buf_update(int len)
eca1b9
 }
eca1b9
 
eca1b9
 /* Append content to buffer as part of the current field */
eca1b9
-__attribute__((format(printf, 1, 2)))
eca1b9
-static void out(const char *fmt, ...)
eca1b9
+static void vout(const char *fmt, va_list args)
eca1b9
 {
eca1b9
 	struct column *f = current_field;
eca1b9
-	va_list args;
eca1b9
+	va_list _args;
eca1b9
 	char *pos;
eca1b9
 	int len;
eca1b9
 
eca1b9
@@ -1068,18 +1069,27 @@ static void out(const char *fmt, ...)
eca1b9
 		buffer.head = buf_chunk_new();
eca1b9
 
eca1b9
 again:	/* Append to buffer: if we have a new chunk, print again */
eca1b9
+	va_copy(_args, args);
eca1b9
 
eca1b9
 	pos = buffer.cur->data + buffer.cur->len;
eca1b9
-	va_start(args, fmt);
eca1b9
 
eca1b9
 	/* Limit to tail room. If we hit the limit, buf_update() will tell us */
eca1b9
-	len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, args);
eca1b9
-	va_end(args);
eca1b9
+	len = vsnprintf(pos, buf_chunk_avail(buffer.tail), fmt, _args);
eca1b9
 
eca1b9
 	if (buf_update(len))
eca1b9
 		goto again;
eca1b9
 }
eca1b9
 
eca1b9
+__attribute__((format(printf, 1, 2)))
eca1b9
+static void out(const char *fmt, ...)
eca1b9
+{
eca1b9
+	va_list args;
eca1b9
+
eca1b9
+	va_start(args, fmt);
eca1b9
+	vout(fmt, args);
eca1b9
+	va_end(args);
eca1b9
+}
eca1b9
+
eca1b9
 static int print_left_spacing(struct column *f, int stored, int printed)
eca1b9
 {
eca1b9
 	int s;
eca1b9
@@ -3413,6 +3423,9 @@ static struct bpf_map_opts {
eca1b9
 	struct bpf_sk_storage_map_info {
eca1b9
 		unsigned int id;
eca1b9
 		int fd;
eca1b9
+		struct bpf_map_info info;
eca1b9
+		struct btf *btf;
eca1b9
+		struct btf_dump *dump;
eca1b9
 	} maps[MAX_NR_BPF_MAP_ID_OPTS];
eca1b9
 	bool show_all;
eca1b9
 } bpf_map_opts;
eca1b9
@@ -3423,10 +3436,36 @@ static void bpf_map_opts_mixed_error(void)
eca1b9
 		"ss: --bpf-maps and --bpf-map-id cannot be used together\n");
eca1b9
 }
eca1b9
 
eca1b9
+static int bpf_maps_opts_load_btf(struct bpf_map_info *info, struct btf **btf)
eca1b9
+{
eca1b9
+	if (info->btf_value_type_id) {
eca1b9
+		*btf = btf__load_from_kernel_by_id(info->btf_id);
eca1b9
+		if (!*btf) {
eca1b9
+			fprintf(stderr, "ss: failed to load BTF for map ID %u\n",
eca1b9
+				info->id);
eca1b9
+			return -1;
eca1b9
+		}
eca1b9
+	} else {
eca1b9
+		*btf = NULL;
eca1b9
+	}
eca1b9
+
eca1b9
+	return 0;
eca1b9
+}
eca1b9
+
eca1b9
+static void out_bpf_sk_storage_print_fn(void *ctx, const char *fmt, va_list args)
eca1b9
+{
eca1b9
+	vout(fmt, args);
eca1b9
+}
eca1b9
+
eca1b9
 static int bpf_map_opts_load_info(unsigned int map_id)
eca1b9
 {
eca1b9
+	struct btf_dump_opts dopts = {
eca1b9
+		.sz = sizeof(struct btf_dump_opts)
eca1b9
+	};
eca1b9
 	struct bpf_map_info info = {};
eca1b9
 	uint32_t len = sizeof(info);
eca1b9
+	struct btf_dump *dump;
eca1b9
+	struct btf *btf;
eca1b9
 	int fd;
eca1b9
 	int r;
eca1b9
 
eca1b9
@@ -3464,8 +3503,25 @@ static int bpf_map_opts_load_info(unsigned int map_id)
eca1b9
 		return -1;
eca1b9
 	}
eca1b9
 
eca1b9
+	r = bpf_maps_opts_load_btf(&info, &btf);
eca1b9
+	if (r) {
eca1b9
+		close(fd);
eca1b9
+		return -1;
eca1b9
+	}
eca1b9
+
eca1b9
+	dump = btf_dump__new(btf, out_bpf_sk_storage_print_fn, NULL, &dopts);
eca1b9
+	if (!dump) {
eca1b9
+		btf__free(btf);
eca1b9
+		close(fd);
eca1b9
+		fprintf(stderr, "Failed to create btf_dump object\n");
eca1b9
+		return -1;
eca1b9
+	}
eca1b9
+
eca1b9
 	bpf_map_opts.maps[bpf_map_opts.nr_maps].id = map_id;
eca1b9
-	bpf_map_opts.maps[bpf_map_opts.nr_maps++].fd = fd;
eca1b9
+	bpf_map_opts.maps[bpf_map_opts.nr_maps].fd = fd;
eca1b9
+	bpf_map_opts.maps[bpf_map_opts.nr_maps].info = info;
eca1b9
+	bpf_map_opts.maps[bpf_map_opts.nr_maps].btf = btf;
eca1b9
+	bpf_map_opts.maps[bpf_map_opts.nr_maps++].dump = dump;
eca1b9
 
eca1b9
 	return 0;
eca1b9
 }
eca1b9
@@ -3517,8 +3573,11 @@ static void bpf_map_opts_destroy(void)
eca1b9
 {
eca1b9
 	int i;
eca1b9
 
eca1b9
-	for (i = 0; i < bpf_map_opts.nr_maps; ++i)
eca1b9
+	for (i = 0; i < bpf_map_opts.nr_maps; ++i) {
eca1b9
+		btf_dump__free(bpf_map_opts.maps[i].dump);
eca1b9
+		btf__free(bpf_map_opts.maps[i].btf);
eca1b9
 		close(bpf_map_opts.maps[i].fd);
eca1b9
+	}
eca1b9
 }
eca1b9
 
eca1b9
 static struct rtattr *bpf_map_opts_alloc_rta(void)
eca1b9
@@ -3571,10 +3630,74 @@ static struct rtattr *bpf_map_opts_alloc_rta(void)
eca1b9
 	return stgs_rta;
eca1b9
 }
eca1b9
 
eca1b9
+static void out_bpf_sk_storage_oneline(struct bpf_sk_storage_map_info *info,
eca1b9
+	const void *data, size_t len)
eca1b9
+{
eca1b9
+	struct btf_dump_type_data_opts opts = {
eca1b9
+		.sz = sizeof(struct btf_dump_type_data_opts),
eca1b9
+		.emit_zeroes = 1,
eca1b9
+		.compact = 1
eca1b9
+	};
eca1b9
+	int r;
eca1b9
+
eca1b9
+	out(" map_id:%d", info->id);
eca1b9
+	r = btf_dump__dump_type_data(info->dump, info->info.btf_value_type_id,
eca1b9
+				     data, len, &opts);
eca1b9
+	if (r < 0)
eca1b9
+		out("failed to dump data: %d", r);
eca1b9
+}
eca1b9
+
eca1b9
+static void out_bpf_sk_storage_multiline(struct bpf_sk_storage_map_info *info,
eca1b9
+	const void *data, size_t len)
eca1b9
+{
eca1b9
+	struct btf_dump_type_data_opts opts = {
eca1b9
+		.sz = sizeof(struct btf_dump_type_data_opts),
eca1b9
+		.indent_level = 2,
eca1b9
+		.emit_zeroes = 1
eca1b9
+	};
eca1b9
+	int r;
eca1b9
+
eca1b9
+	out("\n\tmap_id:%d [\n", info->id);
eca1b9
+
eca1b9
+	r = btf_dump__dump_type_data(info->dump, info->info.btf_value_type_id,
eca1b9
+				     data, len, &opts);
eca1b9
+	if (r < 0)
eca1b9
+		out("\t\tfailed to dump data: %d", r);
eca1b9
+
eca1b9
+	out("\n\t]");
eca1b9
+}
eca1b9
+
eca1b9
+static void out_bpf_sk_storage(int map_id, const void *data, size_t len)
eca1b9
+{
eca1b9
+	struct bpf_sk_storage_map_info *map_info;
eca1b9
+
eca1b9
+	map_info = bpf_map_opts_get_info(map_id);
eca1b9
+	if (!map_info) {
eca1b9
+		/* The kernel might return a map we can't get info for, skip
eca1b9
+		 * it but print the other ones.
eca1b9
+		 */
eca1b9
+		out("\n\tmap_id: %d failed to fetch info, skipping\n", map_id);
eca1b9
+		return;
eca1b9
+	}
eca1b9
+
eca1b9
+	if (map_info->info.value_size != len) {
eca1b9
+		fprintf(stderr,
eca1b9
+			"map_id: %d: invalid value size, expecting %u, got %lu\n",
eca1b9
+			map_id, map_info->info.value_size, len);
eca1b9
+		return;
eca1b9
+	}
eca1b9
+
eca1b9
+	if (oneline)
eca1b9
+		out_bpf_sk_storage_oneline(map_info, data, len);
eca1b9
+	else
eca1b9
+		out_bpf_sk_storage_multiline(map_info, data, len);
eca1b9
+}
eca1b9
+
eca1b9
 static void show_sk_bpf_storages(struct rtattr *bpf_stgs)
eca1b9
 {
eca1b9
 	struct rtattr *tb[SK_DIAG_BPF_STORAGE_MAX + 1], *bpf_stg;
eca1b9
-	unsigned int rem;
eca1b9
+	unsigned int rem, map_id;
eca1b9
+	struct rtattr *value;
eca1b9
 
eca1b9
 	for (bpf_stg = RTA_DATA(bpf_stgs), rem = RTA_PAYLOAD(bpf_stgs);
eca1b9
 		RTA_OK(bpf_stg, rem); bpf_stg = RTA_NEXT(bpf_stg, rem)) {
eca1b9
@@ -3586,8 +3709,11 @@ static void show_sk_bpf_storages(struct rtattr *bpf_stgs)
eca1b9
 				    (struct rtattr *)bpf_stg);
eca1b9
 
eca1b9
 		if (tb[SK_DIAG_BPF_STORAGE_MAP_ID]) {
eca1b9
-			out(" map_id:%u",
eca1b9
-			    rta_getattr_u32(tb[SK_DIAG_BPF_STORAGE_MAP_ID]));
eca1b9
+			map_id = rta_getattr_u32(tb[SK_DIAG_BPF_STORAGE_MAP_ID]);
eca1b9
+			value = tb[SK_DIAG_BPF_STORAGE_MAP_VALUE];
eca1b9
+
eca1b9
+			out_bpf_sk_storage(map_id, RTA_DATA(value),
eca1b9
+					   RTA_PAYLOAD(value));
eca1b9
 		}
eca1b9
 	}
eca1b9
 }
eca1b9
-- 
eca1b9
2.41.0
eca1b9