Improve fabric.p4 to reduce pipeline resources and refactor pipeconf impl

This patch affects both the P4 pipeline implementation and the
Java pipeconf.

P4 PIPELINE
- Less tables and smarter use of metadata to reduce inter-tables
dependencies and favor parallel execution of tables.
- Removed unused actions / renamed existing ones to make forwarding
behavior clearer (e.g. ingress_port_vlan table)
- Remove co-existence of simple and hansed table. Hashed should be the
default one, but implementations that do not support action profiles
might compile fabric.p4 to use the simple one.
- Use @name annotations for match fields to make control plane
independent of table implementation.
- Use @hidden to avoid showing actions and table on the p4info that
cannot be controlled at runtime.
- First attempt to support double VLAN cross-connect (xconnect table).
- New design has been tested with "fabric-refactoring" branch of
fabric-p4test:
github.com/opennetworkinglab/fabric-p4test/tree/fabric-refactoring

JAVA PIPECONF
This patch brings a major refactoring that reflects the experience
gathered in the past months of working on fabric.p4 and reasoning on its
pipeconf implementation. Indeed, the FlowObjective API is
under-specified and sometimes ambiguous which makes the process of
creating and maintaining a pipeliner implementation tedious. This
refactoring brings a simplified implementation by removing unused/
unnecessary functionalities and by recognizing commonality when possible
(e.g. by means of abstract and utility classes). It also makes design
patterns more explicit and consistent. Overall, the goal is to reduce
technical debt and to make it easier to support new features as we
evolve fabric.p4

Changes include:
- Changes in pipeliner/interpreter to reflect new pipeline design.
- By default translate objective treatment to PiAction. This favors
debuggability of flow rules in ONOS.
- Support new NextObjective’s NextTreatment class.
- Remove lots of unused/unnecessary code (e.g. async callback handling
for pending objective install status in pipeliner as current
implementation was always returning success)
- Gather commonality in abstract classes and simplify implementation
for objective translator (filtering, forwarding, next)
- New implementation of ForwardingFunctionTypes (FFT) that looks at
criterion instance values along with their types (to avoid relying on
case-specific if-else conditions to recognize variants of an FFT)
- Adaptive translation of NextObjective based on presence of simple or
hashed table.
- Support DENY FilteringObjective

Also:
- Fix onos-p4-gen-constants to avoid generating conflicting
PiMatchFieldId variable names.
- Install Graphviz tools in p4vm to generate p4c graphs
- Generate p4c graphs by default when compiling fabric.p4
- Use more compact Hex string when printing PI values

Change-Id: Ife79e44054dc5bc48833f95d0551a7370150eac5
diff --git a/pipelines/fabric/src/main/resources/include/control/acl.p4 b/pipelines/fabric/src/main/resources/include/control/acl.p4
new file mode 100644
index 0000000..9eece9a
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/acl.p4
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+
+#include <core.p4>
+#include <v1model.p4>
+
+#include "../define.p4"
+#include "../header.p4"
+
+control Acl (inout parsed_headers_t hdr,
+             inout fabric_metadata_t fabric_metadata,
+             inout standard_metadata_t standard_metadata) {
+
+    /*
+     * ACL Table.
+     */
+    direct_counter(CounterType.packets_and_bytes) acl_counter;
+
+    action set_next_id_acl(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        acl_counter.count();
+    }
+
+    // Send immendiatelly to CPU - skip the rest of ingress.
+    action punt_to_cpu() {
+        standard_metadata.egress_spec = CPU_PORT;
+        fabric_metadata.skip_next = _TRUE;
+        acl_counter.count();
+    }
+
+    action clone_to_cpu() {
+        // FIXME: works only if pkt will be replicated via PRE multicast group.
+        fabric_metadata.clone_to_cpu = _TRUE;
+        acl_counter.count();
+    }
+
+    action drop() {
+        mark_to_drop();
+        fabric_metadata.skip_next = _TRUE;
+        acl_counter.count();
+    }
+
+    action nop_acl() {
+        acl_counter.count();
+    }
+
+    table acl {
+        key = {
+            standard_metadata.ingress_port: ternary @name("ig_port"); // 9
+            fabric_metadata.ip_proto: ternary @name("ip_proto"); // 8
+            fabric_metadata.l4_sport: ternary @name("l4_sport"); // 16
+            fabric_metadata.l4_dport: ternary @name("l4_dport"); // 16
+            hdr.ethernet.dst_addr: ternary @name("eth_src"); // 48
+            hdr.ethernet.src_addr: ternary @name("eth_dst"); // 48
+            hdr.vlan_tag.vlan_id: ternary @name("vlan_id"); // 12
+            fabric_metadata.eth_type: ternary @name("eth_type"); //16
+            hdr.ipv4.src_addr: ternary @name("ipv4_src"); // 32
+            hdr.ipv4.dst_addr: ternary @name("ipv4_dst"); // 32
+            hdr.icmp.icmp_type: ternary @name("icmp_type"); // 8
+            hdr.icmp.icmp_code: ternary @name("icmp_code"); // 8
+        }
+
+        actions = {
+            set_next_id_acl;
+            punt_to_cpu;
+            clone_to_cpu;
+            drop;
+            nop_acl;
+        }
+
+        const default_action = nop_acl();
+        size = 128;
+        counters = acl_counter;
+    }
+
+    apply {
+        acl.apply();
+    }
+}
diff --git a/pipelines/fabric/src/main/resources/include/control/filtering.p4 b/pipelines/fabric/src/main/resources/include/control/filtering.p4
index 3c5bfcb..638d9a1 100644
--- a/pipelines/fabric/src/main/resources/include/control/filtering.p4
+++ b/pipelines/fabric/src/main/resources/include/control/filtering.p4
@@ -18,80 +18,63 @@
 #include <v1model.p4>
 
 #include "../header.p4"
-#include "../action.p4"
 
-control Filtering (
-    inout parsed_headers_t hdr,
-    inout fabric_metadata_t fabric_metadata,
-    inout standard_metadata_t standard_metadata) {
+control Filtering (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.
+     *
+     * Filter packets based on ingress port and VLAN tag.
      */
     direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter;
 
-    action drop() {
-        mark_to_drop();
+    action deny() {
+        // Packet from unconfigured port. Skip forwarding and next block.
+        // Do ACL table in case we want to punt to cpu.
+        fabric_metadata.skip_forwarding = _TRUE;
+        fabric_metadata.skip_next = _TRUE;
         ingress_port_vlan_counter.count();
     }
 
-    action set_vlan(vlan_id_t new_vlan_id) {
-        hdr.vlan_tag.vlan_id = new_vlan_id;
+    action permit() {
+        // Allow packet as is.
         ingress_port_vlan_counter.count();
     }
 
-    action push_internal_vlan(vlan_id_t new_vlan_id) {
-        // Add internal VLAN header, will be removed before packet emission.
-        // cfi and pri values are dummy.
-        hdr.vlan_tag.setValid();
-        hdr.vlan_tag.cfi = 0;
-        hdr.vlan_tag.pri = 0;
-        hdr.vlan_tag.ether_type = hdr.ethernet.ether_type;
-        hdr.ethernet.ether_type = ETHERTYPE_VLAN;
-        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 nop_ingress_port_vlan() {
-        nop();
+    action permit_with_internal_vlan(vlan_id_t vlan_id) {
+        fabric_metadata.vlan_id = vlan_id;
         ingress_port_vlan_counter.count();
     }
 
     table ingress_port_vlan {
         key = {
-            standard_metadata.ingress_port: exact;
-            hdr.vlan_tag.isValid(): exact @name("hdr.vlan_tag.is_valid");
-            hdr.vlan_tag.vlan_id: ternary;
+            standard_metadata.ingress_port: exact @name("ig_port");
+            hdr.vlan_tag.isValid(): exact @name("vlan_is_valid");
+            hdr.vlan_tag.vlan_id: ternary @name("vlan_id");
         }
-
         actions = {
-            push_internal_vlan;
-            set_vlan;
-            drop;
-            nop_ingress_port_vlan();
+            deny();
+            permit();
+            permit_with_internal_vlan();
         }
-
-        const default_action = push_internal_vlan(DEFAULT_VLAN_ID);
+        const default_action = deny();
         counters = ingress_port_vlan_counter;
     }
 
     /*
      * Forwarding Classifier.
-     * Setup Forwarding Type metadata for Forwarding control block.
+     *
+     * Set which type of forwarding behavior to execute in the next 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)
+     *   MPLS(0x8847)
      * - IP Multicast: destination mac address is multicast address and ethernet
-     *                 type is IP(0x0800 or 0x86dd)
+     *   type is IP(0x0800 or 0x86dd)
      * - IP Unicast: destination mac address is router mac and ethernet type is
-     *               IP(0x0800 or 0x86dd)
+     *   IP(0x0800 or 0x86dd)
      */
     direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter;
 
@@ -102,26 +85,35 @@
 
     table fwd_classifier {
         key = {
-            standard_metadata.ingress_port: exact;
-            hdr.ethernet.dst_addr: ternary;
-            hdr.vlan_tag.ether_type: exact;
+            standard_metadata.ingress_port: exact @name("ig_port");
+            hdr.ethernet.dst_addr: ternary @name("eth_dst");
+            fabric_metadata.eth_type: exact @name("eth_type");
         }
-
         actions = {
             set_forwarding_type;
         }
-
         const default_action = set_forwarding_type(FWD_BRIDGING);
         counters = fwd_classifier_counter;
     }
 
     apply {
-        if (ingress_port_vlan.apply().hit) {
-            fwd_classifier.apply();
-        } else {
-            // Packet from unconfigured port. Skip forwarding processing,
-            // except for ACL table in case we want to punt to cpu.
-            fabric_metadata.fwd_type = FWD_UNKNOWN;
+        // Initialize lookup metadata. Packets without a VLAN header will be
+        // treated as belonging to a default VLAN ID (see parser).
+        if (hdr.vlan_tag.isValid()) {
+            fabric_metadata.eth_type = hdr.vlan_tag.eth_type;
+            fabric_metadata.vlan_id = hdr.vlan_tag.vlan_id;
+            fabric_metadata.vlan_pri = hdr.vlan_tag.pri;
+            fabric_metadata.vlan_cfi = hdr.vlan_tag.cfi;
         }
+        if (!hdr.mpls.isValid()) {
+            // Packets with a valid MPLS header will have
+            // fabric_metadata.mpls_ttl set to the packet's MPLS ttl value (see
+            // parser). In any case, if we are forwarding via MPLS, ttl will be
+            // decremented in egress.
+            fabric_metadata.mpls_ttl = DEFAULT_MPLS_TTL + 1;
+        }
+
+        ingress_port_vlan.apply();
+        fwd_classifier.apply();
     }
 }
diff --git a/pipelines/fabric/src/main/resources/include/control/forwarding.p4 b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
index 7c69092..e5f89cb 100644
--- a/pipelines/fabric/src/main/resources/include/control/forwarding.p4
+++ b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
@@ -19,68 +19,70 @@
 
 #include "../define.p4"
 #include "../header.p4"
-#include "../action.p4"
 
 
-control Forwarding (
-    inout parsed_headers_t hdr,
-    inout fabric_metadata_t fabric_metadata,
-    inout standard_metadata_t standard_metadata) {
+control Forwarding (inout parsed_headers_t hdr,
+                    inout fabric_metadata_t fabric_metadata,
+                    inout standard_metadata_t standard_metadata) {
+
+    @hidden
+    action set_next_id(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+    }
 
     /*
      * Bridging Table.
-     * Matches destination mac address and VLAN Id and make egress decision.
      */
     direct_counter(CounterType.packets_and_bytes) bridging_counter;
 
     action set_next_id_bridging(next_id_t next_id) {
-        fabric_metadata.next_id = next_id;
+        set_next_id(next_id);
         bridging_counter.count();
     }
 
     table bridging {
         key = {
-            hdr.vlan_tag.vlan_id: exact;
-            hdr.ethernet.dst_addr: ternary;
+            fabric_metadata.vlan_id: exact @name("vlan_id");
+            hdr.ethernet.dst_addr: ternary @name("eth_dst");
         }
-
         actions = {
             set_next_id_bridging;
+            @defaultonly nop;
         }
+        const default_action = nop();
         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;
+        fabric_metadata.mpls_label = 0;
+        set_next_id(next_id);
         mpls_counter.count();
     }
 
     table mpls {
         key = {
-            hdr.mpls.label: exact;
+            fabric_metadata.mpls_label: exact @name("mpls_label");
         }
-
         actions = {
             pop_mpls_and_next;
+            @defaultonly nop;
         }
+        const default_action = nop();
         counters = mpls_counter;
     }
 
     /*
      * IPv4 Routing Table.
-     * Matches IPv4 prefix and make egress decision.
      */
     direct_counter(CounterType.packets_and_bytes) routing_v4_counter;
 
     action set_next_id_routing_v4(next_id_t next_id) {
-        fabric_metadata.next_id = next_id;
+        set_next_id(next_id);
         routing_v4_counter.count();
     }
 
@@ -90,115 +92,47 @@
 
     table routing_v4 {
         key = {
-            hdr.ipv4.dst_addr: lpm;
+            hdr.ipv4.dst_addr: lpm @name("ipv4_dst");
         }
-
         actions = {
             set_next_id_routing_v4;
             nop_routing_v4;
+            @defaultonly nop;
         }
+        const default_action = nop();
         counters = routing_v4_counter;
     }
 
-    /*
-     * ACL Table.
-     * Make final egress decision based on general metch fields.
-     */
-    direct_counter(CounterType.packets_and_bytes) acl_counter;
-
-    action set_next_id_acl(next_id_t next_id) {
-        fabric_metadata.next_id = next_id;
-        acl_counter.count();
-    }
-
-    // Send immendiatelly to CPU - skip the rest of pipeline.
-    action punt_to_cpu() {
-        standard_metadata.egress_spec = CPU_PORT;
-        acl_counter.count();
-        exit;
-    }
-
-    action clone_to_cpu() {
-        // FIXME: works only if pkt will be replicated via PRE multicast group.
-        fabric_metadata.clone_to_cpu = _TRUE;
-        acl_counter.count();
-    }
-
-    action drop() {
-        mark_to_drop();
-        acl_counter.count();
-    }
-
-    action nop_acl() {
-        acl_counter.count();
-    }
-
-    table acl {
-        key = {
-            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
-            hdr.vlan_tag.vlan_id: ternary; // 12
-            hdr.vlan_tag.ether_type: ternary; //16
-            hdr.ipv4.src_addr: ternary; // 32
-            hdr.ipv4.dst_addr: ternary; // 32
-            hdr.icmp.icmp_type: ternary; // 8
-            hdr.icmp.icmp_code: ternary; // 8
-        }
-
-        actions = {
-            set_next_id_acl;
-            punt_to_cpu;
-            clone_to_cpu;
-            drop;
-            nop_acl;
-        }
-
-        const default_action = nop_acl();
-        size = 128;
-        counters = acl_counter;
-    }
-
 #ifdef WITH_IPV6
     /*
      * IPv6 Routing Table.
-     * Matches IPv6 prefix and make egress decision.
      */
     direct_counter(CounterType.packets_and_bytes) routing_v6_counter;
 
     action set_next_id_routing_v6(next_id_t next_id) {
-        fabric_metadata.next_id = next_id;
+        set_next_id(next_id);
         routing_v6_counter.count();
     }
 
     table routing_v6 {
         key = {
-            hdr.ipv6.dst_addr: lpm;
+            hdr.ipv6.dst_addr: lpm @name("ipv6_dst");
         }
-
         actions = {
             set_next_id_routing_v6;
+            @defaultonly nop;
         }
+        const default_action = nop();
         counters = routing_v6_counter;
     }
 #endif // WITH_IPV6
 
     apply {
-        if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
-        else if (fabric_metadata.fwd_type == FWD_MPLS) {
-            mpls.apply();
-
-            // TODO: IPv6
-            hdr.vlan_tag.ether_type = ETHERTYPE_IPV4;
-        }
+        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_IPV4_UNICAST) routing_v4.apply();
 #ifdef WITH_IPV6
         else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) routing_v6.apply();
 #endif // WITH_IPV6
-        acl.apply();
     }
 }
diff --git a/pipelines/fabric/src/main/resources/include/control/next.p4 b/pipelines/fabric/src/main/resources/include/control/next.p4
index 69e622b..98b3812 100644
--- a/pipelines/fabric/src/main/resources/include/control/next.p4
+++ b/pipelines/fabric/src/main/resources/include/control/next.p4
@@ -18,63 +18,104 @@
 #include <v1model.p4>
 
 #include "../header.p4"
-#include "../action.p4"
 
-control Next (
-    inout parsed_headers_t hdr,
-    inout fabric_metadata_t fabric_metadata,
-    inout standard_metadata_t standard_metadata) {
+control Next (inout parsed_headers_t hdr,
+              inout fabric_metadata_t fabric_metadata,
+              inout standard_metadata_t standard_metadata) {
 
     /*
      * General actions.
      */
-    action pop_vlan() {
-        hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
-        hdr.vlan_tag.setInvalid();
+    @hidden
+    action output(port_num_t port_num) {
+     standard_metadata.egress_spec = port_num;
     }
 
+    @hidden
     action rewrite_smac(mac_addr_t smac) {
         hdr.ethernet.src_addr = smac;
     }
 
+    @hidden
     action rewrite_dmac(mac_addr_t dmac) {
         hdr.ethernet.dst_addr = dmac;
     }
 
-    action push_mpls (mpls_label_t label, bit<3> tc) {
-        // Suppose that the maximum number of label is one.
-        hdr.mpls.setValid();
-        hdr.vlan_tag.ether_type = ETHERTYPE_MPLS;
-        hdr.mpls.label = label;
-        hdr.mpls.tc = tc;
-        hdr.mpls.bos = 1w1; // BOS = TRUE
-        hdr.mpls.ttl = DEFAULT_MPLS_TTL;
+    @hidden
+    action set_mpls_label(mpls_label_t label) {
+        fabric_metadata.mpls_label = label;
+    }
+
+    @hidden
+    action routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
+        rewrite_smac(smac);
+        rewrite_dmac(dmac);
+        output(port_num);
+    }
+
+    @hidden
+    action mpls_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                        mpls_label_t label) {
+        set_mpls_label(label);
+        routing(port_num, smac, dmac);
     }
 
     /*
-     * VLAN Metadata Table.
-     * Modify VLAN Id according to metadata from NextObjective(next id).
+     * Next VLAN table.
+     * Modify VLAN ID based on next ID.
      */
-    direct_counter(CounterType.packets_and_bytes) vlan_meta_counter;
+    direct_counter(CounterType.packets_and_bytes) next_vlan_counter;
 
-    action set_vlan(vlan_id_t new_vlan_id) {
-        hdr.vlan_tag.vlan_id = new_vlan_id;
-        vlan_meta_counter.count();
+    action set_vlan(vlan_id_t vlan_id) {
+        fabric_metadata.vlan_id = vlan_id;
+        next_vlan_counter.count();
     }
 
-    table vlan_meta {
+    table next_vlan {
         key = {
-            fabric_metadata.next_id: exact;
+            fabric_metadata.next_id: exact @name("next_id");
         }
-
         actions = {
             set_vlan;
             @defaultonly nop;
         }
-        default_action = nop;
-        counters = vlan_meta_counter;
+        const default_action = nop();
+        counters = next_vlan_counter;
     }
 
+#ifdef WITH_XCONNECT
+    /*
+     * Cross-connect table.
+     * Bidirectional forwarding for the same next id.
+     */
+    direct_counter(CounterType.packets_and_bytes) xconnect_counter;
+
+    action output_xconnect(port_num_t port_num) {
+        output(port_num);
+        xconnect_counter.count();
+    }
+
+    action set_next_id_xconnect(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+        xconnect_counter.count();
+    }
+
+    table xconnect {
+        key = {
+            standard_metadata.ingress_port: exact @name("ig_port");
+            fabric_metadata.next_id: exact @name("next_id");
+        }
+        actions = {
+            output_xconnect;
+            set_next_id_xconnect;
+            @defaultonly nop;
+        }
+        counters = xconnect_counter;
+        const default_action = nop();
+    }
+#endif // WITH_XCONNECT
+
+#ifdef WITH_SIMPLE_NEXT
     /*
      * Simple Table.
      * Do a single egress action based on next id.
@@ -82,197 +123,212 @@
     direct_counter(CounterType.packets_and_bytes) simple_counter;
 
     action output_simple(port_num_t port_num) {
-        standard_metadata.egress_spec = port_num;
+        output(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 routing_simple(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
+        routing(port_num, smac, dmac);
+        simple_counter.count();
     }
 
-    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);
+    action mpls_routing_simple(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                               mpls_label_t label) {
+        mpls_routing(port_num, smac, dmac, label);
+        simple_counter.count();
     }
 
     table simple {
         key = {
-            fabric_metadata.next_id: exact;
+            fabric_metadata.next_id: exact @name("next_id");
         }
-
         actions = {
             output_simple;
-            set_vlan_output;
-            l3_routing_simple;
-            mpls_routing_v4_simple;
-            mpls_routing_v6_simple;
-            l3_routing_vlan;
+            routing_simple;
+            mpls_routing_simple;
+            @defaultonly nop;
         }
+        const default_action = nop();
         counters = simple_counter;
     }
+#endif // WITH_SIMPLE_NEXT
 
+#ifdef WITH_HASHED_NEXT
     /*
      * 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.
+     * Execute an action profile selector based on next id.
      */
-    action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector;
+    action_selector(HashAlgorithm.crc16, 32w64, 32w16) hashed_selector;
     direct_counter(CounterType.packets_and_bytes) hashed_counter;
 
     action output_hashed(port_num_t port_num) {
-        standard_metadata.egress_spec = port_num;
+        output(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 routing_hashed(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
+        routing(port_num, smac, dmac);
+        hashed_counter.count();
     }
 
-    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);
+    action mpls_routing_hashed(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
+                               mpls_label_t label) {
+        mpls_routing(port_num, smac, dmac, label);
+        hashed_counter.count();
     }
 
     table hashed {
         key = {
-            fabric_metadata.next_id: exact;
+            fabric_metadata.next_id: exact @name("next_id");
             hdr.ipv4.dst_addr: selector;
             hdr.ipv4.src_addr: selector;
             fabric_metadata.ip_proto: selector;
-            fabric_metadata.l4_src_port: selector;
-            fabric_metadata.l4_dst_port: selector;
+            fabric_metadata.l4_sport: selector;
+            fabric_metadata.l4_dport: selector;
         }
-
         actions = {
-            l3_routing_hashed;
-            mpls_routing_v4_hashed;
-            mpls_routing_v6_hashed;
+            output_hashed;
+            routing_hashed;
+            mpls_routing_hashed;
+            @defaultonly nop;
         }
-
-        implementation = ecmp_selector;
+        implementation = hashed_selector;
         counters = hashed_counter;
+        const default_action = nop();
     }
+#endif // WITH_HASHED_NEXT
 
     /*
-     * Multicast Table.
-     * Setup multicast group id for packet replication engine (PRE).
+     * Multicast
+     * Maps next IDs to PRE multicat group IDs.
      */
     direct_counter(CounterType.packets_and_bytes) multicast_counter;
 
-    action set_mcast_group(group_id_t gid) {
-        standard_metadata.mcast_grp = gid;
+    action set_mcast_group_id(mcast_group_id_t group_id) {
+        standard_metadata.mcast_grp = group_id;
         fabric_metadata.is_multicast = _TRUE;
         multicast_counter.count();
     }
 
     table multicast {
         key = {
-            fabric_metadata.next_id: exact;
+            fabric_metadata.next_id: exact @name("next_id");
         }
         actions = {
-            set_mcast_group;
+            set_mcast_group_id;
+            @defaultonly nop;
         }
         counters = multicast_counter;
+        const default_action = nop();
     }
 
     apply {
-        vlan_meta.apply();
-        if (!simple.apply().hit) {
-            if (!hashed.apply().hit) {
-                if (!multicast.apply().hit) {
-                    // Next ID doesn't match any table.
-                    return;
-                }
-            }
-        }
-        // Decrement TTL
-        if (!hdr.mpls.isValid()) {
-            if(hdr.ipv4.isValid()) {
-                hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
-            }
-#ifdef WITH_IPV6
-            else if (hdr.ipv6.isValid()) {
-                hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
-            }
-#endif // WITH_IPV6
-        }
+#ifdef WITH_XCONNECT
+        // xconnect might set a new next_id.
+        xconnect.apply();
+#endif // WITH_XCONNECT
+#ifdef WITH_SIMPLE_NEXT
+        simple.apply();
+#endif // WITH_SIMPLE_NEXT
+#ifdef WITH_HASHED_NEXT
+        hashed.apply();
+#endif // WITH_HASHED_NEXT
+        multicast.apply();
+        next_vlan.apply();
     }
 }
 
-control EgressNextControl (
-    inout parsed_headers_t hdr,
-    inout fabric_metadata_t fabric_metadata,
-    inout standard_metadata_t standard_metadata) {
+control EgressNextControl (inout parsed_headers_t hdr,
+                           inout fabric_metadata_t fabric_metadata,
+                           inout standard_metadata_t standard_metadata) {
+    @hidden
+    action pop_mpls_if_present() {
+        hdr.mpls.setInvalid();
+        // Assuming there's an IP header after the MPLS one.
+        fabric_metadata.eth_type = fabric_metadata.ip_eth_type;
+    }
+
+    @hidden
+    action set_mpls() {
+        hdr.mpls.setValid();
+        hdr.mpls.label = fabric_metadata.mpls_label;
+        hdr.mpls.tc = 3w0;
+        hdr.mpls.bos = 1w1; // BOS = TRUE
+        hdr.mpls.ttl = fabric_metadata.mpls_ttl; // Decrement after push.
+        fabric_metadata.eth_type = ETHERTYPE_MPLS;
+    }
+
+    @hidden
+    action push_vlan() {
+        // If VLAN is already valid, we overwrite it with a potentially new VLAN
+        // ID, and same CFI, PRI, and eth_type values found in ingress.
+        hdr.vlan_tag.setValid();
+        hdr.vlan_tag.cfi = fabric_metadata.vlan_cfi;
+        hdr.vlan_tag.pri = fabric_metadata.vlan_pri;
+        hdr.vlan_tag.eth_type = fabric_metadata.eth_type;
+        hdr.vlan_tag.vlan_id = fabric_metadata.vlan_id;
+        hdr.ethernet.eth_type = ETHERTYPE_VLAN;
+    }
 
     /*
      * Egress VLAN Table.
-     * Pops VLAN tag according to interface(Port and VLAN) configuration.
+     * Pops the VLAN tag if the pair egress port and VLAN ID is matched.
      */
     direct_counter(CounterType.packets_and_bytes) egress_vlan_counter;
 
     action pop_vlan() {
-        hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
+        hdr.ethernet.eth_type = fabric_metadata.eth_type;
         hdr.vlan_tag.setInvalid();
         egress_vlan_counter.count();
     }
 
     table egress_vlan {
         key = {
-            hdr.vlan_tag.vlan_id: exact;
-            standard_metadata.egress_port: exact;
+            fabric_metadata.vlan_id: exact @name("vlan_id");
+            standard_metadata.egress_port: exact @name("eg_port");
         }
         actions = {
             pop_vlan;
             @defaultonly nop;
         }
-        default_action = nop;
+        const default_action = nop();
         counters = egress_vlan_counter;
     }
 
     apply {
         if (fabric_metadata.is_multicast == _TRUE
              && standard_metadata.ingress_port == standard_metadata.egress_port) {
-            drop_now();
+            mark_to_drop();
         }
-        egress_vlan.apply();
+
+        if (fabric_metadata.mpls_label == 0) {
+            if (hdr.mpls.isValid()) pop_mpls_if_present();
+        } else {
+            set_mpls();
+        }
+
+        if (!egress_vlan.apply().hit) {
+            // Push VLAN tag if not the default one.
+            if (fabric_metadata.vlan_id != DEFAULT_VLAN_ID) {
+                push_vlan();
+            }
+        }
+
+        // TTL decrement and check.
+        if (hdr.mpls.isValid()) {
+            hdr.mpls.ttl = hdr.mpls.ttl - 1;
+            if (hdr.mpls.ttl == 0) mark_to_drop();
+        } else {
+            if(hdr.ipv4.isValid()) {
+                hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
+                if (hdr.ipv4.ttl == 0) mark_to_drop();
+            }
+#ifdef WITH_IPV6
+            else if (hdr.ipv6.isValid()) {
+                hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
+                if (hdr.ipv6.hop_limit == 0) mark_to_drop();
+            }
+#endif // WITH_IPV6
+        }
     }
 }
diff --git a/pipelines/fabric/src/main/resources/include/control/packetio.p4 b/pipelines/fabric/src/main/resources/include/control/packetio.p4
index a77d82a..a29c044 100644
--- a/pipelines/fabric/src/main/resources/include/control/packetio.p4
+++ b/pipelines/fabric/src/main/resources/include/control/packetio.p4
@@ -15,12 +15,11 @@
  */
 
 #include "../header.p4"
-#include "../action.p4"
 
-control PacketIoIngress(
-inout parsed_headers_t hdr,
-inout fabric_metadata_t fabric_metadata,
-inout standard_metadata_t standard_metadata) {
+control PacketIoIngress(inout parsed_headers_t hdr,
+                        inout fabric_metadata_t fabric_metadata,
+                        inout standard_metadata_t standard_metadata) {
+
     apply {
         if (hdr.packet_out.isValid()) {
             standard_metadata.egress_spec = hdr.packet_out.egress_port;
@@ -32,27 +31,20 @@
     }
 }
 
-control PacketIoEgress(
-        inout parsed_headers_t hdr,
-        inout fabric_metadata_t fabric_metadata,
-        inout standard_metadata_t standard_metadata) {
-    action pop_vlan() {
-        hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
-        hdr.vlan_tag.setInvalid();
-    }
+control PacketIoEgress(inout parsed_headers_t hdr,
+                       inout fabric_metadata_t fabric_metadata,
+                       inout standard_metadata_t standard_metadata) {
+
     apply {
         if (fabric_metadata.is_controller_packet_out == _TRUE) {
             // Transmit right away.
             exit;
         }
         if (standard_metadata.egress_port == CPU_PORT) {
-            if (hdr.vlan_tag.isValid() && fabric_metadata.pop_vlan_when_packet_in == _TRUE) {
-                pop_vlan();
-            }
             if (fabric_metadata.is_multicast == _TRUE &&
                 fabric_metadata.clone_to_cpu == _FALSE) {
                 // Is multicast but clone was not requested.
-                drop_now();
+                mark_to_drop();
             }
             hdr.packet_in.setValid();
             hdr.packet_in.ingress_port = standard_metadata.ingress_port;
diff --git a/pipelines/fabric/src/main/resources/include/control/port_counter.p4 b/pipelines/fabric/src/main/resources/include/control/port_counter.p4
index 3e9ea00..a34a738 100644
--- a/pipelines/fabric/src/main/resources/include/control/port_counter.p4
+++ b/pipelines/fabric/src/main/resources/include/control/port_counter.p4
@@ -19,7 +19,10 @@
 #include "../define.p4"
 #include "../header.p4"
 
-control PortCountersControl(inout parsed_headers_t hdr, inout fabric_metadata_t fabric_metadata, inout standard_metadata_t standard_metadata) {
+control PortCountersControl(inout parsed_headers_t hdr,
+                            inout fabric_metadata_t fabric_metadata,
+                            inout standard_metadata_t standard_metadata) {
+
     counter(MAX_PORTS, CounterType.packets_and_bytes) egress_port_counter;
     counter(MAX_PORTS, CounterType.packets_and_bytes) ingress_port_counter;