]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/bpf: install log callback and suppress most messages from libbpf
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 1 Jun 2022 22:27:52 +0000 (00:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 2 Jun 2022 08:48:31 +0000 (10:48 +0200)
$ build/test-socket-bind
...
libbpf: load bpf program failed: Operation not permitted
libbpf: failed to load program 'sd_bind4'
libbpf: failed to load object 'socket_bind_bpf'
libbpf: failed to load BPF skeleton 'socket_bind_bpf': -1
Failed to load BPF object: Operation not permitted

Now all lines with "libbpf:" are at debug level and will be hidden by
default.

Partially fixes https://bugzilla.redhat.com/show_bug.cgi?id=2084955#c14
(i.e. the error that was exposed when the initial error was fixed.)

src/shared/bpf-dlopen.c
src/shared/bpf-dlopen.h

index 6f82002ff83f387786df7550be984fbd5b878bc8..d8e778794c2ada375147e89f448476623a310fc0 100644 (file)
@@ -9,7 +9,6 @@ static void *bpf_dl = NULL;
 
 struct bpf_link* (*sym_bpf_program__attach_cgroup)(struct bpf_program *, int);
 struct bpf_link* (*sym_bpf_program__attach_lsm)(struct bpf_program *);
-long (*sym_libbpf_get_error)(const void *);
 int (*sym_bpf_link__fd)(const struct bpf_link *);
 int (*sym_bpf_link__destroy)(struct bpf_link *);
 int (*sym_bpf_map__fd)(const struct bpf_map *);
@@ -26,9 +25,27 @@ void (*sym_bpf_object__detach_skeleton)(struct bpf_object_skeleton *);
 void (*sym_bpf_object__destroy_skeleton)(struct bpf_object_skeleton *);
 bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
 const char* (*sym_bpf_program__name)(const struct bpf_program *);
+libbpf_print_fn_t (*sym_libbpf_set_print)(libbpf_print_fn_t);
+long (*sym_libbpf_get_error)(const void *);
+
+_printf_(2,0)
+static int bpf_print_func(enum libbpf_print_level level, const char *fmt, va_list ap) {
+#if !LOG_TRACE
+        /* libbpf logs a lot of details at its debug level, which we don't need to see. */
+        if (level == LIBBPF_DEBUG)
+                return 0;
+#endif
+        /* All other levels are downgraded to LOG_DEBUG */
+
+        /* errno is used here, on the assumption that if the log message uses %m, errno will be set to
+         * something useful. Otherwise, it shouldn't matter, we may pass 0 or some bogus value. */
+        return log_internalv(LOG_DEBUG, errno, NULL, 0, NULL, fmt, ap);
+}
 
 int dlopen_bpf(void) {
-        return dlopen_many_sym_or_warn(
+        int r;
+
+        r = dlopen_many_sym_or_warn(
                         &bpf_dl, "libbpf.so.0", LOG_DEBUG,
                         DLSYM_ARG(bpf_link__destroy),
                         DLSYM_ARG(bpf_link__fd),
@@ -48,7 +65,14 @@ int dlopen_bpf(void) {
                         DLSYM_ARG(bpf_program__attach_cgroup),
                         DLSYM_ARG(bpf_program__attach_lsm),
                         DLSYM_ARG(bpf_program__name),
+                        DLSYM_ARG(libbpf_set_print),
                         DLSYM_ARG(libbpf_get_error));
+        if (r < 0)
+                return r;
+
+        /* We set the print helper unconditionally. Otherwise libbpf will emit not useful log messages. */
+        (void) sym_libbpf_set_print(bpf_print_func);
+        return r;
 }
 
 #else
index 713c41c3f40225d29ad7ae280f4ef56d9c92b6b5..f0d40325d90a3b183cf3d15cc74bf24a516f30b3 100644 (file)
@@ -8,7 +8,6 @@
 
 extern struct bpf_link* (*sym_bpf_program__attach_cgroup)(struct bpf_program *, int);
 extern struct bpf_link* (*sym_bpf_program__attach_lsm)(struct bpf_program *);
-extern long (*sym_libbpf_get_error)(const void *);
 extern int (*sym_bpf_link__fd)(const struct bpf_link *);
 extern int (*sym_bpf_link__destroy)(struct bpf_link *);
 extern int (*sym_bpf_map__fd)(const struct bpf_map *);
@@ -27,6 +26,8 @@ extern void (*sym_bpf_object__detach_skeleton)(struct bpf_object_skeleton *);
 extern void (*sym_bpf_object__destroy_skeleton)(struct bpf_object_skeleton *);
 extern bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
 extern const char* (*sym_bpf_program__name)(const struct bpf_program *);
+extern libbpf_print_fn_t (*sym_libbpf_set_print)(libbpf_print_fn_t);
+extern long (*sym_libbpf_get_error)(const void *);
 
 #endif