fix: IPV6 packet generation, packet loss reporting
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_machine.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from rapid_log import RapidLog 
20 from prox_ctrl import prox_ctrl
21 import re
22
23 class RapidMachine(object):
24     """
25     Class to deal with a PROX instance (VM, bare metal, container)
26     """
27     def __init__(self, key, user, vim, rundir, machine_params):
28         self.name = machine_params['name']
29         self.ip = machine_params['admin_ip']
30         self.key = key
31         self.user = user
32         self.rundir = rundir
33         self.dp_ports = []
34         self.dpdk_port_index = []
35         index = 1
36         while True:
37             ip_key = 'dp_ip{}'.format(index)
38             mac_key = 'dp_mac{}'.format(index)
39             if ip_key in machine_params.keys() and mac_key in machine_params.keys():
40                 dp_port = {'ip': machine_params[ip_key], 'mac' : machine_params[mac_key]}
41                 self.dp_ports.append(dict(dp_port))
42                 self.dpdk_port_index.append(index - 1)
43                 index += 1
44             else:
45                 break
46         self.rundir = rundir
47         self.machine_params = machine_params
48         self._client = prox_ctrl(self.ip, self.key, self.user)
49         self._client.connect()
50         if vim in ['OpenStack']:
51             self.devbind()
52         self.generate_lua(vim)
53         self._client.scp_put(self.machine_params['config_file'], '{}/{}'.format(self.rundir, machine_params['config_file']))
54
55     def get_cores(self):
56         return (self.machine_params['cores'])
57
58     def devbind(self):
59         # Script to bind the right network interface to the poll mode driver
60         for index, dp_port in enumerate(self.dp_ports, start = 1):
61             DevBindFileName = self.rundir + '/devbind-{}-port{}.sh'.format(self.ip, index)
62             self._client.scp_put('./devbind.sh', DevBindFileName)
63             cmd =  'sed -i \'s/MACADDRESS/' + dp_port['mac'] + '/\' ' + DevBindFileName 
64             result = self._client.run_cmd(cmd)
65             RapidLog.debug('devbind.sh MAC updated for port {} on {} {}'.format(index, self.name, result))
66             result = self._client.run_cmd(DevBindFileName)
67             RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
68
69     def generate_lua(self, vim, appendix = ''):
70         PROXConfigfile =  open (self.machine_params['config_file'], 'r')
71         PROXConfig = PROXConfigfile.read()
72         PROXConfigfile.close()
73         self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",PROXConfig))
74         self.LuaFileName = 'parameters-{}.lua'.format(self.ip)
75         with open(self.LuaFileName, "w") as LuaFile:
76             LuaFile.write('require "helper"\n')
77             LuaFile.write('name="%s"\n'% self.name)
78             for index, dp_port in enumerate(self.dp_ports, start = 1):
79                 LuaFile.write('local_ip{}="{}"\n'.format(index, dp_port['ip']))
80                 LuaFile.write('local_hex_ip{}=convertIPToHex(local_ip{})\n'.format(index, index))
81             if vim in ['kubernetes']:
82                 LuaFile.write("eal=\"--socket-mem=512,0 --file-prefix %s --pci-whitelist %s\"\n" % (self.name, self.machine_params['dp_pci_dev']))
83             else:
84                 LuaFile.write("eal=\"\"\n")
85             if 'cores' in self.machine_params.keys():
86                 LuaFile.write('cores="%s"\n'% ','.join(map(str, self.machine_params['cores'])))
87             if 'ports' in self.machine_params.keys():
88                 LuaFile.write('ports="%s"\n'% ','.join(map(str, self.machine_params['ports'])))
89             if 'dest_ports' in self.machine_params.keys():
90                 for index, dest_port in enumerate(self.machine_params['dest_ports'], start = 1):
91                     LuaFile.write('dest_ip{}="{}"\n'.format(index, dest_port['ip']))
92                     LuaFile.write('dest_hex_ip{}=convertIPToHex(dest_ip{})\n'.format(index, index))
93                     LuaFile.write('dest_hex_mac{}="{}"\n'.format(index , dest_port['mac'].replace(':',' ')))
94             LuaFile.write(appendix)
95         self._client.scp_put(self.LuaFileName, self.rundir + '/parameters.lua')
96         self._client.scp_put('helper.lua', self.rundir + '/helper.lua')
97
98     def start_prox(self, autostart=''):
99         if self.machine_params['prox_launch_exit']:
100             cmd = 'sudo {}/prox {} -t -o cli -f {}/{}'.format(self.rundir, autostart, self.rundir, self.machine_params['config_file'])
101             result = self._client.fork_cmd(cmd, 'PROX Testing on {}'.format(self.name))
102             RapidLog.debug("Starting PROX on {}: {}, {}".format(self.name, cmd, result))
103         self.socket = self._client.connect_socket()
104
105     def start(self):
106         self.socket.start(self.get_cores())
107
108     def stop(self):
109         self.socket.stop(self.get_cores())
110
111     def reset_stats(self):
112         self.socket.reset_stats()
113
114     def core_stats(self):
115         return (self.socket.core_stats(self.get_cores(), self.all_tasks_for_this_cfg))
116
117     def multi_port_stats(self):
118         return (self.socket.multi_port_stats(self.dpdk_port_index))