acea4ecbf65f7b2d3c2980097e20e11c0215378e
[vswitchperf.git] / vswitches / ovs_vanilla.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 """VSPERF Vanilla OVS implementation
16 """
17
18 import logging
19 from conf import settings
20 from vswitches.vswitch import IVSwitch
21 from src.ovs import VSwitchd, OFBridge
22 from tools.module_manager import ModuleManager
23 from tools import tasks
24
25 _LOGGER = logging.getLogger(__name__)
26 VSWITCHD_CONST_ARGS = ['--', '--log-file']
27
28 class OvsVanilla(IVSwitch):
29     """VSwitch Vanilla implementation
30
31     This is wrapper for functionality implemented in src.ovs.
32
33     The method docstrings document only considerations specific to this
34     implementation. For generic information of the nature of the methods,
35     see the interface definition.
36     """
37
38     _logger = logging.getLogger()
39     _ports = settings.getValue('VSWITCH_VANILLA_PHY_PORT_NAMES')
40     _current_id = 0
41     _vport_id = 0
42
43     def __init__(self):
44         #vswitchd_args = VSWITCHD_CONST_ARGS
45         vswitchd_args = ["unix:%s" % VSwitchd.get_db_sock_path()]
46         vswitchd_args += settings.getValue('VSWITCHD_VANILLA_ARGS')
47         self._vswitchd = VSwitchd(vswitchd_args=vswitchd_args,
48                                   expected_cmd="db.sock: connected")
49         self._bridges = {}
50         self._module_manager = ModuleManager()
51
52     def start(self):
53         """See IVswitch for general description
54
55         Activates kernel modules, ovsdb and vswitchd.
56         """
57         self._module_manager.insert_modules(
58             settings.getValue('VSWITCH_VANILLA_KERNEL_MODULES'))
59         self._logger.info("Starting Vswitchd...")
60         self._vswitchd.start()
61         self._logger.info("Vswitchd...Started.")
62
63     def stop(self):
64         """See IVswitch for general description
65
66         Kills ovsdb and vswitchd and removes kernel modules.
67         """
68         # remove all tap interfaces
69         for i in range(self._vport_id):
70             tapx = 'tap' + str(i)
71             tasks.run_task(['sudo', 'ip', 'tuntap', 'del',
72                             tapx, 'mode', 'tap'],
73                            _LOGGER, 'Deleting ' + tapx, False)
74         self._vport_id = 0
75
76         self._vswitchd.kill()
77         self._module_manager.remove_modules()
78
79
80     def add_switch(self, switch_name):
81         """See IVswitch for general description
82         """
83         bridge = OFBridge(switch_name)
84         bridge.create()
85         bridge.set_db_attribute('Open_vSwitch', '.',
86                                 'other_config:max-idle', '60000')
87         self._bridges[switch_name] = bridge
88
89     def del_switch(self, switch_name):
90         """See IVswitch for general description
91         """
92         bridge = self._bridges[switch_name]
93         self._bridges.pop(switch_name)
94         bridge.destroy()
95
96     def add_phy_port(self, switch_name):
97         """
98         Method adds port based on configured VSWITCH_VANILLA_PHY_PORT_NAMES
99         stored in config file.
100
101         See IVswitch for general description
102         """
103         if self._current_id == len(self._ports):
104             self._logger.error("Can't add port! There are only " +
105                                len(self._ports) + " ports " +
106                                "defined in config!")
107             raise
108
109         if not self._ports[self._current_id]:
110             self._logger.error("VSWITCH_VANILLA_PHY_PORT_NAMES not set")
111             raise ValueError("Invalid VSWITCH_VANILLA_PHY_PORT_NAMES")
112
113         bridge = self._bridges[switch_name]
114         port_name = self._ports[self._current_id]
115         params = []
116
117         # For PVP only
118         tasks.run_task(['sudo', 'ifconfig', port_name, '0'],
119                        _LOGGER, 'Remove IP', False)
120
121         of_port = bridge.add_port(port_name, params)
122         self._current_id += 1
123         return (port_name, of_port)
124
125     def add_vport(self, switch_name):
126         """
127         Method adds virtual port into OVS vanilla
128
129         See IVswitch for general description
130         """
131         # Create tap devices for the VM
132         tap_name = 'tap' + str(self._vport_id)
133         self._vport_id += 1
134
135         tasks.run_task(['sudo', 'ip', 'tuntap', 'del',
136                         tap_name, 'mode', 'tap'],
137                        _LOGGER, 'Creating tap device...', False)
138
139         tasks.run_task(['sudo', 'ip', 'tuntap', 'add',
140                         tap_name, 'mode', 'tap'],
141                        _LOGGER, 'Creating tap device...', False)
142
143         tasks.run_task(['sudo', 'ifconfig', tap_name, '0'],
144                        _LOGGER, 'Bring up ' + tap_name, False)
145
146         bridge = self._bridges[switch_name]
147         of_port = bridge.add_port(tap_name, [])
148         return (tap_name, of_port)
149
150
151     def get_ports(self, switch_name):
152         """See IVswitch for general description
153         """
154         bridge = self._bridges[switch_name]
155         ports = list(bridge.get_ports().items())
156         return [(name, of_port) for (name, (of_port, _)) in ports]
157
158     def del_port(self, switch_name, port_name):
159         """See IVswitch for general description
160         """
161         bridge = self._bridges[switch_name]
162         bridge.del_port(port_name)
163
164     def add_flow(self, switch_name, flow):
165         """See IVswitch for general description
166         """
167         bridge = self._bridges[switch_name]
168         bridge.add_flow(flow)
169
170     def del_flow(self, switch_name, flow=None):
171         """See IVswitch for general description
172         """
173         flow = flow or {}
174         bridge = self._bridges[switch_name]
175         bridge.del_flow(flow)
176
177     def dump_flows(self, switch_name):
178         """See IVswitch for general description
179         """
180         bridge = self._bridges[switch_name]
181         bridge.dump_flows()