Docs.rs
  • bpf-script-0.5.4
    • bpf-script 0.5.4
    • Permalink
    • Docs.rs crate page
    • MIT
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • arcjustin
    • Dependencies
      • bpf-ins ^0.7 normal
      • btf ^0.5 normal
      • peginator ^0.3.0 normal
      • peginator_macro ^0.3.0 normal
      • thiserror ^1.0.37 normal
    • Versions
    • 62.82% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate bpf_script

bpf_script0.5.4

  • All Items

Sections

  • Usage
  • TODO
  • License

Crate Items

  • Modules

Crates

  • bpf_script

Crate bpf_script

Source
Expand description

Build Status crates.io mio Lines of Code

A small scripting language and compiler for creating eBPF programs at runtime without bcc or llvm.

The intent behind building this crate was to primarily learn more about BPF internals and, secondly, to provide a dependency-free way of writing BPF programs, using a higher-level language, that could be compiled at run-time without the need to shell out to a compiler and load/patch BPF from an ELF file.

The syntax for the language resembles Rust with a lot of features stripped out. For example, a simple u/k probe program that calls a helper and returns the value looks like so:

ⓘ
fn(regs: &bpf_user_pt_regs_t)
    a = get_current_uid_gid()
    map_push_elem(queue, &a, 0)
    return a

This crate is made to work together with the following crates but they are not required:

  • btf A BTF parsing library.
  • bpf-script-derive Allows you to seamlessly share types between Rust and this compiler.
  • bpf-api Creating programs, probes, maps, etc.

§Usage

use bpf_script::compiler::Compiler;
use bpf_script::types::{AddToTypeDatabase, TypeDatabase};

let mut types = TypeDatabase::default();
u32::add_to_database(&mut types).expect("Failed to add type");

let mut compiler = Compiler::create(&types);
compiler.compile(r#"
    fn(a: u32)
        return a
"#).expect("Compilation failed");

for ins in compiler.get_instructions() {
    println!("{}", ins);
}

§TODO

  • Add control flow.
  • Write more thorough tests.

§License

  • MIT license

Modules§

compiler
error
types

Results

Settings
Help
    struct
    bpf_script::types::Array
    Represents the physical properties of an array.
    enum variant
    bpf_script::types::BaseType::Array
    method
    bpf_script::types::TypeDatabase::add_array
    Convenience function for adding an array to the database.
    struct field
    bpf_script::types::Array::size
    Array -> u32
    Cached size.
    struct field
    bpf_script::types::Array::num_elements
    Array -> u32
    The number of elements in the array.
    struct field
    bpf_script::types::Array::element_type_id
    Array -> usize
    The type of element the array stores.
    method
    bpf_script::types::Array::clone
    &Array -> Array
    method
    bpf_script::types::Array::get_size
    &Array -> u32
    Returns the size of the array in bytes.
    method
    bpf_script::types::Array::fmt
    &Array, &mut Formatter -> Result
    method
    bpf_script::types::Array::default
    -> Array
    method
    bpf_script::types::Array::clone
    &Array -> Array