Skip to content

Commit e61bdc4

Browse files
authored
Initial commit
0 parents  commit e61bdc4

30 files changed

+618161
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"build": {
3+
"context": "..",
4+
"dockerfile": "../dev.dockerfile"
5+
}
6+
}

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.github/workflows/docker.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Build and public docker image
2+
3+
on:
4+
push:
5+
branches: "master"
6+
7+
jobs:
8+
build-and-push-image:
9+
runs-on: ubuntu-latest
10+
# run only when code is compiling and tests are passing
11+
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
12+
# steps to perform in job
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
with:
17+
submodules: 'recursive'
18+
19+
- name: install deps
20+
run: |
21+
sudo apt-get install -y --no-install-recommends \
22+
libelf1 libelf-dev zlib1g-dev libclang-dev \
23+
make git clang llvm pkg-config build-essential
24+
25+
- name: build package
26+
run: make build
27+
28+
# setup Docker buld action
29+
- name: Set up Docker Buildx
30+
id: buildx
31+
uses: docker/setup-buildx-action@v2
32+
33+
- name: Login to Github Packages
34+
uses: docker/login-action@v2
35+
with:
36+
registry: ghcr.io
37+
username: ${{ github.repository_owner }}
38+
password: ${{ secrets.GITHUB_TOKEN }}
39+
40+
- name: Build image and push to GitHub Container Registry
41+
uses: docker/build-push-action@v2
42+
with:
43+
# relative path to the place where source code with Dockerfile is located
44+
context: ./
45+
file: dockerfile
46+
platforms: linux/amd64
47+
# Note: tags has to be all lower-case
48+
tags: |
49+
ghcr.io/${{ github.repository }}
50+
push: true
51+
52+
- name: Image digest
53+
run: echo ${{ steps.docker_build.outputs.digest }}

