summaryrefslogtreecommitdiffstats
path: root/nfvbench/chain_managers.py
blob: ab340bf03bc7230cb6cd29d5d5e621a6ec124a42 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python
# Copyright 2016 Cisco Systems, Inc.  All rights reserved.
#
#    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.
#
import time

from log import LOG
from network import Network
from packet_analyzer import PacketAnalyzer
from specs import ChainType
from stats_collector import IntervalCollector


class StageManager(object):
    """A class to stage resources in the systenm under test."""

    def __init__(self, config, cred, factory):
        self.config = config
        self.client = None
        # conditions due to EXT chain special cases
        if (config.vlan_tagging and not config.vlans) or not config.no_int_config:
            VM_CLASS = factory.get_stage_class(config.service_chain)
            self.client = VM_CLASS(config, cred)
            self.client.setup()

    def get_vlans(self):
        return self.client.get_vlans() if self.client else []

    def get_host_ips(self):
        return self.client.get_host_ips()

    def get_networks_uuids(self):
        return self.client.get_networks_uuids()

    def disable_port_security(self):
        self.client.disable_port_security()

    def get_vms(self):
        return self.client.vms

    def get_nets(self):
        return self.client.nets

    def get_ports(self):
        return self.client.ports

    def get_compute_nodes(self):
        return self.client.compute_nodes

    def set_vm_macs(self):
        if self.client and self.config.service_chain != ChainType.EXT:
            self.config.generator_config.set_vm_mac_list(self.client.get_end_port_macs())

    def close(self):
        if not self.config.no_cleanup and self.client:
            self.client.dispose()


class PVPStatsManager(object):
    """A class to generate traffic and extract results for PVP chains."""

    def __init__(self, config, clients, specs, factory, vlans, notifier=None):
        self.config = config
        self.clients = clients
        self.specs = specs
        self.notifier = notifier
        self.interval_collector = None
        self.vlans = vlans
        self.factory = factory
        self._setup()

    def set_vlan_tag(self, device, vlan):
        self.worker.set_vlan_tag(device, vlan)

    def _setup(self):
        WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
                                                     self.config.service_chain)
        self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
        try:
            self.worker.set_vlans(self.vlans)
            self._config_interfaces()
        except Exception as exc:
            # since the wrorker is up and running, we need to close it
            # in case of exception
            self.close()
            raise exc

    def _get_data(self):
        return self.worker.get_data() if self.worker else {}

    def _get_network(self, traffic_port, stats, reverse=False):
        """Get the Network object corresponding to a given TG port.

        :param traffic_port: must be either 0 or 1
        :param stats: TG stats for given traffic port
        :param reverse: specifies if the interface list for this network
        should go from TG to loopback point (reverse=false) or
        from loopback point to TG (reverse=true)
        """
        # build the interface list in fwd direction (TG To loopback point)
        interfaces = [self.clients['traffic'].get_interface(traffic_port, stats)]
        if self.worker:
            # if available,
            # interfaces for workers must be aligned on the TG port number
            interfaces.extend(self.worker.get_network_interfaces(traffic_port))
        # let Network reverse the interface order if needed
        return Network(interfaces, reverse)

    def _config_interfaces(self):
        if self.config.service_chain != ChainType.EXT:
            self.clients['vm'].disable_port_security()

        self.worker.config_interfaces()

    def _generate_traffic(self):
        if self.config.no_traffic:
            return {}

        self.interval_collector = IntervalCollector(time.time())
        self.interval_collector.attach_notifier(self.notifier)
        LOG.info('Starting to generate traffic...')
        stats = {}
        for stats in self.clients['traffic'].run_traffic():
            self.interval_collector.add(stats)

        LOG.info('...traffic generating ended.')
        return stats

    def get_stats(self):
        return self.interval_collector.get() if self.interval_collector else []

    def get_version(self):
        return self.worker.get_version() if self.worker else {}

    def run(self):
        """Run analysis in both direction and return the analysis."""
        if self.worker:
            self.worker.run()

        stats = self._generate_traffic()
        result = {
            'raw_data': self._get_data(),
            'packet_analysis': {},
            'stats': stats
        }

        # fetch latest stats from traffic gen
        stats = self.clients['traffic'].get_stats()
        LOG.info('Requesting packet analysis on the forward direction...')
        result['packet_analysis']['direction-forward'] = \
            self.get_analysis([self._get_network(0, stats),
                               self._get_network(1, stats, reverse=True)])
        LOG.info('Packet analysis on the forward direction completed')

        LOG.info('Requesting packet analysis on the reverse direction...')
        result['packet_analysis']['direction-reverse'] = \
            self.get_analysis([self._get_network(1, stats),
                               self._get_network(0, stats, reverse=True)])

        LOG.info('Packet analysis on the reverse direction completed')
        return result

    def get_compute_nodes_bios(self):
        return self.worker.get_compute_nodes_bios() if self.worker else {}

    @staticmethod
    def get_analysis(nets):
        LOG.info('Starting traffic analysis...')

        packet_analyzer = PacketAnalyzer()
        # Traffic types are assumed to always alternate in every chain. Add a no stats interface in
        # between if that is not the case.
        tx = True
        for network in nets:
            for interface in network.get_interfaces():
                packet_analyzer.record(interface, 'tx' if tx else 'rx')
                tx = not tx

        LOG.info('...traffic analysis completed')
        return packet_analyzer.get_analysis()

    def close(self):
        if self.worker:
            self.worker.close()


class PVVPStatsManager(PVPStatsManager):
    """A Class to generate traffic and extract results for PVVP chains."""

    def __init__(self, config, clients, specs, factory, vlans, notifier=None):
        PVPStatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)

    def run(self):
        """Run analysis in both direction and return the analysis."""
        fwd_v2v_net, rev_v2v_net = self.worker.run()

        stats = self._generate_traffic()
        result = {
            'raw_data': self._get_data(),
            'packet_analysis': {},
            'stats': stats
        }
        # fetch latest stats from traffic gen
        stats = self.clients['traffic'].get_stats()
        fwd_nets = [self._get_network(0, stats)]
        if fwd_v2v_net:
            fwd_nets.append(fwd_v2v_net)
        fwd_nets.append(self._get_network(1, stats, reverse=True))

        rev_nets = [self._get_network(1, stats)]
        if rev_v2v_net:
            rev_nets.append(rev_v2v_net)
        rev_nets.append(self._get_network(0, stats, reverse=True))

        LOG.info('Requesting packet analysis on the forward direction...')
        result['packet_analysis']['direction-forward'] = self.get_analysis(fwd_nets)
        LOG.info('Packet analysis on the forward direction completed')

        LOG.info('Requesting packet analysis on the reverse direction...')
        result['packet_analysis']['direction-reverse'] = self.get_analysis(rev_nets)

        LOG.info('Packet analysis on the reverse direction completed')
        return result


class EXTStatsManager(PVPStatsManager):
    """A Class to generate traffic and extract results for EXT chains."""

    def __init__(self, config, clients, specs, factory, vlans, notifier=None):
        PVPStatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)

    def _setup(self):
        if self.specs.openstack:
            WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
                                                         self.config.service_chain)
            self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
            self.worker.set_vlans(self.vlans)

            if not self.config.no_int_config:
                self._config_interfaces()
        else:
            self.worker = None