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