Merge "teststeps: Improvements and bugfixing of teststeps"
[vswitchperf.git] / tools / module_manager.py
index 2eb4c63..dd1d92b 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2015-2016 Intel Corporation.
+# Copyright 2015-2017 Intel Corporation.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -31,30 +31,40 @@ class ModuleManager(object):
         """
         self._modules = []
 
-    def insert_module(self, module):
+    def insert_module(self, module, auto_remove=True):
         """Method inserts given module.
 
         In case that module name ends with .ko suffix then insmod will
         be used for its insertion. Otherwise modprobe will be called.
 
         :param module: a name of kernel module
+        :param auto_remove: if True (by default), then module will be
+            automatically removed by remove_modules() method
         """
         module_base_name = os.path.basename(os.path.splitext(module)[0])
 
         if self.is_module_inserted(module):
             self._logger.info('Module already loaded \'%s\'.', module_base_name)
             # add it to internal list, so we can try to remove it at the end
-            self._modules.append(module)
+            if auto_remove:
+                self._modules.append(module)
             return
 
         try:
             if module.endswith('.ko'):
+                # load module dependecies first, but suppress automatic
+                # module removal at the end; Just for case, that module
+                # depends on generic module
+                for depmod in self.get_module_dependecies(module):
+                    self.insert_module(depmod, auto_remove=False)
+
                 tasks.run_task(['sudo', 'insmod', module], self._logger,
                                'Insmod module \'%s\'...' % module_base_name, True)
             else:
                 tasks.run_task(['sudo', 'modprobe', module], self._logger,
                                'Modprobe module \'%s\'...' % module_base_name, True)
-            self._modules.append(module)
+            if auto_remove:
+                self._modules.append(module)
         except subprocess.CalledProcessError:
             # in case of error, show full module name
             self._logger.error('Unable to insert module \'%s\'.', module)
@@ -68,17 +78,6 @@ class ModuleManager(object):
         for module in modules:
             self.insert_module(module)
 
-    def insert_module_group(self, module_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
-        :param path_prefix: A name of directory which contains given
-            group of modules
-        """
-        for (path_suffix, module) in module_group:
-            self.insert_module(os.path.join(path_prefix, path_suffix, '%s.ko' % module))
-
     def remove_module(self, module):
         """Removes a single module.
 
@@ -143,3 +142,25 @@ class ModuleManager(object):
                 return line
 
         return None
+
+    def get_module_dependecies(self, module):
+        """Return list of modules, which must be loaded before module itself
+
+        :param module: a name of kernel module
+        :returns: In case that module has any dependencies, then list of module
+            names will be returned. Otherwise it returns empty list, i.e. [].
+        """
+        deps = ''
+        try:
+            # get list of module dependecies from kernel
+            deps = subprocess.check_output('modinfo -F depends {}'.format(module),
+                                           shell=True).decode().rstrip('\n')
+        except subprocess.CalledProcessError:
+            # in case of error, show full module name...
+            self._logger.info('Unable to get list of dependecies for module \'%s\'.', module)
+            # ...and try to continue, just for case that dependecies are already loaded
+
+        if len(deps):
+            return deps.split(',')
+        else:
+            return []