Merge "Fixed document for Grafana Port usage"
[yardstick.git] / yardstick / network_services / traffic_profile / traffic_profile.py
index 4c6595d..3b19ff9 100644 (file)
@@ -19,6 +19,7 @@ import socket
 import logging
 from random import SystemRandom
 import six
+import ipaddress
 
 from yardstick.network_services.traffic_profile.base import TrafficProfile
 from trex_stl_lib.trex_stl_client import STLStream
@@ -44,6 +45,8 @@ SRC_PORT = 'sport'
 DST_PORT = 'dport'
 TYPE_OF_SERVICE = 'tos'
 
+LOG = logging.getLogger(__name__)
+
 
 class TrexProfile(TrafficProfile):
     """ This class handles Trex Traffic profile generation and execution """
@@ -66,7 +69,8 @@ class TrexProfile(TrafficProfile):
         return f
 
     def _ethernet_range_action_partial(self, direction, _):
-        def partial(min_value, max_value):
+        def partial(min_value, max_value, count):
+            # pylint: disable=unused-argument
             stl_vm_flow_var = STLVmFlowVar(name="mac_{}".format(direction),
                                            min_value=1,
                                            max_value=30,
@@ -80,7 +84,16 @@ class TrexProfile(TrafficProfile):
         return partial
 
     def _ip_range_action_partial(self, direction, count=1):
-        def partial(min_value, max_value):
+        # pylint: disable=unused-argument
+        def partial(min_value, max_value, count):
+            ip1 = int(ipaddress.IPv4Address(min_value))
+            ip2 = int(ipaddress.IPv4Address(max_value))
+            actual_count = (ip2 - ip1)
+            if not actual_count:
+                count = 1
+            elif actual_count < int(count):
+                count = actual_count
+
             stl_vm_flow_var = STLVmFlowVarRepeatableRandom(name="ip4_{}".format(direction),
                                                            min_value=min_value,
                                                            max_value=max_value,
@@ -96,7 +109,8 @@ class TrexProfile(TrafficProfile):
         return partial
 
     def _ip6_range_action_partial(self, direction, _):
-        def partial(min_value, max_value):
+        def partial(min_value, max_value, count):
+            # pylint: disable=unused-argument
             min_value, max_value = self._get_start_end_ipv6(min_value, max_value)
             stl_vm_flow_var = STLVmFlowVar(name="ip6_{}".format(direction),
                                            min_value=min_value,
@@ -112,7 +126,8 @@ class TrexProfile(TrafficProfile):
         return partial
 
     def _dscp_range_action_partial(self, *_):
-        def partial(min_value, max_value):
+        def partial(min_value, max_value, count):
+            # pylint: disable=unused-argument
             stl_vm_flow_var = STLVmFlowVar(name="dscp",
                                            min_value=min_value,
                                            max_value=max_value,
@@ -123,9 +138,17 @@ class TrexProfile(TrafficProfile):
             stl_vm_wr_flow_var = STLVmWrFlowVar(fv_name='dscp',
                                                 pkt_offset='IP.tos')
             self.vm_flow_vars.append(stl_vm_wr_flow_var)
+        return partial
 
     def _udp_range_action_partial(self, field, count=1):
-        def partial(min_value, max_value):
+        # pylint: disable=unused-argument
+        def partial(min_value, max_value, count):
+            actual_count = int(max_value) - int(min_value)
+            if not actual_count:
+                count = 1
+            elif int(count) > actual_count:
+                count = actual_count
+
             stl_vm_flow_var = STLVmFlowVarRepeatableRandom(name="port_{}".format(field),
                                                            min_value=min_value,
                                                            max_value=max_value,
@@ -134,7 +157,7 @@ class TrexProfile(TrafficProfile):
                                                            seed=0x1235)
             self.vm_flow_vars.append(stl_vm_flow_var)
             stl_vm_wr_flow_var = STLVmWrFlowVar(fv_name='port_{}'.format(field),
-                                                pkt_offset=self.udp_sport)
+                                                pkt_offset=self.udp[field])
             self.vm_flow_vars.append(stl_vm_wr_flow_var)
         return partial
 
@@ -156,8 +179,10 @@ class TrexProfile(TrafficProfile):
         self.ip_packet = None
         self.ip6_packet = None
         self.udp_packet = None
-        self.udp_dport = ''
-        self.udp_sport = ''
+        self.udp = {
+            SRC_PORT: '',
+            DST_PORT: '',
+        }
         self.qinq_packet = None
         self.qinq = False
         self.vm_flow_vars = []
@@ -188,9 +213,9 @@ class TrexProfile(TrafficProfile):
                   ),
         }
 
-    def execute(self, traffic_generator):
+    def execute_traffic(self, traffic_generator):
         """ Generate the stream and run traffic on the given ports """
-        pass
+        raise NotImplementedError()
 
     def _call_on_range(self, range, single_action, range_action, count=1, to_int=False):
         def convert_to_int(val):
@@ -203,13 +228,14 @@ class TrexProfile(TrafficProfile):
         except StopIteration:
             single_action(min_value)
         else:
-            range_action(min_value=min_value, max_value=max_value)
+            range_action(min_value=min_value, max_value=max_value, count=count)
 
     def _set_proto_addr(self, protocol, field, address, count=1):
         single_action, range_action, to_int = self._map_proto_actions[protocol]
         self._call_on_range(address,
                             single_action(field),
                             range_action(field, count),
+                            count=count,
                             to_int=to_int,
                             )
 
@@ -266,8 +292,8 @@ class TrexProfile(TrafficProfile):
             ip_params['proto'] = socket.getprotobyname(outer_l3v4['proto'])
             if outer_l3v4['proto'] == 'tcp':
                 self.udp_packet = Pkt.TCP()
-                self.udp_dport = 'TCP.dport'
-                self.udp_sport = 'TCP.sport'
+                self.udp[DST_PORT] = 'TCP.dport'
+                self.udp[SRC_PORT] = 'TCP.sport'
                 tcp_params = {'flags': '', 'window': 0}
                 self._set_proto_fields(UDP, **tcp_params)
         if 'ttl' in outer_l3v4:
@@ -289,8 +315,8 @@ class TrexProfile(TrafficProfile):
             ip6_params['proto'] = outer_l3v6['proto']
             if outer_l3v6['proto'] == 'tcp':
                 self.udp_packet = Pkt.TCP()
-                self.udp_dport = 'TCP.dport'
-                self.udp_sport = 'TCP.sport'
+                self.udp[DST_PORT] = 'TCP.dport'
+                self.udp[SRC_PORT] = 'TCP.sport'
                 tcp_params = {'flags': '', 'window': 0}
                 self._set_proto_fields(UDP, **tcp_params)
         if 'ttl' in outer_l3v6:
@@ -364,8 +390,8 @@ class TrexProfile(TrafficProfile):
         self.ip_packet = Pkt.IP()
         self.ip6_packet = None
         self.udp_packet = Pkt.UDP()
-        self.udp_dport = 'UDP.dport'
-        self.udp_sport = 'UDP.sport'
+        self.udp[DST_PORT] = 'UDP.dport'
+        self.udp[SRC_PORT] = 'UDP.sport'
         self.qinq = False
         self.vm_flow_vars = []
         outer_l2 = packet_definition.get('outer_l2', None)