1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//!  SPDX-License-Identifier: MIT
//!
//! Copyright (c) 2023, eunomia-bpf
//! All rights reserved.
//!

//! # BpfSkeleton
//!
//! This is the main module of the bpf-loader
//!
//! It provides interfaces to load a bpf-skeleton from JSON, parse it and verify it, and receive data (ringbuf, perfevent, or map values) from it
//!
//! ## The main structure
//!
//! Three objects are provided to the user, see below.
//!
//! ### `BpfSkeletonBuilder`
//!
//! The builder of PreLoadBpfSkeleton, also the start point that the user should use
//!
//! It accepts the JSON skeleton, verify the definitions along with the BTF info in the program, and open the bpf_object
//!
//! It will build a `PreLoadBpfSkeleton`
//!
//! ### `PreLoadBpfSkeleton`
//!
//! A verified and opened bpf_object
//!
//! It has a method called `load_and_attach`, which will try to load the bpf_object, and attach the bpf programs in it to the corresponding attach points. On success, it will return a BpfSkeleton
//!
//! ### `BpfSkeleton`
//!
//! This is the most important object that the user will use.
//!
//! It provide abilities to polling data from the bpf program (through ringbuf, perfevent, or maps) in a unified interface. See `wait_and_poll_to_handler` for details.
//!
//! Besides, it provide ability to control the polling progress in another thread. You can get a handle using `create_poll_handle`, then pause/resume/terminate the polling function in another thread.
use std::{any::Any, sync::Arc};

use libbpf_rs::{Map, MapType, Object};
use log::warn;

use self::{handle::PollingHandle, preload::attach::AttachLink};
use crate::{
    btf_container::BtfContainer,
    export_event::{EventExporterBuilder, EventHandler, ExportFormatType},
    meta::{EunomiaObjectMeta, MapSampleMeta, RunnerConfig},
};
use anyhow::{anyhow, Context, Result};

const VMLINUX_BTF_PATH: &str = "/sys/kernel/btf/vmlinux";
const BTF_PATH_ENV_NAME: &str = "BTF_FILE_PATH";

/// The builder of the skeleton
pub mod builder;
/// controlling handles
pub mod handle;
pub(crate) mod poller;
/// The preloaded skeleton
pub mod preload;

#[cfg(test)]
#[cfg(not(feature = "no-load-bpf-tests"))]
mod tests;
/// Represents a polling-ready bpf skeleton. With you can control the ebpf program and poll from it.
pub struct BpfSkeleton {
    pub(crate) handle: PollingHandle,
    ///   data storage
    /// meta data control the behavior of ebpf program:
    /// eg. types of the eBPF maps and prog, export data types
    pub(crate) meta: EunomiaObjectMeta,
    /// config of eunomia itself,
    /// for how we creating, loading and interacting with the eBPF program
    /// eg. poll maps timeout in ms
    /// Note: this field seems to be never used
    #[allow(unused)]
    pub(crate) config_data: RunnerConfig,

    // exporter: EventExporter,
    /// the btf info of the loaded program
    pub(crate) btf: Arc<BtfContainer>,
    /// the links
    #[allow(unused)]
    pub(crate) links: Vec<AttachLink>,
    pub(crate) prog: Object,
}

impl BpfSkeleton {
    /// Create a poll handle to control the poll progress
    /// You can create multiple ones. All handles have the same ability
    pub fn create_poll_handle(&self) -> PollingHandle {
        self.handle.clone()
    }
    /// Get the name of the loaded program
    pub fn get_program_name(&self) -> &str {
        &self.meta.bpf_skel.obj_name
    }
    /// Get the fd of the provided map
    /// returns None if not found
    pub fn get_map_fd(&self, name: impl AsRef<str>) -> Option<i32> {
        self.prog.map(name).map(|m| m.fd())
    }
    /// Get the fd of the provided program
    /// returns None if not found
    pub fn get_prog_fd(&self, name: impl AsRef<str>) -> Option<i32> {
        self.prog.prog(name).map(|p| p.fd())
    }
    /// @brief auto polling and export the data to user space handler
    /// @details The key of the value is the field name in the export json.
    /// This function will block the current thread and poll
    /// If you want to control the poller, just create a handle using `create_poll_handle` before calling this.
    /// Note: this function will set paused and terminating to false before polling.
    pub fn wait_and_poll_to_handler(
        &self,
        export_format_type: ExportFormatType,
        export_event_handler: Option<Arc<dyn EventHandler>>,
        user_context: Option<Arc<dyn Any>>,
    ) -> Result<()> {
        let exporter_builder = EventExporterBuilder::new().set_export_format(export_format_type);
        let exporter_builder = if let Some(hdl) = export_event_handler {
            exporter_builder.set_export_event_handler(hdl)
        } else {
            exporter_builder
        };
        let exporter_builder = if let Some(user_ctx) = user_context {
            exporter_builder.set_user_context(user_ctx)
        } else {
            exporter_builder
        };
        let mut export_map: Option<(String, ExportMapType)> = None;
        for map_meta in self.meta.bpf_skel.maps.iter() {
            let bpf_map = self
                .prog
                .map(&map_meta.name)
                .ok_or_else(|| anyhow!("Map `{}` not found in bpf program", map_meta.name))?;
            if let Some(sample_meta) = &map_meta.sample {
                set_and_warn_existsing_map(
                    &mut export_map,
                    bpf_map,
                    ExportMapType::Sample(sample_meta),
                );
            } else if let MapType::RingBuf = bpf_map.map_type() {
                set_and_warn_existsing_map(&mut export_map, bpf_map, ExportMapType::RingBuffer);
            } else if let MapType::PerfEventArray = bpf_map.map_type() {
                set_and_warn_existsing_map(&mut export_map, bpf_map, ExportMapType::PerfEventArray);
            }
        }
        // Before polling, we should reset the control flags
        self.handle.reset();
        if let Some((map_name, export_type)) = export_map {
            let map = self
                .prog
                .map(&map_name)
                .ok_or_else(|| anyhow!("Invalid map name: {}", map_name))?;
            match export_type {
                ExportMapType::RingBuffer => {
                    let exporter = exporter_builder
                        .build_for_ringbuf(&self.meta.export_types, self.btf.clone())
                        .with_context(|| anyhow!("Failed to build ringbuf exporter"))?;
                    self.wait_and_poll_from_ringbuf(map, exporter)
                        .with_context(|| anyhow!("Failed to poll ringbuf"))?;
                }
                ExportMapType::PerfEventArray => {
                    let exporter = exporter_builder
                        .build_for_ringbuf(&self.meta.export_types, self.btf.clone())
                        .with_context(|| anyhow!("Failed to build perf event exporter"))?;
                    self.wait_and_poll_from_perf_event_array(map, exporter)
                        .with_context(|| anyhow!("Failed to poll perf event"))?;
                }
                ExportMapType::Sample(sample_meta) => {
                    let map_info = map
                        .info()
                        .with_context(|| anyhow!("Failed to get map info for `{}`", map.name()))?;

                    let exporter = exporter_builder
                        .build_for_map_sampling(
                            map_info.info.btf_key_type_id,
                            map_info.info.btf_value_type_id,
                            sample_meta,
                            &self.meta.export_types,
                            self.btf.clone(),
                        )
                        .with_context(|| {
                            anyhow!("Failed to build sampling exporter for `{}`", map.name())
                        })?;
                    self.wait_and_sample_map(map, exporter, sample_meta)
                        .with_context(|| anyhow!("Failed to poll sampling maps"))?;
                }
            };
        } else {
            self.wait_for_no_export_program()
                .with_context(|| anyhow!("Failed to wait for program"))?;
        };
        Ok(())
    }
}

fn set_and_warn_existsing_map<'a>(
    export_map: &mut Option<(String, ExportMapType<'a>)>,
    curr_map: &Map,
    ty: ExportMapType<'a>,
) {
    if let Some((name, _)) = export_map {
        warn!(
            "Multiple export maps found, one is `{}`, another is `{}`",
            name,
            curr_map.name()
        );
    }
    export_map.replace((curr_map.name().into(), ty));
}
enum ExportMapType<'a> {
    RingBuffer,
    PerfEventArray,
    Sample(&'a MapSampleMeta),
}