goebpf

package module
v0.0.0-...-b1e37d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

README

goebpf - eBPF library in Go

This package provides a very simple library for handling eBPF programs. It provides a header file that have to be used to compile eBPF elf binary.

Dependencies

There is no dependency to use the Go library but there are dependencies to build eBPF elf binaries. In order to build an eBPF program this package provides the header file libbpf.h. LLVM/Clang with the support of bpf as target must be available.

  • clang >= version 3.4.0
  • llvm >= version 3.7.0

Install

You can use go get command to retrieve the package:

go get github.com/safchain/goebpf

Examples

In the examples folder there is a test file in Go and there is also a eBPF C file that will be loaded by the Go file once compiled. The Makefile in the examples folder will show to compile eBPF program.

In order to simply build the examples :

make examples

Documentation

Further informations can be found here :

https://github.com/torvalds/linux/tree/master/samples/bpf

License

This software is licensed under the Apache License, Version 2.0 (the "License"); you may not use this software 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.

Documentation

Overview

Package goebpf provides a simple library for handling eBPF programs. The eBPF programs have to be compiled with the header file coming with the package. This package provides a way to load the generated elf binaries and to do lookups on the eBPF maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTimeMonotonicNs

func GetTimeMonotonicNs() uint64

Types

type BPFMap

type BPFMap struct {
	Name string
	// contains filtered or unexported fields
}

BPFMap represents a eBPF map. An eBPF map has to be declared in the C file using the macro MAP provided by the libbpf.h header file.

func (*BPFMap) Delete

func (m *BPFMap) Delete(key interface{}) bool

Delete deletes the map entry for the given key.

func (*BPFMap) Iterator

func (m *BPFMap) Iterator() *BPFMapIterator

Iterator returns a BPFMapIterator

func (*BPFMap) KeySize

func (m *BPFMap) KeySize() uint32

KeySize returns the key size of a BPFMap/eBPF map.

func (*BPFMap) Lookup

func (m *BPFMap) Lookup(key interface{}, value interface{}) bool

Lookup does a lookup on the corresponding BPFMap. Key/values parameters need to be pointers and need to be be used according to the eBPF map definition declared in the eBPF C file. See the libbpf.h file coming with this package.

func (*BPFMap) Type

func (m *BPFMap) Type() BPFMapType

Type returns the type of a BPFMap wich is the type of the eBPF map.

func (*BPFMap) ValueSize

func (m *BPFMap) ValueSize() uint32

ValueSize returns the value size of a BPFMap/eBPF map.

type BPFMapIterator

type BPFMapIterator struct {
	// contains filtered or unexported fields
}

func (*BPFMapIterator) Next

func (i *BPFMapIterator) Next(key interface{}, value interface{}) bool

Next returns the next key, value of the BPFMap, returns true when the next element has been found, false otherwise.

type BPFMapType

type BPFMapType uint32

A BPFMapType represents a eBPF map type

const (
	BPF_MAP_TYPE_UNSPEC BPFMapType = C.BPF_MAP_TYPE_UNSPEC
	BPF_MAP_TYPE_HASH
	BPF_MAP_TYPE_ARRAY
	BPF_MAP_TYPE_PROG_ARRAY
	BPF_MAP_TYPE_PERF_EVENT_ARRAY
	BPF_MAP_TYPE_PERCPU_HASH
	BPF_MAP_TYPE_PERCPU_ARRAY
	BPF_MAP_TYPE_STACK_TRACE
)

Types of maps

type BPFProg

type BPFProg struct {
	// contains filtered or unexported fields
}

BPFMap represents a eBPF program.

func NewBPFProg

func NewBPFProg(r io.ReaderAt) (*BPFProg, error)

NewBPFProg returns a new BPFProg

func (*BPFProg) Attach

func (b *BPFProg) Attach(ifname string) (int, error)

Attach attaches the eBPF program to the given interface.

func (*BPFProg) Detach

func (b *BPFProg) Detach(fd int) error

Detach detaches the eBPF program to the given interface.

func (*BPFProg) Load

func (b *BPFProg) Load() error

Load loads the elf eBPF binary

func (*BPFProg) Log

func (b *BPFProg) Log() []byte

Log returns the log output coming from the eBPF program.

func (*BPFProg) Map

func (b *BPFProg) Map(name string) *BPFMap

Map returns the BPFMap for the given name. The name is the name used for the map declaration with the MAP macro is the eBPF C file.

func (*BPFProg) Maps

func (b *BPFProg) Maps() map[string]*BPFMap

Maps returns a map of BPFMap indexed by their name

func (*BPFProg) Release

func (b *BPFProg) Release()

Release releases the memory allocated by a BPFProg.

func (*BPFProg) SetDefaultMaxEntries

func (b *BPFProg) SetDefaultMaxEntries(max int)

SetDefaultMaxEntries sets the default max_entries for all the maps that will be loaded, if not defined the value

func (*BPFProg) SetMaxEntries

func (b *BPFProg) SetMaxEntries(table string, max int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL