1 # Copyright 2015-2017 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Automation of system configuration for DPDK use.
17 Parts of this based on ``tools/dpdk*bind.py`` script from Intel(R)
21 from sys import platform as _platform
28 from conf import settings as S
29 from tools import tasks
30 from tools.module_manager import ModuleManager
32 _LOGGER = logging.getLogger(__name__)
34 _DPDK_MODULE_MANAGER = ModuleManager()
36 # declare global NIC variables only as their content might not be known yet
45 """Setup system for DPDK.
47 # pylint: disable=global-statement
50 _NICS = S.getValue('NICS')
51 _NICS_PCI = list(nic['pci'] for nic in _NICS)
53 _LOGGER.error('Not running on a compatible Linux version. Exiting...')
61 """Setup system for DPDK.
64 _LOGGER.error('Not running on a compatible Linux version. Exiting...')
72 # basic compatibility test
76 """Check if running on Linux.
78 Many of the functions in this file rely on features commonly found
79 only on Linux (i.e. ``/proc`` is not present on FreeBSD). Hence, this
80 check is important to ensure someone doesn't run this on an incompatible
83 return _platform.startswith('linux') and os.path.isdir('/proc')
89 def _insert_modules():
90 """Ensure required modules are inserted on system.
93 _DPDK_MODULE_MANAGER.insert_modules(S.getValue('TOOLS')['dpdk_modules'])
95 def _remove_modules():
96 """Ensure required modules are removed from system.
98 _DPDK_MODULE_MANAGER.remove_modules()
101 # 'vhost-net' module cleanup
104 def _remove_vhost_net():
105 """Remove vhost-net driver and file.
107 _DPDK_MODULE_MANAGER.remove_module('vhost-net')
109 tasks.run_task(['sudo', 'rm', '-f', '/dev/vhost-net'], _LOGGER,
110 'Removing \'/dev/vhost-net\' directory...', True)
111 except subprocess.CalledProcessError:
112 _LOGGER.error('Unable to remove directory \'/dev/vhost-net\'.')
118 def _vhost_user_cleanup():
119 """Remove files created by vhost-user tests.
121 for sock in glob.glob(os.path.join(S.getValue('TOOLS')['ovs_var_tmp'],
122 S.getValue('VHOST_USER_SOCKS'))):
123 if os.path.exists(sock):
125 tasks.run_task(['sudo', 'rm', sock],
127 'Deleting vhost-user socket \'%s\'...' %
131 except subprocess.CalledProcessError:
132 _LOGGER.error('Unable to delete vhost-user socket \'%s\'.',
141 """Bind NICs using the bind tool specified in the configuration.
144 _LOGGER.info('NICs are not configured - nothing to bind')
148 if 'vfio-pci' in S.getValue('TOOLS')['dpdk_modules']:
150 tasks.run_task(['sudo', 'chmod', 'a+x', '/dev/vfio'],
151 _LOGGER, 'Setting VFIO permissions .. a+x',
153 tasks.run_task(['sudo', 'chmod', '-R', '666', '/dev/vfio/'],
154 _LOGGER, 'Setting VFIO permissions .. 0666',
156 if 'driverctl' in S.getValue('TOOLS')['bind-tool'].lower():
157 for nic in _NICS_PCI:
158 tasks.run_task(['sudo', S.getValue('TOOLS')['bind-tool'], '-v',
159 'set-override'] + [nic] + [_driver], _LOGGER,
160 'Binding NIC %s...' % nic, True)
162 tasks.run_task(['sudo', S.getValue('TOOLS')['bind-tool'],
163 '--bind=' + _driver] +
165 'Binding NICs %s...' % _NICS_PCI,
167 except subprocess.CalledProcessError:
168 _LOGGER.error('Unable to bind NICs %s', str(_NICS_PCI))
172 """Unbind NICs using the bind tool specified in the configuration.
175 _LOGGER.info('NICs are not configured - nothing to unbind')
178 if 'driverctl' in S.getValue('TOOLS')['bind-tool'].lower():
179 for nic in _NICS_PCI:
180 tasks.run_task(['sudo', S.getValue('TOOLS')['bind-tool'], '-v',
181 'unset-override'] + [nic], _LOGGER,
182 'Binding NIC %s...' % nic, True)
184 tasks.run_task(['sudo', S.getValue('TOOLS')['bind-tool'],
187 'Unbinding NICs %s...' % str(_NICS_PCI),
189 except subprocess.CalledProcessError:
190 _LOGGER.error('Unable to unbind NICs %s', str(_NICS_PCI))
191 # Rebind NICs to their original drivers
192 # using the Intel DPDK ``dpdk*bind.py`` tool.
196 if 'driverctl' in S.getValue('TOOLS')['bind-tool'].lower():
197 # driverctl restores the driver automatically on unset
200 tasks.run_task(['sudo', S.getValue('TOOLS')['bind-tool'],
202 nic['driver'], nic['pci']],
203 _LOGGER, 'Binding NIC %s to %s...' %
204 (nic['pci'], nic['driver']),
206 except subprocess.CalledProcessError:
207 _LOGGER.error('Unable to bind NIC %s to driver %s',
208 nic['pci'], nic['driver'])
212 """A context manager for the system init/cleanup.
215 _LOGGER.info('Setting up DPDK')
219 def __exit__(self, type_, value, traceback):
220 _LOGGER.info('Cleaning up DPDK')