Skip to contentSkip to conversation

selftests: move bpf-offload test from bpf to net

PATCH Applied Jakub Kicinski 5 participants 11 messages Apr 9, 2024 – Apr 10, 2024

Subsystems (5)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover selftests: move bpf-offload test from bpf to net

The test_offload.py test fits in networking and bpf equally well. We started adding more Python tests in networking and some of the code in test_offload.py can be reused, so move it to networking. Looks like it bit rotted over time and some fixes are needed.

Admittedly more code could be extracted but I only had the time for a minor cleanup :(

Jakub Kicinski (4):
selftests: move bpf-offload test from bpf to net
selftests: net: bpf_offload: wait for maps
selftests: net: declare section names for bpf_offload
selftests: net: reuse common code in bpf_offload

 tools/testing/selftests/bpf/Makefile          |   1 -
 tools/testing/selftests/net/Makefile          |  11 +-
 .../test_offload.py => net/bpf_offload.py}    | 138 +++++-------------
 tools/testing/selftests/net/lib/py/nsim.py    |   9 +-
 .../sample_map_ret0.bpf.c}                    |   2 +-
 .../sample_ret0.c => net/sample_ret0.bpf.c}   |   3 +
 6 files changed, 57 insertions(+), 107 deletions(-)
 rename tools/testing/selftests/{bpf/test_offload.py => net/bpf_offload.py} (93%)
 rename tools/testing/selftests/{bpf/progs/sample_map_ret0.c => net/sample_map_ret0.bpf.c} (96%)
 rename tools/testing/selftests/{bpf/progs/sample_ret0.c => net/sample_ret0.bpf.c} (70%)
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH 1/4 selftests: move bpf-offload test from bpf to net

We're building more python tests on the netdev side, and some of the classes from the venerable BPF offload tests can be reused.

---
 tools/testing/selftests/bpf/Makefile                      | 1 -
 tools/testing/selftests/net/Makefile                      | 8 +++++++-
 .../selftests/{bpf/test_offload.py => net/bpf_offload.py} | 0
 .../progs/sample_map_ret0.c => net/sample_map_ret0.bpf.c} | 0
 .../{bpf/progs/sample_ret0.c => net/sample_ret0.bpf.c}    | 0
 5 files changed, 7 insertions(+), 2 deletions(-)
 rename tools/testing/selftests/{bpf/test_offload.py => net/bpf_offload.py} (100%)
 rename tools/testing/selftests/{bpf/progs/sample_map_ret0.c => net/sample_map_ret0.bpf.c} (100%)
 rename tools/testing/selftests/{bpf/progs/sample_ret0.c => net/sample_ret0.bpf.c} (100%)
tools/testing/selftests/bpf/Makefile +0 -1
index 3b9eb40d6343..b0be07f29dde 100644
@@ -102,7 +102,6 @@ TEST_PROGS := test_kmod.sh \
 	test_xdp_redirect_multi.sh \
 	test_xdp_meta.sh \
 	test_xdp_veth.sh \
-	test_offload.py \
 	test_sock_addr.sh \
 	test_tunnel.sh \
 	test_lwt_seg6local.sh \
tools/testing/selftests/net/Makefile +7 -1
index 5e34c93aa51b..e8bfa715aa49 100644
@@ -84,6 +84,8 @@ TEST_GEN_FILES += sctp_hello
 TEST_GEN_FILES += csum
 TEST_GEN_FILES += nat6to4.o
 TEST_GEN_FILES += xdp_dummy.o
+TEST_GEN_FILES += sample_ret0.bpf.o
+TEST_GEN_FILES += sample_map_ret0.bpf.o
 TEST_GEN_FILES += ip_local_port_range
 TEST_GEN_FILES += bind_wildcard
 TEST_PROGS += test_vxlan_mdb.sh
@@ -93,6 +95,7 @@ TEST_PROGS += test_bridge_backup_port.sh
 TEST_PROGS += fdb_flush.sh
 TEST_PROGS += fq_band_pktlimit.sh
 TEST_PROGS += vlan_hw_filter.sh
+TEST_PROGS += bpf_offload.py
 
 TEST_FILES := settings
 TEST_FILES += in_netns.sh lib.sh net_helper.sh setup_loopback.sh setup_veth.sh
@@ -142,7 +145,10 @@ endif
 
 CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
 
-$(OUTPUT)/nat6to4.o $(OUTPUT)/xdp_dummy.o: $(OUTPUT)/%.o : %.c $(BPFOBJ) | $(MAKE_DIRS)
+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 $@
 
 $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
