Skip to content

Commit

Permalink
examples: add example for XDP
Browse files Browse the repository at this point in the history
Add an example for attaching an eBPF program to a network
interface with XDP. The example program parses the IPv4
source address from packets (if available) and records
packet counts by IP address in an LRU hash map.

Issue: #645

Signed-off-by: Will Daly <[email protected]>
  • Loading branch information
wedaly committed Jun 10, 2022
1 parent 49ebb13 commit e12ebf5
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Like kprobes, but with better performance and usability, for kernels 5.5 and later.
* [tcp_connect](fentry/) - Trace outgoing IPv4 TCP connections.
* [tcp_close](tcprtt/) - Log RTT of IPv4 TCP connections using eBPF CO-RE helpers.
* XDP - Attach a program to a network interface to process incoming packets.
* [xdp](xdp/) - Print packet counts by IPv4 source address.
* Add your use case(s) here!

## How to run
Expand Down
17 changes: 17 additions & 0 deletions examples/headers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ enum bpf_map_type {
BPF_MAP_TYPE_INODE_STORAGE = 28,
};

enum xdp_action {
XDP_ABORTED = 0,
XDP_DROP = 1,
XDP_PASS = 2,
XDP_TX = 3,
XDP_REDIRECT = 4,
};

struct xdp_md {
__u32 data;
__u32 data_end;
__u32 data_meta;
__u32 ingress_ifindex;
__u32 rx_queue_index;
__u32 egress_ifindex;
};

enum {
BPF_ANY = 0,
BPF_NOEXIST = 1,
Expand Down
119 changes: 119 additions & 0 deletions examples/xdp/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/xdp/bpf_bpfeb.o
Binary file not shown.
119 changes: 119 additions & 0 deletions examples/xdp/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/xdp/bpf_bpfel.o
Binary file not shown.
83 changes: 83 additions & 0 deletions examples/xdp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build linux
// +build linux

// This program demonstrates attaching an eBPF program to a network interface
// with XDP (eXpress Data Path). The program parses the IPv4 source address
// from packets and writes the packet count by IP to an LRU hash map.
// The userspace program (Go code in this file) prints the contents
// of the map to stdout every second.
// It is possible to modify the XDP program to drop or redirect packets
// as well -- give it a try!
package main

import (
"fmt"
"log"
"net"
"os"
"strings"
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
)

// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf xdp.c -- -I../headers

func main() {
if len(os.Args) < 2 {
log.Fatalf("Please specify a network interface")
}

// Look up the network interface by name.
ifaceName := os.Args[1]
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
log.Fatalf("lookup network iface %q: %s", ifaceName, err)
}

// Load pre-compiled programs into the kernel.
objs := bpfObjects{}
if err := loadBpfObjects(&objs, nil); err != nil {
log.Fatalf("loading objects: %s", err)
}
defer objs.Close()

// Attach the program.
l, err := link.AttachXDP(link.XDPOptions{
Program: objs.XdpProgFunc,
Interface: iface.Index,
})
if err != nil {
log.Fatalf("could not attach XDP program: %s", err)
}
defer l.Close()

log.Printf("Attached XDP program to iface %q (index %d)", iface.Name, iface.Index)
log.Printf("Press Ctrl-C to exit and remove the program")

// Print the contents of the BPF hash map (source IP address -> packet count).
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for range ticker.C {
s, err := formatMapContents(objs.XdpStatsMap)
if err != nil {
log.Printf("Error reading map: %s", err)
continue
}
log.Printf("Map contents:\n%s", s)
}
}

func formatMapContents(m *ebpf.Map) (string, error) {
var sb strings.Builder
var key, val uint32
iter := m.Iterate()
for iter.Next(&key, &val) {
sourceIP := net.IPv4(byte(key>>24), byte(key>>16), byte(key>>8), byte(key))
packetCount := val
sb.WriteString(fmt.Sprintf("\t%s => %d\n", sourceIP, packetCount))
}
return sb.String(), iter.Err()
}
Loading

0 comments on commit e12ebf5

Please sign in to comment.