Skip to content

Conversation

@Alan-Jowett
Copy link
Member

@Alan-Jowett Alan-Jowett commented Oct 28, 2025

Moving this PR to draft until the corresponding issue is in the current milestone

Resolves: #4764

Description

This pull request introduces a new eBPF helper function for SOCK_OPS programs that allows retrieving the WFP flow ID associated with a socket operation context. The changes span the core extension, program info, test infrastructure, and add a new sample and test case to validate the helper. The most significant updates are grouped below.

New SOCK_OPS Helper Function: Flow ID

  • Added the bpf_sock_ops_get_flow_id helper function to the eBPF extension, allowing SOCK_OPS programs to access the WFP flow ID for the current connection via the context (bpf_sock_ops_t). This includes updates to the context struct, helper registration, and implementation. [1] [2] [3] [4] [5] [6]

  • Registered the new helper in the program type-specific helper function tables and prototypes for both the extension and test environments, ensuring it is available to eBPF programs and properly mocked for tests. [1] [2] [3] [4]

Sample Program and Test Coverage

  • Added a new sample eBPF program (sockops_flow_id.c) that uses the flow ID helper to audit and store flow IDs in maps for both IPv4 and IPv6 connections, demonstrating usage and providing a basis for validation.

  • Added a comprehensive test case (sock_ops_flow_id_helper_test) in socket_tests.cpp that attaches the sample program, triggers TCP connections, and verifies that the flow ID helper returns valid (non-zero) values via both ring buffer events and map lookups.

Test Infrastructure and Minor Refactoring

  • Updated test helper context creation functions for better readability and consistency in allocation calls. [1] [2] [3]

These changes collectively add robust support for querying and validating WFP flow IDs in SOCK_OPS programs, improving observability and enabling new use cases for eBPF on Windows.

Testing

CI/CD

Documentation

Doxygen comments.

Installation

No.

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
return 0;
}

static uint64_t

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed in addition to the one in netebpfext/net_ebpf_ext_sock_ops.c ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the mock layer that allows BPF programs to be run in unit_tests.exe, entirely in user mode with out netebpfext.sys.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

// Ring buffer event callback context.
std::unique_ptr<ring_buffer_test_event_context_t> context = std::make_unique<ring_buffer_test_event_context_t>();
context->test_event_count = 2; // Expect 2 events for TCP connection (outbound and inbound)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context->test_event_count = 2; // Expect 2 events for TCP connection (outbound and inbound)
context->test_event_count = 2; // Expect 2 events for TCP connection (outbound and inbound).

local_flow_context->parameters.callout_id = net_ebpf_extension_get_callout_id_for_hook(hook_id);

// Store the flow_id in the sock_ops context for the helper function.
local_flow_context->context.flow_id = incoming_metadata_values->flowHandle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we be particularly paranoid and follow WFP runtime checks?

local_flow_context->context.flow_id = 0;
if (FWPS_IS_METADATA_FIELD_PRESENT(incoming_metadata_values, FWPS_METADATA_FIELD_FLOW_HANDLE))
{
local_flow_context->context.flow_id = incoming_metadata_values->flowHandle;
}

@Alan-Jowett Alan-Jowett marked this pull request as draft October 29, 2025 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unique Flow ID Helper for SOCK_OPS Programs

4 participants