test_spec: LTD: Add Caution for Scaleability Address Time-out.
[vswitchperf.git] / tools / module_manager.py
1 # Copyright 2015 Intel Corporation.
2 #
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
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """Simple kernel module manager implementation.
16 """
17
18 import subprocess
19 import logging
20 from tools import tasks
21
22 class KernelModuleInsertMode(object):
23     """Module manager type of insert definition.
24     """
25     MODPROBE = 1
26     INSMOD = 2 #NOT IMPLEMENTED
27
28 class ModuleManager(object):
29     """Simple module manager which acts as system wrapper for Kernel Modules.
30     """
31
32     _logger = logging.getLogger(__name__)
33
34     def __init__(self, insert_mode=KernelModuleInsertMode.MODPROBE):
35         """Initializes data and sets insert mode.
36
37         :param insert_mode: insert mode defines how modules are going to
38                             be inserted in system.
39         """
40         self._modules = None
41         self._insert_mode = insert_mode
42
43     def insert_modules(self, modules):
44         """Method inserts list of modules using defined insert mode.
45
46         :param modules: list of modules to be inserted. Each element on
47                         list should represent format which is expected
48                         by KernelModuleInsertMode (e.g. for MODPROBE it
49                         would be module name).
50
51         :returns: None
52         """
53         self._modules = modules
54         for module in modules:
55             if ModuleManager.is_module_inserted(module):
56                 continue
57
58             try:
59                 if self._insert_mode == KernelModuleInsertMode.MODPROBE:
60                     tasks.run_task(['sudo', 'modprobe', module], self._logger,
61                                    'Inserting module \'%s\'...' % module, True)
62                 else:
63                     self._logger.error(
64                         "Kernel module insert mode NOT IMPLEMENTED.")
65                     raise
66
67             except subprocess.CalledProcessError:
68                 self._logger.error('Unable to insert module \'%s\'.', module)
69                 raise  # fail catastrophically
70
71     def remove_modules(self):
72         """Removes all modules that have been previously instereted.
73         """
74         for module in self._modules:
75             # first check if module is loaded
76             if not ModuleManager.is_module_inserted(module):
77                 continue
78
79             try:
80                 tasks.run_task(['sudo', 'rmmod', module], self._logger,
81                                'Removing module \'%s\'...' % module, True)
82             except subprocess.CalledProcessError:
83                 self._logger.error('Unable to remove module \'%s\'.', module)
84                 continue
85
86     @staticmethod
87     def is_module_inserted(module):
88         """Check if a module is inserted on system.
89         """
90         with open('/proc/modules') as mod_file:
91             loaded_mods = mod_file.readlines()
92
93         # first check if module is loaded
94         for line in loaded_mods:
95             if line.startswith(module):
96                 return True
97         return False