[SDFAB-988] QER rate limiting tests

Change-Id: I4c542a5c9a122c0595b36e5e96d2b093682cfc7c
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/scapy_helper.py b/TestON/tests/USECASE/SegmentRouting/dependencies/scapy_helper.py
index 80546f4..f9e049e 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/scapy_helper.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/scapy_helper.py
@@ -18,6 +18,8 @@
         eth_src=None,
         ip_src="192.168.0.1",
         ip_dst="192.168.0.2",
+        udp_sport=1234,
+        udp_dport=80,
         s1u_addr="100.0.0.1",
         enb_addr="192.168.101.1",
         ip_ttl=64,
@@ -30,7 +32,8 @@
     if ext_psc_type is not None:
         pktlen = pktlen - GTPU_OPTIONS_HDR_BYTES - GTPU_EXT_PSC_BYTES
     pkt = simple_udp_packet(eth_src=eth_src, eth_dst=eth_dst, ip_src=ip_src,
-                            ip_dst=ip_dst, pktlen=pktlen)
+                            ip_dst=ip_dst, udp_sport=udp_sport, udp_dport=udp_dport,
+                            pktlen=pktlen)
     gtp_pkt = pkt_add_gtp(
         pkt,
         out_ipv4_src=enb_addr,
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/trex.py b/TestON/tests/USECASE/SegmentRouting/dependencies/trex.py
index 93a7c70..03a534a 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/trex.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/trex.py
@@ -121,48 +121,79 @@
                 avg_tx, avg_rx)
         )
 
+    def assertRxRate(self, flow_name, duration, delta=0.05):
+        if not self.isFlowStats(flow_name):
+            main.log.info("No flow stats for flow {}".format(flow_name))
+            utilities.assert_equals(
+                expect=True,
+                actual=False,
+                onpass="",
+                onfail="No Flow stats for requested flow: {}".format(flow_name))
+            return
+        expected_rx_rate_bps = int(
+            self.traffic_flows[flow_name].get("expected_rx_bps", "0"))
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
+        flow_id = self.traffic_flows[flow_name]["flow_id"]
+        flow_stats = self.trex_client.getFlowStats(flow_id)
+        actual_rx_rate_bps = (flow_stats.rx_bytes * 8) / duration
+        rates_within_delta = abs((actual_rx_rate_bps/expected_rx_rate_bps) - 1) <= delta
+        utilities.assert_equals(
+            expect=True,
+            actual=rates_within_delta,
+            onpass="Traffic Flow {}: Expected rate ({}) within delta ({}) to actual rate ({})".format(
+                flow_label, expected_rx_rate_bps, delta, actual_rx_rate_bps),
+            onfail="Traffic Flow {}: Expected rate ({}) outside delta ({}) to actual rate ({})".format(
+                flow_label, expected_rx_rate_bps, delta, actual_rx_rate_bps)
+        )
+
     def assertRxPackets(self, flow_name):
         if not self.isFlowStats(flow_name):
             main.log.info("No flow stats for flow {}".format(flow_name))
         expected_min_received = int(
             self.traffic_flows[flow_name].get("expected_min_received", "1"))
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
         flow_id = self.traffic_flows[flow_name]["flow_id"]
         flow_stats = self.trex_client.getFlowStats(flow_id)
         utilities.assert_equals(
             expect=True,
             actual=flow_stats.rx_packets >= expected_min_received,
-            onpass="Traffic Flow {}: Received traffic".format(flow_name),
-            onfail="Traffic Flow {}: No traffic received".format(flow_name))
+            onpass="Traffic Flow {}: Received traffic".format(flow_label),
+            onfail="Traffic Flow {}: No traffic received".format(flow_label))
 
     def assertDroppedPacket(self, flow_name):
         if not self.isFlowStats(flow_name):
             main.log.info("No flow stats for flow {}".format(flow_name))
         expected_max_dropped = int(
             self.traffic_flows[flow_name].get("expected_max_dropped", "0"))
