Terminate the run traffic if initiated after traffic is closed
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_ixia.py
1 # Copyright (c) 2016-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 from __future__ import absolute_import
16
17 import json
18 import time
19 import os
20 import logging
21 import sys
22
23 from yardstick.common.utils import ErrorClass
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
25 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
26 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
27 from yardstick.benchmark.scenarios.networking.vnf_generic import find_relative_file
28
29 LOG = logging.getLogger(__name__)
30
31 WAIT_AFTER_CFG_LOAD = 10
32 WAIT_FOR_TRAFFIC = 30
33 IXIA_LIB = os.path.dirname(os.path.realpath(__file__))
34 IXNET_LIB = os.path.join(IXIA_LIB, "../../libs/ixia_libs/IxNet")
35 sys.path.append(IXNET_LIB)
36
37 try:
38     from IxNet import IxNextgen
39 except ImportError:
40     IxNextgen = ErrorClass
41
42
43 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
44
45     def is_done(self):
46         return self.latency and self.iteration.value > 10
47
48
49 class IxiaResourceHelper(ClientResourceHelper):
50
51     LATENCY_TIME_SLEEP = 120
52
53     def __init__(self, setup_helper, rfc_helper_type=None):
54         super(IxiaResourceHelper, self).__init__(setup_helper)
55         self.scenario_helper = setup_helper.scenario_helper
56
57         self.client = IxNextgen()
58
59         if rfc_helper_type is None:
60             rfc_helper_type = IxiaRfc2544Helper
61
62         self.rfc_helper = rfc_helper_type(self.scenario_helper)
63         self.uplink_ports = None
64         self.downlink_ports = None
65         self._connect()
66
67     def _connect(self, client=None):
68         self.client._connect(self.vnfd_helper)
69
70     def get_stats(self, *args, **kwargs):
71         return self.client.ix_get_statistics()
72
73     def stop_collect(self):
74         self._terminated.value = 1
75         if self.client:
76             self.client.ix_stop_traffic()
77
78     def generate_samples(self, ports, key=None, default=None):
79         stats = self.get_stats()
80         last_result = stats[1]
81         latency = stats[0]
82
83         samples = {}
84         # this is not DPDK port num, but this is whatever number we gave
85         # when we selected ports and programmed the profile
86         for port_num in ports:
87             try:
88                 # reverse lookup port name from port_num so the stats dict is descriptive
89                 intf = self.vnfd_helper.find_interface_by_port(port_num)
90                 port_name = intf["name"]
91                 samples[port_name] = {
92                     "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][port_num]),
93                     "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][port_num]),
94                     "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][port_num]),
95                     "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][port_num]),
96                     "in_packets": int(last_result["Valid_Frames_Rx"][port_num]),
97                     "out_packets": int(last_result["Frames_Tx"][port_num]),
98                     "RxThroughput": int(last_result["Valid_Frames_Rx"][port_num]) / 30,
99                     "TxThroughput": int(last_result["Frames_Tx"][port_num]) / 30,
100                 }
101                 if key:
102                     avg_latency = latency["Store-Forward_Avg_latency_ns"][port_num]
103                     min_latency = latency["Store-Forward_Min_latency_ns"][port_num]
104                     max_latency = latency["Store-Forward_Max_latency_ns"][port_num]
105                     samples[port_name][key] = \
106                         {"Store-Forward_Avg_latency_ns": avg_latency,
107                          "Store-Forward_Min_latency_ns": min_latency,
108                          "Store-Forward_Max_latency_ns": max_latency}
109             except IndexError:
110                 pass
111
112         return samples
113
114     def run_traffic(self, traffic_profile):
115         if self._terminated.value:
116             return
117
118         min_tol = self.rfc_helper.tolerance_low
119         max_tol = self.rfc_helper.tolerance_high
120         default = "00:00:00:00:00:00"
121
122         self._build_ports()
123
124         # we don't know client_file_name until runtime as instantiate
125         client_file_name = \
126             find_relative_file(self.scenario_helper.scenario_cfg['ixia_profile'],
127                                self.scenario_helper.scenario_cfg["task_path"])
128         self.client.ix_load_config(client_file_name)
129         time.sleep(WAIT_AFTER_CFG_LOAD)
130
131         self.client.ix_assign_ports()
132
133         ixia_file = find_relative_file("ixia_traffic.cfg",
134                                        self.scenario_helper.scenario_cfg["task_path"])
135
136         static_traffic = {}
137         with open(ixia_file) as stream:
138             try:
139                 static_traffic = json.load(stream)
140             except Exception:
141                 LOG.exception("")
142         mac = {}
143         for vld_id, traffic in static_traffic.items():
144             intfs = self.vnfd_helper.port_pairs.networks.get(vld_id, [])
145             interface = next(iter(intfs), None)
146             if interface:
147                 virt_intf = self.vnfd_helper.find_interface(name=interface)["virtual-interface"]
148                 # we only know static traffic id by reading the json
149                 # this is used by _get_ixia_traffic_profile
150                 mac["src_mac_{}".format(traffic["id"])] = virt_intf.get("local_mac", default)
151                 mac["dst_mac_{}".format(traffic["id"])] = virt_intf.get("dst_mac", default)
152
153         samples = {}
154         # Generate ixia traffic config...
155         try:
156             while not self._terminated.value:
157                 traffic_profile.execute_traffic(self, self.client, mac, ixia_file)
158                 self.client_started.value = 1
159                 time.sleep(WAIT_FOR_TRAFFIC)
160                 self.client.ix_stop_traffic()
161                 samples = self.generate_samples(traffic_profile.ports)
162                 self._queue.put(samples)
163                 status, samples = traffic_profile.get_drop_percentage(self, samples, min_tol,
164                                                                       max_tol, self.client, mac,
165                                                                       ixia_file)
166
167                 current = samples['CurrentDropPercentage']
168                 if min_tol <= current <= max_tol or status == 'Completed':
169                     self._terminated.value = 1
170
171             self.client.ix_stop_traffic()
172             self._queue.put(samples)
173
174             if not self.rfc_helper.is_done():
175                 self._terminated.value = 1
176                 return
177
178             traffic_profile.execute_traffic(self, self.client, mac, ixia_file)
179             for _ in range(5):
180                 time.sleep(self.LATENCY_TIME_SLEEP)
181                 self.client.ix_stop_traffic()
182                 samples = self.generate_samples(traffic_profile.ports, 'latency', {})
183                 self._queue.put(samples)
184                 traffic_profile.start_ixia_latency(self, self.client, mac, ixia_file)
185                 if self._terminated.value:
186                     break
187
188             self.client.ix_stop_traffic()
189         except Exception:
190             LOG.exception("Run Traffic terminated")
191
192         self._terminated.value = 1
193
194     def collect_kpi(self):
195         self.rfc_helper.iteration.value += 1
196         return super(IxiaResourceHelper, self).collect_kpi()
197
198
199 class IxiaTrafficGen(SampleVNFTrafficGen):
200
201     APP_NAME = 'Ixia'
202
203     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
204         if resource_helper_type is None:
205             resource_helper_type = IxiaResourceHelper
206
207         super(IxiaTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
208                                              resource_helper_type)
209         self._ixia_traffic_gen = None
210         self.ixia_file_name = ''
211         self.vnf_port_pairs = []
212
213     def _check_status(self):
214         pass
215
216     def scale(self, flavor=""):
217         pass
218
219     def listen_traffic(self, traffic_profile):
220         pass
221
222     def terminate(self):
223         self.resource_helper.stop_collect()
224         super(IxiaTrafficGen, self).terminate()
225
226     def wait_for_instantiate(self):
227         # not needed for IxNet
228         pass