4 ## Copyright (c) 2020 Intel Corporation
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
10 ## http://www.apache.org/licenses/LICENSE-2.0
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.
19 from rapid_log import RapidLog
20 from prox_ctrl import prox_ctrl
23 class RapidMachine(object):
25 Class to deal with a PROX instance (VM, bare metal, container)
27 def __init__(self, key, user, vim, rundir, machine_params, configonly):
28 self.name = machine_params['name']
29 self.ip = machine_params['admin_ip']
34 self.dpdk_port_index = []
35 self.configonly = configonly
38 ip_key = 'dp_ip{}'.format(index)
39 mac_key = 'dp_mac{}'.format(index)
40 if ip_key in machine_params.keys() and mac_key in machine_params.keys():
41 dp_port = {'ip': machine_params[ip_key], 'mac' : machine_params[mac_key]}
42 self.dp_ports.append(dict(dp_port))
43 self.dpdk_port_index.append(index - 1)
48 self.machine_params = machine_params
52 if ((not self.configonly) and self.machine_params['prox_socket']):
53 self._client.scp_get('/prox.log', './{}.prox.log'.format(self.name))
56 return (self.machine_params['cores'])
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 if ((not self.configonly) and self.machine_params['prox_launch_exit']):
67 result = self._client.run_cmd(DevBindFileName)
68 RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
70 def generate_lua(self, vim, appendix = ''):
71 PROXConfigfile = open (self.machine_params['config_file'], 'r')
72 PROXConfig = PROXConfigfile.read()
73 PROXConfigfile.close()
74 self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",PROXConfig))
75 self.LuaFileName = 'parameters-{}.lua'.format(self.ip)
76 with open(self.LuaFileName, "w") as LuaFile:
77 LuaFile.write('require "helper"\n')
78 LuaFile.write('name="%s"\n'% self.name)
79 for index, dp_port in enumerate(self.dp_ports, start = 1):
80 LuaFile.write('local_ip{}="{}"\n'.format(index, dp_port['ip']))
81 LuaFile.write('local_hex_ip{}=convertIPToHex(local_ip{})\n'.format(index, index))
82 if vim in ['kubernetes']:
83 LuaFile.write("eal=\"--socket-mem=512,0 --file-prefix %s --pci-whitelist %s\"\n" % (self.name, self.machine_params['dp_pci_dev']))
85 LuaFile.write("eal=\"\"\n")
86 if 'cores' in self.machine_params.keys():
87 LuaFile.write('cores="%s"\n'% ','.join(map(str, self.machine_params['cores'])))
88 if 'ports' in self.machine_params.keys():
89 LuaFile.write('ports="%s"\n'% ','.join(map(str, self.machine_params['ports'])))
90 if 'dest_ports' in self.machine_params.keys():
91 for index, dest_port in enumerate(self.machine_params['dest_ports'], start = 1):
92 LuaFile.write('dest_ip{}="{}"\n'.format(index, dest_port['ip']))
93 LuaFile.write('dest_hex_ip{}=convertIPToHex(dest_ip{})\n'.format(index, index))
94 LuaFile.write('dest_hex_mac{}="{}"\n'.format(index , dest_port['mac'].replace(':',' ')))
95 LuaFile.write(appendix)
96 self._client.scp_put(self.LuaFileName, self.rundir + '/parameters.lua')
97 self._client.scp_put('helper.lua', self.rundir + '/helper.lua')
99 def start_prox(self, autostart=''):
100 if self.machine_params['prox_socket']:
101 self._client = prox_ctrl(self.ip, self.key, self.user)
102 self._client.connect()
103 if self.vim in ['OpenStack']:
105 self.generate_lua(self.vim)
106 self._client.scp_put(self.machine_params['config_file'], '{}/{}'.format(self.rundir, self.machine_params['config_file']))
107 if ((not self.configonly) and self.machine_params['prox_launch_exit']):
108 cmd = 'sudo {}/prox {} -t -o cli -f {}/{}'.format(self.rundir, autostart, self.rundir, self.machine_params['config_file'])
109 RapidLog.debug("Starting PROX on {}: {}".format(self.name, cmd))
110 result = self._client.run_cmd(cmd, 'PROX Testing on {}'.format(self.name))
111 #RapidLog.debug("Finished PROX on {}: {}, {}".format(self.name, cmd, result))
112 RapidLog.debug("Finished PROX on {}: {}".format(self.name, cmd))
114 def close_prox(self):
115 if (not self.configonly) and self.machine_params['prox_socket'] and self.machine_params['prox_launch_exit']:
118 def connect_prox(self):
119 if self.machine_params['prox_socket']:
120 self.socket = self._client.connect_socket()
123 self.socket.start(self.get_cores())
126 self.socket.stop(self.get_cores())
128 def reset_stats(self):
129 self.socket.reset_stats()
131 def core_stats(self):
132 return (self.socket.core_stats(self.get_cores(), self.all_tasks_for_this_cfg))
134 def multi_port_stats(self):
135 return (self.socket.multi_port_stats(self.dpdk_port_index))