Merge "NSB: fix port topology"
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen_dpdk_throughput.py
1 ##############################################################################
2 # Copyright (c) 2017 Nokia and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import pkg_resources
11 import logging
12 import json
13 import time
14
15 import yardstick.ssh as ssh
16 import yardstick.common.utils as utils
17 from yardstick.benchmark.scenarios import base
18
19 LOG = logging.getLogger(__name__)
20
21
22 class PktgenDPDK(base.Scenario):
23     """Execute pktgen-dpdk on one vm and execute testpmd on the other vm
24     """
25     __scenario_type__ = "PktgenDPDK"
26
27     PKTGEN_DPDK_SCRIPT = 'pktgen_dpdk_benchmark.bash'
28     TESTPMD_SCRIPT = 'testpmd_rev.bash'
29
30     def __init__(self, scenario_cfg, context_cfg):
31         self.scenario_cfg = scenario_cfg
32         self.context_cfg = context_cfg
33         self.source_ipaddr = [None] * 2
34         self.source_ipaddr[0] = \
35             self.context_cfg["host"].get("ipaddr", '127.0.0.1')
36         self.target_ipaddr = [None] * 2
37         self.target_ipaddr[0] = \
38             self.context_cfg["target"].get("ipaddr", '127.0.0.1')
39         self.target_macaddr = [None] * 2
40         self.setup_done = False
41         self.dpdk_setup_done = False
42
43     def setup(self):
44         """scenario setup"""
45
46         self.pktgen_dpdk_script = pkg_resources.resource_filename(
47             'yardstick.benchmark.scenarios.networking',
48             PktgenDPDK.PKTGEN_DPDK_SCRIPT)
49         self.testpmd_script = pkg_resources.resource_filename(
50             'yardstick.benchmark.scenarios.networking',
51             PktgenDPDK.TESTPMD_SCRIPT)
52
53         host = self.context_cfg['host']
54         host_user = host.get('user', 'ubuntu')
55         host_ssh_port = host.get('ssh_port', ssh.DEFAULT_PORT)
56         host_ip = host.get('ip', None)
57         host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
58         target = self.context_cfg['target']
59         target_user = target.get('user', 'ubuntu')
60         target_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
61         target_ip = target.get('ip', None)
62         target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
63
64         LOG.info("user:%s, target:%s", target_user, target_ip)
65         self.server = ssh.SSH(target_user, target_ip,
66                               key_filename=target_key_filename,
67                               port=target_ssh_port)
68         self.server.wait(timeout=600)
69
70         # copy script to host
71         self.server._put_file_shell(self.testpmd_script, '~/testpmd_rev.sh')
72
73         LOG.info("user:%s, host:%s", host_user, host_ip)
74         self.client = ssh.SSH(host_user, host_ip,
75                               key_filename=host_key_filename,
76                               port=host_ssh_port)
77         self.client.wait(timeout=600)
78
79         # copy script to host
80         self.client._put_file_shell(self.pktgen_dpdk_script,
81                                     '~/pktgen_dpdk.sh')
82
83         self.setup_done = True
84
85     def dpdk_setup(self):
86         """dpdk setup"""
87
88         # disable Address Space Layout Randomization (ASLR)
89         cmd = "echo 0 | sudo tee /proc/sys/kernel/randomize_va_space"
90         self.server.send_command(cmd)
91         self.client.send_command(cmd)
92
93         if not self._is_dpdk_setup("client"):
94             cmd = "sudo ifup eth1"
95             LOG.debug("Executing command: %s", cmd)
96             self.client.send_command(cmd)
97             time.sleep(1)
98             self.source_ipaddr[1] = utils.get_port_ip(self.client, 'eth1')
99             self.client.run("tee ~/.pktgen-dpdk.ipaddr.eth1 > /dev/null",
100                             stdin=self.source_ipaddr[1])
101         else:
102             cmd = "cat ~/.pktgen-dpdk.ipaddr.eth1"
103             status, stdout, stderr = self.client.execute(cmd)
104             if status:
105                 raise RuntimeError(stderr)
106             self.source_ipaddr[1] = stdout
107
108         if not self._is_dpdk_setup("server"):
109             cmd = "sudo ifup eth1"
110             LOG.debug("Executing command: %s", cmd)
111             self.server.send_command(cmd)
112             time.sleep(1)
113             self.target_ipaddr[1] = utils.get_port_ip(self.server, 'eth1')
114             self.target_macaddr[1] = utils.get_port_mac(self.server, 'eth1')
115             self.server.run("tee ~/.testpmd.ipaddr.eth1 > /dev/null",
116                             stdin=self.target_ipaddr[1])
117             self.server.run("tee ~/.testpmd.macaddr.eth1 > /dev/null",
118                             stdin=self.target_macaddr[1])
119
120             cmd = "screen sudo -E bash ~/testpmd_rev.sh"
121             LOG.debug("Executing command: %s", cmd)
122             self.server.send_command(cmd)
123
124             time.sleep(1)
125         else:
126             cmd = "cat ~/.testpmd.ipaddr.eth1"
127             status, stdout, stderr = self.server.execute(cmd)
128             if status:
129                 raise RuntimeError(stderr)
130             self.target_ipaddr[1] = stdout
131
132             cmd = "cat ~/.testpmd.macaddr.eth1"
133             status, stdout, stderr = self.server.execute(cmd)
134             if status:
135                 raise RuntimeError(stderr)
136             self.target_macaddr[1] = stdout
137
138         self.dpdk_setup_done = True
139
140     def _is_dpdk_setup(self, host):
141         """Is dpdk already setup in the host?"""
142         is_run = True
143         cmd = "ip a | grep eth1 2>/dev/null"
144         LOG.debug("Executing command: %s in %s", cmd, host)
145         if "server" in host:
146             status, stdout, stderr = self.server.execute(cmd)
147             if stdout:
148                 is_run = False
149         else:
150             status, stdout, stderr = self.client.execute(cmd)
151             if stdout:
152                 is_run = False
153
154         return is_run
155
156     def _dpdk_get_result(self):
157         """Get packet statistics from server"""
158         cmd = "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats 2>/dev/null | \
159               awk '$1 ~ /RX-packets/' | cut -d ':' -f2  | cut -d ' ' -f2 | \
160               head -n 1"
161         LOG.debug("Executing command: %s", cmd)
162         status, stdout, stderr = self.server.execute(cmd)
163         if status:
164             raise RuntimeError(stderr)
165         received = int(stdout)
166
167         cmd = "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset" \
168             " > /dev/null 2>&1"
169         self.server.execute(cmd)
170         time.sleep(1)
171         self.server.execute(cmd)
172         return received
173
174     def run(self, result):
175         """execute the benchmark"""
176
177         if not self.setup_done:
178             self.setup()
179
180         if not self.dpdk_setup_done:
181             self.dpdk_setup()
182
183         options = self.scenario_cfg['options']
184         packetsize = options.get("packetsize", 60)
185         rate = options.get("rate", 100)
186         self.number_of_ports = options.get("number_of_ports", 10)
187         # if run by a duration runner
188         duration_time = self.scenario_cfg["runner"].get("duration", None) \
189             if "runner" in self.scenario_cfg else None
190         # if run by an arithmetic runner
191         arithmetic_time = options.get("duration", None)
192
193         if duration_time:
194             duration = duration_time
195         elif arithmetic_time:
196             duration = arithmetic_time
197         else:
198             duration = 20
199
200         cmd = "sudo bash pktgen_dpdk.sh %s %s %s %s %s %s %s" \
201             % (self.source_ipaddr[1],
202                self.target_ipaddr[1], self.target_macaddr[1],
203                self.number_of_ports, packetsize, duration, rate)
204
205         LOG.debug("Executing command: %s", cmd)
206         status, stdout, stderr = self.client.execute(cmd)
207
208         if status:
209             raise RuntimeError(stderr)
210
211         result.update(json.loads(stdout))
212
213         result['packets_received'] = self._dpdk_get_result()
214
215         result['packetsize'] = packetsize
216
217         if "sla" in self.scenario_cfg:
218             sent = result['packets_sent']
219             received = result['packets_received']
220             ppm = 1000000 * (sent - received) / sent
221             # Added by Jing
222             ppm += (sent - received) % sent > 0
223             LOG.debug("Lost packets %d - Lost ppm %d", (sent - received), ppm)
224             sla_max_ppm = int(self.scenario_cfg["sla"]["max_ppm"])
225             assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d; " \
226                 % (ppm, sla_max_ppm)