Merge "add error logs to storperf"
[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 import logging
16
17 from yardstick.common import utils
18 from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
19 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
20 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
21 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
22
23
24 LOG = logging.getLogger(__name__)
25
26 WAIT_AFTER_CFG_LOAD = 10
27 WAIT_FOR_TRAFFIC = 30
28
29
30 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
31
32     def is_done(self):
33         return self.latency and self.iteration.value > 10
34
35
36 class IxiaResourceHelper(ClientResourceHelper):
37
38     LATENCY_TIME_SLEEP = 120
39
40     def __init__(self, setup_helper, rfc_helper_type=None):
41         super(IxiaResourceHelper, self).__init__(setup_helper)
42         self.scenario_helper = setup_helper.scenario_helper
43
44         self.client = ixnet_api.IxNextgen()
45
46         if rfc_helper_type is None:
47             rfc_helper_type = IxiaRfc2544Helper
48
49         self.rfc_helper = rfc_helper_type(self.scenario_helper)
50         self.uplink_ports = None
51         self.downlink_ports = None
52         self._connect()
53
54     def _connect(self, client=None):
55         self.client.connect(self.vnfd_helper)
56
57     def get_stats(self, *args, **kwargs):
58         return self.client.get_statistics()
59
60     def stop_collect(self):
61         self._terminated.value = 1
62
63     def generate_samples(self, ports, duration):
64         stats = self.get_stats()
65
66         samples = {}
67         # this is not DPDK port num, but this is whatever number we gave
68         # when we selected ports and programmed the profile
69         for port_num in ports:
70             try:
71                 # reverse lookup port name from port_num so the stats dict is descriptive
72                 intf = self.vnfd_helper.find_interface_by_port(port_num)
73                 port_name = intf['name']
74                 avg_latency = stats['Store-Forward_Avg_latency_ns'][port_num]
75                 min_latency = stats['Store-Forward_Min_latency_ns'][port_num]
76                 max_latency = stats['Store-Forward_Max_latency_ns'][port_num]
77                 samples[port_name] = {
78                     'rx_throughput_kps': float(stats['Rx_Rate_Kbps'][port_num]),
79                     'tx_throughput_kps': float(stats['Tx_Rate_Kbps'][port_num]),
80                     'rx_throughput_mbps': float(stats['Rx_Rate_Mbps'][port_num]),
81                     'tx_throughput_mbps': float(stats['Tx_Rate_Mbps'][port_num]),
82                     'in_packets': int(stats['Valid_Frames_Rx'][port_num]),
83                     'out_packets': int(stats['Frames_Tx'][port_num]),
84                     'RxThroughput': float(stats['Valid_Frames_Rx'][port_num]) / duration,
85                     'TxThroughput': float(stats['Frames_Tx'][port_num]) / duration,
86                     'Store-Forward_Avg_latency_ns': utils.safe_cast(avg_latency, int, 0),
87                     'Store-Forward_Min_latency_ns': utils.safe_cast(min_latency, int, 0),
88                     'Store-Forward_Max_latency_ns': utils.safe_cast(max_latency, int, 0)
89                 }
90             except IndexError:
91                 pass
92
93         return samples
94
95     def _initialize_client(self):
96         """Initialize the IXIA IxNetwork client and configure the server"""
97         self.client.clear_config()
98         self.client.assign_ports()
99         vports = self.client.get_vports()
100         uplink_vports = vports[::2]
101         downlink_vports = vports[1::2]
102         self.client.create_traffic_model(uplink_vports, downlink_vports)
103
104     def run_traffic(self, traffic_profile, *args):
105         if self._terminated.value:
106             return
107
108         min_tol = self.rfc_helper.tolerance_low
109         max_tol = self.rfc_helper.tolerance_high
110         precision = self.rfc_helper.tolerance_precision
111         default = "00:00:00:00:00:00"
112
113         self._build_ports()
114         self._initialize_client()
115
116         mac = {}
117         for port_name in self.vnfd_helper.port_pairs.all_ports:
118             intf = self.vnfd_helper.find_interface(name=port_name)
119             virt_intf = intf["virtual-interface"]
120             # we only know static traffic id by reading the json
121             # this is used by _get_ixia_trafficrofile
122             port_num = self.vnfd_helper.port_num(intf)
123             mac["src_mac_{}".format(port_num)] = virt_intf.get("local_mac", default)
124             mac["dst_mac_{}".format(port_num)] = virt_intf.get("dst_mac", default)
125
126         try:
127             while not self._terminated.value:
128                 first_run = traffic_profile.execute_traffic(
129                     self, self.client, mac)
130                 self.client_started.value = 1
131                 # pylint: disable=unnecessary-lambda
132                 utils.wait_until_true(lambda: self.client.is_traffic_stopped(),
133                                       timeout=traffic_profile.config.duration * 2)
134                 samples = self.generate_samples(traffic_profile.ports,
135                                                 traffic_profile.config.duration)
136
137                 completed, samples = traffic_profile.get_drop_percentage(
138                     samples, min_tol, max_tol, precision, first_run=first_run)
139                 self._queue.put(samples)
140
141                 if completed:
142                     self._terminated.value = 1
143
144         except Exception:  # pylint: disable=broad-except
145             LOG.exception('Run Traffic terminated')
146
147         self._terminated.value = 1
148
149     def collect_kpi(self):
150         self.rfc_helper.iteration.value += 1
151         return super(IxiaResourceHelper, self).collect_kpi()
152
153
154 class IxiaTrafficGen(SampleVNFTrafficGen):
155
156     APP_NAME = 'Ixia'
157
158     def __init__(self, name, vnfd, task_id, setup_env_helper_type=None,
159                  resource_helper_type=None):
160         if resource_helper_type is None:
161             resource_helper_type = IxiaResourceHelper
162         super(IxiaTrafficGen, self).__init__(
163             name, vnfd, task_id, setup_env_helper_type, resource_helper_type)
164         self._ixia_traffic_gen = None
165         self.ixia_file_name = ''
166         self.vnf_port_pairs = []
167
168     def _check_status(self):
169         pass
170
171     def terminate(self):
172         self.resource_helper.stop_collect()
173         super(IxiaTrafficGen, self).terminate()