503ea97e103301579b9ac767773ddf5116877a75
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen_dpdk.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE corporation 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 time
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17
18
19 class PktgenDPDKLatency(base.Scenario):
20     """Execute pktgen-dpdk on one vm and execute testpmd on the other vm
21
22   Parameters
23     packetsize - packet size in bytes without the CRC
24         type:    int
25         unit:    bytes
26         default: 64
27     """
28     __scenario_type__ = "PktgenDPDKLatency"
29
30     PKTGEN_DPDK_SCRIPT = 'pktgen_dpdk_latency_benchmark.bash'
31     TESTPMD_SCRIPT = 'testpmd_fwd.bash'
32
33     def __init__(self, scenario_cfg, context_cfg):
34         self.scenario_cfg = scenario_cfg
35         self.context_cfg = context_cfg
36         self.setup_done = False
37
38     def setup(self):
39         '''scenario setup'''
40         self.pktgen_dpdk_script = pkg_resources.resource_filename(
41             'yardstick.benchmark.scenarios.networking',
42             PktgenDPDKLatency.PKTGEN_DPDK_SCRIPT)
43         self.testpmd_script = pkg_resources.resource_filename(
44             'yardstick.benchmark.scenarios.networking',
45             PktgenDPDKLatency.TESTPMD_SCRIPT)
46         host = self.context_cfg['host']
47         host_user = host.get('user', 'ubuntu')
48         host_ssh_port = host.get('ssh_port', ssh.DEFAULT_PORT)
49         host_ip = host.get('ip', None)
50         host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
51         target = self.context_cfg['target']
52         target_user = target.get('user', 'ubuntu')
53         target_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
54         target_ip = target.get('ip', None)
55         target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
56         LOG.info("user:%s, target:%s", target_user, target_ip)
57         self.server = ssh.SSH(target_user, target_ip,
58                               key_filename=target_key_filename,
59                               port=target_ssh_port)
60         self.server.wait(timeout=600)
61
62         # copy script to host
63         self.server._put_file_shell(self.testpmd_script, '~/testpmd_fwd.sh')
64
65         LOG.info("user:%s, host:%s", host_user, host_ip)
66         self.client = ssh.SSH(host_user, host_ip,
67                               key_filename=host_key_filename,
68                               port=host_ssh_port)
69         self.client.wait(timeout=600)
70
71         # copy script to host
72         self.client._put_file_shell(
73             self.pktgen_dpdk_script, '~/pktgen_dpdk.sh')
74
75         self.setup_done = True
76         self.testpmd_args = ''
77         self.pktgen_args = []
78
79     @staticmethod
80     def get_port_mac(sshclient, port):
81         cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port
82         LOG.debug("Executing command: %s", cmd)
83         status, stdout, stderr = sshclient.execute(cmd)
84
85         if status:
86             raise RuntimeError(stderr)
87         else:
88             return stdout.rstrip()
89
90     @staticmethod
91     def get_port_ip(sshclient, port):
92         cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' \
93             |cut -d ':' -f2 " % port
94         LOG.debug("Executing command: %s", cmd)
95         status, stdout, stderr = sshclient.execute(cmd)
96
97         if status:
98             raise RuntimeError(stderr)
99         else:
100             return stdout.rstrip()
101
102     def run(self, result):
103         """execute the benchmark"""
104
105         if not self.setup_done:
106             self.setup()
107
108         if not self.testpmd_args:
109             self.testpmd_args = self.get_port_mac(self.client, 'eth2')
110
111         if not self.pktgen_args:
112             server_rev_mac = self.get_port_mac(self.server, 'eth1')
113             server_send_mac = self.get_port_mac(self.server, 'eth2')
114             client_src_ip = self.get_port_ip(self.client, 'eth1')
115             client_dst_ip = self.get_port_ip(self.client, 'eth2')
116
117             self.pktgen_args = [client_src_ip, client_dst_ip,
118                                 server_rev_mac, server_send_mac]
119
120         options = self.scenario_cfg['options']
121         packetsize = options.get("packetsize", 64)
122         rate = options.get("rate", 100)
123
124         cmd = "screen sudo -E bash ~/testpmd_fwd.sh %s " % (self.testpmd_args)
125         LOG.debug("Executing command: %s", cmd)
126         self.server.send_command(cmd)
127
128         time.sleep(1)
129
130         cmd = "screen sudo -E bash ~/pktgen_dpdk.sh %s %s %s %s %s %s" % \
131             (self.pktgen_args[0], self.pktgen_args[1], self.pktgen_args[2],
132              self.pktgen_args[3], rate, packetsize)
133         LOG.debug("Executing command: %s", cmd)
134         self.client.send_command(cmd)
135
136         # wait for finishing test
137         time.sleep(1)
138
139         cmd = "cat ~/result.log -vT \
140                |awk '{match($0,/\[8;40H +[0-9]+/)} \
141                {print substr($0,RSTART,RLENGTH)}' \
142                |grep -v ^$ |awk '{if ($2 != 0) print $2}'"
143         client_status, client_stdout, client_stderr = self.client.execute(cmd)
144
145         if client_status:
146             raise RuntimeError(client_stderr)
147
148         avg_latency = 0
149         if client_stdout:
150             latency_list = client_stdout.split('\n')[0:-2]
151             LOG.info("10 samples of latency: %s", latency_list)
152             latency_sum = 0
153             for i in latency_list:
154                 latency_sum += int(i)
155             avg_latency = latency_sum / len(latency_list)
156
157         result.update({"avg_latency": avg_latency})
158
159         if avg_latency and "sla" in self.scenario_cfg:
160             sla_max_latency = int(self.scenario_cfg["sla"]["max_latency"])
161             LOG.info("avg_latency : %d ", avg_latency)
162             LOG.info("sla_max_latency: %d", sla_max_latency)
163             debug_info = "avg_latency %d > sla_max_latency %d" \
164                 % (avg_latency, sla_max_latency)
165             assert avg_latency <= sla_max_latency, debug_info