aquasecurity / libbpfgo

eBPF library for Go. Powered by libbpf.
Apache License 2.0
739 stars 94 forks source link

[Error] in running go user space code #383

Open VedRatan opened 1 year ago

VedRatan commented 1 year ago

On trying to run my go user space code the following error logs are generated:

CGO_ENABLED=1 go run drop.go
# github.com/aquasecurity/libbpfgo
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:30:9: could not determine kind of name for C.LIBBPF_MAJOR_VERSION
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:35:9: could not determine kind of name for C.LIBBPF_MINOR_VERSION
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:188:55: could not determine kind of name for C.LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:189:55: could not determine kind of name for C.LIBBPF_STRICT_MAP_DEFINITIONS
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:187:55: could not determine kind of name for C.LIBBPF_STRICT_NO_OBJECT_LIST
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:186:55: could not determine kind of name for C.LIBBPF_STRICT_SEC_NAME
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:477:7: could not determine kind of name for C.bpf_object__next_map
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:517:7: could not determine kind of name for C.bpf_object__next_program
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:770:21: could not determine kind of name for C.bpf_program__type
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:1565:16: could not determine kind of name for C.libbpf_probe_bpf_map_type
../go/pkg/mod/github.com/aquasecurity/libbpfgo@v0.5.1-libbpf-1.2/libbpfgo.go:1573:16: could not determine kind of name for C.libbpf_probe_bpf_prog_type

my go.mod file configurations:

module github.com/VedRatan/ebpf

go 1.21.2

require github.com/aquasecurity/libbpfgo v0.5.1-libbpf-1.2

my go file:

package main

import (
    "C"
    "fmt"

    bpf "github.com/aquasecurity/libbpfgo"
)

import (
    "os"
    "os/signal"
    "syscall"
)

func main() {
    sig := make(chan os.Signal, 1)
    signal.Notify(sig, os.Interrupt)

    b, err := bpf.NewModuleFromFile("dropipv4.o")
    if err != nil {
        panic(err)
    }
    defer b.Close()

    err = b.BPFLoadObject()
    if err != nil {
        panic(err)
    }

    prog, err := b.GetProgram("capture_packets")
    if err != nil {
        panic(err)
    }

    // Specify the network interface name to attach the XDP program to
    ifaceName := "wlo1" // Replace with your network interface name

    link, err := prog.AttachXDP(ifaceName)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error attaching XDP program: %v\n", err)
        os.Exit(1)
    }

    fmt.Printf("XDP program attached to wlo1\n")

    // mapName := "rxcnt"

    // mp, err := b.GetMap(mapName)
    // if err != nil {
    //  fmt.Fprintf(os.Stderr, "Error getting map: %v\n", err)
    //  os.Exit(1)
    // }

    // Wait for a signal to keep the program running
    sig = make(chan os.Signal, 1)
    signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
    <-sig

    // Detach the XDP program when the program exits
    if err := link.Destroy(); err != nil {
        fmt.Fprintf(os.Stderr, "Error detaching XDP program: %v\n", err)
    }
}
geyslan commented 1 year ago

Hello @VedRatan. I would suggest you to compose your build based on https://github.com/aquasecurity/libbpfgo/tree/main/selftest/common and your use of the libbpfgo (user/bpf) based on some selftest https://github.com/aquasecurity/libbpfgo/tree/main/selftest/map-update.