-        latency_stats = self.__getLatencyStats(flow_name)
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
+        flow_id = self.traffic_flows[flow_name]["flow_id"]
+        flow_stats = self.trex_client.getFlowStats(flow_id)
+        actual_dropped = flow_stats.tx_packets - flow_stats.rx_packets
         utilities.assert_equals(
             expect=True,
-            actual=latency_stats.dropped <= expected_max_dropped,
-            onpass="Traffic Flow {}: {} packets dropped, below threshold ({})".format(
-                flow_name, latency_stats.dropped,
-                expected_max_dropped),
-            onfail="Traffic Flow {}: {} packets dropped, above threshold ({})".format(
-                flow_name, latency_stats.dropped,
-                expected_max_dropped))
+            actual=actual_dropped <= expected_max_dropped,
+            onpass="Traffic Flow {}: {} packets dropped, below threshold={}".format(
+                flow_label, actual_dropped, expected_max_dropped
+            ),
+            onfail="Traffic Flow {}: {} packets dropped, above threshold={}".format(
+                flow_label, actual_dropped, expected_max_dropped
+            )
+        )
 
     def assertMaxLatency(self, flow_name):
         if not self.isFlowStats(flow_name):
             main.log.info("No flow stats for flow {}".format(flow_name))
         expected_max_latency = int(
             self.traffic_flows[flow_name].get("expected_max_latency", "0"))
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
         latency_stats = self.__getLatencyStats(flow_name)
         utilities.assert_equals(
             expect=True,
             actual=latency_stats.total_max <= expected_max_latency,
             onpass="Traffic Flow {}: Maximum latency below threshold".format(
-                flow_name),
+                flow_label),
             onfail="Traffic Flow {}: Maximum latency is too high {}".format(
-                flow_name, latency_stats.total_max))
+                flow_label, latency_stats.total_max))
 
     def assert99_9PercentileLatency(self, flow_name):
         if not self.isFlowStats(flow_name):
@@ -174,14 +205,15 @@
         expected_99_9_percentile_latency = int(
             self.traffic_flows[flow_name].get(
                 "expected_99_9_percentile_latency", "0"))
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
         latency_stats = self.__getLatencyStats(flow_name)
         utilities.assert_equals(
             expect=True,
             actual=latency_stats.percentile_99_9 <= expected_99_9_percentile_latency,
             onpass="Traffic Flow {}: 99.9th percentile latency below threshold".format(
-                flow_name),
+                flow_label),
             onfail="Traffic Flow {}: 99.9th percentile latency is too high {}".format(
-                flow_name, latency_stats.percentile_99_9))
+                flow_label, latency_stats.percentile_99_9))
 
     def assert90PercentileLatency(self, flow_name):
         if not self.isFlowStats(flow_name):
@@ -193,14 +225,15 @@
         expected_90_percentile_latency = int(
             self.traffic_flows[flow_name].get(
                 "expected_90_percentile_latency", "0"))
+        flow_label = self.traffic_flows[flow_name].get("name", flow_name)
         latency_stats = self.__getLatencyStats(flow_name)
         utilities.assert_equals(
             expect=True,
             actual=latency_stats.percentile_90 <= expected_90_percentile_latency,
             onpass="Traffic Flow {}: 90th percentile latency below threshold".format(
-                flow_name),
+                flow_label),
             onfail="Traffic Flow {}: 90th percentile latency is too high {}".format(
-                flow_name, latency_stats.percentile_90))
+                flow_label, latency_stats.percentile_90))
 
     def logPortStats(self):
         main.log.debug(self.port_stats)
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/up4.py b/TestON/tests/USECASE/SegmentRouting/dependencies/up4.py
index bb285d4..461fe15 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/up4.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/up4.py
@@ -59,6 +59,7 @@
                 <!-- TC 0 means BEST EFFORT -->
                 <tc>2</tc>
                 <five_g>False</five_g>
