Flow Classification extension
[vswitchperf.git] / core / vswitch_controller_p2p.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 """VSwitch controller for Physical to Physical deployment
16 """
17
18 import logging
19
20 from core.vswitch_controller import IVswitchController
21 from conf import settings
22
23 _FLOW_TEMPLATE = {
24     'idle_timeout': '0'
25 }
26
27 class VswitchControllerP2P(IVswitchController):
28     """VSwitch controller for P2P deployment scenario.
29
30     Attributes:
31         _vswitch_class: The vSwitch class to be used.
32         _vswitch: The vSwitch object controlled by this controller
33         _deployment_scenario: A string describing the scenario to set-up in the
34             constructor.
35     """
36     def __init__(self, vswitch_class, traffic):
37         """Initializes up the prerequisites for the P2P deployment scenario.
38
39         :vswitch_class: the vSwitch class to be used.
40         """
41         self._logger = logging.getLogger(__name__)
42         self._vswitch_class = vswitch_class
43         self._vswitch = vswitch_class()
44         self._deployment_scenario = "P2P"
45         self._logger.debug('Creation using ' + str(self._vswitch_class))
46         self._traffic = traffic.copy()
47
48     def setup(self):
49         """Sets up the switch for p2p.
50         """
51         self._logger.debug('Setup using ' + str(self._vswitch_class))
52
53         try:
54             self._vswitch.start()
55
56             bridge = settings.getValue('VSWITCH_BRIDGE_NAME')
57             self._vswitch.add_switch(bridge)
58
59             (_, _) = self._vswitch.add_phy_port(bridge)
60             (_, _) = self._vswitch.add_phy_port(bridge)
61
62             self._vswitch.del_flow(bridge)
63
64             # table#0 - flows designed to force 5 & 13 tuple matches go here
65             flow = {'table':'0', 'priority':'1', 'actions': ['goto_table:1']}
66             self._vswitch.add_flow(bridge, flow)
67
68             # table#1 - flows to route packets between ports goes here. The
69             # chosen port is communicated to subsequent tables by setting the
70             # metadata value to the egress port number
71
72             # configure flows according to the TC definition
73             flow_template = _FLOW_TEMPLATE.copy()
74             if self._traffic['flow_type'] == 'IP':
75                 flow_template.update({'dl_type':'0x0800', 'nw_src':self._traffic['l3']['srcip'],
76                                       'nw_dst':self._traffic['l3']['dstip']})
77
78             flow = flow_template.copy()
79             flow.update({'table':'1', 'priority':'1', 'in_port':'1',
80                          'actions': ['write_actions(output:2)', 'write_metadata:2',
81                                      'goto_table:2']})
82             self._vswitch.add_flow(bridge, flow)
83             flow = flow_template.copy()
84             flow.update({'table':'1', 'priority':'1', 'in_port':'2',
85                          'actions': ['write_actions(output:1)', 'write_metadata:1',
86                                      'goto_table:2']})
87             self._vswitch.add_flow(bridge, flow)
88
89             # Frame modification table. Frame modification flow rules are
90             # isolated in this table so that they can be turned on or off
91             # without affecting the routing or tuple-matching flow rules.
92             flow = {'table':'2', 'priority':'1', 'actions': ['goto_table:3']}
93             self._vswitch.add_flow(bridge, flow)
94
95             # Egress table
96             # (TODO) Billy O'Mahony - the drop action here actually required in
97             # order to egress the packet. This is the subject of a thread on
98             # ovs-discuss 2015-06-30.
99             flow = {'table':'3', 'priority':'1', 'actions': ['drop']}
100             self._vswitch.add_flow(bridge, flow)
101         except:
102             self._vswitch.stop()
103             raise
104
105     def stop(self):
106         """Tears down the switch created in setup().
107         """
108         self._logger.debug('Stop using ' + str(self._vswitch_class))
109         self._vswitch.stop()
110
111     def __enter__(self):
112         self.setup()
113
114     def __exit__(self, type_, value, traceback):
115         self.stop()
116
117     def get_vswitch(self):
118         """See IVswitchController for description
119         """
120         return self._vswitch
121
122     def get_ports_info(self):
123         """See IVswitchController for description
124         """
125         self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
126         return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))
127
128     def dump_vswitch_flows(self):
129         """See IVswitchController for description
130         """
131         self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))