Merge "vpp: Initial support of VPP vSwitch"
[vswitchperf.git] / vswitches / ovs_vanilla.py
1 # Copyright 2015-2017 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 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(OvsVanilla, self).__init__()
39         self._ports = list(nic['device'] for nic in settings.getValue('NICS'))
40         self._logger = logging.getLogger(__name__)
41         self._vswitchd_args += ["unix:%s" % self.get_db_sock_path()]
42         self._vswitchd_args += settings.getValue('VSWITCHD_VANILLA_ARGS')
43
44     def stop(self):
45         """See IVswitch for general description
46
47         Kills ovsdb and vswitchd and removes kernel modules.
48         """
49         # remove all tap interfaces
50         for i in range(self._vport_id):
51             tapx = 'tap' + str(i)
52             tap_cmd_list = ['sudo', 'ip', 'tuntap', 'del', tapx, 'mode', 'tap']
53             # let's assume, that all VMs have NIC QUEUES enabled or disabled
54             # at the same time
55             if int(settings.getValue('GUEST_NIC_QUEUES')[0]):
56                 tap_cmd_list += ['multi_queue']
57             tasks.run_task(tap_cmd_list, self._logger, 'Deleting ' + tapx, False)
58         self._vport_id = 0
59
60         super(OvsVanilla, self).stop()
61         dpctl = DPCtl()
62         dpctl.del_dp()
63
64         self._module_manager.remove_modules()
65
66     def add_phy_port(self, switch_name):
67         """
68         Method adds port based on detected device names.
69
70         See IVswitch for general description
71         """
72         if self._current_id == len(self._ports):
73             self._logger.error("Can't add port! There are only " +
74                                len(self._ports) + " ports " +
75                                "defined in config!")
76             raise RuntimeError('Failed to add phy port')
77         if not self._ports[self._current_id]:
78             self._logger.error("Can't detect device name for NIC %s", self._current_id)
79             raise ValueError("Invalid device name for %s" % self._current_id)
80
81         bridge = self._bridges[switch_name]
82         port_name = self._ports[self._current_id]
83         params = []
84
85         # For PVP only
86         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', port_name],
87                        self._logger, 'Remove IP', False)
88         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', port_name, 'up'],
89                        self._logger, 'Bring up ' + port_name, False)
90
91         of_port = bridge.add_port(port_name, params)
92         self._current_id += 1
93         return (port_name, of_port)
94
95     def add_vport(self, switch_name):
96         """
97         Method adds virtual port into OVS vanilla
98
99         See IVswitch for general description
100         """
101         # Create tap devices for the VM
102         tap_name = 'tap' + str(self._vport_id)
103         self._vport_id += 1
104         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'del', tap_name, 'mode', 'tap']
105         # let's assume, that all VMs have NIC QUEUES enabled or disabled
106         # at the same time
107         if int(settings.getValue('GUEST_NIC_QUEUES')[0]):
108             tap_cmd_list += ['multi_queue']
109         tasks.run_task(tap_cmd_list, self._logger,
110                        'Creating tap device...', False)
111
112         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'add', tap_name, 'mode', 'tap']
113         # let's assume, that all VMs have NIC QUEUES enabled or disabled
114         # at the same time
115         if int(settings.getValue('GUEST_NIC_QUEUES')[0]):
116             tap_cmd_list += ['multi_queue']
117         tasks.run_task(tap_cmd_list, self._logger,
118                        'Creating tap device...', False)
119
120         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', tap_name],
121                        self._logger, 'Remove IP', False)
122         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', tap_name, 'up'],
123                        self._logger, 'Bring up ' + tap_name, False)
124
125         bridge = self._bridges[switch_name]
126         of_port = bridge.add_port(tap_name, [])
127         return (tap_name, of_port)
128
129     def add_connection(self, switch_name, port1, port2, bidir=False):
130         """See IVswitch for general description
131         """
132         raise NotImplementedError()
133
134     def del_connection(self, switch_name, port1, port2, bidir=False):
135         """See IVswitch for general description
136         """
137         raise NotImplementedError()
138
139     def dump_connections(self, switch_name):
140         """See IVswitch for general description
141         """
142         raise NotImplementedError()