Add fixes for heat deployed UDP_Replay and TRex
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / udp_replay.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 import logging
17
18 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
19 from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
20 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
21
22 LOG = logging.getLogger(__name__)
23
24 # UDP_Replay should work the same on all systems, we can provide the binary
25 REPLAY_PIPELINE_COMMAND = (
26     """sudo {tool_path} -c {cpu_mask_hex} -n 4 -w {whitelist} -- """
27     """{hw_csum} -p {ports_len_hex} --config='{config}'"""
28 )
29 # {tool_path} -p {ports_len_hex} -f {cfg_file} -s {script}'
30
31
32 class UdpReplaySetupEnvHelper(DpdkVnfSetupEnvHelper):
33
34     APP_NAME = "UDP_Replay"
35
36
37 class UdpReplayResourceHelper(ClientResourceHelper):
38     pass
39
40
41 class UdpReplayApproxVnf(SampleVNF):
42
43     APP_NAME = "UDP_Replay"
44     APP_WORD = "UDP_Replay"
45     VNF_PROMPT = 'Replay>'
46
47     VNF_TYPE = 'UdpReplay'
48
49     HW_OFFLOADING_NFVI_TYPES = {'baremetal', 'sriov'}
50
51     PIPELINE_COMMAND = REPLAY_PIPELINE_COMMAND
52
53     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
54         if resource_helper_type is None:
55             resource_helper_type = UdpReplayResourceHelper
56
57         if setup_env_helper_type is None:
58             setup_env_helper_type = UdpReplaySetupEnvHelper
59
60         super(UdpReplayApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
61                                                  resource_helper_type)
62
63     def _start_server(self):
64         super(UdpReplayApproxVnf, self)._start_server()
65         self.resource_helper.start()
66
67     def scale(self, flavor=""):
68         """ scale vnfbased on flavor input """
69         raise NotImplementedError
70
71     def _deploy(self):
72         self.generate_port_pairs()
73         super(UdpReplayApproxVnf, self)._deploy()
74
75     def _build_pipeline_kwargs(self):
76         all_ports = [i for i, _ in enumerate(self.vnfd_helper.interfaces)]
77         number_of_ports = len(all_ports)
78
79         tool_path = self.ssh_helper.provision_tool(tool_file=self.APP_NAME)
80         ports_mask = 2 ** number_of_ports - 1
81         ports_mask_hex = hex(ports_mask)
82         cpu_mask_hex = hex(2 ** (number_of_ports + 1) - 1)
83         hw_csum = ""
84         if (not self.scenario_helper.options.get('hw_csum', False) or
85                 self.nfvi_context.attrs.get('nfvi_type') not in self.HW_OFFLOADING_NFVI_TYPES):
86             hw_csum = '--no-hw-csum'
87
88         config_value = "".join(str((port, 0, port + 1)) for port in all_ports)
89
90         whitelist = " -w ".join(self.setup_helper.bound_pci)
91         self.pipeline_kwargs = {
92             'ports_len_hex': ports_mask_hex,
93             'tool_path': tool_path,
94             'hw_csum': hw_csum,
95             'whitelist': whitelist,
96             'cpu_mask_hex': cpu_mask_hex,
97             'config': config_value,
98         }
99
100     def _build_config(self):
101         self._build_pipeline_kwargs()
102         return self.PIPELINE_COMMAND.format(**self.pipeline_kwargs)
103
104     def collect_kpi(self):
105         def get_sum(offset):
106             return sum(int(i) for i in split_stats[offset::5])
107
108         number_of_ports = len(self.vnfd_helper.interfaces)
109
110         stats = self.get_stats()
111         stats_words = stats.split()
112         split_stats = stats_words[stats_words.index('0'):][:number_of_ports * 5]
113         result = {
114             "packets_in": get_sum(1),
115             "packets_fwd": get_sum(2),
116             "packets_dropped": get_sum(3) + get_sum(4),
117             "collect_stats": {},
118         }
119
120         LOG.debug("UDP Replay collect KPIs %s", result)
121         return result
122
123     def get_stats(self):
124         """
125         Method for checking the statistics
126
127         :return:
128            UDP Replay statistics
129         """
130         cmd = 'UDP_Replay stats'
131         out = self.vnf_execute(cmd)
132         return out