remove ebpf_api_initiate, ebpf_api_terminate from sources (#738)

* remove ebpf_api_initiate, ebpf_api_terminate from sources

* cleaup traces

* cleanup traces
This commit is contained in:
saxena-anurag 2022-01-31 08:32:28 -08:00 коммит произвёл GitHub
Родитель a782d86ce1
Коммит 6b4dda75a1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
14 изменённых файлов: 34 добавлений и 73 удалений

Просмотреть файл

@ -73,8 +73,6 @@ EXPORTS
bpf_program__set_type bpf_program__set_type
bpf_program__unload bpf_program__unload
bpf_program__unpin bpf_program__unpin
ebpf_api_initiate
ebpf_api_terminate
ebpf_api_elf_enumerate_sections ebpf_api_elf_enumerate_sections
ebpf_api_elf_disassemble_section ebpf_api_elf_disassemble_section
ebpf_api_elf_verify_section ebpf_api_elf_verify_section

Просмотреть файл

@ -3,6 +3,7 @@
// dllmain.cpp : Defines the entry point for the DLL application. // dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h" #include "pch.h"
#include "api_internal.h"
BOOL APIENTRY BOOL APIENTRY
DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
@ -11,9 +12,16 @@ DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
UNREFERENCED_PARAMETER(lpReserved); UNREFERENCED_PARAMETER(lpReserved);
switch (ul_reason_for_call) { switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
if (ebpf_api_initiate() != 0) {
return FALSE;
}
break;
case DLL_THREAD_ATTACH: case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH: case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
ebpf_api_terminate();
break; break;
} }
return TRUE; return TRUE;

Просмотреть файл

@ -75,7 +75,7 @@
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)libs\api_common;$(SolutionDir)external\ebpf-verifier\src</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<SupportJustMyCode>false</SupportJustMyCode> <SupportJustMyCode>false</SupportJustMyCode>
@ -102,7 +102,7 @@
<PreprocessorDefinitions>NDEBUG;EBPFAPI_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;EBPFAPI_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)libs\api_common;$(SolutionDir)external\ebpf-verifier\src</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>

Просмотреть файл

