integration: Support of integration testcases
[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 VSwitchd, 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     _ports = settings.getValue('VSWITCH_VANILLA_PHY_PORT_NAMES')
36     _current_id = 0
37     _vport_id = 0
38
39     def __init__(self):
40         super(OvsVanilla, self).__init__()
41         self._logger = logging.getLogger(__name__)
42         self._vswitchd_args = ["unix:%s" % VSwitchd.get_db_sock_path()]
43         self._vswitchd_args += settings.getValue('VSWITCHD_VANILLA_ARGS')
44         self._vswitchd = VSwitchd(vswitchd_args=self._vswitchd_args,
45                                   expected_cmd="db.sock: connected")
46         self._bridges = {}
47         self._module_manager = ModuleManager()
48
49     def start(self):
50         """See IVswitch for general description
51
52         Activates kernel modules, ovsdb and vswitchd.
53         """
54         self._module_manager.insert_modules(
55             settings.getValue('VSWITCH_VANILLA_KERNEL_MODULES'))
56         super(OvsVanilla, self).start()
57
58     def stop(self):
59         """See IVswitch for general description
60
61         Kills ovsdb and vswitchd and removes kernel modules.
62         """
63         # remove all tap interfaces
64         for i in range(self._vport_id):
65             tapx = 'tap' + str(i)
66             tasks.run_task(['sudo', 'ip', 'tuntap', 'del',
67                             tapx, 'mode', 'tap'],
68                            self._logger, 'Deleting ' + tapx, False)
69         self._vport_id = 0
70
71         super(OvsVanilla, self).stop()
72         dpctl = DPCtl()
73         dpctl.del_dp()
74
75         self._module_manager.remove_modules()
76
77
78     def add_phy_port(self, switch_name):
79         """
80         Method adds port based on configured VSWITCH_VANILLA_PHY_PORT_NAMES
81         stored in config file.
82
83         See IVswitch for general description
84         """
85         if self._current_id == len(self._ports):
86             self._logger.error("Can't add port! There are only " +
87                                len(self._ports) + " ports " +
88                                "defined in config!")
89             raise
90
91         if not self._ports[self._current_id]:
92             self._logger.error("VSWITCH_VANILLA_PHY_PORT_NAMES not set")
93             raise ValueError("Invalid VSWITCH_VANILLA_PHY_PORT_NAMES")
94
95         bridge = self._bridges[switch_name]
96         port_name = self._ports[self._current_id]
97         params = []
98
99         # For PVP only
100         tasks.run_task(['sudo', 'ifconfig', port_name, '0'],
101                        self._logger, 'Remove IP', False)
102
103         of_port = bridge.add_port(port_name, params)
104         self._current_id += 1
105         return (port_name, of_port)
106
107     def add_vport(self, switch_name):
108         """
109         Method adds virtual port into OVS vanilla
110
111         See IVswitch for general description
112         """
113         # Create tap devices for the VM
114         tap_name = 'tap' + str(self._vport_id)
115         self._vport_id += 1
116
117         tasks.run_task(['sudo', 'ip', 'tuntap', 'del',
118                         tap_name, 'mode', 'tap'],
119                        self._logger, 'Creating tap device...', False)
120
121         tasks.run_task(['sudo', 'ip', 'tuntap', 'add',
122                         tap_name, 'mode', 'tap'],
123                        self._logger, 'Creating tap device...', False)
124
125         tasks.run_task(['sudo', 'ifconfig', tap_name, '0'],
126                        self._logger, 'Bring up ' + tap_name, False)
127
128         bridge = self._bridges[switch_name]
129         of_port = bridge.add_port(tap_name, [])
130         return (tap_name, of_port)
131
132