NFVBENCH-204 Remove some piece of code depending on a non merged patch (NFVBENCH...
[nfvbench.git] / nfvbench / traffic_server.py
1 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    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, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import os
16 import subprocess
17 import yaml
18
19 from .log import LOG
20
21
22 class TrafficServerException(Exception):
23     pass
24
25 class TrafficServer(object):
26     """Base class for traffic servers."""
27
28 class TRexTrafficServer(TrafficServer):
29     """Creates configuration file for TRex and runs server."""
30
31     def __init__(self, trex_base_dir='/opt/trex'):
32         contents = os.listdir(trex_base_dir)
33         # only one version of TRex should be supported in container
34         assert len(contents) == 1
35         self.trex_dir = os.path.join(trex_base_dir, contents[0])
36
37     def run_server(self, generator_config, filename='/etc/trex_cfg.yaml'):
38         """Run TRex server for specified traffic profile.
39
40         :param traffic_profile: traffic profile object based on config file
41         :param filename: path where to save TRex config file
42         """
43         cfg = self.__save_config(generator_config, filename)
44         cores = generator_config.cores
45         vtep_vlan = generator_config.gen_config.get('vtep_vlan')
46         sw_mode = "--software" if generator_config.software_mode else ""
47         vlan_opt = "--vlan" if (generator_config.vlan_tagging or vtep_vlan) else ""
48         if generator_config.mbuf_factor:
49             mbuf_opt = "--mbuf-factor " + str(generator_config.mbuf_factor)
50         else:
51             mbuf_opt = ""
52         hdrh_opt = "--hdrh" if generator_config.hdrh else ""
53         # --unbind-unused-ports: for NIC that have more than 2 ports such as Intel X710
54         # this will instruct trex to unbind all ports that are unused instead of
55         # erroring out with an exception (i40e only)
56         cmd = ['nohup', '/bin/bash', '-c',
57                './t-rex-64 -i -c {} --iom 0 --no-scapy-server '
58                '--unbind-unused-ports --close-at-end {} {} '
59                '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
60                                                                  vlan_opt,
61                                                                  hdrh_opt,
62                                                                  mbuf_opt, cfg)]
63         LOG.info(' '.join(cmd))
64         subprocess.Popen(cmd, cwd=self.trex_dir)
65         LOG.info('TRex server is running...')
66
67     def __load_config(self, filename):
68         result = {}
69         if os.path.exists(filename):
70             with open(filename, 'r') as stream:
71                 try:
72                     result = yaml.safe_load(stream)
73                 except yaml.YAMLError as exc:
74                     print(exc)
75         return result
76
77     def __save_config(self, generator_config, filename):
78         result = self.__prepare_config(generator_config)
79         yaml.safe_load(result)
80         if os.path.exists(filename):
81             os.remove(filename)
82         with open(filename, 'w') as f:
83             f.write(result)
84         return filename
85
86     def __prepare_config(self, generator_config):
87         ifs = ",".join([repr(pci) for pci in generator_config.pcis])
88
89         # For consistency and stability reasons, the T-Rex server
90         # should be forciby restarted each time the value of a
91         # parameter, specified as one of the starting command line
92         # arguments, has been modified since the last launch.
93         # Hence we add some extra fields to the config file
94         # (nb_cores, use_vlan, mbuf_factor, hdrh)
95         # which will serve as a memory between runs -
96         # while being actually ignored by the T-Rex server.
97
98         result = """# Config generated by NFVbench
99         - port_limit   : 2
100           version      : 2
101           zmq_pub_port : {zmq_pub_port}
102           zmq_rpc_port : {zmq_rpc_port}
103           prefix       : {prefix}
104           limit_memory : {limit_memory}
105           command_line :
106             sw_mode    : {sw_mode}
107             mbuf_factor: {mbuf_factor}
108             hdrh       : {hdrh}
109             nb_cores   : {nb_cores}
110             use_vlan   : {use_vlan}
111           interfaces   : [{ifs}]""".format(
112             zmq_pub_port=generator_config.zmq_pub_port,
113             zmq_rpc_port=generator_config.zmq_rpc_port,
114             prefix=generator_config.name,
115             limit_memory=generator_config.limit_memory,
116             sw_mode=generator_config.software_mode,
117             mbuf_factor=generator_config.mbuf_factor,
118             hdrh=generator_config.hdrh,
119             nb_cores=generator_config.cores,
120             use_vlan=generator_config.gen_config.get('vtep_vlan') or
121             generator_config.vlan_tagging,
122             ifs=ifs)
123
124         if hasattr(generator_config, 'mbuf_64') and generator_config.mbuf_64:
125             result += """
126           memory       :
127             mbuf_64           : {mbuf_64}""".format(mbuf_64=generator_config.mbuf_64)
128
129         if self.__check_platform_config(generator_config):
130             try:
131                 platform = """
132           platform     :
133             master_thread_id  : {master_thread_id}
134             latency_thread_id : {latency_thread_id}
135             dual_if:""".format(master_thread_id=generator_config.gen_config.platform.
136                                master_thread_id,
137                                latency_thread_id=generator_config.gen_config.platform.
138                                latency_thread_id)
139                 result += platform
140
141                 for core in generator_config.gen_config.platform.dual_if:
142                     threads = ""
143                     try:
144                         threads = ",".join([repr(thread) for thread in core.threads])
145                     except TypeError:
146                         LOG.warning("No threads defined for socket %s", core.socket)
147                     core_result = """
148                   - socket : {socket}
149                     threads : [{threads}]""".format(socket=core.socket, threads=threads)
150                     result += core_result
151             except (KeyError, AttributeError):
152                 pass
153         return result + "\n"
154
155     def __check_platform_config(self, generator_config):
156         return hasattr(generator_config.gen_config, 'platform') \
157             and hasattr(generator_config.gen_config.platform, "master_thread_id") \
158             and generator_config.gen_config.platform.master_thread_id is not None \
159             and hasattr(generator_config.gen_config.platform, "latency_thread_id") \
160             and generator_config.gen_config.platform.latency_thread_id is not None
161
162     def check_config_updated(self, generator_config):
163         existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')
164         new_config = yaml.safe_load(self.__prepare_config(generator_config))
165         LOG.debug("Existing config: %s", existing_config)
166         LOG.debug("New config: %s", new_config)
167         if existing_config == new_config:
168             return False
169         return True