# under the License.
"""Interface to the traffic generator clients including NDR/PDR binary search."""
-
-from datetime import datetime
+from math import gcd
import socket
import struct
import time
from attrdict import AttrDict
import bitmath
+from hdrh.histogram import HdrHistogram
from netaddr import IPNetwork
# pylint: disable=import-error
from trex.stl.api import Ether
raise IndexError('Index out of bounds: %d (max=%d)' % (index, self.max_available))
return Device.int_to_ip(self.base_ip_int + index * self.step)
- def reserve_ip_range(self, count):
- """Reserve a range of count consecutive IP addresses spaced by step."""
- if self.next_free + count > self.max_available:
+ def reserve_ip_range(self, count, force_ip_reservation=False):
+ """Reserve a range of count consecutive IP addresses spaced by step.
+ force_ip_reservation parameter allows to continue the calculation of IPs when
+ the 2 sides (ports) have different size and the flow is greater than
+ the size as well.
+ """
+ if self.next_free + count > self.max_available and force_ip_reservation is False:
raise IndexError('No more IP addresses next free=%d max_available=%d requested=%d' %
(self.next_free,
self.max_available,
count))
- first_ip = self.get_ip(self.next_free)
- last_ip = self.get_ip(self.next_free + count - 1)
- self.next_free += count
+ if self.next_free + count > self.max_available and force_ip_reservation is True:
+ first_ip = self.get_ip(self.next_free)
+ last_ip = self.get_ip(self.next_free + self.max_available - 1)
+ self.next_free += self.max_available
+ else:
+ first_ip = self.get_ip(self.next_free)
+ last_ip = self.get_ip(self.next_free + count - 1)
+ self.next_free += count
return (first_ip, last_ip)
def reset_reservation(self):
self.next_free = 0
+class UdpPorts(object):
+
+ def __init__(self, src_min, src_max, dst_min, dst_max, step):
+
+ self.src_min = src_min
+ self.src_max = src_max
+ self.dst_min = dst_min
+ self.dst_max = dst_max
+ self.step = step
+
+
class Device(object):
"""Represent a port device and all information associated to it.
"""Create a new device for a given port."""
self.generator_config = generator_config
self.chain_count = generator_config.service_chain_count
- self.flow_count = generator_config.flow_count / 2
+ if generator_config.bidirectional:
+ self.flow_count = generator_config.flow_count / 2
+ else:
+ self.flow_count = generator_config.flow_count
+
self.port = port
self.switch_port = generator_config.interfaces[port].get('switch_port', None)
self.vtep_vlan = None
self.vnis = None
self.vlans = None
self.ip_addrs = generator_config.ip_addrs[port]
- subnet = IPNetwork(self.ip_addrs)
- self.ip = subnet.ip.format()
+ self.ip_src_static = generator_config.ip_src_static
self.ip_addrs_step = generator_config.ip_addrs_step
- self.ip_block = IpBlock(self.ip, self.ip_addrs_step, self.flow_count)
+ if self.ip_addrs_step == 'random':
+ # Set step to 1 to calculate the IP range size (see check_ip_size below)
+ step = '0.0.0.1'
+ else:
+ step = self.ip_addrs_step
+ self.ip_size = self.check_ipsize(IPNetwork(self.ip_addrs).size, Device.ip_to_int(step))
+ self.ip = str(IPNetwork(self.ip_addrs).network)
+ ip_addrs_left = generator_config.ip_addrs[0]
+ ip_addrs_right = generator_config.ip_addrs[1]
+ self.ip_addrs_size = {
+ 'left': self.check_ipsize(IPNetwork(ip_addrs_left).size, Device.ip_to_int(step)),
+ 'right': self.check_ipsize(IPNetwork(ip_addrs_right).size, Device.ip_to_int(step))}
+ udp_src_port = generator_config.gen_config.udp_src_port
+ if udp_src_port is None:
+ udp_src_port = 53
+ udp_dst_port = generator_config.gen_config.udp_dst_port
+ if udp_dst_port is None:
+ udp_dst_port = 53
+ src_max, src_min = self.define_udp_range(udp_src_port, 'udp_src_port')
+ dst_max, dst_min = self.define_udp_range(udp_dst_port, 'udp_dst_port')
+ udp_src_range = int(src_max) - int(src_min) + 1
+ udp_dst_range = int(dst_max) - int(dst_min) + 1
+ lcm_port = self.lcm(udp_src_range, udp_dst_range)
+ if self.ip_src_static is True:
+ lcm_ip = self.lcm(1, min(self.ip_addrs_size['left'], self.ip_addrs_size['right']))
+ else:
+ lcm_ip = self.lcm(self.ip_addrs_size['left'], self.ip_addrs_size['right'])
+ flow_max = self.lcm(lcm_port, lcm_ip)
+ if self.flow_count > flow_max:
+ raise TrafficClientException('Trying to set unachievable traffic (%d > %d)' %
+ (self.flow_count, flow_max))
+
+ # manage udp range regarding flow count value
+ # UDP dst range is greater than FC => range will be limited to min + FC
+ if self.flow_count <= udp_dst_range:
+ dst_max = int(dst_min) + self.flow_count - 1
+ # UDP src range is greater than FC => range will be limited to min + FC
+ if self.flow_count <= udp_src_range:
+ src_max = int(src_min) + self.flow_count - 1
+ # Define IP block limit regarding flow count
+ if self.flow_count <= self.ip_size:
+ self.ip_block = IpBlock(self.ip, step, self.flow_count)
+ else:
+ self.ip_block = IpBlock(self.ip, step, self.ip_size)
+
+ self.udp_ports = UdpPorts(src_min, src_max, dst_min, dst_max,
+ generator_config.gen_config.udp_port_step)
self.gw_ip_block = IpBlock(generator_config.gateway_ips[port],
generator_config.gateway_ip_addrs_step,
self.chain_count)
self.tg_gw_ip_block = IpBlock(self.tg_gateway_ip_addrs,
generator_config.tg_gateway_ip_addrs_step,
self.chain_count)
- self.udp_src_port = generator_config.udp_src_port
- self.udp_dst_port = generator_config.udp_dst_port
+
+ @staticmethod
+ def define_udp_range(udp_port, property_name):
+ if isinstance(udp_port, int):
+ min = udp_port
+ max = min
+ elif isinstance(udp_port, tuple):
+ min = udp_port[0]
+ max = udp_port[1]
+ else:
+ raise TrafficClientException('Invalid %s property value (53 or [\'53\',\'1024\'])'
+ % property_name)
+ return max, min
+
+ @staticmethod
+ def lcm(a, b):
+ """Calculate the maximum possible value for both IP and ports,
+ eventually for maximum possible flux."""
+ if a != 0 and b != 0:
+ lcm_value = a * b // gcd(a, b)
+ return lcm_value
+ raise TypeError(" IP size or port range can't be zero !")
+
+ @staticmethod
+ def check_ipsize(ip_size, step):
+ """Check and set the available IPs, considering the step."""
+ try:
+ if ip_size % step == 0:
+ value = int(ip_size / step)
+ else:
+ value = int((ip_size / step)) + 1
+ return value
+ except ZeroDivisionError:
+ raise ZeroDivisionError("step can't be zero !")
def set_mac(self, mac):
"""Set the local MAC for this port device."""
# example 11 flows and 3 chains => 3, 4, 4
flows_per_chain = int((self.flow_count + self.chain_count - 1) / self.chain_count)
cur_chain_flow_count = int(self.flow_count - flows_per_chain * (self.chain_count - 1))
+ force_ip_reservation = False
+ # use case example of this parameter:
+ # - static IP addresses (source & destination), netmask = /30
+ # - 4 varying UDP source ports | 1 UDP destination port
+ # - Flow count = 8
+ # --> parameter 'reserve_ip_range' should have flag 'force_ip_reservation'
+ # in order to assign the maximum available IP on each iteration
+ if self.ip_size < cur_chain_flow_count \
+ or self.ip_addrs_size['left'] != self.ip_addrs_size['right']:
+ force_ip_reservation = True
+
peer = self.get_peer_device()
self.ip_block.reset_reservation()
peer.ip_block.reset_reservation()
dest_macs = self.get_dest_macs()
for chain_idx in range(self.chain_count):
- src_ip_first, src_ip_last = self.ip_block.reserve_ip_range(cur_chain_flow_count)
- dst_ip_first, dst_ip_last = peer.ip_block.reserve_ip_range(cur_chain_flow_count)
+ src_ip_first, src_ip_last = self.ip_block.reserve_ip_range\
+ (cur_chain_flow_count, force_ip_reservation)
+ dst_ip_first, dst_ip_last = peer.ip_block.reserve_ip_range\
+ (cur_chain_flow_count, force_ip_reservation)
configs.append({
'count': cur_chain_flow_count,
'ip_dst_addr_max': dst_ip_last,
'ip_dst_count': cur_chain_flow_count,
'ip_addrs_step': self.ip_addrs_step,
- 'udp_src_port': self.udp_src_port,
- 'udp_dst_port': self.udp_dst_port,
+ 'ip_src_static': self.ip_src_static,
+ 'udp_src_port': self.udp_ports.src_min,
+ 'udp_src_port_max': self.udp_ports.src_max,
+ 'udp_src_count': int(self.udp_ports.src_max) - int(self.udp_ports.src_min) + 1,
+ 'udp_dst_port': self.udp_ports.dst_min,
+ 'udp_dst_port_max': self.udp_ports.dst_max,
+ 'udp_dst_count': int(self.udp_ports.dst_max) - int(self.udp_ports.dst_min) + 1,
+ 'udp_port_step': self.udp_ports.step,
'mac_discovery_gw': self.get_gw_ip(chain_idx),
'ip_src_tg_gw': self.tg_gw_ip_block.get_ip(chain_idx),
'ip_dst_tg_gw': peer.tg_gw_ip_block.get_ip(chain_idx),
self.service_chain_count = config.service_chain_count
self.flow_count = config.flow_count
self.host_name = gen_config.host_name
-
+ self.bidirectional = config.traffic.bidirectional
self.tg_gateway_ip_addrs = gen_config.tg_gateway_ip_addrs
self.ip_addrs = gen_config.ip_addrs
self.ip_addrs_step = gen_config.ip_addrs_step or self.DEFAULT_SRC_DST_IP_STEP
gen_config.tg_gateway_ip_addrs_step or self.DEFAULT_IP_STEP
self.gateway_ip_addrs_step = gen_config.gateway_ip_addrs_step or self.DEFAULT_IP_STEP
self.gateway_ips = gen_config.gateway_ip_addrs
- self.udp_src_port = gen_config.udp_src_port
- self.udp_dst_port = gen_config.udp_dst_port
+ self.ip_src_static = gen_config.ip_src_static
self.vteps = gen_config.get('vteps')
self.devices = [Device(port, self) for port in [0, 1]]
# This should normally always be [0, 1]
def get_stats(self):
"""Collect final stats for previous run."""
- stats = self.gen.get_stats()
- retDict = {'total_tx_rate': stats['total_tx_rate']}
+ stats = self.gen.get_stats(self.ifstats)
+ retDict = {'total_tx_rate': stats['total_tx_rate'],
+ 'offered_tx_rate_bps': stats['offered_tx_rate_bps']}
tx_keys = ['total_pkts', 'total_pkt_bytes', 'pkt_rate', 'pkt_bit_rate']
rx_keys = tx_keys + ['dropped_pkts']
for key in ['pkt_bit_rate', 'pkt_rate']:
for dirc in ['tx', 'rx']:
retDict['overall'][dirc][key] /= 2.0
+ retDict['overall']['hdrh'] = stats.get('hdrh', None)
+ if retDict['overall']['hdrh']:
+ decoded_histogram = HdrHistogram.decode(retDict['overall']['hdrh'])
+ # override min max and avg from hdrh
+ retDict['overall']['rx']['min_delay_usec'] = decoded_histogram.get_min_value()
+ retDict['overall']['rx']['max_delay_usec'] = decoded_histogram.get_max_value()
+ retDict['overall']['rx']['avg_delay_usec'] = decoded_histogram.get_mean_value()
+ retDict['overall']['rx']['lat_percentile'] = {}
+ for percentile in self.config.lat_percentiles:
+ retDict['overall']['rx']['lat_percentile'][percentile] = \
+ decoded_histogram.get_value_at_percentile(percentile)
+
else:
retDict['overall'] = retDict[ports[0]]
retDict['overall']['drop_rate_percent'] = self.__get_dropped_rate(retDict['overall'])
'min_delay_usec': interface['rx']['min_delay_usec'],
}
+ if key == 'overall':
+ stats[key]['hdrh'] = interface.get('hdrh', None)
+ if stats[key]['hdrh']:
+ decoded_histogram = HdrHistogram.decode(stats[key]['hdrh'])
+ # override min max and avg from hdrh
+ stats[key]['min_delay_usec'] = decoded_histogram.get_min_value()
+ stats[key]['max_delay_usec'] = decoded_histogram.get_max_value()
+ stats[key]['avg_delay_usec'] = decoded_histogram.get_mean_value()
+ stats[key]['lat_percentile'] = {}
+ for percentile in self.config.lat_percentiles:
+ stats[key]['lat_percentile'][percentile] = decoded_histogram.\
+ get_value_at_percentile(percentile)
+
+
return stats
def __targets_found(self, rate, targets, results):
LOG.info('Average drop rate: %f', stats['overall']['drop_rate_percent'])
return stats, current_traffic_config['direction-total']
- @staticmethod
- def log_stats(stats):
+ def log_stats(self, stats):
"""Log estimated stats during run."""
- report = {
- 'datetime': str(datetime.now()),
- 'tx_packets': stats['overall']['tx']['total_pkts'],
- 'rx_packets': stats['overall']['rx']['total_pkts'],
- 'drop_packets': stats['overall']['rx']['dropped_pkts'],
- 'drop_rate_percent': stats['overall']['drop_rate_percent']
- }
- LOG.info('TX: %(tx_packets)d; '
- 'RX: %(rx_packets)d; '
- 'Est. Dropped: %(drop_packets)d; '
- 'Est. Drop rate: %(drop_rate_percent).4f%%',
- report)
+ # Calculate a rolling drop rate based on differential to
+ # the previous reading
+ cur_tx = stats['overall']['tx']['total_pkts']
+ cur_rx = stats['overall']['rx']['total_pkts']
+ delta_tx = cur_tx - self.prev_tx
+ delta_rx = cur_rx - self.prev_rx
+ drops = delta_tx - delta_rx
+ drop_rate_pct = 100 * (delta_tx - delta_rx)/delta_tx
+ self.prev_tx = cur_tx
+ self.prev_rx = cur_rx
+ LOG.info('TX: %15s; RX: %15s; (Est.) Dropped: %12s; Drop rate: %8.4f%%',
+ format(cur_tx, ',d'),
+ format(cur_rx, ',d'),
+ format(drops, ',d'),
+ drop_rate_pct)
def run_traffic(self):
"""Start traffic and return intermediate stats for each interval."""
stats = self.runner.run()
+ self.prev_tx = 0
+ self.prev_rx = 0
while self.runner.is_running:
self.log_stats(stats)
yield stats
for chain_idx in range(self.config.service_chain_count)]
# note that we need to make a copy of the ifs list so that any modification in the
# list from pps will not change the list saved in self.ifstats
- self.pps_list = [PacketPathStats(list(ifs)) for ifs in self.ifstats]
+ self.pps_list = [PacketPathStats(self.config, list(ifs)) for ifs in self.ifstats]
# insert the corresponding pps in the passed list
pps_list.extend(self.pps_list)
]
"""
if diff:
- stats = self.gen.get_stats()
+ stats = self.gen.get_stats(self.ifstats)
for chain_idx, ifs in enumerate(self.ifstats):
# each ifs has exactly 2 InterfaceStats and 2 Latency instances
# corresponding to the