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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of bpfman
// TODO(astoycos) see issue #881
// use std::{
// collections::HashMap,
// path::{Path, PathBuf},
// };
// use anyhow::bail;
// use bpfman_api::{
// util::directories::CFGDIR_STATIC_PROGRAMS, ProgramType, TcProceedOn, XdpProceedOn,
// };
// use log::{info, warn};
// use serde::Deserialize;
// use tokio::fs;
// use crate::{
// command::{
// Direction,
// Location::{File, Image},
// Program, ProgramData, TcProgram, TracepointProgram, XdpProgram,
// },
// oci_utils::image_manager::BytecodeImage,
// utils::read_to_string,
// };
// #[derive(Debug, Clone, Deserialize)]
// pub(crate) struct XdpAttachInfo {
// pub(crate) priority: i32,
// pub(crate) iface: String,
// pub(crate) proceed_on: XdpProceedOn,
// }
// #[derive(Debug, Clone, Deserialize)]
// pub(crate) struct TcAttachInfo {
// pub(crate) priority: i32,
// pub(crate) iface: String,
// pub(crate) proceed_on: TcProceedOn,
// pub(crate) direction: Direction,
// }
// #[derive(Debug, Clone, Deserialize)]
// pub(crate) struct TracepointAttachInfo {
// pub(crate) tracepoint: String,
// }
// // TODO not yet implemented
// // #[derive(Debug, Clone, Deserialize)]
// // pub(crate) struct KprobeAttachInfo {
// // pub(crate) fn_name: String,
// // pub(crate) offset: u64,
// // pub(crate) retprobe: bool,
// // pub(crate) container_pid: Option<i32>,
// // }
// // #[derive(Debug, Clone, Deserialize)]
// // pub(crate) struct UprobeAttachInfo {
// // pub(crate) fn_name: Option<String>,
// // pub(crate) offset: u64,
// // pub(crate) target: String,
// // pub(crate) retprobe: bool,
// // pub(crate) pid: Option<i32>,
// // pub(crate) container_pid: Option<i32>,
// // }
// #[derive(Debug, Deserialize, Clone)]
// pub struct StaticProgramEntry {
// bytecode_image: Option<BytecodeImage>,
// file_path: Option<String>,
// name: String,
// global_data: HashMap<String, Vec<u8>>,
// program_type: ProgramType,
// xdp_attach: Option<XdpAttachInfo>,
// tc_attach: Option<TcAttachInfo>,
// tracepoint_attach: Option<TracepointAttachInfo>,
// }
// impl StaticProgramEntry {
// pub(crate) fn get_bytecode_image(self) -> Option<BytecodeImage> {
// self.bytecode_image
// }
// }
// #[derive(Debug, Deserialize)]
// pub struct NetworkAttach {
// pub interface: String,
// pub priority: i32,
// pub proceed_on: Vec<String>,
// }
// #[derive(Debug, Deserialize, Clone)]
// struct StaticProgramManager {
// #[serde(skip)]
// path: PathBuf,
// programs: Vec<StaticProgramEntry>,
// }
// impl StaticProgramManager {
// fn programs_from_directory(mut self) -> Result<(), anyhow::Error> {
// if let Ok(mut entries) = fs::read_dir(self.path) {
// while let Some(file) = entries.next_entry()? {
// let path = &file.path();
// // ignore directories
// if path.is_dir() {
// continue;
// }
// if let Ok(contents) = read_to_string(path) {
// let program = toml::from_str(&contents)?;
// self.programs.push(program);
// } else {
// warn!("Failed to parse program static file {:?}.", path.to_str());
// continue;
// }
// }
// }
// Ok(())
// }
// }
// pub(crate) fn get_static_programs<P: AsRef<Path>>(
// path: P,
// ) -> Result<Vec<Program>, anyhow::Error> {
// let static_program_manager = StaticProgramManager {
// path: path.as_ref().to_path_buf(),
// programs: Vec::new(),
// };
// static_program_manager
// .clone()
// .programs_from_directory()
// ?;
// let mut programs: Vec<Program> = Vec::new();
// // Load any static programs first
// if !static_program_manager.programs.is_empty() {
// info!("Loading static programs from {CFGDIR_STATIC_PROGRAMS}");
// for program in static_program_manager.programs {
// let location = match program.file_path {
// Some(p) => File(p),
// None => Image(
// program
// .clone()
// .get_bytecode_image()
// .expect("static program did not provide bytecode"),
// ),
// };
// let data = ProgramData::new(
// location,
// program.name,
// HashMap::new(),
// program.global_data,
// None,
// );
// let prog = match program.program_type {
// ProgramType::Xdp => {
// if let Some(m) = program.xdp_attach {
// Program::Xdp(XdpProgram::new(data, m.priority, m.iface, m.proceed_on))
// } else {
// bail!("invalid info for xdp program")
// }
// }
// ProgramType::Tc => {
// if let Some(m) = program.tc_attach {
// Program::Tc(TcProgram::new(
// data,
// m.priority,
// m.iface,
// m.proceed_on,
// m.direction,
// ))
// } else {
// bail!("invalid attach type for tc program")
// }
// }
// ProgramType::Tracepoint => {
// if let Some(m) = program.tracepoint_attach {
// Program::Tracepoint(TracepointProgram::new(data, m.tracepoint))
// } else {
// bail!("invalid attach type for tc program")
// }
// }
// m => bail!("program type not yet supported to load statically: {:?}", m),
// };
// programs.push(prog)
// }
// };
// Ok(programs)
// }
// #[cfg(test)]
// mod test {
// use super::*;
// #[tokio::test]
// fn test_parse_program_from_invalid_path() {
// let static_program_manager = StaticProgramManager {
// path: "/tmp/file.toml".into(),
// programs: Vec::new(),
// };
// static_program_manager
// .clone()
// .programs_from_directory()
//
// .unwrap();
// assert!(static_program_manager.programs.is_empty())
// }
// #[test]
// fn test_parse_single_file() {
// let input: &str = r#"
// [[programs]]
// name = "firewall"
// file_path = "/opt/bin/myapp/lib/myebpf.o"
// global_data = { }
// program_type ="Xdp"
// xdp_attach = { iface = "eth0", priority = 50, proceed_on = [], position=0 }
// [[programs]]
// name = "pass"
// bytecode_image = { image_url = "quay.io/bpfman-bytecode/xdp_pass:latest", image_pull_policy="Always" }
// global_data = { }
// program_type ="Xdp"
// xdp_attach = { iface = "eth0", priority = 55, proceed_on = [], position=0 }
// [[programs]]
// name = "counter"
// bytecode_image = { image_url = "quay.io/bpfman-bytecode/xdp_pass:latest", image_pull_policy="Always" }
// global_data = { }
// program_type ="Tc"
// tc_attach = { iface = "eth0", priority = 55, proceed_on = [], position=0, direction="Ingress" }
// [[programs]]
// name = "tracepoint"
// bytecode_image = { image_url = "quay.io/bpfman-bytecode/tracepoint:latest", image_pull_policy="Always" }
// global_data = { }
// program_type ="Tracepoint"
// tracepoint_attach = { tracepoint = "syscalls/sys_enter_openat" }
// "#;
// let mut programs: StaticProgramManager =
// toml::from_str(input).expect("error parsing toml input");
// match programs.programs.pop() {
// Some(i) => {
// if let Some(m) = i.xdp_attach {
// assert_eq!(m.iface, "eth0");
// assert_eq!(m.priority, 55);
// } else if let Some(m) = i.tracepoint_attach {
// assert_eq!(m.tracepoint, "syscalls/sys_enter_openat")
// } else {
// panic!("incorrect attach type")
// }
// }
// None => panic!("expected programs to be present"),
// }
// }
// }