diff --git a/targets/nikss/test/perf/simple.py b/targets/nikss/test/perf/simple.py index 9c648250..5ac65006 100644 --- a/targets/nikss/test/perf/simple.py +++ b/targets/nikss/test/perf/simple.py @@ -1,6 +1,9 @@ from ptf import testutils as ptfutils # type: ignore -from backends.p4tools.modules.flay.targets.nikss.ebpf_ptf_test import P4EbpfTest +from backends.p4tools.modules.flay.targets.nikss.test.ebpf_ptf_test import ( + TEST_PIPELINE_ID, + P4EbpfTest, +) PORT0 = 0 PORT1 = 1 @@ -16,5 +19,6 @@ class SimpleTest(P4EbpfTest): def runTest(self) -> None: # check no connectivity if switching rules are not installed pkt = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + rcvd_pkt = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00" ptfutils.send_packet(self, PORT0, pkt) - ptfutils.verify_packet(self, pkt, PORT1) + ptfutils.verify_packet(self, rcvd_pkt, PORT1) diff --git a/targets/nikss/test/run-flay-ebpf-ptf-benchmark.py b/targets/nikss/test/run-flay-ebpf-ptf-benchmark.py new file mode 100755 index 00000000..e5be3848 --- /dev/null +++ b/targets/nikss/test/run-flay-ebpf-ptf-benchmark.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 + +import argparse +import logging +import os +import sys +import tempfile +from pathlib import Path +from typing import Any, Optional + +import run_ebpf_ptf_test as ebpf_ptf_test + +PARSER = argparse.ArgumentParser() +PARSER.add_argument( + "rootdir", + help="The root directory of the compiler source tree." + "This is used to import P4C's Python libraries", +) +PARSER.add_argument("p4_file", help="the p4 file to process") +PARSER.add_argument( + "-tf", + "--testfile", + dest="testfile", + help="The path for the ptf py file for this test.", +) +PARSER.add_argument( + "-td", + "--testdir", + dest="testdir", + help="The location of the test directory.", +) +PARSER.add_argument( + "-b", + "--nocleanup", + action="store_true", + dest="nocleanup", + help="Do not remove temporary results for failing tests.", +) +PARSER.add_argument( + "-n", + "--num-ifaces", + default=6, + dest="num_ifaces", + help="How many virtual interfaces to create.", +) +PARSER.add_argument( + "-ll", + "--log_level", + dest="log_level", + default="WARNING", + choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"], + help="The log level to choose.", +) +PARSER.add_argument( + "-fb", "--flay_binary", dest="flay_binary", default="flay", help="The path to the flay binary." +) +PARSER.add_argument( + "--config-update-pattern", + dest="config_update_pattern", + default=".*", + help="The config update pattern.", +) + +# Parse options and process argv +ARGS, ARGV = PARSER.parse_known_args() + +# Append the root directory to the import path. +FILE_DIR = Path(__file__).resolve().parent +ROOT_DIR = Path(ARGS.rootdir).absolute() +sys.path.append(str(ROOT_DIR)) + +from tools import testutils # pylint: disable=wrong-import-position + + +class Options(ebpf_ptf_test.Options): + """Options for this testing script. Usually correspond to command line inputs.""" + + def __init__(self) -> None: + super().__init__() + + +def create_options(test_args: Any) -> Optional[Options]: + """Parse the input arguments and create a processed options object.""" + options = Options() + result = testutils.check_if_file(test_args.p4_file) + if not result: + return None + options.p4_file = result + testfile = test_args.testfile + if not testfile: + testutils.log.info("No test file provided. Checking for file in folder.") + testfile = options.p4_file.with_suffix(".py") + result = testutils.check_if_file(testfile) + if not result: + return None + options.testfile = Path(result) + testdir = test_args.testdir + if not testdir: + testutils.log.info("No test directory provided. Generating temporary folder.") + testdir = tempfile.mkdtemp(dir=Path(".").absolute()) + # Generous permissions because the program is usually edited by sudo. + os.chmod(testdir, 0o755) + options.testdir = Path(testdir) + options.rootdir = Path(test_args.rootdir) + options.num_ifaces = test_args.num_ifaces + + # Configure logging. + logging.basicConfig( + filename=options.testdir.joinpath("test.log"), + format="%(levelname)s: %(message)s", + level=getattr(logging, test_args.log_level), + filemode="w", + ) + stderr_log = logging.StreamHandler() + stderr_log.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) + logging.getLogger().addHandler(stderr_log) + return options + + +if __name__ == "__main__": + test_options = create_options(ARGS) + if not test_options: + sys.exit(testutils.FAILURE) + + if not testutils.check_root(): + testutils.log.error("This script requires root privileges; Exiting.") + sys.exit(1) + + # Run the test with the extracted options + test_result = ebpf_ptf_test.run_test(test_options) + if not (ARGS.nocleanup or test_result != testutils.SUCCESS): + testutils.log.info("Removing temporary test directory.") + testutils.del_dir(test_options.testdir) + sys.exit(test_result) diff --git a/targets/nikss/test/run-ebpf-ptf-test.py b/targets/nikss/test/run_ebpf_ptf_test.py similarity index 100% rename from targets/nikss/test/run-ebpf-ptf-test.py rename to targets/nikss/test/run_ebpf_ptf_test.py