NFVBENCH-144 Trex cannot take account NFVBench config (platform thread id 0)
[nfvbench.git] / nfvbench / traffic_server.py
index df0a3be..4dc7dce 100644 (file)
@@ -49,17 +49,42 @@ class TRexTrafficServer(TrafficServer):
             mbuf_opt = "--mbuf-factor " + str(generator_config.mbuf_factor)
         else:
             mbuf_opt = ""
-        subprocess.Popen(['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,
-                                                                            vlan_opt,
-                                                                            mbuf_opt, cfg)],
-                         cwd=self.trex_dir)
+        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)
+        cmd = ['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,
+                                                                 hdrh_opt,
+                                                                 mbuf_opt, cfg)]
+        LOG.info(' '.join(cmd))
+        subprocess.Popen(cmd, cwd=self.trex_dir)
         LOG.info('TRex server is running...')
 
+    def __load_config(self, filename):
+        result = {}
+        if os.path.exists(filename):
+            with open(filename, 'r') as stream:
+                try:
+                    result = yaml.safe_load(stream)
+                except yaml.YAMLError as exc:
+                    print exc
+        return result
+
     def __save_config(self, generator_config, filename):
-        ifs = ",".join([repr(pci) for pci in generator_config.pcis])
+        result = self.__prepare_config(generator_config)
+        yaml.safe_load(result)
+        if os.path.exists(filename):
+            os.remove(filename)
+        with open(filename, 'w') as f:
+            f.write(result)
+        return filename
 
+    def __prepare_config(self, generator_config):
+        ifs = ",".join([repr(pci) for pci in generator_config.pcis])
         result = """# Config generated by NFVbench
         - port_limit : 2
           version    : 2
@@ -72,9 +97,8 @@ class TRexTrafficServer(TrafficServer):
                                          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:
+        if self.__check_platform_config(generator_config):
+            try:
                 platform = """
           platform     :
             master_thread_id  : {master_thread_id}
@@ -93,14 +117,22 @@ class TRexTrafficServer(TrafficServer):
                   - 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.")
+            except (KeyError, AttributeError):
+                pass
+        return result
 
-        yaml.safe_load(result)
-        if os.path.exists(filename):
-            os.remove(filename)
-        with open(filename, 'w') as f:
-            f.write(result)
+    def __check_platform_config(self, generator_config):
+        return hasattr(generator_config, 'platform') \
+            and hasattr(generator_config.platform, "master_thread_id") \
+            and generator_config.platform.master_thread_id is not None \
+            and hasattr(generator_config.platform, "latency_thread_id") \
+            and generator_config.platform.latency_thread_id is not None
 
-        return filename
+    def check_config_updated(self, generator_config):
+        existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')
+        new_config = yaml.safe_load(self.__prepare_config(generator_config))
+        LOG.debug("Existing config: %s", existing_config)
+        LOG.debug("New config: %s", new_config)
+        if existing_config == new_config:
+            return False
+        return True