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