NFVBENCH-177: Add a config item 'user_info' and theoretical max rate value
[nfvbench.git] / nfvbench / traffic_gen / traffic_base.py
index 9c78d7e..df28772 100644 (file)
 import abc
 import sys
 
+import bitmath
+
 from nfvbench.log import LOG
-import traffic_utils
+from . import traffic_utils
 
 
 class Latency(object):
@@ -27,7 +29,7 @@ class Latency(object):
 
         latency_list: aggregate all latency values from list if not None
         """
-        self.min_usec = sys.maxint
+        self.min_usec = sys.maxsize
         self.max_usec = 0
         self.avg_usec = 0
         self.hdrh = None
@@ -42,15 +44,12 @@ class Latency(object):
 
     def available(self):
         """Return True if latency information is available."""
-        return self.min_usec != sys.maxint
+        return self.min_usec != sys.maxsize
 
 
 class TrafficGeneratorException(Exception):
     """Exception for traffic generator."""
 
-    pass
-
-
 class AbstractTrafficGenerator(object):
 
     def __init__(self, traffic_client):
@@ -85,7 +84,7 @@ class AbstractTrafficGenerator(object):
         LOG.info('Modified traffic stream for port %s, new rate=%s.', port, self.rates[port_index])
 
     @abc.abstractmethod
-    def get_stats(self):
+    def get_stats(self, ifstats):
         # Must be implemented by sub classes
         return None
 
@@ -106,7 +105,6 @@ class AbstractTrafficGenerator(object):
 
     def clear_streamblock(self):
         """Clear all streams from the traffic generator."""
-        pass
 
     @abc.abstractmethod
     def resolve_arp(self):
@@ -116,7 +114,6 @@ class AbstractTrafficGenerator(object):
                 else a dict of list of dest macs indexed by port#
                 the dest macs in the list are indexed by the chain id
         """
-        pass
 
     @abc.abstractmethod
     def get_macs(self):
@@ -124,7 +121,6 @@ class AbstractTrafficGenerator(object):
 
         return: a list of MAC addresses indexed by the port#
         """
-        pass
 
     @abc.abstractmethod
     def get_port_speed_gbps(self):
@@ -132,4 +128,32 @@ class AbstractTrafficGenerator(object):
 
         return: a list of speed in Gbps indexed by the port#
         """
-        pass
+
+    def get_theoretical_rates(self, avg_packet_size):
+
+        result = {}
+
+        intf_speeds = self.get_port_speed_gbps()
+        tg_if_speed = bitmath.parse_string(str(intf_speeds[0]) + 'Gb').bits
+        intf_speed = tg_if_speed
+
+        if hasattr(self.config, 'intf_speed') and self.config.intf_speed is not None:
+            # in case of limitation due to config, TG speed is not accurate
+            # value is overridden by conf
+            if self.config.intf_speed != tg_if_speed:
+                intf_speed = bitmath.parse_string(self.config.intf_speed.replace('ps', '')).bits
+
+        if hasattr(self.config, 'user_info') and self.config.user_info is not None:
+            if "extra_encapsulation_bytes" in self.config.user_info:
+                frame_size_full_encapsulation = avg_packet_size + self.config.user_info[
+                    "extra_encapsulation_bytes"]
+                result['theoretical_tx_rate_pps'] = traffic_utils.bps_to_pps(
+                    intf_speed, frame_size_full_encapsulation) * 2
+                result['theoretical_tx_rate_bps'] = traffic_utils.pps_to_bps(
+                    result['theoretical_tx_rate_pps'], avg_packet_size)
+        else:
+            result['theoretical_tx_rate_pps'] = traffic_utils.bps_to_pps(intf_speed,
+                                                                         avg_packet_size) * 2
+            result['theoretical_tx_rate_bps'] = traffic_utils.pps_to_bps(
+                result['theoretical_tx_rate_pps'], avg_packet_size)
+        return result