Refactor fabric.p4

minor refactor for coding style

Change-Id: I7b73b9c3f439c66ce435404cb42fdb38323845b3
diff --git a/pipelines/fabric/src/main/resources/include/control/filtering.p4 b/pipelines/fabric/src/main/resources/include/control/filtering.p4
index a037fcf..050ee8d 100644
--- a/pipelines/fabric/src/main/resources/include/control/filtering.p4
+++ b/pipelines/fabric/src/main/resources/include/control/filtering.p4
@@ -24,15 +24,23 @@
     inout parsed_headers_t hdr,
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
+
+    /*
+     * Ingress Port VLAN Table.
+     * Process packets for different interfaces (Port number + VLAN).
+     * For example, an untagged packet will be tagged when it entered to an
+     * interface with untagged VLAN configuration.
+     */
     direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter;
-    direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter;
 
     action drop() {
         mark_to_drop();
+        ingress_port_vlan_counter.count();
     }
 
     action set_vlan(vlan_id_t new_vlan_id) {
         hdr.vlan_tag.vlan_id = new_vlan_id;
+        ingress_port_vlan_counter.count();
     }
 
     action push_internal_vlan(vlan_id_t new_vlan_id) {
@@ -43,17 +51,13 @@
         hdr.vlan_tag.pri = 0;
         hdr.vlan_tag.ether_type = hdr.ethernet.ether_type;
         hdr.ethernet.ether_type = ETHERTYPE_VLAN;
-        set_vlan(new_vlan_id);
+        hdr.vlan_tag.vlan_id = new_vlan_id;
 
         // pop internal vlan before packet in
         fabric_metadata.pop_vlan_when_packet_in = _TRUE;
+        ingress_port_vlan_counter.count();
     }
 
-    action set_forwarding_type(fwd_type_t fwd_type) {
-        fabric_metadata.fwd_type = fwd_type;
-    }
-
-    // Originally Ingress port and Vlan table in OF-DPA pipeline
     table ingress_port_vlan {
         key = {
             standard_metadata.ingress_port: exact;
@@ -64,7 +68,7 @@
         actions = {
             push_internal_vlan;
             set_vlan;
-            nop;
+            @defaultonly nop;
             drop;
         }
 
@@ -72,7 +76,25 @@
         counters = ingress_port_vlan_counter;
     }
 
-    // Originally TMAC table in OF-DPA pipeline
+    /*
+     * Forwarding Classifier.
+     * Setup Forwarding Type metadata for Forwarding control block.
+     * There are six types of tables in Forwarding control block:
+     * - Bridging: default forwarding type
+     * - MPLS: destination mac address is the router mac and ethernet type is
+     *         MPLS(0x8847)
+     * - IP Multicast: destination mac address is multicast address and ethernet
+     *                 type is IP(0x0800 or 0x86dd)
+     * - IP Unicast: destination mac address is router mac and ethernet type is
+     *               IP(0x0800 or 0x86dd)
+     */
+    direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter;
+
+    action set_forwarding_type(fwd_type_t fwd_type) {
+        fabric_metadata.fwd_type = fwd_type;
+        fwd_classifier_counter.count();
+    }
+
     table fwd_classifier {
         key = {
             standard_metadata.ingress_port: exact;
diff --git a/pipelines/fabric/src/main/resources/include/control/forwarding.p4 b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
index f0bfd5c..bfc180c 100644
--- a/pipelines/fabric/src/main/resources/include/control/forwarding.p4
+++ b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
@@ -27,26 +27,15 @@
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
 
+    /*
+     * Bridging Table.
+     * Matches destination mac address and VLAN Id and make egress decision.
+     */
     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) acl_counter;
 
-    action drop() {
-        mark_to_drop();
-    }
-
-    action set_next_id(next_id_t next_id) {
+    action set_next_id_bridging(next_id_t next_id) {
         fabric_metadata.next_id = next_id;
-    }
-
-    action pop_mpls_and_next(next_id_t next_id) {
-        hdr.mpls.setInvalid();
-        fabric_metadata.next_id = next_id;
-    }
-
-    action duplicate_to_controller() {
-        standard_metadata.egress_spec = CPU_PORT;
+        bridging_counter.count();
     }
 
     table bridging {
@@ -56,11 +45,23 @@
         }
 
         actions = {
-            set_next_id;
+            set_next_id_bridging;
         }
         counters = bridging_counter;
     }
 
+    /*
+     * MPLS Table.
+     * Matches MPLS label and make egress decision.
+     */
+    direct_counter(CounterType.packets_and_bytes) mpls_counter;
+
+    action pop_mpls_and_next(next_id_t next_id) {
+        hdr.mpls.setInvalid();
+        fabric_metadata.next_id = next_id;
+        mpls_counter.count();
+    }
+
     table mpls {
         key = {
             hdr.mpls.label: exact;
@@ -72,63 +73,48 @@
         counters = mpls_counter;
     }
 
+    /*
+     * IPv4 Unicast Table.
+     * Matches IPv4 prefix and make egress decision.
+     */
+    direct_counter(CounterType.packets_and_bytes) unicast_v4_counter;
+
+    action set_next_id_unicast_v4(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        unicast_v4_counter.count();
+    }
+
     table unicast_v4 {
         key = {
             hdr.ipv4.dst_addr: lpm;
         }
 
         actions = {
-            set_next_id;
+            set_next_id_unicast_v4;
         }
         counters = unicast_v4_counter;
     }
 
-#ifdef WITH_MULTICAST
-    direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
+    /*
+     * ACL Table.
+     * Make final egress decision based on general metch fields.
+     */
+    direct_counter(CounterType.packets_and_bytes) acl_counter;
 
-    table multicast_v4 {
-        key = {
-            hdr.vlan_tag.vlan_id: exact;
-            hdr.ipv4.dst_addr: lpm;
-        }
-
-        actions = {
-            set_next_id;
-        }
-        counters = multicast_v4_counter;
-    }
-#endif // WITH_MULTICAST
-
-#ifdef WITH_IPV6
-    direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
-
-    table unicast_v6 {
-        key = {
-            hdr.ipv6.dst_addr: lpm;
-        }
-
-        actions = {
-            set_next_id;
-        }
-        counters = unicast_v6_counter;
+    action set_next_id_acl(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        acl_counter.count();
     }
 
-#ifdef WITH_MULTICAST
-    direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
-
-    table multicast_v6 {
-        key = {
-            hdr.vlan_tag.vlan_id: exact;
-            hdr.ipv6.dst_addr: lpm;
-        }
-
-        actions = {
-            set_next_id;
-        }
-        counters = multicast_v6_counter;
+    action send_to_controller() {
+        standard_metadata.egress_spec = CPU_PORT;
+        acl_counter.count();
     }
-#endif // WITH_MULTICAST
-#endif // WITH_IPV6
+
+    action drop() {
+        mark_to_drop();
+        acl_counter.count();
+    }
 
     table acl {
         key = {
@@ -148,10 +134,10 @@
         }
 
         actions = {
-            set_next_id;
-            duplicate_to_controller;
+            set_next_id_acl;
+            send_to_controller;
             drop;
-            nop;
+            @defaultonly nop;
         }
 
         const default_action = nop();
@@ -159,6 +145,79 @@
         counters = acl_counter;
     }
 
+#ifdef WITH_MULTICAST
+    /*
+     * IPv4 Multicast Table.
+     * Maches multcast IPv4 address and make egress decision.
+     */
+    direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
+    action set_next_id_multicast_v4(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        multicast_v4_counter.count();
+    }
+
+    table multicast_v4 {
+        key = {
+            hdr.vlan_tag.vlan_id: exact;
+            hdr.ipv4.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id_multicast_v4;
+        }
+        counters = multicast_v4_counter;
+    }
+#endif // WITH_MULTICAST
+
+#ifdef WITH_IPV6
+    /*
+     * IPv6 Unicast Table.
+     * Matches IPv6 prefix and make egress decision.
+     */
+    direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
+
+    action set_next_id_unicast_v6(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        unicast_v6_counter.count();
+    }
+
+    table unicast_v6 {
+        key = {
+            hdr.ipv6.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id_unicast_v6;
+        }
+        counters = unicast_v6_counter;
+    }
+
+#ifdef WITH_MULTICAST
+    /*
+     * IPv6 Multicast Table.
+     * Maches multcast IPv6 address and make egress decision.
+     */
+    direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
+
+    action set_next_id_multicast_v6(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        multicast_v6_counter.count();
+    }
+
+    table multicast_v6 {
+        key = {
+            hdr.vlan_tag.vlan_id: exact;
+            hdr.ipv6.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id_multicast_v6;
+        }
+        counters = multicast_v6_counter;
+    }
+#endif // WITH_MULTICAST
+#endif // WITH_IPV6
+
     apply {
         if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
         else if (fabric_metadata.fwd_type == FWD_MPLS) {
diff --git a/pipelines/fabric/src/main/resources/include/control/next.p4 b/pipelines/fabric/src/main/resources/include/control/next.p4
index 0b22ca1..b88bedc 100644
--- a/pipelines/fabric/src/main/resources/include/control/next.p4
+++ b/pipelines/fabric/src/main/resources/include/control/next.p4
@@ -24,29 +24,15 @@
     inout parsed_headers_t hdr,
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
-    action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector;
-    direct_counter(CounterType.packets_and_bytes) vlan_meta_counter;
-    direct_counter(CounterType.packets_and_bytes) simple_counter;
-    direct_counter(CounterType.packets_and_bytes) hashed_counter;
 
-    action output(port_num_t port_num) {
-        standard_metadata.egress_spec = port_num;
-    }
-
-    action set_vlan(vlan_id_t new_vlan_id) {
-        hdr.vlan_tag.vlan_id = new_vlan_id;
-    }
-
+    /*
+     * General actions.
+     */
     action pop_vlan() {
         hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
         hdr.vlan_tag.setInvalid();
     }
 
-    action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){
-        hdr.vlan_tag.vlan_id = new_vlan_id;
-        output(port_num);
-    }
-
     action rewrite_smac(mac_addr_t smac) {
         hdr.ethernet.src_addr = smac;
     }
@@ -55,18 +41,6 @@
         hdr.ethernet.dst_addr = dmac;
     }
 
-    action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
-        rewrite_smac(smac);
-        rewrite_dmac(dmac);
-        output(port_num);
-    }
-
-    action l3_routing_vlan(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, vlan_id_t new_vlan_id) {
-        rewrite_smac(smac);
-        rewrite_dmac(dmac);
-        set_vlan_output(new_vlan_id, port_num);
-    }
-
     action push_mpls (mpls_label_t label, bit<3> tc) {
         // Suppose that the maximum number of label is one.
         hdr.mpls.setValid();
@@ -77,20 +51,15 @@
         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);
+    /*
+     * VLAN Metadata Table.
+     * Modify VLAN Id according to metadata from NextObjective(next id).
+     */
+    direct_counter(CounterType.packets_and_bytes) vlan_meta_counter;
 
-        // 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);
-
-        // TODO: set tc according to traffic_class from ipv4
-        push_mpls(label, 3w0);
+    action set_vlan(vlan_id_t new_vlan_id) {
+        hdr.vlan_tag.vlan_id = new_vlan_id;
+        vlan_meta_counter.count();
     }
 
     table vlan_meta {
@@ -100,27 +69,109 @@
 
         actions = {
             set_vlan;
-            nop;
+            @defaultonly nop;
         }
         default_action = nop;
         counters = vlan_meta_counter;
     }
 
+    /*
+     * Simple Table.
+     * Do a single egress action based on next id.
+     */
+    direct_counter(CounterType.packets_and_bytes) simple_counter;
+
+    action output_simple(port_num_t port_num) {
+        standard_metadata.egress_spec = port_num;
+        simple_counter.count();
+    }
+
+    action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){
+        hdr.vlan_tag.vlan_id = new_vlan_id;
+        output_simple(port_num);
+    }
+
+    action l3_routing_simple(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
+        rewrite_smac(smac);
+        rewrite_dmac(dmac);
+        output_simple(port_num);
+    }
+
+    action mpls_routing_v4_simple(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                            mpls_label_t label) {
+        l3_routing_simple(port_num, smac, dmac);
+
+        // TODO: set tc according to diffserv from ipv4
+        push_mpls(label, 3w0);
+    }
+
+    action mpls_routing_v6_simple (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                            mpls_label_t label) {
+        l3_routing_simple(port_num, smac, dmac);
+
+        // TODO: set tc according to traffic_class from ipv4
+        push_mpls(label, 3w0);
+    }
+
+    action l3_routing_vlan(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, vlan_id_t new_vlan_id) {
+        rewrite_smac(smac);
+        rewrite_dmac(dmac);
+        set_vlan_output(new_vlan_id, port_num);
+    }
+
     table simple {
         key = {
             fabric_metadata.next_id: exact;
         }
 
         actions = {
-            output;
+            output_simple;
             set_vlan_output;
-            l3_routing;
-            mpls_routing_v4;
+            l3_routing_simple;
+            mpls_routing_v4_simple;
+            mpls_routing_v6_simple;
             l3_routing_vlan;
         }
         counters = simple_counter;
     }
 
+    /*
+     * Hashed table.
+     * Execute an action profile group based on next id.
+     * One action profile group may contains multple egress decision.
+     * The execution picks one action profile group memebr by using 5-tuple
+     * hashing.
+     */
+    action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector;
+    direct_counter(CounterType.packets_and_bytes) hashed_counter;
+
+    action output_hashed(port_num_t port_num) {
+        standard_metadata.egress_spec = port_num;
+        hashed_counter.count();
+    }
+
+    action l3_routing_hashed(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
+        rewrite_smac(smac);
+        rewrite_dmac(dmac);
+        output_hashed(port_num);
+    }
+
+    action mpls_routing_v4_hashed (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                            mpls_label_t label) {
+        l3_routing_hashed(port_num, smac, dmac);
+
+        // TODO: set tc according to diffserv from ipv4
+        push_mpls(label, 3w0);
+    }
+
+    action mpls_routing_v6_hashed (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                            mpls_label_t label) {
+        l3_routing_hashed(port_num, smac, dmac);
+
+        // TODO: set tc according to traffic_class from ipv4
+        push_mpls(label, 3w0);
+    }
+
     table hashed {
         key = {
             fabric_metadata.next_id: exact;
@@ -132,9 +183,9 @@
         }
 
         actions = {
-            l3_routing;
-            mpls_routing_v4;
-            mpls_routing_v6;
+            l3_routing_hashed;
+            mpls_routing_v4_hashed;
+            mpls_routing_v6_hashed;
         }
 
         implementation = ecmp_selector;
@@ -142,14 +193,16 @@
     }
 
     /*
-     * Work in progress
+     * Multicast Table.
+     * Setup multicast group id for packet replication engine (PRE).
      */
+    direct_counter(CounterType.packets_and_bytes) multicast_counter;
+
     action set_mcast_group(group_id_t gid) {
         standard_metadata.mcast_grp = gid;
+        multicast_counter.count();
     }
 
-    direct_counter(CounterType.packets_and_bytes) multicast_counter;
-
     table multicast {
         key = {
             fabric_metadata.next_id: exact;
@@ -184,9 +237,16 @@
     inout fabric_metadata_t fabric_metadata,
     inout standard_metadata_t standard_metadata) {
 
+    /*
+     * Egress VLAN Table.
+     * Pops VLAN tag according to interface(Port and VLAN) configuration.
+     */
+    direct_counter(CounterType.packets_and_bytes) egress_vlan_counter;
+
     action pop_vlan() {
         hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
         hdr.vlan_tag.setInvalid();
+        egress_vlan_counter.count();
     }
 
     table egress_vlan {
@@ -196,9 +256,10 @@
         }
         actions = {
             pop_vlan;
-            nop;
+            @defaultonly nop;
         }
         default_action = nop;
+        counters = egress_vlan_counter;
     }
 
     apply {