Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / perf / netmap.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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
10 import time
11 import subprocess
12 import vstf.common.decorator as deco
13 from vstf.common.utils import kill_by_name, my_popen
14
15 import logging
16
17 LOG = logging.getLogger(__name__)
18
19
20 class Netmap(object):
21
22     def __init__(self):
23         self._send_processes = []
24         self._receive_processes = []
25
26     @deco.check("protocol", choices=['udp_bw'], defaults='udp_bw')
27     @deco.check("namespace", defaults=None)
28     @deco.check("dst")
29     @deco.check("src")
30     @deco.check("size", defaults=64)
31     @deco.check("threads", defaults=1)
32     @deco.check("ratep", defaults=0)
33     def send_start(self, **kwargs):
34         cmd = self.format_send_start(**kwargs)
35         LOG.info("cmd:%s", cmd)
36
37         process = my_popen(
38             cmd.split(),
39             stdout=subprocess.PIPE,
40             stderr=subprocess.PIPE)
41         self._send_processes.append(process)
42         time.sleep(0.5)
43
44         ret = process.poll()
45         if ret is None:
46             ret = 0
47             error_str = "start netmap send success"
48         else:
49             error_str = "start netmap send failed, %s" % (str(kwargs))
50             process.wait()
51             self._send_processes.remove(process)
52
53         return ret, error_str
54
55     def send_stop(self, **kwargs):
56         LOG.info("send_stop")
57         results = []
58         ret = 0
59         for process in self._send_processes:
60             process.kill()
61             process.wait()
62             error_str = "stop netmap send success"
63             results.append((ret, error_str))
64         self._send_processes = []
65         return results
66
67     def format_send_start(self, **kwargs):
68         cmd = "pkt-gen -i %(src_iface)s -f tx -l %(pkt_size)s -p %(threads)s -D %(dst_mac)s -R %(ratep)s"
69         context = {
70             'src_iface': kwargs['src'][0]['iface'],
71             'dst_mac': kwargs['dst'][0]['mac'],
72             'pkt_size': kwargs['size'],
73             'threads': kwargs['threads'],
74             'ratep': kwargs['ratep']
75         }
76         cmd = cmd % context
77         return cmd
78
79     @deco.namespace()
80     def format_receive_start(self, **kwargs):
81         cmd = "pkt-gen -i %(iface)s -f rx"
82         context = {
83             'iface': kwargs['dst'][0]['iface']
84         }
85         cmd = cmd % context
86         return cmd
87
88     @deco.check("protocol", choices=['udp_bw'], defaults='udp_bw')
89     @deco.check("namespace", defaults=None)
90     @deco.check("dst")
91     def receive_start(self, **kwargs):
92
93         cmd = self.format_receive_start(**kwargs)
94         LOG.info("cmd:%s", cmd)
95
96         process = my_popen(
97             cmd.split(),
98             stdout=subprocess.PIPE,
99             stderr=subprocess.PIPE)
100         self._receive_processes.append(process)
101         time.sleep(0.5)
102
103         ret = process.poll()
104         if ret is None:
105             ret = 0
106             error_str = "start netmap receive success"
107         else:
108             error_str = "start netmap receive failed, %s" % (str(kwargs))
109             process.wait()
110             self._receive_processes.remove(process)
111
112         return ret, error_str
113
114     def receive_stop(self, **kwargs):
115         LOG.info("receive_stop")
116         ret = 0
117         for process in self._receive_processes:
118             process.kill()
119             process.wait()
120         self._receive_processes = []
121         error_str = "stop netmap receive success"
122         self._receive_processes = []
123         return ret, error_str
124
125     def clean(self):
126         self.send_stop()
127         self.receive_stop()
128         return True
129
130     def force_clean(self):
131         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
132         kill_by_name('pkt-gen')
133         self._send_processes = []
134         self._receive_processes = []
135         return True
136
137
138 def unit_test():
139     perf = Netmap()
140     receive = {
141         "protocol": "udp_bw",
142         # "namespace": "receive",
143         "dst": [
144             {"iface": "p57p2"}
145         ],
146     }
147     ret = perf.receive_start(**receive)
148     LOG.info("*********receive_start***********")
149     LOG.info("ret")
150     send = {
151         # "namespace": "send",
152         "protocol": "udp_bw",
153         "src": [
154             {"iface": "eth4", "mac": "90:e2:ba:20:1f:d8"}
155         ],
156         "dst": [
157             {"mac": "90:e2:ba:20:1f:d9"}
158         ],
159         "size": 64,
160         "threads": 1,
161         "ratep": 0
162     }
163     print perf.send_start(**send)
164     print perf._send_processes
165     time.sleep(10)
166
167     print perf.send_stop()
168     print perf.receive_stop()
169
170
171 if __name__ == "__main__":
172     from vstf.common.log import setup_logging
173
174     setup_logging(
175         level=logging.DEBUG,
176         log_file="/var/log/vstf/vstf-netmap.log",
177         clevel=logging.INFO)
178     unit_test()