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

attach xdp error #379

Open
SohnYu opened this issue Sep 19, 2023 · 0 comments
Open

attach xdp error #379

SohnYu opened this issue Sep 19, 2023 · 0 comments

Comments

@SohnYu
Copy link

SohnYu commented Sep 19, 2023

libbpf: prog 'target': failed to attach to xdp: Invalid argument
failed to attach xdp on device lo to program target: invalid argument

os version: ubuntu20.04-server
kernel version: 5.4.0-162-generic

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
#endif

SEC("xdp")
int target(struct xdp_md *ctx) {
    // Reserve space on the ringbuffer for the sample
    return XDP_PASS;
}

char LICENSE[] SEC("license") = "GPL";
package main

import "C"

import (
        "fmt"
        "os"

        bpf "github.com/aquasecurity/libbpfgo"
)

const (
        deviceName = "lo"
)

func main() {

        bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }
        defer bpfModule.Close()

        err = bpfModule.BPFLoadObject()
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }
        defer bpfModule.Close()

        err = bpfModule.BPFLoadObject()
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }

        xdpProg, err := bpfModule.GetProgram("target")
        if xdpProg == nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }

        xdpProg, err := bpfModule.GetProgram("target")
        if xdpProg == nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }

        _, err = xdpProg.AttachXDP(deviceName)
        if err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(-1)
        }
        select{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant