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

[RFC] Add elf support #6

Merged
merged 2 commits into from Dec 20, 2016
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+1,041 −29
Diff settings

Always

Just for now

Next

Add elf support

Split the project into two subpackages, bcc and elf:

* bcc holds the go bindings for the bcc framework.

* elf holds new code (from https://github.com/kinvolk/gobpf-elf-loader
  *1* in version 1e0ab5f0c647bdd311d90b117941c64f60a62a70) allowing
  users to load and use precompiled eBPF elf files and thus not require
  bcc plus installed kernel headers.

Next step is to move common code into the bpf package and define
interfaces where feasible.

*1* gobpf-elf-loader itself is based on and extending bpf code from
two projects:

https://github.com/safchain/goebpf by Sylvain Afchain and
https://github.com/cilium/cilium by the Cilium Project

Signed-off-by: Michael Schubert <michael@kinvolk.io>
  • Loading branch information...
schu committed Dec 14, 2016
commit 869e637f483f499254d57d443e7aaadad50dce24
Copy path View file
@@ -1,6 +1,8 @@
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/iovisor/gobpf)

This repository provides go bindings for the [bcc framework](https://github.com/iovisor/bcc).
This repository provides go bindings for the [bcc framework](https://github.com/iovisor/bcc)
as well as low-level routines to load and use eBPF programs from .elf
files.

gobpf is in early stage, but usable. Input and contributions are very much welcome.

@@ -15,3 +17,10 @@ Example code can be found in the `examples/` directory.

We recommend to vendor gobpf and pin its version as the API probably
undergoes change during development.

## Building ELF object files

To build ELF object files for usage with `github.com/iovisor/gobpf/elf`,
you must use distinct sections (`SEC("...")`). Currently, only
`kprobe/...` and `maps/...` are supported. For an example, see
`tests/dummy.c`.
Copy path View file
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package bpf
package bcc

import (
"bytes"
Copy path View file
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package bpf
package bcc

import (
"encoding/binary"
Copy path View file
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package bpf
package bcc

import (
"bytes"
Copy path View file
@@ -1,4 +1,4 @@
// Copyright 2016 PLUMgrid
// Copyright 2016 Kinvolk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -13,26 +13,3 @@
// limitations under the License.

package bpf

import (
"testing"
)

var simple1 string = `
BPF_TABLE("hash", int, int, table1, 10);
int func1(void *ctx) {
return 0;
}
`

func TestModuleLoad(t *testing.T) {
b := NewModule(simple1, []string{})
if b == nil {
t.Fatal("prog is nil")
}
defer b.Close()
_, err := b.LoadKprobe("func1")
if err != nil {
t.Fatal(err)
}
}
Copy path View file
@@ -0,0 +1,72 @@
// Copyright 2016 PLUMgrid
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bpf

import (
"testing"

"github.com/iovisor/gobpf/bcc"
"github.com/iovisor/gobpf/elf"
)

var simple1 string = `
BPF_TABLE("hash", int, int, table1, 10);
int func1(void *ctx) {
return 0;
}
`

func TestModuleLoadBCC(t *testing.T) {
b := bcc.NewModule(simple1, []string{})
if b == nil {
t.Fatal("prog is nil")
}
defer b.Close()
_, err := b.LoadKprobe("func1")
if err != nil {
t.Fatal(err)
}
}

func TestModuleLoadELF(t *testing.T) {
b := elf.NewModule("./tests/dummy.o")
if b == nil {
t.Fatal("prog is nil")
}
err := b.Load()
if err != nil {
t.Fatal(err)
}
var maps []*elf.Map
for m := range b.IterMaps() {
maps = append(maps, m)
}
if len(maps) != 1 {
t.Fatal("unexpcted number of maps")
}
if maps[0].Name != "dummy" {
t.Fatalf("map %q doesn't match expected name 'dummy'", maps[0].Name)
}
var probes []*elf.Kprobe
for p := range b.IterKprobes() {
probes = append(probes, p)
}
if len(probes) != 1 {
t.Fatalf("unexpcted number of probes: %d", len(probes))
}
if probes[0].Name != "kprobe/dummy" {
t.Fatalf("probe %q doesn't match expected name 'dummy'", probes[0].Name)
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.