-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbpf.rs
More file actions
351 lines (309 loc) · 10 KB
/
bpf.rs
File metadata and controls
351 lines (309 loc) · 10 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use anyhow::{Context, Result};
use ebpf_ccp_generic::FlowKey;
use lazy_static::lazy_static;
use libbpf_rs::{Link, MapCore, MapFlags, Object, ObjectBuilder, RingBufferBuilder};
use std::collections::{HashMap, VecDeque};
use std::path::Path;
use std::sync::{Arc, Mutex};
use tracing::{debug, info, warn};
#[repr(C)]
#[derive(Debug, Copy, Clone)]
struct FlowEvent {
event_type: u8,
_pad: [u8; 3],
flow: FlowKey,
init_cwnd: u32,
mss: u32,
}
#[repr(C, packed)]
#[derive(Copy, Clone)]
pub struct FlowStatistics {
pub packets_in_flight: u32,
pub bytes_in_flight: u32,
pub bytes_pending: u32,
pub rtt_sample_us: u32,
pub was_timeout: u8,
pub _pad1: [u8; 3],
}
#[repr(C, packed)]
#[derive(Copy, Clone)]
pub struct AckStatistics {
pub bytes_acked: u32,
pub packets_acked: u32,
pub bytes_misordered: u32,
pub packets_misordered: u32,
pub ecn_bytes: u32,
pub ecn_packets: u32,
pub lost_pkts_sample: u32,
pub now: u64,
}
#[repr(C, packed)]
#[derive(Copy, Clone)]
pub struct Measurement {
pub flow: FlowKey,
pub rates: FlowRates,
pub flow_stats: FlowStatistics,
pub ack_stats: AckStatistics,
pub snd_cwnd: u32,
pub snd_ssthresh: u32,
pub pacing_rate: u64,
pub ca_state: u8,
pub measurement_type: u8,
pub _pad: [u8; 2],
}
#[repr(C, packed)]
#[derive(Copy, Clone)]
pub struct FlowRates {
pub rate_incoming: u32,
pub rate_outgoing: u32,
}
#[repr(C)]
pub struct UserUpdate {
pub cwnd_bytes: u32,
pub pacing_rate: u64,
pub ssthresh: u32,
pub use_pacing: u8, // Whether to use pacing rate when updating
pub use_cwnd: u8, // Whether to use cwnd when updating
pub use_ssthresh: u8, // Whether to override ssthresh
pub _pad: u8,
pub flow_command: u32,
}
pub enum DatapathEvent {
FlowCreated {
flow_id: u64,
init_cwnd: u32,
mss: u32,
},
FlowClosed {
flow_id: u64,
},
Measurement {
flow_id: u64,
measurement: Measurement,
},
}
pub struct EbpfDatapath {
obj: Object,
_link: Link, // Keep link alive
events: Arc<Mutex<VecDeque<DatapathEvent>>>,
}
impl EbpfDatapath {
pub fn new(ebpf_path: &str, struct_ops_name: &str) -> Result<Self> {
info!("Loading eBPF object file: {}", ebpf_path);
let obj_path = Path::new(ebpf_path);
if !obj_path.exists() {
anyhow::bail!("eBPF object file not found: {:?}.", obj_path);
}
let mut builder = ObjectBuilder::default();
builder.debug(true);
let open_obj = builder
.open_file(obj_path)
.context("Failed to open BPF object file")?;
let mut obj = open_obj
.load()
.context("Failed to load BPF object into kernel")?;
info!("BPF object loaded successfully, attempting to register with TCP");
// Attach to tcp stack via struct_ops
let mut struct_ops_map = obj
.maps_mut()
.find(|m| m.name() == struct_ops_name)
.with_context(|| format!("Failed to find '{}' struct_ops map", struct_ops_name))?;
let link = struct_ops_map
.attach_struct_ops()
.context("Failed to attach struct_ops to TCP stack")?;
info!(
"struct_ops attached - '{}' registered with TCP",
struct_ops_name
);
// Set up ring buffers
let events = Arc::new(Mutex::new(VecDeque::new()));
// Get measurements ring buffer map
let measurements_map = obj
.maps()
.find(|m| m.name() == "measurements")
.context("Failed to find 'measurements' map")?;
let events_clone = events.clone();
let mut rb_builder = RingBufferBuilder::new();
rb_builder
.add(&measurements_map, move |data: &[u8]| {
let m = unsafe { &*(data.as_ptr() as *const Measurement) };
let flow = m.flow;
let flow_id = flow_key_to_id(&flow);
let rate_incoming = m.rates.rate_incoming;
let rate_outgoing = m.rates.rate_outgoing;
let bytes_acked = m.ack_stats.bytes_acked;
let lost_pkts_sample = m.ack_stats.lost_pkts_sample;
let rtt_sample_us = m.flow_stats.rtt_sample_us;
let bytes_in_flight = m.flow_stats.bytes_in_flight;
let measurement_type = m.measurement_type;
debug!(
"Measurement: flow={:016x}, rate_incoming={}, rate_outgoing={}, acked={}, loss={}, rtt={}us, inflight={}, type={}",
rate_incoming,
rate_outgoing,
flow_id,
bytes_acked,
lost_pkts_sample,
rtt_sample_us,
bytes_in_flight,
measurement_type
);
events_clone
.lock()
.unwrap()
.push_back(DatapathEvent::Measurement {
flow_id,
measurement: *m,
});
0
})
.context("Failed to add measurements ring buffer")?;
// Get flow events ring buffer map
let flow_events_map = obj
.maps()
.find(|m| m.name() == "flow_events")
.context("Failed to find 'flow_events' map")?;
let events_clone2 = events.clone();
rb_builder
.add(&flow_events_map, move |data: &[u8]| {
let e = unsafe { &*(data.as_ptr() as *const FlowEvent) };
let flow_id = flow_key_to_id(&e.flow);
let event = match e.event_type {
1 => {
debug!("Flow created: {:016x}", flow_id);
DatapathEvent::FlowCreated {
flow_id,
init_cwnd: e.init_cwnd,
mss: e.mss,
}
}
2 => {
debug!("Flow closed: {:016x}", flow_id);
DatapathEvent::FlowClosed { flow_id }
}
_ => {
warn!("Unknown flow event type: {}", e.event_type);
return 0;
}
};
events_clone2.lock().unwrap().push_back(event);
0
})
.context("Failed to add flow_events ring buffer")?;
let rb = rb_builder.build().context("Failed to build ring buffers")?;
std::thread::spawn(move || {
loop {
if let Err(e) = rb.poll(std::time::Duration::from_millis(100)) {
warn!("Ring buffer poll error: {}", e);
}
}
});
Ok(Self {
obj,
_link: link,
events,
})
}
pub fn poll(&mut self, _timeout_ms: u64) -> Result<Vec<DatapathEvent>> {
let mut events = self.events.lock().unwrap();
let result: Vec<_> = events.drain(..).collect();
Ok(result)
}
pub fn cleanup_flow(&self, flow_id: u64) {
cleanup_flow_key(flow_id);
}
pub fn update_cwnd(&mut self, flow_id: u64, cwnd_bytes: u32) -> Result<()> {
let update = UserUpdate {
cwnd_bytes,
pacing_rate: 0,
ssthresh: 0,
use_pacing: 0,
use_cwnd: 1,
use_ssthresh: 0,
_pad: 0,
flow_command: 0,
};
self.send_user_update(flow_id, &update)
}
pub fn update_pacing_rate(&mut self, flow_id: u64, pacing_rate: u64) -> Result<()> {
let update = UserUpdate {
cwnd_bytes: 0,
pacing_rate,
ssthresh: 0,
use_pacing: 1,
use_cwnd: 0,
use_ssthresh: 0,
_pad: 0,
flow_command: 0,
};
self.send_user_update(flow_id, &update)
}
pub fn update_cwnd_and_pacing(
&mut self,
flow_id: u64,
cwnd_bytes: u32,
pacing_rate: u64,
) -> Result<()> {
let update = UserUpdate {
cwnd_bytes,
pacing_rate,
ssthresh: 0,
use_pacing: 1,
use_cwnd: 1,
use_ssthresh: 0,
_pad: 0,
flow_command: 0,
};
self.send_user_update(flow_id, &update)
}
pub fn send_user_update(&mut self, flow_id: u64, update: &UserUpdate) -> Result<()> {
let map = self
.obj
.maps()
.find(|m| m.name() == "user_command_map")
.context("Failed to find user_command_map")?;
let key = id_to_flow_key(flow_id);
let key_bytes = unsafe {
std::slice::from_raw_parts(
&key as *const _ as *const u8,
std::mem::size_of::<FlowKey>(),
)
};
let value_bytes = unsafe {
std::slice::from_raw_parts(
update as *const _ as *const u8,
std::mem::size_of::<UserUpdate>(),
)
};
map.update(key_bytes, value_bytes, MapFlags::ANY)
.context("Failed to update user_command_map")?;
Ok(())
}
}
// Mapping from flow_id to flow_key for map lookups
lazy_static! {
static ref FLOW_KEY_MAP: Mutex<HashMap<u64, FlowKey>> = Mutex::new(HashMap::new());
}
fn flow_key_to_id(key: &FlowKey) -> u64 {
let id = ((key.saddr as u64) << 32) | (key.daddr as u64);
FLOW_KEY_MAP.lock().unwrap().insert(id, *key);
id
}
fn id_to_flow_key(id: u64) -> FlowKey {
FLOW_KEY_MAP
.lock()
.unwrap()
.get(&id)
.copied()
.unwrap_or_else(|| {
warn!("Flow key not found for id {:016x}, using partial key", id);
FlowKey {
saddr: (id >> 32) as u32,
daddr: id as u32,
sport: 0,
dport: 0,
}
})
}
fn cleanup_flow_key(id: u64) {
FLOW_KEY_MAP.lock().unwrap().remove(&id);
}