netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv2 bpf-next] libbpf: try to add a name for bpftool self-created maps
@ 2022-08-09 10:12 Hangbin Liu
  0 siblings, 0 replies; only message in thread
From: Hangbin Liu @ 2022-08-09 10:12 UTC (permalink / raw)
  To: netdev
  Cc: Quentin Monnet, Andrii Nakryiko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, Hangbin Liu

As discussed before[1], the bpftool self-created maps can appear in final
map show output due to deferred removal in kernel. These maps don't have
a name, which would make users confused about where it comes from.

Adding names for these maps could make users know what these maps used for.
It also could make some tests (like test_offload.py, which skip base maps
without names as a workaround) filter them out.

As Quentin suggested, add a small wrapper to fall back with no name
if kernel is not supported.

[1] https://lore.kernel.org/bpf/CAEf4BzY66WPKQbDe74AKZ6nFtZjq5e+G3Ji2egcVytB9R6_sGQ@mail.gmail.com/

Suggested-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
v2: rename the wrapper with proper name
---
 tools/lib/bpf/libbpf.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index f7364ea82ac1..a38bcf325198 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4421,6 +4421,22 @@ static int probe_kern_prog_name(void)
 	return probe_fd(ret);
 }
 
+static int map_create_adjust_name(enum bpf_map_type map_type,
+				  const char *map_name, __u32 key_size,
+				  __u32 value_size, __u32 max_entries,
+				  const struct bpf_map_create_opts *opts)
+{
+	int map;
+
+	map = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, opts);
+	if (map < 0 && errno == EINVAL) {
+		/* Retry without name */
+		map = bpf_map_create(map_type, NULL, key_size, value_size, max_entries, opts);
+	}
+
+	return map;
+}
+
 static int probe_kern_global_data(void)
 {
 	char *cp, errmsg[STRERR_BUFSIZE];
@@ -4432,7 +4448,7 @@ static int probe_kern_global_data(void)
 	};
 	int ret, map, insn_cnt = ARRAY_SIZE(insns);
 
-	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
+	map = map_create_adjust_name(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
 	if (map < 0) {
 		ret = -errno;
 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
@@ -4565,7 +4581,7 @@ static int probe_kern_array_mmap(void)
 	LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
 	int fd;
 
-	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), sizeof(int), 1, &opts);
+	fd = map_create_adjust_name(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
 	return probe_fd(fd);
 }
 
@@ -4612,7 +4628,7 @@ static int probe_prog_bind_map(void)
 	};
 	int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
 
-	map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL);
+	map = map_create_adjust_name(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
 	if (map < 0) {
 		ret = -errno;
 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
-- 
2.35.3


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-09 10:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-09 10:12 [PATCHv2 bpf-next] libbpf: try to add a name for bpftool self-created maps Hangbin Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).