bugfix: Graceful shutdown of VM
[vswitchperf.git] / vswitches / ovs_dpdk_vhost.py
1 # Copyright 2015-2016 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 """VSPERF VSwitch implementation using DPDK and vhost ports
16 """
17
18 import logging
19 from conf import settings
20 from vswitches.ovs import IVSwitchOvs
21 from src.ovs import VSwitchd
22 from src.dpdk import dpdk
23
24 class OvsDpdkVhost(IVSwitchOvs):
25     """ Open vSwitch with DPDK support
26
27     Generic OVS wrapper functionality in src.ovs is maximally used. This
28     class wraps DPDK system configuration along with DPDK specific OVS
29     parameters
30
31     The method docstrings document only considerations specific to this
32     implementation. For generic information of the nature of the methods,
33     see the interface.
34     """
35
36     def __init__(self):
37         super(OvsDpdkVhost, self).__init__()
38         self._logger = logging.getLogger(__name__)
39
40         self._vswitchd_args = ['--dpdk']
41         self._vswitchd_args += settings.getValue('VSWITCHD_DPDK_ARGS')
42         if settings.getValue('VNF').endswith('Cuse'):
43             self._logger.info("Inserting VHOST Cuse modules into kernel...")
44             dpdk.insert_vhost_modules()
45
46         self._vswitchd = VSwitchd(vswitchd_args=self._vswitchd_args,
47                                   expected_cmd=
48                                   r'EAL: Master l*core \d+ is ready')
49
50     def start(self):
51         """See IVswitch for general description
52
53         Activates DPDK kernel modules, ovsdb and vswitchd.
54         """
55         dpdk.init()
56         super(OvsDpdkVhost, self).start()
57
58     def stop(self):
59         """See IVswitch for general description
60
61         Kills ovsdb and vswitchd and removes DPDK kernel modules.
62         """
63         super(OvsDpdkVhost, self).stop()
64         dpdk.cleanup()
65         dpdk.remove_vhost_modules()
66
67     def add_switch(self, switch_name, params=None):
68         """See IVswitch for general description
69         """
70         switch_params = ['--', 'set', 'bridge', switch_name, 'datapath_type=netdev']
71         if params:
72             switch_params = switch_params + params
73
74         super(OvsDpdkVhost, self).add_switch(switch_name, switch_params)
75
76         if settings.getValue('VSWITCH_AFFINITIZATION_ON') == 1:
77             # Sets the PMD core mask to VSWITCH_PMD_CPU_MASK
78             # for CPU core affinitization
79             self._bridges[switch_name].set_db_attribute('Open_vSwitch', '.',
80                                                         'other_config:pmd-cpu-mask',
81                                                         settings.getValue('VSWITCH_PMD_CPU_MASK'))
82
83     def add_phy_port(self, switch_name):
84         """See IVswitch for general description
85
86         Creates a port of type dpdk.
87         The new port is named dpdk<n> where n is an integer starting from 0.
88         """
89         bridge = self._bridges[switch_name]
90         dpdk_count = self._get_port_count('type=dpdk')
91         port_name = 'dpdk' + str(dpdk_count)
92         params = ['--', 'set', 'Interface', port_name, 'type=dpdk']
93         of_port = bridge.add_port(port_name, params)
94
95         return (port_name, of_port)
96
97     def add_vport(self, switch_name):
98         """See IVswitch for general description
99
100         Creates a port of type dpdkvhost
101         The new port is named dpdkvhost<n> where n is an integer starting
102         from 0
103         """
104         bridge = self._bridges[switch_name]
105         # Changed dpdkvhost to dpdkvhostuser to be able to run in Qemu 2.2
106         if settings.getValue('VNF').endswith('Cuse'):
107             vhost_count = self._get_port_count('type=dpdkvhostcuse')
108             port_name = 'dpdkvhostcuse' + str(vhost_count)
109             params = ['--', 'set', 'Interface', port_name, 'type=dpdkvhostcuse']
110         else:
111             vhost_count = self._get_port_count('type=dpdkvhostuser')
112             port_name = 'dpdkvhostuser' + str(vhost_count)
113             params = ['--', 'set', 'Interface', port_name, 'type=dpdkvhostuser']
114
115         of_port = bridge.add_port(port_name, params)
116
117         return (port_name, of_port)