Skip to content

Commit

Permalink
Export functions necessary for ebpf-go port (microsoft#4116)
Browse files Browse the repository at this point in the history
* Export ebpf_close_fd and ebpf_dup_fd

Export functions used to manipulate fd_t. This makes the use of the UCRT
_get_osfhandle an implementation detail which is abstracted away from
users of ebpfapi.dll. This also makes it easier to use the API via
run-time dynamic linking.

* Allow attaching a link as an fd

Add a function which returns the link as an fd when attaching a program.
The existing functions are re-jigged to reduce code duplication.

* Export functions to translate between GUID and enums

Allow users to resolve libbpf style enums to Windows GUIDs and vice
versa.

* Fix up spelling mistakes

Co-authored-by: Dave Thaler <dthaler1968@gmail.com>

* Fix sorting of Source.def

* Rename to ebpf_program_attach_fds

* Add tests for ebpf_dup_fd and ebpf_close_fd

* Rename link argument to link_handle

* Document return value of ebpf_program_attach_fds

Co-authored-by: Dave Thaler <dthaler1968@gmail.com>

* Fix ebpf_get_attach_type use-after-free

* Rename ebpf_dup_fd to ebpf_duplicate_fd

* Rename to ebpf_program_attach_by_fds

* Fix docstring of ebpf_get_ebpf_attach_type

* Temporarily disable _wer_report::_terminate_hook

---------

Co-authored-by: Dave Thaler <dthaler1968@gmail.com>
  • Loading branch information
lmb and dthaler authored Feb 13, 2025
1 parent c0bfd35 commit d8b9204
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 116 deletions.
9 changes: 8 additions & 1 deletion ebpfapi/Source.def
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,18 @@ EXPORTS
ebpf_api_close_handle
ebpf_api_get_pinned_map_info
ebpf_api_map_info_free
ebpf_close_fd
ebpf_duplicate_fd
ebpf_enumerate_programs
ebpf_enumerate_sections = ebpf_enumerate_programs
ebpf_free_programs
ebpf_free_sections = ebpf_free_programs
ebpf_free_string
ebpf_get_attach_type_name
ebpf_get_bpf_attach_type
ebpf_get_bpf_program_type
ebpf_get_ebpf_attach_type
ebpf_get_ebpf_program_type
ebpf_get_next_pinned_object_path
ebpf_get_next_pinned_program_path
ebpf_get_program_info_from_verifier
Expand All @@ -127,6 +133,7 @@ EXPORTS
ebpf_object_unpin
ebpf_program_attach
ebpf_program_attach_by_fd
ebpf_program_attach_by_fds
ebpf_program_query_info
ebpf_ring_buffer_map_write
ebpf_store_delete_program_information
Expand All @@ -142,5 +149,5 @@ EXPORTS
libbpf_num_possible_cpus
libbpf_prog_type_by_name
libbpf_strerror
ring_buffer__free
ring_buffer__new
ring_buffer__free
70 changes: 70 additions & 0 deletions include/ebpf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,22 @@ extern "C"
size_t attach_parameters_size,
_Outptr_ struct bpf_link** link) EBPF_NO_EXCEPT;

/**
* @brief Attach an eBPF program by program file descriptor and return
* the link as a file descriptor.
*
* @see ebpf_program_attach_by_fd
*
* @retval EBPF_SUCCESS The operation was successful.
*/
_Must_inspect_result_ ebpf_result_t
ebpf_program_attach_by_fds(
fd_t program_fd,
_In_opt_ const ebpf_attach_type_t* attach_type,
_In_reads_bytes_opt_(attach_parameters_size) void* attach_parameters,
size_t attach_parameters_size,
_Out_ fd_t* link) EBPF_NO_EXCEPT;

/**
* @brief Detach an eBPF program from an attach point represented by
* the bpf_link structure.
Expand Down Expand Up @@ -473,6 +489,17 @@ extern "C"
_Must_inspect_result_ ebpf_result_t
ebpf_close_fd(fd_t fd) EBPF_NO_EXCEPT;

/**
* @brief Duplicate a file descriptor.
*
* @param [in] fd File descriptor to be duplicated.
* @param [out] dup Duplicated file descriptor.
*
* @retval EBPF_SUCCESS The operation was successful.
*/
_Must_inspect_result_ ebpf_result_t
ebpf_duplicate_fd(fd_t fd, _Out_ fd_t* dup) EBPF_NO_EXCEPT;

/**
* @brief Get eBPF program type and expected attach type by name.
*
Expand Down Expand Up @@ -599,6 +626,49 @@ extern "C"
ebpf_ring_buffer_map_write(
fd_t ring_buffer_map_fd, _In_reads_bytes_(data_length) const void* data, size_t data_length) EBPF_NO_EXCEPT;

/**
* @brief Get eBPF program type for the specified BPF program type.
*
* @param[in] program_type BPF program type.
*
* @returns Pointer to eBPF program type, or NULL if not found.
*/
_Ret_maybenull_ const ebpf_program_type_t*
ebpf_get_ebpf_program_type(bpf_prog_type_t bpf_program_type) EBPF_NO_EXCEPT;

/**
* @brief Get eBPF attach type for the specified BPF attach type.
*
* @param[in] bpf_attach_type BPF attach type.
* @param[out] ebpf_attach_type eBPF attach type or GUID_NULL.
*
* @retval EBPF_SUCCESS The operation was successful.
* @retval EBPF_INVALID_ARGUMENT The attach type is unknown.
*/
_Must_inspect_result_ ebpf_result_t
ebpf_get_ebpf_attach_type(bpf_attach_type_t bpf_attach_type, _Out_ ebpf_attach_type_t* ebpf_attach_type)
EBPF_NO_EXCEPT;

/**
* @brief Get BPF program type for the specified eBPF program type.
*
* @param[in] program_type eBPF program type GUID.
*
* @returns BPF program type, or BPF_PROG_TYPE_UNSPEC if not found.
*/
bpf_prog_type_t
ebpf_get_bpf_program_type(_In_ const ebpf_program_type_t* program_type) EBPF_NO_EXCEPT;

/**
* @brief Get BPF attach type for the specified eBPF attach type.
*
* @param[in] attach_type eBPF attach type GUID.
*
* @returns BPF attach type, or BPF_ATTACH_TYPE_UNSPEC if not found.
*/
bpf_attach_type_t
ebpf_get_bpf_attach_type(_In_ const ebpf_attach_type_t* ebpf_attach_type) EBPF_NO_EXCEPT;

#ifdef __cplusplus
}
#endif
30 changes: 0 additions & 30 deletions libs/api/api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,6 @@ ebpf_program_load_bytes(
_Out_ fd_t* program_fd) noexcept;
#endif

/**
* @brief Get eBPF program type for the specified bpf program type.
*
* @param[in] program_type Bpf program type.
*
* @returns Pointer to eBPF program type, or NULL if not found.
*/
_Ret_maybenull_ const ebpf_program_type_t*
ebpf_get_ebpf_program_type(bpf_prog_type_t bpf_program_type) noexcept;

/**
* @brief Get eBPF attach type for the specified bpf attach type.
*
Expand All @@ -732,26 +722,6 @@ ebpf_get_ebpf_program_type(bpf_prog_type_t bpf_program_type) noexcept;
_Ret_maybenull_ const ebpf_attach_type_t*
get_ebpf_attach_type(bpf_attach_type_t bpf_attach_type) noexcept;

/**
* @brief Get bpf program type for the specified eBPF program type.
*
* @param[in] program_type eBPF program type GUID.
*
* @returns Bpf program type, or BPF_PROG_TYPE_UNSPEC if not found.
*/
bpf_prog_type_t
get_bpf_program_type(_In_ const ebpf_program_type_t* program_type) noexcept;

/**
* @brief Get bpf attach type for the specified eBPF attach type.
*
* @param[in] attach_type eBPF attach type GUID.
*
* @returns Bpf attach type, or BPF_ATTACH_TYPE_UNSPEC if not found.
*/
bpf_attach_type_t
get_bpf_attach_type(_In_ const ebpf_attach_type_t* ebpf_attach_type) noexcept;

/**
* @brief Initialize the eBPF library's thread local storage.
*/
Expand Down
Loading

0 comments on commit d8b9204

Please sign in to comment.