src: add appropriate build flags for OVS dpdk
[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, KernelModuleInsertMode
23
24 VSWITCHD_CONST_ARGS = ['--', '--log-file']
25
26 class OvsVanilla(IVSwitch):
27     """VSwitch Vanilla implementation
28
29     This is wrapper for functionality implemented in src.ovs.
30
31     The method docstrings document only considerations specific to this
32     implementation. For generic information of the nature of the methods,
33     see the interface definition.
34     """
35
36     _logger = logging.getLogger()
37     _ports = settings.getValue('VSWITCH_VANILLA_PHY_PORT_NAMES')
38     _current_id = 0
39
40     def __init__(self):
41         #vswitchd_args = VSWITCHD_CONST_ARGS
42         vswitchd_args = ["unix:%s" % VSwitchd.getDbSockPath()]
43         vswitchd_args += settings.getValue('VSWITCHD_VANILLA_ARGS')
44         self._vswitchd = VSwitchd(vswitchd_args=vswitchd_args,
45                                   expected_cmd="db.sock: connected")
46         self._bridges = {}
47         self._module_manager = ModuleManager(KernelModuleInsertMode.MODPROBE)
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         self._logger.info("Starting Vswitchd...")
57         self._vswitchd.start()
58         self._logger.info("Vswitchd...Started.")
59
60     def stop(self):
61         """See IVswitch for general description
62
63         Kills ovsdb and vswitchd and removes kernel modules.
64         """
65         self._vswitchd.kill()
66         self._module_manager.remove_modules()
67
68     def add_switch(self, switch_name):
69         """See IVswitch for general description
70         """
71         bridge = OFBridge(switch_name)
72         bridge.create()
73         bridge.set_db_attribute('Open_vSwitch', '.',
74                                 'other_config:max-idle', '60000')
75         self._bridges[switch_name] = bridge
76
77     def del_switch(self, switch_name):
78         """See IVswitch for general description
79         """
80         bridge = self._bridges[switch_name]
81         self._bridges.pop(switch_name)
82         bridge.destroy()
83
84     def add_phy_port(self, switch_name):
85         """
86         Method adds port based on configured VSWITCH_VANILLA_PHY_PORT_NAMES
87         stored in config file.
88
89         See IVswitch for general description
90         """
91         if self._current_id == len(self._ports):
92             self._logger.error("Can't add port! There are only " +
93                                len(self._ports) + " ports " +
94                                "defined in config!")
95             raise
96
97         bridge = self._bridges[switch_name]
98         port_name = self._ports[self._current_id]
99         params = []
100         of_port = bridge.add_port(port_name, params)
101         self._current_id += 1
102         return (port_name, of_port)
103
104     def add_vport(self, switch_name):
105         """See IVswitch for general description"""
106         raise NotImplementedError("Not implemented for Vanilla OVS.")
107
108     def get_ports(self, switch_name):
109         """See IVswitch for general description
110         """
111         bridge = self._bridges[switch_name]
112         ports = list(bridge.get_ports().items())
113         return [(name, of_port) for (name, (of_port, _)) in ports]
114
115     def del_port(self, switch_name, port_name):
116         """See IVswitch for general description
117         """
118         bridge = self._bridges[switch_name]
119         bridge.del_port(port_name)
120
121     def add_flow(self, switch_name, flow):
122         """See IVswitch for general description
123         """
124         bridge = self._bridges[switch_name]
125         bridge.add_flow(flow)
126
127     def del_flow(self, switch_name, flow=None):
128         """See IVswitch for general description
129         """
130         flow = flow or {}
131         bridge = self._bridges[switch_name]
132         bridge.del_flow(flow)