def load_pcap(pcap_file, ipg_usec=None, speedup=1.0, loop=False): # check filename if not os.path.isfile(pcap_file): raise STLError("file '{0}' does not exists".format(pcap_file)) streams = [] last_ts_usec = 0 pkts = RawPcapReader(pcap_file).read_all() for i, (cap, meta) in enumerate(pkts, start=1): # IPG - if not provided, take from cap if ipg_usec == None: ts_usec = (meta[0] * 1e6 + meta[1]) / float(speedup) else: ts_usec = (ipg_usec * i) / float(speedup) # handle last packet if i == len(pkts): next = 1 if loop else None else: next = i + 1 streams.append( STLStream( name=i, packet=CScapyTRexPktBuilder(pkt_buffer=cap), mode=STLTXSingleBurst(total_pkts=1), self_start=True if (i == 1) else False, isg=(ts_usec - last_ts_usec), # seconds to usec next=next)) last_ts_usec = ts_usec return STLProfile(streams)
def __parse_packet (self, packet_dict): packet_type = set(packet_dict).intersection(['binary', 'pcap']) if len(packet_type) != 1: raise STLError("packet section must contain either 'binary' or 'pcap'") if 'binary' in packet_type: try: pkt_str = base64.b64decode(packet_dict['binary']) except TypeError: raise STLError("'binary' field is not a valid packet format") builder = CScapyTRexPktBuilder(pkt_buffer = pkt_str) elif 'pcap' in packet_type: pcap = os.path.join(self.yaml_path, packet_dict['pcap']) if not os.path.exists(pcap): raise STLError("'pcap' - cannot find '{0}'".format(pcap)) builder = CScapyTRexPktBuilder(pkt = pcap) return builder
def __init__ (self, name = None, packet = None, mode = STLTXCont(pps = 1), enabled = True, self_start = True, isg = 0.0, rx_stats = None, next = None, stream_id = None, action_count = 0, random_seed =0, mac_src_override_by_pkt=None, mac_dst_override_mode=None #see STLStreamDstMAC_xx ): # type checking validate_type('mode', mode, STLTXMode) validate_type('packet', packet, (NoneType, CTrexPktBuilderInterface)) validate_type('enabled', enabled, bool) validate_type('self_start', self_start, bool) validate_type('isg', isg, (int, float)) validate_type('stream_id', stream_id, (NoneType, int)) validate_type('random_seed',random_seed,int); if (type(mode) == STLTXCont) and (next != None): raise STLError("continuous stream cannot have a next stream ID") # tag for the stream and next - can be anything self.name = name self.next = next self.id = stream_id self.fields = {} int_mac_src_override_by_pkt = 0; int_mac_dst_override_mode = 0; if mac_src_override_by_pkt == None: int_mac_src_override_by_pkt=0 if packet : if packet.is_def_src_mac ()==False: int_mac_src_override_by_pkt=1 else: int_mac_src_override_by_pkt = int(mac_src_override_by_pkt); if mac_dst_override_mode == None: int_mac_dst_override_mode = 0; if packet : if packet.is_def_dst_mac ()==False: int_mac_dst_override_mode=STLStreamDstMAC_PKT else: int_mac_dst_override_mode = int(mac_dst_override_mode); self.fields['flags'] = (int_mac_src_override_by_pkt&1) + ((int_mac_dst_override_mode&3)<<1) self.fields['action_count'] = action_count # basic fields self.fields['enabled'] = enabled self.fields['self_start'] = self_start self.fields['isg'] = isg if random_seed !=0 : self.fields['random_seed'] = random_seed # optional # mode self.fields['mode'] = mode.to_json() self.mode_desc = str(mode) # packet self.fields['packet'] = {} self.fields['vm'] = {} if not packet: packet = CScapyTRexPktBuilder(pkt = Ether()/IP()) # packet builder packet.compile() # packet and VM self.fields['packet'] = packet.dump_pkt() self.fields['vm'] = packet.get_vm_data() self.pkt = base64.b64decode(self.fields['packet']['binary']) # this is heavy, calculate lazy self.packet_desc = None if not rx_stats: self.fields['rx_stats'] = STLRxStats.defaults() else: self.fields['rx_stats'] = rx_stats.to_json()
def __init__(self, name=None, packet=None, mode=STLTXCont(1), enabled=True, self_start=True, isg=0.0, rx_stats=None, next=None, stream_id=None): # type checking if not isinstance(mode, STLTXMode): raise STLArgumentError('mode', mode) if packet and not isinstance(packet, CTrexPktBuilderInterface): 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) if (type(mode) == STLTXCont) and (next != None): raise STLError("continuous stream cannot have a next stream ID") # tag for the stream and next - can be anything self.name = name self.next = next # ID self.set_id(stream_id) self.set_next_id(None) self.fields = {} # basic fields self.fields['enabled'] = enabled self.fields['self_start'] = self_start self.fields['isg'] = isg # mode self.fields['mode'] = mode.to_json() self.mode_desc = str(mode) self.fields['packet'] = {} self.fields['vm'] = {} if not packet: packet = CScapyTRexPktBuilder(pkt=Ether() / IP()) # packet builder packet.compile() # packet and VM self.fields['packet'] = packet.dump_pkt() self.fields['vm'] = packet.get_vm_data() self.packet_desc = packet.pkt_layers_desc() if not rx_stats: self.fields['rx_stats'] = {} self.fields['rx_stats']['enabled'] = False else: self.fields['rx_stats'] = rx_stats