dropbox / goebpf

Library to work with eBPF programs from Go
Other
1.14k stars 85 forks source link

LoadElf() loads without error but elfSystem is empty #41

Closed ErvinsK closed 4 years ago

ErvinsK commented 4 years ago

Loading elf files run smoothly without any errors but afterwards it returns empty maps and empty programs (with GetMaps() and GetPrograms()) :( However, eBPF/XDP programs run fine if loaded for example with ip tool. llvm-objdump also show all code and objects! Trying to understand what is the problem? Any help appreciated!

belyalov commented 4 years ago

Hey! )

Could you please provide some samples of the eBPF program / golang part? It is really hard to say why it is empty without code :)

ErvinsK commented 4 years ago

Hi!

Sure - they are dumb simple:

  1. Go part:
package main

import (
    "fmt"
    "os"

    "github.com/dropbox/goebpf"
)

func main() {

    fmt.Println("XDP prog dump")

    bpf := goebpf.NewDefaultEbpfSystem()
    err := bpf.LoadElf("xdp-example.o")

    if err != nil {
        fatalError("LoadElf() failed: %v", err)
    }

    fmt.Println("\nPrograms:")

    for _, prog := range bpf.GetPrograms() {
        fmt.Printf("\t%s: %v, size %d, license \"%s\"\n",
            prog.GetName(), prog.GetType(), prog.GetSize(), prog.GetLicense(),
        )
    }

}
  1. Simple XDP example:
    #include <linux/bpf.h>
    #define SEC(NAME) __attribute__((section(NAME), used))
    SEC("prog")
    int  xdp_prog_simple(struct xdp_md *ctx)
    {
    return XDP_DROP;
    }
    char _license[] SEC("license") = "GPL";
belyalov commented 4 years ago

That's simple! )

The problem is in SEC("prog") - the library uses section name to distinguish between program types, it is a bit different than kernel examples.

So if you check examples you'll find that:

SEC("xdp")
int xdp_dump(struct xdp_md *ctx) {
// ...
}

https://github.com/dropbox/goebpf/blob/master/examples/xdp/xdp_dump/ebpf_prog/xdp_dump.c#L74

Simply rename section to xdp and it will definitely work! )

ErvinsK commented 4 years ago

Yep, it works! Thank you for a prompt resolution! I'm feeling a bit embarrassed for such a simple error but eBPF is tricky to master.