[ONOS-7128] Initial commit of fabric.p4

Change-Id: I5224c411a1eccdbee84b1fc0b1824c5fa922f689
diff --git a/pipelines/fabric/src/main/resources/include/action.p4 b/pipelines/fabric/src/main/resources/include/action.p4
new file mode 100644
index 0000000..c76334a
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/action.p4
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+#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
new file mode 100644
index 0000000..9931090
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/checksum.p4
@@ -0,0 +1,84 @@
+/*
+ * 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.
+ */
+
+#ifndef __CHECKSUM__
+#define __CHECKSUM__
+
+control FabricComputeChecksum(inout parsed_headers_t hdr,
+                              inout fabric_metadata_t meta)
+{
+    apply {
+        update_checksum(hdr.ipv4.isValid(),
+            {
+                hdr.ipv4.version,
+                hdr.ipv4.ihl,
+                hdr.ipv4.diffserv,
+                hdr.ipv4.total_len,
+                hdr.ipv4.identification,
+                hdr.ipv4.flags,
+                hdr.ipv4.frag_offset,
+                hdr.ipv4.ttl,
+                hdr.ipv4.protocol,
+                hdr.ipv4.src_addr,
+                hdr.ipv4.dst_addr
+            },
+            hdr.ipv4.hdr_checksum,
+            HashAlgorithm.csum16
+        );
+        update_checksum(hdr.icmp.isValid(),
+            {
+                hdr.icmp.icmp_type,
+                hdr.icmp.icmp_code
+            },
+            hdr.icmp.checksum,
+            HashAlgorithm.csum16
+        );
+    }
+}
+
+control FabricVerifyChecksum(inout parsed_headers_t hdr,
+                             inout fabric_metadata_t meta)
+{
+    apply {
+        verify_checksum(hdr.ipv4.isValid(),
+            {
+                hdr.ipv4.version,
+                hdr.ipv4.ihl,
+                hdr.ipv4.diffserv,
+                hdr.ipv4.total_len,
+                hdr.ipv4.identification,
+                hdr.ipv4.flags,
+                hdr.ipv4.frag_offset,
+                hdr.ipv4.ttl,
+                hdr.ipv4.protocol,
+                hdr.ipv4.src_addr,
+                hdr.ipv4.dst_addr
+            },
+            hdr.ipv4.hdr_checksum,
+            HashAlgorithm.csum16
+        );
+        verify_checksum(hdr.icmp.isValid(),
+            {
+                hdr.icmp.icmp_type,
+                hdr.icmp.icmp_code
+            },
+            hdr.icmp.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
new file mode 100644
index 0000000..a66657f
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/filtering.p4
@@ -0,0 +1,90 @@
+/*
+ * 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 "../header.p4"
+#include "../action.p4"
+
+control Filtering (
+    inout parsed_headers_t hdr,
+    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 set_vlan(vlan_id_t new_vlan_id) {
+        hdr.vlan_tag.vlan_id = new_vlan_id;
+    }
+
+    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 = ETHERTYPE_VLAN;
+        set_vlan(new_vlan_id);
+
+        // pop internal vlan before output
+        fabric_metadata.pop_vlan_at_egress = true;
+    }
+
+    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;
+            hdr.vlan_tag.isValid(): exact @name("hdr.vlan_tag.is_valid");
+            hdr.vlan_tag.vlan_id: ternary;
+        }
+
+        actions = {
+            push_internal_vlan;
+            set_vlan;
+            nop;
+            drop;
+        }
+        const default_action = drop();
+        counters = ingress_port_vlan_counter;
+    }
+
+    // Originally TMAC table in OF-DPA pipeline
+    table fwd_classifier {
+        key = {
+            standard_metadata.ingress_port: exact;
+            hdr.ethernet.dst_addr: exact;
+            hdr.ethernet.ether_type: exact;
+        }
+
+        actions = {
+            set_forwarding_type;
+        }
+
+        const default_action = set_forwarding_type(FWD_BRIDGING);
+        counters = fwd_classifier_counter;
+    }
+
+    apply {
+        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
new file mode 100644
index 0000000..fb5f231
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/forwarding.p4
@@ -0,0 +1,197 @@
+/*
+ * 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"
+#include "../action.p4"
+
+
+control Forwarding (
+    inout parsed_headers_t hdr,
+    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 set_next_id(next_id_t next_id) {
+        fabric_metadata.next_id = next_id;
+    }
+
+    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;
+    }
+
+    action push_mpls (mpls_label_t label, bit<3> tc) {
+        //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;
+        hdr.mpls.ttl = 64;
+    }
+
+    action push_mpls_and_next_v4 (mpls_label_t label,
+                                  next_id_t next_id) {
+        set_next_id(next_id);
+        push_mpls(label, hdr.ipv4.diffserv[7:5]);
+    }
+
+    action push_mpls_and_next_v6 (mpls_label_t label, next_id_t next_id) {
+        set_next_id(next_id);
+        push_mpls(label, hdr.ipv6.traffic_class[7:5]);
+    }
+
+    action duplicate_to_controller() {
+        fabric_metadata.next_type = NEXT_TYPE_PUNT;
+        standard_metadata.egress_spec = CPU_PORT;
+    }
+
+
+
+    table bridging {
+        key = {
+            hdr.vlan_tag.vlan_id: exact;
+            hdr.ethernet.dst_addr: ternary;
+        }
+
+        actions = {
+            set_next_id;
+        }
+        counters = bridging_counter;
+    }
+
+    table mpls {
+        key = {
+            hdr.mpls.label: exact;
+        }
+
+        actions = {
+            pop_mpls_and_next;
+        }
+        counters = mpls_counter;
+    }
+
+    table unicast_v4 {
+        key = {
+            hdr.ipv4.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id;
+            push_mpls_and_next_v4;
+        }
+        counters = unicast_v4_counter;
+    }
+
+    table multicast_v4 {
+        key = {
+            hdr.vlan_tag.vlan_id: exact;
+            hdr.ipv4.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id;
+        }
+        counters = multicast_v4_counter;
+    }
+
+    table unicast_v6 {
+        key = {
+            hdr.ipv6.dst_addr: lpm;
+        }
+
+        actions = {
+            set_next_id;
+            push_mpls_and_next_v6;
+        }
+        counters = unicast_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;
+    }
+
+    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;
+        }
+
+        actions = {
+            set_next_id;
+            duplicate_to_controller;
+            drop;
+            nop;
+        }
+
+        const default_action = nop();
+        counters = acl_counter;
+    }
+
+    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_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();
+        else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply();
+        acl.apply();
+    }
+}
diff --git a/pipelines/fabric/src/main/resources/include/control/next.p4 b/pipelines/fabric/src/main/resources/include/control/next.p4
new file mode 100644
index 0000000..5ab18fa
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/next.p4
@@ -0,0 +1,154 @@
+/*
+ * 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 "../header.p4"
+#include "../action.p4"
+
+control Next (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata) {
+    direct_counter(CounterType.packets_and_bytes) next_id_mapping_counter;
+    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 set_next_type(next_type_t next_type) {
+        fabric_metadata.next_type = next_type;
+    }
+
+    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){
+        hdr.vlan_tag.vlan_id = new_vlan_id;
+
+        // don't remove the vlan from egress since we set the vlan to it.
+        fabric_metadata.pop_vlan_at_egress = false;
+        output(port_num);
+    }
+
+    action rewrite_smac(mac_addr_t smac) {
+        hdr.ethernet.src_addr = smac;
+    }
+
+    action rewrite_dmac(mac_addr_t dmac) {
+        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 set_mcast_group(group_id_t gid, mac_addr_t smac) {
+        standard_metadata.mcast_grp = gid;
+        rewrite_smac(smac);
+    }
+
+    table next_id_mapping {
+        key = {
+            fabric_metadata.next_id: exact;
+        }
+
+        actions = {
+            set_next_type;
+        }
+        counters = next_id_mapping_counter;
+    }
+
+    table simple {
+        key = {
+            fabric_metadata.next_id: exact;
+        }
+
+        actions = {
+            output;
+            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;
+            fabric_metadata.l4_src_port: selector;
+            fabric_metadata.l4_dst_port: selector;
+        }
+
+        actions = {
+            l3_routing;
+        }
+
+        implementation = ecmp_selector;
+        counters = hashed_counter;
+    }
+
+    /*
+     * Work in progress
+     */
+    table broadcast {
+        key = {
+            fabric_metadata.next_id: exact;
+        }
+        actions = {
+            set_mcast_group;
+        }
+        counters = broadcast_counter;
+    }
+
+    apply {
+        next_id_mapping.apply();
+        if (fabric_metadata.next_type == NEXT_TYPE_SIMPLE) simple.apply();
+        else if (fabric_metadata.next_type == NEXT_TYPE_HASHED) hashed.apply();
+        else if (fabric_metadata.next_type == NEXT_TYPE_BROADCAST) broadcast.apply();
+        // next_type == PUNT, leave it to packet-io egress
+    }
+}
+
+control EgressNextControl (
+    inout parsed_headers_t hdr,
+    inout fabric_metadata_t fabric_metadata,
+    inout standard_metadata_t standard_metadata){
+
+    apply {
+        // pop internal vlan if the meta is set
+        if (fabric_metadata.pop_vlan_at_egress) {
+            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
new file mode 100644
index 0000000..fb5d731
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/packetio.p4
@@ -0,0 +1,43 @@
+/*
+ * 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 "../header.p4"
+#include "../action.p4"
+
+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;
+            exit;
+        }
+    }
+}
+
+control PacketIoEgress(
+inout parsed_headers_t hdr,
+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/control/port_counter.p4 b/pipelines/fabric/src/main/resources/include/control/port_counter.p4
new file mode 100644
index 0000000..80ec64f
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/control/port_counter.p4
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+#ifndef __PORT_COUNTER__
+#define __PORT_COUNTER__
+#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) {
+    counter(MAX_PORTS, CounterType.packets) egress_port_counter;
+    counter(MAX_PORTS, CounterType.packets) ingress_port_counter;
+
+    apply {
+        if (standard_metadata.egress_spec < MAX_PORTS) {
+            egress_port_counter.count((bit<32>)standard_metadata.egress_spec);
+        }
+        if (standard_metadata.ingress_port < MAX_PORTS) {
+            ingress_port_counter.count((bit<32>)standard_metadata.ingress_port);
+        }
+    }
+}
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/define.p4 b/pipelines/fabric/src/main/resources/include/define.p4
new file mode 100644
index 0000000..9ba84e9
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/define.p4
@@ -0,0 +1,63 @@
+/*
+ * 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.
+ */
+
+#ifndef __DEFINE__
+#define __DEFINE__
+
+#define MAX_PORTS 511
+
+typedef bit<3>  fwd_type_t;
+typedef bit<32>  next_id_t;
+typedef bit<3>  next_type_t;
+typedef bit<20> mpls_label_t;
+typedef bit<9>  port_num_t;
+typedef bit<48> mac_addr_t;
+typedef bit<16> group_id_t;
+typedef bit<12> vlan_id_t;
+
+const bit<16> ETHERTYPE_QINQ = 0x88A8;
+const bit<16> ETHERTYPE_QINQ_NON_STD = 0x9100;
+const bit<16> ETHERTYPE_VLAN = 0x8100;
+const bit<16> ETHERTYPE_MPLS = 0x8847;
+const bit<16> ETHERTYPE_MPLS_MULTICAST =0x8848;
+const bit<16> ETHERTYPE_IPV4 = 0x0800;
+const bit<16> ETHERTYPE_IPV6 = 0x86dd;
+const bit<16> ETHERTYPE_ARP  = 0x0806;
+
+const bit<4> IP_VERSION_4 = 4;
+const bit<4> IP_VERSION_6 = 6;
+
+const bit<8> PROTO_ICMP = 1;
+const bit<8> PROTO_TCP = 6;
+const bit<8> PROTO_UDP = 17;
+const bit<8> PROTO_ICMPV6 = 58;
+
+const port_num_t CPU_PORT = 255;
+const port_num_t DROP_PORT = 511;
+
+const fwd_type_t FWD_BRIDGING = 0;
+const fwd_type_t FWD_MPLS = 1;
+const fwd_type_t FWD_IPV4_UNICAST = 2;
+const fwd_type_t FWD_IPV4_MULTICAST = 3;
+const fwd_type_t FWD_IPV6_UNICAST = 4;
+const fwd_type_t FWD_IPV6_MULTICAST = 5;
+
+const next_type_t NEXT_TYPE_SIMPLE = 0;
+const next_type_t NEXT_TYPE_HASHED = 1;
+const next_type_t NEXT_TYPE_BROADCAST = 2;
+const next_type_t NEXT_TYPE_PUNT = 3;
+
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/header.p4 b/pipelines/fabric/src/main/resources/include/header.p4
new file mode 100644
index 0000000..b243c94
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/header.p4
@@ -0,0 +1,139 @@
+/*
+ * 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.
+ */
+
+#ifndef __HEADER__
+#define __HEADER__
+
+#include "define.p4"
+
+@controller_header("packet_in")
+header packet_in_header_t {
+    port_num_t ingress_port;
+}
+
+@controller_header("packet_out")
+header packet_out_header_t {
+    port_num_t egress_port;
+}
+
+header ethernet_t {
+    mac_addr_t dst_addr;
+    mac_addr_t src_addr;
+    bit<16> ether_type;
+}
+
+header vlan_tag_t {
+    bit<3> pri;
+    bit<1> cfi;
+    vlan_id_t vlan_id;
+    bit<16> ether_type;
+}
+
+header mpls_t {
+    bit<20> label;
+    bit<3> tc;
+    bit<1> bos;
+    bit<8> ttl;
+}
+
+header ipv4_t {
+    bit<4> version;
+    bit<4> ihl;
+    bit<8> diffserv;
+    bit<16> total_len;
+    bit<16> identification;
+    bit<3> flags;
+    bit<13> frag_offset;
+    bit<8> ttl;
+    bit<8> protocol;
+    bit<16> hdr_checksum;
+    bit<32> src_addr;
+    bit<32> dst_addr;
+}
+
+header ipv6_t {
+    bit<4> version;
+    bit<8> traffic_class;
+    bit<20> flow_label;
+    bit<16> payload_len;
+    bit<8> next_hdr;
+    bit<8> hop_limit;
+    bit<128> src_addr;
+    bit<128> dst_addr;
+}
+
+header arp_t {
+    bit<16> hw_type;
+    bit<16> proto_type;
+    bit<8> hw_addr_len;
+    bit<8> proto_addr_len;
+    bit<16> opcode;
+}
+
+header tcp_t {
+    bit<16> src_port;
+    bit<16> dst_port;
+    bit<32> seq_no;
+    bit<32> ack_no;
+    bit<4>  data_offset;
+    bit<3>  res;
+    bit<3>  ecn;
+    bit<6>  ctrl;
+    bit<16> window;
+    bit<16> checksum;
+    bit<16> urgent_ptr;
+}
+
+header udp_t {
+    bit<16> src_port;
+    bit<16> dst_port;
+    bit<16> len;
+    bit<16> checksum;
+}
+
+header icmp_t {
+    bit<8> icmp_type;
+    bit<8> icmp_code;
+    bit<16> checksum;
+}
+
+//Custom metadata definition
+struct fabric_metadata_t {
+    fwd_type_t fwd_type;
+    next_id_t next_id;
+    next_type_t next_type;
+    bool pop_vlan_at_egress;
+    bit<8> ip_proto;
+    bit<16> l4_src_port;
+    bit<16> l4_dst_port;
+}
+
+struct parsed_headers_t {
+    ethernet_t ethernet;
+    vlan_tag_t vlan_tag;
+    vlan_tag_t inner_vlan_tag;
+    mpls_t mpls;
+    ipv4_t ipv4;
+    ipv6_t ipv6;
+    arp_t arp;
+    tcp_t tcp;
+    udp_t udp;
+    icmp_t icmp;
+    packet_out_header_t packet_out;
+    packet_in_header_t packet_in;
+}
+
+#endif
diff --git a/pipelines/fabric/src/main/resources/include/parser.p4 b/pipelines/fabric/src/main/resources/include/parser.p4
new file mode 100644
index 0000000..0e13cfd
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/include/parser.p4
@@ -0,0 +1,149 @@
+/*
+ * 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.
+ */
+
+#ifndef __PARSER__
+#define __PARSER__
+
+#include "define.p4"
+
+parser FabricParser (
+packet_in packet,
+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;
+            default: parse_ethernet;
+        }
+    }
+
+    state parse_packet_out {
+        packet.extract(hdr.packet_out);
+        transition parse_ethernet;
+    }
+
+    state parse_ethernet {
+        packet.extract(hdr.ethernet);
+        transition select(hdr.ethernet.ether_type){
+            ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
+            ETHERTYPE_QINQ: parse_vlan_tag;
+            ETHERTYPE_VLAN: parse_vlan_tag;
+            ETHERTYPE_MPLS: parse_mpls;
+            ETHERTYPE_ARP: parse_arp;
+            ETHERTYPE_IPV4: parse_ipv4;
+            ETHERTYPE_IPV6: parse_ipv6;
+            default: accept;
+        }
+    }
+
+    state parse_vlan_tag {
+        packet.extract(hdr.vlan_tag);
+        transition select(hdr.vlan_tag.ether_type){
+            ETHERTYPE_VLAN: parse_inner_vlan_tag;
+            ETHERTYPE_ARP: parse_arp;
+            ETHERTYPE_IPV4: parse_ipv4;
+            ETHERTYPE_IPV6: parse_ipv6;
+            default: accept;
+        }
+    }
+
+    state parse_inner_vlan_tag {
+        packet.extract(hdr.inner_vlan_tag);
+        transition select(hdr.vlan_tag.ether_type){
+            ETHERTYPE_ARP: parse_arp;
+            ETHERTYPE_IPV4: parse_ipv4;
+            ETHERTYPE_IPV6: parse_ipv6;
+            default: accept;
+        }
+    }
+
+    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.
+            IP_VERSION_4: parse_ipv4;
+            IP_VERSION_6: parse_ipv6;
+            default: parse_ethernet;
+        }
+    }
+
+    state parse_ipv4 {
+        packet.extract(hdr.ipv4);
+        fabric_metadata.ip_proto = hdr.ipv4.protocol;
+        //Need header verification?
+        transition select(hdr.ipv4.protocol) {
+            PROTO_TCP: parse_tcp;
+            PROTO_UDP: parse_udp;
+            PROTO_ICMP: parse_icmp;
+            default: accept;
+        }
+    }
+
+    state parse_ipv6 {
+        packet.extract(hdr.ipv6);
+        fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
+        transition select(hdr.ipv6.next_hdr) {
+            PROTO_TCP: parse_tcp;
+            PROTO_UDP: parse_udp;
+            PROTO_ICMPV6: parse_icmp;
+            default: accept;
+        }
+    }
+
+    state parse_arp {
+        packet.extract(hdr.arp);
+        transition accept;
+    }
+
+    state parse_tcp {
+        packet.extract(hdr.tcp);
+        fabric_metadata.l4_src_port = hdr.tcp.src_port;
+        fabric_metadata.l4_dst_port = hdr.tcp.dst_port;
+        transition accept;
+    }
+
+    state parse_udp {
+        packet.extract(hdr.udp);
+        fabric_metadata.l4_src_port = hdr.udp.src_port;
+        fabric_metadata.l4_dst_port = hdr.udp.dst_port;
+        transition accept;
+    }
+
+    state parse_icmp {
+        packet.extract(hdr.icmp);
+        transition accept;
+    }
+}
+
+control FabricDeparser(packet_out packet, in parsed_headers_t hdr) {
+    apply{
+        packet.emit(hdr.packet_in);
+        packet.emit(hdr.ethernet);
+        packet.emit(hdr.vlan_tag);
+        packet.emit(hdr.inner_vlan_tag);
+        packet.emit(hdr.mpls);
+        packet.emit(hdr.arp);
+        packet.emit(hdr.ipv4);
+        packet.emit(hdr.ipv6);
+        packet.emit(hdr.tcp);
+        packet.emit(hdr.udp);
+    }
+}
+
+#endif