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
19 from network import Network
20 from packet_analyzer import PacketAnalyzer
21 from specs import ChainType
22 from stats_collector import IntervalCollector
25 class StageManager(object):
26 """A class to stage resources in the systenm under test."""
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 PVPStatsManager(object):
71 """A class to generate traffic and extract results for PVP chains."""
73 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
75 self.clients = clients
77 self.notifier = notifier
78 self.interval_collector = None
80 self.factory = factory
83 def set_vlan_tag(self, device, vlan):
84 self.worker.set_vlan_tag(device, vlan)
87 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
88 self.config.service_chain)
89 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
91 self.worker.set_vlans(self.vlans)
92 self._config_interfaces()
93 except Exception as exc:
94 # since the wrorker is up and running, we need to close it
95 # in case of exception
100 return self.worker.get_data() if self.worker else {}
102 def _get_network(self, traffic_port, stats, reverse=False):
103 """Get the Network object corresponding to a given TG port.
105 :param traffic_port: must be either 0 or 1
106 :param stats: TG stats for given traffic port
107 :param reverse: specifies if the interface list for this network
108 should go from TG to loopback point (reverse=false) or
109 from loopback point to TG (reverse=true)
111 # build the interface list in fwd direction (TG To loopback point)
112 interfaces = [self.clients['traffic'].get_interface(traffic_port, stats)]
115 # interfaces for workers must be aligned on the TG port number
116 interfaces.extend(self.worker.get_network_interfaces(traffic_port))
117 # let Network reverse the interface order if needed
118 return Network(interfaces, reverse)
120 def _config_interfaces(self):
121 if self.config.service_chain != ChainType.EXT:
122 self.clients['vm'].disable_port_security()
124 self.worker.config_interfaces()
126 def _generate_traffic(self):
127 if self.config.no_traffic:
130 self.interval_collector = IntervalCollector(time.time())
131 self.interval_collector.attach_notifier(self.notifier)
132 LOG.info('Starting to generate traffic...')
134 for stats in self.clients['traffic'].run_traffic():
135 self.interval_collector.add(stats)
137 LOG.info('...traffic generating ended.')
141 return self.interval_collector.get() if self.interval_collector else []
143 def get_version(self):
144 return self.worker.get_version() if self.worker else {}
147 """Run analysis in both direction and return the analysis."""
151 stats = self._generate_traffic()
153 'raw_data': self._get_data(),
154 'packet_analysis': {},
158 # fetch latest stats from traffic gen
159 stats = self.clients['traffic'].get_stats()
160 LOG.info('Requesting packet analysis on the forward direction...')
161 result['packet_analysis']['direction-forward'] = \
162 self.get_analysis([self._get_network(0, stats),
163 self._get_network(1, stats, reverse=True)])
164 LOG.info('Packet analysis on the forward direction completed')
166 LOG.info('Requesting packet analysis on the reverse direction...')
167 result['packet_analysis']['direction-reverse'] = \
168 self.get_analysis([self._get_network(1, stats),
169 self._get_network(0, stats, reverse=True)])
171 LOG.info('Packet analysis on the reverse direction completed')
174 def get_compute_nodes_bios(self):
175 return self.worker.get_compute_nodes_bios() if self.worker else {}
178 def get_analysis(nets):
179 LOG.info('Starting traffic analysis...')
181 packet_analyzer = PacketAnalyzer()
182 # Traffic types are assumed to always alternate in every chain. Add a no stats interface in
183 # between if that is not the case.
186 for interface in network.get_interfaces():
187 packet_analyzer.record(interface, 'tx' if tx else 'rx')
190 LOG.info('...traffic analysis completed')
191 return packet_analyzer.get_analysis()
198 class PVVPStatsManager(PVPStatsManager):
199 """A Class to generate traffic and extract results for PVVP chains."""
201 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
202 PVPStatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
205 """Run analysis in both direction and return the analysis."""
206 fwd_v2v_net, rev_v2v_net = self.worker.run()
208 stats = self._generate_traffic()
210 'raw_data': self._get_data(),
211 'packet_analysis': {},
214 # fetch latest stats from traffic gen
215 stats = self.clients['traffic'].get_stats()
216 fwd_nets = [self._get_network(0, stats)]
218 fwd_nets.append(fwd_v2v_net)
219 fwd_nets.append(self._get_network(1, stats, reverse=True))
221 rev_nets = [self._get_network(1, stats)]
223 rev_nets.append(rev_v2v_net)
224 rev_nets.append(self._get_network(0, stats, reverse=True))
226 LOG.info('Requesting packet analysis on the forward direction...')
227 result['packet_analysis']['direction-forward'] = self.get_analysis(fwd_nets)
228 LOG.info('Packet analysis on the forward direction completed')
230 LOG.info('Requesting packet analysis on the reverse direction...')
231 result['packet_analysis']['direction-reverse'] = self.get_analysis(rev_nets)
233 LOG.info('Packet analysis on the reverse direction completed')
237 class EXTStatsManager(PVPStatsManager):
238 """A Class to generate traffic and extract results for EXT chains."""
240 def __init__(self, config, clients, specs, factory, vlans, notifier=None):
241 PVPStatsManager.__init__(self, config, clients, specs, factory, vlans, notifier)
244 if self.specs.openstack:
245 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
246 self.config.service_chain)
247 self.worker = WORKER_CLASS(self.config, self.clients, self.specs)
248 self.worker.set_vlans(self.vlans)
250 if not self.config.no_int_config:
251 self._config_interfaces()