libbpf_rs/
lib.rs

1//! # libbpf-rs
2//!
3//! `libbpf-rs` is a safe, idiomatic, and opinionated wrapper around
4//! [libbpf](https://github.com/libbpf/libbpf/).
5//!
6//! libbpf-rs, together with `libbpf-cargo` (libbpf cargo plugin) allow you
7//! to write Compile-Once-Run-Everywhere (CO-RE) eBPF programs. Note this document
8//! uses "eBPF" and "BPF" interchangeably.
9//!
10//! More information about CO-RE is [available
11//! here](https://facebookmicrosites.github.io/bpf/blog/2020/02/19/bpf-portability-and-co-re.html).
12//!
13//! ## High level workflow
14//!
15//! 1. Create new rust project (via `cargo new` or similar) at path `$PROJ_PATH`
16//! 2. Create directory `$PROJ_PATH/src/bpf`
17//! 3. Write CO-RE bpf code in `$PROJ_PATH/src/bpf/${MYFILE}.bpf.c`, where `$MYFILE` may be any
18//!    valid filename. Note the `.bpf.c` extension is required.
19//! 4. Create a [build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html) that
20//!    builds and generates a skeleton module using `libbpf_cargo::SkeletonBuilder`
21//! 5. Write your userspace code by importing and using the generated module. Import the
22//!    module by using the [path
23//!    attribute](https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute).
24//!    Your userspace code goes in `$PROJ_PATH/src/` as it would in a normal rust project.
25//! 6. Continue regular rust workflow (ie `cargo build`, `cargo run`, etc)
26//!
27//! ## Alternate workflow
28//!
29//! While using the skeleton is recommended, it is also possible to directly use libbpf-rs.
30//!
31//! 1. Follow steps 1-3 of "High level workflow"
32//! 2. Generate a BPF object file. Options include manually invoking `clang`, creating a build
33//!    script to invoke `clang`, or using `libbpf-cargo` cargo plugins.
34//! 3. Write your userspace code in `$PROJ_PATH/src/` as you would a normal rust project and point
35//!    libbpf-rs at your BPF object file
36//! 4. Continue regular rust workflow (ie `cargo build`, `cargo run`, etc)
37//!
38//! ## Design
39//!
40//! libbpf-rs models various "phases":
41//! ```text
42//!                from_*()        load()
43//!                  |               |
44//!                  v               v
45//!    ObjectBuilder ->  OpenObject  -> Object
46//!                          ^            ^
47//!                          |            |
48//!              <pre-load modifications> |
49//!                                       |
50//!                            <post-load interactions>
51//! ```
52//!
53//! The entry point into libbpf-rs is [`ObjectBuilder`]. `ObjectBuilder` helps open the BPF object
54//! file. After the object file is opened, you are returned an [`OpenObject`] where you can
55//! perform all your pre-load operations. Pre-load means before any BPF maps are created or BPF
56//! programs are loaded and verified by the kernel. Finally, after the BPF object is loaded, you
57//! are returned an [`Object`] instance where you can read/write to BPF maps, attach BPF programs
58//! to hooks, etc.
59//!
60//! You _must_ keep the [`Object`] alive the entire duration you interact with anything inside the
61//! BPF object it represents. This is further documented in [`Object`] documentation.
62//!
63//! ## Example
64//!
65//! This is probably the best way to understand how libbpf-rs and libbpf-cargo work together.
66//!
67//! [See example here](https://github.com/libbpf/libbpf-rs/tree/master/examples/runqslower).
68
69#![allow(clippy::let_and_return, clippy::let_unit_value)]
70#![warn(
71    elided_lifetimes_in_paths,
72    missing_debug_implementations,
73    missing_docs,
74    single_use_lifetimes,
75    clippy::absolute_paths,
76    clippy::wildcard_imports,
77    rustdoc::broken_intra_doc_links
78)]
79#![deny(unsafe_op_in_unsafe_fn)]
80
81pub mod btf;
82mod error;
83mod iter;
84mod link;
85mod linker;
86mod map;
87mod netfilter;
88mod object;
89mod perf_buffer;
90mod print;
91mod program;
92pub mod query;
93mod ringbuf;
94mod skeleton;
95mod tc;
96mod user_ringbuf;
97mod util;
98mod xdp;
99
100pub use libbpf_sys;
101
102pub use crate::btf::Btf;
103pub use crate::btf::HasSize;
104pub use crate::btf::ReferencesType;
105pub use crate::error::Error;
106pub use crate::error::ErrorExt;
107pub use crate::error::ErrorKind;
108pub use crate::error::Result;
109pub use crate::iter::Iter;
110pub use crate::link::Link;
111pub use crate::linker::Linker;
112pub use crate::map::Map;
113pub use crate::map::MapCore;
114pub use crate::map::MapFlags;
115pub use crate::map::MapHandle;
116pub use crate::map::MapImpl;
117pub use crate::map::MapInfo;
118pub use crate::map::MapKeyIter;
119pub use crate::map::MapMut;
120pub use crate::map::MapType;
121pub use crate::map::OpenMap;
122pub use crate::map::OpenMapImpl;
123pub use crate::map::OpenMapMut;
124pub use crate::netfilter::NetfilterOpts;
125pub use crate::netfilter::NFPROTO_IPV4;
126pub use crate::netfilter::NFPROTO_IPV6;
127pub use crate::netfilter::NF_INET_FORWARD;
128pub use crate::netfilter::NF_INET_LOCAL_IN;
129pub use crate::netfilter::NF_INET_LOCAL_OUT;
130pub use crate::netfilter::NF_INET_POST_ROUTING;
131pub use crate::netfilter::NF_INET_PRE_ROUTING;
132pub use crate::object::AsRawLibbpf;
133pub use crate::object::MapIter;
134pub use crate::object::Object;
135pub use crate::object::ObjectBuilder;
136pub use crate::object::OpenObject;
137pub use crate::object::ProgIter;
138pub use crate::perf_buffer::PerfBuffer;
139pub use crate::perf_buffer::PerfBufferBuilder;
140pub use crate::print::get_print;
141pub use crate::print::set_print;
142pub use crate::print::PrintCallback;
143pub use crate::print::PrintLevel;
144pub use crate::program::Input as ProgramInput;
145pub use crate::program::OpenProgram;
146pub use crate::program::OpenProgramImpl;
147pub use crate::program::OpenProgramMut;
148pub use crate::program::Output as ProgramOutput;
149pub use crate::program::Program;
150pub use crate::program::ProgramAttachType;
151pub use crate::program::ProgramImpl;
152pub use crate::program::ProgramMut;
153pub use crate::program::ProgramType;
154pub use crate::program::TracepointOpts;
155pub use crate::program::UprobeOpts;
156pub use crate::program::UsdtOpts;
157pub use crate::ringbuf::RingBuffer;
158pub use crate::ringbuf::RingBufferBuilder;
159pub use crate::tc::TcAttachPoint;
160pub use crate::tc::TcHook;
161pub use crate::tc::TcHookBuilder;
162pub use crate::tc::TC_CUSTOM;
163pub use crate::tc::TC_EGRESS;
164pub use crate::tc::TC_H_CLSACT;
165pub use crate::tc::TC_H_INGRESS;
166pub use crate::tc::TC_H_MIN_EGRESS;
167pub use crate::tc::TC_H_MIN_INGRESS;
168pub use crate::tc::TC_INGRESS;
169pub use crate::user_ringbuf::UserRingBuffer;
170pub use crate::user_ringbuf::UserRingBufferSample;
171pub use crate::util::num_possible_cpus;
172pub use crate::xdp::Xdp;
173pub use crate::xdp::XdpFlags;
174
175/// An unconstructible dummy type used for tagging mutable type
176/// variants.
177#[doc(hidden)]
178#[derive(Copy, Clone, Debug)]
179pub enum Mut {}
180
181
182/// Used for skeleton -- an end user may not consider this API stable
183#[doc(hidden)]
184pub mod __internal_skel {
185    pub use super::skeleton::*;
186}
187
188/// Skeleton related definitions.
189pub mod skel {
190    pub use super::skeleton::OpenSkel;
191    pub use super::skeleton::Skel;
192    pub use super::skeleton::SkelBuilder;
193}