forked from rlane/ubpf
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for custom data relocation Signed-off-by: Alan Jowett <alanjo@microsoft.com> * Fix downlevel builds Signed-off-by: Alan Jowett <alanjo@microsoft.com> * PR feedback Signed-off-by: Alan Jowett <atjowet@gmail.com> * Fix build break Signed-off-by: Alan Jowett <atjowet@gmail.com> * PR Feedback Signed-off-by: Will Hawkins <hawkinsw@obs.cr> --------- Signed-off-by: Alan Jowett <alanjo@microsoft.com> Signed-off-by: Alan Jowett <atjowet@gmail.com> Signed-off-by: Will Hawkins <hawkinsw@obs.cr> Co-authored-by: Alan Jowett <alanjo@microsoft.com> Co-authored-by: Alan Jowett <atjowet@gmail.com>
- Loading branch information
1 parent
e17e8bb
commit 2249c83
Showing
10 changed files
with
678 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) Microsoft Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
find_program(clang_path "clang" REQUIRED) | ||
|
||
execute_process( | ||
COMMAND echo "int main() { return 0;}" | ||
COMMAND ${clang_path} --target=bpf -x c - -c -o /dev/null | ||
ERROR_QUIET OUTPUT_QUIET | ||
RESULT_VARIABLE CLANG_RETURN_CODE | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
|
||
if (CLANG_RETURN_CODE EQUAL 0) | ||
message(STATUS "Clang supports BPF target") | ||
set(CLANG_SUPPORTS_BPF TRUE) | ||
else() | ||
message(WARNING "Clang does not support BPF target, skipping BPF tests") | ||
set(CLANG_SUPPORTS_BPF FALSE) | ||
endif() | ||
|
||
function(build_bpf file_name) | ||
message(STATUS "Building BPF ${file_name}") | ||
|
||
set(bpf_file_name ${file_name}.bpf.c) | ||
set(bpf_file_path ${CMAKE_CURRENT_SOURCE_DIR}/${bpf_file_name}) | ||
set(bpf_obj_file_name ${file_name}.bpf.o) | ||
set(bpf_obj_file_path ${CMAKE_CURRENT_BINARY_DIR}/${bpf_obj_file_name}) | ||
|
||
if (NOT EXISTS ${bpf_file_path}) | ||
message(FATAL_ERROR "BPF file ${bpf_file_path} does not exist") | ||
endif() | ||
|
||
add_custom_command( | ||
OUTPUT ${bpf_obj_file_path} | ||
COMMAND ${clang_path} -g -O2 -target bpf -c ${bpf_file_path} -o ${bpf_obj_file_path} | ||
DEPENDS ${bpf_file_path} | ||
COMMENT "Building BPF object ${bpf_obj_file_path}" | ||
) | ||
|
||
add_custom_target(${file_name}_ELF ALL DEPENDS ${bpf_obj_file_path} SOURCES ${bpf_file_path}) | ||
|
||
add_test(NAME ${file_name}_TEST_INTERPRET COMMAND "${CMAKE_BINARY_DIR}/bin/ubpf_test" "${bpf_obj_file_path}") | ||
set_tests_properties(${file_name}_TEST_INTERPRET PROPERTIES PASS_REGULAR_EXPRESSION "0x0") | ||
add_test(NAME ${file_name}_TEST_JIT COMMAND "${CMAKE_BINARY_DIR}/bin/ubpf_test" "${bpf_obj_file_path}") | ||
set_tests_properties(${file_name}_TEST_JIT PROPERTIES PASS_REGULAR_EXPRESSION "0x0") | ||
endfunction() | ||
|
||
if (CLANG_SUPPORTS_BPF) | ||
build_bpf(map) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <stddef.h> | ||
|
||
struct bpf_map; | ||
|
||
static void* (*bpf_map_lookup_elem)(void* map, const void* key) = (void* (*)(void* map, const void* key))6; | ||
static int (*bpf_map_update_elem)(void* map, const void* key, const void* value, unsigned long flags) = | ||
(int (*)(void*, const void*, const void*, unsigned long))7; | ||
static int (*bpf_map_delete_elem)(void* map, const void* key) = (int (*)(void*, const void*))8; | ||
|
||
#define BPF_MAP_TYPE_ARRAY 2 | ||
|
||
struct bpf_map_def | ||
{ | ||
unsigned int type; | ||
unsigned int key_size; | ||
unsigned int value_size; | ||
unsigned int max_entries; | ||
unsigned int map_flags; | ||
unsigned int inner_map_idx; | ||
unsigned int numa_node; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "bpf.h" | ||
|
||
struct bpf_map_def map = { | ||
.type = BPF_MAP_TYPE_ARRAY, | ||
.key_size = sizeof(unsigned int), | ||
.value_size = sizeof(unsigned long), | ||
.max_entries = 10, | ||
}; | ||
|
||
unsigned long | ||
map_test(void* memory, size_t memory_length) | ||
{ | ||
unsigned int key = 5; | ||
unsigned long new_value = 0x1234567890ABCDEF; | ||
unsigned long* value = bpf_map_lookup_elem(&map, &key); | ||
if (value == NULL) { | ||
return 1; | ||
} | ||
|
||
if (*value != 0) { | ||
return 2; | ||
} | ||
|
||
if (bpf_map_update_elem(&map, &key, &new_value, 0) != 0) { | ||
return 3; | ||
} | ||
|
||
value = bpf_map_lookup_elem(&map, &key); | ||
if (value == NULL) { | ||
return 4; | ||
} | ||
|
||
if (*value != new_value) { | ||
return 5; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.