|
| 1 | +# **libbpf-starter-template** |
| 2 | + |
| 3 | + |
| 4 | +[](https://github.com/eunomia-bpf/libbpf-starter-template/actions/workflows/publish.yml) |
| 5 | + |
| 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. |
0 commit comments