NFVBENCH-215 Fix wrong throughput ratio in latency tests
[nfvbench.git] / nfvbench / traffic_server.py
index 91608dd..2c85286 100644 (file)
@@ -16,7 +16,7 @@ import os
 import subprocess
 import yaml
 
-from log import LOG
+from .log import LOG
 
 
 class TrafficServerException(Exception):
@@ -49,26 +49,40 @@ class TRexTrafficServer(TrafficServer):
             mbuf_opt = "--mbuf-factor " + str(generator_config.mbuf_factor)
         else:
             mbuf_opt = ""
+        hdrh_opt = "--hdrh" if generator_config.hdrh else ""
         # --unbind-unused-ports: for NIC that have more than 2 ports such as Intel X710
         # this will instruct trex to unbind all ports that are unused instead of
         # erroring out with an exception (i40e only)
-        subprocess.Popen(['nohup', '/bin/bash', '-c',
-                          './t-rex-64 -i -c {} --iom 0 --no-scapy-server '
-                          '--unbind-unused-ports --close-at-end {} '
-                          '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
-                                                                            vlan_opt,
-                                                                            mbuf_opt, cfg)],
-                         cwd=self.trex_dir)
-        LOG.info('TRex server is running...')
+        # Try: --ignore-528-issue -> neither unbind nor exit with error,
+        #                            just proceed cause it might work!
+        # Note that force unbinding is probably a bad choice:
+        # we can't assume for sure that other ports are "unused".
+        # The default TRex behaviour - exit - is indeed a safer option;
+        # a message informs about the ports that should be unbound.
+        i40e_opt = ("--ignore-528-issue" if
+                    generator_config.config.i40e_mixed == 'ignore' else
+                    "--unbind-unused-ports" if
+                    generator_config.config.i40e_mixed == 'unbind' else "")
+        cmd = ['nohup', '/bin/bash', '-c',
+               './t-rex-64 -i -c {} --iom 0 --no-scapy-server '
+               '--close-at-end {} {} {} '
+               '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
+                                                                 i40e_opt,
+                                                                 vlan_opt,
+                                                                 hdrh_opt,
+                                                                 mbuf_opt, cfg)]
+        LOG.info(' '.join(cmd))
+        with subprocess.Popen(cmd, cwd=self.trex_dir) as trex_process:
+            LOG.info('TRex server is running (PID: %s)...', trex_process.pid)
 
     def __load_config(self, filename):
         result = {}
         if os.path.exists(filename):
             with open(filename, 'r') as stream:
                 try:
-                    result = yaml.load(stream)
+                    result = yaml.safe_load(stream)
                 except yaml.YAMLError as exc:
-                    print exc
+                    print(exc)
         return result
 
     def __save_config(self, generator_config, filename):
@@ -82,43 +96,81 @@ class TRexTrafficServer(TrafficServer):
 
     def __prepare_config(self, generator_config):
         ifs = ",".join([repr(pci) for pci in generator_config.pcis])
+
+        # For consistency and stability reasons, the T-Rex server
+        # should be forciby restarted each time the value of a
+        # parameter, specified as one of the starting command line
+        # arguments, has been modified since the last launch.
+        # Hence we add some extra fields to the config file
+        # (nb_cores, use_vlan, mbuf_factor, i40e_mixed, hdrh)
+        # which will serve as a memory between runs -
+        # while being actually ignored by the T-Rex server.
+
         result = """# Config generated by NFVbench
-        - port_limit : 2
-          version    : 2
+        - port_limit   : 2
+          version      : 2
           zmq_pub_port : {zmq_pub_port}
           zmq_rpc_port : {zmq_rpc_port}
           prefix       : {prefix}
           limit_memory : {limit_memory}
-          interfaces : [{ifs}]""".format(zmq_pub_port=generator_config.zmq_pub_port,
-                                         zmq_rpc_port=generator_config.zmq_rpc_port,
-                                         prefix=generator_config.name,
-                                         limit_memory=generator_config.limit_memory,
-                                         ifs=ifs)
-        if hasattr(generator_config, 'platform'):
-            if generator_config.platform.master_thread_id \
-                    and generator_config.platform.latency_thread_id:
+          command_line :
+            sw_mode    : {sw_mode}
+            mbuf_factor: {mbuf_factor}
+            hdrh       : {hdrh}
+            nb_cores   : {nb_cores}
+            use_vlan   : {use_vlan}
+            i40e_mixed : {i40e_mixed}
+          interfaces   : [{ifs}]""".format(
+            zmq_pub_port=generator_config.zmq_pub_port,
+            zmq_rpc_port=generator_config.zmq_rpc_port,
+            prefix=generator_config.name,
+            limit_memory=generator_config.limit_memory,
+            sw_mode=generator_config.software_mode,
+            mbuf_factor=generator_config.mbuf_factor,
+            hdrh=generator_config.hdrh,
+            nb_cores=generator_config.cores,
+            use_vlan=generator_config.gen_config.get('vtep_vlan') or
+            generator_config.vlan_tagging,
+            i40e_mixed=generator_config.config.i40e_mixed,
+            ifs=ifs)
+
+        if hasattr(generator_config, 'mbuf_64') and generator_config.mbuf_64:
+            result += """
+          memory       :
+            mbuf_64           : {mbuf_64}""".format(mbuf_64=generator_config.mbuf_64)
+
+        if self.__check_platform_config(generator_config):
+            try:
                 platform = """
           platform     :
             master_thread_id  : {master_thread_id}
             latency_thread_id : {latency_thread_id}
-            dual_if:""".format(master_thread_id=generator_config.platform.master_thread_id,
-                               latency_thread_id=generator_config.platform.latency_thread_id)
+            dual_if:""".format(master_thread_id=generator_config.gen_config.platform.
+                               master_thread_id,
+                               latency_thread_id=generator_config.gen_config.platform.
+                               latency_thread_id)
                 result += platform
 
-                for core in generator_config.platform.dual_if:
+                for core in generator_config.gen_config.platform.dual_if:
                     threads = ""
                     try:
                         threads = ",".join([repr(thread) for thread in core.threads])
                     except TypeError:
-                        LOG.warn("No threads defined for socket %s", core.socket)
+                        LOG.warning("No threads defined for socket %s", core.socket)
                     core_result = """
                   - socket : {socket}
                     threads : [{threads}]""".format(socket=core.socket, threads=threads)
                     result += core_result
-            else:
-                LOG.info("Generator profile 'platform' sub-properties are set but not filled in \
-                         config file. TRex will use default values.")
-        return result
+            except (KeyError, AttributeError):
+                pass
+        return result + "\n"
+
+    def __check_platform_config(self, generator_config):
+        return hasattr(generator_config.gen_config, 'platform') \
+            and hasattr(generator_config.gen_config.platform, "master_thread_id") \
+            and generator_config.gen_config.platform.master_thread_id is not None \
+            and hasattr(generator_config.gen_config.platform, "latency_thread_id") \
+            and generator_config.gen_config.platform.latency_thread_id is not None
 
     def check_config_updated(self, generator_config):
         existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')