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
21 class Latency(object):
22 """A class to hold latency data."""
24 def __init__(self, latency_list=None):
25 """Create a latency instance.
27 latency_list: aggregate all latency values from list if not None
29 self.min_usec = sys.maxint
33 for lat in latency_list:
35 self.min_usec = min(self.min_usec, lat.min_usec)
36 self.max_usec = max(self.max_usec, lat.max_usec)
37 self.avg_usec += lat.avg_usec
38 # round to nearest usec
39 self.avg_usec = int(round(float(self.avg_usec) / len(latency_list)))
42 """Return True if latency information is available."""
43 return self.min_usec != sys.maxint
46 class TrafficGeneratorException(Exception):
47 """Exception for traffic generator."""
52 class AbstractTrafficGenerator(object):
53 def __init__(self, traffic_client):
54 self.traffic_client = traffic_client
55 self.generator_config = traffic_client.generator_config
56 self.config = traffic_client.config
57 self.imix_l2_sizes = [64, 594, 1518]
58 self.imix_ratios = [7, 4, 1]
59 self.imix_avg_l2_size = 0
60 self.adjust_imix_min_size(64)
63 def get_version(self):
64 # Must be implemented by sub classes
69 # Must be implemented by sub classes
73 def create_traffic(self, l2frame_size, rates, bidirectional, latency=True):
74 # Must be implemented by sub classes
77 def modify_rate(self, rate, reverse):
78 """Change the rate per port.
80 rate: new rate in % (0 to 100)
81 reverse: 0 for port 0, 1 for port 1
83 port_index = int(reverse)
84 port = self.port_handle[port_index]
85 self.rates[port_index] = traffic_utils.to_rate_str(rate)
86 LOG.info('Modified traffic stream for port %s, new rate=%s.', port, self.rates[port_index])
90 # Must be implemented by sub classes
94 def start_traffic(self):
95 # Must be implemented by sub classes
99 def stop_traffic(self):
100 # Must be implemented by sub classes
105 """Cleanup the traffic generator."""
108 def clear_streamblock(self):
109 """Clear all streams from the traffic generator."""
113 def resolve_arp(self):
114 """Resolve all configured remote IP addresses.
116 return: True if ARP resolved successfully
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#
136 def adjust_imix_min_size(self, min_size):
137 # assume the min size is always the first entry
138 self.imix_l2_sizes[0] = min_size
139 self.imix_avg_l2_size = sum(
140 [1.0 * imix[0] * imix[1] for imix in zip(self.imix_l2_sizes, self.imix_ratios)]) / sum(
142 traffic_utils.imix_avg_l2_size = self.imix_avg_l2_size