documentation: Installation prerequisites
[vswitchperf.git] / core / vswitch_controller_p2p.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 """VSwitch controller for Physical to Physical deployment
16 """
17
18 import logging
19 import netaddr
20
21 from core.vswitch_controller import IVswitchController
22 from conf import settings
23
24 _FLOW_TEMPLATE = {
25     'idle_timeout': '0'
26 }
27
28 _PROTO_TCP = 6
29 _PROTO_UDP = 17
30
31 class VswitchControllerP2P(IVswitchController):
32     """VSwitch controller for P2P deployment scenario.
33
34     Attributes:
35         _vswitch_class: The vSwitch class to be used.
36         _vswitch: The vSwitch object controlled by this controller
37         _deployment_scenario: A string describing the scenario to set-up in the
38             constructor.
39     """
40     def __init__(self, vswitch_class, traffic):
41         """Initializes up the prerequisites for the P2P deployment scenario.
42
43         :vswitch_class: the vSwitch class to be used.
44         """
45         self._logger = logging.getLogger(__name__)
46         self._vswitch_class = vswitch_class
47         self._vswitch = vswitch_class()
48         self._deployment_scenario = "P2P"
49         self._logger.debug('Creation using ' + str(self._vswitch_class))
50         self._traffic = traffic.copy()
51
52     def setup(self):
53         """Sets up the switch for p2p.
54         """
55         self._logger.debug('Setup using ' + str(self._vswitch_class))
56
57         try:
58             self._vswitch.start()
59
60             bridge = settings.getValue('VSWITCH_BRIDGE_NAME')
61             self._vswitch.add_switch(bridge)
62
63             (_, _) = self._vswitch.add_phy_port(bridge)
64             (_, _) = self._vswitch.add_phy_port(bridge)
65
66             self._vswitch.del_flow(bridge)
67
68             # table#0 - flows designed to force 5 & 13 tuple matches go here
69             flow = {'table':'0', 'priority':'1', 'actions': ['goto_table:1']}
70             self._vswitch.add_flow(bridge, flow)
71
72             # table#1 - flows to route packets between ports goes here. The
73             # chosen port is communicated to subsequent tables by setting the
74             # metadata value to the egress port number
75
76             # configure flows according to the TC definition
77             flow_template = _FLOW_TEMPLATE.copy()
78             if self._traffic['flow_type'] == 'IP':
79                 flow_template.update({'dl_type':'0x0800', 'nw_src':self._traffic['l3']['srcip'],
80                                       'nw_dst':self._traffic['l3']['dstip']})
81
82             flow = flow_template.copy()
83             flow.update({'table':'1', 'priority':'1', 'in_port':'1',
84                          'actions': ['write_actions(output:2)', 'write_metadata:0x2',
85                                      'goto_table:2']})
86             self.process_flow_template(bridge, flow)
87             flow = flow_template.copy()
88             flow.update({'table':'1', 'priority':'1', 'in_port':'2',
89                          'actions': ['write_actions(output:1)', 'write_metadata:0x1',
90                                      'goto_table:2']})
91             self.process_flow_template(bridge, flow)
92
93             # Frame modification table. Frame modification flow rules are
94             # isolated in this table so that they can be turned on or off
95             # without affecting the routing or tuple-matching flow rules.
96             flow = {'table':'2', 'priority':'1', 'actions': ['goto_table:3']}
97             self._vswitch.add_flow(bridge, flow)
98
99             # Egress table
100             # (TODO) Billy O'Mahony - the drop action here actually required in
101             # order to egress the packet. This is the subject of a thread on
102             # ovs-discuss 2015-06-30.
103             flow = {'table':'3', 'priority':'1', 'actions': ['drop']}
104             self._vswitch.add_flow(bridge, flow)
105         except:
106             self._vswitch.stop()
107             raise
108
109     def stop(self):
110         """Tears down the switch created in setup().
111         """
112         self._logger.debug('Stop using ' + str(self._vswitch_class))
113         self._vswitch.stop()
114
115     def __enter__(self):
116         self.setup()
117
118     def __exit__(self, type_, value, traceback):
119         self.stop()
120
121     def get_vswitch(self):
122         """See IVswitchController for description
123         """
124         return self._vswitch
125
126     def get_ports_info(self):
127         """See IVswitchController for description
128         """
129         self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
130         return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))
131
132     def dump_vswitch_flows(self):
133         """See IVswitchController for description
134         """
135         self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))
136
137     def process_flow_template(self, bridge, flow_template):
138         """Method adds flows into the vswitch based on given flow template
139            and configuration of multistream feature.
140         """
141         if ('pre_installed_flows' in self._traffic and
142                 self._traffic['pre_installed_flows'].lower() == 'yes' and
143                 'multistream' in self._traffic and self._traffic['multistream'] > 0 and
144                 'stream_type' in self._traffic):
145             # multistream feature is enabled and flows should be inserted into OVS
146             # so generate flows based on template and multistream configuration
147             if self._traffic['stream_type'] == 'L2':
148                 # iterate through destimation MAC address
149                 dst_mac_value = netaddr.EUI(self._traffic['l2']['dstmac']).value
150                 for i in range(self._traffic['multistream']):
151                     tmp_mac = netaddr.EUI(dst_mac_value + i)
152                     tmp_mac.dialect = netaddr.mac_unix_expanded
153                     flow_template.update({'dl_dst':tmp_mac})
154                     # optimize flow insertion by usage of cache
155                     self._vswitch.add_flow(bridge, flow_template, cache='on')
156             elif self._traffic['stream_type'] == 'L3':
157                 # iterate through destimation IP address
158                 dst_ip_value = netaddr.IPAddress(self._traffic['l3']['dstip']).value
159                 for i in range(self._traffic['multistream']):
160                     tmp_ip = netaddr.IPAddress(dst_ip_value + i)
161                     flow_template.update({'dl_type':'0x0800', 'nw_dst':tmp_ip})
162                     # optimize flow insertion by usage of cache
163                     self._vswitch.add_flow(bridge, flow_template, cache='on')
164             elif self._traffic['stream_type'] == 'L4':
165                 # read transport protocol from configuration and iterate through its destination port
166                 proto = _PROTO_TCP if self._traffic['l3']['proto'].lower() == 'tcp' else _PROTO_UDP
167                 for i in range(self._traffic['multistream']):
168                     flow_template.update({'dl_type':'0x0800', 'nw_proto':proto, 'tp_dst':i})
169                     # optimize flow insertion by usage of cache
170                     self._vswitch.add_flow(bridge, flow_template, cache='on')
171             else:
172                 self._logger.error('Stream type is set to uknown value %s', self._traffic['stream_type'])
173             # insert cached flows into the OVS
174             self._vswitch.add_flow(bridge, [], cache='flush')
175         else:
176             self._vswitch.add_flow(bridge, flow_template)