profile_bee_aya/bpf.rs
1use std::{
2 borrow::Cow,
3 collections::{HashMap, HashSet},
4 fs, io, iter,
5 os::fd::{AsFd as _, AsRawFd as _},
6 path::{Path, PathBuf},
7 sync::{Arc, LazyLock},
8};
9
10use aya_obj::{
11 EbpfSectionKind, Features, Object, ParseError, ProgramSection,
12 btf::{Btf, BtfError, BtfFeatures, BtfRelocationError},
13 generated::{BPF_F_SLEEPABLE, BPF_F_XDP_HAS_FRAGS, bpf_map_type},
14 relocation::EbpfRelocationError,
15};
16use log::{debug, warn};
17use thiserror::Error;
18
19use crate::{
20 maps::{Map, MapData, MapError},
21 programs::{
22 BtfTracePoint, CgroupDevice, CgroupSkb, CgroupSkbAttachType, CgroupSock, CgroupSockAddr,
23 CgroupSockopt, CgroupSysctl, Extension, FEntry, FExit, FlowDissector, Iter, KProbe,
24 LircMode2, Lsm, LsmCgroup, PerfEvent, ProbeKind, Program, ProgramData, ProgramError,
25 RawTracePoint, SchedClassifier, SkLookup, SkMsg, SkSkb, SkSkbKind, SockOps, SocketFilter,
26 TracePoint, UProbe, Xdp,
27 },
28 sys::{
29 bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
30 is_btf_datasec_supported, is_btf_datasec_zero_supported, is_btf_decl_tag_supported,
31 is_btf_enum64_supported, is_btf_float_supported, is_btf_func_global_supported,
32 is_btf_func_supported, is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported,
33 is_probe_read_kernel_supported, is_prog_id_supported, is_prog_name_supported,
34 retry_with_verifier_logs,
35 },
36 util::{bytes_of, bytes_of_slice, nr_cpus, page_size},
37};
38
39/// Marker trait for types that can safely be converted to and from byte slices.
40///
41/// # Safety
42///
43/// This trait is unsafe because it allows for the conversion of types to and
44/// from byte slices.
45pub unsafe trait Pod: Copy + 'static {}
46
47macro_rules! unsafe_impl_pod {
48 ($($struct_name:ident),+ $(,)?) => {
49 $(
50 unsafe impl Pod for $struct_name { }
51 )+
52 }
53}
54
55unsafe_impl_pod!(i8, u8, i16, u16, i32, u32, i64, u64, u128, i128);
56
57// It only makes sense that an array of POD types is itself POD
58unsafe impl<T: Pod, const N: usize> Pod for [T; N] {}
59
60pub use aya_obj::maps::{PinningType, bpf_map_def};
61
62pub(crate) static FEATURES: LazyLock<Features> = LazyLock::new(detect_features);
63
64fn detect_features() -> Features {
65 let btf = is_btf_supported().then(|| {
66 BtfFeatures::new(
67 is_btf_func_supported(),
68 is_btf_func_global_supported(),
69 is_btf_datasec_supported(),
70 is_btf_datasec_zero_supported(),
71 is_btf_float_supported(),
72 is_btf_decl_tag_supported(),
73 is_btf_type_tag_supported(),
74 is_btf_enum64_supported(),
75 )
76 });
77 let f = Features::new(
78 is_prog_name_supported(),
79 is_probe_read_kernel_supported(),
80 is_perf_link_supported(),
81 is_bpf_global_data_supported(),
82 is_bpf_cookie_supported(),
83 is_prog_id_supported(bpf_map_type::BPF_MAP_TYPE_CPUMAP),
84 is_prog_id_supported(bpf_map_type::BPF_MAP_TYPE_DEVMAP),
85 btf,
86 );
87 debug!("BPF Feature Detection: {f:#?}");
88 f
89}
90
91/// Returns a reference to the detected BPF features.
92pub fn features() -> &'static Features {
93 &FEATURES
94}
95
96/// Builder style API for advanced loading of eBPF programs.
97///
98/// Loading eBPF code involves a few steps, including loading maps and applying
99/// relocations. You can use `EbpfLoader` to customize some of the loading
100/// options.
101///
102/// # Examples
103///
104/// ```no_run
105/// use aya::{EbpfLoader, Btf};
106/// use std::fs;
107///
108/// let bpf = EbpfLoader::new()
109/// // load the BTF data from /sys/kernel/btf/vmlinux
110/// .btf(Btf::from_sys_fs().ok().as_ref())
111/// // load pinned maps from /sys/fs/bpf/my-program
112/// .default_map_pin_directory("/sys/fs/bpf/my-program")
113/// // finally load the code
114/// .load_file("file.o")?;
115/// # Ok::<(), aya::EbpfError>(())
116/// ```
117#[derive(Debug)]
118pub struct EbpfLoader<'a> {
119 btf: Option<Cow<'a, Btf>>,
120 default_map_pin_directory: Option<PathBuf>,
121 globals: HashMap<&'a str, (&'a [u8], bool)>,
122 // Max entries overrides the max_entries field of the map that matches the provided name
123 // before the map is created.
124 max_entries: HashMap<&'a str, u32>,
125 // Map pin path overrides the pin path of the map that matches the provided name before
126 // it is created.
127 map_pin_path_by_name: HashMap<&'a str, Cow<'a, Path>>,
128
129 extensions: HashSet<&'a str>,
130 verifier_log_level: VerifierLogLevel,
131 allow_unsupported_maps: bool,
132}
133
134/// Builder style API for advanced loading of eBPF programs.
135#[deprecated(since = "0.13.0", note = "use `EbpfLoader` instead")]
136pub type BpfLoader<'a> = EbpfLoader<'a>;
137
138bitflags::bitflags! {
139 /// Used to set the verifier log level flags in [EbpfLoader](EbpfLoader::verifier_log_level()).
140 #[derive(Clone, Copy, Debug)]
141 pub struct VerifierLogLevel: u32 {
142 /// Sets no verifier logging.
143 const DISABLE = 0;
144 /// Enables debug verifier logging.
145 const DEBUG = 1;
146 /// Enables verbose verifier logging.
147 const VERBOSE = 2 | Self::DEBUG.bits();
148 /// Enables verifier stats.
149 const STATS = 4;
150 }
151}
152
153impl Default for VerifierLogLevel {
154 fn default() -> Self {
155 Self::DEBUG | Self::STATS
156 }
157}
158
159impl<'a> EbpfLoader<'a> {
160 /// Creates a new loader instance.
161 pub fn new() -> Self {
162 Self {
163 btf: Btf::from_sys_fs().ok().map(Cow::Owned),
164 default_map_pin_directory: None,
165 globals: HashMap::new(),
166 max_entries: HashMap::new(),
167 map_pin_path_by_name: HashMap::new(),
168 extensions: HashSet::new(),
169 verifier_log_level: VerifierLogLevel::default(),
170 allow_unsupported_maps: false,
171 }
172 }
173
174 /// Sets the target [BTF](Btf) info.
175 ///
176 /// The loader defaults to loading `BTF` info using [`Btf::from_sys_fs`].
177 /// Use this method if you want to load `BTF` from a custom location or
178 /// pass `None` to disable `BTF` relocations entirely.
179 /// # Example
180 ///
181 /// ```no_run
182 /// use aya::{EbpfLoader, Btf, Endianness};
183 ///
184 /// let bpf = EbpfLoader::new()
185 /// // load the BTF data from a custom location
186 /// .btf(Btf::parse_file("/custom_btf_file", Endianness::default()).ok().as_ref())
187 /// .load_file("file.o")?;
188 ///
189 /// # Ok::<(), aya::EbpfError>(())
190 /// ```
191 pub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut Self {
192 self.btf = btf.map(Cow::Borrowed);
193 self
194 }
195
196 /// Allows programs containing unsupported maps to be loaded.
197 ///
198 /// By default programs containing unsupported maps will fail to load. This
199 /// method can be used to configure the loader so that unsupported maps will
200 /// be loaded, but won't be accessible from userspace. Can be useful when
201 /// using unsupported maps that are only accessed from eBPF code and don't
202 /// require any userspace interaction.
203 ///
204 /// # Example
205 ///
206 /// ```no_run
207 /// use aya::EbpfLoader;
208 ///
209 /// let bpf = EbpfLoader::new()
210 /// .allow_unsupported_maps()
211 /// .load_file("file.o")?;
212 /// # Ok::<(), aya::EbpfError>(())
213 /// ```
214 ///
215 pub const fn allow_unsupported_maps(&mut self) -> &mut Self {
216 self.allow_unsupported_maps = true;
217 self
218 }
219
220 /// Sets the base directory path for pinned maps.
221 ///
222 /// Pinned maps will be loaded from `path/MAP_NAME`.
223 /// The caller is responsible for ensuring the directory exists.
224 ///
225 /// Note that if a path is provided for a specific map via [`EbpfLoader::map_pin_path`],
226 /// it will take precedence over this path.
227 ///
228 /// # Example
229 ///
230 /// ```no_run
231 /// use aya::EbpfLoader;
232 ///
233 /// let bpf = EbpfLoader::new()
234 /// .default_map_pin_directory("/sys/fs/bpf/my-program")
235 /// .load_file("file.o")?;
236 /// # Ok::<(), aya::EbpfError>(())
237 /// ```
238 ///
239 pub fn default_map_pin_directory<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
240 self.default_map_pin_directory = Some(path.as_ref().to_owned());
241 self
242 }
243
244 /// Override the value of a global variable.
245 ///
246 /// If the `must_exist` argument is `true`, [`EbpfLoader::load`] will fail with [`ParseError::SymbolNotFound`] if the loaded object code does not contain the variable.
247 ///
248 /// From Rust eBPF, a global variable can be defined using `Global` - please refer to the `aya-ebpf` documentation.
249 ///
250 /// The type of a global variable must be `Pod` (plain old data), for instance `u8`, `u32` and
251 /// all other primitive types. You may use custom types as well, but you must ensure that those
252 /// types are `#[repr(C)]` and only contain other `Pod` types.
253 ///
254 /// The type used here and in the eBPF must be the same (or have a compatible layout).
255 ///
256 /// From C eBPF, you would annotate a global variable as `volatile const`.
257 ///
258 /// # Example
259 ///
260 /// ```no_run
261 /// use aya::EbpfLoader;
262 ///
263 /// let bpf = EbpfLoader::new()
264 /// .override_global("VERSION", &2, true)
265 /// .override_global("PIDS", &[1234u16, 5678], true)
266 /// .load_file("file.o")?;
267 /// # Ok::<(), aya::EbpfError>(())
268 /// ```
269 ///
270 pub fn override_global<T: Into<GlobalData<'a>>>(
271 &mut self,
272 name: &'a str,
273 value: T,
274 must_exist: bool,
275 ) -> &mut Self {
276 self.globals.insert(name, (value.into().bytes, must_exist));
277 self
278 }
279
280 /// Override the value of a global variable.
281 #[deprecated(since = "0.13.2", note = "please use `override_global` instead")]
282 pub fn set_global<T: Into<GlobalData<'a>>>(
283 &mut self,
284 name: &'a str,
285 value: T,
286 must_exist: bool,
287 ) -> &mut Self {
288 self.override_global(name, value, must_exist)
289 }
290
291 /// Set the `max_entries` for specified map.
292 ///
293 /// Overwrite the value of `max_entries` of the map that matches
294 /// the provided name before the map is created.
295 ///
296 /// # Example
297 ///
298 /// ```no_run
299 /// use aya::EbpfLoader;
300 ///
301 /// let bpf = EbpfLoader::new()
302 /// .map_max_entries("map", 64)
303 /// .load_file("file.o")?;
304 /// # Ok::<(), aya::EbpfError>(())
305 /// ```
306 ///
307 pub fn map_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self {
308 self.max_entries.insert(name, size);
309 self
310 }
311
312 /// Set the `max_entries` for specified map.
313 #[deprecated(since = "0.13.2", note = "please use `map_max_entries` instead")]
314 pub fn set_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self {
315 self.map_max_entries(name, size)
316 }
317
318 /// Set the pin path for the map that matches the provided name.
319 ///
320 /// Note that this is an absolute path to the pinned map; it is not a prefix
321 /// to be combined with the map name, and it is not relative to the
322 /// configured base directory for pinned maps.
323 ///
324 /// Each call to this function with the same name overwrites the path to the
325 /// pinned map; last one wins.
326 ///
327 /// # Example
328 ///
329 /// ```no_run
330 /// # use std::path::Path;
331 ///
332 /// # let mut loader = aya::EbpfLoader::new();
333 /// # let pin_path = Path::new("/sys/fs/bpf/my-pinned-map");
334 /// let bpf = loader
335 /// .map_pin_path("map", pin_path)
336 /// .load_file("file.o")?;
337 /// # Ok::<(), aya::EbpfError>(())
338 /// ```
339 ///
340 pub fn map_pin_path<P: Into<Cow<'a, Path>>>(&mut self, name: &'a str, path: P) -> &mut Self {
341 self.map_pin_path_by_name.insert(name, path.into());
342 self
343 }
344
345 /// Treat the provided program as an [`Extension`]
346 ///
347 /// When attempting to load the program with the provided `name`
348 /// the program type is forced to be ] [`Extension`] and is not
349 /// inferred from the ELF section name.
350 ///
351 /// # Example
352 ///
353 /// ```no_run
354 /// use aya::EbpfLoader;
355 ///
356 /// let bpf = EbpfLoader::new()
357 /// .extension("myfunc")
358 /// .load_file("file.o")?;
359 /// # Ok::<(), aya::EbpfError>(())
360 /// ```
361 ///
362 pub fn extension(&mut self, name: &'a str) -> &mut Self {
363 self.extensions.insert(name);
364 self
365 }
366
367 /// Sets BPF verifier log level.
368 ///
369 /// # Example
370 ///
371 /// ```no_run
372 /// use aya::{EbpfLoader, VerifierLogLevel};
373 ///
374 /// let bpf = EbpfLoader::new()
375 /// .verifier_log_level(VerifierLogLevel::VERBOSE | VerifierLogLevel::STATS)
376 /// .load_file("file.o")?;
377 /// # Ok::<(), aya::EbpfError>(())
378 /// ```
379 ///
380 pub const fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut Self {
381 self.verifier_log_level = level;
382 self
383 }
384
385 /// Loads eBPF bytecode from a file.
386 ///
387 /// # Examples
388 ///
389 /// ```no_run
390 /// use aya::EbpfLoader;
391 ///
392 /// let bpf = EbpfLoader::new().load_file("file.o")?;
393 /// # Ok::<(), aya::EbpfError>(())
394 /// ```
395 pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> Result<Ebpf, EbpfError> {
396 let path = path.as_ref();
397 self.load(&fs::read(path).map_err(|error| EbpfError::FileError {
398 path: path.to_owned(),
399 error,
400 })?)
401 }
402
403 /// Loads eBPF bytecode from a buffer.
404 ///
405 /// The buffer needs to be 4-bytes aligned. If you are bundling the bytecode statically
406 /// into your binary, it is recommended that you do so using
407 /// [`include_bytes_aligned`](crate::include_bytes_aligned).
408 ///
409 /// # Examples
410 ///
411 /// ```no_run
412 /// use aya::EbpfLoader;
413 /// use std::fs;
414 ///
415 /// let data = fs::read("file.o").unwrap();
416 /// let bpf = EbpfLoader::new().load(&data)?;
417 /// # Ok::<(), aya::EbpfError>(())
418 /// ```
419 pub fn load(&mut self, data: &[u8]) -> Result<Ebpf, EbpfError> {
420 let Self {
421 btf,
422 default_map_pin_directory,
423 globals,
424 max_entries,
425 extensions,
426 verifier_log_level,
427 allow_unsupported_maps,
428 map_pin_path_by_name,
429 } = self;
430 let mut obj = Object::parse(data)?;
431 obj.patch_map_data(globals.clone())?;
432
433 let btf_fd = if let Some(features) = &FEATURES.btf() {
434 if let Some(btf) = obj.fixup_and_sanitize_btf(features)? {
435 match load_btf(btf.to_bytes(), *verifier_log_level) {
436 Ok(btf_fd) => Some(Arc::new(btf_fd)),
437 // Only report an error here if the BTF is truly needed, otherwise proceed without.
438 Err(err) => {
439 for program in obj.programs.values() {
440 match program.section {
441 ProgramSection::Extension
442 | ProgramSection::FEntry { sleepable: _ }
443 | ProgramSection::FExit { sleepable: _ }
444 | ProgramSection::Lsm { sleepable: _ }
445 | ProgramSection::LsmCgroup
446 | ProgramSection::BtfTracePoint
447 | ProgramSection::Iter { sleepable: _ } => {
448 return Err(EbpfError::BtfError(err));
449 }
450 ProgramSection::KRetProbe
451 | ProgramSection::KProbe
452 | ProgramSection::UProbe { sleepable: _ }
453 | ProgramSection::URetProbe { sleepable: _ }
454 | ProgramSection::TracePoint
455 | ProgramSection::SocketFilter
456 | ProgramSection::Xdp {
457 frags: _,
458 attach_type: _,
459 }
460 | ProgramSection::SkMsg
461 | ProgramSection::SkSkbStreamParser
462 | ProgramSection::SkSkbStreamVerdict
463 | ProgramSection::SockOps
464 | ProgramSection::SchedClassifier
465 | ProgramSection::CgroupSkb
466 | ProgramSection::CgroupSkbIngress
467 | ProgramSection::CgroupSkbEgress
468 | ProgramSection::CgroupSockAddr { attach_type: _ }
469 | ProgramSection::CgroupSysctl
470 | ProgramSection::CgroupSockopt { attach_type: _ }
471 | ProgramSection::LircMode2
472 | ProgramSection::PerfEvent
473 | ProgramSection::RawTracePoint
474 | ProgramSection::SkLookup
475 | ProgramSection::FlowDissector
476 | ProgramSection::CgroupSock { attach_type: _ }
477 | ProgramSection::CgroupDevice => {}
478 }
479 }
480
481 if obj.has_btf_relocations() {
482 return Err(EbpfError::BtfError(err));
483 }
484
485 warn!("object BTF couldn't be loaded in the kernel: {err}");
486
487 None
488 }
489 }
490 } else {
491 None
492 }
493 } else {
494 None
495 };
496
497 if let Some(btf) = &btf {
498 obj.relocate_btf(btf)?;
499 }
500
501 const fn is_map_of_maps(map_type: bpf_map_type) -> bool {
502 matches!(
503 map_type,
504 bpf_map_type::BPF_MAP_TYPE_ARRAY_OF_MAPS | bpf_map_type::BPF_MAP_TYPE_HASH_OF_MAPS
505 )
506 }
507
508 // The kernel requires inner_map_fd when creating map-of-maps, so inner
509 // maps must be created first. Partition into regular maps and map-of-maps.
510 let mut regular_maps: Vec<(String, aya_obj::Map)> = Vec::new();
511 let mut maps_of_maps: Vec<(String, aya_obj::Map)> = Vec::new();
512
513 for (name, map_obj) in obj.maps.drain() {
514 if let (false, EbpfSectionKind::Bss | EbpfSectionKind::Data | EbpfSectionKind::Rodata) =
515 (FEATURES.bpf_global_data(), map_obj.section_kind())
516 {
517 continue;
518 }
519 let map_type: bpf_map_type = map_obj.map_type().try_into().map_err(MapError::from)?;
520 if is_map_of_maps(map_type) {
521 maps_of_maps.push((name, map_obj));
522 } else {
523 regular_maps.push((name, map_obj));
524 }
525 }
526
527 let mut maps: HashMap<String, MapData> = HashMap::new();
528
529 // Regular maps first, so they're available as inner maps below.
530 for ((name, mut map_obj), is_map_of_maps) in regular_maps
531 .into_iter()
532 .zip(iter::repeat(false))
533 .chain(maps_of_maps.into_iter().zip(iter::repeat(true)))
534 {
535 let num_cpus = || {
536 Ok(nr_cpus().map_err(|(path, error)| EbpfError::FileError {
537 path: PathBuf::from(path),
538 error,
539 })? as u32)
540 };
541 let map_type: bpf_map_type = map_obj.map_type().try_into().map_err(MapError::from)?;
542 if let Some(max_entries_val) = max_entries_override(
543 map_type,
544 max_entries.get(name.as_str()).copied(),
545 || map_obj.max_entries(),
546 num_cpus,
547 || page_size() as u32,
548 )? {
549 map_obj.set_max_entries(max_entries_val)
550 }
551 if let Some(value_size) = value_size_override(map_type) {
552 map_obj.set_value_size(value_size)
553 }
554
555 let btf_fd = btf_fd.as_deref().map(|fd| fd.as_fd());
556
557 // Defer inner map creation to avoid a BPF_MAP_CREATE when the outer map is already pinned.
558 let inner_map_obj = if is_map_of_maps {
559 Some(map_obj.inner().ok_or_else(|| {
560 EbpfError::MapError(MapError::MissingInnerMapDefinition {
561 outer_name: name.clone(),
562 })
563 })?)
564 } else {
565 None
566 };
567 let mut map = if let Some(pin_path) = map_pin_path_by_name.get(name.as_str()) {
568 MapData::create_pinned_by_name(pin_path, map_obj, &name, btf_fd, inner_map_obj)?
569 } else {
570 match map_obj.pinning() {
571 PinningType::None => {
572 let btf_inner_map;
573 let inner_map_fd = if let Some(inner) = inner_map_obj {
574 btf_inner_map =
575 MapData::create(inner, &format!("{name}.inner"), btf_fd)?;
576 Some(btf_inner_map.fd().as_fd())
577 } else {
578 None
579 };
580 MapData::create_with_inner_map_fd(map_obj, &name, btf_fd, inner_map_fd)?
581 }
582 PinningType::ByName => {
583 // pin maps in /sys/fs/bpf by default to align with libbpf
584 // behavior https://github.com/libbpf/libbpf/blob/v1.2.2/src/libbpf.c#L2161.
585 let path = default_map_pin_directory
586 .as_deref()
587 .unwrap_or_else(|| Path::new("/sys/fs/bpf"));
588 let path = path.join(&name);
589
590 MapData::create_pinned_by_name(path, map_obj, &name, btf_fd, inner_map_obj)?
591 }
592 }
593 };
594 map.finalize()?;
595 maps.insert(name, map);
596 }
597
598 let text_sections = obj
599 .functions
600 .keys()
601 .map(|(section_index, _)| *section_index)
602 .collect();
603
604 obj.relocate_maps(
605 maps.iter()
606 .map(|(s, data)| (s.as_str(), data.fd().as_fd().as_raw_fd(), data.obj())),
607 &text_sections,
608 )?;
609 obj.relocate_calls(&text_sections)?;
610 obj.sanitize_functions(&FEATURES);
611
612 let programs = obj
613 .programs
614 .drain()
615 .map(|(name, prog_obj)| {
616 let function_obj = obj.functions[&prog_obj.function_key()].clone();
617
618 let prog_name = FEATURES.bpf_name().then(|| name.clone().into());
619 let section = prog_obj.section.clone();
620 let obj = (prog_obj, function_obj);
621
622 let btf_fd = btf_fd.as_ref().map(Arc::clone);
623 let program = if extensions.contains(name.as_str()) {
624 Program::Extension(Extension {
625 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
626 })
627 } else {
628 match §ion {
629 ProgramSection::KProbe => Program::KProbe(KProbe {
630 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
631 kind: ProbeKind::Entry,
632 }),
633 ProgramSection::KRetProbe => Program::KProbe(KProbe {
634 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
635 kind: ProbeKind::Return,
636 }),
637 ProgramSection::UProbe { sleepable } => {
638 let mut data =
639 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
640 if *sleepable {
641 data.flags = BPF_F_SLEEPABLE;
642 }
643 Program::UProbe(UProbe {
644 data,
645 kind: ProbeKind::Entry,
646 })
647 }
648 ProgramSection::URetProbe { sleepable } => {
649 let mut data =
650 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
651 if *sleepable {
652 data.flags = BPF_F_SLEEPABLE;
653 }
654 Program::UProbe(UProbe {
655 data,
656 kind: ProbeKind::Return,
657 })
658 }
659 ProgramSection::TracePoint => Program::TracePoint(TracePoint {
660 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
661 }),
662 ProgramSection::SocketFilter => Program::SocketFilter(SocketFilter {
663 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
664 }),
665 ProgramSection::Xdp {
666 frags, attach_type, ..
667 } => {
668 let mut data =
669 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
670 if *frags {
671 data.flags = BPF_F_XDP_HAS_FRAGS;
672 }
673 Program::Xdp(Xdp {
674 data,
675 attach_type: *attach_type,
676 })
677 }
678 ProgramSection::SkMsg => Program::SkMsg(SkMsg {
679 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
680 }),
681 ProgramSection::CgroupSysctl => Program::CgroupSysctl(CgroupSysctl {
682 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
683 }),
684 ProgramSection::CgroupSockopt { attach_type, .. } => {
685 Program::CgroupSockopt(CgroupSockopt {
686 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
687 attach_type: *attach_type,
688 })
689 }
690 ProgramSection::SkSkbStreamParser => Program::SkSkb(SkSkb {
691 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
692 kind: SkSkbKind::StreamParser,
693 }),
694 ProgramSection::SkSkbStreamVerdict => Program::SkSkb(SkSkb {
695 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
696 kind: SkSkbKind::StreamVerdict,
697 }),
698 ProgramSection::SockOps => Program::SockOps(SockOps {
699 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
700 }),
701 ProgramSection::SchedClassifier => {
702 Program::SchedClassifier(SchedClassifier {
703 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
704 })
705 }
706 ProgramSection::CgroupSkb => Program::CgroupSkb(CgroupSkb {
707 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
708 attach_type: None,
709 }),
710 ProgramSection::CgroupSkbIngress => Program::CgroupSkb(CgroupSkb {
711 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
712 attach_type: Some(CgroupSkbAttachType::Ingress),
713 }),
714 ProgramSection::CgroupSkbEgress => Program::CgroupSkb(CgroupSkb {
715 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
716 attach_type: Some(CgroupSkbAttachType::Egress),
717 }),
718 ProgramSection::CgroupSockAddr { attach_type, .. } => {
719 Program::CgroupSockAddr(CgroupSockAddr {
720 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
721 attach_type: *attach_type,
722 })
723 }
724 ProgramSection::LircMode2 => Program::LircMode2(LircMode2 {
725 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
726 }),
727 ProgramSection::PerfEvent => Program::PerfEvent(PerfEvent {
728 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
729 }),
730 ProgramSection::RawTracePoint => Program::RawTracePoint(RawTracePoint {
731 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
732 }),
733 ProgramSection::Lsm { sleepable } => {
734 let mut data =
735 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
736 if *sleepable {
737 data.flags = BPF_F_SLEEPABLE;
738 }
739 Program::Lsm(Lsm { data })
740 }
741 ProgramSection::LsmCgroup => Program::LsmCgroup(LsmCgroup {
742 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
743 }),
744 ProgramSection::BtfTracePoint => Program::BtfTracePoint(BtfTracePoint {
745 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
746 }),
747 ProgramSection::FEntry { sleepable } => {
748 let mut data =
749 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
750 if *sleepable {
751 data.flags = BPF_F_SLEEPABLE;
752 }
753 Program::FEntry(FEntry { data })
754 }
755 ProgramSection::FExit { sleepable } => {
756 let mut data =
757 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
758 if *sleepable {
759 data.flags = BPF_F_SLEEPABLE;
760 }
761 Program::FExit(FExit { data })
762 }
763 ProgramSection::FlowDissector => Program::FlowDissector(FlowDissector {
764 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
765 }),
766 ProgramSection::Extension => Program::Extension(Extension {
767 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
768 }),
769 ProgramSection::SkLookup => Program::SkLookup(SkLookup {
770 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
771 }),
772 ProgramSection::CgroupSock { attach_type, .. } => {
773 Program::CgroupSock(CgroupSock {
774 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
775 attach_type: *attach_type,
776 })
777 }
778 ProgramSection::CgroupDevice => Program::CgroupDevice(CgroupDevice {
779 data: ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level),
780 }),
781 ProgramSection::Iter { sleepable } => {
782 let mut data =
783 ProgramData::new(prog_name, obj, btf_fd, *verifier_log_level);
784 if *sleepable {
785 data.flags = BPF_F_SLEEPABLE;
786 }
787 Program::Iter(Iter { data })
788 }
789 }
790 };
791 (name, program)
792 })
793 .collect();
794 let maps = maps
795 .drain()
796 .map(|data| parse_map(data, *allow_unsupported_maps))
797 .collect::<Result<HashMap<String, Map>, EbpfError>>()?;
798
799 Ok(Ebpf { maps, programs })
800 }
801}
802
803fn parse_map(
804 data: (String, MapData),
805 allow_unsupported_maps: bool,
806) -> Result<(String, Map), EbpfError> {
807 let (name, map) = data;
808 let map_type = bpf_map_type::try_from(map.obj().map_type()).map_err(MapError::from)?;
809 let map = match map_type {
810 bpf_map_type::BPF_MAP_TYPE_ARRAY => Map::Array(map),
811 bpf_map_type::BPF_MAP_TYPE_PERCPU_ARRAY => Map::PerCpuArray(map),
812 bpf_map_type::BPF_MAP_TYPE_PROG_ARRAY => Map::ProgramArray(map),
813 bpf_map_type::BPF_MAP_TYPE_HASH => Map::HashMap(map),
814 bpf_map_type::BPF_MAP_TYPE_LRU_HASH => Map::LruHashMap(map),
815 bpf_map_type::BPF_MAP_TYPE_PERCPU_HASH => Map::PerCpuHashMap(map),
816 bpf_map_type::BPF_MAP_TYPE_LRU_PERCPU_HASH => Map::PerCpuLruHashMap(map),
817 bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY => Map::PerfEventArray(map),
818 bpf_map_type::BPF_MAP_TYPE_RINGBUF => Map::RingBuf(map),
819 bpf_map_type::BPF_MAP_TYPE_SOCKHASH => Map::SockHash(map),
820 bpf_map_type::BPF_MAP_TYPE_SOCKMAP => Map::SockMap(map),
821 bpf_map_type::BPF_MAP_TYPE_BLOOM_FILTER => Map::BloomFilter(map),
822 bpf_map_type::BPF_MAP_TYPE_LPM_TRIE => Map::LpmTrie(map),
823 bpf_map_type::BPF_MAP_TYPE_STACK => Map::Stack(map),
824 bpf_map_type::BPF_MAP_TYPE_STACK_TRACE => Map::StackTraceMap(map),
825 bpf_map_type::BPF_MAP_TYPE_QUEUE => Map::Queue(map),
826 bpf_map_type::BPF_MAP_TYPE_CPUMAP => Map::CpuMap(map),
827 bpf_map_type::BPF_MAP_TYPE_DEVMAP => Map::DevMap(map),
828 bpf_map_type::BPF_MAP_TYPE_DEVMAP_HASH => Map::DevMapHash(map),
829 bpf_map_type::BPF_MAP_TYPE_XSKMAP => Map::XskMap(map),
830 bpf_map_type::BPF_MAP_TYPE_SK_STORAGE => Map::SkStorage(map),
831 bpf_map_type::BPF_MAP_TYPE_ARRAY_OF_MAPS => Map::ArrayOfMaps(map),
832 bpf_map_type::BPF_MAP_TYPE_HASH_OF_MAPS => Map::HashOfMaps(map),
833 m_type => {
834 if allow_unsupported_maps {
835 Map::Unsupported(map)
836 } else {
837 return Err(EbpfError::MapError(MapError::Unsupported {
838 name,
839 map_type: m_type,
840 }));
841 }
842 }
843 };
844
845 Ok((name, map))
846}
847
848/// Computes the value which should be used to override the `max_entries` value of the map
849/// based on the user-provided override and the rules for that map type.
850fn max_entries_override(
851 map_type: bpf_map_type,
852 user_override: Option<u32>,
853 current_value: impl Fn() -> u32,
854 num_cpus: impl Fn() -> Result<u32, EbpfError>,
855 page_size: impl Fn() -> u32,
856) -> Result<Option<u32>, EbpfError> {
857 let max_entries = || user_override.unwrap_or_else(¤t_value);
858 Ok(match map_type {
859 bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY if max_entries() == 0 => Some(num_cpus()?),
860 bpf_map_type::BPF_MAP_TYPE_RINGBUF => Some(adjust_to_page_size(max_entries(), page_size()))
861 .filter(|adjusted| *adjusted != max_entries())
862 .or(user_override),
863 _ => user_override,
864 })
865}
866
867/// Computes the value which should be used to override the `value_size` value of the map
868/// based on the rules for that map type.
869fn value_size_override(map_type: bpf_map_type) -> Option<u32> {
870 match map_type {
871 bpf_map_type::BPF_MAP_TYPE_CPUMAP => Some(if FEATURES.cpumap_prog_id() { 8 } else { 4 }),
872 bpf_map_type::BPF_MAP_TYPE_DEVMAP | bpf_map_type::BPF_MAP_TYPE_DEVMAP_HASH => {
873 Some(if FEATURES.devmap_prog_id() { 8 } else { 4 })
874 }
875 bpf_map_type::BPF_MAP_TYPE_RINGBUF => Some(0),
876 _ => None,
877 }
878}
879
880// Adjusts the byte size of a RingBuf map to match a power-of-two multiple of the page size.
881//
882// This mirrors the logic used by libbpf.
883// See https://github.com/libbpf/libbpf/blob/ec6f716eda43/src/libbpf.c#L2461-L2463
884const fn adjust_to_page_size(byte_size: u32, page_size: u32) -> u32 {
885 // If the byte_size is zero, return zero and let the verifier reject the map
886 // when it is loaded. This is the behavior of libbpf.
887 if byte_size == 0 {
888 return 0;
889 }
890 // TODO: Replace with primitive method when int_roundings (https://github.com/rust-lang/rust/issues/88581)
891 // is stabilized.
892 const fn div_ceil(n: u32, rhs: u32) -> u32 {
893 let d = n / rhs;
894 let r = n % rhs;
895 if r > 0 && rhs > 0 { d + 1 } else { d }
896 }
897 let pages_needed = div_ceil(byte_size, page_size);
898 page_size * pages_needed.next_power_of_two()
899}
900
901#[cfg(test)]
902mod tests {
903 use aya_obj::generated::bpf_map_type::*;
904
905 const PAGE_SIZE: u32 = 4096;
906 const NUM_CPUS: u32 = 4;
907
908 #[test]
909 fn test_adjust_to_page_size() {
910 use super::adjust_to_page_size;
911 for (exp, input) in [
912 (0, 0),
913 (4096, 1),
914 (4096, 4095),
915 (4096, 4096),
916 (8192, 4097),
917 (8192, 8192),
918 (16384, 8193),
919 ] {
920 assert_eq!(exp, adjust_to_page_size(input, PAGE_SIZE));
921 }
922 }
923
924 #[test]
925 fn test_max_entries_override() {
926 use super::max_entries_override;
927 for (map_type, user_override, current_value, exp) in [
928 (BPF_MAP_TYPE_RINGBUF, Some(1), 1, Some(PAGE_SIZE)),
929 (BPF_MAP_TYPE_RINGBUF, None, 1, Some(PAGE_SIZE)),
930 (BPF_MAP_TYPE_RINGBUF, None, PAGE_SIZE, None),
931 (BPF_MAP_TYPE_PERF_EVENT_ARRAY, None, 1, None),
932 (BPF_MAP_TYPE_PERF_EVENT_ARRAY, Some(42), 1, Some(42)),
933 (BPF_MAP_TYPE_PERF_EVENT_ARRAY, Some(0), 1, Some(NUM_CPUS)),
934 (BPF_MAP_TYPE_PERF_EVENT_ARRAY, None, 0, Some(NUM_CPUS)),
935 (BPF_MAP_TYPE_PERF_EVENT_ARRAY, None, 42, None),
936 (BPF_MAP_TYPE_ARRAY, None, 1, None),
937 (BPF_MAP_TYPE_ARRAY, Some(2), 1, Some(2)),
938 ] {
939 assert_eq!(
940 exp,
941 max_entries_override(
942 map_type,
943 user_override,
944 || current_value,
945 || Ok(NUM_CPUS),
946 || PAGE_SIZE,
947 )
948 .unwrap()
949 );
950 }
951 }
952}
953
954impl Default for EbpfLoader<'_> {
955 fn default() -> Self {
956 EbpfLoader::new()
957 }
958}
959
960/// The main entry point into the library, used to work with eBPF programs and maps.
961#[derive(Debug)]
962pub struct Ebpf {
963 maps: HashMap<String, Map>,
964 programs: HashMap<String, Program>,
965}
966
967/// The main entry point into the library, used to work with eBPF programs and maps.
968#[deprecated(since = "0.13.0", note = "use `Ebpf` instead")]
969pub type Bpf = Ebpf;
970
971impl Ebpf {
972 /// Loads eBPF bytecode from a file.
973 ///
974 /// Parses the given object code file and initializes the [maps](crate::maps) defined in it. If
975 /// the kernel supports [BTF](Btf) debug info, it is automatically loaded from
976 /// `/sys/kernel/btf/vmlinux`.
977 ///
978 /// For more loading options, see [`EbpfLoader`].
979 ///
980 /// # Examples
981 ///
982 /// ```no_run
983 /// use aya::Ebpf;
984 ///
985 /// let bpf = Ebpf::load_file("file.o")?;
986 /// # Ok::<(), aya::EbpfError>(())
987 /// ```
988 pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Self, EbpfError> {
989 EbpfLoader::new()
990 .btf(Btf::from_sys_fs().ok().as_ref())
991 .load_file(path)
992 }
993
994 /// Loads eBPF bytecode from a buffer.
995 ///
996 /// Parses the object code contained in `data` and initializes the
997 /// [maps](crate::maps) defined in it. If the kernel supports [BTF](Btf)
998 /// debug info, it is automatically loaded from `/sys/kernel/btf/vmlinux`.
999 ///
1000 /// The buffer needs to be 4-bytes aligned. If you are bundling the bytecode statically
1001 /// into your binary, it is recommended that you do so using
1002 /// [`include_bytes_aligned`](crate::include_bytes_aligned).
1003 ///
1004 /// For more loading options, see [`EbpfLoader`].
1005 ///
1006 /// # Examples
1007 ///
1008 /// ```no_run
1009 /// use aya::{Ebpf, Btf};
1010 /// use std::fs;
1011 ///
1012 /// let data = fs::read("file.o").unwrap();
1013 /// // load the BTF data from /sys/kernel/btf/vmlinux
1014 /// let bpf = Ebpf::load(&data)?;
1015 /// # Ok::<(), aya::EbpfError>(())
1016 /// ```
1017 pub fn load(data: &[u8]) -> Result<Self, EbpfError> {
1018 EbpfLoader::new()
1019 .btf(Btf::from_sys_fs().ok().as_ref())
1020 .load(data)
1021 }
1022
1023 /// Returns a reference to the map with the given name.
1024 ///
1025 /// The returned type is mostly opaque. In order to do anything useful with it you need to
1026 /// convert it to a [typed map](crate::maps).
1027 ///
1028 /// For more details and examples on maps and their usage, see the [maps module
1029 /// documentation][crate::maps].
1030 pub fn map(&self, name: &str) -> Option<&Map> {
1031 self.maps.get(name)
1032 }
1033
1034 /// Returns a mutable reference to the map with the given name.
1035 ///
1036 /// The returned type is mostly opaque. In order to do anything useful with it you need to
1037 /// convert it to a [typed map](crate::maps).
1038 ///
1039 /// For more details and examples on maps and their usage, see the [maps module
1040 /// documentation][crate::maps].
1041 pub fn map_mut(&mut self, name: &str) -> Option<&mut Map> {
1042 self.maps.get_mut(name)
1043 }
1044
1045 /// Takes ownership of a map with the given name.
1046 ///
1047 /// Use this when borrowing with [`map`](crate::Ebpf::map) or [`map_mut`](crate::Ebpf::map_mut)
1048 /// is not possible (eg when using the map from an async task). The returned
1049 /// map will be closed on `Drop`, therefore the caller is responsible for
1050 /// managing its lifetime.
1051 ///
1052 /// The returned type is mostly opaque. In order to do anything useful with it you need to
1053 /// convert it to a [typed map](crate::maps).
1054 ///
1055 /// For more details and examples on maps and their usage, see the [maps module
1056 /// documentation][crate::maps].
1057 pub fn take_map(&mut self, name: &str) -> Option<Map> {
1058 self.maps.remove(name)
1059 }
1060
1061 /// An iterator over all the maps.
1062 ///
1063 /// # Examples
1064 /// ```no_run
1065 /// # let mut bpf = aya::Ebpf::load(&[])?;
1066 /// for (name, map) in bpf.maps() {
1067 /// println!(
1068 /// "found map `{}`",
1069 /// name,
1070 /// );
1071 /// }
1072 /// # Ok::<(), aya::EbpfError>(())
1073 /// ```
1074 pub fn maps(&self) -> impl Iterator<Item = (&str, &Map)> {
1075 self.maps.iter().map(|(name, map)| (name.as_str(), map))
1076 }
1077
1078 /// A mutable iterator over all the maps.
1079 ///
1080 /// # Examples
1081 /// ```no_run
1082 /// # use std::path::Path;
1083 /// # #[derive(thiserror::Error, Debug)]
1084 /// # enum Error {
1085 /// # #[error(transparent)]
1086 /// # Ebpf(#[from] aya::EbpfError),
1087 /// # #[error(transparent)]
1088 /// # Pin(#[from] aya::pin::PinError)
1089 /// # }
1090 /// # let mut bpf = aya::Ebpf::load(&[])?;
1091 /// # let pin_path = Path::new("/tmp/pin_path");
1092 /// for (_, map) in bpf.maps_mut() {
1093 /// map.pin(pin_path)?;
1094 /// }
1095 /// # Ok::<(), Error>(())
1096 /// ```
1097 pub fn maps_mut(&mut self) -> impl Iterator<Item = (&str, &mut Map)> {
1098 self.maps.iter_mut().map(|(name, map)| (name.as_str(), map))
1099 }
1100
1101 /// Attempts to get mutable references to `N` maps at once.
1102 ///
1103 /// Returns an array of length `N` with the results of each query, in the same order
1104 /// as the requested map names. For soundness, at most one mutable reference will be
1105 /// returned to any map. `None` will be used if a map with the given name is missing.
1106 ///
1107 /// This method performs a check to ensure that there are no duplicate map names,
1108 /// which currently has a time-complexity of *O(n²)*. Be careful when passing a large
1109 /// number of names.
1110 ///
1111 /// # Panics
1112 ///
1113 /// Panics if any names are duplicated.
1114 ///
1115 /// # Examples
1116 /// ```no_run
1117 /// # let mut bpf = aya::Ebpf::load(&[])?;
1118 /// match bpf.maps_disjoint_mut(["MAP1", "MAP2"]) {
1119 /// [Some(m1), Some(m2)] => println!("Got MAP1 and MAP2"),
1120 /// [Some(m1), None] => println!("Got only MAP1"),
1121 /// [None, Some(m2)] => println!("Got only MAP2"),
1122 /// [None, None] => println!("No maps"),
1123 /// }
1124 /// # Ok::<(), aya::EbpfError>(())
1125 /// ```
1126 pub fn maps_disjoint_mut<const N: usize>(&mut self, names: [&str; N]) -> [Option<&mut Map>; N] {
1127 self.maps.get_disjoint_mut(names)
1128 }
1129
1130 /// Returns a reference to the program with the given name.
1131 ///
1132 /// You can use this to inspect a program and its properties. To load and attach a program, use
1133 /// [`program_mut`](Self::program_mut) instead.
1134 ///
1135 /// For more details on programs and their usage, see the [programs module
1136 /// documentation](crate::programs).
1137 ///
1138 /// # Examples
1139 ///
1140 /// ```no_run
1141 /// # let bpf = aya::Ebpf::load(&[])?;
1142 /// let program = bpf.program("SSL_read").unwrap();
1143 /// println!("program SSL_read is of type {:?}", program.prog_type());
1144 /// # Ok::<(), aya::EbpfError>(())
1145 /// ```
1146 pub fn program(&self, name: &str) -> Option<&Program> {
1147 self.programs.get(name)
1148 }
1149
1150 /// Returns a mutable reference to the program with the given name.
1151 ///
1152 /// Used to get a program before loading and attaching it. For more details on programs and
1153 /// their usage, see the [programs module documentation](crate::programs).
1154 ///
1155 /// # Examples
1156 ///
1157 /// ```no_run
1158 /// # let mut bpf = aya::Ebpf::load(&[])?;
1159 /// use aya::programs::UProbe;
1160 ///
1161 /// let program: &mut UProbe = bpf.program_mut("SSL_read").unwrap().try_into()?;
1162 /// program.load()?;
1163 /// program.attach("SSL_read", "libssl", None)?;
1164 /// # Ok::<(), aya::EbpfError>(())
1165 /// ```
1166 pub fn program_mut(&mut self, name: &str) -> Option<&mut Program> {
1167 self.programs.get_mut(name)
1168 }
1169
1170 /// An iterator over all the programs.
1171 ///
1172 /// # Examples
1173 /// ```no_run
1174 /// # let bpf = aya::Ebpf::load(&[])?;
1175 /// for (name, program) in bpf.programs() {
1176 /// println!(
1177 /// "found program `{}` of type `{:?}`",
1178 /// name,
1179 /// program.prog_type()
1180 /// );
1181 /// }
1182 /// # Ok::<(), aya::EbpfError>(())
1183 /// ```
1184 pub fn programs(&self) -> impl Iterator<Item = (&str, &Program)> {
1185 self.programs.iter().map(|(s, p)| (s.as_str(), p))
1186 }
1187
1188 /// An iterator mutably referencing all of the programs.
1189 ///
1190 /// # Examples
1191 /// ```no_run
1192 /// # use std::path::Path;
1193 /// # #[derive(thiserror::Error, Debug)]
1194 /// # enum Error {
1195 /// # #[error(transparent)]
1196 /// # Ebpf(#[from] aya::EbpfError),
1197 /// # #[error(transparent)]
1198 /// # Pin(#[from] aya::pin::PinError)
1199 /// # }
1200 /// # let mut bpf = aya::Ebpf::load(&[])?;
1201 /// # let pin_path = Path::new("/tmp/pin_path");
1202 /// for (_, program) in bpf.programs_mut() {
1203 /// program.pin(pin_path)?;
1204 /// }
1205 /// # Ok::<(), Error>(())
1206 /// ```
1207 pub fn programs_mut(&mut self) -> impl Iterator<Item = (&str, &mut Program)> {
1208 self.programs.iter_mut().map(|(s, p)| (s.as_str(), p))
1209 }
1210}
1211
1212/// The error type returned by [`Ebpf::load_file`] and [`Ebpf::load`].
1213#[derive(Debug, Error)]
1214pub enum EbpfError {
1215 /// Error loading file
1216 #[error("error loading {path}")]
1217 FileError {
1218 /// The file path
1219 path: PathBuf,
1220 #[source]
1221 /// The original [`io::Error`]
1222 error: io::Error,
1223 },
1224
1225 /// Unexpected pinning type
1226 #[error("unexpected pinning type {name}")]
1227 UnexpectedPinningType {
1228 /// The value encountered
1229 name: u32,
1230 },
1231
1232 /// Error parsing BPF object
1233 #[error("error parsing BPF object: {0}")]
1234 ParseError(#[from] ParseError),
1235
1236 /// Error parsing BTF object
1237 #[error("BTF error: {0}")]
1238 BtfError(#[from] BtfError),
1239
1240 /// Error performing relocations
1241 #[error("error relocating function")]
1242 RelocationError(#[from] EbpfRelocationError),
1243
1244 /// Error performing relocations
1245 #[error("error relocating section")]
1246 BtfRelocationError(#[from] BtfRelocationError),
1247
1248 /// No BTF parsed for object
1249 #[error("no BTF parsed for object")]
1250 NoBTF,
1251
1252 #[error("map error: {0}")]
1253 /// A map error
1254 MapError(#[from] MapError),
1255
1256 #[error("program error: {0}")]
1257 /// A program error
1258 ProgramError(#[from] ProgramError),
1259}
1260
1261/// The error type returned by [`Bpf::load_file`] and [`Bpf::load`].
1262#[deprecated(since = "0.13.0", note = "use `EbpfError` instead")]
1263pub type BpfError = EbpfError;
1264
1265fn load_btf(
1266 raw_btf: Vec<u8>,
1267 verifier_log_level: VerifierLogLevel,
1268) -> Result<crate::MockableFd, BtfError> {
1269 let (ret, verifier_log) = retry_with_verifier_logs(10, |logger| {
1270 bpf_load_btf(raw_btf.as_slice(), logger, verifier_log_level)
1271 });
1272 ret.map_err(|io_error| BtfError::LoadError {
1273 io_error,
1274 verifier_log,
1275 })
1276}
1277
1278/// Global data that can be exported to eBPF programs before they are loaded.
1279///
1280/// Valid global data includes `Pod` types and slices of `Pod` types. See also
1281/// [`EbpfLoader::override_global`].
1282pub struct GlobalData<'a> {
1283 bytes: &'a [u8],
1284}
1285
1286impl<'a, T: Pod> From<&'a [T]> for GlobalData<'a> {
1287 fn from(s: &'a [T]) -> Self {
1288 GlobalData {
1289 bytes: bytes_of_slice(s),
1290 }
1291 }
1292}
1293
1294impl<'a, T: Pod> From<&'a T> for GlobalData<'a> {
1295 fn from(v: &'a T) -> Self {
1296 GlobalData {
1297 // Safety: v is Pod
1298 bytes: bytes_of(v),
1299 }
1300 }
1301}