Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TraceLoggingBinary for map find, update and delete. #3781

Merged
merged 49 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
45a9158
Initial commit
shpalani Aug 17, 2024
55b5c2a
Merge branch 'main' into shpalan/map-find
shpalani Aug 21, 2024
532aa5e
Initial commit
shpalani Aug 22, 2024
abeb90d
Added _DEBUG around tail call display
shpalani Aug 23, 2024
8084769
Merge branch 'main' into shpalan/map-find
shpalani Aug 23, 2024
96e6220
Renamed key to data
shpalani Aug 23, 2024
e3bcd07
Merge branch 'shpalan/map-find' of https://github.com/shpalani/ebpf-f…
shpalani Aug 23, 2024
4d77de6
Removed _DEBUG
shpalani Aug 24, 2024
e2be6c6
Added SAL annotation, as per the comments
shpalani Aug 24, 2024
d3fa44f
Added SAL annotation, as per the comments
shpalani Aug 24, 2024
9354006
Merge branch 'main' into shpalan/map-find
shpalani Aug 24, 2024
3cf1e04
Merge branch 'main' into shpalan/map-find
shpalani Aug 26, 2024
da02fd1
Fix crash
shpalani Aug 26, 2024
dd7da62
make data non-optional
shpalani Aug 26, 2024
ef71fc2
Added the check for .key_size != 0
shpalani Aug 27, 2024
3d557d7
Merge branch 'main' into shpalan/map-find
shpalani Aug 29, 2024
ca9c05a
Merge branch 'main' into shpalan/map-find
shpalani Sep 3, 2024
45e57f4
Merge branch 'main' into shpalan/map-find
shpalani Sep 5, 2024
36f7c2e
Removed additional tracing in EBPF_LOG_MESSAGE_BINARY macro
shpalani Sep 6, 2024
152fca5
Merge branch 'shpalan/map-find' of https://github.com/shpalani/ebpf-f…
shpalani Sep 6, 2024
13a7c50
Merge branch 'main' into shpalan/map-find
shpalani Sep 9, 2024
ec67f4e
Merged with main
shpalani Sep 9, 2024
6342c9b
Merge branch 'main' into shpalan/map-find
shpalani Sep 9, 2024
0e471eb
Trying 16KB of stack size
shpalani Sep 10, 2024
17f3495
Addressed PR comment
shpalani Sep 10, 2024
8c45f68
Added macro with combined traces
shpalani Sep 12, 2024
f2ef43d
Increased stack size to 32K for DEBUG image
shpalani Sep 17, 2024
1de366d
Merge branch 'main' into shpalan/map-find
shpalani Sep 17, 2024
af32fae
Fix the string concatenation in macro
shpalani Sep 18, 2024
5fc7ede
Merge branch 'main' into shpalan/map-find
shpalani Sep 18, 2024
b6d53cc
Merge branch 'shpalan/map-find' of https://github.com/shpalani/ebpf-f…
shpalani Sep 18, 2024
f809dac
Merge branch 'main' into shpalan/map-find
shpalani Sep 19, 2024
714ff58
Merge branch 'main' into shpalan/map-find
shpalani Sep 20, 2024
284ae4f
Merge branch 'main' into shpalan/map-find
shpalani Sep 23, 2024
7a53ce9
Merge branch 'main' into shpalan/map-find
shpalani Sep 24, 2024
7c45c39
Merge branch 'shpalan/map-find' of https://github.com/shpalani/ebpf-f…
shpalani Sep 24, 2024
7c03419
Increase the stack size to 64, and added some logs to check the crash…
shpalani Sep 24, 2024
ac25174
Added retry when the dump file cannot be compressed because it is use…
shpalani Sep 25, 2024
17294e2
Expanded stack size for all netebpf wfp callouts
shpalani Sep 27, 2024
fc505fd
Merge branch 'main' into shpalan/map-find
shpalani Sep 27, 2024
94e826c
Addressed PR comments
shpalani Sep 27, 2024
4646ac7
Added _DEBUG back
shpalani Sep 27, 2024
44a3cab
Increased stack size to 20K for DEBUG
shpalani Sep 28, 2024
6b4c2ed
Add optimatization
shpalani Sep 30, 2024
c0df804
With 4K stack expansion size
shpalani Oct 1, 2024
a1550c0
Merge branch 'main' into shpalan/map-find
shpalani Oct 1, 2024
396af87
Final commit
shpalani Oct 2, 2024
9c8312c
Merge branch 'main' into shpalan/map-find
shpalani Oct 2, 2024
5a34919
Merge branch 'shpalan/map-find' of https://github.com/shpalani/ebpf-f…
shpalani Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions libs/execution_context/ebpf_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ typedef uint8_t* ebpf_lru_entry_t;
#define EBPF_LRU_ENTRY_KEY_PTR(map, entry) \
((uint8_t*)(((uint8_t*)entry) + EBPF_LRU_ENTRY_KEY_OFFSET(map->partition_count)))

