python 3 support - phase 2
authorimarom <[email protected]>
Mon, 21 Mar 2016 14:20:41 +0000 (16:20 +0200)
committerimarom <[email protected]>
Mon, 21 Mar 2016 14:20:41 +0000 (16:20 +0200)
scripts/automation/trex_control_plane/stl/examples/stl_imix.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/api.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_ext.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_jsonrpc_client.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_packet_builder_scapy.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_sim.py
scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_streams.py

index 9416561..56fd3cf 100644 (file)
@@ -34,7 +34,7 @@ def imix_test (server):
         dir_0 = [x[0] for x in table['bi']]
         dir_1 = [x[1] for x in table['bi']]
 
-        print "Mapped ports to sides {0} <--> {1}".format(dir_0, dir_1)
+        print("Mapped ports to sides {0} <--> {1}".format(dir_0, dir_1))
 
         # load IMIX profile
         profile = STLProfile.load_py('../../../../stl/imix.py')
@@ -50,7 +50,7 @@ def imix_test (server):
         # choose rate and start traffic for 10 seconds on 5 mpps
         duration = 10
         mult = "30%"
-        print "Injecting {0} <--> {1} on total rate of '{2}' for {3} seconds".format(dir_0, dir_1, mult, duration)
+        print("Injecting {0} <--> {1} on total rate of '{2}' for {3} seconds".format(dir_0, dir_1, mult, duration))
 
         c.start(ports = (dir_0 + dir_1), mult = mult, duration = duration, total = True)
         
@@ -75,11 +75,11 @@ def imix_test (server):
         lost_0 = dir_0_opackets - dir_1_ipackets
         lost_1 = dir_1_opackets - dir_0_ipackets
 
-        print "\nPackets injected from {0}: {1:,}".format(dir_0, dir_0_opackets)
-        print "Packets injected from {0}: {1:,}".format(dir_1, dir_1_opackets)
+        print("\nPackets injected from {0}: {1:,}".format(dir_0, dir_0_opackets))
+        print("Packets injected from {0}: {1:,}".format(dir_1, dir_1_opackets))
 
-        print "\npackets lost from {0} --> {1}:   {2:,} pkts".format(dir_0, dir_0, lost_0)
-        print "packets lost from {0} --> {1}:   {2:,} pkts".format(dir_1, dir_1, lost_1)
+        print("\npackets lost from {0} --> {1}:   {2:,} pkts".format(dir_0, dir_0, lost_0))
+        print("packets lost from {0} --> {1}:   {2:,} pkts".format(dir_1, dir_1, lost_1))
 
         if (lost_0 <= 0) and (lost_1 <= 0): # less or equal because we might have incoming arps etc.
             passed = True
@@ -89,16 +89,16 @@ def imix_test (server):
 
     except STLError as e:
         passed = False
-        print e
+        print(e)
 
     finally:
         c.disconnect()
 
     if passed:
-        print "\nTest has passed :-)\n"
+        print("\nTest has passed :-)\n")
         sys.exit(0)
     else:
-        print "\nTest has failed :-(\n"
+        print("\nTest has failed :-(\n")
         sys.exit(-1)
 
 parser = argparse.ArgumentParser(description="Example for TRex Stateless, sending IMIX traffic")
index 082cd1a..bd95a20 100644 (file)
@@ -8,7 +8,7 @@ from .trex_stl_streams import *
 
 # packet builder
 from .trex_stl_packet_builder_scapy import *
-#from scapy.all import *
+from scapy.all import *
 
 
 # simulator
index 3440299..9d76115 100644 (file)
@@ -17,9 +17,9 @@ if not TREX_STL_EXT_PATH:
 # py-dep requires python2/python3 directories
 # arch-dep requires cel59/fedora and 32bit/64bit directories
 CLIENT_UTILS_MODULES = [ {'name': 'dpkt-1.8.6'},
-                         {'name': 'pyyaml-3.11', 'py-dep': True},
                          {'name': 'texttable-0.8.4'},
-                         {'name': 'scapy-2.3.1'},
+                         {'name': 'pyyaml-3.11', 'py-dep': True},
+                         {'name': 'scapy-2.3.1', 'py-dep': True},
                          {'name': 'pyzmq-14.5.0', 'py-dep': True, 'arch-dep': True}
                         ]
 
@@ -52,7 +52,7 @@ def import_module_list(modules_list):
         full_path = generate_module_path(p, is_python3, is_64bit, is_cel)
 
         if not os.path.exists(full_path):
