1 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
18 from nfvbench.log import LOG
22 class Latency(object):
23 """A class to hold latency data."""
25 def __init__(self, latency_list=None):
26 """Create a latency instance.
28 latency_list: aggregate all latency values from list if not None
30 self.min_usec = sys.maxint
34 for lat in latency_list:
36 self.min_usec = min(self.min_usec, lat.min_usec)
37 self.max_usec = max(self.max_usec, lat.max_usec)
38 self.avg_usec += lat.avg_usec
39 # round to nearest usec
40 self.avg_usec = int(round(float(self.avg_usec) / len(latency_list)))
43 """Return True if latency information is available."""
44 return self.min_usec != sys.maxint
47 class TrafficGeneratorException(Exception):
48 """Exception for traffic generator."""
53 class AbstractTrafficGenerator(object):
55 def __init__(self, traffic_client):
56 self.traffic_client = traffic_client
57 self.generator_config = traffic_client.generator_config
58 self.config = traffic_client.config
61 def get_version(self):
62 # Must be implemented by sub classes
67 # Must be implemented by sub classes
71 def create_traffic(self, l2frame_size, rates, bidirectional, latency=True):
72 # Must be implemented by sub classes
75 def modify_rate(self, rate, reverse):
76 """Change the rate per port.
78 rate: new rate in % (0 to 100)
79 reverse: 0 for port 0, 1 for port 1
81 port_index = int(reverse)
82 port = self.port_handle[port_index]
83 self.rates[port_index] = traffic_utils.to_rate_str(rate)
84 LOG.info('Modified traffic stream for port %s, new rate=%s.', port, self.rates[port_index])
88 # Must be implemented by sub classes
92 def start_traffic(self):
93 # Must be implemented by sub classes
97 def stop_traffic(self):
98 # Must be implemented by sub classes
103 """Cleanup the traffic generator."""
106 def clear_streamblock(self):
107 """Clear all streams from the traffic generator."""
111 def resolve_arp(self):
112 """Resolve all configured remote IP addresses.
114 return: None if ARP failed to resolve for all IP addresses
115 else a dict of list of dest macs indexed by port#
116 the dest macs in the list are indexed by the chain id
122 """Return the local port MAC addresses.
124 return: a list of MAC addresses indexed by the port#
129 def get_port_speed_gbps(self):
130 """Return the local port speeds.
132 return: a list of speed in Gbps indexed by the port#