tg_rfc2544_ixia: use traffic id from JSON to update mac
[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         for port_name in ports:
85             try:
86                 # this is not DPDK port num, but this is whatever number we gave
87                 # when we selected ports and programmed the profile
88                 port_num = self.vnfd_helper.port_num(port_name)
89                 samples[port_name] = {
90                     "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][port_num]),
91                     "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][port_num]),
92                     "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][port_num]),
93                     "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][port_num]),
94                     "in_packets": int(last_result["Valid_Frames_Rx"][port_num]),
95                     "out_packets": int(last_result["Frames_Tx"][port_num]),
96                     "RxThroughput": int(last_result["Valid_Frames_Rx"][port_num]) / 30,
97                     "TxThroughput": int(last_result["Frames_Tx"][port_num]) / 30,
98                 }
99                 if key:
100                     avg_latency = latency["Store-Forward_Avg_latency_ns"][port_num]
101                     min_latency = latency["Store-Forward_Min_latency_ns"][port_num]
102                     max_latency = latency["Store-Forward_Max_latency_ns"][port_num]
103                     samples[port_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         ixia_file = find_relative_file("ixia_traffic.cfg",
132                                        self.scenario_helper.scenario_cfg["task_path"])
133
134         static_traffic = {}
135         with open(ixia_file) as stream:
136             try:
137                 static_traffic = json.load(stream)
138             except Exception:
139                 LOG.exception("")
140         mac = {}
141         for vld_id, traffic in static_traffic.items():
142             intfs = self.vnfd_helper.port_pairs.networks.get(vld_id, [])
143             interface = next(intfs, None)
144             if interface:
145                 virt_intf = interface["virtual-interface"]
146                 # we only know static traffic id by reading the json
147                 # this is used by _get_ixia_traffic_profile
148                 mac["src_mac_{}".format(traffic["id"])] = virt_intf.get("local_mac", default)
149                 mac["dst_mac_{}".format(traffic["id"])] = virt_intf.get("dst_mac", default)
150
151         samples = {}
152         # Generate ixia traffic config...
153         try:
154             while not self._terminated.value:
155                 traffic_profile.execute_traffic(self, self.client, mac, ixia_file)
156                 self.client_started.value = 1
157                 time.sleep(WAIT_FOR_TRAFFIC)
158                 self.client.ix_stop_traffic()
159                 samples = self.generate_samples(traffic_profile.ports)
160                 self._queue.put(samples)
161                 status, samples = traffic_profile.get_drop_percentage(self, samples, min_tol,
162                                                                       max_tol, self.client, mac,
163                                                                       ixia_file)
164
165                 current = samples['CurrentDropPercentage']
166                 if min_tol <= current <= max_tol or status == 'Completed':
167                     self._terminated.value = 1
168
169             self.client.ix_stop_traffic()
170             self._queue.put(samples)
171         except Exception:
172             LOG.info("Run Traffic terminated")
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         self._terminated.value = 1
190
191     def collect_kpi(self):
192         self.rfc_helper.iteration.value += 1
193         return super(IxiaResourceHelper, self).collect_kpi()
194
195
196 class IxiaTrafficGen(SampleVNFTrafficGen):
197
198     APP_NAME = 'Ixia'
199
200     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
201         if resource_helper_type is None:
202             resource_helper_type = IxiaResourceHelper
203
204         super(IxiaTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
205                                              resource_helper_type)
206         self._ixia_traffic_gen = None
207         self.ixia_file_name = ''
208         self.vnf_port_pairs = []
209
210     def _check_status(self):
211         pass
212
213     def scale(self, flavor=""):
214         pass
215
216     def listen_traffic(self, traffic_profile):
217         pass
218
219     def terminate(self):
220         self.resource_helper.stop_collect()
221         super(IxiaTrafficGen, self).terminate()
222
223     def wait_for_instantiate(self):
224         # not needed for IxNet
225         pass