The generic XDP tests in ping.py only cover XDP_PASS. Add a test that redirects all packets to a cpumap with an xdp.frags program. It runs the same ping and TCP checks as the other tests. xdp_cpumap.bpf.c is a new program. It redirects every packet to entry 0 of a cpumap. It is not added to xdp_dummy.bpf.c. test_xdp_offload loads xdp_dummy.bpf.o in offload mode, which would fail on a cpumap. The new test extends _set_xdp_generic_mb_on() with two optional arguments. The first argument, prog, is the XDP program file name, with default "xdp_dummy.bpf.o". The second argument, on_attach, is a callback function, with default None. on_attach runs right after the program is attached. The new test passes "xdp_cpumap.bpf.o" to prog and a lambda that adds the cpumap entry to on_attach. Without the previous patch, the new test fails on netdevsim because ping gets no reply. Signed-off-by: Tianyi Gao --- tools/testing/selftests/drivers/net/ping.py | 27 ++++++++++++++++--- .../selftests/net/lib/xdp_cpumap.bpf.c | 19 +++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/net/lib/xdp_cpumap.bpf.c diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py index da3623c5e8a9..a0de9a98d906 100755 --- a/tools/testing/selftests/drivers/net/ping.py +++ b/tools/testing/selftests/drivers/net/ping.py @@ -7,7 +7,7 @@ from lib.py import ksft_run, ksft_exit from lib.py import ksft_eq, KsftSkipEx, KsftFailEx from lib.py import EthtoolFamily, NetDrvEpEnv from lib.py import bkg, cmd, wait_port_listen, rand_port -from lib.py import defer, ethtool, ip +from lib.py import bpf_map_set, defer, ethtool, ip no_sleep=False @@ -75,16 +75,23 @@ def _set_xdp_generic_sb_on(cfg) -> None: if no_sleep != True: time.sleep(10) -def _set_xdp_generic_mb_on(cfg) -> None: - prog = cfg.net_lib_dir / "xdp_dummy.bpf.o" +def _set_xdp_generic_mb_on(cfg, prog="xdp_dummy.bpf.o", on_attach=None) -> None: + prog = cfg.net_lib_dir / prog cmd(f"ip link set dev {cfg.remote_ifname} mtu 9000", shell=True, host=cfg.remote) defer(ip, f"link set dev {cfg.remote_ifname} mtu 1500", host=cfg.remote) ip("link set dev %s mtu 9000 xdpgeneric obj %s sec xdp.frags" % (cfg.ifname, prog)) defer(ip, f"link set dev {cfg.ifname} mtu 1500 xdpgeneric off") + if on_attach: + on_attach() if no_sleep != True: time.sleep(10) +def _set_xdp_generic_cpumap_mb_on(cfg) -> None: + cfg.require_cmd("bpftool") + _set_xdp_generic_mb_on(cfg, "xdp_cpumap.bpf.o", + on_attach=lambda: bpf_map_set("map_cpumap", 0, 2048)) + def _set_xdp_native_sb_on(cfg) -> None: prog = cfg.net_lib_dir / "xdp_dummy.bpf.o" cmd(f"ip link set dev {cfg.remote_ifname} mtu 1500", shell=True, host=cfg.remote) @@ -192,6 +199,19 @@ def test_xdp_generic_mb(cfg, netnl) -> None: _test_v6(cfg) _test_tcp(cfg) +def test_xdp_generic_cpumap_mb(cfg, netnl) -> None: + """Redirect all packets to a cpumap from a generic xdp.frags program.""" + _schedule_checksum_reset(cfg, netnl) + _set_xdp_generic_cpumap_mb_on(cfg) + _set_offload_checksum(cfg, netnl, "off") + _test_v4(cfg) + _test_v6(cfg) + _test_tcp(cfg) + _set_offload_checksum(cfg, netnl, "on") + _test_v4(cfg) + _test_v6(cfg) + _test_tcp(cfg) + def test_xdp_native_sb(cfg, netnl) -> None: _schedule_checksum_reset(cfg, netnl) _set_xdp_native_sb_on(cfg) @@ -230,6 +250,7 @@ def main() -> None: test_default_v6, test_xdp_generic_sb, test_xdp_generic_mb, + test_xdp_generic_cpumap_mb, test_xdp_native_sb, test_xdp_native_mb, test_xdp_offload], diff --git a/tools/testing/selftests/net/lib/xdp_cpumap.bpf.c b/tools/testing/selftests/net/lib/xdp_cpumap.bpf.c new file mode 100644 index 000000000000..150352343114 --- /dev/null +++ b/tools/testing/selftests/net/lib/xdp_cpumap.bpf.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_CPUMAP); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} map_cpumap SEC(".maps"); + +SEC("xdp.frags") +int xdp_cpumap_prog_frags(struct xdp_md *ctx) +{ + return bpf_redirect_map(&map_cpumap, 0, XDP_DROP); +} + +char _license[] SEC("license") = "GPL"; -- 2.55.0