stl_functional.py revision bcea0262
1import stl_path
2from trex_stl_lib.api import *
3
4"""
5An example on how to use TRex for functional tests
6
7It can be used for various tasks and can replace simple Pagent/Scapy
8low rate tests
9"""
10
11def test_dot1q (c, rx_port, tx_port):
12
13    # activate service mode on RX code
14    c.set_service_mode(ports = rx_port)
15
16    # generate a simple Dot1Q
17    pkt = Ether() / Dot1Q(vlan = 100) / IP()
18
19    # start a capture
20    capture = c.start_capture(rx_ports = rx_port)
21
22    # push the Dot1Q packet to TX port... we need 'force' because this is under service mode
23    print('\nSending 1 Dot1Q packet(s) on port {}'.format(tx_port))
24
25    c.push_packets(ports = tx_port, pkts = pkt, force = True)
26    c.wait_on_traffic(ports = tx_port)
27
28    rx_pkts = []
29    c.stop_capture(capture_id = capture['id'], output = rx_pkts)
30
31    print('\nRecived {} packets on port {}:\n'.format(len(rx_pkts), rx_port))
32
33    c.set_service_mode(ports = rx_port, enabled = False)
34
35    # got back one packet
36    assert(len(rx_pkts) == 1)
37    rx_scapy_pkt = Ether(rx_pkts[0]['binary'])
38
39    # it's a Dot1Q with the same VLAN
40    assert('Dot1Q' in rx_scapy_pkt)
41    assert(rx_scapy_pkt.vlan == 100)
42
43
44    rx_scapy_pkt.show2()
45
46
47
48def main ():
49
50    # create a client
51    c = STLClient()
52
53    try:
54        # connect to the server
55        c.connect()
56
57        # there should be at least two ports connected
58        tx_port, rx_port = stl_map_ports(c)['bi'][0]
59        c.reset(ports = [tx_port, rx_port])
60
61        # call the test
62        test_dot1q(c, tx_port, rx_port)
63
64
65    except STLError as e:
66        print(e)
67
68    finally:
69        c.stop()
70        c.remove_all_captures()
71        c.set_service_mode(enabled = False)
72        c.disconnect()
73
74
75
76if __name__ == '__main__':
77    main()
78
79