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
17 from nfvbench.utils import multiplier_map
19 # IMIX frame size including the 4-byte FCS field
20 IMIX_L2_SIZES = [64, 594, 1518]
21 IMIX_RATIOS = [7, 4, 1]
22 # weighted average l2 frame size includng the 4-byte FCS
23 IMIX_AVG_L2_FRAME_SIZE = sum(
24 [1.0 * imix[0] * imix[1] for imix in zip(IMIX_L2_SIZES, IMIX_RATIOS)]) / sum(IMIX_RATIOS)
27 def convert_rates(l2frame_size, rate, intf_speed):
28 """Convert a given rate unit into the other rate units.
30 l2frame_size: size of the L2 frame in bytes (includes 32-bit FCS) or 'IMIX'
31 rate: a dict that has at least one of the following key:
32 'rate_pps', 'rate_bps', 'rate_percent'
33 with the corresponding input value
34 intf_speed: the line rate speed in bits per second
36 avg_packet_size = get_average_packet_size(l2frame_size)
37 if 'rate_pps' in rate:
39 initial_rate_type = 'rate_pps'
40 pps = rate['rate_pps']
41 bps = pps_to_bps(pps, avg_packet_size)
42 load = bps_to_load(bps, intf_speed)
43 elif 'rate_bps' in rate:
44 # input = bits per second
45 initial_rate_type = 'rate_bps'
46 bps = rate['rate_bps']
47 load = bps_to_load(bps, intf_speed)
48 pps = bps_to_pps(bps, avg_packet_size)
49 elif 'rate_percent' in rate:
50 # input = percentage of the line rate (between 0.0 and 100.0)
51 initial_rate_type = 'rate_percent'
52 load = rate['rate_percent']
53 bps = load_to_bps(load, intf_speed)
54 pps = bps_to_pps(bps, avg_packet_size)
56 raise Exception('Traffic config needs to have a rate type key')
58 'initial_rate_type': initial_rate_type,
59 'rate_pps': int(float(pps)),
61 'rate_bps': int(float(bps))
65 def get_average_packet_size(l2frame_size):
66 """Retrieve the average L2 frame size
68 l2frame_size: an L2 frame size in bytes (including FCS) or 'IMIX'
69 return: average l2 frame size inlcuding the 32-bit FCS
71 if l2frame_size.upper() == 'IMIX':
72 return IMIX_AVG_L2_FRAME_SIZE
73 return float(l2frame_size)
76 def load_to_bps(load_percentage, intf_speed):
77 return float(load_percentage) / 100.0 * intf_speed
80 def bps_to_load(bps, intf_speed):
81 return float(bps) / intf_speed * 100.0
84 def bps_to_pps(bps, avg_packet_size):
85 return float(bps) / (avg_packet_size + 20.0) / 8
88 def pps_to_bps(pps, avg_packet_size):
89 return float(pps) * (avg_packet_size + 20.0) * 8
92 def weighted_avg(weight, count):
95 return sum([x[0] * x[1] for x in zip(weight, count)]) / sum(weight)
98 def _get_bitmath_rate(rate_bps):
99 rate = rate_bps.replace('ps', '').strip()
100 bitmath_rate = bitmath.parse_string(rate)
101 if bitmath_rate.bits <= 0:
102 raise Exception('%s is out of valid range' % rate_bps)
105 def parse_rate_str(rate_str):
106 if rate_str.endswith('pps'):
107 rate_pps = rate_str[:-3]
109 raise Exception('%s is missing a numeric value' % rate_str)
111 multiplier = multiplier_map[rate_pps[-1].upper()]
112 rate_pps = rate_pps[:-1]
115 rate_pps = int(float(rate_pps.strip()) * multiplier)
117 raise Exception('%s is out of valid range' % rate_str)
118 return {'rate_pps': str(rate_pps)}
119 if rate_str.endswith('ps'):
120 rate = rate_str.replace('ps', '').strip()
121 bit_rate = bitmath.parse_string(rate).bits
123 raise Exception('%s is out of valid range' % rate_str)
124 return {'rate_bps': str(int(bit_rate))}
125 if rate_str.endswith('%'):
126 rate_percent = float(rate_str.replace('%', '').strip())
127 if rate_percent <= 0 or rate_percent > 100.0:
128 raise Exception('%s is out of valid range (must be 1-100%%)' % rate_str)
129 return {'rate_percent': str(rate_percent)}
130 raise Exception('Unknown rate string format %s' % rate_str)
132 def get_load_from_rate(rate_str, avg_frame_size=64, line_rate='10Gbps'):
133 '''From any rate string (with unit) return the corresponding load (in % unit)
135 :param str rate_str: the rate to convert - must end with a unit (e.g. 1Mpps, 30%, 1Gbps)
136 :param int avg_frame_size: average frame size in bytes (needed only if pps is given)
137 :param str line_rate: line rate ending with bps unit (e.g. 1Mbps, 10Gbps) is the rate that
138 corresponds to 100% rate
139 :return float: the corresponding rate in % of line rate
141 rate_dict = parse_rate_str(rate_str)
142 if 'rate_percent' in rate_dict:
143 return float(rate_dict['rate_percent'])
144 lr_bps = _get_bitmath_rate(line_rate).bits
145 if 'rate_bps' in rate_dict:
146 bps = int(rate_dict['rate_bps'])
149 pps = rate_dict['rate_pps']
150 bps = pps_to_bps(pps, avg_frame_size)
151 return bps_to_load(bps, lr_bps)
153 def divide_rate(rate, divisor):
154 if 'rate_pps' in rate:
156 value = int(rate[key])
157 elif 'rate_bps' in rate:
159 value = int(rate[key])
162 value = float(rate[key])
165 rate[key] = str(value) if value else str(1)
169 def to_rate_str(rate):
170 if 'rate_pps' in rate:
171 pps = rate['rate_pps']
172 return '{}pps'.format(pps)
173 if 'rate_bps' in rate:
174 bps = rate['rate_bps']
175 return '{}bps'.format(bps)
176 if 'rate_percent' in rate:
177 load = rate['rate_percent']
178 return '{}%'.format(load)
180 # avert pylint warning
185 """Replaces every occurence of 'N/A' with float nan."""
186 for k, v in d.items():
187 if isinstance(v, dict):
194 """Converts MAC address to integer representation."""
195 return int(mac.translate(None, ":.- "), 16)
199 """Converts integer representation of MAC address to hex string."""
200 mac = format(i, 'x').zfill(12)
201 blocks = [mac[x:x + 2] for x in range(0, len(mac), 2)]
202 return ':'.join(blocks)