nsb_installation: updates
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_trex.py
index 616b331..458f1b8 100644 (file)
@@ -21,14 +21,23 @@ import os
 
 import yaml
 
-from yardstick.common.utils import mac_address_to_hex_list
+from yardstick.common.utils import mac_address_to_hex_list, try_int
 from yardstick.network_services.utils import get_nsb_option
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
+from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
 
 LOG = logging.getLogger(__name__)
 
 
+class TrexDpdkVnfSetupEnvHelper(DpdkVnfSetupEnvHelper):
+    APP_NAME = "t-rex-64"
+    CFG_CONFIG = ""
+    CFG_SCRIPT = ""
+    PIPELINE_COMMAND = ""
+    VNF_TYPE = "TG"
+
+
 class TrexResourceHelper(ClientResourceHelper):
 
     CONF_FILE = '/tmp/trex_cfg.yaml'
@@ -36,40 +45,40 @@ class TrexResourceHelper(ClientResourceHelper):
     RESOURCE_WORD = 'trex'
     RUN_DURATION = 0
 
-    SYNC_PORT = 4500
-    ASYNC_PORT = 4501
+    ASYNC_PORT = 4500
+    SYNC_PORT = 4501
 
     def generate_cfg(self):
-        ext_intf = self.vnfd_helper.interfaces
+        port_names = self.vnfd_helper.port_pairs.all_ports
         vpci_list = []
         port_list = []
-        trex_cfg = {
-            'port_limit': 0,
-            'version': '2',
-            'interfaces': vpci_list,
-            'port_info': port_list,
-            "port_limit": len(ext_intf),
-            "version": '2',
-        }
-        cfg_file = [trex_cfg]
 
-        for interface in ext_intf:
+        port_nums = sorted(self.vnfd_helper.port_nums(port_names))
+        for port_num in port_nums:
+            interface = self.vnfd_helper.find_interface_by_port(port_num)
             virtual_interface = interface['virtual-interface']
-            vpci_list.append(virtual_interface["vpci"])
             dst_mac = virtual_interface["dst_mac"]
 
+            # why skip?, ordering is based on DPDK port number so we can't skip
             if not dst_mac:
                 continue
-
+            # TRex ports must be in DPDK port number, so order of append matters
+            vpci_list.append(virtual_interface["vpci"])
             local_mac = virtual_interface["local_mac"]
             port_list.append({
                 "src_mac": mac_address_to_hex_list(local_mac),
                 "dest_mac": mac_address_to_hex_list(dst_mac),
             })
+        trex_cfg = {
+            'interfaces': vpci_list,
+            'port_info': port_list,
+            "port_limit": len(port_names),
+            "version": '2',
+        }
+        cfg_file = [trex_cfg]
 
         cfg_str = yaml.safe_dump(cfg_file, default_flow_style=False, explicit_start=True)
         self.ssh_helper.upload_config_file(os.path.basename(self.CONF_FILE), cfg_str)
-        self._vpci_ascending = sorted(vpci_list)
 
     def check_status(self):
         status, _, _ = self.ssh_helper.execute("sudo lsof -i:%s" % self.SYNC_PORT)
@@ -79,6 +88,7 @@ class TrexResourceHelper(ClientResourceHelper):
     DISABLE_DEPLOY = True
 
     def setup(self):
+        super(TrexResourceHelper, self).setup()
         if self.DISABLE_DEPLOY:
             return
 
@@ -101,15 +111,28 @@ class TrexResourceHelper(ClientResourceHelper):
 
         self.ssh_helper.execute("sudo pkill -9 rex > /dev/null 2>&1")
 
+        # We MUST default to 1 because TRex won't work on single-queue devices with
+        # more than one core per port
+        # We really should be trying to find the number of queues in the driver,
+        # but there doesn't seem to be a way to do this
+        # TRex Error: the number of cores should be 1 when the driver
+        # support only one tx queue and one rx queue. Please use -c 1
+        threads_per_port = try_int(self.scenario_helper.options.get("queues_per_port"), 1)
+
         trex_path = self.ssh_helper.join_bin_path("trex", "scripts")
         path = get_nsb_option("trex_path", trex_path)
 
-        # cmd = "sudo ./t-rex-64 -i --cfg %s > /dev/null 2>&1" % self.CONF_FILE
-        cmd = "./t-rex-64 -i --cfg '{}'".format(self.CONF_FILE)
+        cmd = "./t-rex-64 --no-scapy-server -i -c {} --cfg '{}'".format(threads_per_port,
+                                                                        self.CONF_FILE)
 
-        # if there are errors we want to see them
+        if self.scenario_helper.options.get("trex_server_debug"):
+            # if there are errors we want to see them
+            redir = ""
+        else:
+            redir = ">/dev/null"
         # we have to sudo cd because the path might be owned by root
-        trex_cmd = """sudo bash -c "cd '{}' ; {}" >/dev/null""".format(path, cmd)
+        trex_cmd = """sudo bash -c "cd '{}' ; {}" {}""".format(path, cmd, redir)
+        LOG.debug(trex_cmd)
         self.ssh_helper.execute(trex_cmd)
 
     def terminate(self):
@@ -130,6 +153,9 @@ class TrexTrafficGen(SampleVNFTrafficGen):
         if resource_helper_type is None:
             resource_helper_type = TrexResourceHelper
 
+        if setup_env_helper_type is None:
+            setup_env_helper_type = TrexDpdkVnfSetupEnvHelper
+
         super(TrexTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
                                              resource_helper_type)