summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/doc_stl/api/client_code.rst
blob: 4ae2b9fd95d2aebaf3317eaa0146abe15273fc1c (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
Client Module 
==================

The TRex Client provides access to the TRex server.

**Client and interfaces**

Multiple users can interact with one TRex server. Each user "owns" a different set of interfaces.
The protocol is JSON-RPC2 over ZMQ transport.

In addition to the Python API, a console-based API interface is also available.

Python-like example::
    
   c.start(ports = [0, 1], mult = "5mpps", duration = 10)
   
Console-like example::

  c.start_line (" -f stl/udp_1pkt_simple.py -m 10mpps --port 0 1 ")



Example 1 - Typical Python API::

    c = STLClient(username = "itay",server = "10.0.0.10", verbose_level = LoggerApi.VERBOSE_HIGH)

    try:
        # connect to server
        c.connect()

        # prepare our ports (my machine has 0 <--> 1 with static route)
        c.reset(ports = [0, 1])

        # add both streams to ports
        c.add_streams(s1, ports = [0])

        # clear the stats before injecting
        c.clear_stats()

        c.start(ports = [0, 1], mult = "5mpps", duration = 10)

        # block until done
        c.wait_on_traffic(ports = [0, 1])

    finally:
        c.disconnect()


STLClient class
---------------

.. autoclass:: trex_stl_lib.trex_stl_client.STLClient
    :members: 
    :member-order: bysource
    

STLClient snippet
-----------------


.. code-block:: python
    :caption: Example 1: Minimal example of client interacting with the TRex server

    c = STLClient()

    try:
        # connect to server
        c.connect()

        # prepare our ports (my machine has 0 <--> 1 with static route)
        c.reset(ports = [0, 1])

        # add both streams to ports
        c.add_streams(s1, ports = [0])

        # clear the stats before injecting
        c.clear_stats()

        c.start(ports = [0, 1], mult = "5mpps", duration = 10)

        # block until done
        c.wait_on_traffic(ports = [0, 1])

    finally:
        c.disconnect()


.. code-block:: python
    :caption: Example 2: Client can execute other functions while the TRex server is generating traffic


    c = STLClient()
    try:
        #connect to server
        c.connect()

        #..

        c.start(ports = [0, 1], mult = "5mpps", duration = 10)

        # block until done
        while True  :
                # do somthing else
                os.sleep(1) # sleep for 1 sec 
                # check if the port is still active 
                if c.is_traffic_active(ports = [0, 1])==False
                        break;
                
    finally:
        c.disconnect()



.. code-block:: python
    :caption: Example 3: Console-like API interface


        def simple ():
        
            # create client
            #verbose_level = LoggerApi.VERBOSE_HIGH  # set to see JSON-RPC commands
            c = STLClient(verbose_level = LoggerApi.VERBOSE_REGULAR)
            passed = True
            
            try:
                # connect to server
                c.connect()
        
                my_ports=[0,1]
        
                # prepare our ports
                c.reset(ports = my_ports)
        
                print (" is connected {0}".format(c.is_connected()))
        
                print (" number of ports {0}".format(c.get_port_count()))
                print (" acquired_ports {0}".format(c.get_acquired_ports()))
                # port stats
                print c.get_stats(my_ports)  
        
                # port info, mac-addr info, speed 
                print c.get_port_info(my_ports)
        
                c.ping()
        
                print("start") 
                # start traffic on port 0,1 each 10mpps 
                c.start_line (" -f ../../../../stl/udp_1pkt_simple.py -m 10mpps --port 0 1 ")
                time.sleep(2);
                c.pause_line("--port 0 1");
                time.sleep(2);
                c.resume_line("--port 0 1");
                time.sleep(2);
                c.update_line("--port 0 1 -m 5mpps");   # reduce to 5 mpps
                time.sleep(2);
                c.stop_line("--port 0 1");  # stop both ports
        
            except STLError as e:
                passed = False
                print e
        
            finally:
                c.disconnect()


Example 4: Load profile from a file::

  def simple ():

    # create client
    #verbose_level = LoggerApi.VERBOSE_HIGH
    c = STLClient(verbose_level = LoggerApi.VERBOSE_REGULAR)
    passed = True
    
    try:
        # connect to server
        c.connect()

        my_ports=[0,1]

        # prepare our ports
        c.reset(ports = my_ports)

        profile_file =   "../../../../stl/udp_1pkt_simple.py"   # a traffic profile file 

        try:                                                    # load a profile
            profile = STLProfile.load(profile_file)
        except STLError as e:
            print format_text("\nError while loading profile '{0}'\n".format(profile_file), 'bold')
            print e.brief() + "\n"
            return

        print profile.dump_to_yaml()                            # print it as YAML

        c.remove_all_streams(my_ports)                          # remove all streams


        c.add_streams(profile.get_streams(), ports = my_ports)  # add them 

        c.start(ports = [0, 1], mult = "5mpps", duration = 10)  # start for 10 sec

        # block until done
        c.wait_on_traffic(ports = [0, 1])                       # wait 


    finally:
        c.disconnect()