1 ##############################################################################
2 # Copyright (c) 2017 Nokia and others.
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
15 import yardstick.ssh as ssh
16 import yardstick.common.utils as utils
17 from yardstick.benchmark.scenarios import base
19 LOG = logging.getLogger(__name__)
22 class PktgenDPDK(base.Scenario):
23 """Execute pktgen-dpdk on one vm and execute testpmd on the other vm
25 __scenario_type__ = "PktgenDPDK"
27 PKTGEN_DPDK_SCRIPT = 'pktgen_dpdk_benchmark.bash'
28 TESTPMD_SCRIPT = 'testpmd_rev.bash'
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
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)
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')
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,
68 self.server.wait(timeout=600)
71 self.server._put_file_shell(self.testpmd_script, '~/testpmd_rev.sh')
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,
77 self.client.wait(timeout=600)
80 self.client._put_file_shell(self.pktgen_dpdk_script,
83 self.setup_done = True
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)
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)
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])
102 cmd = "cat ~/.pktgen-dpdk.ipaddr.eth1"
103 status, stdout, stderr = self.client.execute(cmd)
105 raise RuntimeError(stderr)
106 self.source_ipaddr[1] = stdout
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)
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])
120 cmd = "screen sudo -E bash ~/testpmd_rev.sh"
121 LOG.debug("Executing command: %s", cmd)
122 self.server.send_command(cmd)
126 cmd = "cat ~/.testpmd.ipaddr.eth1"
127 status, stdout, stderr = self.server.execute(cmd)
129 raise RuntimeError(stderr)
130 self.target_ipaddr[1] = stdout
132 cmd = "cat ~/.testpmd.macaddr.eth1"
133 status, stdout, stderr = self.server.execute(cmd)
135 raise RuntimeError(stderr)
136 self.target_macaddr[1] = stdout
138 self.dpdk_setup_done = True
140 def _is_dpdk_setup(self, host):
141 """Is dpdk already setup in the host?"""
143 cmd = "ip a | grep eth1 2>/dev/null"
144 LOG.debug("Executing command: %s in %s", cmd, host)
146 status, stdout, stderr = self.server.execute(cmd)
150 status, stdout, stderr = self.client.execute(cmd)
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 | \
161 LOG.debug("Executing command: %s", cmd)
162 status, stdout, stderr = self.server.execute(cmd)
164 raise RuntimeError(stderr)
165 received = int(stdout)
167 cmd = "sudo /dpdk/destdir/bin/dpdk-procinfo -- --stats-reset" \
169 self.server.execute(cmd)
171 self.server.execute(cmd)
174 def run(self, result):
175 """execute the benchmark"""
177 if not self.setup_done:
180 if not self.dpdk_setup_done:
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)
194 duration = duration_time
195 elif arithmetic_time:
196 duration = arithmetic_time
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)
205 LOG.debug("Executing command: %s", cmd)
206 status, stdout, stderr = self.client.execute(cmd)
209 raise RuntimeError(stderr)
211 result.update(json.loads(stdout))
213 result['packets_received'] = self._dpdk_get_result()
215 result['packetsize'] = packetsize
217 if "sla" in self.scenario_cfg:
218 sent = result['packets_sent']
219 received = result['packets_received']
220 ppm = 1000000 * (sent - received) / sent
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; " \