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