From: WanBingjiang Date: Thu, 23 Jul 2026 07:10:19 +0000 (+0800) Subject: test: (mkfds::mapped-bpf-map) new factory X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f6b19d80fa7914e0c716d09658dd7d1293c96a00;p=thirdparty%2Futil-linux.git test: (mkfds::mapped-bpf-map) new factory Signed-off-by: WanBingjiang --- diff --git a/configure.ac b/configure.ac index 4347e9bf7..0ded064ce 100644 --- a/configure.ac +++ b/configure.ac @@ -591,6 +591,11 @@ AC_CHECK_DECL([BPF_LINK_CREATE], [#include ]) AS_IF([test "x$have_bpf_link_create" = xyes], [AC_DEFINE([HAVE_BPF_LINK_CREATE], [1], [Define if BPF_LINK_CREATE is available])]) +AC_CHECK_DECL([BPF_F_MMAPABLE], + [have_bpf_f_mmapable=yes], [have_bpf_f_mmapable=no], + [#include ]) +AS_IF([test "x$have_bpf_f_mmapable" = xyes], + [AC_DEFINE([HAVE_BPF_F_MMAPABLE], [1], [Define if BPF_F_MMAPABLE is available])]) AC_CHECK_DECL([TIOCGLCKTRMIOS], [have_tiocglcktrmios=yes], [have_tiocglcktrmios=no], diff --git a/meson.build b/meson.build index 70cf39552..27b2a4966 100644 --- a/meson.build +++ b/meson.build @@ -4065,6 +4065,9 @@ if LINUX and lib_rt.found() if cc.has_header_symbol('linux/bpf.h', 'BPF_LINK_CREATE') test_mkfds_c_args += ['-DHAVE_BPF_LINK_CREATE'] endif + if cc.has_header_symbol('linux/bpf.h', 'BPF_F_MMAPABLE') + test_mkfds_c_args += ['-DHAVE_BPF_F_MMAPABLE'] + endif exe = executable( 'test_mkfds', 'tests/helpers/test_mkfds.c', diff --git a/tests/helpers/test_mkfds.c b/tests/helpers/test_mkfds.c index a8ee45129..a4daca977 100644 --- a/tests/helpers/test_mkfds.c +++ b/tests/helpers/test_mkfds.c @@ -3094,6 +3094,56 @@ static void free_bpf_link(const struct factory * factory _U_, void *data) free(data); } #endif /* HAVE_BPF_LINK_CREATE */ +#ifdef HAVE_BPF_F_MMAPABLE +static void *make_mmapped_bpf_map(const struct factory *factory, struct fdesc fdescs[], + int argc, char ** argv) +{ + struct arg name = decode_arg("name", factory->params, argc, argv); + const char *sname = ARG_STRING(name); + + int bfd; + union bpf_attr attr; + struct munmap_data *munmap_data; + + memset(&attr, 0, sizeof(attr)); + attr.map_type = BPF_MAP_TYPE_ARRAY; + attr.map_flags = BPF_F_MMAPABLE; + attr.value_size = 4; + attr.key_size = 4; + attr.max_entries = 10; + + strncpy(attr.map_name, sname, sizeof(attr.map_name) - 1); + + free_arg(&name); + + bfd = syscall(SYS_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); + if (bfd < 0) + err_nosys(EXIT_FAILURE, "failed in bpf(BPF_MAP_CREATE)"); + + if (bfd != fdescs[0].fd) { + if (dup2(bfd, fdescs[0].fd) < 0) { + err(EXIT_FAILURE, "failed to dup %d -> %d", bfd, fdescs[0].fd); + } + close(bfd); + } + + munmap_data = xmalloc(sizeof(*munmap_data)); + munmap_data->len = 4096; + munmap_data->ptr = mmap(NULL, munmap_data->len, + PROT_READ | PROT_WRITE, + MAP_SHARED, fdescs[0].fd, 0); + if (munmap_data->ptr == MAP_FAILED) + err(EXIT_FAILURE, "failed to mmap bpf-map"); + + fdescs[0] = (struct fdesc){ + .fd = fdescs[0].fd, + .close = close_fdesc_after_munmap, + .data = munmap_data, + }; + + return NULL; +} +#endif /* HAVE_BPF_F_MMAPABLE */ static void *make_pty(const struct factory *factory _U_, struct fdesc fdescs[], int argc _U_, char ** argv _U_) @@ -4423,6 +4473,25 @@ static const struct factory factories[] = { }, }, #endif +#ifdef HAVE_BPF_F_MMAPABLE + { + .name = "mapped-bpf-map", + .desc = "mmap'ed bpf map", + .priv = true, + .N = 1, + .EX_N = 0, + .make = make_mmapped_bpf_map, + .params = (struct parameter []) { + { + .name = "name", + .type = PTYPE_STRING, + .desc = "name assigned to the bpf map object", + .defv.string = "mkfds_bpf_map", + }, + PARAM_END + } + }, +#endif /* HAVE_BPF_F_MMAPABLE */ { .name = "pty", .desc = "make a pair of ptmx and pts",