vnfs: Enable PVP using vhost-cuse
[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 vswitches.utils import add_ports_to_flow
22
23 _FLOW_TEMPLATE = {
24     'idle_timeout': '0'
25 }
26 BRIDGE_NAME = 'br0'
27
28 class VswitchControllerP2P(IVswitchController):
29     """VSwitch controller for P2P deployment scenario.
30
31     Attributes:
32         _vswitch_class: The vSwitch class to be used.
33         _vswitch: The vSwitch object controlled by this controller
34         _deployment_scenario: A string describing the scenario to set-up in the
35             constructor.
36     """
37     def __init__(self, vswitch_class):
38         """Initializes up the prerequisites for the P2P deployment scenario.
39
40         :vswitch_class: the vSwitch class to be used.
41         """
42         self._logger = logging.getLogger(__name__)
43         self._vswitch_class = vswitch_class
44         self._vswitch = vswitch_class()
45         self._deployment_scenario = "P2P"
46         self._logger.debug('Creation using ' + str(self._vswitch_class))
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             self._vswitch.add_switch(BRIDGE_NAME)
57
58             (_, phy1_number) = self._vswitch.add_phy_port(BRIDGE_NAME)
59             (_, phy2_number) = self._vswitch.add_phy_port(BRIDGE_NAME)
60
61             self._vswitch.del_flow(BRIDGE_NAME)
62
63             # table#0 - flows designed to force 5 & 13 tuple matches go here
64             flow = {'table':'0', 'priority':'1', 'actions': ['goto_table:1']}
65             self._vswitch.add_flow(BRIDGE_NAME, flow)
66
67             # table#1 - flows to route packets between ports goes here. The
68             # chosen port is communicated to subsequent tables by setting the
69             # metadata value to the egress port number
70             flow = {'table':'1', 'priority':'1', 'in_port':'1',
71                     'actions': ['write_actions(output:2)', 'write_metadata:2',
72                         'goto_table:2']}
73             self._vswitch.add_flow(BRIDGE_NAME, flow)
74             flow = {'table':'1', 'priority':'1', 'in_port':'2',
75                     'actions': ['write_actions(output:1)', 'write_metadata:1',
76                         'goto_table:2']}
77             self._vswitch.add_flow(BRIDGE_NAME, flow)
78
79             # Frame modification table. Frame modification flow rules are
80             # isolated in this table so that they can be turned on or off
81             # without affecting the routing or tuple-matching flow rules.
82             flow = {'table':'2', 'priority':'1', 'actions': ['goto_table:3']}
83             self._vswitch.add_flow(BRIDGE_NAME, flow)
84
85             # Egress table
86             # (TODO) Billy O'Mahony - the drop action here actually required in
87             # order to egress the packet. This is the subject of a thread on
88             # ovs-discuss 2015-06-30.
89             flow = {'table':'3', 'priority':'1', 'actions': ['drop']}
90             self._vswitch.add_flow(BRIDGE_NAME, flow)
91
92             flow = add_ports_to_flow(_FLOW_TEMPLATE, phy2_number, phy1_number)
93             self._vswitch.add_flow(BRIDGE_NAME, flow)
94
95         except:
96             self._vswitch.stop()
97             raise
98
99     def stop(self):
100         """Tears down the switch created in setup().
101         """
102         self._logger.debug('Stop using ' + str(self._vswitch_class))
103         self._vswitch.stop()
104
105     def __enter__(self):
106         self.setup()
107
108     def __exit__(self, type_, value, traceback):
109         self.stop()
110
111     def get_vswitch(self):
112         """See IVswitchController for description
113         """
114         return self._vswitch
115
116     def get_ports_info(self):
117         """See IVswitchController for description
118         """
119         self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
120         return self._vswitch.get_ports(BRIDGE_NAME)