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
20 from network import Network
21 from packet_analyzer import PacketAnalyzer
22 from specs import ChainType
23 from stats_collector import IntervalCollector
26 class StageManager(object):
28 def __init__(self, config, cred, factory):
31 # conditions due to EXT chain special cases
32 if (config.vlan_tagging and not config.vlans) or not config.no_int_config:
33 VM_CLASS = factory.get_stage_class(config.service_chain)
34 self.client = VM_CLASS(config, cred)
38 return self.client.get_vlans() if self.client else []
40 def get_host_ips(self):
41 return self.client.get_host_ips()
43 def get_networks_uuids(self):
44 return self.client.get_networks_uuids()
46 def disable_port_security(self):
47 self.client.disable_port_security()
50 return self.client.vms
53 return self.client.nets
56 return self.client.ports
58 def get_compute_nodes(self):
59 return self.client.compute_nodes
61 def set_vm_macs(self):
62 if self.client and self.config.service_chain != ChainType.EXT:
63 self.config.generator_config.set_vm_mac_list(self.client.get_end_port_macs())
66 if not self.config.no_cleanup and self.client:
70 class StatsManager(object):
72 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
74 self.clients = clients
76 self.notifier = notifier
77 self.interval_collector = None
79 self.factory = factory
82 def set_vlan_tag(self, device, vlan):
83 self.worker.set_vlan_tag(device, vlan)
86 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
87 self.config.service_chain)
88 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
90 self.worker.set_vlans(self.vlans)
91 self._config_interfaces()
92 except Exception as exc:
93 # since the wrorker is up and running, we need to close it
94 # in case of exception
99 return self.worker.get_data()
101 def _get_network(self, traffic_port, index=None, reverse=False):
102 interfaces = [self.clients['traffic'].get_interface(traffic_port)]
103 interfaces.extend(self.worker.get_network_interfaces(index))
104 return Network(interfaces, reverse)
106 def _config_interfaces(self):
107 if self.config.service_chain != ChainType.EXT:
108 self.clients['vm'].disable_port_security()
110 self.worker.config_interfaces()
112 def _generate_traffic(self):
113 if self.config.no_traffic:
116 self.interval_collector = IntervalCollector(time.time())
117 self.interval_collector.attach_notifier(self.notifier)
118 LOG.info('Starting to generate traffic...')
120 for stats in self.clients['traffic'].run_traffic():
121 self.interval_collector.add(stats)
123 LOG.info('...traffic generating ended.')
127 return self.interval_collector.get() if self.interval_collector else []
129 def get_version(self):
130 return self.worker.get_version()
134 Run analysis in both direction and return the analysis
138 stats = self._generate_traffic()
140 'raw_data': self._get_data(),
141 'packet_analysis': {},
145 LOG.info('Requesting packet analysis on the forward direction...')
146 result['packet_analysis']['direction-forward'] = \
147 self.get_analysis([self._get_network(0, 0),
148 self._get_network(0, 1, reverse=True)])
149 LOG.info('Packet analysis on the forward direction completed')
151 LOG.info('Requesting packet analysis on the reverse direction...')
152 result['packet_analysis']['direction-reverse'] = \
153 self.get_analysis([self._get_network(1, 1),
154 self._get_network(1, 0, reverse=True)])
156 LOG.info('Packet analysis on the reverse direction completed')
159 def get_compute_nodes_bios(self):
160 return self.worker.get_compute_nodes_bios()
163 def get_analysis(nets):
164 LOG.info('Starting traffic analysis...')
166 packet_analyzer = PacketAnalyzer()
167 # Traffic types are assumed to always alternate in every chain. Add a no stats interface in
168 # between if that is not the case.
171 for interface in network.get_interfaces():
172 packet_analyzer.record(interface, 'tx' if tx else 'rx')
175 LOG.info('...traffic analysis completed')
176 return packet_analyzer.get_analysis()
182 class PVPStatsManager(StatsManager):
184 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
185 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
188 class PVVPStatsManager(StatsManager):
190 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
191 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
195 Run analysis in both direction and return the analysis
197 fwd_v2v_net, rev_v2v_net = self.worker.run()
199 stats = self._generate_traffic()
201 'raw_data': self._get_data(),
202 'packet_analysis': {},
206 fwd_nets = [self._get_network(0, 0)]
208 fwd_nets.append(fwd_v2v_net)
209 fwd_nets.append(self._get_network(0, 1, reverse=True))
211 rev_nets = [self._get_network(1, 1)]
213 rev_nets.append(rev_v2v_net)
214 rev_nets.append(self._get_network(1, 0, reverse=True))
216 LOG.info('Requesting packet analysis on the forward direction...')
217 result['packet_analysis']['direction-forward'] = self.get_analysis(fwd_nets)
218 LOG.info('Packet analysis on the forward direction completed')
220 LOG.info('Requesting packet analysis on the reverse direction...')
221 result['packet_analysis']['direction-reverse'] = self.get_analysis(rev_nets)
223 LOG.info('Packet analysis on the reverse direction completed')
227 class EXTStatsManager(StatsManager):
228 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
229 StatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
232 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
233 self.config.service_chain)
234 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
235 self.worker.set_vlans(self.vlans)
237 if not self.config.no_int_config:
238 self._config_interfaces()