-            print("Unable to find required module library: '{0}'".format(p))
+            print("Unable to find required module library: '{0}'".format(p['name']))
             print("Please provide the correct path using TREX_STL_EXT_PATH variable")
             print("current path used: '{0}'".format(full_path))
             exit(0)
@@ -62,4 +62,5 @@ def import_module_list(modules_list):
 
 
 
+
 import_module_list(CLIENT_UTILS_MODULES)
index 1c78304..166fd64 100644 (file)
@@ -143,16 +143,21 @@ class JsonRpcClient(object):
         if self.logger.check_verbose(self.logger.VERBOSE_HIGH):
             self.verbose_msg("Sending Request To Server:\n\n" + self.pretty_json(msg) + "\n")
 
-        if len(msg) > self.MSG_COMPRESS_THRESHOLD:
-            response = self.send_raw_msg(self.compress_msg(msg))
+        # encode string to buffer
+        buffer = msg.encode()
+
+        if len(buffer) > self.MSG_COMPRESS_THRESHOLD:
+            response = self.send_raw_msg(self.compress_msg(buffer))
             if response:
                 response = self.decompress_msg(response)
         else:
-            response = self.send_raw_msg(msg)
+            response = self.send_raw_msg(buffer)
 
         if not response:
             return response
 
+        # return to string
+        response = response.decode()
 
         # print after
         if self.logger.check_verbose(self.logger.VERBOSE_HIGH):
@@ -177,7 +182,7 @@ class JsonRpcClient(object):
         tries = 0
         while True:
             try:
-                self.socket.send_string(msg)
+                self.socket.send(msg)
                 break
             except zmq.Again:
                 tries += 1
@@ -189,7 +194,7 @@ class JsonRpcClient(object):
         tries = 0
         while True:
             try:
-                response = self.socket.recv_string()
+                response = self.socket.recv()
                 break
             except zmq.Again:
                 tries += 1
index 643b024..f4a056f 100644 (file)
@@ -7,9 +7,10 @@ import yaml
 import binascii
 import base64
 import inspect
+import copy
 
-from trex_stl_packet_builder_interface import CTrexPktBuilderInterface
-from trex_stl_types import *
+from .trex_stl_packet_builder_interface import CTrexPktBuilderInterface
+from .trex_stl_types import *
 from scapy.all import *
 
 class CTRexPacketBuildException(Exception):
@@ -28,22 +29,30 @@ class CTRexPacketBuildException(Exception):
 
 ################################################################################################
 
+def safe_ord (c):
+    if type(c) is str:
+        return ord(c)
+    elif type(c) is int:
+        return c
+    else:
+        raise TypeError("cannot convert: {0} of type: {1}".format(c, type(c)))
+
 def _buffer_to_num(str_buffer):
-    validate_type('str_buffer', str_buffer, str)
+    validate_type('str_buffer', str_buffer, bytes)
     res=0
     for i in str_buffer:
         res = res << 8
-        res += ord(i)
+        res += safe_ord(i)
     return res
 
 
 def ipv4_str_to_num (ipv4_buffer):
-    validate_type('ipv4_buffer', ipv4_buffer, str)
+    validate_type('ipv4_buffer', ipv4_buffer, bytes)
     assert len(ipv4_buffer)==4, 'size of ipv4_buffer is not 4'
     return _buffer_to_num(ipv4_buffer)
 
 def mac_str_to_num (mac_buffer):
-    validate_type('mac_buffer', mac_buffer, str)
+    validate_type('mac_buffer', mac_buffer, bytes)
     assert len(mac_buffer)==6, 'size of mac_buffer is not 6'
     return _buffer_to_num(mac_buffer)
 
@@ -52,10 +61,10 @@ def is_valid_ipv4(ip_addr):
     """
     return buffer in network order
     """
-    if  type(ip_addr)==str and len(ip_addr) == 4:
+    if  type(ip_addr) == bytes and len(ip_addr) == 4:
         return ip_addr
 
-    if  type(ip_addr)==int :
+    if  type(ip_addr)== int:
         ip_addr = socket.inet_ntoa(struct.pack("!I", ip_addr))
 
     try:
@@ -70,7 +79,7 @@ def is_valid_ipv6(ipv6_addr):
     """
     return buffer in network order
     """
