diff --git a/ebpfapi/Source.def b/ebpfapi/Source.def index d71778ccd..fa9c720c9 100644 --- a/ebpfapi/Source.def +++ b/ebpfapi/Source.def @@ -95,11 +95,9 @@ EXPORTS ebpf_free_string ebpf_get_attach_type_name ebpf_get_next_pinned_program_path - ebpf_get_next_program ebpf_get_program_type_by_name ebpf_get_program_type_name ebpf_link_close - ebpf_map_pin ebpf_object_get ebpf_object_unpin ebpf_program_attach diff --git a/include/ebpf_api.h b/include/ebpf_api.h index de0448055..ae45d928e 100644 --- a/include/ebpf_api.h +++ b/include/ebpf_api.h @@ -74,17 +74,6 @@ extern "C" uint32_t map_flags, _Out_ fd_t* map_fd); - /** - * @brief Get file descriptor to the next eBPF program. - * @param[in] previous_fd File descriptor of the previous eBPF program or ebpf_fd_invalid to - * start enumeration. - * @param[out] next_fd File descriptor of the next eBPF program or ebpf_fd_invalid if - * this is the last program. - * @retval EBPF_SUCCESS The operation was successful. - */ - ebpf_result_t - ebpf_get_next_program(fd_t previous_fd, _Out_ fd_t* next_fd); - /** * @brief Query info about an eBPF program. * @param[in] fd File descriptor of an eBPF program. diff --git a/libs/api/ebpf_api.cpp b/libs/api/ebpf_api.cpp index 168db24ee..d13f6cda7 100644 --- a/libs/api/ebpf_api.cpp +++ b/libs/api/ebpf_api.cpp @@ -851,39 +851,6 @@ ebpf_object_get(_In_z_ const char* path) return fd; } -ebpf_result_t -ebpf_get_next_program(fd_t previous_fd, _Out_ fd_t* next_fd) -{ - ebpf_assert(next_fd); - fd_t local_fd = previous_fd; - *next_fd = ebpf_fd_invalid; - - ebpf_handle_t previous_handle = _get_handle_from_file_descriptor(local_fd); - ebpf_operation_get_next_program_request_t request{ - sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_PROGRAM, previous_handle}; - - ebpf_operation_get_next_program_reply_t reply; - - uint32_t retval = invoke_ioctl(request, reply); - if (retval == ERROR_SUCCESS) { - ebpf_assert(reply.header.id == ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_PROGRAM); - ebpf_handle_t next_handle = reply.next_handle; - if (next_handle != ebpf_handle_invalid) { - fd_t fd = _create_file_descriptor_for_handle(next_handle); - if (fd == ebpf_fd_invalid) { - // Some error getting fd for the handle. - Platform::CloseHandle(next_handle); - retval = ERROR_OUTOFMEMORY; - } else { - *next_fd = fd; - } - } else { - *next_fd = ebpf_fd_invalid; - } - } - return win32_error_code_to_ebpf_result(retval); -} - ebpf_result_t ebpf_program_query_info( fd_t fd, diff --git a/libs/ebpfnetsh/programs.cpp b/libs/ebpfnetsh/programs.cpp index b99a2ed3e..99ebbe76c 100644 --- a/libs/ebpfnetsh/programs.cpp +++ b/libs/ebpfnetsh/programs.cpp @@ -649,26 +649,23 @@ handle_ebpf_show_programs( std::cout << "====== ==== ===== ========= ============= ====================\n"; } + uint32_t program_id = 0; fd_t program_fd = ebpf_fd_invalid; for (;;) { const char* program_file_name; const char* program_section_name; const char* execution_type_name; ebpf_execution_type_t program_execution_type; - fd_t next_program_fd; - status = ebpf_get_next_program(program_fd, &next_program_fd); - if (status != ERROR_SUCCESS) { + uint32_t next_program_id; + if (bpf_prog_get_next_id(program_id, &next_program_id) < 0) { break; } + program_id = next_program_id; if (program_fd != ebpf_fd_invalid) { Platform::_close(program_fd); } - program_fd = next_program_fd; - - if (program_fd == ebpf_fd_invalid) { - break; - } + program_fd = bpf_prog_get_fd_by_id(program_id); struct bpf_prog_info info; uint32_t info_size = (uint32_t)sizeof(info); diff --git a/libs/execution_context/ebpf_core.c b/libs/execution_context/ebpf_core.c index b2cfcc1da..bc34c6b03 100644 --- a/libs/execution_context/ebpf_core.c +++ b/libs/execution_context/ebpf_core.c @@ -848,17 +848,6 @@ _ebpf_core_protocol_get_next_map( EBPF_RETURN_RESULT(_ebpf_core_get_next_handle(request->previous_handle, EBPF_OBJECT_MAP, &reply->next_handle)); } -static ebpf_result_t -_ebpf_core_protocol_get_next_program( - _In_ const struct _ebpf_operation_get_next_program_request* request, - _Inout_ struct _ebpf_operation_get_next_program_reply* reply, - uint16_t reply_length) -{ - EBPF_LOG_ENTRY(); - UNREFERENCED_PARAMETER(reply_length); - EBPF_RETURN_RESULT(_ebpf_core_get_next_handle(request->previous_handle, EBPF_OBJECT_PROGRAM, &reply->next_handle)); -} - static ebpf_result_t _ebpf_core_protocol_query_program_info( _In_ const struct _ebpf_operation_query_program_info_request* request, @@ -1846,11 +1835,6 @@ static ebpf_protocol_handler_t _ebpf_protocol_handlers[] = { sizeof(struct _ebpf_operation_get_next_map_request), sizeof(struct _ebpf_operation_get_next_map_reply)}, - // EBPF_OPERATION_GET_NEXT_PROGRAM - {(ebpf_result_t(__cdecl*)(const void*))_ebpf_core_protocol_get_next_program, - sizeof(struct _ebpf_operation_get_next_program_request), - sizeof(struct _ebpf_operation_get_next_program_reply)}, - // EBPF_OPERATION_QUERY_PROGRAM_INFO {(ebpf_result_t(__cdecl*)(const void*))_ebpf_core_protocol_query_program_info, sizeof(struct _ebpf_operation_query_program_info_request), diff --git a/libs/execution_context/ebpf_protocol.h b/libs/execution_context/ebpf_protocol.h index 77b96a505..0670f340f 100644 --- a/libs/execution_context/ebpf_protocol.h +++ b/libs/execution_context/ebpf_protocol.h @@ -20,7 +20,6 @@ typedef enum _ebpf_operation_id EBPF_OPERATION_MAP_DELETE_ELEMENT, EBPF_OPERATION_MAP_GET_NEXT_KEY, EBPF_OPERATION_GET_NEXT_MAP, - EBPF_OPERATION_GET_NEXT_PROGRAM, EBPF_OPERATION_QUERY_PROGRAM_INFO, EBPF_OPERATION_UPDATE_PINNING, EBPF_OPERATION_GET_PINNING, @@ -177,18 +176,6 @@ typedef struct _ebpf_operation_get_next_map_reply ebpf_handle_t next_handle; } ebpf_operation_get_next_map_reply_t; -typedef struct _ebpf_operation_get_next_program_request -{ - struct _ebpf_operation_header header; - ebpf_handle_t previous_handle; -} ebpf_operation_get_next_program_request_t; - -typedef struct _ebpf_operation_get_next_program_reply -{ - struct _ebpf_operation_header header; - ebpf_handle_t next_handle; -} ebpf_operation_get_next_program_reply_t; - typedef struct _ebpf_operation_get_handle_by_id_request { struct _ebpf_operation_header header; diff --git a/tests/api_test/api_test.cpp b/tests/api_test/api_test.cpp index 15444dc3b..a7ce3748c 100644 --- a/tests/api_test/api_test.cpp +++ b/tests/api_test/api_test.cpp @@ -72,8 +72,6 @@ _test_program_load( ebpf_result_t result; struct bpf_object* object = nullptr; fd_t program_fd; - fd_t previous_fd = ebpf_fd_invalid; - fd_t next_fd = ebpf_fd_invalid; result = _program_load_helper(file_name, program_type, execution_type, &object, &program_fd); REQUIRE(result == expected_load_result); @@ -84,9 +82,12 @@ _test_program_load( return; } + uint32_t next_id; + REQUIRE(bpf_prog_get_next_id(0, &next_id) == 0); + // Query loaded programs to verify this program is loaded. - REQUIRE(ebpf_get_next_program(previous_fd, &next_fd) == EBPF_SUCCESS); - REQUIRE(next_fd != ebpf_fd_invalid); + program_fd = bpf_prog_get_fd_by_id(next_id); + REQUIRE(program_fd > 0); const char* program_file_name; const char* program_section_name; @@ -94,6 +95,7 @@ _test_program_load( REQUIRE( ebpf_program_query_info(program_fd, &program_execution_type, &program_file_name, &program_section_name) == EBPF_SUCCESS); + _close(program_fd); // Set the default execution type to JIT. This will eventually // be decided by a system-wide policy. TODO(Issue #288): Configure @@ -106,17 +108,13 @@ _test_program_load( REQUIRE(strcmp(program_file_name, file_name) == 0); // Next program should not be present. - previous_fd = next_fd; - REQUIRE(ebpf_get_next_program(previous_fd, &next_fd) == EBPF_SUCCESS); - REQUIRE(next_fd == ebpf_fd_invalid); + uint32_t previous_id = next_id; + REQUIRE(bpf_prog_get_next_id(previous_id, &next_id) == -ENOENT); - _close(previous_fd); - previous_fd = ebpf_fd_invalid; bpf_object__close(object); // We have closed both handles to the program. Program should be unloaded now. - REQUIRE(ebpf_get_next_program(previous_fd, &next_fd) == ERROR_SUCCESS); - REQUIRE(next_fd == ebpf_fd_invalid); + REQUIRE(bpf_prog_get_next_id(0, &next_id) == -ENOENT); } struct _ebpf_program_load_test_parameters diff --git a/tests/end_to_end/end_to_end.cpp b/tests/end_to_end/end_to_end.cpp index 8a095a975..4db70d664 100644 --- a/tests/end_to_end/end_to_end.cpp +++ b/tests/end_to_end/end_to_end.cpp @@ -1059,8 +1059,8 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]") { _test_helper_end_to_end test_helper; - fd_t program_fd; - fd_t next_program_fd; + uint32_t program_id; + uint32_t next_program_id; const char* error_message = nullptr; ebpf_result_t result; const char* file_name = nullptr; @@ -1097,11 +1097,13 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]") REQUIRE(result == EBPF_SUCCESS); ebpf_execution_type_t type; - program_fd = ebpf_fd_invalid; - REQUIRE(ebpf_get_next_program(program_fd, &next_program_fd) == EBPF_SUCCESS); - REQUIRE(next_program_fd != ebpf_fd_invalid); - program_fd = next_program_fd; + program_id = 0; + REQUIRE(bpf_prog_get_next_id(program_id, &next_program_id) == 0); + program_id = next_program_id; + fd_t program_fd = bpf_prog_get_fd_by_id(program_id); + REQUIRE(program_fd > 0); REQUIRE(ebpf_program_query_info(program_fd, &type, &file_name, §ion_name) == EBPF_SUCCESS); + Platform::_close(program_fd); REQUIRE(type == EBPF_EXECUTION_JIT); REQUIRE(strcmp(file_name, SAMPLE_PATH "droppacket.o") == 0); ebpf_free_string(file_name); @@ -1110,11 +1112,12 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]") ebpf_free_string(section_name); section_name = nullptr; - REQUIRE(ebpf_get_next_program(program_fd, &next_program_fd) == EBPF_SUCCESS); - REQUIRE(next_program_fd != ebpf_fd_invalid); - Platform::_close(program_fd); - program_fd = next_program_fd; + REQUIRE(bpf_prog_get_next_id(program_id, &next_program_id) == 0); + program_id = next_program_id; + program_fd = bpf_prog_get_fd_by_id(program_id); + REQUIRE(program_fd > 0); REQUIRE(ebpf_program_query_info(program_fd, &type, &file_name, §ion_name) == EBPF_SUCCESS); + Platform::_close(program_fd); REQUIRE(type == EBPF_EXECUTION_INTERPRET); REQUIRE(strcmp(file_name, SAMPLE_PATH "droppacket.o") == 0); REQUIRE(strcmp(section_name, "xdp") == 0); @@ -1123,9 +1126,7 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]") file_name = nullptr; section_name = nullptr; - REQUIRE(ebpf_get_next_program(program_fd, &next_program_fd) == EBPF_SUCCESS); - REQUIRE(next_program_fd == ebpf_fd_invalid); - Platform::_close(program_fd); + REQUIRE(bpf_prog_get_next_id(program_id, &next_program_id) == -ENOENT); for (int i = 0; i < _countof(object); i++) { bpf_object__close(object[i]); @@ -1175,9 +1176,8 @@ TEST_CASE("implicit_detach", "[end_to_end]") // detach the program from the hook and unload the program. bpf_object__close(object); - program_fd = ebpf_fd_invalid; - REQUIRE(ebpf_get_next_program(program_fd, &program_fd) == EBPF_SUCCESS); - REQUIRE(program_fd == ebpf_fd_invalid); + uint32_t program_id; + REQUIRE(bpf_prog_get_next_id(0, &program_id) == -ENOENT); // Close link handle (without detaching). This should delete the link // object. ebpf_object_tracking_terminate() which is called when the test @@ -1222,9 +1222,8 @@ TEST_CASE("implicit_detach_2", "[end_to_end]") // detach the program from the hook and unload the program. bpf_object__close(object); - program_fd = ebpf_fd_invalid; - REQUIRE(ebpf_get_next_program(program_fd, &program_fd) == EBPF_SUCCESS); - REQUIRE(program_fd == ebpf_fd_invalid); + uint32_t program_id; + REQUIRE(bpf_prog_get_next_id(0, &program_id) == -ENOENT); // Close link handle (without detaching). This should delete the link // object. ebpf_object_tracking_terminate() which is called when the test @@ -1272,9 +1271,8 @@ TEST_CASE("explicit_detach", "[end_to_end]") // Close program handle. bpf_object__close(object); - program_fd = ebpf_fd_invalid; - REQUIRE(ebpf_get_next_program(program_fd, &program_fd) == EBPF_SUCCESS); - REQUIRE(program_fd == ebpf_fd_invalid); + uint32_t program_id; + REQUIRE(bpf_prog_get_next_id(0, &program_id) == -ENOENT); } TEST_CASE("implicit_explicit_detach", "[end_to_end]") @@ -1311,9 +1309,8 @@ TEST_CASE("implicit_explicit_detach", "[end_to_end]") // Close program handle. That should detach the program from the hook // and unload the program. bpf_object__close(object); - program_fd = ebpf_fd_invalid; - REQUIRE(ebpf_get_next_program(program_fd, &program_fd) == EBPF_SUCCESS); - REQUIRE(program_fd == ebpf_fd_invalid); + uint32_t program_id; + REQUIRE(bpf_prog_get_next_id(0, &program_id) == -ENOENT); // Detach and close link handle. // ebpf_object_tracking_terminate() which is called when the test