Skip to content

Commit

Permalink
Bpf export (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
shankarseal authored Jan 23, 2024
1 parent 6deaa9d commit f026933
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 49 deletions.
57 changes: 57 additions & 0 deletions src/bpfexport/bpfexport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (c) Microsoft Corporation.
//

#include <stdio.h>
#include <windows.h>
//
// Work around various eBPF compilation bugs.
//
#define USER_MODE 1
#pragma warning(push)
#pragma warning(disable:4005) // 'WIN32_LEAN_AND_MEAN': macro redefinition
#include <ebpf_store_helper.h>
#include <ebpfstore.h>
#pragma warning(pop)
#undef USER_MODE

INT
__cdecl
main(
INT argc,
CHAR *argv[]
)
{
ebpf_result_t result;
int exit_code = 0;

if (argc == 2 && !_strcmpi("--clear", argv[1])) {
for (uint32_t i = 0; i < RTL_NUMBER_OF(EbpfXdpSectionInfo); i++) {
result = ebpf_store_delete_section_information(&EbpfXdpSectionInfo[i]);
if (result != EBPF_SUCCESS) {
fprintf(stderr, "ebpf_store_delete_section_information failed: %u\n", result);
exit_code = -1;
}
}

result = ebpf_store_delete_program_information(&EbpfXdpProgramInfo);
if (result != EBPF_SUCCESS) {
fprintf(stderr, "ebpf_store_delete_program_information failed: %u\n", result);
exit_code = -1;
}
} else {
result = ebpf_store_update_section_information(&EbpfXdpSectionInfo[0], RTL_NUMBER_OF(EbpfXdpSectionInfo));
if (result != EBPF_SUCCESS) {
fprintf(stderr, "ebpf_store_update_section_information failed: %u\n", result);
exit_code = -1;
}

result = ebpf_store_update_program_information(&EbpfXdpProgramInfo, 1);
if (result != EBPF_SUCCESS) {
fprintf(stderr, "ebpf_store_update_program_information failed: %u\n", result);
exit_code = -1;
}
}

return exit_code;
}
56 changes: 56 additions & 0 deletions src/bpfexport/bpfexport.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (C) Microsoft. All rights reserved.
//

//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//

#include <windows.h>
#include <xdpversion.h>

#define VER_FILETYPE VFT_APP
#define VER_FILESUBTYPE VFT2_UNKNOWN

#define VER_FILEVERSION XDP_MAJOR_VERSION, XDP_MINOR_VERSION, XDP_PATCH_VERSION, 0
#define VER_FILEVERSION_STR STR(XDP_MAJOR_VERSION) "." STR(XDP_MINOR_VERSION) "." STR(XDP_PATCH_VERSION) ".0\0"
#define VER_PRODUCTVERSION_STR XDP_VERSION_STR

#define VER_FILEDESCRIPTION_STR "Microsoft\256 XDP eBPF Export Tool"
#define VER_ORIGINALFILENAME_STR "bpfexport.exe"

#define VER_COMPANYNAME_STR "Microsoft Corporation"
#define VER_LEGALCOPYRIGHT_STR "\251 Microsoft Corporation. All rights reserved."
#define VER_PRODUCTNAME_STR "Microsoft\256 XDP"
#define VER_INTERNALNAME_STR "bpfexport"

VS_VERSION_INFO VERSIONINFO
FILEVERSION VER_FILEVERSION
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS 0
FILEOS VOS_NT_WINDOWS32
FILETYPE VER_FILETYPE
FILESUBTYPE VER_FILESUBTYPE

BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904B0"
BEGIN
VALUE "CompanyName", VER_COMPANYNAME_STR
VALUE "FileDescription", VER_FILEDESCRIPTION_STR
VALUE "FileVersion", VER_FILEVERSION_STR
VALUE "InternalName", VER_INTERNALNAME_STR
VALUE "LegalCopyright", VER_LEGALCOPYRIGHT_STR
VALUE "OriginalFilename", VER_ORIGINALFILENAME_STR
VALUE "ProductName", VER_PRODUCTNAME_STR
VALUE "ProductVersion", VER_PRODUCTVERSION_STR
END
END

BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x0409, 0x04B0
END
END
68 changes: 68 additions & 0 deletions src/bpfexport/bpfexport.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)src\xdp.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<Import Project="$(SolutionDir)src\xdp.cpp.default.props" />
<Import Project="$(EbpfPackagePath)build\native\ebpf-for-windows.props" Condition="Exists('$(EbpfPackagePath)build\native\ebpf-for-windows.props')" />
<ItemGroup>
<ClCompile Include="bpfexport.c" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{8f8830ff-1648-4772-87ed-f5da091fc931}</ProjectGuid>
<RootNamespace>bpfexport</RootNamespace>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<TargetVersion>Windows10</TargetVersion>
<PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset>
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(SolutionDir)src\xdp.cpp.props" />
<Import Project="$(SolutionDir)src\xdp.cpp.user.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<TargetName>xdp_bpfexport</TargetName>
<OutDir>$(SolutionDir)artifacts\bin\$(Platform)_$(Configuration)\</OutDir>
</PropertyGroup>
<ItemGroup>
<ResourceCompile Include="bpfexport.rc">
<AdditionalIncludeDirectories>
$(SolutionDir)published\private;
%(AdditionalIncludeDirectories)
</AdditionalIncludeDirectories>
<PreprocessorDefinitions>
XDP_MAJOR_VERSION=$(XdpMajorVersion);
XDP_MINOR_VERSION=$(XdpMinorVersion);
XDP_PATCH_VERSION=$(XdpPatchVersion);
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(BUILD_BUILDID)' != ''">
XDP_BUILD_VERSION=$(BUILD_BUILDID);
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
</ResourceCompile>
</ItemGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>
$(SolutionDir)src\xdp;
%(AdditionalIncludeDirectories)
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>
onecore.lib;
%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
58 changes: 58 additions & 0 deletions src/xdp/ebpfstore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright (c) Microsoft Corporation.
//

#pragma once

#include <ebpf_extension.h>
#include <ebpf_extension_uuids.h>
#include <ebpf_nethooks.h>
#include <ebpf_program_attach_type_guids.h>
#include <ebpf_program_types.h>
#include <ebpf_structs.h>
#include <ebpf_private_extension.h>

static const ebpf_context_descriptor_t EbpfXdpContextDescriptor = {
.size = sizeof(xdp_md_t),
.data = FIELD_OFFSET(xdp_md_t, data),
.end = FIELD_OFFSET(xdp_md_t, data_end),
.meta = FIELD_OFFSET(xdp_md_t, data_meta),
};

#define XDP_EXT_HELPER_FUNCTION_START EBPF_MAX_GENERAL_HELPER_FUNCTION

// XDP helper function prototype descriptors.
static const ebpf_helper_function_prototype_t EbpfXdpHelperFunctionPrototype[] = {
{
.helper_id = XDP_EXT_HELPER_FUNCTION_START + 1,
.name = "bpf_xdp_adjust_head",
.return_type = EBPF_RETURN_TYPE_INTEGER,
.arguments = {
EBPF_ARGUMENT_TYPE_PTR_TO_CTX,
EBPF_ARGUMENT_TYPE_ANYTHING,
},
},
};

#pragma warning(suppress:4090) // 'initializing': different 'const' qualifiers
const ebpf_program_info_t EbpfXdpProgramInfo = {
#pragma warning(suppress:4090) // 'initializing': different 'const' qualifiers
.program_type_descriptor = {
.name = "xdp",
.context_descriptor = &EbpfXdpContextDescriptor,
.program_type = EBPF_PROGRAM_TYPE_XDP_INIT,
BPF_PROG_TYPE_XDP,
},
.count_of_program_type_specific_helpers = RTL_NUMBER_OF(EbpfXdpHelperFunctionPrototype),
.program_type_specific_helper_prototype = EbpfXdpHelperFunctionPrototype,
};