-    if type(ipv6_addr)==str and len(ipv6_addr) == 16:
+    if type(ipv6_addr) == bytes and len(ipv6_addr) == 16:
         return ipv6_addr
     try:
         return socket.inet_pton(socket.AF_INET6, ipv6_addr)
@@ -347,15 +356,15 @@ class CTRexVmEngine(object):
        def dump (self):
            cnt=0;
            for obj in self.ins:
-               print "ins",cnt
+               print("ins",cnt)
                cnt = cnt +1
-               print obj.__dict__
+               print(obj.__dict__)
 
        def dump_bjson (self):
-          print json.dumps(self.get_json(), sort_keys=True, indent=4)
+          print(json.dumps(self.get_json(), sort_keys=True, indent=4))
 
        def dump_as_yaml (self):
-          print yaml.dump(self.get_json(), default_flow_style=False)
+          print(yaml.dump(self.get_json(), default_flow_style=False))
 
 
 
@@ -517,10 +526,10 @@ class CTRexVmDescBase(object):
         return self.get_obj().__dict__
 
     def dump_bjson(self):
-       print json.dumps(self.get_json(), sort_keys=True, indent=4)
+       print(json.dumps(self.get_json(), sort_keys=True, indent=4))
 
     def dump_as_yaml(self):
-       print yaml.dump(self.get_json(), default_flow_style=False)
+       print(yaml.dump(self.get_json(), default_flow_style=False))
 
 
     def get_var_ref (self):
@@ -1185,7 +1194,7 @@ class STLPktBuilder(CTrexPktBuilderInterface):
 
 
     def dump_vm_data_as_yaml(self):
-        print yaml.dump(self.get_vm_data(), default_flow_style=False)
+        print(yaml.dump(self.get_vm_data(), default_flow_style=False))
 
     def get_vm_data(self):
         """
@@ -1224,7 +1233,7 @@ class STLPktBuilder(CTrexPktBuilderInterface):
         """
         pkt_buf = self._get_pkt_as_str()
 
-        return {'binary': base64.b64encode(pkt_buf) if encode else pkt_buf,
+        return {'binary': base64.b64encode(pkt_buf).decode() if encode else pkt_buf,
                 'meta': self.metadata}
 
 
@@ -1239,7 +1248,7 @@ class STLPktBuilder(CTrexPktBuilderInterface):
 
     def dump_as_hex (self):
         pkt_buf = self._get_pkt_as_str()
-        print hexdump(pkt_buf)
+        print(hexdump(pkt_buf))
 
     def pkt_layers_desc (self):
         """
@@ -1389,7 +1398,7 @@ class STLPktBuilder(CTrexPktBuilderInterface):
 
             if var_names :
                 for var_name in var_names:
-                    if vars.has_key(var_name):
+                    if var_name in vars:
                         raise CTRexPacketBuildException(-11,("variable %s define twice ") % (var_name)  );
                     else:
                         vars[var_name]=1
@@ -1398,7 +1407,7 @@ class STLPktBuilder(CTrexPktBuilderInterface):
         for desc in obj.commands:
             var_name =  desc.get_var_ref()
             if var_name :
-                if not vars.has_key(var_name):
+                if not var_name in vars:
                     raise CTRexPacketBuildException(-11,("variable %s does not exists  ") % (var_name) );
             desc.compile(self);
 
@@ -1451,10 +1460,13 @@ class STLPktBuilder(CTrexPktBuilderInterface):
         return p_utl.get_field_offet_by_str(field_name)
 
     def _get_pkt_as_str(self):
+
         if self.pkt:
-            return str(self.pkt)
+            return bytes(self.pkt)
+
         if self.pkt_raw:
             return self.pkt_raw
+
         raise CTRexPacketBuildException(-11, 'empty packet');
 
     def _add_tuple_gen(self,tuple_gen):
index eb1ffff..75e15ca 100644 (file)
@@ -31,7 +31,7 @@ import argparse
 import tempfile
 import subprocess
 import os
-#from dpkt import pcap
+from dpkt import pcap
 from operator import itemgetter
 
 class BpSimException(Exception):
index 4d0444b..c258c74 100644 (file)
@@ -6,7 +6,7 @@ from .trex_stl_packet_builder_interface import CTrexPktBuilderInterface
 from .trex_stl_packet_builder_scapy import STLPktBuilder, Ether, IP, UDP, TCP, RawPcapReader
 from collections import OrderedDict, namedtuple
 
-#from scapy.utils import ltoa
+from scapy.utils import ltoa
 import random
 import yaml
 import base64