Skip to content

Commit

Permalink
[P4Testgen] Add EliminateInvalidHeaders midend pass (#4239)
Browse files Browse the repository at this point in the history
* testgen: Add EliminateInvalidHeaders midend pass
* Add BMV2 test with {#} literal
  • Loading branch information
vlstill authored Nov 13, 2023
1 parent 72f3983 commit dd43a45
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backends/p4tools/common/compiler/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "midend/convertEnums.h"
#include "midend/convertErrors.h"
#include "midend/copyStructures.h"
#include "midend/eliminateInvalidHeaders.h"
#include "midend/eliminateNewtype.h"
#include "midend/eliminateSerEnums.h"
#include "midend/eliminateSwitch.h"
Expand Down Expand Up @@ -95,6 +96,8 @@ void MidEnd::addDefaultPasses() {
new P4::EliminateSwitch(&refMap, &typeMap),
// Replace types introduced by 'type' with 'typedef'.
new P4::EliminateNewtype(&refMap, &typeMap),
// Remove the invalid header / header-union literal, except for constant expressions
new P4::EliminateInvalidHeaders(&refMap, &typeMap),
// Replace serializable enum constants with their values.
new P4::EliminateSerEnums(&refMap, &typeMap),
// Make sure that we have no TypeDef left in the program.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <core.p4>
#include <v1model.p4>

header ethernet_t {
bit<48> dst_addr;
bit<48> src_addr;
bit<16> eth_type;
}

header h0_t {
bit<32> f0;
bit<32> f1;
}

struct headers {
ethernet_t eth_hdr;
h0_t h0;
}

struct Meta {}

parser p(packet_in pkt, out headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
transition parse_hdrs;
}
state parse_hdrs {
pkt.extract(hdr.eth_hdr);
pkt.extract(hdr.h0);
transition select(hdr.h0.f0) {
0: invalidate;
default: accept;
}
}
state invalidate {
hdr.h0 = (h0_t){#};
transition accept;
}
}

control ingress(inout headers h, inout Meta m, inout standard_metadata_t sm) {
bit<32> a = 0;
apply {
if (h.eth_hdr.isValid())
a = a | 1;
if (h.h0 == (h0_t){#})
a = a | 2;
h.eth_hdr = (ethernet_t){#};
h.h0.setValid();
h.h0.f0 = a;
}
}

control vrfy(inout headers h, inout Meta m) {
apply {}
}

control update(inout headers hdr, inout Meta m) { apply {
}}

control egress(inout headers h, inout Meta m, inout standard_metadata_t sm) { apply {} }

control deparser(packet_out pkt, in headers h) {
apply {
pkt.emit(h);
}
}
V1Switch(p(), vrfy(), ingress(), egress(), update(), deparser()) main;

0 comments on commit dd43a45

Please sign in to comment.