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):
28 self.name = machine_params['name']
29 self.ip = machine_params['admin_ip']
34 self.dpdk_port_index = []
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)
47 self.machine_params = machine_params
51 if self.machine_params['prox_socket']:
52 self._client.scp_get('/prox.log', './{}.prox.log'.format(self.name))
55 return (self.machine_params['cores'])
57 def devbind(self, configonly):
58 # Script to bind the right network interface to the poll mode driver
59 for index, dp_port in enumerate(self.dp_ports, start = 1):
60 DevBindFileName = self.rundir + '/devbind-{}-port{}.sh'.format(self.ip, index)
61 self._client.scp_put('./devbind.sh', DevBindFileName)
62 cmd = 'sed -i \'s/MACADDRESS/' + dp_port['mac'] + '/\' ' + DevBindFileName
63 result = self._client.run_cmd(cmd)
64 RapidLog.debug('devbind.sh MAC updated for port {} on {} {}'.format(index, self.name, result))
65 if ((not configonly) and self.machine_params['prox_launch_exit']):
66 result = self._client.run_cmd(DevBindFileName)
67 RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
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']))
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')
98 def start_prox(self, configonly=False, autostart=''):
99 if self.machine_params['prox_socket']:
100 self._client = prox_ctrl(self.ip, self.key, self.user)
101 self._client.connect()
102 if self.vim in ['OpenStack']:
103 self.devbind(configonly)
104 self.generate_lua(self.vim)
105 self._client.scp_put(self.machine_params['config_file'], '{}/{}'.format(self.rundir, self.machine_params['config_file']))
106 if ((not configonly) and self.machine_params['prox_launch_exit']):
107 cmd = 'sudo {}/prox {} -t -o cli -f {}/{}'.format(self.rundir, autostart, self.rundir, self.machine_params['config_file'])
108 RapidLog.debug("Starting PROX on {}: {}".format(self.name, cmd))
109 result = self._client.run_cmd(cmd, 'PROX Testing on {}'.format(self.name))
110 #RapidLog.debug("Finished PROX on {}: {}, {}".format(self.name, cmd, result))
111 RapidLog.debug("Finished PROX on {}: {}".format(self.name, cmd))
113 def close_prox(self):
114 if self.machine_params['prox_socket'] and self.machine_params['prox_launch_exit']:
117 def connect_prox(self):
118 if self.machine_params['prox_socket']:
119 self.socket = self._client.connect_socket()
122 self.socket.start(self.get_cores())
125 self.socket.stop(self.get_cores())
127 def reset_stats(self):
128 self.socket.reset_stats()
130 def core_stats(self):
131 return (self.socket.core_stats(self.get_cores(), self.all_tasks_for_this_cfg))
133 def multi_port_stats(self):
134 return (self.socket.multi_port_stats(self.dpdk_port_index))