Skip to content

How do I attach XDP to a network interface? #483

Answered by mehrdadrad
lmb asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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"]…

Replies: 3 comments 4 replies

Comment options

lmb
Nov 11, 2021
Maintainer Author

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@diegobes
Comment options

@dreamerlzl
Comment options

@ti-mo
Comment options

Answer selected by lmb
Comment options

You must be logged in to vote
1 reply
@ti-mo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants