[ONOS-7128] Initial commit of fabric.p4
Change-Id: I5224c411a1eccdbee84b1fc0b1824c5fa922f689
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