Usage Examples

End-to-End cycle

This example (pkt_generation_for_trex.py) demonstrates a full cycle of using the API.

Note

this module uses the Scapy in order to generate packets to be used as a basis of the traffic injection. It is recommended to install this module to best experience the example.

The demo takes the user a full circle:
  1. Generating packets (using Scapy)

  2. exporting the generated packets into .pcap file named dns_traffic.pcap.

  3. Use the trex_yaml_gen.CTRexYaml generator to include that pcap file in the yaml object.

  4. Export the YAML object onto a YAML file named dns_traffic.yaml

  5. Push the generated files to TRex server.

  6. Run TRex based on the generated (and pushed) files.

Code Excerpt 1

 1
 2
 3import logging
 4import time
 5import trex_root_path
 6from client.trex_client import *
 7from client_utils.general_utils import *
 8from client_utils.trex_yaml_gen import *
 9from pprint import pprint
10from argparse import ArgumentParser
11
12# import scapy package
13logging.getLogger("scapy.runtime").setLevel(logging.ERROR)  # supress scapy import warnings from being displayed
14from scapy.all import *
15
16
17def generate_dns_packets (src_ip, dst_ip):
18    dns_rqst = Ether(src='00:15:17:a7:75:a3', dst='e0:5f:b9:69:e9:22')/IP(src=src_ip,dst=dst_ip,version=4L)/UDP(dport=53, sport=1030)/DNS(rd=1,qd=DNSQR(qname="www.cisco.com"))
19    dns_resp = Ether(src='e0:5f:b9:69:e9:22', dst='00:15:17:a7:75:a3')/IP(src=dst_ip,dst=src_ip,version=4L)/UDP(dport=1030, sport=53)/DNS(aa=1L, qr=1L, an=DNSRR(rclass=1, rrname='www.cisco.com.', rdata='100.100.100.100', type=1), ad=0L, qdcount=1, ns=None, tc=0L, rd=0L, ar=None, opcode=0L, ra=1L, cd=0L, z=0L, rcode=0L, qd=DNSQR(qclass=1, qtype=1, qname='www.cisco.com.'))
20    return [dns_rqst, dns_resp]
21
22def pkts_to_pcap (pcap_filename, packets):
23    wrpcap(pcap_filename, packets)
24
25
26def main (args):
27    # instantiate TRex client
28    trex = CTRexClient('trex-dan', verbose = args.verbose)
29
30    if args.steps:
31        print "\nNext step: .pcap generation."
32        raw_input("Press Enter to continue...")
33    # generate TRex traffic.
34    pkts = generate_dns_packets('21.0.0.2', '22.0.0.12')    # In this case - DNS traffic (request-response)
35    print "\ngenerated traffic:"
36    print "=================="
37    map(lambda x: pprint(x.summary()) , pkts)
38    pkts_to_pcap("dns_traffic.pcap", pkts)              # Export the generated to a .pcap file
39
40    if args.steps:
41        print "\nNext step: .yaml generation."
42        raw_input("Press Enter to continue...")
43    # Generate .yaml file that uses the generated .pcap file
44    trex_files_path = trex.get_trex_files_path()        # fetch the path in which packets are saved on TRex server
45    yaml_obj        = CTRexYaml(trex_files_path)        # instantiate CTRexYaml obj
46
47    # set .yaml file parameters according to need and use
48    ret_idx = yaml_obj.add_pcap_file("dns_traffic.pcap")
49    yaml_obj.set_cap_info_param('cps', 1.1, ret_idx)
50
51    # export yaml_ob to .yaml file
52    yaml_file_path = trex_files_path + 'dns_traffic.yaml'
53    yaml_obj.to_yaml('dns_traffic.yaml')
54    print "\ngenerated .yaml file:"
55    print "===================="
56    yaml_obj.dump()
57
58    if args.steps:
59        print "\nNext step: run TRex with provided files."
60        raw_input("Press Enter to continue...")
61    # push all relevant files to server
62    trex.push_files( yaml_obj.get_file_list() )
63
64    print "\nStarting TRex..."
65    trex.start_trex(c = 2,
66        m = 1.5,
67        nc = True,
68        p = True,
69        d = 30,
70        f = yaml_file_path,             # <-- we use out generated .yaml file here
71        l = 1000)
72    
73    if args.verbose:
74        print "TRex state changed to 'Running'."
75        print "Sampling TRex in 0.2 samples/sec (single sample every 5 secs)"
76    
77    last_res = dict()
78    while trex.is_running(dump_out = last_res):
79        print "CURRENT RESULT OBJECT:"
80        obj = trex.get_result_obj()
81        print obj
82        time.sleep(5)
83
84
85if __name__ == "__main__":
86    parser = ArgumentParser(description = 'Run TRex client API end-to-end example.',
87        usage = """pkt_generation_for_trex [options]""" )
88
89    parser.add_argument("-s", "--step-by-step", dest="steps",
90        action="store_false", help="Switch OFF step-by-step script overview. Default is: ON.", 
91        default = True )
92    parser.add_argument("--verbose", dest="verbose",
93        action="store_true", help="Switch ON verbose option at TRex client. Default is: OFF.",
94        default = False )
95    args = parser.parse_args()
96    main(args)

Footnotes

1

The marked codelines corresponds with the steps mentioned above.