Code Monkey home page Code Monkey logo

zig-bpf-intro's Introduction

zig-bpf-intro

build

Reminder! this repo has a git submodule, so remember to clone with --recursive!

If you are new to Zig: Getting Started

If you find yourself here it's likely from my article about using BPF with Zig. To get your hands on the Zig compiler I suggest downloading master from here, or you can run a script included in this repo:

sudo ./zig-install.sh

and that will put Zig into /usr/local. Test by running:

zig version

In order to build and run the example program, simply:

sudo zig build run

sudo is needed here because we'll be loading BPF programs and that requires the SYS_ADMIN capability. TODO: improve capability aspect for education and fun

And in another terminal, just:

ping localhost

and you'll see output from our BPF wielding program. If you restart ping, you might also see the cpu change (depending on whether you have multiple cpus that is)

Exploring

For more resources on Zig:

For locations internal to this repo:

  • src/probe.zig contains the BPF program
  • src/main.zig is our main program, it loads the BPF program and initializes a perf buffer
  • src/common.zig contains a function that wraps C code to instantiate a raw socket
  • libs/bpf is a git submodule containing the Zig BPF Library
  • libs/bpf/src/object.zig contains the BPF loader code

zig-bpf-intro's People

Contributors

mattnite avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

zig-bpf-intro's Issues

Failed to compile on Zig 0.8.1

~/code/zig-bpf-intro> zig version
0.8.1
~/code/zig-bpf-intro> zig build
error: FileNotFound
probe...The following command exited with error code 1:
/home/tw/Downloads/zig-linux-x86_64-0.8.1/zig build-obj /home/tw/code/zig-bpf-intro/src/probe.zig -OReleaseFast --cache-dir /home/tw/code/zig-bpf-intro/zig-cache --global-cache-dir /tmp/.cache/zig --name probe -target bpfel-freestanding
--pkg-begin bpf /home/tw/code/zig-bpf-intro/libs/bpf/exports.zig --pkg-end --enable-cache
error: the following build command failed with exit code 1:
/home/tw/code/zig-bpf-intro/zig-cache/o/4d008e93f2cfe568ad49f582b1998ce0/build /home/tw/Downloads/zig-linux-x86_64-0.8.1/zig /home/tw/code/zig-bpf-intro /home/tw/code/zig-bpf-intro/zig-cache /tmp/.cache/zig

Although we need some minor fix to support 0.8.1, following is my changes to build.zig and libs/bpf:

diff --git a/build.zig b/build.zig
index 47b14a5..e5f7003 100644
--- a/build.zig
+++ b/build.zig
@@ -13,7 +13,7 @@ pub fn build(b: *Builder) void {

     const obj = b.addObject("probe", "src/probe.zig");
     obj.setTarget(std.zig.CrossTarget{
-        .cpu_arch = switch ((target.cpu_arch orelse builtin.arch).endian()) {
+        .cpu_arch = switch ((target.cpu_arch orelse builtin.cpu.arch).endian()) {
             .Big => .bpfeb,
             .Little => .bpfel,
         },
diff --git a/exports.zig b/exports.zig
index b65c685..5f0436f 100644
--- a/exports.zig
+++ b/exports.zig
@@ -1,6 +1,6 @@
 const builtin = @import("builtin");

-pub usingnamespace switch (builtin.arch) {
+pub usingnamespace switch (builtin.cpu.arch) {
     .bpfel, .bpfeb => @import("src/kern.zig"),
     else => @import("src/user.zig"),
 };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.