Skip to content

Commit

Permalink
Add example of a program where string comparison are folded.
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy committed Aug 13, 2024
1 parent e89f843 commit 8ef288e
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 0 deletions.
59 changes: 59 additions & 0 deletions testdata/p4_16_samples/string_ops-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

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

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

void set_hdr(inout bit<48> addr, in bool option) {
if (option) {
addr = 0xAABBCCDDEEFF;
}
}

parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
set_hdr(h.eth_hdr.dst_addr, "a" != "b");
set_hdr(h.eth_hdr.src_addr, "a" == "b");
}
}

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

control update(inout Headers h, 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;
58 changes: 58 additions & 0 deletions testdata/p4_16_samples_outputs/string_ops-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

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

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

void set_hdr(inout bit<48> addr, in bool option) {
if (option) {
addr = 48w0xaabbccddeeff;
}
}
parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract<ethernet_t>(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
set_hdr(h.eth_hdr.dst_addr, true);
set_hdr(h.eth_hdr.src_addr, false);
}
}

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

control update(inout Headers h, 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<Headers>(h);
}
}

V1Switch<Headers, Meta>(p(), vrfy(), ingress(), egress(), update(), deparser()) main;
67 changes: 67 additions & 0 deletions testdata/p4_16_samples_outputs/string_ops-bmv2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

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

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract<ethernet_t>(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
@name("ingress.addr_0") bit<48> addr;
@name("ingress.option_0") bool option;
@name("ingress.addr_1") bit<48> addr_2;
@name("ingress.option_1") bool option_2;
apply {
addr = h.eth_hdr.dst_addr;
option = true;
if (option) {
addr = 48w0xaabbccddeeff;
}
h.eth_hdr.dst_addr = addr;
addr_2 = h.eth_hdr.src_addr;
option_2 = false;
if (option_2) {
addr_2 = 48w0xaabbccddeeff;
}
h.eth_hdr.src_addr = addr_2;
}
}

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

control update(inout Headers h, 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<Headers>(h);
}
}

V1Switch<Headers, Meta>(p(), vrfy(), ingress(), egress(), update(), deparser()) main;
61 changes: 61 additions & 0 deletions testdata/p4_16_samples_outputs/string_ops-bmv2-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

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

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract<ethernet_t>(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
@hidden action act() {
h.eth_hdr.dst_addr = 48w0xaabbccddeeff;
}
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
apply {
tbl_act.apply();
}
}

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

control update(inout Headers h, 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<ethernet_t>(h.eth_hdr);
}
}

V1Switch<Headers, Meta>(p(), vrfy(), ingress(), egress(), update(), deparser()) main;
58 changes: 58 additions & 0 deletions testdata/p4_16_samples_outputs/string_ops-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

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

struct Headers {
ethernet_t eth_hdr;
}

struct Meta {
}

void set_hdr(inout bit<48> addr, in bool option) {
if (option) {
addr = 0xaabbccddeeff;
}
}
parser p(packet_in pkt, out Headers hdr, inout Meta m, inout standard_metadata_t sm) {
state start {
pkt.extract(hdr.eth_hdr);
transition accept;
}
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
apply {
set_hdr(h.eth_hdr.dst_addr, "a" != "b");
set_hdr(h.eth_hdr.src_addr, "a" == "b");
}
}

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

control update(inout Headers h, 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;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# proto-file: p4/v1/p4runtime.proto
# proto-message: p4.v1.WriteRequest

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# proto-file: p4/config/v1/p4info.proto
# proto-message: p4.config.v1.P4Info

pkg_info {
arch: "v1model"
}

0 comments on commit 8ef288e

Please sign in to comment.