tools/testing/selftests/net/bpf_offload.py renamed from tools/testing/selftests/bpf/test_offload.py +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/test_offload.py
rename to tools/testing/selftests/net/bpf_offload.py
tools/testing/selftests/net/sample_map_ret0.bpf.c renamed from tools/testing/selftests/bpf/progs/sample_map_ret0.c +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/progs/sample_map_ret0.c
rename to tools/testing/selftests/net/sample_map_ret0.bpf.c
tools/testing/selftests/net/sample_ret0.bpf.c renamed from tools/testing/selftests/bpf/progs/sample_ret0.c +0 -0
similarity index 100%
rename from tools/testing/selftests/bpf/progs/sample_ret0.c
rename to tools/testing/selftests/net/sample_ret0.bpf.c
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH 2/4 selftests: net: bpf_offload: wait for maps

In reply to Jakub Kicinski

Maps are removed asynchronously. Either there's a bigger delay now or the test has always been flaky. Retry waiting in the loop.

---
 tools/testing/selftests/net/bpf_offload.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/net/bpf_offload.py +8 -8
index 6157f884d091..174dba1a48d3 100755
@@ -201,11 +201,11 @@ netns = [] # net namespaces to be removed
         time.sleep(0.05)
     raise Exception("Time out waiting for program counts to stabilize want %d, have %d" % (expected, nprogs))
 
-def bpftool_map_list_wait(expected=0, n_retry=20):
+def bpftool_map_list_wait(expected=0, n_retry=20, ns=""):
     for i in range(n_retry):
-        nmaps = len(bpftool_map_list())
-        if nmaps == expected:
-            return
+        maps = bpftool_map_list(ns=ns)
+        if len(maps) == expected:
+            return maps
         time.sleep(0.05)
     raise Exception("Time out waiting for map counts to stabilize want %d, have %d" % (expected, nmaps))
 
@@ -605,7 +605,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
     return file_name, bpf_pinned(file_name)
 
 def pin_map(file_name, idx=0, expected=1):
-    maps = bpftool_map_list(expected=expected)
+    maps = bpftool_map_list_wait(expected=expected)
     m = maps[idx]
     bpftool("map pin id %d %s" % (m["id"], file_name))
     files.append(file_name)
@@ -618,7 +618,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
     ret, err = bpftool("prog show pin %s" % (prog_file), fail=False)
     fail(ret != 0, "failed to show prog with removed device")
 
-    bpftool_map_list(expected=0)
+    bpftool_map_list_wait(expected=0)
     ret, err = bpftool("map show pin %s" % (map_file), fail=False)
     fail(ret == 0, "Showing map with removed device did not fail")
     fail(err["error"].find("No such device") == -1,
@@ -642,7 +642,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
     else:
         fail("ifname" in dev.keys(), "Ifname is reported for other ns")
 
-    maps = bpftool_map_list(expected=2, ns=ns)
+    maps = bpftool_map_list_wait(expected=2, ns=ns)
     for m in maps:
         fail("dev" not in m.keys(), "Device parameters not reported")
         fail(dev != m["dev"], "Map's device different than program's")
@@ -1206,7 +1206,7 @@ netns = []
 
     start_test("Test map update (no flags)...")
     sim.set_xdp(map_obj, "offload", JSON=False) # map fixup msg breaks JSON
-    maps = bpftool_map_list(expected=2)
+    maps = bpftool_map_list_wait(expected=2)
     array = maps[0] if maps[0]["type"] == "array" else maps[1]
     htab = maps[0] if maps[0]["type"] == "hash" else maps[1]
     for m in maps:
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH 3/4 selftests: net: declare section names for bpf_offload

In reply to Jakub Kicinski

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.

---
 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(-)
tools/testing/selftests/net/Makefile +2 -1
index e8bfa715aa49..a3c781cb8367 100644
@@ -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					       \
tools/testing/selftests/net/bpf_offload.py +1 -1
index 174dba1a48d3..76b53ac2c8c6 100755
@@ -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):
tools/testing/selftests/net/sample_map_ret0.bpf.c +1 -1
index 495990d355ef..43ca92594926 100644
@@ -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;
tools/testing/selftests/net/sample_ret0.bpf.c +3 -0
index fec99750d6ea..1df5ca98bb65 100644
@@ -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;
Signature · 1 line
2.44.0
author maintainer
#5 lore raw

PATCH 4/4 selftests: net: reuse common code in bpf_offload

