aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/test_offload.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/bpf/test_offload.py')
-rwxr-xr-xtools/testing/selftests/bpf/test_offload.py135
1 files changed, 80 insertions, 55 deletions
diff --git a/tools/testing/selftests/bpf/test_offload.py b/tools/testing/selftests/bpf/test_offload.py
index d59642e70f56..84bea3985d64 100755
--- a/tools/testing/selftests/bpf/test_offload.py
+++ b/tools/testing/selftests/bpf/test_offload.py
@@ -23,6 +23,7 @@ import string
23import struct 23import struct
24import subprocess 24import subprocess
25import time 25import time
26import traceback
26 27
27logfile = None 28logfile = None
28log_level = 1 29log_level = 1
@@ -78,7 +79,9 @@ def fail(cond, msg):
78 if not cond: 79 if not cond:
79 return 80 return
80 print("FAIL: " + msg) 81 print("FAIL: " + msg)
81 log("FAIL: " + msg, "", level=1) 82 tb = "".join(traceback.extract_stack().format())
83 print(tb)
84 log("FAIL: " + msg, tb, level=1)
82 os.sys.exit(1) 85 os.sys.exit(1)
83 86
84def start_test(msg): 87def start_test(msg):
@@ -589,6 +592,15 @@ def check_verifier_log(output, reference):
589 return 592 return
590 fail(True, "Missing or incorrect message from netdevsim in verifier log") 593 fail(True, "Missing or incorrect message from netdevsim in verifier log")
591 594
595def check_multi_basic(two_xdps):
596 fail(two_xdps["mode"] != 4, "Bad mode reported with multiple programs")
597 fail("prog" in two_xdps, "Base program reported in multi program mode")
598 fail(len(two_xdps["attached"]) != 2,
599 "Wrong attached program count with two programs")
600 fail(two_xdps["attached"][0]["prog"]["id"] ==
601 two_xdps["attached"][1]["prog"]["id"],
602 "Offloaded and other programs have the same id")
603
592def test_spurios_extack(sim, obj, skip_hw, needle): 604def test_spurios_extack(sim, obj, skip_hw, needle):
593 res = sim.cls_bpf_add_filter(obj, prio=1, handle=1, skip_hw=skip_hw, 605 res = sim.cls_bpf_add_filter(obj, prio=1, handle=1, skip_hw=skip_hw,
594 include_stderr=True) 606 include_stderr=True)
@@ -600,6 +612,67 @@ def test_spurios_extack(sim, obj, skip_hw, needle):
600 include_stderr=True) 612 include_stderr=True)
601 check_no_extack(res, needle) 613 check_no_extack(res, needle)
602 614
615def test_multi_prog(sim, obj, modename, modeid):
616 start_test("Test multi-attachment XDP - %s + offload..." %
617 (modename or "default", ))
618 sim.set_xdp(obj, "offload")
619 xdp = sim.ip_link_show(xdp=True)["xdp"]
620 offloaded = sim.dfs_read("bpf_offloaded_id")
621 fail("prog" not in xdp, "Base program not reported in single program mode")
622 fail(len(xdp["attached"]) != 1,
623 "Wrong attached program count with one program")
624
625 sim.set_xdp(obj, modename)
626 two_xdps = sim.ip_link_show(xdp=True)["xdp"]
627
628 fail(xdp["attached"][0] not in two_xdps["attached"],
629 "Offload program not reported after other activated")
630 check_multi_basic(two_xdps)
631
632 offloaded2 = sim.dfs_read("bpf_offloaded_id")
633 fail(offloaded != offloaded2,
634 "Offload ID changed after loading other program")
635
636 start_test("Test multi-attachment XDP - replace...")
637 ret, _, err = sim.set_xdp(obj, "offload", fail=False, include_stderr=True)
638 fail(ret == 0, "Replaced one of programs without -force")
639 check_extack(err, "XDP program already attached.", args)
640
641 if modename == "" or modename == "drv":
642 othermode = "" if modename == "drv" else "drv"
643 start_test("Test multi-attachment XDP - detach...")
644 ret, _, err = sim.unset_xdp(othermode, force=True,
645 fail=False, include_stderr=True)
646 fail(ret == 0, "Removed program with a bad mode")
647 check_extack(err, "program loaded with different flags.", args)
648
649 sim.unset_xdp("offload")
650 xdp = sim.ip_link_show(xdp=True)["xdp"]
651 offloaded = sim.dfs_read("bpf_offloaded_id")
652
653 fail(xdp["mode"] != modeid, "Bad mode reported after multiple programs")
654 fail("prog" not in xdp,
655 "Base program not reported after multi program mode")
656 fail(xdp["attached"][0] not in two_xdps["attached"],
657 "Offload program not reported after other activated")
658 fail(len(xdp["attached"]) != 1,
659 "Wrong attached program count with remaining programs")
660 fail(offloaded != "0", "Offload ID reported with only other program left")
661
662 start_test("Test multi-attachment XDP - reattach...")
663 sim.set_xdp(obj, "offload")
664 two_xdps = sim.ip_link_show(xdp=True)["xdp"]
665
666 fail(xdp["attached"][0] not in two_xdps["attached"],
667 "Other program not reported after offload activated")
668 check_multi_basic(two_xdps)
669
670 start_test("Test multi-attachment XDP - device remove...")
671 sim.remove()
672
673 sim = NetdevSim()
674 sim.set_ethtool_tc_offloads(True)
675 return sim
603 676
604# Parse command line 677# Parse command line
605parser = argparse.ArgumentParser() 678parser = argparse.ArgumentParser()
@@ -842,7 +915,9 @@ try:
842 ret, _, err = sim.set_xdp(obj, "generic", force=True, 915 ret, _, err = sim.set_xdp(obj, "generic", force=True,
843 fail=False, include_stderr=True) 916 fail=False, include_stderr=True)
844 fail(ret == 0, "Replaced XDP program with a program in different mode") 917 fail(ret == 0, "Replaced XDP program with a program in different mode")
845 fail(err.count("File exists") != 1, "Replaced driver XDP with generic") 918 check_extack(err,
919 "native and generic XDP can't be active at the same time.",
920 args)
846 ret, _, err = sim.set_xdp(obj, "", force=True, 921 ret, _, err = sim.set_xdp(obj, "", force=True,
847 fail=False, include_stderr=True) 922 fail=False, include_stderr=True)
848 fail(ret == 0, "Replaced XDP program with a program in different mode") 923 fail(ret == 0, "Replaced XDP program with a program in different mode")
@@ -931,59 +1006,9 @@ try:
931 rm(pin_file) 1006 rm(pin_file)
932 bpftool_prog_list_wait(expected=0) 1007 bpftool_prog_list_wait(expected=0)
933 1008
934 start_test("Test multi-attachment XDP - attach...") 1009 sim = test_multi_prog(sim, obj, "", 1)
935 sim.set_xdp(obj, "offload") 1010 sim = test_multi_prog(sim, obj, "drv", 1)
936 xdp = sim.ip_link_show(xdp=True)["xdp"] 1011 sim = test_multi_prog(sim, obj, "generic", 2)
937 offloaded = sim.dfs_read("bpf_offloaded_id")
938 fail("prog" not in xdp, "Base program not reported in single program mode")
939 fail(len(ipl["xdp"]["attached"]) != 1,
940 "Wrong attached program count with one program")
941
942 sim.set_xdp(obj, "")
943 two_xdps = sim.ip_link_show(xdp=True)["xdp"]
944 offloaded2 = sim.dfs_read("bpf_offloaded_id")
945
946 fail(two_xdps["mode"] != 4, "Bad mode reported with multiple programs")
947 fail("prog" in two_xdps, "Base program reported in multi program mode")
948 fail(xdp["attached"][0] not in two_xdps["attached"],
949 "Offload program not reported after driver activated")
950 fail(len(two_xdps["attached"]) != 2,
951 "Wrong attached program count with two programs")
952 fail(two_xdps["attached"][0]["prog"]["id"] ==
953 two_xdps["attached"][1]["prog"]["id"],
954 "offloaded and drv programs have the same id")
955 fail(offloaded != offloaded2,
956 "offload ID changed after loading driver program")
957
958 start_test("Test multi-attachment XDP - replace...")
959 ret, _, err = sim.set_xdp(obj, "offload", fail=False, include_stderr=True)
960 fail(err.count("busy") != 1, "Replaced one of programs without -force")
961
962 start_test("Test multi-attachment XDP - detach...")
963 ret, _, err = sim.unset_xdp("drv", force=True,
964 fail=False, include_stderr=True)
965 fail(ret == 0, "Removed program with a bad mode")
966 check_extack(err, "program loaded with different flags.", args)
967
968 sim.unset_xdp("offload")
969 xdp = sim.ip_link_show(xdp=True)["xdp"]
970 offloaded = sim.dfs_read("bpf_offloaded_id")
971
972 fail(xdp["mode"] != 1, "Bad mode reported after multiple programs")
973 fail("prog" not in xdp,
974 "Base program not reported after multi program mode")
975 fail(xdp["attached"][0] not in two_xdps["attached"],
976 "Offload program not reported after driver activated")
977 fail(len(ipl["xdp"]["attached"]) != 1,
978 "Wrong attached program count with remaining programs")
979 fail(offloaded != "0", "offload ID reported with only driver program left")
980
981 start_test("Test multi-attachment XDP - device remove...")
982 sim.set_xdp(obj, "offload")
983 sim.remove()
984
985 sim = NetdevSim()
986 sim.set_ethtool_tc_offloads(True)
987 1012
988 start_test("Test mixing of TC and XDP...") 1013 start_test("Test mixing of TC and XDP...")
989 sim.tc_add_ingress() 1014 sim.tc_add_ingress()