#define EBPF_LOG_MAP_OPERATION(flags, operation, map, key) \
if (((flags) & EBPF_MAP_FLAG_HELPER) && (map)->ebpf_map_definition.key_size != 0) { \
EBPF_LOG_MESSAGE_UTF8_STRING( \
EBPF_TRACELOG_LEVEL_VERBOSE, EBPF_TRACELOG_KEYWORD_MAP, "Map "##operation, &(map)->name); \
EBPF_LOG_MESSAGE_BINARY( \
EBPF_TRACELOG_LEVEL_VERBOSE, \
EBPF_TRACELOG_KEYWORD_MAP, \
"Key", \
(key), \
(map)->ebpf_map_definition.key_size); \
}

/**
* @brief The partition of the LRU map key history.
*/
Expand Down Expand Up @@ -2447,6 +2459,7 @@ ebpf_map_find_entry(
{
// High volume call - Skip entry/exit logging.
uint8_t* return_value = NULL;

if (!(flags & EBPF_MAP_FLAG_HELPER) && (key_size != map->ebpf_map_definition.key_size)) {
EBPF_LOG_MESSAGE_UINT64_UINT64(
EBPF_TRACELOG_LEVEL_ERROR,
Expand Down Expand Up @@ -2486,6 +2499,8 @@ ebpf_map_find_entry(
return EBPF_INVALID_ARGUMENT;
}

EBPF_LOG_MAP_OPERATION(flags, "lookup", map, key);

ebpf_core_object_t* object = ebpf_map_metadata_tables[type].get_object_from_entry(map, key);
if (object) {
return_value = (uint8_t*)object;
Expand Down Expand Up @@ -2605,6 +2620,8 @@ ebpf_map_update_entry(
return EBPF_OPERATION_NOT_SUPPORTED;
}

EBPF_LOG_MAP_OPERATION(flags, "update", map, key);

if ((flags & EBPF_MAP_FLAG_HELPER) &&
ebpf_map_metadata_tables[map->ebpf_map_definition.type].update_entry_per_cpu) {
result = ebpf_map_metadata_tables[map->ebpf_map_definition.type].update_entry_per_cpu(map, key, value, option);
Expand Down Expand Up @@ -2668,6 +2685,8 @@ ebpf_map_delete_entry(_In_ ebpf_map_t* map, size_t key_size, _In_reads_(key_size
return EBPF_OPERATION_NOT_SUPPORTED;
}

EBPF_LOG_MAP_OPERATION(flags, "delete", map, key);

ebpf_result_t result = ebpf_map_metadata_tables[map->ebpf_map_definition.type].delete_entry(map, key);
return result;
}
Expand Down
5 changes: 5 additions & 0 deletions libs/execution_context/ebpf_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,11 @@ ebpf_program_invoke(

if (current_program->parameters.code_type == EBPF_CODE_JIT ||
current_program->parameters.code_type == EBPF_CODE_NATIVE) {
EBPF_LOG_MESSAGE_UTF8_STRING(
EBPF_TRACELOG_LEVEL_VERBOSE,
EBPF_TRACELOG_KEYWORD_PROGRAM,
"Tail call program",
&current_program->parameters.program_name);
ebpf_program_entry_point_t function_pointer;
function_pointer = (ebpf_program_entry_point_t)(current_program->code_or_vm.code.code_pointer);
*result = (function_pointer)(context);
Expand Down
12 changes: 12 additions & 0 deletions libs/shared/ebpf_tracelog.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ extern "C"
ebpf_log_message_uint64_uint64(_##trace_level##, _##keyword##, message, value1, value2); \
}

void
ebpf_log_message_binary(
ebpf_tracelog_level_t trace_level,
ebpf_tracelog_keyword_t keyword,
_In_z_ const char* message,
_In_reads_bytes_(data_size) const void* data,
uint32_t data_size);
#define EBPF_LOG_MESSAGE_BINARY(trace_level, keyword, message, data, data_size) \
if (TraceLoggingProviderEnabled(ebpf_tracelog_provider, trace_level, keyword)) { \
ebpf_log_message_binary(_##trace_level##, _##keyword##, message, data, data_size); \
}

void
ebpf_log_message_error(
ebpf_tracelog_level_t trace_level,
Expand Down
6 changes: 5 additions & 1 deletion libs/shared/kernel/shared_kernel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
<ItemGroup>
<ClCompile Include="..\ebpf_serialize.c" />
<ClCompile Include="..\shared_common.c" />
<ClCompile Include="..\tracelog.c" />
<ClCompile Include="..\tracelog.c" >
<Optimization>Full</Optimization>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<WholeProgramOptimization>true</WholeProgramOptimization>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\ebpf_ring_buffer.h" />
Expand Down
79 changes: 79 additions & 0 deletions libs/shared/tracelog.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,85 @@ __declspec(noinline) void ebpf_log_message_uint64_uint64(
}
}

#define _EBPF_LOG_MESSAGE_BINARY(trace_level, keyword, message, data, data_size) \
TraceLoggingWrite( \
ebpf_tracelog_provider, \
EBPF_TRACELOG_EVENT_GENERIC_MESSAGE, \
TraceLoggingLevel((trace_level)), \
TraceLoggingKeyword((keyword)), \
TraceLoggingString((message), "Message"), \
TraceLoggingBinary((data), (data_size)));
#define EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(trace_level, message, data, data_size) \
switch (keyword) { \
CASE_FUNCTION_ENTRY_EXIT: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_FUNCTION_ENTRY_EXIT, message, data, data_size); \
break; \
CASE_BASE: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_BASE, message, data, data_size); \
break; \
CASE_ERROR: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_ERROR, message, data, data_size); \
break; \
CASE_EPOCH: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_EPOCH, message, data, data_size); \
break; \
CASE_CORE: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_CORE, message, data, data_size); \
break; \
CASE_LINK: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_LINK, message, data, data_size); \
break; \
CASE_MAP: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_MAP, message, data, data_size); \
break; \
CASE_PROGRAM: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_PROGRAM, message, data, data_size); \
break; \
CASE_API: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_API, message, data, data_size); \
break; \
CASE_PRINTK: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_PRINTK, message, data, data_size); \
break; \
CASE_NATIVE: \
_EBPF_LOG_MESSAGE_BINARY(trace_level, KEYWORD_NATIVE, message, data, data_size); \
break; \
default: \
ebpf_assert(!"Invalid keyword"); \
break; \
}
__declspec(noinline) void ebpf_log_message_binary(
ebpf_tracelog_level_t trace_level,
ebpf_tracelog_keyword_t keyword,
_In_z_ const char* message,
_In_reads_bytes_(data_size) const void* data,
uint32_t data_size)
{
switch (trace_level) {
CASE_LOG_ALWAYS:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_LOG_ALWAYS, message, data, data_size);
break;
CASE_CRITICAL:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_CRITICAL, message, data, data_size);
break;
CASE_LEVEL_ERROR:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_ERROR, message, data, data_size);
break;
CASE_WARNING:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_WARNING, message, data, data_size);
break;
CASE_INFO:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_INFO, message, data, data_size);
break;
CASE_VERBOSE:
EBPF_LOG_MESSAGE_BINARY_KEYWORD_SWITCH(LEVEL_VERBOSE, message, data, data_size);
break;
default:
ebpf_assert(!"Invalid trace level");
break;
}
}

