diff mbox series

[net-next,3/4] selftests: net: declare section names for bpf_offload

Message ID 20240409031549.3531084-4-kuba@kernel.org (mailing list archive)
State Accepted
Commit b1c2ce11d42886d08cfa28e38ee07f2b606ced0b
Delegated to: Netdev Maintainers
Headers show
Series selftests: move bpf-offload test from bpf to net | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 9 maintainers not CCed: hawk@kernel.org john.fastabend@gmail.com morbo@google.com daniel@iogearbox.net nathan@kernel.org ast@kernel.org justinstitt@google.com ndesaulniers@google.com llvm@lists.linux.dev
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
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: 8 this patch: 8
netdev/checkpatch fail ERROR: Bad function definition - int func() should probably be int func(void) WARNING: Prefer __section(name) over __attribute__((section(name))) WARNING: Prefer __used over __attribute__((used))
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-10--06-00 (tests: 962)

Commit Message

Jakub Kicinski April 9, 2024, 3:15 a.m. UTC
Non-ancient ip (iproute2-5.15.0, libbpf 0.7.0) refuses to load
the sample with maps because we don't generate BTF:

   libbpf: BTF is required, but is missing or corrupted.
   ERROR: opening BPF object file failed

Enable BTF by adding -g to clang flags. With that done
neither of the programs load:

  libbpf: prog 'func': error relocating .BTF.ext function info: -22
  libbpf: prog 'func': failed to relocate calls: -22
  libbpf: failed to load object 'ksft-net-drv/net/sample_ret0.bpf.o'

Andrii explains that this is because we don't specify
section names for the code. Add the section names, too.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/testing/selftests/net/Makefile              | 3 ++-
 tools/testing/selftests/net/bpf_offload.py        | 2 +-
 tools/testing/selftests/net/sample_map_ret0.bpf.c | 2 +-
 tools/testing/selftests/net/sample_ret0.bpf.c     | 3 +++
 4 files changed, 7 insertions(+), 3 deletions(-)

Comments

Alexei Starovoitov April 9, 2024, 5:32 p.m. UTC | #1
On Mon, Apr 8, 2024 at 8:17 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> Non-ancient ip (iproute2-5.15.0, libbpf 0.7.0) refuses to load
> the sample with maps because we don't generate BTF:
>
>    libbpf: BTF is required, but is missing or corrupted.
>    ERROR: opening BPF object file failed
>
> Enable BTF by adding -g to clang flags. With that done
> neither of the programs load:
>
>   libbpf: prog 'func': error relocating .BTF.ext function info: -22
>   libbpf: prog 'func': failed to relocate calls: -22
>   libbpf: failed to load object 'ksft-net-drv/net/sample_ret0.bpf.o'
>
> Andrii explains that this is because we don't specify
> section names for the code. Add the section names, too.
>
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
>  tools/testing/selftests/net/Makefile              | 3 ++-
>  tools/testing/selftests/net/bpf_offload.py        | 2 +-
>  tools/testing/selftests/net/sample_map_ret0.bpf.c | 2 +-
>  tools/testing/selftests/net/sample_ret0.bpf.c     | 3 +++
>  4 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
> index e8bfa715aa49..a3c781cb8367 100644
> --- a/tools/testing/selftests/net/Makefile
> +++ b/tools/testing/selftests/net/Makefile
> @@ -149,7 +149,8 @@ BPF_PROG_OBJS := $(OUTPUT)/nat6to4.o $(OUTPUT)/xdp_dummy.o \
>         $(OUTPUT)/sample_map_ret0.bpf.o $(OUTPUT)/sample_ret0.bpf.o
>
>  $(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS)
> -       $(CLANG) -O2 --target=bpf -c $< $(CCINCLUDE) $(CLANG_SYS_INCLUDES) -o $@
> +       $(CLANG) -O2 -g --target=bpf $(CCINCLUDE) $(CLANG_SYS_INCLUDES) \
> +       -c $< -o $@
>
>  $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)                    \
>            $(APIDIR)/linux/bpf.h                                               \
> diff --git a/tools/testing/selftests/net/bpf_offload.py b/tools/testing/selftests/net/bpf_offload.py
> index 174dba1a48d3..76b53ac2c8c6 100755
> --- a/tools/testing/selftests/net/bpf_offload.py
> +++ b/tools/testing/selftests/net/bpf_offload.py
> @@ -237,7 +237,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
>  def ethtool(dev, opt, args, fail=True):
>      return cmd("ethtool %s %s %s" % (opt, dev["ifname"], args), fail=fail)
>
> -def bpf_obj(name, sec=".text", path=bpf_test_dir,):
> +def bpf_obj(name, sec="xdp", path=bpf_test_dir,):
>      return "obj %s sec %s" % (os.path.join(path, name), sec)
>
>  def bpf_pinned(name):
> diff --git a/tools/testing/selftests/net/sample_map_ret0.bpf.c b/tools/testing/selftests/net/sample_map_ret0.bpf.c
> index 495990d355ef..43ca92594926 100644
> --- a/tools/testing/selftests/net/sample_map_ret0.bpf.c
> +++ b/tools/testing/selftests/net/sample_map_ret0.bpf.c
> @@ -17,7 +17,7 @@ struct {
>  } array SEC(".maps");
>
>  /* Sample program which should always load for testing control paths. */
> -SEC(".text") int func()
> +SEC("xdp") int func()

