267f5d2f0378bafac504fa8d3a5533273f849e81
[csit.git] / resources / libraries / python / TrafficGenerator.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Performance testing traffic generator library."""
15
16 from robot.api import logger
17 from robot.libraries.BuiltIn import BuiltIn
18
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.topology import NodeType
21 from resources.libraries.python.topology import NodeSubTypeTG
22 from resources.libraries.python.topology import Topology
23 from resources.libraries.python.DropRateSearch import DropRateSearch
24
25 __all__ = ['TrafficGenerator', 'TGDropRateSearchImpl']
26
27
28 class TGDropRateSearchImpl(DropRateSearch):
29     """Drop Rate Search implementation."""
30
31     def __init__(self):
32         super(TGDropRateSearchImpl, self).__init__()
33
34     def measure_loss(self, rate, frame_size, loss_acceptance,
35                      loss_acceptance_type, traffic_type):
36
37         # we need instance of TrafficGenerator instantiated by Robot Framework
38         # to be able to use trex_stl-*()
39         tg_instance = BuiltIn().get_library_instance(
40             'resources.libraries.python.TrafficGenerator')
41
42         if tg_instance._node['subtype'] is None:
43             raise Exception('TG subtype not defined')
44         elif tg_instance._node['subtype'] == NodeSubTypeTG.TREX:
45             unit_rate = str(rate) + self.get_rate_type_str()
46             tg_instance.trex_stl_start_remote_exec(self.get_duration(),
47                                                    unit_rate, frame_size,
48                                                    traffic_type, False)
49             loss = tg_instance.get_loss()
50             sent = tg_instance.get_sent()
51             if self.loss_acceptance_type_is_percentage():
52                 loss = (float(loss) / float(sent)) * 100
53
54             # TODO: getters for tg_instance
55             logger.trace("comparing: {} < {} {}".format(loss,
56                                                         loss_acceptance,
57                                                         loss_acceptance_type))
58             if float(loss) > float(loss_acceptance):
59                 return False
60             else:
61                 return True
62         else:
63             raise NotImplementedError("TG subtype not supported")
64
65
66 class TrafficGenerator(object):
67     """Traffic Generator."""
68
69     # use one instance of TrafficGenerator for all tests in test suite
70     ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
71
72     def __init__(self):
73         self._result = None
74         self._loss = None
75         self._sent = None
76         self._received = None
77         self._node = None
78         # T-REX interface order mapping
79         self._ifaces_reordered = 0
80
81     def get_loss(self):
82         """Return number of lost packets.
83
84         :return: Number of lost packets.
85         :rtype: str
86         """
87         return self._loss
88
89     def get_sent(self):
90         """Return number of sent packets.
91
92         :return: Number of sent packets.
93         :rtype: str
94         """
95         return self._sent
96
97     def get_received(self):
98         """Return number of received packets.
99
100         :return: Number of received packets.
101         :rtype: str
102         """
103         return self._received
104
105     #pylint: disable=too-many-arguments, too-many-locals
106     def initialize_traffic_generator(self, tg_node, tg_if1, tg_if2,
107                                      dut1_node, dut1_if1, dut1_if2,
108                                      dut2_node, dut2_if1, dut2_if2,
109                                      test_type):
110         """TG initialization.
111
112         :param tg_node: Traffic generator node.
113         :param tg_if1: TG - name of first interface.
114         :param tg_if2: TG - name of second interface.
115         :param dut1_node: DUT1 node.
116         :param dut1_if1: DUT1 - name of first interface.
117         :param dut1_if2: DUT1 - name of second interface.
118         :param dut2_node: DUT2 node.
119         :param dut2_if1: DUT2 - name of first interface.
120         :param dut2_if2: DUT2 - name of second interface.
121         :test_type: 'L2' or 'L3' - src/dst MAC address.
122         :type tg_node: dict
123         :type tg_if1: str
124         :type tg_if2: str
125         :type dut1_node: dict
126         :type dut1_if1: str
127         :type dut1_if2: str
128         :type dut2_node: dict
129         :type dut2_if1: str
130         :type dut2_if2: str
131         :type test_type: str
132         :return: nothing
133         """
134
135         trex_path = "/opt/trex-core-2.02"
136
137         topo = Topology()
138
139         if tg_node['type'] != NodeType.TG:
140             raise Exception('Node type is not a TG')
141         self._node = tg_node
142
143         if tg_node['subtype'] == NodeSubTypeTG.TREX:
144             ssh = SSH()
145             ssh.connect(tg_node)
146
147             if1_pci = topo.get_interface_pci_addr(tg_node, tg_if1)
148             if2_pci = topo.get_interface_pci_addr(tg_node, tg_if2)
149             if1_mac = topo.get_interface_mac(tg_node, tg_if1)
150             if2_mac = topo.get_interface_mac(tg_node, tg_if2)
151
152             if test_type == 'L2':
153                 if1_adj_mac = if2_mac
154                 if2_adj_mac = if1_mac
155             elif test_type == 'L3':
156                 if1_adj_mac = topo.get_interface_mac(dut1_node, dut1_if1)
157                 if2_adj_mac = topo.get_interface_mac(dut2_node, dut2_if2)
158             else:
159                 raise Exception("test_type unknown")
160
161             if min(if1_pci, if2_pci) != if1_pci:
162                 if1_mac, if2_mac = if2_mac, if1_mac
163                 if1_pci, if2_pci = if2_pci, if1_pci
164                 if1_adj_mac, if2_adj_mac = if2_adj_mac, if1_adj_mac
165                 self._ifaces_reordered = 1
166
167             if1_mac_hex = "0x"+if1_mac.replace(":", ",0x")
168             if2_mac_hex = "0x"+if2_mac.replace(":", ",0x")
169             if1_adj_mac_hex = "0x"+if1_adj_mac.replace(":", ",0x")
170             if2_adj_mac_hex = "0x"+if2_adj_mac.replace(":", ",0x")
171
172             (ret, stdout, stderr) = ssh.exec_command(
173                 "sudo sh -c 'cat << EOF > /etc/trex_cfg.yaml\n"
174                 "- port_limit      : 2\n"
175                 "  version         : 2\n"
176                 "  interfaces      : [\"{}\",\"{}\"]\n"
177                 "  port_bandwidth_gb : 10\n"
178                 "  port_info       :\n"
179                 "          - dest_mac        :   [{}]\n"
180                 "            src_mac         :   [{}]\n"
181                 "          - dest_mac        :   [{}]\n"
182                 "            src_mac         :   [{}]\n"
183                 "EOF'"\
184                 .format(if1_pci, if2_pci,
185                         if1_adj_mac_hex, if1_mac_hex,
186                         if2_adj_mac_hex, if2_mac_hex))
187             if int(ret) != 0:
188                 logger.error("failed to create t-rex config: {}"\
189                 .format(stdout + stderr))
190                 raise RuntimeError('trex config generation error')
191
192             (ret, stdout, stderr) = ssh.exec_command(
193                 "sh -c 'cd {0}/scripts/ && "
194                 "sudo ./trex-cfg'"\
195                 .format(trex_path))
196             if int(ret) != 0:
197                 logger.error('trex-cfg failed: {0}'.format(stdout + stderr))
198                 raise RuntimeError('trex-cfg failed')
199
200             (ret, _, _) = ssh.exec_command(
201                 "sh -c 'pgrep t-rex && sudo pkill t-rex'")
202
203             (ret, _, _) = ssh.exec_command(
204                 "sh -c 'cd {0}/scripts/ && "
205                 "sudo nohup ./t-rex-64 -i -c 7 --iom 0 > /dev/null 2>&1 &'"
206                 "> /dev/null"\
207                 .format(trex_path))
208             if int(ret) != 0:
209                 raise RuntimeError('t-rex-64 startup failed')
210
211     @staticmethod
212     def teardown_traffic_generator(node):
213         """TG teardown.
214
215         :param node: Traffic generator node.
216         :type node: dict
217         :return: nothing
218         """
219         if node['type'] != NodeType.TG:
220             raise Exception('Node type is not a TG')
221         if node['subtype'] == NodeSubTypeTG.TREX:
222             ssh = SSH()
223             ssh.connect(node)
224             (ret, stdout, stderr) = ssh.exec_command(
225                 "sh -c 'sudo pkill t-rex'")
226             if int(ret) != 0:
227                 logger.error('pkill t-rex failed: {0}'.format(stdout + stderr))
228                 raise RuntimeError('pkill t-rex failed')
229
230     @staticmethod
231     def trex_stl_stop_remote_exec(node):
232         """Execute script on remote node over ssh to stop running traffic.
233
234         :param node: T-REX generator node.
235         :type node: dict
236         :return: Nothing
237         """
238         ssh = SSH()
239         ssh.connect(node)
240
241         (ret, stdout, stderr) = ssh.exec_command(
242             "sh -c '/tmp/openvpp-testing/resources/tools/t-rex/"
243             "t-rex-stateless-stop.py'")
244         logger.trace(ret)
245         logger.trace(stdout)
246         logger.trace(stderr)
247
248         if int(ret) != 0:
249             raise RuntimeError('T-rex stateless runtime error')
250
251     def trex_stl_start_remote_exec(self, duration, rate, framesize,
252                                    traffic_type, async_call, warmup_time=5):
253         """Execute script on remote node over ssh to start traffic.
254
255         :param duration: Time expresed in seconds for how long to send traffic.
256         :param rate: Traffic rate expressed with units (pps, %)
257         :param framesize: L2 frame size to send (without padding and IPG).
258         :param traffic_type: Traffic profile.
259         :param async_call: If enabled then don't wait for all incomming trafic.
260         :param warmup_time: Warmup time period.
261         :type duration: int
262         :type rate: str
263         :type framesize: int
264         :type traffic_type: str
265         :type async_call: bool
266         :type warmup_time: int
267         :return: Nothing
268         """
269         ssh = SSH()
270         ssh.connect(self._node)
271
272         _p0 = 1
273         _p1 = 2
274         _async = ""
275
276         if async_call:
277             _async = "--async"
278         if self._ifaces_reordered != 0:
279             _p0, _p1 = _p1, _p0
280
281         if traffic_type in ["3-node-xconnect", "3-node-bridge"]:
282             (ret, stdout, stderr) = ssh.exec_command(
283                 "sh -c '/tmp/openvpp-testing/resources/tools/t-rex/"
284                 "t-rex-stateless.py "
285                 "--duration={0} -r {1} -s {2} "
286                 "--p{3}_src_start_ip 10.10.10.1 "
287                 "--p{3}_src_end_ip 10.10.10.254 "
288                 "--p{3}_dst_start_ip 20.20.20.1 "
289                 "--p{4}_src_start_ip 20.20.20.1 "
290                 "--p{4}_src_end_ip 20.20.20.254 "
291                 "--p{4}_dst_start_ip 10.10.10.1 "
292                 "{5} --warmup_time={6}'".format(duration, rate, framesize, _p0,
293                                                 _p1, _async, warmup_time),
294                 timeout=int(duration)+60)
295         elif traffic_type in ["3-node-IPv4"]:
296             (ret, stdout, stderr) = ssh.exec_command(
297                 "sh -c '/tmp/openvpp-testing/resources/tools/t-rex/"
298                 "t-rex-stateless.py "
299                 "--duration={0} -r {1} -s {2} "
300                 "--p{3}_src_start_ip 10.10.10.2 "
301                 "--p{3}_src_end_ip 10.10.10.254 "
302                 "--p{3}_dst_start_ip 20.20.20.2 "
303                 "--p{4}_src_start_ip 20.20.20.2 "
304                 "--p{4}_src_end_ip 20.20.20.254 "
305                 "--p{4}_dst_start_ip 10.10.10.2 "
306                 "{5} --warmup_time={6}'".format(duration, rate, framesize, _p0,
307                                                 _p1, _async, warmup_time),
308                 timeout=int(duration)+60)
309         elif traffic_type in ["3-node-IPv6"]:
310             (ret, stdout, stderr) = ssh.exec_command(
311                 "sh -c '/tmp/openvpp-testing/resources/tools/t-rex/"
312                 "t-rex-stateless.py "
313                 "--duration={0} -r {1} -s {2} -6 "
314                 "--p{3}_src_start_ip 2001:1::2 "
315                 "--p{3}_src_end_ip 2001:1::FE "
316                 "--p{3}_dst_start_ip 2001:2::2 "
317                 "--p{4}_src_start_ip 2001:2::2 "
318                 "--p{4}_src_end_ip 2001:2::FE "
319                 "--p{4}_dst_start_ip 2001:1::2 "
320                 "{5} --warmup_time={6}'".format(duration, rate, framesize, _p0,
321                                                 _p1, _async, warmup_time),
322                 timeout=int(duration)+60)
323         else:
324             raise NotImplementedError('Unsupported traffic type')
325
326         logger.trace(ret)
327         logger.trace(stdout)
328         logger.trace(stderr)
329
330         if int(ret) != 0:
331             raise RuntimeError('T-rex stateless runtime error')
332         elif async_call:
333             #no result
334             self._received = None
335             self._sent = None
336             self._loss = None
337         else:
338             # last line from console output
339             line = stdout.splitlines()[-1]
340
341             self._result = line
342             logger.info('TrafficGen result: {0}'.format(self._result))
343
344             self._received = self._result.split(', ')[1].split('=')[1]
345             self._sent = self._result.split(', ')[2].split('=')[1]
346             self._loss = self._result.split(', ')[3].split('=')[1]
347
348     def stop_traffic_on_tg(self):
349         """Stop all traffic on TG
350
351         :return: Nothing
352         """
353         if self._node is None:
354             raise RuntimeError("TG is not set")
355         if self._node['subtype'] == NodeSubTypeTG.TREX:
356             self.trex_stl_stop_remote_exec(self._node)
357
358     def send_traffic_on_tg(self, duration, rate, framesize,
359                            traffic_type, warmup_time=5, async_call=False):
360         """Send traffic from all configured interfaces on TG.
361
362         :param duration: Duration of test traffic generation in seconds.
363         :param rate: Offered load per interface (e.g. 1%, 3gbps, 4mpps, ...).
364         :param framesize: Frame size (L2) in Bytes.
365         :param traffic_type: Traffic profile.
366         :type duration: str
367         :type rate: str
368         :type framesize: str
369         :type traffic_type: str
370         :return: TG output.
371         :rtype: str
372         """
373
374         node = self._node
375         if node is None:
376             raise RuntimeError("TG is not set")
377
378         if node['type'] != NodeType.TG:
379             raise Exception('Node type is not a TG')
380
381         if node['subtype'] is None:
382             raise Exception('TG subtype not defined')
383         elif node['subtype'] == NodeSubTypeTG.TREX:
384             self.trex_stl_start_remote_exec(duration, rate, framesize,
385                                             traffic_type, async_call,
386                                             warmup_time=warmup_time)
387         else:
388             raise NotImplementedError("TG subtype not supported")
389
390         return self._result
391
392     def no_traffic_loss_occurred(self):
393         """Fail if loss occurred in traffic run.
394
395         :return: nothing
396         """
397         if self._loss is None:
398             raise Exception('The traffic generation has not been issued')
399         if self._loss != '0':
400             raise Exception('Traffic loss occurred: {0}'.format(self._loss))
401
402     def partial_traffic_loss_accepted(self, loss_acceptance,
403                                       loss_acceptance_type):
404         """Fail if loss is higher then accepted in traffic run.
405
406         :return: nothing
407         """
408         if self._loss is None:
409             raise Exception('The traffic generation has not been issued')
410
411         if loss_acceptance_type == 'percentage':
412             loss = (float(self._loss) / float(self._sent)) * 100
413         elif loss_acceptance_type == 'frames':
414             loss = float(self._loss)
415         else:
416             raise Exception('Loss acceptance type not supported')
417
418         if loss > float(loss_acceptance):
419             raise Exception("Traffic loss {} above loss acceptance: {}".format(
420                 loss, loss_acceptance))