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