[dts] [next][PATCH V1 4/14] framework/pktgen: utils methods

yufengmx yufengx.mo at intel.com
Sun Apr 28 04:49:11 CEST 2019


new utils methods used by pktgen

Set convert_int2ip/convert_ip2int input parameter with a default ipv4 value.
Add convert_mac2long/convert_mac2str methods to deal with mac convert(str <--> int).

Signed-off-by: yufengmx <yufengx.mo at intel.com>
---
 framework/utils.py | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/framework/utils.py b/framework/utils.py
index ce97d5f..8f5a233 100644
--- a/framework/utils.py
+++ b/framework/utils.py
@@ -214,7 +214,11 @@ def create_mask(indexes):
 
     return hex(val).rstrip("L")
 
-def convert_int2ip(value, ip_type):
+def convert_int2ip(value, ip_type=4):
+    '''
+    @change:
+    2019.0403 set default value
+    '''
     if ip_type == 4:
         ip_str = socket.inet_ntop(socket.AF_INET, struct.pack('!I', value))
     else:
@@ -224,7 +228,11 @@ def convert_int2ip(value, ip_type):
 
     return ip_str
 
-def convert_ip2int(ip_str, ip_type):
+def convert_ip2int(ip_str, ip_type=4):
+    '''
+    @change:
+    2019.0403 set default value
+    '''
     if ip_type == 4:
         ip_val = struct.unpack("!I", socket.inet_aton(ip_str))[0]
     else:
@@ -234,6 +242,26 @@ def convert_ip2int(ip_str, ip_type):
 
     return ip_val
 
+def convert_mac2long(mac_str):
+    """
+    convert the MAC type from the string into the int.
+    """
+    mac_hex = '0x'
+    for mac_part in mac_str.lower().split(':'):
+        mac_hex += mac_part
+    ret  = long(mac_hex, 16)
+    return ret
+
+def convert_mac2str(mac_long):
+    """
+    convert the MAC type from the int into the string.
+    """
+    mac = hex(mac_long)[2:-1].zfill(12)
+    b = []
+    [b.append(mac[n:n+2]) for n in range(len(mac)) if n % 2 == 0 ]
+    new_mac = ":".join(b)
+    return new_mac
+
 def get_backtrace_object(file_name, obj_name):
     import inspect
     frame = inspect.currentframe()
-- 
1.9.3



More information about the dts mailing list