settings.getValue('RTE_SDK'), 'tools', 'dpdk_nic_bind.py')
_DPDK_MODULE_MANAGER = ModuleManager()
-
#
# system management
#
if not _is_linux():
_LOGGER.error('Not running on a compatible Linux version. Exiting...')
return
-
_insert_modules()
_remove_vhost_net()
_bind_nics()
_remove_modules()
_vhost_user_cleanup()
-
-#
-# vhost specific modules management
-#
-
-
-def insert_vhost_modules():
- """Inserts VHOST related kernel modules
- """
- mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
- 'lib',
- 'librte_vhost')
- _insert_module_group('VHOST_MODULE', mod_path_prefix)
-
-
-def remove_vhost_modules():
- """Removes all VHOST related kernel modules
- """
- _remove_module_group('VHOST_MODULE')
-
#
# basic compatibility test
#
-
def _is_linux():
"""Check if running on Linux.
# module management
#
-
-def _is_module_inserted(module):
- """Check if a module is inserted on system.
- """
- with open('/proc/modules') as mod_file:
- loaded_mods = mod_file.readlines()
-
- # first check if module is loaded
- for line in loaded_mods:
- if line.startswith(module):
- return True
- return False
-
-
def _insert_modules():
"""Ensure required modules are inserted on system.
"""
_DPDK_MODULE_MANAGER.insert_modules(settings.getValue('SYS_MODULES'))
mod_path_prefix = settings.getValue('OVS_DIR')
- _insert_module_group('OVS_MODULES', mod_path_prefix)
- mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
- settings.getValue('RTE_TARGET'))
- _insert_module_group('DPDK_MODULES', mod_path_prefix)
-
-
-def _insert_module_group(module_group, group_path_prefix):
- """Ensure all modules in a group are inserted into the system.
-
- :param module_group: A name of configuration item containing a list
- of module names
- """
- for module in settings.getValue(module_group):
- # first check if module is loaded
- if _is_module_inserted(module[1]):
- continue
-
- try:
- mod_path = os.path.join(group_path_prefix, module[0],
- '%s.ko' % module[1])
- tasks.run_task(['sudo', 'insmod', mod_path], _LOGGER,
- 'Inserting module \'%s\'...' % module[1], True)
- except subprocess.CalledProcessError:
- _LOGGER.error('Unable to insert module \'%s\'.', module[1])
- raise # fail catastrophically
-
+ _DPDK_MODULE_MANAGER.insert_module_group(settings.getValue('OVS_MODULES'),
+ mod_path_prefix)
+ if 'vfio-pci' not in settings.getValue('DPDK_MODULES'):
+ mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
+ settings.getValue('RTE_TARGET'))
+ _DPDK_MODULE_MANAGER.insert_module_group(settings.getValue('DPDK_MODULES'),
+ mod_path_prefix)
+ else:
+ _DPDK_MODULE_MANAGER.insert_modules(settings.getValue('DPDK_MODULES'))
def _remove_modules():
"""Ensure required modules are removed from system.
"""
- _remove_module_group('OVS_MODULES')
- _remove_module_group('DPDK_MODULES')
-
_DPDK_MODULE_MANAGER.remove_modules()
-def _remove_module_group(module_group):
- """Ensure all modules in a group are removed from the system.
+#
+# vhost specific modules management
+#
- :param module_group: A name of configuration item containing a list
- of module names
+def insert_vhost_modules():
+ """Inserts VHOST related kernel modules
"""
- for module in settings.getValue(module_group):
- # first check if module is loaded
- if not _is_module_inserted(module[1]):
- continue
+ mod_path_prefix = os.path.join(settings.getValue('RTE_SDK'),
+ 'lib',
+ 'librte_vhost')
+ _DPDK_MODULE_MANAGER.insert_module_group('VHOST_MODULE', mod_path_prefix)
- try:
- tasks.run_task(['sudo', 'rmmod', module[1]], _LOGGER,
- 'Removing module \'%s\'...' % module[1], True)
- except subprocess.CalledProcessError:
- _LOGGER.error('Unable to remove module \'%s\'.', module[1])
- continue
+
+def remove_vhost_modules():
+ """Removes all VHOST related kernel modules
+ """
+ _DPDK_MODULE_MANAGER.remove_module_group(settings.getValue('VHOST_MODULE'))
#
-# 'vhost-net' module management
+# 'vhost-net' module cleanup
#
def _remove_vhost_net():
"""Remove vhost-net driver and file.
"""
- if _is_module_inserted('vhost_net'):
- try:
- tasks.run_task(['sudo', 'rmmod', 'vhost_net'], _LOGGER,
- 'Removing \'/dev/vhost-net\' directory...', True)
- except subprocess.CalledProcessError:
- _LOGGER.error('Unable to remove module \'vhost_net\'.')
-
+ _DPDK_MODULE_MANAGER.remove_module('vhost-net')
try:
tasks.run_task(['sudo', 'rm', '-f', '/dev/vhost-net'], _LOGGER,
'Removing \'/dev/vhost-net\' directory...', True)
_LOGGER.error('Unable to remove directory \'/dev/vhost-net\'.')
#
+# Vhost-user cleanup
+#
+
+def _vhost_user_cleanup():
+ """Remove files created by vhost-user tests.
+ """
+ for sock in settings.getValue('VHOST_USER_SOCKS'):
+ if os.path.exists(sock):
+ try:
+ tasks.run_task(['sudo', 'rm', sock],
+ _LOGGER,
+ 'Deleting vhost-user socket \'%s\'...' %
+ sock,
+ True)
+
+ except subprocess.CalledProcessError:
+ _LOGGER.error('Unable to delete vhost-user socket \'%s\'.',
+ sock)
+ continue
+#
# NIC management
#
"""Bind NICs using the Intel DPDK ``dpdk_nic_bind.py`` tool.
"""
try:
- tasks.run_task(['sudo', RTE_PCI_TOOL, '--bind', 'igb_uio'] +
+ _driver = 'igb_uio'
+ if 'vfio-pci' in settings.getValue('DPDK_MODULES'):
+ _driver = 'vfio-pci'
+ tasks.run_task(['sudo', 'chmod', 'a+x', '/dev/vfio'],
+ _LOGGER, 'Setting VFIO permissions .. a+x',
+ True)
+ tasks.run_task(['sudo', 'chmod', '-R', '666', '/dev/vfio/'],
+ _LOGGER, 'Setting VFIO permissions .. 0666',
+ True)
+
+ tasks.run_task(['sudo', RTE_PCI_TOOL, '--bind=' + _driver] +
settings.getValue('WHITELIST_NICS'), _LOGGER,
'Binding NICs %s...' %
settings.getValue('WHITELIST_NICS'),
str(settings.getValue('WHITELIST_NICS')),
nic_drivers)
-
-
-#
-# Vhost-user cleanup
-#
-
-def _vhost_user_cleanup():
- """Remove files created by vhost-user tests.
- """
- for sock in settings.getValue('VHOST_USER_SOCKS'):
- if os.path.exists(sock):
- try:
- tasks.run_task(['sudo', 'rm', sock],
- _LOGGER,
- 'Deleting vhost-user socket \'%s\'...' %
- sock,
- True)
-
- except subprocess.CalledProcessError:
- _LOGGER.error('Unable to delete vhost-user socket \'%s\'.',
- sock)
- continue
-
-
class Dpdk(object):
"""A context manager for the system init/cleanup.
"""
-# Copyright 2015 Intel Corporation.
+# Copyright 2015-2016 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
"""Simple kernel module manager implementation.
"""
-
+import os
import subprocess
import logging
+
from tools import tasks
+_LOGGER = logging.getLogger(__name__)
class ModuleManager(object):
"""Simple module manager which acts as system wrapper for Kernel Modules.
"""
def __init__(self):
"""Initializes data
"""
- self._modules = None
+ self._modules = []
def insert_modules(self, modules):
"""Method inserts list of modules. In case that module name ends
:returns: None
"""
- self._modules = modules
+
for module in modules:
- if ModuleManager.is_module_inserted(module):
+ if self.is_module_inserted(module):
continue
try:
else:
tasks.run_task(['sudo', 'modprobe', module], self._logger,
'Modprobe module \'%s\'...' % module, True)
-
+ _LOGGER.info("Inserted Module %s", module)
+ self._modules.append(module)
except subprocess.CalledProcessError:
self._logger.error('Unable to insert module \'%s\'.', module)
raise # fail catastrophically
+ def insert_module_group(self, module_group, group_path_prefix):
+ """Ensure all modules in a group are inserted into the system.
+
+ :param module_group: A name of configuration item containing a list
+ of module names
+ """
+ for module in module_group:
+ # first check if module is loaded
+ if self.is_module_inserted(module[1]):
+ continue
+
+ try:
+ mod_path = os.path.join(group_path_prefix, module[0],
+ '%s.ko' % module[1])
+ tasks.run_task(['sudo', 'insmod', mod_path], _LOGGER,
+ 'Inserting module \'%s\'...' % module[1], True)
+ self._modules.append(module)
+ except subprocess.CalledProcessError:
+ _LOGGER.error('Unable to insert module \'%s\'.', module[1])
+ raise # fail catastrophically
+
def remove_modules(self):
- """Removes all modules that have been previously instereted.
+ """Removes all modules that have been previously inserted.
"""
for module in self._modules:
# first check if module is loaded
- if not ModuleManager.is_module_inserted(module):
+ if not self.is_module_inserted(module):
continue
try:
# with .ko suffix
tasks.run_task(['sudo', 'rmmod', module], self._logger,
'Removing module \'%s\'...' % module, True)
+ self._modules.remove(module)
except subprocess.CalledProcessError:
self._logger.error('Unable to remove module \'%s\'.', module)
continue
-
@staticmethod
def is_module_inserted(module):
"""Check if a module is inserted on system.
"""
+ if module.endswith('.ko'):
# get module base name, i.e strip path and .ko suffix if possible
- module_base_name = module.split('.')[0].split('/').pop()
+ module_base_name = os.path.basename(os.path.splitext(module)[0])
+ else:
+ module_base_name = module
# get list of modules from kernel
with open('/proc/modules') as mod_file:
if line.startswith(module_base_name):
return True
return False
+
+ def remove_module(self, module):
+ """Removes a single module.
+ """
+ if self.is_module_inserted(module):
+ # get module base name, i.e strip path and .ko suffix if possible
+ module_base_name = os.path.basename(os.path.splitext(module)[0])
+
+ try:
+ # rmmod supports both simple module name and full module path
+ # with .ko suffix
+ tasks.run_task(['sudo', 'rmmod', module_base_name], self._logger,
+ 'Removing module \'%s\'...' % module, True)
+ self._modules.remove(module)
+ except subprocess.CalledProcessError:
+ self._logger.error('Unable to remove module \'%s\'.', module_base_name)
+
+ def remove_module_group(self, module_group):
+ """Removes all modules in the modules group.
+ """
+ for module in module_group:
+ if not self.is_module_inserted(module[1]):
+ continue
+ # get module base name, i.e strip path and .ko suffix if possible
+ module_base_name = os.path.basename(os.path.splitext(module)[0])
+
+ try:
+ # rmmod supports both simple module name and full module path
+ # with .ko suffix
+ tasks.run_task(['sudo', 'rmmod', module_base_name], self._logger,
+ 'Removing module \'%s\'...' % module, True)
+ self._modules.remove(module)
+ except subprocess.CalledProcessError:
+ self._logger.error('Unable to remove module \'%s\'.', module_base_name)