Implement INT Telemetry Reporting functionality

[ONOS-7335] Add telemetry reporting functionality to basic int.p4

Change-Id: I3ddd776857598d0b9c1bb70aab22a302c0d6bcc0
diff --git a/pipelines/basic/src/main/resources/include/checksums.p4 b/pipelines/basic/src/main/resources/include/checksums.p4
index 188cc9d..1971a29 100644
--- a/pipelines/basic/src/main/resources/include/checksums.p4
+++ b/pipelines/basic/src/main/resources/include/checksums.p4
@@ -29,7 +29,47 @@
 control compute_checksum_control(inout headers_t hdr,
                                  inout local_metadata_t local_metadata) {
     apply {
-        // No need to recompute.
+        update_checksum(hdr.ipv4.isValid(),
+            {
+                hdr.ipv4.version,
+                hdr.ipv4.ihl,
+                hdr.ipv4.dscp,
+                hdr.ipv4.ecn,
+                hdr.ipv4.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
+        );
+
+        // Need to recompute for the cloned packet.
+        //TODO: https://github.com/p4lang/p4app/issues/43#issuecomment-378061934
+        #ifdef __INT_HEADERS__
+        update_checksum(hdr.report_ipv4.isValid(),
+            {
+                hdr.report_ipv4.version,
+                hdr.report_ipv4.ihl,
+                hdr.report_ipv4.dscp,
+                hdr.report_ipv4.ecn,
+                hdr.report_ipv4.len,
+                hdr.report_ipv4.identification,
+                hdr.report_ipv4.flags,
+                hdr.report_ipv4.frag_offset,
+                hdr.report_ipv4.ttl,
+                hdr.report_ipv4.protocol,
+                hdr.report_ipv4.src_addr,
+                hdr.report_ipv4.dst_addr
+            },
+            hdr.report_ipv4.hdr_checksum,
+            HashAlgorithm.csum16
+        );
+        #endif // __INT_HEADERS__
     }
 }
 
diff --git a/pipelines/basic/src/main/resources/include/defines.p4 b/pipelines/basic/src/main/resources/include/defines.p4
index ba5952e..4933ec2 100644
--- a/pipelines/basic/src/main/resources/include/defines.p4
+++ b/pipelines/basic/src/main/resources/include/defines.p4
@@ -20,8 +20,14 @@
 #define ETH_TYPE_IPV4 0x0800
 #define IP_PROTO_TCP 8w6
 #define IP_PROTO_UDP 8w17
+#define IP_VERSION_4 4w4
+#define IPV4_IHL_MIN 4w5
 #define MAX_PORTS 511
 
+
+typedef bit<48> mac_t;
+typedef bit<32> ip_address_t;
+typedef bit<16> l4_port_t;
 typedef bit<9>  port_t;
 typedef bit<16> next_hop_id_t;
 
@@ -31,4 +37,14 @@
 const MeterColor MeterColor_GREEN = 8w0;
 const MeterColor MeterColor_YELLOW = 8w1;
 const MeterColor MeterColor_RED = 8w2;
+
+// FIXME: this works only on BMv2
+#define PKT_INSTANCE_TYPE_NORMAL 0
+#define PKT_INSTANCE_TYPE_INGRESS_CLONE 1
+#define PKT_INSTANCE_TYPE_EGRESS_CLONE 2
+#define PKT_INSTANCE_TYPE_COALESCED 3
+#define PKT_INSTANCE_TYPE_INGRESS_RECIRC 4
+#define PKT_INSTANCE_TYPE_REPLICATION 5
+#define PKT_INSTANCE_TYPE_RESUBMIT 6
+
 #endif
diff --git a/pipelines/basic/src/main/resources/include/headers.p4 b/pipelines/basic/src/main/resources/include/headers.p4
index 1676646..0c868242 100644
--- a/pipelines/basic/src/main/resources/include/headers.p4
+++ b/pipelines/basic/src/main/resources/include/headers.p4
@@ -36,6 +36,7 @@
     bit<48> src_addr;
     bit<16> ether_type;
 }
+const bit<8> ETH_HEADER_LEN = 14;
 
 header ipv4_t {
     bit<4>  version;
@@ -52,6 +53,7 @@
     bit<32> src_addr;
     bit<32> dst_addr;
 }
+const bit<8> IPV4_MIN_HEAD_LEN = 20;
 
 header tcp_t {
     bit<16> src_port;
@@ -73,5 +75,6 @@
     bit<16> length_;
     bit<16> checksum;
 }
+const bit<8> UDP_HEADER_LEN = 8;
 
 #endif
diff --git a/pipelines/basic/src/main/resources/include/int_definitions.p4 b/pipelines/basic/src/main/resources/include/int_definitions.p4
index 18125f2..17fd69e 100644
--- a/pipelines/basic/src/main/resources/include/int_definitions.p4
+++ b/pipelines/basic/src/main/resources/include/int_definitions.p4
@@ -29,5 +29,8 @@
 const bit<8> INT_HEADER_LEN_WORD = 4;
 
 const bit<8> CPU_MIRROR_SESSION_ID = 250;
+const bit<32> REPORT_MIRROR_SESSION_ID = 500;
+const bit<6> HW_ID = 1;
+const bit<8> REPORT_HDR_TTL = 64;
 
-#endif
\ No newline at end of file
+#endif
diff --git a/pipelines/basic/src/main/resources/include/int_headers.p4 b/pipelines/basic/src/main/resources/include/int_headers.p4
index d55c571..c98ea31 100644
--- a/pipelines/basic/src/main/resources/include/int_headers.p4
+++ b/pipelines/basic/src/main/resources/include/int_headers.p4
@@ -18,7 +18,11 @@
 #ifndef __CUSTOM_HEADERS__
 #define __CUSTOM_HEADERS__
 
-/* INT headers */
+#ifndef __INT_HEADERS__
+#define __INT_HEADERS__
+#include "telemetry_report_headers.p4"
+
+// INT headers
 header int_header_t {
     bit<2>  ver;
     bit<2>  rep;
@@ -70,14 +74,14 @@
     varbit<8032> data;
 }
 
-/* INT shim header for TCP/UDP */
+// INT shim header for TCP/UDP
 header intl4_shim_t {
     bit<8> int_type;
     bit<8> rsvd1;
     bit<8> len;
     bit<8> rsvd2;
 }
-/* INT tail header for TCP/UDP */
+// INT tail header for TCP/UDP
 header intl4_tail_t {
     bit<8> next_proto;
     bit<16> dest_port;
@@ -97,11 +101,18 @@
 struct headers_t {
     packet_out_header_t packet_out;
     packet_in_header_t packet_in;
+    // INT Report Encapsulation
+    ethernet_t report_ethernet;
+    ipv4_t report_ipv4;
+    udp_t report_udp;
+    // INT Report Headers
+    report_fixed_header_t report_fixed_header;
+    local_report_t report_local;
+    // Original packet's headers
     ethernet_t ethernet;
     ipv4_t ipv4;
     tcp_t tcp;
     udp_t udp;
-
     // INT specific headers
     intl4_shim_t intl4_shim;
     int_header_t int_header;
@@ -123,6 +134,8 @@
     next_hop_id_t next_hop_id;
     bit<16>       selector;
     int_metadata_t int_meta;
+    bool compute_checksum;
 }
 
-#endif
\ No newline at end of file
+#endif // __INT_HEADERS__
+#endif // __CUSTOM_HEADERS__
\ No newline at end of file
diff --git a/pipelines/basic/src/main/resources/include/int_parser.p4 b/pipelines/basic/src/main/resources/include/int_parser.p4
index b178ea4..2d03c20 100644
--- a/pipelines/basic/src/main/resources/include/int_parser.p4
+++ b/pipelines/basic/src/main/resources/include/int_parser.p4
@@ -106,6 +106,10 @@
     in headers_t hdr) {
     apply {
         packet.emit(hdr.packet_in);
+        packet.emit(hdr.report_ethernet);
+        packet.emit(hdr.report_ipv4);
+        packet.emit(hdr.report_udp);
+        packet.emit(hdr.report_fixed_header);
         packet.emit(hdr.ethernet);
         packet.emit(hdr.ipv4);
         packet.emit(hdr.tcp);
@@ -125,4 +129,4 @@
     }
 }
 
-#endif
\ No newline at end of file
+#endif
diff --git a/pipelines/basic/src/main/resources/include/int_report.p4 b/pipelines/basic/src/main/resources/include/int_report.p4
new file mode 100644
index 0000000..b7ca357
--- /dev/null
+++ b/pipelines/basic/src/main/resources/include/int_report.p4
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __INT_REPORT__
+#define __INT_REPORT__
+
+#include "telemetry_report_headers.p4"
+
+control process_int_report (
+    inout headers_t hdr,
+    inout local_metadata_t local_metadata,
+    inout standard_metadata_t standard_metadata) {
+
+    action add_report_fixed_header() {
+        /* Device should include its own INT metadata as embedded,
+         * we'll not use local_report_header for this purpose.
+         */
+        hdr.report_fixed_header.setValid();
+        hdr.report_fixed_header.ver = 0;
+        /* only support for flow_watchlist */
+        hdr.report_fixed_header.nproto = NPROTO_ETHERNET;
+        hdr.report_fixed_header.d = 0;
+        hdr.report_fixed_header.q = 0;
+        hdr.report_fixed_header.f = 1;
+        hdr.report_fixed_header.rsvd = 0;
+        //TODO how to get information specific to the switch
+        hdr.report_fixed_header.hw_id = HW_ID;
+        // TODO how save a variable and increment
+        hdr.report_fixed_header.seq_no = 0;
+        //TODO how to get timestamp from ingress ns
+        hdr.report_fixed_header.ingress_tstamp =  (bit<32>) standard_metadata.enq_timestamp;
+
+    }
+
+    action do_report_encapsulation(mac_t src_mac, mac_t mon_mac, ip_address_t src_ip,
+                        ip_address_t mon_ip, l4_port_t mon_port) {
+        //Report Ethernet Header
+        hdr.report_ethernet.setValid();
+        hdr.report_ethernet.dst_addr = mon_mac;
+        hdr.report_ethernet.src_addr = src_mac;
+        hdr.report_ethernet.ether_type = ETH_TYPE_IPV4;
+
+        //Report IPV4 Header
+        hdr.report_ipv4.setValid();
+        hdr.report_ipv4.version = IP_VERSION_4;
+        hdr.report_ipv4.ihl = IPV4_IHL_MIN;
+        hdr.report_ipv4.dscp = 6w0;
+        hdr.report_ipv4.ecn = 2w0;
+        /* Total Len is report_ipv4_len + report_udp_len + report_fixed_hdr_len + ethernet_len + ipv4_totalLen */
+        hdr.report_ipv4.len = (bit<16>) IPV4_MIN_HEAD_LEN + (bit<16>) UDP_HEADER_LEN +
+                                (bit<16>) REPORT_FIXED_HEADER_LEN +  (bit<16>) ETH_HEADER_LEN + hdr.ipv4.len;
+        /* Dont Fragment bit should be set */
+        hdr.report_ipv4.identification = 0;
+        hdr.report_ipv4.flags = 0;
+        hdr.report_ipv4.frag_offset = 0;
+        hdr.report_ipv4.ttl = REPORT_HDR_TTL;
+        hdr.report_ipv4.protocol = IP_PROTO_UDP;
+        hdr.report_ipv4.src_addr = src_ip;
+        hdr.report_ipv4.dst_addr = mon_ip;
+
+        //Report UDP Header
+        hdr.report_udp.setValid();
+        hdr.report_udp.src_port = 0;
+        hdr.report_udp.dst_port = mon_port;
+        hdr.report_udp.length_ =  (bit<16>) UDP_HEADER_LEN + (bit<16>) REPORT_FIXED_HEADER_LEN +
+                                    (bit<16>) ETH_HEADER_LEN + hdr.ipv4.len;
+
+        local_metadata.compute_checksum = true;
+        add_report_fixed_header();
+    }
+
+    /* Cloned packet instance_type is PKT_INSTANCE_TYPE_INGRESS_CLONE=1
+     * Packet is forwarded according to the mirroring_add command
+     */
+    table tb_generate_report {
+        key = {
+            standard_metadata.instance_type: exact;
+        }
+        actions = {
+            do_report_encapsulation;
+        }
+    }
+
+    apply {
+        tb_generate_report.apply();
+    }
+}
+#endif
diff --git a/pipelines/basic/src/main/resources/include/int_sink.p4 b/pipelines/basic/src/main/resources/include/int_sink.p4
index f621533..7023671 100644
--- a/pipelines/basic/src/main/resources/include/int_sink.p4
+++ b/pipelines/basic/src/main/resources/include/int_sink.p4
@@ -18,7 +18,6 @@
 #ifndef __INT_SINK__
 #define __INT_SINK__
 
-// TODO: implement report logic to external collector
 control process_int_sink (
     inout headers_t hdr,
     inout local_metadata_t local_metadata,
@@ -30,7 +29,7 @@
 
     action int_sink() {
         // restore length fields of IPv4 header and UDP header
-        hdr.ipv4.len = hdr.ipv4.len - (bit<16>)((hdr.intl4_shim.len - (bit<8>)hdr.int_header.ins_cnt) << 2); 
+        hdr.ipv4.len = hdr.ipv4.len - (bit<16>)((hdr.intl4_shim.len - (bit<8>)hdr.int_header.ins_cnt) << 2);
         hdr.udp.length_ = hdr.udp.length_ - (bit<16>)((hdr.intl4_shim.len - (bit<8>)hdr.int_header.ins_cnt) << 2);
         // remove all the INT information from the packet
         hdr.int_header.setInvalid();
@@ -52,4 +51,4 @@
         int_sink();
     }
 }
-#endif
\ No newline at end of file
+#endif
diff --git a/pipelines/basic/src/main/resources/include/int_source.p4 b/pipelines/basic/src/main/resources/include/int_source.p4
index 1f16ba8..6fd2730 100644
--- a/pipelines/basic/src/main/resources/include/int_source.p4
+++ b/pipelines/basic/src/main/resources/include/int_source.p4
@@ -110,7 +110,7 @@
     }
     table tb_set_sink {
         key = {
-            standard_metadata.egress_port: exact;
+            standard_metadata.egress_spec: exact;
         }
         actions = {
             int_set_sink;
diff --git a/pipelines/basic/src/main/resources/include/telemetry_report_headers.p4 b/pipelines/basic/src/main/resources/include/telemetry_report_headers.p4
new file mode 100644
index 0000000..bf0f6f7
--- /dev/null
+++ b/pipelines/basic/src/main/resources/include/telemetry_report_headers.p4
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+
+/* -*- P4_16 -*- */
+#ifndef __TELEMETRY_REPORT_HEADERS__
+#define __TELEMETRY_REPORT_HEADERS__
+
+const bit<4> NPROTO_ETHERNET = 0;
+const bit<4> NPROTO_TELEMETRY_DROP_HEADER = 1;
+const bit<4> NPROTO_TELEMETRY_SWITCH_LOCAL_HEADER = 2;
+
+
+// Report Telemetry Headers
+header report_fixed_header_t {
+    bit<4>  ver;
+    bit<4>  nproto;
+    bit<1>  d;
+    bit<1>  q;
+    bit<1>  f;
+    bit<15> rsvd;
+    bit<6>  hw_id;
+    bit<32> seq_no;
+    bit<32> ingress_tstamp;
+}
+const bit<8> REPORT_FIXED_HEADER_LEN = 12;
+
+// Telemetry drop report header
+header drop_report_header_t {
+    bit<32> switch_id;
+    bit<16> ingress_port_id;
+    bit<16> egress_port_id;
+    bit<8>  queue_id;
+    bit<8>  drop_reason;
+    bit<16> pad;
+}
+const bit<8> DROP_REPORT_HEADER_LEN = 12;
+
+// Switch Local Report Header
+header local_report_header_t {
+    bit<32> switch_id;
+    bit<16> ingress_port_id;
+    bit<16> egress_port_id;
+    bit<8>  queue_id;
+    bit<24> queue_occupancy;
+    bit<32> egress_tstamp;
+}
+const bit<8> LOCAL_REPORT_HEADER_LEN = 16;
+
+header_union local_report_t {
+    drop_report_header_t drop_report_header;
+    local_report_header_t local_report_header;
+}
+
+#endif