summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/helper-scripts/rapid/prox_ctrl.py
blob: ba21913c881037ee480eaa8e89148084def0a60a (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
##
## Copyright (c) 2010-2019 Intel Corporation
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##

from __future__ import print_function

import os
import subprocess
import socket

class prox_ctrl(object):
    def __init__(self, ip, key=None, user=None):
        self._ip   = ip
        self._key  = key
        self._user = user
        self._children = []
        self._proxsock = []

    def ip(self):
        return self._ip

    def connect(self):
        """Simply try to run 'true' over ssh on remote system.
        On failure, raise RuntimeWarning exception when possibly worth
        retrying, and raise RuntimeError exception otherwise.
        """
        return self.run_cmd('true', True)

    def close(self):
        """Must be called before program termination."""
#        for prox in self._proxsock:
#            prox.quit()
        children = len(self._children)
        if children == 0:
            return
        if children > 1:
            print('Waiting for %d child processes to complete ...' % children)
        for child in self._children:
            ret = os.waitpid(child[0], os.WNOHANG)
            if ret[0] == 0:
                print("Waiting for child process '%s' to complete ..." % child[1])
                ret = os.waitpid(child[0], 0)
            rc = ret[1]
            if os.WIFEXITED(rc):
                if os.WEXITSTATUS(rc) == 0:
                    print("Child process '%s' completed successfully" % child[1])
                else:
                    print("Child process '%s' returned exit status %d" % (
                            child[1], os.WEXITSTATUS(rc)))
            elif os.WIFSIGNALED(rc):
                print("Child process '%s' exited on signal %d" % (
                        child[1], os.WTERMSIG(rc)))
            else:
                print("Wait status for child process '%s' is 0x%04x" % (
                        child[1], rc))

    def run_cmd(self, command, _connect=False):
        """Execute command over ssh on remote system.
        Wait for remote command completion.
        Return command output (combined stdout and stderr).
        _connect argument is reserved for connect() method.
        """
        cmd = self._build_ssh(command)
        try:
            return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as ex:
            if _connect and ex.returncode == 255:
                raise RuntimeWarning(ex.output.strip())
            raise RuntimeError('ssh returned exit status %d:\n%s'
                    % (ex.returncode, ex.output.strip()))

    def fork_cmd(self, command, name=None):
        """Execute command over ssh on remote system, in a child process.
        Do not wait for remote command completion.
        Return child process id.
        """
        if name is None:
            name = command
        cmd = self._build_ssh(command)
        pid = os.fork()
        if (pid != 0):
            # In the parent process
            self._children.append((pid, name))
            return pid
        # In the child process: use os._exit to terminate
        try:
            # Actually ignore output on success, but capture stderr on failure
            subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as ex:
            raise RuntimeError("Child process '%s' failed:\n"
                    'ssh returned exit status %d:\n%s'
                    % (name, ex.returncode, ex.output.strip()))
        os._exit(0)

    def prox_sock(self, port=8474):
        """Connect to the PROX instance on remote system.
        Return a prox_sock object on success, None on failure.
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect((self._ip, port))
            prox = prox_sock(sock)
            self._proxsock.append(prox)
            return prox
        except:
            return None

    def scp_put(self, src, dst):
        """Copy src file from local system to dst on remote system."""
        cmd = [ 'scp',
                '-B',
                '-oStrictHostKeyChecking=no',
                '-oUserKnownHostsFile=/dev/null',
                '-oLogLevel=ERROR' ]
        if self._key is not None:
            cmd.extend(['-i', self._key])
        cmd.append(src)
        remote = ''
        if self._user is not None:
            remote += self._user + '@'
        remote += self._ip + ':' + dst
        cmd.append(remote)
        try:
            # Actually ignore output on success, but capture stderr on failure
            subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as ex:
            raise RuntimeError('scp returned exit status %d:\n%s'
                    % (ex.returncode, ex.output.strip()))

    def _build_ssh(self, command):
        cmd = [ 'ssh',
                '-oBatchMode=yes',
                '-oStrictHostKeyChecking=no',
                '-oUserKnownHostsFile=/dev/null',
                '-oLogLevel=ERROR' ]
        if self._key is not None:
            cmd.extend(['-i', self._key])
        remote = ''
        if self._user is not None:
            remote += self._user + '@'
        remote += self._ip
        cmd.append(remote)
        cmd.append(command)
        return cmd

class prox_sock(object):
    def __init__(self, sock):
        self._sock = sock
        self._rcvd = b''

    def quit(self):
        if self._sock is not None:
            self._send('quit')
            self._sock.close()
            self._sock = None

    def start(self, cores):
        self._send('start %s' % ','.join(map(str, cores)))

    def stop(self, cores):
        self._send('stop %s' % ','.join(map(str, cores)))

    def speed(self, speed, cores, tasks=[0]):
        for core in cores:
        	for task in tasks:
			self._send('speed %s %s %s' % (core, task, speed))

    def reset_stats(self):
        self._send('reset stats')

    def lat_stats(self, cores, tasks=[0]):
        min_lat = 999999999
	max_lat = avg_lat = 0
	number_tasks_returning_stats = 0
	buckets = [0] * 128
        self._send('lat all stats %s %s' % (','.join(map(str, cores)), ','.join(map(str, tasks))))
        for core in cores:
	    	for task in tasks:
	        	stats = self._recv().split(',')
			if 'is not measuring' in stats[0]:
				continue
			if stats[0].startswith('error'):
				log.critical("lat stats error: unexpected reply from PROX (potential incompatibility between scripts and PROX)")
				raise Exception("lat stats error")
			number_tasks_returning_stats += 1
			min_lat = min(int(stats[0]),min_lat)
			max_lat = max(int(stats[1]),max_lat)
			avg_lat += int(stats[2])
			#min_since begin = int(stats[3])
			#max_since_begin = int(stats[4])
			tsc = int(stats[5]) # Taking the last tsc as the timestamp since PROX will return the same tsc for each core/task combination 
			hz = int(stats[6])
			#coreid = int(stats[7])
			#taskid = int(stats[8])
	        	stats = self._recv().split(':')
			if stats[0].startswith('error'):
				log.critical("lat stats error: unexpected lat bucket reply (potential incompatibility between scripts and PROX)")
				raise Exception("lat bucket reply error")
			buckets[0] = int(stats[1])
			for i in range(1, 128):
	        		stats = self._recv().split(':')
				buckets[i] = int(stats[1])
        avg_lat = avg_lat/number_tasks_returning_stats
        self._send('stats latency(0).used')
        used = float(self._recv())
        self._send('stats latency(0).total')
        total = float(self._recv())
        return min_lat, max_lat, avg_lat, (used/total), tsc, hz, buckets

    def old_lat_stats(self, cores, tasks=[0]):
        min_lat = 999999999
	max_lat = avg_lat = 0
	number_tasks_returning_stats = 0
	buckets = [0] * 128
        self._send('lat stats %s %s' % (','.join(map(str, cores)), ','.join(map(str, tasks))))
        for core in cores:
	    	for task in tasks:
	        	stats = self._recv().split(',')
			if stats[0].startswith('error'):
				if stats[0].startswith('error: invalid syntax'):
		                	log.critical("lat stats error: unexpected invalid syntax (potential incompatibility between scripts and PROX)")
			                raise Exception("lat stats error")
				continue
			number_tasks_returning_stats += 1
			min_lat = min(int(stats[0]),min_lat)
			max_lat = max(int(stats[1]),max_lat)
			avg_lat += int(stats[2])
			#min_since begin = int(stats[3])
			#max_since_begin = int(stats[4])
			tsc = int(stats[5])
			hz = int(stats[6])
			#coreid = int(stats[7])
			#taskid = int(stats[8])
        avg_lat = avg_lat/number_tasks_returning_stats
        self._send('stats latency(0).used')
        used = float(self._recv())
        self._send('stats latency(0).total')
        total = float(self._recv())
        return min_lat, max_lat, avg_lat, (used/total), tsc, hz, buckets

    def irq_stats(self, core, bucket, task=0):
        self._send('stats task.core(%s).task(%s).irq(%s)' % (core, task, bucket))
        stats = self._recv().split(',')
        return int(stats[0])

    def show_irq_buckets(self, core, task=0):
        rx = tx = drop = tsc = hz = 0
        self._send('show irq buckets %s %s' % (core,task))
        buckets = self._recv().split(';')
	buckets = buckets[:-1]
        return buckets

    def core_stats(self, cores, tasks=[0]):
        rx = tx = drop = tsc = hz = rx_non_dp = tx_non_dp = tx_fail = 0
        self._send('dp core stats %s %s' % (','.join(map(str, cores)), ','.join(map(str, tasks))))
        for core in cores:
        	for task in tasks:
			stats = self._recv().split(',')
			if stats[0].startswith('error'):  
				if stats[0].startswith('error: invalid syntax'):
					log.critical("dp core stats error: unexpected invalid syntax (potential incompatibility between scripts and PROX)")
					raise Exception("dp core stats error")
				continue
			rx += int(stats[0])
			tx += int(stats[1])
			rx_non_dp += int(stats[2])
			tx_non_dp += int(stats[3])
			drop += int(stats[4])
			tx_fail += int(stats[5])
			tsc = int(stats[6])
			hz = int(stats[7])
        return rx, rx_non_dp, tx, tx_non_dp, drop, tx_fail, tsc, hz

    def multi_port_stats(self, ports=[0]):
        rx = tx = port_id = tsc = no_mbufs = errors = 0
        self._send('multi port stats %s' % (','.join(map(str, ports))))
	result = self._recv().split(';')
	if result[0].startswith('error'):  
		log.critical("multi port stats error: unexpected invalid syntax (potential incompatibility between scripts and PROX)")
		raise Exception("multi port stats error")
        for statistics in result:
		stats = statistics.split(',')
		port_id = int(stats[0])
		rx += int(stats[1])
		tx += int(stats[2])
		no_mbufs += int(stats[3])
		errors += int(stats[4])
		tsc = int(stats[5])
        return rx, tx, no_mbufs, errors, tsc

    def set_random(self, cores, task, offset, mask, length):
        self._send('set random %s %s %s %s %s' % (','.join(map(str, cores)), task, offset, mask, length))

    def set_size(self, cores, task, pkt_size):
        self._send('pkt_size %s %s %s' % (','.join(map(str, cores)), task, pkt_size))

    def set_value(self, cores, task, offset, value, length):
        self._send('set value %s %s %s %s %s' % (','.join(map(str, cores)), task, offset, value, length))

    def _send(self, cmd):
        """Append LF and send command to the PROX instance."""
        if self._sock is None:
            raise RuntimeError("PROX socket closed, cannot send '%s'" % cmd)
        self._sock.sendall(cmd.encode() + b'\n')

    def _recv(self):
        """Receive response from PROX instance, and return it with LF removed."""
        if self._sock is None:
            raise RuntimeError("PROX socket closed, cannot receive anymore")
        pos = self._rcvd.find(b'\n')
        while pos == -1:
            self._rcvd += self._sock.recv(256)
            pos = self._rcvd.find(b'\n')
        rsp = self._rcvd[:pos]
        self._rcvd = self._rcvd[pos+1:]
        return rsp.decode()