Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Jun 29, 2023
1 parent b4eb79d commit fc5213e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 155 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.direnv
/build
*.o
90 changes: 6 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,14 @@
# **libbpf-starter-template**
# **An example to show how to use libbpf for bpf relocate**

![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)
[![Build and publish](https://github.com/eunomia-bpf/libbpf-starter-template/actions/workflows/publish.yml/badge.svg)](https://github.com/eunomia-bpf/libbpf-starter-template/actions/workflows/publish.yml)
![GitHub stars](https://img.shields.io/github/stars/eunomia-bpf/libbpf-starter-template?style=social)

Welcome to the **`libbpf-starter-template`**! This project template is designed to help you quickly start
developing eBPF projects using libbpf in C. The template provides a solid starting point with a Makefile,
Dockerfile, and GitHub action, along with all necessary dependencies to simplify your development process.

借助于 GitHub 模板和 Github Codespace,可以轻松构建 eBPF 项目和开发环境,一键在线编译运行 eBPF 程序。关于中文的文档和详细的 eBPF 开发教程,可以参考:https://github.com/eunomia-bpf/bpf-developer-tutorial

There are other templates for other languages:

- <https://github.com/eunomia-bpf/libbpf-starter-template>: eBPF project template based on the C language and the libbpf framework.
- <https://github.com/eunomia-bpf/cilium-ebpf-starter-template>: eBPF project template based on the Go language and the cilium/ebpf framework.
- <https://github.com/eunomia-bpf/libbpf-rs-starter-template>: eBPF project template based on the Rust language and the libbpf-rs framework.
- <https://github.com/eunomia-bpf/eunomia-template>: eBPF project template based on the C language and the eunomia-bpf framework.

## **Getting Started**

To get started, simply click the "Use this template" button on the GitHub repository page. This will create
a new repository in your account with the same files and structure as this template.

### Use docker

Run the following code to run the eBPF code from the cloud to your local machine in one line:

```console
$ sudo docker run --rm -it --privileged ghcr.io/eunomia-bpf/libbpf-template:latest
TIME EVENT COMM PID PPID FILENAME/EXIT CODE
09:25:14 EXEC sh 28142 1788 /bin/sh
09:25:14 EXEC playerctl 28142 1788 /nix/store/vf3rsb7j3p7zzyjpb0a3axl8yq4z1sq5-playerctl-2.4.1/bin/playerctl
09:25:14 EXIT playerctl 28142 1788 [1] (6ms)
09:25:15 EXEC sh 28145 1788 /bin/sh
09:25:15 EXEC playerctl 28145 1788 /nix/store/vf3rsb7j3p7zzyjpb0a3axl8yq4z1sq5-playerctl-2.4.1/bin/playerctl
09:25:15 EXIT playerctl 28145 1788 [1] (6ms)
```

### Use Nix

Using [direnv](https://github.com/direnv/direnv) and nix, you can quickly access a dev shell with a complete development environment.

With direnv, you can automatically load the required dependencies when you enter the directory.
This way you don't have to worry about installing dependencies to break your other project development environment.

See how to install direnv and Nix:
- direnv: https://github.com/direnv/direnv/blob/master/docs/installation.md
- Nix: run
```
sh <(curl -L https://nixos.org/nix/install) --daemon
```

Then use the following command to enable direnv support in this directory.

```sh
direnv allow
```

If you want use nix flake without direnv, simply run:

```sh
nix develop
```

## **Features**

This starter template includes the following features:

- A **`Makefile`** that allows you to build the project in one command
- A **`Dockerfile`** to create a containerized environment for your project
- A **`flake.nix`** to enter a dev shell with needed dependencies
- A GitHub action to automate your build and publish process
and docker image
- All necessary dependencies for C development with libbpf
An example to show how to use libbpf for bpf relocate

## **How to use**

### **1. Create a new repository using this template**

Click the "Use this template" button on the GitHub repository page to create a new repository based on this template.

### **2. Clone your new repository**
### **Clone your new repository**

Clone your newly created repository to your local machine:

Expand All @@ -94,7 +22,7 @@ Or after clone the repo, you can update the git submodule with following command
git submodule update --init --recursive
```

### **3. Install dependencies**
### **Install dependencies**

For dependencies, it varies from distribution to distribution. You can refer to shell.nix and dockerfile for installation.

Expand All @@ -108,7 +36,7 @@ sudo apt-get install -y --no-install-recommends \

to install dependencies.

### **4. Build the project**
### **Build the project**

To build the project, run the following command:

Expand All @@ -123,13 +51,7 @@ This will compile your code and create the necessary binaries. You can you the `
You can run the binary with:

```console
sudo src/bootstrap
```

Or with Github Packages locally:

```console
docker run --rm -it --privileged -v $(pwd):/examples ghcr.io/eunomia-bpf/libbpf-template:latest
sudo src/relo
```

### **7. GitHub Actions**
Expand Down
20 changes: 20 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BPF compiler
BPF_CC = clang
# BPF C flags
BPF_CFLAGS = -O2 -target bpf -c -g
# BPF source files
BPF_SRCS = $(wildcard *.bpf.c)
# BPF object files
BPF_OBJS = $(BPF_SRCS:.c=.o)

all: $(BPF_OBJS) base.btf

%.bpf.o: %.bpf.c
$(BPF_CC) $(BPF_CFLAGS) $< -o $@

base.btf:
clang -g -c btf-base.c -o btf-base.bpf.o
pahole --btf_encode_detached base.btf btf-base.bpf.o

clean:
rm -f $(BPF_OBJS) base.btf
Binary file added examples/base.btf
Binary file not shown.
15 changes: 15 additions & 0 deletions examples/btf-base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// #define USE_NEW_VERSION
// use a different struct
struct data {
int a;
#ifdef USE_NEW_VERSION
int b;
#endif
int c;
int d;
};

int add_test(struct data *d) {
return d->a + d->c;
}

24 changes: 24 additions & 0 deletions examples/btf-relo.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define USE_NEW_VERSION

#ifndef BPF_NO_PRESERVE_ACCESS_INDEX
#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)
#endif

struct data {
int a;
#ifdef USE_NEW_VERSION
int b;
#endif
int c;
int d;
};

#ifndef BPF_NO_PRESERVE_ACCESS_INDEX
#pragma clang attribute pop
#endif


int add_test(struct data *d) {
return d->a + d->c;
}

2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ INCLUDES := -I$(OUTPUT) -I../libbpf/include/uapi -I$(dir $(VMLINUX))
CFLAGS := -g -Wall
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)

APPS = bootstrap
APPS = relo

# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
Expand Down
Loading

0 comments on commit fc5213e

Please sign in to comment.