Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan Jowett committed Jun 25, 2024
1 parent 90a92c2 commit c0f1cd7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
2 changes: 1 addition & 1 deletion external/ebpf-verifier
2 changes: 1 addition & 1 deletion external/usersim
Submodule usersim updated 1 files
+1 −1 external/Catch2
22 changes: 22 additions & 0 deletions include/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,28 @@ void
ring_buffer__free(struct ring_buffer* rb);
/** @} */

/**
* @brief Query the BPF program flags.
*ebpf_extension_data_v2_t
* @param[in] prog A pointer to the BPF program.
* @return The flags of the BPF program.
*/
uint32_t
bpf_program__flags(const struct bpf_program* prog);

/**
* @brief Set the BPF program flags.
*ebpf_extension_data_v2_t
* @param[in] prog A pointer to the BPF program.
* @param[in] flags The flags to set.
*ebpf_extension_data_v2_t
* @retval 0 The operation was successful.
* @retval <0 An error occured, and errno was set.
*ebpf_extension_data_v2_t
*/
int
bpf_program__set_flags(struct bpf_program* prog, uint32_t flags);

#else
#pragma warning(push)
#pragma warning(disable : 4200) // Zero-sized array in struct/union
Expand Down
10 changes: 5 additions & 5 deletions include/ebpf_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ typedef struct _ebpf_extension_program_dispatch_table
ebpf_program_batch_end_invoke_function_t ebpf_program_batch_end_invoke_function;
} ebpf_extension_program_dispatch_table_t;

typedef struct _ebpf_extension_data_1
typedef struct _ebpf_extension_data_v1
{
ebpf_extension_header_t header;
const void* data;
} ebpf_extension_data_t_1;
} ebpf_extension_data_v1_t;

typedef struct _ebpf_extension_data_2
typedef struct _ebpf_extension_data_v2
{
ebpf_extension_header_t header;
const void* data;
size_t data_size;
uint64_t flags;
} ebpf_extension_data_t_2;
} ebpf_extension_data_v2_t;

typedef ebpf_extension_data_t_2 ebpf_extension_data_t;
typedef ebpf_extension_data_v2_t ebpf_extension_data_t;

typedef struct _ebpf_attach_provider_data
{
Expand Down
6 changes: 3 additions & 3 deletions include/ebpf_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ typedef enum _ebpf_helper_function

#define EBPF_MAX_GENERAL_HELPER_FUNCTION 0xFFFF

// Warning: Version 1 of the eBPF extension data structures is being deprecated.
// Warning: Version 1 of the eBPF extension data structures will be deprecated in a future release.
// All extension data structures should be updated to version 2.
// Leaving version 1 for backward compatibility for now.
#define EBPF_ATTACH_CLIENT_DATA_VERSION_1 1
#define EBPF_ATTACH_CLIENT_DATA_VERSION_2 2
#define EBPF_ATTACH_CLIENT_DATA_CURRENT_VERSION 2

#define EBPF_ATTACH_CLIENT_DATA_VERSION_1_SIZE EBPF_SIZE_INCLUDING_FIELD(ebpf_extension_data_t_1, data)
#define EBPF_ATTACH_CLIENT_DATA_VERSION_1_TOTAL_SIZE sizeof(ebpf_extension_data_t_1)
#define EBPF_ATTACH_CLIENT_DATA_VERSION_1_SIZE EBPF_SIZE_INCLUDING_FIELD(ebpf_extension_data_v1_t, data)
#define EBPF_ATTACH_CLIENT_DATA_VERSION_1_TOTAL_SIZE sizeof(ebpf_extension_data_v1_t)
#define EBPF_ATTACH_CLIENT_DATA_HEADER_VERSION_1 \
{ \
EBPF_ATTACH_CLIENT_DATA_VERSION_1, EBPF_ATTACH_CLIENT_DATA_VERSION_1_SIZE, \
Expand Down
68 changes: 35 additions & 33 deletions libs/execution_context/ebpf_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ typedef struct _ebpf_link
_Guarded_by_(lock) bpf_attach_type_t bpf_attach_type;
_Guarded_by_(lock) enum bpf_link_type link_type;
_Guarded_by_(lock) ebpf_program_type_t program_type;
_Guarded_by_(lock) ebpf_extension_data_t_1 client_data_v1;
_Guarded_by_(lock) ebpf_extension_data_t client_data_v2;
// Note: The new v2 data structure is uses to store either the v1 or the v2 data.
// The biggest difference is that the v2 data structure has a flags field and uses the header.size field
// to represent the size of ebpf_extension_data_t rather than the size of the data being pointed to.
// The v2 structure passes the size of the structure via the header.size field and the size of the data
// being pointed to via the data_size field.
_Guarded_by_(lock) ebpf_extension_data_v2_t client_data;
_Guarded_by_(lock) NPI_CLIENT_CHARACTERISTICS client_characteristics;
_Guarded_by_(lock) HANDLE nmr_client_handle;
_Guarded_by_(lock) bool provider_attached;
Expand Down Expand Up @@ -221,7 +225,7 @@ static void
_ebpf_link_free(_Frees_ptr_ ebpf_core_object_t* object)
{
ebpf_link_t* link = (ebpf_link_t*)object;
ebpf_free((void*)link->client_data_v1.data);
ebpf_free((void*)link->client_data.data);
ebpf_lock_destroy(&link->lock);
ebpf_epoch_free(link);
}
Expand Down Expand Up @@ -254,27 +258,18 @@ ebpf_link_create(
ebpf_lock_state_t state = ebpf_lock_lock(&local_link->lock);
lock_held = true;

local_link->client_data_v1.header.version = EBPF_ATTACH_CLIENT_DATA_VERSION_1;
local_link->client_data_v1.header.size = context_data_length;
local_link->client_data.header.version = EBPF_ATTACH_CLIENT_DATA_VERSION_1;
local_link->client_data.header.size = context_data_length;

if (context_data_length > 0) {
local_link->client_data_v1.data = ebpf_allocate_with_tag(context_data_length, EBPF_POOL_TAG_LINK);
if (!local_link->client_data_v1.data) {
local_link->client_data.data = ebpf_allocate_with_tag(context_data_length, EBPF_POOL_TAG_LINK);
if (!local_link->client_data.data) {
retval = EBPF_NO_MEMORY;
goto Exit;
}
memcpy((void*)local_link->client_data_v1.data, context_data, context_data_length);
memcpy((void*)local_link->client_data.data, context_data, context_data_length);
}

ebpf_extension_data_t client_attach_data = {
.header = EBPF_ATTACH_CLIENT_DATA_HEADER_VERSION_2,
.data = local_link->client_data_v1.data,
.data_size = context_data_length,
.flags = 0,
};

local_link->client_data_v2 = client_attach_data;

local_link->module_id.Guid = module_id;
local_link->module_id.Type = MIT_GUID;
local_link->module_id.Length = sizeof(local_link->module_id);
Expand Down Expand Up @@ -335,16 +330,25 @@ ebpf_link_attach_program(_Inout_ ebpf_link_t* link, _Inout_ ebpf_program_t* prog

ebpf_assert(link->program == NULL);

link->client_data_v2.flags = ebpf_program_get_flags(program);
// If any of the flags are set, use the V2 data structure.
if (link->client_data_v2.flags != 0) {
link->client_characteristics.ClientRegistrationInstance.NpiSpecificCharacteristics = &link->client_data_v2;
}
// If no flags are set, use the V1 data structure.
else {
link->client_characteristics.ClientRegistrationInstance.NpiSpecificCharacteristics = &link->client_data_v1;
uint64_t program_flags = ebpf_program_get_flags(program);

// If any of the flags are set, convert to the V2 data structure.
if (program_flags != 0) {
// Save values from the V1 data structure.
size_t data_size = link->client_data.header.size;
const void* data = link->client_data.data;

// Rewrite the V1 data structure with the V2 data structure.
link->client_data.header.version = EBPF_ATTACH_CLIENT_DATA_VERSION_2;
link->client_data.header.size = EBPF_ATTACH_CLIENT_DATA_VERSION_2_SIZE;
link->client_data.header.total_size = EBPF_ATTACH_CLIENT_DATA_VERSION_2_TOTAL_SIZE;
link->client_data.flags = program_flags;
link->client_data.data = data;
link->client_data.data_size = data_size;
}

link->client_characteristics.ClientRegistrationInstance.NpiSpecificCharacteristics = &link->client_data;

link->program = program;
link->program_type = ebpf_program_type_uuid(link->program);

Expand Down Expand Up @@ -458,13 +462,11 @@ ebpf_link_detach_program(_Inout_ ebpf_link_t* link)

ebpf_program_detach_link(link->program, link);

ebpf_free((void*)link->client_data_v1.data);

link->client_data_v1.data = NULL;
link->client_data_v1.header.size = 0;
link->client_data.data = NULL;
link->client_data.header.size = 0;
link->client_data.header.version = EBPF_ATTACH_CLIENT_DATA_VERSION_1;
link->client_data.data_size = 0;
link->program = NULL;
link->client_data_v2.data = NULL;
link->client_data_v2.data_size = 0;

Done:
if (lock_held) {
Expand Down Expand Up @@ -573,8 +575,8 @@ ebpf_link_get_info(

// Copy any additional parameters.
size_t size = sizeof(struct bpf_link_info) - FIELD_OFFSET(struct bpf_link_info, attach_data);
if ((link->client_data_v1.header.size > 0) && (link->client_data_v1.header.size <= size)) {
memcpy(&info->attach_data, link->client_data_v1.data, link->client_data_v1.header.size);
if ((link->client_data.header.size > 0) && (link->client_data.header.size <= size)) {
memcpy(&info->attach_data, link->client_data.data, link->client_data.header.size);
}

ebpf_lock_unlock((ebpf_lock_t*)&link->lock, state);
Expand Down

0 comments on commit c0f1cd7

Please sign in to comment.