-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuserland.rs
More file actions
342 lines (318 loc) · 12.2 KB
/
userland.rs
File metadata and controls
342 lines (318 loc) · 12.2 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Copyright 2024 Oxide Computer Company
//! Userland packet parsing and processing microbenchmarks.
use criterion::BenchmarkId;
use criterion::Criterion;
use criterion::Throughput;
use criterion::criterion_group;
use criterion::criterion_main;
use opte::engine::packet::Packet;
use opte_bench::MeasurementInfo;
use opte_bench::alloc::*;
use opte_bench::packet::BenchPacket;
use opte_bench::packet::BenchPacketInstance;
use opte_bench::packet::Dhcp4;
use opte_bench::packet::Dhcp6;
use opte_bench::packet::Icmp4;
use opte_bench::packet::Icmp6;
use opte_bench::packet::ParserKind;
use opte_bench::packet::TestCase;
use opte_bench::packet::ULP_FAST_PATH;
use opte_bench::packet::ULP_SLOW_PATH;
use opte_test_utils::*;
use oxide_vpc::api::IpAddr;
use oxide_vpc::api::Ipv4Addr;
use oxide_vpc::api::Ipv6Addr;
use oxide_vpc::api::SourceFilter;
use std::collections::BTreeSet;
use std::hint::black_box;
// Top level runner. Specifies packet classes.
//
// Timing/memory measurements are selected by `config` in the below
// `criterion_group!` invocations.
pub fn block<M: MeasurementInfo + 'static>(
c: &mut Criterion<M>,
do_parse: bool,
) {
let all_tests: Vec<Box<dyn BenchPacket>> = vec![
Box::new(Dhcp4),
Box::new(Dhcp6),
Box::new(Icmp4),
Box::new(Icmp6),
Box::new(ULP_FAST_PATH),
Box::new(ULP_SLOW_PATH),
];
for experiment in &all_tests {
for case in experiment.test_cases() {
if do_parse {
test_parse(c, &**experiment, &*case);
}
test_handle(c, &**experiment, &*case);
}
}
}
pub fn parse_and_process<M: MeasurementInfo + 'static>(c: &mut Criterion<M>) {
block(c, true)
}
pub fn process_only<M: MeasurementInfo + 'static>(c: &mut Criterion<M>) {
block(c, false)
}
// Run benchmarks for parsing a given type of packet.
pub fn test_parse<M: MeasurementInfo + 'static>(
c: &mut Criterion<M>,
experiment: &dyn BenchPacket,
case: &dyn BenchPacketInstance,
) {
let mut c = c.benchmark_group(format!(
"parse/{}/{}",
experiment.packet_label(),
M::label()
));
let parser = case.parse_with();
c.bench_with_input(
BenchmarkId::from_parameter(case.instance_name()),
&case,
|b, inp| {
b.iter_batched(
|| inp.generate(),
// match *outside* the closure to prevent its selection from being timed.
match parser {
ParserKind::Generic => {
|(mut in_pkt, direction): TestCase| {
black_box(match direction {
In => Packet::parse_inbound(
in_pkt.iter_mut(),
GenericUlp {},
),
Out => Packet::parse_outbound(
in_pkt.iter_mut(),
GenericUlp {},
),
})
.unwrap();
}
}
ParserKind::OxideVpc => {
|(mut in_pkt, direction): TestCase| {
black_box(match direction {
In => {
Packet::parse_inbound(
in_pkt.iter_mut(),
VpcParser {},
)
.unwrap();
}
Out => {
Packet::parse_outbound(
in_pkt.iter_mut(),
VpcParser {},
)
.unwrap();
}
});
}
}
},
criterion::BatchSize::LargeInput,
)
},
);
}
// Run benchmarks for processing (e.g., generating hairpins, rewriting
// fields, encapsulation) for a given type of packet.
pub fn test_handle<M: MeasurementInfo + 'static>(
c: &mut Criterion<M>,
experiment: &dyn BenchPacket,
case: &dyn BenchPacketInstance,
) {
let port = match case.create_port() {
Some(port) => port,
None => {
let g1_cfg = g1_cfg();
let mut g1 = oxide_net_setup("g1_port", &g1_cfg, None, None);
g1.port.start();
set!(g1, "port_state=running");
g1
}
};
let mut c = c.benchmark_group(format!(
"process/{}/{}",
experiment.packet_label(),
M::label()
));
let parser = case.parse_with();
c.bench_with_input(
BenchmarkId::from_parameter(case.instance_name()),
&case,
|b, _i| {
b.iter_batched(
|| {
let (init_pkt, dir) = case.generate();
case.pre_handle(&port);
(init_pkt, dir)
},
// Can't seem to match outside here -- must be missing something.
// Sadly, we can't elide parsing here as the
// packet is now a view over the generated pkt.
|(mut pkt_m, dir): TestCase| match parser {
ParserKind::Generic => {
let res = match dir {
In => {
let pkt = Packet::parse_inbound(
pkt_m.iter_mut(),
GenericUlp {},
)
.unwrap();
port.port.process(dir, black_box(pkt)).unwrap()
}
Out => {
let pkt = Packet::parse_outbound(
pkt_m.iter_mut(),
GenericUlp {},
)
.unwrap();
port.port.process(dir, black_box(pkt)).unwrap()
}
};
assert!(!matches!(res, ProcessResult::Drop { .. }));
if let Modified(spec) = res {
black_box(spec.apply(pkt_m));
}
}
ParserKind::OxideVpc => {
let res = match dir {
In => {
let pkt = Packet::parse_inbound(
pkt_m.iter_mut(),
VpcParser {},
)
.unwrap();
port.port.process(dir, black_box(pkt)).unwrap()
}
Out => {
let pkt = Packet::parse_outbound(
pkt_m.iter_mut(),
VpcParser {},
)
.unwrap();
port.port.process(dir, black_box(pkt)).unwrap()
}
};
assert!(!matches!(res, ProcessResult::Drop { .. }));
if let Modified(spec) = res {
black_box(spec.apply(pkt_m));
}
}
},
criterion::BatchSize::LargeInput,
)
},
);
}
/// Generate a source IP address for filter testing (10.0.0.x).
fn make_src_v4(i: u32) -> IpAddr {
IpAddr::Ip4(Ipv4Addr::from(0x0a000000u32 + i))
}
/// Generate a source IP address for filter testing (fd00::x).
fn make_src_v6(i: u32) -> IpAddr {
let mut bytes = [0u8; 16];
bytes[0..4].copy_from_slice(&[0xfd, 0x00, 0x00, 0x00]);
bytes[12..16].copy_from_slice(&i.to_be_bytes());
IpAddr::Ip6(Ipv6Addr::from(bytes))
}
/// Benchmark [`SourceFilter::allows`] for various filter configurations.
fn source_filter_allows(c: &mut Criterion) {
let mut group = c.benchmark_group("source_filter/allows");
group.throughput(Throughput::Elements(1));
let src_v4 = make_src_v4(100); // Not in any source list
let src_v6 = make_src_v6(100);
// Fast path: EXCLUDE() with empty sources (*, G)
let filter_any = SourceFilter::default();
group.bench_function("exclude_empty_v4", |b| {
b.iter(|| black_box(filter_any.allows(black_box(src_v4))))
});
group.bench_function("exclude_empty_v6", |b| {
b.iter(|| black_box(filter_any.allows(black_box(src_v6))))
});
// EXCLUDE with sources: "Miss" case where source is not in exclusion list
for size in [1, 5, 10, 50, 100] {
let sources_v4: BTreeSet<_> = (0..size).map(make_src_v4).collect();
let filter_v4 = SourceFilter::Exclude(sources_v4);
group.bench_with_input(
BenchmarkId::new("exclude_miss_v4", size),
&filter_v4,
|b, f| b.iter(|| black_box(f.allows(black_box(src_v4)))),
);
let src_in_list_v4 = make_src_v4(0);
group.bench_with_input(
BenchmarkId::new("exclude_hit_v4", size),
&filter_v4,
|b, f| b.iter(|| black_box(f.allows(black_box(src_in_list_v4)))),
);
let sources_v6: BTreeSet<_> = (0..size).map(make_src_v6).collect();
let filter_v6 = SourceFilter::Exclude(sources_v6);
group.bench_with_input(
BenchmarkId::new("exclude_miss_v6", size),
&filter_v6,
|b, f| b.iter(|| black_box(f.allows(black_box(src_v6)))),
);
let src_in_list_v6 = make_src_v6(0);
group.bench_with_input(
BenchmarkId::new("exclude_hit_v6", size),
&filter_v6,
|b, f| b.iter(|| black_box(f.allows(black_box(src_in_list_v6)))),
);
}
// INCLUDE with sources: "Hit" case where source is in inclusion list
for size in [1, 5, 10, 50, 100] {
let sources_v4: BTreeSet<_> = (0..size).map(make_src_v4).collect();
let filter_v4 = SourceFilter::Include(sources_v4);
let src_in_list_v4 = make_src_v4(0);
group.bench_with_input(
BenchmarkId::new("include_hit_v4", size),
&filter_v4,
|b, f| b.iter(|| black_box(f.allows(black_box(src_in_list_v4)))),
);
group.bench_with_input(
BenchmarkId::new("include_miss_v4", size),
&filter_v4,
|b, f| b.iter(|| black_box(f.allows(black_box(src_v4)))),
);
let sources_v6: BTreeSet<_> = (0..size).map(make_src_v6).collect();
let filter_v6 = SourceFilter::Include(sources_v6);
let src_in_list_v6 = make_src_v6(0);
group.bench_with_input(
BenchmarkId::new("include_hit_v6", size),
&filter_v6,
|b, f| b.iter(|| black_box(f.allows(black_box(src_in_list_v6)))),
);
group.bench_with_input(
BenchmarkId::new("include_miss_v6", size),
&filter_v6,
|b, f| b.iter(|| black_box(f.allows(black_box(src_v6)))),
);
}
// INCLUDE() empty, rejecting all
let filter_none = SourceFilter::Include(BTreeSet::new());
group.bench_function("include_empty_v4", |b| {
b.iter(|| black_box(filter_none.allows(black_box(src_v4))))
});
group.bench_function("include_empty_v6", |b| {
b.iter(|| black_box(filter_none.allows(black_box(src_v6))))
});
group.finish();
}
criterion_group!(wall, parse_and_process, source_filter_allows);
criterion_group!(
name = alloc;
config = new_crit(Allocs);
targets = process_only
);
criterion_group!(
name = byte_alloc;
config = new_crit(BytesAlloced);
targets = process_only
);
criterion_main!(wall, alloc, byte_alloc);