Refactor fabric.p4 INT support to allow compilation on more P4 targets

Also use constant entries for instruction mask tables to avoid
programming them at runtime.

Change-Id: Ia1ab1ecd42a433daec171f9a30bcdba3b8484061
diff --git a/pipelines/fabric/src/main/resources/include/int/int_header.p4 b/pipelines/fabric/src/main/resources/include/int/int_header.p4
new file mode 100644
index 0000000..f339995
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_header.p4
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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 __INT_HEADER__
+#define __INT_HEADER__
+
+#include "../define.p4"
+
+struct int_metadata_t {
+    _BOOL   source;
+    _BOOL   transit;
+    _BOOL   sink;
+    bit<32> switch_id;
+    bit<8>  new_words;
+    bit<16> new_bytes;
+}
+
+// INT headers - 8 bytes
+header int_header_t {
+    bit<2>  ver;
+    bit<2>  rep;
+    bit<1>  c;
+    bit<1>  e;
+    bit<5>  rsvd1;
+    bit<5>  ins_cnt;
+    bit<8>  max_hop_cnt;
+    bit<8>  total_hop_cnt;
+    bit<4>  instruction_mask_0003; /* split the bits for lookup */
+    bit<4>  instruction_mask_0407;
+    bit<4>  instruction_mask_0811;
+    bit<4>  instruction_mask_1215;
+    bit<16> rsvd2;
+}
+
+// INT shim header for TCP/UDP - 4 bytes
+header intl4_shim_t {
+    bit<8> int_type;
+    bit<8> rsvd1;
+    bit<8> len_words; // 4-byte words.
+    bit<8> rsvd2;
+}
+// INT tail header for TCP/UDP - 4 bytes
+header intl4_tail_t {
+    bit<8> next_proto;
+    bit<16> dest_port;
+    bit<2> padding;
+    bit<6> dscp;
+}
+
+header int_data_t {
+    // Maximum int metadata stack size in bits:
+    // (0xFF -4) * 32 (excluding INT shim header, tail header and INT header)
+    varbit<8032> data;
+}
+
+#ifdef WITH_INT_TRANSIT
+// INT meta-value headers - 4 bytes each
+// Different header for each value type
+header int_switch_id_t {
+    bit<32> switch_id;
+}
+header int_port_ids_t {
+    bit<16> ingress_port_id;
+    bit<16> egress_port_id;
+}
+header int_hop_latency_t {
+    bit<32> hop_latency;
+}
+header int_q_occupancy_t {
+    bit<8> q_id;
+    bit<24> q_occupancy;
+}
+header int_ingress_tstamp_t {
+    bit<32> ingress_tstamp;
+}
+header int_egress_tstamp_t {
+    bit<32> egress_tstamp;
+}
+header int_q_congestion_t {
+    bit<8> q_id;
+    bit<24> q_congestion;
+}
+header int_egress_port_tx_util_t {
+    bit<32> egress_port_tx_util;
+}
+#endif // WITH_INT_TRANSIT
+
+#ifdef WITH_INT_SINK
+// Report Telemetry Headers
+header report_fixed_header_t {
+    bit<4>  ver;
+    bit<4>  nproto;
+    bit<1>  d;
+    bit<1>  q;
+    bit<1>  f;
+    bit<15> rsvd;
+    bit<6>  hw_id;
+    bit<32> seq_no;
+    bit<32> ingress_tstamp;
+}
+
+// Telemetry drop report header
+header drop_report_header_t {
+    bit<32> switch_id;
+    bit<16> ingress_port_id;
+    bit<16> egress_port_id;
+    bit<8>  queue_id;
+    bit<8>  drop_reason;
+    bit<16> pad;
+}
+
+// Switch Local Report Header
+header local_report_header_t {
+    bit<32> switch_id;
+    bit<16> ingress_port_id;
+    bit<16> egress_port_id;
+    bit<8>  queue_id;
+    bit<24> queue_occupancy;
+    bit<32> egress_tstamp;
+}
+
+header_union local_report_t {
+    drop_report_header_t drop_report_header;
+    local_report_header_t local_report_header;
+}
+#endif // WITH_INT_SINK
+
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/int/int_main.p4 b/pipelines/fabric/src/main/resources/include/int/int_main.p4
new file mode 100644
index 0000000..ef81cc9
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_main.p4
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_MAIN__
+#define __INT_MAIN__
+
+#ifdef WITH_INT_SOURCE
+#include "int_source.p4"
+#endif // WITH_INT_SOURCE
+
+#ifdef WITH_INT_TRANSIT
+#include "int_transit.p4"
+#endif // WITH_INT_TRANSIT
+
+#ifdef WITH_INT_SINK
+#include "int_sink.p4"
+#include "int_report.p4"
+#endif // WITH_INT_SINK
+
+control process_set_source_sink (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata) {
+
+    direct_counter(CounterType.packets_and_bytes) counter_set_source;
+
+    action int_set_source () {
+        fabric_metadata.int_meta.source = _TRUE;
+        counter_set_source.count();
+    }
+
+    table tb_set_source {
+        key = {
+            standard_metadata.ingress_port: exact;
+        }
+        actions = {
+            int_set_source;
+        }
+        counters = counter_set_source;
+        size = MAX_PORTS;
+    }
+
+#ifdef WITH_INT_SINK
+    direct_counter(CounterType.packets_and_bytes) counter_set_sink;
+
+    action int_set_sink () {
+        fabric_metadata.int_meta.sink = _TRUE;
+        counter_set_sink.count();
+    }
+
+    table tb_set_sink {
+        key = {
+            standard_metadata.egress_spec: exact;
+        }
+        actions = {
+            int_set_sink;
+        }
+        counters = counter_set_sink;
+        size = MAX_PORTS;
+    }
+#endif // WITH_INT_SINK
+
+    apply {
+        tb_set_source.apply();
+
+#ifdef WITH_INT_SINK
+        tb_set_sink.apply();
+        if(fabric_metadata.int_meta.sink == _TRUE) {
+            // FIXME: this works only on BMv2
+            #ifdef __TARGET_BMV2__
+            clone(CloneType.I2E, REPORT_MIRROR_SESSION_ID);
+            #endif
+        }
+#endif // WITH_INT_SINK
+    }
+}
+
+control process_int_main (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata) {
+
+    apply {
+        if (standard_metadata.ingress_port != CPU_PORT &&
+            standard_metadata.egress_port != CPU_PORT &&
+            (hdr.udp.isValid() || hdr.tcp.isValid())) {
+#ifdef WITH_INT_SOURCE
+            if (fabric_metadata.int_meta.source == _TRUE) {
+                process_int_source.apply(hdr, fabric_metadata, standard_metadata);
+            }
+#endif // WITH_INT_SOURCE
+            if(hdr.int_header.isValid()) {
+#ifdef WITH_INT_TRANSIT
+                process_int_transit.apply(hdr, fabric_metadata, standard_metadata);
+#endif // WITH_INT_TRANSIT
+#ifdef WITH_INT_SINK
+                if (standard_metadata.instance_type == PKT_INSTANCE_TYPE_INGRESS_CLONE) {
+                    /* send int report */
+                    process_int_report.apply(hdr, fabric_metadata, standard_metadata);
+                }
+                if (fabric_metadata.int_meta.sink == _TRUE) {
+                    // int sink
+                    process_int_sink.apply(hdr, fabric_metadata);
+                }
+#endif // WITH_INT_SINK
+            }
+        }
+    }
+}
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/int/int_report.p4 b/pipelines/fabric/src/main/resources/include/int/int_report.p4
new file mode 100644
index 0000000..9326375
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_report.p4
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_REPORT__
+#define __INT_REPORT__
+
+control process_int_report (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata) {
+
+    action add_report_fixed_header() {
+        /* Device should include its own INT metadata as embedded,
+         * we'll not use fabric_report_header for this purpose.
+         */
+        hdr.report_fixed_header.setValid();
+        hdr.report_fixed_header.ver = 0;
+        /* only support for flow_watchlist */
+        hdr.report_fixed_header.nproto = NPROTO_ETHERNET;
+        hdr.report_fixed_header.d = 0;
+        hdr.report_fixed_header.q = 0;
+        hdr.report_fixed_header.f = 1;
+        hdr.report_fixed_header.rsvd = 0;
+        //TODO how to get information specific to the switch
+        hdr.report_fixed_header.hw_id = HW_ID;
+        // TODO how save a variable and increment
+        hdr.report_fixed_header.seq_no = 0;
+        //TODO how to get timestamp from ingress ns
+        hdr.report_fixed_header.ingress_tstamp = (bit<32>) standard_metadata.enq_timestamp;
+    }
+
+    action do_report_encapsulation(mac_addr_t src_mac, mac_addr_t mon_mac, ipv4_addr_t src_ip,
+                        ipv4_addr_t mon_ip, l4_port_t mon_port) {
+        //Report Ethernet Header
+        hdr.report_ethernet.setValid();
+        hdr.report_ethernet.dst_addr = mon_mac;
+        hdr.report_ethernet.src_addr = src_mac;
+        hdr.report_ethernet.ether_type = ETHERTYPE_IPV4;
+
+        //Report IPV4 Header
+        hdr.report_ipv4.setValid();
+        hdr.report_ipv4.version = 4w4;
+        hdr.report_ipv4.ihl = 4w5;
+        hdr.report_ipv4.dscp = 6w0;
+        hdr.report_ipv4.ecn = 2w0;
+        /* Total Len is report_ipv4_len + report_udp_len + report_fixed_hdr_len + ethernet_len + ipv4_totalLen */
+        hdr.report_ipv4.total_len = (bit<16>) IPV4_MIN_HEAD_LEN + (bit<16>) UDP_HEADER_LEN +
+                                (bit<16>) REPORT_FIXED_HEADER_LEN +  (bit<16>) ETH_HEADER_LEN + hdr.ipv4.total_len;
+        /* Dont Fragment bit should be set */
+        hdr.report_ipv4.identification = 0;
+        hdr.report_ipv4.flags = 0;
+        hdr.report_ipv4.frag_offset = 0;
+        hdr.report_ipv4.ttl = 0xFF;
+        hdr.report_ipv4.protocol = PROTO_UDP;
+        hdr.report_ipv4.src_addr = src_ip;
+        hdr.report_ipv4.dst_addr = mon_ip;
+
+        //Report UDP Header
+        hdr.report_udp.setValid();
+        hdr.report_udp.src_port = 0;
+        hdr.report_udp.dst_port = mon_port;
+        hdr.report_udp.len =  (bit<16>) UDP_HEADER_LEN + (bit<16>) REPORT_FIXED_HEADER_LEN +
+                                    (bit<16>) ETH_HEADER_LEN + hdr.ipv4.total_len;
+
+        add_report_fixed_header();
+    }
+
+    /* Cloned packet instance_type is PKT_INSTANCE_TYPE_INGRESS_CLONE=1
+     * Packet is forwarded according to the mirroring_add command
+     */
+    table tb_generate_report {
+        key = {
+        }
+        actions = {
+            do_report_encapsulation;
+        }
+    }
+
+    apply {
+        tb_generate_report.apply();
+    }
+}
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/int/int_sink.p4 b/pipelines/fabric/src/main/resources/include/int/int_sink.p4
new file mode 100644
index 0000000..6c64e32
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_sink.p4
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_SINK__
+#define __INT_SINK__
+
+control process_int_sink (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata) {
+
+    action restore_header () {
+        hdr.udp.dst_port = hdr.intl4_tail.dest_port;
+        hdr.ipv4.dscp = hdr.intl4_tail.dscp;
+    }
+
+    action int_sink() {
+        // restore length fields of IPv4 header and UDP header
+        bit<16> len_bytes = (bit<16>) (hdr.intl4_shim.len_words << 5w2);
+        hdr.ipv4.total_len = hdr.ipv4.total_len - len_bytes;
+        hdr.udp.len = hdr.udp.len - len_bytes;
+        // remove all the INT information from the packet
+        hdr.int_header.setInvalid();
+        hdr.int_data.setInvalid();
+        hdr.intl4_shim.setInvalid();
+        hdr.intl4_tail.setInvalid();
+        hdr.int_switch_id.setInvalid();
+        hdr.int_port_ids.setInvalid();
+        hdr.int_hop_latency.setInvalid();
+        hdr.int_q_occupancy.setInvalid();
+        hdr.int_ingress_tstamp.setInvalid();
+        hdr.int_egress_tstamp.setInvalid();
+        hdr.int_q_congestion.setInvalid();
+        hdr.int_egress_tx_util.setInvalid();
+    }
+
+    apply {
+        restore_header();
+        int_sink();
+    }
+}
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/int/int_source.p4 b/pipelines/fabric/src/main/resources/include/int/int_source.p4
new file mode 100644
index 0000000..245fe7e
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_source.p4
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_SOURCE__
+#define __INT_SOURCE__
+
+// Insert INT header to the packet
+control process_int_source (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata) {
+
+    direct_counter(CounterType.packets_and_bytes) counter_int_source;
+
+    action int_source(bit<8> max_hop, bit<5> ins_cnt, bit<4> ins_mask0003, bit<4> ins_mask0407) {
+        // Insert INT shim header.
+        hdr.intl4_shim.setValid();
+        // int_type: Hop-by-hop type (1) , destination type (2)
+        hdr.intl4_shim.int_type = 1;
+        hdr.intl4_shim.len_words = INT_HEADER_LEN_WORDS;
+        // Insert INT header.
+        hdr.int_header.setValid();
+        hdr.int_header.ver = 0;
+        hdr.int_header.rep = 0;
+        hdr.int_header.c = 0;
+        hdr.int_header.e = 0;
+        hdr.int_header.rsvd1 = 0;
+        hdr.int_header.ins_cnt = ins_cnt;
+        hdr.int_header.max_hop_cnt = max_hop;
+        hdr.int_header.total_hop_cnt = 0;
+        hdr.int_header.instruction_mask_0003 = ins_mask0003;
+        hdr.int_header.instruction_mask_0407 = ins_mask0407;
+        hdr.int_header.instruction_mask_0811 = 0; // not supported
+        hdr.int_header.instruction_mask_1215 = 0; // not supported
+        // Insert INT tail header.
+        hdr.intl4_tail.setValid();
+        hdr.intl4_tail.next_proto = hdr.ipv4.protocol;
+        hdr.intl4_tail.dest_port = fabric_metadata.l4_dst_port;
+        hdr.intl4_tail.dscp = hdr.ipv4.dscp;
+        // Update IP and UDP (if not valid we don't care) lens (in bytes).
+        hdr.ipv4.total_len = hdr.ipv4.total_len + INT_HEADER_LEN_BYTES;
+        hdr.udp.len = hdr.udp.len + INT_HEADER_LEN_BYTES;
+    }
+
+    action int_source_dscp(bit<8> max_hop, bit<5> ins_cnt, bit<4> ins_mask0003, bit<4> ins_mask0407) {
+        int_source(max_hop, ins_cnt, ins_mask0003, ins_mask0407);
+        hdr.ipv4.dscp = INT_DSCP;
+        counter_int_source.count();
+    }
+
+    table tb_int_source {
+        key = {
+            hdr.ipv4.src_addr: ternary;
+            hdr.ipv4.dst_addr: ternary;
+            fabric_metadata.l4_src_port: ternary;
+            fabric_metadata.l4_dst_port: ternary;
+        }
+        actions = {
+            int_source_dscp;
+        }
+        counters = counter_int_source;
+    }
+
+    apply {
+        tb_int_source.apply();
+    }
+}
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/int/int_transit.p4 b/pipelines/fabric/src/main/resources/include/int/int_transit.p4
new file mode 100644
index 0000000..4d4568b
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/int/int_transit.p4
@@ -0,0 +1,389 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_TRANSIT__
+#define __INT_TRANSIT__
+control process_int_transit (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fmeta,
+    inout standard_metadata_t smeta) {
+
+    action init_metadata(bit<32> switch_id) {
+        fmeta.int_meta.transit = _TRUE;
+#ifdef _INT_INIT_METADATA
+        // Allow other targets to initialize INT metadata in their own way.
+        _INT_INIT_METADATA
+#else
+        fmeta.int_meta.switch_id = switch_id;
+#endif // _INT_INIT_METADATA
+    }
+
+#ifdef _INT_METADATA_ACTIONS
+    _INT_METADATA_ACTIONS
+#else
+    // Switch ID.
+    action int_set_header_0() {
+        hdr.int_switch_id.setValid();
+        hdr.int_switch_id.switch_id = fmeta.int_meta.switch_id;
+    }
+    // Port IDs.
+    action int_set_header_1() {
+        hdr.int_port_ids.setValid();
+        hdr.int_port_ids.ingress_port_id = (bit<16>) smeta.ingress_port;
+        hdr.int_port_ids.egress_port_id = (bit<16>) smeta.egress_port;
+    }
+    // Hop latency.
+    action int_set_header_2() {
+        hdr.int_hop_latency.setValid();
+        hdr.int_hop_latency.hop_latency = (bit<32>) smeta.deq_timedelta;
+    }
+    // Queue occupancy.
+    action int_set_header_3() {
+        hdr.int_q_occupancy.setValid();
+        // TODO: support queues in BMv2. ATM we assume only one.
+        hdr.int_q_occupancy.q_id = 8w0;
+        hdr.int_q_occupancy.q_occupancy = (bit<24>) smeta.deq_qdepth;
+    }
+    // Ingress timestamp.
+    action int_set_header_4() {
+        hdr.int_ingress_tstamp.setValid();
+        hdr.int_ingress_tstamp.ingress_tstamp = (bit<32>) smeta.enq_timestamp;
+    }
+    // Egress timestamp.
+    action int_set_header_5() {
+        hdr.int_egress_tstamp.setValid();
+        hdr.int_egress_tstamp.egress_tstamp = (bit<32>) smeta.enq_timestamp + (bit<32>) smeta.deq_timedelta;
+    }
+    // Queue congestion.
+    action int_set_header_6() {
+        hdr.int_q_congestion.setValid();
+        // TODO: support queue congestion.
+        hdr.int_q_congestion.q_id = 8w0;
+        hdr.int_q_congestion.q_congestion = 24w0;
+    }
+    // Egress port utilization.
+    action int_set_header_7() {
+        hdr.int_egress_tx_util.setValid();
+        // TODO: implement tx utilization support in BMv2.
+        hdr.int_egress_tx_util.egress_port_tx_util = 32w0;
+    }
+#endif // _INT_METADATA_ACTIONS
+
+    // Actions to keep track of the new metadata added.
+    action add_1() {
+        fmeta.int_meta.new_words = fmeta.int_meta.new_words + 1;
+        fmeta.int_meta.new_bytes = fmeta.int_meta.new_bytes + 4;
+    }
+
+    action add_2() {
+        fmeta.int_meta.new_words = fmeta.int_meta.new_words + 2;
+        fmeta.int_meta.new_bytes = fmeta.int_meta.new_bytes + 8;
+    }
+
+    action add_3() {
+        fmeta.int_meta.new_words = fmeta.int_meta.new_words + 3;
+        fmeta.int_meta.new_bytes = fmeta.int_meta.new_bytes + 12;
+    }
+
+    action add_4() {
+        fmeta.int_meta.new_words = fmeta.int_meta.new_words + 4;
+        fmeta.int_meta.new_bytes = fmeta.int_meta.new_bytes + 16;
+    }
+
+    // Action function for bits 0-3 combinations, 0 is msb, 3 is lsb.
+    // Each bit set indicates that corresponding INT header should be added.
+    action int_set_header_0003_i0() {
+    }
+    action int_set_header_0003_i1() {
+        int_set_header_3();
+        add_1();
+    }
+    action int_set_header_0003_i2() {
+        int_set_header_2();
+        add_1();
+    }
+    action int_set_header_0003_i3() {
+        int_set_header_3();
+        int_set_header_2();
+        add_2();
+    }
+    action int_set_header_0003_i4() {
+        int_set_header_1();
+        add_1();
+    }
+    action int_set_header_0003_i5() {
+        int_set_header_3();
+        int_set_header_1();
+        add_2();
+    }
+    action int_set_header_0003_i6() {
+        int_set_header_2();
+        int_set_header_1();
+        add_2();
+    }
+    action int_set_header_0003_i7() {
+        int_set_header_3();
+        int_set_header_2();
+        int_set_header_1();
+        add_3();
+    }
+    action int_set_header_0003_i8() {
+        int_set_header_0();
+        add_1();
+    }
+    action int_set_header_0003_i9() {
+        int_set_header_3();
+        int_set_header_0();
+        add_2();
+    }
+    action int_set_header_0003_i10() {
+        int_set_header_2();
+        int_set_header_0();
+        add_2();
+    }
+    action int_set_header_0003_i11() {
+        int_set_header_3();
+        int_set_header_2();
+        int_set_header_0();
+        add_3();
+    }
+    action int_set_header_0003_i12() {
+        int_set_header_1();
+        int_set_header_0();
+        add_2();
+    }
+    action int_set_header_0003_i13() {
+        int_set_header_3();
+        int_set_header_1();
+        int_set_header_0();
+        add_3();
+    }
+    action int_set_header_0003_i14() {
+        int_set_header_2();
+        int_set_header_1();
+        int_set_header_0();
+        add_3();
+    }
+    action int_set_header_0003_i15() {
+        int_set_header_3();
+        int_set_header_2();
+        int_set_header_1();
+        int_set_header_0();
+        add_4();
+    }
+
+    // Action function for bits 4-7 combinations, 4 is msb, 7 is lsb.
+    action int_set_header_0407_i0() {
+    }
+    action int_set_header_0407_i1() {
+        int_set_header_7();
+        add_1();
+    }
+    action int_set_header_0407_i2() {
+        int_set_header_6();
+        add_1();
+    }
+    action int_set_header_0407_i3() {
+        int_set_header_7();
+        int_set_header_6();
+        add_2();
+    }
+    action int_set_header_0407_i4() {
+        int_set_header_5();
+        add_1();
+    }
+    action int_set_header_0407_i5() {
+        int_set_header_7();
+        int_set_header_5();
+        add_2();
+    }
+    action int_set_header_0407_i6() {
+        int_set_header_6();
+        int_set_header_5();
+        add_2();
+    }
+    action int_set_header_0407_i7() {
+        int_set_header_7();
+        int_set_header_6();
+        int_set_header_5();
+        add_3();
+    }
+    action int_set_header_0407_i8() {
+        int_set_header_4();
+        add_1();
+    }
+    action int_set_header_0407_i9() {
+        int_set_header_7();
+        int_set_header_4();
+        add_2();
+    }
+    action int_set_header_0407_i10() {
+        int_set_header_6();
+        int_set_header_4();
+        add_2();
+    }
+    action int_set_header_0407_i11() {
+        int_set_header_7();
+        int_set_header_6();
+        int_set_header_4();
+        add_3();
+    }
+    action int_set_header_0407_i12() {
+        int_set_header_5();
+        int_set_header_4();
+        add_2();
+    }
+    action int_set_header_0407_i13() {
+        int_set_header_7();
+        int_set_header_5();
+        int_set_header_4();
+        add_3();
+    }
+    action int_set_header_0407_i14() {
+        int_set_header_6();
+        int_set_header_5();
+        int_set_header_4();
+        add_3();
+    }
+    action int_set_header_0407_i15() {
+        int_set_header_7();
+        int_set_header_6();
+        int_set_header_5();
+        int_set_header_4();
+        add_4();
+    }
+
+    // Default action used to set switch ID.
+    table tb_int_insert {
+        key = {}
+        actions = {
+            init_metadata;
+        }
+        size = 0;
+    }
+
+    // Table to process instruction bits 0-3.
+    table tb_int_inst_0003 {
+        key = {
+            hdr.int_header.instruction_mask_0003 : exact;
+        }
+        actions = {
+            int_set_header_0003_i0;
+            int_set_header_0003_i1;
+            int_set_header_0003_i2;
+            int_set_header_0003_i3;
+            int_set_header_0003_i4;
+            int_set_header_0003_i5;
+            int_set_header_0003_i6;
+            int_set_header_0003_i7;
+            int_set_header_0003_i8;
+            int_set_header_0003_i9;
+            int_set_header_0003_i10;
+            int_set_header_0003_i11;
+            int_set_header_0003_i12;
+            int_set_header_0003_i13;
+            int_set_header_0003_i14;
+            int_set_header_0003_i15;
+        }
+        size = 16;
+        const entries = {
+            (0x0) : int_set_header_0003_i0();
+            (0x1) : int_set_header_0003_i1();
+            (0x2) : int_set_header_0003_i2();
+            (0x3) : int_set_header_0003_i3();
+            (0x4) : int_set_header_0003_i4();
+            (0x5) : int_set_header_0003_i5();
+            (0x6) : int_set_header_0003_i6();
+            (0x7) : int_set_header_0003_i7();
+            (0x8) : int_set_header_0003_i8();
+            (0x9) : int_set_header_0003_i9();
+            (0xA) : int_set_header_0003_i10();
+            (0xB) : int_set_header_0003_i11();
+            (0xC) : int_set_header_0003_i12();
+            (0xD) : int_set_header_0003_i13();
+            (0xE) : int_set_header_0003_i14();
+            (0xF) : int_set_header_0003_i15();
+        }
+    }
+
+    // Table to process instruction bits 4-7.
+    table tb_int_inst_0407 {
+        key = {
+            hdr.int_header.instruction_mask_0407 : exact;
+        }
+        actions = {
+            int_set_header_0407_i0;
+            int_set_header_0407_i1;
+            int_set_header_0407_i2;
+            int_set_header_0407_i3;
+            int_set_header_0407_i4;
+            int_set_header_0407_i5;
+            int_set_header_0407_i6;
+            int_set_header_0407_i7;
+            int_set_header_0407_i8;
+            int_set_header_0407_i9;
+            int_set_header_0407_i10;
+            int_set_header_0407_i11;
+            int_set_header_0407_i12;
+            int_set_header_0407_i13;
+            int_set_header_0407_i14;
+            int_set_header_0407_i15;
+        }
+        size = 16;
+        const entries = {
+            (0x0) : int_set_header_0407_i0();
+            (0x1) : int_set_header_0407_i1();
+            (0x2) : int_set_header_0407_i2();
+            (0x3) : int_set_header_0407_i3();
+            (0x4) : int_set_header_0407_i4();
+            (0x5) : int_set_header_0407_i5();
+            (0x6) : int_set_header_0407_i6();
+            (0x7) : int_set_header_0407_i7();
+            (0x8) : int_set_header_0407_i8();
+            (0x9) : int_set_header_0407_i9();
+            (0xA) : int_set_header_0407_i10();
+            (0xB) : int_set_header_0407_i11();
+            (0xC) : int_set_header_0407_i12();
+            (0xD) : int_set_header_0407_i13();
+            (0xE) : int_set_header_0407_i14();
+            (0xF) : int_set_header_0407_i15();
+        }
+    }
+
+    apply {
+        tb_int_insert.apply();
+        if (fmeta.int_meta.transit == _FALSE) {
+            return;
+        }
+        tb_int_inst_0003.apply();
+        tb_int_inst_0407.apply();
+        // Increment hop cnt
+        hdr.int_header.total_hop_cnt = hdr.int_header.total_hop_cnt + 1;
+        // Update headers lengths.
+        if (hdr.ipv4.isValid()) {
+            hdr.ipv4.total_len = hdr.ipv4.total_len + fmeta.int_meta.new_bytes;
+        }
+        if (hdr.udp.isValid()) {
+            hdr.udp.len = hdr.udp.len + fmeta.int_meta.new_bytes;
+        }
+        if (hdr.intl4_shim.isValid()) {
+            hdr.intl4_shim.len_words = hdr.intl4_shim.len_words + fmeta.int_meta.new_words;
+        }
+    }
+}
+
+#endif