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

Add ring buffer tests #39

Merged
merged 40 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ac51cce
draft
gtrevi Mar 29, 2024
1efa35e
wip
gtrevi Mar 29, 2024
d457574
add parsing two options
gtrevi Mar 29, 2024
761b6b5
add support for multiple options
gtrevi Mar 29, 2024
64d6623
wip
gtrevi Mar 29, 2024
62cc34e
wip
gtrevi Mar 29, 2024
89c55a8
test max_entries
gtrevi Mar 29, 2024
90af880
description
gtrevi Mar 29, 2024
c3c3ca7
reduce size
gtrevi Mar 29, 2024
6ac6b42
revise RB sizes to ~100Mb
gtrevi Mar 30, 2024
dab4922
Merge branch 'microsoft:main' into gtrevi/add-rb-tests
gtrevi Apr 1, 2024
d5c5afe
add debug messages
gtrevi Apr 1, 2024
e068eb8
fix
gtrevi Apr 1, 2024
e92384d
wip
gtrevi Apr 1, 2024
0d626f9
test
gtrevi Apr 1, 2024
dbee75d
test
gtrevi Apr 1, 2024
9add67b
test
gtrevi Apr 1, 2024
d3d726f
Merge branch 'gtrevi/add-rb-tests' of https://github.com/gtrevi/bpf_p…
gtrevi Apr 1, 2024
95476ad
wip
gtrevi Apr 1, 2024
2923b8d
remove hard-coded options
gtrevi Apr 1, 2024
4ecbad5
wip
gtrevi Apr 1, 2024
1ca9804
wip
gtrevi Apr 2, 2024
dfd29e0
Merge branch 'microsoft:main' into gtrevi/add-rb-tests
gtrevi Apr 4, 2024
bd14bf0
fix
gtrevi Apr 4, 2024
e348dd1
test
gtrevi Apr 4, 2024
31bdb15
wip
gtrevi Apr 5, 2024
50d5110
cmakelists ok
gtrevi Apr 5, 2024
6e48641
wip
gtrevi Apr 5, 2024
1887299
param test
gtrevi Apr 8, 2024
68b5f19
buffer to separate map
gtrevi Apr 15, 2024
8d0981c
wip
gtrevi Apr 15, 2024
7f57caa
wip
gtrevi Apr 15, 2024
de3145a
Merge branch 'microsoft:main' into gtrevi/add-rb-tests
gtrevi Apr 16, 2024
2cae368
wip
gtrevi Apr 16, 2024
58a9adb
wip
gtrevi Apr 16, 2024
70f9840
wip
gtrevi Apr 16, 2024
8a0ad82
Merge branch 'microsoft:main' into gtrevi/add-rb-tests
gtrevi Apr 16, 2024
4c8e938
update helpers test case
gtrevi Apr 18, 2024
fabfda9
Revert "update helpers test case"
gtrevi Apr 18, 2024
63b925f
Reapply "update helpers test case"
gtrevi Apr 18, 2024
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
73 changes: 35 additions & 38 deletions bpf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,59 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

# Each test consists of a C file, an output file name, and an optional option, seperated by commas.
# Each test consists of a C file, an output file name, and an optional option-list, seperated by commas.
set(test_cases
"baseline"
"baseline,baseline,-DBPF"
"generic_map,hash,-DTYPE=BPF_MAP_TYPE_HASH"
"generic_map,percpu_hash,-DTYPE=BPF_MAP_TYPE_PERCPU_HASH"
"generic_map,lru_hash,-DTYPE=BPF_MAP_TYPE_LRU_HASH"
"generic_map,lru_per_cpu_hash,-DTYPE=BPF_MAP_TYPE_LRU_PERCPU_HASH"
"generic_map,array,-DTYPE=BPF_MAP_TYPE_ARRAY"
"generic_map,percpu_array,-DTYPE=BPF_MAP_TYPE_PERCPU_ARRAY"
"helpers"
"helpers,helpers"
"lpm,lpm_1024,-DMAX_ENTRIES=1024"
"lpm,lpm_16384,-DMAX_ENTRIES=16384"
"lpm,lpm_262144,-DMAX_ENTRIES=262144"
"lpm,lpm_1048576,-DMAX_ENTRIES=1048576"
"map_in_map,hash_of_array,-DTYPE=BPF_MAP_TYPE_HASH_OF_MAPS"
"map_in_map,array_of_array,-DTYPE=BPF_MAP_TYPE_ARRAY_OF_MAPS"
"ringbuf"
"rolling_lru"
"tail_call"
"xdp"
"max_tail_call"
# The smallest power of 2 that is >= (128 * 1024) is 2^17 = 131072
"ringbuf,ringbuf,-DBPF -DRB_SIZE=131072 -DRECORD_SIZE=128"
# The smallest power of 2 that is >= (400 * 300000) is 2^27 = 134217728
"ringbuf,ringbuf_300K_400b,-DBPF -DRB_SIZE=134217728 -DRECORD_SIZE=400"
# The smallest power of 2 that is >= (1420 * 100000) is 2^28 = 268435456
"ringbuf,ringbuf_100K_1420b,-DBPF -DRB_SIZE=268435456 -DRECORD_SIZE=1420"
"rolling_lru,rolling_lru,-DBPF"
"tail_call,tail_call,-DBPF"
"xdp,xdp,-DBPF"
"max_tail_call,max_tail_call,-DBPF"
)

function(process_test_cases worker)
foreach(test ${test_cases})
function(process_test_cases worker test_list)
foreach(test ${test_list})
# Split test into list of strings
string(REPLACE "," " " elements ${test})
separate_arguments(elements NATIVE_COMMAND ${elements})

