Skip to content

Commit 37f0fd5

Browse files
committed
ebpf: support native images on Windows
Allow loading eBPF programs packaged as signed Windows drivers. Using native images significantly reduces which modifications can be made before load time. The .sys in testdata/windows were taken from the eBPF for Windows project, available under the MIT license. Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
1 parent b45f034 commit 37f0fd5

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

collection.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"errors"
66
"fmt"
7+
"path/filepath"
78
"reflect"
89
"strings"
910

@@ -815,6 +816,13 @@ func resolveKconfig(m *MapSpec) error {
815816
// Omitting Collection.Close() during application shutdown is an error.
816817
// See the package documentation for details around Map and Program lifecycle.
817818
func LoadCollection(file string) (*Collection, error) {
819+
if platform.IsWindows {
820+
// This mirrors a check in efW.
821+
if ext := filepath.Ext(file); ext == ".sys" {
822+
return loadCollectionFromNativeImage(file)
823+
}
824+
}
825+
818826
spec, err := LoadCollectionSpec(file)
819827
if err != nil {
820828
return nil, err

collection_other.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !windows
2+
3+
package ebpf
4+
5+
import "github.com/cilium/ebpf/internal"
6+
7+
func loadCollectionFromNativeImage(_ string) (*Collection, error) {
8+
return nil, internal.ErrNotSupportedOnOS
9+
}

collection_windows.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package ebpf
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"unsafe"
7+
8+
"github.com/cilium/ebpf/internal/efw"
9+
"github.com/cilium/ebpf/internal/sys"
10+
"github.com/cilium/ebpf/internal/unix"
11+
)
12+
13+
func loadCollectionFromNativeImage(file string) (_ *Collection, err error) {
14+
mapFds := make([]efw.FD, 16)
15+
programFds := make([]efw.FD, 16)
16+
var maps map[string]*Map
17+
var programs map[string]*Program
18+
19+
defer func() {
20+
if err == nil {
21+
return
22+
}
23+
24+
for _, fd := range append(mapFds, programFds...) {
25+
// efW never uses fd 0.
26+
if fd != 0 {
27+
_ = efw.EbpfCloseFd(int(fd))
28+
}
29+
}
30+
31+
for _, m := range maps {
32+
_ = m.Close()
33+
}
34+
35+
for _, p := range programs {
36+
_ = p.Close()
37+
}
38+
}()
39+
40+
nMaps, nPrograms, err := efw.EbpfObjectLoadNativeFds(file, mapFds, programFds)
41+
if errors.Is(err, efw.EBPF_NO_MEMORY) && (nMaps > len(mapFds) || nPrograms > len(programFds)) {
42+
mapFds = make([]efw.FD, nMaps)
43+
programFds = make([]efw.FD, nPrograms)
44+
45+
nMaps, nPrograms, err = efw.EbpfObjectLoadNativeFds(file, mapFds, programFds)
46+
}
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
mapFds = mapFds[:nMaps]
52+
programFds = programFds[:nPrograms]
53+
54+
// The maximum length of a name is only 16 bytes on Linux, longer names
55+
// are truncated. This is not a problem when loading from an ELF, since
56+
// we get the full object name from the symbol table.
57+
// When loading a native image we do not have this luxury. Use an efW native
58+
// API to retrieve up to 64 bytes of the object name.
59+
60+
maps = make(map[string]*Map, len(mapFds))
61+
for _, raw := range mapFds {
62+
fd, err := sys.NewFD(int(raw))
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
m, mapErr := newMapFromFD(fd)
68+
if mapErr != nil {
69+
_ = fd.Close()
70+
return nil, mapErr
71+
}
72+
73+
var efwMapInfo efw.BpfMapInfo
74+
size := uint32(unsafe.Sizeof(efwMapInfo))
75+
_, err = efw.EbpfObjectGetInfoByFd(m.FD(), unsafe.Pointer(&efwMapInfo), &size)
76+
if err != nil {
77+
_ = m.Close()
78+
return nil, err
79+
}
80+
81+
if size >= uint32(unsafe.Offsetof(efwMapInfo.Name)+unsafe.Sizeof(efwMapInfo.Name)) {
82+
m.name = unix.ByteSliceToString(efwMapInfo.Name[:])
83+
}
84+
85+
if m.name == "" {
86+
_ = m.Close()
87+
return nil, fmt.Errorf("unnamed map")
88+
}
89+
90+
if _, ok := maps[m.name]; ok {
91+
return nil, fmt.Errorf("duplicate map with the same name: %s", m.name)
92+
}
93+
94+
maps[m.name] = m
95+
}
96+
97+
programs = make(map[string]*Program, len(programFds))
98+
for _, raw := range programFds {
99+
fd, err := sys.NewFD(int(raw))
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
program, err := newProgramFromFD(fd)
105+
if err != nil {
106+
_ = fd.Close()
107+
return nil, err
108+
}
109+
110+
var efwProgInfo efw.BpfProgInfo
111+
size := uint32(unsafe.Sizeof(efwProgInfo))
112+
_, err = efw.EbpfObjectGetInfoByFd(program.FD(), unsafe.Pointer(&efwProgInfo), &size)
113+
if err != nil {
114+
_ = program.Close()
115+
return nil, err
116+
}
117+
118+
if size >= uint32(unsafe.Offsetof(efwProgInfo.Name)+unsafe.Sizeof(efwProgInfo.Name)) {
119+
program.name = unix.ByteSliceToString(efwProgInfo.Name[:])
120+
}
121+
122+
if program.name == "" {
123+
_ = program.Close()
124+
return nil, fmt.Errorf("unnamed program")
125+
}
126+
127+
if _, ok := programs[program.name]; ok {
128+
_ = program.Close()
129+
return nil, fmt.Errorf("duplicate program with the same name: %s", program.name)
130+
}
131+
132+
programs[program.name] = program
133+
}
134+
135+
return &Collection{programs, maps, nil}, nil
136+
}

collection_windows_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ebpf
2+
3+
import (
4+
"path/filepath"
5+
"sort"
6+
"testing"
7+
8+
"github.com/go-quicktest/qt"
9+
)
10+
11+
func TestLoadNativeImage(t *testing.T) {
12+
for _, tc := range []struct {
13+
file string
14+
maps []string
15+
programs []string
16+
}{
17+
{
18+
"testdata/windows/cgroup_sock_addr.sys",
19+
[]string{
20+
"egress_connection_policy_map",
21+
"ingress_connection_policy_map",
22+
"socket_cookie_map",
23+
},
24+
[]string{
25+
"authorize_connect4",
26+
"authorize_connect6",
27+
"authorize_recv_accept4",
28+
"authorize_recv_accept6",
29+
},
30+
},
31+
} {
32+
t.Run(filepath.Base(tc.file), func(t *testing.T) {
33+
coll, err := LoadCollection(tc.file)
34+
qt.Assert(t, qt.IsNil(err))
35+
defer coll.Close()
36+
37+
var mapNames []string
38+
for name, obj := range coll.Maps {
39+
qt.Assert(t, qt.Equals(obj.name, name))
40+
mapNames = append(mapNames, name)
41+
}
42+
sort.Strings(mapNames)
43+
qt.Assert(t, qt.DeepEquals(mapNames, tc.maps))
44+
45+
var programNames []string
46+
for name, obj := range coll.Programs {
47+
qt.Assert(t, qt.Equals(obj.name, name))
48+
programNames = append(programNames, name)
49+
}
50+
sort.Strings(programNames)
51+
qt.Assert(t, qt.DeepEquals(programNames, tc.programs))
52+
})
53+
}
54+
}

internal/efw/native.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:build windows
2+
3+
package efw
4+
5+
import (
6+
"syscall"
7+
"unsafe"
8+
9+
"golang.org/x/sys/windows"
10+
)
11+
12+
/*
13+
ebpf_result_t ebpf_object_load_native_by_fds(
14+
15+
_In_z_ const char* file_name,
16+
_Inout_ size_t* count_of_maps,
17+
_Out_writes_opt_(count_of_maps) fd_t* map_fds,
18+
_Inout_ size_t* count_of_programs,
19+
_Out_writes_opt_(count_of_programs) fd_t* program_fds)
20+
*/
21+
var ebpfObjectLoadNativeByFdsProc = newProc("ebpf_object_load_native_by_fds")
22+
23+
func EbpfObjectLoadNativeFds(fileName string, mapFds []FD, programFds []FD) (int, int, error) {
24+
addr, err := ebpfObjectLoadNativeByFdsProc.Find()
25+
if err != nil {
26+
return 0, 0, err
27+
}
28+
29+
fileBytes, err := windows.ByteSliceFromString(fileName)
30+
if err != nil {
31+
return 0, 0, err
32+
}
33+
34+
countOfMaps := Size(len(mapFds))
35+
countOfPrograms := Size(len(programFds))
36+
err = errorResult(syscall.SyscallN(addr,
37+
uintptr(unsafe.Pointer(&fileBytes[0])),
38+
uintptr(unsafe.Pointer(&countOfMaps)),
39+
uintptr(unsafe.Pointer(&mapFds[0])),
40+
uintptr(unsafe.Pointer(&countOfPrograms)),
41+
uintptr(unsafe.Pointer(&programFds[0])),
42+
))
43+
return int(countOfMaps), int(countOfPrograms), err
44+
}

testdata/windows/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) eBPF for Windows contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

testdata/windows/cgroup_sock_addr.sys

15 KB
Binary file not shown.

0 commit comments

Comments
 (0)