1 # Copyright 2015-2016 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 Tunnel Endpoint to Physical deployment
20 from core.vswitch_controller import IVswitchController
21 from vswitches.utils import add_ports_to_flow
22 from conf import settings
23 from tools import tasks
29 class VswitchControllerOP2P(IVswitchController):
30 """VSwitch controller for OP2P deployment scenario.
33 _vswitch_class: The vSwitch class to be used.
34 _vswitch: The vSwitch object controlled by this controller
35 _deployment_scenario: A string describing the scenario to set-up in the
38 def __init__(self, vswitch_class, traffic, tunnel_operation=None):
39 """Initializes up the prerequisites for the OP2P deployment scenario.
41 :vswitch_class: the vSwitch class to be used.
43 self._logger = logging.getLogger(__name__)
44 self._vswitch_class = vswitch_class
45 self._vswitch = vswitch_class()
46 self._deployment_scenario = "OP2P"
47 self._traffic = traffic.copy()
48 self._tunnel_operation = tunnel_operation
49 self._logger.debug('Creation using ' + str(self._vswitch_class))
52 """ Sets up the switch for overlay P2P (tunnel encap or decap)
54 self._logger.debug('Setting up ' + str(self._tunnel_operation))
55 if self._tunnel_operation == "encapsulation":
58 if settings.getValue('VSWITCH').endswith('Vanilla'):
59 self._setup_decap_vanilla()
63 def _setup_encap(self):
64 """ Sets up the switch for overlay P2P encapsulation test
66 Create 2 bridges br0 (integration bridge) and br-ext and a VXLAN port
69 self._logger.debug('Setup using ' + str(self._vswitch_class))
73 bridge = settings.getValue('TUNNEL_INTEGRATION_BRIDGE')
74 bridge_ext = settings.getValue('TUNNEL_EXTERNAL_BRIDGE')
75 bridge_ext_ip = settings.getValue('TUNNEL_EXTERNAL_BRIDGE_IP')
76 tg_port2_mac = settings.getValue('TRAFFICGEN_PORT2_MAC')
77 vtep_ip2 = settings.getValue('VTEP_IP2')
78 self._vswitch.add_switch(bridge)
80 tasks.run_task(['sudo', 'ifconfig', bridge,
81 settings.getValue('VTEP_IP1')],
82 self._logger, 'Assign ' +
83 settings.getValue('VTEP_IP1') + ' to ' + bridge,
86 tunnel_type = self._traffic['tunnel_type']
88 self._vswitch.add_switch(bridge_ext)
89 (_, phy1_number) = self._vswitch.add_phy_port(bridge)
90 (_, phy2_number) = self._vswitch.add_tunnel_port(bridge,
93 self._vswitch.add_phy_port(bridge_ext)
95 tasks.run_task(['sudo', 'ip', 'addr', 'add',
97 'dev', bridge_ext], self._logger, 'Assign ' +
98 bridge_ext_ip + ' to ' + bridge_ext)
100 tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', bridge_ext,
102 'Set ' + bridge_ext + 'status to up')
104 self._vswitch.add_route(bridge,
105 settings.getValue('VTEP_IP2_SUBNET'),
108 if settings.getValue('VSWITCH').endswith('Vanilla'):
109 tasks.run_task(['sudo', 'arp', '-s', vtep_ip2, tg_port2_mac],
111 'Set ' + bridge_ext + ' status to up')
113 self._vswitch.set_tunnel_arp(vtep_ip2,
117 # Test is unidirectional for now
118 self._vswitch.del_flow(bridge)
119 flow1 = add_ports_to_flow(_FLOW_TEMPLATE, phy1_number,
121 self._vswitch.add_flow(bridge, flow1)
127 def _setup_decap(self):
128 """ Sets up the switch for overlay P2P decapsulation test
130 self._logger.debug('Setup using ' + str(self._vswitch_class))
133 self._vswitch.start()
134 bridge = settings.getValue('TUNNEL_INTEGRATION_BRIDGE')
135 bridge_ext = settings.getValue('TUNNEL_EXTERNAL_BRIDGE')
136 bridge_ext_ip = settings.getValue('TUNNEL_EXTERNAL_BRIDGE_IP')
137 tgen_ip1 = settings.getValue('TRAFFICGEN_PORT1_IP')
138 self._vswitch.add_switch(bridge)
140 tasks.run_task(['sudo', 'ifconfig', bridge,
141 settings.getValue('VTEP_IP1')],
142 self._logger, 'Assign ' +
143 settings.getValue('VTEP_IP1') + ' to ' + bridge, False)
145 tunnel_type = self._traffic['tunnel_type']
147 self._vswitch.add_switch(bridge_ext)
148 self._vswitch.add_phy_port(bridge)
149 (_, phy2_number) = self._vswitch.add_phy_port(bridge_ext)
150 if tunnel_type == "vxlan":
151 vxlan_vni = 'options:key=' + settings.getValue('VXLAN_VNI')
152 (_, phy3_number) = self._vswitch.add_tunnel_port(bridge_ext,
157 (_, phy3_number) = self._vswitch.add_tunnel_port(bridge_ext,
160 tasks.run_task(['sudo', 'ip', 'addr', 'add',
163 self._logger, 'Assign ' +
165 + ' to ' + bridge_ext)
167 tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', bridge_ext,
170 'Set ' + bridge_ext + ' status to up')
172 self._vswitch.set_tunnel_arp(tgen_ip1,
173 settings.getValue('TRAFFICGEN_PORT1_MAC'),
175 # Test is unidirectional for now
176 self._vswitch.del_flow(bridge_ext)
177 flow1 = add_ports_to_flow(_FLOW_TEMPLATE, phy3_number,
179 self._vswitch.add_flow(bridge_ext, flow1)
185 def _setup_decap_vanilla(self):
186 """ Sets up the switch for overlay P2P decapsulation test
188 self._logger.debug('Setup decap vanilla ' + str(self._vswitch_class))
191 self._vswitch.start()
192 bridge = settings.getValue('TUNNEL_INTEGRATION_BRIDGE')
193 bridge_ext = settings.getValue('TUNNEL_EXTERNAL_BRIDGE')
194 bridge_ext_ip = settings.getValue('TUNNEL_EXTERNAL_BRIDGE_IP')
195 tgen_ip1 = settings.getValue('TRAFFICGEN_PORT1_IP')
196 self._vswitch.add_switch(bridge)
198 tasks.run_task(['sudo', 'ifconfig', bridge,
199 settings.getValue('TUNNEL_INT_BRIDGE_IP')],
200 self._logger, 'Assign ' +
201 settings.getValue('TUNNEL_INT_BRIDGE_IP') + ' to ' + bridge, False)
203 tunnel_type = self._traffic['tunnel_type']
205 self._vswitch.add_switch(bridge_ext)
206 self._vswitch.add_phy_port(bridge_ext)
207 (_, phy2_number) = self._vswitch.add_phy_port(bridge)
209 if tunnel_type == "vxlan":
210 vxlan_vni = 'options:key=' + settings.getValue('VXLAN_VNI')
211 self._vswitch.add_tunnel_port(bridge, tgen_ip1, tunnel_type,
214 self._vswitch.add_tunnel_port(bridge, tgen_ip1, tunnel_type)
216 tasks.run_task(['sudo', 'ip', 'addr', 'add',
219 self._logger, 'Assign ' +
221 + ' to ' + bridge_ext)
223 tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', bridge_ext,
226 'Set ' + bridge_ext + ' status to up')
228 tg_port2_mac = settings.getValue('TRAFFICGEN_PORT2_MAC')
229 vtep_ip2 = settings.getValue('TRAFFICGEN_PORT2_IP')
231 self._vswitch.set_tunnel_arp(vtep_ip2,
235 self._vswitch.add_route(bridge,
236 settings.getValue('VTEP_IP2_SUBNET'),
240 tasks.run_task(['sudo', 'arp', '-s', vtep_ip2, tg_port2_mac],
242 'Set ' + bridge_ext + ' status to up')
245 # Test is unidirectional for now
246 self._vswitch.del_flow(bridge_ext)
248 flow1 = add_ports_to_flow(_FLOW_TEMPLATE, phy2_number, 'LOCAL')
249 self._vswitch.add_flow(bridge_ext, flow1)
256 """Tears down the switch created in setup().
258 self._logger.debug('Stop using ' + str(self._vswitch_class))
264 def __exit__(self, type_, value, traceback):
267 def get_vswitch(self):
268 """See IVswitchController for description
272 def get_ports_info(self):
273 """See IVswitchController for description
275 self._logger.debug('get_ports_info using ' + str(self._vswitch_class))
276 return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))
278 def dump_vswitch_flows(self):
279 """See IVswitchController for description
281 self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))