@ -70,18 +70,6 @@ extern "C"
struct bpf_map; struct bpf_map;
struct bpf_link; struct bpf_link;
/**
* @brief Initialize the eBPF user mode library.
*/
uint32_t
ebpf_api_initiate();
/**
* @brief Terminate the eBPF user mode library.
*/
void
ebpf_api_terminate();
/** /**
* @brief Create an eBPF map with input parameters. * @brief Create an eBPF map with input parameters.
* *

Просмотреть файл

@ -72,6 +72,18 @@ typedef struct bpf_object
bool loaded = false; bool loaded = false;
} ebpf_object_t; } ebpf_object_t;
/**
* @brief Initialize the eBPF user mode library.
*/
uint32_t
ebpf_api_initiate();
/**
* @brief Terminate the eBPF user mode library.
*/
void
ebpf_api_terminate();
ebpf_result_t ebpf_result_t
ebpf_get_program_byte_code( ebpf_get_program_byte_code(
_In_z_ const char* file_name, _In_z_ const char* file_name,

Просмотреть файл

@ -184,18 +184,10 @@ _test_program_next_previous(const char* file_name, int expected_program_count)
bpf_object__close(object); bpf_object__close(object);
} }
TEST_CASE("pinned_map_enum", "[pinned_map_enum]") TEST_CASE("pinned_map_enum", "[pinned_map_enum]") { ebpf_test_pinned_map_enum(); }
{
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
ebpf_test_pinned_map_enum();
ebpf_api_terminate();
}
TEST_CASE("test_ebpf_program_load", "[test_ebpf_program_load]") TEST_CASE("test_ebpf_program_load", "[test_ebpf_program_load]")
{ {
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
// Load droppacket (JIT) without providing expected program type. // Load droppacket (JIT) without providing expected program type.
_test_program_load("droppacket.o", nullptr, EBPF_EXECUTION_JIT, true); _test_program_load("droppacket.o", nullptr, EBPF_EXECUTION_JIT, true);
@ -222,28 +214,18 @@ TEST_CASE("test_ebpf_program_load", "[test_ebpf_program_load]")
// Try to load an unsafe program. // Try to load an unsafe program.
_test_program_load("droppacket_unsafe.o", nullptr, EBPF_EXECUTION_ANY, false); _test_program_load("droppacket_unsafe.o", nullptr, EBPF_EXECUTION_ANY, false);
ebpf_api_terminate();
} }
TEST_CASE("test_ebpf_program_next_previous", "[test_ebpf_program_next_previous]") TEST_CASE("test_ebpf_program_next_previous", "[test_ebpf_program_next_previous]")
{ {
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
_test_program_next_previous("droppacket.o", DROP_PACKET_PROGRAM_COUNT); _test_program_next_previous("droppacket.o", DROP_PACKET_PROGRAM_COUNT);
_test_program_next_previous("bindmonitor.o", BIND_MONITOR_PROGRAM_COUNT); _test_program_next_previous("bindmonitor.o", BIND_MONITOR_PROGRAM_COUNT);
ebpf_api_terminate();
} }
TEST_CASE("test_ebpf_map_next_previous", "[test_ebpf_map_next_previous]") TEST_CASE("test_ebpf_map_next_previous", "[test_ebpf_map_next_previous]")
{ {
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
_test_map_next_previous("droppacket.o", DROP_PACKET_MAP_COUNT); _test_map_next_previous("droppacket.o", DROP_PACKET_MAP_COUNT);
_test_map_next_previous("bindmonitor.o", BIND_MONITOR_MAP_COUNT); _test_map_next_previous("bindmonitor.o", BIND_MONITOR_MAP_COUNT);
ebpf_api_terminate();
} }
void void
@ -279,7 +261,7 @@ ring_buffer_api_test(ebpf_execution_type_t execution_type)
struct bpf_object* object = nullptr; struct bpf_object* object = nullptr;
hook_helper_t hook(EBPF_ATTACH_TYPE_BIND); hook_helper_t hook(EBPF_ATTACH_TYPE_BIND);
program_load_attach_helper_t _helper( program_load_attach_helper_t _helper(
"bindmonitor_ringbuf.o", EBPF_PROGRAM_TYPE_BIND, "bind_monitor", execution_type, hook, true); "bindmonitor_ringbuf.o", EBPF_PROGRAM_TYPE_BIND, "bind_monitor", execution_type, hook);
object = _helper.get_object(); object = _helper.get_object();
fd_t process_map_fd = bpf_object__find_map_fd_by_name(object, "process_map"); fd_t process_map_fd = bpf_object__find_map_fd_by_name(object, "process_map");

Просмотреть файл

@ -4,7 +4,6 @@
*/ */
#pragma once #pragma once
#include "ebpf_api.h" #include "ebpf_api.h"
#include "ebpf_link.h"
#include "ebpf_nethooks.h" #include "ebpf_nethooks.h"
#include "ebpf_platform.h" #include "ebpf_platform.h"
#include "ebpf_program_types.h" #include "ebpf_program_types.h"

Просмотреть файл

@ -7,7 +7,7 @@ using namespace std::chrono_literals;
#include "bpf/bpf.h" #include "bpf/bpf.h"
#include "catch_wrapper.hpp" #include "catch_wrapper.hpp"
#include "ebpf_api.h" #include "api_internal.h"
#include "ebpf_async.h" #include "ebpf_async.h"
#include "ebpf_core.h" #include "ebpf_core.h"
#include "helpers.h" #include "helpers.h"

Просмотреть файл

