Commit 10b62d6a authored by Hangbin Liu's avatar Hangbin Liu Committed by Andrii Nakryiko

libbpf: Add names for auxiliary maps

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.

With a libbpf_ prefix name, users could know who created these maps.
It also could make some tests (like test_offload.py, which skip base maps
without names as a workaround) filter them out.

Kernel adds bpf prog/map name support in the same merge
commit fadad670 ("Merge branch 'bpf-extend-info'"). So we can also use
kernel_supports(NULL, FEAT_PROG_NAME) to check if kernel supports map name.

As discussed [1], Let's make bpf_map_create accept non-null
name string, and silently ignore the name if kernel doesn't support.

  [1] https://lore.kernel.org/bpf/CAEf4BzYL1TQwo1231s83pjTdFPk9XWWhfZC5=KzkU-VO0k=0Ug@mail.gmail.com/Signed-off-by: default avatarHangbin Liu <liuhangbin@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220811034020.529685-1-liuhangbin@gmail.com
parent 73cf09a3
...@@ -183,7 +183,7 @@ int bpf_map_create(enum bpf_map_type map_type, ...@@ -183,7 +183,7 @@ int bpf_map_create(enum bpf_map_type map_type,
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
attr.map_type = map_type; attr.map_type = map_type;
if (map_name) if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name)); libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
attr.key_size = key_size; attr.key_size = key_size;
attr.value_size = value_size; attr.value_size = value_size;
......
...@@ -4437,7 +4437,7 @@ static int probe_kern_global_data(void) ...@@ -4437,7 +4437,7 @@ static int probe_kern_global_data(void)
}; };
int ret, map, insn_cnt = ARRAY_SIZE(insns); int ret, map, insn_cnt = ARRAY_SIZE(insns);
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL); map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
if (map < 0) { if (map < 0) {
ret = -errno; ret = -errno;
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
...@@ -4570,7 +4570,7 @@ static int probe_kern_array_mmap(void) ...@@ -4570,7 +4570,7 @@ static int probe_kern_array_mmap(void)
LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE); LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
int fd; int fd;
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), sizeof(int), 1, &opts); fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
return probe_fd(fd); return probe_fd(fd);
} }
...@@ -4617,7 +4617,7 @@ static int probe_prog_bind_map(void) ...@@ -4617,7 +4617,7 @@ static int probe_prog_bind_map(void)
}; };
int ret, map, prog, insn_cnt = ARRAY_SIZE(insns); int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(int), 32, 1, NULL); map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
if (map < 0) { if (map < 0) {
ret = -errno; ret = -errno;
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment