1 # Copyright 2015-2016 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_nic_bind.py`` script from Intel(R)
21 from sys import platform as _platform
27 from tools import tasks
28 from conf import settings
29 from tools.module_manager import ModuleManager
31 _LOGGER = logging.getLogger(__name__)
32 RTE_PCI_TOOL = os.path.join(
33 settings.getValue('RTE_SDK_USER'), 'tools', 'dpdk_nic_bind.py')
35 _DPDK_MODULE_MANAGER = ModuleManager()
37 # declare global NIC variables only as their content might not be known yet
46 """Setup system for DPDK.
50 _NICS = settings.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(settings.getValue('SYS_MODULES'))
95 mod_path_prefix = settings.getValue('OVS_DIR')
96 _DPDK_MODULE_MANAGER.insert_module_group(settings.getValue('OVS_MODULES'),
98 if 'vfio-pci' not in settings.getValue('DPDK_MODULES'):
99 mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
100 settings.getValue('RTE_TARGET'))
101 _DPDK_MODULE_MANAGER.insert_module_group(settings.getValue('DPDK_MODULES'),
104 _DPDK_MODULE_MANAGER.insert_modules(settings.getValue('DPDK_MODULES'))
106 def _remove_modules():
107 """Ensure required modules are removed from system.
109 _DPDK_MODULE_MANAGER.remove_modules()
112 # vhost specific modules management
115 def insert_vhost_modules():
116 """Inserts VHOST related kernel modules
118 mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
121 _DPDK_MODULE_MANAGER.insert_module_group(settings.getValue('VHOST_MODULE'), mod_path_prefix)
124 def remove_vhost_modules():
125 """Removes all VHOST related kernel modules
127 # all modules are removed automatically by _remove_modules() method
132 # 'vhost-net' module cleanup
135 def _remove_vhost_net():
136 """Remove vhost-net driver and file.
138 _DPDK_MODULE_MANAGER.remove_module('vhost-net')
140 tasks.run_task(['sudo', 'rm', '-f', '/dev/vhost-net'], _LOGGER,
141 'Removing \'/dev/vhost-net\' directory...', True)
142 except subprocess.CalledProcessError:
143 _LOGGER.error('Unable to remove directory \'/dev/vhost-net\'.')
149 def _vhost_user_cleanup():
150 """Remove files created by vhost-user tests.
152 for sock in settings.getValue('VHOST_USER_SOCKS'):
153 if os.path.exists(sock):
155 tasks.run_task(['sudo', 'rm', sock],
157 'Deleting vhost-user socket \'%s\'...' %
161 except subprocess.CalledProcessError:
162 _LOGGER.error('Unable to delete vhost-user socket \'%s\'.',
171 """Bind NICs using the Intel DPDK ``dpdk_nic_bind.py`` tool.
175 if 'vfio-pci' in settings.getValue('DPDK_MODULES'):
177 tasks.run_task(['sudo', 'chmod', 'a+x', '/dev/vfio'],
178 _LOGGER, 'Setting VFIO permissions .. a+x',
180 tasks.run_task(['sudo', 'chmod', '-R', '666', '/dev/vfio/'],
181 _LOGGER, 'Setting VFIO permissions .. 0666',
184 tasks.run_task(['sudo', RTE_PCI_TOOL, '--bind=' + _driver] +
186 'Binding NICs %s...' % _NICS_PCI,
188 except subprocess.CalledProcessError:
189 _LOGGER.error('Unable to bind NICs %s', str(_NICS_PCI))
192 """Unbind NICs using the Intel DPDK ``dpdk_nic_bind.py`` tool.
195 tasks.run_task(['sudo', RTE_PCI_TOOL, '--unbind'] +
197 'Unbinding NICs %s...' % str(_NICS_PCI),
199 except subprocess.CalledProcessError:
200 _LOGGER.error('Unable to unbind NICs %s', str(_NICS_PCI))
201 # Rebind NICs to their original drivers
202 # using the Intel DPDK ``dpdk_nic_bind.py`` tool.
206 tasks.run_task(['sudo', RTE_PCI_TOOL, '--bind',
207 nic['driver'], nic['pci']],
208 _LOGGER, 'Binding NIC %s to %s...' %
209 (nic['pci'], nic['driver']),
211 except subprocess.CalledProcessError:
212 _LOGGER.error('Unable to bind NIC %s to driver %s',
213 nic['pci'], nic['driver'])
216 """A context manager for the system init/cleanup.
219 _LOGGER.info('Setting up DPDK')
223 def __exit__(self, type_, value, traceback):
224 _LOGGER.info('Cleaning up DPDK')