naccyde / rpms / systemd

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