2 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 from network import Network
19 from packet_analyzer import PacketAnalyzer
20 from specs import ChainType
21 from stats_collector import IntervalCollector
25 class StageManager(object):
27 def __init__(self, config, cred, factory):
30 # conditions due to EXT chain special cases
31 if (config.vlan_tagging and not config.vlans) or not config.no_int_config:
32 VM_CLASS = factory.get_stage_class(config.service_chain)
33 self.client = VM_CLASS(config, cred)
37 return self.client.get_vlans() if self.client else []
39 def get_host_ips(self):
40 return self.client.get_host_ips()
42 def get_networks_uuids(self):
43 return self.client.get_networks_uuids()
45 def disable_port_security(self):
46 self.client.disable_port_security()
49 return self.client.vms
52 return self.client.nets
55 return self.client.ports
57 def get_compute_nodes(self):
58 return self.client.compute_nodes
60 def set_vm_macs(self):
61 if self.client and self.config.service_chain != ChainType.EXT:
62 self.config.generator_config.set_vm_mac_list(self.client.get_end_port_macs())
65 if not self.config.no_cleanup and self.client:
69 class StatsManager(object):
71 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
73 self.clients = clients
75 self.notifier = notifier
76 self.interval_collector = None
78 self.factory = factory
81 def set_vlan_tag(self, device, vlan):
82 self.worker.set_vlan_tag(device, vlan)
85 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
86 self.config.service_chain)
87 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
88 self.worker.set_vlans(self.vlans)
89 self._config_interfaces()
92 return self.worker.get_data()
94 def _get_network(self, traffic_port, index=None, reverse=False):
95 interfaces = [self.clients['traffic'].get_interface(traffic_port)]
96 interfaces.extend(self.worker.get_network_interfaces(index))
97 return Network(interfaces, reverse)
99 def _config_interfaces(self):
100 if self.config.service_chain != ChainType.EXT:
101 self.clients['vm'].disable_port_security()
103 self.worker.config_interfaces()
105 def _generate_traffic(self):
106 if self.config.no_traffic:
109 self.interval_collector = IntervalCollector(time.time())
110 self.interval_collector.attach_notifier(self.notifier)
111 LOG.info('Starting to generate traffic...')
113 for stats in self.clients['traffic'].run_traffic():
114 self.interval_collector.add(stats)
116 LOG.info('...traffic generating ended.')
120 return self.interval_collector.get() if self.interval_collector else []
122 def get_version(self):
123 return self.worker.get_version()
127 Run analysis in both direction and return the analysis
131 stats = self._generate_traffic()
133 'raw_data': self._get_data(),
134 'packet_analysis': {},
138 LOG.info('Requesting packet analysis on the forward direction...')
139 result['packet_analysis']['direction-forward'] = \
140 self.get_analysis([self._get_network(0, 0),
141 self._get_network(0, 1, reverse=True)])
142 LOG.info('Packet analysis on the forward direction completed')
144 LOG.info('Requesting packet analysis on the reverse direction...')
145 result['packet_analysis']['direction-reverse'] = \
146 self.get_analysis([self._get_network(1, 1),
147 self._get_network(1, 0, reverse=True)])
149 LOG.info('Packet analysis on the reverse direction completed')
152 def get_compute_nodes_bios(self):
153 return self.worker.get_compute_nodes_bios()
156 def get_analysis(nets):
157 LOG.info('Starting traffic analysis...')
159 packet_analyzer = PacketAnalyzer()
160 # Traffic types are assumed to always alternate in every chain. Add a no stats interface in
161 # between if that is not the case.
164 for interface in network.get_interfaces():
165 packet_analyzer.record(interface, 'tx' if tx else 'rx')
168 LOG.info('...traffic analysis completed')
169 return packet_analyzer.get_analysis()
175 class PVPStatsManager(StatsManager):
177 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
178 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
181 class PVVPStatsManager(StatsManager):
183 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
184 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
188 Run analysis in both direction and return the analysis
190 fwd_v2v_net, rev_v2v_net = self.worker.run()
192 stats = self._generate_traffic()
194 'raw_data': self._get_data(),
195 'packet_analysis': {},
199 fwd_nets = [self._get_network(0, 0)]
201 fwd_nets.append(fwd_v2v_net)
202 fwd_nets.append(self._get_network(0, 1, reverse=True))
204 rev_nets = [self._get_network(1, 1)]
206 rev_nets.append(rev_v2v_net)
207 rev_nets.append(self._get_network(1, 0, reverse=True))
209 LOG.info('Requesting packet analysis on the forward direction...')
210 result['packet_analysis']['direction-forward'] = self.get_analysis(fwd_nets)
211 LOG.info('Packet analysis on the forward direction completed')
213 LOG.info('Requesting packet analysis on the reverse direction...')
214 result['packet_analysis']['direction-reverse'] = self.get_analysis(rev_nets)
216 LOG.info('Packet analysis on the reverse direction completed')
220 class EXTStatsManager(StatsManager):
221 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
222 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
225 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
226 self.config.service_chain)
227 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
228 self.worker.set_vlans(self.vlans)
230 if not self.config.no_int_config:
231 self._config_interfaces()