In reply to Jakub Kicinski

net/lib/py/nsim.py already contains the most useful parts of the netdevsim wrapper classes. Reuse them.

---
 tools/testing/selftests/net/bpf_offload.py | 120 +++++----------------
 tools/testing/selftests/net/lib/py/nsim.py |   9 +-
 2 files changed, 35 insertions(+), 94 deletions(-)
tools/testing/selftests/net/bpf_offload.py +28 -92
index 76b53ac2c8c6..3efe44f6e92a 100755
@@ -29,6 +29,9 @@ import subprocess
 import time
 import traceback
 
+from lib.py import NetdevSim, NetdevSimDev
+
+
 logfile = None
 log_level = 1
 skip_extack = False
@@ -145,8 +148,10 @@ netns = [] # net namespaces to be removed
     if JSON:
         params += "%s " % (flags["json"])
 
-    if ns != "":
+    if ns:
         ns = "ip netns exec %s " % (ns)
+    elif ns is None:
+        ns = ""
 
     if include_stderr:
         ret, stdout, stderr = cmd(ns + name + " " + params + args,
@@ -334,72 +339,16 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
 
         return dfs
 
-class NetdevSimDev:
+class BpfNetdevSimDev(NetdevSimDev):
     """
     Class for netdevsim bus device and its attributes.
     """
-    @staticmethod
-    def ctrl_write(path, val):
-        fullpath = os.path.join("/sys/bus/netdevsim/", path)
-        try:
-            with open(fullpath, "w") as f:
-                f.write(val)
-        except OSError as e:
-            log("WRITE %s: %r" % (fullpath, val), -e.errno)
-            raise e
-        log("WRITE %s: %r" % (fullpath, val), 0)
-
-    def __init__(self, port_count=1):
-        addr = 0
-        while True:
-            try:
-                self.ctrl_write("new_device", "%u %u" % (addr, port_count))
-            except OSError as e:
-                if e.errno == errno.ENOSPC:
-                    addr += 1
-                    continue
-                raise e
-            break
-        self.addr = addr
-
-        # As probe of netdevsim device might happen from a workqueue,
-        # so wait here until all netdevs appear.
-        self.wait_for_netdevs(port_count)
-
-        ret, out = cmd("udevadm settle", fail=False)
-        if ret:
-            raise Exception("udevadm settle failed")
-        ifnames = self.get_ifnames()
-
+    def __init__(self, port_count=1, ns=None):
+        super().__init__(port_count, ns=ns)
         devs.append(self)
-        self.dfs_dir = "/sys/kernel/debug/netdevsim/netdevsim%u/" % addr
 
-        self.nsims = []
-        for port_index in range(port_count):
-            self.nsims.append(NetdevSim(self, port_index, ifnames[port_index]))
-
-    def get_ifnames(self):
-        ifnames = []
-        listdir = os.listdir("/sys/bus/netdevsim/devices/netdevsim%u/net/" % self.addr)
-        for ifname in listdir:
-            ifnames.append(ifname)
-        ifnames.sort()
-        return ifnames
-
-    def wait_for_netdevs(self, port_count):
-        timeout = 5
-        timeout_start = time.time()
-
-        while True:
-            try:
-                ifnames = self.get_ifnames()
-            except FileNotFoundError as e:
-                ifnames = []
-            if len(ifnames) == port_count:
-                break
-            if time.time() < timeout_start + timeout:
-                continue
-            raise Exception("netdevices did not appear within timeout")
+    def _make_port(self, port_index, ifname):
+        return BpfNetdevSim(self, port_index, ifname, self.ns)
 
     def dfs_num_bound_progs(self):
         path = os.path.join(self.dfs_dir, "bpf_bound_progs")
@@ -415,33 +364,20 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
         return progs
 
     def remove(self):
-        self.ctrl_write("del_device", "%u" % (self.addr, ))
+        super().remove()
         devs.remove(self)
 
-    def remove_nsim(self, nsim):
-        self.nsims.remove(nsim)
-        self.ctrl_write("devices/netdevsim%u/del_port" % (self.addr, ),
-                        "%u" % (nsim.port_index, ))
 
-class NetdevSim:
+class BpfNetdevSim(NetdevSim):
     """
     Class for netdevsim netdevice and its attributes.
     """
 
-    def __init__(self, nsimdev, port_index, ifname):
-        # In case udev renamed the netdev to according to new schema,
-        # check if the name matches the port_index.
-        nsimnamere = re.compile("eni\d+np(\d+)")
-        match = nsimnamere.match(ifname)
-        if match and int(match.groups()[0]) != port_index + 1:
-            raise Exception("netdevice name mismatches the expected one")
+    def __init__(self, nsimdev, port_index, ifname, ns=None):
+        super().__init__(nsimdev, port_index, ifname, ns=ns)
 
-        self.nsimdev = nsimdev
-        self.port_index = port_index
-        self.ns = ""
         self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index)
         self.dfs_refresh()