@ -9,20 +9,14 @@ _program_load_attach_helper::_program_load_attach_helper(
ebpf_program_type_t program_type, ebpf_program_type_t program_type,
_In_z_ const char* program_name, _In_z_ const char* program_name,
ebpf_execution_type_t execution_type, ebpf_execution_type_t execution_type,
hook_helper_t& hook, hook_helper_t& hook)
bool initiate_api)
: _file_name(file_name), _program_type(program_type), _program_name(program_name), _execution_type(execution_type), : _file_name(file_name), _program_type(program_type), _program_name(program_name), _execution_type(execution_type),
_link(nullptr), _object(nullptr), _api_initialized(false) _link(nullptr), _object(nullptr)
{ {
ebpf_result_t result; ebpf_result_t result;
fd_t program_fd; fd_t program_fd;
const char* log_buffer = nullptr; const char* log_buffer = nullptr;
if (initiate_api) {
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
_api_initialized = true;
}
// Load BPF object from ELF file. // Load BPF object from ELF file.
result = ebpf_program_load( result = ebpf_program_load(
_file_name.c_str(), &_program_type, nullptr, _execution_type, &_object, &program_fd, &log_buffer); _file_name.c_str(), &_program_type, nullptr, _execution_type, &_object, &program_fd, &log_buffer);
@ -48,9 +42,6 @@ _program_load_attach_helper::~_program_load_attach_helper()
{ {
bpf_link__destroy(_link); bpf_link__destroy(_link);
bpf_object__close(_object); bpf_object__close(_object);
if (_api_initialized)
ebpf_api_terminate();
} }
struct bpf_object* struct bpf_object*

Просмотреть файл

@ -17,8 +17,7 @@ typedef class _program_load_attach_helper
ebpf_program_type_t program_type, ebpf_program_type_t program_type,
_In_z_ const char* program_name, _In_z_ const char* program_name,
ebpf_execution_type_t execution_type, ebpf_execution_type_t execution_type,
hook_helper_t& hook, hook_helper_t& hook);
bool initiate_api = false);
~_program_load_attach_helper(); ~_program_load_attach_helper();
@ -32,5 +31,4 @@ typedef class _program_load_attach_helper
ebpf_execution_type_t _execution_type; ebpf_execution_type_t _execution_type;
bpf_link* _link; bpf_link* _link;
bpf_object* _object; bpf_object* _object;
bool _api_initialized;
} program_load_attach_helper_t; } program_load_attach_helper_t;

Просмотреть файл

@ -114,7 +114,7 @@ TEST_CASE("jit_test", "[sample_ext_test]")
struct bpf_object* object = nullptr; struct bpf_object* object = nullptr;
hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE);
program_load_attach_helper_t _helper( program_load_attach_helper_t _helper(
"test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_program_entry", EBPF_EXECUTION_JIT, hook, true); "test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_program_entry", EBPF_EXECUTION_JIT, hook);
object = _helper.get_object(); object = _helper.get_object();
@ -126,7 +126,7 @@ TEST_CASE("interpret_test", "[sample_ext_test]")
struct bpf_object* object = nullptr; struct bpf_object* object = nullptr;
hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE);
program_load_attach_helper_t _helper( program_load_attach_helper_t _helper(
"test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_program_entry", EBPF_EXECUTION_INTERPRET, hook, true); "test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_program_entry", EBPF_EXECUTION_INTERPRET, hook);
object = _helper.get_object(); object = _helper.get_object();
@ -139,7 +139,7 @@ utility_helpers_test(ebpf_execution_type_t execution_type)
struct bpf_object* object = nullptr; struct bpf_object* object = nullptr;
hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE);
program_load_attach_helper_t _helper( program_load_attach_helper_t _helper(
"test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_utility_helpers", execution_type, hook, true); "test_sample_ebpf.o", EBPF_PROGRAM_TYPE_SAMPLE, "test_utility_helpers", execution_type, hook);
object = _helper.get_object(); object = _helper.get_object();
std::vector<char> dummy(1); std::vector<char> dummy(1);
@ -154,7 +154,6 @@ TEST_CASE("utility_helpers_test_interpret", "[sample_ext_test]") { utility_helpe
TEST_CASE("utility_helpers_test_jit", "[sample_ext_test]") { utility_helpers_test(EBPF_EXECUTION_JIT); } TEST_CASE("utility_helpers_test_jit", "[sample_ext_test]") { utility_helpers_test(EBPF_EXECUTION_JIT); }
TEST_CASE("netsh_add_program_test_sample_ebpf", "[sample_ext_test]") TEST_CASE("netsh_add_program_test_sample_ebpf", "[sample_ext_test]")
{ {
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
int result; int result;
std::string output = std::string output =
_run_netsh_command(handle_ebpf_add_program, L"test_sample_ebpf.o", L"pinned=none", nullptr, &result); _run_netsh_command(handle_ebpf_add_program, L"test_sample_ebpf.o", L"pinned=none", nullptr, &result);

Просмотреть файл

@ -1125,9 +1125,9 @@ TEST_CASE("enumerate link IDs", "[libbpf]")
// Load and attach some programs. // Load and attach some programs.
program_load_attach_helper_t xdp_helper( program_load_attach_helper_t xdp_helper(
"droppacket.o", EBPF_PROGRAM_TYPE_XDP, "DropPacket", EBPF_EXECUTION_JIT, xdp_hook, false); "droppacket.o", EBPF_PROGRAM_TYPE_XDP, "DropPacket", EBPF_EXECUTION_JIT, xdp_hook);
program_load_attach_helper_t bind_helper( program_load_attach_helper_t bind_helper(
"bindmonitor.o", EBPF_PROGRAM_TYPE_BIND, "BindMonitor", EBPF_EXECUTION_JIT, bind_hook, false); "bindmonitor.o", EBPF_PROGRAM_TYPE_BIND, "BindMonitor", EBPF_EXECUTION_JIT, bind_hook);
// Now enumerate the IDs. // Now enumerate the IDs.
REQUIRE(bpf_link_get_next_id(0, &id1) == 0); REQUIRE(bpf_link_get_next_id(0, &id1) == 0);
@ -1152,7 +1152,7 @@ TEST_CASE("bpf_obj_get_info_by_fd", "[libbpf]")
program_info_provider_t xdp_program_info(EBPF_PROGRAM_TYPE_XDP); program_info_provider_t xdp_program_info(EBPF_PROGRAM_TYPE_XDP);
single_instance_hook_t xdp_hook(EBPF_PROGRAM_TYPE_XDP, EBPF_ATTACH_TYPE_XDP); single_instance_hook_t xdp_hook(EBPF_PROGRAM_TYPE_XDP, EBPF_ATTACH_TYPE_XDP);
program_load_attach_helper_t xdp_helper( program_load_attach_helper_t xdp_helper(
"droppacket.o", EBPF_PROGRAM_TYPE_XDP, "DropPacket", EBPF_EXECUTION_JIT, xdp_hook, false); "droppacket.o", EBPF_PROGRAM_TYPE_XDP, "DropPacket", EBPF_EXECUTION_JIT, xdp_hook);
struct bpf_object* object = xdp_helper.get_object(); struct bpf_object* object = xdp_helper.get_object();
REQUIRE(object != nullptr); REQUIRE(object != nullptr);

Просмотреть файл

@ -15,8 +15,6 @@
#include "programs.h" #include "programs.h"
#include "resource.h" #include "resource.h"
DWORD g_ApiDllInitialized = ERROR_SUCCESS;
static const GUID g_EbpfHelperGuid = {/* 634d21b8-13f9-46a3-945f-885cbd661c13 */ static const GUID g_EbpfHelperGuid = {/* 634d21b8-13f9-46a3-945f-885cbd661c13 */
0x634d21b8, 0x634d21b8,
0x13f9, 0x13f9,
@ -117,11 +115,5 @@ __declspec(dllexport) DWORD InitHelperDll(DWORD netshVersion, void* reserved)
DWORD status = RegisterHelper(NULL, &attributes); DWORD status = RegisterHelper(NULL, &attributes);
if (status == ERROR_SUCCESS) {
// Allow for this to fail since some commands currently
// don't require the ebpf_api.
g_ApiDllInitialized = ebpf_api_initiate();
}
return status; return status;
} }

Просмотреть файл

@ -184,12 +184,6 @@ print_usage(char* path)
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
uint32_t result = ebpf_api_initiate();
if (result != ERROR_SUCCESS) {
fprintf(stderr, "ebpf_api_initiate failed: %d\n", result);
return 1;
}
if (argc < 2) { if (argc < 2) {
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;