-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go generate && go build && ./ebpf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |