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
use crate::error::Error;
use crate::platform::linux::bpf::{AttachType, CallBpf, Command};
use crate::platform::linux::syscalls::close;

use std::io::Write;

#[derive(Default)]
#[repr(C, align(8))]
#[derive(Copy, Clone)]
struct BpfProgramAttr {
    pub prog_type: u32,
    pub insn_cnt: u32,
    pub insns: u64,
    pub license: u64,
    pub log_level: u32,
    pub log_size: u32,
    pub log_buf: u64,
    pub kern_version: u32,
    pub prog_flags: u32,
    pub prog_name: [u8; 16],
    pub prog_ifindex: u32,
    pub expected_attach_type: u32,
    pub prog_btf_fd: u32,
    pub func_info_rec_size: u32,
    pub func_info: u64,
    pub func_info_count: u32,
    pub line_info_rec_size: u32,
    pub line_info: u64,
    pub line_info_count: u32,
    pub attach_btf_id: u32,
}

#[derive(Copy, Clone)]
pub enum ProgramType {
    Unspec = 0,
    SocketFilter,
    Kprobe,
    SchedCls,
    SchedAct,
    Tracepoint,
    Xdp,
    PerfEvent,
    CgroupSkb,
    CgroupSock,
    LwtIn,
    LwtOut,
    LwtXmit,
    SockOps,
    SkSkb,
    CgroupDevice,
    SkMsg,
    RawTracepoint,
    CgroupSockAddr,
    LwtSeg6local,
    LircMode2,
    SkReuseport,
    FlowDissector,
    CgroupSysctl,
    RawTracepointWritable,
    CgroupSockopt,
    Tracing,
    StructOps,
    Ext,
    Lsm,
    SkLookup,
    Syscall,
}

#[derive(Clone)]
pub struct ProgramAttr {
    /// The BTF id of the function that this program is to be attached to. Mutually-exclusive with
    /// the `attach_name` field.
    pub attach_btf_id: Option<u32>,

    /// The name of the function that this program is to be attached to. Mutually-exclusive with
    /// the `attach_btf_id` field.
    pub attach_name: Option<String>,

    /// The type of attachment.
    pub expected_attach_type: Option<AttachType>,

    /// An optional name for the program.
    pub prog_name: [u8; 16],

    /// The type of program. Only certain program types can be attached to certain names/btf ids,
    /// so this field and the `attach_*` fields need to be coordinated properly.
    pub prog_type: ProgramType,
}

pub struct Program {
    attr: ProgramAttr,
    fd: u32,
}

impl Program {
    const LICENSE: &'static str = "GPL\0";

    /// Creates a program with the given attributes and instructions. Optionally,
    /// an object implementing the Write trait can be passed in that receives the
    /// kernel eBPF logging output.
    ///
    /// # Arguments
    ///
    /// * `attr` - The program attributes.
    /// * `instructions` - The raw eBPF instructions describing the program.
    /// * `log_out` - The logger object.
    pub fn create(
        attr: &ProgramAttr,
        instructions: &[u64],
        log_out: Option<&mut dyn Write>,
    ) -> Result<Self, Error> {
        let mut buf = vec![0; 1 << 20];

        let expected_attach_type = if let Some(t) = attr.expected_attach_type {
            t as u32
        } else {
            0
        };

        let attach_btf_id = if let Some(id) = attr.attach_btf_id {
            id as u32
        } else {
            0
        };

        let bpf_attr = BpfProgramAttr {
            prog_type: attr.prog_type as u32,
            insns: instructions.as_ptr() as u64,
            insn_cnt: instructions.len() as u32,
            license: Self::LICENSE.as_ptr() as u64,
            log_level: 1,
            log_size: 1 << 20,
            log_buf: buf.as_mut_ptr() as u64,
            kern_version: 0,
            prog_flags: 0,
            prog_name: attr.prog_name,
            prog_ifindex: 0,
            expected_attach_type,
            prog_btf_fd: 0,
            func_info_rec_size: 0,
            func_info: 0,
            func_info_count: 0,
            line_info_rec_size: 0,
            line_info: 0,
            line_info_count: 0,
            attach_btf_id,
        };

        let r = bpf_attr.call_bpf(Command::ProgLoad);

        if let (Ok(s), Some(log_out)) = (std::str::from_utf8(&buf), log_out) {
            let _ = write!(log_out, "{}", s);
        }

        match r {
            Err(e) => Err(e),
            Ok(r) => Ok(Self {
                fd: r as u32,
                attr: attr.clone(),
            }),
        }
    }

    /// Retrieves the attributes for the program.
    pub fn get_attr(&self) -> &ProgramAttr {
        &self.attr
    }

    /// Retrieves the underlying fd for the program.
    pub fn get_fd(&self) -> u32 {
        self.fd
    }
}

impl Drop for Program {
    fn drop(&mut self) {
        close(self.fd);
    }
}