29a87f2ee0854598e0acee83b8fa3e0a55e72d8b
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / benchmarks / instantiation_validation_benchmark.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
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 os
16 import commands
17 import signal
18 import time
19 from experimental_framework.benchmarks import benchmark_base_class as base
20 from experimental_framework.constants import framework_parameters as fp
21 from experimental_framework.constants import conf_file_sections as cfs
22 from experimental_framework.packet_generators import dpdk_packet_generator \
23     as dpdk
24 import experimental_framework.common as common
25
26
27 THROUGHPUT = 'throughput'
28 VLAN_SENDER = 'vlan_sender'
29 VLAN_RECEIVER = 'vlan_receiver'
30 PACKETS_FILE_NAME = 'packets.res'
31 PACKET_CHECKER_PROGRAM_NAME = 'test_sniff'
32 MULTICAST_GROUP = '224.192.16.1'
33
34
35 class InstantiationValidationBenchmark(base.BenchmarkBaseClass):
36
37     def __init__(self, name, params):
38         base.BenchmarkBaseClass.__init__(self, name, params)
39         self.base_dir = common.get_base_dir() + \
40             fp.EXPERIMENTAL_FRAMEWORK_DIR + fp.DPDK_PKTGEN_DIR
41         self.results_file = self.base_dir + PACKETS_FILE_NAME
42         self.lua_file = self.base_dir + 'constant_traffic.lua'
43         self.res_dir = ''
44         self.interface_name = ''
45
46         # Set the packet checker command
47         self.pkt_checker_command = common.get_base_dir()
48         self.pkt_checker_command += 'experimental_framework/libraries/'
49         self.pkt_checker_command += 'packet_checker/'
50         self.pkt_checker_command += PACKET_CHECKER_PROGRAM_NAME + ' '
51
52     def init(self):
53         """
54         Initialize the benchmark
55         :return: None
56         """
57         pass
58
59     def finalize(self):
60         """
61         Finalizes the benchmark
62         :return: None
63         """
64         pass
65
66     def get_features(self):
67         features = dict()
68         features['description'] = 'Instantiation Validation Benchmark'
69         features['parameters'] = [THROUGHPUT, VLAN_SENDER, VLAN_RECEIVER]
70         features['allowed_values'] = dict()
71         features['allowed_values'][THROUGHPUT] = map(str, range(0, 100))
72         features['allowed_values'][VLAN_SENDER] = map(str, range(-1, 4096))
73         features['allowed_values'][VLAN_RECEIVER] = map(str, range(-1, 4096))
74         features['default_values'] = dict()
75         features['default_values'][THROUGHPUT] = '1'
76         features['default_values'][VLAN_SENDER] = '-1'
77         features['default_values'][VLAN_RECEIVER] = '-1'
78         return features
79
80     def run(self):
81         # Setup packet generator
82         traffic_time = '10'
83         packet_size = '512'
84         traffic_rate_percentage = self.params[THROUGHPUT]
85
86         dpdk_pktgen_vars = common.get_dpdk_pktgen_vars()
87         # bus_address = dpdk_pktgen_vars[cfs.CFSP_DPDK_BUS_SLOT_NIC_2]
88         self.interface_name = dpdk_pktgen_vars[cfs.CFSP_DPDK_NAME_IF_2]
89         packetgen = dpdk.DpdkPacketGenerator()
90         self._configure_lua_file(traffic_rate_percentage, traffic_time)
91         packetgen.init_dpdk_pktgen(dpdk_interfaces=1,
92                                    pcap_file_0='packet_' + packet_size +
93                                                '.pcap',
94                                    pcap_file_1='igmp.pcap',
95                                    lua_script='constant_traffic.lua',
96                                    vlan_0=self.params[VLAN_SENDER],
97                                    vlan_1=self.params[VLAN_RECEIVER])
98
99         self._init_packet_checker()
100         # Send constant traffic at a specified rate
101         common.LOG.debug('Start the packet generator')
102         packetgen.send_traffic()
103         common.LOG.debug('Stop the packet generator')
104         time.sleep(5)
105         self._finalize_packet_checker()
106         self._reset_lua_file(traffic_rate_percentage, traffic_time)
107         return self._get_results()
108
109     def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
110         """
111         Configure the packet gen to write the results into the right file
112         :return: None
113         """
114         common.replace_in_file(self.lua_file, 'local out_file = ""',
115                                'local out_file = "' +
116                                self.results_file + '"')
117         common.replace_in_file(self.lua_file, 'local traffic_rate = 0',
118                                'local traffic_rate = ' +
119                                traffic_rate_percentage)
120         common.replace_in_file(self.lua_file, 'local traffic_delay = 0',
121                                'local traffic_delay = ' + traffic_time)
122
123     def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
124         """
125         Configure the packet gen to write the results into the right file
126         :param traffic_rate_percentage:
127         :param traffic_time:
128         :return: None
129         """
130
131         common.replace_in_file(self.lua_file, 'local out_file = "' +
132                                self.results_file + '"',
133                                'local out_file = ""')
134         common.replace_in_file(self.lua_file, 'local traffic_rate = ' +
135                                traffic_rate_percentage,
136                                'local traffic_rate = 0')
137         common.replace_in_file(self.lua_file, 'local traffic_delay = ' +
138                                traffic_time, 'local traffic_delay = 0')
139
140     def _get_results(self):
141         ret_val = dict()
142         packet_checker_res = 0
143         if self.res_dir:
144             packet_checker_res = \
145                 int(common.get_file_first_line(self.res_dir +
146                                                '/packet_checker.res'))
147         pkt_gen_res = int(common.get_file_first_line(self.results_file))
148         if pkt_gen_res <= packet_checker_res or \
149            (float(pkt_gen_res - packet_checker_res) / pkt_gen_res) <= 0.1:
150             ret_val['failure'] = '0'
151         else:
152             ret_val['failure'] = '1'
153         return ret_val
154
155     def _init_packet_checker(self):
156         """
157         Sets up the multicast and starts the packet checker
158         :return:
159         """
160         # Kill any other process running from previous failed execution
161         self.res_dir = os.getcwd()
162         pids = self._get_pids()
163         for pid in pids:
164             os.kill(pid, signal.SIGTERM)
165
166         # initialization of the VLAN interface
167         command = "ip link add link "
168         command += self.interface_name
169         command += " name "
170         command += self.interface_name + '.' + self.params[VLAN_RECEIVER]
171         command += " type vlan id " + self.params[VLAN_RECEIVER]
172         common.run_command(command)
173
174         # set up the new
175         command = 'ifconfig ' + self.interface_name + '.' + \
176                   self.params[VLAN_RECEIVER]
177         # An IP address is required for the interface to receive a multicast
178         # flow. The specific address is not important
179         command += ' 10.254.254.254 up'
180         common.run_command(command)
181
182         # configure smcroute
183         command = "echo 'mgroup from "
184         command += self.interface_name + '.' + self.params[VLAN_RECEIVER]
185         command += " group "
186         command += MULTICAST_GROUP
187         command += "' > /etc/smcroute.conf"
188         common.run_command(command)
189
190         # run smcroute on the interface
191         command = 'smcroute -d'
192         common.run_command(command)
193
194         # Start the packet checker
195         command = self.pkt_checker_command
196         command += self.interface_name + '.' + self.params[VLAN_RECEIVER]
197         command += ' 128'
198         command += ' &'
199         common.run_command(command)
200
201     def _finalize_packet_checker(self):
202         """
203         Obtains the PID of the packet checker and sends an alarm to
204         terminate it
205         :return: None
206         """
207         pids = self._get_pids()
208         for pid in pids:
209             os.kill(pid, signal.SIGTERM)
210
211         # stop smcroute on the interface
212         command = 'smcroute -k'
213         common.run_command(command)
214
215         # finalization of the VLAN interface
216         command = "ip link delete "
217         command += self.interface_name + '.' + self.params[VLAN_RECEIVER]
218         common.run_command(command)
219
220     def _get_pids(self):
221         """
222         Returns a list of integers containing the pid or the pids of the
223         processes currently running on the host
224         :return: type: list of int
225         """
226         output = commands.getoutput("ps -ef |pgrep " +
227                                     PACKET_CHECKER_PROGRAM_NAME)
228         if not output:
229             pids = []
230         else:
231             pids = map(int, output.split('\n'))
232         return pids