Skip to content

Commit

Permalink
Test case to validate that #461 is fixed (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Budiu authored Nov 8, 2017
1 parent eeea437 commit 760c41e
Show file tree
Hide file tree
Showing 6 changed files with 775 additions and 0 deletions.
202 changes: 202 additions & 0 deletions testdata/p4_16_samples/issue461-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
Copyright 2017 Cisco Systems, Inc.
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.
*/

#include <core.p4>
#include <v1model.p4>

struct fwd_metadata_t {
bit<32> l2ptr;
bit<24> out_bd;
}

header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}

header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
bit<32> srcAddr;
bit<32> dstAddr;
}

struct metadata {
fwd_metadata_t fwd_metadata;
}

struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
}

action my_drop() {
mark_to_drop();
}

parser ParserImpl(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata)
{
const bit<16> ETHERTYPE_IPV4 = 16w0x0800;

state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
ETHERTYPE_IPV4: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition accept;
}
state start {
transition parse_ethernet;
}
}

control ingress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
// 2017-Apr-08 version of p4c-bm2-ss gives warning that direct
// counter ipv4_da_lpm_stats is not used. However, if you comment
// out the following line, it gives errors for the later lines
// that call ipv4_da_lpm_stats.count(). The warning seems like a
// bug.
direct_counter(CounterType.packets) ipv4_da_lpm_stats;
action set_l2ptr(bit<32> l2ptr) {
ipv4_da_lpm_stats.count();
meta.fwd_metadata.l2ptr = l2ptr;
}
action drop_with_count() {
ipv4_da_lpm_stats.count();
mark_to_drop();
}
action set_bd_dmac_intf(bit<24> bd, bit<48> dmac, bit<9> intf) {
meta.fwd_metadata.out_bd = bd;
hdr.ethernet.dstAddr = dmac;
standard_metadata.egress_spec = intf;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}
table ipv4_da_lpm {
actions = {
set_l2ptr;
drop_with_count;
}
key = {
hdr.ipv4.dstAddr: lpm;
}
default_action = drop_with_count;
counters = ipv4_da_lpm_stats;
}
table mac_da {
actions = {
set_bd_dmac_intf;
my_drop;
}
key = {
meta.fwd_metadata.l2ptr: exact;
}
default_action = my_drop;
}
apply {
ipv4_da_lpm.apply();
mac_da.apply();
}
}

control egress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata)
{
action rewrite_mac(bit<48> smac) {
hdr.ethernet.srcAddr = smac;
}
table send_frame {
actions = {
rewrite_mac;
my_drop;
}
key = {
meta.fwd_metadata.out_bd: exact;
}
default_action = my_drop;
}
apply {
send_frame.apply();
}
}

control DeparserImpl(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
}
}

control verifyChecksum(inout headers hdr, inout metadata meta) {
apply {
verify_checksum(hdr.ipv4.ihl == 4w5,
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr },
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
}
}

control computeChecksum(inout headers hdr, inout metadata meta) {
apply {
update_checksum(hdr.ipv4.ihl == 4w5,
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr },
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
}
}

V1Switch(ParserImpl(),
verifyChecksum(),
ingress(),
egress(),
computeChecksum(),
DeparserImpl()) main;
141 changes: 141 additions & 0 deletions testdata/p4_16_samples_outputs/issue461-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <core.p4>
#include <v1model.p4>

struct fwd_metadata_t {
bit<32> l2ptr;
bit<24> out_bd;
}

header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}

header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
bit<32> srcAddr;
bit<32> dstAddr;
}

struct metadata {
fwd_metadata_t fwd_metadata;
}

struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
}

action my_drop() {
mark_to_drop();
}
parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
const bit<16> ETHERTYPE_IPV4 = 16w0x800;
state parse_ethernet {
packet.extract<ethernet_t>(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
16w0x800: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract<ipv4_t>(hdr.ipv4);
transition accept;
}
state start {
transition parse_ethernet;
}
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
direct_counter(CounterType.packets) ipv4_da_lpm_stats;
action set_l2ptr(bit<32> l2ptr) {
ipv4_da_lpm_stats.count();
meta.fwd_metadata.l2ptr = l2ptr;
}
action drop_with_count() {
ipv4_da_lpm_stats.count();
mark_to_drop();
}
action set_bd_dmac_intf(bit<24> bd, bit<48> dmac, bit<9> intf) {
meta.fwd_metadata.out_bd = bd;
hdr.ethernet.dstAddr = dmac;
standard_metadata.egress_spec = intf;
hdr.ipv4.ttl = hdr.ipv4.ttl + 8w255;
}
table ipv4_da_lpm {
actions = {
set_l2ptr();
drop_with_count();
}
key = {
hdr.ipv4.dstAddr: lpm @name("hdr.ipv4.dstAddr") ;
}
default_action = drop_with_count();
counters = ipv4_da_lpm_stats;
}
table mac_da {
actions = {
set_bd_dmac_intf();
my_drop();
}
key = {
meta.fwd_metadata.l2ptr: exact @name("meta.fwd_metadata.l2ptr") ;
}
default_action = my_drop();
}
apply {
ipv4_da_lpm.apply();
mac_da.apply();
}
}

control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
action rewrite_mac(bit<48> smac) {
hdr.ethernet.srcAddr = smac;
}
table send_frame {
actions = {
rewrite_mac();
my_drop();
}
key = {
meta.fwd_metadata.out_bd: exact @name("meta.fwd_metadata.out_bd") ;
}
default_action = my_drop();
}
apply {
send_frame.apply();
}
}

control DeparserImpl(packet_out packet, in headers hdr) {
apply {
packet.emit<ethernet_t>(hdr.ethernet);
packet.emit<ipv4_t>(hdr.ipv4);
}
}

control verifyChecksum(inout headers hdr, inout metadata meta) {
apply {
verify_checksum<tuple<bit<4>, bit<4>, bit<8>, bit<16>, bit<16>, bit<3>, bit<13>, bit<8>, bit<8>, bit<32>, bit<32>>, bit<16>>(hdr.ipv4.ihl == 4w5, { hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr }, hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
}
}

control computeChecksum(inout headers hdr, inout metadata meta) {
apply {
update_checksum<tuple<bit<4>, bit<4>, bit<8>, bit<16>, bit<16>, bit<3>, bit<13>, bit<8>, bit<8>, bit<32>, bit<32>>, bit<16>>(hdr.ipv4.ihl == 4w5, { hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr }, hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
}
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;
Loading

0 comments on commit 760c41e

Please sign in to comment.