1 # Copyright 2015 Intel Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 """VSwitch controller for Physical to Physical deployment
21 from core.vswitch_controller import IVswitchController
22 from conf import settings
31 class VswitchControllerP2P(IVswitchController):
32 """VSwitch controller for P2P deployment scenario.
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
40 def __init__(self, vswitch_class, traffic):
41 """Initializes up the prerequisites for the P2P deployment scenario.
43 :vswitch_class: the vSwitch class to be used.
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()
53 """Sets up the switch for p2p.
55 self._logger.debug('Setup using ' + str(self._vswitch_class))
60 bridge = settings.getValue('VSWITCH_BRIDGE_NAME')
61 self._vswitch.add_switch(bridge)
63 (_, _) = self._vswitch.add_phy_port(bridge)
64 (_, _) = self._vswitch.add_phy_port(bridge)
66 self._vswitch.del_flow(bridge)
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)
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
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']})
82 flow = flow_template.copy()
83 flow.update({'table':'1', 'priority':'1', 'in_port':'1',
84 'actions': ['write_actions(output:2)', 'write_metadata:0x2',
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',
91 self.process_flow_template(bridge, flow)
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)
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)
110 """Tears down the switch created in setup().
112 self._logger.debug('Stop using ' + str(self._vswitch_class))
118 def __exit__(self, type_, value, traceback):
121 def get_vswitch(self):
122 """See IVswitchController for description
126 def get_ports_info(self):
127 """See IVswitchController for description
129 self._logger.debug('get_ports_info using ' + str(self._vswitch_class))
130 return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))
132 def dump_vswitch_flows(self):
133 """See IVswitchController for description
135 self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))
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.
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(int(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(int(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(int(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')
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')
176 self._vswitch.add_flow(bridge, flow_template)