#define DECLARE_XDP_SECTION(_section_name) \
(const wchar_t*)_section_name, &EBPF_PROGRAM_TYPE_XDP, &EBPF_ATTACH_TYPE_XDP, \
BPF_PROG_TYPE_XDP, BPF_XDP

const ebpf_program_section_info_t DECLSPEC_SELECTANY EbpfXdpSectionInfo[] = {
{
DECLARE_XDP_SECTION(L"xdp")
}
};
36 changes: 1 addition & 35 deletions src/xdp/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//

#include "precomp.h"
#include "ebpfstore.h"
#include "programinspect.h"
#include "program.tmh"

Expand Down Expand Up @@ -621,41 +622,6 @@ XdpProgramTraceObject(
XdpProgramTrace(&ProgramObject->Program);
}

static const ebpf_context_descriptor_t EbpfXdpContextDescriptor = {
.size = sizeof(xdp_md_t),
.data = FIELD_OFFSET(xdp_md_t, data),
.end = FIELD_OFFSET(xdp_md_t, data_end),
.meta = FIELD_OFFSET(xdp_md_t, data_meta),
};

#define XDP_EXT_HELPER_FUNCTION_START EBPF_MAX_GENERAL_HELPER_FUNCTION

// XDP helper function prototype descriptors.
static const ebpf_helper_function_prototype_t EbpfXdpHelperFunctionPrototype[] = {
{
.helper_id = XDP_EXT_HELPER_FUNCTION_START + 1,
.name = "bpf_xdp_adjust_head",
.return_type = EBPF_RETURN_TYPE_INTEGER,
.arguments = {
EBPF_ARGUMENT_TYPE_PTR_TO_CTX,
EBPF_ARGUMENT_TYPE_ANYTHING,
},
},
};

#pragma warning(suppress:4090) // 'initializing': different 'const' qualifiers
static const ebpf_program_info_t EbpfXdpProgramInfo = {
#pragma warning(suppress:4090) // 'initializing': different 'const' qualifiers
.program_type_descriptor = {
.name = "xdp",
.context_descriptor = &EbpfXdpContextDescriptor,
.program_type = EBPF_PROGRAM_TYPE_XDP_INIT,
BPF_PROG_TYPE_XDP,
},
.count_of_program_type_specific_helpers = RTL_NUMBER_OF(EbpfXdpHelperFunctionPrototype),
.program_type_specific_helper_prototype = EbpfXdpHelperFunctionPrototype,
};

