1 # Copyright (c) 2016-2019 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from yardstick.common import utils
20 from yardstick.network_services.vnf_generic.vnf import sample_vnf
21 from yardstick.network_services.vnf_generic.vnf import tg_trex
22 from trex_stl_lib.trex_stl_exceptions import STLError
25 LOG = logging.getLogger(__name__)
28 class TrexRfcResourceHelper(tg_trex.TrexResourceHelper):
33 def __init__(self, setup_helper):
34 super(TrexRfcResourceHelper, self).__init__(setup_helper)
35 self.rfc2544_helper = sample_vnf.Rfc2544ResourceHelper(
38 def _run_traffic_once(self, traffic_profile):
39 self.client_started.value = 1
40 ports, port_pg_id = traffic_profile.execute_traffic(self)
43 timeout = int(traffic_profile.config.duration) - self.TRANSIENT_PERIOD
44 time.sleep(self.TRANSIENT_PERIOD)
45 for _ in utils.Timer(timeout=timeout):
46 samples.append(self._get_samples(ports, port_pg_id=port_pg_id))
47 time.sleep(self.SAMPLING_PERIOD)
49 traffic_profile.stop_traffic(self)
50 completed, output = traffic_profile.get_drop_percentage(
51 samples, self.rfc2544_helper.tolerance_low,
52 self.rfc2544_helper.tolerance_high,
53 self.rfc2544_helper.correlated_traffic,
54 self.rfc2544_helper.resolution)
55 self._queue.put(output)
58 def start_client(self, ports, mult=None, duration=None, force=True):
59 self.client.start(ports=ports, mult=mult, duration=duration, force=force)
61 def clear_client_stats(self, ports):
62 self.client.clear_stats(ports=ports)
64 def run_test(self, traffic_profile, tasks_queue, results_queue, *args): # pragma: no cover
65 LOG.debug("Trex resource_helper run_test")
66 if self._terminated.value:
68 # if we don't do this we can hang waiting for the queue to drain
69 # have to do this in the subprocess
70 self._queue.cancel_join_thread()
73 self.client = self._connect()
74 self.client.reset(ports=self.all_ports)
75 self.client.remove_all_streams(self.all_ports) # remove all streams
76 traffic_profile.register_generator(self)
79 self.rfc2544_helper.iteration.value = 0
80 self.client_started.value = 1
81 while completed is False and not self._terminated.value:
82 LOG.debug("Wait for task ...")
84 task = tasks_queue.get(True, 5)
85 except moves.queue.Empty:
86 LOG.debug("Wait for task timeout, continue waiting...")
89 if task != 'RUN_TRAFFIC':
91 self.rfc2544_helper.iteration.value += 1
92 LOG.info("Got %s task, start iteration %d", task,
93 self.rfc2544_helper.iteration.value)
94 completed = self._run_traffic_once(traffic_profile)
96 LOG.debug("%s::run_test - test completed",
97 self.__class__.__name__)
98 results_queue.put('COMPLETE')
100 results_queue.put('CONTINUE')
101 tasks_queue.task_done()
103 self.client.stop(self.all_ports)
104 self.client.disconnect()
105 self._terminated.value = 0
107 if self._terminated.value:
108 LOG.debug("traffic generator is stopped")
109 return # return if trex/tg server is stopped.
112 self.client_started.value = 0
113 LOG.debug("%s::run_test done", self.__class__.__name__)
115 class TrexTrafficGenRFC(tg_trex.TrexTrafficGen):
117 This class handles mapping traffic profile and generating
118 traffic for rfc2544 testcase.
121 def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
122 if resource_helper_type is None:
123 resource_helper_type = TrexRfcResourceHelper
125 super(TrexTrafficGenRFC, self).__init__(name, vnfd, setup_env_helper_type,
126 resource_helper_type)