Minor refactoring for fabric.p4

Change-Id: I736b20fc95b4a03afa1074eb777a23507530b44f
diff --git a/pipelines/fabric/src/main/resources/include/action.p4 b/pipelines/fabric/src/main/resources/include/action.p4
index c76334a..3ce1f0d 100644
--- a/pipelines/fabric/src/main/resources/include/action.p4
+++ b/pipelines/fabric/src/main/resources/include/action.p4
@@ -17,12 +17,6 @@
 #ifndef __ACTION__
 #define __ACTION__
 #include "header.p4"
-
 action nop() {
 }
-
-action drop() {
-    mark_to_drop();
-}
-
 #endif
diff --git a/pipelines/fabric/src/main/resources/include/checksum.p4 b/pipelines/fabric/src/main/resources/include/checksum.p4
index 00b4e70..dfcadb6 100644
--- a/pipelines/fabric/src/main/resources/include/checksum.p4
+++ b/pipelines/fabric/src/main/resources/include/checksum.p4
@@ -21,6 +21,7 @@
                               inout fabric_metadata_t meta)
 {
     apply {
+#ifdef TARGET_BMV2
         update_checksum(hdr.ipv4.isValid(),
             {
                 hdr.ipv4.version,
@@ -38,6 +39,7 @@
             hdr.ipv4.hdr_checksum,
             HashAlgorithm.csum16
         );
+#endif
     }
 }
 
@@ -45,6 +47,7 @@
                              inout fabric_metadata_t meta)
 {
     apply {
+#ifdef TARGET_BMV2
         verify_checksum(hdr.ipv4.isValid(),
             {
                 hdr.ipv4.version,
@@ -62,6 +65,7 @@
             hdr.ipv4.hdr_checksum,
             HashAlgorithm.csum16
         );
+#endif
     }
 }
 
diff --git a/pipelines/fabric/src/main/resources/include/control/filtering.p4 b/pipelines/fabric/src/main/resources/include/control/filtering.p4
index a66657f..5a93e5b 100644
--- a/pipelines/fabric/src/main/resources/include/control/filtering.p4
+++ b/pipelines/fabric/src/main/resources/include/control/filtering.p4
@@ -25,8 +25,9 @@
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
 
-    direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter;
-    direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter;
+    action drop() {
+        mark_to_drop();
+    }
 
     action set_vlan(vlan_id_t new_vlan_id) {
         hdr.vlan_tag.vlan_id = new_vlan_id;
@@ -38,7 +39,8 @@
         hdr.vlan_tag.setValid();
         hdr.vlan_tag.cfi = 0;
         hdr.vlan_tag.pri = 0;
-        hdr.vlan_tag.ether_type = ETHERTYPE_VLAN;
+        hdr.vlan_tag.ether_type = hdr.ethernet.ether_type;
+        hdr.ethernet.ether_type = ETHERTYPE_VLAN;
         set_vlan(new_vlan_id);
 
         // pop internal vlan before output
@@ -64,7 +66,6 @@
             drop;
         }
         const default_action = drop();
-        counters = ingress_port_vlan_counter;
     }
 
     // Originally TMAC table in OF-DPA pipeline
@@ -72,7 +73,7 @@
         key = {
             standard_metadata.ingress_port: exact;
             hdr.ethernet.dst_addr: exact;
-            hdr.ethernet.ether_type: exact;
+            fabric_metadata.original_ether_type: exact;
         }
 
         actions = {
@@ -80,7 +81,6 @@
         }
 
         const default_action = set_forwarding_type(FWD_BRIDGING);
-        counters = fwd_classifier_counter;
     }
 
     apply {
diff --git a/pipelines/fabric/src/main/resources/include/control/forwarding.p4 b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
index 5015e58..055f77b 100644
--- a/pipelines/fabric/src/main/resources/include/control/forwarding.p4
+++ b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
@@ -27,13 +27,9 @@
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
 
-    direct_counter(CounterType.packets_and_bytes) bridging_counter;
-    direct_counter(CounterType.packets_and_bytes) mpls_counter;
-    direct_counter(CounterType.packets_and_bytes) unicast_v4_counter;
-    direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
-    direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
-    direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
-    direct_counter(CounterType.packets_and_bytes) acl_counter;
+    action drop() {
+        mark_to_drop();
+    }
 
     action set_next_id(next_id_t next_id) {
         fabric_metadata.next_id = next_id;
@@ -41,11 +37,6 @@
 
     action pop_mpls_and_next(next_id_t next_id) {
         hdr.mpls.setInvalid();
-        if (hdr.ipv4.isValid()) {
-            hdr.ethernet.ether_type = ETHERTYPE_IPV4;
-        } else {
-            hdr.ethernet.ether_type = ETHERTYPE_IPV6;
-        }
         fabric_metadata.next_id = next_id;
     }
 
@@ -62,7 +53,6 @@
         actions = {
             set_next_id;
         }
-        counters = bridging_counter;
     }
 
     table mpls {
@@ -73,7 +63,6 @@
         actions = {
             pop_mpls_and_next;
         }
-        counters = mpls_counter;
     }
 
     table unicast_v4 {
@@ -84,7 +73,6 @@
         actions = {
             set_next_id;
         }
-        counters = unicast_v4_counter;
     }
 
     table multicast_v4 {
@@ -96,7 +84,6 @@
         actions = {
             set_next_id;
         }
-        counters = multicast_v4_counter;
     }
 
     table unicast_v6 {
@@ -107,7 +94,6 @@
         actions = {
             set_next_id;
         }
-        counters = unicast_v6_counter;
     }
 
     table multicast_v6 {
@@ -119,33 +105,27 @@
         actions = {
             set_next_id;
         }
-        counters = multicast_v6_counter;
     }
 
     table acl {
         key = {
-            standard_metadata.ingress_port: ternary;
-            fabric_metadata.ip_proto: ternary;
-            hdr.ethernet.dst_addr: ternary;
-            hdr.ethernet.src_addr: ternary;
-            hdr.ethernet.ether_type: ternary;
-            hdr.vlan_tag.vlan_id: ternary;
-            hdr.vlan_tag.pri: ternary;
-            hdr.mpls.tc: ternary;
-            hdr.mpls.bos: ternary;
-            hdr.mpls.label: ternary;
-            hdr.ipv4.src_addr: ternary;
-            hdr.ipv4.dst_addr: ternary;
-            hdr.ipv4.protocol: ternary;
-            hdr.ipv6.src_addr: ternary;
-            hdr.ipv6.dst_addr: ternary;
-            hdr.ipv6.next_hdr: ternary;
-            hdr.tcp.src_port: ternary;
-            hdr.tcp.dst_port: ternary;
-            hdr.udp.src_port: ternary;
-            hdr.udp.dst_port: ternary;
-            hdr.icmp.icmp_type: ternary;
-            hdr.icmp.icmp_code: ternary;
+            standard_metadata.ingress_port: ternary; // 9
+            fabric_metadata.ip_proto: ternary; // 8
+            fabric_metadata.l4_src_port: ternary; // 16
+            fabric_metadata.l4_dst_port: ternary; // 16
+
+            hdr.ethernet.dst_addr: ternary; // 48
+            hdr.ethernet.src_addr: ternary; // 48
+            fabric_metadata.original_ether_type: ternary; //16
+            hdr.vlan_tag.vlan_id: ternary; // 12
+            hdr.mpls.bos: ternary; // 1
+            hdr.mpls.label: ternary; // 20
+            hdr.ipv4.src_addr: ternary; // 32
+            hdr.ipv4.dst_addr: ternary; // 32
+            hdr.ipv6.src_addr: ternary; // 128
+            hdr.ipv6.dst_addr: ternary; // 128
+            hdr.icmp.icmp_type: ternary; // 8
+            hdr.icmp.icmp_code: ternary; // 8
         }
 
         actions = {
@@ -156,12 +136,21 @@
         }
 
         const default_action = nop();
-        counters = acl_counter;
+        size = 256;
     }
 
     apply {
         if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
-        else if (fabric_metadata.fwd_type == FWD_MPLS) mpls.apply();
+        else if (fabric_metadata.fwd_type == FWD_MPLS) {
+            mpls.apply();
+            if (hdr.ipv4.isValid()) {
+                hdr.ethernet.ether_type = ETHERTYPE_IPV4;
+                fabric_metadata.original_ether_type = ETHERTYPE_IPV4;
+            } else {
+                hdr.ethernet.ether_type = ETHERTYPE_IPV6;
+                fabric_metadata.original_ether_type = ETHERTYPE_IPV6;
+            }
+        }
         else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply();
         else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply();
         else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply();
diff --git a/pipelines/fabric/src/main/resources/include/control/next.p4 b/pipelines/fabric/src/main/resources/include/control/next.p4
index 19d9308..a58e88e 100644
--- a/pipelines/fabric/src/main/resources/include/control/next.p4
+++ b/pipelines/fabric/src/main/resources/include/control/next.p4
@@ -24,21 +24,10 @@
     inout parsed_headers_t hdr,
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
-    direct_counter(CounterType.packets_and_bytes) simple_counter;
-    direct_counter(CounterType.packets_and_bytes) hashed_counter;
-    direct_counter(CounterType.packets_and_bytes) broadcast_counter;
     action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector;
 
     action output(port_num_t port_num) {
         standard_metadata.egress_spec = port_num;
-        if(!hdr.mpls.isValid()) {
-            if(hdr.ipv4.isValid()) {
-                hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
-            }
-            else if (hdr.ipv6.isValid()) {
-                hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
-            }
-        }
     }
 
     action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){
@@ -69,25 +58,29 @@
     }
 
     action push_mpls (mpls_label_t label, bit<3> tc) {
-        //Suppose that the maximum number of label is one.
+        // Suppose that the maximum number of label is one.
         hdr.mpls.setValid();
         hdr.ethernet.ether_type = ETHERTYPE_MPLS;
         hdr.mpls.label = label;
         hdr.mpls.tc = tc;
-        hdr.mpls.bos = 1; // BOS = TRUE
+        hdr.mpls.bos = 1w1; // BOS = TRUE
         hdr.mpls.ttl = DEFAULT_MPLS_TTL;
     }
 
     action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
                             mpls_label_t label) {
         l3_routing(port_num, smac, dmac);
-        push_mpls(label, hdr.ipv4.diffserv[7:5]);
+
+        // TODO: set tc according to diffserv from ipv4
+        push_mpls(label, 3w0);
     }
 
     action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
                             mpls_label_t label) {
         l3_routing(port_num, smac, dmac);
-        push_mpls(label, hdr.ipv6.traffic_class[7:5]);
+
+        // TODO: set tc according to traffic_class from ipv4
+        push_mpls(label, 3w0);
     }
 
     table simple {
@@ -100,18 +93,14 @@
             set_vlan_output;
             l3_routing;
         }
-        counters = simple_counter;
     }
 
     table hashed {
         key = {
             fabric_metadata.next_id: exact;
-            hdr.ipv4.src_addr: selector;
-            hdr.ipv4.dst_addr: selector;
-            hdr.ipv4.protocol: selector;
-            hdr.ipv6.src_addr: selector;
-            hdr.ipv6.dst_addr: selector;
-            hdr.ipv6.next_hdr: selector;
+            hdr.ethernet.dst_addr: selector;
+            hdr.ethernet.src_addr: selector;
+            fabric_metadata.ip_proto: selector;
             fabric_metadata.l4_src_port: selector;
             fabric_metadata.l4_dst_port: selector;
         }
@@ -123,7 +112,6 @@
         }
 
         implementation = ecmp_selector;
-        counters = hashed_counter;
     }
 
     /*
@@ -136,11 +124,19 @@
         actions = {
             set_mcast_group;
         }
-        counters = broadcast_counter;
     }
 
     apply {
-        simple.apply();
+        if (simple.apply().hit) {
+            if (!hdr.mpls.isValid()) {
+                if(hdr.ipv4.isValid()) {
+                    hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
+                }
+                else if (hdr.ipv6.isValid()) {
+                    hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
+                }
+            }
+        }
         hashed.apply();
         broadcast.apply();
     }
@@ -154,6 +150,11 @@
     apply {
         // pop internal vlan if the meta is set
         if (fabric_metadata.pop_vlan_at_egress) {
+            if (hdr.mpls.isValid()) {
+                hdr.ethernet.ether_type = ETHERTYPE_MPLS;
+            } else {
+                hdr.ethernet.ether_type = fabric_metadata.original_ether_type;
+            }
             hdr.vlan_tag.setInvalid();
         }
     }
diff --git a/pipelines/fabric/src/main/resources/include/control/packetio.p4 b/pipelines/fabric/src/main/resources/include/control/packetio.p4
index fb5d731..9e0771c 100644
--- a/pipelines/fabric/src/main/resources/include/control/packetio.p4
+++ b/pipelines/fabric/src/main/resources/include/control/packetio.p4
@@ -24,6 +24,7 @@
     apply {
         if (hdr.packet_out.isValid()) {
             standard_metadata.egress_spec = hdr.packet_out.egress_port;
+            hdr.packet_out.setInvalid();
             exit;
         }
     }
@@ -34,7 +35,6 @@
 inout fabric_metadata_t fabric_metadata,
 inout standard_metadata_t standard_metadata) {
     apply {
-        hdr.packet_out.setInvalid();
         if (standard_metadata.egress_port == CPU_PORT) {
             hdr.packet_in.setValid();
             hdr.packet_in.ingress_port = standard_metadata.ingress_port;
diff --git a/pipelines/fabric/src/main/resources/include/define.p4 b/pipelines/fabric/src/main/resources/include/define.p4
index 0ee3ff3..4765824 100644
--- a/pipelines/fabric/src/main/resources/include/define.p4
+++ b/pipelines/fabric/src/main/resources/include/define.p4
@@ -44,8 +44,9 @@
 const bit<8> PROTO_UDP = 17;
 const bit<8> PROTO_ICMPV6 = 58;
 
+#ifndef CPU_PORT
 const port_num_t CPU_PORT = 255;
-const port_num_t DROP_PORT = 511;
+#endif
 
 const fwd_type_t FWD_BRIDGING = 0;
 const fwd_type_t FWD_MPLS = 1;
diff --git a/pipelines/fabric/src/main/resources/include/header.p4 b/pipelines/fabric/src/main/resources/include/header.p4
index 3b153be..df9958b 100644
--- a/pipelines/fabric/src/main/resources/include/header.p4
+++ b/pipelines/fabric/src/main/resources/include/header.p4
@@ -22,11 +22,13 @@
 @controller_header("packet_in")
 header packet_in_header_t {
     port_num_t ingress_port;
+    bit<7> _pad;
 }
 
 @controller_header("packet_out")
 header packet_out_header_t {
     port_num_t egress_port;
+    bit<7> _pad;
 }
 
 header ethernet_t {
@@ -121,6 +123,7 @@
     bit<8> ip_proto;
     bit<16> l4_src_port;
     bit<16> l4_dst_port;
+    bit<16> original_ether_type;
 }
 
 struct parsed_headers_t {
diff --git a/pipelines/fabric/src/main/resources/include/parser.p4 b/pipelines/fabric/src/main/resources/include/parser.p4
index d191ff9..c5653dc 100644
--- a/pipelines/fabric/src/main/resources/include/parser.p4
+++ b/pipelines/fabric/src/main/resources/include/parser.p4
@@ -24,6 +24,7 @@
 out parsed_headers_t hdr,
 inout fabric_metadata_t fabric_metadata,
 inout standard_metadata_t standard_metadata) {
+
     state start {
         transition select(standard_metadata.ingress_port) {
             CPU_PORT: parse_packet_out;
@@ -38,6 +39,7 @@
 
     state parse_ethernet {
         packet.extract(hdr.ethernet);
+        fabric_metadata.original_ether_type = hdr.ethernet.ether_type;
         transition select(hdr.ethernet.ether_type){
             ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
             ETHERTYPE_QINQ: parse_vlan_tag;
@@ -73,7 +75,6 @@
 
     state parse_mpls {
         packet.extract(hdr.mpls);
-
         //There is only one MPLS label for this fabric.
         transition select(packet.lookahead<ipv4_t>().version) {
             //The packet should be either IPv4 or IPv6.