src: update make install for DPDK
[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.vswitch import IVSwitch
21 from src.ovs import VSwitchd, OFBridge, DPCtl
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         dpctl = DPCtl()
78         dpctl.del_dp()
79
80         self._module_manager.remove_modules()
81
82
83     def add_switch(self, switch_name, params=None):
84         """See IVswitch for general description
85         """
86         bridge = OFBridge(switch_name)
87         bridge.create(params)
88         bridge.set_db_attribute('Open_vSwitch', '.',
89                                 'other_config:max-idle', '60000')
90         self._bridges[switch_name] = bridge
91
92     def del_switch(self, switch_name):
93         """See IVswitch for general description
94         """
95         bridge = self._bridges[switch_name]
96         self._bridges.pop(switch_name)
97         bridge.destroy()
98
99     def add_phy_port(self, switch_name):
100         """
101         Method adds port based on configured VSWITCH_VANILLA_PHY_PORT_NAMES
102         stored in config file.
103
104         See IVswitch for general description
105         """
106         if self._current_id == len(self._ports):
107             self._logger.error("Can't add port! There are only " +
108                                len(self._ports) + " ports " +
109                                "defined in config!")
110             raise
111
112         if not self._ports[self._current_id]:
113             self._logger.error("VSWITCH_VANILLA_PHY_PORT_NAMES not set")
114             raise ValueError("Invalid VSWITCH_VANILLA_PHY_PORT_NAMES")
115
116         bridge = self._bridges[switch_name]
117         port_name = self._ports[self._current_id]
118         params = []
119
120         # For PVP only
121         tasks.run_task(['sudo', 'ifconfig', port_name, '0'],
122                        _LOGGER, 'Remove IP', False)
123
124         of_port = bridge.add_port(port_name, params)
125         self._current_id += 1
126         return (port_name, of_port)
127
128     def add_vport(self, switch_name):
129         """
130         Method adds virtual port into OVS vanilla
131
132         See IVswitch for general description
133         """
134         # Create tap devices for the VM
135         tap_name = 'tap' + str(self._vport_id)
136         self._vport_id += 1
137
138         tasks.run_task(['sudo', 'ip', 'tuntap', 'del',
139                         tap_name, 'mode', 'tap'],
140                        _LOGGER, 'Creating tap device...', False)
141
142         tasks.run_task(['sudo', 'ip', 'tuntap', 'add',
143                         tap_name, 'mode', 'tap'],
144                        _LOGGER, 'Creating tap device...', False)
145
146         tasks.run_task(['sudo', 'ifconfig', tap_name, '0'],
147                        _LOGGER, 'Bring up ' + tap_name, False)
148
149         bridge = self._bridges[switch_name]
150         of_port = bridge.add_port(tap_name, [])
151         return (tap_name, of_port)
152
153     def add_tunnel_port(self, switch_name, remote_ip, tunnel_type='vxlan',
154                         params=None):
155         """Creates tunneling port
156         """
157         bridge = self._bridges[switch_name]
158         pcount = str(self._get_port_count('type=' + tunnel_type))
159         port_name = tunnel_type + pcount
160         local_params = ['--', 'set', 'Interface', port_name,
161                         'type=' + tunnel_type,
162                         'options:remote_ip=' + remote_ip]
163
164         if params is not None:
165             local_params = local_params + params
166
167         of_port = bridge.add_port(port_name, local_params)
168         return (port_name, of_port)
169
170     def get_ports(self, switch_name):
171         """See IVswitch for general description
172         """
173         bridge = self._bridges[switch_name]
174         ports = list(bridge.get_ports().items())
175         return [(name, of_port) for (name, (of_port, _)) in ports]
176
177     def del_port(self, switch_name, port_name):
178         """See IVswitch for general description
179         """
180         bridge = self._bridges[switch_name]
181         bridge.del_port(port_name)
182
183     def add_flow(self, switch_name, flow, cache='off'):
184         """See IVswitch for general description
185         """
186         bridge = self._bridges[switch_name]
187         bridge.add_flow(flow, cache=cache)
188
189     def del_flow(self, switch_name, flow=None):
190         """See IVswitch for general description
191         """
192         flow = flow or {}
193         bridge = self._bridges[switch_name]
194         bridge.del_flow(flow)
195
196     def dump_flows(self, switch_name):
197         """See IVswitch for general description
198         """
199         bridge = self._bridges[switch_name]
200         bridge.dump_flows()
201
202     def add_route(self, switch_name, network, destination):
203         """See IVswitch for general description
204         """
205         bridge = self._bridges[switch_name]
206         bridge.add_route(network, destination)
207
208     def set_tunnel_arp(self, ip_addr, mac_addr, switch_name):
209         """See IVswitch for general description
210         """
211         bridge = self._bridges[switch_name]
212         bridge.set_tunnel_arp(ip_addr, mac_addr, switch_name)
213
214     def _get_port_count(self, param):
215         """Returns the number of ports having a certain parameter
216
217         :param bridge: The src.ovs.ofctl.OFBridge on which to operate
218         :param param: The parameter to search for
219         :returns: Count of matches
220         """
221         cnt = 0
222         for k in self._bridges:
223             pparams = [c for (_, (_, c)) in list(self._bridges[k].get_ports().items())]
224             phits = [i for i in pparams if param in i]
225             cnt += len(phits)
226
227         if cnt is None:
228             cnt = 0
229         return cnt
230
231