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