summaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/networking/pktgen.py
blob: 8038cad607e5a2fb28a96b71bff900baeaca662f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
##############################################################################
# Copyright (c) 2015 Ericsson AB and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import pkg_resources
import logging
import json

import yardstick.ssh as ssh
from yardstick.benchmark.scenarios import base

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


class Pktgen(base.Scenario):
    """Executes a pktgen benchmark between two hosts"""
    __scenario_type__ = "Pktgen"

    TARGET_SCRIPT = 'pktgen_benchmark.bash'

    def __init__(self, context):
        self.context = context
        self.setup_done = False

    def setup(self):
        '''scenario setup'''
        self.target_script = pkg_resources.resource_filename(
            'yardstick.benchmark.scenarios.networking',
            Pktgen.TARGET_SCRIPT)
        user = self.context.get('user', 'ubuntu')
        host = self.context.get('host', None)
        target = self.context.get('target', None)
        key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')

        LOG.debug("user:%s, target:%s", user, target)
        self.server = ssh.SSH(user, target, key_filename=key_filename)
        self.server.wait(timeout=600)

        LOG.debug("user:%s, host:%s", user, host)
        self.client = ssh.SSH(user, host, key_filename=key_filename)
        self.client.wait(timeout=600)

        # copy script to host
        self.client.run("cat > ~/pktgen.sh",
                        stdin=open(self.target_script, "rb"))

        self.setup_done = True

    def _iptables_setup(self):
        """Setup iptables on server to monitor for received packets"""
        cmd = "sudo iptables -F; " \
              "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
              % (1000 + self.number_of_ports)
        LOG.debug("Executing command: %s", cmd)
        status, _, stderr = self.server.execute(cmd)
        if status:
            raise RuntimeError(stderr)

    def _iptables_get_result(self):
        """Get packet statistics from server"""
        cmd = "sudo iptables -L INPUT -vnx |" \
              "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
              % (1000 + self.number_of_ports)
        LOG.debug("Executing command: %s", cmd)
        status, stdout, stderr = self.server.execute(cmd)
        if status:
            raise RuntimeError(stderr)
        return int(stdout)

    def run(self, args):
        """execute the benchmark"""

        if not self.setup_done:
            self.setup()

        ipaddr = args.get("ipaddr", '127.0.0.1')

        options = args['options']
        packetsize = options.get("packetsize", 60)
        self.number_of_ports = options.get("number_of_ports", 10)

        self._iptables_setup()

        cmd = "sudo bash pktgen.sh %s %s %s" \
            % (ipaddr, self.number_of_ports, packetsize)
        LOG.debug("Executing command: %s", cmd)
        status, stdout, stderr = self.client.execute(cmd)

        if status:
            raise RuntimeError(stderr)

        data = json.loads(stdout)

        data['packets_received'] = self._iptables_get_result()

        if "sla" in args:
            sent = data['packets_sent']
            received = data['packets_received']
            ppm = 1000000 * (sent - received) / sent
            sla_max_ppm = int(args["sla"]["max_ppm"])
            assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
                % (ppm, sla_max_ppm)

        return data


def _test():
    '''internal test function'''
    key_filename = pkg_resources.resource_filename('yardstick.resources',
                                                   'files/yardstick_key')
    ctx = {'host': '172.16.0.137',
           'target': '172.16.0.138',
           'user': 'ubuntu',
           'key_filename': key_filename
           }

    logger = logging.getLogger('yardstick')
    logger.setLevel(logging.DEBUG)

    p = Pktgen(ctx)

    options = {'packetsize': 120}

    args = {'options': options,
            'ipaddr': '192.168.111.31'}
    result = p.run(args)
    print result

if __name__ == '__main__':
    _test()