forked from hakiri/p4-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.p4
76 lines (70 loc) · 2.89 KB
/
debug.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
75
76
// Credits to Andy Fingerhut
// https://github.com/jafingerhut/p4-guide/blob/master/checksum/debug-utils.p4
// Because open source P4 tools do not support using a value of type
// `error` as a field in a table's search key as of 2018-Sep-21, work
// around that limitation by converting parser_error to an unsigned
// integer. We will use _that_ converted value for debug table
// purposes.
// It is easy to add more possible `error` type values to this
// conversion if you wish, but there is no way in P4_16 to
// automatically iterate over all additional possible values that a
// user program might have defined for type `error`.
control convert_error_to_bitvector(out bit<8> error_as_int,
in error e)
{
apply {
if (e == error.NoError) {
error_as_int = 0;
} else if (e == error.PacketTooShort) {
error_as_int = 1;
} else if (e == error.NoMatch) {
error_as_int = 2;
} else if (e == error.StackOutOfBounds) {
error_as_int = 3;
} else if (e == error.HeaderTooShort) {
error_as_int = 4;
} else if (e == error.ParserTimeout) {
error_as_int = 5;
} else {
// Unknown value
error_as_int = 0xff;
}
}
}
control debug_std_meta(in standard_metadata_t standard_metadata)
{
bit<8> parser_error_as_int;
convert_error_to_bitvector() convert_err;
table dbg_table {
key = {
// This is a complete list of fields inside of the struct
// standard_metadata_t as of the 2021-Feb-23 version of
// p4c in the file p4c/p4include/v1model.p4.
// standard_metadata.parser_error is commented out because
// the p4c backend for bmv2 as of that date gives an error
// if you include a field of type 'error' in a table key.
standard_metadata.ingress_port : exact;
standard_metadata.egress_spec : exact;
standard_metadata.egress_port : exact;
standard_metadata.instance_type : exact;
standard_metadata.packet_length : exact;
standard_metadata.enq_timestamp : exact;
standard_metadata.enq_qdepth : exact;
standard_metadata.deq_timedelta : exact;
standard_metadata.deq_qdepth : exact;
standard_metadata.ingress_global_timestamp : exact;
standard_metadata.egress_global_timestamp : exact;
standard_metadata.mcast_grp : exact;
standard_metadata.egress_rid : exact;
standard_metadata.checksum_error : exact;
//standard_metadata.parser_error : exact;
parser_error_as_int : exact;
}
actions = { NoAction; }
const default_action = NoAction();
}
apply {
convert_err.apply(parser_error_as_int, standard_metadata.parser_error);
dbg_table.apply();
}
}