#define _EBPF_LOG_MESSAGE_ERROR(trace_level, keyword, message, error) \
TraceLoggingWrite( \
ebpf_tracelog_provider, \
Expand Down
4 changes: 2 additions & 2 deletions netebpfext/net_ebpf_ext_hook_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ebpf_extension_uuids.h"
#include "net_ebpf_ext_hook_provider.h"

#define NET_EBPF_EXT_STACK_EXPANSION_SIZE 1024 * 10
#define NET_EBPF_EXT_STACK_EXPANSION_SIZE 1024 * 4

typedef struct _net_ebpf_ext_hook_client_rundown
{
Expand Down Expand Up @@ -315,7 +315,7 @@ net_ebpf_extension_hook_expand_stack_and_invoke_programs(
#pragma warning(disable : 28160) // Error annotation: DISPATCH_LEVEL is only supported on Windows 7 or later.
// Expand the stack and call the program.
status = KeExpandKernelStackAndCalloutEx(
(PEXPAND_STACK_CALLOUT)_net_ebpf_extension_invoke_programs_callout,
_net_ebpf_extension_invoke_programs_callout,
&invoke_parameters,
NET_EBPF_EXT_STACK_EXPANSION_SIZE,
FALSE,
Expand Down
3 changes: 2 additions & 1 deletion netebpfext/net_ebpf_ext_sock_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,8 @@ net_ebpf_extension_sock_addr_authorize_recv_accept_classify(
goto Exit;
}

program_result = net_ebpf_extension_hook_invoke_programs(sock_addr_ctx, &filter_context->base, &result);
program_result =
net_ebpf_extension_hook_expand_stack_and_invoke_programs(sock_addr_ctx, &filter_context->base, &result);
shpalani marked this conversation as resolved.
Show resolved Hide resolved
if (program_result == EBPF_OBJECT_NOT_FOUND) {
// No eBPF program is attached to this filter.
goto Exit;
Expand Down
24 changes: 20 additions & 4 deletions scripts/Run-Self-Hosted-Runner-Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ function GetDriveFreeSpaceGB

# Convert drive to single letter (eg. "C:" to "C") for Get-Volume.
$DriveSpecification = $DriveSpecification -replace ".$"
$FreeSpaceGB = (((Get-Volume $DriveSpecification).SizeRemaining) / 1GB).ToString("F2")
$Volume = Get-Volume $DriveSpecification
if ($Volume -eq $Null) {
ThrowWithErrorMessage -ErrorMessage "*** ERROR *** Drive $DriveSpecification not found."
}
$FreeSpaceGB = (($Volume.SizeRemaining) / 1GB).ToString("F2")

return $FreeSpaceGB
}
Expand Down Expand Up @@ -136,13 +140,19 @@ if ($VerbosePreference -eq 'Continue') {
}

# Get the available free space before test start (useful in investigating dump file creation failures)
$BeforeTestFreeSpaceGB = GetDriveFreeSpaceGB -DriveSpecification $Env:SystemDrive
try {
$BeforeTestFreeSpaceGB = GetDriveFreeSpaceGB -DriveSpecification $Env:SystemDrive
} catch {
Write-Log "Error getting available disk space: $_"
$BeforeTestFreeSpaceGB = "Unknown"
# Continue with the test.
}
Write-Log "Available System disk space (Before test start): $BeforeTestFreeSpaceGB GB"

# Start the test process using the provided command and arguments.
$FullTestCommandSpec = Join-Path $Pwd $TestCommand
Write-Log "`n`n"
Write-Log "Staring Test command: $FullTestCommandSpec $TestArguments"
Write-Log "Starting Test command: $FullTestCommandSpec $TestArguments"
Write-Log "Test hang timeout: $TestHangTimeout (seconds)"
Write-Log "`n"

Expand Down Expand Up @@ -175,7 +185,13 @@ if (-not $WaitResult) {

# Get the available free space at this point in case the test creates its own files.
# (useful in investigating user and/or kernel dump file creation failures).
$DriveFreeSpaceGB = GetDriveFreeSpaceGB -DriveSpecification $Env:SystemDrive
try {
$DriveFreeSpaceGB = GetDriveFreeSpaceGB -DriveSpecification $Env:SystemDrive
} catch {
Write-Log "Error getting available disk space: $_"
$DriveFreeSpaceGB = "Unknown"
# Continue with the test.
}
Write-Log "Current available disk space: $DriveFreeSpaceGB GB`n"

# $TestProcess refers to 'cmd.exe' which ends up running the real test application.
Expand Down
26 changes: 21 additions & 5 deletions scripts/config_test_vm.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,27 @@ function ArchiveKernelModeDumpOnVM

Write-Output `
"Compressing kernel dump files: $KernelModeDumpFileSourcePath -> $KernelModeDumpFileDestinationPath"
Compress-Archive `
-Path $KernelModeDumpFileSourcePath\*.dmp `
-DestinationPath $KernelModeDumpFileDestinationPath\km_dumps.zip `
-CompressionLevel Fastest `
-Force

# Retry 3 times to ensure compression operation succeeds.
# To mitigate error message: "The process cannot access the file 'C:\Windows\MEMORY.DMP' because it is being used by another process."
$retryCount = 1
while ($retryCount -lt 4) {
$error.clear()
Compress-Archive `
-Path "$KernelModeDumpFileSourcePath\*.dmp" `
-DestinationPath "$KernelModeDumpFileDestinationPath\km_dumps.zip" `
-CompressionLevel Fastest `
-Force
if ($error[0] -ne $null) {
$ErrorMessage = "*** ERROR *** Failed to compress kernel mode dump files: $error. Retrying $retryCount"
Write-Output $ErrorMessage
Start-Sleep -seconds (5 * $retryCount)
$retryCount++
} else {
# Compression succeeded.
break;
}
}

if (Test-Path $KernelModeDumpFileDestinationPath\km_dumps.zip -PathType Leaf) {
$CompressedDumpFile = get-childitem -Path $KernelModeDumpFileDestinationPath\km_dumps.zip
Expand Down
Loading