NFVBENCH-190: Add a 'i40e_mixed' option, trex accepts other i40e driven ports to...
[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 __apply_trex_patches(self):
38         parent_dir = os.path.dirname(os.path.realpath(__file__))
39         patches_dir = os.path.join(parent_dir, "trex_patches")
40         patches = os.listdir(patches_dir)
41         for patch in patches:
42             patch = os.path.join(patches_dir, patch)
43             command = (
44                 "patch --directory=" + self.trex_dir + " --strip=0"
45                 " --forward --no-backup-if-mismatch --reject-file=-"
46                 " --force --input=" + patch + " >&-")
47             os.system(command)
48
49     def run_server(self, generator_config, filename='/etc/trex_cfg.yaml'):
50         """Run TRex server for specified traffic profile.
51
52         :param traffic_profile: traffic profile object based on config file
53         :param filename: path where to save TRex config file
54         """
55         # in order to allow for customized behaviors, let's apply some patches
56         # this scheme keeps acceptable since we have only simple modifications
57         self.__apply_trex_patches()
58         cfg = self.__save_config(generator_config, filename)
59         cores = generator_config.cores
60         vtep_vlan = generator_config.gen_config.get('vtep_vlan')
61         sw_mode = "--software" if generator_config.software_mode else ""
62         vlan_opt = "--vlan" if (generator_config.vlan_tagging or vtep_vlan) else ""
63         if generator_config.mbuf_factor:
64             mbuf_opt = "--mbuf-factor " + str(generator_config.mbuf_factor)
65         else:
66             mbuf_opt = ""
67         hdrh_opt = "--hdrh" if generator_config.hdrh else ""
68         # --unbind-unused-ports: for NIC that have more than 2 ports such as Intel X710
69         # this will instruct trex to unbind all ports that are unused instead of
70         # erroring out with an exception (i40e only)
71         # Try: --ignore-528-issue -> neither unbind nor exit with error,
72         #                            just proceed cause it might work!
73         # Note that force unbinding is probably a bad choice:
74         # we can't assume for sure that other ports are "unused".
75         # The default TRex behaviour - exit - is indeed a safer option;
76         # a message informs about the ports that should be unbound.
77         i40e_opt = ("--ignore-528-issue" if
78                     generator_config.config.i40e_mixed == 'ignore' else
79                     "--unbind-unused-ports" if
80                     generator_config.config.i40e_mixed == 'unbind' else "")
81         cmd = ['nohup', '/bin/bash', '-c',
82                './t-rex-64 -i -c {} --iom 0 --no-scapy-server '
83                '--close-at-end {} {} {} '
84                '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
85                                                                  i40e_opt,
86                                                                  vlan_opt,
87                                                                  hdrh_opt,
88                                                                  mbuf_opt, cfg)]
89         LOG.info(' '.join(cmd))
90         subprocess.Popen(cmd, cwd=self.trex_dir)
91         LOG.info('TRex server is running...')
92
93     def __load_config(self, filename):
94         result = {}
95         if os.path.exists(filename):
96             with open(filename, 'r') as stream:
97                 try:
98                     result = yaml.safe_load(stream)
99                 except yaml.YAMLError as exc:
100                     print(exc)
101         return result
102
103     def __save_config(self, generator_config, filename):
104         result = self.__prepare_config(generator_config)
105         yaml.safe_load(result)
106         if os.path.exists(filename):
107             os.remove(filename)
108         with open(filename, 'w') as f:
109             f.write(result)
110         return filename
111
112     def __prepare_config(self, generator_config):
113         ifs = ",".join([repr(pci) for pci in generator_config.pcis])
114
115         # For consistency and stability reasons, the T-Rex server
116         # should be forciby restarted each time the value of a
117         # parameter, specified as one of the starting command line
118         # arguments, has been modified since the last launch.
119         # Hence we add some extra fields to the config file
120         # (nb_cores, use_vlan, mbuf_factor, i40e_mixed, hdrh)
121         # which will serve as a memory between runs -
122         # while being actually ignored by the T-Rex server.
123
124         result = """# Config generated by NFVbench
125         - port_limit   : 2
126           version      : 2
127           zmq_pub_port : {zmq_pub_port}
128           zmq_rpc_port : {zmq_rpc_port}
129           prefix       : {prefix}
130           limit_memory : {limit_memory}
131           command_line :
132             sw_mode    : {sw_mode}
133             mbuf_factor: {mbuf_factor}
134             hdrh       : {hdrh}
135             nb_cores   : {nb_cores}
136             use_vlan   : {use_vlan}
137             i40e_mixed : {i40e_mixed}
138           interfaces   : [{ifs}]""".format(
139             zmq_pub_port=generator_config.zmq_pub_port,
140             zmq_rpc_port=generator_config.zmq_rpc_port,
141             prefix=generator_config.name,
142             limit_memory=generator_config.limit_memory,
143             sw_mode=generator_config.software_mode,
144             mbuf_factor=generator_config.mbuf_factor,
145             hdrh=generator_config.hdrh,
146             nb_cores=generator_config.cores,
147             use_vlan=generator_config.gen_config.get('vtep_vlan') or
148             generator_config.vlan_tagging,
149             i40e_mixed=generator_config.config.i40e_mixed,
150             ifs=ifs)
151
152         if hasattr(generator_config, 'mbuf_64') and generator_config.mbuf_64:
153             result += """
154           memory       :
155             mbuf_64           : {mbuf_64}""".format(mbuf_64=generator_config.mbuf_64)
156
157         if self.__check_platform_config(generator_config):
158             try:
159                 platform = """
160           platform     :
161             master_thread_id  : {master_thread_id}
162             latency_thread_id : {latency_thread_id}
163             dual_if:""".format(master_thread_id=generator_config.gen_config.platform.
164                                master_thread_id,
165                                latency_thread_id=generator_config.gen_config.platform.
166                                latency_thread_id)
167                 result += platform
168
169                 for core in generator_config.gen_config.platform.dual_if:
170                     threads = ""
171                     try:
172                         threads = ",".join([repr(thread) for thread in core.threads])
173                     except TypeError:
174                         LOG.warning("No threads defined for socket %s", core.socket)
175                     core_result = """
176                   - socket : {socket}
177                     threads : [{threads}]""".format(socket=core.socket, threads=threads)
178                     result += core_result
179             except (KeyError, AttributeError):
180                 pass
181         return result + "\n"
182
183     def __check_platform_config(self, generator_config):
184         return hasattr(generator_config.gen_config, 'platform') \
185             and hasattr(generator_config.gen_config.platform, "master_thread_id") \
186             and generator_config.gen_config.platform.master_thread_id is not None \
187             and hasattr(generator_config.gen_config.platform, "latency_thread_id") \
188             and generator_config.gen_config.platform.latency_thread_id is not None
189
190     def check_config_updated(self, generator_config):
191         existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')
192         new_config = yaml.safe_load(self.__prepare_config(generator_config))
193         LOG.debug("Existing config: %s", existing_config)
194         LOG.debug("New config: %s", new_config)
195         if existing_config == new_config:
196             return False
197         return True