-        _, [self.dev] = ip("link show dev %s" % ifname)
 
     def __getitem__(self, key):
         return self.dev[key]
@@ -468,7 +404,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
         raise Exception("Time out waiting for program counts to stabilize want %d/%d, have %d bound, %d loaded" % (bound, total, nbound, nprogs))
 
     def set_ns(self, ns):
-        name = "1" if ns == "" else ns
+        name = ns if ns else "1"
         ip("link set dev %s netns %s" % (self.dev["ifname"], name), ns=self.ns)
         self.ns = ns
 
@@ -744,7 +680,7 @@ def bpftool_prog_load(sample, file_name, maps=[], prog_type="xdp", dev=None,
     start_test("Test multi-attachment XDP - device remove...")
     simdev.remove()
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.set_ethtool_tc_offloads(True)
     return [simdev, sim]
@@ -809,13 +745,13 @@ netns = []
     bytecode = bpf_bytecode("1,6 0 0 4294967295,")
 
     start_test("Test destruction of generic XDP...")
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.set_xdp(obj, "generic")
     simdev.remove()
     bpftool_prog_list_wait(expected=0)
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.tc_add_ingress()
 
@@ -967,7 +903,7 @@ netns = []
     simdev.remove()
     bpftool_prog_list_wait(expected=0)
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.set_ethtool_tc_offloads(True)
 
@@ -976,7 +912,7 @@ netns = []
     simdev.remove()
     bpftool_prog_list_wait(expected=0)
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.set_ethtool_tc_offloads(True)
 
@@ -1080,7 +1016,7 @@ netns = []
     bpftool_prog_list_wait(expected=0)
 
     start_test("Test attempt to use a program for a wrong device...")
-    simdev2 = NetdevSimDev()
+    simdev2 = BpfNetdevSimDev()
     sim2, = simdev2.nsims
     sim2.set_xdp(obj, "offload")
     pin_file, pinned = pin_prog("/sys/fs/bpf/tmp")
@@ -1169,7 +1105,7 @@ netns = []
     clean_up()
     bpftool_prog_list_wait(expected=0)
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     map_obj = bpf_obj("sample_map_ret0.bpf.o")
     start_test("Test loading program with maps...")
@@ -1201,7 +1137,7 @@ netns = []
     clean_up()
     bpftool_prog_list_wait(expected=0)
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
 
     start_test("Test map update (no flags)...")
@@ -1285,14 +1221,14 @@ netns = []
     bpftool_map_list_wait(expected=0)
     simdev.remove()
 
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.set_xdp(map_obj, "offload", JSON=False) # map fixup msg breaks JSON
     simdev.remove()
     bpftool_map_list_wait(expected=0)
 
     start_test("Test map creation fail path...")
-    simdev = NetdevSimDev()
+    simdev = BpfNetdevSimDev()
     sim, = simdev.nsims
     sim.dfs["bpf_map_accept"] = "N"
     ret, _ = sim.set_xdp(map_obj, "offload", JSON=False, fail=False)
@@ -1302,9 +1238,9 @@ netns = []
     simdev.remove()
 
     start_test("Test multi-dev ASIC program reuse...")
-    simdevA = NetdevSimDev()
+    simdevA = BpfNetdevSimDev()
     simA, = simdevA.nsims
-    simdevB = NetdevSimDev(3)
+    simdevB = BpfNetdevSimDev(3)
     simB1, simB2, simB3 = simdevB.nsims
     sims = (simA, simB1, simB2, simB3)
     simB = (simB1, simB2, simB3)
tools/testing/selftests/net/lib/py/nsim.py +7 -2
index b2d696e12805..97457aca7e08 100644
@@ -21,8 +21,11 @@ from .utils import cmd, ip
         if match and int(match.groups()[0]) != port_index + 1:
             raise Exception("netdevice name mismatches the expected one")
 
+        self.ifname = ifname
         self.nsimdev = nsimdev
         self.port_index = port_index
+        self.ns = ns
+        self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index)
         ret = ip("-j link show dev %s" % ifname, ns=ns)
         self.dev = json.loads(ret.stdout)[0]
 
