Skip to content

Commit ce77c22

Browse files
authored
Merge branch 'main' into clean-issue4261
2 parents bcaf06d + d9955f7 commit ce77c22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+924
-475
lines changed

.github/workflows/cicd.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ jobs:
291291
# Always run this job.
292292
# Only run this on repos that have self-host runners.
293293
needs: regular
294-
# Disabled for now for pull_request, merge_group, and push until in-progress breaking changes are completed.
295-
if: github.repository == 'microsoft/ebpf-for-windows' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
294+
# Disabled for now for schedule, pull_request, merge_group, and push until in-progress breaking changes are completed.
295+
if: github.repository == 'microsoft/ebpf-for-windows' && (github.event_name == 'workflow_dispatch')
296296
uses: ./.github/workflows/reusable-test.yml
297297
with:
298298
pre_test: .\setup_ebpf_cicd_tests.ps1 -TestMode "Regression" -RegressionArtifactsVersion "0.21.0" -GranularTracing

docs/NativeCodeGeneration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,6 @@ differ for each person regenerating them.
315315
316316
These paths therefore need to be canonicalized to make them portable and this is done by running the following command
317317
from the ```ebpf-for-windows``` project root directory:
318-
``` .\scripts\generate_expected_bpf2c_output.ps1 .\x64\Debug\```
318+
``` .\scripts\generate_expected_bpf2c_output.ps1 .\x64\Debug\```.
319+
320+
*Note: Tests may also fail if files generated by this script are formatted through clang-format.*

ebpfapi/Source.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ EXPORTS
1212
bpf_link__destroy
1313
bpf_link__disconnect
1414
bpf_link__fd
15+
bpf_link__open
1516
bpf_link__pin
1617
bpf_link__unpin
1718
bpf_link_detach
@@ -128,6 +129,7 @@ EXPORTS
128129
ebpf_get_program_type_by_name
129130
ebpf_get_program_type_name
130131
ebpf_link_close
132+
ebpf_link_mark_as_legacy_mode
131133
ebpf_map_set_wait_handle
132134
ebpf_object_get
133135
ebpf_object_get_execution_type

ebpfapi/rpc_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ clean_up_rpc_binding()
193193
_Must_inspect_result_ _Ret_maybenull_
194194
_Post_writable_byte_size_(size) void* __RPC_USER MIDL_user_allocate(_In_ size_t size)
195195
{
196-
return ebpf_allocate(size);
196+
return ebpf_allocate_with_tag(size, EBPF_POOL_TAG_DEFAULT);
197197
}
198198

199199
void __RPC_USER

ebpfsvc/rpc_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ shutdown_rpc_server()
117117
_Must_inspect_result_ _Ret_maybenull_
118118
_Post_writable_byte_size_(size) void* __RPC_USER MIDL_user_allocate(_In_ size_t size)
119119
{
120-
return ebpf_allocate(size);
120+
return ebpf_allocate_with_tag(size, EBPF_POOL_TAG_DEFAULT);
121121
}
122122

123123
void __RPC_USER

include/ebpf_api.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,18 @@ extern "C"
906906
_In_opt_ void* ctx,
907907
_In_opt_ const struct ebpf_perf_buffer_opts* opts) EBPF_NO_EXCEPT;
908908

909+
/**
910+
* @brief Mark a link as operating in legacy mode, which means it doesn't detach
911+
* automatically when last user mode reference is closed.
912+
* This is used by bpf_prog_attach and related APIs to implement legacy behavior.
913+
*
914+
* @param[in] link File descriptor for the link.
915+
*
916+
* @retval EBPF_SUCCESS The operation was successful.
917+
* @retval EBPF_INVALID_ARGUMENT One or more parameters are wrong.
918+
*/
919+
_Must_inspect_result_ ebpf_result_t
920+
ebpf_link_mark_as_legacy_mode(fd_t link) EBPF_NO_EXCEPT;
909921
#ifdef __cplusplus
910922
}
911923
#endif

include/ebpf_nethooks.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bind_hook_t(bind_md_t* context);
5656

5757
#define BPF_SOCK_ADDR_VERDICT_REJECT 0
5858
#define BPF_SOCK_ADDR_VERDICT_PROCEED 1
59+
#define BPF_SOCK_ADDR_VERDICT_PROCEED_HARD 2
5960

6061
#ifdef _MSC_VER
6162
#pragma warning(push)
@@ -134,10 +135,11 @@ EBPF_HELPER(int, bpf_sock_addr_set_redirect_context, (bpf_sock_addr_t * ctx, voi
134135
* \ref EBPF_ATTACH_TYPE_CGROUP_INET6_RECV_ACCEPT
135136
*
136137
* @param[in] context \ref bpf_sock_addr_t
137-
* @retval BPF_SOCK_ADDR_VERDICT_PROCEED Block the socket operation.
138-
* @retval BPF_SOCK_ADDR_VERDICT_REJECT Allow the socket operation.
138+
* @retval BPF_SOCK_ADDR_VERDICT_REJECT Block the socket operation. Maps to a hard block in WFP.
139+
* @retval BPF_SOCK_ADDR_VERDICT_PROCEED Allow the socket operation. Maps to a soft permit in WFP.
140+
* @retval BPF_SOCK_ADDR_VERDICT_PROCEED_HARD Allow the socket operation. Maps to a hard permit in WFP.
139141
*
140-
* Any other return value other than the two mentioned above is treated as BPF_SOCK_ADDR_VERDICT_REJECT.
142+
* Any return value other than the ones mentioned above is treated as BPF_SOCK_ADDR_VERDICT_REJECT.
141143
*/
142144
typedef int
143145
sock_addr_hook_t(bpf_sock_addr_t* context);

libs/api/Verifier.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ load_byte_code(
326326
}
327327

328328
for (auto& raw_program : raw_programs) {
329-
program = (ebpf_program_t*)ebpf_allocate(sizeof(ebpf_program_t));
329+
program = (ebpf_program_t*)ebpf_allocate_with_tag(sizeof(ebpf_program_t), EBPF_POOL_TAG_DEFAULT);
330330
if (program == nullptr) {
331331
result = EBPF_NO_MEMORY;
332332
goto Exit;
@@ -348,7 +348,7 @@ load_byte_code(
348348
goto Exit;
349349
}
350350
size_t ebpf_bytes = instruction_count * sizeof(ebpf_inst);
351-
program->instructions = (ebpf_inst*)ebpf_allocate(ebpf_bytes);
351+
program->instructions = (ebpf_inst*)ebpf_allocate_with_tag(ebpf_bytes, EBPF_POOL_TAG_DEFAULT);
352352
if (program->instructions == nullptr) {
353353
result = EBPF_NO_MEMORY;
354354
goto Exit;
@@ -405,7 +405,7 @@ load_byte_code(
405405
goto Exit;
406406
}
407407

408-
map = (ebpf_map_t*)ebpf_allocate(sizeof(ebpf_map_t));
408+
map = (ebpf_map_t*)ebpf_allocate_with_tag(sizeof(ebpf_map_t), EBPF_POOL_TAG_DEFAULT);
409409
if (map == nullptr) {
410410
result = EBPF_NO_MEMORY;
411411
goto Exit;
@@ -435,7 +435,7 @@ load_byte_code(
435435
} catch (std::runtime_error& err) {
436436
auto message = err.what();
437437
auto message_length = strlen(message) + 1;
438-
char* error = reinterpret_cast<char*>(ebpf_allocate(message_length + 1));
438+
char* error = reinterpret_cast<char*>(ebpf_allocate_with_tag(message_length + 1, EBPF_POOL_TAG_DEFAULT));
439439
if (error) {
440440
strcpy_s(error, message_length, message);
441441
}
@@ -468,7 +468,7 @@ load_byte_code(
468468
static void
469469
_ebpf_add_stat(_Inout_ ebpf_api_program_info_t* info, std::string key, int value) noexcept(false)
470470
{
471-
ebpf_stat_t* stat = (ebpf_stat_t*)ebpf_allocate(sizeof(*stat));
471+
ebpf_stat_t* stat = (ebpf_stat_t*)ebpf_allocate_with_tag(sizeof(*stat), EBPF_POOL_TAG_DEFAULT);
472472
if (stat == nullptr) {
473473
throw std::runtime_error("Out of memory");
474474
}
@@ -503,7 +503,7 @@ ebpf_api_elf_enumerate_programs(
503503
try {
504504
auto raw_programs = read_elf(file, section ? std::string(section) : std::string(), verifier_options, platform);
505505
for (const auto& raw_program : raw_programs) {
506-
info = (ebpf_api_program_info_t*)ebpf_allocate(sizeof(*info));
506+
info = (ebpf_api_program_info_t*)ebpf_allocate_with_tag(sizeof(*info), EBPF_POOL_TAG_DEFAULT);
507507
if (info == nullptr) {
508508
throw std::runtime_error("Out of memory");
509509
}
@@ -548,7 +548,7 @@ ebpf_api_elf_enumerate_programs(
548548
info->offset_in_section = raw_program.insn_off;
549549
std::vector<uint8_t> raw_data = convert_ebpf_program_to_bytes(raw_program.prog);
550550
info->raw_data_size = raw_data.size();
551-
info->raw_data = (char*)ebpf_allocate(info->raw_data_size);
551+
info->raw_data = (char*)ebpf_allocate_with_tag(info->raw_data_size, EBPF_POOL_TAG_DEFAULT);
552552
if (info->raw_data == nullptr) {
553553
throw std::runtime_error("Out of memory");
554554
}

libs/api/ebpf_api.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,8 +1483,8 @@ ebpf_program_query_info(
14831483

14841484
size_t file_name_length = reply->section_name_offset - reply->file_name_offset;
14851485
size_t section_name_length = reply->header.length - reply->section_name_offset;
1486-
char* local_file_name = reinterpret_cast<char*>(ebpf_allocate(file_name_length + 1));
1487-
char* local_section_name = reinterpret_cast<char*>(ebpf_allocate(section_name_length + 1));
1486+
char* local_file_name = reinterpret_cast<char*>(ebpf_allocate_with_tag(file_name_length + 1, EBPF_POOL_TAG_DEFAULT));
1487+
char* local_section_name = reinterpret_cast<char*>(ebpf_allocate_with_tag(section_name_length + 1, EBPF_POOL_TAG_DEFAULT));
14881488

14891489
if (!local_file_name || !local_section_name) {
14901490
ebpf_free(local_file_name);
@@ -1656,7 +1656,7 @@ ebpf_program_attach(
16561656

16571657
ebpf_assert(program);
16581658

1659-
ebpf_link_t* new_link = (ebpf_link_t*)ebpf_allocate(sizeof(ebpf_link_t));
1659+
ebpf_link_t* new_link = (ebpf_link_t*)ebpf_allocate_with_tag(sizeof(ebpf_link_t), EBPF_POOL_TAG_DEFAULT);
16601660
if (new_link == nullptr) {
16611661
EBPF_RETURN_RESULT(EBPF_NO_MEMORY);
16621662
}
@@ -1712,7 +1712,7 @@ ebpf_program_attach_by_fd(
17121712
EBPF_LOG_ENTRY();
17131713
ebpf_assert(link);
17141714

1715-
ebpf_link_t* new_link = (ebpf_link_t*)ebpf_allocate(sizeof(ebpf_link_t));
1715+
ebpf_link_t* new_link = (ebpf_link_t*)ebpf_allocate_with_tag(sizeof(ebpf_link_t), EBPF_POOL_TAG_DEFAULT);
17161716
if (new_link == nullptr) {
17171717
EBPF_RETURN_RESULT(EBPF_NO_MEMORY);
17181718
}
@@ -2286,7 +2286,7 @@ _initialize_ebpf_object_from_native_file(
22862286
object.execution_type = EBPF_EXECUTION_NATIVE;
22872287

22882288
for (ebpf_api_program_info_t* info = infos; info; info = info->next) {
2289-
program = (ebpf_program_t*)ebpf_allocate(sizeof(ebpf_program_t));
2289+
program = (ebpf_program_t*)ebpf_allocate_with_tag(sizeof(ebpf_program_t), EBPF_POOL_TAG_DEFAULT);
22902290
if (program == nullptr) {
22912291
result = EBPF_NO_MEMORY;
22922292
goto Exit;
@@ -2580,7 +2580,7 @@ _ebpf_pe_get_map_definitions(
25802580
break;
25812581
}
25822582

2583-
map = (ebpf_map_t*)ebpf_allocate(sizeof(ebpf_map_t));
2583+
map = (ebpf_map_t*)ebpf_allocate_with_tag(sizeof(ebpf_map_t), EBPF_POOL_TAG_DEFAULT);
25842584
if (map == nullptr) {
25852585
goto Error;
25862586
}
@@ -2759,7 +2759,7 @@ _ebpf_pe_add_section(
27592759
std::string elf_section_name = pe_context->section_names[pe_section_name];
27602760
std::string program_name = pe_context->program_names[pe_section_name];
27612761

2762-
ebpf_api_program_info_t* info = (ebpf_api_program_info_t*)ebpf_allocate(sizeof(*info));
2762+
ebpf_api_program_info_t* info = (ebpf_api_program_info_t*)ebpf_allocate_with_tag(sizeof(*info), EBPF_POOL_TAG_DEFAULT);
27632763
if (info == nullptr) {
27642764
pe_context->result = EBPF_NO_MEMORY;
27652765
return_value = 1;
@@ -2785,7 +2785,7 @@ _ebpf_pe_add_section(
27852785
info->expected_attach_type = pe_context->section_attach_types[pe_section_name];
27862786

27872787
info->raw_data_size = section_header.Misc.VirtualSize;
2788-
info->raw_data = (char*)ebpf_allocate(section_header.Misc.VirtualSize);
2788+
info->raw_data = (char*)ebpf_allocate_with_tag(section_header.Misc.VirtualSize, EBPF_POOL_TAG_DEFAULT);
27892789
if (info->raw_data == nullptr || info->section_name == nullptr) {
27902790
pe_context->result = EBPF_NO_MEMORY;
27912791
return_value = 1;
@@ -3633,7 +3633,7 @@ _load_native_programs(
36333633
size_t buffer_size = offsetof(ebpf_operation_load_native_programs_reply_t, data) + handles_size;
36343634

36353635
if (count_of_maps > 0) {
3636-
*map_handles = (ebpf_handle_t*)ebpf_allocate(map_handles_size);
3636+
*map_handles = (ebpf_handle_t*)ebpf_allocate_with_tag(map_handles_size, EBPF_POOL_TAG_DEFAULT);
36373637
if (*map_handles == nullptr) {
36383638
EBPF_LOG_MESSAGE(
36393639
EBPF_TRACELOG_LEVEL_ERROR,
@@ -3645,7 +3645,7 @@ _load_native_programs(
36453645
}
36463646

36473647
if (count_of_programs > 0) {
3648-
*program_handles = (ebpf_handle_t*)ebpf_allocate(program_handles_size);
3648+
*program_handles = (ebpf_handle_t*)ebpf_allocate_with_tag(program_handles_size, EBPF_POOL_TAG_DEFAULT);
36493649
if (*program_handles == nullptr) {
36503650
EBPF_LOG_MESSAGE(
36513651
EBPF_TRACELOG_LEVEL_ERROR,
@@ -5222,3 +5222,20 @@ ebpf_map_set_wait_handle(fd_t map_fd, uint64_t index, ebpf_handle_t handle) NO_E
52225222
EBPF_RETURN_RESULT(result);
52235223
}
52245224
CATCH_NO_MEMORY_EBPF_RESULT
5225+
5226+
_Must_inspect_result_ ebpf_result_t
5227+
ebpf_link_mark_as_legacy_mode(fd_t link_fd) NO_EXCEPT_TRY
5228+
{
5229+
EBPF_LOG_ENTRY();
5230+
ebpf_result_t result = EBPF_SUCCESS;
5231+
ebpf_handle_t link_handle = _get_handle_from_file_descriptor(link_fd);
5232+
if (link_handle == ebpf_handle_invalid) {
5233+
result = EBPF_INVALID_FD;
5234+
EBPF_RETURN_RESULT(result);
5235+
}
5236+
ebpf_operation_link_set_legacy_mode_request_t request{
5237+
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_LINK_SET_LEGACY_MODE, link_handle};
5238+
result = win32_error_code_to_ebpf_result(invoke_ioctl(request));
5239+
EBPF_RETURN_RESULT(result);
5240+
}
5241+
CATCH_NO_MEMORY_EBPF_RESULT

libs/api/libbpf_link.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@
77
#include "libbpf.h"
88
#include "libbpf_internal.h"
99

10+
struct bpf_link*
11+
bpf_link__open(const char* path)
12+
{
13+
struct bpf_link* link = nullptr;
14+
fd_t link_fd;
15+
ebpf_result_t result;
16+
17+
result = ebpf_object_get(path, &link_fd);
18+
if (result != EBPF_SUCCESS) {
19+
libbpf_result_err(result); // Set errno.
20+
return nullptr;
21+
}
22+
23+
link = (struct bpf_link*)ebpf_allocate_with_tag(sizeof(struct bpf_link), EBPF_POOL_TAG_DEFAULT);
24+
if (!link) {
25+
(void)ebpf_close_fd(link_fd);
26+
libbpf_err(-ENOMEM);
27+
return nullptr;
28+
}
29+
30+
link->fd = link_fd;
31+
link->pin_path = cxplat_duplicate_string(path);
32+
if (!link->pin_path) {
33+
(void)ebpf_close_fd(link_fd);
34+
ebpf_free(link);
35+
libbpf_err(-ENOMEM);
36+
return nullptr;
37+
}
38+
link->disconnected = false;
39+
40+
return link;
41+
}
42+
1043
int
1144
bpf_link__pin(struct bpf_link* link, const char* path)
1245
{

0 commit comments

Comments
 (0)