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/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
+        }
     }
 }