[NFVBENCH-81]With some Intel X710 NIC cards, NFVbench reports erroneous RX counters
[nfvbench.git] / nfvbench / traffic_gen / trex.py
index 68ab95a..683e97e 100644 (file)
@@ -48,6 +48,8 @@ from trex_stl_lib.api import STLVmFlowVarRepetableRandom
 from trex_stl_lib.api import STLVmWrFlowVar
 from trex_stl_lib.api import UDP
 from trex_stl_lib.services.trex_stl_service_arp import STLServiceARP
+
+
 # pylint: enable=import-error
 
 
@@ -64,6 +66,8 @@ class TRex(AbstractTrafficGenerator):
         self.streamblock = defaultdict(list)
         self.rates = []
         self.arps = {}
+        self.capture_id = None
+        self.packet_list = []
 
     def get_version(self):
         return self.client.get_server_version()
@@ -74,21 +78,21 @@ class TRex(AbstractTrafficGenerator):
 
         result = {}
         for ph in self.port_handle:
-            stats = self.__combine_stats(in_stats, ph)
+            stats = in_stats[ph]
             result[ph] = {
                 'tx': {
-                    'total_pkts': cast_integer(stats['tx_pkts']['total']),
-                    'total_pkt_bytes': cast_integer(stats['tx_bytes']['total']),
-                    'pkt_rate': cast_integer(stats['tx_pps']['total']),
-                    'pkt_bit_rate': cast_integer(stats['tx_bps']['total'])
+                    'total_pkts': cast_integer(stats['opackets']),
+                    'total_pkt_bytes': cast_integer(stats['obytes']),
+                    'pkt_rate': cast_integer(stats['tx_pps']),
+                    'pkt_bit_rate': cast_integer(stats['tx_bps'])
                 },
                 'rx': {
-                    'total_pkts': cast_integer(stats['rx_pkts']['total']),
-                    'total_pkt_bytes': cast_integer(stats['rx_bytes']['total']),
-                    'pkt_rate': cast_integer(stats['rx_pps']['total']),
-                    'pkt_bit_rate': cast_integer(stats['rx_bps']['total']),
+                    'total_pkts': cast_integer(stats['ipackets']),
+                    'total_pkt_bytes': cast_integer(stats['ibytes']),
+                    'pkt_rate': cast_integer(stats['rx_pps']),
+                    'pkt_bit_rate': cast_integer(stats['rx_bps']),
                     'dropped_pkts': cast_integer(
-                        stats['tx_pkts']['total'] - stats['rx_pkts']['total'])
+                        stats['opackets'] - stats['ipackets'])
                 }
             }
 
@@ -103,20 +107,6 @@ class TRex(AbstractTrafficGenerator):
         result["total_tx_rate"] = cast_integer(total_tx_pkts / self.config.duration_sec)
         return result
 
-    def __combine_stats(self, in_stats, port_handle):
-        """Traverses TRex result dictionary and combines stream stats. Used for combining latency
-        and regular streams together.
-        """
-        result = defaultdict(lambda: defaultdict(float))
-
-        for pg_id in [self.stream_ids[port_handle]] + self.latencies[port_handle]:
-            record = in_stats['flow_stats'][pg_id]
-            for stat_type, stat_type_values in record.iteritems():
-                for ph, value in stat_type_values.iteritems():
-                    result[stat_type][ph] += value
-
-        return result
-
     def __combine_latencies(self, in_stats, port_handle):
         """Traverses TRex result dictionary and combines chosen latency stats."""
         if not self.latencies[port_handle]:
@@ -138,15 +128,14 @@ class TRex(AbstractTrafficGenerator):
     def create_pkt(self, stream_cfg, l2frame_size):
 
         pkt_base = Ether(src=stream_cfg['mac_src'], dst=stream_cfg['mac_dst'])
-
         if stream_cfg['vlan_tag'] is not None:
             # 50 = 14 (Ethernet II) + 4 (Vlan tag) + 4 (CRC Checksum) + 20 (IPv4) + 8 (UDP)
             pkt_base /= Dot1Q(vlan=stream_cfg['vlan_tag'])
-            payload = 'x' * (max(64, int(l2frame_size)) - 50)
+            l2payload_size = int(l2frame_size) - 50
         else:
             # 46 = 14 (Ethernet II) + 4 (CRC Checksum) + 20 (IPv4) + 8 (UDP)
-            payload = 'x' * (max(64, int(l2frame_size)) - 46)
-
+            l2payload_size = int(l2frame_size) - 46
+        payload = 'x' * l2payload_size
         udp_args = {}
         if stream_cfg['udp_src_port']:
             udp_args['sport'] = int(stream_cfg['udp_src_port'])
@@ -201,6 +190,8 @@ class TRex(AbstractTrafficGenerator):
         idx_lat = None
         streams = []
         if l2frame == 'IMIX':
+            min_size = 64 if stream_cfg['vlan_tag'] is None else 68
+            self.adjust_imix_min_size(min_size)
             for t, (ratio, l2_frame_size) in enumerate(zip(self.imix_ratios, self.imix_l2_sizes)):
                 pkt = self.create_pkt(stream_cfg, l2_frame_size)
                 streams.append(STLStream(packet=pkt,
@@ -436,7 +427,7 @@ class TRex(AbstractTrafficGenerator):
         LOG.info('Cleared all existing streams.')
 
     def get_stats(self):
-        stats = self.client.get_pgid_stats()
+        stats = self.client.get_stats()
         return self.extract_stats(stats)
 
     def get_macs(self):
@@ -453,6 +444,24 @@ class TRex(AbstractTrafficGenerator):
     def stop_traffic(self):
         self.client.stop(ports=self.port_handle)
 
+    def start_capture(self):
+        if self.capture_id:
+            self.stop_capture()
+        self.client.set_service_mode(ports=self.port_handle)
+        self.capture_id = self.client.start_capture(rx_ports=self.port_handle)
+
+    def fetch_capture_packets(self):
+        if self.capture_id:
+            self.packet_list = []
+            self.client.fetch_capture_packets(capture_id=self.capture_id['id'],
+                                              output=self.packet_list)
+
+    def stop_capture(self):
+        if self.capture_id:
+            self.client.stop_capture(capture_id=self.capture_id['id'])
+            self.capture_id = None
+            self.client.set_service_mode(ports=self.port_handle, enabled=False)
+
     def cleanup(self):
         if self.client:
             try: