diff mbox series

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

Message ID tencent_7DD02046A8398BE3324F85E0F56ED41EB105@qq.com (mailing list archive)
State Changes Requested
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-VM_Test-1 success Logs for build for s390x with gcc
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 success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
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 s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 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 gcc
bpf/vmtest-bpf-next-VM_Test-18 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 gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix

Commit Message

Rong Tao Oct. 29, 2022, 7:53 a.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, set "socket/xxx" to SOCKET_FILTER
solves this error.

 $ 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 | 3 +++
 1 file changed, 3 insertions(+)

Comments

Andrii Nakryiko Nov. 3, 2022, 6:38 p.m. UTC | #1
On Sat, Oct 29, 2022 at 12:53 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, set "socket/xxx" to SOCKET_FILTER
> solves this error.
>
>  $ 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>
> ---

You need to do changes like this:

diff --git a/samples/bpf/sockex3_kern.c b/samples/bpf/sockex3_kern.c
index b363503357e5..db6a93e7ec53 100644
--- a/samples/bpf/sockex3_kern.c
+++ b/samples/bpf/sockex3_kern.c
@@ -17,7 +17,7 @@
 #define IP_MF          0x2000
 #define IP_OFFSET      0x1FFF

-#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
+#define PROG(F) SEC("socket_filter") int bpf_func_##F

 struct {
        __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
@@ -279,7 +279,7 @@ PROG(PARSE_MPLS)(struct __sk_buff *skb)
        return 0;
 }

-SEC("socket/0")
+SEC("socket_filter")
 int main_prog(struct __sk_buff *skb)
 {
        __u32 nhoff = ETH_HLEN;


Why fixing up after the fact at runtime, if you can just make those
BPF programs conform to libbpf rules?



>  samples/bpf/sockex3_user.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
> index cd6fa79df900..dc79c17ad195 100644
> --- a/samples/bpf/sockex3_user.c
> +++ b/samples/bpf/sockex3_user.c
> @@ -39,6 +39,9 @@ int main(int argc, char **argv)
>                 return 0;
>         }
>
> +       bpf_object__for_each_program(prog, obj)
> +               bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
> +
>         /* load BPF program */
>         if (bpf_object__load(obj)) {
>                 fprintf(stderr, "ERROR: loading BPF object file failed\n");
> --
> 2.31.1
>
Rong Tao Nov. 4, 2022, 3:17 a.m. UTC | #2
We can not just remove the number of program like:

-#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
+#define PROG(F) SEC("socket_filter") int bpf_func_##F

because "sockex3" use the _NUMBER_ as index(see map "jmp_table"), if we
apply the following patch, it's still not recognize "socket_filter/xxx"
as "socket_filter", still have "missing BPF prog type" error:

diff --git a/samples/bpf/sockex3_kern.c b/samples/bpf/sockex3_kern.c
index b363503357e5..ab5a7bde66d0 100644
--- a/samples/bpf/sockex3_kern.c
+++ b/samples/bpf/sockex3_kern.c
@@ -17,7 +17,7 @@
 #define IP_MF          0x2000
 #define IP_OFFSET      0x1FFF

-#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
+#define PROG(F) SEC("socket_filter/"__stringify(F)) int bpf_func_##F

 struct {
        __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
@@ -279,7 +279,7 @@ PROG(PARSE_MPLS)(struct __sk_buff *skb)
        return 0;
 }

-SEC("socket/0")
+SEC("socket_filter/0")
 int main_prog(struct __sk_buff *skb)
 {
        __u32 nhoff = ETH_HLEN;
diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
index cd6fa79df900..63fc9a8077b1 100644
--- a/samples/bpf/sockex3_user.c
+++ b/samples/bpf/sockex3_user.c
@@ -56,7 +56,7 @@ int main(int argc, char **argv)
                fd = bpf_program__fd(prog);

                section = bpf_program__section_name(prog);
-               if (sscanf(section, "socket/%d", &key) != 1) {
+               if (sscanf(section, "socket_filter/%d", &key) != 1) {
                        fprintf(stderr, "ERROR: finding prog failed\n");
                        goto cleanup;
                }

For a reason, in sockex3_kern.c only have five PROGs, list all five:

#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
PROG(PARSE_IP)(struct __sk_buff *skb)
PROG(PARSE_IPV6)(struct __sk_buff *skb)
PROG(PARSE_VLAN)(struct __sk_buff *skb)
PROG(PARSE_MPLS)(struct __sk_buff *skb)
SEC("socket/0") int main_prog(struct __sk_buff *skb)

As you can see, all those PROGs are BPF_PROG_TYPE_SOCKET_FILTER, so we can
use the follow patch specify bpf program type as SOCKET_FILTER:

https://lore.kernel.org/lkml/tencent_7DD02046A8398BE3324F85E0F56ED41EB105@qq.com/

diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
index cd6fa79df900..dc79c17ad195 100644
--- a/samples/bpf/sockex3_user.c
+++ b/samples/bpf/sockex3_user.c
@@ -39,6 +39,9 @@ int main(int argc, char **argv)
                return 0;
        }

+       bpf_object__for_each_program(prog, obj)
+               bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
+
        /* load BPF program */
        if (bpf_object__load(obj)) {
                fprintf(stderr, "ERROR: loading BPF object file failed\n");

This patch above totally solved the compile error.
Andrii Nakryiko Nov. 4, 2022, 10:53 p.m. UTC | #3
On Thu, Nov 3, 2022 at 8:17 PM Rong Tao <rtoax@foxmail.com> wrote:
>
> We can not just remove the number of program like:
>
> -#define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
> +#define PROG(F) SEC("socket_filter") int bpf_func_##F
>
> because "sockex3" use the _NUMBER_ as index(see map "jmp_table"), if we
> apply the following patch, it's still not recognize "socket_filter/xxx"
> as "socket_filter", still have "missing BPF prog type" error:

Ok, let's keep unwinding this. This is an old and manual way to set up
tail call map. Libbpf supports declarative way to do, so
sockex3_user.c won't have to do anything at all.

See progs/test_prog_array_init.c for an example. Let's convert samples
to use this as well.

This programmatic setting of program type works, there is no doubt
about this. But it's a signal that something is not exactly how it
should be. So let's use this as an opportunity to modernize samples,
instead of adding workarounds.

I hope you sympathize with this goal.

>
> diff --git a/samples/bpf/sockex3_kern.c b/samples/bpf/sockex3_kern.c
> index b363503357e5..ab5a7bde66d0 100644
> --- a/samples/bpf/sockex3_kern.c
> +++ b/samples/bpf/sockex3_kern.c
> @@ -17,7 +17,7 @@
>  #define IP_MF          0x2000
>  #define IP_OFFSET      0x1FFF
>

[...]
diff mbox series

Patch

diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c
index cd6fa79df900..dc79c17ad195 100644
--- a/samples/bpf/sockex3_user.c
+++ b/samples/bpf/sockex3_user.c
@@ -39,6 +39,9 @@  int main(int argc, char **argv)
 		return 0;
 	}
 
+	bpf_object__for_each_program(prog, obj)
+		bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
+
 	/* load BPF program */
 	if (bpf_object__load(obj)) {
 		fprintf(stderr, "ERROR: loading BPF object file failed\n");