vnfs: Enable PVP using vhost-cuse
[vswitchperf.git] / core / traffic_controller_rfc2544.py
1 # Copyright 2015 Intel Corporation.
2 #
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
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,
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.
14 """RFC2544 Traffic Controller implementation.
15 """
16 import logging
17
18 from core.traffic_controller import ITrafficController
19 from core.results.results_constants import ResultsConstants
20 from core.results.results import IResults
21 from conf import settings
22 from conf import get_test_param
23
24
25 class TrafficControllerRFC2544(ITrafficController, IResults):
26     """Traffic controller for RFC2544 traffic
27
28     Used to setup and control a traffic generator for an RFC2544 deployment
29     traffic scenario.
30     """
31
32     def __init__(self, traffic_gen_class):
33         """Initialise the trafficgen and store.
34
35         :param traffic_gen_class: The traffic generator class to be used.
36         """
37         self._logger = logging.getLogger(__name__)
38         self._logger.debug("__init__")
39         self._traffic_gen_class = traffic_gen_class()
40         self._traffic_started = False
41         self._traffic_started_call_count = 0
42         self._packet_sizes = settings.getValue('TRAFFICGEN_PKT_SIZES')
43         self._trials = get_test_param('rfc2544_trials', 1)
44         self._results = []
45
46     def __enter__(self):
47         """Call initialisation function.
48         """
49         self._traffic_gen_class.connect()
50
51     def __exit__(self, type_, value, traceback):
52         """Stop traffic, clean up.
53         """
54         if self._traffic_started:
55             self.stop_traffic()
56
57     @staticmethod
58     def _append_results(result_dict, packet_size):
59         """Adds common values to traffic generator results.
60
61         :param result_dict: Dictionary containing results from trafficgen
62         :param packet_size: Packet size value.
63
64         :returns: dictionary of results with addictional entries.
65         """
66
67         ret_value = result_dict
68
69         #TODO Old TOIT controller had knowledge about scenario beeing
70         #executed, should new controller also fill Configuration & ID,
71         # or this should be passed to TestCase?
72         ret_value[ResultsConstants.TYPE] = 'rfc2544'
73         ret_value[ResultsConstants.PACKET_SIZE] = str(packet_size)
74
75         return ret_value
76
77     def send_traffic(self, traffic):
78         """See ITrafficController for description
79         """
80         self._logger.debug('send_traffic with ' +
81                            str(self._traffic_gen_class))
82
83         for packet_size in self._packet_sizes:
84             traffic['l2'] = {'framesize': packet_size}
85             if traffic['traffic_type'] == 'back2back':
86                 result = self._traffic_gen_class.send_rfc2544_back2back(
87                     traffic, trials=int(self._trials),
88                     duration=int(get_test_param('rfc2544_duration', 20)))
89             elif traffic['traffic_type'] == 'continuous':
90                 result = self._traffic_gen_class.send_cont_traffic(
91                     traffic, time=int(get_test_param('rfc2544_duration', 30)))
92             else:
93                 result = self._traffic_gen_class.send_rfc2544_throughput(
94                     traffic, trials=int(self._trials),
95                     duration=int(get_test_param('rfc2544_duration', 20)))
96
97             result = TrafficControllerRFC2544._append_results(result,
98                                                               packet_size)
99             self._results.append(result)
100
101     def send_traffic_async(self, traffic, function):
102         """See ITrafficController for description
103         """
104         self._logger.debug('send_traffic_async with ' +
105                            str(self._traffic_gen_class))
106
107         for packet_size in self._packet_sizes:
108             traffic['l2'] = {'framesize': packet_size}
109             self._traffic_gen_class.start_rfc2544_throughput(
110                 traffic,
111                 trials=int(self._trials),
112                 duration=int(get_test_param('rfc2544_duration', 20)))
113             self._traffic_started = True
114             if len(function['args']) > 0:
115                 function['function'](function['args'])
116             else:
117                 function['function']()
118             result = self._traffic_gen_class.wait_rfc2544_throughput()
119             result = TrafficControllerRFC2544._append_results(result,
120                                                               packet_size)
121             self._results.append(result)
122
123     def stop_traffic(self):
124         """Kills traffic being sent from the traffic generator.
125         """
126         self._logger.debug("stop_traffic()")
127
128     def print_results(self):
129         """IResult interface implementation.
130         """
131         counter = 0
132         for item in self._results:
133             logging.info("Record: " + str(counter))
134             counter += 1
135             for(key, value) in list(item.items()):
136                 logging.info("         Key: " + str(key) +
137                              ", Value: " + str(value))
138
139
140     def get_results(self):
141         """IResult interface implementation.
142         """
143         return self._results