Add support for native Kubernetes CPU Manager
[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 os
22 import re
23
24 class RapidMachine(object):
25     """
26     Class to deal with a PROX instance (VM, bare metal, container)
27     """
28     def __init__(self, key, user, vim, rundir, resultsdir, machine_params,
29             configonly):
30         self.name = machine_params['name']
31         self.ip = machine_params['admin_ip']
32         self.key = key
33         self.user = user
34         self.rundir = rundir
35         self.resultsdir = resultsdir
36         self.dp_ports = []
37         self.dpdk_port_index = []
38         self.configonly = configonly
39         index = 1
40         while True:
41             ip_key = 'dp_ip{}'.format(index)
42             mac_key = 'dp_mac{}'.format(index)
43             if ip_key in machine_params.keys() and mac_key in machine_params.keys():
44                 dp_port = {'ip': machine_params[ip_key], 'mac' : machine_params[mac_key]}
45                 self.dp_ports.append(dict(dp_port))
46                 self.dpdk_port_index.append(index - 1)
47                 index += 1
48             else:
49                 break
50         self.machine_params = machine_params
51         self.vim = vim
52         self.cpu_mapping = None
53
54     def __del__(self):
55         if ((not self.configonly) and self.machine_params['prox_socket']):
56             self._client.scp_get('/prox.log', '{}/{}.prox.log'.format(
57                 self.resultsdir, self.name))
58
59     def get_cores(self):
60         return (self.machine_params['cores'])
61
62     def expand_cpuset(self, cpuset):
63         """Expand cpuset provided as comma-separated list of CPU numbers and
64         CPU ranges of numbers. For more information please see
65         https://man7.org/linux/man-pages/man7/cpuset.7.html
66         """
67         cpuset_expanded = []
68         for cpu in cpuset.split(','):
69             if '-' in cpu:
70                 cpu_range = cpu.split('-')
71                 cpuset_expanded += range(int(cpu_range[0]), int(cpu_range[1]) + 1)
72             else:
73                 cpuset_expanded.append(int(cpu))
74         return cpuset_expanded
75
76     def read_cpuset(self):
77         """Read list of cpus on which we allowed to execute
78         """
79         cmd = 'cat /sys/fs/cgroup/cpuset/cpuset.cpus'
80         cpuset_cpus = self._client.run_cmd(cmd).decode().rstrip()
81         RapidLog.debug('{} ({}): Allocated cpuset: {}'.format(self.name, self.ip, cpuset_cpus))
82         self.cpu_mapping = self.expand_cpuset(cpuset_cpus)
83         RapidLog.debug('{} ({}): Expanded cpuset: {}'.format(self.name, self.ip, self.cpu_mapping))
84
85         # Log CPU core mapping for user information
86         cpu_mapping_str = ''
87         for i in range(len(self.cpu_mapping)):
88             cpu_mapping_str = cpu_mapping_str + '[' + str(i) + '->' + str(self.cpu_mapping[i]) + '], '
89         cpu_mapping_str = cpu_mapping_str[:-2]
90         RapidLog.debug('{} ({}): CPU mapping: {}'.format(self.name, self.ip, cpu_mapping_str))
91
92     def remap_cpus(self, cpus):
93         """Convert relative cpu ids provided as function parameter to match
94         cpu ids from allocated list
95         """
96         cpus_remapped = []
97         for cpu in cpus:
98             cpus_remapped.append(self.cpu_mapping[cpu])
99         return cpus_remapped
100
101     def remap_all_cpus(self):
102         """Convert relative cpu ids for different parameters (mcore, cores)
103         """
104         if self.cpu_mapping is None:
105             RapidLog.debug('{} ({}): cpu mapping is not defined! Please check the configuration!'.format(self.name, self.ip))
106             return
107
108         if 'mcore' in self.machine_params.keys():
109             cpus_remapped = self.remap_cpus(self.machine_params['mcore'])
110             RapidLog.debug('{} ({}): mcore {} remapped to {}'.format(self.name, self.ip, self.machine_params['mcore'], cpus_remapped))
111             self.machine_params['mcore'] = cpus_remapped
112
113         if 'cores' in self.machine_params.keys():
114             cpus_remapped = self.remap_cpus(self.machine_params['cores'])
115             RapidLog.debug('{} ({}): cores {} remapped to {}'.format(self.name, self.ip, self.machine_params['cores'], cpus_remapped))
116             self.machine_params['cores'] = cpus_remapped
117
118     def devbind(self):
119         # Script to bind the right network interface to the poll mode driver
120         for index, dp_port in enumerate(self.dp_ports, start = 1):
121             DevBindFileName = self.rundir + '/devbind-{}-port{}.sh'.format(self.ip, index)
122             self._client.scp_put('./devbind.sh', DevBindFileName)
123             cmd =  'sed -i \'s/MACADDRESS/' + dp_port['mac'] + '/\' ' + DevBindFileName 
124             result = self._client.run_cmd(cmd)
125             RapidLog.debug('devbind.sh MAC updated for port {} on {} {}'.format(index, self.name, result))
126             if ((not self.configonly) and self.machine_params['prox_launch_exit']):
127                 result = self._client.run_cmd(DevBindFileName)
128                 RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
129
130     def generate_lua(self, vim, prox_config_file, appendix = ''):
131         PROXConfigfile =  open (prox_config_file, 'r')
132         PROXConfig = PROXConfigfile.read()
133         PROXConfigfile.close()
134         self.all_tasks_for_this_cfg = set(re.findall("task\s*=\s*(\d+)",PROXConfig))
135         self.LuaFileName = 'parameters-{}.lua'.format(self.ip)
136         with open(self.LuaFileName, "w") as LuaFile:
137             LuaFile.write('require "helper"\n')
138             LuaFile.write('name="%s"\n'% self.name)
139             for index, dp_port in enumerate(self.dp_ports, start = 1):
140                 LuaFile.write('local_ip{}="{}"\n'.format(index, dp_port['ip']))
141                 LuaFile.write('local_hex_ip{}=convertIPToHex(local_ip{})\n'.format(index, index))
142             if vim in ['kubernetes']:
143                 LuaFile.write("eal=\"--socket-mem=512,0 --file-prefix %s --pci-whitelist %s\"\n" % (self.name, self.machine_params['dp_pci_dev']))
144             else:
145                 LuaFile.write("eal=\"\"\n")
146             if 'mcore' in self.machine_params.keys():
147                 LuaFile.write('mcore="%s"\n'% ','.join(map(str, self.machine_params['mcore'])))
148             if 'cores' in self.machine_params.keys():
149                 LuaFile.write('cores="%s"\n'% ','.join(map(str, self.machine_params['cores'])))
150             if 'ports' in self.machine_params.keys():
151                 LuaFile.write('ports="%s"\n'% ','.join(map(str, self.machine_params['ports'])))
152             if 'dest_ports' in self.machine_params.keys():
153                 for index, dest_port in enumerate(self.machine_params['dest_ports'], start = 1):
154                     LuaFile.write('dest_ip{}="{}"\n'.format(index, dest_port['ip']))
155                     LuaFile.write('dest_hex_ip{}=convertIPToHex(dest_ip{})\n'.format(index, index))
156                     LuaFile.write('dest_hex_mac{}="{}"\n'.format(index , dest_port['mac'].replace(':',' ')))
157             LuaFile.write(appendix)
158         self._client.scp_put(self.LuaFileName, self.rundir + '/parameters.lua')
159         self._client.scp_put('helper.lua', self.rundir + '/helper.lua')
160
161     def start_prox(self, autostart=''):
162         if self.machine_params['prox_socket']:
163             self._client = prox_ctrl(self.ip, self.key, self.user)
164             self._client.connect()
165             if self.vim in ['OpenStack']:
166                 self.devbind()
167             if self.vim in ['kubernetes']:
168                 self.read_cpuset()
169                 self.remap_all_cpus()
170             _, prox_config_file_name = os.path.split(self.machine_params['config_file'])
171             self.generate_lua(self.vim, self.machine_params['config_file'])
172             self._client.scp_put(self.machine_params['config_file'], '{}/{}'.format(self.rundir, prox_config_file_name))
173             if ((not self.configonly) and self.machine_params['prox_launch_exit']):
174                 cmd = 'sudo {}/prox {} -t -o cli -f {}/{}'.format(self.rundir, autostart, self.rundir, prox_config_file_name)
175                 RapidLog.debug("Starting PROX on {}: {}".format(self.name, cmd))
176                 result = self._client.run_cmd(cmd, 'PROX Testing on {}'.format(self.name))
177                 RapidLog.debug("Finished PROX on {}: {}".format(self.name, cmd))
178
179     def close_prox(self):
180         if (not self.configonly) and self.machine_params['prox_socket'] and self.machine_params['prox_launch_exit']:
181             self.socket.quit_prox()
182
183     def connect_prox(self):
184         if self.machine_params['prox_socket']:
185            self.socket = self._client.connect_socket()
186
187     def start(self):
188         self.socket.start(self.get_cores())
189
190     def stop(self):
191         self.socket.stop(self.get_cores())
192
193     def reset_stats(self):
194         self.socket.reset_stats()
195
196     def core_stats(self):
197         return (self.socket.core_stats(self.get_cores(), self.all_tasks_for_this_cfg))
198
199     def multi_port_stats(self):
200         return (self.socket.multi_port_stats(self.dpdk_port_index))