Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bpf2go example + Devcontainer example #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions devcontainer-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Devcontainer Examples

The following directory includes examples of [Visual Studio Code Devcontainer](https://code.visualstudio.com/docs/devcontainers/containers) configurations for developing ebpf applications.
33 changes: 33 additions & 0 deletions devcontainer-example/ebpf-and-go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM mcr.microsoft.com/devcontainers/go:1.23-bookworm

RUN apt update

RUN apt install -y build-essential
RUN apt install -y pkg-config
RUN apt install -y clang
RUN apt install -y llvm
RUN apt install -y git
RUN apt install -y libelf-dev
RUN apt install -y libpcap-dev
RUN apt install -y iproute2
RUN apt install -y iputils-ping
RUN apt install -y linux-headers-generic
RUN apt install -y libbpf-dev
RUN apt install -y linux-libc-dev
RUN apt install -y cmake
RUN apt install -y libpcap-dev
RUN apt install -y libcap-ng-dev
RUN apt install -y libbfd-dev
RUN apt install -y gcc-multilib
RUN apt install -y m4
RUN ln -sf /usr/include/asm-generic/ /usr/include/asm
RUN apt install -y libcap-dev
RUN ln -sf /usr/local/go/bin/go /bin/go
RUN ln -sf /usr/include/asm-generic/ /usr/include/asm

RUN mkdir /sources/
WORKDIR /sources/
RUN git clone --recurse-submodules https://github.com/libbpf/bpftool.git
RUN make -C bpftool/src/ install
RUN git clone --recurse-submodules https://github.com/xdp-project/xdp-tools.git
RUN make -C xdp-tools/ install
22 changes: 22 additions & 0 deletions devcontainer-example/ebpf-and-go/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Go-ebpf-env",
"build": {"dockerfile": "Dockerfile"},

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools",
"nicknickolaev.ebpf-assembly",
"golang.Go"
]
}
},

"runArgs": ["--network=host"],
"privileged": true
}
3 changes: 3 additions & 0 deletions xdp-bpf2go-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
xdp-bpf2go-example
**bpfeb.go
**bpfel.go
49 changes: 49 additions & 0 deletions xdp-bpf2go-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# xdp-bpf2go-example

This examples leverages a `Go` based User Space application that reads from an eBPF map populated with packet protocol information.

The Kernel space application leverages `xdp` to inspect incoming packets and performs the following:

* Check for a valid IP packet
* Extract the `protocol` header value
* Place in a eBPF array map

The protocol header field will contain a [protocol number](https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers) that corresponds to the respective protocol. For example, `ICMP=1`, `IGMP=2`, and so on.

The Kernel Space application keeps count of each recorded instance of a protocol and stores it into a array map, which can be visualised as:

```
+----------------------------------------------------+
| eBPF Array Map |
| |
| +------+ +------+ +------+ +------+ +------+ |
| | 0 | | 1 | | 2 | | ... | | 254 | |
| |------| |------| |------| |------| |------| |
| | ? | | ? | | ? | | ... | | ? | |
| +------+ +------+ +------+ +------+ +------+ |
| |
+----------------------------------------------------+
```

Where `key` represents the IP Protocol Number, and `value` counting the number of instances.

The Go application reads this map array, and leverages a helper function to map the protocol number to a name and output to stdout:

![Diagram depicting ebpf kernel and user land applications](ebpf-diagram.png)

## To Run:

* Change the interface name if required in main.go

```
go generate
go run .
```

## To Build:

```
go generate
go build .
```
Binary file added xdp-bpf2go-example/ebpf-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions xdp-bpf2go-example/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 packetProtocol packetProtocol.c
7 changes: 7 additions & 0 deletions xdp-bpf2go-example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/xdp-project/bpf-examples/xdp-bpf2go-example

go 1.22.10

require github.com/cilium/ebpf v0.17.2

require golang.org/x/sys v0.30.0 // indirect
28 changes: 28 additions & 0 deletions xdp-bpf2go-example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/cilium/ebpf v0.17.2 h1:IQTaTVu0vKA8WTemFuBnxW9YbAwMkJVKHsNHW4lHv/g=
github.com/cilium/ebpf v0.17.2/go.mod h1:9X5VAsIOck/nCAp0+nCSVzub1Q7x+zKXXItTMYfNE+E=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/jsimonetti/rtnetlink/v2 v2.0.1 h1:xda7qaHDSVOsADNouv7ukSuicKZO7GgVUCXxpaIEIlM=
github.com/jsimonetti/rtnetlink/v2 v2.0.1/go.mod h1:7MoNYNbb3UaDHtF8udiJo/RH6VsTKP1pqKLUTVCvToE=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Binary file added xdp-bpf2go-example/l3-packet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading