From 6b4dda75a19a6fbf140a8177cfacd5f4d00dcd85 Mon Sep 17 00:00:00 2001 From: saxena-anurag <43585259+saxena-anurag@users.noreply.github.com> Date: Mon, 31 Jan 2022 08:32:28 -0800 Subject: [PATCH] remove ebpf_api_initiate, ebpf_api_terminate from sources (#738) * remove ebpf_api_initiate, ebpf_api_terminate from sources * cleaup traces * cleanup traces --- ebpfapi/Source.def | 2 -- ebpfapi/dllmain.cpp | 8 ++++++++ ebpfapi/ebpfapi.vcxproj | 4 ++-- include/ebpf_api.h | 12 ------------ libs/api/api_internal.h | 12 ++++++++++++ tests/api_test/api_test.cpp | 22 ++-------------------- tests/end_to_end/helpers.h | 1 - tests/end_to_end/test_helper.cpp | 2 +- tests/libs/util/program_helper.cpp | 13 ++----------- tests/libs/util/program_helper.h | 4 +--- tests/sample/ext/app/sample_ext_app.cpp | 7 +++---- tests/unit/libbpf_test.cpp | 6 +++--- tools/netsh/dllmain.c | 8 -------- tools/port_quota/port_quota.cpp | 6 ------ 14 files changed, 34 insertions(+), 73 deletions(-) diff --git a/ebpfapi/Source.def b/ebpfapi/Source.def index 1e6827f4d..5bb692353 100644 --- a/ebpfapi/Source.def +++ b/ebpfapi/Source.def @@ -73,8 +73,6 @@ EXPORTS bpf_program__set_type bpf_program__unload bpf_program__unpin - ebpf_api_initiate - ebpf_api_terminate ebpf_api_elf_enumerate_sections ebpf_api_elf_disassemble_section ebpf_api_elf_verify_section diff --git a/ebpfapi/dllmain.cpp b/ebpfapi/dllmain.cpp index 037c652ee..cadb2dc38 100644 --- a/ebpfapi/dllmain.cpp +++ b/ebpfapi/dllmain.cpp @@ -3,6 +3,7 @@ // dllmain.cpp : Defines the entry point for the DLL application. #include "pch.h" +#include "api_internal.h" BOOL APIENTRY 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); switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: + if (ebpf_api_initiate() != 0) { + return FALSE; + } + break; case DLL_THREAD_ATTACH: + break; case DLL_THREAD_DETACH: + break; case DLL_PROCESS_DETACH: + ebpf_api_terminate(); break; } return TRUE; diff --git a/ebpfapi/ebpfapi.vcxproj b/ebpfapi/ebpfapi.vcxproj index 68106d23c..dd984645c 100644 --- a/ebpfapi/ebpfapi.vcxproj +++ b/ebpfapi/ebpfapi.vcxproj @@ -75,7 +75,7 @@ pch.h MultiThreadedDebugDll ProgramDatabase - $(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context + $(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 true stdcpp17 false @@ -102,7 +102,7 @@ NDEBUG;EBPFAPI_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) true pch.h - $(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context + $(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 MultiThreadedDLL ProgramDatabase true diff --git a/include/ebpf_api.h b/include/ebpf_api.h index 383f6a8fe..f0567b194 100644 --- a/include/ebpf_api.h +++ b/include/ebpf_api.h @@ -70,18 +70,6 @@ extern "C" struct bpf_map; 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. * diff --git a/libs/api/api_internal.h b/libs/api/api_internal.h index 685f58df5..aa67b89f0 100644 --- a/libs/api/api_internal.h +++ b/libs/api/api_internal.h @@ -72,6 +72,18 @@ typedef struct bpf_object bool loaded = false; } 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_get_program_byte_code( _In_z_ const char* file_name, diff --git a/tests/api_test/api_test.cpp b/tests/api_test/api_test.cpp index 994ae8dd1..85fefaab0 100644 --- a/tests/api_test/api_test.cpp +++ b/tests/api_test/api_test.cpp @@ -184,18 +184,10 @@ _test_program_next_previous(const char* file_name, int expected_program_count) bpf_object__close(object); } -TEST_CASE("pinned_map_enum", "[pinned_map_enum]") -{ - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); - - ebpf_test_pinned_map_enum(); - ebpf_api_terminate(); -} +TEST_CASE("pinned_map_enum", "[pinned_map_enum]") { ebpf_test_pinned_map_enum(); } TEST_CASE("test_ebpf_program_load", "[test_ebpf_program_load]") { - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); - // Load droppacket (JIT) without providing expected program type. _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. _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]") { - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); - _test_program_next_previous("droppacket.o", DROP_PACKET_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]") { - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); - _test_map_next_previous("droppacket.o", DROP_PACKET_MAP_COUNT); _test_map_next_previous("bindmonitor.o", BIND_MONITOR_MAP_COUNT); - - ebpf_api_terminate(); } void @@ -279,7 +261,7 @@ ring_buffer_api_test(ebpf_execution_type_t execution_type) struct bpf_object* object = nullptr; hook_helper_t hook(EBPF_ATTACH_TYPE_BIND); 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(); fd_t process_map_fd = bpf_object__find_map_fd_by_name(object, "process_map"); diff --git a/tests/end_to_end/helpers.h b/tests/end_to_end/helpers.h index d338372af..3a6565ccd 100644 --- a/tests/end_to_end/helpers.h +++ b/tests/end_to_end/helpers.h @@ -4,7 +4,6 @@ */ #pragma once #include "ebpf_api.h" -#include "ebpf_link.h" #include "ebpf_nethooks.h" #include "ebpf_platform.h" #include "ebpf_program_types.h" diff --git a/tests/end_to_end/test_helper.cpp b/tests/end_to_end/test_helper.cpp index 786e2f0b8..b17fad66a 100644 --- a/tests/end_to_end/test_helper.cpp +++ b/tests/end_to_end/test_helper.cpp @@ -7,7 +7,7 @@ using namespace std::chrono_literals; #include "bpf/bpf.h" #include "catch_wrapper.hpp" -#include "ebpf_api.h" +#include "api_internal.h" #include "ebpf_async.h" #include "ebpf_core.h" #include "helpers.h" diff --git a/tests/libs/util/program_helper.cpp b/tests/libs/util/program_helper.cpp index 958203573..2f3808172 100644 --- a/tests/libs/util/program_helper.cpp +++ b/tests/libs/util/program_helper.cpp @@ -9,20 +9,14 @@ _program_load_attach_helper::_program_load_attach_helper( ebpf_program_type_t program_type, _In_z_ const char* program_name, ebpf_execution_type_t execution_type, - hook_helper_t& hook, - bool initiate_api) + hook_helper_t& hook) : _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; fd_t program_fd; const char* log_buffer = nullptr; - if (initiate_api) { - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); - _api_initialized = true; - } - // Load BPF object from ELF file. result = ebpf_program_load( _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_object__close(_object); - - if (_api_initialized) - ebpf_api_terminate(); } struct bpf_object* diff --git a/tests/libs/util/program_helper.h b/tests/libs/util/program_helper.h index d16196c7f..5b4429363 100644 --- a/tests/libs/util/program_helper.h +++ b/tests/libs/util/program_helper.h @@ -17,8 +17,7 @@ typedef class _program_load_attach_helper ebpf_program_type_t program_type, _In_z_ const char* program_name, ebpf_execution_type_t execution_type, - hook_helper_t& hook, - bool initiate_api = false); + hook_helper_t& hook); ~_program_load_attach_helper(); @@ -32,5 +31,4 @@ typedef class _program_load_attach_helper ebpf_execution_type_t _execution_type; bpf_link* _link; bpf_object* _object; - bool _api_initialized; } program_load_attach_helper_t; \ No newline at end of file diff --git a/tests/sample/ext/app/sample_ext_app.cpp b/tests/sample/ext/app/sample_ext_app.cpp index fc352d259..973161cf7 100644 --- a/tests/sample/ext/app/sample_ext_app.cpp +++ b/tests/sample/ext/app/sample_ext_app.cpp @@ -114,7 +114,7 @@ TEST_CASE("jit_test", "[sample_ext_test]") struct bpf_object* object = nullptr; hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); 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(); @@ -126,7 +126,7 @@ TEST_CASE("interpret_test", "[sample_ext_test]") struct bpf_object* object = nullptr; hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); 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(); @@ -139,7 +139,7 @@ utility_helpers_test(ebpf_execution_type_t execution_type) struct bpf_object* object = nullptr; hook_helper_t hook(EBPF_ATTACH_TYPE_SAMPLE); 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(); std::vector 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("netsh_add_program_test_sample_ebpf", "[sample_ext_test]") { - REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS); int result; std::string output = _run_netsh_command(handle_ebpf_add_program, L"test_sample_ebpf.o", L"pinned=none", nullptr, &result); diff --git a/tests/unit/libbpf_test.cpp b/tests/unit/libbpf_test.cpp index ea55fa286..77f6fdc6c 100644 --- a/tests/unit/libbpf_test.cpp +++ b/tests/unit/libbpf_test.cpp @@ -1125,9 +1125,9 @@ TEST_CASE("enumerate link IDs", "[libbpf]") // Load and attach some programs. 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( - "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. 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); single_instance_hook_t xdp_hook(EBPF_PROGRAM_TYPE_XDP, EBPF_ATTACH_TYPE_XDP); 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(); REQUIRE(object != nullptr); diff --git a/tools/netsh/dllmain.c b/tools/netsh/dllmain.c index fc9941d4a..e5338393d 100644 --- a/tools/netsh/dllmain.c +++ b/tools/netsh/dllmain.c @@ -15,8 +15,6 @@ #include "programs.h" #include "resource.h" -DWORD g_ApiDllInitialized = ERROR_SUCCESS; - static const GUID g_EbpfHelperGuid = {/* 634d21b8-13f9-46a3-945f-885cbd661c13 */ 0x634d21b8, 0x13f9, @@ -117,11 +115,5 @@ __declspec(dllexport) DWORD InitHelperDll(DWORD netshVersion, void* reserved) 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; } diff --git a/tools/port_quota/port_quota.cpp b/tools/port_quota/port_quota.cpp index f9ebe37fd..31b9c8239 100644 --- a/tools/port_quota/port_quota.cpp +++ b/tools/port_quota/port_quota.cpp @@ -184,12 +184,6 @@ print_usage(char* path) int 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) { print_usage(argv[0]); return 1;