forked from p4lang/p4app-switchML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop_simulator.p4
74 lines (59 loc) · 2.61 KB
/
drop_simulator.p4
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
/*
Copyright 2021 Intel-KAUST-Microsoft
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _DROP_SIMULATOR_
#define _DROP_SIMULATOR_
#include "configuration.p4"
#include "types.p4"
#include "headers.p4"
control IngressDropSimulator(
// Used for drop probability and to signal ingress drop
inout port_metadata_t port_metadata) {
Random<drop_probability_t>() rng;
apply {
// If drop probability from parser is nonzero, decide if we should drop this packet.
// Do so by adding random value to drop probability.
// Use saturating addition so that the result never wraps around.
// A result of 0xffff means to drop the packet.
// This will be consumed later in the pipeline when checking bitmaps.
if (port_metadata.ingress_drop_probability != 0) {
port_metadata.ingress_drop_probability = rng.get() |+| port_metadata.ingress_drop_probability;
}
}
}
control EgressDropSimulator(
// Used for drop probability
inout port_metadata_t port_metadata,
in queue_pair_index_t qp_index,
// Used to signal egress drop
out bool simulate_egress_drop) {
Random<drop_probability_t>() rng;
Counter<counter_t, queue_pair_index_t>(max_num_queue_pairs, CounterType_t.PACKETS) simulated_drop_packet_counter;
apply {
// If drop probability from parser is nonzero, decide if we should drop this packet.
// Do so by adding random value to drop probability.
// Use saturating addition so that the result never wraps around.
// A result of 0xffff means to drop the packet.
if (port_metadata.egress_drop_probability != 0) {
port_metadata.egress_drop_probability = rng.get() |+| port_metadata.egress_drop_probability;
}
simulate_egress_drop = false;
if (port_metadata.egress_drop_probability == 0xffff) {
simulate_egress_drop = true;
}
if (port_metadata.ingress_drop_probability == 0xffff ||
port_metadata.egress_drop_probability == 0xffff) {
simulated_drop_packet_counter.count(qp_index);
}
}
}
#endif /* _DROP_SIMULATOR_ */