diff mbox series

[bpf-next] samples/bpf: Fix sockex3: missing BPF prog type

Message ID tencent_C0A1D82C934B5895D65788D11A4456835607@qq.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next] samples/bpf: Fix sockex3: missing BPF prog type | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Alignment should match open parenthesis
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix

Commit Message

Rong Tao Oct. 27, 2022, 1:38 p.m. UTC
From: Rong Tao <rongtao@cestc.cn>

since commit 450b167fb9be("libbpf: clean up SEC() handling"),
sec_def_matches() does not recognize "socket/xxx" as "socket", therefore,
the BPF program type is not recognized, we should add a custom program
type handler for "socket/xxx".

 $ cd samples/bpf
 $ sudo ./sockex3
 libbpf: prog 'bpf_func_PARSE_IP': missing BPF prog type, check ELF section name 'socket/3'
 libbpf: prog 'bpf_func_PARSE_IP': failed to load: -22
 libbpf: failed to load object './sockex3_kern.o'
 ERROR: loading BPF object file failed

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 samples/bpf/sockex3_user.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Andrii Nakryiko Oct. 27, 2022, 8:15 p.m. UTC | #1
On Thu, Oct 27, 2022 at 6:38 AM Rong Tao <rtoax@foxmail.com> wrote:
>
> From: Rong Tao <rongtao@cestc.cn>
>
> since commit 450b167fb9be("libbpf: clean up SEC() handling"),
> sec_def_matches() does not recognize "socket/xxx" as "socket", therefore,
> the BPF program type is not recognized, we should add a custom program
> type handler for "socket/xxx".

I don't think we should, we should just switch to SEC("socket") or
whatever the right annotation has to be. Let's fix the BPF-side code.

>
>  $ cd samples/bpf
>  $ sudo ./sockex3
>  libbpf: prog 'bpf_func_PARSE_IP': missing BPF prog type, check ELF section name 'socket/3'
>  libbpf: prog 'bpf_func_PARSE_IP': failed to load: -22
>  libbpf: failed to load object './sockex3_kern.o'
>  ERROR: loading BPF object file failed
>
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  samples/bpf/sockex3_user.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
> index cd6fa79df900..d18d7a3600b0 100644
> --- a/samples/bpf/sockex3_user.c
> +++ b/samples/bpf/sockex3_user.c
> @@ -22,6 +22,14 @@ struct pair {
>         __u64 bytes;
>  };
>
> +static int socket_prog_type_id;
> +
> +__attribute__((destructor))
> +static void unregister_socket_sec_handlers(void)
> +{
> +       libbpf_unregister_prog_handler(socket_prog_type_id);
> +}
> +
>  int main(int argc, char **argv)
>  {
>         int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd;
> @@ -31,6 +39,13 @@ int main(int argc, char **argv)
>         char filename[256];
>         FILE *f;
>
> +       LIBBPF_OPTS(libbpf_prog_handler_opts, socket_opts,
> +               .cookie = 1,
> +       );
> +
> +       socket_prog_type_id = libbpf_register_prog_handler("socket/",
> +                       BPF_PROG_TYPE_SOCKET_FILTER, 0, &socket_opts);
> +
>         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
>
>         obj = bpf_object__open_file(filename, NULL);
> --
> 2.31.1
>
Rong Tao Oct. 28, 2022, 1:01 a.m. UTC | #2
Thanks for your reply, actually, i tried another method, which can solved
this error, recognize "socket/xxx" as "socket". However, it maybe influence
other BPF prog or not? What do you think the following patch?

--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8659,7 +8659,7 @@ static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_n
                return false;
        }

-       return strcmp(sec_name, sec_def->sec) == 0;
+       return strncmp(sec_name, sec_def->sec, len) == 0;
 }
Andrii Nakryiko Oct. 28, 2022, 5:09 p.m. UTC | #3
On Thu, Oct 27, 2022 at 6:01 PM Rong Tao <rtoax@foxmail.com> wrote:
>
> Thanks for your reply, actually, i tried another method, which can solved
> this error, recognize "socket/xxx" as "socket". However, it maybe influence
> other BPF prog or not? What do you think the following patch?

Don't fix libbpf, it's not broken. Fix the sample.

>
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8659,7 +8659,7 @@ static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_n
>                 return false;
>         }
>
> -       return strcmp(sec_name, sec_def->sec) == 0;
> +       return strncmp(sec_name, sec_def->sec, len) == 0;
>  }
diff mbox series

Patch

diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
index cd6fa79df900..d18d7a3600b0 100644
--- a/samples/bpf/sockex3_user.c
+++ b/samples/bpf/sockex3_user.c
@@ -22,6 +22,14 @@  struct pair {
 	__u64 bytes;
 };
 
+static int socket_prog_type_id;
+
+__attribute__((destructor))
+static void unregister_socket_sec_handlers(void)
+{
+	libbpf_unregister_prog_handler(socket_prog_type_id);
+}
+
 int main(int argc, char **argv)
 {
 	int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd;
@@ -31,6 +39,13 @@  int main(int argc, char **argv)
 	char filename[256];
 	FILE *f;
 
+	LIBBPF_OPTS(libbpf_prog_handler_opts, socket_opts,
+		.cookie = 1,
+	);
+
+	socket_prog_type_id = libbpf_register_prog_handler("socket/",
+			BPF_PROG_TYPE_SOCKET_FILTER, 0, &socket_opts);
+
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
 	obj = bpf_object__open_file(filename, NULL);