fe435f63e56c1418b252ad7cf9dde3bf8e50fb92
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_trex.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Trex acts as traffic generation and vnf definitions based on IETS Spec """
15
16 from __future__ import absolute_import
17 from __future__ import print_function
18
19 import logging
20 import os
21
22 import yaml
23
24 from yardstick.common.utils import mac_address_to_hex_list, try_int
25 from yardstick.network_services.utils import get_nsb_option
26 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
27 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
28 from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
29
30 LOG = logging.getLogger(__name__)
31
32
33 class TrexDpdkVnfSetupEnvHelper(DpdkVnfSetupEnvHelper):
34     APP_NAME = "t-rex-64"
35     CFG_CONFIG = ""
36     CFG_SCRIPT = ""
37     PIPELINE_COMMAND = ""
38     VNF_TYPE = "TG"
39
40
41 class TrexResourceHelper(ClientResourceHelper):
42
43     CONF_FILE = '/tmp/trex_cfg.yaml'
44     QUEUE_WAIT_TIME = 1
45     RESOURCE_WORD = 'trex'
46     RUN_DURATION = 0
47
48     ASYNC_PORT = 4500
49     SYNC_PORT = 4501
50
51     def generate_cfg(self):
52         ext_intf = self.vnfd_helper.interfaces
53         vpci_list = []
54         port_list = []
55         trex_cfg = {
56             'interfaces': vpci_list,
57             'port_info': port_list,
58             "port_limit": len(ext_intf),
59             "version": '2',
60         }
61         cfg_file = [trex_cfg]
62
63         for interface in ext_intf:
64             virtual_interface = interface['virtual-interface']
65             vpci_list.append(virtual_interface["vpci"])
66             dst_mac = virtual_interface["dst_mac"]
67
68             if not dst_mac:
69                 continue
70
71             local_mac = virtual_interface["local_mac"]
72             port_list.append({
73                 "src_mac": mac_address_to_hex_list(local_mac),
74                 "dest_mac": mac_address_to_hex_list(dst_mac),
75             })
76
77         cfg_str = yaml.safe_dump(cfg_file, default_flow_style=False, explicit_start=True)
78         self.ssh_helper.upload_config_file(os.path.basename(self.CONF_FILE), cfg_str)
79
80     def check_status(self):
81         status, _, _ = self.ssh_helper.execute("sudo lsof -i:%s" % self.SYNC_PORT)
82         return status
83
84     # temp disable
85     DISABLE_DEPLOY = True
86
87     def setup(self):
88         super(TrexResourceHelper, self).setup()
89         if self.DISABLE_DEPLOY:
90             return
91
92         trex_path = self.ssh_helper.join_bin_path('trex')
93
94         err = self.ssh_helper.execute("which {}".format(trex_path))[0]
95         if err == 0:
96             return
97
98         LOG.info("Copying %s to destination...", self.RESOURCE_WORD)
99         self.ssh_helper.run("sudo mkdir -p '{}'".format(os.path.dirname(trex_path)))
100         self.ssh_helper.put("~/.bash_profile", "~/.bash_profile")
101         self.ssh_helper.put(trex_path, trex_path, True)
102         ko_src = os.path.join(trex_path, "scripts/ko/src/")
103         self.ssh_helper.execute(self.MAKE_INSTALL.format(ko_src))
104
105     def start(self, ports=None, *args, **kwargs):
106         cmd = "sudo fuser -n tcp {0.SYNC_PORT} {0.ASYNC_PORT} -k > /dev/null 2>&1"
107         self.ssh_helper.execute(cmd.format(self))
108
109         self.ssh_helper.execute("sudo pkill -9 rex > /dev/null 2>&1")
110
111         # We MUST default to 1 because TRex won't work on single-queue devices with
112         # more than one core per port
113         # We really should be trying to find the number of queues in the driver,
114         # but there doesn't seem to be a way to do this
115         # TRex Error: the number of cores should be 1 when the driver
116         # support only one tx queue and one rx queue. Please use -c 1
117         threads_per_port = try_int(self.scenario_helper.options.get("queues_per_port"), 1)
118
119         trex_path = self.ssh_helper.join_bin_path("trex", "scripts")
120         path = get_nsb_option("trex_path", trex_path)
121
122         cmd = "./t-rex-64 --no-scapy-server -i -c {} --cfg '{}'".format(threads_per_port,
123                                                                         self.CONF_FILE)
124
125         if self.scenario_helper.options.get("trex_server_debug"):
126             # if there are errors we want to see them
127             redir = ""
128         else:
129             redir = ">/dev/null"
130         # we have to sudo cd because the path might be owned by root
131         trex_cmd = """sudo bash -c "cd '{}' ; {}" {}""".format(path, cmd, redir)
132         LOG.debug(trex_cmd)
133         self.ssh_helper.execute(trex_cmd)
134
135     def terminate(self):
136         super(TrexResourceHelper, self).terminate()
137         cmd = "sudo fuser -n tcp %s %s -k > /dev/null 2>&1"
138         self.ssh_helper.execute(cmd % (self.SYNC_PORT, self.ASYNC_PORT))
139
140
141 class TrexTrafficGen(SampleVNFTrafficGen):
142     """
143     This class handles mapping traffic profile and generating
144     traffic for given testcase
145     """
146
147     APP_NAME = 'TRex'
148
149     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
150         if resource_helper_type is None:
151             resource_helper_type = TrexResourceHelper
152
153         if setup_env_helper_type is None:
154             setup_env_helper_type = TrexDpdkVnfSetupEnvHelper
155
156         super(TrexTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
157                                              resource_helper_type)
158
159     def _check_status(self):
160         return self.resource_helper.check_status()
161
162     def _start_server(self):
163         super(TrexTrafficGen, self)._start_server()
164         self.resource_helper.start()
165
166     def scale(self, flavor=""):
167         pass
168
169     def listen_traffic(self, traffic_profile):
170         pass
171
172     def terminate(self):
173         self.resource_helper.terminate()