summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/client/trex_hltapi.py
blob: 848d5a9e86a5ddbc0016f2218f7a88d1b36ef0ba (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/router/bin/python

import trex_root_path
from client_utils.packet_builder import CTRexPktBuilder
from trex_stateless_client import CTRexStatelessClient
from common.trex_streams import *
from client_utils.general_utils import id_count_gen
import dpkt


class CTRexHltApi(object):

    def __init__(self):
        self.trex_client = None
        self.connected = False
        # self._stream_db = CStreamList()
        self._port_data = {}

    # ----- session functions ----- #
    # sync = RPC, async = ZMQ
    def connect(self, device, port_list, username, sync_port = 4501, async_port = 4500, reset=False, break_locks=False):
        ret_dict = {"status": 0}
        self.trex_client = CTRexStatelessClient(username, device, sync_port, async_port)

        rc = self.trex_client.connect()
        if rc.bad():

            self.trex_client = None
            ret_dict.update({"log": rc.err()})
            return ret_dict
        # arrived here, connection successfully created with server
        # next, try acquiring ports of TRex
        port_list = self.parse_port_list(port_list)
        response = self.trex_client.acquire(port_list, force=break_locks)
        res_ok, log = CTRexHltApi.process_response(port_list, response)
        if not res_ok:
            self.trex_client.disconnect()
            self.trex_client = None
            ret_dict.update({"log": log})
            # TODO: should revert taken ports?!
            return ret_dict
        # arrived here, all desired ports were successfully acquired
        print log
        if reset:
            # remove all port traffic configuration from TRex
            response = self.trex_client.remove_all_streams(port_list)
            res_ok, log = CTRexHltApi.process_response(port_list, response)
            if not res_ok:
                self.trex_client.disconnect()
                self.trex_client = None
                ret_dict.update({"log": log})
                return ret_dict
        print log
        port_handle = {device: {port: port               # since only supporting single TRex at the moment, 1:1 map
                                for port in port_list}
                       }
        ret_dict.update({"status": 1,
                         "log": None,
                         "port_handle": port_handle,
                         "offline": 0})  # ports are online
        self.connected = True
        self._port_data_init(port_list)
        return ret_dict

    def cleanup_session(self, port_list, maintain_lock=False):
        ret_dict = {"status": 0}
        if not maintain_lock:
            # release taken ports
            if port_list == "all":
                port_list = self.trex_client.get_acquired_ports()
            else:
                port_list = self.parse_port_list(port_list)
            response = self.trex_client.release(port_list)
            res_ok, log = CTRexHltApi.process_response(port_list, response)
            if not res_ok:
                ret_dict.update({"log": log})
                return ret_dict
        ret_dict.update({"status": 1,
                         "log": None})
        self.trex_client.disconnect()
        self.trex_client = None
        self.connected = False
        return ret_dict

    def interface_config(self, port_handle, mode="config"):
        ALLOWED_MODES = ["config", "modify", "destroy"]
        if mode not in ALLOWED_MODES:
            raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES))
        # pass this function for now...
        return {"status": 1, "log": None}

    # ----- traffic functions ----- #
    def traffic_config(self, mode, port_list,
                       l2_encap="ethernet_ii", mac_src="00:00:01:00:00:01", mac_dst="00:00:00:00:00:00",
                       l3_protocol="ipv4", ip_src_addr="0.0.0.0", ip_dst_addr="192.0.0.1", l3_length=110,
                       transmit_mode="continuous", rate_pps=100,
                       **kwargs):
        if type(port_list) is not list():
            port_list = [port_list]
        ALLOWED_MODES = ["create", "modify", "remove", "enable", "disable", "reset"]
        if mode not in ALLOWED_MODES:
            raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES))
        if mode == "create":
            # create a new stream with desired attributes, starting by creating packet
            try:
                packet = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst,
                                                     l3_protocol, ip_src_addr, ip_dst_addr, l3_length)
                # set transmission attributes
                tx_mode = CTxMode(transmit_mode, rate_pps, **kwargs)
                # set rx_stats
                rx_stats = CRxStats()   # defaults with disabled
                # join the generated data into stream
                stream_obj = CStream()
                stream_obj_params = {"enabled": True,
                                     "self_start": True,
                                     "next_stream_id": -1,
                                     "isg": 0.0,
                                     "mode": tx_mode,
                                     "rx_stats": rx_stats,
                                     "packet": packet}  # vm is excluded from this list since CTRexPktBuilder obj is passed
                stream_obj.load_data(**stream_obj_params)
            except Exception as e:
                # some exception happened during the stream creation
                return {"status": 0, "log": str(e)}
            # try adding the stream per port, until free stream_id is found
            for port_id in port_list:
                port_data = self._port_data.get(port_id)
                id_candidate = None
                # TODO: change this to better implementation
                while True:
                    id_candidate = port_data["stream_id_gen"].next()
                    response = self.trex_client.add_stream(stream_id=id_candidate,
                                                            stream_obj=stream_obj.dump(),
                                                            port_id_list=port_id)
                    res_ok, log = CTRexHltApi.process_response(port_id, response)
                    if res_ok:
                        # found non-taken stream_id on server
                        # save it for modifying needs
                        port_data["streams"].update({id_candidate: stream_obj})
                        break
                    else:
                        print log
                        # proceed to another iteration to use another id
                        print 'need another iteration?'
                        continue
                return {"status": 1,
                        "stream_id": id_candidate,
                        "log": None}

        else:
            raise NotImplementedError("mode '{0}' is not supported yet on TRex".format(mode))

    def traffic_control(self, action, port_handle, **kwargs):
        ALLOWED_ACTIONS = ["clear_stats", "run", "stop", "sync_run"]
        if action not in ALLOWED_ACTIONS:
            raise ValueError("action must be one of the following values: {actions}".format(actions=ALLOWED_ACTIONS))
        # ret_dict = {"status": 0, "stopped": 1}
        port_list = self.parse_port_list(port_handle)
        if type(port_list) is not list():
            port_list = [port_list]
        if action == "run":
            if not set(kwargs.keys()) >= set(['mul', 'duration']):
                raise ValueError("For 'run' action should be specified mul and duration arguments")
            response = self.trex_client.start_traffic(kwargs['mul'], kwargs['duration'], port_id_list=port_list)
            res_ok, log = CTRexHltApi.process_response(port_list, response)
            if res_ok:
                return {"status": 1,
                        "stopped": 0,
                        "log": None}
            else:
                print log
        elif action == "stop":
            response = self.trex_client.stop_traffic(port_id_list=port_list)
            res_ok, log = CTRexHltApi.process_response(port_list, response)
            if res_ok:
                return {"status": 1,
                        "stopped": 1,
                        "log": None}
        else:
            raise NotImplementedError("action '{0}' is not supported yet on TRex".format(action))

        # if we arrived here, this means that operation FAILED!
        return {"status": 0,
                "log": log}


    def traffic_stats(self, port_handle, mode):
        ALLOWED_MODES = ["aggregate", "streams", "all"]
        if mode not in ALLOWED_MODES:
            raise ValueError("mode must be one of the following values: {modes}".format(modes=ALLOWED_MODES))
        # pass this function for now...
        if mode == "aggregate":
            # create a new stream with desired attributes, starting by creating packet
            try:
                packet = CTRexHltApi.generate_stream(l2_encap, mac_src, mac_dst,
                                                     l3_protocol, ip_src_addr, ip_dst_addr, l3_length)
                # set transmission attributes
                tx_mode = CTxMode(transmit_mode, rate_pps, **kwargs)
                # set rx_stats
                rx_stats = CRxStats()   # defaults with disabled
                # join the generated data into stream
                stream_obj = CStream()
                stream_obj_params = {"enabled": True,
                                     "self_start": True,
                                     "next_stream_id": -1,
                                     "isg": 0.0,
                                     "mode": tx_mode,
                                     "rx_stats": rx_stats,
                                     "packet": packet}  # vm is excluded from this list since CTRexPktBuilder obj is passed
                stream_obj.load_data(**stream_obj_params)
            except Exception as e:
                # some exception happened during the stream creation
                return {"status": 0, "log": str(e)}
            # try adding the stream, until free stream_id is found
            port_data = self._port_data.get(port_handle)
            id_candidate = None
            # TODO: change this to better implementation
            while True:
                 id_candidate = port_data["stream_id_gen"].next()
                 response = self.trex_client.add_stream(stream_id=id_candidate,
                                                        stream_obj=stream_obj,
                                                        port_id=port_handle)
                 res_ok, log = CTRexHltApi.process_response(port_handle, response)
                 if res_ok:
                     # found non-taken stream_id on server
                     # save it for modifying needs
                     port_data["streams"].update({id_candidate: stream_obj})
                     break
                 else:
                     # proceed to another iteration to use another id
                     continue
            return {"status": 1,
                    "stream_id": id_candidate,
                    "log": None}
        else:
            raise NotImplementedError("mode '{0}' is not supported yet on TRex".format(mode))

    def get_aggregate_port_stats(self, port_handle):
        return self.traffic_stats(port_handle, mode="aggregate")

    def get_stream_stats(self, port_handle):
        return self.traffic_stats(port_handle, mode="streams")

    # ----- internal functions ----- #
    def _port_data_init(self, port_list):
        for port in port_list:
            self._port_data[port] = {"stream_id_gen": id_count_gen(),
                                     "streams": {}}

    @staticmethod
    def process_response(port_list, response):
        log = response.data() if response.good() else response.err()
        if isinstance(port_list, list):
            log = CTRexHltApi.join_batch_response(log)
        return response.good(), log

    @staticmethod
    def parse_port_list(port_list):
        if isinstance(port_list, str):
            return [int(port)
                    for port in port_list.split()]
        elif isinstance(port_list, list):
            return [int(port)
                    for port in port_list]
        else:
            return port_list

    @staticmethod
    def join_batch_response(responses):
        if type(responses) is list():
            return "\n". join([str(response) for response in responses])
        return responses

    @staticmethod
    def generate_stream(l2_encap, mac_src, mac_dst,
                       l3_protocol, ip_src_addr, ip_dst_addr, l3_length):
        ALLOWED_L3_PROTOCOL = {"ipv4": dpkt.ethernet.ETH_TYPE_IP,
                               "ipv6": dpkt.ethernet.ETH_TYPE_IP6,
                               "arp": dpkt.ethernet.ETH_TYPE_ARP}
        ALLOWED_L4_PROTOCOL = {"tcp": dpkt.ip.IP_PROTO_TCP,
                               "udp": dpkt.ip.IP_PROTO_UDP,
                               "icmp": dpkt.ip.IP_PROTO_ICMP,
                               "icmpv6": dpkt.ip.IP_PROTO_ICMP6,
                               "igmp": dpkt.ip.IP_PROTO_IGMP,
                               "rtp": dpkt.ip.IP_PROTO_IRTP,
                               "isis": dpkt.ip.IP_PROTO_ISIS,
                               "ospf": dpkt.ip.IP_PROTO_OSPF}

        pkt_bld = CTRexPktBuilder()
        if l2_encap == "ethernet_ii":
            pkt_bld.add_pkt_layer("l2", dpkt.ethernet.Ethernet())
            # set Ethernet layer attributes
            pkt_bld.set_eth_layer_addr("l2", "src", mac_src)
            pkt_bld.set_eth_layer_addr("l2", "dst", mac_dst)
        else:
            raise NotImplementedError("l2_encap does not support the desired encapsulation '{0}'".format(l2_encap))
        # set l3 on l2
        if l3_protocol not in ALLOWED_L3_PROTOCOL:
            raise ValueError("l3_protocol must be one of the following: {0}".format(ALLOWED_L3_PROTOCOL))
        pkt_bld.set_layer_attr("l2", "type", ALLOWED_L3_PROTOCOL[l3_protocol])

        # set l3 attributes
        if l3_protocol == "ipv4":
            pkt_bld.add_pkt_layer("l3", dpkt.ip.IP())
            pkt_bld.set_ip_layer_addr("l3", "src", ip_src_addr)
            pkt_bld.set_ip_layer_addr("l3", "dst", ip_dst_addr)
            pkt_bld.set_layer_attr("l3", "len", l3_length)
        else:
            raise NotImplementedError("l3_protocol '{0}' is not supported by TRex yet.".format(l3_protocol))

        pkt_bld.dump_pkt_to_pcap("stream_test.pcap")
        return pkt_bld



if __name__ == "__main__":
    pass