Add support for openSUSE Leap 42.2
[vswitchperf.git] / tools / pkt_gen / dummy / dummy.py
1 # Copyright 2015-2017 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
15 """
16 Dummy traffic generator, designed for user intput.
17
18 Provides a model for the Dummy traffic generator - a psuedo "traffic
19 generator" that doesn't actually generate any traffic. Instead the
20 user is required to send traffic using their own choice of traffic
21 generator *outside of the framework*. The Dummy traffic generator
22 then returns the results - manually entered by the user - as its
23 own.
24 """
25
26 import json
27
28 from conf import settings
29 from conf import merge_spec
30 from tools.pkt_gen import trafficgen
31 from core.results.results_constants import ResultsConstants
32
33 def _get_user_traffic_stat(stat_type):
34     """
35     Request user input for traffic.
36
37     :param stat_type: Name of statistic required from user
38
39     :returns: Value of stat provided by user
40     """
41     true_vals = ('yes', 'y', 'ye', None)
42     false_vals = ('no', 'n')
43
44     while True:
45         result = input('What was the result for \'%s\'? ' % stat_type)
46
47         try:
48             result = int(result)
49         except ValueError:
50             print('That was not a valid integer result. Try again.')
51             continue
52
53         while True:
54             choice = input('Is \'%d\' correct? ' % result).lower()
55             if not choice or choice in true_vals:
56                 return result
57             elif choice and choice in false_vals:
58                 break
59             else:
60                 print('Please respond with \'yes\' or \'no\' ', end='')
61
62 def get_user_traffic(traffic_type, traffic_conf, flow_conf, traffic_stats):
63     """
64     Request user input for traffic.
65
66     :param traffic_type: Name of traffic type.
67     :param traffic_conf: Configuration of traffic to be sent.
68     :param traffic_conf: Configuration of flow to be sent.
69     :param traffic_stats: Required output statistics (i.e. what's needed)
70
71     :returns: List of stats corresponding to those in traffic_stats
72     """
73     results = []
74
75     print('Please send \'%s\' traffic with the following stream config:\n%s\n'
76           'and the following flow config:\n%s'
77           % (traffic_type, traffic_conf, json.dumps(flow_conf, indent=4)))
78
79     for stat in traffic_stats:
80         if stat in settings.getValue('TRAFFICGEN_DUMMY_RESULTS'):
81             results.append(settings.getValue('TRAFFICGEN_DUMMY_RESULTS')[stat])
82         else:
83             results.append(_get_user_traffic_stat(stat))
84
85     return results
86
87
88 class Dummy(trafficgen.ITrafficGenerator):
89     """
90     A dummy traffic generator whose data is generated by the user.
91
92     This traffic generator is useful when a user does not wish to write
93     a wrapper for a given type of traffic generator. By using this
94     "traffic generator", the user is asked to send traffic when
95     required and enter the results manually. The user controls the
96     real traffic generator and is responsible for ensuring the flows
97     are setup correctly.
98     """
99     def connect(self):
100         """
101         Do nothing.
102         """
103         return self
104
105     def disconnect(self):
106         """
107         Do nothing.
108         """
109         pass
110
111     def send_burst_traffic(self, traffic=None, numpkts=100, duration=20):
112         """
113         Send a burst of traffic.
114         """
115         traffic_ = self.traffic_defaults.copy()
116         result = {}
117
118         if traffic:
119             traffic_ = merge_spec(traffic_, traffic)
120
121         results = get_user_traffic(
122             'burst',
123             '%dpkts, %dmS' % (numpkts, duration),
124             traffic_,
125             ('frames rx', 'payload errors', 'sequence errors'))
126
127         # builds results by using user-supplied values where possible
128         # and guessing remainder using available info
129         result[ResultsConstants.TX_FRAMES] = numpkts
130         result[ResultsConstants.RX_FRAMES] = results[0]
131         result[ResultsConstants.TX_BYTES] = traffic_['l2']['framesize'] \
132                                             * numpkts
133         result[ResultsConstants.RX_BYTES] = traffic_['l2']['framesize'] \
134                                             * results[0]
135         result[ResultsConstants.PAYLOAD_ERR] = results[1]
136         result[ResultsConstants.SEQ_ERR] = results[2]
137
138         return results
139
140     def send_cont_traffic(self, traffic=None, duration=30):
141         """
142         Send a continuous flow of traffic.
143         """
144         traffic_ = self.traffic_defaults.copy()
145         result = {}
146
147         if traffic:
148             traffic_ = merge_spec(traffic_, traffic)
149
150         results = get_user_traffic(
151             'continuous',
152             '%dmpps, multistream %s, duration %d' % (traffic['frame_rate'],
153                                                      traffic['multistream'],
154                                                      duration), traffic_,
155             ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
156              'max latency', 'avg latency', 'frameloss %'))
157
158         framesize = traffic_['l2']['framesize']
159
160         # builds results by using user-supplied values
161         # and guessing remainder using available info
162         result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
163         result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
164         result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
165                                                  * (framesize * 8 / 1000000)
166         result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
167                                                  * (framesize * 8 / 1000000)
168         result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
169         result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
170         result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
171         result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
172         result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
173         result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
174         return result
175
176     def send_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
177                                 lossrate=0.0):
178         """
179         Send traffic per RFC2544 throughput test specifications.
180         """
181         traffic_ = self.traffic_defaults.copy()
182         result = {}
183
184         if traffic:
185             traffic_ = merge_spec(traffic_, traffic)
186
187         results = get_user_traffic(
188             'throughput',
189             '%d tests, %d seconds iterations, %f packet loss, multistream '
190             '%s' % (tests, duration, lossrate, traffic['multistream']),
191             traffic_,
192             ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
193              'max latency', 'avg latency', 'frameloss %'))
194
195         framesize = traffic_['l2']['framesize']
196
197         # builds results by using user-supplied values
198         # and guessing remainder using available info
199         result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
200         result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
201         result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
202                                                  * (framesize * 8 / 1000000)
203         result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
204                                                  * (framesize * 8 / 1000000)
205         result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
206         result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
207         result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
208         result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
209         result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
210         result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
211         return result
212
213     def send_rfc2544_back2back(self, traffic=None, tests=1, duration=2,
214                                lossrate=0.0):
215         """
216         Send traffic per RFC2544 back2back test specifications.
217         """
218         traffic_ = self.traffic_defaults.copy()
219         result = {}
220
221         if traffic:
222             traffic_ = merge_spec(traffic_, traffic)
223
224         results = get_user_traffic(
225             'back2back',
226             '%d tests, %d seconds iterations, %f packet loss, multistream '
227             '%s' % (tests, duration, lossrate, traffic['multistream']),
228             traffic_,
229             ('b2b frames', 'b2b frame loss %'))
230
231         # builds results by using user-supplied values
232         # and guessing remainder using available info
233         result[ResultsConstants.B2B_FRAMES] = float(results[0])
234         result[ResultsConstants.B2B_FRAME_LOSS_PERCENT] = float(results[1])
235         return result
236
237     def start_cont_traffic(self, traffic=None, duration=20):
238         return NotImplementedError('Dummy does not implement start_cont_traffic')
239
240     def stop_cont_traffic(self):
241         return NotImplementedError(
242             'Dummy does not implement stop_cont_traffic')
243
244     def start_rfc2544_back2back(self, traffic=None, tests=1, duration=20,
245                                 lossrate=0.0):
246         return NotImplementedError(
247             'Dummy does not implement start_rfc2544_back2back')
248
249     def wait_rfc2544_back2back(self):
250         return NotImplementedError(
251             'Dummy does not implement stop_cont_traffic')
252
253     def start_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
254                                  lossrate=0.0):
255         return NotImplementedError(
256             'Dummy does not implement start_rfc2544_throughput')
257
258     def wait_rfc2544_throughput(self):
259         return NotImplementedError(
260             'Dummy does not implement wait_rfc2544_throughput')
261
262 if __name__ == '__main__':
263     TRAFFIC = {
264         'l3': {
265             'proto': 'tcp',
266             'srcip': '1.1.1.1',
267             'dstip': '90.90.90.90',
268         },
269     }
270
271     with Dummy() as dev:
272         print(dev.send_burst_traffic(traffic=TRAFFIC))
273         print(dev.send_cont_traffic(traffic=TRAFFIC))
274         print(dev.send_rfc2544_throughput(traffic=TRAFFIC))
275         print(dev.send_rfc2544_back2back(traffic=TRAFFIC))
276         print(dev.send_rfc(traffic=TRAFFIC))