How do I attach XDP to a network interface? #483
-
Mathias Kolehmainen writes on Slack:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The library doesn't have a way to attach XDP programs out of the box, for now, since that requires interacting with netlink. You can use a third party netlink library to achieve what you want: |
Beta Was this translation helpful? Give feedback.
-
Now you can attach XDP programs to network interfaces with Cilium eBPF library. This feature has added to the library since version 0.8.0 and it requires at least Linux 5.9. (It uses bpf_link to attach, not netlink) package main
import (
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
)
func main() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
coll, err := ebpf.LoadCollection("xdp.o")
if err != nil {
log.Fatal(err)
}
ifce, err := net.InterfaceByName("eth0")
if err != nil {
log.Fatal(err)
}
l, err := link.AttachXDP(link.XDPOptions{
Program: coll.Programs["xdp_prog"],
Interface: ifce.Index,
})
if err != nil {
log.Fatal(err)
}
defer l.Close()
<-sig
} |
Beta Was this translation helpful? Give feedback.
-
Are there any methods to remove XDP programs from a certain network interface using only Cilium eBPF library? |
Beta Was this translation helpful? Give feedback.
Now you can attach XDP programs to network interfaces with Cilium eBPF library. This feature has added to the library since version 0.8.0 and it requires at least Linux 5.9. (It uses bpf_link to attach, not netlink)
Sample code: