summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/examples/using_rpc_proxy.py
blob: d2fcdff3f8a61b0438c4743e6515b614ba105eb8 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/router/bin/python

import argparse
import sys
import os
from time import sleep
from pprint import pprint

# ext libs
ext_libs = os.path.join(os.pardir, os.pardir, os.pardir, os.pardir, 'external_libs')
sys.path.append(os.path.join(ext_libs, 'jsonrpclib-pelix-0.2.5'))
import jsonrpclib

def fail(msg):
    print(msg)
    sys.exit(1)

def verify(res):
    if not res[0]:
        fail(res[1])
    return res

def verify_hlt(res):
    if res['status'] == 0:
        fail(res['log'])
    return res

### Main ###

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = 'Use of Stateless through rpc_proxy. (Can be implemented in any language)')
    parser.add_argument('-s', '--server', type=str, default = 'localhost', dest='server', action = 'store',
                        help = 'Address of rpc proxy.')
    parser.add_argument('-p', '--port', type=int, default = 8095, dest='port', action = 'store',
                        help = 'Port of rpc proxy.\nDefault is 8095.')
    parser.add_argument('--master_port', type=int, default = 8091, dest='master_port', action = 'store',
                        help = 'Port of Master daemon.\nDefault is 8091.')
    args = parser.parse_args()

    server = jsonrpclib.Server('http://%s:%s' % (args.server, args.port))
    master = jsonrpclib.Server('http://%s:%s' % (args.server, args.master_port))

# Connecting

    try:
        print('Connecting to STL RPC proxy server')
        server.check_connectivity()
        print('Connected')
    except Exception as e:
        print('Could not connect to STL RPC proxy server: %s\nTrying to start it from Master daemon.' % e)
        try:
            master.check_connectivity()
            master.start_stl_rpc_proxy()
            print('Started')
        except Exception as e:
            print('Could not start it from Master daemon. Error: %s' % e)
            sys.exit(-1)


# Native API

    print('Initializing Native Client')
    verify(server.native_proxy_init(server = args.server, force = True))

    print('Connecting to TRex server')
    verify(server.connect())

    print('Resetting all ports')
    verify(server.reset())

    print('Getting ports info')
    res = verify(server.native_method(func_name = 'get_port_info'))
    print('Ports info is: %s' % res[1])
    ports = [port['index'] for port in res[1]]

    print('Sending pcap to ports %s' % ports)
    verify(server.push_remote(pcap_filename = 'stl/sample.pcap'))
    sleep(3)

    print('Getting stats')
    res = verify(server.get_stats())
    pprint(res[1])

    print('Resetting all ports')
    verify(server.reset())

    imix_path_1 = '../../../../stl/imix.py'
    imix_path_2 = '../../stl/imix.py'
    if os.path.exists(imix_path_1):
        imix_path = imix_path_1
    elif os.path.exists(imix_path_2):
        imix_path = imix_path_2
    else:
        print('Could not find path of imix profile, skipping')
        imix_path = None

    if imix_path:
        print('Adding profile %s' % imix_path)
        verify(server.native_method(func_name = 'add_profile', filename = imix_path))

        print('Start traffic for 5 sec')
        verify(server.native_method('start'))
        sleep(5)

        print('Getting stats')
        res = verify(server.get_stats())
        pprint(res[1])

        print('Resetting all ports')
        verify(server.reset())

    print('Deleting Native Client instance')
    verify(server.native_proxy_del())

# HLTAPI

    print('Initializing HLTAPI Client')
    verify_hlt(server.hltapi_proxy_init(force = True))
    print('HLTAPI Client initiated')

    print('HLTAPI connect')
    verify_hlt(server.hlt_connect(device = args.server, port_list = ports, reset = True, break_locks = True))

    print('Creating traffic')
    verify_hlt(server.traffic_config(
            mode = 'create', bidirectional = True,
            port_handle = ports[0], port_handle2 = ports[1],
            frame_size = 100,
            l3_protocol = 'ipv4',
            ip_src_addr = '10.0.0.1', ip_src_mode = 'increment', ip_src_count = 254,
            ip_dst_addr = '8.0.0.1', ip_dst_mode = 'increment', ip_dst_count = 254,
            l4_protocol = 'udp',
            udp_dst_port = 12, udp_src_port = 1025,
            rate_percent = 10, ignore_macs = True,
            ))

    print('Starting traffic for 5 sec')
    verify_hlt(server.traffic_control(action = 'run', port_handle = ports[:2]))

    sleep(5)
    print('Stopping traffic')
    verify_hlt(server.traffic_control(action = 'stop', port_handle = ports[:2]))

    print('Getting stats')
    res = verify_hlt(server.traffic_stats(mode = 'aggregate', port_handle = ports[:2]))
    pprint(res)

    print('Deleting HLTAPI Client instance')
    verify_hlt(server.hltapi_proxy_del())