It bitrotted so much.. ouch.

For the series:
Acked-by: Alexei Starovoitov <ast@kernel.org>
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index e8bfa715aa49..a3c781cb8367 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -149,7 +149,8 @@  BPF_PROG_OBJS := $(OUTPUT)/nat6to4.o $(OUTPUT)/xdp_dummy.o \
 	$(OUTPUT)/sample_map_ret0.bpf.o $(OUTPUT)/sample_ret0.bpf.o
 
 $(BPF_PROG_OBJS): $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS)
-	$(CLANG) -O2 --target=bpf -c $< $(CCINCLUDE) $(CLANG_SYS_INCLUDES) -o $@
+	$(CLANG) -O2 -g --target=bpf $(CCINCLUDE) $(CLANG_SYS_INCLUDES) \
+	-c $< -o $@
 
 $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
 	   $(APIDIR)/linux/bpf.h					       \
diff --git a/tools/testing/selftests/net/bpf_offload.py b/tools/testing/selftests/net/bpf_offload.py
index 174dba1a48d3..76b53ac2c8c6 100755
--- a/tools/testing/selftests/net/bpf_offload.py
+++ b/tools/testing/selftests/net/bpf_offload.py
@@ -237,7 +237,7 @@  def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
 def ethtool(dev, opt, args, fail=True):
     return cmd("ethtool %s %s %s" % (opt, dev["ifname"], args), fail=fail)
 
-def bpf_obj(name, sec=".text", path=bpf_test_dir,):
+def bpf_obj(name, sec="xdp", path=bpf_test_dir,):
     return "obj %s sec %s" % (os.path.join(path, name), sec)
 
 def bpf_pinned(name):
diff --git a/tools/testing/selftests/net/sample_map_ret0.bpf.c b/tools/testing/selftests/net/sample_map_ret0.bpf.c
index 495990d355ef..43ca92594926 100644
--- a/tools/testing/selftests/net/sample_map_ret0.bpf.c
+++ b/tools/testing/selftests/net/sample_map_ret0.bpf.c
@@ -17,7 +17,7 @@  struct {
 } array SEC(".maps");
 
 /* Sample program which should always load for testing control paths. */
-SEC(".text") int func()
+SEC("xdp") int func()
 {
 	__u64 key64 = 0;
 	__u32 key = 0;
diff --git a/tools/testing/selftests/net/sample_ret0.bpf.c b/tools/testing/selftests/net/sample_ret0.bpf.c
index fec99750d6ea..1df5ca98bb65 100644
--- a/tools/testing/selftests/net/sample_ret0.bpf.c
+++ b/tools/testing/selftests/net/sample_ret0.bpf.c
@@ -1,6 +1,9 @@ 
 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
 
+#define SEC(name) __attribute__((section(name), used))
+
 /* Sample program which should always load for testing control paths. */
+SEC("xdp")
 int func()
 {
 	return 0;