Vanilla_Multi_Queue: Add vanilla ovs multi-queue functionality
[vswitchperf.git] / vswitches / ovs_vanilla.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 Vanilla OVS implementation
16 """
17
18 import logging
19 from conf import settings
20 from vswitches.ovs import IVSwitchOvs
21 from src.ovs import DPCtl
22 from tools.module_manager import ModuleManager
23 from tools import tasks
24
25 class OvsVanilla(IVSwitchOvs):
26     """ Open vSwitch
27
28     This is wrapper for functionality implemented in src.ovs.
29
30     The method docstrings document only considerations specific to this
31     implementation. For generic information of the nature of the methods,
32     see the interface definition.
33     """
34
35     _current_id = 0
36     _vport_id = 0
37
38     def __init__(self):
39         super(OvsVanilla, self).__init__()
40         self._ports = list(nic['device'] for nic in settings.getValue('NICS'))
41         self._logger = logging.getLogger(__name__)
42         self._vswitchd_args += ["unix:%s" % self.get_db_sock_path()]
43         self._vswitchd_args += settings.getValue('VSWITCHD_VANILLA_ARGS')
44         self._module_manager = ModuleManager()
45
46     def start(self):
47         """See IVswitch for general description
48
49         Activates kernel modules, ovsdb and vswitchd.
50         """
51         self._module_manager.insert_modules(
52             settings.getValue('VSWITCH_VANILLA_KERNEL_MODULES'))
53         super(OvsVanilla, self).start()
54
55     def stop(self):
56         """See IVswitch for general description
57
58         Kills ovsdb and vswitchd and removes kernel modules.
59         """
60         # remove all tap interfaces
61         for i in range(self._vport_id):
62             tapx = 'tap' + str(i)
63             tap_cmd_list = ['sudo', 'ip', 'tuntap', 'del', tapx, 'mode', 'tap']
64             if int(settings.getValue('GUEST_NIC_QUEUES')):
65                 tap_cmd_list += ['multi_queue']
66             tasks.run_task(tap_cmd_list, self._logger, 'Deleting ' + tapx, False)
67         self._vport_id = 0
68
69         super(OvsVanilla, self).stop()
70         dpctl = DPCtl()
71         dpctl.del_dp()
72
73         self._module_manager.remove_modules()
74
75     def add_phy_port(self, switch_name):
76         """
77         Method adds port based on detected device names.
78
79         See IVswitch for general description
80         """
81         if self._current_id == len(self._ports):
82             self._logger.error("Can't add port! There are only " +
83                                len(self._ports) + " ports " +
84                                "defined in config!")
85             raise
86
87         if not self._ports[self._current_id]:
88             self._logger.error("Can't detect device name for NIC %s", self._current_id)
89             raise ValueError("Invalid device name for %s" % self._current_id)
90
91         bridge = self._bridges[switch_name]
92         port_name = self._ports[self._current_id]
93         params = []
94
95         # For PVP only
96         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', port_name],
97                        self._logger, 'Remove IP', False)
98         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', port_name, 'up'],
99                        self._logger, 'Bring up ' + port_name, False)
100
101         of_port = bridge.add_port(port_name, params)
102         self._current_id += 1
103         return (port_name, of_port)
104
105     def add_vport(self, switch_name):
106         """
107         Method adds virtual port into OVS vanilla
108
109         See IVswitch for general description
110         """
111         # Create tap devices for the VM
112         tap_name = 'tap' + str(self._vport_id)
113         self._vport_id += 1
114         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'del', tap_name, 'mode', 'tap']
115         if int(settings.getValue('GUEST_NIC_QUEUES')):
116             tap_cmd_list += ['multi_queue']
117         tasks.run_task(tap_cmd_list, self._logger,
118                        'Creating tap device...', False)
119
120         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'add', tap_name, 'mode', 'tap']
121         if int(settings.getValue('GUEST_NIC_QUEUES')):
122             tap_cmd_list += ['multi_queue']
123         tasks.run_task(tap_cmd_list, self._logger,
124                        'Creating tap device...', False)
125
126         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', tap_name],
127                        self._logger, 'Remove IP', False)
128         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', tap_name, 'up'],
129                        self._logger, 'Bring up ' + tap_name, False)
130
131         bridge = self._bridges[switch_name]
132         of_port = bridge.add_port(tap_name, [])
133         return (tap_name, of_port)
134
135