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, traffic_profile, filename='/etc/trex_cfg.yaml'):
39 Runs TRex server for specified traffic profile.
41 :param traffic_profile: traffic profile object based on config file
42 :param filename: path where to save TRex config file
44 cfg = self.__save_config(traffic_profile, filename)
45 cores = traffic_profile.cores
46 subprocess.Popen(['nohup', '/bin/bash', '-c',
47 './t-rex-64 -i -c {} --iom 0 --no-scapy-server --close-at-end --vlan'
48 ' --cfg {} &> /tmp/trex.log & disown'.format(cores, cfg)],
50 LOG.info('TRex server is running...')
52 def __save_config(self, traffic_profile, filename):
53 ifs = ",".join([repr(pci) for pci in traffic_profile.pcis])
55 result = """# Config generated by NFVBench tool
58 interfaces : [{ifs}]""".format(ifs=ifs)
60 yaml.safe_load(result)
61 if os.path.exists(filename):
63 with open(filename, 'w') as f: