| 1 |
# Working on the Windows port |
| 2 |
|
| 3 |
The library has basic support for interacting with eBPF for Windows (efW). |
| 4 |
Things are subject to change because eBPF for Windows has not had a stable (signed) release yet. |
| 5 |
|
| 6 |
## Differences between Linux and eBPF for Windows |
| 7 |
|
| 8 |
* eBPF for Windows has three distinct modes of operation: an interpreter, a JIT |
| 9 |
and a way to compile eBPF to a native Windows driver. The native driver can |
| 10 |
be signed using the usual mechanisms. It is likely that a stable release of |
| 11 |
eBPF for Windows will only support native drivers. |
| 12 |
The library supports both mechanisms, and relies on the JIT for its testsuite. |
| 13 |
This is because the native Windows driver mechanism still comes with significant |
| 14 |
downsides. |
| 15 |
* eBPF for Windows has a large user-space component which ebpf-go calls into |
| 16 |
via dynamic runtime linking. This uses the same infrastructure as CGo but |
| 17 |
does not require a C toolchain and is therefore trivial to distribute. |
| 18 |
|
| 19 |
## Exported API |
| 20 |
|
| 21 |
The library only supports a subset of the full API on Windows, because the eBPF for |
| 22 |
Windows runtime doesn't yet or never will support certain features. API which |
| 23 |
are not supported will return `ErrNotSupported`. Some interfaces such as Linux-specific |
| 24 |
link types are removed outright, but this is kept to a minimum since it is very |
| 25 |
cumbersome for users to deal with API that change based on platform. |
| 26 |
|
| 27 |
## Development setup |
| 28 |
|
| 29 |
The port is developed using a Windows VM running on a Linux host. |
| 30 |
There is a [script](https://github.com/cilium/ebpf/tree/main/scripts/windows) |
| 31 |
which automates the Windows installation. |
| 32 |
After the installation finishes you should be able to SSH to |
| 33 |
the VM and [follow the instructions to clone and build eBPF for Windows][efw-clone]. |
| 34 |
__Execute `Import-VsEnv` (installed by the setup script) to add `msbuild` to PATH.__ |
| 35 |
|
| 36 |
``` |
| 37 |
PS C:\Users\lmbauer> Import-VsEnv |
| 38 |
********************************************************************** |
| 39 |
** Visual Studio 2022 Developer PowerShell v17.10.4 |
| 40 |
** Copyright (c) 2022 Microsoft Corporation |
| 41 |
********************************************************************** |
| 42 |
PS C:\Users\lmbauer> msbuild |
| 43 |
MSBuild version 17.10.4+10fbfbf2e for .NET Framework |
| 44 |
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file. |
| 45 |
``` |
| 46 |
|
| 47 |
### Compiling the runtime |
| 48 |
|
| 49 |
!!! note "Pre-built eBPF for Windows binaries" |
| 50 |
You may be able to download precompiled binaries from the [efW CI/CD] pipeline. |
| 51 |
Look for an artifact called "Build-x64-Debug", which should contain |
| 52 |
`setup-ebpf.ps1` mentioned below. |
| 53 |
|
| 54 |
The upstream instructions currently explain how to compile the full project, which takes quite a long time. |
| 55 |
It is possible to build only some parts from the command line: |
| 56 |
|
| 57 |
* Installer: `msbuild /m /p:Configuration=Debug /p:Platform=x64 ebpf-for-windows.sln -t:"installer\ebpf-for-windows"` |
| 58 |
* Unit tests: `msbuild /m /p:Configuration=Debug /p:Platform=x64 ebpf-for-windows.sln -t:"tests\unit_tests"` |
| 59 |
* Clean: `msbuild /m /p:Configuration=Debug /p:Platform=x64 ebpf-for-windows.sln -t:"Clean"` |
| 60 |
|
| 61 |
After compilation of the installer finishes you can install the runtime: |
| 62 |
|
| 63 |
``` |
| 64 |
.\x64\Debug\setup-ebpf.ps1 |
| 65 |
``` |
| 66 |
|
| 67 |
_(You can pass `-Uninstall` to the script to remove a previous installation.)_ |
| 68 |
|
| 69 |
You can now run the Go unit tests of the library: |
| 70 |
|
| 71 |
``` |
| 72 |
go test ./internal/sys |
| 73 |
``` |
| 74 |
|
| 75 |
!!! note "Tests fail with `load ebpfapi.dll: not found`" |
| 76 |
This usually means that either the Windows runtime is not installed or that |
| 77 |
the efW installation folder is not on the PATH yet. The latter tends to |
| 78 |
happen when executing tests via ssh, since sshd doesn't pick up |
| 79 |
changes in the environment without restarting. |
| 80 |
Restart the service by issuing `Restart-Service sshd` from a powershell |
| 81 |
prompt and then re-establish the ssh session. |
| 82 |
|
| 83 |
### efW extensions |
| 84 |
|
| 85 |
efW separates the runtime from the implementation of the various hooks / program |
| 86 |
types. The hooks are shipped as extensions in a separate Windows kernel service. |
| 87 |
Installing an extension involves two steps: |
| 88 |
|
| 89 |
1. Installing the extension as a Windows kernel service. |
| 90 |
2. Registering the program type(s) in the "eBPF Store". |
| 91 |
|
| 92 |
For [ntosebpfext] the setup process looks as follows, assuming the extension has |
| 93 |
already been built: |
| 94 |
|
| 95 |
``` |
| 96 |
PS C:\Users\lorenz\ntosebpfext> .\tests\process_monitor.Tests\Setup-ProcessMonitorTests.ps1 -ArtifactsRoot .\x64\Debug\ |
| 97 |
Creating and starting the ntosebpfext service from C:\Users\lorenz\ntosebpfext\x64\Debug\\ntosebpfext.sys. |
| 98 |
PS C:\Users\lorenz\ntosebpfext> .\x64\Debug\ntos_ebpf_ext_export_program_info.exe |
| 99 |
Exporting program information. |
| 100 |
Exporting section information. |
| 101 |
``` |
| 102 |
|
| 103 |
## Debugging |
| 104 |
|
| 105 |
Debugging on Windows is a bit painful, since we call from Go into `ebpfapi.dll` |
| 106 |
which is implemented in C++. There is currently no debugger which understands |
| 107 |
both C++ and Go. |
| 108 |
|
| 109 |
The most fruitful approach is to use [WinDbg]. |
| 110 |
It will catch exceptions in C++ code, give useful backtraces and allows stepping |
| 111 |
through source code. |
| 112 |
|
| 113 |
Run the WinDbg GUI as an administrator and then open the executable via `Ctrl-E`. |
| 114 |
At the prompt you can set a breakpoint on `bpf()`: |
| 115 |
|
| 116 |
``` |
| 117 |
bu ebpfapi!bpf |
| 118 |
g |
| 119 |
``` |
| 120 |
|
| 121 |
This will halt execution once the library calls into `bpf()` inside `ebpfapi.dll`. |
| 122 |
Use the [`CDB` commands][cdb-commands] or the GUI to navigate. |
| 123 |
|
| 124 |
It may be possible to use [CDB] to debug via the command line, but this doesn't |
| 125 |
seem to work via ssh. |
| 126 |
|
| 127 |
### Windows trace log |
| 128 |
|
| 129 |
The `testmain` package has a small bit of instrumentation which enables tracing |
| 130 |
of the efW subsystem on demand. Simply pass the `-trace-log` flag when running |
| 131 |
tests: |
| 132 |
|
| 133 |
``` |
| 134 |
PS C:\Users\lorenz\ebpf> go test -run '^TestMap$' -v -trace-log |
| 135 |
=== RUN TestMap |
| 136 |
map_test.go:54: WindowsArray#3 |
| 137 |
--- PASS: TestMap (0.02s) |
| 138 |
PASS |
| 139 |
100% [>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] |
| 140 |
base ebpf_api_initiate returned success |
| 141 |
entry-exit ebpf_map_create |
| 142 |
entry-exit _create_map |
| 143 |
entry-exit _ebpf_core_protocol_create_map |
| 144 |
entry-exit ebpf_core_create_map |
| 145 |
entry-exit ebpf_map_create |
| 146 |
base eBPF object initialized object=0xFFFF8982A875BF30 object_type= 1 |
| 147 |
base ebpf_map_create returned success |
| 148 |
entry-exit ebpf_handle_create |
| 149 |
core ebpf_handle_create: returning handle value=376 |
| 150 |
base ebpf_handle_create returned success |
| 151 |
base ebpf_core_create_map returned success |
| 152 |
... |
| 153 |
``` |
| 154 |
|
| 155 |
Enabling the instrumentation can fail if the tests crashed too often. In that |
| 156 |
case you can manually stop and remove the tracing entries via the GUI: |
| 157 |
`compmgmt.msc` -> "Performance" -> "Data Collector Sets" -> "Event Trace Sessions". |
| 158 |
Look for sessions containing "ebpf-go". |
| 159 |
Rebooting might also help. |
| 160 |
|
| 161 |
### Interpreting error codes |
| 162 |
|
| 163 |
efW uses several layers of error codes. |
| 164 |
|
| 165 |
* Windows [system error codes] and [RPC errors] are sometimes exposed by |
| 166 |
exceptions, which appear in the trace log. |
| 167 |
* [`ebpf_result_t`][ebpf_result_t]: wraps Windows errors and |
| 168 |
is returned from "native" efW API. |
| 169 |
* Unix-style errno, as defined by Windows' [`errno.h`][errno.h]: |
| 170 |
wraps `ebpf_result_t` and is returned from libbpf and `bpf()` API. |
| 171 |
Unfortunately not all [errno values] line up with Linux. |
| 172 |
This usually manifests in cryptic `Errno(119)` errors. |
| 173 |
|
| 174 |
[efw-clone]: https://github.com/microsoft/ebpf-for-windows/blob/main/docs/GettingStarted.md#how-to-clone-and-build-the-project-using-visual-studio |
| 175 |
[CDB]: https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/debugging-using-cdb-and-ntsd |
| 176 |
[cdb-commands]: https://learn.microsoft.com/en-us/windows-hardware/drivers/debuggercmds/commands |
| 177 |
[WinDbg]: https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/ |
| 178 |
[ebpf_result_t]: https://github.com/microsoft/ebpf-for-windows/blob/main/include/ebpf_result.h |
| 179 |
[system error codes]: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- |
| 180 |
[RPC errors]: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--1700-3999- |
| 181 |
[errno.h]: https://learn.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-170 |
| 182 |
[errno values]: https://github.com/microsoft/ebpf-for-windows/issues/3729#issuecomment-2289025455 |
| 183 |
[ntosebpfext]: https://github.com/microsoft/ntosebpfext |
| 184 |
[access the debug version of the msvc runtime]: https://github.com/microsoft/ebpf-for-windows/issues/3872 |
| 185 |
[msvc debug DLLs]: https://github.com/microsoft/ebpf-for-windows/blob/7005b7ff47e7281843d6b414cd69fc5a979507c8/scripts/setup-ebpf.ps1#L17-L27 |
| 186 |
[efW CI/CD]: https://github.com/microsoft/ebpf-for-windows/actions/workflows/cicd.yml?query=branch%3Amain+is%3Acompleted |