]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
test: (mkfds::mapped-bpf-map) new factory
authorWanBingjiang <wanbingjiang@webray.com.cn>
Thu, 23 Jul 2026 07:10:19 +0000 (15:10 +0800)
committerWanBingjiang <wanbingjiang@webray.com.cn>
Thu, 23 Jul 2026 10:34:48 +0000 (18:34 +0800)
Signed-off-by: WanBingjiang <wanbingjiang@webray.com.cn>
configure.ac
meson.build
tests/helpers/test_mkfds.c

index 4347e9bf772c02911598eeee9d69558531a00b11..0ded064ce6d18aae14f88e98f850e83a35291a88 100644 (file)
@@ -591,6 +591,11 @@ AC_CHECK_DECL([BPF_LINK_CREATE],
              [#include <linux/bpf.h>])
 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 <linux/bpf.h>])
+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],
index 70cf395523f0dfbbbf463082fe5d3547d52e952c..27b2a49662c2ba36c4db027311d4872dda821f1d 100644 (file)
@@ -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',
index a8ee45129370f112002a096bf00f7ba22b8adc94..a4daca9771838f64e8b83d4505990b4433b3c372 100644 (file)
@@ -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",