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 subprocess.Popen(['nohup', '/bin/bash', '-c',
53 './t-rex-64 -i -c {} --iom 0 --no-scapy-server --close-at-end {} '
54 '{} {} --cfg {} &> /tmp/trex.log & disown'.format(cores, sw_mode,
58 LOG.info('TRex server is running...')
60 def __load_config(self, filename):
62 if os.path.exists(filename):
63 with open(filename, 'r') as stream:
65 result = yaml.load(stream)
66 except yaml.YAMLError as exc:
70 def __save_config(self, generator_config, filename):
71 result = self.__prepare_config(generator_config)
72 yaml.safe_load(result)
73 if os.path.exists(filename):
75 with open(filename, 'w') as f:
79 def __prepare_config(self, generator_config):
80 ifs = ",".join([repr(pci) for pci in generator_config.pcis])
81 result = """# Config generated by NFVbench
84 zmq_pub_port : {zmq_pub_port}
85 zmq_rpc_port : {zmq_rpc_port}
87 limit_memory : {limit_memory}
88 interfaces : [{ifs}]""".format(zmq_pub_port=generator_config.zmq_pub_port,
89 zmq_rpc_port=generator_config.zmq_rpc_port,
90 prefix=generator_config.name,
91 limit_memory=generator_config.limit_memory,
93 if hasattr(generator_config, 'platform'):
94 if generator_config.platform.master_thread_id \
95 and generator_config.platform.latency_thread_id:
98 master_thread_id : {master_thread_id}
99 latency_thread_id : {latency_thread_id}
100 dual_if:""".format(master_thread_id=generator_config.platform.master_thread_id,
101 latency_thread_id=generator_config.platform.latency_thread_id)
104 for core in generator_config.platform.dual_if:
107 threads = ",".join([repr(thread) for thread in core.threads])
109 LOG.warn("No threads defined for socket %s", core.socket)
112 threads : [{threads}]""".format(socket=core.socket, threads=threads)
113 result += core_result
115 LOG.info("Generator profile 'platform' sub-properties are set but not filled in \
116 config file. TRex will use default values.")
119 def check_config_updated(self, generator_config):
120 existing_config = self.__load_config(filename='/etc/trex_cfg.yaml')
121 new_config = yaml.safe_load(self.__prepare_config(generator_config))
122 LOG.debug("Existing config: %s", existing_config)
123 LOG.debug("New config: %s", new_config)
124 if existing_config == new_config: