Minor refactoring for fabric.p4

Change-Id: I736b20fc95b4a03afa1074eb777a23507530b44f
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;