.github/workflows/publish.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Build and publish
2+
3+
on:
4+
push:
5+
branches: "master"
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]')"
11+
strategy:
12+
matrix:
13+
target: [ x86_64-unknown-linux-gnu ]
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
submodules: 'recursive'
19+
20+
- name: Set latest release version
21+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
22+
id: set_version
23+
uses: actions/github-script@v6
24+
with:
25+
result-encoding: string
26+
script: |
27+
const { data: releases } = await github.rest.repos.listReleases({
28+
owner: context.repo.owner,
29+
repo: context.repo.repo,
30+
});
31+
32+
const { data: tags } = await github.rest.repos.listTags({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo
35+
});
36+
37+
if (releases.length === 0) { return "v0.0.1"; }
38+
39+
function increase_v(version) {
40+
const parts = version.split(".");
41+
const last = parseInt(parts[2]) + 1;
42+
const next_version = `${parts[0]}.${parts[1]}.${last.toString()}`;
43+
return next_version;
44+
}
45+
46+
const latest_release_tag = releases[0].tag_name;
47+
48+
const tag = tags.find(tag => tag.commit.sha === context.sha);
49+
50+
return tag ? tag.name : increase_v(latest_release_tag)
51+
52+
- name: install deps
53+
run: |
54+
sudo apt-get install -y --no-install-recommends \
55+
libelf1 libelf-dev zlib1g-dev libclang-dev \
56+
make git clang llvm pkg-config build-essential
57+
58+
- name: build package
59+
run: make build
60+
61+
- name: Publish
62+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
63+
uses: softprops/action-gh-release@v1
64+
with:
65+
files: |
66+
src/bootstrap
67+
prerelease: false
68+
tag_name: ${{ steps.set_version.outputs.result }}
69+
generate_release_notes: true
70+
env:
71+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.direnv
2+
/build

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "libbpf"]
2+
path = libbpf
3+
url = https://github.com/libbpf/libbpf.git
4+
[submodule "bpftool"]
5+
path = bpftool
6+
url = https://github.com/libbpf/bpftool.git

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 eunomia-bpf org.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build:
2+
make -C src
3+
4+
clean:
5+
make -C src clean
6+
7+
install:
8+
sudo apt update
9+
sudo apt-get install -y --no-install-recommends \
10+
libelf1 libelf-dev zlib1g-dev \
11+
make clang llvm

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# **libbpf-starter-template**
2+
3+
![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)
4+
[![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)
5+
![GitHub stars](https://img.shields.io/github/stars/eunomia-bpf/libbpf-starter-template?style=social)
6+
7+
Welcome to the **`libbpf-starter-template`**! This project template is designed to help you quickly start
8+
developing eBPF projects using libbpf in C. The template provides a solid starting point with a Makefile,
9+
Dockerfile, and GitHub action, along with all necessary dependencies to simplify your development process.
10+
11+
借助于 GitHub 模板和 Github Codespace,可以轻松构建 eBPF 项目和开发环境,一键在线编译运行 eBPF 程序。关于中文的文档和详细的 eBPF 开发教程,可以参考:https://github.com/eunomia-bpf/bpf-developer-tutorial
12+
13+
There are other templates for other languages:
14+
15+
- <https://github.com/eunomia-bpf/libbpf-starter-template>: eBPF project template based on the C language and the libbpf framework.
16+
- <https://github.com/eunomia-bpf/cilium-ebpf-starter-template>: eBPF project template based on the Go language and the cilium/ebpf framework.
17+
- <https://github.com/eunomia-bpf/libbpf-rs-starter-template>: eBPF project template based on the Rust language and the libbpf-rs framework.
18+
- <https://github.com/eunomia-bpf/eunomia-template>: eBPF project template based on the C language and the eunomia-bpf framework.
19+
20+
## **Getting Started**
21+
22+
To get started, simply click the "Use this template" button on the GitHub repository page. This will create
23+
a new repository in your account with the same files and structure as this template.
24+
25+
### Use docker
26+
27+
Run the following code to run the eBPF code from the cloud to your local machine in one line:
28+
29+
```console
30+
$ sudo docker run --rm -it --privileged ghcr.io/eunomia-bpf/libbpf-template:latest
31+
TIME EVENT COMM PID PPID FILENAME/EXIT CODE
32+
09:25:14 EXEC sh 28142 1788 /bin/sh
33+
09:25:14 EXEC playerctl 28142 1788 /nix/store/vf3rsb7j3p7zzyjpb0a3axl8yq4z1sq5-playerctl-2.4.1/bin/playerctl
34+
09:25:14 EXIT playerctl 28142 1788 [1] (6ms)
35+
09:25:15 EXEC sh 28145 1788 /bin/sh
36+
09:25:15 EXEC playerctl 28145 1788 /nix/store/vf3rsb7j3p7zzyjpb0a3axl8yq4z1sq5-playerctl-2.4.1/bin/playerctl
37+
09:25:15 EXIT playerctl 28145 1788 [1] (6ms)
38+
```
39+
40+
### Use Nix
41+
42+
Using [direnv](https://github.com/direnv/direnv) and nix, you can quickly access a dev shell with a complete development environment.
43+
44+
With direnv, you can automatically load the required dependencies when you enter the directory.
45+
This way you don't have to worry about installing dependencies to break your other project development environment.
46+
47+
See how to install direnv and Nix:
48+
- direnv: https://github.com/direnv/direnv/blob/master/docs/installation.md
49+
- Nix: run
50+
```
51+
sh <(curl -L https://nixos.org/nix/install) --daemon
52+
```
53+
54+
Then use the following command to enable direnv support in this directory.
55+
56+
```sh
57+
direnv allow
58+
```
59+
60+
If you want use nix flake without direnv, simply run:
61+
62+
```sh
63+
nix develop
64+
```
65+
66+
## **Features**
67+
68+
This starter template includes the following features:
69+
70+
- A **`Makefile`** that allows you to build the project in one command
71+
- A **`Dockerfile`** to create a containerized environment for your project
72+
- A **`flake.nix`** to enter a dev shell with needed dependencies
73+
- A GitHub action to automate your build and publish process
74+
and docker image
75+
- All necessary dependencies for C development with libbpf
76+
77+
## **How to use**
78+
79+
### **1. Create a new repository using this template**
80+
81+
Click the "Use this template" button on the GitHub repository page to create a new repository based on this template.
82+
83+
### **2. Clone your new repository**
84+
85+
Clone your newly created repository to your local machine:
86+
87+
```sh
88+
git clone https://github.com/your_username/your_new_repository.git --recursive
89+
```
90+
91+
Or after clone the repo, you can update the git submodule with following commands:
92+
93+
```sh
94+
git submodule update --init --recursive
95+
```
96+
97+
### **3. Install dependencies**
98+
99+
For dependencies, it varies from distribution to distribution. You can refer to shell.nix and dockerfile for installation.
100+
101+
On Ubuntu, you may run `make install` or
102+
103+
```sh
104+
sudo apt-get install -y --no-install-recommends \
105+
libelf1 libelf-dev zlib1g-dev \
106+
make clang llvm
107+
```
108+
109+
to install dependencies.
110+
111+
### **4. Build the project**
112+
113+
To build the project, run the following command:
114+
115+
```sh
116+
make build
117+
```
118+
119+
This will compile your code and create the necessary binaries. You can you the `Github Code space` or `Github Action` to build the project as well.
120+
121+
### ***Run the Project***
122+
123+
You can run the binary with:
124+
125+
```console
126+
sudo src/bootstrap
127+
```
128+
129+
Or with Github Packages locally:
130+
131+
```console
132+
docker run --rm -it --privileged -v $(pwd):/examples ghcr.io/eunomia-bpf/libbpf-template:latest
133+
```
134+
135+
### **7. GitHub Actions**
136+
137+
This template also includes a GitHub action that will automatically build and publish your project when you push to the repository.
138+
To customize this action, edit the **`.github/workflows/publish.yml`** file.
139+
140+
## **Contributing**
141+
142+
We welcome contributions to improve this template! If you have any ideas or suggestions,
143+
feel free to create an issue or submit a pull request.
144+
145+
## **License**
146+
147+
This project is licensed under the MIT License. See the **[LICENSE](LICENSE)** file for more information.

bpftool

Submodule bpftool added at 4a0b800

0 commit comments

Comments
 (0)