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