From 2828fc9aab33b742c59a499dbf06ea2239ec6220 Mon Sep 17 00:00:00 2001 From: imarom Date: Tue, 26 Jan 2016 08:57:43 -0500 Subject: API simplification and example --- .../trex_control_plane/common/trex_stats.py | 3 +- .../common/trex_stl_exceptions.py | 53 +++++++ .../trex_control_plane/common/trex_streams.py | 162 +++++++++++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 scripts/automation/trex_control_plane/common/trex_stl_exceptions.py (limited to 'scripts/automation/trex_control_plane/common') diff --git a/scripts/automation/trex_control_plane/common/trex_stats.py b/scripts/automation/trex_control_plane/common/trex_stats.py index 52c0c0a1..464ee56a 100755 --- a/scripts/automation/trex_control_plane/common/trex_stats.py +++ b/scripts/automation/trex_control_plane/common/trex_stats.py @@ -223,7 +223,8 @@ class CTRexInfoGenerator(object): info_table = text_tables.TRexTextTable() info_table.set_cols_align(["c"] + ["l"] + ["r"] + ["c"] + ["r"] + ["c"]) - info_table.set_cols_width([4] + [20] + [8] + [16] + [10] + [12]) + info_table.set_cols_width([10] + [20] + [8] + [16] + [10] + [12]) + info_table.set_cols_dtype(["t"] + ["t"] + ["t"] + ["t"] + ["t"] + ["t"]) info_table.add_rows([v.values() for k, v in return_streams_data['streams'].iteritems()], diff --git a/scripts/automation/trex_control_plane/common/trex_stl_exceptions.py b/scripts/automation/trex_control_plane/common/trex_stl_exceptions.py new file mode 100644 index 00000000..9be20db9 --- /dev/null +++ b/scripts/automation/trex_control_plane/common/trex_stl_exceptions.py @@ -0,0 +1,53 @@ +import os +import sys +from common.text_opts import * + +# basic error for API +class STLError(Exception): + def __init__ (self, msg): + self.msg = str(msg) + + def __str__ (self): + exc_type, exc_obj, exc_tb = sys.exc_info() + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] + + + s = "\n******\n" + s += "Error at {0}:{1}\n\n".format(format_text(fname, 'bold'), format_text(exc_tb.tb_lineno), 'bold') + s += "specific error:\n\n{0}\n".format(format_text(self.msg, 'bold')) + + return s + + def brief (self): + return self.msg + + +# raised when the client state is invalid for operation +class STLStateError(STLError): + def __init__ (self, op, state): + self.msg = "Operation '{0}' is not valid while '{1}'".format(op, state) + + +# port state error +class STLPortStateError(STLError): + def __init__ (self, port, op, state): + self.msg = "Operation '{0}' on port(s) '{1}' is not valid while port(s) '{2}'".format(op, port, state) + + +# raised when argument is not valid for operation +class STLArgumentError(STLError): + def __init__ (self, name, got, valid_values = None, extended = None): + self.msg = "Argument: '{0}' invalid value: '{1}'".format(name, got) + if valid_values: + self.msg += " - valid values are '{0}'".format(valid_values) + + if extended: + self.msg += "\n{0}".format(extended) + +# raised when timeout occurs +class STLTimeoutError(STLError): + def __init__ (self, timeout): + self.msg = "Timeout: operation took more than '{0}' seconds".format(timeout) + + + diff --git a/scripts/automation/trex_control_plane/common/trex_streams.py b/scripts/automation/trex_control_plane/common/trex_streams.py index ea3d71d1..90cb812d 100755 --- a/scripts/automation/trex_control_plane/common/trex_streams.py +++ b/scripts/automation/trex_control_plane/common/trex_streams.py @@ -4,10 +4,12 @@ import external_packages from client_utils.packet_builder import CTRexPktBuilder from collections import OrderedDict, namedtuple from client_utils.yaml_utils import * +import trex_stl_exceptions import dpkt import struct import copy import os +import random StreamPack = namedtuple('StreamPack', ['stream_id', 'stream']) LoadedStreamList = namedtuple('LoadedStreamList', ['name', 'loaded', 'compiled']) @@ -323,3 +325,163 @@ class CStreamsDB(object): else: return self.stream_packs.get(name) + +########################### Simple Streams ########################### +from trex_stl_exceptions import * + +class STLStream(object): + + def __init__ (self, + packet, + pps = 1, + enabled = True, + self_start = True, + isg = 0.0, + rx_stats = None, + next_stream_id = -1): + + # type checking + if not isinstance(pps, (int, float)): + raise STLArgumentError('pps', pps) + + if not isinstance(packet, CTRexPktBuilder): + raise STLArgumentError('packet', packet) + + if not isinstance(enabled, bool): + raise STLArgumentError('enabled', enabled) + + if not isinstance(self_start, bool): + raise STLArgumentError('self_start', self_start) + + if not isinstance(isg, (int, float)): + raise STLArgumentError('isg', isg) + + # use a random 31 bit for ID + self.stream_id = random.getrandbits(31) + + self.fields = {} + + # basic fields + self.fields['enabled'] = enabled + self.fields['self_start'] = self_start + self.fields['isg'] = isg + + self.fields['next_stream_id'] = next_stream_id + + # mode + self.fields['mode'] = {} + self.fields['mode']['pps'] = pps + + # packet and VM + self.fields['packet'] = packet.dump_pkt() + self.fields['vm'] = packet.get_vm_data() + + self.fields['rx_stats'] = {} + if not rx_stats: + self.fields['rx_stats']['enabled'] = False + + + def __str__ (self): + return json.dumps(self.fields, indent = 4, separators=(',', ': '), sort_keys = True) + + def to_json (self): + return self.fields + + def get_id (self): + return self.stream_id + + +# continuous stream +class STLContStream(STLStream): + def __init__ (self, + packet, + pps = 1, + enabled = True, + self_start = True, + isg = 0.0, + rx_stats = None): + + super(STLContStream, self).__init__(packet, + pps, + enabled, + self_start, + isg, + rx_stats, + next_stream_id = -1) + + # type + self.fields['mode']['type'] = "continuous" + + + +# single burst +class STLSingleBurstStream(STLStream): + def __init__ (self, + packet, + total_pkts, + pps = 1, + enabled = True, + self_start = True, + isg = 0.0, + rx_stats = None, + next_stream_id = -1): + + + if not isinstance(total_pkts, int): + raise STLArgumentError('total_pkts', total_pkts) + + super(STLSingleBurstStream, self).__init__(packet, + pps, + enabled, + self_start, + isg, + rx_stats, + next_stream_id) + + self.fields['mode']['type'] = "single_burst" + self.fields['mode']['total_pkts'] = total_pkts + + +# multi burst stream +class STLMultiBurstStream(STLStream): + def __init__ (self, + packet, + pkts_per_burst = 1, + pps = 1, + ibg = 0.0, + count = 1, + enabled = True, + self_start = True, + isg = 0.0, + rx_stats = None, + next_stream_id = -1): + + + if not isinstance(pkts_per_burst, int): + raise STLArgumentError('pkts_per_burst', pkts_per_burst) + + if not isinstance(count, int): + raise STLArgumentError('count', count) + + if not isinstance(ibg, (int, float)): + raise STLArgumentError('ibg', ibg) + + super(STLMultiBurstStream, self).__init__(packet, enabled, self_start, isg, rx_stats) + + self.fields['mode']['type'] = "single_burst" + self.fields['mode']['pkts_per_burst'] = pkts_per_burst + self.fields['mode']['ibg'] = ibg + self.fields['mode']['count'] = count + + +# REMOVE ME when can - convert from stream pack to a simple stream +class HACKSTLStream(STLStream): + def __init__ (self, stream_pack): + if not isinstance(stream_pack, StreamPack): + raise Exception("internal error") + + packet = CTRexPktBuilder() + packet.load_from_stream_obj(stream_pack.stream) + super(HACKSTLStream, self).__init__(packet) + + self.fields = stream_pack.stream -- cgit 1.2.3-korg