Merge "Fix typo in classname AclUknownActionTemplate"
[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, task_id, setup_env_helper_type=None,
64                  resource_helper_type=None):
65         if resource_helper_type is None:
66             resource_helper_type = UdpReplayResourceHelper
67         if setup_env_helper_type is None:
68             setup_env_helper_type = UdpReplaySetupEnvHelper
69         super(UdpReplayApproxVnf, self).__init__(
70             name, vnfd, task_id, setup_env_helper_type, resource_helper_type)
71
72     def _build_pipeline_kwargs(self):
73         ports = self.vnfd_helper.port_pairs.all_ports
74         number_of_ports = len(ports)
75
76         tool_path = self.ssh_helper.provision_tool(tool_file=self.APP_NAME)
77         port_nums = self.vnfd_helper.port_nums(ports)
78         ports_mask_hex = hex(sum(2 ** num for num in port_nums))
79         # one core extra for master
80         cpu_mask_hex = hex(2 ** (number_of_ports + 1) - 1)
81         nfvi_context = ctx_base.Context.get_context_from_server(
82             self.scenario_helper.nodes[self.name])
83         hw_csum = ""
84         if (not self.scenario_helper.options.get('hw_csum', False) or
85                 nfvi_context.attrs.get('nfvi_type') not in self.HW_OFFLOADING_NFVI_TYPES):
86             hw_csum = '--no-hw-csum'
87
88         # tuples of (FLD_PORT, FLD_QUEUE, FLD_LCORE)
89         #  [--config (port,queue,lcore)[,(port,queue,lcore]]"
90         # start with lcore = 1 since we use lcore=0 for master
91         config_value = ",".join(
92             str((self.vnfd_helper.port_num(port), 0, core)).replace(" ", "") for core, port in
93             enumerate(self.vnfd_helper.port_pairs.all_ports, 1))
94
95         whitelist = " -w ".join(self.setup_helper.bound_pci)
96         self.pipeline_kwargs = {
97             'port_mask_hex': ports_mask_hex,
98             'tool_path': tool_path,
99             'hw_csum': hw_csum,
100             'whitelist': whitelist,
101             'cpu_mask_hex': cpu_mask_hex,
102             'config': config_value,
103         }
104
105     def _build_config(self):
106         self._build_pipeline_kwargs()
107         return self.PIPELINE_COMMAND.format(**self.pipeline_kwargs)
108
109     def collect_kpi(self):
110         def get_sum(offset):
111             return sum(int(i) for i in split_stats[offset::5])
112         # we can't get KPIs if the VNF is down
113         check_if_process_failed(self._vnf_process)
114
115         number_of_ports = len(self.vnfd_helper.port_pairs.all_ports)
116
117         stats = self.get_stats()
118         stats_words = stats.split()
119         split_stats = stats_words[stats_words.index('0'):][:number_of_ports * 5]
120
121         physical_node = ctx_base.Context.get_physical_node_from_server(
122             self.scenario_helper.nodes[self.name])
123
124         result = {
125             "physical_node": physical_node,
126             "packets_in": get_sum(1),
127             "packets_fwd": get_sum(2),
128             "packets_dropped": get_sum(3) + get_sum(4),
129             'collect_stats': self.resource_helper.collect_kpi(),
130         }
131
132         LOG.debug("UDP Replay collect KPIs %s", result)
133         return result
134
135     def get_stats(self):
136         """
137         Method for checking the statistics
138
139         :return:
140            UDP Replay statistics
141         """
142         cmd = 'UDP_Replay stats'
143         out = self.vnf_execute(cmd)
144         return out