static
int
EbpfXdpAdjustHead(
Expand Down
4 changes: 3 additions & 1 deletion src/xdpinstaller/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ SPDX-License-Identifier: MIT

<!-- Define the product components -->
<Fragment>
<!-- XDP user-mode components -->
<ComponentGroup Id="xdp" Directory="INSTALLFOLDER">
<Component Id ="xdpPATH" Guid="{0445C769-0841-4FD9-939F-C2E7F514E61A}" KeyPath="yes">
<Environment Id="xdpPATH" Name="PATH" Value="[INSTALLFOLDER]" Permanent="no" Part="last" Action="set" System="yes" Separator=";" />
Expand All @@ -73,6 +72,9 @@ SPDX-License-Identifier: MIT
<Component Id="xdpcfg.exe" Guid="{2F099CCD-54DA-495C-9741-3F3EFE357087}">
<File Id="xdpcfg.exe" Name="xdpcfg.exe" Source="$(var.TargetDir)..\xdpcfg.exe" KeyPath="yes" />
</Component>
<Component Id="xdp_bpfexport.exe" Guid="{E190E2ED-566C-424A-83F3-0422EB865FCA}">
<File Id="xdp_bpfexport.exe" Name="xdp_bpfexport.exe" Source="$(var.TargetDir)..\xdp_bpfexport.exe" KeyPath="yes" />
</Component>
<Component Id="xdp.sys" Guid="{5E1B9729-E58D-4A4D-A845-FB6D2EE0C498}">
<File Id="xdp.sys" Name="xdp.sys" Source="$(var.TargetDir)..\xdp\xdp.sys" KeyPath="yes" />
</Component>
Expand Down
20 changes: 20 additions & 0 deletions test/bpf/bpf.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)src\xdp.props" />
<Import Project="$(EbpfPackagePath)build\native\ebpf-for-windows.props" Condition="Exists('$(EbpfPackagePath)build\native\ebpf-for-windows.props')" />
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src\bpfexport\bpfexport.vcxproj">
<Project>{8f8830ff-1648-4772-87ed-f5da091fc931}</Project>
<Private>false</Private>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
Expand All @@ -25,6 +31,20 @@
<TargetName>bpf</TargetName>
<OutDir>$(SolutionDir)artifacts\bin\$(Platform)_$(Configuration)\bpf\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup>
<PreBuildEvent>
<Command>
set PATH=%PATH%;$(EbpfPackagePath)build\native\bin
$(SolutionDir)artifacts\bin\$(Platform)_$(Configuration)\xdp_bpfexport.exe
</Command>
</PreBuildEvent>
<PostBuildEvent>
<Command>
set PATH=%PATH%;$(EbpfPackagePath)build\native\bin
$(SolutionDir)artifacts\bin\$(Platform)_$(Configuration)\xdp_bpfexport.exe --clear
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<CustomBuild Include="allow_ipv6.c">
<FileType>CppCode</FileType>
Expand Down
2 changes: 2 additions & 0 deletions tools/create-devkit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ copy "artifacts\bin\$($Platform)_$($Config)\rxfilter.exe" $DstPath\bin
copy "artifacts\bin\$($Platform)_$($Config)\xdpcfg.exe" $DstPath\bin
copy "artifacts\bin\$($Platform)_$($Config)\xskbench.exe" $DstPath\bin
copy "artifacts\bin\$($Platform)_$($Config)\xskfwd.exe" $DstPath\bin
copy "artifacts\bin\$($Platform)_$($Config)\xdp_bpfexport.exe" $DstPath\bin

New-Item -Path $DstPath\symbols -ItemType Directory > $null
copy "artifacts\bin\$($Platform)_$($Config)\xdp.pdb" $DstPath\symbols
Expand All @@ -45,6 +46,7 @@ copy "artifacts\bin\$($Platform)_$($Config)\rxfilter.pdb" $DstPath\symbols
copy "artifacts\bin\$($Platform)_$($Config)\xdpcfg.pdb" $DstPath\symbols
copy "artifacts\bin\$($Platform)_$($Config)\xskbench.pdb" $DstPath\symbols
copy "artifacts\bin\$($Platform)_$($Config)\xskfwd.pdb" $DstPath\symbols
copy "artifacts\bin\$($Platform)_$($Config)\xdp_bpfexport.pdb" $DstPath\symbols

New-Item -Path $DstPath\include -ItemType Directory > $null
copy -Recurse published\external\* $DstPath\include
Expand Down
9 changes: 0 additions & 9 deletions tools/prepare-machine.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,6 @@ if ($Cleanup) {
if (!(cmd /c "clang --version 2>&1" | Select-String "clang version 11.")) {
Write-Error "Compiling eBPF programs on Windows requires clang version 11"
}

$EbpfExportProgram = "$EbpfNugetRestoreDir/build/native/bin/export_program_info.exe"

if (!(Test-Path $EbpfExportProgram)) {
Write-Error "Missing eBPF helper export_program_info.exe. Is the NuGet package installed?"
}

Write-Verbose $EbpfExportProgram
& $EbpfExportProgram | Write-Verbose
}

if ($ForFunctionalTest) {
Expand Down
2 changes: 0 additions & 2 deletions tools/setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,6 @@ function Install-Ebpf {
if (!$? -or !(Test-Path $EbpfPath)) {
Write-Error "eBPF could not be installed"
}
# Stop eBPF's XDP hook since it conflicts with our XDP implementation.
Stop-Service netebpfext
Refresh-Path
}

Expand Down
Loading

0 comments on commit f026933

Please sign in to comment.