documentation: Traffic Capture methods
[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             self._logger.error("Can't add port! There are only " +
79                                len(self._ports) + " ports " +
80                                "defined in config!")
81             raise RuntimeError('Failed to add phy port')
82         if not self._ports[self._current_id]:
83             self._logger.error("Can't detect device name for NIC %s", self._current_id)
84             raise ValueError("Invalid device name for %s" % self._current_id)
85
86         bridge = self._bridges[switch_name]
87         port_name = self._ports[self._current_id]
88         params = []
89
90         # For PVP only
91         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', port_name],
92                        self._logger, 'Remove IP', False)
93         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', port_name, 'up'],
94                        self._logger, 'Bring up ' + port_name, False)
95
96         of_port = bridge.add_port(port_name, params)
97         self._current_id += 1
98         return (port_name, of_port)
99
100     def add_vport(self, switch_name):
101         """
102         Method adds virtual port into OVS vanilla
103
104         See IVswitch for general description
105         """
106         # Create tap devices for the VM
107         tap_name = 'tap' + str(self._vport_id)
108         self._vport_id += 1
109         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'del', tap_name, 'mode', 'tap']
110         # let's assume, that all VMs have NIC QUEUES enabled or disabled
111         # at the same time
112         if int(settings.getValue('GUEST_NIC_QUEUES')[0]):
113             tap_cmd_list += ['multi_queue']
114         tasks.run_task(tap_cmd_list, self._logger,
115                        'Creating tap device...', False)
116
117         tap_cmd_list = ['sudo', 'ip', 'tuntap', 'add', tap_name, 'mode', 'tap']
118         # let's assume, that all VMs have NIC QUEUES enabled or disabled
119         # at the same time
120         if int(settings.getValue('GUEST_NIC_QUEUES')[0]):
121             tap_cmd_list += ['multi_queue']
122         tasks.run_task(tap_cmd_list, self._logger,
123                        'Creating tap device...', False)
124         if settings.getValue('VSWITCH_JUMBO_FRAMES_ENABLED'):
125             tasks.run_task(['ifconfig', tap_name, 'mtu',
126                             str(settings.getValue('VSWITCH_JUMBO_FRAMES_SIZE'))],
127                            self._logger, 'Setting mtu size', False)
128
129         tasks.run_task(['sudo', 'ip', 'addr', 'flush', 'dev', tap_name],
130                        self._logger, 'Remove IP', False)
131         tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', tap_name, 'up'],
132                        self._logger, 'Bring up ' + tap_name, False)
133
134         bridge = self._bridges[switch_name]
135         of_port = bridge.add_port(tap_name, [])
136         return (tap_name, of_port)
137
138     def add_connection(self, switch_name, port1, port2, bidir=False):
139         """See IVswitch for general description
140         """
141         raise NotImplementedError()
142
143     def del_connection(self, switch_name, port1, port2, bidir=False):
144         """See IVswitch for general description
145         """
146         raise NotImplementedError()
147
148     def dump_connections(self, switch_name):
149         """See IVswitch for general description
150         """
151         raise NotImplementedError()