Merge "namespace_bug: Initial creation of namespace fails"
[vswitchperf.git] / vnfs / qemu / qemu_pci_passthrough.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 """Automation of QEMU hypervisor with direct access to host NICs via
16    PCI passthrough.
17 """
18
19 import logging
20 import subprocess
21 import os
22 import glob
23
24 from conf import settings as S
25 from vnfs.qemu.qemu import IVnfQemu
26 from tools import tasks
27 from tools.module_manager import ModuleManager
28
29 _MODULE_MANAGER = ModuleManager()
30 _RTE_PCI_TOOL = glob.glob(os.path.join(S.getValue('RTE_SDK'), 'tools', 'dpdk*bind.py'))[0]
31
32 class QemuPciPassthrough(IVnfQemu):
33     """
34     Control an instance of QEMU with direct access to the host network devices
35     """
36     def __init__(self):
37         """
38         Initialization function.
39         """
40         super(QemuPciPassthrough, self).__init__()
41         self._logger = logging.getLogger(__name__)
42         self._nics = S.getValue('NICS')
43
44         # in case of SRIOV and PCI passthrough we must ensure, that MAC addresses are swapped
45         if S.getValue('SRIOV_ENABLED') and not self._testpmd_fwd_mode.startswith('mac'):
46             self._logger.info("SRIOV detected, forwarding mode of testpmd was changed from '%s' to '%s'",
47                               self._testpmd_fwd_mode, 'mac')
48             self._testpmd_fwd_mode = 'mac'
49
50         for nic in self._nics:
51             self._cmd += ['-device', 'vfio-pci,host=' + nic['pci']]
52
53     def start(self):
54         """
55         Start QEMU instance, bind host NICs to vfio-pci driver
56         """
57         # load vfio-pci
58         _MODULE_MANAGER.insert_modules(['vfio-pci'])
59
60         # bind every interface to vfio-pci driver
61         try:
62             nics_list = list(tmp_nic['pci'] for tmp_nic in self._nics)
63             tasks.run_task(['sudo', _RTE_PCI_TOOL, '--bind=vfio-pci'] + nics_list,
64                            self._logger, 'Binding NICs %s...' % nics_list, True)
65
66         except subprocess.CalledProcessError:
67             self._logger.error('Unable to bind NICs %s', self._nics)
68
69         super(QemuPciPassthrough, self).start()
70
71     def stop(self):
72         """
73         Stop QEMU instance, bind host NICs to the original driver
74         """
75         super(QemuPciPassthrough, self).stop()
76
77         # bind original driver to every interface
78         for nic in self._nics:
79             if nic['driver']:
80                 try:
81                     tasks.run_task(['sudo', _RTE_PCI_TOOL, '--bind=' + nic['driver'], nic['pci']],
82                                    self._logger, 'Binding NIC %s...' % nic['pci'], True)
83
84                 except subprocess.CalledProcessError:
85                     self._logger.error('Unable to bind NIC %s to driver %s', nic['pci'], nic['driver'])
86
87         # unload vfio-pci driver
88         _MODULE_MANAGER.remove_modules()