# Append out_file and options if they are missing
list(LENGTH elements num_elements)

if (num_elements EQUAL 1)
list(GET elements 0 file_name)
set(out_name ${file_name})
set(option "-DBPF")
endif()
if (num_elements EQUAL 2)
list(GET elements 0 file_name)
list(GET elements 1 out_name)
set(option "-DBPF")
endif()
if (num_elements EQUAL 3)
list(GET elements 0 file_name)
list(GET elements 1 out_name)
list(GET elements 2 option)
endif()

cmake_language(CALL ${worker} ${file_name} ${out_name} ${option})
# Extract file_name, out_name, and option_list
list(GET elements 0 file_name)
list(GET elements 1 out_name)
list(REMOVE_AT elements 0 1)

# Build the option_list from the remaining elements
set(option_list "")
foreach(element IN LISTS elements)
list(APPEND option_list "${element}")
endforeach()

message(STATUS "Calling cmake_language(), using: worker=${worker}, file_name=${file_name}, out_name=${out_name}, option_list=${option_list}")
cmake_language(CALL ${worker} ${file_name} ${out_name} "${option_list}")
endforeach()
endfunction()

find_program(clang_path "clang")

function(build_bpf file_name out_name option)
function(build_bpf file_name out_name option_list)
message(STATUS "Building BPF ${out_name}")

set(optimize_flags "-O2")
Expand All @@ -74,18 +72,18 @@ function(build_bpf file_name out_name option)
message(FATAL_ERROR "BPF file ${bpf_file_path} does not exist")
endif()

message(STATUS "Calling add_custom_command(), using: ${clang_path} ${option_list} -I ${EBPF_INC_PATH} -I ${CMAKE_CURRENT_BINARY_DIR} -g ${optimize_flags} -target bpf -c ${bpf_file_path} -o ${bpf_obj_file_path}")
add_custom_command(
OUTPUT ${bpf_obj_file_path}
COMMAND ${clang_path} ${option} -I ${EBPF_INC_PATH} -I ${CMAKE_CURRENT_BINARY_DIR} -g ${optimize_flags} -target bpf -c ${bpf_file_path} -o ${bpf_obj_file_path}
COMMAND ${clang_path} ${option_list} -I ${EBPF_INC_PATH} -I ${CMAKE_CURRENT_BINARY_DIR} -g ${optimize_flags} -target bpf -c ${bpf_file_path} -o ${bpf_obj_file_path}
DEPENDS ${bpf_file_path}
COMMENT "Building BPF object ${bpf_obj_file_path}"
COMMENT "-- Building BPF object ${bpf_obj_file_path}"
)

add_custom_target(${out_name}_ELF ALL DEPENDS ${bpf_obj_file_path} SOURCES ${bpf_file_path})
endfunction()

# Run packages\eBPF-for-Windows\build\native\bin\Convert-BpfToNative.ps1 on each .o file to produce a .sys file
function(convert_to_native file_name out_name option)
function(convert_to_native file_name out_name option_list)
message(STATUS "Converting BPF ${out_name} to native")

set(bpf_obj_file_name ${out_name}.o)
Expand Down Expand Up @@ -114,7 +112,6 @@ function(convert_to_native file_name out_name option)
POST_BUILD
)


# Copy the .sys file to the output directory as part of post build
add_custom_command(
OUTPUT ${bpf_sys_file_output_path}
Expand Down Expand Up @@ -142,12 +139,12 @@ configure_file(
${CMAKE_CURRENT_BINARY_DIR}/bpf.h
)

process_test_cases("build_bpf")
process_test_cases("build_bpf" "${test_cases}")

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/tests.yml
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests.yml COPYONLY)

if (PLATFORM_WINDOWS)
process_test_cases("convert_to_native")
process_test_cases("convert_to_native" "${test_cases}")
endif()
28 changes: 24 additions & 4 deletions bpf/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@

#include "bpf.h"

#if !defined(RB_SIZE)
#define RB_SIZE (128 * 1024)
#endif

#if !defined(RECORD_SIZE)
#define RECORD_SIZE 128
#endif

struct
{
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 128 * 1024);
} map SEC(".maps");
__uint(max_entries, RB_SIZE);
} rb_map SEC(".maps");

struct
{
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__uint(max_entries, 1);
__uint(value_size, RECORD_SIZE);
} buf_map SEC(".maps");

SEC("sockops/bpf_ringbuf_output") int output(void* ctx)
{
char msg[] = "Hello, World!";
if (bpf_ringbuf_output(&map, msg, sizeof(msg), 0) < 0) {
int key = 0;
void* msg = bpf_map_lookup_elem(&buf_map, &key);
if (!msg) {
return 1;
}
if (bpf_ringbuf_output(&rb_map, msg, RECORD_SIZE, 0) < 0) {
return 1;
} else {
return 0;
Expand Down
15 changes: 14 additions & 1 deletion bpf/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,18 @@ tests:
iteration_count: 10000000
program_cpu_assignment:
output: all


- name: BPF_MAP_TYPE_RINGBUF output - 300K entries of 400bytes
description: Tests the bpf_ringbuf_output helper writing 300K entries of 400bytes.
elf_file: ringbuf_300K_400b.o
iteration_count: 300000
program_cpu_assignment:
output: all

- name: BPF_MAP_TYPE_RINGBUF output - 100K entries of 1420bytes
description: Tests the bpf_ringbuf_output helper writing 100K entries of 1420bytes.
elf_file: ringbuf_100K_1420b.o
iteration_count: 100000
program_cpu_assignment:
output: all
# Add more test cases as needed
Loading