+                <max_bps>200000000</max_bps>
             </ue2>
         </ues>
         <switch_to_kill>Leaf2</switch_to_kill> # Component name of the switch to kill in CASE 5
@@ -137,9 +138,6 @@
                                            enableGtp=True)
             if self.pdn_host is not None:
                 self.pdn_host.startScapy(ifaceName=self.pdn_interface["name"])
-        # TODO: configure interfaces table. Currently, we rely on netcfg or
-        #  PFCP agent to push interface entries, but we should explicitly push
-        #  them here
 
     def startMockSmfPcap(self, smfComponent, pcapIface="eth0"):
         compName = "smf-pcap"
@@ -563,19 +561,29 @@
             app_id
         )
 
-    def upUeSessionOnosString(self, teid=None, teid_up=None, sess_meter_idx=DEFAULT_SESSION_METER_IDX, **kwargs):
+    def upUeSessionOnosString(self, teid=None, teid_up=None, up_id=None,
+                              sess_meter_idx=None, max_bps=None, **kwargs):
         if teid_up is None and teid is not None:
             teid_up = teid
+        if up_id is not None:
+            if max_bps is not None:
+                sess_meter_idx = up_id
+            else:
+                sess_meter_idx = DEFAULT_SESSION_METER_IDX
         if sess_meter_idx is None:
             sess_meter_idx = "\d+"
         return "UpfSessionUL\(Match\(tun_dst_addr={}, teid={}\) -> Action\(FWD, session_meter_idx={}\)\)".format(
             self.s1u_address, teid_up, sess_meter_idx)
 
     def downUeSessionOnosString(self, ue_address, down_id=None,
-                                tunn_peer_id=None, sess_meter_idx=DEFAULT_SESSION_METER_IDX,
+                                tunn_peer_id=None, sess_meter_idx=None, max_bps=None,
                                 **kwargs):
         if down_id is not None:
             tunn_peer_id = down_id
+            if max_bps is not None:
+                sess_meter_idx = down_id
+            else:
+                sess_meter_idx = DEFAULT_SESSION_METER_IDX
         if tunn_peer_id is None:
             tunn_peer_id = "\d+"
         if sess_meter_idx is None:
@@ -584,9 +592,13 @@
             ue_address, tunn_peer_id, sess_meter_idx)
 
     def upTerminationOnosString(self, ue_address, app_filter, up_id=None,
-                                ctr_id_up=None, tc=None, app_meter_idx=DEFAULT_APP_METER_IDX, **kwargs):
+                                ctr_id_up=None, tc=None, app_meter_idx=None, **kwargs):
         if up_id is not None:
             ctr_id_up = up_id
+            if "max_bps" in app_filter:
+                app_meter_idx = int(up_id) + int(app_filter["app_id"])
+            else:
+                app_meter_idx = DEFAULT_APP_METER_IDX
         if ctr_id_up is None:
             ctr_id_up = "\d+"
         if tc is None or int(tc) == 0:
@@ -605,7 +617,7 @@
 
     def downTerminationOnosString(self, ue_address, app_filter, teid=None,
                                   down_id=None, ctr_id_down=None, teid_down=None,
-                                  tc=None, app_meter_idx=DEFAULT_APP_METER_IDX,
+                                  tc=None, app_meter_idx=None,
                                   **kwargs):
         if down_id is not None:
             ctr_id_down = down_id
@@ -613,6 +625,10 @@
             ctr_id_down = "\d+"
         if teid_down is None and teid is not None:
             teid_down = int(teid) + 1
+            if "max_bps" in app_filter:
+                app_meter_idx = int(down_id) + int(app_filter["app_id"])
+            else:
+                app_meter_idx = DEFAULT_APP_METER_IDX
         if tc is None or int(tc) == 0:
             tc = "(?:0|null)"
         if app_meter_idx is None:
@@ -643,6 +659,11 @@
             ue["five_g"] = bool(strtobool(ue["five_g"]))
         if "tc" in ue and ue["tc"] == "":
             ue["tc"] = 0
+        if "max_bps" in ue:
+            if ue["max_bps"] == "" or ue["max_bps"] is None:
+                ue["max_bps"] = None
+            else:
+                ue["max_bps"] = int(ue["max_bps"])
         if smf:
             ue["up_id"] = None
             ue["down_id"] = None
@@ -695,28 +716,34 @@
                  teid_up=None, teid_down=None,
                  ctr_id_up=None, ctr_id_down=None,
                  tunn_peer_id=None,
-                 tc=None, five_g=False, **kwargs):
+                 tc=None, five_g=False, max_bps=None,
+                 sess_meter_idx_up=None, sess_meter_idx_down=None, **kwargs):
         self.__programUeRules(ue_name,
                               ue_address,
                               teid, up_id, down_id,
                               teid_up, teid_down,
                               ctr_id_up, ctr_id_down,
                               tunn_peer_id,
-                              tc, five_g, op="program")
+                              tc, five_g, max_bps,
+                              sess_meter_idx_up, sess_meter_idx_down,
+                              op="program")
 
     def detachUe(self, ue_name, ue_address,
                  teid=None, up_id=None, down_id=None,
                  teid_up=None, teid_down=None,
                  ctr_id_up=None, ctr_id_down=None,
                  tunn_peer_id=None,
-                 tc=None, five_g=False, **kwargs):
+                 tc=None, five_g=False, max_bps=None,
+                 sess_meter_idx_up=None, sess_meter_idx_down=None, **kwargs):
         self.__programUeRules(ue_name,
                               ue_address,
                               teid, up_id, down_id,
                               teid_up, teid_down,
                               ctr_id_up, ctr_id_down,
                               tunn_peer_id,
-                              tc, five_g, op="clear")
+                              tc, five_g, max_bps,
+                              sess_meter_idx_up, sess_meter_idx_down,
+                              op="clear")
 
     def __programAppFilter(self, app_id, slice_id, ip_prefix=None, ip_proto=None,
                            port_range=None, priority=0, op="program", **kwargs):
@@ -769,13 +796,21 @@
                          teid=None, up_id=None, down_id=None,
                          teid_up=None, teid_down=None, ctr_id_up=None,
                          ctr_id_down=None, tunn_peer_id=None,
-                         tc=0, five_g=False,
+                         tc=0, five_g=False, max_bps=None,
+                         sess_meter_idx_up=None, sess_meter_idx_down=None,
                          op="program"):
+        if max_bps is None:
+            sess_meter_idx_up = DEFAULT_SESSION_METER_IDX
+            sess_meter_idx_down = DEFAULT_SESSION_METER_IDX
         if up_id is not None:
             ctr_id_up = up_id
+            if max_bps is not None:
+                sess_meter_idx_up = int(up_id)
         if down_id is not None:
             tunn_peer_id = down_id
             ctr_id_down = down_id
+            if max_bps is not None:
+                sess_meter_idx_down = int(down_id)
         if teid is not None:
             teid_up = teid
             teid_down = int(teid) + 1
@@ -786,6 +821,25 @@
         enb_address = self.__getEnbAddress(ue_name)
 
         # ========================#
+        # Session Meters
+        # ========================#
+        if max_bps is not None:
+            if not self.__mod_meter(
+                    'PreQosPipe.session_meter',
+                    sess_meter_idx_up,
+                    max_bps,
+                    op
+            ):
+                return False
+            if not self.__mod_meter(
+                    'PreQosPipe.session_meter',
+                    sess_meter_idx_down,
+                    max_bps,
+                    op
+            ):
+                return False
+
+        # ========================#
         # UE Session Entries
         # ========================#
 
@@ -798,7 +852,7 @@
         matchFields['n3_address'] = str(self.s1u_address)
         matchFields['teid'] = str(teid_up)
         # Action params
-        actionParams["session_meter_idx"] = str(DEFAULT_SESSION_METER_IDX)
+        actionParams["session_meter_idx"] = str(sess_meter_idx_up)
         if five_g:
             # TODO: currently QFI match is unsupported in TNA
             main.log.warn("Matching on QFI is currently unsupported in TNA")
@@ -815,7 +869,7 @@
         matchFields['ue_address'] = str(ue_address)
         # Action params
         actionParams['tunnel_peer_id'] = str(tunn_peer_id)
-        actionParams["session_meter_idx"] = str(DEFAULT_SESSION_METER_IDX)
+        actionParams["session_meter_idx"] = str(sess_meter_idx_down)
         if not self.__add_entry(tableName, actionName, matchFields,
                                 actionParams, entries, op):
             return False
@@ -828,6 +882,17 @@
 
         # Uplink
         for f in self.app_filters.values():
+            if "max_bps" in f:
+                app_meter_idx_up = sess_meter_idx_up + int(f['app_id'])
+                if not self.__mod_meter(
+                        'PreQosPipe.app_meter',
+                        app_meter_idx_up,
+                        int(f["max_bps"]),
+                        op
+                ):
+                    return False
+            else:
+                app_meter_idx_up = DEFAULT_APP_METER_IDX
             tableName = 'PreQosPipe.terminations_uplink'
             matchFields = {}
             actionParams = {}
@@ -839,7 +904,7 @@
             # Action params
             if f['action'] == 'allow':
                 actionName = 'PreQosPipe.uplink_term_fwd'
-                actionParams['app_meter_idx'] = str(DEFAULT_APP_METER_IDX)
+                actionParams['app_meter_idx'] = str(app_meter_idx_up)
                 actionParams['tc'] = str(tc)
             else:
                 actionName = 'PreQosPipe.uplink_term_drop'
@@ -851,6 +916,17 @@
 
         # Downlink
         for f in self.app_filters.values():
+            if "max_bps" in f:
+                app_meter_idx_down = sess_meter_idx_down + int(f['app_id'])
+                if not self.__mod_meter(
+                        'PreQosPipe.app_meter',
+                        app_meter_idx_down,
+                        int(f["max_bps"]),
+                        op
+                ):
+                    return False
+            else:
+                app_meter_idx_down = DEFAULT_APP_METER_IDX
             tableName = 'PreQosPipe.terminations_downlink'
             matchFields = {}
             actionParams = {}
@@ -866,7 +942,7 @@
                 # 1-1 mapping between QFI and TC
                 actionParams['tc'] = str(tc)
                 actionParams['qfi'] = str(tc)
-                actionParams['app_meter_idx'] = str(DEFAULT_APP_METER_IDX)
+                actionParams['app_meter_idx'] = str(app_meter_idx_down)
             else:
                 actionName = 'PreQosPipe.downlink_term_drop'
             actionParams['ctr_idx'] = str(ctr_id_down)
@@ -916,6 +992,28 @@
         })
         return True
 
+    def __mod_meter(self, name, index, max_bps, op):
+        cir = 0
+        cburst = 0
+        pir = max_bps // 8
+        # TRex/DPDK can generate burst of 32 packets, considering MTU=1500, 32x1500B=48KB
+        # Burst must be greater than 48KB
+        pburst = 100000
+        if op == "program":
+            self.up4_client.buildP4RtMeterEntry(
+                meterName=name, index=index, cir=cir, cburst=cburst, pir=pir,
+                pburst=pburst
+            )
+        else:
+            # in case of "clear" don't specify bands to clear meters
+            self.up4_client.buildP4RtMeterEntry(meterName=name, index=index)
+        if self.up4_client.modifyMeterEntry(debug=True) == main.TRUE:
+            main.log.info("*** Meter modified.")
+        else:
+            main.log.error("Error during meter modification")
+            return False
+        return True
+
     def __clear_entries(self, entries):
         for i, entry in enumerate(entries):
             self.up4_client.buildP4RtTableEntry(**entry)