diff options
author | 2016-02-10 16:32:54 +0200 | |
---|---|---|
committer | 2016-02-10 16:32:54 +0200 | |
commit | 0f03cc467be9c0b37310b7e2a589a86a1771c96f (patch) | |
tree | 32983679b9033b0379d2036740b796d8fe557c3f /scripts/automation/trex_control_plane/client | |
parent | a69c4dd80e88b6fb4ebfae8d84366b11561d7660 (diff) |
update hltapi + functional regression + remove old packet_builder tests
Diffstat (limited to 'scripts/automation/trex_control_plane/client')
-rwxr-xr-x | scripts/automation/trex_control_plane/client/trex_hltapi.py | 392 |
1 files changed, 294 insertions, 98 deletions
diff --git a/scripts/automation/trex_control_plane/client/trex_hltapi.py b/scripts/automation/trex_control_plane/client/trex_hltapi.py index c2a08306..fc507497 100755 --- a/scripts/automation/trex_control_plane/client/trex_hltapi.py +++ b/scripts/automation/trex_control_plane/client/trex_hltapi.py @@ -26,12 +26,21 @@ traffic_config_kwargs = { 'transmit_mode': 'continuous', # ( continuous | multi_burst | single_burst ) 'rate_pps': 1, 'stream_id': None, + 'name': None, 'bidirectional': 0, # stream builder parameters 'pkts_per_burst': 1, 'burst_loop_count': 1, 'inter_burst_gap': 12, - 'length_mode': 'fixed', # ( auto | fixed | increment | decrement | random ) + 'length_mode': 'fixed', # ( auto | fixed | increment | decrement | random | imix ) + 'l3_imix1_size': 60, + 'l3_imix1_ratio': 28, + 'l3_imix2_size': 590, + 'l3_imix2_ratio': 20, + 'l3_imix3_size': 1514, + 'l3_imix3_ratio': 4, + 'l3_imix4_size': 9226, + 'l3_imix4_ratio': 0, #L2 'frame_size': 64, 'frame_size_min': 64, @@ -40,6 +49,11 @@ traffic_config_kwargs = { 'l2_encap': 'ethernet_ii', # ( ethernet_ii ) 'mac_src': '00:00:01:00:00:01', 'mac_dst': '00:00:00:00:00:00', + 'mac_src2': '00:00:01:00:00:01', + 'mac_dst2': '00:00:00:00:00:00', + 'vlan_user_priority': 1, + 'vlan_id': 0, + 'vlan_cfi': 1, #L3, IP 'l3_protocol': 'ipv4', # ( ipv4 ) 'ip_tos_field': 0, @@ -60,6 +74,8 @@ traffic_config_kwargs = { 'l3_length_max': 238, 'l3_length_step': 1, # has to be 1 #L3, IPv6 (TODO: add) + 'ipv6_src_addr': None, + 'ipv6_dst_addr': None, #L4, TCP 'l4_protocol': 'tcp', # ( tcp | udp ) 'tcp_src_port': 1024, @@ -108,23 +124,15 @@ traffic_stats_kwargs = { } -#HACK FIX ME START import sys import os CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(CURRENT_PATH, '../stl/')) -#HACK FIX ME END from trex_stl_lib.api import * -import trex_stl_lib.api as pkt_bld -#import trex_stl_lib.trex_stl_packet_builder_scapy as pkt_bld -#from trex_stl_lib.trex_stl_packet_builder_scapy import import CTRexVmDescFlowVar, CTRexVmDescWrFlowVar -#from trex_stl_lib.trex_stl_client import STLClient -#from common.trex_streams import * - -import trex_root_path -from client_utils.general_utils import get_integer + +from client_utils.general_utils import get_number import socket import copy from misc_methods import print_r @@ -148,15 +156,100 @@ class HLT_OK(dict): dict.update(self, kwargs) def merge_kwargs(default_kwargs, user_kwargs): - extra_args = [] kwargs = copy.deepcopy(default_kwargs) for key, value in user_kwargs.items(): if key in kwargs: kwargs[key] = value - elif key not in ('save_to_yaml', 'save_to_pcap'): # internal debug arguments - print("Warning: provided parameter '%s' not supported" % key) + elif key in ('save_to_yaml', 'save_to_pcap'): # internal debug arguments + kwargs[key] = value + else: + print("Warning: provided parameter '%s' is not supported" % key) return kwargs +# change MACs from HLT formats {a b c d e f} or a-b-c-d-e-f or a.b.c.d.e.f to Scapy format a:b:c:d:e:f +def correct_macs(kwargs): + list_of_mac_args = ['mac_src', 'mac_dst', 'mac_src2', 'mac_dst2'] + for mac_arg in list_of_mac_args: + if mac_arg in kwargs: + if type(kwargs[mac_arg]) is not str: raise Exception('Argument %s should be str' % mac_arg) + kwargs[mac_arg] = kwargs[mac_arg].replace('{', '').replace('}', '').strip().replace('-', ':').replace(' ', ':').replace('.', ':') + kwargs[mac_arg] = ':'.join(kwargs[mac_arg].split(':')) # remove duplicate ':' if any + try: + mac2str(kwargs[mac_arg]) # verify we are ok + except: + raise Exception('Incorrect MAC %s=%s, please use 01:23:45:67:89:10 or 01-23-45-67-89-10 or 01.23.45.67.89.10 or {01 23 45 67 89 10}' % (mac_arg, kwargs[mac_arg])) + +# dict of streams per port +# hlt_history = False: holds list of stream_id per port +# hlt_history = True: act as dictionary (per port) stream_id -> hlt arguments used for build +class CStreamsPerPort(dict): + def __init__(self, port_list, hlt_history = False): + if type(port_list) is not list: + raise Exception('CStreamsPerPort: should be init with list of ports') + self.hlt_history = hlt_history + if self.hlt_history: + dict.__init__(self, dict((port, {}) for port in port_list)) + else: + dict.__init__(self, dict((port, []) for port in port_list)) + + def get_stream_list(self, ports_list = None): + if self.hlt_history: + if ports_list is None: + ports_list = self.keys() + elif not isinstance(ports_list, list): + ports_list = [ports_list] + ret = {} + for port in ports_list: + ret[port] = self[port].keys() + return ret + else: + return self + + # add to stream_id list per port, no HLT args, res = HLT result + def add_streams_from_res(self, res): + if self.hlt_history: raise Exception('CStreamsPerPort: this object is not meant for HLT history, try init with hlt_history = False') + if not isinstance(res, dict): raise Exception('CStreamsPerPort: res should be dict') + if res.get('status') != 1: raise Exception('CStreamsPerPort: res has status %s' % res.get('status')) + res_streams = res.get('stream_id') + if not isinstance(res_streams, dict): + raise Exception('CStreamsPerPort: stream_id in res should be dict') + for port, port_stream_ids in res_streams.items(): + if port not in self: + raise Exception('CStreamsPerPort: port %s not defined' % port) + if type(port_stream_ids) is not list: + port_stream_ids = [port_stream_ids] + self[port].extend(port_stream_ids) + + # save HLT args to modify streams later + def save_stream_args(self, ports_list, stream_id, stream_hlt_args): + if not self.hlt_history: raise Exception('CStreamsPerPort: this object works only with HLT history, try init with hlt_history = True') + if type(stream_id) not in (int, long): raise Exception('CStreamsPerPort: stream_id should be number') + if not isinstance(stream_hlt_args, dict): raise Exception('CStreamsPerPort: stream_hlt_args should be dict') + if not isinstance(ports_list, list): + ports_list = [ports_list] + for port in ports_list: + if port not in self: + raise Exception('CStreamsPerPort: port %s not defined' % port) + del stream_hlt_args['port_handle'] + if stream_id in self[port]: + self[port][stream_id].update(stream_hlt_args) + else: + self[port][stream_id] = stream_hlt_args + + def remove_stream(self, ports_list, stream_id): + if not isinstance(ports_list, list): + ports_list = [ports_list] + if not isinstance(stream_id, dict): + raise Exception('CStreamsPerPort: stream_hlt_args should be dict') + for port in ports_list: + if port not in self: + raise Exception('CStreamsPerPort: port %s not defined' % port) + if stream_id not in self[port]: + raise Exception('CStreamsPerPort: stream_id %s not found at port %s' % (port, stream_id)) + if self.hlt_history: + del self[port][stream_id] + else: + self[port].pop(stream_id) class CTRexHltApi(object): @@ -164,7 +257,7 @@ class CTRexHltApi(object): self.trex_client = None self.connected = False self.verbose = verbose - self._hlt_streams_history = {} # streams per stream_id in format of HLT arguments for modify later + self._streams_history = {} # streams per stream_id per port in format of HLT arguments for modify later ########################### @@ -190,14 +283,14 @@ class CTRexHltApi(object): return HLT_ERR('Could not init stateless client %s: %s' % (device, e)) try: self.trex_client.connect() - except Exception as e: + #except Exception as e: + except ValueError as e: self.trex_client = None return HLT_ERR('Could not connect to device %s: %s' % (device, e)) # connection successfully created with server, try acquiring ports of TRex try: port_list = self.parse_port_list(kwargs['port_list']) - print kwargs['break_locks'] self.trex_client.acquire(ports = port_list, force = kwargs['break_locks']) except Exception as e: self.trex_client = None @@ -210,13 +303,13 @@ class CTRexHltApi(object): if kwargs['reset']: # remove all port traffic configuration from TRex try: - print 'reseting' self.trex_client.stop(ports = port_list) self.trex_client.reset(ports = port_list) except Exception as e: self.trex_client = None return HLT_ERR('Error in reset traffic: %s' % e) + self._streams_history = CStreamsPerPort(port_handle, hlt_history = True) self.connected = True return HLT_OK(port_handle = port_handle) @@ -245,6 +338,8 @@ class CTRexHltApi(object): return HLT_OK() def interface_config(self, port_handle, mode='config'): + if not self.connected: + return HLT_ERR('connect first') ALLOWED_MODES = ['config', 'modify', 'destroy'] if mode not in ALLOWED_MODES: return HLT_ERR('Mode must be one of the following values: %s' % ALLOWED_MODES) @@ -257,12 +352,18 @@ class CTRexHltApi(object): ########################### def traffic_config(self, **user_kwargs): + if not self.connected: + return HLT_ERR('connect first') + try: + correct_macs(user_kwargs) + except Exception as e: + return HLT_ERR(e) kwargs = merge_kwargs(traffic_config_kwargs, user_kwargs) stream_id = kwargs['stream_id'] if type(stream_id) is list: - del kwargs['stream_id'] for each_stream_id in stream_id: - res = self.traffic_config(stream_id = each_stream_id, **kwargs) + user_kwargs[stream_id] = each_stream_id + res = self.traffic_config(stream_id = each_stream_id, **user_kwargs) if type(res) is HLT_ERR: return res return HLT_OK() @@ -294,73 +395,152 @@ class CTRexHltApi(object): try: self._remove_stream(stream_id, port_handle) except Exception as e: - return HLT_ERR('Could not remove streams with specified by %s, error: %s' % (stream_id, log)) + return HLT_ERR('Could not remove streams with specified by %s, error: %s' % (stream_id, e)) return HLT_OK() #if mode == 'enable': # stream_id = kwargs.get('stream_id') # if stream_id is None: # return HLT_ERR('Please specify stream_id to enable.') - # if stream_id not in self._hlt_streams_history: + # if stream_id not in self._streams_history: # return HLT_ERR('This stream_id (%s) was not used before, please create new.' % stream_id) - # self._hlt_streams_history[stream_id].update(kwargs) # <- the modification + # self._streams_history[stream_id].update(kwargs) # <- the modification if mode == 'modify': # we remove stream and create new one with same stream_id stream_id = kwargs.get('stream_id') if stream_id is None: return HLT_ERR('Please specify stream_id to modify.') - if stream_id not in self._hlt_streams_history: - return HLT_ERR('This stream_id (%s) was not used before, please create new.' % stream_id) - self._hlt_streams_history[stream_id].update(kwargs) # <- the modification - #for port_id in port_handle: - # if stream_id not in self.trex_client.get_stream_id_list(port_id): - # return HLT_ERR('Port %s does not have stream_id %s.' % (port_id, stream_id)) + + if len(port_handle) > 1: + for port in port_handle: + user_kwargs[port_handle] = port + res = self.traffic_config(**user_kwargs) # recurse per port, each port can have different stream with such id + else: + if type(port_handle) is list: + port = port_handle[0] + else: + port = port_handle + if port not in self._streams_history: + return HLT_ERR('Port %s was not used/acquired' % port) + if stream_id not in self._streams_history[port]: + return HLT_ERR('This stream_id (%s) was not used before at port %s, please create new.' % (stream_id, port)) + kwargs.update(self._streams_history[port][stream_id]) + kwargs.update(user_kwargs) try: self.trex_client.remove_streams(stream_id, port_handle) - except Exception as e: - return HLT_ERR('Could not remove streams specified by %s: %s' % (stream_id, e)) + #except Exception as e: + except ValueError as e: + return HLT_ERR('Could not remove stream(s) %s from port(s) %s: %s' % (stream_id, port_handle, e)) if mode == 'create' or mode == 'modify': # create a new stream with desired attributes, starting by creating packet - + streams_per_port = CStreamsPerPort(port_handle) if kwargs['bidirectional']: # two streams with opposite directions - del kwargs['bidirectional'] - del kwargs['port_handle'] + del user_kwargs['bidirectional'] + save_to_yaml = user_kwargs.get('save_to_yaml') bidirect_err = 'When using bidirectional flag, ' if len(port_handle) != 2: return HLT_ERR(bidirect_err + 'number of ports should be exactly 2') try: - res1 = self.traffic_config(port_handle = port_handle[0], **kwargs) - kwargs['mac_src'] = kwargs['mac_src2'] - kwargs['mac_dst'] = kwargs['mac_dst2'] - kwargs['ip_src_addr'] = kwargs['ip_dst_addr'] - kwargs['ip_dst_addr'] = kwargs['ip_src_addr'] - kwargs['ipv6_src_addr'] = kwargs['ipv6_dst_addr'] - kwargs['ipv6_dst_addr'] = kwargs['ipv6_src_addr'] - res2 = self.traffic_config(port_handle = port_handle[1], **kwargs) + if save_to_yaml and type(save_to_yaml) is str: + user_kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_bi1.yaml') + user_kwargs['port_handle'] = port_handle[0] + res1 = self.traffic_config(**user_kwargs) + if res1['status'] == 0: + raise Exception('Could not create bidirectional stream 1: %s' % res1['log']) + streams_per_port.add_streams_from_res(res1) + user_kwargs['mac_src'] = kwargs['mac_src2'] + user_kwargs['mac_dst'] = kwargs['mac_dst2'] + user_kwargs['ip_src_addr'] = kwargs['ip_dst_addr'] + user_kwargs['ip_dst_addr'] = kwargs['ip_src_addr'] + user_kwargs['ipv6_src_addr'] = kwargs['ipv6_dst_addr'] + user_kwargs['ipv6_dst_addr'] = kwargs['ipv6_src_addr'] + if save_to_yaml and type(save_to_yaml) is str: + user_kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_bi2.yaml') + user_kwargs['port_handle'] = port_handle[1] + res2 = self.traffic_config(**user_kwargs) + if res2['status'] == 0: + raise Exception('Could not create bidirectional stream 2: %s' % res2['log']) + streams_per_port.add_streams_from_res(res2) except Exception as e: return HLT_ERR('Could not generate bidirectional traffic: %s' % e) - return HLT_OK(stream_id = {port_handle[0]: res1['stream_id'], port_handle[1]: res2['stream_id']}) - stream_obj = CTRexHltApiPktBuilder.generate_stream(**kwargs) + return HLT_OK(stream_id = streams_per_port) + + if kwargs['length_mode'] == 'imix': # several streams with given length + user_kwargs['length_mode'] = 'fixed' + if kwargs['l3_imix1_size'] < 32 or kwargs['l3_imix2_size'] < 32 or kwargs['l3_imix3_size'] < 32 or kwargs['l3_imix4_size'] < 32: + return HLT_ERR('l3_imix*_size should be at least 32') + total_rate = kwargs['l3_imix1_ratio'] + kwargs['l3_imix2_ratio'] + kwargs['l3_imix3_ratio'] + kwargs['l3_imix4_ratio'] + if total_rate == 0: + return HLT_ERR('Used length_mode imix, but all the ratio are 0') + save_to_yaml = kwargs.get('save_to_yaml') + rate_pps = float(kwargs['rate_pps']) + try: + if kwargs['l3_imix1_ratio'] > 0: + if save_to_yaml and type(save_to_yaml) is str: + user_kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_imix1.yaml') + user_kwargs['frame_size'] = kwargs['l3_imix1_size'] + user_kwargs['rate_pps'] = rate_pps * kwargs['l3_imix1_ratio'] / total_rate + res = self.traffic_config(**user_kwargs) + if res['status'] == 0: + return HLT_ERR('Could not create stream imix1: %s' % res['log']) + streams_per_port.add_streams_from_res(res) + + if kwargs['l3_imix2_ratio'] > 0: + if save_to_yaml and type(save_to_yaml) is str: + user_kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_imix2.yaml') + user_kwargs['frame_size'] = kwargs['l3_imix2_size'] + user_kwargs['rate_pps'] = rate_pps * kwargs['l3_imix2_ratio'] / total_rate + res = self.traffic_config(**user_kwargs) + if res['status'] == 0: + return HLT_ERR('Could not create stream imix2: %s' % res['log']) + streams_per_port.add_streams_from_res(res) + if kwargs['l3_imix3_ratio'] > 0: + if save_to_yaml and type(save_to_yaml) is str: + kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_imix3.yaml') + user_kwargs['frame_size'] = kwargs['l3_imix3_size'] + user_kwargs['rate_pps'] = rate_pps * kwargs['l3_imix3_ratio'] / total_rate + res = self.traffic_config(**user_kwargs) + if res['status'] == 0: + return HLT_ERR('Could not create stream imix3: %s' % res['log']) + streams_per_port.add_streams_from_res(res) + if kwargs['l3_imix4_ratio'] > 0: + if save_to_yaml and type(save_to_yaml) is str: + user_kwargs['save_to_yaml'] = save_to_yaml.replace('.yaml', '_imix4.yaml') + user_kwargs['frame_size'] = kwargs['l3_imix4_size'] + user_kwargs['rate_pps'] = rate_pps * kwargs['l3_imix4_ratio'] / total_rate + res = self.traffic_config(**user_kwargs) + if res['status'] == 0: + return HLT_ERR('Could not create stream imix4: %s' % res['log']) + streams_per_port.add_streams_from_res(res) + except Exception as e: + return HLT_ERR('Could not generate imix streams: %s' % e) + return HLT_OK(stream_id = streams_per_port) try: - stream_obj = CTRexHltApiPktBuilder.generate_stream(**kwargs) + stream_obj = CTRexHltApiBuilder.generate_stream(**kwargs) except Exception as e: return HLT_ERR('Could not create stream: %s' % e) - stream_id = stream_obj.get_id() # try adding the stream per ports try: - self.trex_client.add_streams(streams=stream_obj, - ports=port_handle) - self._hlt_streams_history[stream_id] = kwargs + stream_id_arr = self.trex_client.add_streams(streams=stream_obj, + ports=port_handle) + #print stream_id_arr, port_handle + #print self._streams_history + for port in port_handle: + #print type(user_kwargs) + self._streams_history.save_stream_args(port_handle, stream_id_arr[0], user_kwargs) + #print 'done' except Exception as e: return HLT_ERR('Could not add stream to ports: %s' % e) - return HLT_OK(stream_id = stream_obj.get_id()) + return HLT_OK(stream_id = dict((port, stream_id_arr) for port in port_handle)) return HLT_ERR('Got to the end of traffic_config, mode not implemented or forgot "return" function somewhere.') def traffic_control(self, **user_kwargs): + if not self.connected: + return HLT_ERR('connect first') kwargs = merge_kwargs(traffic_control_kwargs, user_kwargs) action = kwargs['action'] port_handle = kwargs['port_handle'] @@ -392,6 +572,8 @@ class CTRexHltApi(object): return HLT_ERR("Probably action '%s' is not implemented" % action) def traffic_stats(self, **user_kwargs): + if not self.connected: + return HLT_ERR('connect first') kwargs = merge_kwargs(traffic_stats_kwargs, user_kwargs) mode = kwargs['mode'] port_handle = kwargs['port_handle'] @@ -399,7 +581,7 @@ class CTRexHltApi(object): if mode not in ALLOWED_MODES: return HLT_ERR("'mode' must be one of the following values: %s" % ALLOWED_MODES) if mode == 'streams': - return HLT_ERR("'mode = streams' not implemented'") + return HLT_ERR("mode 'streams' not implemented'") if mode in ('all', 'aggregate'): hlt_stats_dict = {} try: @@ -407,7 +589,7 @@ class CTRexHltApi(object): except Exception as e: return HLT_ERR('Could not retrieve stats: %s' % e) for port_id, stat_dict in stats.iteritems(): - if type(port_id) is int: + if type(port_id) in (int, long): hlt_stats_dict[port_id] = { 'aggregate': { 'tx': { @@ -439,21 +621,22 @@ class CTRexHltApi(object): # * list - list of stream_id values or strings (see below) # * string - exact stream_id value, mix of ranges/list separated by comma: 2, 4-13 def _remove_stream(self, stream_id, port_handle): - if get_integer(stream_id) is not None: # exact value of int or str - self.trex_client.remove_stream(get_integer(stream_id), port_handle) # actual remove + if get_number(stream_id) is not None: # exact value of int or str + self.trex_client.remove_streams(get_number(stream_id), port_handle) # actual remove + return if type(stream_id) is list: # list of values/strings for each_stream_id in stream_id: - self._remove_stream(each_stream_id, port_handle) # recurse + self._remove_stream(each_stream_id, port_handle) # recurse return if type(stream_id) is str: # range or list in string if stream_id.find(',') != -1: for each_stream_id_element in stream_id.split(','): - self._remove_stream(each_stream_id_element, port_handle) # recurse + self._remove_stream(each_stream_id_element, port_handle) # recurse return if stream_id.find('-') != -1: stream_id_min, stream_id_max = stream_id.split('-', 1) - stream_id_min = get_integer(stream_id_min) - stream_id_max = get_integer(stream_id_max) + stream_id_min = get_number(stream_id_min) + stream_id_max = get_number(stream_id_max) if stream_id_min is None: raise Exception('_remove_stream: wrong range param %s' % stream_id_min) if stream_id_max is None: @@ -461,7 +644,7 @@ class CTRexHltApi(object): if stream_id_max < stream_id_min: raise Exception('_remove_stream: right range param is smaller than left one: %s-%s' % (stream_id_min, stream_id_max)) for each_stream_id in xrange(stream_id_min, stream_id_max + 1): - self._remove_stream(each_stream_id, port_handle) # recurse + self._remove_stream(each_stream_id, port_handle) # recurse return raise Exception('_remove_stream: wrong param %s' % stream_id) @@ -495,27 +678,27 @@ class CTRexHltApi(object): return '\n'. join([str(response) for response in responses]) return responses -class CTRexHltApiPktBuilder: +class CTRexHltApiBuilder: @staticmethod def generate_stream(**user_kwargs): kwargs = merge_kwargs(traffic_config_kwargs, user_kwargs) try: - packet = CTRexHltApiPktBuilder.generate_packet(**kwargs) - except Exception as e: + packet = CTRexHltApiBuilder.generate_packet(**kwargs) + #except Exception as e: + except ValueError as e: raise Exception('Could not generate packet: %s' % e) try: transmit_mode = kwargs['transmit_mode'] rate_pps = kwargs['rate_pps'] pkts_per_burst = kwargs['pkts_per_burst'] - burst_loop_count = kwargs['burst_loop_count'] - inter_burst_gap = kwargs['inter_burst_gap'] if transmit_mode == 'continuous': transmit_mode_class = STLTXCont(pps = rate_pps) elif transmit_mode == 'single_burst': transmit_mode_class = STLTXSingleBurst(pps = rate_pps, total_pkts = pkts_per_burst) elif transmit_mode == 'multi_burst': - transmit_mode_class = STLTXMultiBurst(pps = rate_pps, total_pkts = pkts_per_burst, count = burst_loop_count, ibg = inter_burst_gap) + transmit_mode_class = STLTXMultiBurst(pps = rate_pps, total_pkts = pkts_per_burst, + count = kwargs['burst_loop_count'], ibg = kwargs['inter_burst_gap']) else: raise Exception('transmit_mode %s not supported/implemented') except Exception as e: @@ -527,30 +710,41 @@ class CTRexHltApiPktBuilder: #self_start = True, mode = transmit_mode_class, #rx_stats = rx_stats, - #next_stream_id = -1 + #next_stream_id = -1, + stream_id = kwargs.get('stream_id'), + name = kwargs.get('name'), ) except Exception as e: raise Exception('Could not create stream: %s' % e) debug_filename = kwargs.get('save_to_yaml') if type(debug_filename) is str: - STLProfile(stream_obj).dump_to_yaml(debug_filename) + stream_obj.dump_to_yaml(debug_filename) return stream_obj @staticmethod def generate_packet(**user_kwargs): kwargs = merge_kwargs(traffic_config_kwargs, user_kwargs) - pkt = pkt_bld.CScapyTRexPktBuilder() + pkt = STLPktBuilder() vm_cmds = [] fix_ipv4_checksum = False ### L2 ### - if kwargs['l2_encap'] == 'ethernet_ii': + if kwargs['l2_encap'] in ('ethernet_ii', 'ethernet_ii_vlan'): #fields_desc = [ MACField("dst","00:00:00:01:00:00"), # MACField("src","00:00:00:02:00:00"), # XShortEnumField("type", 0x9000, ETHER_TYPES) ] - l2_layer = pkt_bld.Ether(src = kwargs['mac_src'], dst = kwargs['mac_dst']) + l2_layer = Ether(src = kwargs['mac_src'], dst = kwargs['mac_dst']) + if kwargs['l2_encap'] == 'ethernet_ii_vlan': + #fields_desc = [ BitField("prio", 0, 3), + # BitField("id", 0, 1), + # BitField("vlan", 1, 12), + # XShortEnumField("type", 0x0000, ETHER_TYPES) ] + l2_layer /= Dot1Q(prio = kwargs['vlan_user_priority'], + vlan = kwargs['vlan_id'], + id = kwargs['vlan_cfi'], + ) else: raise NotImplementedError("l2_encap does not support the desired encapsulation '%s'" % kwargs['l2_encap']) base_pkt = l2_layer @@ -572,15 +766,15 @@ class CTRexHltApiPktBuilder: # Emph(IPField("src", "16.0.0.1")), # Emph(IPField("dst", "48.0.0.1")), # PacketListField("options", [], IPOption, length_from=lambda p:p.ihl*4-20) ] - l3_layer = pkt_bld.IP(tos = kwargs['ip_tos_field'], - len = None if kwargs['length_mode'] == 'auto' else kwargs['l3_length'], - id = kwargs['ip_id'], - frag = kwargs['ip_fragment_offset'], - ttl = kwargs['ip_ttl'], - chksum = kwargs['ip_checksum'], - src = kwargs['ip_src_addr'], - dst = kwargs['ip_dst_addr'] - ) + l3_layer = IP(tos = kwargs['ip_tos_field'], + len = None if kwargs['length_mode'] == 'auto' else kwargs['l3_length'], + id = kwargs['ip_id'], + frag = kwargs['ip_fragment_offset'], + ttl = kwargs['ip_ttl'], + chksum = kwargs['ip_checksum'], + src = kwargs['ip_src_addr'], + dst = kwargs['ip_dst_addr'], + ) # IPv4 VM if kwargs['ip_src_mode'] != 'fixed': fix_ipv4_checksum = True @@ -588,7 +782,7 @@ class CTRexHltApiPktBuilder: raise Exception('ip_src_step has to be 1 (TRex limitation)') if kwargs['ip_src_count'] < 1: raise Exception('ip_src_count has to be at least 1') - ip_src_addr_num = pkt_bld.ipv4_str_to_num(pkt_bld.is_valid_ipv4(kwargs['ip_src_addr'])) + ip_src_addr_num = ipv4_str_to_num(is_valid_ipv4(kwargs['ip_src_addr'])) if kwargs['ip_src_mode'] == 'increment': vm_cmds.append(CTRexVmDescFlowVar(name = 'ip_src', size = 4, op = 'inc', min_value = ip_src_addr_num, @@ -611,7 +805,7 @@ class CTRexHltApiPktBuilder: raise Exception('ip_dst_step has to be 1 (TRex limitation)') if kwargs['ip_dst_count'] < 1: raise Exception('ip_dst_count has to be at least 1') - ip_dst_addr_num = pkt_bld.ipv4_str_to_num(pkt_bld.is_valid_ipv4(kwargs['ip_dst_addr'])) + ip_dst_addr_num = ipv4_str_to_num(is_valid_ipv4(kwargs['ip_dst_addr'])) if kwargs['ip_dst_mode'] == 'increment': vm_cmds.append(CTRexVmDescFlowVar(name = 'ip_dst', size = 4, op = 'inc', min_value = ip_dst_addr_num, @@ -651,16 +845,16 @@ class CTRexHltApiPktBuilder: 'A' if kwargs['tcp_ack_flag'] else '' + 'U' if kwargs['tcp_urg_flag'] else '') - l4_layer = pkt_bld.TCP(sport = kwargs['tcp_src_port'], - dport = kwargs['tcp_dst_port'], - seq = kwargs['tcp_seq_num'], - ack = kwargs['tcp_ack_num'], - dataofs = kwargs['tcp_data_offset'], - flags = tcp_flags, - window = kwargs['tcp_window'], - chksum = kwargs['tcp_checksum'], - urgptr = kwargs['tcp_urgent_ptr'], - ) + l4_layer = TCP(sport = kwargs['tcp_src_port'], + dport = kwargs['tcp_dst_port'], + seq = kwargs['tcp_seq_num'], + ack = kwargs['tcp_ack_num'], + dataofs = kwargs['tcp_data_offset'], + flags = tcp_flags, + window = kwargs['tcp_window'], + chksum = kwargs['tcp_checksum'], + urgptr = kwargs['tcp_urgent_ptr'], + ) # TCP VM if kwargs['tcp_src_port_count'] != 1: fix_ipv4_checksum = True @@ -712,10 +906,10 @@ class CTRexHltApiPktBuilder: # ShortEnumField("dport", 53, UDP_SERVICES), # ShortField("len", None), # XShortField("chksum", None), ] - l4_layer = pkt_bld.UDP(sport = kwargs['udp_src_port'], - dport = kwargs['udp_dst_port'], - len = kwargs['udp_length'], - chksum = kwargs['udp_checksum']) + l4_layer = UDP(sport = kwargs['udp_src_port'], + dport = kwargs['udp_dst_port'], + len = kwargs['udp_length'], + chksum = kwargs['udp_checksum']) # UDP VM if kwargs['udp_src_port_count'] != 1: fix_ipv4_checksum = True @@ -768,6 +962,8 @@ class CTRexHltApiPktBuilder: payload_len = 0 elif kwargs['length_mode'] == 'fixed': payload_len = kwargs['frame_size'] - len(base_pkt) + elif kwargs['length_mode'] == 'imix': + raise Exception("Should not use length_mode 'imix' directly in packet building, convert it to other mode at higher levels (stream?)") else: fix_ipv4_checksum = True if kwargs['frame_size_step'] != 1 or kwargs['l3_length_step'] != 1: @@ -784,7 +980,7 @@ class CTRexHltApiPktBuilder: min_value = kwargs['l3_length_min'] + len(l2_layer), max_value = kwargs['l3_length_max'] + len(l2_layer))) payload_len = kwargs['l3_length_max'] + len(l2_layer) - len(base_pkt) - vm_cmds.append(pkt_bld.CTRexVmDescTrimPktSize('fv_rand')) + vm_cmds.append(CTRexVmDescTrimPktSize('fv_rand')) if l3_layer.name == 'IP' or l4_layer.name == 'UDP': # add here other things need to fix due to size change if l3_layer.name == 'IP': vm_cmds.append(CTRexVmDescWrFlowVar(fv_name = 'fv_rand', pkt_offset = 'IP.len', add_val = -len(l2_layer))) @@ -797,9 +993,9 @@ class CTRexHltApiPktBuilder: pkt.set_packet(base_pkt) if fix_ipv4_checksum and l3_layer.name == 'IP' and kwargs['ip_checksum'] is None: - vm_cmds.append(pkt_bld.CTRexVmDescFixIpv4(offset = 'IP')) + vm_cmds.append(CTRexVmDescFixIpv4(offset = 'IP')) if vm_cmds: - pkt.add_command(pkt_bld.CTRexScRaw(vm_cmds)) + pkt.add_command(CTRexScRaw(vm_cmds)) # debug (only the base packet, without VM) debug_filename = kwargs.get('save_to_pcap') |