Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def generate_loaded_streams_sum(self):
        if self.state == self.STATE_DOWN:
            return {}

        data = {}
        for id, obj in self.streams.iteritems():

            # lazy build scapy repr.
            if not 'pkt_type' in obj:
                obj['pkt_type'] = CScapyTRexPktBuilder.pkt_layers_desc_from_buffer(obj['pkt'])
            
            data[id] = OrderedDict([ ('id',  id),
                                     ('packet_type',  obj['pkt_type']),
                                     ('L2 len',       len(obj['pkt']) + 4),
                                     ('mode',         obj['mode']),
                                     ('rate',         obj['rate']),
                                     ('next_stream',  obj['next_id'])
                                    ])
    
        return {"streams" : OrderedDict(sorted(data.items())) }
Ejemplo n.º 4
0
    def get_pkt_type (self):
        if self.packet_desc == None:
            self.packet_desc = CScapyTRexPktBuilder.pkt_layers_desc_from_buffer(self.get_pkt())

        return self.packet_desc
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
    def get_pkt_type (self):
        """ Get packet description for example IP:UDP """
        if self.packet_desc == None:
            self.packet_desc = CScapyTRexPktBuilder.pkt_layers_desc_from_buffer(self.get_pkt())

        return self.packet_desc
Ejemplo n.º 7
0
    def __init__ (self,
                  name = None,
                  packet = None,
                  mode = STLTXCont(pps = 1),
                  enabled = True,
                  self_start = True,
                  isg = 0.0,
                  flow_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
                  ):
        """ 
        Stream object 

        :parameters:

                  name  : string 
                       The name of the stream. Needed if this stream is dependent on another stream and another stream need to refer to this stream by its name.

                  packet :  STLPktBuilder 
                       The template packet and field engine program e.g. packet = STLPktBuilder(pkt = base_pkt/pad) 

                  mode : STLTXCont or STLTXSingleBurst or STLTXMultiBurst

                  enabled : bool 
                      if the stream is enabled. 

                  self_start : bool 
                      In case it is False another stream will activate it

                  isg : float 
                     Inter stream gap in usec. time to wait until stream will send the first packet  

                  flow_stats : STLFlowStats
                      Per stream statistic object see STLFlowStats

                  next : string 
                      The name of the stream to activate 
                    
                  stream_id :
                        for HLTAPI usage 

                  action_count : uint16_t
                        In case there is a next stream how many loops until stopping. Default is zero, which mean unlimited

                  random_seed: uint16_t 
                       If given the seed for this stream will be this value. Good in case you need a deterministic random value 
                        
                  mac_src_override_by_pkt : bool 
                        Template packet will set src MAC 

                  mac_dst_override_mode=None : STLStreamDstMAC_xx
                        Template packet will set dst MAC 


        :return:
          None

        :raises:
          None

        """


        # 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.mac_src_override_by_pkt = mac_src_override_by_pkt # save for easy construct code from stream object
        self.mac_dst_override_mode = mac_dst_override_mode
        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_default_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_default_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())

        self.scapy_pkt_builder = packet
        # 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 flow_stats:
            self.fields['flow_stats'] = STLFlowStats.defaults()
        else:
            self.fields['flow_stats'] = flow_stats.to_json()
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    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