@@ -79,8 +82,10 @@ from .utils import cmd, ip
 
         self.nsims = []
         for port_index in range(port_count):
-            self.nsims.append(NetdevSim(self, port_index, ifnames[port_index],
-                                        ns=ns))
+            self.nsims.append(self._make_port(port_index, ifnames[port_index]))
+
+    def _make_port(self, port_index, ifname):
+        return NetdevSim(self, port_index, ifname, self.ns)
 
     def get_ifnames(self):
         ifnames = []
Signature · 1 line
2.44.0
Acked-by on the v1 series
#6 lore raw
In reply to Jakub Kicinski
Quoted 8 lines · Jakub Kicinski

The test_offload.py test fits in networking and bpf equally well. We started adding more Python tests in networking and some of the code in test_offload.py can be reused, so move it to networking. Looks like it bit rotted over time and some fixes are needed.

Admittedly more code could be extracted but I only had the time for a minor cleanup :(

Far too often I've seen this test broken because it's not in the CI :-( Hope you can put it in the netdev one so we get a better signal.

author maintainer
#7 lore raw
Quoted 1 line · On Tue, 9 Apr 2024 09:10:04 -0700 Stanislav Fomichev wrote:

Thanks!

Quoted 2 lines

Far too often I've seen this test broken because it's not in the CI :-( Hope you can put it in the netdev one so we get a better signal.

We use kselftest infra directly, so they got auto-included. But I gotta investigate what's going on on the runner because it seems to be failing right now:
https://netdev.bots.linux.dev/contest.html?test=bpf-offload-py

Acked-by on v1 3/4
#4 lore raw

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

In reply to Jakub Kicinski
Quoted 57 lines · Jakub Kicinski

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.

---
 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(-)
tools/testing/selftests/net/Makefile +2 -1
index e8bfa715aa49..a3c781cb8367 100644
@@ -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                                               \
tools/testing/selftests/net/bpf_offload.py +1 -1
index 174dba1a48d3..76b53ac2c8c6 100755
@@ -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):
tools/testing/selftests/net/sample_map_ret0.bpf.c +0 -0
index 495990d355ef..43ca92594926 100644

tools/testing/selftests/net/sample_map_ret0.bpf.c struct { · line 20

Quoted 5 lines
tools/testing/selftests/net/sample_map_ret0.bpf.c +1 -1
@@ -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:

reviewer Acked-by on the v1 series
#9 lore raw
In reply to Jakub Kicinski
Quoted 5 lines · Jakub Kicinski

The test_offload.py test fits in networking and bpf equally well. We started adding more Python tests in networking and some of the code in test_offload.py can be reused, so move it to networking. Looks like it bit rotted over time and some fixes are needed.

author maintainer
#8 lore raw
In reply to Jakub Kicinski
Quoted 7 lines · On Tue, 9 Apr 2024 09:19:08 -0700 Jakub Kicinski wrote:
Quoted 2 lines

Far too often I've seen this test broken because it's not in the CI :-( Hope you can put it in the netdev one so we get a better signal.

We use kselftest infra directly, so they got auto-included. But I gotta investigate what's going on on the runner because it seems to be failing right now:
https://netdev.bots.linux.dev/contest.html?test=bpf-offload-py

FWIW looks like iproute2 on the worker was built without libbpf support. Fixed now, let's see if it turns green :)

#10 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Mon, 8 Apr 2024 20:15:45 -0700 you wrote:

The test_offload.py test fits in networking and bpf equally well. We started adding more Python tests in networking and some of the code in test_offload.py can be reused, so move it to networking. Looks like it bit rotted over time and some fixes are needed.

Admittedly more code could be extracted but I only had the time for a minor cleanup :(

[...]

Here is the summary with links:
- [net-next,1/4] selftests: move bpf-offload test from bpf to net

    https://git.kernel.org/netdev/net-next/c/e59f0e93e92e
  - [net-next,2/4] selftests: net: bpf_offload: wait for maps
    https://git.kernel.org/netdev/net-next/c/fc50c698c28b
  - [net-next,3/4] selftests: net: declare section names for bpf_offload
    https://git.kernel.org/netdev/net-next/c/b1c2ce11d428
  - [net-next,4/4] selftests: net: reuse common code in bpf_offload
    https://git.kernel.org/netdev/net-next/c/6ce2b689932b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html