Add support to list and show runners & scenarios
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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 import pkg_resources
10 import logging
11 import json
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17 LOG.setLevel(logging.DEBUG)
18
19
20 class Pktgen(base.Scenario):
21     """Execute pktgen between two hosts
22
23   Parameters
24     TBD
25     """
26     __scenario_type__ = "Pktgen"
27
28     TARGET_SCRIPT = 'pktgen_benchmark.bash'
29
30     def __init__(self, context):
31         self.context = context
32         self.setup_done = False
33
34     def setup(self):
35         '''scenario setup'''
36         self.target_script = pkg_resources.resource_filename(
37             'yardstick.benchmark.scenarios.networking',
38             Pktgen.TARGET_SCRIPT)
39         user = self.context.get('user', 'ubuntu')
40         host = self.context.get('host', None)
41         target = self.context.get('target', None)
42         key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
43
44         LOG.debug("user:%s, target:%s", user, target)
45         self.server = ssh.SSH(user, target, key_filename=key_filename)
46         self.server.wait(timeout=600)
47
48         LOG.debug("user:%s, host:%s", user, host)
49         self.client = ssh.SSH(user, host, key_filename=key_filename)
50         self.client.wait(timeout=600)
51
52         # copy script to host
53         self.client.run("cat > ~/pktgen.sh",
54                         stdin=open(self.target_script, "rb"))
55
56         self.setup_done = True
57
58     def _iptables_setup(self):
59         """Setup iptables on server to monitor for received packets"""
60         cmd = "sudo iptables -F; " \
61               "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
62               % (1000 + self.number_of_ports)
63         LOG.debug("Executing command: %s", cmd)
64         status, _, stderr = self.server.execute(cmd)
65         if status:
66             raise RuntimeError(stderr)
67
68     def _iptables_get_result(self):
69         """Get packet statistics from server"""
70         cmd = "sudo iptables -L INPUT -vnx |" \
71               "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
72               % (1000 + self.number_of_ports)
73         LOG.debug("Executing command: %s", cmd)
74         status, stdout, stderr = self.server.execute(cmd)
75         if status:
76             raise RuntimeError(stderr)
77         return int(stdout)
78
79     def run(self, args):
80         """execute the benchmark"""
81
82         if not self.setup_done:
83             self.setup()
84
85         ipaddr = args.get("ipaddr", '127.0.0.1')
86
87         options = args['options']
88         packetsize = options.get("packetsize", 60)
89         self.number_of_ports = options.get("number_of_ports", 10)
90
91         self._iptables_setup()
92
93         cmd = "sudo bash pktgen.sh %s %s %s" \
94             % (ipaddr, self.number_of_ports, packetsize)
95         LOG.debug("Executing command: %s", cmd)
96         status, stdout, stderr = self.client.execute(cmd)
97
98         if status:
99             raise RuntimeError(stderr)
100
101         data = json.loads(stdout)
102
103         data['packets_received'] = self._iptables_get_result()
104
105         if "sla" in args:
106             sent = data['packets_sent']
107             received = data['packets_received']
108             ppm = 1000000 * (sent - received) / sent
109             sla_max_ppm = int(args["sla"]["max_ppm"])
110             assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
111                 % (ppm, sla_max_ppm)
112
113         return data
114
115
116 def _test():
117     '''internal test function'''
118     key_filename = pkg_resources.resource_filename('yardstick.resources',
119                                                    'files/yardstick_key')
120     ctx = {'host': '172.16.0.137',
121            'target': '172.16.0.138',
122            'user': 'ubuntu',
123            'key_filename': key_filename
124            }
125
126     logger = logging.getLogger('yardstick')
127     logger.setLevel(logging.DEBUG)
128
129     p = Pktgen(ctx)
130
131     options = {'packetsize': 120}
132
133     args = {'options': options,
134             'ipaddr': '192.168.111.31'}
135     result = p.run(args)
136     print result
137
138 if __name__ == '__main__':
139     _test()