Merge "Change "KubernetesObject" class name to "ReplicationController""
[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.common.process import check_if_process_failed
19 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
20 from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
21 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
22 from yardstick.benchmark.contexts import base as ctx_base
23
24 LOG = logging.getLogger(__name__)
25
26 # UDP_Replay should work the same on all systems, we can provide the binary
27
28 # we can't match the prompt regexp due to extra noise
29 # yardstick.ssh ssh.py:302 DEBUG stdout: UDP_Replay: lcore 0 has nothing to do
30 # eplUDP_Replay:  -- lcoreid=1 portid=0 rxqueueid=0
31 # ay>
32 #
33 # try decreasing log level to RTE_LOG_NOTICE (5)
34 REPLAY_PIPELINE_COMMAND = (
35     """sudo {tool_path} --log-level=5 -c {cpu_mask_hex} -n 4 -w {whitelist} -- """
36     """{hw_csum} -p {port_mask_hex} --config='{config}'"""
37 )
38 # {tool_path} -p {port_mask_hex} -f {cfg_file} -s {script}'
39
40
41 class UdpReplaySetupEnvHelper(DpdkVnfSetupEnvHelper):
42
43     APP_NAME = "UDP_Replay"
44
45
46 class UdpReplayResourceHelper(ClientResourceHelper):
47     pass
48
49
50 class UdpReplayApproxVnf(SampleVNF):
51
52     APP_NAME = "UDP_Replay"
53     APP_WORD = "UDP_Replay"
54     # buffering issue?
55     VNF_PROMPT = 'eplay>'
56
57     VNF_TYPE = 'UdpReplay'
58
59     HW_OFFLOADING_NFVI_TYPES = {'baremetal', 'sriov'}
60
61     PIPELINE_COMMAND = REPLAY_PIPELINE_COMMAND
62
63     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
64         if resource_helper_type is None:
65             resource_helper_type = UdpReplayResourceHelper
66
67         if setup_env_helper_type is None:
68             setup_env_helper_type = UdpReplaySetupEnvHelper
69
70         super(UdpReplayApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
71                                                  resource_helper_type)
72
73     def _build_pipeline_kwargs(self):
74         ports = self.vnfd_helper.port_pairs.all_ports
75         number_of_ports = len(ports)
76
77         tool_path = self.ssh_helper.provision_tool(tool_file=self.APP_NAME)
78         port_nums = self.vnfd_helper.port_nums(ports)
79         ports_mask_hex = hex(sum(2 ** num for num in port_nums))
80         # one core extra for master
81         cpu_mask_hex = hex(2 ** (number_of_ports + 1) - 1)
82         nfvi_context = ctx_base.Context.get_context_from_server(
83             self.scenario_helper.nodes[self.name])
84         hw_csum = ""
85         if (not self.scenario_helper.options.get('hw_csum', False) or
86                 nfvi_context.attrs.get('nfvi_type') not in self.HW_OFFLOADING_NFVI_TYPES):
87             hw_csum = '--no-hw-csum'
88
89         # tuples of (FLD_PORT, FLD_QUEUE, FLD_LCORE)
90         #  [--config (port,queue,lcore)[,(port,queue,lcore]]"
91         # start with lcore = 1 since we use lcore=0 for master
92         config_value = ",".join(
93             str((self.vnfd_helper.port_num(port), 0, core)).replace(" ", "") for core, port in
94             enumerate(self.vnfd_helper.port_pairs.all_ports, 1))
95
96         whitelist = " -w ".join(self.setup_helper.bound_pci)
97         self.pipeline_kwargs = {
98             'port_mask_hex': ports_mask_hex,
99             'tool_path': tool_path,
100             'hw_csum': hw_csum,
101             'whitelist': whitelist,
102             'cpu_mask_hex': cpu_mask_hex,
103             'config': config_value,
104         }
105
106     def _build_config(self):
107         self._build_pipeline_kwargs()
108         return self.PIPELINE_COMMAND.format(**self.pipeline_kwargs)
109
110     def collect_kpi(self):
111         def get_sum(offset):
112             return sum(int(i) for i in split_stats[offset::5])
113         # we can't get KPIs if the VNF is down
114         check_if_process_failed(self._vnf_process)
115
116         number_of_ports = len(self.vnfd_helper.port_pairs.all_ports)
117
118         stats = self.get_stats()
119         stats_words = stats.split()
120         split_stats = stats_words[stats_words.index('0'):][:number_of_ports * 5]
121
122         physical_node = ctx_base.Context.get_physical_node_from_server(
123             self.scenario_helper.nodes[self.name])
124
125         result = {
126             "physical_node": physical_node,
127             "packets_in": get_sum(1),
128             "packets_fwd": get_sum(2),
129             "packets_dropped": get_sum(3) + get_sum(4),
130             'collect_stats': self.resource_helper.collect_kpi(),
131         }
132
133         LOG.debug("UDP Replay collect KPIs %s", result)
134         return result
135
136     def get_stats(self):
137         """
138         Method for checking the statistics
139
140         :return:
141            UDP Replay statistics
142         """
143         cmd = 'UDP_Replay stats'
144         out = self.vnf_execute(cmd)
145         return out