1 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
22 class TrafficServerException(Exception):
25 class TrafficServer(object):
26 """Base class for traffic servers."""
28 class TRexTrafficServer(TrafficServer):
29 """Creates configuration file for TRex and runs server."""
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])
37 def run_server(self, generator_config, filename='/etc/trex_cfg.yaml'):
38 """Run TRex server for specified traffic profile.
40 :param traffic_profile: traffic profile object based on config file
41 :param filename: path where to save TRex config file
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)
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 # Try: --ignore-528-issue -> neither unbind nor exit with error,
57 # just proceed cause it might work!
58 # Note that force unbinding is probably a bad choice:
59 # we can't assume for sure that other ports are "unused".
60 # The default TRex behaviour - exit - is indeed a safer option;
61 # a message informs about the ports that should be unbound.
62 i40e_opt = ("--ignore-528-issue" if
63 generator_config.config.i40e_mixed == 'ignore' else
64 "--unbind-unused-ports" if
65 generator_config.config.i40e_mixed == 'unbind' else "")
66 cmd = ['nohup', '/bin/bash', '-c',
67 './t-rex-64 -i -c {} --iom 0 --no-scapy-server '
68 '--close-at-end {} {} {} '
69 '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
74 LOG.info(' '.join(cmd))
75 with subprocess.Popen(cmd, cwd=self.trex_dir) as trex_process:
76 LOG.info('TRex server is running (PID: %s)...', trex_process.pid)
78 def __load_config(self, filename):
80 if os.path.exists(filename):
81 with open(filename, 'r', encoding="utf-8") as stream:
83 result = yaml.safe_load(stream)
84 except yaml.YAMLError as exc:
88 def __save_config(self, generator_config, filename):
89 result = self.__prepare_config(generator_config)
90 yaml.safe_load(result)
91 if os.path.exists(filename):
93 with open(filename, 'w', encoding="utf-8") as f:
97 def __prepare_config(self, generator_config):
98 ifs = ",".join([repr(pci) for pci in generator_config.pcis])
100 # For consistency and stability reasons, the T-Rex server
101 # should be forciby restarted each time the value of a
102 # parameter, specified as one of the starting command line
103 # arguments, has been modified since the last launch.
104 # Hence we add some extra fields to the config file
105 # (nb_cores, use_vlan, mbuf_factor, i40e_mixed, hdrh)
106 # which will serve as a memory between runs -
107 # while being actually ignored by the T-Rex server.
109 result = """# Config generated by NFVbench
112 zmq_pub_port : {zmq_pub_port}
113 zmq_rpc_port : {zmq_rpc_port}
115 limit_memory : {limit_memory}
118 mbuf_factor: {mbuf_factor}
120 nb_cores : {nb_cores}
121 use_vlan : {use_vlan}
122 i40e_mixed : {i40e_mixed}
123 interfaces : [{ifs}]""".format(
124 zmq_pub_port=generator_config.zmq_pub_port,
125 zmq_rpc_port=generator_config.zmq_rpc_port,
126 prefix=generator_config.name,
127 limit_memory=generator_config.limit_memory,
128 sw_mode=generator_config.software_mode,
129 mbuf_factor=generator_config.mbuf_factor,
130 hdrh=generator_config.hdrh,
131 nb_cores=generator_config.cores,
132 use_vlan=generator_config.gen_config.get('vtep_vlan') or
133 generator_config.vlan_tagging,
134 i40e_mixed=generator_config.config.i40e_mixed,
137 if hasattr(generator_config, 'mbuf_64') and generator_config.mbuf_64:
140 mbuf_64 : {mbuf_64}""".format(mbuf_64=generator_config.mbuf_64)
142 if self.__check_platform_config(generator_config):
146 master_thread_id : {master_thread_id}
147 latency_thread_id : {latency_thread_id}
148 dual_if:""".format(master_thread_id=generator_config.gen_config.platform.
150 latency_thread_id=generator_config.gen_config.platform.
154 for core in generator_config.gen_config.platform.dual_if:
157 threads = ",".join([repr(thread) for thread in core.threads])
159 LOG.warning("No threads defined for socket %s", core.socket)
162 threads : [{threads}]""".format(socket=core.socket, threads=threads)
163 result += core_result
164 except (KeyError, AttributeError):
168 def __check_platform_config(self, generator_config):
169 return hasattr(generator_config.gen_config, 'platform') \
170 and hasattr(generator_config.gen_config.platform, "master_thread_id") \
171 and generator_config.gen_config.platform.master_thread_id is not None \
172 and hasattr(generator_config.gen_config.platform, "latency_thread_id") \
173 and generator_config.gen_config.platform.latency_thread_id is not None
175 def check_config_updated(self, generator_config):
176 existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')
177 new_config = yaml.safe_load(self.__prepare_config(generator_config))
178 LOG.debug("Existing config: %s", existing_config)
179 LOG.debug("New config: %s", new_config)
180 if existing_config == new_config: