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