aboutsummaryrefslogtreecommitdiffstats
path: root/core/vswitch_controller_ptunp.py
blob: b10da2a912527cffe82324a9dcfeef4fd103d08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2015-2018 Intel Corporation., Tieto
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""VSwitch controller for Physical to VxLAN Tunnel Endpoint to Physical
   deployment with mod operation.
"""
from netaddr import EUI, IPNetwork, mac_unix

from core.vswitch_controller import IVswitchController
from vswitches.utils import add_ports_to_flow
from conf import settings
from tools import tasks

class VswitchControllerPtunP(IVswitchController):
    """VSwitch controller for VxLAN ptunp deployment scenario.
    The deployment scenario is to test VxLAN tunneling feature without using an
    overlay ingress traffic. The VxLAN encap and decap performed in the virtual
    switch in each direction.

    Attributes:
        _vswitch_class: The vSwitch class to be used.
        _vswitch: The vSwitch object controlled by this controller
        _deployment_scenario: A string describing the scenario to set-up in the
            constructor.
    """
    def __init__(self, deployment, vswitch_class, traffic):
        """See IVswitchController for general description
        """
        super().__init__(deployment, vswitch_class, traffic)
        self.bridge_phy1 = settings.getValue('TUNNEL_EXTERNAL_BRIDGE1')
        self.bridge_phy2 = settings.getValue('TUNNEL_EXTERNAL_BRIDGE2')
        self.bridge_mod1 = settings.getValue('TUNNEL_MODIFY_BRIDGE1')
        self.bridge_mod2 = settings.getValue('TUNNEL_MODIFY_BRIDGE2')
        self.br_mod_mac1 = settings.getValue('TUNNEL_MODIFY_BRIDGE_MAC1')
        self.br_mod_mac2 = settings.getValue('TUNNEL_MODIFY_BRIDGE_MAC2')
        self.br_mod_ip1 = settings.getValue('TUNNEL_MODIFY_BRIDGE_IP1')
        self.br_mod_ip2 = settings.getValue('TUNNEL_MODIFY_BRIDGE_IP2')
        self.tunnel_type = settings.getValue('TUNNEL_TYPE')

    def setup(self):
        """ Sets up the switch for VxLAN overlay PTUNP (tunnel encap or decap)
        """
        self._logger.debug('Setting up phy-tun-phy tunneling scenario')
        if self.tunnel_type == 'vxlan':
            self._setup_vxlan_encap_decap()
        else:
            self._logger.error("Only VxLAN is supported for now")
            raise NotImplementedError

    def _setup_vxlan_encap_decap(self):
        """ Sets up switches for VxLAN overlay P-TUN-P test.

            Create 2 bridges br-phy1 and br-phy2 (The bridge to connect
            physical ports. Two more bridges br-mod1 and br-mod2 to mangle
            and redirect the packets from one tunnel port to other.
        """
        self._logger.debug('Setup using %s', str(self._vswitch_class))
        try:
            self._vswitch.start()
            self._vswitch.add_switch(self.bridge_phy1)
            self._vswitch.add_switch(self.bridge_phy2)
            self._vswitch.add_switch(self.bridge_mod1,
                                     params=["other_config:hwaddr=" +
                                             self.br_mod_mac1
                                            ])
            self._vswitch.add_switch(self.bridge_mod2,
                                     params=["other_config:hwaddr=" +
                                             self.br_mod_mac2
                                            ])

            tasks.run_task(['sudo', 'iptables', '-F'],
                           self._logger, 'Clean ip tables',
                           False)
            tasks.run_task(['sudo', 'ip', 'addr', 'add',
                            self.br_mod_ip1, 'dev', self.bridge_mod1],
                           self._logger, 'Assign ' +
                           self.br_mod_ip1 + ' to ' + self.bridge_mod1,
                           False)
            tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', self.bridge_mod1, 'up'],
                           self._logger, 'Bring up ' + self.bridge_mod1, False)

            tasks.run_task(['sudo', 'ip', 'addr', 'add',
                            self.br_mod_ip2, 'dev', self.bridge_mod2],
                           self._logger, 'Assign ' +
                           self.br_mod_ip2 + ' to ' + self.bridge_mod2,
                           False)
            tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', self.bridge_mod2, 'up'],
                           self._logger, 'Bring up ' + self.bridge_mod2, False)

            tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', self.bridge_phy1, 'up'],
                           self._logger, 'Bring up ' + self.bridge_phy1, False)
            tasks.run_task(['sudo', 'ip', 'link', 'set', 'dev', self.bridge_phy2, 'up'],
                           self._logger, 'Bring up ' + self.bridge_phy2, False)
            self._vswitch.add_route(self.bridge_phy1, self.br_mod_ip1, self.bridge_mod1)
            self._vswitch.add_route(self.bridge_phy2, self.br_mod_ip2, self.bridge_mod2)

            # Create tunnel ip and mac from the bridge ips
            vxlan_local_ip1 = str(IPNetwork(self.br_mod_ip1).ip)
            vxlan_local_ip2 = str(IPNetwork(self.br_mod_ip2).ip)
            vxlan_rem_ip1 = str(IPNetwork(self.br_mod_ip1).ip + 1)
            vxlan_rem_ip2 = str(IPNetwork(self.br_mod_ip2).ip + 1)
            vxlan_rem_mac1 = EUI(int(EUI(self.br_mod_mac1)) + 1)
            vxlan_rem_mac1.dialect = mac_unix
            vxlan_rem_mac2 = EUI(int(EUI(self.br_mod_mac2)) + 1)
            vxlan_rem_mac2.dialect = mac_unix
            self._vswitch.set_tunnel_arp(vxlan_local_ip1, self.br_mod_mac1,
                                         self.bridge_phy1)
            self._vswitch.set_tunnel_arp(vxlan_local_ip2, self.br_mod_mac2,
                                         self.bridge_phy2)
            self._vswitch.set_tunnel_arp(vxlan_rem_ip1, str(vxlan_rem_mac1),
                                         self.bridge_mod1)
            self._vswitch.set_tunnel_arp(vxlan_rem_ip2, str(vxlan_rem_mac2),
                                         self.bridge_mod2)

            # Lets add the ports to bridges
            (_, phy1_number) = self._vswitch.add_phy_port(self.bridge_phy1)
            (_, phy2_number) = self._vswitch.add_phy_port(self.bridge_phy2)
            vxlan_vni = 'options:key=' + settings.getValue('VXLAN_VNI')
            (_, phy3_number) = self._vswitch.add_tunnel_port(self.bridge_phy1,
                                                             vxlan_rem_ip1,
                                                             "vxlan",
                                                             params=[vxlan_vni])
            (_, phy4_number) = self._vswitch.add_tunnel_port(self.bridge_phy2,
                                                             vxlan_rem_ip2,
                                                             "vxlan",
                                                             params=[vxlan_vni])
            [(_, phy5_number), (_, phy6_number)] = \
                     self._vswitch.add_veth_pair_port(self.bridge_mod1, self.bridge_mod2)

            # Set up flows for the switches
            self._vswitch.del_flow(self.bridge_phy1)
            self._vswitch.del_flow(self.bridge_phy2)
            self._vswitch.del_flow(self.bridge_mod1)
            self._vswitch.del_flow(self.bridge_mod2)
            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy1_number,
                                     phy3_number)
            self._vswitch.add_flow(self.bridge_phy1, flow)
            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy3_number,
                                     phy1_number)
            self._vswitch.add_flow(self.bridge_phy1, flow)

            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy2_number,
                                     phy4_number)
            self._vswitch.add_flow(self.bridge_phy2, flow)
            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy4_number,
                                     phy2_number)
            self._vswitch.add_flow(self.bridge_phy2, flow)
            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy5_number,
                                     'LOCAL')
            self._vswitch.add_flow(self.bridge_mod1, flow)
            mod_flow_template = settings.getValue('OVS_FLOW_TEMPLATE').copy()
            mod_flow_template.update({'ip':'',
                                      'actions':
                                      ['mod_dl_src:' + str(vxlan_rem_mac2),
                                       'mod_dl_dst:' + self.br_mod_mac2,
                                       'mod_nw_src:' + vxlan_rem_ip2,
                                       'mod_nw_dst:' + vxlan_local_ip2
                                      ]
                                     })
            flow = add_ports_to_flow(mod_flow_template, 'LOCAL', phy5_number)
            self._vswitch.add_flow(self.bridge_mod1, flow)
            flow = add_ports_to_flow(settings.getValue('OVS_FLOW_TEMPLATE'), phy6_number,
                                     'LOCAL')
            self._vswitch.add_flow(self.bridge_mod2, flow)
            mod_flow_template = settings.getValue('OVS_FLOW_TEMPLATE').copy()
            mod_flow_template.update({'ip':'',
                                      'actions':
                                      ['mod_dl_src:' + str(vxlan_rem_mac1),
                                       'mod_dl_dst:' + self.br_mod_mac1,
                                       'mod_nw_src:' + vxlan_rem_ip1,
                                       'mod_nw_dst:' + vxlan_local_ip1]
                                     })
            flow = add_ports_to_flow(mod_flow_template, 'LOCAL', phy6_number)
            self._vswitch.add_flow(self.bridge_mod2, flow)

        except:
            self._vswitch.stop()
            raise

    def stop(self):
        """Tears down the switch created in setup().
        """
        self._logger.debug('Stop using %s', str(self._vswitch_class))
        self._vswitch.stop()

    def get_ports_info(self):
        """See IVswitchController for description
        """
        self._logger.debug('get_ports_info using %s', str(self._vswitch_class))
        ports = self._vswitch.get_ports(self.bridge_phy1) +\
                self._vswitch.get_ports(self.bridge_mod1) +\
                self._vswitch.get_ports(self.bridge_phy2) +\
                self._vswitch.get_ports(self.bridge_mod2)
        return ports

    def dump_vswitch_connections(self):
        """See IVswitchController for description
        """
        self._logger.debug('dump_connections using %s', str(self._vswitch_class))
        self._vswitch.dump_connections(self.bridge_phy1)
        self._vswitch.dump_connections(self.bridge_mod1)
        self._vswitch.dump_connections(self.bridge_phy2)
        self._vswitch.dump_connections(self.bridge_mod2)