summaryrefslogtreecommitdiffstats
path: root/scripts/stl/pcap_with_vm.py
blob: 7cf2906b22d6bbc8e81b3c71090bf52f0c2c94e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
from trex_stl_lib.api import *

# PCAP profile
class STLPcap(object):

    def __init__ (self, pcap_file):
        self.pcap_file = pcap_file

    def create_vm (self, ip_src_range, ip_dst_range):
        if not ip_src_range and not ip_dst_range:
            return None

        vm = []

        if ip_src_range:
            vm += [STLVmFlowVar(name="src", min_value = ip_src_range['start'], max_value = ip_src_range['end'], size = 4, op = "inc"),
                   STLVmWrFlowVar(fv_name="src",pkt_offset= "IP.src")
                  ]

        if ip_dst_range:
            vm += [STLVmFlowVar(name="dst", min_value = ip_dst_range['start'], max_value = ip_dst_range['end'], size = 4, op = "inc"),
                   STLVmWrFlowVar(fv_name="dst",pkt_offset = 30)
                   ]

        vm += [STLVmFixIpv4(offset = "IP")
              ]

        return vm


    def get_streams (self,
                     direction = 0,
                     ipg_usec = 10.0,
                     loop_count = 5,
                     ip_src_range = None,
                     ip_dst_range = {'start' : '10.0.0.1', 'end': '10.0.0.254'},
                     **kwargs):

        vm = self.create_vm(ip_src_range, ip_dst_range)

        profile = STLProfile.load_pcap(self.pcap_file,
                                       ipg_usec = ipg_usec,
                                       loop_count = loop_count,
                                       vm = vm)

        return profile.get_streams()



# dynamic load - used for trex console or simulator
def register():
    # get file relative to profile dir
    return STLPcap(os.path.join(os.path.dirname(__file__), 'sample.pcap'))