1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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 ##############################################################################
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
19 class Pktgen(base.Scenario):
20 """Execute pktgen between two hosts
23 packetsize - packet size in bytes without the CRC
27 number_of_ports - number of UDP ports to test
31 duration - duration of the test
36 __scenario_type__ = "Pktgen"
38 TARGET_SCRIPT = 'pktgen_benchmark.bash'
40 def __init__(self, scenario_cfg, context_cfg):
41 self.scenario_cfg = scenario_cfg
42 self.context_cfg = context_cfg
43 self.setup_done = False
47 self.target_script = pkg_resources.resource_filename(
48 'yardstick.benchmark.scenarios.networking',
50 host = self.context_cfg['host']
51 host_user = host.get('user', 'ubuntu')
52 host_ip = host.get('ip', None)
53 host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
54 target = self.context_cfg['target']
55 target_user = target.get('user', 'ubuntu')
56 target_ip = target.get('ip', None)
57 target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
59 LOG.info("user:%s, target:%s", target_user, target_ip)
60 self.server = ssh.SSH(target_user, target_ip,
61 key_filename=target_key_filename)
62 self.server.wait(timeout=600)
64 LOG.info("user:%s, host:%s", host_user, host_ip)
65 self.client = ssh.SSH(host_user, host_ip,
66 key_filename=host_key_filename)
67 self.client.wait(timeout=600)
70 self.client.run("cat > ~/pktgen.sh",
71 stdin=open(self.target_script, "rb"))
73 self.setup_done = True
75 def _iptables_setup(self):
76 """Setup iptables on server to monitor for received packets"""
77 cmd = "sudo iptables -F; " \
78 "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
79 % (1000 + self.number_of_ports)
80 LOG.debug("Executing command: %s", cmd)
81 status, _, stderr = self.server.execute(cmd)
83 raise RuntimeError(stderr)
85 def _iptables_get_result(self):
86 """Get packet statistics from server"""
87 cmd = "sudo iptables -L INPUT -vnx |" \
88 "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
89 % (1000 + self.number_of_ports)
90 LOG.debug("Executing command: %s", cmd)
91 status, stdout, stderr = self.server.execute(cmd)
93 raise RuntimeError(stderr)
96 def run(self, result):
97 """execute the benchmark"""
99 if not self.setup_done:
102 ipaddr = self.context_cfg["target"].get("ipaddr", '127.0.0.1')
104 options = self.scenario_cfg['options']
105 packetsize = options.get("packetsize", 60)
106 self.number_of_ports = options.get("number_of_ports", 10)
107 # if run by a duration runner
108 duration_time = self.scenario_cfg["runner"].get("duration", None) \
109 if "runner" in self.scenario_cfg else None
110 # if run by an arithmetic runner
111 arithmetic_time = options.get("duration", None)
114 duration = duration_time
115 elif arithmetic_time:
116 duration = arithmetic_time
120 self._iptables_setup()
122 cmd = "sudo bash pktgen.sh %s %s %s %s" \
123 % (ipaddr, self.number_of_ports, packetsize, duration)
124 LOG.debug("Executing command: %s", cmd)
125 status, stdout, stderr = self.client.execute(cmd)
128 raise RuntimeError(stderr)
130 result.update(json.loads(stdout))
132 result['packets_received'] = self._iptables_get_result()
134 if "sla" in self.scenario_cfg:
135 sent = result['packets_sent']
136 received = result['packets_received']
137 ppm = 1000000 * (sent - received) / sent
138 sla_max_ppm = int(self.scenario_cfg["sla"]["max_ppm"])
139 assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d; " \
144 '''internal test function'''
145 key_filename = pkg_resources.resource_filename('yardstick.resources',
146 'files/yardstick_key')
149 'ip': '10.229.47.137',
151 'key_filename': key_filename
154 'ip': '10.229.47.137',
156 'key_filename': key_filename,
157 'ipaddr': '10.229.47.137',
161 logger = logging.getLogger('yardstick')
162 logger.setLevel(logging.DEBUG)
164 options = {'packetsize': 120}
165 args = {'options': options}
168 p = Pktgen(args, ctx)
172 if __name__ == '__main__':