Skip to content
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

bpf_iter and cgroup support #110

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ func (ec *elfCode) loadPrograms(progSections map[elf.SectionIndex]*elf.Section,
return nil, xerrors.Errorf("program %s: can't unmarshal instructions: %w", funcSym.Name, err)
}

progType, attachType := getProgType(sec.Name)
progType, attachType, attachTo := getProgType(sec.Name)

spec := &ProgramSpec{
Name: funcSym.Name,
Type: progType,
AttachType: attachType,
AttachTo: attachTo,
License: ec.license,
KernelVersion: ec.version,
Instructions: insns,
Expand Down Expand Up @@ -567,7 +568,7 @@ func (ec *elfCode) loadDataSections(maps map[string]*MapSpec, dataSections map[e
return nil
}

func getProgType(sectionName string) (ProgramType, AttachType) {
func getProgType(sectionName string) (ProgramType, AttachType, string) {
types := map[string]struct {
progType ProgramType
attachType AttachType
Expand All @@ -593,6 +594,7 @@ func getProgType(sectionName string) (ProgramType, AttachType) {
"sk_msg": {SkMsg, AttachSkSKBStreamVerdict},
"lirc_mode2": {LircMode2, AttachLircMode2},
"flow_dissector": {FlowDissector, AttachFlowDissector},
"iter/": {Tracing, AttachTraceIter},

"cgroup_skb/ingress": {CGroupSKB, AttachCGroupInetIngress},
"cgroup_skb/egress": {CGroupSKB, AttachCGroupInetEgress},
Expand All @@ -617,12 +619,18 @@ func getProgType(sectionName string) (ProgramType, AttachType) {
}

for prefix, t := range types {
if strings.HasPrefix(sectionName, prefix) {
return t.progType, t.attachType
if !strings.HasPrefix(sectionName, prefix) {
continue
}

if !strings.HasSuffix(prefix, "/") {
return t.progType, t.attachType, ""
}

return t.progType, t.attachType, sectionName[len(prefix):]
}

return UnspecifiedProgram, AttachNone
return UnspecifiedProgram, AttachNone, ""
}

func (ec *elfCode) loadRelocations(sections map[elf.SectionIndex]*elf.Section) (map[elf.SectionIndex]map[uint64]elf.Symbol, error) {
Expand Down
16 changes: 11 additions & 5 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,27 @@ func TestGetProgType(t *testing.T) {
section string
pt ProgramType
at AttachType
to string
}{
{"socket/garbage", SocketFilter, AttachNone},
{"kprobe/func", Kprobe, AttachNone},
{"xdp/foo", XDP, AttachNone},
{"cgroup_skb/ingress", CGroupSKB, AttachCGroupInetIngress},
{"socket/garbage", SocketFilter, AttachNone, ""},
{"kprobe/func", Kprobe, AttachNone, "func"},
{"xdp/foo", XDP, AttachNone, ""},
{"cgroup_skb/ingress", CGroupSKB, AttachCGroupInetIngress, ""},
{"iter/bpf_map", Tracing, AttachTraceIter, "bpf_map"},
}

for _, tc := range testcases {
pt, at := getProgType(tc.section)
pt, at, to := getProgType(tc.section)
if pt != tc.pt {
t.Errorf("section %s: expected type %s, got %s", tc.section, tc.pt, pt)
}

if at != tc.at {
t.Errorf("section %s: expected attach type %s, got %s", tc.section, tc.at, at)
}

if to != tc.to {
t.Errorf("section %s: expected attachment to be %q, got %q", tc.section, tc.to, to)
}
}
}
6 changes: 6 additions & 0 deletions internal/fd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"os"
"runtime"
"strconv"

Expand Down Expand Up @@ -61,3 +62,8 @@ func (fd *FD) Dup() (*FD, error) {

return NewFD(uint32(dup)), nil
}

func (fd *FD) File(name string) *os.File {
fd.Forget()
return os.NewFile(uintptr(fd.raw), name)
}
169 changes: 169 additions & 0 deletions link/cgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package link

import (
"os"

"github.com/cilium/ebpf"

"golang.org/x/xerrors"
)

type cgroupAttachFlags uint32

// cgroup attach flags
const (
flagAllowOverride cgroupAttachFlags = 1 << iota
flagAllowMulti
flagReplace
)

type CgroupOptions struct {
// Path to a cgroupv2 folder.
Path string
// One of the AttachCgroup* constants
Attach ebpf.AttachType
// Program must be of type CGroup*, and the attach type must match Attach.
Program *ebpf.Program
}

// AttachCgroup links a BPF program to a cgroup.
func AttachCgroup(opts CgroupOptions) (Link, error) {
cgroup, err := os.Open(opts.Path)
if err != nil {
return nil, xerrors.Errorf("can't open cgroup: %s", err)
}

clone, err := opts.Program.Clone()
if err != nil {
cgroup.Close()
return nil, err
}

var cg Link
cg, err = newLinkCgroup(cgroup, opts.Attach, clone)
if xerrors.Is(err, ErrNotSupported) {
cg, err = newProgAttachCgroup(cgroup, opts.Attach, clone, flagAllowMulti)
}
if xerrors.Is(err, ErrNotSupported) {
cg, err = newProgAttachCgroup(cgroup, opts.Attach, clone, flagAllowOverride)
}
if err != nil {
cgroup.Close()
clone.Close()
return nil, err
}

return cg, nil
}

// LoadPinnedCgroup loads a pinned cgroup from a bpffs.
func LoadPinnedCgroup(fileName string) (Link, error) {
link, err := LoadPinnedRawLink(fileName)
if err != nil {
return nil, err
}

return &linkCgroup{link}, nil
}

type progAttachCgroup struct {
cgroup *os.File
current *ebpf.Program
attachType ebpf.AttachType
flags cgroupAttachFlags
}

var _ Link = (*progAttachCgroup)(nil)

func (cg *progAttachCgroup) isLink() {}

func newProgAttachCgroup(cgroup *os.File, attach ebpf.AttachType, prog *ebpf.Program, flags cgroupAttachFlags) (*progAttachCgroup, error) {
if flags&flagAllowMulti > 0 {
if err := haveProgAttachReplace(); err != nil {
return nil, xerrors.Errorf("can't support multiple programs: %w", err)
}
}

err := RawAttachProgram(RawAttachProgramOptions{
Target: int(cgroup.Fd()),
Program: prog,
Flags: uint32(flags),
Attach: attach,
})
if err != nil {
return nil, xerrors.Errorf("cgroup: %w", err)
}

return &progAttachCgroup{cgroup, prog, attach, flags}, nil
}

func (cg *progAttachCgroup) Close() error {
defer cg.cgroup.Close()
defer cg.current.Close()

err := RawDetachProgram(RawDetachProgramOptions{
Target: int(cg.cgroup.Fd()),
Program: cg.current,
Attach: cg.attachType,
})
if err != nil {
return xerrors.Errorf("close cgroup: %s", err)
}
return nil
}

func (cg *progAttachCgroup) Update(prog *ebpf.Program) error {
new, err := prog.Clone()
if err != nil {
return err
}

args := RawAttachProgramOptions{
Target: int(cg.cgroup.Fd()),
Program: prog,
Attach: cg.attachType,
Flags: uint32(cg.flags),
}

if cg.flags&flagAllowMulti > 0 {
// Atomically replacing multiple programs requires at least
// 5.5 (commit 7dd68b3279f17921 "bpf: Support replacing cgroup-bpf
// program in MULTI mode")
args.Flags |= uint32(flagReplace)
args.Replace = cg.current
}

if err := RawAttachProgram(args); err != nil {
new.Close()
return xerrors.Errorf("can't update cgroup: %s", err)
}

cg.current.Close()
cg.current = new
return nil
}

func (cg *progAttachCgroup) Pin(string) error {
return xerrors.Errorf("can't pin cgroup: %w", ErrNotSupported)
}

type linkCgroup struct {
*RawLink
}

var _ Link = (*linkCgroup)(nil)

func (cg *linkCgroup) isLink() {}

func newLinkCgroup(cgroup *os.File, attach ebpf.AttachType, prog *ebpf.Program) (*linkCgroup, error) {
link, err := AttachRawLink(RawLinkOptions{
Target: int(cgroup.Fd()),
Program: prog,
Attach: attach,
})
if err != nil {
return nil, err
}

return &linkCgroup{link}, err
}
81 changes: 81 additions & 0 deletions link/cgroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package link

import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/testutils"
)

func TestAttachCgroup(t *testing.T) {
cgroup, prog, cleanup := mustCgroupFixtures(t)
defer cleanup()

link, err := AttachCgroup(CgroupOptions{
Path: cgroup.Name(),
Attach: ebpf.AttachCGroupInetEgress,
Program: prog,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}

if haveBPFLink() == nil {
if _, ok := link.(*linkCgroup); !ok {
t.Fatalf("Have support for bpf_link, but got %T instead of linkCgroup", link)
}
} else {
if _, ok := link.(*progAttachCgroup); !ok {
t.Fatalf("Expected progAttachCgroup, got %T instead", link)
}
}
}

func TestProgAttachCgroup(t *testing.T) {
cgroup, prog, cleanup := mustCgroupFixtures(t)
defer cleanup()

link, err := newProgAttachCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog, 0)
if err != nil {
t.Fatal("Can't create link:", err)
}

testLink(t, link, testLinkOptions{
prog: prog,
})
}

func TestProgAttachCgroupAllowMulti(t *testing.T) {
cgroup, prog, cleanup := mustCgroupFixtures(t)
defer cleanup()

link, err := newProgAttachCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog, flagAllowMulti)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create link:", err)
}

// It's currently not possible for a program to replace
// itself.
prog2 := mustCgroupEgressProgram(t)
testLink(t, link, testLinkOptions{
prog: prog2,
})
}

func TestLinkCgroup(t *testing.T) {
cgroup, prog, cleanup := mustCgroupFixtures(t)
defer cleanup()

link, err := newLinkCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create link:", err)
}

testLink(t, link, testLinkOptions{
prog: prog,
loadPinned: LoadPinnedCgroup,
})
}
Loading