Skip to content

Commit

Permalink
add ebpf-go module
Browse files Browse the repository at this point in the history
  • Loading branch information
chaolihf committed Feb 16, 2024
1 parent b6c2aff commit 59b86b4
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
}
],
"preLaunchTask": "buildJattach"
},
{
"name": "EBPF测试",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/pkg/ebpf/",
"cwd": "${workspaceFolder}/pkg/ebpf",
"args": []
}
]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"un.h": "c",
"sockaddr.h": "c",
"string.h": "c",
"common.h": "c"
"common.h": "c",
"bpf_helpers.h": "c"
},
"cmake.configureOnOpen": false
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module com.chinatelecom.oneops.exporter/OneAgent

go 1.19
go 1.21.0

toolchain go1.21.6

require (
github.com/chaolihf/udpgo v0.0.13
Expand All @@ -27,6 +29,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chaolihf/gopsutil v0.0.7 // indirect
github.com/cilium/ebpf v0.13.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/chaolihf/udpgo v0.0.11 h1:pVG6cMzWOjRDC0/Glh7bAjcRS7GCox0Gq2E0Ak/ySGA
github.com/chaolihf/udpgo v0.0.11/go.mod h1:+MgC8ewxYznU81AmQqGz7Xp5X1M27AJEN4WKjBM9kkw=
github.com/chaolihf/udpgo v0.0.13 h1:PffwmW49o/WxbYD4j1T3cw7niCmMm2GzRW1gLdIWJsU=
github.com/chaolihf/udpgo v0.0.13/go.mod h1:+MgC8ewxYznU81AmQqGz7Xp5X1M27AJEN4WKjBM9kkw=
github.com/cilium/ebpf v0.13.0 h1:K+41peBnbROzY6nHc9Kq79B4lnJpiF/BMpBuoTGAWSY=
github.com/cilium/ebpf v0.13.0/go.mod h1:DHp1WyrLeiBh19Cf/tfiSMhqheEiK8fXFZ4No0P1Hso=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/cgroups/v3 v3.0.1 h1:4hfGvu8rfGIwVIDd+nLzn/B9ZXx4BcCjzt5ToenJRaE=
Expand Down
1 change: 1 addition & 0 deletions pkg/ebpf/all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go generate && go build && ./ebpf
26 changes: 26 additions & 0 deletions pkg/ebpf/counter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

//go:build ignore

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u64);
__uint(max_entries, 1);
} pkt_count SEC(".maps");

// count_packets atomically increases a packet counter on every invocation.
SEC("xdp")
int count_packets() {
__u32 key = 0;
__u64 *count = bpf_map_lookup_elem(&pkt_count, &key);
if (count) {
__sync_fetch_and_add(count, 1);
}

return XDP_PASS;
}

char __license[] SEC("license") = "Dual MIT/GPL";
64 changes: 64 additions & 0 deletions pkg/ebpf/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"log"
"net"
"os"
"os/signal"
"time"

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

func main() {
// Remove resource limits for kernels <5.11.
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal("Removing memlock:", err)
}

// Load the compiled eBPF ELF and load it into the kernel.
var objs counterObjects
if err := loadCounterObjects(&objs, nil); err != nil {
log.Fatal("Loading eBPF objects:", err)
}
defer objs.Close()

ifname := "enp0s3" // Change this to an interface on your machine.
iface, err := net.InterfaceByName(ifname)
if err != nil {
log.Fatalf("Getting interface %s: %s", ifname, err)
}

// Attach count_packets to the network interface.
link, err := link.AttachXDP(link.XDPOptions{
Program: objs.CountPackets,
Interface: iface.Index,
})
if err != nil {
log.Fatal("Attaching XDP:", err)
}
defer link.Close()

log.Printf("Counting incoming packets on %s..", ifname)

// Periodically fetch the packet counter from PktCount,
// exit the program when interrupted.
tick := time.Tick(time.Second)
stop := make(chan os.Signal, 5)
signal.Notify(stop, os.Interrupt)
for {
select {
case <-tick:
var count uint64
err := objs.PktCount.Lookup(uint32(0), &count)
if err != nil {
log.Fatal("Map lookup:", err)
}
log.Printf("Received %d packets", count)
case <-stop:
log.Print("Received signal, exiting..")
return
}
}
}
3 changes: 3 additions & 0 deletions pkg/ebpf/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go counter counter.c

0 comments on commit 59b86b4

Please sign in to comment.