Merge "Modify install.yaml to support Ubuntu 18"
[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 ipaddress
16 import logging
17 import six
18
19 from yardstick.common import utils
20 from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
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 WAIT_PROTOCOLS_STARTED = 360
31
32
33 class IxiaBasicScenario(object):
34     def __init__(self, client, context_cfg, ixia_cfg):
35
36         self.client = client
37         self.context_cfg = context_cfg
38         self.ixia_cfg = ixia_cfg
39
40         self._uplink_vports = None
41         self._downlink_vports = None
42
43     def apply_config(self):
44         pass
45
46     def create_traffic_model(self):
47         vports = self.client.get_vports()
48         self._uplink_vports = vports[::2]
49         self._downlink_vports = vports[1::2]
50         self.client.create_traffic_model(self._uplink_vports,
51                                          self._downlink_vports)
52
53     def run_protocols(self):
54         pass
55
56     def stop_protocols(self):
57         pass
58
59
60 class IxiaPppoeClientScenario(object):
61     def __init__(self, client, context_cfg, ixia_cfg):
62
63         self.client = client
64
65         self._uplink_vports = None
66         self._downlink_vports = None
67
68         self._access_topologies = []
69         self._core_topologies = []
70
71         self._context_cfg = context_cfg
72         self._ixia_cfg = ixia_cfg
73         self.protocols = []
74
75     def apply_config(self):
76         vports = self.client.get_vports()
77         self._uplink_vports = vports[::2]
78         self._downlink_vports = vports[1::2]
79         self._fill_ixia_config()
80         self._apply_access_network_config()
81         self._apply_core_network_config()
82
83     def create_traffic_model(self):
84         self.client.create_ipv4_traffic_model(self._access_topologies,
85                                               self._core_topologies)
86
87     def run_protocols(self):
88         LOG.info('PPPoE Scenario - Start Protocols')
89         self.client.start_protocols()
90         utils.wait_until_true(
91             lambda: self.client.is_protocols_running(self.protocols),
92             timeout=WAIT_PROTOCOLS_STARTED, sleep=2)
93
94     def stop_protocols(self):
95         LOG.info('PPPoE Scenario - Stop Protocols')
96         self.client.stop_protocols()
97
98     def _get_intf_addr(self, intf):
99         """Retrieve interface IP address and mask
100
101         :param intf: could be the string which represents IP address
102         with mask (e.g 192.168.10.2/24) or a dictionary with the host
103         name and the port (e.g. {'tg__0': 'xe1'})
104         :return: (tuple) pair of ip address and mask
105         """
106         if isinstance(intf, six.string_types):
107             ip, mask = tuple(intf.split('/'))
108             return ip, int(mask)
109
110         node_name, intf_name = next(iter(intf.items()))
111         node = self._context_cfg["nodes"].get(node_name, {})
112         interface = node.get("interfaces", {})[intf_name]
113         ip = interface["local_ip"]
114         mask = interface["netmask"]
115         ipaddr = ipaddress.ip_network(six.text_type('{}/{}'.format(ip, mask)),
116                                       strict=False)
117         return ip, ipaddr.prefixlen
118
119     def _fill_ixia_config(self):
120         pppoe = self._ixia_cfg["pppoe_client"]
121         ipv4 = self._ixia_cfg["ipv4_client"]
122
123         _ip = [self._get_intf_addr(intf)[0] for intf in pppoe["ip"]]
124         self._ixia_cfg["pppoe_client"]["ip"] = _ip
125
126         _ip = [self._get_intf_addr(intf)[0] for intf in ipv4["gateway_ip"]]
127         self._ixia_cfg["ipv4_client"]["gateway_ip"] = _ip
128
129         addrs = [self._get_intf_addr(intf) for intf in ipv4["ip"]]
130         _ip = [addr[0] for addr in addrs]
131         _prefix = [addr[1] for addr in addrs]
132
133         self._ixia_cfg["ipv4_client"]["ip"] = _ip
134         self._ixia_cfg["ipv4_client"]["prefix"] = _prefix
135
136     def _apply_access_network_config(self):
137         pppoe = self._ixia_cfg["pppoe_client"]
138         sessions_per_port = pppoe['sessions_per_port']
139         sessions_per_svlan = pppoe['sessions_per_svlan']
140         svlan_count = int(sessions_per_port / sessions_per_svlan)
141
142         # add topology per uplink port (access network)
143         for access_tp_id, vport in enumerate(self._uplink_vports):
144             name = 'Topology access {}'.format(access_tp_id)
145             tp = self.client.add_topology(name, vport)
146             self._access_topologies.append(tp)
147             # add device group per svlan
148             for dg_id in range(svlan_count):
149                 s_vlan_id = int(pppoe['s_vlan']) + dg_id + access_tp_id * svlan_count
150                 s_vlan = ixnet_api.Vlan(vlan_id=s_vlan_id)
151                 c_vlan = ixnet_api.Vlan(vlan_id=pppoe['c_vlan'], vlan_id_step=1)
152                 name = 'SVLAN {}'.format(s_vlan_id)
153                 dg = self.client.add_device_group(tp, name, sessions_per_svlan)
154                 # add ethernet layer to device group
155                 ethernet = self.client.add_ethernet(dg, 'Ethernet')
156                 self.protocols.append(ethernet)
157                 self.client.add_vlans(ethernet, [s_vlan, c_vlan])
158                 # add ppp over ethernet
159                 if 'pap_user' in pppoe:
160                     ppp = self.client.add_pppox_client(ethernet, 'pap',
161                                                        pppoe['pap_user'],
162                                                        pppoe['pap_password'])
163                 else:
164                     ppp = self.client.add_pppox_client(ethernet, 'chap',
165                                                        pppoe['chap_user'],
166                                                        pppoe['chap_password'])
167                 self.protocols.append(ppp)
168
169     def _apply_core_network_config(self):
170         ipv4 = self._ixia_cfg["ipv4_client"]
171         sessions_per_port = ipv4['sessions_per_port']
172         sessions_per_vlan = ipv4['sessions_per_vlan']
173         vlan_count = int(sessions_per_port / sessions_per_vlan)
174
175         # add topology per downlink port (core network)
176         for core_tp_id, vport in enumerate(self._downlink_vports):
177             name = 'Topology core {}'.format(core_tp_id)
178             tp = self.client.add_topology(name, vport)
179             self._core_topologies.append(tp)
180             # add device group per vlan
181             for dg_id in range(vlan_count):
182                 name = 'Core port {}'.format(core_tp_id)
183                 dg = self.client.add_device_group(tp, name, sessions_per_vlan)
184                 # add ethernet layer to device group
185                 ethernet = self.client.add_ethernet(dg, 'Ethernet')
186                 self.protocols.append(ethernet)
187                 if 'vlan' in ipv4:
188                     vlan_id = int(ipv4['vlan']) + dg_id + core_tp_id * vlan_count
189                     vlan = ixnet_api.Vlan(vlan_id=vlan_id)
190                     self.client.add_vlans(ethernet, [vlan])
191                 # add ipv4 layer
192                 gw_ip = ipv4['gateway_ip'][core_tp_id]
193                 # use gw addr to generate ip addr from the same network
194                 ip_addr = ipaddress.IPv4Address(gw_ip) + 1
195                 ipv4_obj = self.client.add_ipv4(ethernet, name='ipv4',
196                                                 addr=ip_addr,
197                                                 addr_step='0.0.0.1',
198                                                 prefix=ipv4['prefix'][core_tp_id],
199                                                 gateway=gw_ip)
200                 self.protocols.append(ipv4_obj)
201                 if ipv4.get("bgp"):
202                     bgp_peer_obj = self.client.add_bgp(ipv4_obj,
203                                                        dut_ip=ipv4["bgp"]["dut_ip"],
204                                                        local_as=ipv4["bgp"]["as_number"],
205                                                        bgp_type=ipv4["bgp"].get("bgp_type"))
206                     self.protocols.append(bgp_peer_obj)
207
208
209 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
210
211     def is_done(self):
212         return self.latency and self.iteration.value > 10
213
214
215 class IxiaResourceHelper(ClientResourceHelper):
216
217     LATENCY_TIME_SLEEP = 120
218
219     def __init__(self, setup_helper, rfc_helper_type=None):
220         super(IxiaResourceHelper, self).__init__(setup_helper)
221         self.scenario_helper = setup_helper.scenario_helper
222
223         self._ixia_scenarios = {
224             "IxiaBasic": IxiaBasicScenario,
225             "IxiaPppoeClient": IxiaPppoeClientScenario,
226         }
227
228         self.client = ixnet_api.IxNextgen()
229
230         if rfc_helper_type is None:
231             rfc_helper_type = IxiaRfc2544Helper
232
233         self.rfc_helper = rfc_helper_type(self.scenario_helper)
234         self.uplink_ports = None
235         self.downlink_ports = None
236         self.context_cfg = None
237         self._ix_scenario = None
238         self._connect()
239
240     def _connect(self, client=None):
241         self.client.connect(self.vnfd_helper)
242
243     def get_stats(self, *args, **kwargs):
244         return self.client.get_statistics()
245
246     def setup(self):
247         super(IxiaResourceHelper, self).setup()
248         self._init_ix_scenario()
249
250     def stop_collect(self):
251         self._ix_scenario.stop_protocols()
252         self._terminated.value = 1
253
254     def generate_samples(self, ports, duration):
255         stats = self.get_stats()
256
257         samples = {}
258         # this is not DPDK port num, but this is whatever number we gave
259         # when we selected ports and programmed the profile
260         for port_num in ports:
261             try:
262                 # reverse lookup port name from port_num so the stats dict is descriptive
263                 intf = self.vnfd_helper.find_interface_by_port(port_num)
264                 port_name = intf['name']
265                 avg_latency = stats['Store-Forward_Avg_latency_ns'][port_num]
266                 min_latency = stats['Store-Forward_Min_latency_ns'][port_num]
267                 max_latency = stats['Store-Forward_Max_latency_ns'][port_num]
268                 samples[port_name] = {
269                     'rx_throughput_kps': float(stats['Rx_Rate_Kbps'][port_num]),
270                     'tx_throughput_kps': float(stats['Tx_Rate_Kbps'][port_num]),
271                     'rx_throughput_mbps': float(stats['Rx_Rate_Mbps'][port_num]),
272                     'tx_throughput_mbps': float(stats['Tx_Rate_Mbps'][port_num]),
273                     'in_packets': int(stats['Valid_Frames_Rx'][port_num]),
274                     'out_packets': int(stats['Frames_Tx'][port_num]),
275                     'RxThroughput': float(stats['Valid_Frames_Rx'][port_num]) / duration,
276                     'TxThroughput': float(stats['Frames_Tx'][port_num]) / duration,
277                     'Store-Forward_Avg_latency_ns': utils.safe_cast(avg_latency, int, 0),
278                     'Store-Forward_Min_latency_ns': utils.safe_cast(min_latency, int, 0),
279                     'Store-Forward_Max_latency_ns': utils.safe_cast(max_latency, int, 0)
280                 }
281             except IndexError:
282                 pass
283
284         return samples
285
286     def _init_ix_scenario(self):
287         ixia_config = self.scenario_helper.scenario_cfg.get('ixia_config', 'IxiaBasic')
288
289         if ixia_config in self._ixia_scenarios:
290             scenario_type = self._ixia_scenarios[ixia_config]
291
292             self._ix_scenario = scenario_type(self.client, self.context_cfg,
293                                               self.scenario_helper.scenario_cfg['options'])
294         else:
295             raise RuntimeError(
296                 "IXIA config type '{}' not supported".format(ixia_config))
297
298     def _initialize_client(self):
299         """Initialize the IXIA IxNetwork client and configure the server"""
300         self.client.clear_config()
301         self.client.assign_ports()
302         self._ix_scenario.apply_config()
303         self._ix_scenario.create_traffic_model()
304
305     def run_traffic(self, traffic_profile, *args):
306         if self._terminated.value:
307             return
308
309         min_tol = self.rfc_helper.tolerance_low
310         max_tol = self.rfc_helper.tolerance_high
311         precision = self.rfc_helper.tolerance_precision
312         default = "00:00:00:00:00:00"
313
314         self._build_ports()
315         self._initialize_client()
316
317         mac = {}
318         for port_name in self.vnfd_helper.port_pairs.all_ports:
319             intf = self.vnfd_helper.find_interface(name=port_name)
320             virt_intf = intf["virtual-interface"]
321             # we only know static traffic id by reading the json
322             # this is used by _get_ixia_trafficrofile
323             port_num = self.vnfd_helper.port_num(intf)
324             mac["src_mac_{}".format(port_num)] = virt_intf.get("local_mac", default)
325             mac["dst_mac_{}".format(port_num)] = virt_intf.get("dst_mac", default)
326
327         self._ix_scenario.run_protocols()
328
329         try:
330             while not self._terminated.value:
331                 first_run = traffic_profile.execute_traffic(
332                     self, self.client, mac)
333                 self.client_started.value = 1
334                 # pylint: disable=unnecessary-lambda
335                 utils.wait_until_true(lambda: self.client.is_traffic_stopped(),
336                                       timeout=traffic_profile.config.duration * 2)
337                 samples = self.generate_samples(traffic_profile.ports,
338                                                 traffic_profile.config.duration)
339
340                 completed, samples = traffic_profile.get_drop_percentage(
341                     samples, min_tol, max_tol, precision, first_run=first_run)
342                 self._queue.put(samples)
343
344                 if completed:
345                     self._terminated.value = 1
346
347         except Exception:  # pylint: disable=broad-except
348             LOG.exception('Run Traffic terminated')
349
350         self._ix_scenario.stop_protocols()
351         self._terminated.value = 1
352
353     def collect_kpi(self):
354         self.rfc_helper.iteration.value += 1
355         return super(IxiaResourceHelper, self).collect_kpi()
356
357
358 class IxiaTrafficGen(SampleVNFTrafficGen):
359
360     APP_NAME = 'Ixia'
361
362     def __init__(self, name, vnfd, task_id, setup_env_helper_type=None,
363                  resource_helper_type=None):
364         if resource_helper_type is None:
365             resource_helper_type = IxiaResourceHelper
366         super(IxiaTrafficGen, self).__init__(
367             name, vnfd, task_id, setup_env_helper_type, resource_helper_type)
368         self._ixia_traffic_gen = None
369         self.ixia_file_name = ''
370         self.vnf_port_pairs = []
371
372     def _check_status(self):
373         pass
374
375     def terminate(self):
376         self.resource_helper.stop_collect()
377         super(IxiaTrafficGen, self).terminate()