Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First take at p4tc automated tests #5011

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement automated test framework for the tc backend
First take at implementing the automated testing code for tc backend.
In a nutshell, what it's doing is expanding on the already existing
run-tc-test.py script. The expansion consists of:

- Compiling the generated C files to eBPF bytecode
- Extracting a P4TC kernel image from github
- Compiling P4TC's version of iproute2
- Spawning a VM using virtme to boot the P4TC kernel
- Executing the template script generated by the compiler
- Loading the generated eBPF parser and control blocks binaries using a
  TC P4 filter
- Parsing an STF in the samples directory detailing what packets to
  send/expect and what runtime rules to load
- Loading any specified runtime rules
- Sending the packets using scapy
- Verifying that the sent packets (and eventual received packets from
  the p4tc pipeline) and correct according to the STF file

The commands are sent to the VM using ssh through a bridge that connects
the host to the VM
After the test is finished both the bridge and the VM are destroyed

We also added an example (arp_responder) to exercise the testing framework

Signed-off-by: Victor Nogueira <victor@mojatatu.com>
  • Loading branch information
vbnogueira committed Dec 18, 2024
commit cfcb5ed350935352b29a305f91b572b011871c68
3 changes: 2 additions & 1 deletion .github/workflows/ci-test-fedora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ jobs:

- name: Run p4c tests (Fedora Linux)
run: |
export PATH="$HOME/.local/bin:$PATH"; ctest --output-on-failure --schedule-random
# Avoid running p4tc stf tests for now
export PATH="$HOME/.local/bin:$PATH"; ctest --output-on-failure --schedule-random -E "p4tc_samples_stf|p4tc_cleanup|p4tc_setup"
working-directory: ./build
4 changes: 2 additions & 2 deletions .github/workflows/ci-test-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Run tests (MacOS)
run: |
source ~/.bash_profile
ctest --output-on-failure --schedule-random -E "bpf|ubpf|testgen|smith"
ctest --output-on-failure --schedule-random -E "bpf|ubpf|testgen|smith|p4tc"
working-directory: ./build

# Build and test p4c on MacOS 13 on x86.
Expand Down Expand Up @@ -105,5 +105,5 @@ jobs:
- name: Run tests (MacOS)
run: |
source ~/.bash_profile
ctest --output-on-failure --schedule-random -E "bpf|ubpf|testgen|smith"
ctest --output-on-failure --schedule-random -E "bpf|ubpf|testgen|smith|p4tc"
working-directory: ./build
59 changes: 59 additions & 0 deletions backends/tc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,49 @@
# and limitations under the License.
#*****************************************************************************/

if(NOT APPLE)
# Fetch and declare the libbpf library. Print out download state while setting up libbpf.
set(FETCHCONTENT_QUIET_PREV ${FETCHCONTENT_QUIET})
set(FETCHCONTENT_QUIET OFF)
fetchcontent_declare(
p4cbpfrepo
URL https://github.com/libbpf/libbpf/archive/refs/tags/v1.5.0.tar.gz
URL_HASH SHA256=53492aff6dd47e4da04ef5e672d753b9743848bdb38e9d90eafbe190b7983c44
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/runtime/libbpf
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
fetchcontent_makeavailable(p4cbpfrepo)
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
message("Building libbpf...")
execute_process(
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/runtime/build-libbpf
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND_ECHO STDOUT
)
message("Done with setting up libbpf for P4C.")

fetchcontent_declare(
iproute2repo
URL https://github.com/p4tc-dev/iproute2-p4tc-pub/archive/refs/tags/release-v17-rc6.tar.gz
URL_HASH SHA256=624c32a571f9f30d1070d9b23e96121ac79f9273df9ff6db4ee6d034ab983c5d
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/runtime/iproute2-p4tc-pub
USES_TERMINAL_DOWNLOAD TRUE
GIT_PROGRESS TRUE
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
fetchcontent_makeavailable(iproute2repo)
set(FETCHCONTENT_QUIET ${FETCHCONTENT_QUIET_PREV})
message("Building iproute2...")
execute_process(
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/runtime/build-iproute2 ${CMAKE_CURRENT_SOURCE_DIR}/runtime
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND_ECHO STDOUT
)
message("Done with setting up iproute2 for P4C.")
endif()

set(P4TC_BACKEND_SOURCES
backend.cpp
ebpfCodeGen.cpp
Expand Down Expand Up @@ -122,4 +165,20 @@ set(P4TC_COMPILER_DRIVER "${CMAKE_CURRENT_SOURCE_DIR}/run-tc-test.py")
set (P4_16_SUITES
"${P4C_SOURCE_DIR}/testdata/p4tc_samples/*.p4")

# Setup fixture
add_test(NAME p4tc_setup COMMAND bash ${P4C_SOURCE_DIR}/backends/tc/runtime/setup "https://api.github.com/repos/p4tc-dev/linux-p4tc-pub/releases/latest")
set_tests_properties(p4tc_setup PROPERTIES FIXTURES_SETUP P4TCFixture)

add_test(NAME p4tc_cleanup COMMAND bash ${P4C_SOURCE_DIR}/backends/tc/runtime/cleanup)
set_tests_properties(p4tc_cleanup PROPERTIES FIXTURES_CLEANUP P4TCFixture)

macro(p4tc_add_test_with_args tag driver isXfail alias p4test test_args cmake_args)
p4c_add_test_with_args(${tag} ${driver} ${isXfail} ${alias} ${p4test} ${test_args} "")
p4c_test_set_name(__testname ${tag} ${alias})
set_tests_properties(${__testname} PROPERTIES FIXTURES_REQUIRED P4TCFixture RESOURCE_LOCK "shared_lock")
set_tests_properties(${__testname} PROPERTIES RESOURCE_LOCK "shared_lock")
set_tests_properties(${__testname} PROPERTIES TIMEOUT 1000)
endmacro(p4tc_add_test_with_args)

p4c_add_tests("p4tc" ${P4TC_COMPILER_DRIVER} "${P4_16_SUITES}" "")
p4tc_add_test_with_args("p4tc" ${P4TC_COMPILER_DRIVER} FALSE "testdata/p4tc_samples_stf/arp_respond.p4" "testdata/p4tc_samples_stf/arp_respond.p4" "-tf ${P4C_SOURCE_DIR}/testdata/p4tc_samples_stf/arp_respond.stf" "")
Loading