-
Notifications
You must be signed in to change notification settings - Fork 746
bpf2go: enable ebpf code reuse across go packages #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,24 +32,12 @@ func TestRun(t *testing.T) { | |
} | ||
|
||
modDir := t.TempDir() | ||
execInModule := func(name string, args ...string) { | ||
t.Helper() | ||
|
||
cmd := exec.Command(name, args...) | ||
cmd.Dir = modDir | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
if out := string(out); out != "" { | ||
t.Log(out) | ||
} | ||
t.Fatalf("Can't execute %s: %v", name, args) | ||
} | ||
} | ||
|
||
module := internal.CurrentModule | ||
|
||
execInModule("go", "mod", "init", "bpf2go-test") | ||
execInDir(t, modDir, "go", "mod", "init", "bpf2go-test") | ||
|
||
execInModule("go", "mod", "edit", | ||
execInDir(t, modDir, "go", "mod", "edit", | ||
// Require the module. The version doesn't matter due to the replace | ||
// below. | ||
fmt.Sprintf("-require=%[email protected]", module), | ||
|
@@ -106,6 +94,70 @@ func main() { | |
} | ||
} | ||
|
||
func execInDir(t *testing.T, dir, name string, args ...string) { | ||
t.Helper() | ||
|
||
cmd := exec.Command(name, args...) | ||
cmd.Dir = dir | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
if out := string(out); out != "" { | ||
t.Log(out) | ||
} | ||
t.Fatalf("Can't execute %s: %v", name, args) | ||
} | ||
} | ||
|
||
func TestImports(t *testing.T) { | ||
dir := t.TempDir() | ||
mustWriteFile(t, dir, "foo/foo.go", "package foo") | ||
mustWriteFile(t, dir, "foo/foo.h", "#define EXAMPLE_ORG__FOO__FOO_H 1") | ||
mustWriteFile(t, dir, "bar/nested/nested.go", "package nested") | ||
mustWriteFile(t, dir, "bar/nested/nested.h", "#define EXAMPLE_ORG__BAR__NESTED__NESTED_H 1") | ||
mustWriteFile(t, dir, "bar/bpf2go.hfiles.go", ` | ||
package bar | ||
import ( | ||
_ "example.org/bar/nested" | ||
_ "example.org/foo" | ||
)`) | ||
mustWriteFile(t, dir, "bar/bar.c", ` | ||
//go:build ignore | ||
|
||
// include from current module, package listed in bpf2go.hfiles.go | ||
#include "example.org/bar/nested/nested.h" | ||
#ifndef EXAMPLE_ORG__BAR__NESTED__NESTED_H | ||
#error "example.org/bar/nested/nested.h: unexpected file contents" | ||
#endif | ||
|
||
// include from external module, package listed in bpf2go.hfiles.go | ||
#include "example.org/foo/foo.h" | ||
#ifndef EXAMPLE_ORG__FOO__FOO_H | ||
#error "example.org/foo/foo.h: unexpected file contents" | ||
#endif`) | ||
|
||
fooModDir := filepath.Join(dir, "foo") | ||
execInDir(t, fooModDir, "go", "mod", "init", "example.org/foo") | ||
|
||
barModDir := filepath.Join(dir, "bar") | ||
execInDir(t, barModDir, "go", "mod", "init", "example.org/bar") | ||
execInDir(t, barModDir, "go", "mod", "edit", "-require=example.org/[email protected]") | ||
|
||
execInDir(t, dir, "go", "work", "init") | ||
execInDir(t, dir, "go", "work", "use", fooModDir) | ||
execInDir(t, dir, "go", "work", "use", barModDir) | ||
|
||
err := run(io.Discard, []string{ | ||
"-go-package", "bar", | ||
"-output-dir", barModDir, | ||
"-cc", testutils.ClangBin(t), | ||
"bar", | ||
filepath.Join(barModDir, "bar.c"), | ||
}) | ||
|
||
if err != nil { | ||
t.Fatal("Can't run:", err) | ||
} | ||
} | ||
|
||
func TestHelp(t *testing.T) { | ||
var stdout bytes.Buffer | ||
err := run(&stdout, []string{"-help"}) | ||
|
@@ -383,6 +435,9 @@ func TestParseArgs(t *testing.T) { | |
func mustWriteFile(tb testing.TB, dir, name, contents string) { | ||
tb.Helper() | ||
tmpFile := filepath.Join(dir, name) | ||
if err := os.MkdirAll(filepath.Dir(tmpFile), 0770); err != nil { | ||
tb.Fatal(err) | ||
} | ||
if err := os.WriteFile(tmpFile, []byte(contents), 0660); err != nil { | ||
tb.Fatal(err) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"go/build" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
// vfs is LLVM virtual file system parsed from a file | ||
// | ||
// In a nutshell, it is a tree of "directory" nodes with leafs being | ||
// either "file" (a reference to file) or "directory-remap" (a reference | ||
// to directory). | ||
// | ||
// https://github.com/llvm/llvm-project/blob/llvmorg-18.1.0/llvm/include/llvm/Support/VirtualFileSystem.h#L637 | ||
type vfs struct { | ||
Version int `json:"version"` | ||
CaseSensitive bool `json:"case-sensitive"` | ||
Roots []vfsItem `json:"roots"` | ||
} | ||
|
||
type vfsItem struct { | ||
Name string `json:"name"` | ||
Type vfsItemType `json:"type"` | ||
Contents []vfsItem `json:"contents,omitempty"` | ||
ExternalContents string `json:"external-contents,omitempty"` | ||
} | ||
|
||
type vfsItemType string | ||
|
||
const ( | ||
vfsFile vfsItemType = "file" | ||
vfsDirectory vfsItemType = "directory" | ||
) | ||
|
||
func (vi *vfsItem) addDir(path string) (*vfsItem, error) { | ||
for _, name := range strings.Split(path, "/") { | ||
idx := vi.index(name) | ||
if idx == -1 { | ||
idx = len(vi.Contents) | ||
vi.Contents = append(vi.Contents, vfsItem{Name: name, Type: vfsDirectory}) | ||
} | ||
vi = &vi.Contents[idx] | ||
if vi.Type != vfsDirectory { | ||
return nil, fmt.Errorf("adding %q: non-directory object already exists", path) | ||
} | ||
} | ||
return vi, nil | ||
} | ||
|
||
func (vi *vfsItem) index(name string) int { | ||
return slices.IndexFunc(vi.Contents, func(item vfsItem) bool { | ||
return item.Name == name | ||
}) | ||
} | ||
|
||
func persistVfs(vfs *vfs) (_ string, retErr error) { | ||
temp, err := os.CreateTemp("", "") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer func() { | ||
temp.Close() | ||
if retErr != nil { | ||
os.Remove(temp.Name()) | ||
} | ||
}() | ||
|
||
if err = json.NewEncoder(temp).Encode(vfs); err != nil { | ||
return "", err | ||
} | ||
|
||
return temp.Name(), nil | ||
} | ||
|
||
// vfsRootDir is the (virtual) directory where we mount go module sources | ||
// for the C includes to pick them, e.g. "<vfsRootDir>/github.com/cilium/ebpf". | ||
const vfsRootDir = "/.vfsoverlay.d" | ||
|
||
// createVfs produces a vfs from a list of packages. It creates a | ||
// (virtual) directory tree reflecting package import paths and adds | ||
// links to header files. E.g. for github.com/foo/bar containing awesome.h: | ||
// | ||
// github.com/ | ||
// foo/ | ||
// bar/ | ||
// awesome.h -> $HOME/go/pkg/mod/github.com/foo/bar@version/awesome.h | ||
func createVfs(pkgs []*build.Package) (*vfs, error) { | ||
roots := [1]vfsItem{{Name: vfsRootDir, Type: vfsDirectory}} | ||
for _, pkg := range pkgs { | ||
var headers []vfsItem | ||
for _, h := range hfiles(pkg) { | ||
headers = append(headers, vfsItem{Name: h, Type: vfsFile, | ||
ExternalContents: filepath.Join(pkg.Dir, h)}) | ||
} | ||
dir, err := roots[0].addDir(pkg.ImportPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dir.Contents = headers // NB don't append inplace, same package could be imported twice | ||
} | ||
return &vfs{CaseSensitive: true, Roots: roots[:]}, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.