aboutsummaryrefslogtreecommitdiffstats
path: root/tools/extvswitchflctl.py
blob: 56eeafd0fb8a109757c5a5771ddf3723df66916f (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
"""
Tool to configure flows for Kubernetes Usecases.
"""

from tools import tasks
from conf import settings as S

class ExtVswitchFlowCtl(tasks.Process):
    """
    Virtual Switch Flow Control
    """
    def __init__(self):
        """
        Initialization
        """
        super().__init__()
        self._vpp_ctl = ['sudo', S.getValue('EXT_VSWITCH_VPP_FLOWCTL')]
        self._ovs_ctl = ['sudo', S.getValue('EXT_VSWITCH_OVS_FLOWCTL')]

    def get_vpp_interfaces(self):
        """
        Get VPP interfaces Names
        """
        ifargs = ['show', 'interface']
        output = self.run_vppctl(ifargs)
        ifaces = output[0].split('\n')
        pifaces = []
        vifaces = []
        for iface in ifaces:
            name = iface.split()[0]
            if 'Name' in name or 'local' in name:
                continue
            if 'Ethernet' in name:
                pifaces.append(name)
            if 'memif' in name:
                vifaces.append(name)
        assert len(vifaces) == 2 or len(vifaces) == 4
        assert len(pifaces) == 2
        assert pifaces[0][:-1] in pifaces[1][:-1]
        return pifaces, vifaces

    def add_connections(self):
        """
        Add Connections of OVS or VPP
        """
        if 'VPP' in S.getValue('EXT_VSWITCH_TYPE'):
            self.add_vpp_xconnect()
        else:
            self.add_ovs_xconnect()

    def add_ovs_xconnect(self):
        """
        Add connections to OVS
        """
        entries = [['--timeout', '10', '-o', 'OpenFlow13', 'add-flow',
                    S.getValue('EXT_VSWITCH_OVS_BRIDGE'),
                    'in_port=1,idle_timeout=0,action=output:3'],
                   ['--timeout', '10', '-o', 'OpenFlow13', 'add-flow',
                    S.getValue('EXT_VSWITCH_OVS_BRIDGE'),
                    'in_port=3,idle_timeout=0,action=output:1'],
                   ['--timeout', '10', '-o', 'OpenFlow13', 'add-flow',
                    S.getValue('EXT_VSWITCH_OVS_BRIDGE'),
                    'in_port=2,idle_timeout=0,action=output:4'],
                   ['--timeout', '10', '-o', 'OpenFlow13', 'add-flow',
                    S.getValue('EXT_VSWITCH_OVS_BRIDGE'),
                    'in_port=4,idle_timeout=0,action=output:2']]
        for entry in entries:
            self.run_ovsfctl(entry)

    def add_vpp_xconnect(self):
        """
        Add Connections to VPP
        """
        pifaces, vifaces = self.get_vpp_interfaces()
        # Bring Physical interface up - In case it is down.
        for piface in pifaces:
            ifargs = ['set', 'interface', 'state', piface, 'up']
            self.run_vppctl(ifargs)
        if len(vifaces) == 2:
            entries = [['test', 'l2patch', 'rx',
                        pifaces[0], 'tx', vifaces[0]],
                       ['test', 'l2patch', 'rx',
                        vifaces[0], 'tx', pifaces[0]],
                       ['test', 'l2patch', 'rx',
                        pifaces[1], 'tx', vifaces[1]],
                       ['test', 'l2patch', 'rx',
                        vifaces[1], 'tx', pifaces[1]]]
        elif len(vifaces) == 4:
            entries = [['test', 'l2patch', 'rx',
                        pifaces[0], 'tx', vifaces[0]],
                       ['test', 'l2patch', 'rx',
                        vifaces[0], 'tx', pifaces[0]],
                       ['test', 'l2patch', 'rx',
                        vifaces[1], 'tx', vifaces[2]],
                       ['test', 'l2patch', 'rx',
                        vifaces[2], 'tx', vifaces[1]],
                       ['test', 'l2patch', 'rx',
                        pifaces[1], 'tx', vifaces[3]],
                       ['test', 'l2patch', 'rx',
                        vifaces[3], 'tx', pifaces[1]]]

        for entry in entries:
            self.run_vppctl(entry)

    def run_ovsfctl(self, args, check_error=False):
        """Run ``ovs-ofctl`` with supplied arguments.

        :param args: Arguments to pass to ``vppctl``
        :param check_error: Throw exception on error

        :return: None
        """
        cmd = self._ovs_ctl + args
        return tasks.run_task(cmd, self._logger,
                              'Running ovs-ofctl...',
                              check_error)

    def run_vppctl(self, args, check_error=False):
        """Run ``vppctl`` with supplied arguments.

        :param args: Arguments to pass to ``vppctl``
        :param check_error: Throw exception on error

        :return: None
        """
        cmd = self._vpp_ctl + args
        return tasks.run_task(cmd, self._logger,
                              'Running vppctl...',
                              check_error)